1 # Function to make it easier to type taskwarrior tasks as command line
2 # arguments. As you type, the input character is analyzed and, if it may need
3 # quoting, the first word is checked for a task command (inc aliases). If one
4 # is found and the current word is not already in quotes, a backslash is
5 # inserted before the input character.
7 # This widget was inspired by url-quote-magic.
10 # autoload -Uz task-quote-magic
11 # zle -N self-insert task-quote-magic
13 # A number of zstyles may be set to control the quoting behavior.
16 # Lists characters that should be considered command separators, redirections,
17 # history references, etc. The default is to quote the standard set of shell
18 # separators, excluding those that overlap with the extended globbing
19 # characters, but including '<' and '>' and the first character of $histchars.
22 # List of commands for which quoting should be done. By default, task and any
23 # command that aliases task.
25 # Establish default values for styles, but only if not already set
26 local -a reply match mbegin mend
28 zstyle -m ':task-quote-magic:\*' task-seps '*' ||
29 zstyle -e ':task-quote-magic:*' task-seps 'reply=("#{}\`\"&<>''${histchars[1]}")'
31 # TODO: recursively check if cmd is an alias of an alias of task
32 zstyle -m ':task-quote-magic' task-cmds '*' ||
33 zstyle -e ':task-quote-magic' task-cmds \
34 'zmodload -i zsh/parameter;
36 ${(k)aliases[(R)(* |)task( *|)]:-} )'
38 function task-quote-magic {
39 setopt localoptions noksharrays extendedglob
40 local qkey="${(q)KEYS}" # quote(KEYS)
41 local -a reply match mbegin mend
42 # is key different than quoted key
43 if [[ "$KEYS" != "$qkey" ]]
45 local lbuf="$LBUFFER$qkey"
46 # unquote(lbuffer)+key == unquote(lbuffer+qkey)
47 if [[ "${(Q)LBUFFER}$KEYS" == "${(Q)lbuf}" ]]
50 words=("${(@Q)${(z)lbuf}}") # array_quote(unquote(split_words(lbuf)))
51 local taskseps taskcmds
52 zstyle -s ":task-quote-magic" task-cmds taskcmds '|'
53 if [[ "$words[1]" == (#b)${~taskcmds} ]]
55 zstyle -s ":task-quote-magic:$match[1]" task-seps taskseps ''
57 [[ "$taskseps" == *"$KEYS"* ]] &&
64 # Handle zsh autoloading conventions
66 [[ -o kshautoload ]] || task-quote-magic "$@"