#!/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. 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