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