forked from politza/tablist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtablist.el
1945 lines (1724 loc) · 65.5 KB
/
tablist.el
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
;;; tablist.el --- Extended tabulated-list-mode -*- lexical-binding: t -*-
;; Copyright (C) 2013, 2014 Andreas Politz
;; Author: Andreas Politz <[email protected]>
;; Keywords: extensions, lisp
;; Package: tablist
;; Version: 1.0
;; Package-Requires: ((emacs "24.3"))
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; This package adds marks and filters to tabulated-list-mode. It
;; also kind of puts a Dired face on tabulated list buffers.
;;
;; It can be used by deriving from tablist-mode and some features by
;; using tablist-minor-mode inside a tabulated-list-mode buffer.
;;
;;; Code:
(require 'cl-lib)
(require 'tabulated-list)
(require 'dired)
(require 'tablist-filter)
;;
;; *Macros
;;
(defmacro tablist-save-marks (&rest body)
"Eval BODY, while preserving all marks."
(let ((marks (make-symbol "marks")))
`(let (,marks)
(save-excursion
(goto-char (point-min))
(while (re-search-forward "^\\([^ ]\\)" nil t)
(push (cons (tabulated-list-get-id)
(tablist-get-mark-state))
,marks)))
(unwind-protect
(progn ,@body)
(save-excursion
(dolist (m ,marks)
(let ((id (pop m)))
(goto-char (point-min))
(while (and id (not (eobp)))
(when (equal id (tabulated-list-get-id))
(tablist-put-mark-state m)
(setq id nil))
(forward-line)))))))))
(defmacro tablist-with-remembering-entry (&rest body)
"Remember where BODY left of and restore previous position.
If the current entry is still visible, move to it. Otherwise move
to the next visible one after it. If that also fails, goto to
the beginning of the buffer. Finally move point to the major
column."
(declare (indent 0) (debug t))
(let ((re (make-symbol "re"))
(id (make-symbol "id"))
(col (make-symbol "col")))
`(let ((,re
(replace-regexp-in-string
"[\t ]+" "[\t ]*" (regexp-quote
(or (thing-at-point 'line) ""))
t t))
(,id (tabulated-list-get-id))
(,col (tablist-current-column)))
(progn
,@body
(let (success pos)
(goto-char (point-min))
(setq pos (point))
(while (and (setq success (re-search-forward ,re nil t))
(> (point) (prog1 pos (setq pos (point))))
(forward-line -1)
(not (equal ,id (tabulated-list-get-id))))
(forward-line))
(unless success
(goto-char (point-min))
(while (and (not (eobp))
(not success))
(if (equal (tabulated-list-get-id) ,id)
(setq success t)
(forward-line))))
(unless (and success (not (invisible-p (point))))
(goto-char (point-min)))
(tablist-skip-invisible-entries)
(tablist-move-to-column
(or ,col (car (tablist-major-columns))))
(dolist (win (get-buffer-window-list))
(set-window-point win (point))))))))
(defmacro tablist-with-filter-displayed (&rest body)
"Display the current filter in the mode while evalling BODY."
(let ((state (make-symbol "state")))
`(let ((,state (tablist-display-filter 'state)))
(tablist-display-filter t)
(unwind-protect
(progn ,@body)
(tablist-display-filter ,state)))))
;;
;; *Mode Maps
;;
(defvar tablist-mode-filter-map
(let ((kmap (make-sparse-keymap)))
(define-key kmap "p" #'tablist-pop-filter)
(define-key kmap "r" #'tablist-push-regexp-filter)
(define-key kmap "=" #'tablist-push-equal-filter)
(define-key kmap "n" #'tablist-push-numeric-filter)
(define-key kmap "!" #'tablist-negate-filter)
(define-key kmap "t" #'tablist-toggle-first-filter-logic)
(define-key kmap "/" #'tablist-display-filter)
(define-key kmap "z" #'tablist-suspend-filter)
(define-key kmap "a" #'tablist-push-named-filter)
(define-key kmap "s" #'tablist-name-current-filter)
(define-key kmap "D" #'tablist-delete-named-filter)
(define-key kmap "d" #'tablist-deconstruct-named-filter)
(define-key kmap "e" #'tablist-edit-filter)
(define-key kmap "C" #'tablist-clear-filter)
kmap))
(defvar tablist-mode-mark-map
(let ((kmap (make-sparse-keymap)))
(define-key kmap "c" #'tablist-change-marks)
(define-key kmap "!" #'tablist-unmark-all-marks)
(define-key kmap "r" #'tablist-mark-items-regexp)
(define-key kmap "n" #'tablist-mark-items-numeric)
(define-key kmap "m" #'tablist-mark-forward)
kmap))
(defvar tablist-mode-regexp-map
(let ((kmap (make-sparse-keymap)))
;; (define-key kmap "&" #'tablist-flag-gargabe-items)
(define-key kmap "m" #'tablist-mark-items-regexp)
kmap))
(defvar tablist-minor-mode-map
(let ((kmap (make-sparse-keymap)))
(define-key kmap "m" #'tablist-mark-forward)
(define-key kmap (kbd "DEL") #'tablist-unmark-backward)
(define-key kmap "k" #'tablist-do-kill-lines)
(define-key kmap "U" #'tablist-unmark-all-marks)
(define-key kmap "u" #'tablist-unmark-forward)
(define-key kmap "t" #'tablist-toggle-marks)
(define-key kmap (kbd "TAB") #'tablist-forward-column)
(define-key kmap "\t" #'tablist-forward-column)
(define-key kmap [backtab] #'tablist-backward-column)
(define-key kmap "%" tablist-mode-regexp-map)
(define-key kmap "*" tablist-mode-mark-map)
(define-key kmap "/" tablist-mode-filter-map)
;; (define-key kmap "e" #'tablist-edit-column)
;; (define-key kmap "i" #'tablist-insert-entry)
(define-key kmap "s" #'tablist-sort)
(define-key kmap [remap back-to-indentation] #'tablist-move-to-major-column)
(define-key kmap [remap next-line] #'tablist-next-line)
(define-key kmap [remap previous-line] #'tablist-previous-line)
(define-key kmap "<" #'tablist-shrink-column)
(define-key kmap ">" #'tablist-enlarge-column)
(define-key kmap "q" #'tablist-quit)
(define-key kmap "G" #'tablist-revert)
(define-key kmap (kbd "C-c C-e") #'tablist-export-csv)
kmap))
(defvar tablist-mode-map
(let ((kmap (copy-keymap tablist-minor-mode-map)))
(set-keymap-parent kmap tabulated-list-mode-map)
(define-key kmap "d" #'tablist-flag-forward)
(define-key kmap (kbd "RET") #'tablist-find-entry)
(define-key kmap "f" #'tablist-find-entry)
;; (define-key kmap "~" #'tablist-flag-gargabe-items)
(define-key kmap "D" #'tablist-do-delete)
;; (define-key kmap "C" #'tablist-do-copy)
;; (define-key kmap "R" #'tablist-do-rename)
(define-key kmap "x" #'tablist-do-flagged-delete)
;; (define-key kmap "F" #'tablist-find-marked-items)
;; (define-key kmap (kbd "C-o") #'tablist-display-item)
kmap))
;;
;; *Variables
;;
;; Marking
(defvar tablist-umark-filtered-entries t)
(defvar tablist-marker-char dired-marker-char
"The character used for marking.")
(defvar tablist-marker-face 'dired-mark
"The face used for the mark character.")
(defvar tablist-marked-face 'dired-marked
"The face used for marked major columns.")
;; Operations
(defvar-local tablist-operations-function nil
"A function for handling operations on the entries.
The function is called with varying number of arguments, while
the first one is always a symbol describing one of the following
operations.
`supported-operations'
This is the only mandatory operation. There are no other
arguments and the function should return a list of symbols of
supported operations.
`delete'
The 2nd argument will be a list of entry ID's. The function
should somehow delete these entries and update
`tabulated-list-entries'.
`find-entry'
The 2nd argument is the ID of an entry. The function should
somehow find/display this entry, i.e. a kind of default
operation.
`edit-column'
The function is called with 3 further arguments: ID, INDEX and
NEW-COLUMN, where ID represents the entry to edit, INDEX is the index
of the column and NEW-COLUMN is the proposed new value for this
column. It should either
i. return a new edited complete entry and update
`tabulated-list-entries', or
ii. throw an error, if NEW-COLUMN is not a valid value for this
column.
`complete'
The function is called with 4 further arguments: ID, INDEX,
STRING and POS, where ID represents an entry, INDEX is the index
of the column to complete, STRING it's current value and POS an
offset of the current position of point into STRING.
The function should return a collection for this column, suitable
as argument for the function `completion-in-region'.")
;; Differentiating columns
(defvar-local tablist-major-columns nil
"Columns used to mark and when querying.")
;; Filter
(defvar-local tablist-current-filter nil)
(defvar-local tablist-filter-suspended nil)
(defvar tablist-named-filter nil)
;; History variables
(defvar tablist-column-name-history nil)
;; Hooks
(defvar tablist-selection-changed-functions nil
"A hook run when ever point moves to a different entry.")
;; Context Window
(defvar-local tablist-context-window nil)
(defvar-local tablist-context-window-function nil)
(defvar tablist-context-window-display-action
`((display-buffer-reuse-window
tablist-display-buffer-split-below-and-attach)
(window-height . 0.25)
(inhibit-same-window . t)))
;;
;; *Setup
;;
(defvar savehist-additional-variables)
(add-hook 'savehist-save-hook
(lambda nil
(add-to-list 'savehist-additional-variables 'tablist-named-filter)))
;;;###autoload
(define-minor-mode tablist-minor-mode
"Minor mode for tablist support."
:group 'tablist
(unless (derived-mode-p 'tabulated-list-mode)
(error "Buffer is not in Tabulated List Mode"))
(tablist-init (not tablist-minor-mode)))
;;;###autoload
(define-derived-mode tablist-mode tabulated-list-mode "TL"
(tablist-init))
(defun tablist-init (&optional disable)
(let ((cleaned-misc (cl-remove 'tablist-current-filter
mode-line-misc-info :key #'car-safe)))
(cond
((not disable)
(set (make-local-variable 'mode-line-misc-info)
(append
(list
(list 'tablist-current-filter
'(:eval (format " [%s]"
(if tablist-filter-suspended
"suspended"
"filtered")))))))
(add-hook 'post-command-hook
'tablist-selection-changed-handler nil t)
(add-hook 'tablist-selection-changed-functions
'tablist-context-window-update nil t))
(t
(setq mode-line-misc-info cleaned-misc)
(remove-hook 'post-command-hook
'tablist-selection-changed-handler t)
(remove-hook 'tablist-selection-changed-functions
'tablist-context-window-update t)))))
(defun tablist-quit ()
(interactive)
(tablist-hide-context-window)
(quit-window))
(defvar-local tablist-selected-id nil)
(defvar tablist-edit-column-minor-mode)
(defun tablist-selection-changed-handler ()
(unless tablist-edit-column-minor-mode
(let ((id tablist-selected-id)
(selected (tabulated-list-get-id)))
(unless (eq selected id)
(setq tablist-selected-id selected)
(run-hook-with-args
'tablist-selection-changed-functions
tablist-selected-id)))))
(defvar tablist-context-window-update--timer nil)
(defun tablist-context-window-update (&optional id)
(when (and tablist-context-window-function
(window-live-p tablist-context-window)
(not tablist-edit-column-minor-mode))
(unless id
(setq id (tabulated-list-get-id)))
(when (timerp tablist-context-window-update--timer)
(cancel-timer tablist-context-window-update--timer))
(setq tablist-context-window-update--timer
(run-with-idle-timer 0.1 nil
(lambda (fn window)
(when (window-live-p window)
(with-selected-window window
(set-window-dedicated-p nil nil)
(save-selected-window
(funcall fn id))
(when (window-live-p (selected-window))
(set-window-dedicated-p nil t)))))
tablist-context-window-function
tablist-context-window))))
(defun tablist-display-context-window ()
(interactive)
(unless tablist-context-window-function
(error "No function for handling a context is defined"))
(unless (window-live-p tablist-context-window)
(setq tablist-context-window
(display-buffer
(current-buffer)
tablist-context-window-display-action)))
(prog1
tablist-context-window
(tablist-context-window-update)))
(defun tablist-hide-context-window ()
(interactive)
(when (window-live-p tablist-context-window)
(let ((ignore-window-parameters t))
(delete-window tablist-context-window)))
(setq tablist-context-window nil))
(defun tablist-toggle-context-window ()
(interactive)
(if (window-live-p tablist-context-window)
(tablist-hide-context-window)
(tablist-display-context-window)))
;;
;; *Marking
;;
(defun tablist-revert ()
"Revert the list with marks preserved, position kept."
(interactive)
(tablist-save-marks
(tablist-with-remembering-entry
(tabulated-list-revert))))
(defun tablist-major-columns ()
(if (null tablist-major-columns)
(number-sequence 0 (1- (length tabulated-list-format)))
(if (numberp tablist-major-columns)
(list tablist-major-columns)
tablist-major-columns)))
(defun tablist-put-mark (&optional pos)
"Put a mark before the entry at POS.
POS defaults to point. Use `tablist-marker-char',
`tablist-marker-face', `tablist-marked-face' and
`tablist-major-columns' to determine how to mark and what to put
a face on."
(when (or (null tabulated-list-padding)
(< tabulated-list-padding 1))
(setq tabulated-list-padding 1)
(tabulated-list-revert))
(save-excursion
(and pos (goto-char pos))
(unless (tabulated-list-get-id)
(error "No entry at this position"))
(let ((inhibit-read-only t))
(tabulated-list-put-tag
(string tablist-marker-char))
(put-text-property
(line-beginning-position)
(1+ (line-beginning-position))
'face tablist-marker-face)
(let ((columns (tablist-column-offsets)))
(dolist (c (tablist-major-columns))
(when (and (>= c 0)
(< c (length columns)))
(let ((beg (+ (line-beginning-position)
(nth c columns)))
(end (if (= c (1- (length columns)))
(line-end-position)
(+ (line-beginning-position)
(nth (1+ c) columns)))))
(cond
((and tablist-marked-face
(not (eq tablist-marker-char ?\s)))
(tablist--save-face-property beg end)
(put-text-property
beg end 'face tablist-marked-face))
(t (tablist--restore-face-property beg end))))))))))
(defun tablist-mark-forward (&optional arg interactive)
"Mark ARG entries forward.
ARG is interpreted as a `prefix-arg'. If INTERACTIVE is non-nil,
maybe use the active region instead of ARG.
See `tablist-put-mark' for how entries are marked."
(interactive (list current-prefix-arg t))
(cond
;; Mark files in the active region.
((and interactive (use-region-p))
(save-excursion
(goto-char (region-beginning))
(beginning-of-line)
(tablist-repeat-over-lines
(1+ (count-lines
(point)
(save-excursion
(goto-char (region-end))
(beginning-of-line)
(point))))
'tablist-put-mark)))
;; Mark the current (or next ARG) files.
(t
(tablist-repeat-over-lines
(prefix-numeric-value arg)
'tablist-put-mark))))
(defun tablist-mark-backward (&optional arg interactive)
"Mark ARG entries backward.
See `tablist-mark-forward'."
(interactive (list current-prefix-arg t))
(tablist-mark-forward (- (prefix-numeric-value arg))
interactive))
(defun tablist-unmark-forward (&optional arg interactive)
"Unmark ARG entries forward.
See `tablist-mark-forward'."
(interactive (list current-prefix-arg t))
(let ((tablist-marker-char ?\s)
tablist-marked-face)
(tablist-mark-forward arg interactive)))
(defun tablist-unmark-backward (&optional arg interactive)
"Unmark ARG entries backward.
See `tablist-mark-forward'."
(interactive (list current-prefix-arg t))
(let ((tablist-marker-char ?\s)
tablist-marked-face)
(tablist-mark-backward arg interactive)))
(defun tablist-flag-forward (&optional arg interactive)
"Flag ARG entries forward.
See `tablist-mark-forward'."
(interactive (list current-prefix-arg t))
(let ((tablist-marker-char ?D)
(tablist-marked-face 'dired-flagged))
(tablist-mark-forward arg interactive)))
(defun tablist-change-marks (old new)
"Change all OLD marks to NEW marks.
OLD and NEW are both characters used to mark files."
(interactive
(let* ((cursor-in-echo-area t)
(old (progn (message "Change (old mark): ") (read-char)))
(new (progn (message "Change %c marks to (new mark): " old)
(read-char))))
(list old new)))
(when (eq new ?\n)
(error "Mark character \\n is not allowed"))
(let ((default-mark-p (equal tablist-marker-char new))
(tablist-marker-char old))
(save-excursion
(tablist-map-over-marks
(lambda nil
(pcase new
(?D
(tablist-flag-forward 1))
(_
(let ((tablist-marker-char new)
(tablist-marked-face
(and default-mark-p
tablist-marked-face)))
(tablist-put-mark)))))))))
(defun tablist-unmark-all-marks (&optional marks interactive)
"Remove all marks in MARKS.
MARKS should be a string of mark characters to match and defaults
to all marks. Interactively, remove all marks, unless a prefix
arg was given, in which case ask about which ones to remove.
Give a message, if INTERACTIVE is non-nil.
Returns the number of unmarked marks."
(interactive
(list (if current-prefix-arg
(read-string "Remove marks: ")) t))
(let ((re (if marks
(tablist-marker-regexp marks)
"^[^ ]"))
(removed 0))
(save-excursion
(goto-char (point-min))
(while (re-search-forward re nil t)
(let ((tablist-marker-char ?\s)
tablist-marker-face
tablist-marked-face)
(tablist-put-mark))
(cl-incf removed)))
(when interactive
(message "Removed %d marks" removed))
removed))
(defun tablist-toggle-marks ()
"Unmark all marked and mark all unmarked entries.
See `tablist-put-mark'."
(interactive)
(let ((marked-re (tablist-marker-regexp))
(not-marked-re
(let ((tablist-marker-char ?\s))
(tablist-marker-regexp))))
(save-excursion
(goto-char (point-min))
(tablist-skip-invisible-entries)
(while (not (eobp))
(cond
((looking-at marked-re)
(save-excursion (tablist-unmark-backward -1)))
((looking-at not-marked-re)
(tablist-put-mark)))
(tablist-forward-entry)))
(tablist-move-to-major-column)))
(defun tablist-get-marked-items (&optional arg distinguish-one-marked)
"Return marked or ARG entries."
(let ((items (save-excursion
(tablist-map-over-marks
(lambda () (cons (tabulated-list-get-id)
(tabulated-list-get-entry)))
arg nil distinguish-one-marked))))
(if (and distinguish-one-marked
(eq (car items) t))
items
(nreverse items))))
(defun tablist-mark-items-regexp (column-name regexp)
"Mark entries matching REGEXP in column COLUMN-NAME."
(interactive
(tablist-read-regexp-filter "Mark" current-prefix-arg ))
(tablist-map-with-filter
'tablist-put-mark
`(=~ ,column-name ,regexp)))
(defun tablist-mark-items-numeric (binop column-name operand)
"Mark items fulfilling BINOP with arg OPERAND in column COLUMN-NAME.
First the column's value is coerced to a number N. Then the test
proceeds as \(BINOP N OPERAND\)."
(interactive
(tablist-read-numeric-filter "Mark" current-prefix-arg))
(tablist-map-with-filter
'tablist-put-mark
`(,binop ,column-name ,operand)))
(defun tablist-map-over-marks (fn &optional arg show-progress
distinguish-one-marked)
(prog1
(cond
((and arg (integerp arg))
(let (results)
(tablist-repeat-over-lines
arg
(lambda ()
(if show-progress (sit-for 0))
(push (funcall fn) results)))
(if (< arg 0)
(nreverse results)
results)))
(arg
;; non-nil, non-integer ARG means use current item:
(tablist-skip-invisible-entries)
(unless (eobp)
(list (funcall fn))))
(t
(cl-labels ((search (re)
(let (success)
(tablist-skip-invisible-entries)
(while (and (setq success
(re-search-forward re nil t))
(invisible-p (point)))
(tablist-forward-entry))
success)))
(let ((regexp (tablist-marker-regexp))
next-position results found)
(save-excursion
(goto-char (point-min))
;; remember position of next marked file before BODY
;; can insert lines before the just found file,
;; confusing us by finding the same marked file again
;; and again and...
(setq next-position (and (search regexp)
(point-marker))
found (not (null next-position)))
(while next-position
(goto-char next-position)
(if show-progress (sit-for 0))
(push (funcall fn) results)
;; move after last match
(goto-char next-position)
(forward-line 1)
(set-marker next-position nil)
(setq next-position (and (search regexp)
(point-marker)))))
(if (and distinguish-one-marked (= (length results) 1))
(setq results (cons t results)))
(if found
results
(unless (or (eobp) (invisible-p (point)))
(list (funcall fn))))))))
(tablist-move-to-major-column)))
(defun tablist-marker-regexp (&optional marks)
"Return a regexp matching marks in MARKS.
MARKS should be a string of mark characters to match and defaults
to the current value of `tablist-marker-char' as a string."
(concat (format "^[%s]"
(or marks (string tablist-marker-char)))))
(defun tablist-get-mark-state ()
"Return the mark state of the entry at point."
(save-excursion
(beginning-of-line)
(when (looking-at "^\\([^ ]\\)")
(let ((mark (buffer-substring
(match-beginning 1)
(match-end 1))))
(tablist-move-to-major-column)
(list (aref mark 0)
(get-text-property 0 'face mark)
(get-text-property (point) 'face))))))
(defun tablist-put-mark-state (state)
"Set the mark of the entry at point according to STATE.
STATE is a return value of `tablist-get-mark-state'."
(cl-destructuring-bind (tablist-marker-char
tablist-marker-face
tablist-marked-face)
state
(tablist-put-mark)))
(defun tablist-mark-prompt (arg items)
"Return a string suitable for use in a tablist prompt."
;; distinguish-one-marked can cause the first element to be just t.
(if (eq (car items) t) (setq items (cdr items)))
(let ((count (length items)))
(if (= count 1)
(car items)
;; more than 1 item:
(if (integerp arg)
;; abs(arg) = count
;; Perhaps this is nicer, but it also takes more screen space:
;;(format "[%s %d items]" (if (> arg 0) "next" "previous")
;; count)
(format "[next %d item%s]"
arg (dired-plural-s arg))
(format "%c [%d item%s]" dired-marker-char count
(dired-plural-s count))))))
;;
;; *Movement
;;
(defun tablist-forward-entry (&optional n)
"Move past the next N unfiltered entries."
(unless n (setq n 1))
(while (and (> n 0)
(not (eobp)))
(forward-line)
(when (invisible-p (point))
(tablist-skip-invisible-entries))
(cl-decf n))
(while (and (< n 0)
(not (bobp)))
(forward-line -1)
(when (invisible-p (point))
(tablist-skip-invisible-entries t))
(cl-incf n))
n)
(defun tablist-next-line (&optional n)
(interactive "p")
(when (and (< n 0)
(save-excursion
(end-of-line 0)
(tablist-skip-invisible-entries t)
(bobp)))
(signal 'beginning-of-buffer nil))
(when (and (> n 0)
(save-excursion
(tablist-forward-entry)
(eobp)))
(signal 'end-of-buffer nil))
(let ((col (tablist-current-column)))
(tablist-forward-entry (or n 1))
(if col
(tablist-move-to-column col)
(tablist-move-to-major-column))))
(defun tablist-previous-line (&optional n)
(interactive "p")
(tablist-next-line (- (or n 1))))
(defun tablist-repeat-over-lines (arg function)
"Call FUNCTION for the next ARG entries."
;; Move out of potentially invisible area.
(tablist-skip-invisible-entries)
(let ((pos (make-marker)))
(while (and (> arg 0)
(not (eobp)))
(cl-decf arg)
(save-excursion
(tablist-forward-entry)
(move-marker pos (1+ (point))))
(unless (eobp)
(save-excursion (funcall function)))
;; Advance to the next line--actually, to the line that *was* next.
;; (If FUNCTION inserted some new lines in between, skip them.)
(goto-char pos))
(while (and (< arg 0) (not (bobp)))
(cl-incf arg)
(tablist-forward-entry -1)
(save-excursion (funcall function)))
(move-marker pos nil)
(tablist-move-to-major-column)))
(defun tablist-move-to-column (n)
"Move to the N'th list column."
(interactive "p")
(when (tabulated-list-get-id)
(let ((columns (tablist-column-offsets)))
(when (or (< n 0)
(>= n (length columns)))
(error "No such column: %s" n))
(beginning-of-line)
(forward-char (nth n columns))
(when (and (plist-get (nthcdr 3 (elt tabulated-list-format n))
:right-align)
(not (= n (1- (length columns)))))
(forward-char (1- (car (cdr (elt tabulated-list-format n)))))))))
(defun tablist-move-to-major-column (&optional first-skip-invisible-p)
"Move to the first major column."
(interactive (list t))
(when first-skip-invisible-p
(tablist-skip-invisible-entries))
(tablist-move-to-column (car (tablist-major-columns))))
(defun tablist-forward-column (n)
"Move N columns forward, while wrapping around."
(interactive "p")
(unless (tabulated-list-get-id)
(error "No entry on this line"))
(let* ((columns (tablist-column-offsets))
(current (1- (length columns))))
;; find current column
(while (and (>= current 0)
(> (nth current columns)
(current-column)))
(cl-decf current))
;; there may be an invisible spec here
(when (bolp)
(forward-char))
;; before any columns
(when (< current 0)
(goto-char (+ (line-beginning-position) (if (> n 0)
(car columns)
(car (last columns)))))
(setq n (* (cl-signum n) (1- (abs n)))))
(when (/= n 0)
(tablist-move-to-column
(mod (+ current n) (length columns))))))
(defun tablist-backward-column (n)
"Move N columns backward, while wrapping around."
(interactive "p")
(tablist-forward-column (- n)))
;;
(defun tablist-skip-invisible-entries (&optional backward)
"Skip invisible entries BACKWARD or forward.
Do nothing, if the entry at point is visible. Otherwise move to
the beginning of the next visible entry in the direction
determined by BACKWARD.
Return t, if point is now in a visible area."
(cond
((and backward
(not (bobp))
(get-text-property (point) 'invisible))
(when (get-text-property (1- (point)) 'invisible)
(goto-char (previous-single-property-change
(point)
'invisible nil (point-min))))
(forward-line -1))
((and (not backward)
(not (eobp))
(get-text-property (point) 'invisible))
(goto-char (next-single-property-change
(point)
'invisible nil (point-max)))))
(not (invisible-p (point))))
;;
;; *Operations
;;
(defun tablist-yes-or-no-p (operation arg items)
"Query the user whether to proceed with some operation.
Operation should be a symbol or string describing the operation,
ARG the `prefix-arg' of the command used in
`tablist-get-marked-items' or elsewhere, to get the ITEMS."
(let ((pp-items (mapcar 'tablist-pretty-print-entry
(mapcar 'cdr items)))
dired-no-confirm
(op-str (upcase-initials
(if (stringp operation)
operation
(symbol-name operation)))))
(dired-mark-pop-up
(format " *%s*" op-str) nil
pp-items
dired-deletion-confirmer
(format "%s %s "
op-str
(tablist-mark-prompt arg pp-items)))))
(defun tablist-operation-available-p (op)
(and (functionp tablist-operations-function)
(memq op (funcall tablist-operations-function
'supported-operations))))
(defun tablist-do-delete (&optional arg)
"Delete ARG entries."
(interactive "P")
(unless (tablist-operation-available-p 'delete)
(error "Deleting entries is not available in this buffer"))
(let ((items (tablist-get-marked-items arg)))
(when (tablist-yes-or-no-p 'delete arg items)
(tablist-do-kill-lines arg)
(funcall tablist-operations-function
'delete (mapcar 'car items))
(tablist-move-to-major-column))))
(defun tablist-do-flagged-delete (&optional interactive)
"Delete all entries marked with a D."
(interactive "p")
(let* ((tablist-marker-char ?D))
(if (save-excursion
(goto-char (point-min))
(re-search-forward (tablist-marker-regexp) nil t))
(tablist-do-delete)
(or (not interactive)
(message "(No deletions requested)")))))
(defun tablist-do-kill-lines (&optional arg interactive)
"Remove ARG lines from the display."
(interactive (list current-prefix-arg t))
(save-excursion
(let ((positions
(tablist-map-over-marks 'point arg))
(inhibit-read-only t))
(dolist (pos positions)
(goto-char pos)
(tabulated-list-delete-entry))
(when interactive
(message (format "Killed %d line%s"
(length positions)
(dired-plural-s (length positions))))))))
(defun tablist-do-operation (arg fn operation &optional delete-p revert-p)
"Operate on marked items.
ARG should be the `current-prefix-arg', FN is a function of two
arguments \(ID ENTRY\) handling the operation. It gets called
repeatedly with all marked items. OPERATION is a symbol or string
describing the operation, it is used for display.
Optional non-nil DELETE-P means, remove the items from the display.
Optional REVERT-P means, revert the display afterwards."
(let ((items (tablist-get-marked-items arg)))
(unless items
(error "No items marked"))
(when (tablist-yes-or-no-p operation arg items)
(when delete-p
(tablist-do-kill-lines arg))
(dolist (item items)
(funcall fn (car item)))
(when revert-p
(tablist-revert))
(tablist-move-to-major-column))))
;;
;; *Editing
;;
(defvar tablist-edit-column-minor-mode-map
(let ((kmap (make-sparse-keymap)))
(set-keymap-parent kmap (current-global-map))
(define-key kmap [remap self-insert-command] #'self-insert-command)
(define-key kmap "\r" #'tablist-edit-column-commit)
(define-key kmap (kbd "C-g") #'tablist-edit-column-quit)
(define-key kmap (kbd "C-c C-c") #'tablist-edit-column-commit)
(define-key kmap (kbd "C-c C-q") #'tablist-edit-column-quit)
(define-key kmap "\t" #'tablist-edit-column-complete)
(define-key kmap (kbd "TAB") #'tablist-edit-column-complete)
(define-key kmap [remap end-of-buffer] #'end-of-line)
(define-key kmap [remap beginning-of-buffer] #'beginning-of-line)
(define-key kmap [remap mark-whole-buffer] #'tablist-edit-column-mark-field)
kmap))
(define-minor-mode tablist-edit-column-minor-mode
"Minor mode for tablist editing column support."
:group 'tablist
(unless (or tablist-minor-mode
(derived-mode-p 'tablist-mode))
(error "Not in a tablist buffer"))
(cond
(tablist-edit-column-minor-mode
(add-to-list 'mode-line-misc-info
'(tablist-edit-column-minor-mode "[edit]"))
(add-hook 'post-command-hook 'tablist-edit-column-constrain-point nil t)