]> git.rmz.io Git - dotfiles.git/blob - zsh/functions/task-quote-magic
zsh: don't do task-quote-magic on global aliases
[dotfiles.git] / zsh / functions / task-quote-magic
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.
6
7 # This widget was inspired by url-quote-magic.
8
9 # Setup:
10 # autoload -Uz task-quote-magic
11 # zle -N self-insert task-quote-magic
12
13 # A number of zstyles may be set to control the quoting behavior.
14 #
15 # task-seps
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.
20 #
21 # task-cmds
22 # List of commands for which quoting should be done. By default, task and any
23 # command that aliases task.
24
25 # Establish default values for styles, but only if not already set
26 local -a reply match mbegin mend
27
28 zstyle -m ':task-quote-magic:\*' task-seps '*' ||
29 zstyle -e ':task-quote-magic:*' task-seps 'reply=("#{}&<>''${histchars[1]}")'
30
31 zstyle -m ':task-quote-magic' task-cmds '*' ||
32 zstyle -e ':task-quote-magic' task-cmds \
33 'zmodload -i zsh/parameter;
34 reply=( task
35 ${(k)aliases[(R)(* |)task( *|)]:-} )'
36
37 function task-quote-magic {
38 setopt localoptions noksharrays extendedglob
39 local qkey="${(q)KEYS}" # quote(KEYS)
40 local -a reply match mbegin mend
41 # is key different than quoted key
42 if [[ "$KEYS" != "$qkey" ]]
43 then
44 local lbuf="$LBUFFER$qkey"
45 # unquote(lbuffer)+key == unquote(lbuffer+qkey)
46 if [[ "${(Q)LBUFFER}$KEYS" == "${(Q)lbuf}" ]]
47 then
48 local -a words
49 words=("${(@Q)${(z)lbuf}}") # array_quote(unquote(split_words(lbuf)))
50 local taskseps taskcmds
51 zstyle -s ":task-quote-magic" task-cmds taskcmds '|'
52 if [[ "$words[1]" == (#b)${~taskcmds} ]]
53 then
54 zstyle -s ":task-quote-magic:$match[1]" task-seps taskseps ''
55 fi
56 [[ "$taskseps" == *"$KEYS"* ]] &&
57 LBUFFER="$LBUFFER\\"
58 fi
59 fi
60 zle .self-insert
61 }
62
63 # Handle zsh autoloading conventions
64
65 [[ -o kshautoload ]] || task-quote-magic "$@"