1 ;;; ls-lisp.el --- emulate insert-directory completely in Emacs Lisp
3 ;; Copyright (C) 1992, 1994, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008, 2009, 2010 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>
9 ;; Keywords: unix, dired
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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 ;; 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
42 ;; RESTRICTIONS ======================================================
44 ;; * A few obscure ls switches are still ignored: see the docstring of
45 ;; `insert-directory'.
47 ;; TO DO =============================================================
49 ;; Complete handling of F switch (if/when possible).
51 ;; FJW: May be able to sort much faster by consing the sort key onto
52 ;; the front of each list element, sorting and then stripping the key
57 ;; Written originally by Sebastian Kremer <sk@thp.uni-koeln.de>
58 ;; Revised by Andrew Innes and Geoff Volker (and maybe others).
60 ;; Modified by Francis J. Wright <F.J.Wright@maths.qmw.ac.uk>, mainly
61 ;; to support many more ls options, "platform emulation" and more
66 (eval-when-compile (require 'cl
))
69 "Emulate the ls program completely in Emacs Lisp."
73 (defun ls-lisp-set-options ()
74 "Reset the ls-lisp options that depend on `ls-lisp-emulation'."
75 (mapc 'custom-reevaluate-setting
76 '(ls-lisp-ignore-case ls-lisp-dirs-first ls-lisp-verbosity
)))
78 (defcustom ls-lisp-emulation
79 (cond ;; ((eq system-type 'windows-nt) 'MS-Windows)
80 ((memq system-type
'(hpux usg-unix-v 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: nil, `MacOS', `MS-Windows', `UNIX'.
85 Set this to your preferred value; it need not match the actual platform
88 This variable does not affect the behavior of ls-lisp directly.
89 Rather, it controls the default values for some variables that do:
90 `ls-lisp-ignore-case', `ls-lisp-dirs-first', and `ls-lisp-verbosity'.
92 If you change this variable directly (without using customize)
93 after loading `ls-lisp', you should use `ls-lisp-set-options' to
94 update the dependent variables."
95 :type
'(choice (const :tag
"GNU" nil
)
99 :initialize
'custom-initialize-default
100 :set
(lambda (symbol value
)
101 (unless (equal value
(eval symbol
))
102 (custom-set-default symbol value
)
103 (ls-lisp-set-options)))
106 (defcustom ls-lisp-ignore-case
107 ;; Name change for consistency with other option names.
108 (or (memq ls-lisp-emulation
'(MS-Windows MacOS
))
109 (and (boundp 'ls-lisp-dired-ignore-case
) ls-lisp-dired-ignore-case
))
110 "Non-nil causes ls-lisp alphabetic sorting to ignore case."
111 :set-after
'(ls-lisp-emulation)
115 (defcustom ls-lisp-dirs-first
(eq ls-lisp-emulation
'MS-Windows
)
116 "Non-nil causes ls-lisp to sort directories first in any ordering.
117 \(Or last if it is reversed.) Follows Microsoft Windows Explorer."
118 ;; Functionality suggested by Chris McMahan <cmcmahan@one.net>
119 :set-after
'(ls-lisp-emulation)
123 (defcustom ls-lisp-verbosity
124 (cond ((eq ls-lisp-emulation
'MacOS
) nil
)
125 ((eq ls-lisp-emulation
'MS-Windows
)
126 (if (and (fboundp 'w32-using-nt
) (w32-using-nt))
127 '(links))) ; distinguish NT/2K from 9x
128 ((eq ls-lisp-emulation
'UNIX
) '(links uid
)) ; UNIX ls
129 (t '(links uid gid
))) ; GNU ls
130 "A list of optional file attributes that ls-lisp should display.
131 It should contain none or more of the symbols: links, uid, gid.
132 A value of nil (or an empty list) means display none of them.
134 Concepts come from UNIX: `links' means count of names associated with
135 the file; `uid' means user (owner) identifier; `gid' means group
138 If emulation is MacOS then default is nil;
139 if emulation is MS-Windows then default is `(links)' if platform is
140 Windows NT/2K, nil otherwise;
141 if emulation is UNIX then default is `(links uid)';
142 if emulation is GNU then default is `(links uid gid)'."
143 :set-after
'(ls-lisp-emulation)
144 ;; Functionality suggested by Howard Melman <howard@silverstream.com>
145 :type
'(set (const :tag
"Show Link Count" links
)
146 (const :tag
"Show User" uid
)
147 (const :tag
"Show Group" gid
))
150 (defcustom ls-lisp-use-insert-directory-program
151 (not (memq system-type
'(ms-dos windows-nt
)))
152 "Non-nil causes ls-lisp to revert back to using `insert-directory-program'.
153 This is useful on platforms where ls-lisp is dumped into Emacs, such as
154 Microsoft Windows, but you would still like to use a program to list
155 the contents of a directory."
159 ;;; Autoloaded because it is let-bound in `recover-session', `mail-recover-1'.
161 (defcustom ls-lisp-support-shell-wildcards t
162 "Non-nil means ls-lisp treats file patterns as shell wildcards.
163 Otherwise they are treated as Emacs regexps (for backward compatibility)."
167 (defcustom ls-lisp-format-time-list
170 "List of `format-time-string' specs to display file time stamps.
171 These specs are used ONLY if a valid locale can not be determined.
173 If `ls-lisp-use-localized-time-format' is non-nil, these specs are used
174 regardless of whether the locale can be determined.
176 Syntax: (EARLY-TIME-FORMAT OLD-TIME-FORMAT)
178 The EARLY-TIME-FORMAT is used if file has been modified within the
179 current year. The OLD-TIME-FORMAT is used for older files. To use ISO
180 8601 dates, you could set:
182 \(setq ls-lisp-format-time-list
185 :type
'(list (string :tag
"Early time format")
186 (string :tag
"Old time format"))
189 (defcustom ls-lisp-use-localized-time-format nil
190 "Non-nil means to always use `ls-lisp-format-time-list' for time stamps.
191 This applies even if a valid locale is specified.
193 WARNING: Using localized date/time format might cause Dired columns
194 to fail to line up, e.g. if month names are not all of the same length."
198 (defvar original-insert-directory nil
199 "This holds the original function definition of `insert-directory'.")
201 (defvar ls-lisp-uid-d-fmt
"-%d"
202 "Format to display integer UIDs.")
203 (defvar ls-lisp-uid-s-fmt
"-%s"
204 "Format to display user names.")
205 (defvar ls-lisp-gid-d-fmt
"-%d"
206 "Format to display integer GIDs.")
207 (defvar ls-lisp-gid-s-fmt
"-%s"
208 "Format to display user group names.")
209 (defvar ls-lisp-filesize-d-fmt
"%d"
210 "Format to display integer file sizes.")
211 (defvar ls-lisp-filesize-f-fmt
"%.0f"
212 "Format to display float file sizes.")
214 ;; Remember the original insert-directory function
215 (or (featurep 'ls-lisp
) ; FJW: unless this file is being reloaded!
216 (setq original-insert-directory
(symbol-function 'insert-directory
)))
219 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
221 (defun insert-directory (file switches
&optional wildcard full-directory-p
)
222 "Insert directory listing for FILE, formatted according to SWITCHES.
223 Leaves point after the inserted text.
224 SWITCHES may be a string of options, or a list of strings.
225 Optional third arg WILDCARD means treat FILE as shell wildcard.
226 Optional fourth arg FULL-DIRECTORY-P means file is a directory and
227 switches do not contain `d', so that a full listing is expected.
229 This version of the function comes from `ls-lisp.el'.
230 If the value of `ls-lisp-use-insert-directory-program' is non-nil then
231 it works exactly like the version from `files.el' and runs a directory
232 listing program whose name is in the variable
233 `insert-directory-program'; if also WILDCARD is non-nil then it runs
234 the shell specified by `shell-file-name'. If the value of
235 `ls-lisp-use-insert-directory-program' is nil then it runs a Lisp
238 The Lisp emulation does not run any external programs or shells. It
239 supports ordinary shell wildcards if `ls-lisp-support-shell-wildcards'
240 is non-nil; otherwise, it interprets wildcards as regular expressions
241 to match file names. It does not support all `ls' switches -- those
242 that work are: A a B C c F G g h i n R r S s t U u X. The l switch
243 is assumed to be always present and cannot be turned off."
244 (if ls-lisp-use-insert-directory-program
245 (funcall original-insert-directory
246 file switches wildcard full-directory-p
)
247 ;; We need the directory in order to find the right handler.
248 (let ((handler (find-file-name-handler (expand-file-name file
)
253 (funcall handler
'insert-directory file switches
254 wildcard full-directory-p
)
255 ;; Remove --dired switch
256 (if (string-match "--dired " switches
)
257 (setq switches
(replace-match "" nil nil switches
)))
258 ;; Convert SWITCHES to a list of characters.
259 (setq switches
(delete ?\
(delete ?-
(append switches nil
))))
260 ;; Sometimes we get ".../foo*/" as FILE. While the shell and
261 ;; `ls' don't mind, we certainly do, because it makes us think
262 ;; there is no wildcard, only a directory name.
263 (if (and ls-lisp-support-shell-wildcards
264 (string-match "[[?*]" file
)
265 ;; Prefer an existing file to wildcards, like
266 ;; dired-noselect does.
267 (not (file-exists-p file
)))
269 (or (not (eq (aref file
(1- (length file
))) ?
/))
270 (setq file
(substring file
0 (1- (length file
)))))
273 (setq wildcard-regexp
274 (if ls-lisp-support-shell-wildcards
275 (wildcard-to-regexp (file-name-nondirectory file
))
276 (file-name-nondirectory file
))
277 file
(file-name-directory file
))
278 (if (memq ?B switches
) (setq wildcard-regexp
"[^~]\\'")))
280 (ls-lisp-insert-directory
281 file switches
(ls-lisp-time-index switches
)
282 wildcard-regexp full-directory-p
)
284 ;; Maybe they wanted a literal file that just happens to
285 ;; use characters special to shell wildcards.
286 (if (equal (cadr err
) "Unmatched [ or [^")
288 (setq wildcard-regexp
(if (memq ?B switches
) "[^~]\\'")
289 file
(file-relative-name orig-file
))
290 (ls-lisp-insert-directory
291 file switches
(ls-lisp-time-index switches
)
292 nil full-directory-p
))
293 (signal (car err
) (cdr err
)))))
294 ;; Try to insert the amount of free space.
296 (goto-char (point-min))
297 ;; First find the line to put it on.
298 (when (re-search-forward "^total" nil t
)
299 (let ((available (get-free-disk-space ".")))
301 ;; Replace "total" with "total used", to avoid confusion.
302 (replace-match "total used in directory")
304 (insert " available " available
)))))))))
306 (defun ls-lisp-insert-directory
307 (file switches time-index wildcard-regexp full-directory-p
)
308 "Insert directory listing for FILE, formatted according to SWITCHES.
309 Leaves point after the inserted text. This is an internal function
310 optionally called by the `ls-lisp.el' version of `insert-directory'.
311 It is called recursively if the -R switch is used.
312 SWITCHES is a *list* of characters. TIME-INDEX is the time index into
313 file-attributes according to SWITCHES. WILDCARD-REGEXP is nil or an *Emacs
314 regexp*. FULL-DIRECTORY-P means file is a directory and SWITCHES does
315 not contain `d', so that a full listing is expected."
316 (if (or wildcard-regexp full-directory-p
)
317 (let* ((dir (file-name-as-directory file
))
318 (default-directory dir
) ; so that file-attributes works
320 (directory-files-and-attributes dir nil wildcard-regexp t
321 (if (memq ?n switches
)
328 ;; do all bindings here for speed
329 total-line files elt short file-size fil attr
330 fuid fgid uid-len gid-len
)
331 (cond ((memq ?A switches
)
333 (ls-lisp-delete-matching "^\\.\\.?$" file-alist
)))
334 ((not (memq ?a switches
))
335 ;; if neither -A nor -a, flush . files
337 (ls-lisp-delete-matching "^\\." file-alist
))))
339 (ls-lisp-handle-switches file-alist switches
))
340 (if (memq ?C switches
) ; column (-C) format
341 (ls-lisp-column-format file-alist
)
342 (setq total-line
(cons (point) (car-safe file-alist
)))
343 ;; Find the appropriate format for displaying uid, gid, and
344 ;; file size, by finding the longest strings among all the
345 ;; files we are about to display.
346 (dolist (elt file-alist
)
349 uid-len
(if (stringp fuid
) (string-width fuid
)
350 (length (format "%d" fuid
)))
352 gid-len
(if (stringp fgid
) (string-width fgid
)
353 (length (format "%d" fgid
)))
354 file-size
(nth 7 attr
))
355 (if (> uid-len max-uid-len
)
356 (setq max-uid-len uid-len
))
357 (if (> gid-len max-gid-len
)
358 (setq max-gid-len gid-len
))
359 (if (> file-size max-file-size
)
360 (setq max-file-size file-size
)))
361 (setq ls-lisp-uid-d-fmt
(format " %%-%dd" max-uid-len
))
362 (setq ls-lisp-uid-s-fmt
(format " %%-%ds" max-uid-len
))
363 (setq ls-lisp-gid-d-fmt
(format " %%-%dd" max-gid-len
))
364 (setq ls-lisp-gid-s-fmt
(format " %%-%ds" max-gid-len
))
365 (setq ls-lisp-filesize-d-fmt
367 (if (memq ?s switches
)
368 (length (format "%.0f"
369 (fceiling (/ max-file-size
1024.0))))
370 (length (format "%.0f" max-file-size
)))))
371 (setq ls-lisp-filesize-f-fmt
373 (if (memq ?s switches
)
374 (length (format "%.0f"
375 (fceiling (/ max-file-size
1024.0))))
376 (length (format "%.0f" max-file-size
)))))
377 (setq files file-alist
)
378 (while files
; long (-l) format
379 (setq elt
(car files
)
383 file-size
(nth 7 attr
))
385 (setq sum
(+ file-size
386 ;; Even if neither SUM nor file's size
387 ;; overflow, their sum could.
388 (if (or (< sum
(- 134217727 file-size
))
393 (insert (ls-lisp-format short attr file-size
394 switches time-index
))))
395 ;; Insert total size of all files:
397 (goto-char (car total-line
))
399 ;; Shell says ``No match'' if no files match
400 ;; the wildcard; let's say something similar.
401 (insert "(No match)\n"))
402 (insert (format "total %.0f\n" (fceiling (/ sum
1024.0))))))
403 (if (memq ?R switches
)
404 ;; List the contents of all directories recursively.
405 ;; cadr of each element of `file-alist' is t for
406 ;; directory, string (name linked to) for symbolic
409 (setq elt
(car file-alist
)
410 file-alist
(cdr file-alist
))
411 (when (and (eq (cadr elt
) t
) ; directory
412 ;; Under -F, we have already decorated all
413 ;; directories, including "." and "..", with
414 ;; a /, so allow for that as well.
415 (not (string-match "\\`\\.\\.?/?\\'" (car elt
))))
416 (setq elt
(expand-file-name (car elt
) dir
))
417 (insert "\n" elt
":\n")
418 (ls-lisp-insert-directory
419 elt switches time-index wildcard-regexp full-directory-p
)))))
420 ;; If not full-directory-p, FILE *must not* end in /, as
421 ;; file-attributes will not recognize a symlink to a directory,
422 ;; so must make it a relative filename as ls does:
423 (if (file-name-absolute-p file
) (setq file
(expand-file-name file
)))
424 (if (eq (aref file
(1- (length file
))) ?
/)
425 (setq file
(substring file
0 -
1)))
426 (let ((fattr (file-attributes file
'string
)))
428 (insert (ls-lisp-format
429 (if (memq ?F switches
)
430 (ls-lisp-classify-file file fattr
)
433 switches time-index
))
434 (message "%s: doesn't exist or is inaccessible" file
)
435 (ding) (sit-for 2))))) ; to show user the message!
437 (defun ls-lisp-column-format (file-alist)
438 "Insert the file names (only) in FILE-ALIST into the current buffer.
439 Format in columns, sorted vertically, following GNU ls -C.
440 Responds to the window width as ls should but may not!"
441 (let (files fmt ncols collen
(nfiles 0) (colwid 0))
442 ;; Count number of files as `nfiles', build list of filenames as
443 ;; `files', and find maximum filename length as `colwid':
446 (setq nfiles
(1+ nfiles
)
447 file
(caar file-alist
)
448 files
(cons file files
)
449 file-alist
(cdr file-alist
)
451 (if (> len colwid
) (setq colwid len
))))
452 (setq files
(nreverse files
)
453 colwid
(+ 2 colwid
) ; 2 character column gap
454 fmt
(format "%%-%ds" colwid
) ; print format
455 ncols
(/ (window-width) colwid
) ; no of columns
456 collen
(/ nfiles ncols
)) ; floor of column length
457 (if (> nfiles
(* collen ncols
)) (setq collen
(1+ collen
)))
458 ;; Output the file names in columns, sorted vertically:
463 (insert (format fmt
(nth j files
)))
464 (setq j
(+ j collen
)))
465 ;; FJW: This is completely unnecessary, but I don't like
466 ;; trailing white space...
467 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
471 (defun ls-lisp-delete-matching (regexp list
)
472 "Delete all elements matching REGEXP from LIST, return new list."
473 ;; Should perhaps use setcdr for efficiency.
476 (or (string-match regexp
(caar list
))
477 (setq result
(cons (car list
) result
)))
478 (setq list
(cdr list
)))
481 (defsubst ls-lisp-string-lessp
(s1 s2
)
482 "Return t if string S1 is less than string S2 in lexicographic order.
483 Case is significant if `ls-lisp-ignore-case' is nil.
484 Unibyte strings are converted to multibyte for comparison."
485 (let ((u (compare-strings s1
0 nil s2
0 nil ls-lisp-ignore-case
)))
486 (and (numberp u
) (< u
0))))
488 (defun ls-lisp-handle-switches (file-alist switches
)
489 "Return new FILE-ALIST sorted according to SWITCHES.
490 SWITCHES is a list of characters. Default sorting is alphabetic."
491 ;; FILE-ALIST's elements are (FILE . FILE-ATTRIBUTES).
492 (or (memq ?U switches
) ; unsorted
493 ;; Catch and ignore unexpected sorting errors
497 ;; Copy file-alist in case of error
498 (sort (copy-sequence file-alist
) ; modifies its argument!
499 (cond ((memq ?S switches
)
500 (lambda (x y
) ; sorted on size
501 ;; 7th file attribute is file size
502 ;; Make largest file come first
505 ((setq index
(ls-lisp-time-index switches
))
506 (lambda (x y
) ; sorted on time
507 (time-less-p (nth index
(cdr y
))
508 (nth index
(cdr x
)))))
510 (lambda (x y
) ; sorted on extension
511 (ls-lisp-string-lessp
512 (ls-lisp-extension (car x
))
513 (ls-lisp-extension (car y
)))))
515 (lambda (x y
) ; sorted alphabetically
516 (ls-lisp-string-lessp (car x
) (car y
))))))))
517 (error (message "Unsorted (ls-lisp sorting error) - %s"
518 (error-message-string err
))
519 (ding) (sit-for 2)))) ; to show user the message!
520 (if (memq ?F switches
) ; classify switch
521 (setq file-alist
(mapcar 'ls-lisp-classify file-alist
)))
522 (if ls-lisp-dirs-first
523 ;; Re-sort directories first, without otherwise changing the
524 ;; ordering, and reverse whole list. cadr of each element of
525 ;; `file-alist' is t for directory, string (name linked to) for
526 ;; symbolic link, or nil.
529 (if (or (eq (cadr (setq el
(car file-alist
))) t
) ; directory
530 (and (stringp (cadr el
))
531 (file-directory-p (cadr el
)))) ; symlink to a directory
532 (setq dirs
(cons el dirs
))
533 (setq files
(cons el files
)))
534 (setq file-alist
(cdr file-alist
)))
536 (if (memq ?U switches
) ; unsorted order is reversed
540 ;; Finally reverse file alist if necessary.
541 ;; (eq below MUST compare `(not (memq ...))' to force comparison of
542 ;; `t' or `nil', rather than list tails!)
543 (if (eq (eq (not (memq ?U switches
)) ; unsorted order is reversed
544 (not (memq ?r switches
))) ; reversed sort order requested
545 ls-lisp-dirs-first
) ; already reversed
546 (nreverse file-alist
)
549 (defun ls-lisp-classify-file (filename fattr
)
550 "Append a character to FILENAME indicating the file type.
552 FATTR is the file attributes returned by `file-attributes' for the file.
553 The file type indicators are `/' for directories, `@' for symbolic
554 links, `|' for FIFOs, `=' for sockets, `*' for regular files that
555 are executable, and nothing for other types of files."
556 (let* ((type (car fattr
))
557 (modestr (nth 8 fattr
))
558 (typestr (substring modestr
0 1)))
561 (concat filename
(if (eq type t
) "/" "@")))
562 ((string-match "x" modestr
)
563 (concat filename
"*"))
564 ((string= "p" typestr
)
565 (concat filename
"|"))
566 ((string= "s" typestr
)
567 (concat filename
"="))
570 (defun ls-lisp-classify (filedata)
571 "Append a character to file name in FILEDATA indicating the file type.
573 FILEDATA has the form (FILENAME . ATTRIBUTES), where ATTRIBUTES is the
574 structure returned by `file-attributes' for that file.
576 The file type indicators are `/' for directories, `@' for symbolic
577 links, `|' for FIFOs, `=' for sockets, `*' for regular files that
578 are executable, and nothing for other types of files."
579 (let ((file-name (car filedata
))
580 (fattr (cdr filedata
)))
581 (setq file-name
(propertize file-name
'dired-filename t
))
582 (cons (ls-lisp-classify-file file-name fattr
) fattr
)))
584 (defun ls-lisp-extension (filename)
585 "Return extension of FILENAME (ignoring any version extension)
586 FOLLOWED by null and full filename, SOLELY for full alpha sort."
587 ;; Force extension sort order: `no ext' then `null ext' then `ext'
588 ;; to agree with GNU ls.
590 (let* ((i (length filename
)) end
)
591 (if (= (aref filename
(1- i
)) ?.
) ; null extension
593 (while (and (>= (setq i
(1- i
)) 0)
594 (/= (aref filename i
) ?.
)))
595 (if (< i
0) "\0\0" ; no extension
596 (if (/= (aref filename
(1+ i
)) ?~
)
597 (substring filename
(1+ i
))
598 ;; version extension found -- ignore it
600 (while (and (>= (setq i
(1- i
)) 0)
601 (/= (aref filename i
) ?.
)))
602 (if (< i
0) "\0\0" ; no extension
603 (substring filename
(1+ i
) end
))))
606 (defun ls-lisp-format (file-name file-attr file-size switches time-index
)
607 "Format one line of long ls output for file FILE-NAME.
608 FILE-ATTR and FILE-SIZE give the file's attributes and size.
609 SWITCHES and TIME-INDEX give the full switch list and time data."
610 (let ((file-type (nth 0 file-attr
))
611 ;; t for directory, string (name linked to)
612 ;; for symbolic link, or nil.
613 (drwxrwxrwx (nth 8 file-attr
))) ; attribute string ("drwxrwxrwx")
614 (concat (if (memq ?i switches
) ; inode number
615 (let ((inode (nth 10 file-attr
)))
617 (if (consp (cdr inode
))
618 ;; 2^(24+16) = 1099511627776.0, but
619 ;; multiplying by it and then adding the
620 ;; other members of the cons cell in one go
621 ;; loses precision, since a double does not
622 ;; have enough significant digits to hold a
623 ;; full 64-bit value. So below we split
624 ;; 1099511627776 into high 13 and low 5
625 ;; digits and compute in two parts.
626 (let ((p1 (* (car inode
) 10995116.0))
627 (p2 (+ (* (car inode
) 27776.0)
628 (* (cadr inode
) 65536.0)
630 (format " %13.0f%05.0f "
631 ;; Use floor to emulate integer
633 (+ p1
(floor p2
100000.0))
636 (+ (* (car inode
) 65536.0)
638 (format " %18d " inode
))))
639 ;; nil is treated like "" in concat
640 (if (memq ?s switches
) ; size in K
641 (format ls-lisp-filesize-f-fmt
642 (fceiling (/ file-size
1024.0))))
643 drwxrwxrwx
; attribute string
644 (if (memq 'links ls-lisp-verbosity
)
645 (format "%3d" (nth 1 file-attr
))) ; link count
646 ;; Numeric uid/gid are more confusing than helpful;
647 ;; Emacs should be able to make strings of them.
648 ;; They tend to be bogus on non-UNIX platforms anyway so
649 ;; optionally hide them.
650 (if (memq 'uid ls-lisp-verbosity
)
651 ;; uid can be a string or an integer
652 (let ((uid (nth 2 file-attr
)))
653 (format (if (stringp uid
)
657 (if (not (memq ?G switches
)) ; GNU ls -- shows group by default
658 (if (or (memq ?g switches
) ; UNIX ls -- no group by default
659 (memq 'gid ls-lisp-verbosity
))
660 (let ((gid (nth 3 file-attr
)))
661 (format (if (stringp gid
)
665 (ls-lisp-format-file-size file-size
(memq ?h switches
))
667 (ls-lisp-format-time file-attr time-index
)
669 (if (not (memq ?F switches
)) ; ls-lisp-classify already did that
670 (propertize file-name
'dired-filename t
)
672 (if (stringp file-type
) ; is a symbolic link
673 (concat " -> " file-type
))
677 (defun ls-lisp-time-index (switches)
678 "Return time index into file-attributes according to ls SWITCHES list.
679 Return nil if no time switch found."
680 ;; FJW: Default of nil is IMPORTANT and used in `ls-lisp-handle-switches'!
681 (cond ((memq ?c switches
) 6) ; last mode change
682 ((memq ?t switches
) 5) ; last modtime
683 ((memq ?u switches
) 4))) ; last access
685 (defun ls-lisp-format-time (file-attr time-index
)
686 "Format time for file with attributes FILE-ATTR according to TIME-INDEX.
687 Use the same method as ls to decide whether to show time-of-day or year,
688 depending on distance between file date and the current time.
689 All ls time options, namely c, t and u, are handled."
690 (let* ((time (nth (or time-index
5) file-attr
)) ; default is last modtime
691 (diff (- (float-time time
) (float-time)))
692 ;; Consider a time to be recent if it is within the past six
693 ;; months. A Gregorian year has 365.2425 * 24 * 60 * 60 ==
694 ;; 31556952 seconds on the average, and half of that is 15778476.
695 ;; Write the constant explicitly to avoid roundoff error.
696 (past-cutoff -
15778476)) ; half a Gregorian year
698 ;; Use traditional time format in the C or POSIX locale,
699 ;; ISO-style time format otherwise, so columns line up.
700 (let ((locale system-time-locale
))
702 (let ((vars '("LC_ALL" "LC_TIME" "LANG")))
703 (while (and vars
(not (setq locale
(getenv (car vars
)))))
704 (setq vars
(cdr vars
)))))
705 (if (member locale
'("C" "POSIX"))
708 (if (and (<= past-cutoff diff
) (<= diff
0))
709 (if (and locale
(not ls-lisp-use-localized-time-format
))
711 (nth 0 ls-lisp-format-time-list
))
712 (if (and locale
(not ls-lisp-use-localized-time-format
))
714 (nth 1 ls-lisp-format-time-list
)))
716 (error "Unk 0 0000"))))
718 (defun ls-lisp-format-file-size (file-size human-readable
)
719 (if (not human-readable
)
720 (format (if (floatp file-size
)
721 ls-lisp-filesize-f-fmt
722 ls-lisp-filesize-d-fmt
)
724 (if (< file-size
1024)
725 (format " %4d" file-size
)
726 (do ((file-size (/ file-size
1024.0) (/ file-size
1024.0))
727 ;; kilo, mega, giga, tera, peta, exa
728 (post-fixes (list "k" "M" "G" "T" "P" "E") (cdr post-fixes
)))
730 (format " %3.0f%s" file-size
(car post-fixes
)))))))
734 ;;; ls-lisp.el ends here