--- /dev/null
+# Function to make it easier to type taskwarrior tasks as command line
+# arguments. As you type, the input character is analyzed and, if it may need
+# quoting, the first word is checked for a task command (inc aliases). If one
+# is found and the current word is not already in quotes, a backslash is
+# inserted before the input character.
+
+# This widget was inspired by url-quote-magic.
+
+# Setup:
+# autoload -Uz task-quote-magic
+# zle -N self-insert task-quote-magic
+
+# A number of zstyles may be set to control the quoting behavior.
+#
+# task-seps
+# Lists characters that should be considered command separators, redirections,
+# history references, etc. The default is to quote the standard set of shell
+# separators, excluding those that overlap with the extended globbing
+# characters, but including '<' and '>' and the first character of $histchars.
+#
+# task-cmds
+# List of commands for which quoting should be done. By default, task and any
+# command that aliases task.
+
+# Establish default values for styles, but only if not already set
+local -a reply match mbegin mend
+
+zstyle -m ':task-quote-magic:\*' task-seps '*' ||
+ zstyle -e ':task-quote-magic:*' task-seps 'reply=("#{}&<>''${histchars[1]}")'
+
+zstyle -m ':task-quote-magic' task-cmds '*' ||
+ zstyle -e ':task-quote-magic' task-cmds \
+ 'zmodload -i zsh/parameter;
+ reply=( task
+ ${(k)galiases[(R)(* |)task( *|)]:-}
+ ${(k)aliases[(R)(* |)task( *|)]:-} )'
+
+function task-quote-magic {
+ setopt localoptions noksharrays extendedglob
+ local qkey="${(q)KEYS}" # quote(KEYS)
+ local -a reply match mbegin mend
+ # is key different than quoted key
+ if [[ "$KEYS" != "$qkey" ]]
+ then
+ local lbuf="$LBUFFER$qkey"
+ # unquote(lbuffer)+key == unquote(lbuffer+qkey)
+ if [[ "${(Q)LBUFFER}$KEYS" == "${(Q)lbuf}" ]]
+ then
+ local -a words
+ words=("${(@Q)${(z)lbuf}}") # array_quote(unquote(split_words(lbuf)))
+ local taskseps taskcmds
+ zstyle -s ":task-quote-magic" task-cmds taskcmds '|'
+ if [[ "$words[1]" == (#b)${~taskcmds} ]]
+ then
+ zstyle -s ":task-quote-magic:$match[1]" task-seps taskseps ''
+ fi
+ [[ "$taskseps" == *"$KEYS"* ]] &&
+ LBUFFER="$LBUFFER\\"
+ fi
+ fi
+ zle .self-insert
+}
+
+# Handle zsh autoloading conventions
+
+[[ -o kshautoload ]] || task-quote-magic "$@"