]> git.rmz.io Git - dotfiles.git/blob - zsh/functions/task-quote-magic
vim: do not set pastetoggle in nvim
[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 # 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;
35 reply=( task
36 ${(k)aliases[(R)(* |)task( *|)]:-} )'
37
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" ]]
44 then
45 local lbuf="$LBUFFER$qkey"
46 # unquote(lbuffer)+key == unquote(lbuffer+qkey)
47 if [[ "${(Q)LBUFFER}$KEYS" == "${(Q)lbuf}" ]]
48 then
49 local -a words
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} ]]
54 then
55 zstyle -s ":task-quote-magic:$match[1]" task-seps taskseps ''
56 fi
57 [[ "$taskseps" == *"$KEYS"* ]] &&
58 LBUFFER="$LBUFFER\\"
59 fi
60 fi
61 zle .self-insert
62 }
63
64 # Handle zsh autoloading conventions
65
66 [[ -o kshautoload ]] || task-quote-magic "$@"