Mercurial > emacs
annotate lisp/eshell/em-glob.el @ 37590:003e23c611eb
(rmail-summary-sort-by-labels): Renamed
from rmail-summary-sort-by-keywords. Doc fix.
(rmail-summary-mode): Doc fix.
author | Gerd Moellmann <gerd@gnu.org> |
---|---|
date | Mon, 07 May 2001 10:05:41 +0000 |
parents | e21feeab77fb |
children | 67b464da13ec |
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 |
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 (make-local-hook 'eshell-parse-argument-hook) | |
136 (add-hook 'eshell-parse-argument-hook 'eshell-parse-glob-chars t t) | |
137 (make-local-hook 'eshell-pre-rewrite-command-hook) | |
138 (add-hook 'eshell-pre-rewrite-command-hook | |
139 'eshell-no-command-globbing nil t)) | |
140 | |
141 (defun eshell-no-command-globbing (terms) | |
142 "Don't glob the command argument. Reflect this by modifying TERMS." | |
143 (ignore | |
144 (when (and (listp (car terms)) | |
145 (eq (caar terms) 'eshell-extended-glob)) | |
146 (setcar terms (cadr (car terms)))))) | |
147 | |
148 (defun eshell-add-glob-modifier () | |
149 "Add `eshell-extended-glob' to the argument modifier list." | |
150 (when (memq 'expand-file-name eshell-current-modifiers) | |
151 (setq eshell-current-modifiers | |
152 (delq 'expand-file-name eshell-current-modifiers)) | |
153 ;; if this is a glob pattern than needs to be expanded, then it | |
154 ;; will need to expand each member of the resulting glob list | |
155 (add-to-list 'eshell-current-modifiers | |
156 '(lambda (list) | |
157 (if (listp list) | |
158 (mapcar 'expand-file-name list) | |
159 (expand-file-name list))))) | |
160 (add-to-list 'eshell-current-modifiers 'eshell-extended-glob)) | |
161 | |
162 (defun eshell-parse-glob-chars () | |
163 "Parse a globbing delimiter. | |
164 The character is not advanced for ordinary globbing characters, so | |
165 that other function may have a chance to override the globbing | |
166 interpretation." | |
167 (when (memq (char-after) eshell-glob-chars-list) | |
168 (if (not (memq (char-after) '(?\( ?\[))) | |
169 (ignore (eshell-add-glob-modifier)) | |
170 (let ((here (point))) | |
171 (forward-char) | |
172 (let* ((delim (char-before)) | |
173 (end (eshell-find-delimiter | |
174 delim (if (eq delim ?\[) ?\] ?\))))) | |
175 (if (not end) | |
176 (throw 'eshell-incomplete delim) | |
177 (if (and (eshell-using-module 'eshell-pred) | |
178 (eshell-arg-delimiter (1+ end))) | |
179 (ignore (goto-char here)) | |
180 (eshell-add-glob-modifier) | |
181 (prog1 | |
182 (buffer-substring-no-properties (1- (point)) (1+ end)) | |
183 (goto-char (1+ end)))))))))) | |
184 | |
185 (defun eshell-glob-regexp (pattern) | |
186 "Convert glob-pattern PATTERN to a regular expression. | |
187 The basic syntax is: | |
188 | |
189 glob regexp meaning | |
190 ---- ------ ------- | |
191 ? . matches any single character | |
192 * .* matches any group of characters (or none) | |
193 # * matches zero or more occurrences of preceding | |
194 ## + matches one or more occurrences of preceding | |
195 (x) \(x\) makes 'x' a regular expression group | |
196 | \| boolean OR within an expression group | |
197 [a-b] [a-b] matches a character or range | |
198 [^a] [^a] excludes a character or range | |
199 | |
200 If any characters in PATTERN have the text property `eshell-escaped' | |
201 set to true, then these characters will match themselves in the | |
202 resulting regular expression." | |
203 (let ((matched-in-pattern 0) ; How much of PATTERN handled | |
204 regexp) | |
205 (while (string-match eshell-glob-chars-regexp | |
206 pattern matched-in-pattern) | |
207 (let* ((op-begin (match-beginning 0)) | |
208 (op-char (aref pattern op-begin))) | |
209 (setq regexp | |
210 (concat regexp | |
211 (regexp-quote | |
212 (substring pattern matched-in-pattern op-begin)))) | |
213 (if (get-text-property op-begin 'escaped pattern) | |
214 (setq regexp (concat regexp | |
215 (regexp-quote (char-to-string op-char))) | |
216 matched-in-pattern (1+ op-begin)) | |
217 (let ((xlat (assq op-char eshell-glob-translate-alist))) | |
218 (if (not xlat) | |
219 (error "Unrecognized globbing character '%c'" op-char) | |
220 (if (stringp (cdr xlat)) | |
221 (setq regexp (concat regexp (cdr xlat)) | |
222 matched-in-pattern (1+ op-begin)) | |
223 (let ((result (funcall (cdr xlat) pattern op-begin))) | |
224 (setq regexp (concat regexp (car result)) | |
225 matched-in-pattern (cdr result))))))))) | |
226 (concat "\\`" | |
227 regexp | |
228 (regexp-quote (substring pattern matched-in-pattern)) | |
229 "\\'"))) | |
230 | |
231 (defun eshell-extended-glob (glob) | |
232 "Return a list of files generated from GLOB, perhaps looking for DIRS-ONLY. | |
233 This function almost fully supports zsh style filename generation | |
234 syntax. Things that are not supported are: | |
235 | |
236 ^foo for matching everything but foo | |
237 (foo~bar) tilde within a parenthesis group | |
238 foo<1-10> numeric ranges | |
239 foo~x(a|b) (a|b) will be interpreted as a predicate/modifier list | |
240 | |
241 Mainly they are not supported because file matching is done with Emacs | |
242 regular expressions, and these cannot support the above constructs. | |
243 | |
244 If this routine fails, it returns nil. Otherwise, it returns a list | |
245 the form: | |
246 | |
247 (INCLUDE-REGEXP EXCLUDE-REGEXP (PRED-FUNC-LIST) (MOD-FUNC-LIST))" | |
248 (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
|
249 matches message-shown ange-cache) |
29876 | 250 (unwind-protect |
251 (if (and (cdr paths) | |
252 (file-name-absolute-p (car paths))) | |
253 (eshell-glob-entries (file-name-as-directory (car paths)) | |
254 (cdr paths)) | |
255 (eshell-glob-entries (file-name-as-directory ".") paths)) | |
256 (if message-shown | |
257 (message nil))) | |
258 (or (and matches (nreverse matches)) | |
259 (if eshell-error-if-no-glob | |
260 (error "No matches found: %s" glob) | |
261 glob)))) | |
262 | |
263 (eval-when-compile | |
264 (defvar matches) | |
265 (defvar message-shown)) | |
266 | |
267 ;; jww (1999-11-18): this function assumes that directory-sep-char is | |
268 ;; a forward slash (/) | |
269 | |
270 (defun eshell-glob-entries (path globs &optional recurse-p) | |
271 "Glob the entries in PATHS, possibly recursing if RECURSE-P is non-nil." | |
272 (let* ((entries (ignore-errors | |
273 (file-name-all-completions "" path))) | |
274 (case-fold-search eshell-glob-case-insensitive) | |
275 (glob (car globs)) | |
276 (len (length glob)) | |
277 dirs rdirs | |
278 incl excl | |
279 name isdir pathname) | |
280 (while (cond | |
281 ((and (= len 3) (equal glob "**/")) | |
282 (setq recurse-p 2 | |
283 globs (cdr globs) | |
284 glob (car globs) | |
285 len (length glob))) | |
286 ((and (= len 4) (equal glob "***/")) | |
287 (setq recurse-p 3 | |
288 globs (cdr globs) | |
289 glob (car globs) | |
290 len (length glob))))) | |
291 (if (and recurse-p (not glob)) | |
292 (error "'**' cannot end a globbing pattern")) | |
293 (let ((index 1)) | |
294 (setq incl glob) | |
295 (while (and (eq incl glob) | |
296 (setq index (string-match "~" glob index))) | |
297 (if (or (get-text-property index 'escaped glob) | |
298 (or (= (1+ index) len))) | |
299 (setq index (1+ index)) | |
300 (setq incl (substring glob 0 index) | |
301 excl (substring glob (1+ index)))))) | |
302 ;; can't use `directory-file-name' because it strips away text | |
303 ;; properties in the string | |
304 (let ((len (1- (length incl)))) | |
305 (if (eq (aref incl len) directory-sep-char) | |
306 (setq incl (substring incl 0 len))) | |
307 (when excl | |
308 (setq len (1- (length excl))) | |
309 (if (eq (aref excl len) directory-sep-char) | |
310 (setq excl (substring excl 0 len))))) | |
311 (setq incl (eshell-glob-regexp incl) | |
312 excl (and excl (eshell-glob-regexp excl))) | |
313 (if (or eshell-glob-include-dot-files | |
314 (eq (aref glob 0) ?.)) | |
315 (unless (or eshell-glob-include-dot-dot | |
316 (cdr globs)) | |
317 (setq excl (if excl | |
318 (concat "\\(\\`\\.\\.?\\'\\|" excl "\\)") | |
319 "\\`\\.\\.?\\'"))) | |
320 (setq excl (if excl | |
321 (concat "\\(\\`\\.\\|" excl "\\)") | |
322 "\\`\\."))) | |
323 (when (and recurse-p eshell-glob-show-progress) | |
324 (message "Building file list...%d so far: %s" | |
325 (length matches) path) | |
326 (setq message-shown t)) | |
327 (if (equal path "./") (setq path "")) | |
328 (while entries | |
329 (setq name (car entries) | |
330 len (length name) | |
331 isdir (eq (aref name (1- len)) directory-sep-char)) | |
332 (if (let ((fname (directory-file-name name))) | |
333 (and (not (and excl (string-match excl fname))) | |
334 (string-match incl fname))) | |
335 (if (cdr globs) | |
336 (if isdir | |
337 (setq dirs (cons (concat path name) dirs))) | |
338 (setq matches (cons (concat path name) matches)))) | |
339 (if (and recurse-p isdir | |
340 (or (> len 3) | |
341 (not (or (and (= len 2) (equal name "./")) | |
342 (and (= len 3) (equal name "../"))))) | |
343 (setq pathname (concat path name)) | |
344 (not (and (= recurse-p 2) | |
345 (file-symlink-p | |
346 (directory-file-name pathname))))) | |
347 (setq rdirs (cons pathname rdirs))) | |
348 (setq entries (cdr entries))) | |
349 (setq dirs (nreverse dirs) | |
350 rdirs (nreverse rdirs)) | |
351 (while dirs | |
352 (eshell-glob-entries (car dirs) (cdr globs)) | |
353 (setq dirs (cdr dirs))) | |
354 (while rdirs | |
355 (eshell-glob-entries (car rdirs) globs recurse-p) | |
356 (setq rdirs (cdr rdirs))))) | |
357 | |
358 ;;; Code: | |
359 | |
360 ;;; em-glob.el ends here |