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