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