Mercurial > emacs
annotate lisp/eshell/em-glob.el @ 50955:7a718ea39fba
(sgml-namespace-re): New const.
(sgml-namespace-face): New face.
(sgml-font-lock-keywords-1): Use them.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Mon, 12 May 2003 15:46:02 +0000 |
parents | eeded772a8a2 |
children | 695cf19ef79e d7ddb3e565de |
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 | |
48210
eeded772a8a2
Require esh-util.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46852
diff
changeset
|
24 ;;; Code: |
eeded772a8a2
Require esh-util.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46852
diff
changeset
|
25 |
29876 | 26 (provide 'em-glob) |
27 | |
28 (eval-when-compile (require 'esh-maint)) | |
48210
eeded772a8a2
Require esh-util.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46852
diff
changeset
|
29 (require 'esh-util) |
29876 | 30 |
31 (defgroup eshell-glob nil | |
32 "This module provides extended globbing syntax, similar what is used | |
33 by zsh for filename generation." | |
34 :tag "Extended filename globbing" | |
35 :group 'eshell-module) | |
36 | |
37 ;;; Commentary: | |
38 | |
39 ;; The globbing code used by Eshell closely follows the syntax used by | |
40 ;; zsh. Basically, here is a summary of examples: | |
41 ;; | |
42 ;; echo a* ; anything starting with 'a' | |
43 ;; echo a#b ; zero or more 'a's, then 'b' | |
44 ;; echo a##b ; one or more 'a's, then 'b' | |
45 ;; echo a? ; a followed by any character | |
46 ;; echo a*~ab ; 'a', then anything, but not 'ab' | |
47 ;; echo c*~*~ ; all files beginning with 'c', except backups (*~) | |
48 ;; | |
49 ;; Recursive globbing is also supported: | |
50 ;; | |
51 ;; echo **/*.c ; all '.c' files at or under current directory | |
52 ;; echo ***/*.c ; same as above, but traverse symbolic links | |
53 ;; | |
54 ;; Using argument predication, the recursive globbing syntax is | |
55 ;; sufficient to replace the use of 'find <expr> | xargs <cmd>' in | |
56 ;; most cases. For example, to change the readership of all files | |
57 ;; belonging to 'johnw' in the '/tmp' directory or lower, use: | |
58 ;; | |
59 ;; chmod go-r /tmp/**/*(u'johnw') | |
60 ;; | |
61 ;; The glob above matches all of the files beneath '/tmp' that are | |
62 ;; owned by the user 'johnw'. See [Value modifiers and predicates], | |
63 ;; for more information about argument predication. | |
64 | |
65 ;;; User Variables: | |
66 | |
67 (defcustom eshell-glob-load-hook '(eshell-glob-initialize) | |
68 "*A list of functions to run when `eshell-glob' is loaded." | |
69 :type 'hook | |
70 :group 'eshell-glob) | |
71 | |
72 (defcustom eshell-glob-include-dot-files nil | |
73 "*If non-nil, glob patterns will match files beginning with a dot." | |
74 :type 'boolean | |
75 :group 'eshell-glob) | |
76 | |
77 (defcustom eshell-glob-include-dot-dot t | |
78 "*If non-nil, glob patterns that match dots will match . and .." | |
79 :type 'boolean | |
80 :group 'eshell-glob) | |
81 | |
46852
6eb625bead4f
Removed eshell-under-cygwin-p, and all uses of it.
John Wiegley <johnw@newartisans.com>
parents:
46820
diff
changeset
|
82 (defcustom eshell-glob-case-insensitive (eshell-under-windows-p) |
29876 | 83 "*If non-nil, glob pattern matching will ignore case." |
84 :type 'boolean | |
85 :group 'eshell-glob) | |
86 | |
33020 | 87 (defcustom eshell-glob-show-progress nil |
88 "*If non-nil, display progress messages during a recursive glob. | |
89 This option slows down recursive glob processing by quite a bit." | |
29876 | 90 :type 'boolean |
91 :group 'eshell-glob) | |
92 | |
93 (defcustom eshell-error-if-no-glob nil | |
94 "*If non-nil, it is an error for a glob pattern not to match. | |
95 This mimcs the behavior of zsh if non-nil, but bash if nil." | |
96 :type 'boolean | |
97 :group 'eshell-glob) | |
98 | |
99 (defcustom eshell-glob-chars-list '(?\] ?\[ ?* ?? ?~ ?\( ?\) ?| ?#) | |
100 "*List of additional characters used in extended globbing." | |
101 :type '(repeat character) | |
102 :group 'eshell-glob) | |
103 | |
104 (defcustom eshell-glob-translate-alist | |
105 '((?\] . "]") | |
106 (?\[ . "[") | |
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 ;;; Internal Variables: | |
126 | |
127 (defvar eshell-glob-chars-regexp nil) | |
128 | |
129 ;;; Functions: | |
130 | |
131 (defun eshell-glob-initialize () | |
132 "Initialize the extended globbing code." | |
133 ;; 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
|
134 (when (boundp 'eshell-special-chars-outside-quoting) |
eeded772a8a2
Require esh-util.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46852
diff
changeset
|
135 (set (make-local-variable 'eshell-special-chars-outside-quoting) |
eeded772a8a2
Require esh-util.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46852
diff
changeset
|
136 (append eshell-glob-chars-list eshell-special-chars-outside-quoting))) |
29876 | 137 (set (make-local-variable 'eshell-glob-chars-regexp) |
138 (format "[%s]+" (apply 'string eshell-glob-chars-list))) | |
139 (add-hook 'eshell-parse-argument-hook 'eshell-parse-glob-chars t t) | |
140 (add-hook 'eshell-pre-rewrite-command-hook | |
141 'eshell-no-command-globbing nil t)) | |
142 | |
143 (defun eshell-no-command-globbing (terms) | |
144 "Don't glob the command argument. Reflect this by modifying TERMS." | |
145 (ignore | |
146 (when (and (listp (car terms)) | |
147 (eq (caar terms) 'eshell-extended-glob)) | |
148 (setcar terms (cadr (car terms)))))) | |
149 | |
150 (defun eshell-add-glob-modifier () | |
151 "Add `eshell-extended-glob' to the argument modifier list." | |
152 (when (memq 'expand-file-name eshell-current-modifiers) | |
153 (setq eshell-current-modifiers | |
154 (delq 'expand-file-name eshell-current-modifiers)) | |
155 ;; if this is a glob pattern than needs to be expanded, then it | |
156 ;; will need to expand each member of the resulting glob list | |
157 (add-to-list 'eshell-current-modifiers | |
158 '(lambda (list) | |
159 (if (listp list) | |
160 (mapcar 'expand-file-name list) | |
161 (expand-file-name list))))) | |
162 (add-to-list 'eshell-current-modifiers 'eshell-extended-glob)) | |
163 | |
164 (defun eshell-parse-glob-chars () | |
165 "Parse a globbing delimiter. | |
166 The character is not advanced for ordinary globbing characters, so | |
167 that other function may have a chance to override the globbing | |
168 interpretation." | |
169 (when (memq (char-after) eshell-glob-chars-list) | |
170 (if (not (memq (char-after) '(?\( ?\[))) | |
171 (ignore (eshell-add-glob-modifier)) | |
172 (let ((here (point))) | |
173 (forward-char) | |
174 (let* ((delim (char-before)) | |
175 (end (eshell-find-delimiter | |
176 delim (if (eq delim ?\[) ?\] ?\))))) | |
177 (if (not end) | |
178 (throw 'eshell-incomplete delim) | |
179 (if (and (eshell-using-module 'eshell-pred) | |
180 (eshell-arg-delimiter (1+ end))) | |
181 (ignore (goto-char here)) | |
182 (eshell-add-glob-modifier) | |
183 (prog1 | |
184 (buffer-substring-no-properties (1- (point)) (1+ end)) | |
185 (goto-char (1+ end)))))))))) | |
186 | |
187 (defun eshell-glob-regexp (pattern) | |
188 "Convert glob-pattern PATTERN to a regular expression. | |
189 The basic syntax is: | |
190 | |
191 glob regexp meaning | |
192 ---- ------ ------- | |
193 ? . matches any single character | |
194 * .* matches any group of characters (or none) | |
195 # * matches zero or more occurrences of preceding | |
196 ## + matches one or more occurrences of preceding | |
197 (x) \(x\) makes 'x' a regular expression group | |
198 | \| boolean OR within an expression group | |
199 [a-b] [a-b] matches a character or range | |
200 [^a] [^a] excludes a character or range | |
201 | |
202 If any characters in PATTERN have the text property `eshell-escaped' | |
203 set to true, then these characters will match themselves in the | |
204 resulting regular expression." | |
205 (let ((matched-in-pattern 0) ; How much of PATTERN handled | |
206 regexp) | |
207 (while (string-match eshell-glob-chars-regexp | |
208 pattern matched-in-pattern) | |
209 (let* ((op-begin (match-beginning 0)) | |
210 (op-char (aref pattern op-begin))) | |
211 (setq regexp | |
212 (concat regexp | |
213 (regexp-quote | |
214 (substring pattern matched-in-pattern op-begin)))) | |
215 (if (get-text-property op-begin 'escaped pattern) | |
216 (setq regexp (concat regexp | |
217 (regexp-quote (char-to-string op-char))) | |
218 matched-in-pattern (1+ op-begin)) | |
219 (let ((xlat (assq op-char eshell-glob-translate-alist))) | |
220 (if (not xlat) | |
221 (error "Unrecognized globbing character '%c'" op-char) | |
222 (if (stringp (cdr xlat)) | |
223 (setq regexp (concat regexp (cdr xlat)) | |
224 matched-in-pattern (1+ op-begin)) | |
225 (let ((result (funcall (cdr xlat) pattern op-begin))) | |
226 (setq regexp (concat regexp (car result)) | |
227 matched-in-pattern (cdr result))))))))) | |
228 (concat "\\`" | |
229 regexp | |
230 (regexp-quote (substring pattern matched-in-pattern)) | |
231 "\\'"))) | |
232 | |
233 (defun eshell-extended-glob (glob) | |
234 "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
|
235 This function almost fully supports zsh style filename generation |
eeded772a8a2
Require esh-util.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46852
diff
changeset
|
236 syntax. Things that are not supported are: |
29876 | 237 |
238 ^foo for matching everything but foo | |
239 (foo~bar) tilde within a parenthesis group | |
240 foo<1-10> numeric ranges | |
241 foo~x(a|b) (a|b) will be interpreted as a predicate/modifier list | |
242 | |
48210
eeded772a8a2
Require esh-util.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46852
diff
changeset
|
243 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
|
244 regular expressions, and these cannot support the above constructs. |
29876 | 245 |
48210
eeded772a8a2
Require esh-util.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
46852
diff
changeset
|
246 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
|
247 the form: |
29876 | 248 |
249 (INCLUDE-REGEXP EXCLUDE-REGEXP (PRED-FUNC-LIST) (MOD-FUNC-LIST))" | |
250 (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
|
251 matches message-shown ange-cache) |
29876 | 252 (unwind-protect |
253 (if (and (cdr paths) | |
254 (file-name-absolute-p (car paths))) | |
255 (eshell-glob-entries (file-name-as-directory (car paths)) | |
256 (cdr paths)) | |
257 (eshell-glob-entries (file-name-as-directory ".") paths)) | |
258 (if message-shown | |
259 (message nil))) | |
260 (or (and matches (nreverse matches)) | |
261 (if eshell-error-if-no-glob | |
262 (error "No matches found: %s" glob) | |
263 glob)))) | |
264 | |
265 (eval-when-compile | |
266 (defvar matches) | |
267 (defvar message-shown)) | |
268 | |
269 ;; jww (1999-11-18): this function assumes that directory-sep-char is | |
270 ;; a forward slash (/) | |
271 | |
272 (defun eshell-glob-entries (path globs &optional recurse-p) | |
273 "Glob the entries in PATHS, possibly recursing if RECURSE-P is non-nil." | |
274 (let* ((entries (ignore-errors | |
275 (file-name-all-completions "" path))) | |
276 (case-fold-search eshell-glob-case-insensitive) | |
277 (glob (car globs)) | |
278 (len (length glob)) | |
279 dirs rdirs | |
280 incl excl | |
281 name isdir pathname) | |
282 (while (cond | |
283 ((and (= len 3) (equal glob "**/")) | |
284 (setq recurse-p 2 | |
285 globs (cdr globs) | |
286 glob (car globs) | |
287 len (length glob))) | |
288 ((and (= len 4) (equal glob "***/")) | |
289 (setq recurse-p 3 | |
290 globs (cdr globs) | |
291 glob (car globs) | |
292 len (length glob))))) | |
293 (if (and recurse-p (not glob)) | |
294 (error "'**' cannot end a globbing pattern")) | |
295 (let ((index 1)) | |
296 (setq incl glob) | |
297 (while (and (eq incl glob) | |
298 (setq index (string-match "~" glob index))) | |
299 (if (or (get-text-property index 'escaped glob) | |
300 (or (= (1+ index) len))) | |
301 (setq index (1+ index)) | |
302 (setq incl (substring glob 0 index) | |
303 excl (substring glob (1+ index)))))) | |
304 ;; can't use `directory-file-name' because it strips away text | |
305 ;; properties in the string | |
306 (let ((len (1- (length incl)))) | |
307 (if (eq (aref incl len) directory-sep-char) | |
308 (setq incl (substring incl 0 len))) | |
309 (when excl | |
310 (setq len (1- (length excl))) | |
311 (if (eq (aref excl len) directory-sep-char) | |
312 (setq excl (substring excl 0 len))))) | |
313 (setq incl (eshell-glob-regexp incl) | |
314 excl (and excl (eshell-glob-regexp excl))) | |
315 (if (or eshell-glob-include-dot-files | |
316 (eq (aref glob 0) ?.)) | |
317 (unless (or eshell-glob-include-dot-dot | |
318 (cdr globs)) | |
319 (setq excl (if excl | |
320 (concat "\\(\\`\\.\\.?\\'\\|" excl "\\)") | |
321 "\\`\\.\\.?\\'"))) | |
322 (setq excl (if excl | |
323 (concat "\\(\\`\\.\\|" excl "\\)") | |
324 "\\`\\."))) | |
325 (when (and recurse-p eshell-glob-show-progress) | |
326 (message "Building file list...%d so far: %s" | |
327 (length matches) path) | |
328 (setq message-shown t)) | |
329 (if (equal path "./") (setq path "")) | |
330 (while entries | |
331 (setq name (car entries) | |
332 len (length name) | |
333 isdir (eq (aref name (1- len)) directory-sep-char)) | |
334 (if (let ((fname (directory-file-name name))) | |
335 (and (not (and excl (string-match excl fname))) | |
336 (string-match incl fname))) | |
337 (if (cdr globs) | |
338 (if isdir | |
339 (setq dirs (cons (concat path name) dirs))) | |
340 (setq matches (cons (concat path name) matches)))) | |
341 (if (and recurse-p isdir | |
342 (or (> len 3) | |
343 (not (or (and (= len 2) (equal name "./")) | |
344 (and (= len 3) (equal name "../"))))) | |
345 (setq pathname (concat path name)) | |
346 (not (and (= recurse-p 2) | |
347 (file-symlink-p | |
348 (directory-file-name pathname))))) | |
349 (setq rdirs (cons pathname rdirs))) | |
350 (setq entries (cdr entries))) | |
351 (setq dirs (nreverse dirs) | |
352 rdirs (nreverse rdirs)) | |
353 (while dirs | |
354 (eshell-glob-entries (car dirs) (cdr globs)) | |
355 (setq dirs (cdr dirs))) | |
356 (while rdirs | |
357 (eshell-glob-entries (car rdirs) globs recurse-p) | |
358 (setq rdirs (cdr rdirs))))) | |
359 | |
360 ;;; em-glob.el ends here |