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