-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdisk-usage.el
More file actions
332 lines (282 loc) · 7.37 KB
/
disk-usage.el
File metadata and controls
332 lines (282 loc) · 7.37 KB
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
;;; disk-usage.el --- show disk usage
;; Copyright (C) 2010, 2011, 2012 niels giesen
;; Author: niels giesen <sharik@matroshka>
;; Keywords:
;; 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:
;; Requires the UNIX "du" utility
;; Disk-usage shows sorted disk usage in a directory in a dedicated
;; buffer. This may help one to clean up ones disk. Small files can be
;; ignored (see variable `du-ignored-size'). Options to "du" command
;; line can be customized and/or edited in place when `du' is invoked
;; with a prefix argument. Press 'd' in a *du* buffer to visit a line
;; in Dired.
;; Put something like the following in your init file (~/.emacs,
;; ~/.emacs.d/init.el or something similar):
;; (add-to-list 'load-path "~/.emacs.d/disk-usage")
;; (autoload 'du "disk-usage")
;;; Run with M-x du
;;; Customization is available under M-x customize-group du
;;; See function `du-mode' for more info.
;;; Code:
(defun du (dir &optional edit-args)
"List disk usage in DIR.
With prefix arg EDIT-ARGS, let user to edit arguments given to du."
(interactive
"DDirectory: \nP")
(save-window-excursion
(let*
((cmd (format
"du %s %s 2>%s"
(if edit-args
(read-from-minibuffer "Arguments: du " du-args)
du-args)
(shell-quote-argument
(expand-file-name dir))
du-err-log))
(proc (progn
(switch-to-buffer cmd)
(erase-buffer)
(start-process-shell-command "du" cmd cmd))))
(put 'du 'last-requested-at (current-time))
(set-process-sentinel proc #'du-callback))))
(defun du-callback (proc sentinel)
"Callback to run when \"du\" returns."
(save-window-excursion
(let ((result-string
(with-current-buffer
(process-buffer proc)
(buffer-substring-no-properties
(point-min)
(point-max)))))
(switch-to-buffer "*du*")
(du-mode t)
(let (buffer-read-only)
(erase-buffer)
(mapc
#'du-insert
(nreverse
(sort
(remove-if #'du-to-be-ignored-p
(split-string
result-string
"\n" t))
#'du-compare)))
(if (du-empty-buffer-p)
(insert-file-contents du-err-log)
(delete-char -1)))
(goto-char (point-min))))
(if
(> (time-to-seconds
(time-since (get 'du 'last-requested-at))) 3)
(message "Output of %s retrieved in %S"
(buffer-name (process-buffer proc))
(get-buffer "*du*"))
(switch-to-buffer "*du*"))
(unless du-debug
(kill-buffer (process-buffer proc))))
(defun du-to-be-ignored-p (s)
(<= (du-dehumanize-string s)
du-ignored-size))
(defgroup du nil
"Customization group for du (disk-usage)")
(defcustom du-debug nil
"Debug switch for `du'"
:group 'du
:type 'bool)
(defcustom du-ignored-size 1048576
"Ignore files of or below this size."
:group 'du
:type '(number))
(defcustom du-err-log "/tmp/du-err.log"
"Error log file for du."
:type '(file)
:group 'du)
(defcustom du-args
"--max-depth=1 -h -a"
"Arguments to be given to du.
Allowed are:
-a, --all
--apparent-size
-B, --block-size=SIZE
-b, --bytes
-c, --total
-D, --dereference-args
--files0-from=F
-H like --si, but also evokes a warning; will soon change
--si like -h, but use powers of 1000 not 1024
-k like --block-size=1K
-l, --count-links
-m like --block-size=1M
-L, --dereference
-P, --no-dereference
-S, --separate-dirs
-s, --summarize
-x, --one-file-system
-X FILE, --exclude-from=FILE
--exclude=PATTERN
--max-depth=N
See man(du) for explanation of those arguments"
:group 'du
:type 'string)
(defun du-size-part (s)
(string-to-number s))
(du-dehumanize-string foo)
(defun du-dehumanize-string (s)
(string-match
du-size-regex
s)
(setq foo s)
(* (string-to-number
(replace-regexp-in-string
"," "."
(match-string 1 s)))
(cdr (assoc
(match-string 2 s)
du-size-alist))))
(defun du-compare (s1 s2)
(< (du-dehumanize-string s1)
(du-dehumanize-string s2)))
(defun du-insert (s)
(string-match
du-size-regex
s)
(insert
(format
"% 10s%2s %s"
(match-string 1 s)
(match-string 2 s)
(substring s (match-end 3)))
?\n))
(defvar du-sizes
'(""
""
"kB"
"K"
"MB"
"M"
"G"
"T"
"P"
"E"
"Z"
"Y"))
(defvar du-size-regex
(eval
`(rx
string-start
(group
(1+ (or digit "." ",")))
(group
(or
,@du-sizes))
(group (1+ space))))
"Regular expression matching file sizes")
(defvar du-inserted-string-regex
(eval
`(rx
string-start
(1+ space)
(group
(1+ (or digit ".")))
(1+ space)
(group
(or
,@du-sizes))
(group (1+ space))))
"Regular epression matching file sizes")
(defvar du-size-alist
(let (result)
(dotimes (n (length du-sizes) result)
(push
(cons
(nth n du-sizes)
(expt (if (zerop (% n 2))
1000.0
1024.0)
(/ n 2)))
result)))
"Following kB 1000, K 1024, MB 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y")
(defun du-file-at-line ()
(let ((line (buffer-substring-no-properties
(point-at-bol)
(point-at-eol))))
(string-match du-inserted-string-regex line)
(substring line (match-end 3))))
(defun du-down-dir ()
(interactive)
(let ((file-or-dir (du-file-at-line)))
(if
(file-directory-p file-or-dir)
(du file-or-dir)
(message
"Not a directory: %s" file-or-dir))))
(defun du-up-dir ()
"Go up a dir"
(interactive)
(let ((file-or-dir (du-file-at-line)))
(du
(file-name-directory
(substring file-or-dir
0 -1)))))
(defvar du-map
(let ((m (make-sparse-keymap)))
(define-key m "\C-m" 'du-down-dir)
(define-key m [(:)] 'du-up-dir)
(define-key m [(d)] 'du-dired)
(define-key m [(q)] 'bury-buffer)
(define-key m [(u)] 'du)
(define-key m [(D)] 'du-delete)
m))
(defun du-dired ()
"View dir in dired"
(interactive)
(let ((file-or-dir (du-file-at-line)))
(dired
(if (file-directory-p file-or-dir)
file-or-dir
(file-name-directory file-or-dir)))
(when (not (file-directory-p file-or-dir))
(goto-char (point-min))
(search-forward (file-name-nondirectory file-or-dir))
(goto-char (match-beginning 0)))))
(defun du-empty-buffer-p ()
(= (point-min)
(point-max)))
(defun du-delete ()
"Directly delete a file or directory in Disk Usage buffer.
To keep it simple, this command does not delete the file or directory from the buffer."
(interactive)
(let* ((pos (point))
(file-or-dir (du-file-at-line))
(type (if (file-directory-p file-or-dir)
'dir
'file)))
(when (yes-or-no-p
(format "Delete %S? " type))
(case type
(file (delete-file file-or-dir))
(dir (delete-directory file-or-dir t)))
(goto-char pos)
(let (buffer-read-only)
(delete-region (point-at-bol) (point-at-bol 2))))))
(define-minor-mode du-mode
"Minor mode for du.
Du shows disk usage and allows navigation and switching to dired.
Key bindings:
\\{du-map}"
nil
" du"
du-map
(setq buffer-read-only t))
(provide 'disk-usage)
;;; disk-usage.el ends here