-
Notifications
You must be signed in to change notification settings - Fork 20
/
zsh-abbr.zsh
executable file
·1900 lines (1474 loc) · 51.5 KB
/
zsh-abbr.zsh
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env zsh
# abbreviation management for zsh, inspired by fish shell and enhanced
# https://github.com/olets/zsh-abbr
# v6.0.1
# Copyright (c) 2019-present Henry Bley-Vroman
# CONFIGURATION
# -------------
# Should `abbr-load` run before every `abbr` command? (default true)
typeset -gi ABBR_AUTOLOAD=${ABBR_AUTOLOAD:-1}
# Log debugging messages?
typeset -gi ABBR_DEBUG=${ABBR_DEBUG:-0}
# Whether to add default bindings (expand on SPACE, expand and accept on ENTER,
# add CTRL for normal SPACE/ENTER; in incremental search mode expand on CTRL+SPACE)
# (default true)
typeset -gi ABBR_DEFAULT_BINDINGS=${ABBR_DEFAULT_BINDINGS:-1}
# Behave as if `--dry-run` was passed? (default false)
typeset -gi ABBR_DRY_RUN=${ABBR_DRY_RUN:-0}
# See ABBR_SET_LINE_CURSOR
typeset -g ABBR_LINE_CURSOR_MARKER=${ABBR_LINE_CURSOR_MARKER:-%}
# See ABBR_SET_EXPANSION_CURSOR
typeset -g ABBR_EXPANSION_CURSOR_MARKER=${ABBR_EXPANSION_CURSOR_MARKER:-$ABBR_LINE_CURSOR_MARKER}
# Behave as if `--force` was passed? (default false)
typeset -gi ABBR_FORCE=${ABBR_FORCE:-0}
# Check whether you could have used an abbreviation? (default false)
# See also ABBR_LOG_AVAILABLE_ABBREVIATION
typeset -gi ABBR_GET_AVAILABLE_ABBREVIATION=${ABBR_GET_AVAILABLE_ABBREVIATION:-0}
# Log anything found by ABBR_GET_AVAILABLE_ABBREVIATION? (default false)
typeset -gi ABBR_LOG_AVAILABLE_ABBREVIATION=${ABBR_LOG_AVAILABLE_ABBREVIATION:-0}
# If ABBR_LOG_AVAILABLE_ABBREVIATION is non-zero, should
# it log come _after_ the command output? (default false)
typeset -gi ABBR_LOG_AVAILABLE_ABBREVIATION_AFTER=${ABBR_LOG_AVAILABLE_ABBREVIATION_AFTER:-0}
# Should abbr-expand-and-accept push the unexpanded line to the shell history? (default false)
# If true, if abbr-expand-and-accept expands an abbreviation there will be two history entries:
# the first is line with the abbreviation, the second is what was run (with the expansion).
# With this caveat: if ABBR_EXPAND_PUSH_ABBREVIATION_TO_HISTORY is true, abbr-expand-and-accept
# will only push the line with the abbreviation to history if it's different from what abbr-expand
# pushed to history. That is, if you
# % abbr a=b
# % a[Enter]
# the history will be
# abbr a=b
# a
# b
# not
# abbr a=b
# a
# a
# b
typeset -gi ABBR_EXPAND_AND_ACCEPT_PUSH_ABBREVIATED_LINE_TO_HISTORY=${ABBR_EXPAND_AND_ACCEPT_PUSH_ABBREVIATED_LINE_TO_HISTORY:-0}
# Should abbr-expand push the abbreviation to the shell history? (default false)
typeset -gi ABBR_EXPAND_PUSH_ABBREVIATION_TO_HISTORY=${ABBR_EXPAND_PUSH_ABBREVIATION_TO_HISTORY:-0}
# Limitation: doesn't support the user changing their hist_ignore_space setting interactively
typeset -gi _abbr_hist_ignore_space
_abbr_hist_ignore_space=$options[hist_ignore_space]
# Behave as if `--quiet` was passed? (default false)
typeset -gi ABBR_QUIET=${ABBR_QUIET:-0}
# Behave as if `--quieter` was passed? (default false)
typeset -gi ABBR_QUIETER=${ABBR_QUIETER:-0}
# In expansions, replace the first instance of ABBR_LINE_CURSOR_MARKER with the cursor
typeset -gi ABBR_SET_LINE_CURSOR=${ABBR_SET_LINE_CURSOR:-0}
# In expansions, replace the first instance of ABBR_EXPANSION_CURSOR_MARKER with the cursor
typeset -gi ABBR_SET_EXPANSION_CURSOR=${ABBR_SET_EXPANSION_CURSOR:-0}
# The directory temp files are stored in
typeset -g _abbr_tmpdir=${${ABBR_TMPDIR:-${${TMPDIR:-/tmp}%/}/zsh-abbr}%/}/
if [[ ${(%):-%#} == '#' ]]; then
_abbr_tmpdir=${${ABBR_TMPDIR:-${${TMPDIR:-/tmp}%/}/zsh-abbr-privileged-users}%/}/
fi
# The file abbreviations are stored in
typeset -g ABBR_USER_ABBREVIATIONS_FILE=$ABBR_USER_ABBREVIATIONS_FILE
if [[ -z $ABBR_USER_ABBREVIATIONS_FILE ]]; then
# Legacy support for the zsh-abbr < v5.0.0 default
ABBR_USER_ABBREVIATIONS_FILE=${XDG_CONFIG_HOME:-$HOME/.config}/zsh/abbreviations
if [[ ! -f $ABBR_USER_ABBREVIATIONS_FILE ]]; then
ABBR_USER_ABBREVIATIONS_FILE=${XDG_CONFIG_HOME:-$HOME/.config}/zsh-abbr/user-abbreviations
fi
fi
if [[ ${(t)ABBR_REGULAR_ABBREVIATION_SCALAR_PREFIXES} == ${${(t)ABBR_REGULAR_ABBREVIATION_SCALAR_PREFIXES}#array} ]]; then
typeset -ga ABBR_REGULAR_ABBREVIATION_SCALAR_PREFIXES=( 'sudo ' )
fi
if [[ ${(t)ABBR_REGULAR_ABBREVIATION_GLOB_PREFIXES} == ${${(t)ABBR_REGULAR_ABBREVIATION_GLOB_PREFIXES}#array} ]]; then
typeset -ga ABBR_REGULAR_ABBREVIATION_GLOB_PREFIXES=( ' ' )
fi
# FUNCTIONS
# ---------
abbr() {
emulate -LR zsh
_abbr_debugger
{
local action error_color job_id logs_silent_when_quiet logs_silent_when_quieter \
opt output release_date scope success_color type version warn_color
local -i dry_run force has_error number_opts quiet quieter should_exit
dry_run=$ABBR_DRY_RUN
force=$ABBR_FORCE
number_opts=0
quiet=$ABBR_QUIET
quiet=$(( ABBR_QUIETER || ABBR_QUIET ))
quieter=$ABBR_QUIETER
release_date="December 10 2024"
version="zsh-abbr version 6.0.1"
# Deprecation notices for values that could be meaningfully set after initialization
# Example form:
# (( ${+DEPRECATED_VAL} )) && _abbr_warn_deprecation DEPRECATED_VAL VAL
# VAL=$DEPRECATED_VAL
if (( ABBR_LOADING_USER_ABBREVIATIONS )); then
quiet=1
quieter=1
elif ! _abbr_no_color; then
error_color="$fg[red]"
success_color="$fg[green]"
# @DUPE (nearly) abbr, _abbr_log_available_abbreviation
warn_color="$fg[yellow]"
fi
_abbr:add() {
_abbr_debugger
local abbreviation
local expansion
if [[ $# > 1 ]]; then
_abbr:util_error "abbr add: Expected one argument, got $#: $*"
return
fi
abbreviation=${1%%=*}
expansion=${1#*=}
if ! (( ABBR_LOADING_USER_ABBREVIATIONS )); then
abbreviation=${(q)abbreviation}
expansion=${(q)expansion}
fi
if [[ -z $abbreviation || -z $expansion || $abbreviation == $1 ]]; then
_abbr:util_error "abbr add: Requires abbreviation and expansion"
return
fi
_abbr:util_add $abbreviation $expansion
}
_abbr:git() {
_abbr_debugger
local abbreviation
local expansion
local type_saved
if [[ $# > 1 ]]; then
_abbr:util_error "abbr add: Expected one argument, got $#: $*"
return
fi
abbreviation=${1%%=*}
expansion=${1#*=}
type_saved=$type
type='regular'
_abbr:add ${abbreviation}="git $expansion"
type='global'
_abbr:add "git ${abbreviation}"="git $expansion"
type=$type_saved
}
_abbr:clear_session() {
_abbr_debugger
if [[ $# > 0 ]]; then
_abbr:util_error "abbr clear-session: Unexpected argument"
return
fi
ABBR_REGULAR_SESSION_ABBREVIATIONS=( )
ABBR_GLOBAL_SESSION_ABBREVIATIONS=( )
}
_abbr:erase() {
_abbr_debugger
local abbreviation
local abbreviations_set
local -a abbreviations_sets
local message
local verb_phrase
if [[ $# > 1 ]]; then
_abbr:util_error "abbr erase: Expected one argument"
return
elif [[ $# < 1 ]]; then
_abbr:util_error "abbr erase: Erase needs a variable name"
return
fi
abbreviation=$1
if [[ $scope != 'user' ]]; then
if [[ $type != 'regular' ]]; then
if (( ${+ABBR_GLOBAL_SESSION_ABBREVIATIONS[${(qqq)${(Q)abbreviation}}]} )); then
(( ABBR_DEBUG )) && 'builtin' 'echo' " Found a global session abbreviation"
abbreviations_sets+=( ABBR_GLOBAL_SESSION_ABBREVIATIONS )
fi
fi
if [[ $type != 'global' ]]; then
if (( ${+ABBR_REGULAR_SESSION_ABBREVIATIONS[${(qqq)${(Q)abbreviation}}]} )); then
(( ABBR_DEBUG )) && 'builtin' 'echo' " Found a regular session abbreviation"
abbreviations_sets+=( ABBR_REGULAR_SESSION_ABBREVIATIONS )
fi
fi
fi
if [[ $scope != 'session' ]]; then
if [[ $type != 'regular' ]]; then
if ! (( ABBR_LOADING_USER_ABBREVIATIONS )); then
source ${_abbr_tmpdir}global-user-abbreviations
fi
if (( ${+ABBR_GLOBAL_USER_ABBREVIATIONS[${(qqq)${(Q)abbreviation}}]} )); then
(( ABBR_DEBUG )) && 'builtin' 'echo' " Found a global user abbreviation"
abbreviations_sets+=( ABBR_GLOBAL_USER_ABBREVIATIONS )
fi
fi
if [[ $type != 'global' ]]; then
if ! (( ABBR_LOADING_USER_ABBREVIATIONS )); then
source ${_abbr_tmpdir}regular-user-abbreviations
fi
if (( ${+ABBR_REGULAR_USER_ABBREVIATIONS[${(qqq)${(Q)abbreviation}}]} )); then
(( ABBR_DEBUG )) && 'builtin' 'echo' " Found a regular user abbreviation"
abbreviations_sets+=( ABBR_REGULAR_USER_ABBREVIATIONS )
fi
fi
fi
if ! (( ${#abbreviations_sets} )); then
_abbr:util_error "abbr erase: No${type:+ $type}${scope:+ $scope} abbreviation \`${(Q)abbreviation}\` found"
elif (( ${#abbreviations_sets} == 1 )); then
verb_phrase="Would erase"
if ! (( dry_run )); then
verb_phrase="Erased"
unset "${abbreviations_sets}[${(b)${(qqq)${(Q)abbreviation}}}]" # quotation marks required
if [[ $abbreviations_sets =~ USER ]]; then
_abbr:util_sync_user
fi
fi
_abbr:util_log_unless_quiet "$success_color$verb_phrase$reset_color $(_abbr:util_set_to_typed_scope $abbreviations_sets) \`${(Q)abbreviation}\`"
else
verb_phrase="Did not erase"
(( dry_run )) && verb_phrase="Would not erase"
message="$error_color$verb_phrase$reset_color abbreviation \`${(Q)abbreviation}\`. Please specify one of\\n"
for abbreviations_set in $abbreviations_sets; do
message+=" $(_abbr:util_set_to_typed_scope $abbreviations_set)\\n"
done
_abbr:util_error $message
fi
}
_abbr:expand() {
_abbr_debugger
local expansion
if ! (( $# )); then
_abbr:util_error "abbr expand: requires an argument"
return
fi
expansion=$(_abbr:expansion $*)
_abbr:util_print $expansion
}
_abbr:expansion() {
_abbr_debugger
local abbreviation
local expansion
if ! (( $# )); then
_abbr:util_error "_abbr:expansion requires an argument"
return
fi
abbreviation=$*
expansion=$(_abbr_regular_expansion "$abbreviation")
if [[ ! "$expansion" ]]; then
expansion=$(_abbr_global_expansion "$abbreviation" 1)
fi
if [[ ! "$expansion" ]]; then
_abbr_create_files
source ${_abbr_tmpdir}global-user-abbreviations
expansion=$(_abbr_global_expansion "$abbreviation" 0)
fi
'builtin' 'echo' - $expansion
}
_abbr:export_aliases() {
_abbr_debugger
local type_saved
type_saved=$type
if [[ $# > 0 ]]; then
_abbr:util_error "abbr export-aliases: Unexpected argument"
return
fi
include_expansion=1
session_prefix="alias"
user_prefix="alias"
_abbr:util_list $include_expansion $session_prefix $user_prefix
}
_abbr:import_aliases() {
_abbr_debugger
local alias_to_import
local abbreviation
local expansion
local saved_type
typeset -a aliases_to_import
saved_type=$type
if [[ $# > 0 ]]; then
_abbr:util_error "abbr import-aliases: Unexpected argument"
return
fi
if [[ $saved_type != 'global' ]]; then
aliases_to_import=( ${(f)"$('builtin' 'alias' -r)"} )
for alias_to_import in $aliases_to_import; do
_abbr:util_import_alias $alias_to_import
done
fi
if [[ $saved_type != 'regular' ]]; then
type='global'
aliases_to_import=( ${(f)"$('builtin' 'alias' -g)"} )
for alias_to_import in $aliases_to_import; do
_abbr:util_import_alias $alias_to_import
done
fi
type=$saved_type
}
_abbr:import_fish() {
_abbr_debugger
local abbreviation
local abbreviations
local expansion
local input_file
if [[ $# != 1 ]]; then
_abbr:util_error "abbr import-fish: requires exactly one argument"
return
fi
input_file=$1
abbreviations=( ${(f)"$(<$input_file)"} )
for abbreviation in $abbreviations; do
def=${line#* -- }
abbreviation=${def%% *}
expansion=${def#* }
_abbr:util_add $abbreviation $expansion
done
}
_abbr:import_git_aliases() {
_abbr_debugger
local config_file
local git_alias
local prefix
local -a git_aliases
while (( $# )); do
case $1 in
"--file")
if [[ -z $2 ]]; then
_abbr:util_error "abbr import-git-aliases: --file requires a file path"
return
fi
config_file=$2
shift 2
;;
"--prefix")
if [[ -z $2 ]]; then
_abbr:util_error "abbr import-git-aliases: --prefix requires a prefix string"
return
fi
prefix=$2
shift 2
;;
*)
_abbr:util_error "abbr import-git-aliases: Unexpected argument"
return
esac
done
if [[ -n $config_file ]]; then
if [[ ! -f $config_file ]]; then
_abbr:util_error "abbr import-git-aliases: Config file not found"
return
fi
git_aliases=( ${(ps|\nalias.|)"$(git config --file $config_file --get-regexp '^alias\.')"} )
else
git_aliases=( ${(ps|\nalias.|)"$(git config --get-regexp '^alias\.')"} )
fi
for git_alias in $git_aliases; do
key=${${git_alias%% *}#alias.}
value=${git_alias#* }
if [[ ${value[1]} == '!' ]]; then
verb_phrase="Did not"
((dry_run)) && verb_phrase="Would not"
_abbr:util_warn "$verb_phrase import the Git alias \`$key\` because its expansion is a function"
else
if ! (( ABBR_LOADING_USER_ABBREVIATIONS )); then
key=${(q)key}
value=${(q)value}
fi
_abbr:util_add "$prefix$key" "git $value"
fi
done
}
_abbr:list() {
_abbr_debugger
local -i include_expansion
if [[ $# > 0 ]]; then
_abbr:util_error "abbr list definitions: Unexpected argument"
return
fi
include_expansion=1
_abbr:util_list $include_expansion
}
_abbr:list_abbreviations() {
_abbr_debugger
if [[ $# > 0 ]]; then
_abbr:util_error "abbr list: Unexpected argument"
return
fi
_abbr:util_list
}
_abbr:list_commands() {
_abbr_debugger
local -i include_expansion
local session_prefix
local user_prefix
if [[ $# > 0 ]]; then
_abbr:util_error "abbr list commands: Unexpected argument"
return
fi
include_expansion=1
session_prefix="abbr -S"
user_prefix=abbr
_abbr:util_list $include_expansion $session_prefix $user_prefix
}
_abbr:print_version() {
_abbr_debugger
if [[ $# > 0 ]]; then
_abbr:util_error "abbr version: Unexpected argument"
return
fi
_abbr:util_print $version
}
_abbr:profile() {
_abbr_debugger
local zsh_version
if [[ $# > 0 ]]; then
_abbr:util_error "abbr version: Unexpected argument"
return
fi
zsh_version=$(zsh --version)
_abbr:util_print $version
_abbr:util_print $zsh_version
_abbr:util_print "OSTYPE $OSTYPE"
}
_abbr:rename() {
_abbr_debugger
local err
local expansion
local new
local old
if [[ $# != 2 ]]; then
_abbr:util_error "abbr rename: Requires exactly two arguments"
return
fi
current_abbreviation=$1
new_abbreviation=$2
job_group='_abbr:rename'
expansion=$(_abbr:expansion $current_abbreviation)
if [[ -n $expansion ]]; then
_abbr:util_add $new_abbreviation $expansion
if (( $? )); then
_abbr:util_error "abbr rename: ${type:+$type }${scope:+$scope }abbreviation \`${(Q)current_abbreviation}\` left untouched"
return 1
fi
_abbr:erase $current_abbreviation
else
_abbr:util_error "abbr rename: No${type:+ $type}${scope:+ $scope} abbreviation \`${(Q)current_abbreviation}\` exists"
fi
}
_abbr:util_add() {
_abbr_debugger
local abbreviation
local abbreviations_set
local cmd
local expansion
local existing_expansion
local job_group
local -a success
local typed_scope
local verb_phrase
abbreviation=$1
expansion=$2
success=0
verb_phrase="Added"
(( dry_run )) && verb_phrase="Would add"
if [[ ${abbreviation%=*} != $abbreviation ]]; then
_abbr:util_error "abbr add: ABBREVIATION (\`${(Q)abbreviation}\`) may not contain an equals sign"
return 1
fi
if [[ $scope == 'session' ]]; then
if [[ $type == 'global' ]]; then
abbreviations_set=ABBR_GLOBAL_SESSION_ABBREVIATIONS
else
abbreviations_set=ABBR_REGULAR_SESSION_ABBREVIATIONS
fi
else
if [[ $type == 'global' ]]; then
abbreviations_set=ABBR_GLOBAL_USER_ABBREVIATIONS
if ! (( ABBR_LOADING_USER_ABBREVIATIONS )); then
source ${_abbr_tmpdir}global-user-abbreviations
fi
else
abbreviations_set=ABBR_REGULAR_USER_ABBREVIATIONS
if ! (( ABBR_LOADING_USER_ABBREVIATIONS )); then
source ${_abbr_tmpdir}regular-user-abbreviations
fi
fi
fi
typed_scope=$(_abbr:util_set_to_typed_scope $abbreviations_set)
existing_expansion=${${(P)abbreviations_set}[${(qqq)${(Q)abbreviation}}]}
if [[ -n $existing_expansion ]]; then
if (( ! force )); then
verb_phrase="Did not add"
(( dry_run )) && verb_phrase="Would not add"
_abbr:util_error "$verb_phrase the $typed_scope \`${(Q)abbreviation}\`. It already has an expansion"
return 2
fi
verb_phrase="Redefined"
(( dry_run )) && verb_phrase="Would redefine"
fi
_abbr:util_check_command $abbreviation || return 3
if ! (( dry_run )); then
eval $abbreviations_set'[${(qqq)${(Q)abbreviation}}]=${(qqq)${(Q)expansion}}'
fi
if [[ $scope != 'session' ]]; then
_abbr:util_sync_user
fi
_abbr:util_log_unless_quiet "$success_color$verb_phrase$reset_color the $typed_scope \`${(Q)abbreviation}\`"
}
_abbr:util_alias() {
_abbr_debugger
local abbreviation
local abbreviations_set
local expansion
abbreviations_set=$1
for abbreviation in ${(iko)${(P)abbreviations_set}}; do
expansion=${${(P)abbreviations_set}[$abbreviation]}
alias_definition="alias "
if [[ $type == 'global' ]]; then
alias_definition+="-g "
fi
alias_definition+="$abbreviation='$expansion'"
'builtin' 'print' "$alias_definition"
done
}
_abbr:util_bad_options() {
_abbr_debugger
_abbr:util_error "abbr: Illegal combination of options"
}
_abbr:util_error() {
_abbr_debugger
has_error=1
logs_silent_when_quiet+="${logs_silent_when_quiet:+\\n}$error_color$@$reset_color"
should_exit=1
}
_abbr:util_import_alias() {
local abbreviation
local expansion
abbreviation=${1%%=*}
expansion=${1#*=}
_abbr:util_add $abbreviation "$('builtin' 'echo' $expansion)"
}
_abbr:util_check_command() {
_abbr_debugger
local abbreviation
abbreviation=$1
(( ABBR_LOADING_USER_ABBREVIATIONS )) && return 0
(( force && quieter )) && return 0
# Warn if abbreviation would interfere with system command use, e.g. `cp="git cherry-pick"`
# To add regardless, use --force
if (( $+commands[$abbreviation] && ! $+aliases[$abbreviation] )); then
if (( force )); then
verb_phrase="will now expand"
(( dry_run )) && verb_phrase="would now expand"
_abbr:util_log_unless_quieter "\`${(Q)abbreviation}\` $verb_phrase as an abbreviation"
else
verb_phrase="Did not"
(( dry_run )) && verb_phrase="Would not"
_abbr:util_warn "$verb_phrase add the abbreviation \`${(Q)abbreviation}\` because a command with the same name exists"
return 1
fi
fi
}
_abbr:util_list() {
_abbr_debugger
local abbreviation
local expansion
local -i include_expansion
local session_prefix
local user_prefix
include_expansion=$1
session_prefix=$2
user_prefix=$3
if [[ $scope != 'session' ]]; then
if [[ $type != 'regular' ]]; then
for abbreviation in ${(iko)ABBR_GLOBAL_USER_ABBREVIATIONS}; do
(( include_expansion )) && expansion=${ABBR_GLOBAL_USER_ABBREVIATIONS[$abbreviation]}
_abbr:util_list_item $abbreviation $expansion ${user_prefix:+$user_prefix -g}
done
fi
if [[ $type != 'global' ]]; then
for abbreviation in ${(iko)ABBR_REGULAR_USER_ABBREVIATIONS}; do
(( include_expansion )) && expansion=${ABBR_REGULAR_USER_ABBREVIATIONS[$abbreviation]}
_abbr:util_list_item $abbreviation $expansion $user_prefix
done
fi
fi
if [[ $scope != 'user' ]]; then
if [[ $type != 'regular' ]]; then
for abbreviation in ${(iko)ABBR_GLOBAL_SESSION_ABBREVIATIONS}; do
(( include_expansion )) && expansion=${ABBR_GLOBAL_SESSION_ABBREVIATIONS[$abbreviation]}
_abbr:util_list_item $abbreviation $expansion ${session_prefix:+$session_prefix -g}
done
fi
if [[ $type != 'global' ]]; then
for abbreviation in ${(iko)ABBR_REGULAR_SESSION_ABBREVIATIONS}; do
(( include_expansion )) && expansion=${ABBR_REGULAR_SESSION_ABBREVIATIONS[$abbreviation]}
_abbr:util_list_item $abbreviation $expansion $session_prefix
done
fi
fi
}
_abbr:util_list_item() {
_abbr_debugger
local abbreviation
local expansion
local prefix
abbreviation=$1
expansion=$2
prefix=$3
result=$abbreviation
if [[ $expansion ]]; then
result+="=${(qqq)${(Q)expansion}}"
fi
if [[ $prefix ]]; then
result="$prefix $result"
fi
_abbr:util_print $result
}
_abbr:util_log_unless_quiet() {
_abbr_debugger
logs_silent_when_quiet+="${logs_silent_when_quiet:+\\n}$1"
}
_abbr:util_log_unless_quieter() {
_abbr_debugger
logs_silent_when_quieter+="${logs_silent_when_quieter:+\\n}$1"
}
_abbr:util_print() {
_abbr_debugger
output+="${output:+\\n}$1"
}
_abbr:util_set_once() {
_abbr_debugger
local option value
option=$1
value=$2
if [[ ${(P)option} ]]; then
return
fi
eval $option=$value
((number_opts++))
}
_abbr:util_sync_user() {
_abbr_debugger
(( ABBR_LOADING_USER_ABBREVIATIONS )) && return
local abbreviation
local expansion
local user_updated
user_updated=$(mktemp ${_abbr_tmpdir}regular-user-abbreviations_updated.XXXXXX)
typeset -p ABBR_GLOBAL_USER_ABBREVIATIONS > ${_abbr_tmpdir}global-user-abbreviations
for abbreviation in ${(iko)ABBR_GLOBAL_USER_ABBREVIATIONS}; do
expansion=${ABBR_GLOBAL_USER_ABBREVIATIONS[$abbreviation]}
'builtin' 'echo' "abbr -g $abbreviation=$expansion" >> "$user_updated"
done
typeset -p ABBR_REGULAR_USER_ABBREVIATIONS > ${_abbr_tmpdir}regular-user-abbreviations
for abbreviation in ${(iko)ABBR_REGULAR_USER_ABBREVIATIONS}; do
expansion=${ABBR_REGULAR_USER_ABBREVIATIONS[$abbreviation]}
'builtin' 'echo' "abbr $abbreviation=$expansion" >> $user_updated
done
'command' 'mv' $user_updated $ABBR_USER_ABBREVIATIONS_FILE
}
_abbr:util_set_to_typed_scope() {
_abbr_debugger
local abbreviations_set
abbreviations_set=$1
'builtin' 'echo' ${${${${abbreviations_set:l}%s}#abbr_}//_/ }
}
_abbr:util_usage() {
_abbr_debugger
'command' 'man' abbr 2>/dev/null || 'command' 'man' ${ABBR_SOURCE_PATH}/man/man1/abbr.1
}
_abbr:util_warn() {
_abbr_debugger
logs_silent_when_quiet+="${logs_silent_when_quiet:+\\n}$warn_color$@$reset_color"
}
for opt in "$@"; do
if (( should_exit )); then
return
fi
case $opt in
"add"|\
"a")
_abbr:util_set_once action add
;;
"git"|\
"g")
_abbr:util_set_once action git
;;
"clear-session"|\
"c")
_abbr:util_set_once action clear_session
;;
"--dry-run")
dry_run=1
((number_opts++))
;;
"erase"|\
"e")
_abbr:util_set_once action erase
;;
"expand"|\
"x")
_abbr:util_set_once action expand
;;
"export-aliases")
_abbr:util_set_once action export_aliases
;;
"--force"|\
"-f")
force=1
((number_opts++))
;;
"--global"|\
"-g")
_abbr:util_set_once type global
;;
"help"|\
"--help")
_abbr:util_usage
should_exit=1
;;
"import-aliases")
_abbr:util_set_once action import_aliases
;;
"import-fish")
_abbr:util_set_once action import_fish
;;
"import-git-aliases")
_abbr:util_set_once action import_git_aliases
;;
"list")
_abbr:util_set_once action list
;;
"list-abbreviations"|\
"l")
_abbr:util_set_once action list_abbreviations
;;
"list-commands"|\
"L"|\
"-L")
# -L option is to match the builtin alias's `-L`
_abbr:util_set_once action list_commands
;;
"load")
_abbr_load_user_abbreviations
should_exit=1
;;
"profile")
_abbr:util_set_once action profile
;;
"--quiet"|\
"-q")
quiet=1
((number_opts++))
;;
"--quieter"|\
"-qq")
quiet=1
quieter=1
((number_opts++))
;;
"--regular"|\
"-r")
_abbr:util_set_once type regular
;;
"rename"|\
"R")
_abbr:util_set_once action rename
;;
"--session"|\
"-S")
_abbr:util_set_once scope session
;;
"--user"|\
"-U")
_abbr:util_set_once scope user
;;