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