forked from olets/zsh-abbr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zsh-abbr.zsh
executable file
·1076 lines (856 loc) · 28.1 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
# fish shell-like abbreviation management for zsh.
# https://github.com/olets/zsh-abbr
# v3.2
# Copyright (c) 2019-2020 Henry Bley-Vroman
# CONFIGURATION
# -------------
# 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)
ZSH_ABBR_DEFAULT_BINDINGS=${ZSH_ABBR_DEFAULT_BINDINGS=1}
# File abbreviations are stored in
ZSH_ABBR_USER_PATH=${ZSH_ABBR_USER_PATH=${HOME}/.config/zsh/abbreviations}
# FUNCTIONS
# ---------
_zsh_abbr() {
{
local action number_opts opt dry_run release_date scope should_exit \
text_bold text_reset type version
dry_run=${ZSH_ABBR_DRY_RUN:-0}
number_opts=0
release_date="April 7 2020"
text_bold="\\033[1m"
text_reset="\\033[0m"
version="zsh-abbr version 3.2"
function _zsh_abbr:add() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:add"
local abbreviation
local expansion
if [[ $# > 1 ]]; then
_zsh_abbr:util_error " add: Expected one argument, got $#: $*"
return
fi
abbreviation=${1%%=*}
expansion=${1#*=}
if ! (( ZSH_ABBR_INITIALIZING )); then
abbreviation=${(q)abbreviation}
expansion=${(q)expansion}
fi
if ! [[ $abbreviation && $expansion && $abbreviation != $1 ]]; then
_zsh_abbr:util_error " add: Requires abbreviation and expansion"
return
fi
_zsh_abbr:util_add $abbreviation $expansion
}
function _zsh_abbr:clear_session() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:clear_session"
if [[ $# > 0 ]]; then
_zsh_abbr:util_error " clear-session: Unexpected argument"
return
fi
REGULAR_SESSION_ABBREVIATIONS=()
GLOBAL_SESSION_ABBREVIATIONS=()
}
function _zsh_abbr:erase() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:erase"
local abbreviation
local abbreviations_sets
local message
if [[ $# > 1 ]]; then
_zsh_abbr:util_error " erase: Expected one argument"
return
elif [[ $# < 1 ]]; then
_zsh_abbr:util_error " erase: Erase needs a variable name"
return
fi
abbreviation=$1
abbreviations_sets=()
if [[ $scope != 'user' ]]; then
if [[ $type != 'regular' ]]; then
if (( ${+GLOBAL_SESSION_ABBREVIATIONS[$abbreviation]} )); then
(( ZSH_ABBR_DEBUG )) && echo " Found a global session abbreviation"
abbreviations_sets+=( GLOBAL_SESSION_ABBREVIATIONS )
fi
fi
if [[ $type != 'global' ]]; then
if (( ${+REGULAR_SESSION_ABBREVIATIONS[$abbreviation]} )); then
(( ZSH_ABBR_DEBUG )) && echo " Found a regular session abbreviation"
abbreviations_sets+=( REGULAR_SESSION_ABBREVIATIONS )
fi
fi
fi
if [[ $scope != 'session' ]]; then
if [[ $type != 'regular' ]]; then
if ! (( ZSH_ABBR_INITIALIZING )); then
source ${TMPDIR:-/tmp/}zsh-abbr/global-user-abbreviations
fi
if (( ${+GLOBAL_USER_ABBREVIATIONS[$abbreviation]} )); then
(( ZSH_ABBR_DEBUG )) && echo " Found a global user abbreviation"
abbreviations_sets+=( GLOBAL_USER_ABBREVIATIONS )
fi
fi
if [[ $type != 'global' ]]; then
if ! (( ZSH_ABBR_INITIALIZING )); then
source ${TMPDIR:-/tmp/}zsh-abbr/regular-user-abbreviations
fi
if (( ${+REGULAR_USER_ABBREVIATIONS[$abbreviation]} )); then
(( ZSH_ABBR_DEBUG )) && echo " Found a regular user abbreviation"
abbreviations_sets+=( REGULAR_USER_ABBREVIATIONS )
fi
fi
fi
if ! (( ${#abbreviations_sets} )); then
_zsh_abbr:util_error " erase: No ${type:-regular} ${scope:-user} abbreviation \`$abbreviation\` found"
elif [[ ${#abbreviations_sets} == 1 ]]; then
if (( dry_run )); then
echo "Erase ${type:-regular} ${scope:-user} abbreviation \`$abbreviation\`"
else
unset "${abbreviations_sets}[${(b)abbreviation}]" # quotation marks required
if [[ $abbreviations_sets =~ USER ]]; then
_zsh_abbr:util_sync_user
fi
fi
else
message=" erase: Multiple abbreviations \`$abbreviation\`. Please specify one of\\n"
for abbreviations_set in ${abbreviations_sets[@]}; do
message+=" ${${${abbreviations_set:l}//_/ }//abbreviations/}\\n"
done
_zsh_abbr:util_error $message
fi
}
function _zsh_abbr:expand() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:expand"
local abbreviation
local expansion
abbreviation=$1
if [[ $# != 1 ]]; then
echo "expand requires exactly one argument"
return
fi
expansion=$(_zsh_abbr_cmd_expansion "$abbreviation")
if [ ! "$expansion" ]; then
expansion=$(_zsh_abbr_global_expansion "$abbreviation")
fi
echo - "${(Q)expansion}"
}
function _zsh_abbr:export_aliases() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:export_aliases"
local type_saved
local output_path
type_saved=$type
output_path=$1
if [[ $# > 1 ]]; then
_zsh_abbr:util_error " export-aliases: Unexpected argument"
return
fi
include_expansion=1
session_prefix="alias"
user_prefix="alias"
_zsh_abbr:util_list $include_expansion $session_prefix $user_prefix
}
function _zsh_abbr:import_aliases() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:import_aliases"
local _alias
if [[ $# > 0 ]]; then
_zsh_abbr:util_error " import-aliases: Unexpected argument"
return
fi
while read -r _alias; do
_zsh_abbr:add $_alias
done < <(alias -r)
type='global'
while read -r _alias; do
_zsh_abbr:add $_alias
done < <(alias -g)
if ! (( dry_run )); then
echo "Aliases imported."
fi
}
function _zsh_abbr:import_fish() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:import_fish"
local abbreviation
local expansion
local input_file
if [[ $# != 1 ]]; then
echo "expand requires exactly one argument"
return
fi
input_file=$1
while read -r line; do
def=${line#* -- }
abbreviation=${def%% *}
expansion=${def#* }
_zsh_abbr:util_add $abbreviation $expansion
done < $input_file
if ! (( dry_run )); then
echo "Abbreviations imported.\\n"
fi
}
function _zsh_abbr:import_git_aliases() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:import_git_aliases"
local git_aliases
if [[ $# > 0 ]]; then
_zsh_abbr:util_error " import-git-aliases: Unexpected argument"
return
fi
typeset -a git_aliases
while read -r line; do
git_aliases+=($line)
done < <(git config --get-regexp '^alias\.')
for git_alias in $git_aliases; do
key=${${git_alias%% *}#alias.}
value=${git_alias#* }
if [[ ${value[1]} == '!' ]]; then
echo "\\nThe following Git alias was not imported because it is a function:\\n $git_alias\\n"
else
if ! (( ZSH_ABBR_INITIALIZING )); then
abbreviation=${(q)abbreviation}
expansion=${(q)expansion}
fi
type="global"
_zsh_abbr:util_add "g$key" "git $value"
type="regular"
_zsh_abbr:util_add "$key" "git $value"
fi
done
if ! (( dry_run )); then
echo "Git aliases imported."
fi
}
function _zsh_abbr:list() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:list"
if [[ $# > 0 ]]; then
_zsh_abbr:util_error " list: Unexpected argument"
return
fi
_zsh_abbr:util_list
}
function _zsh_abbr:list_commands() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:list_commands"
local include_expansion
local session_prefix
local user_prefix
if [[ $# > 0 ]]; then
_zsh_abbr:util_error " list commands: Unexpected argument"
return
fi
include_expansion=1
session_prefix="abbr -S"
user_prefix=abbr
_zsh_abbr:util_list $include_expansion $session_prefix $user_prefix
}
function _zsh_abbr:list_abbreviations() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:list_abbreviations"
local include_expansion
if [[ $# > 0 ]]; then
_zsh_abbr:util_error " list definitions: Unexpected argument"
return
fi
include_expansion=1
_zsh_abbr:util_list $include_expansion
}
function _zsh_abbr:print_version() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:print_version"
if [[ $# > 0 ]]; then
_zsh_abbr:util_error " version: Unexpected argument"
return
fi
echo $version
}
function _zsh_abbr:rename() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:rename"
local err
local expansion
local new
local old
if [[ $# != 2 ]]; then
_zsh_abbr:util_error " rename: Requires exactly two arguments"
return
fi
current_abbreviation=$1
new_abbreviation=$2
job_group='_zsh_abbr:rename'
if [[ $scope == 'session' ]]; then
if [[ $type == 'global' ]]; then
expansion=${GLOBAL_SESSION_ABBREVIATIONS[$current_abbreviation]}
else
expansion=${REGULAR_SESSION_ABBREVIATIONS[$current_abbreviation]}
fi
else
if [[ $type == 'global' ]]; then
expansion=${GLOBAL_USER_ABBREVIATIONS[$current_abbreviation]}
else
expansion=${REGULAR_USER_ABBREVIATIONS[$current_abbreviation]}
fi
fi
if [ $expansion ]; then
_zsh_abbr:util_add $new_abbreviation $expansion
_zsh_abbr:erase $current_abbreviation
else
_zsh_abbr:util_error " rename: No ${type:-regular} ${scope:-user} abbreviation $current_abbreviation exists"
fi
}
function _zsh_abbr:util_add() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:util_add"
local abbreviation
local expansion
local job_group
local success
abbreviation=$1
expansion=$2
success=0
if [[ ${(w)#abbreviation} > 1 ]]; then
_zsh_abbr:util_error " add: ABBREVIATION (\`$abbreviation\`) must be only one word"
return
fi
if [[ ${abbreviation%=*} != $abbreviation ]]; then
_zsh_abbr:util_error " add: ABBREVIATION (\`$abbreviation\`) may not contain an equals sign"
fi
if [[ $scope == 'session' ]]; then
if [[ $type == 'global' ]]; then
if ! (( ${+GLOBAL_SESSION_ABBREVIATIONS[$abbreviation]} )); then
if (( dry_run )); then
echo "abbr -S -g $abbreviation=${(Q)expansion}"
else
GLOBAL_SESSION_ABBREVIATIONS[$abbreviation]=$expansion
fi
success=1
fi
elif ! (( ${+REGULAR_SESSION_ABBREVIATIONS[$abbreviation]} )); then
if (( dry_run )); then
echo "abbr -S $abbreviation=${(Q)expansion}"
else
REGULAR_SESSION_ABBREVIATIONS[$abbreviation]=$expansion
fi
success=1
fi
else
if [[ $type == 'global' ]]; then
if ! (( ZSH_ABBR_INITIALIZING )); then
source ${TMPDIR:-/tmp/}zsh-abbr/global-user-abbreviations
fi
if ! (( ${+GLOBAL_USER_ABBREVIATIONS[$abbreviation]} )); then
if (( dry_run )); then
echo "abbr -g $abbreviation=${(Q)expansion}"
else
GLOBAL_USER_ABBREVIATIONS[$abbreviation]=$expansion
_zsh_abbr:util_sync_user
fi
success=1
fi
else
if ! (( ZSH_ABBR_INITIALIZING )); then
source ${TMPDIR:-/tmp/}zsh-abbr/regular-user-abbreviations
fi
if ! (( ${+REGULAR_USER_ABBREVIATIONS[$abbreviation]} )); then
if (( dry_run )); then
echo "abbr ${(Q)abbreviation}=${(Q)expansion}"
else
REGULAR_USER_ABBREVIATIONS[$abbreviation]=$expansion
_zsh_abbr:util_sync_user
fi
success=1
fi
fi
fi
if ! (( success )); then
message="A ${type:-regular} ${scope:-user} abbreviation \`$abbreviation\` already exists"
if (( importing )); then
echo "$message"
else
_zsh_abbr:util_error " add: $message"
fi
fi
}
_zsh_abbr:util_alias() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:util_alias"
local abbreviation
local abbreviations_set
local expansion
local output_path
abbreviations_set=$1
output_path=$2
for abbreviation in ${(iko)${(P)abbreviations_set}}; do
alias_definition="alias "
expansion=${${(P)abbreviations_set}[$abbreviation]}
if [[ $type == 'global' ]]; then
alias_definition+="-g "
fi
alias_definition+="$abbreviation='$expansion'"
if [ $output_path ]; then
echo "$alias_definition" >> "$output_path"
else
print "$alias_definition"
fi
done
}
function _zsh_abbr:util_bad_options() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:util_bad_options"
_zsh_abbr:util_error ": Illegal combination of options"
}
function _zsh_abbr:util_error() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:util_error"
echo "abbr$@"
echo "For help run abbr --help"
should_exit=1
}
function _zsh_abbr:util_list() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:util_list"
local abbreviation
local expansion
local 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)GLOBAL_USER_ABBREVIATIONS}; do
expansion=${include_expansion:+${GLOBAL_USER_ABBREVIATIONS[$abbreviation]}}
_zsh_abbr:util_list_item $abbreviation $expansion ${user_prefix:+$user_prefix -g}
done
fi
if [[ $type != 'global' ]]; then
for abbreviation in ${(iko)REGULAR_USER_ABBREVIATIONS}; do
expansion=${include_expansion:+${REGULAR_USER_ABBREVIATIONS[$abbreviation]}}
_zsh_abbr:util_list_item $abbreviation $expansion $user_prefix
done
fi
fi
if [[ $scope != 'user' ]]; then
if [[ $type != 'regular' ]]; then
for abbreviation in ${(iko)GLOBAL_SESSION_ABBREVIATIONS}; do
expansion=${include_expansion:+${GLOBAL_SESSION_ABBREVIATIONS[$abbreviation]}}
_zsh_abbr:util_list_item $abbreviation $expansion ${session_prefix:+$session_prefix -g}
done
fi
if [[ $type != 'global' ]]; then
for abbreviation in ${(iko)REGULAR_SESSION_ABBREVIATIONS}; do
expansion=${include_expansion:+${REGULAR_SESSION_ABBREVIATIONS[$abbreviation]}}
_zsh_abbr:util_list_item $abbreviation $expansion $session_prefix
done
fi
fi
}
function _zsh_abbr:util_list_item() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:util_list_item"
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
echo $result
}
function _zsh_abbr:util_set_once() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:util_set_once"
local option value
option=$1
value=$2
(( ZSH_ABBR_DEBUG )) && echo "util_set_once $option $value"
if [ ${(P)option} ]; then
_zsh_abbr:util_bad_options
return
fi
eval $option=$value
((number_opts++))
}
function _zsh_abbr:util_sync_user() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:util_sync_user"
(( ZSH_ABBR_INITIALIZING )) && return
local abbreviation
local expansion
local user_updated
user_updated=$(mktemp ${TMPDIR:-/tmp/}zsh-abbr/regular-user-abbreviations_updated.XXXXXX)
typeset -p GLOBAL_USER_ABBREVIATIONS > ${TMPDIR:-/tmp/}zsh-abbr/global-user-abbreviations
for abbreviation in ${(iko)GLOBAL_USER_ABBREVIATIONS}; do
expansion=${GLOBAL_USER_ABBREVIATIONS[$abbreviation]}
echo "abbr -g ${abbreviation}=${(qqq)${(Q)expansion}}" >> "$user_updated"
done
typeset -p REGULAR_USER_ABBREVIATIONS > ${TMPDIR:-/tmp/}zsh-abbr/regular-user-abbreviations
for abbreviation in ${(iko)REGULAR_USER_ABBREVIATIONS}; do
expansion=${REGULAR_USER_ABBREVIATIONS[$abbreviation]}
echo "abbr ${abbreviation}=${(qqq)${(Q)expansion}}" >> $user_updated
done
mv $user_updated $ZSH_ABBR_USER_PATH
}
function _zsh_abbr:util_usage() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr:util_usage"
man abbr 2>/dev/null || cat ${ZSH_ABBR_SOURCE_PATH}/man/abbr.txt | less -F
}
for opt in "$@"; do
if (( should_exit )); then
return
fi
case "$opt" in
"--add"|\
"-a")
_zsh_abbr:util_set_once "action" "add"
;;
"--clear-session"|\
"-c")
_zsh_abbr:util_set_once "action" "clear_session"
;;
"--dry-run")
dry_run=1
((number_opts++))
;;
"--erase"|\
"-e")
_zsh_abbr:util_set_once "action" "erase"
;;
"--expand"|\
"-x")
_zsh_abbr:util_set_once "action" "expand"
;;
"--export-aliases")
_zsh_abbr:util_set_once "action" "export_aliases"
;;
"--global"|\
"-g")
_zsh_abbr:util_set_once "type" "global"
;;
"--help"|\
"-h")
_zsh_abbr:util_usage
should_exit=1
;;
"--import-aliases")
_zsh_abbr:util_set_once "action" "import_aliases"
importing=1
;;
"--import-fish")
_zsh_abbr:util_set_once "action" "import_fish"
importing=1
;;
"--import-git-aliases")
_zsh_abbr:util_set_once "action" "import_git_aliases"
importing=1
;;
"--list")
_zsh_abbr:util_set_once "action" "list"
;;
"--list-abbreviations"|\
"-l")
_zsh_abbr:util_set_once "action" "list_abbreviations"
;;
"--list-commands"|\
"-L"|\
"--show"|\
"-s") # "show" is for backwards compatability with v2
_zsh_abbr:util_set_once "action" "list_commands"
;;
"--regular"|\
"-r")
_zsh_abbr:util_set_once "type" "regular"
;;
"--rename"|\
"-R")
_zsh_abbr:util_set_once "action" "rename"
;;
"--session"|\
"-S")
_zsh_abbr:util_set_once "scope" "session"
;;
"--user"|\
"-U")
_zsh_abbr:util_set_once "scope" "user"
;;
"--version"|\
"-v")
_zsh_abbr:util_set_once "action" "print_version"
;;
"--")
((number_opts++))
break
;;
esac
done
if ! (( should_exit )); then
shift $number_opts
if ! (( ZSH_ABBR_INITIALIZING )) && [[ $scope != 'session' ]]; then
job=$(_zsh_abbr_job_name)
_zsh_abbr_job_push $job $action
fi
if [ $action ]; then
_zsh_abbr:$action $@
elif [[ $# > 0 ]]; then
# default if arguments are provided
_zsh_abbr:add $@
else
# default if no argument is provided
_zsh_abbr:list_abbreviations $@
fi
fi
if ! (( ZSH_ABBR_INITIALIZING )); then
_zsh_abbr_job_pop $job
fi
} always {
unfunction -m _zsh_abbr:add
unfunction -m _zsh_abbr:clear_session
unfunction -m _zsh_abbr:erase
unfunction -m _zsh_abbr:expand
unfunction -m _zsh_abbr:export_aliases
unfunction -m _zsh_abbr:import_aliases
unfunction -m _zsh_abbr:import_fish
unfunction -m _zsh_abbr:import_git_aliases
unfunction -m _zsh_abbr:list
unfunction -m _zsh_abbr:list_commands
unfunction -m _zsh_abbr:list_abbreviations
unfunction -m _zsh_abbr:print_version
unfunction -m _zsh_abbr:rename
unfunction -m _zsh_abbr:util_bad_options
unfunction -m _zsh_abbr:util_add
unfunction -m _zsh_abbr:util_alias
unfunction -m _zsh_abbr:util_error
unfunction -m _zsh_abbr:util_list
unfunction -m _zsh_abbr:util_list_item
unfunction -m _zsh_abbr:util_set_once
unfunction -m _zsh_abbr:util_sync_user
unfunction -m _zsh_abbr:util_usage
}
}
_zsh_abbr_bind_widgets() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr_bind_widgets"
# spacebar expands abbreviations
zle -N _zsh_abbr_expand_and_space
bindkey " " _zsh_abbr_expand_and_space
# control-spacebar is a normal space
bindkey "^ " magic-space
# when running an incremental search,
# spacebar behaves normally and control-space expands abbreviations
bindkey -M isearch "^ " _zsh_abbr_expand_and_space
bindkey -M isearch " " magic-space
# enter key expands and accepts abbreviations
zle -N _zsh_abbr_expand_and_accept
bindkey "^M" _zsh_abbr_expand_and_accept
}
_zsh_abbr_cmd_expansion() {
# cannout support debug message
local abbreviation
local expansion
abbreviation=$1
expansion=${REGULAR_SESSION_ABBREVIATIONS[$abbreviation]}
if [ ! $expansion ]; then
source ${TMPDIR:-/tmp/}zsh-abbr/regular-user-abbreviations
expansion=${REGULAR_USER_ABBREVIATIONS[$abbreviation]}
fi
echo - $expansion
}
_zsh_abbr_expand_and_accept() {
# do not support debug message
local trailing_space
trailing_space=${LBUFFER##*[^[:IFSSPACE:]]}
if [[ -z $trailing_space ]]; then
zle _zsh_abbr_expand_widget
fi
zle accept-line
}
_zsh_abbr_expand_and_space() {
# do not support debug message
zle _zsh_abbr_expand_widget
zle self-insert
}
_zsh_abbr_global_expansion() {
# cannout support debug message
local abbreviation
local expansion
abbreviation=$1
expansion=${GLOBAL_SESSION_ABBREVIATIONS[$abbreviation]}
if [ ! $expansion ]; then
source ${TMPDIR:-/tmp/}zsh-abbr/global-user-abbreviations
expansion=${GLOBAL_USER_ABBREVIATIONS[$abbreviation]}
fi
echo - $expansion
}
_zsh_abbr_init() {
{
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr_init"
local job
local shwordsplit_on
job=$(_zsh_abbr_job_name)
shwordsplit_on=0
typeset -gA REGULAR_USER_ABBREVIATIONS
typeset -gA GLOBAL_USER_ABBREVIATIONS
typeset -gA REGULAR_SESSION_ABBREVIATIONS
typeset -gA GLOBAL_SESSION_ABBREVIATIONS
REGULAR_SESSION_ABBREVIATIONS=()
GLOBAL_SESSION_ABBREVIATIONS=()
REGULAR_USER_ABBREVIATIONS=()
GLOBAL_USER_ABBREVIATIONS=()
function _zsh_abbr_init:create() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr_init:create"
if ! [ -d ${TMPDIR:-/tmp/}zsh-abbr ]; then
mkdir -p ${TMPDIR:-/tmp/}zsh-abbr
fi
if ! [ -f ${TMPDIR:-/tmp/}zsh-abbr/regular-user-abbreviations ]; then
touch ${TMPDIR:-/tmp/}zsh-abbr/regular-user-abbreviations
fi
if ! [ -f ${TMPDIR:-/tmp/}zsh-abbr/global-user-abbreviations ]; then
touch ${TMPDIR:-/tmp/}zsh-abbr/global-user-abbreviations
fi
# clean up deprecated temp files
if [ -d ${TMPDIR:-/tmp/}zsh-abbr-jobs ]; then
rm -rf ${TMPDIR:-/tmp/}zsh-abbr-jobs 2> /dev/null
fi
if [ -f ${TMPDIR:-/tmp/}zsh-user-global-abbreviations ]; then
rm ${TMPDIR:-/tmp/}zsh-user-global-abbreviations 2> /dev/null
fi
if [ -f ${TMPDIR:-/tmp/}zsh-user-abbreviations ]; then
rm ${TMPDIR:-/tmp/}zsh-user-abbreviations 2> /dev/null
fi
if [ -f ${TMPDIR:-/tmp/}zsh-abbr-initializing ]; then
rm ${TMPDIR:-/tmp/}zsh-abbr-initializing
fi
if [ -f ${TMPDIR:-/tmp/}abbr_universals ]; then
rm ${TMPDIR:-/tmp/}abbr_universals
fi
}
function _zsh_abbr_init:seed() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr_init:seed"
local arguments
local program
if [[ $options[shwordsplit] = on ]]; then
shwordsplit_on=1
fi
# Load saved user abbreviations
if [ -f $ZSH_ABBR_USER_PATH ]; then
unsetopt shwordsplit
while read -r line; do
program="${line%% *}"
arguments="${line#* }"
# Only execute abbr commands
if [[ $program == "abbr" && $program != $line ]]; then
abbr ${(z)arguments}
fi
done < $ZSH_ABBR_USER_PATH
if (( shwordsplit_on )); then
setopt shwordsplit
fi
unset shwordsplit_on
else
mkdir -p ${ZSH_ABBR_USER_PATH:A:h}
touch $ZSH_ABBR_USER_PATH
fi
typeset -p REGULAR_USER_ABBREVIATIONS > ${TMPDIR:-/tmp/}zsh-abbr/regular-user-abbreviations
typeset -p GLOBAL_USER_ABBREVIATIONS > ${TMPDIR:-/tmp/}zsh-abbr/global-user-abbreviations
}
ZSH_ABBR_INITIALIZING=1
_zsh_abbr_job_push $job initialization
_zsh_abbr_init:create
_zsh_abbr_init:seed
_zsh_abbr_job_pop $job
ZSH_ABBR_INITIALIZING=0
} always {
unfunction -m _zsh_abbr_init:create
unfunction -m _zsh_abbr_init:seed
}
}
_zsh_abbr_job_push() {
{
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr_job_push"
local next_job
local next_job_age
local next_job_path
local job_description
local job_dir
local job_id
local job_path
local timeout_age
job_id=${(q)1}
job_description=$2
job_dir=${TMPDIR:-/tmp/}zsh-abbr/jobs
job_path=$job_dir/$job_id
timeout_age=30 # seconds
function _zsh_abbr_job_push:add_job() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr_job_push:add_job"
if ! [ -d $job_dir ]; then
mkdir -p $job_dir
fi
echo $job_description > $job_path
}
function _zsh_abbr_job_push:next_job_id() {
# cannout support debug message
ls -t $job_dir | tail -1
}
function _zsh_abbr_job_push:handle_timeout() {
(( ZSH_ABBR_DEBUG )) && echo "_zsh_abbr_job_push:handle_timeout"
next_job_path=$job_dir/$next_job
echo "abbr: A job added at $(strftime '%T %b %d %Y' ${next_job%.*}) has timed out."
echo "The job was related to $(cat $next_job_path)."
echo "This could be the result of manually terminating an zsh-abbr activity, for example during session startup."
echo "If you believe it reflects a zsh-abbr bug, please report it at https://github.com/olets/zsh-abbr/issues/new"
echo
rm $next_job_path &>/dev/null
}
function _zsh_abbr_job_push:wait_turn() {
while [[ $(_zsh_abbr_job_push:next_job_id) != $job_id ]]; do
next_job=$(_zsh_abbr_job_push:next_job_id)
next_job_age=$(( $(date +%s) - ${next_job%.*} ))
if (( $next_job_age > $timeout_age )); then
_zsh_abbr_job_push:handle_timeout
fi