1 ;;; ls-lisp.el --- emulate insert-directory completely in Emacs Lisp
3 ;; Copyright (C) 1992, 1994 Free Software Foundation, Inc.
5 ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
9 ;; This file is part of GNU Emacs.
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)
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.
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.
28 ;; INSTALLATION =======================================================
30 ;; Put this file into your load-path. To use it, load it
31 ;; with (load "ls-lisp").
33 ;; OVERVIEW ===========================================================
35 ;; This file overloads the function insert-directory to implement it
36 ;; directly from Emacs lisp, without running `ls' in a subprocess.
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.
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$'.
47 ;; RESTRICTIONS =====================================================
49 ;; * many ls switches are ignored, see docstring of `insert-directory'.
51 ;; * Only numeric uid/gid
53 ;; TODO ==============================================================
55 ;; Recognize some more ls switches: R F
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'
66 (defvar ls-lisp-dired-ignore-case nil
67 "Non-nil causes dired buffers to sort alphabetically regardless of case.")
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.")
75 ;; Remember the original insert-directory function.
76 (fset 'original-insert-directory
(symbol-function 'insert-directory
))
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.
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'
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
)))
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.
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
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
))
112 (funcall handler
'insert-directory file switches
113 wildcard full-directory-p
)
114 ;; Sometimes we get ".../foo*/" as FILE. While the shell and
115 ;; `ls' don't mind, we certainly do, because it makes us think
116 ;; there is no wildcard, only a directory name.
117 (if (and ls-lisp-support-shell-wildcards
118 (string-match "[[?*]" file
))
120 (or (not (eq (aref file
(1- (length file
))) ?
/))
121 (setq file
(substring file
0 (1- (length file
)))))
123 ;; Convert SWITCHES to a list of characters.
124 (setq switches
(append switches nil
))
127 (if ls-lisp-support-shell-wildcards
128 (wildcard-to-regexp (file-name-nondirectory file
))
129 (file-name-nondirectory file
))
130 file
(file-name-directory file
)))
133 (let* ((dir (file-name-as-directory file
))
134 (default-directory dir
);; so that file-attributes works
138 (file-alist (directory-files-and-attributes dir nil wildcard
))
140 ;; do all bindings here for speed
143 (cond ((memq ?A switches
)
145 (ls-lisp-delete-matching "^\\.\\.?$" file-alist
)))
146 ((not (memq ?a switches
))
147 ;; if neither -A nor -a, flush . files
149 (ls-lisp-delete-matching "^\\." file-alist
))))
150 ;; ``Total'' line (filled in afterwards).
151 (insert (if (car-safe file-alist
)
153 ;; Shell says ``No match'' if no files match
154 ;; the wildcard; let's say something similar.
155 "(No match)\ntotal \007\n"))
157 (ls-lisp-handle-switches file-alist switches
))
159 (setq elt
(car file-alist
)
160 file-alist
(cdr file-alist
)
163 file-size
(nth 7 attr
))
166 ;; Even if neither SUM nor file's size
167 ;; overflow, their sum could.
168 (if (or (< sum
(- 134217727 file-size
))
172 (+ (float sum
) file-size
)))
173 (insert (ls-lisp-format short attr file-size switches now
))
175 ;; Fill in total size of all files:
177 (search-backward "total \007")
178 (goto-char (match-end 0))
180 (insert (format "%.0f" (fceiling (/ sum
1024.0))))))
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 (if (eq (aref file
(1- (length file
))) ?
/)
185 (setq file
(substring file
0 (1- (length file
)))))
186 (setq fattr
(file-attributes file
))
188 (insert (ls-lisp-format file fattr
(nth 7 fattr
)
189 switches
(current-time)))
190 (message "%s: doesn't exist or is inaccessible" file
)
194 (defun ls-lisp-delete-matching (regexp list
)
195 ;; Delete all elements matching REGEXP from LIST, return new list.
196 ;; Should perhaps use setcdr for efficiency.
199 (or (string-match regexp
(car (car list
)))
200 (setq result
(cons (car list
) result
)))
201 (setq list
(cdr list
)))
204 (defun ls-lisp-handle-switches (file-alist switches
)
205 ;; FILE-ALIST's elements are (FILE . FILE-ATTRIBUTES).
206 ;; Return new alist sorted according to SWITCHES which is a list of
207 ;; characters. Default sorting is alphabetically.
211 (cond ((memq ?S switches
) ; sorted on size
214 ;; 7th file attribute is file size
215 ;; Make largest file come first
218 ((memq ?t switches
) ; sorted on time
219 (setq index
(ls-lisp-time-index switches
))
222 (ls-lisp-time-lessp (nth index
(cdr y
))
223 (nth index
(cdr x
))))))
224 (t ; sorted alphabetically
225 (if ls-lisp-dired-ignore-case
228 (string-lessp (upcase (car x
))
232 (string-lessp (car x
)
234 (if (memq ?r switches
) ; reverse sort order
235 (setq file-alist
(nreverse file-alist
)))
238 ;; From Roland McGrath. Can use this to sort on time.
239 (defun ls-lisp-time-lessp (time0 time1
)
240 (let ((hi0 (car time0
))
242 (lo0 (car (cdr time0
)))
243 (lo1 (car (cdr time1
))))
249 (defun ls-lisp-format (file-name file-attr file-size switches now
)
250 (let ((file-type (nth 0 file-attr
)))
251 (concat (if (memq ?i switches
) ; inode number
252 (format "%6d " (nth 10 file-attr
)))
253 ;; nil is treated like "" in concat
254 (if (memq ?s switches
) ; size in K
255 (format "%4.0f " (fceiling (/ file-size
1024.0))))
256 (nth 8 file-attr
) ; permission bits
257 ;; numeric uid/gid are more confusing than helpful
258 ;; Emacs should be able to make strings of them.
259 ;; user-login-name and user-full-name could take an
261 (format (if (floatp file-size
)
262 " %3d %-8s %-8s %8.0f "
263 " %3d %-8s %-8s %8d ")
264 (nth 1 file-attr
) ; no. of links
265 (if (= (user-uid) (nth 2 file-attr
))
267 (int-to-string (nth 2 file-attr
))) ; uid
268 (if (eq system-type
'ms-dos
)
269 "root" ; everything is root on MSDOS.
270 (int-to-string (nth 3 file-attr
))) ; gid
273 (ls-lisp-format-time file-attr switches now
)
276 (if (stringp file-type
) ; is a symbolic link
277 (concat " -> " file-type
)
282 (defun ls-lisp-time-index (switches)
283 ;; Return index into file-attributes according to ls SWITCHES.
285 ((memq ?c switches
) 6) ; last mode change
286 ((memq ?u switches
) 4) ; last access
287 ;; default is last modtime
290 (defun ls-lisp-format-time (file-attr switches now
)
291 ;; Format time string for file with attributes FILE-ATTR according
292 ;; to SWITCHES (a list of ls option letters of which c and u are recognized).
293 ;; Use the same method as `ls' to decide whether to show time-of-day or year,
294 ;; depending on distance between file date and NOW.
295 (let* ((time (nth (ls-lisp-time-index switches
) file-attr
))
296 (diff16 (- (car time
) (car now
)))
297 (diff (+ (ash diff16
16) (- (car (cdr time
)) (car (cdr now
)))))
298 (past-cutoff (- (* 6 30 24 60 60))) ; 6 30-day months
299 (future-cutoff (* 60 60))) ; 1 hour
303 (<= past-cutoff diff
) (<= diff future-cutoff
)
304 ;; Sanity check in case `diff' computation overflowed.
305 (<= (1- (ash past-cutoff -
16)) diff16
)
306 (<= diff16
(1+ (ash future-cutoff -
16))))
310 (error "Unk 0 0000"))))
314 ;;; ls-lisp.el ends here