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