(custom-face-attributes): Handle roman slant.
[bpt/emacs.git] / lisp / ls-lisp.el
CommitLineData
76550a57
ER
1;;; ls-lisp.el --- emulate insert-directory completely in Emacs Lisp
2
c90f2757 3;; Copyright (C) 1992, 1994, 2000, 2001, 2002, 2003, 2004,
409cc4a3 4;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
b578f267 5
55535639
PJ
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
d88c0e93 10
b578f267 11;; This file is part of GNU Emacs.
d88c0e93 12
b578f267 13;; GNU Emacs is free software; you can redistribute it and/or modify
d88c0e93 14;; it under the terms of the GNU General Public License as published by
b4aa6026 15;; the Free Software Foundation; either version 3, or (at your option)
d88c0e93 16;; any later version.
b578f267
EN
17
18;; GNU Emacs is distributed in the hope that it will be useful,
d88c0e93
SK
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.
b578f267 22
d88c0e93 23;; You should have received a copy of the GNU General Public License
b578f267 24;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
25;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26;; Boston, MA 02110-1301, USA.
b578f267
EN
27
28;;; Commentary:
738eb4e7 29
3f51f5a9 30;; OVERVIEW ==========================================================
d9a0f717 31
3f51f5a9
EZ
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.
738eb4e7 37
3f51f5a9
EZ
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$'.
738eb4e7 42
3f51f5a9 43;; RESTRICTIONS ======================================================
738eb4e7 44
3f51f5a9
EZ
45;; * A few obscure ls switches are still ignored: see the docstring of
46;; `insert-directory'.
d88c0e93 47
3f51f5a9 48;; TO DO =============================================================
738eb4e7 49
3f51f5a9 50;; Complete handling of F switch (if/when possible).
738eb4e7 51
3f51f5a9
EZ
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!
738eb4e7 55
3f51f5a9 56;;; History:
76550a57 57
3f51f5a9
EZ
58;; Written originally by Sebastian Kremer <sk@thp.uni-koeln.de>
59;; Revised by Andrew Innes and Geoff Volker (and maybe others).
3045b163 60
3f51f5a9 61;; Modified by Francis J. Wright <F.J.Wright@maths.qmw.ac.uk>, mainly
5eba3394
LH
62;; to support many more ls options, "platform emulation" and more
63;; robust sorting.
3f51f5a9
EZ
64
65;;; Code:
97b927b3 66
a3723f13
JB
67(eval-when-compile (require 'cl))
68
3f51f5a9
EZ
69(defgroup ls-lisp nil
70 "Emulate the ls program completely in Emacs Lisp."
eff409ba 71 :version "21.1"
3f51f5a9
EZ
72 :group 'dired)
73
74(defcustom ls-lisp-emulation
75 (cond ((eq system-type 'macos) 'MacOS)
76 ;; ((eq system-type 'windows-nt) 'MS-Windows)
77 ((memq system-type
f3041af1 78 '(hpux usg-unix-v unisoft-unix irix berkeley-unix))
3f51f5a9
EZ
79 'UNIX)) ; very similar to GNU
80 ;; Anything else defaults to nil, meaning GNU.
81 "*Platform to emulate: GNU (default), MacOS, MS-Windows, UNIX.
82Corresponding value is one of the atoms: nil, MacOS, MS-Windows, UNIX.
83Sets default values for: `ls-lisp-ignore-case', `ls-lisp-dirs-first',
84`ls-lisp-verbosity'. Need not match actual platform. Changing this
85option 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)
91
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)
99
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)
106
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.
115It should contain none or more of the symbols: links, uid, gid.
3aeb19ad 116A value of nil (or an empty list) means display none of them.
3f51f5a9
EZ
117
118Concepts come from UNIX: `links' means count of names associated with
119the file\; `uid' means user (owner) identifier\; `gid' means group
120identifier.
121
122If emulation is MacOS then default is nil\;
123if emulation is MS-Windows then default is `(links)' if platform is
124Windows NT/2K, nil otherwise\;
125if emulation is UNIX then default is `(links uid)'\;
126if 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)
132
73916123
MR
133(defcustom ls-lisp-use-insert-directory-program
134 (not (memq system-type '(macos ms-dos windows-nt)))
3f51f5a9 135 "*Non-nil causes ls-lisp to revert back to using `insert-directory-program'.
0cb0ba6c
GV
136This is useful on platforms where ls-lisp is dumped into Emacs, such as
137Microsoft Windows, but you would still like to use a program to list
3f51f5a9
EZ
138the contents of a directory."
139 :type 'boolean
140 :group 'ls-lisp)
141
73916123
MR
142;;; Autoloaded because it is let-bound in `recover-session', `mail-recover-1'.
143;;;###autoload
3f51f5a9
EZ
144(defcustom ls-lisp-support-shell-wildcards t
145 "*Non-nil means ls-lisp treats file patterns as shell wildcards.
146Otherwise they are treated as Emacs regexps (for backward compatibility)."
147 :type 'boolean
148 :group 'ls-lisp)
149
2686cdc0
RS
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.
ea88e775
EZ
154These specs are used ONLY if a valid locale can not be determined.
155
156If `ls-lisp-use-localized-time-format' is non-nil, these specs are used
157regardless of whether the locale can be determined.
2686cdc0
RS
158
159Syntax: (EARLY-TIME-FORMAT OLD-TIME-FORMAT)
160
161The EARLY-TIME-FORMAT is used if file has been modified within the
162current year. The OLD-TIME-FORMAT is used for older files. To use ISO
1638601 dates, you could set:
164
165\(setq ls-lisp-format-time-list
166 '(\"%Y-%m-%d %H:%M\"
167 \"%Y-%m-%d \"))"
401c1968
MR
168 :type '(list (string :tag "Early time format")
169 (string :tag "Old time format"))
2686cdc0
RS
170 :group 'ls-lisp)
171
ea88e775
EZ
172(defcustom ls-lisp-use-localized-time-format nil
173 "*Non-nil causes ls-lisp to use `ls-lisp-format-time-list' even if
174a valid locale is specified.
175
176WARNING: Using localized date/time format might cause Dired columns
177to fail to lign up, e.g. if month names are not all of the same length."
178 :type 'boolean
179 :group 'ls-lisp)
180
44a9ca8b
RS
181(defvar original-insert-directory nil
182 "This holds the original function definition of `insert-directory'.")
183
3f51f5a9
EZ
184;; Remember the original insert-directory function
185(or (featurep 'ls-lisp) ; FJW: unless this file is being reloaded!
44a9ca8b 186 (setq original-insert-directory (symbol-function 'insert-directory)))
3f51f5a9 187
3f51f5a9
EZ
188\f
189;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
0cb0ba6c
GV
190
191(defun insert-directory (file switches &optional wildcard full-directory-p)
192 "Insert directory listing for FILE, formatted according to SWITCHES.
193Leaves point after the inserted text.
194SWITCHES may be a string of options, or a list of strings.
195Optional third arg WILDCARD means treat FILE as shell wildcard.
196Optional fourth arg FULL-DIRECTORY-P means file is a directory and
197switches do not contain `d', so that a full listing is expected.
198
3f51f5a9
EZ
199This version of the function comes from `ls-lisp.el'.
200If the value of `ls-lisp-use-insert-directory-program' is non-nil then
201it works exactly like the version from `files.el' and runs a directory
202listing program whose name is in the variable
203`insert-directory-program'; if also WILDCARD is non-nil then it runs
204the shell specified by `shell-file-name'. If the value of
205`ls-lisp-use-insert-directory-program' is nil then it runs a Lisp
206emulation.
207
208The Lisp emulation does not run any external programs or shells. It
209supports ordinary shell wildcards if `ls-lisp-support-shell-wildcards'
210is non-nil; otherwise, it interprets wildcards as regular expressions
211to match file names. It does not support all `ls' switches -- those
de4db6f1 212that work are: A a c i r S s t u U X g G B C R n and F partly."
0cb0ba6c 213 (if ls-lisp-use-insert-directory-program
44a9ca8b
RS
214 (funcall original-insert-directory
215 file switches wildcard full-directory-p)
3f51f5a9
EZ
216 ;; We need the directory in order to find the right handler.
217 (let ((handler (find-file-name-handler (expand-file-name file)
94e72368 218 'insert-directory))
43bed668 219 (orig-file file)
94e72368 220 wildcard-regexp)
3f51f5a9
EZ
221 (if handler
222 (funcall handler 'insert-directory file switches
223 wildcard full-directory-p)
d4939c66
JPW
224 ;; Remove --dired switch
225 (if (string-match "--dired " switches)
226 (setq switches (replace-match "" nil nil switches)))
3f51f5a9
EZ
227 ;; Convert SWITCHES to a list of characters.
228 (setq switches (delete ?- (append switches nil)))
94e72368
RS
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
9b40e204 233 (string-match "[[?*]" file)
43bed668 234 ;; Prefer an existing file to wildcards, like
9b40e204 235 ;; dired-noselect does.
43bed668 236 (not (file-exists-p file)))
94e72368
RS
237 (progn
238 (or (not (eq (aref file (1- (length file))) ?/))
239 (setq file (substring file 0 (1- (length file)))))
240 (setq wildcard t)))
3f51f5a9 241 (if wildcard
94e72368 242 (setq wildcard-regexp
3f51f5a9
EZ
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))
94e72368 247 (if (memq ?B switches) (setq wildcard-regexp "[^~]\\'")))
43bed668
EZ
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)))))
7f1b5edc
EZ
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
7ad0c1c3
EZ
270 ;; Replace "total" with "total used", to avoid confusion.
271 (replace-match "total used in directory")
7f1b5edc
EZ
272 (end-of-line)
273 (insert " available " available)))))))))
3f51f5a9
EZ
274
275(defun ls-lisp-insert-directory
94e72368 276 (file switches time-index wildcard-regexp full-directory-p)
3045b163 277 "Insert directory listing for FILE, formatted according to SWITCHES.
3f51f5a9
EZ
278Leaves point after the inserted text. This is an internal function
279optionally called by the `ls-lisp.el' version of `insert-directory'.
280It is called recursively if the -R switch is used.
281SWITCHES is a *list* of characters. TIME-INDEX is the time index into
94e72368 282file-attributes according to SWITCHES. WILDCARD-REGEXP is nil or an *Emacs
3f51f5a9
EZ
283regexp*. FULL-DIRECTORY-P means file is a directory and SWITCHES does
284not contain `d', so that a full listing is expected."
94e72368 285 (if (or wildcard-regexp full-directory-p)
3f51f5a9
EZ
286 (let* ((dir (file-name-as-directory file))
287 (default-directory dir) ; so that file-attributes works
288 (file-alist
de4db6f1
EZ
289 (directory-files-and-attributes dir nil wildcard-regexp t
290 (if (memq ?n switches)
291 'integer
292 'string)))
3f51f5a9
EZ
293 (now (current-time))
294 (sum 0)
295 ;; do all bindings here for speed
296 total-line files elt short file-size fil attr)
297 (cond ((memq ?A switches)
298 (setq file-alist
299 (ls-lisp-delete-matching "^\\.\\.?$" file-alist)))
300 ((not (memq ?a switches))
301 ;; if neither -A nor -a, flush . files
302 (setq file-alist
303 (ls-lisp-delete-matching "^\\." file-alist))))
304 (setq file-alist
305 (ls-lisp-handle-switches file-alist switches))
306 (if (memq ?C switches) ; column (-C) format
307 (ls-lisp-column-format file-alist)
308 (setq total-line (cons (point) (car-safe file-alist)))
309 (setq files file-alist)
310 (while files ; long (-l) format
311 (setq elt (car files)
312 files (cdr files)
313 short (car elt)
314 attr (cdr elt)
315 file-size (nth 7 attr))
316 (and attr
317 (setq sum (+ file-size
318 ;; Even if neither SUM nor file's size
319 ;; overflow, their sum could.
320 (if (or (< sum (- 134217727 file-size))
321 (floatp sum)
322 (floatp file-size))
323 sum
324 (float sum))))
325 (insert (ls-lisp-format short attr file-size
326 switches time-index now))))
327 ;; Insert total size of all files:
328 (save-excursion
329 (goto-char (car total-line))
330 (or (cdr total-line)
331 ;; Shell says ``No match'' if no files match
332 ;; the wildcard; let's say something similar.
333 (insert "(No match)\n"))
334 (insert (format "total %.0f\n" (fceiling (/ sum 1024.0))))))
335 (if (memq ?R switches)
336 ;; List the contents of all directories recursively.
337 ;; cadr of each element of `file-alist' is t for
338 ;; directory, string (name linked to) for symbolic
339 ;; link, or nil.
9dce08b6
RS
340 (while file-alist
341 (setq elt (car file-alist)
3f51f5a9
EZ
342 file-alist (cdr file-alist))
343 (when (and (eq (cadr elt) t) ; directory
344 (not (string-match "\\`\\.\\.?\\'" (car elt))))
345 (setq elt (expand-file-name (car elt) dir))
346 (insert "\n" elt ":\n")
347 (ls-lisp-insert-directory
94e72368 348 elt switches time-index wildcard-regexp full-directory-p)))))
3f51f5a9
EZ
349 ;; If not full-directory-p, FILE *must not* end in /, as
350 ;; file-attributes will not recognize a symlink to a directory,
351 ;; so must make it a relative filename as ls does:
352 (if (eq (aref file (1- (length file))) ?/)
353 (setq file (substring file 0 -1)))
80ca5799 354 (let ((fattr (file-attributes file 'string)))
3f51f5a9
EZ
355 (if fattr
356 (insert (ls-lisp-format file fattr (nth 7 fattr)
357 switches time-index (current-time)))
358 (message "%s: doesn't exist or is inaccessible" file)
359 (ding) (sit-for 2))))) ; to show user the message!
360
361(defun ls-lisp-column-format (file-alist)
362 "Insert the file names (only) in FILE-ALIST into the current buffer.
363Format in columns, sorted vertically, following GNU ls -C.
364Responds to the window width as ls should but may not!"
365 (let (files fmt ncols collen (nfiles 0) (colwid 0))
366 ;; Count number of files as `nfiles', build list of filenames as
367 ;; `files', and find maximum filename length as `colwid':
368 (let (file len)
369 (while file-alist
370 (setq nfiles (1+ nfiles)
371 file (caar file-alist)
372 files (cons file files)
373 file-alist (cdr file-alist)
374 len (length file))
375 (if (> len colwid) (setq colwid len))))
376 (setq files (nreverse files)
377 colwid (+ 2 colwid) ; 2 character column gap
378 fmt (format "%%-%ds" colwid) ; print format
379 ncols (/ (window-width) colwid) ; no of columns
380 collen (/ nfiles ncols)) ; floor of column length
381 (if (> nfiles (* collen ncols)) (setq collen (1+ collen)))
382 ;; Output the file names in columns, sorted vertically:
383 (let ((i 0) j)
384 (while (< i collen)
385 (setq j i)
386 (while (< j nfiles)
387 (insert (format fmt (nth j files)))
388 (setq j (+ j collen)))
389 ;; FJW: This is completely unnecessary, but I don't like
390 ;; trailing white space...
391 (delete-region (point) (progn (skip-chars-backward " \t") (point)))
392 (insert ?\n)
393 (setq i (1+ i))))))
9dce08b6
RS
394
395(defun ls-lisp-delete-matching (regexp list)
3f51f5a9 396 "Delete all elements matching REGEXP from LIST, return new list."
d6d472d5 397 ;; Should perhaps use setcdr for efficiency.
6467926f
SK
398 (let (result)
399 (while list
3f51f5a9 400 (or (string-match regexp (caar list))
6467926f
SK
401 (setq result (cons (car list) result)))
402 (setq list (cdr list)))
403 result))
404
3f51f5a9
EZ
405(defsubst ls-lisp-string-lessp (s1 s2)
406 "Return t if string S1 is less than string S2 in lexicographic order.
407Case is significant if `ls-lisp-ignore-case' is nil.
408Unibyte strings are converted to multibyte for comparison."
409 (let ((u (compare-strings s1 0 nil s2 0 nil ls-lisp-ignore-case)))
410 (and (numberp u) (< u 0))))
411
9dce08b6 412(defun ls-lisp-handle-switches (file-alist switches)
3f51f5a9
EZ
413 "Return new FILE-ALIST sorted according to SWITCHES.
414SWITCHES is a list of characters. Default sorting is alphabetic."
6467926f 415 ;; FILE-ALIST's elements are (FILE . FILE-ATTRIBUTES).
3f51f5a9
EZ
416 (or (memq ?U switches) ; unsorted
417 ;; Catch and ignore unexpected sorting errors
418 (condition-case err
419 (setq file-alist
420 (let (index)
421 ;; Copy file-alist in case of error
422 (sort (copy-sequence file-alist) ; modifies its argument!
423 (cond ((memq ?S switches)
424 (lambda (x y) ; sorted on size
425 ;; 7th file attribute is file size
426 ;; Make largest file come first
427 (< (nth 7 (cdr y))
428 (nth 7 (cdr x)))))
429 ((setq index (ls-lisp-time-index switches))
430 (lambda (x y) ; sorted on time
431 (ls-lisp-time-lessp (nth index (cdr y))
432 (nth index (cdr x)))))
433 ((memq ?X switches)
434 (lambda (x y) ; sorted on extension
435 (ls-lisp-string-lessp
436 (ls-lisp-extension (car x))
437 (ls-lisp-extension (car y)))))
438 (t
439 (lambda (x y) ; sorted alphabetically
440 (ls-lisp-string-lessp (car x) (car y))))))))
441 (error (message "Unsorted (ls-lisp sorting error) - %s"
442 (error-message-string err))
443 (ding) (sit-for 2)))) ; to show user the message!
444 (if (memq ?F switches) ; classify switch
445 (setq file-alist (mapcar 'ls-lisp-classify file-alist)))
446 (if ls-lisp-dirs-first
447 ;; Re-sort directories first, without otherwise changing the
448 ;; ordering, and reverse whole list. cadr of each element of
449 ;; `file-alist' is t for directory, string (name linked to) for
450 ;; symbolic link, or nil.
451 (let (el dirs files)
452 (while file-alist
5eba3394
LH
453 (if (or (eq (cadr (setq el (car file-alist))) t) ; directory
454 (and (stringp (cadr el))
455 (file-directory-p (cadr el)))) ; symlink to a directory
3f51f5a9
EZ
456 (setq dirs (cons el dirs))
457 (setq files (cons el files)))
458 (setq file-alist (cdr file-alist)))
459 (setq file-alist
460 (if (memq ?U switches) ; unsorted order is reversed
461 (nconc dirs files)
462 (nconc files dirs)
463 ))))
464 ;; Finally reverse file alist if necessary.
465 ;; (eq below MUST compare `(not (memq ...))' to force comparison of
466 ;; `t' or `nil', rather than list tails!)
467 (if (eq (eq (not (memq ?U switches)) ; unsorted order is reversed
468 (not (memq ?r switches))) ; reversed sort order requested
469 ls-lisp-dirs-first) ; already reversed
470 (nreverse file-alist)
471 file-alist))
472
473(defun ls-lisp-classify (filedata)
474 "Append a character to each file name indicating the file type.
475Also, for regular files that are executable, append `*'.
476The file type indicators are `/' for directories, `@' for symbolic
477links, `|' for FIFOs, `=' for sockets, and nothing for regular files.
71bf3033 478\[But FIFOs and sockets are not recognized.]
3f51f5a9
EZ
479FILEDATA has the form (filename . `file-attributes'). Its `cadr' is t
480for directory, string (name linked to) for symbolic link, or nil."
5eba3394
LH
481 (let ((file-name (car filedata))
482 (type (cadr filedata)))
483 (cond (type
3f51f5a9 484 (cons
5eba3394 485 (concat file-name (if (eq type t) "/" "@"))
3f51f5a9
EZ
486 (cdr filedata)))
487 ((string-match "x" (nth 9 filedata))
488 (cons
489 (concat file-name "*")
490 (cdr filedata)))
491 (t filedata))))
492
493(defun ls-lisp-extension (filename)
494 "Return extension of FILENAME (ignoring any version extension)
495FOLLOWED by null and full filename, SOLELY for full alpha sort."
496 ;; Force extension sort order: `no ext' then `null ext' then `ext'
497 ;; to agree with GNU ls.
498 (concat
499 (let* ((i (length filename)) end)
500 (if (= (aref filename (1- i)) ?.) ; null extension
501 "\0"
502 (while (and (>= (setq i (1- i)) 0)
503 (/= (aref filename i) ?.)))
504 (if (< i 0) "\0\0" ; no extension
505 (if (/= (aref filename (1+ i)) ?~)
506 (substring filename (1+ i))
507 ;; version extension found -- ignore it
508 (setq end i)
509 (while (and (>= (setq i (1- i)) 0)
510 (/= (aref filename i) ?.)))
511 (if (< i 0) "\0\0" ; no extension
512 (substring filename (1+ i) end))))
513 )) "\0" filename))
d88c0e93 514
e54241c5 515;; From Roland McGrath. Can use this to sort on time.
9dce08b6 516(defun ls-lisp-time-lessp (time0 time1)
3f51f5a9
EZ
517 "Return t if time TIME0 is earlier than time TIME1."
518 (let ((hi0 (car time0)) (hi1 (car time1)))
e54241c5
SK
519 (or (< hi0 hi1)
520 (and (= hi0 hi1)
3f51f5a9
EZ
521 (< (cadr time0) (cadr time1))))))
522
523(defun ls-lisp-format (file-name file-attr file-size switches time-index now)
524 "Format one line of long ls output for file FILE-NAME.
525FILE-ATTR and FILE-SIZE give the file's attributes and size.
526SWITCHES, TIME-INDEX and NOW give the full switch list and time data."
527 (let ((file-type (nth 0 file-attr))
528 ;; t for directory, string (name linked to)
529 ;; for symbolic link, or nil.
530 (drwxrwxrwx (nth 8 file-attr))) ; attribute string ("drwxrwxrwx")
6467926f 531 (concat (if (memq ?i switches) ; inode number
3f51f5a9 532 (format " %6d" (nth 10 file-attr)))
d6d472d5 533 ;; nil is treated like "" in concat
6467926f 534 (if (memq ?s switches) ; size in K
3f51f5a9
EZ
535 (format " %4.0f" (fceiling (/ file-size 1024.0))))
536 drwxrwxrwx ; attribute string
537 (if (memq 'links ls-lisp-verbosity)
538 (format " %3d" (nth 1 file-attr))) ; link count
539 ;; Numeric uid/gid are more confusing than helpful;
6467926f 540 ;; Emacs should be able to make strings of them.
3f51f5a9
EZ
541 ;; They tend to be bogus on non-UNIX platforms anyway so
542 ;; optionally hide them.
543 (if (memq 'uid ls-lisp-verbosity)
80ca5799 544 ;; uid can be a sting or an integer
3f51f5a9 545 (let ((uid (nth 2 file-attr)))
80ca5799 546 (format (if (stringp uid) " %-8s" " %-8d") uid)))
3f51f5a9
EZ
547 (if (not (memq ?G switches)) ; GNU ls -- shows group by default
548 (if (or (memq ?g switches) ; UNIX ls -- no group by default
549 (memq 'gid ls-lisp-verbosity))
80ca5799
LH
550 (let ((gid (nth 3 file-attr)))
551 (format (if (stringp gid) " %-8s" " %-8d") gid))))
a3723f13 552 (ls-lisp-format-file-size file-size (memq ?h switches))
3f51f5a9
EZ
553 " "
554 (ls-lisp-format-time file-attr time-index now)
738eb4e7 555 " "
b3df990b 556 (propertize file-name 'dired-filename t)
d88c0e93 557 (if (stringp file-type) ; is a symbolic link
3f51f5a9 558 (concat " -> " file-type))
d88c0e93
SK
559 "\n"
560 )))
561
9dce08b6 562(defun ls-lisp-time-index (switches)
3f51f5a9
EZ
563 "Return time index into file-attributes according to ls SWITCHES list.
564Return nil if no time switch found."
565 ;; FJW: Default of nil is IMPORTANT and used in `ls-lisp-handle-switches'!
566 (cond ((memq ?c switches) 6) ; last mode change
567 ((memq ?t switches) 5) ; last modtime
568 ((memq ?u switches) 4))) ; last access
569
d81a647c
PE
570(defun ls-lisp-time-to-seconds (time)
571 "Convert TIME to a floating point number."
572 (+ (* (car time) 65536.0)
573 (cadr time)
574 (/ (or (nth 2 time) 0) 1000000.0)))
575
3f51f5a9
EZ
576(defun ls-lisp-format-time (file-attr time-index now)
577 "Format time for file with attributes FILE-ATTR according to TIME-INDEX.
578Use the same method as ls to decide whether to show time-of-day or year,
579depending on distance between file date and NOW.
580All ls time options, namely c, t and u, are handled."
581 (let* ((time (nth (or time-index 5) file-attr)) ; default is last modtime
d81a647c
PE
582 (diff (- (ls-lisp-time-to-seconds time)
583 (ls-lisp-time-to-seconds now)))
584 ;; Consider a time to be recent if it is within the past six
585 ;; months. A Gregorian year has 365.2425 * 24 * 60 * 60 ==
586 ;; 31556952 seconds on the average, and half of that is 15778476.
587 ;; Write the constant explicitly to avoid roundoff error.
588 (past-cutoff -15778476)) ; half a Gregorian year
f8a10234 589 (condition-case nil
d81a647c
PE
590 ;; Use traditional time format in the C or POSIX locale,
591 ;; ISO-style time format otherwise, so columns line up.
592 (let ((locale system-time-locale))
593 (if (not locale)
594 (let ((vars '("LC_ALL" "LC_TIME" "LANG")))
595 (while (and vars (not (setq locale (getenv (car vars)))))
596 (setq vars (cdr vars)))))
597 (if (member locale '("C" "POSIX"))
598 (setq locale nil))
599 (format-time-string
600 (if (and (<= past-cutoff diff) (<= diff 0))
ea88e775
EZ
601 (if (and locale (not ls-lisp-use-localized-time-format))
602 "%m-%d %H:%M"
603 (nth 0 ls-lisp-format-time-list))
604 (if (and locale (not ls-lisp-use-localized-time-format))
605 "%Y-%m-%d "
606 (nth 1 ls-lisp-format-time-list)))
d81a647c 607 time))
452e47d7 608 (error "Unk 0 0000"))))
738eb4e7 609
a3723f13
JB
610(defun ls-lisp-format-file-size (file-size human-readable)
611 (if (or (not human-readable)
612 (< file-size 1024))
e0d47340 613 (format (if (floatp file-size) " %9.0f" " %9d") file-size)
a3723f13
JB
614 (do ((file-size (/ file-size 1024.0) (/ file-size 1024.0))
615 ;; kilo, mega, giga, tera, peta, exa
616 (post-fixes (list "k" "M" "G" "T" "P" "E") (cdr post-fixes)))
e0d47340 617 ((< file-size 1024) (format " %8.0f%s" file-size (car post-fixes))))))
a3723f13 618
9dce08b6 619(provide 'ls-lisp)
738eb4e7 620
ab5796a9 621;;; arch-tag: e55f399b-05ec-425c-a6d5-f5e349c35ab4
76550a57 622;;; ls-lisp.el ends here