-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdired-file-info.el
403 lines (342 loc) · 14.2 KB
/
dired-file-info.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
;;; dired-file-info.el --- Dired File Information -*- lexical-binding: t; -*-
;; Copyright (C) 2022 AKIYAMA Kouhei
;; Author: AKIYAMA Kouhei <[email protected]>
;; Keywords: files
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;; Show more detailed file information than y(dired-show-file-type) in
;; dired-mode.
;;; Usage
;; (autoload #'dired-file-info "dired-file-info" "" t)
;; (with-eval-after-load "dired"
;; (define-key dired-mode-map "y" #'dired-file-info))
;;; Code:
;; TODO:
;; - Support marked files
(require 'dired)
(require 'cl-lib)
;;;; Settings
(defgroup dired-file-info nil
"Dired File Info"
:prefix "dired-file-info-"
:group 'dired
:group 'files)
(defcustom dired-file-info-message-language nil
"Language of displayed messages."
:group 'dired-file-info
:type '(choice (const :tag "Auto" nil)
(string :tag "Language"))
:set (lambda (symbol value)
(set-default symbol value)
(when (fboundp 'dired-file-info-update-message-language)
(dired-file-info-update-message-language))))
(defcustom dired-file-info-timestamp-format "%Y-%m-%d %H:%M:%S"
"Timestamp format."
:group 'dired-file-info
:type '(choice (string :tag "Use format-time-string")
(function :tag "Function (time)")))
(defcustom dired-file-info-details-local-file-command
(if (executable-find "exiftool")
"exiftool -c \"%+.06f\""
"stat")
"Command to output local file details."
:group 'dired-file-info
:type '(choice (string :tag "command line to pass to dired-do-shell-command")
(function :tag "Function (file)")))
(defcustom dired-file-info-details-remote-file-command "stat"
"Command to output remote file details."
:group 'dired-file-info
:type '(choice (string :tag "command line to pass to dired-do-shell-command")
(function :tag "Function (file)")))
(defcustom dired-file-info-details-local-dir-command "stat"
"Command to output local directory details."
:group 'dired-file-info
:type '(choice (string :tag "command line to pass to dired-do-shell-command")
(function :tag "Function (file)")))
(defcustom dired-file-info-details-remote-dir-command "stat"
"Command to output remote directory details."
:group 'dired-file-info
:type '(choice (string :tag "command line to pass to dired-do-shell-command")
(function :tag "Function (file)")))
(defcustom dired-file-info-overview-items
(list #'dired-file-info--file-type
#'dired-file-info--directory-size
#'dired-file-info--file-size
#'dired-file-info--file-timestamps)
"List of overview items."
:group 'dired-file-info
:type '(repeat (function :tag "Function (file deref-symlinks)")))
;;;; Message Catalog
(defconst dired-file-info--messages
'(("Japanese" .
#s(hash-table
size 30
test equal
data (
"Scanning directory %s" "スキャン中 %s"
"Size" "サイズ"
"B" "B"
"bytes" "バイト"
"Files" "ファイル数"
"Directories" "ディレクトリ数"
"Accessed" "アクセス"
"Modified" "内容更新"
"Status Changed" "状態更新"
"More details" "詳細"
)))))
(defvar dired-file-info--current-messages nil)
(defun dired-file-info-set-message-language (lang)
(setq dired-file-info--current-messages
(alist-get (or lang current-language-environment)
dired-file-info--messages nil nil #'equal)))
(defun dired-file-info-update-message-language ()
(dired-file-info-set-message-language dired-file-info-message-language))
(dired-file-info-update-message-language)
(defun dired-file-info--msg (str)
(or (and dired-file-info--current-messages
(gethash str dired-file-info--current-messages))
str))
;;;; Debug
(defun dired-file-info--warn (format &rest args)
(display-warning 'dired-file-info
(apply #'format-message format args)))
;;;; Directory Scan
(defun dired-file-info--accumulate-entry (path
fun-file
&optional
fun-enter
fun-leave
deref-symlinks
only-one-filesystem
depth
attr)
(when (null depth) (setq depth 0))
(let* ((attr (or attr (file-attributes path)))
(type (file-attribute-type attr))
(symlink (and (stringp type) type)))
;; Note: On MS-Windows, (file-attributes "Long filename...") seems
;; to return nil.
(unless attr
(dired-file-info--warn "Ignore file `%s' (`file-attributes' returns nil)"
path))
(when (and attr
(not (and only-one-filesystem
(/= only-one-filesystem
(file-attribute-device-number attr)))))
(cond
;; Symbolic Link
(symlink
(if deref-symlinks
(dired-file-info--accumulate-entry
symlink fun-file fun-enter fun-leave
deref-symlinks only-one-filesystem depth)
(funcall fun-file path attr)))
;; Directory
((eq type t)
(dired-file-info--accumulate-directory
path fun-file fun-enter fun-leave
deref-symlinks only-one-filesystem (1+ depth)))
;; Normal File
(t
(funcall fun-file path attr))))))
(defun dired-file-info--files-and-attributes (dir)
(condition-case err
;; On MS-Windows, a long DIR-PATH will cause a file-missing
;; ("No such file or directory") error.
(directory-files-and-attributes dir)
(error
(dired-file-info--warn
"Ignore dir `%s' (`directory-files-and-attributes' signals error `%s')"
dir err)
nil)))
(defun dired-file-info--accumulate-directory (dir-path
fun-file
&optional
fun-enter
fun-leave
deref-symlinks
only-one-filesystem
depth)
(when (null depth) (setq depth 0))
(when fun-enter (funcall fun-enter dir-path))
(cl-loop
for (entry-name . attr) in (dired-file-info--files-and-attributes dir-path)
unless (string-match "\\`\\.\\.?\\'" entry-name)
do (dired-file-info--accumulate-entry
(concat dir-path "/" entry-name)
fun-file fun-enter fun-leave
deref-symlinks only-one-filesystem depth
;; By reusing already obtained attributes, not only performance
;; is improved but also errors in `file-attributes' for long
;; path names can be avoided.
;; While (file-attributes "long file name..") returns nil, the
;; attributes obtained from the `directory-files-and-attributes'
;; function do not seem to be nil.
attr))
(when fun-leave (funcall fun-leave dir-path)))
(defun dired-file-info--summarize-directory (dir-path
&optional
deref-symlinks
only-one-filesystem)
(let ((size 0.0)
(num-files 0)
(num-dirs 0))
(dired-file-info--accumulate-entry ;;-directory? deref first symlink?
dir-path
(lambda (_path attr)
(cl-incf num-files)
(cl-incf size (file-attribute-size attr)))
(lambda (path)
(cl-incf num-dirs)
(let ((message-log-max nil))
(message (dired-file-info--msg "Scanning directory %s") path)))
nil
deref-symlinks
only-one-filesystem
0)
(list size num-files num-dirs)))
;;;; Dereference Symlinks
(defun dired-file-info--deref-symlink (path deref-symlinks)
(when deref-symlinks
(let (target)
(while (setq target (file-symlink-p path))
(setq path target)))) ;;@todo infinite loop
path)
(defun dired-file-info--directory-p (path deref-symlinks)
(and (file-directory-p path)
;; Exclude symlink when not deref-symlinks
(or (not (file-symlink-p path))
deref-symlinks)))
;;;; Format Text
(defun dired-file-info--format-size (size)
(when (stringp size)
(setq size
(and (string-match "\\`\\([0-9]+\\)" size)
(string-to-number (match-string 1 size)))))
(when (numberp size)
(concat
(dired-file-info--msg "Size")
": " (file-size-human-readable size) (dired-file-info--msg "B")
" (" (format "%d" size) (dired-file-info--msg "bytes") ")")))
(defun dired-file-info--format-time (time)
(cond
((stringp dired-file-info-timestamp-format)
(format-time-string dired-file-info-timestamp-format time))
((functionp dired-file-info-timestamp-format)
(funcall dired-file-info-timestamp-format time))
(t "")))
;;;; Retrieve Property
(defun dired-file-info--file-type (file &optional deref-symlinks)
(let (process-file-side-effects)
(with-temp-buffer
(if deref-symlinks
(process-file "file" nil t t "-L" "--" file)
(process-file "file" nil t t "--" file))
(when (bolp)
(backward-delete-char 1))
(buffer-string))))
;; (defun dired-file-info--directory-size-du (file)
;; (and (file-directory-p file)
;; (not (file-remote-p file))
;; (let ((du (progn
;; (message "Executing du...")
;; (shell-command-to-string (mapconcat #'shell-quote-argument (list "du" "-bs" file) " ")))))
;; (message "Executing du...done")
;; (concat "\n" (dired-file-info--format-size du)))))
(defun dired-file-info--directory-size (file &optional deref-symlinks)
(when (dired-file-info--directory-p file deref-symlinks)
(let* ((summary (dired-file-info--summarize-directory
file deref-symlinks nil))
(size (nth 0 summary))
(num-files (nth 1 summary))
(num-dirs (nth 2 summary)))
(concat "\n" (dired-file-info--format-size size)
"\n" (dired-file-info--msg "Files") ": " (format "%d" num-files)
"\n" (dired-file-info--msg "Directories") ": " (format "%d" num-dirs)))))
(defun dired-file-info--file-size (file &optional deref-symlinks)
(and (not (dired-file-info--directory-p file deref-symlinks))
(concat "\n" (dired-file-info--format-size
(file-attribute-size
(file-attributes
(dired-file-info--deref-symlink file deref-symlinks)))))))
(defun dired-file-info--file-timestamps (file &optional deref-symlinks)
(let ((attr (file-attributes (dired-file-info--deref-symlink file deref-symlinks))))
(concat
(concat "\n" (dired-file-info--msg "Accessed") ": "
(dired-file-info--format-time
(file-attribute-access-time attr)))
(concat "\n" (dired-file-info--msg "Modified") ": "
(dired-file-info--format-time
(file-attribute-modification-time attr)))
(concat "\n" (dired-file-info--msg "Status Changed") ": "
(dired-file-info--format-time
(file-attribute-status-change-time attr))))))
;;;; Info List
(defun dired-file-info--show-overview (file &optional deref-symlinks)
"Display overview FILE information."
(message
"%s"
(concat
(mapconcat (lambda (fun)
(funcall fun file deref-symlinks))
dired-file-info-overview-items
"")
"\n"
"("
(if deref-symlinks "C-u ")
(substitute-command-keys "\\[dired-file-info]:")
(dired-file-info--msg "More details")
")"
)))
(defconst dired-file-info-buffer-name "*Dired File Info*")
(defun dired-file-info--show-details (file &optional deref-symlinks)
"Display detailed FILE information."
(setq file (dired-file-info--deref-symlink file deref-symlinks))
(let ((command (if (file-remote-p (expand-file-name file))
(if (file-directory-p file)
dired-file-info-details-remote-dir-command
dired-file-info-details-remote-file-command)
(if (file-directory-p file)
dired-file-info-details-local-dir-command
dired-file-info-details-local-file-command))))
(cond
((stringp command)
(let ((shell-command-buffer-name dired-file-info-buffer-name))
(dired-do-shell-command command nil (list file))
(when-let ((buffer (get-buffer dired-file-info-buffer-name)))
(with-current-buffer buffer
(view-mode)))))
((functionp command)
(funcall command file)))))
;;;; Command
(defvar dired-file-info--last-args nil)
;;;###autoload
(defun dired-file-info (file &optional deref-symlinks details)
"Display FILE information.
Outputs detailed information when executed twice in a row."
(interactive
(let* ((file (dired-get-filename t))
(deref-symlinks current-prefix-arg)
(details (and (eq last-command 'dired-file-info)
(equal (nth 0 dired-file-info--last-args) file)
(equal (nth 1 dired-file-info--last-args) deref-symlinks))))
(list file deref-symlinks details)))
(if details
;; Details
(progn
(dired-file-info--show-details file deref-symlinks)
(setq dired-file-info--last-args nil))
;; Overview
(dired-file-info--show-overview file deref-symlinks)
(setq dired-file-info--last-args (list file deref-symlinks details))))
(provide 'dired-file-info)
;;; dired-file-info.el ends here