Setup auto-fill-chars.
[bpt/emacs.git] / lisp / ls-lisp.el
1 ;;; ls-lisp.el --- emulate insert-directory completely in Emacs Lisp
2
3 ;; Copyright (C) 1992, 1994 Free Software Foundation, Inc.
4
5 ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
6 ;; Maintainer: FSF
7 ;; Keywords: unix
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; INSTALLATION =======================================================
29 ;;
30 ;; Put this file into your load-path. To use it, load it
31 ;; with (load "ls-lisp").
32
33 ;; OVERVIEW ===========================================================
34
35 ;; This file overloads the function insert-directory to implement it
36 ;; directly from Emacs lisp, without running `ls' in a subprocess.
37
38 ;; It is useful if you cannot afford to fork Emacs on a real memory UNIX,
39 ;; under VMS, or if you don't have the ls program, or if you want
40 ;; different format from what ls offers.
41
42 ;; This function uses regexps instead of shell
43 ;; wildcards. If you enter regexps remember to double each $ sign.
44 ;; For example, to include files *.el, enter `.*\.el$$',
45 ;; resulting in the regexp `.*\.el$'.
46
47 ;; RESTRICTIONS =====================================================
48
49 ;; * many ls switches are ignored, see docstring of `insert-directory'.
50
51 ;; * Only numeric uid/gid
52
53 ;; TODO ==============================================================
54
55 ;; Recognize some more ls switches: R F
56 \f
57 ;;; Code:
58
59 ;;;###autoload
60 (defvar ls-lisp-support-shell-wildcards t
61 "*Non-nil means file patterns are treated as shell wildcards.
62 nil means they are treated as Emacs regexps (for backward compatibility).
63 This variable is checked by \\[insert-directory] only when `ls-lisp.el'
64 package is used.")
65
66 (defvar ls-lisp-dired-ignore-case nil
67 "Non-nil causes dired buffers to sort alphabetically regardless of case.")
68
69 (defvar ls-lisp-use-insert-directory-program nil
70 "Non-nil causes ls-lisp to revert back to using `insert-directory-program'.
71 This is useful on platforms where ls-lisp is dumped into Emacs, such as
72 Microsoft Windows, but you would still like to use a program to list
73 the contents of a directory.")
74
75 ;; Remember the original insert-directory function.
76 (fset 'original-insert-directory (symbol-function 'insert-directory))
77
78 (defun insert-directory (file switches &optional wildcard full-directory-p)
79 "Insert directory listing for FILE, formatted according to SWITCHES.
80 Leaves point after the inserted text.
81 SWITCHES may be a string of options, or a list of strings.
82 Optional third arg WILDCARD means treat FILE as shell wildcard.
83 Optional fourth arg FULL-DIRECTORY-P means file is a directory and
84 switches do not contain `d', so that a full listing is expected.
85
86 This version of the function comes from `ls-lisp.el'. Depending upon
87 the value of `ls-lisp-use-insert-directory-program', it will use an
88 external program if non-nil or the lisp function `ls-lisp-insert-directory'
89 otherwise."
90 (if ls-lisp-use-insert-directory-program
91 (original-insert-directory file switches wildcard full-directory-p)
92 (ls-lisp-insert-directory file switches wildcard full-directory-p)))
93
94 (defun ls-lisp-insert-directory (file switches &optional wildcard full-directory-p)
95 "Insert directory listing for FILE, formatted according to SWITCHES.
96 Leaves point after the inserted text.
97 Optional third arg WILDCARD means treat FILE as shell wildcard.
98 Optional fourth arg FULL-DIRECTORY-P means file is a directory and
99 switches do not contain `d', so that a full listing is expected.
100
101 This version of the function comes from `ls-lisp.el'. It does not
102 run any external programs or shells. It supports ordinary shell
103 wildcards if `ls-lisp-support-shell-wildcards' variable is non-nil;
104 otherwise, it interprets wildcards as regular expressions to match
105 file names.
106
107 Not all `ls' switches are supported. The switches that work
108 are: A a c i r S s t u"
109 (let ((handler (find-file-name-handler file 'insert-directory)))
110 (if handler
111 (funcall handler 'insert-directory file switches
112 wildcard full-directory-p)
113 ;; Sometimes we get ".../foo*/" as FILE. While the shell and
114 ;; `ls' don't mind, we certainly do, because it makes us think
115 ;; there is no wildcard, only a directory name.
116 (if (and ls-lisp-support-shell-wildcards
117 (string-match "[[?*]" file))
118 (progn
119 (or (not (eq (aref file (1- (length file))) ?/))
120 (setq file (substring file 0 (1- (length file)))))
121 (setq wildcard t)))
122 ;; Convert SWITCHES to a list of characters.
123 (setq switches (append switches nil))
124 (if wildcard
125 (setq wildcard
126 (if ls-lisp-support-shell-wildcards
127 (wildcard-to-regexp (file-name-nondirectory file))
128 (file-name-nondirectory file))
129 file (file-name-directory file)))
130 (if (or wildcard
131 full-directory-p)
132 (let* ((dir (file-name-as-directory file))
133 (default-directory dir);; so that file-attributes works
134 (sum 0)
135 elt
136 short
137 (file-list (directory-files dir nil wildcard))
138 file-alist
139 (now (current-time))
140 ;; do all bindings here for speed
141 fil attr)
142 (cond ((memq ?A switches)
143 (setq file-list
144 (ls-lisp-delete-matching "^\\.\\.?$" file-list)))
145 ((not (memq ?a switches))
146 ;; if neither -A nor -a, flush . files
147 (setq file-list
148 (ls-lisp-delete-matching "^\\." file-list))))
149 (setq file-alist
150 (mapcar
151 (function
152 (lambda (x)
153 ;; file-attributes("~bogus") bombs
154 (cons x (file-attributes (expand-file-name x)))))
155 ;; inserting the call to directory-files right here
156 ;; seems to stimulate an Emacs bug
157 ;; ILLEGAL DATATYPE (#o37777777727) or #o67
158 file-list))
159 ;; ``Total'' line (filled in afterwards).
160 (insert (if (car-safe file-alist)
161 "total \007\n"
162 ;; Shell says ``No match'' if no files match
163 ;; the wildcard; let's say something similar.
164 "(No match)\ntotal \007\n"))
165 (setq file-alist
166 (ls-lisp-handle-switches file-alist switches))
167 (while file-alist
168 (setq elt (car file-alist)
169 file-alist (cdr file-alist)
170 short (car elt)
171 attr (cdr elt))
172 (and attr
173 (setq sum (+ sum (nth 7 attr)))
174 (insert (ls-lisp-format short attr switches now))))
175 ;; Fill in total size of all files:
176 (save-excursion
177 (search-backward "total \007")
178 (goto-char (match-end 0))
179 (delete-char -1)
180 (insert (format "%d" (if (zerop sum) 0 (1+ (/ sum 1024)))))))
181 ;; if not full-directory-p, FILE *must not* end in /, as
182 ;; file-attributes will not recognize a symlink to a directory
183 ;; must make it a relative filename as ls does:
184 (setq file (file-name-nondirectory file))
185 (insert (ls-lisp-format file (file-attributes file) switches
186 (current-time)))))))
187
188 (defun ls-lisp-delete-matching (regexp list)
189 ;; Delete all elements matching REGEXP from LIST, return new list.
190 ;; Should perhaps use setcdr for efficiency.
191 (let (result)
192 (while list
193 (or (string-match regexp (car list))
194 (setq result (cons (car list) result)))
195 (setq list (cdr list)))
196 result))
197
198 (defun ls-lisp-handle-switches (file-alist switches)
199 ;; FILE-ALIST's elements are (FILE . FILE-ATTRIBUTES).
200 ;; Return new alist sorted according to SWITCHES which is a list of
201 ;; characters. Default sorting is alphabetically.
202 (let (index)
203 (setq file-alist
204 (sort file-alist
205 (cond ((memq ?S switches) ; sorted on size
206 (function
207 (lambda (x y)
208 ;; 7th file attribute is file size
209 ;; Make largest file come first
210 (< (nth 7 (cdr y))
211 (nth 7 (cdr x))))))
212 ((memq ?t switches) ; sorted on time
213 (setq index (ls-lisp-time-index switches))
214 (function
215 (lambda (x y)
216 (ls-lisp-time-lessp (nth index (cdr y))
217 (nth index (cdr x))))))
218 (t ; sorted alphabetically
219 (if ls-lisp-dired-ignore-case
220 (function
221 (lambda (x y)
222 (string-lessp (upcase (car x))
223 (upcase (car y)))))
224 (function
225 (lambda (x y)
226 (string-lessp (car x)
227 (car y))))))))))
228 (if (memq ?r switches) ; reverse sort order
229 (setq file-alist (nreverse file-alist)))
230 file-alist)
231
232 ;; From Roland McGrath. Can use this to sort on time.
233 (defun ls-lisp-time-lessp (time0 time1)
234 (let ((hi0 (car time0))
235 (hi1 (car time1))
236 (lo0 (car (cdr time0)))
237 (lo1 (car (cdr time1))))
238 (or (< hi0 hi1)
239 (and (= hi0 hi1)
240 (< lo0 lo1)))))
241
242
243 (defun ls-lisp-format (file-name file-attr switches now)
244 (let ((file-type (nth 0 file-attr)))
245 (concat (if (memq ?i switches) ; inode number
246 (format "%6d " (nth 10 file-attr)))
247 ;; nil is treated like "" in concat
248 (if (memq ?s switches) ; size in K
249 (format "%4d " (1+ (/ (nth 7 file-attr) 1024))))
250 (nth 8 file-attr) ; permission bits
251 ;; numeric uid/gid are more confusing than helpful
252 ;; Emacs should be able to make strings of them.
253 ;; user-login-name and user-full-name could take an
254 ;; optional arg.
255 (format " %3d %-8s %-8s %8d "
256 (nth 1 file-attr) ; no. of links
257 (if (= (user-uid) (nth 2 file-attr))
258 (user-login-name)
259 (int-to-string (nth 2 file-attr))) ; uid
260 (if (eq system-type 'ms-dos)
261 "root" ; everything is root on MSDOS.
262 (int-to-string (nth 3 file-attr))) ; gid
263 (nth 7 file-attr) ; size in bytes
264 )
265 (ls-lisp-format-time file-attr switches now)
266 " "
267 file-name
268 (if (stringp file-type) ; is a symbolic link
269 (concat " -> " file-type)
270 "")
271 "\n"
272 )))
273
274 (defun ls-lisp-time-index (switches)
275 ;; Return index into file-attributes according to ls SWITCHES.
276 (cond
277 ((memq ?c switches) 6) ; last mode change
278 ((memq ?u switches) 4) ; last access
279 ;; default is last modtime
280 (t 5)))
281
282 (defun ls-lisp-format-time (file-attr switches now)
283 ;; Format time string for file with attributes FILE-ATTR according
284 ;; to SWITCHES (a list of ls option letters of which c and u are recognized).
285 ;; Use the same method as `ls' to decide whether to show time-of-day or year,
286 ;; depending on distance between file date and NOW.
287 (let* ((time (nth (ls-lisp-time-index switches) file-attr))
288 (diff16 (- (car time) (car now)))
289 (diff (+ (ash diff16 16) (- (car (cdr time)) (car (cdr now)))))
290 (past-cutoff (- (* 6 30 24 60 60))) ; 6 30-day months
291 (future-cutoff (* 60 60))) ; 1 hour
292 (format-time-string
293 (if (and
294 (<= past-cutoff diff) (<= diff future-cutoff)
295 ;; Sanity check in case `diff' computation overflowed.
296 (<= (1- (ash past-cutoff -16)) diff16)
297 (<= diff16 (1+ (ash future-cutoff -16))))
298 "%b %e %H:%M"
299 "%b %e %Y")
300 time)))
301
302 (provide 'ls-lisp)
303
304 ;;; ls-lisp.el ends here