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