-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
dired-filter.el
1419 lines (1218 loc) · 55.5 KB
/
dired-filter.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
;;; dired-filter.el --- Ibuffer-like filtering for dired -*- lexical-binding: t -*-
;; Copyright (C) 2014-2015 Matúš Goljer
;; Author: Matúš Goljer <matus.goljer@gmail.com>
;; Maintainer: Matúš Goljer <matus.goljer@gmail.com>
;; Keywords: files
;; Version: 0.0.2
;; Created: 14th February 2014
;; Package-Requires: ((dash "2.10.0") (dired-hacks-utils "0.0.1") (f "0.17.0") (cl-lib "0.3") (emacs "24"))
;; URL: https://github.com/Fuco1/dired-hacks
;; 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:
;; Adds much-lacking filtering facilities to dired.
;; Introduction
;; ------------
;; The filtering system is designed after ibuffer: every dired
;; buffer has associated "filter stack" where user can push
;; filters (predicates). These filters are by default
;; logically "anded", meaning, only the files satsifying all the
;; predicates are shown.
;; Some filters take additional input from the user such as part of
;; name, regexp or extension, other filters only use a predefined
;; predicate such as "show only directories" or "omit dot files".
;; In addition, there are two "metafilters", the `or' filter and the
;; `not' filter. These take other filters as arguments and change
;; their logical interpretation. The `or' filter takes the two
;; filters on top of the stack, pops them and pushes a filter that
;; matches files satisfying one or the other (or both) filters. The
;; `not' filter pops the top filter and pushes its logical negation.
;; To enable or disable the filters toggle minor mode
;; `dired-filter-mode'. Toggling this mode preserves the filter
;; stack, so you can use it to quickly hide/unhide files filtered by
;; the current filter setup.
;; All the provided interactive functions are available from
;; `dired-filter-map'. You can customize `dired-filter-prefix' to set
;; a prefix for this map or bind it manually to a prefix of your
;; choice using:
;;
;; (define-key dired-mode-map (kbd "some-key") dired-filter-map)
;; The bindings follow a convention where the filters are mapped on
;; lower-case letters or punctuation, operators are mapped on symbols
;; (such as !, |, * etc.) and group commands are mapped on upper-case
;; letters. The exception to this is `p' which is bound to
;; `dired-filter-pop', which is a very common operation and warrants a
;; quick binding.
;; In addition to filtering, you can also use the same predicates to
;; only mark files without removing the rest. All the filtering
;; functions of the form `dired-filter-by-*' have their marking
;; counterpart `dired-filter-mark-by-*'. These are available from
;; `dired-filter-mark-map'. You can customize
;; `dired-filter-mark-prefix' a prefix for this map or bind it
;; manually to a prefix of your choice using:
;;
;; (define-key dired-mode-map (kbd "some-key") dired-filter-mark-map)
;; The marking operations are not placed on stack, instead, the marks
;; are immediately updated by "OR"-ing them together. To remove marks
;; that would otherwise be selected by a filter, use prefix argument
;; (usually bound to `C-u'). To logically negate the meaning of the
;; filter, you can call the function with a double prefix argument
;; (usually `C-u' `C-u')
;; You can use saved filters to mark files by calling
;; `dired-filter-mark-by-saved-filters'.
;; Stack operations
;; ----------------
;; To remove the filter from the stack, use `dired-filter-pop' or
;; `dired-filter-pop-all'
;; To break a metafilter apart, you can use `dired-filter-decompose'
;; to decompose the parts of the metafilter and push them back to
;; the stack.
;; You can transpose the filters on the top of the stack using
;; `dired-filter-transpose'
;; Built-in filters
;; ----------------
;; Here's a list of built-in filters:
;; * `dired-filter-by-name'
;; * `dired-filter-by-regexp'
;; * `dired-filter-by-extension'
;; * `dired-filter-by-dot-files'
;; * `dired-filter-by-omit'
;; * `dired-filter-by-garbage'
;; * `dired-filter-by-predicate'
;; * `dired-filter-by-file'
;; * `dired-filter-by-directory'
;; * `dired-filter-by-mode'
;; * `dired-filter-by-symlink'
;; * `dired-filter-by-executable'
;; You can see their documentation by calling M-x `describe-function'.
;; Specifically, `dired-filter-by-omit' removes the files that would
;; be removed by `dired-omit-mode', so you should not need to use
;; both---in fact it is discouraged, as it would make the read-in
;; slower.
;; When called with negative prefix argument, some filters can read
;; multiple values. The resulting predicate is often much faster than
;; having the filter repeated with single argument. Read the
;; documentation to learn more about the calling conventions.
;; Currently, these filters support reading multiple arguments:
;; * `dired-filter-by-extension'
;; To define your own filters, you can use the macro
;; `dired-filter-define'. If you define some interesting filter,
;; please consider contributing it to the upstream.
;; Saved filters
;; -------------
;; In addition to the built-in filters and your own custom filters,
;; this package provides an option to save complex compound filters
;; for later use. When you set up a filter stack you would like to
;; save, call `dired-filter-save-filters'. You will be prompted for a
;; name under which this stack will be saved.
;; The saved filter will be added to `dired-filter-saved-filters'
;; variable, which you can also customize via the customize interface
;; or manually add entries with `push' or `add-to-list'. If you use
;; customize, calling `dired-filter-save-filters' will automatically
;; save the new value into your customize file.
;; You can delete saved filters with `dired-filter-delete-saved-filters'.
;; To use a saved filter, you can use either
;; `dired-filter-add-saved-filters' or
;; `dired-filter-load-saved-filters'. The first pushes the saved
;; filter on top of the currently active stack, the second clears
;; current filter stack before loading the saved filter configuration.
;; An example use is to create filters for "logical groups" of files,
;; such as media files, image files or files used when programming in
;; certain environment (for example, show files with .h and .c
;; extensions). Saved filters save you the time of setting up the
;; filters each time you want this specific view.
;; As a concrete example of above, author uses a saved filter "media"
;; with value:
;; (extension "ogg" "flv" "mpg" "avi" "mp4" "mp3")
;; ;; show all files matching any of these extensions
;; Filter groups
;; -------------
;; Furthermore, instead of only filtering the dired buffer by
;; removing lines you are not interested in, you can also group
;; lines together by filters. That is, lines (files,
;; directories...) satisfying a filter will be moved together under
;; a common drawer. This mechanism works in analogy with ibuffer
;; filter groups.
;; The variable `dired-filter-group-saved-groups' contains
;; definitions of filter groups. You can create and save multiple
;; filter groups (views) and switch between them by setting the
;; `dired-filter-group' variable.
;; To enable or disable the filter groups toggle minor mode
;; `dired-filter-group-mode'. Toggling this mode preserves the active
;; filter group so you can use it to quickly group and ungroup the
;; files.
;; Here is a screenshot with an active filter group. Notice that regular
;; filtering works also with filter groups.
;; http://i.imgur.com/qtiDX1c.png
;; Placing the point on the drawer header and hitting `RET' folds it.
;; Hitting `RET' again expands it.
;; http://i.imgur.com/TDUsEKq.png
;; The `dired-filter-group-saved-groups' used in the above screenshot is the following:
;; (("default"
;; ("PDF"
;; (extension . "pdf"))
;; ("LaTeX"
;; (extension "tex" "bib"))
;; ("Org"
;; (extension . "org"))
;; ("Archives"
;; (extension "zip" "rar" "gz" "bz2" "tar"))))
;; Other features
;; --------------
;; You can clone the currently visible dired buffer by calling
;; `dired-filter-clone-filtered-buffer'.
;; See https://github.com/Fuco1/dired-hacks for the entire collection.
;;; Code:
(require 'dired-x)
(require 'dired-aux)
(require 'dired-hacks-utils)
(require 'dash)
(require 'thingatpt)
(require 'cl-lib)
(require 'f)
;; silence the compiler warning
(defvar dired-filter-mode nil)
(defvar dired-filter-marker-char ?\x3FF
"Temporary marker used by Dired-Filter.")
(defvar dired-filter-alist nil
"Definitions of filters.
Entries are of type (name desc body)")
(defgroup dired-filter ()
"Ibuffer-like filtering for `dired'."
:group 'dired-hacks
:prefix "dired-filter-")
(defgroup dired-filter-group ()
"Ibuffer-like filter groups for `dired-filter'."
:group 'dired-filter
:prefix "dired-filter-group")
(define-widget 'dired-filter 'lazy
"A dired filter type."
:tag "Filter"
:type '(choice (cons :tag "Filter expression"
(symbol :tag "Filter")
(sexp :tag "Qualifier"))
(cons :tag "Logical OR of filters" (const or) (repeat dired-filter))
(list :tag "Logical negation of a filter" (const not) dired-filter)
(string :tag "Saved filter stack")))
(define-widget 'dired-filter-saved 'lazy
"A named dired filter."
:tag "Named filter"
:type '(cons
(string :tag "Filter name")
(repeat :tag "Filter stack" dired-filter)))
(define-widget 'dired-filter-drawer 'lazy
"A named dired filter drawer."
:tag "Drawer"
:type '(cons
(string :tag "Drawer name")
(repeat :tag "Filter stack" dired-filter)))
(defcustom dired-filter-stack '((omit))
"Filter stack.
You can customize this variable to change what filters are active
when new dired buffer is created.
The stack is a list of conses where car is the symbol
representing the filter (it's the part of `dired-filter-by-...')
and cdr is current value of its argument, or nil if filter
doesn't take argument.
By default, `dired-filter-by-omit' is active."
:type '(repeat dired-filter)
:group 'dired-filter)
(make-variable-buffer-local 'dired-filter-stack)
(defcustom dired-filter-inherit-filter-stack nil
"When non-nil, subdirectories inherit the filter of the parent directory."
:type 'boolean
:group 'dired-filter)
(defcustom dired-filter-save-with-custom t
"When non-nil, use Custom to save interactively changed variables.
Currently, this only applies to `dired-filter-saved-filters'."
:type 'boolean
:group 'dired-filter)
(defcustom dired-filter-saved-filters nil
"An alist of saved named filter."
:type '(repeat :tag "Saved filters" dired-filter-saved)
:group 'dired-filter)
(defcustom dired-filter-verbose t
"If non-nil, print debug messages."
:type 'boolean
:group 'dired-filter)
(defcustom dired-filter-show-filters t
"If non-nil, show a description of active filters in header line.
If `dired-filter-stack' is nil, no header is shown.
This modifies `header-line-format' by appending
`dired-filter-header-line-format' to it."
:type 'boolean
:group 'dired-filter)
(defcustom dired-filter-header-line-format '((:eval (format "Active filters: %s" (dired-filter--describe-filters))))
"A format expression for dired-filter's header line.
Has the same format as `mode-line-format'."
:type 'sexp
:group 'dired-filter)
(put 'dired-filter-header-line-format 'risky-local-variable t)
(defcustom dired-filter-keep-expanded-subtrees t
"If non-nil, keep expanded subtree headers while filtering.
This setting has any effect only if `dired-subtree' is installed
as well."
:type 'boolean
:group 'dired-filter)
(defcustom dired-filter-revert 'ask
"Should we revert the dired buffer when applying new filter?
The value 'never means never revert the buffer and add the filter
without refreshing the content. This might provide incorrect
results if some previously filtered file should not be filtered
after the change.
The value 'always always reverts the buffer.
The value 'ask will ask if we should revert if the revert
function is non-standard, that is, not `dired-revert'. This
means the dired buffer might come from `find-dired' or similar
operation and the reverting might be costly."
:type '(radio
(const :tag "Never revert automatically." never)
(const :tag "Always revert automatically." always)
(const :tag "Revert automatically only in standard dired buffers, ask otherwise." ask))
:group 'dired-filter)
(defcustom dired-filter-group "default"
"Active filter group.
Can be either a named filter group specified in
`dired-filter-group-saved-groups' or an anonymous filter stack."
:type '(choice
(string :tag "Filter group")
(repeat :tag "Drawers" dired-filter-drawer))
:group 'dired-filter-group)
(make-variable-buffer-local 'dired-filter-group)
(defcustom dired-filter-group-saved-groups '(("default"))
"An alist of saved named filter groups.
A filter group is a list whose car is the name of the filter
group and whose cdr is a list of lists of the form
\(NAME . FILTER-STACK).
Each NAME defines the name of the drawer where files matching
FILTER-STACK are grouped together.
See `dired-filter-stack' for the format of FILTER-STACK."
:type '(repeat
:tag "Filter groups"
(cons
(string :tag "Filter group name")
(repeat :tag "Drawers" dired-filter-drawer)))
:group 'dired-filter-group)
(defface dired-filter-group-header
'((t (:inherit header-line)))
"The face used to highlight pair overlays."
:group 'dired-filter-group)
(defconst dired-filter-group-imenu-generic-expression '("Group" "^ *\\[ \\(.*\\) \\]" 1)
"An alist of menus for accessing locations in documents with imenu.
It is a list of lists of the form
\(MENU-TITLE REGEXP INDEX [FUNCTION] [ARGUMENTS...])
See `imenu-generic-expression' for more information.")
(defun dired-filter-add-group-imenu-generic-expression ()
(add-to-list 'imenu-generic-expression dired-filter-group-imenu-generic-expression))
(defun dired-filter-remove-group-imenu-generic-expression ()
(setq imenu-generic-expression (delete dired-filter-group-imenu-generic-expression imenu-generic-expression)))
;;;###autoload
(defvar dired-filter-map
(let ((map (make-sparse-keymap)))
(define-key map "n" 'dired-filter-by-name)
(define-key map "r" 'dired-filter-by-regexp)
(define-key map "." 'dired-filter-by-extension)
(define-key map "h" 'dired-filter-by-dot-files) ;; hidden-files
(define-key map "o" 'dired-filter-by-omit)
(define-key map "g" 'dired-filter-by-garbage)
(define-key map "e" 'dired-filter-by-predicate)
(define-key map "f" 'dired-filter-by-file)
(define-key map "d" 'dired-filter-by-directory)
(define-key map "m" 'dired-filter-by-mode)
(define-key map "s" 'dired-filter-by-symlink)
(define-key map "x" 'dired-filter-by-executable)
(define-key map "ig" 'dired-filter-by-git-ignored)
(define-key map "|" 'dired-filter-or)
(define-key map "!" 'dired-filter-negate)
(define-key map "*" 'dired-filter-decompose)
(define-key map (kbd "TAB") 'dired-filter-transpose)
(define-key map "p" 'dired-filter-pop)
(define-key map "/" 'dired-filter-pop-all)
(define-key map "S" 'dired-filter-save-filters)
(define-key map "D" 'dired-filter-delete-saved-filters)
(define-key map "A" 'dired-filter-add-saved-filters)
(define-key map "L" 'dired-filter-load-saved-filters)
map)
"Keymap used for filtering files.")
;;;###autoload
(defvar dired-filter-mark-map
(let ((map (make-sparse-keymap)))
(define-key map "n" 'dired-filter-mark-by-name)
(define-key map "r" 'dired-filter-mark-by-regexp)
(define-key map "." 'dired-filter-mark-by-extension)
(define-key map "h" 'dired-filter-mark-by-dot-files) ;; hidden-files
(define-key map "o" 'dired-filter-mark-by-omit)
(define-key map "g" 'dired-filter-mark-by-garbage)
(define-key map "e" 'dired-filter-mark-by-predicate)
(define-key map "f" 'dired-filter-mark-by-file)
(define-key map "d" 'dired-filter-mark-by-directory)
(define-key map "m" 'dired-filter-mark-by-mode)
(define-key map "s" 'dired-filter-mark-by-symlink)
(define-key map "x" 'dired-filter-mark-by-executable)
(define-key map "ig" 'dired-filter-mark-by-git-ignored)
(define-key map "L" 'dired-filter-mark-by-saved-filters)
map)
"Keymap used for marking files.")
(defun dired-filter--set-prefix-key (varname value)
"Set VARNAME to VALUE.
Setter for `dired-filter-prefix' user variable."
(when varname
(set-default varname value))
(when value
(define-key dired-mode-map (read-kbd-macro value) dired-filter-map)))
(defun dired-filter--set-mark-prefix-key (varname value)
"Set VARNAME to VALUE.
Setter for `dired-filter-mark-prefix' user variable."
(when varname
(set-default varname value))
(when value
(define-key dired-mode-map (read-kbd-macro value) dired-filter-mark-map)))
(defcustom dired-filter-prefix "/"
"Prefix key for `dired-filter-map'."
:type '(choice (string :tag "Prefix")
(const :tag "Don't set" nil))
:group 'dired-filter
:set 'dired-filter--set-prefix-key)
(defcustom dired-filter-mark-prefix nil
"Prefix key for `dired-filter-mark-map'."
:type '(choice (string :tag "Prefix")
(const :tag "Don't set" nil))
:group 'dired-filter
:set 'dired-filter--set-mark-prefix-key)
(defvar dired-filter-group-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "<tab>") 'dired-filter-group-forward-drawer)
(define-key map (kbd "<backtab>") 'dired-filter-group-backward-drawer)
map)
"Keymap used in `dired-filter-group-mode'.")
;; internals
(defun dired-filter--push (filter)
"Push FILTER onto the active filter stack."
(push filter dired-filter-stack))
;; TODO: save the filters in better structure to avoid undescriptive `cadddr'
(defun dired-filter--make-filter-1 (stack)
"Translate STACK to a filter form."
(cond
((stringp stack)
`(and ,@(mapcar 'dired-filter--make-filter-1
(or (cdr (assoc stack dired-filter-saved-filters))
(error "Saved filter %s does not exist" stack)))))
((stringp (car stack))
`(and ,@(mapcar 'dired-filter--make-filter-1 (cdr stack))))
((eq (car stack) 'or)
`(or ,@(mapcar 'dired-filter--make-filter-1 (cdr stack))))
((eq (car stack) 'not)
`(not ,@(mapcar 'dired-filter--make-filter-1 (cdr stack))))
(t (let* ((def (assoc (car stack) dired-filter-alist))
(remove (cl-cadddr def))
(qualifier (cond
((eq (car stack) 'predicate)
(let* ((predicate (cdr stack))
(predicate-keywords (mapcar (lambda (sym) (intern (concat ":" (symbol-name sym))))
(-filter 'symbolp (-flatten predicate))))
(keywords (-intersection dired-utils-info-keywords predicate-keywords))
(varlist (mapcar (lambda (keyword)
`(,(intern (substring (symbol-name keyword) 1))
(dired-utils-get-info ,keyword))) keywords)))
`'(let ,varlist
,predicate)))
((eq (car stack) 'omit)
;; special hack for omit filter, to
;; recompute the filter regexp
(dired-omit-regexp))
((eq (car stack) 'git-ignored)
`',(with-temp-buffer
(insert (mapconcat 'f-slash (f-entries ".") "\0"))
(call-process-region (point-min) (point-max) "git" t t nil "check-ignore" "-z" "--stdin")
(split-string (buffer-string) "\0" t)))
((eq (car stack) 'extension)
(if (listp (cdr stack))
(concat "\\." (regexp-opt (-uniq (cdr stack))) "\\'")
(concat "\\." (regexp-quote (cdr stack)) "\\'")))
(t (cdr stack)))))
(if qualifier
`(let ((qualifier ,qualifier))
,(if remove
`(not ,(car (cl-cddddr def)))
(car (cl-cddddr def))))
(if remove
`(not ,(car (cl-cddddr def)))
(car (cl-cddddr def))))))))
(defun dired-filter--make-filter (filter-stack)
"Build the expression that filters the files according to FILTER-STACK.
When this expression evals to non-nil, file is kept in the
listing."
`(and ,@(mapcar 'dired-filter--make-filter-1 filter-stack)))
(defun dired-filter--describe-filters-1 (stack)
"Return a string describing STACK.
STACK is a filter stack with the format of `dired-filter-stack'."
(cond
((stringp stack)
(format "[Saved filter: %s]" stack))
((stringp (car stack))
(format "[Saved filter: %s]" (car stack)))
((eq (car stack) 'or)
(concat "[OR " (mapconcat 'dired-filter--describe-filters-1 (cdr stack) " ") "]"))
((eq (car stack) 'not)
(concat "[NOT " (mapconcat 'dired-filter--describe-filters-1 (cdr stack) " ") "]"))
(t (let* ((def (assoc (car stack) dired-filter-alist))
(desc (cadr def))
(desc-qual (cl-caddr def))
(remove (if (cl-cadddr def) "!" ""))
(qualifier (cdr stack))
(qual-formatted (funcall `(lambda (qualifier) ,desc-qual) qualifier)))
(if qual-formatted
(format "[%s%s: %s]" remove desc qual-formatted)
(format "[%s%s]" remove desc))))))
(defun dired-filter--describe-filters ()
"Return a string describing `dired-filter-stack'."
(mapconcat 'dired-filter--describe-filters-1 dired-filter-stack " "))
(defun dired-filter--apply ()
"Apply the filters and filter groups."
(dired-filter--expunge)
(dired-filter-group--apply
(if (stringp dired-filter-group)
(assoc dired-filter-group dired-filter-group-saved-groups)
(cons "Anonymous" dired-filter-group))))
(defun dired-filter--update ()
"Re-run the filters."
(let ((file-name (dired-utils-get-filename)))
(if (eq revert-buffer-function 'dired-revert)
(revert-buffer)
(if (cond
((eq dired-filter-revert 'never) nil)
((eq dired-filter-revert 'always) t)
((eq dired-filter-revert 'ask)
(not (y-or-n-p "It appears the revert function for this dired buffer is non-standard. Reverting might take a long time.
Do you want to apply the filters without reverting (this might provide incorrect results in some situations)?"))))
(revert-buffer)
(dired-filter--apply)))
(if (and dired-filter-mode
dired-filter-show-filters
dired-filter-stack)
(add-to-list 'header-line-format '("" dired-filter-header-line-format) t)
(setq header-line-format (--remove (equal it '("" dired-filter-header-line-format)) header-line-format)))
(when file-name
(dired-utils-goto-line file-name))))
(defun dired-filter--narow-to-subdir ()
"Narrow to subdir at point."
(let ((beg (progn
(dired-next-subdir 0)
(line-beginning-position)))
(end (progn
(if (dired-next-subdir 1 t)
(line-beginning-position)
(point-max)))))
(narrow-to-region beg end)))
(defun dired-filter--extract-lines (filter)
"Extract all lines in the current directory matching FILTER.
The matched lines are returned as a string."
(save-excursion
(save-restriction
(widen)
(dired-filter--narow-to-subdir)
(goto-char (point-min))
(let* ((buffer-read-only nil)
(filter `(lambda (file-name) ,(dired-filter--make-filter filter)))
(re nil))
(while (not (eobp))
(let ((file-name (dired-utils-get-filename 'no-dir)))
(if (and file-name
(funcall filter file-name))
(push (delete-and-extract-region
(line-beginning-position)
(progn (forward-line 1) (point)))
re)
(forward-line 1))))
(apply 'concat (nreverse re))))))
(defvar dired-filter-group-header-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "RET") 'dired-filter-group-toggle-header)
map)
"Keymap used when over a group header.")
(defun dired-filter-group--make-header (name &optional collapsed)
"Make group header named by NAME.
Optional argument COLLAPSED specifies if the header is collapsed
by default."
(concat (propertize
(concat
" "
(propertize (format "[ %s%s ]" name (if collapsed " ..." ""))
'font-lock-face 'dired-filter-group-header))
'keymap dired-filter-group-header-map
'dired-filter-group-collapsed collapsed
'dired-filter-group-header name)
"\n"))
(defun dired-filter-group--apply (filter-group)
"Apply FILTER-GROUP."
(when (and dired-filter-group-mode
dired-filter-group)
(save-excursion
(save-restriction
(widen)
(when (ignore-errors (dired-next-subdir 0))
(goto-char (point-min))
(let ((inhibit-read-only t))
(while (--when-let (text-property-any (point-min) (point-max) 'font-lock-face 'dired-filter-group-header)
(goto-char it))
(delete-region (line-beginning-position) (1+ (line-end-position))))
(goto-char (point-min))
(let ((next t))
(while next
(let ((name-group-alist nil))
(-when-let ((_ . filter-stacks) filter-group)
(dired-hacks-next-file)
(beginning-of-line)
(--each (reverse filter-stacks)
(-let* (((name . filter-stack) it)
;; TODO: extract only from last line following the last
;; filter group
(group (dired-filter--extract-lines filter-stack)))
(when (/= (length group) 0)
(push (cons name group) name-group-alist))))
(--each name-group-alist
(-let (((name . group) it))
(insert (dired-filter-group--make-header name) group)))
(when (and (text-property-any
(save-excursion (dired-next-subdir 0))
(point-max) 'font-lock-face 'dired-filter-group-header)
(save-excursion (backward-char 1) (dired-hacks-next-file)))
(insert (dired-filter-group--make-header "Default")))))
(setq next (ignore-errors (dired-next-subdir 1))))))
(when (featurep 'dired-details)
(dired-details-delete-overlays)
(dired-details-activate)))))))
(defun dired-filter-group-toggle-header ()
"Collapse or expand a filter group."
(interactive)
(let ((inhibit-read-only t)
(name (save-excursion
(beginning-of-line)
(get-text-property (point) 'dired-filter-group-header)))
(collapsed (save-excursion
(beginning-of-line)
(get-text-property (point) 'dired-filter-group-collapsed)))
(beg (save-excursion
(forward-line 1)
(point)))
(end (save-excursion
(end-of-line)
(min (or (next-single-property-change (point) 'dired-filter-group-header)
(point-max))
(dired-subdir-max)))))
(if collapsed
(alter-text-property
beg end 'invisible
(lambda (prop)
(delete 'dired-filter-group-toggle-header (-list prop))))
(alter-text-property
beg end 'invisible
(lambda (prop)
(cons 'dired-filter-group-toggle-header (-list prop)))))
(save-excursion
(-let [(beg . end) (bounds-of-thing-at-point 'line)] (delete-region beg end))
(insert (dired-filter-group--make-header name (not collapsed))))))
(defun dired-filter-group-forward-drawer (&optional count)
"Move point forward by COUNT drawers."
(interactive "p")
(--dotimes count
(-when-let (next-drawer (progn
(end-of-line)
(or (next-single-property-change (point) 'dired-filter-group-header)
(progn
(goto-char (point-min))
(next-single-property-change (point) 'dired-filter-group-header)))))
(goto-char next-drawer)
(forward-char 2))))
(defun dired-filter-group-backward-drawer (&optional count)
"Move point backward by COUNT drawers."
(interactive "p")
(--dotimes count
(-when-let (previous-drawer (progn
(beginning-of-line)
(or (previous-single-property-change (point) 'dired-filter-group-header)
(progn
(goto-char (point-max))
(previous-single-property-change (point) 'dired-filter-group-header)))))
(goto-char previous-drawer)
(beginning-of-line)
(forward-char 2))))
(defun dired-filter-group-get-groups ()
"Return a hash table mapping drawer name to its files."
(save-excursion
(dired-next-subdir 0)
(let ((groups (make-hash-table :test 'equal)))
(while (dired-hacks-next-file)
(let ((file (dired-utils-get-filename :local))
(group (save-excursion
(dired-filter-group-backward-drawer 1)
(get-text-property (point) 'dired-filter-group-header))))
(puthash group (cons file (gethash group groups nil)) groups)))
(maphash (lambda (k v) (puthash k (nreverse (gethash k groups nil)) groups)) groups)
groups)))
(defvar dired-filter--expanded-dirs nil
"List of expanded subtrees.
This adds support for `dired-subtree' package.")
(defun dired-filter--expunge ()
"Remove the files specified by `dired-filter-stack' from the listing."
(interactive)
(when (and dired-filter-mode
dired-filter-stack)
(let ((filter (dired-filter--make-filter dired-filter-stack))
(dired-marker-char dired-filter-marker-char)
(old-modified-p (buffer-modified-p))
count)
(when dired-filter-verbose (message "Filtering..."))
(if (eval (dired-filter--mark-unmarked filter))
(setq count (dired-do-kill-lines nil (if dired-filter-verbose "Filtered %d line%s." "")))
(when dired-filter-verbose (message "Nothing to filter")))
;; we need to go over all the directories again and remove those
;; without children
(when (and dired-filter-keep-expanded-subtrees
(featurep 'dired-subtree))
(--each dired-filter--expanded-dirs
(save-excursion
(dired-utils-goto-line it)
(when (and (/= (point) (point-max))
(not (dired-subtree--is-expanded-p)))
(dired-kill-line)))))
(set-buffer-modified-p (and old-modified-p
(save-excursion
(goto-char (point-min))
(re-search-forward dired-re-mark nil t))))
count)))
(defun dired-filter--mark-unmarked (filter)
"Mark originally unmarked files according to FILTER.
If a file satisfies a filter, it is not marked. Marked files are
removed when filtering.
Implementation note: when this function is called
`dired-marker-char' is set to a special value so that the regular
marks are preserved during filtering. Files marked by user are
preserved even in case they should have been removed by the
filter"
(if (and dired-filter-keep-expanded-subtrees
(featurep 'dired-subtree))
(progn
(setq dired-filter--expanded-dirs nil)
`(dired-mark-if
(let ((file-name (dired-utils-get-filename 'no-dir)))
(and
file-name
(looking-at " ")
(if (dired-subtree--is-expanded-p)
(progn
(push (dired-get-filename) dired-filter--expanded-dirs)
nil)
t)
(not ,filter)))
nil))
`(dired-mark-if
(let ((file-name (dired-utils-get-filename 'no-dir)))
(and
file-name
(looking-at " ")
(not ,filter)))
nil)))
(defun dired-filter--mark (filter)
"Helper used by `dired-filter-mark-by-' family.
This ORs the current selection with the one specified by selected FILTER.
If prefix argument \\[universal-argument] is used, unmark the matched files instead
\(including any previously marked files).
If prefix argument \\[universal-argument] \\[universal-argument] is used, mark the files that would normally
not be marked, that is, reverse the logical meaning of the filter."
(let* ((remove (cl-cadddr (assoc (car filter) dired-filter-alist)))
(filter (if (equal current-prefix-arg '(16))
`(not ,(dired-filter--make-filter (list filter)))
(dired-filter--make-filter (list filter))))
(filter (if remove `(not ,filter) filter))
(dired-marker-char (if (equal current-prefix-arg '(4)) ?\040 dired-marker-char)))
(eval `(dired-mark-if
(let ((file-name (dired-utils-get-filename 'no-dir)))
(and
file-name
,filter))
nil))))
;; filters & filter definitions
;;;###autoload
(cl-defmacro dired-filter-define (name documentation
(&key
description
(qualifier-description '(identity qualifier))
reader
remove)
&rest body)
"Create a filter NAME.
Files matched by the predicate are kept in the listing.
For filters where the reverse behaviour makes more sense as
default, you can set the `:remove' argument to `t' to flip the
truth value by default. Do not flip the value in the predicate
itself!
DOCUMENTATION is the documentation of the created filter.
BODY should contain forms which will be evaluated to test whether
or not a particular file should be displayed or not. The forms
in BODY will be evaluated with FILE-NAME bound to the file name,
and QUALIFIER bound to the current argument of the filter.
During the evaluation point is at the beginning of line.
:description is a short description of this filter (usually one
or two words).
:reader is a form that is used by `interactive' to read optional
argument. If not specified or nil, the filter does not accept
argument from user.
:qualifier-description is a form to format qualifier for display.
:remove reverses the default matching strategy of the filter."
(declare (indent 2) (doc-string 2))
(let ((fn-name (intern (concat "dired-filter-by-" (symbol-name name))))
(fn-mark-name (intern (concat "dired-filter-mark-by-" (symbol-name name)))))
`(progn
(defun ,fn-name (&optional qualifier)
,(or (and documentation
(if remove
(concat documentation "\n\nBy default, files matched by this filter are /removed/.")
documentation))
"This filter is not documented.")
(interactive (list ,reader))
(dired-filter--push (cons ',name qualifier))
(when dired-filter-verbose
(message "%s" (concat ,(format "Filter by %s added" description)
(--if-let (eval ,qualifier-description)
(format ": %s" it)
""))))
(when (not dired-filter-mode)
(dired-filter-mode 1))
(dired-filter--update))
(defun ,fn-mark-name (&optional qualifier)
,(or (and documentation
(concat (if remove
(concat documentation "\n\nBy default, files matched by this filter are /removed/.")
documentation)
"\n\nThis function only marks the matched files and does not filter the buffer view.
If prefix argument \\[universal-argument] is used, unmark the
matched files instead (including any perviously marked files).
If prefix argument \\[universal-argument] \\[universal-argument] is used, mark the files that would normally
not be marked, that is, reverse the logical meaning of the
filter."))
"This matcher is not documented.")
(interactive (list ,reader))
(dired-filter--mark (cons ',name qualifier))
(when dired-filter-verbose
(let ((qual (--if-let (eval ,qualifier-description)
(format (if (equal current-prefix-arg '(16))
": (NOT %s)"
": %s") it)
"")))
(message "%s" (concat
(if (equal current-prefix-arg '(4)) "Unm" "M")
(if (and (equal qual "")
(equal current-prefix-arg '(16)))
,(format "arked by (NOT %s)" description)
,(format "arked by %s" description))
qual)))))
(push (list ',name ,description ',qualifier-description
,remove ',(if (= (length body) 1)
`,(car body)
`(progn ,@body)))
dired-filter-alist))))
;;;###autoload (autoload 'dired-filter-by-dot-files "dired-filter")
;;;###autoload (autoload 'dired-filter-mark-by-dot-files "dired-filter")
(dired-filter-define dot-files
"Toggle current view to dot-files."
(:description "dot-files"
:reader nil
:remove t)
(string-match-p "^\\." file-name))
;;;###autoload (autoload 'dired-filter-by-name "dired-filter")
;;;###autoload (autoload 'dired-filter-mark-by-name "dired-filter")
(dired-filter-define name
"Toggle current view to files matching QUALIFIER.
The matching uses smart-case convention: match is
case-insensitive if the QUALIFIER does not contain upper-case
letter, otherwise it is case-sensitive."
(:description "name"
:reader (regexp-quote (read-string "Pattern: ")))
(let ((case-fold-search nil))
(if (string-match-p "[A-Z]" qualifier)
(string-match-p qualifier file-name)
(let ((case-fold-search t))
(string-match-p qualifier file-name)))))
;;;###autoload (autoload 'dired-filter-by-regexp "dired-filter")
;;;###autoload (autoload 'dired-filter-mark-by-regexp "dired-filter")
(dired-filter-define regexp
"Toggle current view to files matching QUALIFIER as a regular expression.