(menu-bar-options-menu): Delete "Syntax
[bpt/emacs.git] / lisp / ls-lisp.el
CommitLineData
76550a57
ER
1;;; ls-lisp.el --- emulate insert-directory completely in Emacs Lisp
2
0d30b337
TTN
3;; Copyright (C) 1992, 1994, 2000, 2002, 2003, 2004,
4;; 2005 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
7c938215 15;; the Free Software Foundation; either version 2, 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;; * Generally only numeric uid/gid.
d88c0e93 49
3f51f5a9 50;; TO DO =============================================================
738eb4e7 51
3f51f5a9 52;; Complete handling of F switch (if/when possible).
738eb4e7 53
3f51f5a9
EZ
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!
738eb4e7 57
3f51f5a9 58;;; History:
76550a57 59
3f51f5a9
EZ
60;; Written originally by Sebastian Kremer <sk@thp.uni-koeln.de>
61;; Revised by Andrew Innes and Geoff Volker (and maybe others).
3045b163 62
3f51f5a9
EZ
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.
66
67;;; Code:
97b927b3 68
a3723f13
JB
69(eval-when-compile (require 'cl))
70
3f51f5a9
EZ
71(defgroup ls-lisp nil
72 "Emulate the ls program completely in Emacs Lisp."
eff409ba 73 :version "21.1"
3f51f5a9
EZ
74 :group 'dired)
75
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.
84Corresponding value is one of the atoms: nil, MacOS, MS-Windows, UNIX.
85Sets default values for: `ls-lisp-ignore-case', `ls-lisp-dirs-first',
86`ls-lisp-verbosity'. Need not match actual platform. Changing this
87option 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)
93
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)
101
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)
108
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.
117It should contain none or more of the symbols: links, uid, gid.
f0529b5b 118nil (or an empty list) means display none of them.
3f51f5a9
EZ
119
120Concepts come from UNIX: `links' means count of names associated with
121the file\; `uid' means user (owner) identifier\; `gid' means group
122identifier.
123
124If emulation is MacOS then default is nil\;
125if emulation is MS-Windows then default is `(links)' if platform is
126Windows NT/2K, nil otherwise\;
127if emulation is UNIX then default is `(links uid)'\;
128if 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)
134
73916123
MR
135(defcustom ls-lisp-use-insert-directory-program
136 (not (memq system-type '(macos ms-dos windows-nt)))
3f51f5a9 137 "*Non-nil causes ls-lisp to revert back to using `insert-directory-program'.
0cb0ba6c
GV
138This is useful on platforms where ls-lisp is dumped into Emacs, such as
139Microsoft Windows, but you would still like to use a program to list
3f51f5a9
EZ
140the contents of a directory."
141 :type 'boolean
142 :group 'ls-lisp)
143
73916123
MR
144;;; Autoloaded because it is let-bound in `recover-session', `mail-recover-1'.
145;;;###autoload
3f51f5a9
EZ
146(defcustom ls-lisp-support-shell-wildcards t
147 "*Non-nil means ls-lisp treats file patterns as shell wildcards.
148Otherwise they are treated as Emacs regexps (for backward compatibility)."
149 :type 'boolean
150 :group 'ls-lisp)
151
2686cdc0
RS
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.
156They are used whenever a locale is not specified to use instead.
157
158Syntax: (EARLY-TIME-FORMAT OLD-TIME-FORMAT)
159
160The EARLY-TIME-FORMAT is used if file has been modified within the
161current year. The OLD-TIME-FORMAT is used for older files. To use ISO
1628601 dates, you could set:
163
164\(setq ls-lisp-format-time-list
165 '(\"%Y-%m-%d %H:%M\"
166 \"%Y-%m-%d \"))"
401c1968
MR
167 :type '(list (string :tag "Early time format")
168 (string :tag "Old time format"))
2686cdc0
RS
169 :group 'ls-lisp)
170
44a9ca8b
RS
171(defvar original-insert-directory nil
172 "This holds the original function definition of `insert-directory'.")
173
3f51f5a9
EZ
174;; Remember the original insert-directory function
175(or (featurep 'ls-lisp) ; FJW: unless this file is being reloaded!
44a9ca8b 176 (setq original-insert-directory (symbol-function 'insert-directory)))
3f51f5a9
EZ
177
178;; This stub is to allow ls-lisp to parse symbolic links via another
179;; library such as w32-symlinks.el from
1e05661f 180;; http://centaur.maths.qmw.ac.uk/Emacs/:
3f51f5a9
EZ
181(defun ls-lisp-parse-symlink (file-name)
182 "This stub may be redefined to parse FILE-NAME as a symlink.
183It should return nil or the link target as a string."
184 nil)
0cb0ba6c 185
3f51f5a9
EZ
186\f
187;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
0cb0ba6c
GV
188
189(defun insert-directory (file switches &optional wildcard full-directory-p)
190 "Insert directory listing for FILE, formatted according to SWITCHES.
191Leaves point after the inserted text.
192SWITCHES may be a string of options, or a list of strings.
193Optional third arg WILDCARD means treat FILE as shell wildcard.
194Optional fourth arg FULL-DIRECTORY-P means file is a directory and
195switches do not contain `d', so that a full listing is expected.
196
3f51f5a9
EZ
197This version of the function comes from `ls-lisp.el'.
198If the value of `ls-lisp-use-insert-directory-program' is non-nil then
199it works exactly like the version from `files.el' and runs a directory
200listing program whose name is in the variable
201`insert-directory-program'; if also WILDCARD is non-nil then it runs
202the shell specified by `shell-file-name'. If the value of
203`ls-lisp-use-insert-directory-program' is nil then it runs a Lisp
204emulation.
205
206The Lisp emulation does not run any external programs or shells. It
207supports ordinary shell wildcards if `ls-lisp-support-shell-wildcards'
208is non-nil; otherwise, it interprets wildcards as regular expressions
209to match file names. It does not support all `ls' switches -- those
210that work are: A a c i r S s t u U X g G B C R and F partly."
0cb0ba6c 211 (if ls-lisp-use-insert-directory-program
44a9ca8b
RS
212 (funcall original-insert-directory
213 file switches wildcard full-directory-p)
3f51f5a9
EZ
214 ;; We need the directory in order to find the right handler.
215 (let ((handler (find-file-name-handler (expand-file-name file)
94e72368
RS
216 'insert-directory))
217 wildcard-regexp)
3f51f5a9
EZ
218 (if handler
219 (funcall handler 'insert-directory file switches
220 wildcard full-directory-p)
d4939c66
JPW
221 ;; Remove --dired switch
222 (if (string-match "--dired " switches)
223 (setq switches (replace-match "" nil nil switches)))
3f51f5a9
EZ
224 ;; Convert SWITCHES to a list of characters.
225 (setq switches (delete ?- (append switches nil)))
94e72368
RS
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)))
3f51f5a9 235 (if wildcard
94e72368 236 (setq wildcard-regexp
3f51f5a9
EZ
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))
94e72368 241 (if (memq ?B switches) (setq wildcard-regexp "[^~]\\'")))
3f51f5a9
EZ
242 (ls-lisp-insert-directory
243 file switches (ls-lisp-time-index switches)
94e72368 244 wildcard-regexp full-directory-p)
7f1b5edc
EZ
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
7ad0c1c3
EZ
252 ;; Replace "total" with "total used", to avoid confusion.
253 (replace-match "total used in directory")
7f1b5edc
EZ
254 (end-of-line)
255 (insert " available " available)))))))))
3f51f5a9
EZ
256
257(defun ls-lisp-insert-directory
94e72368 258 (file switches time-index wildcard-regexp full-directory-p)
3045b163 259 "Insert directory listing for FILE, formatted according to SWITCHES.
3f51f5a9
EZ
260Leaves point after the inserted text. This is an internal function
261optionally called by the `ls-lisp.el' version of `insert-directory'.
262It is called recursively if the -R switch is used.
263SWITCHES is a *list* of characters. TIME-INDEX is the time index into
94e72368 264file-attributes according to SWITCHES. WILDCARD-REGEXP is nil or an *Emacs
3f51f5a9
EZ
265regexp*. FULL-DIRECTORY-P means file is a directory and SWITCHES does
266not contain `d', so that a full listing is expected."
94e72368 267 (if (or wildcard-regexp full-directory-p)
3f51f5a9
EZ
268 (let* ((dir (file-name-as-directory file))
269 (default-directory dir) ; so that file-attributes works
270 (file-alist
80ca5799 271 (directory-files-and-attributes dir nil wildcard-regexp t 'string))
3f51f5a9
EZ
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))
302 sum
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.
9dce08b6
RS
319 (while file-alist
320 (setq elt (car file-alist)
3f51f5a9
EZ
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
94e72368 327 elt switches time-index wildcard-regexp full-directory-p)))))
3f51f5a9
EZ
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)))
80ca5799 333 (let ((fattr (file-attributes file 'string)))
3f51f5a9
EZ
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!
339
340(defun ls-lisp-column-format (file-alist)
341 "Insert the file names (only) in FILE-ALIST into the current buffer.
342Format in columns, sorted vertically, following GNU ls -C.
343Responds 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))))))
9dce08b6
RS
373
374(defun ls-lisp-delete-matching (regexp list)
3f51f5a9 375 "Delete all elements matching REGEXP from LIST, return new list."
d6d472d5 376 ;; Should perhaps use setcdr for efficiency.
6467926f
SK
377 (let (result)
378 (while list
3f51f5a9 379 (or (string-match regexp (caar list))
6467926f
SK
380 (setq result (cons (car list) result)))
381 (setq list (cdr list)))
382 result))
383
3f51f5a9
EZ
384(defsubst ls-lisp-string-lessp (s1 s2)
385 "Return t if string S1 is less than string S2 in lexicographic order.
386Case is significant if `ls-lisp-ignore-case' is nil.
387Unibyte 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))))
390
9dce08b6 391(defun ls-lisp-handle-switches (file-alist switches)
3f51f5a9
EZ
392 "Return new FILE-ALIST sorted according to SWITCHES.
393SWITCHES is a list of characters. Default sorting is alphabetic."
6467926f 394 ;; FILE-ALIST's elements are (FILE . FILE-ATTRIBUTES).
3f51f5a9
EZ
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)))))
417 (t
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))
449
450(defun ls-lisp-classify (filedata)
451 "Append a character to each file name indicating the file type.
452Also, for regular files that are executable, append `*'.
453The file type indicators are `/' for directories, `@' for symbolic
454links, `|' for FIFOs, `=' for sockets, and nothing for regular files.
71bf3033 455\[But FIFOs and sockets are not recognized.]
3f51f5a9
EZ
456FILEDATA has the form (filename . `file-attributes'). Its `cadr' is t
457for 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))))
470
471(defun ls-lisp-extension (filename)
472 "Return extension of FILENAME (ignoring any version extension)
473FOLLOWED 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))
d88c0e93 492
e54241c5 493;; From Roland McGrath. Can use this to sort on time.
9dce08b6 494(defun ls-lisp-time-lessp (time0 time1)
3f51f5a9
EZ
495 "Return t if time TIME0 is earlier than time TIME1."
496 (let ((hi0 (car time0)) (hi1 (car time1)))
e54241c5
SK
497 (or (< hi0 hi1)
498 (and (= hi0 hi1)
3f51f5a9
EZ
499 (< (cadr time0) (cadr time1))))))
500
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.
503FILE-ATTR and FILE-SIZE give the file's attributes and size.
504SWITCHES, 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
6467926f 513 (concat (if (memq ?i switches) ; inode number
3f51f5a9 514 (format " %6d" (nth 10 file-attr)))
d6d472d5 515 ;; nil is treated like "" in concat
6467926f 516 (if (memq ?s switches) ; size in K
3f51f5a9
EZ
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;
6467926f 522 ;; Emacs should be able to make strings of them.
3f51f5a9
EZ
523 ;; They tend to be bogus on non-UNIX platforms anyway so
524 ;; optionally hide them.
525 (if (memq 'uid ls-lisp-verbosity)
80ca5799 526 ;; uid can be a sting or an integer
3f51f5a9 527 (let ((uid (nth 2 file-attr)))
80ca5799 528 (format (if (stringp uid) " %-8s" " %-8d") uid)))
3f51f5a9
EZ
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))
80ca5799
LH
532 (let ((gid (nth 3 file-attr)))
533 (format (if (stringp gid) " %-8s" " %-8d") gid))))
a3723f13 534 (ls-lisp-format-file-size file-size (memq ?h switches))
3f51f5a9
EZ
535 " "
536 (ls-lisp-format-time file-attr time-index now)
738eb4e7 537 " "
b3df990b 538 (propertize file-name 'dired-filename t)
d88c0e93 539 (if (stringp file-type) ; is a symbolic link
3f51f5a9 540 (concat " -> " file-type))
d88c0e93
SK
541 "\n"
542 )))
543
9dce08b6 544(defun ls-lisp-time-index (switches)
3f51f5a9
EZ
545 "Return time index into file-attributes according to ls SWITCHES list.
546Return 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
551
d81a647c
PE
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)))
557
3f51f5a9
EZ
558(defun ls-lisp-format-time (file-attr time-index now)
559 "Format time for file with attributes FILE-ATTR according to TIME-INDEX.
560Use the same method as ls to decide whether to show time-of-day or year,
561depending on distance between file date and NOW.
562All ls time options, namely c, t and u, are handled."
563 (let* ((time (nth (or time-index 5) file-attr)) ; default is last modtime
d81a647c
PE
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
f8a10234 571 (condition-case nil
d81a647c
PE
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))
2686cdc0
RS
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)))
d81a647c 585 time))
452e47d7 586 (error "Unk 0 0000"))))
738eb4e7 587
a3723f13
JB
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))))))
596
9dce08b6 597(provide 'ls-lisp)
738eb4e7 598
ab5796a9 599;;; arch-tag: e55f399b-05ec-425c-a6d5-f5e349c35ab4
76550a57 600;;; ls-lisp.el ends here