8634d9542a974ce4700ca14c3d3bc410945cc20a

Author
Matthew Turney <matthew.a.turney@gmail.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

Add git-escape-magic plugin (#2847)

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/git-escape-magic/README.md b/plugins/git-escape-magic/README.md
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..a14364f2206963f97831d38cc45704713fa2db75
  4--- /dev/null
  5+++ b/plugins/git-escape-magic/README.md
  6@@ -0,0 +1,17 @@
  7+# Git Escape Magic
  8+
  9+This plugin is copied from the original at
 10+https://github.com/knu/zsh-git-escape-magic. All credit for the
 11+functionality enabled by this plugin should go to @knu.
 12+
 13+An excerpt from that project's readme explains it's purpose.
 14+
 15diff --git a/plugins/git-escape-magic/git-escape-magic b/plugins/git-escape-magic/git-escape-magic
 16new file mode 100644
 17index 0000000000000000000000000000000000000000..94a8d7b0fc0862ec9f364d4fbbba8d4022ba348c
 18--- /dev/null
 19+++ b/plugins/git-escape-magic/git-escape-magic
 20@@ -0,0 +1,135 @@
 21+# -*- mode: sh -*-
 22+#
 23+# git-escape-magic - zle tweak for git command line arguments
 24+#
 25+# Copyright (c) 2011, 2012, 2014 Akinori MUSHA
 26+# Licensed under the 2-clause BSD license.
 27+#
 28+# This tweak eliminates the need for manually escaping shell
 29+# meta-characters such as [~^{}] that are used for specifying a git
 30+# object (commit or tree).  Every time you type one of these
 31+# characters on a git command line, it is automatically escaped with a
 32+# backslash as necessary and as appropriate.
 33+#
 34+# If you want to use this with url-quote-magic, make sure to enable it
 35+# first.
 36+#
 37+# Usage:
 38+#     autoload -Uz git-escape-magic
 39+#     git-escape-magic
 40+#
 41+
 42+git-escape-magic.self-insert() {
 43+    emulate -L zsh
 44+    setopt extendedglob
 45+    local self_insert_function
 46+    zstyle -s ':git-escape-magic' self-insert-function self_insert_function
 47+
 48+    if [[ "$KEYS" == [{}~^]* ]] && {
 49+        local qkey="${(q)KEYS}"
 50+        [[ "$KEYS" != "$qkey" ]]
 51+    } && {
 52+        local lbuf="$LBUFFER$qkey"
 53+        [[ "${(Q)LBUFFER}$KEYS" == "${(Q)lbuf}" ]]
 54+    } && {
 55+        local -a words
 56+        words=("${(@Q)${(z)lbuf}}")
 57+        [[ "$words[(i)(*/|)git(|-[^/]##)]" -le $#words ]]
 58+    }
 59+    then
 60+        local i
 61+        i="$words[(I)([;(){\&]|\&[\&\!]|\|\||[=<>]\(*)]"
 62+        if [[ $i -gt 0 ]]; then
 63+            shift $((i-1)) words
 64+            if [[ "$words[1]" == [\=\<\>]\(* ]]; then
 65+                words[1]="${words[1]#[=<>]\(}"
 66+            else
 67+                [[ "$words[1]" == \; && $words[2] == (then|else|elif|do) ]] && shift words
 68+                shift words
 69+            fi
 70+        fi
 71+        while [[ "$words[1]" == (if|while|until|\!) ]]; do
 72+            shift words
 73+        done
 74+        while [[ "$words[1]" == [A-Za-z_][A-Za-z0-9_]#=* ]]; do
 75+            shift words
 76+        done
 77+        [[ "$words[1]" == (*/|)git(|-[^/]##) ]] && {
 78+            local subcommand
 79+            subcommand="${words[1]##*/git-}"
 80+            if [[ -z "$subcommand" ]]; then
 81+                shift words
 82+                subcommand="$words[1]"
 83+            fi
 84+            [[ $#words -ge 2 ]]
 85+        } &&
 86+        case "$subcommand" in
 87+            # commands that may take pathspec but never take refspec with [{}~^]
 88diff --git a/plugins/git-escape-magic/git-escape-magic.plugin.zsh b/plugins/git-escape-magic/git-escape-magic.plugin.zsh
 89new file mode 100644
 90index 0000000000000000000000000000000000000000..c021ea707cafc85469f087f683bab2c5135774c9
 91--- /dev/null
 92+++ b/plugins/git-escape-magic/git-escape-magic.plugin.zsh
 93@@ -0,0 +1,9 @@
 94+# Automatically detect and escape zsh globbing meta-characters when used with
 95+# git refspec characters like `[^~{}]`. NOTE: This must be loaded _after_
 96+# url-quote-magic.
 97+#
 98+# This trick is detailed at https://github.com/knu/zsh-git-escape-magic and is
 99+# what allowed this plugin to exist.
100+
101+autoload -Uz git-escape-magic
102+git-escape-magic