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