From 202f892e85972ce4eb5248825dd2e2cfe537b64b Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Sun, 20 Nov 2011 14:17:22 +0100 Subject: [PATCH 1/8] Include todo Bash completion from the todo.txt Wiki. This is f32aba2 of https://github.com/ginatrapani/todo.txt-cli/wiki/Tips-and-Tricks, last edited 30-Oct-2011. --- todo_completion | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 todo_completion diff --git a/todo_completion b/todo_completion new file mode 100644 index 0000000..9600e9f --- /dev/null +++ b/todo_completion @@ -0,0 +1,33 @@ +_todo() +{ + local cur prev opts + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[COMP_CWORD-1]}" + 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" + # Add custom commands from add-ons, if installed. + COMMANDS="$COMMANDS $('ls' ${TODO_ACTIONS_DIR:-$HOME/.todo.actions.d}/ 2>/dev/null)" + OPTS="-@ -@@ -+ -++ -d -f -h -p -P -PP -a -n -t -v -vv -V -x" + if [ "${cur:0:1}" == "+" ]; then + completions="$(todo.sh listproj)" + elif [ "${cur:0:1}" == "@" ]; then + completions="$(todo.sh listcon)" + elif [ $COMP_CWORD -eq 1 ]; then + completions="$COMMANDS $OPTS" + else + case "${prev}" in + -*) completions="$COMMANDS $OPTS";; + *) return 0;; + 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 +complete -F _todo todo From 2b0921f4ec0f42a276e1e0116df8ad6d593c57a5 Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Sun, 20 Nov 2011 14:20:25 +0100 Subject: [PATCH 2/8] Add todo_completion to todo.txt distribution. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 9b67a76833535d4a826e03bd6556b456dd068a8b Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Sun, 20 Nov 2011 14:22:48 +0100 Subject: [PATCH 3/8] Do not automatically complete for guessed aliases. Don't infringe against the principle of least astonishment (they user may have completely unrelated aliases). Rather, if the user sets up his own alias, make him apply the same to todo_completion. --- todo_completion | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/todo_completion b/todo_completion index 9600e9f..5e5a0e2 100644 --- a/todo_completion +++ b/todo_completion @@ -29,5 +29,4 @@ _todo() 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 -complete -F _todo todo +#complete -F _todo t From 285d5039f21605996c9e42b86acabdfb59a0747c Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Sun, 20 Nov 2011 14:26:52 +0100 Subject: [PATCH 4/8] Add shebang line and check for Bash. The shebang is ignored when sourcing the script (but still helps many text editors auto-detect the file type), and will cause an error when the script is mistakenly executed. The Bash check allows to have this called from a generic place (e.g. .profile), and do no harm when under a different shell. --- todo_completion | 3 +++ 1 file changed, 3 insertions(+) diff --git a/todo_completion b/todo_completion index 5e5a0e2..0d19e2a 100644 --- a/todo_completion +++ b/todo_completion @@ -1,3 +1,6 @@ +#!/bin/bash source-this-script +[ "$BASH_VERSION" ] || return + _todo() { local cur prev opts From 8cba7533b98a78cd1ed9bbdf011698a6756b9cef Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Sun, 20 Nov 2011 14:36:44 +0100 Subject: [PATCH 5/8] Avoid leaking internal variables into the shell + robustness against add-ons. Use "local" to avoid that the internal completion variables are accessible from the user's shell. Use "todo.sh command" for the context/project lookups to avoid interference with custom add-ons of the same name, and reset TODOTXT_VERBOSE to avoid adding any message output (currently there is none). --- todo_completion | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/todo_completion b/todo_completion index 0d19e2a..f620ce1 100644 --- a/todo_completion +++ b/todo_completion @@ -7,25 +7,33 @@ _todo() COMPREPLY=() cur="${COMP_WORDS[COMP_CWORD]}" prev="${COMP_WORDS[COMP_CWORD-1]}" - 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" + + 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" + # Add custom commands from add-ons, if installed. - COMMANDS="$COMMANDS $('ls' ${TODO_ACTIONS_DIR:-$HOME/.todo.actions.d}/ 2>/dev/null)" - OPTS="-@ -@@ -+ -++ -d -f -h -p -P -PP -a -n -t -v -vv -V -x" - if [ "${cur:0:1}" == "+" ]; then - completions="$(todo.sh listproj)" - elif [ "${cur:0:1}" == "@" ]; then - completions="$(todo.sh listcon)" - elif [ $COMP_CWORD -eq 1 ]; then - completions="$COMMANDS $OPTS" + 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 - -*) completions="$COMMANDS $OPTS";; - *) return 0;; + 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);; + *) return 0;; + esac esac fi + COMPREPLY=( $( compgen -W "$completions" -- $cur )) return 0 } From f5270be4634c5c0d55c0afd4ad8e02d217236ea2 Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Sun, 20 Nov 2011 14:57:36 +0100 Subject: [PATCH 6/8] ENH: Append task text as shell comment when completing task number. This is useful for the paranoid before a destructive todo.txt operation. Appending the text as a shell comment doesn't affect the todo.txt command itself, but shows that the task number corresponds to the task you had in mind. --- todo_completion | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/todo_completion b/todo_completion index f620ce1..45f8245 100644 --- a/todo_completion +++ b/todo_completion @@ -29,8 +29,28 @@ _todo() *) case "$cur" in +*) completions=$(TODOTXT_VERBOSE=0 todo.sh command listproj);; @*) completions=$(TODOTXT_VERBOSE=0 todo.sh command listcon);; - *) return 0;; + *) 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 From b1d894f65ddff91ebef864a4a96c40e174d832c0 Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Sun, 20 Nov 2011 15:01:14 +0100 Subject: [PATCH 7/8] Add recently added "shorthelp" built-in command. --- todo_completion | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/todo_completion b/todo_completion index 45f8245..cc1bd54 100644 --- a/todo_completion +++ b/todo_completion @@ -13,7 +13,7 @@ _todo() 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" + 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)" From d2e0f6e38d25bb0b368c20ca007cb8a4bd05ac0c Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Sun, 20 Nov 2011 15:03:19 +0100 Subject: [PATCH 8/8] FIX: Support $HOME and $TODOTXT_ACTIONS_DIR containing spaces. And document a minor discrepancy in action completion to todo.sh. --- todo_completion | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/todo_completion b/todo_completion index cc1bd54..c80fba5 100644 --- a/todo_completion +++ b/todo_completion @@ -16,7 +16,8 @@ _todo() 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)" + # 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