]> code.delx.au - gnu-emacs/blob - lisp/ls-lisp.el
(auto-raise-mode): When enabling the mode,
[gnu-emacs] / 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-alist (directory-files-and-attributes dir nil wildcard))
138 (now (current-time))
139 ;; do all bindings here for speed
140 file-size
141 fil attr)
142 (cond ((memq ?A switches)
143 (setq file-alist
144 (ls-lisp-delete-matching "^\\.\\.?$" file-alist)))
145 ((not (memq ?a switches))
146 ;; if neither -A nor -a, flush . files
147 (setq file-alist
148 (ls-lisp-delete-matching "^\\." file-alist))))
149 ;; ``Total'' line (filled in afterwards).
150 (insert (if (car-safe file-alist)
151 "total \007\n"
152 ;; Shell says ``No match'' if no files match
153 ;; the wildcard; let's say something similar.
154 "(No match)\ntotal \007\n"))
155 (setq file-alist
156 (ls-lisp-handle-switches file-alist switches))
157 (while file-alist
158 (setq elt (car file-alist)
159 file-alist (cdr file-alist)
160 short (car elt)
161 attr (cdr elt)
162 file-size (nth 7 attr))
163 (and attr
164 (setq sum
165 ;; Even if neither SUM nor file's size
166 ;; overflow, their sum could.
167 (if (or (< sum (- 134217727 file-size))
168 (floatp sum)
169 (floatp file-size))
170 (+ sum file-size)
171 (+ (float sum) file-size)))
172 (insert (ls-lisp-format short attr file-size switches now))
173 ))
174 ;; Fill in total size of all files:
175 (save-excursion
176 (search-backward "total \007")
177 (goto-char (match-end 0))
178 (delete-char -1)
179 (insert (format "%.0f" (fceiling (/ sum 1024.0))))))
180 ;; if not full-directory-p, FILE *must not* end in /, as
181 ;; file-attributes will not recognize a symlink to a directory
182 ;; must make it a relative filename as ls does:
183 (setq file (file-name-nondirectory file))
184 (insert (ls-lisp-format file (file-attributes file)
185 (nth 7 (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 (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 file-size 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 "%4.0f " (fceiling (/ file-size 1024.0))))
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 (if (floatp file-size)
256 " %3d %-8s %-8s %8.0f "
257 " %3d %-8s %-8s %8d ")
258 (nth 1 file-attr) ; no. of links
259 (if (= (user-uid) (nth 2 file-attr))
260 (user-login-name)
261 (int-to-string (nth 2 file-attr))) ; uid
262 (if (eq system-type 'ms-dos)
263 "root" ; everything is root on MSDOS.
264 (int-to-string (nth 3 file-attr))) ; gid
265 file-size
266 )
267 (ls-lisp-format-time file-attr switches now)
268 " "
269 file-name
270 (if (stringp file-type) ; is a symbolic link
271 (concat " -> " file-type)
272 "")
273 "\n"
274 )))
275
276 (defun ls-lisp-time-index (switches)
277 ;; Return index into file-attributes according to ls SWITCHES.
278 (cond
279 ((memq ?c switches) 6) ; last mode change
280 ((memq ?u switches) 4) ; last access
281 ;; default is last modtime
282 (t 5)))
283
284 (defun ls-lisp-format-time (file-attr switches now)
285 ;; Format time string for file with attributes FILE-ATTR according
286 ;; to SWITCHES (a list of ls option letters of which c and u are recognized).
287 ;; Use the same method as `ls' to decide whether to show time-of-day or year,
288 ;; depending on distance between file date and NOW.
289 (let* ((time (nth (ls-lisp-time-index switches) file-attr))
290 (diff16 (- (car time) (car now)))
291 (diff (+ (ash diff16 16) (- (car (cdr time)) (car (cdr now)))))
292 (past-cutoff (- (* 6 30 24 60 60))) ; 6 30-day months
293 (future-cutoff (* 60 60))) ; 1 hour
294 (condition-case nil
295 (format-time-string
296 (if (and
297 (<= past-cutoff diff) (<= diff future-cutoff)
298 ;; Sanity check in case `diff' computation overflowed.
299 (<= (1- (ash past-cutoff -16)) diff16)
300 (<= diff16 (1+ (ash future-cutoff -16))))
301 "%b %e %H:%M"
302 "%b %e %Y")
303 time)
304 (error "Unk 0 0000"))))
305
306 (provide 'ls-lisp)
307
308 ;;; ls-lisp.el ends here