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 (defun insert-directory (file &optional switches wildcard full-directory-p)
70 "Insert directory listing for FILE, formatted according to SWITCHES.
71 Leaves point after the inserted text.
72 Optional third arg WILDCARD means treat FILE as shell wildcard.
73 Optional fourth arg FULL-DIRECTORY-P means file is a directory and
74 switches do not contain `d', so that a full listing is expected.
76 This version of the function comes from `ls-lisp.el'. It doesn not
77 run any external programs or shells. It supports ordinary shell
78 wildcards if `ls-lisp-support-shell-wildcards' variable is non-nil;
79 otherwise, it interprets wildcards as regular expressions to match
82 Not all `ls' switches are supported. The switches that work
83 are: A a c i r S s t u"
84 (let ((handler (find-file-name-handler file 'insert-directory)))
86 (funcall handler 'insert-directory file switches
87 wildcard full-directory-p)
88 ;; Sometimes we get ".../foo*/" as FILE. While the shell and
89 ;; `ls' don't mind, we certainly do, because it makes us think
90 ;; there is no wildcard, only a directory name.
91 (if (and ls-lisp-support-shell-wildcards
92 (string-match "[[?*]" file))
94 (or (not (eq (aref file (1- (length file))) ?/))
95 (setq file (substring file 0 (1- (length file)))))
97 ;; Convert SWITCHES to a list of characters.
98 (setq switches (append switches nil))
101 (if ls-lisp-support-shell-wildcards
102 (wildcard-to-regexp (file-name-nondirectory file))
103 (file-name-nondirectory file))
104 file (file-name-directory file)))
107 (let* ((dir (file-name-as-directory file))
108 (default-directory dir);; so that file-attributes works
112 (file-list (directory-files dir nil wildcard))
115 ;; do all bindings here for speed
117 (cond ((memq ?A switches)
119 (ls-lisp-delete-matching "^\\.\\.?$" file-list)))
120 ((not (memq ?a switches))
121 ;; if neither -A nor -a, flush . files
123 (ls-lisp-delete-matching "^\\." file-list))))
128 ;; file-attributes("~bogus") bombs
129 (cons x (file-attributes (expand-file-name x)))))
130 ;; inserting the call to directory-files right here
131 ;; seems to stimulate an Emacs bug
132 ;; ILLEGAL DATATYPE (#o37777777727) or #o67
134 ;; ``Total'' line (filled in afterwards).
135 (insert (if (car-safe file-alist)
137 ;; Shell says ``No match'' if no files match
138 ;; the wildcard; let's say something similar.
139 "(No match)\ntotal \007\n"))
141 (ls-lisp-handle-switches file-alist switches))
143 (setq elt (car file-alist)
144 file-alist (cdr file-alist)
148 (setq sum (+ sum (nth 7 attr)))
149 (insert (ls-lisp-format short attr switches now))))
150 ;; Fill in total size of all files:
152 (search-backward "total \007")
153 (goto-char (match-end 0))
155 (insert (format "%d" (if (zerop sum) 0 (1+ (/ sum 1024)))))))
156 ;; if not full-directory-p, FILE *must not* end in /, as
157 ;; file-attributes will not recognize a symlink to a directory
158 ;; must make it a relative filename as ls does:
159 (setq file (file-name-nondirectory file))
160 (insert (ls-lisp-format file (file-attributes file) switches
163 (defun ls-lisp-delete-matching (regexp list)
164 ;; Delete all elements matching REGEXP from LIST, return new list.
165 ;; Should perhaps use setcdr for efficiency.
168 (or (string-match regexp (car list))
169 (setq result (cons (car list) result)))
170 (setq list (cdr list)))
173 (defun ls-lisp-handle-switches (file-alist switches)
174 ;; FILE-ALIST's elements are (FILE . FILE-ATTRIBUTES).
175 ;; Return new alist sorted according to SWITCHES which is a list of
176 ;; characters. Default sorting is alphabetically.
180 (cond ((memq ?S switches) ; sorted on size
183 ;; 7th file attribute is file size
184 ;; Make largest file come first
187 ((memq ?t switches) ; sorted on time
188 (setq index (ls-lisp-time-index switches))
191 (ls-lisp-time-lessp (nth index (cdr y))
192 (nth index (cdr x))))))
193 (t ; sorted alphabetically
194 (if ls-lisp-dired-ignore-case
197 (string-lessp (upcase (car x))
201 (string-lessp (car x)
203 (if (memq ?r switches) ; reverse sort order
204 (setq file-alist (nreverse file-alist)))
207 ;; From Roland McGrath. Can use this to sort on time.
208 (defun ls-lisp-time-lessp (time0 time1)
209 (let ((hi0 (car time0))
211 (lo0 (car (cdr time0)))
212 (lo1 (car (cdr time1))))
218 (defun ls-lisp-format (file-name file-attr switches now)
219 (let ((file-type (nth 0 file-attr)))
220 (concat (if (memq ?i switches) ; inode number
221 (format "%6d " (nth 10 file-attr)))
222 ;; nil is treated like "" in concat
223 (if (memq ?s switches) ; size in K
224 (format "%4d " (1+ (/ (nth 7 file-attr) 1024))))
225 (nth 8 file-attr) ; permission bits
226 ;; numeric uid/gid are more confusing than helpful
227 ;; Emacs should be able to make strings of them.
228 ;; user-login-name and user-full-name could take an
230 (format " %3d %-8s %-8s %8d "
231 (nth 1 file-attr) ; no. of links
232 (if (= (user-uid) (nth 2 file-attr))
234 (int-to-string (nth 2 file-attr))) ; uid
235 (if (eq system-type 'ms-dos)
236 "root" ; everything is root on MSDOS.
237 (int-to-string (nth 3 file-attr))) ; gid
238 (nth 7 file-attr) ; size in bytes
240 (ls-lisp-format-time file-attr switches now)
243 (if (stringp file-type) ; is a symbolic link
244 (concat " -> " file-type)
249 (defun ls-lisp-time-index (switches)
250 ;; Return index into file-attributes according to ls SWITCHES.
252 ((memq ?c switches) 6) ; last mode change
253 ((memq ?u switches) 4) ; last access
254 ;; default is last modtime
257 (defun ls-lisp-format-time (file-attr switches now)
258 ;; Format time string for file with attributes FILE-ATTR according
259 ;; to SWITCHES (a list of ls option letters of which c and u are recognized).
260 ;; Use the same method as `ls' to decide whether to show time-of-day or year,
261 ;; depending on distance between file date and NOW.
262 (let* ((time (nth (ls-lisp-time-index switches) file-attr))
263 (diff16 (- (car time) (car now)))
264 (diff (+ (ash diff16 16) (- (car (cdr time)) (car (cdr now)))))
265 (past-cutoff (- (* 6 30 24 60 60))) ; 6 30-day months
266 (future-cutoff (* 60 60))) ; 1 hour
269 (<= past-cutoff diff) (<= diff future-cutoff)
270 ;; Sanity check in case `diff' computation overflowed.
271 (<= (1- (ash past-cutoff -16)) diff16)
272 (<= diff16 (1+ (ash future-cutoff -16))))
279 ;;; ls-lisp.el ends here