Commit | Line | Data |
---|---|---|
76550a57 ER |
1 | ;;; ls-lisp.el --- emulate insert-directory completely in Emacs Lisp |
2 | ||
3 | ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de> | |
4 | ;; Keywords: unix | |
d88c0e93 | 5 | |
738eb4e7 | 6 | ;; Copyright (C) 1992 by Sebastian Kremer <sk@thp.uni-koeln.de> |
d88c0e93 | 7 | |
738eb4e7 | 8 | ;; This program is free software; you can redistribute it and/or modify |
d88c0e93 SK |
9 | ;; it under the terms of the GNU General Public License as published by |
10 | ;; the Free Software Foundation; either version 1, or (at your option) | |
11 | ;; any later version. | |
738eb4e7 SK |
12 | ;; |
13 | ;; This program is distributed in the hope that it will be useful, | |
d88c0e93 SK |
14 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | ;; GNU General Public License for more details. | |
738eb4e7 | 17 | ;; |
d88c0e93 | 18 | ;; You should have received a copy of the GNU General Public License |
738eb4e7 SK |
19 | ;; along with this program; if not, write to the Free Software |
20 | ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
21 | ||
738eb4e7 SK |
22 | ;; INSTALLATION ======================================================= |
23 | ;; | |
9dce08b6 RS |
24 | ;; Put this file into your load-path. To use it, load it |
25 | ;; with (load "directory"). | |
d9a0f717 | 26 | |
738eb4e7 SK |
27 | ;; OVERVIEW =========================================================== |
28 | ||
9dce08b6 RS |
29 | ;; This file overloads the function insert-directory to implement it |
30 | ;; directly from Emacs lisp, without running `ls' in a subprocess. | |
738eb4e7 | 31 | |
9dce08b6 | 32 | ;; It is useful if you cannot afford to fork Emacs on a real memory UNIX, |
738eb4e7 SK |
33 | ;; under VMS, or if you don't have the ls program, or if you want |
34 | ;; different format from what ls offers. | |
35 | ||
9dce08b6 RS |
36 | ;; This function uses regexps instead of shell |
37 | ;; wildcards. If you enter regexps remember to double each $ sign. | |
38 | ;; For example, to include files *.el, enter `.*\.el$$', | |
738eb4e7 | 39 | ;; resulting in the regexp `.*\.el$'. |
d88c0e93 | 40 | |
738eb4e7 | 41 | ;; RESTRICTIONS ===================================================== |
d88c0e93 | 42 | |
9dce08b6 | 43 | ;; * many ls switches are ignored, see docstring of `insert-directory'. |
738eb4e7 SK |
44 | |
45 | ;; * Only numeric uid/gid | |
46 | ||
738eb4e7 SK |
47 | ;; TODO ============================================================== |
48 | ||
d9a0f717 | 49 | ;; Recognize some more ls switches: R F |
738eb4e7 | 50 | \f |
76550a57 ER |
51 | ;;; Code: |
52 | ||
9dce08b6 RS |
53 | (defun insert-directory (file &optional switches wildcard full-directory-p) |
54 | "Insert directory listing for of FILE, formatted according to SWITCHES. | |
55 | Leaves point after the inserted text. | |
56 | Optional third arg WILDCARD means treat FILE as shell wildcard. | |
6467926f | 57 | Optional fourth arg FULL-DIRECTORY-P means file is a directory and |
9dce08b6 RS |
58 | switches do not contain `d', so that a full listing is expected. |
59 | ||
60 | This version of the function comes from `directory.el'. | |
61 | It does not support ordinary shell wildcards; instead, it allows | |
62 | regular expressions to match file names. | |
63 | ||
64 | The switches that work are: A a c i r S s t u" | |
5dbfdacd | 65 | (let (handler ((find-file-name-handler file))) |
9dce08b6 RS |
66 | (if handler |
67 | (funcall handler 'insert-directory file switches | |
68 | wildcard full-directory-p) | |
69 | (if wildcard | |
70 | (setq wildcard (file-name-nondirectory file) ; actually emacs regexp | |
71 | ;; perhaps convert it from shell to emacs syntax? | |
72 | file (file-name-directory file))) | |
73 | (if (or wildcard | |
74 | full-directory-p) | |
75 | (let* ((dir (file-name-as-directory file)) | |
76 | (default-directory dir);; so that file-attributes works | |
77 | (sum 0) | |
78 | elt | |
79 | short | |
80 | (file-list (directory-files dir nil wildcard)) | |
81 | file-alist | |
82 | ;; do all bindings here for speed | |
83 | fil attr) | |
84 | (cond ((memq ?A switches) | |
85 | (setq file-list | |
86 | (ls-lisp-delete-matching "^\\.\\.?$" file-list))) | |
87 | ((not (memq ?a switches)) | |
88 | ;; if neither -A nor -a, flush . files | |
89 | (setq file-list | |
90 | (ls-lisp-delete-matching "^\\." file-list)))) | |
91 | (setq file-alist | |
92 | (mapcar | |
93 | (function | |
94 | (lambda (x) | |
95 | ;; file-attributes("~bogus") bombs | |
96 | (cons x (file-attributes (expand-file-name x))))) | |
97 | ;; inserting the call to directory-files right here | |
98 | ;; seems to stimulate an Emacs bug | |
99 | ;; ILLEGAL DATATYPE (#o37777777727) or #o67 | |
100 | file-list)) | |
101 | (insert "total \007\n") ; filled in afterwards | |
102 | (setq file-alist | |
103 | (ls-lisp-handle-switches file-alist switches)) | |
104 | (while file-alist | |
105 | (setq elt (car file-alist) | |
106 | short (car elt) | |
107 | attr (cdr elt) | |
108 | file-alist (cdr file-alist) | |
109 | fil (concat dir short) | |
110 | sum (+ sum (nth 7 attr))) | |
111 | (insert (ls-lisp-format short attr switches))) | |
112 | ;; Fill in total size of all files: | |
113 | (save-excursion | |
114 | (search-backward "total \007") | |
115 | (goto-char (match-end 0)) | |
116 | (delete-char -1) | |
117 | (insert (format "%d" (1+ (/ sum 1024)))))) | |
118 | ;; if not full-directory-p, FILE *must not* end in /, as | |
119 | ;; file-attributes will not recognize a symlink to a directory | |
120 | ;; must make it a relative filename as ls does: | |
121 | (setq file (file-name-nondirectory file)) | |
122 | (insert (ls-lisp-format file (file-attributes file) switches)))))) | |
123 | ||
124 | (defun ls-lisp-delete-matching (regexp list) | |
6467926f | 125 | ;; Delete all elements matching REGEXP from LIST, return new list. |
d6d472d5 | 126 | ;; Should perhaps use setcdr for efficiency. |
6467926f SK |
127 | (let (result) |
128 | (while list | |
129 | (or (string-match regexp (car list)) | |
130 | (setq result (cons (car list) result))) | |
131 | (setq list (cdr list))) | |
132 | result)) | |
133 | ||
9dce08b6 | 134 | (defun ls-lisp-handle-switches (file-alist switches) |
6467926f | 135 | ;; FILE-ALIST's elements are (FILE . FILE-ATTRIBUTES). |
738eb4e7 SK |
136 | ;; Return new alist sorted according to SWITCHES which is a list of |
137 | ;; characters. Default sorting is alphabetically. | |
e54241c5 SK |
138 | (let (index) |
139 | (setq file-alist | |
140 | (sort file-alist | |
141 | (cond ((memq ?S switches) ; sorted on size | |
142 | (function | |
143 | (lambda (x y) | |
144 | ;; 7th file attribute is file size | |
145 | ;; Make largest file come first | |
146 | (< (nth 7 (cdr y)) | |
147 | (nth 7 (cdr x)))))) | |
148 | ((memq ?t switches) ; sorted on time | |
9dce08b6 | 149 | (setq index (ls-lisp-time-index switches)) |
e54241c5 SK |
150 | (function |
151 | (lambda (x y) | |
9dce08b6 RS |
152 | (ls-lisp-time-lessp (nth index (cdr y)) |
153 | (nth index (cdr x)))))) | |
e54241c5 SK |
154 | (t ; sorted alphabetically |
155 | (function | |
156 | (lambda (x y) | |
157 | (string-lessp (car x) | |
158 | (car y))))))))) | |
6467926f SK |
159 | (if (memq ?r switches) ; reverse sort order |
160 | (setq file-alist (nreverse file-alist))) | |
161 | file-alist) | |
d88c0e93 | 162 | |
e54241c5 | 163 | ;; From Roland McGrath. Can use this to sort on time. |
9dce08b6 | 164 | (defun ls-lisp-time-lessp (time0 time1) |
e54241c5 SK |
165 | (let ((hi0 (car time0)) |
166 | (hi1 (car time1)) | |
167 | (lo0 (car (cdr time0))) | |
168 | (lo1 (car (cdr time1)))) | |
169 | (or (< hi0 hi1) | |
170 | (and (= hi0 hi1) | |
171 | (< lo0 lo1))))) | |
172 | ||
173 | ||
9dce08b6 | 174 | (defun ls-lisp-format (file-name file-attr &optional switches) |
d88c0e93 | 175 | (let ((file-type (nth 0 file-attr))) |
6467926f | 176 | (concat (if (memq ?i switches) ; inode number |
d6d472d5 SK |
177 | (format "%6d " (nth 10 file-attr))) |
178 | ;; nil is treated like "" in concat | |
6467926f | 179 | (if (memq ?s switches) ; size in K |
d6d472d5 | 180 | (format "%4d " (1+ (/ (nth 7 file-attr) 1024)))) |
6467926f | 181 | (nth 8 file-attr) ; permission bits |
d88c0e93 | 182 | ;; numeric uid/gid are more confusing than helpful |
6467926f SK |
183 | ;; Emacs should be able to make strings of them. |
184 | ;; user-login-name and user-full-name could take an | |
185 | ;; optional arg. | |
d6d472d5 SK |
186 | (format " %3d %-8d %-8d %8d " |
187 | (nth 1 file-attr) ; no. of links | |
188 | (nth 2 file-attr) ; uid | |
189 | (nth 3 file-attr) ; gid | |
190 | (nth 7 file-attr) ; size in bytes | |
191 | ) | |
9dce08b6 | 192 | (ls-lisp-format-time file-attr switches) |
738eb4e7 | 193 | " " |
d88c0e93 SK |
194 | file-name |
195 | (if (stringp file-type) ; is a symbolic link | |
196 | (concat " -> " file-type) | |
197 | "") | |
198 | "\n" | |
199 | ))) | |
200 | ||
9dce08b6 | 201 | (defun ls-lisp-time-index (switches) |
e54241c5 SK |
202 | ;; Return index into file-attributes according to ls SWITCHES. |
203 | (cond | |
204 | ((memq ?c switches) 6) ; last mode change | |
205 | ((memq ?u switches) 4) ; last access | |
206 | ;; default is last modtime | |
207 | (t 5))) | |
208 | ||
9dce08b6 | 209 | (defun ls-lisp-format-time (file-attr switches) |
738eb4e7 SK |
210 | ;; Format time string for file with attributes FILE-ATTR according |
211 | ;; to SWITCHES (a list of ls option letters of which c and u are recognized). | |
212 | ;; file-attributes's time is in a braindead format | |
213 | ;; Emacs 19 can format it using a new optional argument to | |
214 | ;; current-time-string, for Emacs 18 we just return the faked fixed | |
215 | ;; date "Jan 00 00:00 ". | |
216 | (condition-case error-data | |
217 | (let* ((time (current-time-string | |
9dce08b6 | 218 | (nth (ls-lisp-time-index switches) file-attr))) |
738eb4e7 SK |
219 | (date (substring time 4 11)) ; "Apr 30 " |
220 | (clock (substring time 11 16)) ; "11:27" | |
221 | (year (substring time 19 24)) ; " 1992" | |
222 | (same-year (equal year (substring (current-time-string) 19 24)))) | |
223 | (concat date ; has trailing SPC | |
224 | (if same-year | |
225 | ;; this is not exactly the same test used by ls | |
226 | ;; ls tests if the file is older than 6 months | |
227 | ;; but we can't do time differences easily | |
228 | clock | |
229 | year))) | |
230 | (error | |
231 | "Jan 00 00:00"))) | |
232 | ||
9dce08b6 | 233 | (provide 'ls-lisp) |
738eb4e7 | 234 | |
76550a57 | 235 | ;;; ls-lisp.el ends here |