From: Samir Benmendil Date: Sun, 14 Jan 2024 23:45:15 +0000 (+0000) Subject: zsh: auto quote task notes X-Git-Url: https://git.rmz.io/dotfiles.git/commitdiff_plain/442f99c7e99107502b4d3c732f12e538719fbd81?ds=inline zsh: auto quote task notes This was inspired from url-quote-magic. --- diff --git a/zsh/functions/task-quote-magic b/zsh/functions/task-quote-magic new file mode 100644 index 0000000..9634320 --- /dev/null +++ b/zsh/functions/task-quote-magic @@ -0,0 +1,66 @@ +# 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 "$@" diff --git a/zsh/lib/taskwarrior.zsh b/zsh/lib/taskwarrior.zsh index 1759b2f..7aae965 100644 --- a/zsh/lib/taskwarrior.zsh +++ b/zsh/lib/taskwarrior.zsh @@ -4,3 +4,6 @@ alias in="noglob task add +in" alias inn="in rc.context=none" alias tw="task context wfh" alias th="task context home" + +autoload -Uz task-quote-magic +zle -N self-insert task-quote-magic