todotxt / todo.txt-cli

☑️ A simple and extensible shell script for managing your todo.txt file.
http://todotxt.org
GNU General Public License v3.0
5.56k stars 713 forks source link

Autocomplete wrapper function translation to ZSH #256

Closed pabgan closed 5 months ago

pabgan commented 6 years ago

Do you want to request a feature or report a bug? I want to request help, or feature in documentation, as you wish. Mailing list links are dead.

What is the current behavior? I cannot make autocompletion to work in ZSH with two different configurations.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. In my .zshrc I have:

alias t="todo.sh -a -d $TODO_TXT/personal-todo.cfg"
_t()
{
    local _todo_sh='todo.sh -d "$TODO_TXT/personal-todo.cfg"'
    _todo "$@"
}
compdef _t t

alias tt="todo.sh -a -d $TODO_TXT/trabajo-todo.cfg"
_tt()
{
    local _todo_sh='todo.sh -d "$TODO_TXT/trabajo-todo.cfg"'
    _todo "$@"
}
compdef _tt tt 

But it does not autocomplete or expand @ nor +. If I make

$ ln -s $TODO_TXT/personal-todo.cfg $TODO_TXT/todo.cfg

Then both aliases t and tt autocomplete with personal-todo.cfg

What is the expected behavior? I learnt that I should use compdef instead of complete -F in ZSH. But I guess the problem is now in the wrapper function, that it does not work in ZSH. Does somebody know how to translate it? Can this be added to documentation?

Which versions todo.sh are you using?

Run todo.sh -V

TODO.TXT Command Line Interface v2.11.0

Which Operating System are you using?

% lsb_release -a
LSB Version:    :core-4.1-amd64:core-4.1-noarch
Distributor ID: Fedora
Description:    Fedora release 28 (Twenty Eight)
Release:    28
Codename:   TwentyEight

Which version of bash are you using?

Run bash --version

% zsh --version
zsh 5.5.1 (x86_64-redhat-linux-gnu)
carlotrimarchi commented 5 years ago

I'm experiencing similar issues. I'm on a Mac, I'm using zsh and I'm keeping the todo-txt configuration file in a separate folder inside Dropbox.

Inside .zshrc I added this:

source /usr/local/Cellar/todo-txt/2.11.0/etc/bash_completion.d/todo_completion complete -F _todo t
alias t="/usr/local/bin/todo.sh -d $HOME/Dropbox/todo/todo.cfg"

I'm not even sure it's completely correct, but the auto completion works, somehow. The commands and parameters are completed correctly, while the contexts and tags are completed using a previous todo-txt installation. So it's not that it doesn't work, it doesn't work with the correct file.

Maybe your problem is similar

mbugert commented 3 years ago

I was facing the same issue but found a solution.

First, the reason why @pabgan's approach does not work is that the local _todo_sh='...' workaround is meant for the bash autocompletion script, while zsh ships with an entirely different todotxt autocompletion script which doesn't have this variable. Worse even, the zsh completion script just calls todo.sh with no possibility to override its parameters.

A solution is to use wrapper completion functions which use aliases for todo.sh:

# Without this, zsh would expand the t and tt aliases too early, causing
# it to use the vanilla _todo.sh completion function which reads the
# todotxt config from the default location, i.e. not what we want.
# zsh documentation: http://zsh.sourceforge.net/Doc/Release/Options.html
setopt COMPLETE_ALIASES

alias t="todo.sh -a -d $TODO_TXT/personal-todo.cfg"
_t()
{
    alias todo.sh='todo.sh -d $TODO_TXT/personal-todo.cfg'
    # This is needed to make aliases work in a non-interactive shell,
    # see https://stackoverflow.com/questions/23258413/expand-aliases-in-non-interactive-shells
    setopt ALIASES
    _todo.sh "$@"
}
compdef _t t

alias tt="todo.sh -a -d $TODO_TXT/trabajo-todo.cfg"
_tt()
{
    alias todo.sh='todo.sh -d $TODO_TXT/trabajo-todo.cfg'
    setopt ALIASES
    _todo.sh "$@"
}
compdef _tt tt

A cleaner solution could be achieved of course if the zsh autocompletion script had a variable like the bash one does.

My system for completeness:

pabgan commented 5 months ago

At the time I could not make that work so I went for a completely different approach, linking the config needed when needed. Now, three years after, with much more knowledge I could make it work with @mbugert solution. Thank you very much.