Commit | Line | Data |
---|---|---|
76550a57 ER |
1 | ;;; ls-lisp.el --- emulate insert-directory completely in Emacs Lisp |
2 | ||
c90f2757 | 3 | ;; Copyright (C) 1992, 1994, 2000, 2001, 2002, 2003, 2004, |
5df4f04c | 4 | ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. |
b578f267 | 5 | |
55535639 PJ |
6 | ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de> |
7 | ;; Modified by: Francis J. Wright <F.J.Wright@maths.qmw.ac.uk> | |
8 | ;; Maintainer: FSF | |
9 | ;; Keywords: unix, dired | |
d88c0e93 | 10 | |
b578f267 | 11 | ;; This file is part of GNU Emacs. |
d88c0e93 | 12 | |
eb3fa2cf | 13 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
d88c0e93 | 14 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
15 | ;; the Free Software Foundation, either version 3 of the License, or |
16 | ;; (at your option) any later version. | |
b578f267 EN |
17 | |
18 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
d88c0e93 SK |
19 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | ;; GNU General Public License for more details. | |
b578f267 | 22 | |
d88c0e93 | 23 | ;; You should have received a copy of the GNU General Public License |
eb3fa2cf | 24 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
b578f267 EN |
25 | |
26 | ;;; Commentary: | |
738eb4e7 | 27 | |
3f51f5a9 | 28 | ;; OVERVIEW ========================================================== |
d9a0f717 | 29 | |
3f51f5a9 EZ |
30 | ;; This file redefines the function `insert-directory' to implement it |
31 | ;; directly from Emacs lisp, without running ls in a subprocess. It | |
32 | ;; is useful if you cannot afford to fork Emacs on a real memory UNIX, | |
7c2fb837 | 33 | ;; or other non-UNIX platforms if you don't have the ls |
3f51f5a9 | 34 | ;; program, or if you want a different format from what ls offers. |
738eb4e7 | 35 | |
3f51f5a9 EZ |
36 | ;; This function can use regexps instead of shell wildcards. If you |
37 | ;; enter regexps remember to double each $ sign. For example, to | |
38 | ;; include files *.el, enter `.*\.el$$', resulting in the regexp | |
39 | ;; `.*\.el$'. | |
738eb4e7 | 40 | |
3f51f5a9 | 41 | ;; RESTRICTIONS ====================================================== |
738eb4e7 | 42 | |
3f51f5a9 EZ |
43 | ;; * A few obscure ls switches are still ignored: see the docstring of |
44 | ;; `insert-directory'. | |
d88c0e93 | 45 | |
3f51f5a9 | 46 | ;; TO DO ============================================================= |
738eb4e7 | 47 | |
3f51f5a9 | 48 | ;; Complete handling of F switch (if/when possible). |
738eb4e7 | 49 | |
3f51f5a9 EZ |
50 | ;; FJW: May be able to sort much faster by consing the sort key onto |
51 | ;; the front of each list element, sorting and then stripping the key | |
52 | ;; off again! | |
738eb4e7 | 53 | |
3f51f5a9 | 54 | ;;; History: |
76550a57 | 55 | |
3f51f5a9 EZ |
56 | ;; Written originally by Sebastian Kremer <sk@thp.uni-koeln.de> |
57 | ;; Revised by Andrew Innes and Geoff Volker (and maybe others). | |
3045b163 | 58 | |
3f51f5a9 | 59 | ;; Modified by Francis J. Wright <F.J.Wright@maths.qmw.ac.uk>, mainly |
5eba3394 LH |
60 | ;; to support many more ls options, "platform emulation" and more |
61 | ;; robust sorting. | |
3f51f5a9 EZ |
62 | |
63 | ;;; Code: | |
97b927b3 | 64 | |
a3723f13 JB |
65 | (eval-when-compile (require 'cl)) |
66 | ||
3f51f5a9 EZ |
67 | (defgroup ls-lisp nil |
68 | "Emulate the ls program completely in Emacs Lisp." | |
eff409ba | 69 | :version "21.1" |
3f51f5a9 EZ |
70 | :group 'dired) |
71 | ||
72 | (defcustom ls-lisp-emulation | |
a3374680 | 73 | (cond ;; ((eq system-type 'windows-nt) 'MS-Windows) |
3f51f5a9 | 74 | ((memq system-type |
a3374680 | 75 | '(hpux usg-unix-v irix berkeley-unix)) |
3f51f5a9 EZ |
76 | 'UNIX)) ; very similar to GNU |
77 | ;; Anything else defaults to nil, meaning GNU. | |
9201cc28 | 78 | "Platform to emulate: GNU (default), MacOS, MS-Windows, UNIX. |
3f51f5a9 EZ |
79 | Corresponding value is one of the atoms: nil, MacOS, MS-Windows, UNIX. |
80 | Sets default values for: `ls-lisp-ignore-case', `ls-lisp-dirs-first', | |
81 | `ls-lisp-verbosity'. Need not match actual platform. Changing this | |
82 | option will have no effect until you restart Emacs." | |
83 | :type '(choice (const :tag "GNU" nil) | |
84 | (const MacOS) | |
85 | (const MS-Windows) | |
86 | (const UNIX)) | |
87 | :group 'ls-lisp) | |
88 | ||
b010e1ba GM |
89 | ;; Only made an obsolete alias in 23.3. Before that, the initial |
90 | ;; value was set according to: | |
91 | ;; (or (memq ls-lisp-emulation '(MS-Windows MacOS)) | |
92 | ;; (and (boundp 'ls-lisp-dired-ignore-case) ls-lisp-dired-ignore-case)) | |
93 | ;; Which isn't the right thing to do. | |
94 | (define-obsolete-variable-alias 'ls-lisp-dired-ignore-case | |
95 | 'ls-lisp-ignore-case "21.1") | |
96 | ||
3f51f5a9 | 97 | (defcustom ls-lisp-ignore-case |
b010e1ba | 98 | (memq ls-lisp-emulation '(MS-Windows MacOS)) |
9201cc28 | 99 | "Non-nil causes ls-lisp alphabetic sorting to ignore case." |
3f51f5a9 EZ |
100 | :type 'boolean |
101 | :group 'ls-lisp) | |
102 | ||
103 | (defcustom ls-lisp-dirs-first (eq ls-lisp-emulation 'MS-Windows) | |
9201cc28 | 104 | "Non-nil causes ls-lisp to sort directories first in any ordering. |
3f51f5a9 EZ |
105 | \(Or last if it is reversed.) Follows Microsoft Windows Explorer." |
106 | ;; Functionality suggested by Chris McMahan <cmcmahan@one.net> | |
107 | :type 'boolean | |
108 | :group 'ls-lisp) | |
109 | ||
110 | (defcustom ls-lisp-verbosity | |
111 | (cond ((eq ls-lisp-emulation 'MacOS) nil) | |
112 | ((eq ls-lisp-emulation 'MS-Windows) | |
113 | (if (and (fboundp 'w32-using-nt) (w32-using-nt)) | |
114 | '(links))) ; distinguish NT/2K from 9x | |
115 | ((eq ls-lisp-emulation 'UNIX) '(links uid)) ; UNIX ls | |
116 | (t '(links uid gid))) ; GNU ls | |
9201cc28 | 117 | "A list of optional file attributes that ls-lisp should display. |
3f51f5a9 | 118 | It should contain none or more of the symbols: links, uid, gid. |
3aeb19ad | 119 | A value of nil (or an empty list) means display none of them. |
3f51f5a9 EZ |
120 | |
121 | Concepts come from UNIX: `links' means count of names associated with | |
122 | the file\; `uid' means user (owner) identifier\; `gid' means group | |
123 | identifier. | |
124 | ||
125 | If emulation is MacOS then default is nil\; | |
126 | if emulation is MS-Windows then default is `(links)' if platform is | |
127 | Windows NT/2K, nil otherwise\; | |
128 | if emulation is UNIX then default is `(links uid)'\; | |
129 | if emulation is GNU then default is `(links uid gid)'." | |
130 | ;; Functionality suggested by Howard Melman <howard@silverstream.com> | |
131 | :type '(set (const :tag "Show Link Count" links) | |
132 | (const :tag "Show User" uid) | |
133 | (const :tag "Show Group" gid)) | |
134 | :group 'ls-lisp) | |
135 | ||
73916123 | 136 | (defcustom ls-lisp-use-insert-directory-program |
a3374680 | 137 | (not (memq system-type '(ms-dos windows-nt))) |
9201cc28 | 138 | "Non-nil causes ls-lisp to revert back to using `insert-directory-program'. |
0cb0ba6c GV |
139 | This is useful on platforms where ls-lisp is dumped into Emacs, such as |
140 | Microsoft Windows, but you would still like to use a program to list | |
3f51f5a9 EZ |
141 | the contents of a directory." |
142 | :type 'boolean | |
143 | :group 'ls-lisp) | |
144 | ||
73916123 MR |
145 | ;;; Autoloaded because it is let-bound in `recover-session', `mail-recover-1'. |
146 | ;;;###autoload | |
3f51f5a9 | 147 | (defcustom ls-lisp-support-shell-wildcards t |
9201cc28 | 148 | "Non-nil means ls-lisp treats file patterns as shell wildcards. |
3f51f5a9 EZ |
149 | Otherwise they are treated as Emacs regexps (for backward compatibility)." |
150 | :type 'boolean | |
151 | :group 'ls-lisp) | |
152 | ||
2686cdc0 RS |
153 | (defcustom ls-lisp-format-time-list |
154 | '("%b %e %H:%M" | |
155 | "%b %e %Y") | |
9201cc28 | 156 | "List of `format-time-string' specs to display file time stamps. |
ea88e775 EZ |
157 | These specs are used ONLY if a valid locale can not be determined. |
158 | ||
159 | If `ls-lisp-use-localized-time-format' is non-nil, these specs are used | |
160 | regardless of whether the locale can be determined. | |
2686cdc0 RS |
161 | |
162 | Syntax: (EARLY-TIME-FORMAT OLD-TIME-FORMAT) | |
163 | ||
164 | The EARLY-TIME-FORMAT is used if file has been modified within the | |
165 | current year. The OLD-TIME-FORMAT is used for older files. To use ISO | |
166 | 8601 dates, you could set: | |
167 | ||
168 | \(setq ls-lisp-format-time-list | |
169 | '(\"%Y-%m-%d %H:%M\" | |
170 | \"%Y-%m-%d \"))" | |
401c1968 MR |
171 | :type '(list (string :tag "Early time format") |
172 | (string :tag "Old time format")) | |
2686cdc0 RS |
173 | :group 'ls-lisp) |
174 | ||
ea88e775 | 175 | (defcustom ls-lisp-use-localized-time-format nil |
9201cc28 | 176 | "Non-nil causes ls-lisp to use `ls-lisp-format-time-list' even if |
ea88e775 EZ |
177 | a valid locale is specified. |
178 | ||
179 | WARNING: Using localized date/time format might cause Dired columns | |
180 | to fail to lign up, e.g. if month names are not all of the same length." | |
181 | :type 'boolean | |
182 | :group 'ls-lisp) | |
183 | ||
44a9ca8b RS |
184 | (defvar original-insert-directory nil |
185 | "This holds the original function definition of `insert-directory'.") | |
186 | ||
df3d23ee EZ |
187 | (defvar ls-lisp-uid-d-fmt "-%d" |
188 | "Format to display integer UIDs.") | |
189 | (defvar ls-lisp-uid-s-fmt "-%s" | |
190 | "Format to display user names.") | |
191 | (defvar ls-lisp-gid-d-fmt "-%d" | |
192 | "Format to display integer GIDs.") | |
193 | (defvar ls-lisp-gid-s-fmt "-%s" | |
194 | "Format to display user group names.") | |
195 | (defvar ls-lisp-filesize-d-fmt "%d" | |
196 | "Format to display integer file sizes.") | |
197 | (defvar ls-lisp-filesize-f-fmt "%.0f" | |
198 | "Format to display float file sizes.") | |
199 | ||
3f51f5a9 EZ |
200 | ;; Remember the original insert-directory function |
201 | (or (featurep 'ls-lisp) ; FJW: unless this file is being reloaded! | |
44a9ca8b | 202 | (setq original-insert-directory (symbol-function 'insert-directory))) |
3f51f5a9 | 203 | |
3f51f5a9 EZ |
204 | \f |
205 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
0cb0ba6c GV |
206 | |
207 | (defun insert-directory (file switches &optional wildcard full-directory-p) | |
208 | "Insert directory listing for FILE, formatted according to SWITCHES. | |
209 | Leaves point after the inserted text. | |
210 | SWITCHES may be a string of options, or a list of strings. | |
211 | Optional third arg WILDCARD means treat FILE as shell wildcard. | |
212 | Optional fourth arg FULL-DIRECTORY-P means file is a directory and | |
213 | switches do not contain `d', so that a full listing is expected. | |
214 | ||
3f51f5a9 EZ |
215 | This version of the function comes from `ls-lisp.el'. |
216 | If the value of `ls-lisp-use-insert-directory-program' is non-nil then | |
217 | it works exactly like the version from `files.el' and runs a directory | |
218 | listing program whose name is in the variable | |
219 | `insert-directory-program'; if also WILDCARD is non-nil then it runs | |
220 | the shell specified by `shell-file-name'. If the value of | |
221 | `ls-lisp-use-insert-directory-program' is nil then it runs a Lisp | |
222 | emulation. | |
223 | ||
224 | The Lisp emulation does not run any external programs or shells. It | |
225 | supports ordinary shell wildcards if `ls-lisp-support-shell-wildcards' | |
226 | is non-nil; otherwise, it interprets wildcards as regular expressions | |
227 | to match file names. It does not support all `ls' switches -- those | |
21620882 EZ |
228 | that work are: A a B C c F G g h i n R r S s t U u X. The l switch |
229 | is assumed to be always present and cannot be turned off." | |
0cb0ba6c | 230 | (if ls-lisp-use-insert-directory-program |
44a9ca8b RS |
231 | (funcall original-insert-directory |
232 | file switches wildcard full-directory-p) | |
3f51f5a9 EZ |
233 | ;; We need the directory in order to find the right handler. |
234 | (let ((handler (find-file-name-handler (expand-file-name file) | |
94e72368 | 235 | 'insert-directory)) |
43bed668 | 236 | (orig-file file) |
94e72368 | 237 | wildcard-regexp) |
3f51f5a9 EZ |
238 | (if handler |
239 | (funcall handler 'insert-directory file switches | |
240 | wildcard full-directory-p) | |
d4939c66 JPW |
241 | ;; Remove --dired switch |
242 | (if (string-match "--dired " switches) | |
243 | (setq switches (replace-match "" nil nil switches))) | |
3f51f5a9 | 244 | ;; Convert SWITCHES to a list of characters. |
169759a0 | 245 | (setq switches (delete ?\ (delete ?- (append switches nil)))) |
94e72368 RS |
246 | ;; Sometimes we get ".../foo*/" as FILE. While the shell and |
247 | ;; `ls' don't mind, we certainly do, because it makes us think | |
248 | ;; there is no wildcard, only a directory name. | |
249 | (if (and ls-lisp-support-shell-wildcards | |
9b40e204 | 250 | (string-match "[[?*]" file) |
43bed668 | 251 | ;; Prefer an existing file to wildcards, like |
9b40e204 | 252 | ;; dired-noselect does. |
43bed668 | 253 | (not (file-exists-p file))) |
94e72368 RS |
254 | (progn |
255 | (or (not (eq (aref file (1- (length file))) ?/)) | |
256 | (setq file (substring file 0 (1- (length file))))) | |
257 | (setq wildcard t))) | |
3f51f5a9 | 258 | (if wildcard |
94e72368 | 259 | (setq wildcard-regexp |
3f51f5a9 EZ |
260 | (if ls-lisp-support-shell-wildcards |
261 | (wildcard-to-regexp (file-name-nondirectory file)) | |
262 | (file-name-nondirectory file)) | |
263 | file (file-name-directory file)) | |
94e72368 | 264 | (if (memq ?B switches) (setq wildcard-regexp "[^~]\\'"))) |
43bed668 EZ |
265 | (condition-case err |
266 | (ls-lisp-insert-directory | |
267 | file switches (ls-lisp-time-index switches) | |
268 | wildcard-regexp full-directory-p) | |
269 | (invalid-regexp | |
270 | ;; Maybe they wanted a literal file that just happens to | |
271 | ;; use characters special to shell wildcards. | |
272 | (if (equal (cadr err) "Unmatched [ or [^") | |
273 | (progn | |
274 | (setq wildcard-regexp (if (memq ?B switches) "[^~]\\'") | |
275 | file (file-relative-name orig-file)) | |
276 | (ls-lisp-insert-directory | |
277 | file switches (ls-lisp-time-index switches) | |
278 | nil full-directory-p)) | |
279 | (signal (car err) (cdr err))))) | |
7f1b5edc EZ |
280 | ;; Try to insert the amount of free space. |
281 | (save-excursion | |
282 | (goto-char (point-min)) | |
283 | ;; First find the line to put it on. | |
284 | (when (re-search-forward "^total" nil t) | |
285 | (let ((available (get-free-disk-space "."))) | |
286 | (when available | |
7ad0c1c3 EZ |
287 | ;; Replace "total" with "total used", to avoid confusion. |
288 | (replace-match "total used in directory") | |
7f1b5edc EZ |
289 | (end-of-line) |
290 | (insert " available " available))))))))) | |
3f51f5a9 EZ |
291 | |
292 | (defun ls-lisp-insert-directory | |
94e72368 | 293 | (file switches time-index wildcard-regexp full-directory-p) |
3045b163 | 294 | "Insert directory listing for FILE, formatted according to SWITCHES. |
3f51f5a9 EZ |
295 | Leaves point after the inserted text. This is an internal function |
296 | optionally called by the `ls-lisp.el' version of `insert-directory'. | |
297 | It is called recursively if the -R switch is used. | |
298 | SWITCHES is a *list* of characters. TIME-INDEX is the time index into | |
94e72368 | 299 | file-attributes according to SWITCHES. WILDCARD-REGEXP is nil or an *Emacs |
3f51f5a9 EZ |
300 | regexp*. FULL-DIRECTORY-P means file is a directory and SWITCHES does |
301 | not contain `d', so that a full listing is expected." | |
94e72368 | 302 | (if (or wildcard-regexp full-directory-p) |
3f51f5a9 EZ |
303 | (let* ((dir (file-name-as-directory file)) |
304 | (default-directory dir) ; so that file-attributes works | |
305 | (file-alist | |
de4db6f1 EZ |
306 | (directory-files-and-attributes dir nil wildcard-regexp t |
307 | (if (memq ?n switches) | |
308 | 'integer | |
309 | 'string))) | |
3f51f5a9 EZ |
310 | (now (current-time)) |
311 | (sum 0) | |
df3d23ee EZ |
312 | (max-uid-len 0) |
313 | (max-gid-len 0) | |
314 | (max-file-size 0) | |
3f51f5a9 | 315 | ;; do all bindings here for speed |
df3d23ee EZ |
316 | total-line files elt short file-size fil attr |
317 | fuid fgid uid-len gid-len) | |
3f51f5a9 EZ |
318 | (cond ((memq ?A switches) |
319 | (setq file-alist | |
320 | (ls-lisp-delete-matching "^\\.\\.?$" file-alist))) | |
321 | ((not (memq ?a switches)) | |
322 | ;; if neither -A nor -a, flush . files | |
323 | (setq file-alist | |
324 | (ls-lisp-delete-matching "^\\." file-alist)))) | |
325 | (setq file-alist | |
326 | (ls-lisp-handle-switches file-alist switches)) | |
327 | (if (memq ?C switches) ; column (-C) format | |
328 | (ls-lisp-column-format file-alist) | |
329 | (setq total-line (cons (point) (car-safe file-alist))) | |
df3d23ee EZ |
330 | ;; Find the appropriate format for displaying uid, gid, and |
331 | ;; file size, by finding the longest strings among all the | |
332 | ;; files we are about to display. | |
333 | (dolist (elt file-alist) | |
334 | (setq attr (cdr elt) | |
335 | fuid (nth 2 attr) | |
a43e3054 EZ |
336 | uid-len (if (stringp fuid) (string-width fuid) |
337 | (length (format "%d" fuid))) | |
df3d23ee | 338 | fgid (nth 3 attr) |
a43e3054 EZ |
339 | gid-len (if (stringp fgid) (string-width fgid) |
340 | (length (format "%d" fgid))) | |
df3d23ee EZ |
341 | file-size (nth 7 attr)) |
342 | (if (> uid-len max-uid-len) | |
343 | (setq max-uid-len uid-len)) | |
344 | (if (> gid-len max-gid-len) | |
345 | (setq max-gid-len gid-len)) | |
346 | (if (> file-size max-file-size) | |
347 | (setq max-file-size file-size))) | |
348 | (setq ls-lisp-uid-d-fmt (format " %%-%dd" max-uid-len)) | |
349 | (setq ls-lisp-uid-s-fmt (format " %%-%ds" max-uid-len)) | |
350 | (setq ls-lisp-gid-d-fmt (format " %%-%dd" max-gid-len)) | |
351 | (setq ls-lisp-gid-s-fmt (format " %%-%ds" max-gid-len)) | |
352 | (setq ls-lisp-filesize-d-fmt | |
353 | (format " %%%dd" | |
354 | (if (memq ?s switches) | |
355 | (length (format "%.0f" | |
356 | (fceiling (/ max-file-size 1024.0)))) | |
357 | (length (format "%.0f" max-file-size))))) | |
358 | (setq ls-lisp-filesize-f-fmt | |
359 | (format " %%%d.0f" | |
360 | (if (memq ?s switches) | |
361 | (length (format "%.0f" | |
362 | (fceiling (/ max-file-size 1024.0)))) | |
363 | (length (format "%.0f" max-file-size))))) | |
3f51f5a9 EZ |
364 | (setq files file-alist) |
365 | (while files ; long (-l) format | |
366 | (setq elt (car files) | |
367 | files (cdr files) | |
368 | short (car elt) | |
369 | attr (cdr elt) | |
370 | file-size (nth 7 attr)) | |
371 | (and attr | |
372 | (setq sum (+ file-size | |
373 | ;; Even if neither SUM nor file's size | |
374 | ;; overflow, their sum could. | |
375 | (if (or (< sum (- 134217727 file-size)) | |
376 | (floatp sum) | |
377 | (floatp file-size)) | |
378 | sum | |
379 | (float sum)))) | |
380 | (insert (ls-lisp-format short attr file-size | |
381 | switches time-index now)))) | |
382 | ;; Insert total size of all files: | |
383 | (save-excursion | |
384 | (goto-char (car total-line)) | |
385 | (or (cdr total-line) | |
386 | ;; Shell says ``No match'' if no files match | |
387 | ;; the wildcard; let's say something similar. | |
388 | (insert "(No match)\n")) | |
389 | (insert (format "total %.0f\n" (fceiling (/ sum 1024.0)))))) | |
390 | (if (memq ?R switches) | |
391 | ;; List the contents of all directories recursively. | |
392 | ;; cadr of each element of `file-alist' is t for | |
393 | ;; directory, string (name linked to) for symbolic | |
394 | ;; link, or nil. | |
9dce08b6 RS |
395 | (while file-alist |
396 | (setq elt (car file-alist) | |
3f51f5a9 EZ |
397 | file-alist (cdr file-alist)) |
398 | (when (and (eq (cadr elt) t) ; directory | |
0c6af6dd EZ |
399 | ;; Under -F, we have already decorated all |
400 | ;; directories, including "." and "..", with | |
401 | ;; a /, so allow for that as well. | |
402 | (not (string-match "\\`\\.\\.?/?\\'" (car elt)))) | |
3f51f5a9 EZ |
403 | (setq elt (expand-file-name (car elt) dir)) |
404 | (insert "\n" elt ":\n") | |
405 | (ls-lisp-insert-directory | |
94e72368 | 406 | elt switches time-index wildcard-regexp full-directory-p))))) |
3f51f5a9 EZ |
407 | ;; If not full-directory-p, FILE *must not* end in /, as |
408 | ;; file-attributes will not recognize a symlink to a directory, | |
409 | ;; so must make it a relative filename as ls does: | |
a8e4290b | 410 | (if (file-name-absolute-p file) (setq file (expand-file-name file))) |
3f51f5a9 EZ |
411 | (if (eq (aref file (1- (length file))) ?/) |
412 | (setq file (substring file 0 -1))) | |
80ca5799 | 413 | (let ((fattr (file-attributes file 'string))) |
3f51f5a9 | 414 | (if fattr |
169759a0 EZ |
415 | (insert (ls-lisp-format |
416 | (if (memq ?F switches) | |
417 | (ls-lisp-classify-file file fattr) | |
418 | file) | |
419 | fattr (nth 7 fattr) | |
3f51f5a9 EZ |
420 | switches time-index (current-time))) |
421 | (message "%s: doesn't exist or is inaccessible" file) | |
422 | (ding) (sit-for 2))))) ; to show user the message! | |
423 | ||
424 | (defun ls-lisp-column-format (file-alist) | |
425 | "Insert the file names (only) in FILE-ALIST into the current buffer. | |
426 | Format in columns, sorted vertically, following GNU ls -C. | |
427 | Responds to the window width as ls should but may not!" | |
428 | (let (files fmt ncols collen (nfiles 0) (colwid 0)) | |
429 | ;; Count number of files as `nfiles', build list of filenames as | |
430 | ;; `files', and find maximum filename length as `colwid': | |
431 | (let (file len) | |
432 | (while file-alist | |
433 | (setq nfiles (1+ nfiles) | |
434 | file (caar file-alist) | |
435 | files (cons file files) | |
436 | file-alist (cdr file-alist) | |
437 | len (length file)) | |
438 | (if (> len colwid) (setq colwid len)))) | |
439 | (setq files (nreverse files) | |
440 | colwid (+ 2 colwid) ; 2 character column gap | |
441 | fmt (format "%%-%ds" colwid) ; print format | |
442 | ncols (/ (window-width) colwid) ; no of columns | |
443 | collen (/ nfiles ncols)) ; floor of column length | |
444 | (if (> nfiles (* collen ncols)) (setq collen (1+ collen))) | |
445 | ;; Output the file names in columns, sorted vertically: | |
446 | (let ((i 0) j) | |
447 | (while (< i collen) | |
448 | (setq j i) | |
449 | (while (< j nfiles) | |
450 | (insert (format fmt (nth j files))) | |
451 | (setq j (+ j collen))) | |
452 | ;; FJW: This is completely unnecessary, but I don't like | |
453 | ;; trailing white space... | |
454 | (delete-region (point) (progn (skip-chars-backward " \t") (point))) | |
455 | (insert ?\n) | |
456 | (setq i (1+ i)))))) | |
9dce08b6 RS |
457 | |
458 | (defun ls-lisp-delete-matching (regexp list) | |
3f51f5a9 | 459 | "Delete all elements matching REGEXP from LIST, return new list." |
d6d472d5 | 460 | ;; Should perhaps use setcdr for efficiency. |
6467926f SK |
461 | (let (result) |
462 | (while list | |
3f51f5a9 | 463 | (or (string-match regexp (caar list)) |
6467926f SK |
464 | (setq result (cons (car list) result))) |
465 | (setq list (cdr list))) | |
466 | result)) | |
467 | ||
3f51f5a9 EZ |
468 | (defsubst ls-lisp-string-lessp (s1 s2) |
469 | "Return t if string S1 is less than string S2 in lexicographic order. | |
470 | Case is significant if `ls-lisp-ignore-case' is nil. | |
471 | Unibyte strings are converted to multibyte for comparison." | |
472 | (let ((u (compare-strings s1 0 nil s2 0 nil ls-lisp-ignore-case))) | |
473 | (and (numberp u) (< u 0)))) | |
474 | ||
9dce08b6 | 475 | (defun ls-lisp-handle-switches (file-alist switches) |
3f51f5a9 EZ |
476 | "Return new FILE-ALIST sorted according to SWITCHES. |
477 | SWITCHES is a list of characters. Default sorting is alphabetic." | |
6467926f | 478 | ;; FILE-ALIST's elements are (FILE . FILE-ATTRIBUTES). |
3f51f5a9 EZ |
479 | (or (memq ?U switches) ; unsorted |
480 | ;; Catch and ignore unexpected sorting errors | |
481 | (condition-case err | |
482 | (setq file-alist | |
483 | (let (index) | |
484 | ;; Copy file-alist in case of error | |
485 | (sort (copy-sequence file-alist) ; modifies its argument! | |
486 | (cond ((memq ?S switches) | |
487 | (lambda (x y) ; sorted on size | |
488 | ;; 7th file attribute is file size | |
489 | ;; Make largest file come first | |
490 | (< (nth 7 (cdr y)) | |
491 | (nth 7 (cdr x))))) | |
492 | ((setq index (ls-lisp-time-index switches)) | |
493 | (lambda (x y) ; sorted on time | |
494 | (ls-lisp-time-lessp (nth index (cdr y)) | |
495 | (nth index (cdr x))))) | |
496 | ((memq ?X switches) | |
497 | (lambda (x y) ; sorted on extension | |
498 | (ls-lisp-string-lessp | |
499 | (ls-lisp-extension (car x)) | |
500 | (ls-lisp-extension (car y))))) | |
501 | (t | |
502 | (lambda (x y) ; sorted alphabetically | |
503 | (ls-lisp-string-lessp (car x) (car y)))))))) | |
504 | (error (message "Unsorted (ls-lisp sorting error) - %s" | |
505 | (error-message-string err)) | |
506 | (ding) (sit-for 2)))) ; to show user the message! | |
507 | (if (memq ?F switches) ; classify switch | |
508 | (setq file-alist (mapcar 'ls-lisp-classify file-alist))) | |
509 | (if ls-lisp-dirs-first | |
510 | ;; Re-sort directories first, without otherwise changing the | |
511 | ;; ordering, and reverse whole list. cadr of each element of | |
512 | ;; `file-alist' is t for directory, string (name linked to) for | |
513 | ;; symbolic link, or nil. | |
514 | (let (el dirs files) | |
515 | (while file-alist | |
5eba3394 LH |
516 | (if (or (eq (cadr (setq el (car file-alist))) t) ; directory |
517 | (and (stringp (cadr el)) | |
518 | (file-directory-p (cadr el)))) ; symlink to a directory | |
3f51f5a9 EZ |
519 | (setq dirs (cons el dirs)) |
520 | (setq files (cons el files))) | |
521 | (setq file-alist (cdr file-alist))) | |
522 | (setq file-alist | |
523 | (if (memq ?U switches) ; unsorted order is reversed | |
524 | (nconc dirs files) | |
525 | (nconc files dirs) | |
526 | )))) | |
527 | ;; Finally reverse file alist if necessary. | |
528 | ;; (eq below MUST compare `(not (memq ...))' to force comparison of | |
529 | ;; `t' or `nil', rather than list tails!) | |
530 | (if (eq (eq (not (memq ?U switches)) ; unsorted order is reversed | |
531 | (not (memq ?r switches))) ; reversed sort order requested | |
532 | ls-lisp-dirs-first) ; already reversed | |
533 | (nreverse file-alist) | |
534 | file-alist)) | |
535 | ||
169759a0 EZ |
536 | (defun ls-lisp-classify-file (filename fattr) |
537 | "Append a character to FILENAME indicating the file type. | |
538 | ||
539 | FATTR is the file attributes returned by `file-attributes' for the file. | |
540 | The file type indicators are `/' for directories, `@' for symbolic | |
541 | links, `|' for FIFOs, `=' for sockets, `*' for regular files that | |
542 | are executable, and nothing for other types of files." | |
543 | (let* ((type (car fattr)) | |
544 | (modestr (nth 8 fattr)) | |
545 | (typestr (substring modestr 0 1))) | |
546 | (cond | |
547 | (type | |
548 | (concat filename (if (eq type t) "/" "@"))) | |
549 | ((string-match "x" modestr) | |
550 | (concat filename "*")) | |
551 | ((string= "p" typestr) | |
552 | (concat filename "|")) | |
553 | ((string= "s" typestr) | |
554 | (concat filename "=")) | |
555 | (t filename)))) | |
556 | ||
3f51f5a9 | 557 | (defun ls-lisp-classify (filedata) |
169759a0 EZ |
558 | "Append a character to file name in FILEDATA indicating the file type. |
559 | ||
560 | FILEDATA has the form (FILENAME . ATTRIBUTES), where ATTRIBUTES is the | |
561 | structure returned by `file-attributes' for that file. | |
562 | ||
3f51f5a9 | 563 | The file type indicators are `/' for directories, `@' for symbolic |
169759a0 EZ |
564 | links, `|' for FIFOs, `=' for sockets, `*' for regular files that |
565 | are executable, and nothing for other types of files." | |
5eba3394 | 566 | (let ((file-name (car filedata)) |
169759a0 EZ |
567 | (fattr (cdr filedata))) |
568 | (setq file-name (propertize file-name 'dired-filename t)) | |
569 | (cons (ls-lisp-classify-file file-name fattr) fattr))) | |
3f51f5a9 EZ |
570 | |
571 | (defun ls-lisp-extension (filename) | |
572 | "Return extension of FILENAME (ignoring any version extension) | |
573 | FOLLOWED by null and full filename, SOLELY for full alpha sort." | |
574 | ;; Force extension sort order: `no ext' then `null ext' then `ext' | |
575 | ;; to agree with GNU ls. | |
576 | (concat | |
577 | (let* ((i (length filename)) end) | |
578 | (if (= (aref filename (1- i)) ?.) ; null extension | |
579 | "\0" | |
580 | (while (and (>= (setq i (1- i)) 0) | |
581 | (/= (aref filename i) ?.))) | |
582 | (if (< i 0) "\0\0" ; no extension | |
583 | (if (/= (aref filename (1+ i)) ?~) | |
584 | (substring filename (1+ i)) | |
585 | ;; version extension found -- ignore it | |
586 | (setq end i) | |
587 | (while (and (>= (setq i (1- i)) 0) | |
588 | (/= (aref filename i) ?.))) | |
589 | (if (< i 0) "\0\0" ; no extension | |
590 | (substring filename (1+ i) end)))) | |
591 | )) "\0" filename)) | |
d88c0e93 | 592 | |
e54241c5 | 593 | ;; From Roland McGrath. Can use this to sort on time. |
9dce08b6 | 594 | (defun ls-lisp-time-lessp (time0 time1) |
3f51f5a9 EZ |
595 | "Return t if time TIME0 is earlier than time TIME1." |
596 | (let ((hi0 (car time0)) (hi1 (car time1))) | |
e54241c5 SK |
597 | (or (< hi0 hi1) |
598 | (and (= hi0 hi1) | |
3f51f5a9 EZ |
599 | (< (cadr time0) (cadr time1)))))) |
600 | ||
601 | (defun ls-lisp-format (file-name file-attr file-size switches time-index now) | |
602 | "Format one line of long ls output for file FILE-NAME. | |
603 | FILE-ATTR and FILE-SIZE give the file's attributes and size. | |
604 | SWITCHES, TIME-INDEX and NOW give the full switch list and time data." | |
605 | (let ((file-type (nth 0 file-attr)) | |
606 | ;; t for directory, string (name linked to) | |
607 | ;; for symbolic link, or nil. | |
608 | (drwxrwxrwx (nth 8 file-attr))) ; attribute string ("drwxrwxrwx") | |
6467926f | 609 | (concat (if (memq ?i switches) ; inode number |
c902c8a7 EZ |
610 | (let ((inode (nth 10 file-attr))) |
611 | (if (consp inode) | |
612 | (if (consp (cdr inode)) | |
0e5016a6 EZ |
613 | ;; 2^(24+16) = 1099511627776.0, but |
614 | ;; multiplying by it and then adding the | |
615 | ;; other members of the cons cell in one go | |
616 | ;; loses precision, since a double does not | |
617 | ;; have enough significant digits to hold a | |
618 | ;; full 64-bit value. So below we split | |
619 | ;; 1099511627776 into high 13 and low 5 | |
620 | ;; digits and compute in two parts. | |
621 | (let ((p1 (* (car inode) 10995116.0)) | |
622 | (p2 (+ (* (car inode) 27776.0) | |
623 | (* (cadr inode) 65536.0) | |
624 | (cddr inode)))) | |
625 | (format " %13.0f%05.0f " | |
626 | ;; Use floor to emulate integer | |
627 | ;; division. | |
628 | (+ p1 (floor p2 100000.0)) | |
629 | (mod p2 100000.0))) | |
630 | (format " %18.0f " | |
c902c8a7 EZ |
631 | (+ (* (car inode) 65536.0) |
632 | (cdr inode)))) | |
0e5016a6 | 633 | (format " %18d " inode)))) |
d6d472d5 | 634 | ;; nil is treated like "" in concat |
6467926f | 635 | (if (memq ?s switches) ; size in K |
df3d23ee EZ |
636 | (format ls-lisp-filesize-f-fmt |
637 | (fceiling (/ file-size 1024.0)))) | |
3f51f5a9 EZ |
638 | drwxrwxrwx ; attribute string |
639 | (if (memq 'links ls-lisp-verbosity) | |
df3d23ee | 640 | (format "%3d" (nth 1 file-attr))) ; link count |
3f51f5a9 | 641 | ;; Numeric uid/gid are more confusing than helpful; |
6467926f | 642 | ;; Emacs should be able to make strings of them. |
3f51f5a9 EZ |
643 | ;; They tend to be bogus on non-UNIX platforms anyway so |
644 | ;; optionally hide them. | |
645 | (if (memq 'uid ls-lisp-verbosity) | |
c0943d3d | 646 | ;; uid can be a string or an integer |
3f51f5a9 | 647 | (let ((uid (nth 2 file-attr))) |
df3d23ee EZ |
648 | (format (if (stringp uid) |
649 | ls-lisp-uid-s-fmt | |
650 | ls-lisp-uid-d-fmt) | |
651 | uid))) | |
3f51f5a9 EZ |
652 | (if (not (memq ?G switches)) ; GNU ls -- shows group by default |
653 | (if (or (memq ?g switches) ; UNIX ls -- no group by default | |
654 | (memq 'gid ls-lisp-verbosity)) | |
80ca5799 | 655 | (let ((gid (nth 3 file-attr))) |
df3d23ee EZ |
656 | (format (if (stringp gid) |
657 | ls-lisp-gid-s-fmt | |
658 | ls-lisp-gid-d-fmt) | |
659 | gid)))) | |
a3723f13 | 660 | (ls-lisp-format-file-size file-size (memq ?h switches)) |
3f51f5a9 EZ |
661 | " " |
662 | (ls-lisp-format-time file-attr time-index now) | |
738eb4e7 | 663 | " " |
40077a52 EZ |
664 | (if (not (memq ?F switches)) ; ls-lisp-classify already did that |
665 | (propertize file-name 'dired-filename t) | |
666 | file-name) | |
d88c0e93 | 667 | (if (stringp file-type) ; is a symbolic link |
3f51f5a9 | 668 | (concat " -> " file-type)) |
d88c0e93 SK |
669 | "\n" |
670 | ))) | |
671 | ||
9dce08b6 | 672 | (defun ls-lisp-time-index (switches) |
3f51f5a9 EZ |
673 | "Return time index into file-attributes according to ls SWITCHES list. |
674 | Return nil if no time switch found." | |
675 | ;; FJW: Default of nil is IMPORTANT and used in `ls-lisp-handle-switches'! | |
676 | (cond ((memq ?c switches) 6) ; last mode change | |
677 | ((memq ?t switches) 5) ; last modtime | |
678 | ((memq ?u switches) 4))) ; last access | |
679 | ||
d81a647c PE |
680 | (defun ls-lisp-time-to-seconds (time) |
681 | "Convert TIME to a floating point number." | |
682 | (+ (* (car time) 65536.0) | |
683 | (cadr time) | |
684 | (/ (or (nth 2 time) 0) 1000000.0))) | |
685 | ||
3f51f5a9 EZ |
686 | (defun ls-lisp-format-time (file-attr time-index now) |
687 | "Format time for file with attributes FILE-ATTR according to TIME-INDEX. | |
688 | Use the same method as ls to decide whether to show time-of-day or year, | |
689 | depending on distance between file date and NOW. | |
690 | All ls time options, namely c, t and u, are handled." | |
691 | (let* ((time (nth (or time-index 5) file-attr)) ; default is last modtime | |
d81a647c PE |
692 | (diff (- (ls-lisp-time-to-seconds time) |
693 | (ls-lisp-time-to-seconds now))) | |
694 | ;; Consider a time to be recent if it is within the past six | |
695 | ;; months. A Gregorian year has 365.2425 * 24 * 60 * 60 == | |
696 | ;; 31556952 seconds on the average, and half of that is 15778476. | |
697 | ;; Write the constant explicitly to avoid roundoff error. | |
698 | (past-cutoff -15778476)) ; half a Gregorian year | |
f8a10234 | 699 | (condition-case nil |
d81a647c PE |
700 | ;; Use traditional time format in the C or POSIX locale, |
701 | ;; ISO-style time format otherwise, so columns line up. | |
702 | (let ((locale system-time-locale)) | |
703 | (if (not locale) | |
704 | (let ((vars '("LC_ALL" "LC_TIME" "LANG"))) | |
705 | (while (and vars (not (setq locale (getenv (car vars))))) | |
706 | (setq vars (cdr vars))))) | |
707 | (if (member locale '("C" "POSIX")) | |
708 | (setq locale nil)) | |
709 | (format-time-string | |
710 | (if (and (<= past-cutoff diff) (<= diff 0)) | |
ea88e775 EZ |
711 | (if (and locale (not ls-lisp-use-localized-time-format)) |
712 | "%m-%d %H:%M" | |
713 | (nth 0 ls-lisp-format-time-list)) | |
714 | (if (and locale (not ls-lisp-use-localized-time-format)) | |
715 | "%Y-%m-%d " | |
716 | (nth 1 ls-lisp-format-time-list))) | |
d81a647c | 717 | time)) |
452e47d7 | 718 | (error "Unk 0 0000")))) |
738eb4e7 | 719 | |
a3723f13 | 720 | (defun ls-lisp-format-file-size (file-size human-readable) |
df3d23ee EZ |
721 | (if (not human-readable) |
722 | (format (if (floatp file-size) | |
723 | ls-lisp-filesize-f-fmt | |
724 | ls-lisp-filesize-d-fmt) | |
725 | file-size) | |
726 | (if (< file-size 1024) | |
727 | (format " %4d" file-size) | |
728 | (do ((file-size (/ file-size 1024.0) (/ file-size 1024.0)) | |
729 | ;; kilo, mega, giga, tera, peta, exa | |
730 | (post-fixes (list "k" "M" "G" "T" "P" "E") (cdr post-fixes))) | |
731 | ((< file-size 1024) | |
732 | (format " %3.0f%s" file-size (car post-fixes))))))) | |
a3723f13 | 733 | |
9dce08b6 | 734 | (provide 'ls-lisp) |
738eb4e7 | 735 | |
cbee283d | 736 | ;; arch-tag: e55f399b-05ec-425c-a6d5-f5e349c35ab4 |
76550a57 | 737 | ;;; ls-lisp.el ends here |