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