3 # git-escape-magic - zle tweak for git command line arguments
5 # Copyright (c) 2011, 2012, 2014 Akinori MUSHA
6 # Licensed under the 2-clause BSD license.
8 # This tweak eliminates the need for manually escaping shell
9 # meta-characters such as [~^{}] that are used for specifying a git
10 # object (commit or tree). Every time you type one of these
11 # characters on a git command line, it is automatically escaped with a
12 # backslash as necessary and as appropriate.
14 # If you want to use this with url-quote-magic, make sure to enable it
18 # autoload -Uz git-escape-magic
22 git-escape-magic.self-insert() {
25 local self_insert_function
26 zstyle -s ':git-escape-magic' self-insert-function self_insert_function
28 if [[ "$KEYS" == [{}~^]* ]] && {
29 local qkey="${(q)KEYS}"
30 [[ "$KEYS" != "$qkey" ]]
32 local lbuf="$LBUFFER$qkey"
33 [[ "${(Q)LBUFFER}$KEYS" == "${(Q)lbuf}" ]]
36 words=("${(@Q)${(z)lbuf}}")
37 [[ "$words[(i)(*/|)git(|-[^/]##)]" -le $#words ]]
41 i="$words[(I)([;(){\&]|\&[\&\!]|\|\||[=<>]\(*)]"
42 if [[ $i -gt 0 ]]; then
44 if [[ "$words[1]" == [\=\<\>]\(* ]]; then
45 words[1]="${words[1]#[=<>]\(}"
47 [[ "$words[1]" == \; && $words[2] == (then|else|elif|do) ]] && shift words
51 while [[ "$words[1]" == (if|while|until|\!) ]]; do
54 while [[ "$words[1]" == [A-Za-z_][A-Za-z0-9_]#=* ]]; do
57 [[ "$words[1]" == (*/|)git(|-[^/]##) ]] && {
59 subcommand="${words[1]##*/git-}"
60 if [[ -z "$subcommand" ]]; then
62 subcommand="$words[1]"
67 # commands that may take pathspec but never take refspec with [{}~^]
68 (add|rm|am|apply|check-attr|checkout-index|clean|clone|config|diff-files|hash-object|help|index-pack|mailinfo|mailsplit|merge-file|merge-index|mergetool|mktag|mv|pack-objects|pack-redundant|relink|send-email|show-index|show-ref|stage|status|verify-pack)
70 # commands that may take pathspec but rarely take refspec with [{}~^]
71 (for-each-ref|grep|ls-files|update-index)
75 "$words[-2]" == [^-]* ]] ;;
78 "$words[-2]" == [^-]* &&
79 "$words[-3]" == [^-]* ]] ;;
81 [[ $words[(i)--] -gt $#words ]] ;;
83 case "${words[-1]%%"$KEYS"}" in
85 [[ "$KEYS" == [{~^]* ]] ;;
87 [[ "$KEYS" == \}* ]] ;;
89 [[ "$KEYS" == [~^]* ]] ;;
96 zle "$self_insert_function"
99 git-escape-magic.on() {
101 local self_insert_function="${$(zle -lL | awk \
102 '$1=="zle"&&$2=="-N"&&$3=="self-insert"{print $4;exit}'):-.self-insert}"
104 [[ "$self_insert_function" == git-escape-magic.self-insert ]] &&
107 # For url-quote-magic which does not zle -N itself
108 zle -la "$self_insert_function" || zle -N "$self_insert_function"
110 zstyle ':git-escape-magic' self-insert-function "$self_insert_function"
112 zle -A git-escape-magic.self-insert self-insert
116 git-escape-magic.off() {
118 local self_insert_function
119 zstyle -s ':git-escape-magic' self-insert-function self_insert_function
121 [[ -n "$self_insert_function" ]] &&
122 zle -A "$self_insert_function" self-insert
126 zle -N git-escape-magic.self-insert
127 zle -N git-escape-magic.on
128 zle -N git-escape-magic.off
134 [[ -o kshautoload ]] || git-escape-magic "$@"