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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
# -*- mode: sh; sh-indentation: 4; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# Copyright (c) 2018 Sebastian Gniazdowski
# Copyright (c) 2018 plexigras
#
# The old chroma function for command `git'. It colorizes the part of command
# line that holds `git' invocation.
#
# $1 - 0 or 1, denoting if it's first call to the chroma, or following one
# $2 - the current token, also accessible by $__arg from the above scope -
# basically a private copy of $__arg
# $3 - a private copy of $_start_pos, i.e. the position of the token in the
# command line buffer, used to add region_highlight entry (see man),
# because Zsh colorizes by *ranges* in command line buffer
# $4 - a private copy of $_end_pos from the above scope
#
(( next_word = 2 | 8192 ))
local __first_call="$1" __wrd="$2" __start_pos="$3" __end_pos="$4"
local __style
integer __idx1 __idx2
local -a __lines_list chroma_git_remote_subcommands
chroma_git_remote_subcommands=(add rename remove set-head set-branches get-url set-url set-url set-url show prune update)
if (( __first_call )); then
# Called for the first time - new command
# FAST_HIGHLIGHT is used because it survives between calls, and
# allows to use a single global hash only, instead of multiple
# global variables
FAST_HIGHLIGHT[chroma-git-counter]=0
FAST_HIGHLIGHT[chroma-git-got-subcommand]=0
FAST_HIGHLIGHT[chroma-git-subcommand]=""
FAST_HIGHLIGHT[chrome-git-got-msg1]=0
FAST_HIGHLIGHT[chrome-git-got-anymsg]=0
FAST_HIGHLIGHT[chrome-git-occurred-double-hyphen]=0
FAST_HIGHLIGHT[chroma-git-checkout-new]=0
FAST_HIGHLIGHT[chroma-git-fetch-multiple]=0
FAST_HIGHLIGHT[chroma-git-branch-change]=0
FAST_HIGHLIGHT[chroma-git-option-with-argument-active]=0
return 1
else
# Following call, i.e. not the first one
# Check if chroma should end – test if token is of type
# "starts new command", if so pass-through – chroma ends
[[ "$__arg_type" = 3 ]] && return 2
if [[ "$__wrd" = "--" ]]; then
FAST_HIGHLIGHT[chrome-git-occurred-double-hyphen]=1
__style=${FAST_THEME_NAME}double-hyphen-option
elif [[ "$__wrd" = -* && ${FAST_HIGHLIGHT[chroma-git-got-subcommand]} -eq 0 ]]; then
# Options occuring before a subcommand
if (( FAST_HIGHLIGHT[chroma-git-option-with-argument-active] == 0 )); then
if [[ "$__wrd" = -[^[:space:]-]#C ]]; then
FAST_HIGHLIGHT[chroma-git-option-with-argument-active]=2
elif [[ "$__wrd" = -[^[:space:]-]#c ]]; then
FAST_HIGHLIGHT[chroma-git-option-with-argument-active]=1
fi
fi
return 1
else
# If at e.g. '>' or destination/source spec (of the redirection)
if (( in_redirection > 0 || this_word & 128 )) || [[ $__wrd == "<<<" ]]; then
return 1
# If at main git option taking argument in a separate word (-C and -c)
elif (( FAST_HIGHLIGHT[chroma-git-option-with-argument-active] > 0 && \
0 == FAST_HIGHLIGHT[chroma-git-got-subcommand] ))
then
# Remember the value
__idx2=${FAST_HIGHLIGHT[chroma-git-option-with-argument-active]}
# Reset the is-argument mark-field
FAST_HIGHLIGHT[chroma-git-option-with-argument-active]=0
(( __idx2 == 2 )) && return 1
# Other options' args (i.e. arg of -c) aren't routed to the big-loop
# as they aren't paths and aren't handled in any special way there
elif (( FAST_HIGHLIGHT[chroma-git-got-subcommand] == 0 )); then
FAST_HIGHLIGHT[chroma-git-got-subcommand]=1
# Check if the command is an alias - we want to highlight the
# aliased command just like the target command of the alias
-fast-run-command "git config --get-regexp 'alias.*'" chroma-git-alias-list "" $(( 10 * 60 ))
# Grep for line: alias.{user-entered-subcmd}[[:space:]], and remove alias. prefix
__lines_list=( ${${(M)__lines_list[@]:#alias.${__wrd}[[:space:]]##*}#alias.} )
if (( ${#__lines_list} > 0 )); then
# (*)
# First remove alias name (#*[[:space:]]) and the space after it, then
# remove any leading spaces from what's left (##[[:space:]]##), then
# remove everything except the first word that's in the left line
# (%%[[:space:]]##*, i.e.: "everything from right side up to any space")
FAST_HIGHLIGHT[chroma-git-subcommand]="${${${__lines_list[1]#*[[:space:]]}##[[:space:]]##}%%[[:space:]]##*}"
else
FAST_HIGHLIGHT[chroma-git-subcommand]="$__wrd"
fi
if (( __start_pos >= 0 )); then
# if subcommand exists
LANG=C -fast-run-command "git help -a" chroma-git-subcmd-list "" $(( 10 * 60 ))
# (s: :) will split on every space, but because the expression
# isn't double-quoted, the empty elements will be eradicated
# Some further knowledge-base: s-flag is special, it skips
# empty elements and creates an array (not a concatenated
# string) even when double-quoted. The normally needed @-flag
# that logically breaks the concaetnated string back into array
# in case of double-quoting has additional effect for s-flag:
# it finally blocks empty-elements eradication.
if [[ "${__lines_list[1]}" = See* ]]; then
# (**)
# git >= v2.20
__lines_list=( ${(M)${${${(M)__lines_list[@]:# [[:blank:]]#[a-z]*}##[[:blank:]]##}%%[[:blank:]]##*}:#${FAST_HIGHLIGHT[chroma-git-subcommand]}} )
else
# (**)
# git < v2.20
__lines_list=( ${(M)${(s: :)${(M)__lines_list[@]:# [a-z]*}}:#${FAST_HIGHLIGHT[chroma-git-subcommand]}} )
fi
# Above we've checked:
# 1) If given subcommand is an alias (*)
# 2) If the command, or command pointed by the alias, exists (**)
# 3) There's little problem, git v2.20 outputs aliases in git help -a,
# which means that alias will be recognized as correct if it will
# point at another alias or on itself. That's a minor problem, a
# TODO for future planned optimization for v2.20 Git
# 4) Notice that the above situation is better than the previous - the
# alias is being verified to point to a valid git subcommand
# That's all that's needed to decide on the correctnes:
if (( ${#__lines_list} > 0 )); then
__style=${FAST_THEME_NAME}subcommand
else
__style=${FAST_THEME_NAME}incorrect-subtle
fi
fi
# The counter includes the subcommand itself
(( FAST_HIGHLIGHT[chroma-git-counter] += 1 ))
else
__wrd="${__wrd//\`/x}"
__arg="${__arg//\`/x}"
__wrd="${(Q)__wrd}"
if [[ "${FAST_HIGHLIGHT[chroma-git-subcommand]}" = "push" \
|| "${FAST_HIGHLIGHT[chroma-git-subcommand]}" = "pull" \
|| "${FAST_HIGHLIGHT[chroma-git-subcommand]}" = "fetch" ]] \
&& (( ${FAST_HIGHLIGHT[chroma-git-fetch-multiple]} == 0 )); then
# if not option
if [[ "$__wrd" != -* || "${FAST_HIGHLIGHT[chrome-git-occurred-double-hyphen]}" -eq 1 ]]; then
(( FAST_HIGHLIGHT[chroma-git-counter] += 1, __idx1 = FAST_HIGHLIGHT[chroma-git-counter] ))
if (( __idx1 == 2 )); then
-fast-run-git-command "git remote" "chroma-git-remotes" ""
else
__wrd="${__wrd%%:*}"
-fast-run-git-command "git for-each-ref --format='%(refname:short)' refs/heads" "chroma-git-branches" "refs/heads"
fi
# if remote/ref exists
if [[ -n ${__lines_list[(r)$__wrd]} ]]; then
(( __start=__start_pos-${#PREBUFFER}, __end=__start_pos+${#__wrd}-${#PREBUFFER}, __start >= 0 )) && \
reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}correct-subtle]}")
# if ref (__idx1 == 3) does not exist and subcommand is push
elif (( __idx1 != 2 )) && [[ "${FAST_HIGHLIGHT[chroma-git-subcommand]}" = "push" ]]; then
(( __start=__start_pos-${#PREBUFFER}, __end=__start_pos+${#__wrd}-${#PREBUFFER}, __start >= 0 )) && \
reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}incorrect-subtle]}")
# if not existing remote name, because not an URL (i.e. no colon)
elif [[ $__idx1 -eq 2 && $__wrd != *:* ]]; then
(( __start=__start_pos-${#PREBUFFER}, __end=__start_pos+${#__wrd}-${#PREBUFFER}, __start >= 0 )) && \
reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}incorrect-subtle]}")
fi
# if option
else
if [[ "$__wrd" = "--multiple" && "${FAST_HIGHLIGHT[chroma-git-subcommand]}" = "fetch" ]]; then
FAST_HIGHLIGHT[chroma-git-fetch-multiple]=1
__style=${FAST_THEME_NAME}double-hyphen-option
else
return 1
fi
fi
elif (( ${FAST_HIGHLIGHT[chroma-git-fetch-multiple]} )) \
&& [[ "$__wrd" != -* || "${FAST_HIGHLIGHT[chrome-git-occurred-double-hyphen]}" -eq 1 ]]; then
-fast-run-git-command "git remote" "chroma-git-remotes" ""
if [[ -n ${__lines_list[(r)$__wrd]} ]]; then
__style=${FAST_THEME_NAME}correct-subtle
fi
elif [[ "${FAST_HIGHLIGHT[chroma-git-subcommand]}" = "commit" ]]; then
match[1]=""
match[2]=""
# if previous argument is -m or current argument is --message=something
if (( FAST_HIGHLIGHT[chrome-git-got-msg1] == 1 && ! FAST_HIGHLIGHT[chrome-git-got-anymsg] )) \
|| [[ "$__wrd" = (#b)(--message=)(*) && "${FAST_HIGHLIGHT[chrome-git-occurred-double-hyphen]}" = 0 ]]; then
FAST_HIGHLIGHT[chrome-git-got-msg1]=0
FAST_HIGHLIGHT[chrome-git-got-anymsg]=1
if [[ -n "${match[1]}" ]]; then
__wrd="${(Q)${match[2]//\`/x}}"
# highlight (--message=)something
(( __start=__start_pos-${#PREBUFFER}, __end=__start_pos-${#PREBUFFER}+10, __start >= 0 )) && \
reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}double-hyphen-option]}")
# highlight --message=(something)
(( __start=__start_pos-${#PREBUFFER}+10, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && \
reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}double-quoted-argument]}")
else
(( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) && \
reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}double-quoted-argument]}")
fi
local __firstline=${__wrd%%$'\n'*}
if (( ${#__firstline} > 50 )); then
for (( __idx1 = 1, __idx2 = 1; __idx1 <= 50; ++ __idx1, ++ __idx2 )); do
while [[ "${__arg[__idx2]}" != "${__firstline[__idx1]}" ]]; do
(( ++ __idx2 ))
(( __idx2 > __asize )) && { __idx2=-1; break; }
done
(( __idx2 == -1 )) && break
done
if (( __idx2 != -1 )); then
if [[ -n "${match[1]}" ]]; then
(( __start=__start_pos-${#PREBUFFER}+__idx2, __end=__end_pos-${#PREBUFFER}-$#__wrd+$#__firstline-1, __start >= 0 )) && \
reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}incorrect-subtle]}")
else
(( __start=__start_pos-${#PREBUFFER}+__idx2-1, __end=__end_pos-${#PREBUFFER}-$#__wrd+$#__firstline-1, __start >= 0 )) && \
reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[${FAST_THEME_NAME}incorrect-subtle]}")
fi
fi
fi
# if before --
elif [[ "${FAST_HIGHLIGHT[chrome-git-occurred-double-hyphen]}" = 0 ]]; then
if [[ "$__wrd" = -[^[:space:]-]#m ]]; then
FAST_HIGHLIGHT[chrome-git-got-msg1]=1
__style=${FAST_THEME_NAME}single-hyphen-option
else
return 1
fi
# if after -- is file
elif [[ -e "$__wrd" ]]; then
__style=${FAST_THEME_NAME}path
else
__style=${FAST_THEME_NAME}incorrect-subtle
fi
elif [[ "${FAST_HIGHLIGHT[chroma-git-subcommand]}" = "checkout" ]] \
|| [[ "${FAST_HIGHLIGHT[chroma-git-subcommand]}" = "revert" ]] \
|| [[ "${FAST_HIGHLIGHT[chroma-git-subcommand]}" = "merge" ]] \
|| [[ "${FAST_HIGHLIGHT[chroma-git-subcommand]}" = "diff" ]] \
|| [[ "${FAST_HIGHLIGHT[chroma-git-subcommand]}" = "reset" ]] \
|| [[ "${FAST_HIGHLIGHT[chroma-git-subcommand]}" = "rebase" ]]; then
# if doing `git checkout -b ...'
if [[ "$__wrd" = -[^[:space:]-]#b && "${FAST_HIGHLIGHT[chroma-git-subcommand]}" = "checkout" ]]; then
FAST_HIGHLIGHT[chroma-git-checkout-new]=1
__style=${FAST_THEME_NAME}single-hyphen-option
# if command is not checkout -b something
elif [[ "${FAST_HIGHLIGHT[chroma-git-checkout-new]}" = 0 ]]; then
# if not option
if [[ "$__wrd" != -* || "${FAST_HIGHLIGHT[chrome-git-occurred-double-hyphen]}" = 1 ]]; then
(( FAST_HIGHLIGHT[chroma-git-counter] += 1, __idx1 = FAST_HIGHLIGHT[chroma-git-counter] ))
if (( __idx1 == 2 )) || \
[[ "$__idx1" = 3 && "${FAST_HIGHLIGHT[chroma-git-subcommand]}" = "diff" ]]; then
# if is ref
if command git rev-parse --verify --quiet "$__wrd" >/dev/null 2>&1; then
__style=${FAST_THEME_NAME}correct-subtle
# if is file and subcommand is checkout or diff
elif [[ "${FAST_HIGHLIGHT[chroma-git-subcommand]}" = "checkout" \
|| "${FAST_HIGHLIGHT[chroma-git-subcommand]}" = "reset" \
|| "${FAST_HIGHLIGHT[chroma-git-subcommand]}" = "diff" ]] && [[ -e ${~__wrd} ]]; then
__style=${FAST_THEME_NAME}path
elif [[ "${FAST_HIGHLIGHT[chroma-git-subcommand]}" = "checkout" && \
"1" = "$(command git rev-list --count --no-walk --glob="refs/remotes/${$(git \
config --get checkout.defaultRemote):-*}/$__wrd")" ]]
then
__style=${FAST_THEME_NAME}correct-subtle
else
__style=${FAST_THEME_NAME}incorrect-subtle
fi
fi
# if option
else
return 1
fi
# if option
elif [[ "${FAST_HIGHLIGHT[chrome-git-occurred-double-hyphen]}" = 0 && "$__wrd" = -* ]]; then
return 1
fi
elif [[ "${FAST_HIGHLIGHT[chroma-git-subcommand]}" = "remote" && "$__wrd" != -* ]]; then
(( FAST_HIGHLIGHT[chroma-git-counter] += 1, __idx1 = FAST_HIGHLIGHT[chroma-git-counter] ))
if [[ "$__idx1" = 2 ]]; then
if (( ${chroma_git_remote_subcommands[(I)$__wrd]} )); then
FAST_HIGHLIGHT[chroma-git-remote-subcommand]="$__wrd"
__style=${FAST_THEME_NAME}subcommand
else
__style=${FAST_THEME_NAME}incorrect-subtle
fi
elif [[ "$__idx1" = 3 && "$FAST_HIGHLIGHT[chroma-git-remote-subcommand]" = "add" ]]; then
-fast-run-git-command "git remote" "chroma-git-remotes" ""
if [[ -n ${__lines_list[(r)$__wrd]} ]]; then
__style=${FAST_THEME_NAME}incorrect-subtle
fi
elif [[ "$__idx1" = 3 && -n "$FAST_HIGHLIGHT[chroma-git-remote-subcommand]" ]]; then
-fast-run-git-command "git remote" "chroma-git-remotes" ""
if [[ -n ${__lines_list[(r)$__wrd]} ]]; then
__style=${FAST_THEME_NAME}correct-subtle
else
__style=${FAST_THEME_NAME}incorrect-subtle
fi
fi
elif [[ "${FAST_HIGHLIGHT[chroma-git-subcommand]}" = "branch" ]]; then
if [[ "$__wrd" = --delete \
|| "$__wrd" = --edit-description \
|| "$__wrd" = --set-upstream-to=* \
|| "$__wrd" = --unset-upstream \
|| "$__wrd" = -[^[:space:]-]#d \
|| "$__wrd" = -[^[:space:]-]#D ]]; then
FAST_HIGHLIGHT[chroma-git-branch-change]=1
return 1
elif [[ "$__wrd" != -* ]]; then
-fast-run-git-command "git for-each-ref --format='%(refname:short)' refs/heads" "chroma-git-branches" "refs/heads"
if [[ -n ${__lines_list[(r)$__wrd]} ]]; then
__style=${FAST_THEME_NAME}correct-subtle
elif (( FAST_HIGHLIGHT[chroma-git-branch-change] )); then
__style=${FAST_THEME_NAME}incorrect-subtle
fi
else
return 1
fi
elif [[ "${FAST_HIGHLIGHT[chroma-git-subcommand]}" = "tag" ]]; then
if [[ "${FAST_HIGHLIGHT[chroma-git-option-with-argument-active]}" -le 0 ]]; then
if [[ "$__wrd" = -[^[:space:]-]#(u|m) ]]; then
FAST_HIGHLIGHT[chroma-git-option-with-argument-active]=1
elif [[ "$__wrd" = -[^[:space:]-]#F ]]; then
FAST_HIGHLIGHT[chroma-git-option-with-argument-active]=2
elif [[ "$__wrd" = -[^[:space:]-]#d ]]; then
FAST_HIGHLIGHT[chroma-git-option-with-argument-active]=3
elif [[ "$__wrd" = (--contains|--no-contains|--points-at|--merged|--no-merged) ]]; then
FAST_HIGHLIGHT[chroma-git-option-with-argument-active]=4
fi
if [[ "$__wrd" != -* ]]; then
(( FAST_HIGHLIGHT[chroma-git-counter] += 1, __idx1 = FAST_HIGHLIGHT[chroma-git-counter] ))
if [[ ${FAST_HIGHLIGHT[chroma-git-counter]} -eq 2 ]]; then
-fast-run-git-command "git for-each-ref --format='%(refname:short)' refs/heads" "chroma-git-branches" "refs/heads"
-fast-run-git-command "+git tag" "chroma-git-tags" ""
[[ -n ${__lines_list[(r)$__wrd]} ]] && __style=${FAST_THEME_NAME}incorrect-subtle
elif [[ ${FAST_HIGHLIGHT[chroma-git-counter]} -eq 3 ]]; then
fi
else
return 1
fi
else
case "${FAST_HIGHLIGHT[chroma-git-option-with-argument-active]}" in
(1)
__style=${FAST_THEME_NAME}optarg-string
;;
(2)
FAST_HIGHLIGHT[chroma-git-option-with-argument-active]=0
return 1;
;;
(3)
-fast-run-git-command "git tag" "chroma-git-tags" ""
[[ -n ${__lines_list[(r)$__wrd]} ]] && \
__style=${FAST_THEME_NAME}correct-subtle || \
__style=${FAST_THEME_NAME}incorrect-subtle
;;
(4)
if git rev-parse --verify --quiet "$__wrd" >/dev/null 2>&1; then
__style=${FAST_THEME_NAME}correct-subtle
else
__style=${FAST_THEME_NAME}incorrect-subtle
fi
;;
esac
FAST_HIGHLIGHT[chroma-git-option-with-argument-active]=0
fi
else
return 1
fi
fi
fi
fi
# Add region_highlight entry (via `reply' array)
if [[ -n "$__style" ]]; then
(( __start=__start_pos-${#PREBUFFER}, __end=__end_pos-${#PREBUFFER}, __start >= 0 )) \
&& reply+=("$__start $__end ${FAST_HIGHLIGHT_STYLES[$__style]}")
fi
# We aren't passing-through, do obligatory things ourselves
(( this_word = next_word ))
_start_pos=$_end_pos
return 0
# vim:ft=zsh:et:sw=4
|