]> git.rmz.io Git - dotfiles.git/commitdiff
zsh: auto quote task notes
authorSamir Benmendil <me@rmz.io>
Sun, 14 Jan 2024 23:45:15 +0000 (23:45 +0000)
committerSamir Benmendil <me@rmz.io>
Wed, 21 Feb 2024 01:26:27 +0000 (01:26 +0000)
This was inspired from url-quote-magic.

zsh/functions/task-quote-magic [new file with mode: 0644]
zsh/lib/taskwarrior.zsh

diff --git a/zsh/functions/task-quote-magic b/zsh/functions/task-quote-magic
new file mode 100644 (file)
index 0000000..9634320
--- /dev/null
@@ -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 "$@"
index 1759b2f6a9211c6673354b09e4726ac6e652418c..7aae965d74b94a82d2badfceb4e68aa29f51cd32 100644 (file)
@@ -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