about summary refs log tree commit diff
path: root/.config/zsh/plugins/manydots-magic
blob: 1d02c4efac45a9c6fe9041386d67f656de58b480 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# -*- mode: sh -*-
#
# manydots-magic - zle tweak for emulating "..."=="../.." etc.
#
# This tweek helps input ancestor directories beyond the parent (`..')
# in a handy way.  You can just type triple dots to input `../..',
# quadruple dots to `../../..', etc..
#
#     % .. [Hit <.>]
#     % ../.. [Hit <.>]
#     % ../../.. [Hit <^H>]
#     % ../.. [Hit <^H>]
#     % ..
#
# As you see above, each of the `/..' parts complemented by this tweak
# can be deleted by a single invocation of the backward-delete-char
# command, only if invoked right after the magic happens.
#
#     % .. [Hit </><.><.>]
#     % ../.. [Hit <^H>]
#     % ../.
#
# Triple-dot is not a rarely used character sequence, and this tweak
# kind of "knows" when it should be expanded.
#
#     % ruby -e '(1.. [Hit <.>]
#     % ruby -e '(1...
#
#     % git log branch.. [Hit <.>]
#     % git log branch...
#
#     % git diff .. [Hit <.>]
#     % git diff ../.. [Hit <b>]      <- This may be a path...
#     % git diff ...b [Hit <ranch>]   <- Or not.
#     % git diff ...branch
#
# Usage:
#     autoload -Uz manydots-magic
#     manydots-magic
#

manydots-magic.self-insert() {
    emulate -L zsh
    local self_insert_function magic_count
    zstyle -s ':manydots-magic' self-insert-function self_insert_function

    if [[ "$KEYS" == .* && "$LBUFFER" != *...* && "$LBUFFER" == *.. ]] && {
        local -a words
        words=("${(@Q)${(z)LBUFFER}}")
        # `...` is a wildcard operator in go
        [[ ${${(@)words[1,-2]}[(I)go]} = 0 ]] &&
        [[ $words[-1] == (|*[/=]|[\<\>=]\().. ]]
    }
    then
        [[ "$LASTWIDGET" == (self-insert|backward-delete-char) ]] &&
        zstyle -s ':manydots-magic' magic-count magic_count
        zstyle ':manydots-magic' magic-count $((magic_count+1))
        LBUFFER="$LBUFFER/."
        zle "$self_insert_function"
        return
    fi

    # cancel expansion if it does not seem right
    if [[ "$KEYS" != [=/,:\;\|\&\<\>\(\)\[\]{}^~\'\"\`[:space:]]* &&
        "$LASTWIDGET" == (self-insert|backward-delete-char) && "$LBUFFER" == *../.. ]] && {
        zstyle -s ':manydots-magic' magic-count magic_count
        [[ "$magic_count" -gt 0 ]]
    }
    then
        repeat $magic_count LBUFFER="${LBUFFER%/..}"
        repeat $magic_count LBUFFER="$LBUFFER."
    fi

    zstyle ':manydots-magic' magic-count 0

    zle "$self_insert_function"
}

manydots-magic.backward-delete-char() {
    emulate -L zsh
    local backward_delete_char_function
    zstyle -s ':manydots-magic' backward-delete-char-function backward_delete_char_function

    if [[ "$LASTWIDGET" == (self-insert|backward-delete-char) && "$LBUFFER" == *../.. ]] && {
        local magic_count
        zstyle -s ':manydots-magic' magic-count magic_count
        [[ "$magic_count" -gt 0 ]]
    }
    then
        zstyle ':manydots-magic' magic-count $((magic_count-1))
        LBUFFER="${LBUFFER%..}"
    else
        zstyle ':manydots-magic' magic-count 0
    fi

    zle "$backward_delete_char_function"
}

manydots-magic.on() {
    emulate -L zsh
    local self_insert_function="${$(zle -lL | awk \
        '$1=="zle"&&$2=="-N"&&$3=="self-insert"{print $4;exit}'):-.self-insert}"

    [[ "$self_insert_function" == manydots-magic.self-insert ]] &&
        return 0

    # For url-quote-magic which does not zle -N itself
    zle -la "$self_insert_function" || zle -N "$self_insert_function"

    zstyle ':manydots-magic' self-insert-function "$self_insert_function"

    zle -A manydots-magic.self-insert self-insert

    local backward_delete_char_function="$(zle -lL | awk \
        '$1=="zle"&&$2=="-N"&&$3=="backward-delete-char"{print $4;exit}')"

    if [[ -n "$backward_delete_char_function" ]]
    then
        zle -la "$backward_delete_char_function" || zle -N "$backward_delete_char_function"
    else
        zle -A backward-delete-char manydots-magic.orig.backward-delete-char
        backward_delete_char_function=manydots-magic.orig.backward-delete-char
    fi

    zstyle ':manydots-magic' backward-delete-char-function "$backward_delete_char_function"

    zle -A manydots-magic.backward-delete-char backward-delete-char

    zstyle ':manydots-magic' magic-count 0

    return 0
}

manydots-magic.off() {
    emulate -L zsh
    local self_insert_function backward_delete_char_function
    zstyle -s ':manydots-magic' self-insert-function self_insert_function

    [[ -n "$self_insert_function" ]] &&
        zle -A "$self_insert_function" self-insert

    zstyle -s ':manydots-magic' backward-delete-char-function backward_delete_char_function

    [[ -n "$backward_delete_char_function" ]] &&
        zle -A "$backward_delete_char_function" backward-delete-char

    zstyle ':manydots-magic' magic-count 0

    return 0
}

zle -N manydots-magic.self-insert
zle -N manydots-magic.backward-delete-char
zle -N manydots-magic.on
zle -N manydots-magic.off

manydots-magic() {
    manydots-magic.on
}

[[ -o kshautoload ]] || manydots-magic "$@"

# Copyright (c) 2011, 2012 Akinori MUSHA
# 
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.