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