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