Mercurial > emacs
annotate lisp/eshell/em-glob.el @ 31811:1a3a36f6811b
#
author | André Spiegel <spiegel@gnu.org> |
---|---|
date | Thu, 21 Sep 2000 13:31:18 +0000 |
parents | 34b1ab9d583d |
children | aab90b31807c |
rev | line source |
---|---|
29876 | 1 ;;; em-glob --- extended file name globbing |
2 | |
29934
34b1ab9d583d
Change spelling of the Free Software Foundation.
Gerd Moellmann <gerd@gnu.org>
parents:
29876
diff
changeset
|
3 ;; Copyright (C) 1999, 2000 Free Software Foundation |
29876 | 4 |
5 ;; This file is part of GNU Emacs. | |
6 | |
7 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
8 ;; it under the terms of the GNU General Public License as published by | |
9 ;; the Free Software Foundation; either version 2, or (at your option) | |
10 ;; any later version. | |
11 | |
12 ;; GNU Emacs is distributed in the hope that it will be useful, | |
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 ;; GNU General Public License for more details. | |
16 | |
17 ;; You should have received a copy of the GNU General Public License | |
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
20 ;; Boston, MA 02111-1307, USA. | |
21 | |
22 (provide 'em-glob) | |
23 | |
24 (eval-when-compile (require 'esh-maint)) | |
25 | |
26 (defgroup eshell-glob nil | |
27 "This module provides extended globbing syntax, similar what is used | |
28 by zsh for filename generation." | |
29 :tag "Extended filename globbing" | |
30 :group 'eshell-module) | |
31 | |
32 ;;; Commentary: | |
33 | |
34 ;; The globbing code used by Eshell closely follows the syntax used by | |
35 ;; zsh. Basically, here is a summary of examples: | |
36 ;; | |
37 ;; echo a* ; anything starting with 'a' | |
38 ;; echo a#b ; zero or more 'a's, then 'b' | |
39 ;; echo a##b ; one or more 'a's, then 'b' | |
40 ;; echo a? ; a followed by any character | |
41 ;; echo a*~ab ; 'a', then anything, but not 'ab' | |
42 ;; echo c*~*~ ; all files beginning with 'c', except backups (*~) | |
43 ;; | |
44 ;; Recursive globbing is also supported: | |
45 ;; | |
46 ;; echo **/*.c ; all '.c' files at or under current directory | |
47 ;; echo ***/*.c ; same as above, but traverse symbolic links | |
48 ;; | |
49 ;; Using argument predication, the recursive globbing syntax is | |
50 ;; sufficient to replace the use of 'find <expr> | xargs <cmd>' in | |
51 ;; most cases. For example, to change the readership of all files | |
52 ;; belonging to 'johnw' in the '/tmp' directory or lower, use: | |
53 ;; | |
54 ;; chmod go-r /tmp/**/*(u'johnw') | |
55 ;; | |
56 ;; The glob above matches all of the files beneath '/tmp' that are | |
57 ;; owned by the user 'johnw'. See [Value modifiers and predicates], | |
58 ;; for more information about argument predication. | |
59 | |
60 ;;; User Variables: | |
61 | |
62 (defcustom eshell-glob-load-hook '(eshell-glob-initialize) | |
63 "*A list of functions to run when `eshell-glob' is loaded." | |
64 :type 'hook | |
65 :group 'eshell-glob) | |
66 | |
67 (defcustom eshell-glob-include-dot-files nil | |
68 "*If non-nil, glob patterns will match files beginning with a dot." | |
69 :type 'boolean | |
70 :group 'eshell-glob) | |
71 | |
72 (defcustom eshell-glob-include-dot-dot t | |
73 "*If non-nil, glob patterns that match dots will match . and .." | |
74 :type 'boolean | |
75 :group 'eshell-glob) | |
76 | |
77 (defcustom eshell-glob-case-insensitive (eshell-under-windows-p) | |
78 "*If non-nil, glob pattern matching will ignore case." | |
79 :type 'boolean | |
80 :group 'eshell-glob) | |
81 | |
82 (defcustom eshell-glob-show-progress t | |
83 "*If non-nil, display progress messages during a recursive glob." | |
84 :type 'boolean | |
85 :group 'eshell-glob) | |
86 | |
87 (defcustom eshell-error-if-no-glob nil | |
88 "*If non-nil, it is an error for a glob pattern not to match. | |
89 This mimcs the behavior of zsh if non-nil, but bash if nil." | |
90 :type 'boolean | |
91 :group 'eshell-glob) | |
92 | |
93 (defcustom eshell-glob-chars-list '(?\] ?\[ ?* ?? ?~ ?\( ?\) ?| ?#) | |
94 "*List of additional characters used in extended globbing." | |
95 :type '(repeat character) | |
96 :group 'eshell-glob) | |
97 | |
98 (defcustom eshell-glob-translate-alist | |
99 '((?\] . "]") | |
100 (?\[ . "[") | |
101 (?? . ".") | |
102 (?* . ".*") | |
103 (?~ . "~") | |
104 (?\( . "\\(") | |
105 (?\) . "\\)") | |
106 (?\| . "\\|") | |
107 (?# . (lambda (str pos) | |
108 (if (and (< (1+ pos) (length str)) | |
109 (memq (aref str (1+ pos)) '(?* ?# ?+ ??))) | |
110 (cons (if (eq (aref str (1+ pos)) ??) | |
111 "?" | |
112 (if (eq (aref str (1+ pos)) ?*) | |
113 "*" "+")) (+ pos 2)) | |
114 (cons "*" (1+ pos)))))) | |
115 "*An alist for translation of extended globbing characters." | |
116 :type '(repeat (cons character (choice regexp function))) | |
117 :group 'eshell-glob) | |
118 | |
119 ;;; Internal Variables: | |
120 | |
121 (defvar eshell-glob-chars-regexp nil) | |
122 | |
123 ;;; Functions: | |
124 | |
125 (defun eshell-glob-initialize () | |
126 "Initialize the extended globbing code." | |
127 ;; it's important that `eshell-glob-chars-list' come first | |
128 (set (make-local-variable 'eshell-special-chars-outside-quoting) | |
129 (append eshell-glob-chars-list eshell-special-chars-outside-quoting)) | |
130 (set (make-local-variable 'eshell-glob-chars-regexp) | |
131 (format "[%s]+" (apply 'string eshell-glob-chars-list))) | |
132 (make-local-hook 'eshell-parse-argument-hook) | |
133 (add-hook 'eshell-parse-argument-hook 'eshell-parse-glob-chars t t) | |
134 (make-local-hook 'eshell-pre-rewrite-command-hook) | |
135 (add-hook 'eshell-pre-rewrite-command-hook | |
136 'eshell-no-command-globbing nil t)) | |
137 | |
138 (defun eshell-no-command-globbing (terms) | |
139 "Don't glob the command argument. Reflect this by modifying TERMS." | |
140 (ignore | |
141 (when (and (listp (car terms)) | |
142 (eq (caar terms) 'eshell-extended-glob)) | |
143 (setcar terms (cadr (car terms)))))) | |
144 | |
145 (defun eshell-add-glob-modifier () | |
146 "Add `eshell-extended-glob' to the argument modifier list." | |
147 (when (memq 'expand-file-name eshell-current-modifiers) | |
148 (setq eshell-current-modifiers | |
149 (delq 'expand-file-name eshell-current-modifiers)) | |
150 ;; if this is a glob pattern than needs to be expanded, then it | |
151 ;; will need to expand each member of the resulting glob list | |
152 (add-to-list 'eshell-current-modifiers | |
153 '(lambda (list) | |
154 (if (listp list) | |
155 (mapcar 'expand-file-name list) | |
156 (expand-file-name list))))) | |
157 (add-to-list 'eshell-current-modifiers 'eshell-extended-glob)) | |
158 | |
159 (defun eshell-parse-glob-chars () | |
160 "Parse a globbing delimiter. | |
161 The character is not advanced for ordinary globbing characters, so | |
162 that other function may have a chance to override the globbing | |
163 interpretation." | |
164 (when (memq (char-after) eshell-glob-chars-list) | |
165 (if (not (memq (char-after) '(?\( ?\[))) | |
166 (ignore (eshell-add-glob-modifier)) | |
167 (let ((here (point))) | |
168 (forward-char) | |
169 (let* ((delim (char-before)) | |
170 (end (eshell-find-delimiter | |
171 delim (if (eq delim ?\[) ?\] ?\))))) | |
172 (if (not end) | |
173 (throw 'eshell-incomplete delim) | |
174 (if (and (eshell-using-module 'eshell-pred) | |
175 (eshell-arg-delimiter (1+ end))) | |
176 (ignore (goto-char here)) | |
177 (eshell-add-glob-modifier) | |
178 (prog1 | |
179 (buffer-substring-no-properties (1- (point)) (1+ end)) | |
180 (goto-char (1+ end)))))))))) | |
181 | |
182 (defun eshell-glob-regexp (pattern) | |
183 "Convert glob-pattern PATTERN to a regular expression. | |
184 The basic syntax is: | |
185 | |
186 glob regexp meaning | |
187 ---- ------ ------- | |
188 ? . matches any single character | |
189 * .* matches any group of characters (or none) | |
190 # * matches zero or more occurrences of preceding | |
191 ## + matches one or more occurrences of preceding | |
192 (x) \(x\) makes 'x' a regular expression group | |
193 | \| boolean OR within an expression group | |
194 [a-b] [a-b] matches a character or range | |
195 [^a] [^a] excludes a character or range | |
196 | |
197 If any characters in PATTERN have the text property `eshell-escaped' | |
198 set to true, then these characters will match themselves in the | |
199 resulting regular expression." | |
200 (let ((matched-in-pattern 0) ; How much of PATTERN handled | |
201 regexp) | |
202 (while (string-match eshell-glob-chars-regexp | |
203 pattern matched-in-pattern) | |
204 (let* ((op-begin (match-beginning 0)) | |
205 (op-char (aref pattern op-begin))) | |
206 (setq regexp | |
207 (concat regexp | |
208 (regexp-quote | |
209 (substring pattern matched-in-pattern op-begin)))) | |
210 (if (get-text-property op-begin 'escaped pattern) | |
211 (setq regexp (concat regexp | |
212 (regexp-quote (char-to-string op-char))) | |
213 matched-in-pattern (1+ op-begin)) | |
214 (let ((xlat (assq op-char eshell-glob-translate-alist))) | |
215 (if (not xlat) | |
216 (error "Unrecognized globbing character '%c'" op-char) | |
217 (if (stringp (cdr xlat)) | |
218 (setq regexp (concat regexp (cdr xlat)) | |
219 matched-in-pattern (1+ op-begin)) | |
220 (let ((result (funcall (cdr xlat) pattern op-begin))) | |
221 (setq regexp (concat regexp (car result)) | |
222 matched-in-pattern (cdr result))))))))) | |
223 (concat "\\`" | |
224 regexp | |
225 (regexp-quote (substring pattern matched-in-pattern)) | |
226 "\\'"))) | |
227 | |
228 (defun eshell-extended-glob (glob) | |
229 "Return a list of files generated from GLOB, perhaps looking for DIRS-ONLY. | |
230 This function almost fully supports zsh style filename generation | |
231 syntax. Things that are not supported are: | |
232 | |
233 ^foo for matching everything but foo | |
234 (foo~bar) tilde within a parenthesis group | |
235 foo<1-10> numeric ranges | |
236 foo~x(a|b) (a|b) will be interpreted as a predicate/modifier list | |
237 | |
238 Mainly they are not supported because file matching is done with Emacs | |
239 regular expressions, and these cannot support the above constructs. | |
240 | |
241 If this routine fails, it returns nil. Otherwise, it returns a list | |
242 the form: | |
243 | |
244 (INCLUDE-REGEXP EXCLUDE-REGEXP (PRED-FUNC-LIST) (MOD-FUNC-LIST))" | |
245 (let ((paths (eshell-split-path glob)) | |
246 matches message-shown) | |
247 (unwind-protect | |
248 (if (and (cdr paths) | |
249 (file-name-absolute-p (car paths))) | |
250 (eshell-glob-entries (file-name-as-directory (car paths)) | |
251 (cdr paths)) | |
252 (eshell-glob-entries (file-name-as-directory ".") paths)) | |
253 (if message-shown | |
254 (message nil))) | |
255 (or (and matches (nreverse matches)) | |
256 (if eshell-error-if-no-glob | |
257 (error "No matches found: %s" glob) | |
258 glob)))) | |
259 | |
260 (eval-when-compile | |
261 (defvar matches) | |
262 (defvar message-shown)) | |
263 | |
264 ;; jww (1999-11-18): this function assumes that directory-sep-char is | |
265 ;; a forward slash (/) | |
266 | |
267 (defun eshell-glob-entries (path globs &optional recurse-p) | |
268 "Glob the entries in PATHS, possibly recursing if RECURSE-P is non-nil." | |
269 (let* ((entries (ignore-errors | |
270 (file-name-all-completions "" path))) | |
271 (case-fold-search eshell-glob-case-insensitive) | |
272 (glob (car globs)) | |
273 (len (length glob)) | |
274 dirs rdirs | |
275 incl excl | |
276 name isdir pathname) | |
277 (while (cond | |
278 ((and (= len 3) (equal glob "**/")) | |
279 (setq recurse-p 2 | |
280 globs (cdr globs) | |
281 glob (car globs) | |
282 len (length glob))) | |
283 ((and (= len 4) (equal glob "***/")) | |
284 (setq recurse-p 3 | |
285 globs (cdr globs) | |
286 glob (car globs) | |
287 len (length glob))))) | |
288 (if (and recurse-p (not glob)) | |
289 (error "'**' cannot end a globbing pattern")) | |
290 (let ((index 1)) | |
291 (setq incl glob) | |
292 (while (and (eq incl glob) | |
293 (setq index (string-match "~" glob index))) | |
294 (if (or (get-text-property index 'escaped glob) | |
295 (or (= (1+ index) len))) | |
296 (setq index (1+ index)) | |
297 (setq incl (substring glob 0 index) | |
298 excl (substring glob (1+ index)))))) | |
299 ;; can't use `directory-file-name' because it strips away text | |
300 ;; properties in the string | |
301 (let ((len (1- (length incl)))) | |
302 (if (eq (aref incl len) directory-sep-char) | |
303 (setq incl (substring incl 0 len))) | |
304 (when excl | |
305 (setq len (1- (length excl))) | |
306 (if (eq (aref excl len) directory-sep-char) | |
307 (setq excl (substring excl 0 len))))) | |
308 (setq incl (eshell-glob-regexp incl) | |
309 excl (and excl (eshell-glob-regexp excl))) | |
310 (if (or eshell-glob-include-dot-files | |
311 (eq (aref glob 0) ?.)) | |
312 (unless (or eshell-glob-include-dot-dot | |
313 (cdr globs)) | |
314 (setq excl (if excl | |
315 (concat "\\(\\`\\.\\.?\\'\\|" excl "\\)") | |
316 "\\`\\.\\.?\\'"))) | |
317 (setq excl (if excl | |
318 (concat "\\(\\`\\.\\|" excl "\\)") | |
319 "\\`\\."))) | |
320 (when (and recurse-p eshell-glob-show-progress) | |
321 (message "Building file list...%d so far: %s" | |
322 (length matches) path) | |
323 (setq message-shown t)) | |
324 (if (equal path "./") (setq path "")) | |
325 (while entries | |
326 (setq name (car entries) | |
327 len (length name) | |
328 isdir (eq (aref name (1- len)) directory-sep-char)) | |
329 (if (let ((fname (directory-file-name name))) | |
330 (and (not (and excl (string-match excl fname))) | |
331 (string-match incl fname))) | |
332 (if (cdr globs) | |
333 (if isdir | |
334 (setq dirs (cons (concat path name) dirs))) | |
335 (setq matches (cons (concat path name) matches)))) | |
336 (if (and recurse-p isdir | |
337 (or (> len 3) | |
338 (not (or (and (= len 2) (equal name "./")) | |
339 (and (= len 3) (equal name "../"))))) | |
340 (setq pathname (concat path name)) | |
341 (not (and (= recurse-p 2) | |
342 (file-symlink-p | |
343 (directory-file-name pathname))))) | |
344 (setq rdirs (cons pathname rdirs))) | |
345 (setq entries (cdr entries))) | |
346 (setq dirs (nreverse dirs) | |
347 rdirs (nreverse rdirs)) | |
348 (while dirs | |
349 (eshell-glob-entries (car dirs) (cdr globs)) | |
350 (setq dirs (cdr dirs))) | |
351 (while rdirs | |
352 (eshell-glob-entries (car rdirs) globs recurse-p) | |
353 (setq rdirs (cdr rdirs))))) | |
354 | |
355 ;;; Code: | |
356 | |
357 ;;; em-glob.el ends here |