(tibetan-pre-write-conversion): Cancel previous
[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
RS
140 ;; do all bindings here for speed
141 fil attr)
142 (cond ((memq ?A switches)
143 (setq file-list
144 (ls-lisp-delete-matching "^\\.\\.?$" file-list)))
145 ((not (memq ?a switches))
146 ;; if neither -A nor -a, flush . files
147 (setq file-list
148 (ls-lisp-delete-matching "^\\." file-list))))
149 (setq file-alist
150 (mapcar
151 (function
152 (lambda (x)
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
158 file-list))
3045b163
RS
159 ;; ``Total'' line (filled in afterwards).
160 (insert (if (car-safe file-alist)
161 "total \007\n"
162 ;; Shell says ``No match'' if no files match
163 ;; the wildcard; let's say something similar.
164 "(No match)\ntotal \007\n"))
9dce08b6
RS
165 (setq file-alist
166 (ls-lisp-handle-switches file-alist switches))
167 (while file-alist
168 (setq elt (car file-alist)
9dce08b6 169 file-alist (cdr file-alist)
3cfb886e
RS
170 short (car elt)
171 attr (cdr elt))
172 (and attr
173 (setq sum (+ sum (nth 7 attr)))
1fff30af 174 (insert (ls-lisp-format short attr switches now))))
9dce08b6
RS
175 ;; Fill in total size of all files:
176 (save-excursion
177 (search-backward "total \007")
178 (goto-char (match-end 0))
179 (delete-char -1)
3045b163 180 (insert (format "%d" (if (zerop sum) 0 (1+ (/ sum 1024)))))))
9dce08b6
RS
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))
1fff30af
RS
185 (insert (ls-lisp-format file (file-attributes file) switches
186 (current-time)))))))
9dce08b6
RS
187
188(defun ls-lisp-delete-matching (regexp list)
6467926f 189 ;; Delete all elements matching REGEXP from LIST, return new list.
d6d472d5 190 ;; Should perhaps use setcdr for efficiency.
6467926f
SK
191 (let (result)
192 (while list
193 (or (string-match regexp (car list))
194 (setq result (cons (car list) result)))
195 (setq list (cdr list)))
196 result))
197
9dce08b6 198(defun ls-lisp-handle-switches (file-alist switches)
6467926f 199 ;; FILE-ALIST's elements are (FILE . FILE-ATTRIBUTES).
738eb4e7
SK
200 ;; Return new alist sorted according to SWITCHES which is a list of
201 ;; characters. Default sorting is alphabetically.
e54241c5
SK
202 (let (index)
203 (setq file-alist
204 (sort file-alist
205 (cond ((memq ?S switches) ; sorted on size
206 (function
207 (lambda (x y)
208 ;; 7th file attribute is file size
209 ;; Make largest file come first
210 (< (nth 7 (cdr y))
211 (nth 7 (cdr x))))))
212 ((memq ?t switches) ; sorted on time
9dce08b6 213 (setq index (ls-lisp-time-index switches))
e54241c5
SK
214 (function
215 (lambda (x y)
9dce08b6
RS
216 (ls-lisp-time-lessp (nth index (cdr y))
217 (nth index (cdr x))))))
e54241c5 218 (t ; sorted alphabetically
97b927b3
GV
219 (if ls-lisp-dired-ignore-case
220 (function
221 (lambda (x y)
222 (string-lessp (upcase (car x))
223 (upcase (car y)))))
224 (function
225 (lambda (x y)
226 (string-lessp (car x)
227 (car y))))))))))
6467926f
SK
228 (if (memq ?r switches) ; reverse sort order
229 (setq file-alist (nreverse file-alist)))
230 file-alist)
d88c0e93 231
e54241c5 232;; From Roland McGrath. Can use this to sort on time.
9dce08b6 233(defun ls-lisp-time-lessp (time0 time1)
e54241c5
SK
234 (let ((hi0 (car time0))
235 (hi1 (car time1))
236 (lo0 (car (cdr time0)))
237 (lo1 (car (cdr time1))))
238 (or (< hi0 hi1)
239 (and (= hi0 hi1)
240 (< lo0 lo1)))))
241
242
1fff30af 243(defun ls-lisp-format (file-name file-attr switches now)
d88c0e93 244 (let ((file-type (nth 0 file-attr)))
6467926f 245 (concat (if (memq ?i switches) ; inode number
d6d472d5
SK
246 (format "%6d " (nth 10 file-attr)))
247 ;; nil is treated like "" in concat
6467926f 248 (if (memq ?s switches) ; size in K
d6d472d5 249 (format "%4d " (1+ (/ (nth 7 file-attr) 1024))))
6467926f 250 (nth 8 file-attr) ; permission bits
d88c0e93 251 ;; numeric uid/gid are more confusing than helpful
6467926f
SK
252 ;; Emacs should be able to make strings of them.
253 ;; user-login-name and user-full-name could take an
254 ;; optional arg.
3045b163 255 (format " %3d %-8s %-8s %8d "
d6d472d5 256 (nth 1 file-attr) ; no. of links
a12ff9f3
RS
257 (if (= (user-uid) (nth 2 file-attr))
258 (user-login-name)
7b4a3608 259 (int-to-string (nth 2 file-attr))) ; uid
a12ff9f3
RS
260 (if (eq system-type 'ms-dos)
261 "root" ; everything is root on MSDOS.
7b4a3608 262 (int-to-string (nth 3 file-attr))) ; gid
d6d472d5
SK
263 (nth 7 file-attr) ; size in bytes
264 )
1fff30af 265 (ls-lisp-format-time file-attr switches now)
738eb4e7 266 " "
d88c0e93
SK
267 file-name
268 (if (stringp file-type) ; is a symbolic link
269 (concat " -> " file-type)
270 "")
271 "\n"
272 )))
273
9dce08b6 274(defun ls-lisp-time-index (switches)
e54241c5
SK
275 ;; Return index into file-attributes according to ls SWITCHES.
276 (cond
277 ((memq ?c switches) 6) ; last mode change
278 ((memq ?u switches) 4) ; last access
279 ;; default is last modtime
280 (t 5)))
281
1fff30af 282(defun ls-lisp-format-time (file-attr switches now)
738eb4e7
SK
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).
1fff30af
RS
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
292 (format-time-string
293 (if (and
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))))
298 "%b %e %H:%M"
299 "%b %e %Y")
300 time)))
738eb4e7 301
9dce08b6 302(provide 'ls-lisp)
738eb4e7 303
76550a57 304;;; ls-lisp.el ends here