diff --git a/Makefile b/Makefile index 3219f85..0450577 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ VERSION-FILE: .FORCE-VERSION-FILE todo.sh: VERSION-FILE # For packaging -DISTFILES := todo.cfg +DISTFILES := todo.cfg todo_completion DISTNAME=todo.txt_cli-$(VERSION) dist: $(DISTFILES) todo.sh diff --git a/todo_completion b/todo_completion new file mode 100644 index 0000000..c80fba5 --- /dev/null +++ b/todo_completion @@ -0,0 +1,64 @@ +#!/bin/bash source-this-script +[ "$BASH_VERSION" ] || return + +_todo() +{ + local cur prev opts + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + + local -r OPTS="-@ -@@ -+ -++ -d -f -h -p -P -PP -a -n -t -v -vv -V -x" + local -r COMMANDS="\ + add a addto addm append app archive command del \ + rm depri dp do help list ls listall lsa listcon \ + lsc listfile lf listpri lsp listproj lsprj move \ + mv prepend prep pri p replace report shorthelp" + + # Add custom commands from add-ons, if installed. + # TODO: Filter for executable flag of files found in $TODO_ACTIONS_DIR. + local allCommands="$COMMANDS $('ls' "${TODO_ACTIONS_DIR:-$HOME/.todo.actions.d}/" 2>/dev/null)" + + local completions + if [ $COMP_CWORD -eq 1 ]; then + completions="$allCommands $OPTS" + else + case "$prev" in + command) + completions=$COMMANDS;; + -*) completions="$allCommands $OPTS";; + *) case "$cur" in + +*) completions=$(TODOTXT_VERBOSE=0 todo.sh command listproj);; + @*) completions=$(TODOTXT_VERBOSE=0 todo.sh command listcon);; + *) if [[ "$cur" =~ ^[0-9]+$ ]]; then + local item=$(TODOTXT_VERBOSE=0 todo.sh -@ -+ -p -x command ls "^ *${cur} " | head -n 1) + + # Remove the (padded) task number; we prepend the + # user-provided $cur. + item=${item#* } + + # Remove the timestamp prepended by the -t option; + # there's no todo.txt option for that yet. + item=${item#[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] } + + # Append task text as a shell comment. This + # completion can be a safety check before a + # destructive todo.txt operation. + [ "$item" ] && COMPREPLY[0]="$cur # $item" + return 0 + else + return 0 + fi + ;; + esac + ;; + esac + fi + + COMPREPLY=( $( compgen -W "$completions" -- $cur )) + return 0 +} +complete -F _todo todo.sh +# If you define an alias (e.g. "t") to todo.sh, you need to explicitly enable +# completion for it, too: +#complete -F _todo t