File: dired-quick-sort.el

package info (click to toggle)
dired-quick-sort 0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 412 kB
  • sloc: lisp: 152; makefile: 2
file content (242 lines) | stat: -rw-r--r-- 11,167 bytes parent folder | download
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
;;; dired-quick-sort.el --- Persistent quick sorting of dired buffers in various ways. -*- lexical-binding: t; -*-

;; Copyright (C) 2016--2025 Hong Xu <hong@topbug.net>

;; Author: Hong Xu <hong@topbug.net>
;; URL: https://gitlab.com/xuhdev/dired-quick-sort#dired-quick-sort
;; Version: 0.3
;; Package-Requires: ((hydra "0.13.0") (emacs "24"))
;; Keywords: convenience, files

;; This file is not part of GNU Emacs

;; 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 provides ways to quickly sort dired buffers in various ways.
;; With `savehist-mode' enabled (strongly recommended), the last used sorting
;; criteria are automatically used when sorting, even after restarting Emacs.  A
;; hydra is defined to conveniently change sorting criteria.
;;
;; For a quick setup, Add the following configuration to your "~/.emacs" or
;; "~/.emacs.d/init.el":
;;
;;     (require 'dired-quick-sort)
;;     (dired-quick-sort-setup)
;;
;; This will bind "S" in dired-mode to invoke the quick sort hydra and new Dired
;; buffers are automatically sorted according to the setup in this package.  See
;; the document of `dired-quick-sort-setup` if you need a different setup.  It
;; is recommended that at least "-l" should be put into
;; `dired-listing-switches'.  If used with dired+, you may want to set
;; `diredp-hide-details-initially-flag' to nil.
;;
;; To make full use of this extensions, please make sure that the variable
;; `insert-directory-program' points to the GNU version of ls.
;;
;; To comment, ask questions, report bugs or make feature requests, please open
;; a new ticket at the issue tracker
;; <https://gitlab.com/xuhdev/dired-quick-sort/issues>. To contribute, please
;; create a merge request at
;; <https://gitlab.com/xuhdev/dired-quick-sort/merge_requests>.

;;; Code:

(require 'dired)
(require 'ls-lisp)
(require 'savehist)
(require 'hydra)

(defcustom dired-quick-sort-suppress-setup-warning nil
  "How to handle the warning in `dired-quick-sort-setup'."
  :type '(choice (const :tag "Display" nil)
                 (const :tag "Suppress" t)
                 (const :tag "Display as a message" 'message))
  :group 'dired-quick-sort)

(defvar dired-quick-sort-sort-by-last "version"
  "The main sort criterion used last time.

The value should be one of none, time, size, version (i.e., natural, an improved
version of name and extension).

See the documentation of the \"--sort\" option of GNU ls for details.")
(add-to-list 'savehist-additional-variables 'dired-quick-sort-sort-by-last)
(defvar dired-quick-sort-reverse-last ?n
  "Whether reversing was enabled when sorting was used last time.

The value should be either ?y or ?n.")
(add-to-list 'savehist-additional-variables 'dired-quick-sort-reverse-last)
(defvar dired-quick-sort-group-directories-last ?n
  "Whether directories are grouped together when sorting was used last time.

The value should either be ?y or ?n.")
(add-to-list 'savehist-additional-variables
             'dired-quick-sort-group-directories-last)
(defvar dired-quick-sort-time-last "default"
  "The time option used last time.

The value should be one of default (modified time), atime, access, use, ctime or
status.  If the sort-by option is set as \"time\", the specified time will be
used as the key for sorting.

See the documentation of the \"--time\" option of GNU ls for details.")
(add-to-list 'savehist-additional-variables 'dired-quick-sort-time-last)

;;;###autoload
(defun dired-quick-sort (&optional sort-by reverse group-directories time)
  "Sort dired by the given criteria.

The possible values of SORT-BY, REVERSE, GROUP-DIRECTORIES and TIME are
explained in the variable `dired-quick-sort-reverse-last',
`dired-quick-sort-reverse-last', `dired-quick-sort-group-directories-last' and
`dired-quick-sort-time-last' respectively.  Besides, passing nil to any of these
arguments to use the value used last time (that is, the values of the four
variables mentioned before), even after restarting Emacs if `savehist-mode' is
enabled.  When invoked interactively, nil's are passed to all arguments."
  (interactive)
  (setq dired-quick-sort-sort-by-last (or sort-by dired-quick-sort-sort-by-last)
        dired-quick-sort-reverse-last (or reverse dired-quick-sort-reverse-last)
        dired-quick-sort-group-directories-last
        (or group-directories dired-quick-sort-group-directories-last)
        dired-quick-sort-time-last (or time dired-quick-sort-time-last))
  (dired-sort-other (dired-quick-sort--format-switches)))

(defun dired-quick-sort-set-switches ()
  "Set switches according to variables. For use in `dired-mode-hook'."
  (unless dired-sort-inhibit
    (dired-sort-other (dired-quick-sort--format-switches) t)))

(defun dired-quick-sort--format-switches ()
  "Return a dired-listing-switches string according to
`dired-quick-sort' settings."
  (format "%s %s %s %s %s" dired-listing-switches
          (if (string= dired-quick-sort-sort-by-last "default")
              ""
            (concat "--sort=" dired-quick-sort-sort-by-last))
          (if (char-equal dired-quick-sort-reverse-last ?y)
              "-r" "")
          (if (char-equal dired-quick-sort-group-directories-last ?y)
              "--group-directories-first" "")
          (if (not (string= dired-quick-sort-time-last "default"))
              (concat "--time=" dired-quick-sort-time-last) "")))

(defun dired-quick-sort--sort-by-last (field)
  (if (string= dired-quick-sort-sort-by-last field) "[X]" "[ ]"))

(defhydra hydra-dired-quick-sort (:hint none :color pink)
  "
^Sort by^                   ^Reverse^               ^Group Directories^            ^Time
^^^^^^^^^----------------------------------------------------------------------------------------------------------------
_n_: ?n? none               _r_: ?r? yes            _g_: ?g? yes                   _d_: ?d? default (last modified time)
_t_: ?t? time               _R_: ?R? no             _G_: ?G? no                    _A_: ?A? atime
_s_: ?s? size               ^ ^                     ^ ^                            _a_: ?a? access
_v_: ?v? version (natural)  ^ ^                     ^ ^                            _u_: ?u? use
_e_: ?e? extension          ^ ^                     ^ ^                            _c_: ?c? ctime
_D_: ?D? default            ^ ^                     ^ ^                            _S_: ?S? status
_q_: quit                   ^ ^                     ^ ^                            ^ ^
"
  ("n" (dired-quick-sort "none")
   (dired-quick-sort--sort-by-last "none"))
  ("t" (dired-quick-sort "time")
   (dired-quick-sort--sort-by-last "time"))
  ("s" (dired-quick-sort "size")
   (dired-quick-sort--sort-by-last "size"))
  ("v" (dired-quick-sort "version")
   (dired-quick-sort--sort-by-last "version"))
  ("e" (dired-quick-sort "extension")
   (if (string= dired-quick-sort-sort-by-last "extension") "[X]" "[ ]"))
  ;; No --sort switch passed to ls. This can be useful when used in combination with LC_COLLATE="en_US.utf8"
  ("D" (dired-quick-sort "default")
   (if (string= dired-quick-sort-sort-by-last "default") "[X]" "[ ]"))
  ("r" (dired-quick-sort nil ?y)
   (if (char-equal dired-quick-sort-reverse-last ?y) "[X]" "[ ]"))
  ("R" (dired-quick-sort nil ?n)
   (if (char-equal dired-quick-sort-reverse-last ?n) "[X]" "[ ]"))
  ("g" (dired-quick-sort nil nil ?y)
   (if (char-equal dired-quick-sort-group-directories-last ?y) "[X]" "[ ]"))
  ("G" (dired-quick-sort nil nil ?n)
   (if (char-equal dired-quick-sort-group-directories-last ?n) "[X]" "[ ]"))
  ("d" (dired-quick-sort nil nil nil "default")
   (if (string= dired-quick-sort-time-last "default") "[X]" "[ ]"))
  ("A" (dired-quick-sort nil nil nil "atime")
   (if (string= dired-quick-sort-time-last "atime") "[X]" "[ ]"))
  ("a" (dired-quick-sort nil nil nil "access")
   (if (string= dired-quick-sort-time-last "access") "[X]" "[ ]"))
  ("u" (dired-quick-sort nil nil nil "use")
   (if (string= dired-quick-sort-time-last "use") "[X]" "[ ]"))
  ("c" (dired-quick-sort nil nil nil "ctime")
   (if (string= dired-quick-sort-time-last "ctime") "[X]" "[ ]"))
  ("S" (dired-quick-sort nil nil nil "status")
   (if (string= dired-quick-sort-time-last "status") "[X]" "[ ]"))
  ("q" nil "quit" :hint t :color blue))

(defun dired-quick-sort--display-setup-warning (msg)
  "Display setup warning according to
`dired-quick-sort-suppress-setup-warning'."
  (let ((display-func
         (cond
          ((null dired-quick-sort-suppress-setup-warning)
           (lambda (m) (display-warning 'dired-quick-sort m)))
          ((eq dired-quick-sort-suppress-setup-warning 'message) #'message)
          ((eq dired-quick-sort-suppress-setup-warning t) #'ignore))))
    (funcall display-func msg)))

;;;###autoload
(defun dired-quick-sort-setup ()
  "Run the default setup.

This will bind the key S in `dired-mode' to run
`hydra-dired-quick-sort/body', and automatically run the sorting
criteria after entering `dired-mode'.  You can choose to not call
this setup function and run a modified version of this function
to use your own preferred setup:

  ;; Replace \"S\" with other keys to invoke the dired-quick-sort hydra.
  (define-key dired-mode-map \"S\" 'hydra-dired-quick-sort/body)
  ;; Automatically use the sorting defined here to sort.
  (add-hook 'dired-mode-hook 'dired-quick-sort)"

  (if (not ls-lisp-use-insert-directory-program)
      (dired-quick-sort--display-setup-warning
       "`ls-lisp-use-insert-directory-program' is nil. The package `dired-quick-sort'
will not work and thus is not set up by `dired-quick-sort-setup'. Set it to t to
suppress this warning. Alternatively, set
`dired-quick-sort-suppress-setup-warning' to suppress warning and skip setup
silently.")
    (if (not
         (with-temp-buffer
           (call-process insert-directory-program nil t nil "--version")
           (string-match-p "GNU" (buffer-string))))
        (dired-quick-sort--display-setup-warning
         "`insert-directory-program' does
not point to GNU ls.  Please set `insert-directory-program' to GNU ls.  The
package `dired-quick-sort' will not work and thus is not set up by
`dired-quick-sort-setup'. Alternatively, set
`dired-quick-sort-suppress-setup-warning' to suppress warning and skip setup
silently.")
      (define-key dired-mode-map "S" 'hydra-dired-quick-sort/body)
      (add-hook 'dired-mode-hook #'dired-quick-sort-set-switches))))

(provide 'dired-quick-sort)

;;; dired-quick-sort.el ends here

;; Local Variables:
;; coding: utf-8
;; fill-column: 80
;; indent-tabs-mode: nil
;; sentence-end-double-space: t
;; End: