Commit | Line | Data |
---|---|---|
76550a57 ER |
1 | ;;; ls-lisp.el --- emulate insert-directory completely in Emacs Lisp |
2 | ||
3f51f5a9 | 3 | ;; Copyright (C) 1992, 1994, 2000 Free Software Foundation, Inc. |
b578f267 | 4 | |
55535639 PJ |
5 | ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de> |
6 | ;; Modified by: Francis J. Wright <F.J.Wright@maths.qmw.ac.uk> | |
7 | ;; Maintainer: FSF | |
8 | ;; Keywords: unix, dired | |
d88c0e93 | 9 | |
b578f267 | 10 | ;; This file is part of GNU Emacs. |
d88c0e93 | 11 | |
b578f267 | 12 | ;; GNU Emacs is free software; you can redistribute it and/or modify |
d88c0e93 | 13 | ;; it under the terms of the GNU General Public License as published by |
7c938215 | 14 | ;; the Free Software Foundation; either version 2, or (at your option) |
d88c0e93 | 15 | ;; any later version. |
b578f267 EN |
16 | |
17 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
d88c0e93 SK |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
b578f267 | 21 | |
d88c0e93 | 22 | ;; You should have received a copy of the GNU General Public License |
b578f267 EN |
23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
24 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
25 | ;; Boston, MA 02111-1307, USA. | |
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, | |
34 | ;; under VMS or other non-UNIX platforms if you don't have the ls | |
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 | ;; * Generally only numeric uid/gid. |
d88c0e93 | 48 | |
3f51f5a9 | 49 | ;; TO DO ============================================================= |
738eb4e7 | 50 | |
3f51f5a9 | 51 | ;; Complete handling of F switch (if/when possible). |
738eb4e7 | 52 | |
3f51f5a9 EZ |
53 | ;; FJW: May be able to sort much faster by consing the sort key onto |
54 | ;; the front of each list element, sorting and then stripping the key | |
55 | ;; off again! | |
738eb4e7 | 56 | |
3f51f5a9 | 57 | ;;; History: |
76550a57 | 58 | |
3f51f5a9 EZ |
59 | ;; Written originally by Sebastian Kremer <sk@thp.uni-koeln.de> |
60 | ;; Revised by Andrew Innes and Geoff Volker (and maybe others). | |
3045b163 | 61 | |
3f51f5a9 EZ |
62 | ;; Modified by Francis J. Wright <F.J.Wright@maths.qmw.ac.uk>, mainly |
63 | ;; to support many more ls options, "platform emulation", hooks for | |
64 | ;; external symbolic link support and more robust sorting. | |
65 | ||
66 | ;;; Code: | |
97b927b3 | 67 | |
a3723f13 JB |
68 | (eval-when-compile (require 'cl)) |
69 | ||
3f51f5a9 EZ |
70 | (defgroup ls-lisp nil |
71 | "Emulate the ls program completely in Emacs Lisp." | |
eff409ba | 72 | :version "21.1" |
3f51f5a9 EZ |
73 | :group 'dired) |
74 | ||
75 | (defcustom ls-lisp-emulation | |
76 | (cond ((eq system-type 'macos) 'MacOS) | |
77 | ;; ((eq system-type 'windows-nt) 'MS-Windows) | |
78 | ((memq system-type | |
79 | '(hpux dgux usg-unix-v unisoft-unix rtu irix berkeley-unix)) | |
80 | 'UNIX)) ; very similar to GNU | |
81 | ;; Anything else defaults to nil, meaning GNU. | |
82 | "*Platform to emulate: GNU (default), MacOS, MS-Windows, UNIX. | |
83 | Corresponding value is one of the atoms: nil, MacOS, MS-Windows, UNIX. | |
84 | Sets default values for: `ls-lisp-ignore-case', `ls-lisp-dirs-first', | |
85 | `ls-lisp-verbosity'. Need not match actual platform. Changing this | |
86 | option will have no effect until you restart Emacs." | |
87 | :type '(choice (const :tag "GNU" nil) | |
88 | (const MacOS) | |
89 | (const MS-Windows) | |
90 | (const UNIX)) | |
91 | :group 'ls-lisp) | |
92 | ||
93 | (defcustom ls-lisp-ignore-case | |
94 | ;; Name change for consistency with other option names. | |
95 | (or (memq ls-lisp-emulation '(MS-Windows MacOS)) | |
96 | (and (boundp 'ls-lisp-dired-ignore-case) ls-lisp-dired-ignore-case)) | |
97 | "*Non-nil causes ls-lisp alphabetic sorting to ignore case." | |
98 | :type 'boolean | |
99 | :group 'ls-lisp) | |
100 | ||
101 | (defcustom ls-lisp-dirs-first (eq ls-lisp-emulation 'MS-Windows) | |
102 | "*Non-nil causes ls-lisp to sort directories first in any ordering. | |
103 | \(Or last if it is reversed.) Follows Microsoft Windows Explorer." | |
104 | ;; Functionality suggested by Chris McMahan <cmcmahan@one.net> | |
105 | :type 'boolean | |
106 | :group 'ls-lisp) | |
107 | ||
108 | (defcustom ls-lisp-verbosity | |
109 | (cond ((eq ls-lisp-emulation 'MacOS) nil) | |
110 | ((eq ls-lisp-emulation 'MS-Windows) | |
111 | (if (and (fboundp 'w32-using-nt) (w32-using-nt)) | |
112 | '(links))) ; distinguish NT/2K from 9x | |
113 | ((eq ls-lisp-emulation 'UNIX) '(links uid)) ; UNIX ls | |
114 | (t '(links uid gid))) ; GNU ls | |
115 | "*A list of optional file attributes that ls-lisp should display. | |
116 | It should contain none or more of the symbols: links, uid, gid. | |
f0529b5b | 117 | nil (or an empty list) means display none of them. |
3f51f5a9 EZ |
118 | |
119 | Concepts come from UNIX: `links' means count of names associated with | |
120 | the file\; `uid' means user (owner) identifier\; `gid' means group | |
121 | identifier. | |
122 | ||
123 | If emulation is MacOS then default is nil\; | |
124 | if emulation is MS-Windows then default is `(links)' if platform is | |
125 | Windows NT/2K, nil otherwise\; | |
126 | if emulation is UNIX then default is `(links uid)'\; | |
127 | if emulation is GNU then default is `(links uid gid)'." | |
128 | ;; Functionality suggested by Howard Melman <howard@silverstream.com> | |
129 | :type '(set (const :tag "Show Link Count" links) | |
130 | (const :tag "Show User" uid) | |
131 | (const :tag "Show Group" gid)) | |
132 | :group 'ls-lisp) | |
133 | ||
73916123 MR |
134 | (defcustom ls-lisp-use-insert-directory-program |
135 | (not (memq system-type '(macos ms-dos windows-nt))) | |
3f51f5a9 | 136 | "*Non-nil causes ls-lisp to revert back to using `insert-directory-program'. |
0cb0ba6c GV |
137 | This is useful on platforms where ls-lisp is dumped into Emacs, such as |
138 | Microsoft Windows, but you would still like to use a program to list | |
3f51f5a9 EZ |
139 | the contents of a directory." |
140 | :type 'boolean | |
141 | :group 'ls-lisp) | |
142 | ||
73916123 MR |
143 | ;;; Autoloaded because it is let-bound in `recover-session', `mail-recover-1'. |
144 | ;;;###autoload | |
3f51f5a9 EZ |
145 | (defcustom ls-lisp-support-shell-wildcards t |
146 | "*Non-nil means ls-lisp treats file patterns as shell wildcards. | |
147 | Otherwise they are treated as Emacs regexps (for backward compatibility)." | |
148 | :type 'boolean | |
149 | :group 'ls-lisp) | |
150 | ||
2686cdc0 RS |
151 | (defcustom ls-lisp-format-time-list |
152 | '("%b %e %H:%M" | |
153 | "%b %e %Y") | |
154 | "*List of `format-time-string' specs to display file time stamps. | |
155 | They are used whenever a locale is not specified to use instead. | |
156 | ||
157 | Syntax: (EARLY-TIME-FORMAT OLD-TIME-FORMAT) | |
158 | ||
159 | The EARLY-TIME-FORMAT is used if file has been modified within the | |
160 | current year. The OLD-TIME-FORMAT is used for older files. To use ISO | |
161 | 8601 dates, you could set: | |
162 | ||
163 | \(setq ls-lisp-format-time-list | |
164 | '(\"%Y-%m-%d %H:%M\" | |
165 | \"%Y-%m-%d \"))" | |
401c1968 MR |
166 | :type '(list (string :tag "Early time format") |
167 | (string :tag "Old time format")) | |
2686cdc0 RS |
168 | :group 'ls-lisp) |
169 | ||
44a9ca8b RS |
170 | (defvar original-insert-directory nil |
171 | "This holds the original function definition of `insert-directory'.") | |
172 | ||
3f51f5a9 EZ |
173 | ;; Remember the original insert-directory function |
174 | (or (featurep 'ls-lisp) ; FJW: unless this file is being reloaded! | |
44a9ca8b | 175 | (setq original-insert-directory (symbol-function 'insert-directory))) |
3f51f5a9 EZ |
176 | |
177 | ;; This stub is to allow ls-lisp to parse symbolic links via another | |
178 | ;; library such as w32-symlinks.el from | |
1e05661f | 179 | ;; http://centaur.maths.qmw.ac.uk/Emacs/: |
3f51f5a9 EZ |
180 | (defun ls-lisp-parse-symlink (file-name) |
181 | "This stub may be redefined to parse FILE-NAME as a symlink. | |
182 | It should return nil or the link target as a string." | |
183 | nil) | |
0cb0ba6c | 184 | |
3f51f5a9 EZ |
185 | \f |
186 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
0cb0ba6c GV |
187 | |
188 | (defun insert-directory (file switches &optional wildcard full-directory-p) | |
189 | "Insert directory listing for FILE, formatted according to SWITCHES. | |
190 | Leaves point after the inserted text. | |
191 | SWITCHES may be a string of options, or a list of strings. | |
192 | Optional third arg WILDCARD means treat FILE as shell wildcard. | |
193 | Optional fourth arg FULL-DIRECTORY-P means file is a directory and | |
194 | switches do not contain `d', so that a full listing is expected. | |
195 | ||
3f51f5a9 EZ |
196 | This version of the function comes from `ls-lisp.el'. |
197 | If the value of `ls-lisp-use-insert-directory-program' is non-nil then | |
198 | it works exactly like the version from `files.el' and runs a directory | |
199 | listing program whose name is in the variable | |
200 | `insert-directory-program'; if also WILDCARD is non-nil then it runs | |
201 | the shell specified by `shell-file-name'. If the value of | |
202 | `ls-lisp-use-insert-directory-program' is nil then it runs a Lisp | |
203 | emulation. | |
204 | ||
205 | The Lisp emulation does not run any external programs or shells. It | |
206 | supports ordinary shell wildcards if `ls-lisp-support-shell-wildcards' | |
207 | is non-nil; otherwise, it interprets wildcards as regular expressions | |
208 | to match file names. It does not support all `ls' switches -- those | |
209 | that work are: A a c i r S s t u U X g G B C R and F partly." | |
0cb0ba6c | 210 | (if ls-lisp-use-insert-directory-program |
44a9ca8b RS |
211 | (funcall original-insert-directory |
212 | file switches wildcard full-directory-p) | |
3f51f5a9 EZ |
213 | ;; We need the directory in order to find the right handler. |
214 | (let ((handler (find-file-name-handler (expand-file-name file) | |
6b61353c KH |
215 | 'insert-directory)) |
216 | wildcard-regexp) | |
3f51f5a9 EZ |
217 | (if handler |
218 | (funcall handler 'insert-directory file switches | |
219 | wildcard full-directory-p) | |
d4939c66 JPW |
220 | ;; Remove --dired switch |
221 | (if (string-match "--dired " switches) | |
222 | (setq switches (replace-match "" nil nil switches))) | |
3f51f5a9 EZ |
223 | ;; Convert SWITCHES to a list of characters. |
224 | (setq switches (delete ?- (append switches nil))) | |
6b61353c KH |
225 | ;; Sometimes we get ".../foo*/" as FILE. While the shell and |
226 | ;; `ls' don't mind, we certainly do, because it makes us think | |
227 | ;; there is no wildcard, only a directory name. | |
228 | (if (and ls-lisp-support-shell-wildcards | |
229 | (string-match "[[?*]" file)) | |
230 | (progn | |
231 | (or (not (eq (aref file (1- (length file))) ?/)) | |
232 | (setq file (substring file 0 (1- (length file))))) | |
233 | (setq wildcard t))) | |
3f51f5a9 | 234 | (if wildcard |
6b61353c | 235 | (setq wildcard-regexp |
3f51f5a9 EZ |
236 | (if ls-lisp-support-shell-wildcards |
237 | (wildcard-to-regexp (file-name-nondirectory file)) | |
238 | (file-name-nondirectory file)) | |
239 | file (file-name-directory file)) | |
6b61353c | 240 | (if (memq ?B switches) (setq wildcard-regexp "[^~]\\'"))) |
3f51f5a9 EZ |
241 | (ls-lisp-insert-directory |
242 | file switches (ls-lisp-time-index switches) | |
6b61353c | 243 | wildcard-regexp full-directory-p) |
7f1b5edc EZ |
244 | ;; Try to insert the amount of free space. |
245 | (save-excursion | |
246 | (goto-char (point-min)) | |
247 | ;; First find the line to put it on. | |
248 | (when (re-search-forward "^total" nil t) | |
249 | (let ((available (get-free-disk-space "."))) | |
250 | (when available | |
7ad0c1c3 EZ |
251 | ;; Replace "total" with "total used", to avoid confusion. |
252 | (replace-match "total used in directory") | |
7f1b5edc EZ |
253 | (end-of-line) |
254 | (insert " available " available))))))))) | |
3f51f5a9 EZ |
255 | |
256 | (defun ls-lisp-insert-directory | |
6b61353c | 257 | (file switches time-index wildcard-regexp full-directory-p) |
3045b163 | 258 | "Insert directory listing for FILE, formatted according to SWITCHES. |
3f51f5a9 EZ |
259 | Leaves point after the inserted text. This is an internal function |
260 | optionally called by the `ls-lisp.el' version of `insert-directory'. | |
261 | It is called recursively if the -R switch is used. | |
262 | SWITCHES is a *list* of characters. TIME-INDEX is the time index into | |
6b61353c | 263 | file-attributes according to SWITCHES. WILDCARD-REGEXP is nil or an *Emacs |
3f51f5a9 EZ |
264 | regexp*. FULL-DIRECTORY-P means file is a directory and SWITCHES does |
265 | not contain `d', so that a full listing is expected." | |
6b61353c | 266 | (if (or wildcard-regexp full-directory-p) |
3f51f5a9 EZ |
267 | (let* ((dir (file-name-as-directory file)) |
268 | (default-directory dir) ; so that file-attributes works | |
269 | (file-alist | |
6b61353c | 270 | (directory-files-and-attributes dir nil wildcard-regexp t 'string)) |
3f51f5a9 EZ |
271 | (now (current-time)) |
272 | (sum 0) | |
273 | ;; do all bindings here for speed | |
274 | total-line files elt short file-size fil attr) | |
275 | (cond ((memq ?A switches) | |
276 | (setq file-alist | |
277 | (ls-lisp-delete-matching "^\\.\\.?$" file-alist))) | |
278 | ((not (memq ?a switches)) | |
279 | ;; if neither -A nor -a, flush . files | |
280 | (setq file-alist | |
281 | (ls-lisp-delete-matching "^\\." file-alist)))) | |
282 | (setq file-alist | |
283 | (ls-lisp-handle-switches file-alist switches)) | |
284 | (if (memq ?C switches) ; column (-C) format | |
285 | (ls-lisp-column-format file-alist) | |
286 | (setq total-line (cons (point) (car-safe file-alist))) | |
287 | (setq files file-alist) | |
288 | (while files ; long (-l) format | |
289 | (setq elt (car files) | |
290 | files (cdr files) | |
291 | short (car elt) | |
292 | attr (cdr elt) | |
293 | file-size (nth 7 attr)) | |
294 | (and attr | |
295 | (setq sum (+ file-size | |
296 | ;; Even if neither SUM nor file's size | |
297 | ;; overflow, their sum could. | |
298 | (if (or (< sum (- 134217727 file-size)) | |
299 | (floatp sum) | |
300 | (floatp file-size)) | |
301 | sum | |
302 | (float sum)))) | |
303 | (insert (ls-lisp-format short attr file-size | |
304 | switches time-index now)))) | |
305 | ;; Insert total size of all files: | |
306 | (save-excursion | |
307 | (goto-char (car total-line)) | |
308 | (or (cdr total-line) | |
309 | ;; Shell says ``No match'' if no files match | |
310 | ;; the wildcard; let's say something similar. | |
311 | (insert "(No match)\n")) | |
312 | (insert (format "total %.0f\n" (fceiling (/ sum 1024.0)))))) | |
313 | (if (memq ?R switches) | |
314 | ;; List the contents of all directories recursively. | |
315 | ;; cadr of each element of `file-alist' is t for | |
316 | ;; directory, string (name linked to) for symbolic | |
317 | ;; link, or nil. | |
9dce08b6 RS |
318 | (while file-alist |
319 | (setq elt (car file-alist) | |
3f51f5a9 EZ |
320 | file-alist (cdr file-alist)) |
321 | (when (and (eq (cadr elt) t) ; directory | |
322 | (not (string-match "\\`\\.\\.?\\'" (car elt)))) | |
323 | (setq elt (expand-file-name (car elt) dir)) | |
324 | (insert "\n" elt ":\n") | |
325 | (ls-lisp-insert-directory | |
6b61353c | 326 | elt switches time-index wildcard-regexp full-directory-p))))) |
3f51f5a9 EZ |
327 | ;; If not full-directory-p, FILE *must not* end in /, as |
328 | ;; file-attributes will not recognize a symlink to a directory, | |
329 | ;; so must make it a relative filename as ls does: | |
330 | (if (eq (aref file (1- (length file))) ?/) | |
331 | (setq file (substring file 0 -1))) | |
6b61353c | 332 | (let ((fattr (file-attributes file 'string))) |
3f51f5a9 EZ |
333 | (if fattr |
334 | (insert (ls-lisp-format file fattr (nth 7 fattr) | |
335 | switches time-index (current-time))) | |
336 | (message "%s: doesn't exist or is inaccessible" file) | |
337 | (ding) (sit-for 2))))) ; to show user the message! | |
338 | ||
339 | (defun ls-lisp-column-format (file-alist) | |
340 | "Insert the file names (only) in FILE-ALIST into the current buffer. | |
341 | Format in columns, sorted vertically, following GNU ls -C. | |
342 | Responds to the window width as ls should but may not!" | |
343 | (let (files fmt ncols collen (nfiles 0) (colwid 0)) | |
344 | ;; Count number of files as `nfiles', build list of filenames as | |
345 | ;; `files', and find maximum filename length as `colwid': | |
346 | (let (file len) | |
347 | (while file-alist | |
348 | (setq nfiles (1+ nfiles) | |
349 | file (caar file-alist) | |
350 | files (cons file files) | |
351 | file-alist (cdr file-alist) | |
352 | len (length file)) | |
353 | (if (> len colwid) (setq colwid len)))) | |
354 | (setq files (nreverse files) | |
355 | colwid (+ 2 colwid) ; 2 character column gap | |
356 | fmt (format "%%-%ds" colwid) ; print format | |
357 | ncols (/ (window-width) colwid) ; no of columns | |
358 | collen (/ nfiles ncols)) ; floor of column length | |
359 | (if (> nfiles (* collen ncols)) (setq collen (1+ collen))) | |
360 | ;; Output the file names in columns, sorted vertically: | |
361 | (let ((i 0) j) | |
362 | (while (< i collen) | |
363 | (setq j i) | |
364 | (while (< j nfiles) | |
365 | (insert (format fmt (nth j files))) | |
366 | (setq j (+ j collen))) | |
367 | ;; FJW: This is completely unnecessary, but I don't like | |
368 | ;; trailing white space... | |
369 | (delete-region (point) (progn (skip-chars-backward " \t") (point))) | |
370 | (insert ?\n) | |
371 | (setq i (1+ i)))))) | |
9dce08b6 RS |
372 | |
373 | (defun ls-lisp-delete-matching (regexp list) | |
3f51f5a9 | 374 | "Delete all elements matching REGEXP from LIST, return new list." |
d6d472d5 | 375 | ;; Should perhaps use setcdr for efficiency. |
6467926f SK |
376 | (let (result) |
377 | (while list | |
3f51f5a9 | 378 | (or (string-match regexp (caar list)) |
6467926f SK |
379 | (setq result (cons (car list) result))) |
380 | (setq list (cdr list))) | |
381 | result)) | |
382 | ||
3f51f5a9 EZ |
383 | (defsubst ls-lisp-string-lessp (s1 s2) |
384 | "Return t if string S1 is less than string S2 in lexicographic order. | |
385 | Case is significant if `ls-lisp-ignore-case' is nil. | |
386 | Unibyte strings are converted to multibyte for comparison." | |
387 | (let ((u (compare-strings s1 0 nil s2 0 nil ls-lisp-ignore-case))) | |
388 | (and (numberp u) (< u 0)))) | |
389 | ||
9dce08b6 | 390 | (defun ls-lisp-handle-switches (file-alist switches) |
3f51f5a9 EZ |
391 | "Return new FILE-ALIST sorted according to SWITCHES. |
392 | SWITCHES is a list of characters. Default sorting is alphabetic." | |
6467926f | 393 | ;; FILE-ALIST's elements are (FILE . FILE-ATTRIBUTES). |
3f51f5a9 EZ |
394 | (or (memq ?U switches) ; unsorted |
395 | ;; Catch and ignore unexpected sorting errors | |
396 | (condition-case err | |
397 | (setq file-alist | |
398 | (let (index) | |
399 | ;; Copy file-alist in case of error | |
400 | (sort (copy-sequence file-alist) ; modifies its argument! | |
401 | (cond ((memq ?S switches) | |
402 | (lambda (x y) ; sorted on size | |
403 | ;; 7th file attribute is file size | |
404 | ;; Make largest file come first | |
405 | (< (nth 7 (cdr y)) | |
406 | (nth 7 (cdr x))))) | |
407 | ((setq index (ls-lisp-time-index switches)) | |
408 | (lambda (x y) ; sorted on time | |
409 | (ls-lisp-time-lessp (nth index (cdr y)) | |
410 | (nth index (cdr x))))) | |
411 | ((memq ?X switches) | |
412 | (lambda (x y) ; sorted on extension | |
413 | (ls-lisp-string-lessp | |
414 | (ls-lisp-extension (car x)) | |
415 | (ls-lisp-extension (car y))))) | |
416 | (t | |
417 | (lambda (x y) ; sorted alphabetically | |
418 | (ls-lisp-string-lessp (car x) (car y)))))))) | |
419 | (error (message "Unsorted (ls-lisp sorting error) - %s" | |
420 | (error-message-string err)) | |
421 | (ding) (sit-for 2)))) ; to show user the message! | |
422 | (if (memq ?F switches) ; classify switch | |
423 | (setq file-alist (mapcar 'ls-lisp-classify file-alist))) | |
424 | (if ls-lisp-dirs-first | |
425 | ;; Re-sort directories first, without otherwise changing the | |
426 | ;; ordering, and reverse whole list. cadr of each element of | |
427 | ;; `file-alist' is t for directory, string (name linked to) for | |
428 | ;; symbolic link, or nil. | |
429 | (let (el dirs files) | |
430 | (while file-alist | |
431 | (if (eq (cadr (setq el (car file-alist))) t) ; directory | |
432 | (setq dirs (cons el dirs)) | |
433 | (setq files (cons el files))) | |
434 | (setq file-alist (cdr file-alist))) | |
435 | (setq file-alist | |
436 | (if (memq ?U switches) ; unsorted order is reversed | |
437 | (nconc dirs files) | |
438 | (nconc files dirs) | |
439 | )))) | |
440 | ;; Finally reverse file alist if necessary. | |
441 | ;; (eq below MUST compare `(not (memq ...))' to force comparison of | |
442 | ;; `t' or `nil', rather than list tails!) | |
443 | (if (eq (eq (not (memq ?U switches)) ; unsorted order is reversed | |
444 | (not (memq ?r switches))) ; reversed sort order requested | |
445 | ls-lisp-dirs-first) ; already reversed | |
446 | (nreverse file-alist) | |
447 | file-alist)) | |
448 | ||
449 | (defun ls-lisp-classify (filedata) | |
450 | "Append a character to each file name indicating the file type. | |
451 | Also, for regular files that are executable, append `*'. | |
452 | The file type indicators are `/' for directories, `@' for symbolic | |
453 | links, `|' for FIFOs, `=' for sockets, and nothing for regular files. | |
454 | \[But FIFOs and sockets are not recognised.] | |
455 | FILEDATA has the form (filename . `file-attributes'). Its `cadr' is t | |
456 | for directory, string (name linked to) for symbolic link, or nil." | |
457 | (let ((dir (cadr filedata)) (file-name (car filedata))) | |
458 | (cond ((or dir | |
459 | ;; Parsing .lnk files here is perhaps overkill! | |
460 | (setq dir (ls-lisp-parse-symlink file-name))) | |
461 | (cons | |
462 | (concat file-name (if (eq dir t) "/" "@")) | |
463 | (cdr filedata))) | |
464 | ((string-match "x" (nth 9 filedata)) | |
465 | (cons | |
466 | (concat file-name "*") | |
467 | (cdr filedata))) | |
468 | (t filedata)))) | |
469 | ||
470 | (defun ls-lisp-extension (filename) | |
471 | "Return extension of FILENAME (ignoring any version extension) | |
472 | FOLLOWED by null and full filename, SOLELY for full alpha sort." | |
473 | ;; Force extension sort order: `no ext' then `null ext' then `ext' | |
474 | ;; to agree with GNU ls. | |
475 | (concat | |
476 | (let* ((i (length filename)) end) | |
477 | (if (= (aref filename (1- i)) ?.) ; null extension | |
478 | "\0" | |
479 | (while (and (>= (setq i (1- i)) 0) | |
480 | (/= (aref filename i) ?.))) | |
481 | (if (< i 0) "\0\0" ; no extension | |
482 | (if (/= (aref filename (1+ i)) ?~) | |
483 | (substring filename (1+ i)) | |
484 | ;; version extension found -- ignore it | |
485 | (setq end i) | |
486 | (while (and (>= (setq i (1- i)) 0) | |
487 | (/= (aref filename i) ?.))) | |
488 | (if (< i 0) "\0\0" ; no extension | |
489 | (substring filename (1+ i) end)))) | |
490 | )) "\0" filename)) | |
d88c0e93 | 491 | |
e54241c5 | 492 | ;; From Roland McGrath. Can use this to sort on time. |
9dce08b6 | 493 | (defun ls-lisp-time-lessp (time0 time1) |
3f51f5a9 EZ |
494 | "Return t if time TIME0 is earlier than time TIME1." |
495 | (let ((hi0 (car time0)) (hi1 (car time1))) | |
e54241c5 SK |
496 | (or (< hi0 hi1) |
497 | (and (= hi0 hi1) | |
3f51f5a9 EZ |
498 | (< (cadr time0) (cadr time1)))))) |
499 | ||
500 | (defun ls-lisp-format (file-name file-attr file-size switches time-index now) | |
501 | "Format one line of long ls output for file FILE-NAME. | |
502 | FILE-ATTR and FILE-SIZE give the file's attributes and size. | |
503 | SWITCHES, TIME-INDEX and NOW give the full switch list and time data." | |
504 | (let ((file-type (nth 0 file-attr)) | |
505 | ;; t for directory, string (name linked to) | |
506 | ;; for symbolic link, or nil. | |
507 | (drwxrwxrwx (nth 8 file-attr))) ; attribute string ("drwxrwxrwx") | |
508 | (and (null file-type) | |
509 | ;; Maybe no kernel support for symlinks, so... | |
510 | (setq file-type (ls-lisp-parse-symlink file-name)) | |
511 | (aset drwxrwxrwx 0 ?l)) ; symbolic link - update attribute string | |
6467926f | 512 | (concat (if (memq ?i switches) ; inode number |
3f51f5a9 | 513 | (format " %6d" (nth 10 file-attr))) |
d6d472d5 | 514 | ;; nil is treated like "" in concat |
6467926f | 515 | (if (memq ?s switches) ; size in K |
3f51f5a9 EZ |
516 | (format " %4.0f" (fceiling (/ file-size 1024.0)))) |
517 | drwxrwxrwx ; attribute string | |
518 | (if (memq 'links ls-lisp-verbosity) | |
519 | (format " %3d" (nth 1 file-attr))) ; link count | |
520 | ;; Numeric uid/gid are more confusing than helpful; | |
6467926f | 521 | ;; Emacs should be able to make strings of them. |
3f51f5a9 EZ |
522 | ;; They tend to be bogus on non-UNIX platforms anyway so |
523 | ;; optionally hide them. | |
524 | (if (memq 'uid ls-lisp-verbosity) | |
6b61353c | 525 | ;; uid can be a sting or an integer |
3f51f5a9 | 526 | (let ((uid (nth 2 file-attr))) |
6b61353c | 527 | (format (if (stringp uid) " %-8s" " %-8d") uid))) |
3f51f5a9 EZ |
528 | (if (not (memq ?G switches)) ; GNU ls -- shows group by default |
529 | (if (or (memq ?g switches) ; UNIX ls -- no group by default | |
530 | (memq 'gid ls-lisp-verbosity)) | |
6b61353c KH |
531 | (let ((gid (nth 3 file-attr))) |
532 | (format (if (stringp gid) " %-8s" " %-8d") gid)))) | |
a3723f13 | 533 | (ls-lisp-format-file-size file-size (memq ?h switches)) |
3f51f5a9 EZ |
534 | " " |
535 | (ls-lisp-format-time file-attr time-index now) | |
738eb4e7 | 536 | " " |
d88c0e93 SK |
537 | file-name |
538 | (if (stringp file-type) ; is a symbolic link | |
3f51f5a9 | 539 | (concat " -> " file-type)) |
d88c0e93 SK |
540 | "\n" |
541 | ))) | |
542 | ||
9dce08b6 | 543 | (defun ls-lisp-time-index (switches) |
3f51f5a9 EZ |
544 | "Return time index into file-attributes according to ls SWITCHES list. |
545 | Return nil if no time switch found." | |
546 | ;; FJW: Default of nil is IMPORTANT and used in `ls-lisp-handle-switches'! | |
547 | (cond ((memq ?c switches) 6) ; last mode change | |
548 | ((memq ?t switches) 5) ; last modtime | |
549 | ((memq ?u switches) 4))) ; last access | |
550 | ||
d81a647c PE |
551 | (defun ls-lisp-time-to-seconds (time) |
552 | "Convert TIME to a floating point number." | |
553 | (+ (* (car time) 65536.0) | |
554 | (cadr time) | |
555 | (/ (or (nth 2 time) 0) 1000000.0))) | |
556 | ||
3f51f5a9 EZ |
557 | (defun ls-lisp-format-time (file-attr time-index now) |
558 | "Format time for file with attributes FILE-ATTR according to TIME-INDEX. | |
559 | Use the same method as ls to decide whether to show time-of-day or year, | |
560 | depending on distance between file date and NOW. | |
561 | All ls time options, namely c, t and u, are handled." | |
562 | (let* ((time (nth (or time-index 5) file-attr)) ; default is last modtime | |
d81a647c PE |
563 | (diff (- (ls-lisp-time-to-seconds time) |
564 | (ls-lisp-time-to-seconds now))) | |
565 | ;; Consider a time to be recent if it is within the past six | |
566 | ;; months. A Gregorian year has 365.2425 * 24 * 60 * 60 == | |
567 | ;; 31556952 seconds on the average, and half of that is 15778476. | |
568 | ;; Write the constant explicitly to avoid roundoff error. | |
569 | (past-cutoff -15778476)) ; half a Gregorian year | |
f8a10234 | 570 | (condition-case nil |
d81a647c PE |
571 | ;; Use traditional time format in the C or POSIX locale, |
572 | ;; ISO-style time format otherwise, so columns line up. | |
573 | (let ((locale system-time-locale)) | |
574 | (if (not locale) | |
575 | (let ((vars '("LC_ALL" "LC_TIME" "LANG"))) | |
576 | (while (and vars (not (setq locale (getenv (car vars))))) | |
577 | (setq vars (cdr vars))))) | |
578 | (if (member locale '("C" "POSIX")) | |
579 | (setq locale nil)) | |
580 | (format-time-string | |
581 | (if (and (<= past-cutoff diff) (<= diff 0)) | |
2686cdc0 RS |
582 | (if locale "%m-%d %H:%M" (nth 0 ls-lisp-format-time-list)) |
583 | (if locale "%Y-%m-%d " (nth 1 ls-lisp-format-time-list))) | |
d81a647c | 584 | time)) |
452e47d7 | 585 | (error "Unk 0 0000")))) |
738eb4e7 | 586 | |
a3723f13 JB |
587 | (defun ls-lisp-format-file-size (file-size human-readable) |
588 | (if (or (not human-readable) | |
589 | (< file-size 1024)) | |
590 | (format (if (floatp file-size) " %8.0f" " %8d") file-size) | |
591 | (do ((file-size (/ file-size 1024.0) (/ file-size 1024.0)) | |
592 | ;; kilo, mega, giga, tera, peta, exa | |
593 | (post-fixes (list "k" "M" "G" "T" "P" "E") (cdr post-fixes))) | |
594 | ((< file-size 1024) (format " %7.0f%s" file-size (car post-fixes)))))) | |
595 | ||
9dce08b6 | 596 | (provide 'ls-lisp) |
738eb4e7 | 597 | |
6b61353c | 598 | ;;; arch-tag: e55f399b-05ec-425c-a6d5-f5e349c35ab4 |
76550a57 | 599 | ;;; ls-lisp.el ends here |