-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzfm_viewoptions.zsh
executable file
·1218 lines (1175 loc) · 40.8 KB
/
zfm_viewoptions.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
# Last update: 2013-03-06 14:47
# Part of zfm, contains menu portion
#
# ----------------------------------
# for menu_loop we need to source
source $ZFM_DIR/zfm_menu.zsh
# for vared stty -- but messes with vim !
#stty erase
setopt EXTENDED_GLOB
ZFM_CD_COMMAND=${ZFM_CD_COMMAND:-"pushd"}
# pass in a list of files using a command such as:
# files=$(listdir.pl --file-type *(.m0) | nl)
# Displays a list of files and prompts user for a row number
# Then selects the row and filename
# Rows have columns delimited by tabs
# MENU that comes up on ZFM_MENU_KEY
function view_menu() {
select_menu " == M e n u ==" main_menu_options main_menu_command_hash
if [ $? -ne 0 ]; then
perror "Incorrect option $reply"
fi
# pressing menu key again, repeats last seletion
[[ $reply == $ZFM_MENU_KEY ]] && reply=$view_menu_last_choice
view_menu_last_choice=$reply
}
# this implements a drill-down which employs grep.
# You could call this fuzzy
# in that the pattern is not contiguous, if you press abc it matches "a.*b.*c"
#
# NOTE: currently caller are checking selected_files or just selected_file for one file
# but they should check only one : selected_files. Some new methods only check the latter
# and fail if one file selected. Need to make this local and correct all callers.
#
function fuzzyselectrow() {
local files
files=($@)
[[ $#files -eq 0 ]] && return
_CURSOR=1
typeset -U deleted
deleted=()
selected_file=
selected_files=
#local rows= # try to columnate if more than 24 items, based on tput lines
integer rows=$(tput lines)
(( rows-=2 ))
# should we try printing in 2 columns if items more than $rows
#ZFM_AUTO_COLUMNS=${ZFM_AUTO_COLUMNS:-"1"}
ZFM_TRUNCATE=${ZFM_TRUNCATE:-"-1"}
ff=("${(@f)$(print -rl -- $files)}")
local tot
tot=$#ff
local gpatt="" # grep pattern which user types
local sta fin sortrev
sta=1
clear
while (true)
do
## filter the list on rows (used if more rows than can be viewed
#fin=$#ff
(( fin = sta + rows ))
(( sortrev == 1 )) && {
## reverse sort the list on index order
files=(${(Oa)files})
sortrev=0
}
## we need the count after grep and before sed, sed is only temporary window into grepped data
#viewport=$(print -rl -- $files | grep "$gpatt" )
viewport=("${(@f)$(print -rl -- $files | grep $ic "${gpatt}" )}")
tot=$#viewport
[[ $fin -gt $tot ]] && fin=$tot
viewport=(${viewport[$sta, $fin]})
#viewport=$(print -rl -- $files | grep "$gpatt" | sed -n "$sta,$fin p")
vpa=("${(@f)$(print -rl -- $viewport)}")
local _hv=$#vpa # size of result after grep and sed
#print " No.\t Name"
if [[ $ZFM_AUTO_COLUMNS == "1" && $_hv -gt $rows ]]; then
# split into 2 columns
print -rC2 -- "${(@f)$(print -rl -- $viewport | numbernine | sed "s#$HOME#~#g")}"
else
print -rl -- $viewport | numbernine | sed "s#$HOME#~#g"
fi
# PROMPT prompt
#print -n "Select a row [$sta-$fin/$tot] ? Help, C-c/ENTER ($#deleted/$#vpa)/$gpatt/: "
print -n "Select row/s [$sta-$fin/$tot] ? Help, C-c/ENTER ($#deleted/$tot)/$gpatt/: "
_read_numbers $#vpa
#print
#pdebug "Got: $reply , $ckey"
clear
#
# pressing ENTER selects first item by default
[[ $reply = "ENTER" ]] && {
# typically in cases of directories pressing enter selects #1
if [[ -n "$ZFM_SINGLE_SELECT" ]]; then
reply=1 # in case of auto selection we need to exit with all select XXX
# 2013-03-06 - 14:47 auto-select what the cursor is on
reply=$_CURSOR
#elif [[ $#deleted -eq 0 && $#vpa -eq 1 ]]; then
# user has not selected anything, and there's only one row on screen
# assume he is selecting
elif [[ $#deleted -eq 0 ]]; then
# user has not selected anything and presses enter, assume he selects first
#line="$vpa[1]"
# 2013-03-06 - 14:47 auto-select what the cursor is on
line="$vpa[$_CURSOR]"
# only a physical tab was working, \t etc was not working
# split row with tabs into an array
selected_row=("${(s/ /)line}")
selected_file=$selected_row[-1]
selected_files=( $selected_file ) # 2013-01-31 - 20:53
break
else
# put all selection in selected_files and break
# why are we keeping two arrays here, just keep selected CLEANUP
selected_files=()
for line in $deleted
do
selected_files+=( $line )
done
pdebug "$#selected_files selected"
break
fi
}
[[ $reply == "C-c" || $reply == "C-g" ]] && { selected_file=; selected_files=; break }
[[ -z "$reply" ]] && break
case $reply in
"C-n") reply="DOWN" # 2013-02-01 - 17:07
;;
"C-p") reply="UP"
;;
esac
#
# space selects, sadly this is all a repetition of the main list
# and can go out of sync if we change keys.
#
[[ $reply == "C-SPACE" ]] && {
if [[ -n "$_MARK" ]]; then
for (( i = $_MARK; i <= $_CURSOR; i++ )); do
deleted+=( $vpa[$i] )
done
_MARK=
else
_MARK=$_CURSOR
reply="SPACE" ;
fi
}
[[ $reply == "SPACE" ]] && { reply=$_CURSOR ; (( _CURSOR++ )) }
# check for numeric as some values like "o" can cause abort
if [[ "$reply" == <1-> ]]; then
line="$vpa[$reply]"
# only a physical tab was working, \t etc was not working
# split row with tabs into an array
selected_row=("${(s/ /)line}")
_CURSOR=$reply
selected_file=$selected_row[-1]
if [[ -n "$ZFM_SINGLE_SELECT" ]]; then
# select as a user presses a number and get out
selected_files=( $selected_file ) # 2013-01-31 - 20:53
break # 2012-12-26 - 19:05
else
# accumulate selection
if [[ $deleted[(i)$selected_file] -le $#deleted ]]; then
deleted[$deleted[(i)$selected_file]]=()
pinfo "Removing $selected_file from list - $#deleted remaining"
else
deleted+=($selected_file)
pinfo "Adding $selected_file to list - $#deleted selected. Press ENTER when done"
fi
fi
elif [[ "$reply" == "?" ]]; then
print -rl "Keys are <ENTER> Accept selection and return"
print -rl " <C-c> Cancel (return without selection)"
print -rl " [a-zA-Z] to narrow down search"
print -rl " [1-9] to add to selection/remove"
print -rl " SPACE to select/deselect"
print -rl " $ZFM_MENU_KEY menu"
print -rl " ^ Toggle fuzzy mode"
print -rl " | Toggle 2 columns"
print -rl " = Toggle auto-view"
print -rl " M-n/M-p Scroll List Full Page"
print -rl " C-d/C-b Scroll cursor $M_SCROLL lines"
print -rl " C-w / C-r Reverse List"
print -rl " Up/Down arrows"
pause
else
# Use chars to drill down
# Handling backspace
if [[ "$reply" == "BACKSPACE" || "$reply" == "" ]]; then
if [[ -n "$gpatt" ]]; then
#gpatt=${gpatt[1,-2]}
gpatt[-1]=
## remove .* from end of pattern
#[[ $gpatt[-2,-1] == ".*" ]] && gpatt=${gpatt[1,-3]}
gpatt=${gpatt%.*}
fi
elif [[ "$reply" == '|' ]]; then
if [[ $ZFM_AUTO_COLUMNS == "1" ]]; then
ZFM_AUTO_COLUMNS=
else
ZFM_AUTO_COLUMNS="1"
fi
elif [[ "$reply" == '=' ]]; then
pinfo "Toggling auto viewing of selected files/"
toggle_auto_view
elif [[ "$reply" == $ZFM_MENU_KEY ]]; then
# files with spaces are getting split !!!
menu_loop "Options for filtering list" "reject truncate reject_extn accept filter" "rtxaf"
case $menu_text in
"reject")
print "reject all files matching given pattern"
rejpattern=${rejpattern:-"tmp Trash Backups"}
vared -p "Enter pattern to reject: " rejpattern
rejpattern=${rejpattern:gs/ /|/}
local oldc=$#files
files=("${(@f)$(print -rl -- $ff | egrep -v "($rejpattern)")}")
;;
"truncate")
print "truncates beginning of files to shorten name, toggles "
(( ZFM_TRUNCATE = ZFM_TRUNCATE * -1 ))
#pdebug "truncate value is: $ZFM_TRUNCATE "
;;
"reject_extn")
print "removes files for given extensions (space delim)"
xrejpattern=${xrejpattern:-"~ bak swp o pyo class lib"}
vared -p "Enter extensions to reject: " xrejpattern
xrejpattern=${xrejpattern:gs/ /|/}
files=("${(@f)$(print -rl -- $ff | egrep -v "\.($xrejpattern)$")}")
;;
"accept")
print "only show files for given extensions (space delim) remove others"
accpattern=${accpattern:-""}
vared -p "Enter pattern to accept: " accpattern
accpattern=${accpattern:gs/ /|/}
files=("${(@f)$(print -rl -- $ff | egrep "\.($accpattern)$")}")
;;
"filter")
print "Add a command to filter file list, e.g. head / grep foo/ "
vared -c -p "Enter filter: " cfilter
files=("${(@f)$(print -rl -- $ff | eval "$cfilter" )}")
;;
esac
ff=( $files ) # XXX what if nothign changed above ?
tot=$#ff
elif [[ "$reply" == "^" ]]; then
fuzzy_match_toggle
# remove .*s
gpatt=$(pattern_toggle $gpatt)
elif [[ $reply == "M-n" ]]; then
## scroll list down -- neeeded if more rows than can be seen
(( sta += rows ))
elif [[ $reply == "M-p" ]]; then
(( sta -= $rows ))
(( sta < 0 )) && { sta=0 ; _CURSOR=1 }
elif [[ $reply == "C-d" ]]; then
## scroll list down -- neeeded if more rows than can be seen
(( _CURSOR += M_SCROLL ))
(( _CURSOR > $#vpa )) && {
_CURSOR=$#vpa
(( sta += M_SCROLL ))
}
elif [[ $reply == "C-b" ]]; then
(( _CURSOR -= M_SCROLL ))
(( _CURSOR < 0 )) && {
_CURSOR=1
(( sta -= M_SCROLL ))
}
elif [[ $reply == "UP" ]]; then
## scroll list down -- neeeded if more rows than can be seen
# We should only do this movement if there's more than what's visible.
(( _CURSOR-- ))
(( _CURSOR < 1 )) && _CURSOR=1
elif [[ $reply == "DOWN" ]]; then
(( _CURSOR++ ))
(( _CURSOR > $#vpa )) && {
_CURSOR=$#vpa
(( sta += rows ))
}
elif [[ $reply == "PgDn" ]]; then
if (( _CURSOR < $#vpa )); then
_CURSOR=$#vpa
else
_CURSOR=$#vpa
(( sta += rows ))
fi
elif [[ $reply == "PgUp" ]]; then
if (( _CURSOR == 1 )); then
(( sta -= rows ))
(( sta < 0 )) && sta=0
else
(( _CURSOR = 1 ))
fi
elif [[ $reply == "{" ]]; then
(( _CURSOR = 1 ; sta = 1 ))
elif [[ $reply == "}" ]]; then
(( _CURSOR = 1 ; sta = tot ))
elif [[ $reply == "C-w" || $reply == "C-r" ]]; then
# sort reverse order so first comes closest to prompt
# i chose c-w since C-r not working on my terminal ?? even Alt-x just flashin in
# iterm but okay in Terminal.
let sortrev=1
elif [[ $reply == ":" ]]; then
#_text=" ($#deleted) selected files"
if [[ $#deleted -eq 0 ]]; then
local _file="$vpa[$_CURSOR]"
pinfo "File is $_file"
zfm_cmd $_file
else
pinfo "$#deleted files selected including $deleted[1]"
zfm_cmd $deleted
fi
pause
elif [[ -n "$ckey" ]]; then
## we don't want complex keys added into buffer
elif [[ $#reply -gt 1 ]]; then
## we don't want complex keys added into buffer such as PgDn etc
elif [[ -z "$gpatt" ]]; then
gpatt="$reply"
else
## maybe we should check that reply is only one char
if [[ -z "$ZFM_FUZZY_MATCH_DIR" ]]; then
# contiguous search
gpatt="${gpatt}${reply}"
else
gpatt="${gpatt}.*${reply}"
fi
fi
(( sta > tot - rows )) && (( sta = tot - rows ))
(( sta < 1 )) && (( sta = 1 ))
pdebug "gpattern is $gpatt"
if [[ $#files -eq 0 ]] ; then
perror "No files for $gpatt. Use backspace or try another pattern"
elif [[ $#files -eq 1 ]] ; then
# if there's only one file than accept it, no confirmation and break
# # XXX When does this part come ??? It used to work not now.
if [[ -n $ZFM_NO_CONFIRM ]]; then
selected_row=("${(s/ /)files}")
selected_file=$selected_row[-1]
selected_files=( $selected_file )
break
fi
else
fi
fi
done
M_MESSAGE=
unset _CURSOR
}
#
# recursive listing
#
function recviewoptions() {
M_REC_STRING="**/"
M_ACK_REC_FLAG="-r"
viewoptions
}
# non-recursive listing
function nonrecviewoptions(){
M_REC_STRING=""
M_ACK_REC_FLAG="-n"
viewoptions
}
# various canned listings like today's modified files or recent ones
# ACK
function viewoptions() {
local str=""
menu_loop "Directory views" "today ago recent largest dirs extn oldest substring ack" "tarldxosk"
case $menu_text in
"today")
str="(.m0)"
;;
"ago")
print "Examples : 1 -1 2 -2 -5[1,10] -10[10,20] "
ago=${ago:-1}
vared -p "Modified how many days ago: " ago
str="(.m${ago})"
;;
"recent")
str="(.om[1,15])"
;;
"oldest")
str="(.Om[1,15])"
;;
"largest")
#listdir.pl --file-type *(.OL[1,15])
str="(.OL[1,15])"
;;
"extn" )
print -n "Enter one extension e.g log tmp :"
read extn
files=$(eval "listdir.pl ${M_REC_STRING}*.${extn}(.)" )
$ZFM_FILE_SELECT_FUNCTION $files
;;
"substring" )
print "Filenames containing pattern:"
read patt
files=$(eval "listdir.pl ${M_REC_STRING}*${patt}*(.)")
print ${M_REC_STRING}*${patt}*(.)
listdir.pl ${M_REC_STRING}*${patt}*(.)
print
$ZFM_FILE_SELECT_FUNCTION $files
#[[ -n $ZFM_VERBOSE ]] && echo "file: $selected_file"
;;
"ack" )
zfm_ack
;;
"dirs")
# list dirs under current dir
m_child_dirs
#break
;;
esac
[[ -n "$str" ]] && {
pdebug "listdir.pl --file-type ${M_REC_STRING}*${M_EXCLUDE_PATTERN}$str"
files=$(eval "listdir.pl --file-type ${M_REC_STRING}*${M_EXCLUDE_PATTERN}$str")
#selectmulti $files
$ZFM_FILE_SELECT_FUNCTION $files
[[ -n $ZFM_VERBOSE ]] && print "files: $#selected_files"
}
# i am getting selects from a previous selection, i quit this time
[[ $#selected_files -gt 0 ]] && {
handle_selection "$reply" $selected_files
}
}
# handle multiple selection
# e - use editor to edit
# q don't do anything
# * allow user to enter command
## XXX is this really in use now ?
function handle_selection() {
local reply=$1
shift
selected_files=($@:q)
#selected_files=${selected_files:q}
pdebug "$0 with $reply $#selected_files"
case $reply in
"q")
selected_files=
return
#break
;;
"e"|"v")
eval "$EDITOR $selected_files"
#_files=($PWD/${^selected_files}) # prepend PWD to each element
#execute_hooks "fileopen" $_files
#_files=
execute_hooks "fileopen" $selected_files
last_viewed_files=$selected_files
;;
"z")
local arch="$(date +%Y%m%d_%H%M).tgz"
eval "tar zcvf $arch $selected_files"
ls -l $arch
;;
*)
[[ $#selected_files -gt 0 ]] && {
commandpost=${commandpost:-""}
commandpre=${commandpre:-""}
vared -p "Enter command (e.g. git mv) :" commandpre
[[ -z "$commandpre" ]] && { print "No action." ; return }
vared -p "Enter command to place after filenames (e.g. target) :" commandpost
pdebug "$commandpre $selected_files $commandpost"
eval "$commandpre $selected_files $commandpost"
pause
}
;;
esac
selected_files=
}
##
## take a file list and allow user to select one or more files, and then popup a menu of options
# for those files
# Typically you would execute a command that returns a files list and then hand the list
# to this function so the user can select files and then be sent to fileopt or multi depending
# on how many he selected
#
function handle_files() {
files=($@)
if [[ $#files -gt 0 ]]; then
#files=$( echo $files | xargs ls -t )
fuzzyselectrow $files
[[ -n $selected_files ]] && call_fileoptions
else
perror "No files matching $searchpattern"
fi
}
##
# enter a command and select files from output
# e.g. output of locate command
# select_files_from_cmd_output
function command_select() {
vared -c -p "Enter command to pipe to selectrows: " command
files=("${(@f)$( eval "$command" )}")
M_NO_AUTO=1
handle_files $files
}
function zfm_locate() {
searchpattern=${searchpattern:-""}
vared -p "Filename to 'locate' (enter >= 3 characters): " searchpattern
[[ -z $searchpattern ]] && return 1
#files=$( locate "$searchpattern" | grep -P $searchpattern'[^/]*$' )
#if [[ searchpattern[(i)/] -le $#searchpattern ]]; then
files=$( locate -l 100 -i -0 "$searchpattern" | xargs -0 ls -t )
#else
# grep can't deal with zero byte terminating line, and --null is useless here
# sicne there's no filename, grep only sees input
# not sure how this will deal with spaces in file names
#files=$( locate "$searchpattern" | grep -P $searchpattern'[^/]*$' | xargs ls -t)
#fi
handle_files $files
}
#
# toggle between full-indexing and drill down mode.
# I think full-indexing will be useful in selection mode
#
# XXX i think we have now to set the mode and not just do this.
function full_indexing_toggle() {
#if [[ -z "$M_FULL_INDEXING" ]]; then
if [[ $ZFM_MODE != "HINT" ]]; then
M_FULL_INDEXING=1
zfm_set_mode HINT
else
M_FULL_INDEXING=
[[ $ZFM_PREV_MODE == "HINT" ]] && ZFM_PREV_MODE=$ZFM_DEFAULT_MODE
zfm_set_mode $ZFM_PREV_MODE
fi
export M_FULL_INDEXING
}
function show_hidden_toggle() {
if [[ -z "$M_SHOW_HIDDEN" ]]; then
M_SHOW_HIDDEN=1
setopt GLOB_DOTS
pinfo "set glob_dots"
else
M_SHOW_HIDDEN=
unsetopt GLOB_DOTS
setopt NO_GLOB_DOTS
pinfo "unset glob_dots"
fi
export M_SHOW_HIDDEN
}
function fuzzy_match_toggle() {
if [[ -z "$ZFM_FUZZY_MATCH_DIR" ]]; then
ZFM_FUZZY_MATCH_DIR=1
FPATT='.*'
else
ZFM_FUZZY_MATCH_DIR=
FPATT=
fi
PATT=$(pattern_toggle $PATT)
export ZFM_FUZZY_MATCH_DIR
}
function ignore_case_toggle() {
if [[ -z "$ZFM_IGNORE_CASE" ]]; then
ZFM_IGNORE_CASE=1
else
ZFM_IGNORE_CASE=
fi
export ZFM_IGNORE_CASE
}
function approx_match_toggle() {
if [[ -z "ZFM_APPROX_MATCH" ]]; then
ZFM_APPROX_MATCH=1
else
unset ZFM_APPROX_MATCH
fi
export ZFM_APPROX_MATCH
}
#
# Display selected files with an asterisk or using ANSI colors
# THis is because sometimes colors may not show, or long files can have the ANSI escape
# sequence truncated at end
#
function color_toggle() {
if [[ -z "$ZFM_NO_COLOR" ]]; then
ZFM_NO_COLOR=1
pinfo "Selected files will be displayed in bold"
else
ZFM_NO_COLOR=
pinfo "Selected files will be displayed with a '*'"
fi
export ZFM_NO_COLOR
}
function settingsmenu(){
settings_menu_options=("i) Full Indexing toggle" "c) Case toggle" "h) Hidden files toggle" "p) Paging key" "4) Dupe check" \
"a) Auto select action" "A) Toggle Auto Action" "x) Approximate match toggle" "C) Color toggle" "_) Redefine command")
typeset -A settings_menu_command_hash
settings_menu_command_hash=(
i full_indexing_toggle
c ignore_case_toggle
x approx_match_toggle
h show_hidden_toggle
p change_paging_key
4 toggle_duplicate_check
a define_auto_action
A toggle_auto_view
C color_toggle
_ zfm_change_command
)
select_menu "Options" settings_menu_options settings_menu_command_hash
if [ $? -ne 0 ]; then
perror "Incorrect option $reply"
fi
}
function change_paging_key() {
print "Page key is (default <SPACE>: [$M_PAGE_KEY]"
print -n "Enter key to use for paging (should preferable not exist in filenames): "
read -k cha
M_PAGE_KEY=cha
print "Using page key: $cha"
}
## define actions for various file types, if you don't want to be prompted with a menu
function define_auto_action() {
# specify action with various filetypes
# Misses out on OTHER category, not sure what to do
# but some text files land in there, `file` says "data".
print
print "Type Ctrl-u to clear line"
print "Blank line disables auto action"
print
#
local v=""
print -rl "Choose automatic action when selecting :"
for ff in ${(k)FT_EXTNS} ; do
v=$ZFM_AUTO_ACTION[$ff]
vared -p " $ff file: " v
ZFM_AUTO_ACTION[$ff]=$v
done
}
function toggle_duplicate_check() {
print "When pressing hotkeys 1-9, we check if there are files with numbers in that position"
print "Without this check some numbered files can become inaccessible"
print "If you rarely use this, you can switch it off here, or permanently at top of source"
if [[ -z "$M_SWITCH_OFF_DUPL_CHECK" ]]; then
M_SWITCH_OFF_DUPL_CHECK=1
else
M_SWITCH_OFF_DUPL_CHECK=
fi
export M_SWITCH_OFF_DUPL_CHECK
}
# toggle between automatuc viewing on selection, the other mode
# being that the fileopt menu is opened
function toggle_auto_view(){
if [[ "$ZFM_AUTOVIEW_TOGGLE_KEY" == "1" ]]; then
unset_auto_view
else
set_auto_view
fi
}
function set_auto_view(){
ZFM_AUTOVIEW_TOGGLE_KEY="1"
typeset -Ag ZFM_AUTO_ACTION ZFM_AUTO_ACTION_BAK;
ZFM_AUTO_ACTION=("${(@kv)ZFM_AUTO_ACTION_BAK}")
ZFM_AUTO_ACTION[IMAGE]=${ZFM_AUTO_ACTION_BAK[IMAGE]:-"open"}
#ZFM_AUTO_ACTION[OTHER]=${ZFM_AUTO_ACTION_BAK[OTHER]:-"open"}
ZFM_AUTO_ACTION[TXT]=${ZFM_AUTO_ACTION_BAK[TXT]:-$EDITOR}
ZFM_AUTO_ACTION[ZIP]=${ZFM_AUTO_ACTION_BAK[ZIP]:-"view"}
}
function unset_auto_view(){
ZFM_AUTOVIEW_TOGGLE_KEY=
## store values in bak hash and clear this one
typeset -Ag ZFM_AUTO_ACTION_BAK; ZFM_AUTO_ACTION_BAK=("${(@kv)ZFM_AUTO_ACTION}")
unset ZFM_AUTO_ACTION
#for key in ${(k)ZFM_AUTO_ACTION} ; do
#val=$ZFM_AUTO_ACTION[$key]
#ZFM_AUTO_ACTION_BAK[$key]=$val
#ZFM_AUTO_ACTION[$key]=
#done
}
function filteroptions() {
menu_loop "Filter Options " "Pattern Today Files Dirs Recent Old Large Small Hidden Links Clear Edit" "ptfdrolshLce"
# XXX usage of o or O clashes with sort order and gives error, FIXME
case $menu_text in
"Files")
filterstr="."
;;
"Dirs")
filterstr="/"
;;
"Recent")
filterstr=".om[1,15]"
filterstr=".m-7"
[[ $M_EDIT == 1 ]] && vared -p "Edit modified less than (days) m[Mwhms][+-]n : " filterstr
;;
"Today")
filterstr=".m0"
;;
"Old")
filterstr="Om[1,15]"
filterstr="m+365"
[[ $M_EDIT == 1 ]] && vared -p "Edit older than (days) m[Mwhms][+-]n : " filterstr
;;
"Large")
filterstr="OL[1,15]"
filterstr="Lm+2"
[[ $M_EDIT == 1 ]] && vared -p "Edit Size (>MB) L[kmp][+-]n: " filterstr
;;
"Pattern")
zfm_edit_pattern
;;
"Small")
filterstr="oL[1,15]"
filterstr="L-1024"
[[ $M_EDIT == 1 ]] && vared -p "Edit Size (<bytes): " filterstr
;;
"Hidden")
filterstr="D${filterstr}"
;;
"Links")
filterstr="@"
;;
"Clear")
filterstr="M"
;;
"Edit")
[[ -z $M_EDIT ]] && (( M_EDIT = -1 ))
(( M_EDIT = M_EDIT * -1 ))
pinfo "Toggling edit of some filters such as size and modification time."
pause
;;
esac
filterstr=${filterstr:-M}
ZFM_STRING="${pattern}${M_EXCLUDE_PATTERN}(${MFM_LISTORDER}$filterstr)"
export ZFM_STRING
#param=$(eval "print -rl -- ${pattern}(${MFM_LISTORDER}$filterstr)")
#myopts=("${(@f)$(print -rl -- $param)}")
## adding 2013-02-22 - 01:15 LP
zfm_refresh
export param
}
function sortoptions() {
# LIST list section (think of a better key)
menu_loop "Sort Order" "newest oldest largest smallest name rname dirs clear" "nolsmrdc"
case $menu_text in
"newest")
MFM_LISTORDER="om"
;;
"oldest")
MFM_LISTORDER="Om"
;;
"largest")
MFM_LISTORDER="OL"
;;
"smallest")
MFM_LISTORDER="oL"
;;
"name")
MFM_LISTORDER="on"
;;
"rname")
MFM_LISTORDER="On"
;;
"dirs")
MFM_LISTORDER="/"
;;
"clear")
MFM_LISTORDER=""
;;
esac
ZFM_SORT_ORDER=$menu_text
export ZFM_SORT_ORDER
#param=$(eval "print -rl -- *${MFM_LISTORDER}")
#filterstr=${filterstr:-M}
#ZFM_STRING="${pattern}(${MFM_LISTORDER}$filterstr)"
ZFM_STRING="${pattern}${M_EXCLUDE_PATTERN}(${MFM_LISTORDER}$filterstr)"
export ZFM_STRING
#param=$(eval "print -rl -- ${pattern}(${MFM_LISTORDER}$filterstr)")
## adding 2013-02-22 - 01:15 LP
zfm_refresh
export param
}
# cycle through various views
# This should include long listings
function zfm_views() {
#typeset -A views
#views=(om Newest Om Oldest OL Largest oL smallest)
views=(/ om oa Om OL oL On on)
viewlabels=(Dirs Newest Accessed Oldest Largest Smallest Reverse Name)
viewcount=${viewcount:-0}
let viewcount++
(( viewcount > $#views )) && viewcount=0 # zero so that normal view (default) can be shown
MFM_LISTORDER=$views[$viewcount]
ZFM_SORT_ORDER=$viewlabels[$viewcount]
export ZFM_SORT_ORDER
#param=$(eval "print -rl -- *${MFM_LISTORDER}")
#filterstr=${filterstr:-M}
#ZFM_STRING="${pattern}(${MFM_LISTORDER}$filterstr)"
ZFM_STRING="${pattern}${M_EXCLUDE_PATTERN}(${MFM_LISTORDER}$filterstr)"
export ZFM_STRING
#param=$(eval "print -rl -- ${pattern}(${MFM_LISTORDER}$filterstr)")
## adding 2013-02-22 - 01:15 LP
#
zfm_refresh
export param
}
# give directories from dirs command
function m_dirstack() {
if [[ -x "${ZFM_DIR}/zfmdirs" ]]; then
#files=$(listdir.pl $(${ZFM_DIR}/zfmdirs) | nl)
#files=$(print -rl -- $(${ZFM_DIR}/zfmdirs))
files=("${(@f)$(${ZFM_DIR}/zfmdirs )}") # reqd to prevent wrapping on spaces - ugh
else
# this only works when this file is sourced, otherwise relies on current session
# not what is in your zshrc
pbold "These are directories on internal stack (dirs command)"
files=$(eval "listdir.pl $(dirs)" )
fi
pbold "Recent Directories"
ZFM_SINGLE_SELECT=1 fuzzyselectrow $files
[[ -d $selected_file ]] && {
$ZFM_CD_COMMAND $selected_file
push_pwd
}
}
function m_child_dirs() {
local ff
ff=$(print -rl -- *(/) | wc -l)
[[ $ff -eq 0 ]] && { perror "No child dirs." ; return }
if [[ $ff -gt 0 ]]; then
# only send dir name, not details.
# as of 2013-01-20 - 00:29 this works fine if dirs has spaces in them
files=$(eval "print -rl -- ${M_REC_STRING}*(/)" )
#else
#files=$(eval "listdir.pl --file-type ${M_REC_STRING}*(/)" | nl)
fi
pbold "Directories"
ZFM_SINGLE_SELECT=1 fuzzyselectrow $files
[[ -d $selected_file ]] && {
[[ -n $ZFM_VERBOSE ]] && print "file: $selected_file"
$ZFM_CD_COMMAND $selected_file
push_pwd
}
}
function m_recentfiles() {
# recently edited files
typeset -U files
files=""
if [[ -x "${ZFM_DIR}/zfmfiles" ]]; then
# next line resulted in spaces getting broken into multiple files
#files=( $(${ZFM_DIR}/zfmfiles) )
files=("${(@f)$(${ZFM_DIR}/zfmfiles )}") # reqd to prevent wrapping on spaces - ugh
#pdebug "1 got $#files"
(( $#files < 15 )) && {
# if i don't put N then crashes out if no files for match
files+=( *(.Nom[1,10]) ) # add 10 recent files from current dir if not enough
}
else
perror "No ~/.viminfo file found"
# fuzzy doesn't expect dettails i think - it can but won't be able to color bold any longer
#files=$(listdir.pl *(.m0) ~/.vimrc ~/.zshrc ~/.bashrc ~/.screenrc ~/.tmux.conf)
files=( *(.om[1,10]) ~/.vimrc ~/.zshrc ~/.bashrc ~/.screenrc ~/.tmux.conf)
# if no files for today add recent files here TODO
fi
[[ -n "$files" ]] && {
pbold "Recent files"
if [[ -n "$ZFM_RECENT_MULTI" ]]; then
$ZFM_FILE_SELECT_FUNCTION $files
[[ $#selected_files -gt 0 ]] && {
handle_selection "$reply" $selected_files
}
else
tmpfuzz=$ZFM_FUZZY_MATCH_DIR
# we want a contiguous match, not fuzzy
ZFM_FUZZY_MATCH_DIR=
$ZFM_FILE_SELECT_FUNCTION $files
ZFM_FUZZY_MATCH_DIR=$tmpfuzz
#perror "XXX $#selected_files ,, $selected_file,, $selected_files"
[[ -n $selected_files ]] && call_fileoptions
fi
}
}
##
# zfmcommands has similar program, same name
#
zfm_ack() {
print "List / select Files containing string"
cpattern=${cpattern:-""}
vared -p "Enter pattern to search for: " cpattern
#files=$(eval "listdir.pl $(ack -l $M_ACK_REC_FLAG $cpattern)" | nl)
# somehow with eval only first row was coming through
# maybe due to newlines
pinfo "Using ack -l $M_ACK_REC_FLAG (-n non recursive, -r recursive)"
ack $M_ACK_REC_FLAG "$cpattern"
pause
files=$(ack -l $M_ACK_REC_FLAG "$cpattern")
if [[ $#files -gt 0 ]]; then
## next line fails on spaces in files
# MAYBE listdir needs to be fixed so it can take spaces in files. XXX
#files=$(listdir.pl $(ack -l $M_ACK_REC_FLAG $cpattern))
#files=$(listdir.pl $files)
$ZFM_FILE_SELECT_FUNCTION $files
[[ $#selected_files -gt 0 ]] && {
vim -c /$cpattern $selected_files
selected_files=
}
else
pinfo "No files found containing $cpattern (using ack -l $M_ACK_REC_FLAG)"
pause
fi
}
# this is a retake on select_menu using datastructures, so one may add or modify
# items and hotkeys at startup thru a config file
function select_menu() {
local title="$1"
shift
local moptions
moptions=(${(P)1})
typeset -A myhas
myhas=(${(Pkv)2})
print
print "${COLOR_BOLD}${title}${COLOR_DEFAULT}"
#for o in $moptions
#do
#print " $o"
#done
M_BOLD_FIRST=1
columnate $moptions
while (true); do
print -n "\rSelect :"
#read -k reply
_read_keys
local ret=0
if (( ${+myhas[$reply]} )); then
print
#pdebug found $reply in hash as $myhas[$reply]
$myhas[$reply]
ret=0
break
else
#print
#print $#myhas :: $myhas
ret=1
[[ $reply == "q" || $reply == "" ]] && { ret=0; break }
#perror "$0 nothing found for .$reply."
#for f ( ${(k)myhas}) print ".$f. $myhas[$f]"
fi
done
print
return $ret
}
# take an array that has values with some divider for columns and paste into one array
# lets use newlines as end of one column, we don;t know how many columns are coming in
# so use 50 as default. what of titles ??
#
function columnate() {
local FOO BUFF ctr f ff
FOO=($@)
BUFF=()
local width=25
(( ctr = 0 ))
## loop through array creating a buffer called BUFF which contains
# side by side columns
#
for f in $FOO ; do
## newline signifies next column
if [[ $f == "\n" ]]; then
(( ctr = 0 ))
continue
else
(( ctr++ ))
fi
## we assume the first one in each list is a title and should be bolded
ff=${(r:$width:)f}
if [[ -n $M_BOLD_FIRST ]]; then
if [[ $ctr -eq 1 ]]; then
ff="$fg_bold[white]$ff$reset_color"
fi
fi
BUFF[$ctr]+=$ff
## if we are to boldface, we must first pad then boldface otherwise
# ANSI chars get counted in size but actually don't take up size,