Mercurial > emacs
comparison lisp/ls-lisp.el @ 13883:a4eef7470b6b
(ls-lisp-support-shell-wildcards): New variable.
(insert-directory): Convert the filename wildcard to an equivalent
Emacs regexp, when `ls-lisp-support-shell-wildcards' is non-nil.
Handle file patterns like "/foo*/" as if it were "/foo*", like the
shell would. Print zero total for files whose total size is
exactly zero (in particular, for no files at all). Say "No match"
when no files match the given wildcard.
(ls-lisp-format): Make directory listing format more like POSIX ls.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Thu, 28 Dec 1995 23:36:50 +0000 |
parents | ac7375e60931 |
children | 83f275dcd93a |
comparison
equal
deleted
inserted
replaced
13882:2631a29a614c | 13883:a4eef7470b6b |
---|---|
48 | 48 |
49 ;; Recognize some more ls switches: R F | 49 ;; Recognize some more ls switches: R F |
50 | 50 |
51 ;;; Code: | 51 ;;; Code: |
52 | 52 |
53 (defvar ls-lisp-support-shell-wildcards t | |
54 "*Non-nil means file patterns are treated as shell wildcards. | |
55 nil means they are treated as Emacs regexps (for backward compatibility). | |
56 This variable is checked by \\[insert-directory] only when `ls-lisp.el' | |
57 package is used.") | |
58 | |
53 (defun insert-directory (file &optional switches wildcard full-directory-p) | 59 (defun insert-directory (file &optional switches wildcard full-directory-p) |
54 "Insert directory listing for of FILE, formatted according to SWITCHES. | 60 "Insert directory listing for FILE, formatted according to SWITCHES. |
55 Leaves point after the inserted text. | 61 Leaves point after the inserted text. |
56 Optional third arg WILDCARD means treat FILE as shell wildcard. | 62 Optional third arg WILDCARD means treat FILE as shell wildcard. |
57 Optional fourth arg FULL-DIRECTORY-P means file is a directory and | 63 Optional fourth arg FULL-DIRECTORY-P means file is a directory and |
58 switches do not contain `d', so that a full listing is expected. | 64 switches do not contain `d', so that a full listing is expected. |
59 | 65 |
60 This version of the function comes from `ls-lisp.el'. | 66 This version of the function comes from `ls-lisp.el'. It doesn not |
61 It does not support ordinary shell wildcards; instead, it allows | 67 run any external programs or shells. It supports ordinary shell |
62 regular expressions to match file names. | 68 wildcards if `ls-lisp-support-shell-wildcards' variable is non-nil; |
63 | 69 otherwise, it interprets wildcards as regular expressions to match |
64 The switches that work are: A a c i r S s t u" | 70 file names. |
71 | |
72 Not all `ls' switches are supported. The switches that work | |
73 are: A a c i r S s t u" | |
65 (let ((handler (find-file-name-handler file 'insert-directory))) | 74 (let ((handler (find-file-name-handler file 'insert-directory))) |
66 (if handler | 75 (if handler |
67 (funcall handler 'insert-directory file switches | 76 (funcall handler 'insert-directory file switches |
68 wildcard full-directory-p) | 77 wildcard full-directory-p) |
78 ;; Sometimes we get ".../foo*/" as FILE. While the shell and | |
79 ;; `ls' don't mind, we certainly do, because it makes us think | |
80 ;; there is no wildcard, only a directory name. | |
81 (if (and ls-lisp-support-shell-wildcards | |
82 (string-match "[[?*]" file)) | |
83 (progn | |
84 (or (not (eq (aref file (1- (length file))) ?/)) | |
85 (setq file (substring file 0 (1- (length file))))) | |
86 (setq wildcard t))) | |
69 ;; Convert SWITCHES to a list of characters. | 87 ;; Convert SWITCHES to a list of characters. |
70 (setq switches (append switches nil)) | 88 (setq switches (append switches nil)) |
71 (if wildcard | 89 (if wildcard |
72 (setq wildcard (file-name-nondirectory file) ; actually emacs regexp | 90 (setq wildcard |
73 ;; perhaps convert it from shell to emacs syntax? | 91 (if ls-lisp-support-shell-wildcards |
92 (wildcard-to-regexp (file-name-nondirectory file)) | |
93 (file-name-nondirectory file)) | |
74 file (file-name-directory file))) | 94 file (file-name-directory file))) |
75 (if (or wildcard | 95 (if (or wildcard |
76 full-directory-p) | 96 full-directory-p) |
77 (let* ((dir (file-name-as-directory file)) | 97 (let* ((dir (file-name-as-directory file)) |
78 (default-directory dir);; so that file-attributes works | 98 (default-directory dir);; so that file-attributes works |
98 (cons x (file-attributes (expand-file-name x))))) | 118 (cons x (file-attributes (expand-file-name x))))) |
99 ;; inserting the call to directory-files right here | 119 ;; inserting the call to directory-files right here |
100 ;; seems to stimulate an Emacs bug | 120 ;; seems to stimulate an Emacs bug |
101 ;; ILLEGAL DATATYPE (#o37777777727) or #o67 | 121 ;; ILLEGAL DATATYPE (#o37777777727) or #o67 |
102 file-list)) | 122 file-list)) |
103 (insert "total \007\n") ; filled in afterwards | 123 ;; ``Total'' line (filled in afterwards). |
124 (insert (if (car-safe file-alist) | |
125 "total \007\n" | |
126 ;; Shell says ``No match'' if no files match | |
127 ;; the wildcard; let's say something similar. | |
128 "(No match)\ntotal \007\n")) | |
104 (setq file-alist | 129 (setq file-alist |
105 (ls-lisp-handle-switches file-alist switches)) | 130 (ls-lisp-handle-switches file-alist switches)) |
106 (while file-alist | 131 (while file-alist |
107 (setq elt (car file-alist) | 132 (setq elt (car file-alist) |
108 file-alist (cdr file-alist) | 133 file-alist (cdr file-alist) |
114 ;; Fill in total size of all files: | 139 ;; Fill in total size of all files: |
115 (save-excursion | 140 (save-excursion |
116 (search-backward "total \007") | 141 (search-backward "total \007") |
117 (goto-char (match-end 0)) | 142 (goto-char (match-end 0)) |
118 (delete-char -1) | 143 (delete-char -1) |
119 (insert (format "%d" (1+ (/ sum 1024)))))) | 144 (insert (format "%d" (if (zerop sum) 0 (1+ (/ sum 1024))))))) |
120 ;; if not full-directory-p, FILE *must not* end in /, as | 145 ;; if not full-directory-p, FILE *must not* end in /, as |
121 ;; file-attributes will not recognize a symlink to a directory | 146 ;; file-attributes will not recognize a symlink to a directory |
122 ;; must make it a relative filename as ls does: | 147 ;; must make it a relative filename as ls does: |
123 (setq file (file-name-nondirectory file)) | 148 (setq file (file-name-nondirectory file)) |
124 (insert (ls-lisp-format file (file-attributes file) switches)))))) | 149 (insert (ls-lisp-format file (file-attributes file) switches)))))) |
183 (nth 8 file-attr) ; permission bits | 208 (nth 8 file-attr) ; permission bits |
184 ;; numeric uid/gid are more confusing than helpful | 209 ;; numeric uid/gid are more confusing than helpful |
185 ;; Emacs should be able to make strings of them. | 210 ;; Emacs should be able to make strings of them. |
186 ;; user-login-name and user-full-name could take an | 211 ;; user-login-name and user-full-name could take an |
187 ;; optional arg. | 212 ;; optional arg. |
188 (format " %3d %8s %8s %8d " | 213 (format " %3d %-8s %-8s %8d " |
189 (nth 1 file-attr) ; no. of links | 214 (nth 1 file-attr) ; no. of links |
190 (if (= (user-uid) (nth 2 file-attr)) | 215 (if (= (user-uid) (nth 2 file-attr)) |
191 (user-login-name) | 216 (user-login-name) |
192 (int-to-string (nth 2 file-attr))) ; uid | 217 (int-to-string (nth 2 file-attr))) ; uid |
193 (if (eq system-type 'ms-dos) | 218 (if (eq system-type 'ms-dos) |