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