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