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