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
)))
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
))
119 (or (not (eq (aref file
(1- (length file
))) ?
/))
120 (setq file
(substring file
0 (1- (length file
)))))
122 ;; Convert SWITCHES to a list of characters.
123 (setq switches
(append switches nil
))
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
)))
132 (let* ((dir (file-name-as-directory file
))
133 (default-directory dir
);; so that file-attributes works
137 (file-list (directory-files dir nil wildcard
))
140 ;; do all bindings here for speed
142 (cond ((memq ?A switches
)
144 (ls-lisp-delete-matching "^\\.\\.?$" file-list
)))
145 ((not (memq ?a switches
))
146 ;; if neither -A nor -a, flush . files
148 (ls-lisp-delete-matching "^\\." file-list
))))
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
159 ;; ``Total'' line (filled in afterwards).
160 (insert (if (car-safe file-alist
)
162 ;; Shell says ``No match'' if no files match
163 ;; the wildcard; let's say something similar.
164 "(No match)\ntotal \007\n"))
166 (ls-lisp-handle-switches file-alist switches
))
168 (setq elt
(car file-alist
)
169 file-alist
(cdr file-alist
)
173 (setq sum
(+ sum
(nth 7 attr
)))
174 (insert (ls-lisp-format short attr switches now
))))
175 ;; Fill in total size of all files:
177 (search-backward "total \007")
178 (goto-char (match-end 0))
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
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.
193 (or (string-match regexp
(car list
))
194 (setq result
(cons (car list
) result
)))
195 (setq list
(cdr list
)))
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.
205 (cond ((memq ?S switches
) ; sorted on size
208 ;; 7th file attribute is file size
209 ;; Make largest file come first
212 ((memq ?t switches
) ; sorted on time
213 (setq index
(ls-lisp-time-index switches
))
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
222 (string-lessp (upcase (car x
))
226 (string-lessp (car x
)
228 (if (memq ?r switches
) ; reverse sort order
229 (setq file-alist
(nreverse file-alist
)))
232 ;; From Roland McGrath. Can use this to sort on time.
233 (defun ls-lisp-time-lessp (time0 time1
)
234 (let ((hi0 (car time0
))
236 (lo0 (car (cdr time0
)))
237 (lo1 (car (cdr time1
))))
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
255 (format " %3d %-8s %-8s %8d "
256 (nth 1 file-attr
) ; no. of links
257 (if (= (user-uid) (nth 2 file-attr
))
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
265 (ls-lisp-format-time file-attr switches now
)
268 (if (stringp file-type
) ; is a symbolic link
269 (concat " -> " file-type
)
274 (defun ls-lisp-time-index (switches)
275 ;; Return index into file-attributes according to ls SWITCHES.
277 ((memq ?c switches
) 6) ; last mode change
278 ((memq ?u switches
) 4) ; last access
279 ;; default is last modtime
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
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))))
304 ;;; ls-lisp.el ends here