]> code.delx.au - gnu-emacs/blob - lisp/eshell/em-glob.el
Convert consecutive FSF copyright years to ranges.
[gnu-emacs] / lisp / eshell / em-glob.el
1 ;;; em-glob.el --- extended file name globbing
2
3 ;; Copyright (C) 1999-2011 Free Software Foundation, Inc.
4
5 ;; Author: John Wiegley <johnw@gnu.org>
6
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 3 of the License, or
12 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23
24 ;; The globbing code used by Eshell closely follows the syntax used by
25 ;; zsh. Basically, here is a summary of examples:
26 ;;
27 ;; echo a* ; anything starting with 'a'
28 ;; echo a#b ; zero or more 'a's, then 'b'
29 ;; echo a##b ; one or more 'a's, then 'b'
30 ;; echo a? ; a followed by any character
31 ;; echo a*~ab ; 'a', then anything, but not 'ab'
32 ;; echo c*~*~ ; all files beginning with 'c', except backups (*~)
33 ;;
34 ;; Recursive globbing is also supported:
35 ;;
36 ;; echo **/*.c ; all '.c' files at or under current directory
37 ;; echo ***/*.c ; same as above, but traverse symbolic links
38 ;;
39 ;; Using argument predication, the recursive globbing syntax is
40 ;; sufficient to replace the use of 'find <expr> | xargs <cmd>' in
41 ;; most cases. For example, to change the readership of all files
42 ;; belonging to 'johnw' in the '/tmp' directory or lower, use:
43 ;;
44 ;; chmod go-r /tmp/**/*(u'johnw')
45 ;;
46 ;; The glob above matches all of the files beneath '/tmp' that are
47 ;; owned by the user 'johnw'. See [Value modifiers and predicates],
48 ;; for more information about argument predication.
49
50 ;;; Code:
51
52 (eval-when-compile (require 'eshell))
53 (require 'esh-util)
54
55 ;;;###autoload
56 (eshell-defgroup eshell-glob nil
57 "This module provides extended globbing syntax, similar what is used
58 by zsh for filename generation."
59 :tag "Extended filename globbing"
60 :group 'eshell-module)
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
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."
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 (?\| . "\\|")
111 (?# . (lambda (str pos)
112 (if (and (< (1+ pos) (length str))
113 (memq (aref str (1+ pos)) '(?* ?# ?+ ??)))
114 (cons (if (eq (aref str (1+ pos)) ??)
115 "?"
116 (if (eq (aref str (1+ pos)) ?*)
117 "*" "+")) (+ pos 2))
118 (cons "*" (1+ pos))))))
119 "An alist for translation of extended globbing characters."
120 :type '(repeat (cons character (choice regexp function)))
121 :group 'eshell-glob)
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 (when (boundp 'eshell-special-chars-outside-quoting)
129 (set (make-local-variable 'eshell-special-chars-outside-quoting)
130 (append eshell-glob-chars-list eshell-special-chars-outside-quoting)))
131 (add-hook 'eshell-parse-argument-hook 'eshell-parse-glob-chars t t)
132 (add-hook 'eshell-pre-rewrite-command-hook
133 'eshell-no-command-globbing nil t))
134
135 (defun eshell-no-command-globbing (terms)
136 "Don't glob the command argument. Reflect this by modifying TERMS."
137 (ignore
138 (when (and (listp (car terms))
139 (eq (caar terms) 'eshell-extended-glob))
140 (setcar terms (cadr (car terms))))))
141
142 (defun eshell-add-glob-modifier ()
143 "Add `eshell-extended-glob' to the argument modifier list."
144 (when (memq 'expand-file-name eshell-current-modifiers)
145 (setq eshell-current-modifiers
146 (delq 'expand-file-name eshell-current-modifiers))
147 ;; if this is a glob pattern than needs to be expanded, then it
148 ;; will need to expand each member of the resulting glob list
149 (add-to-list 'eshell-current-modifiers
150 '(lambda (list)
151 (if (listp list)
152 (mapcar 'expand-file-name list)
153 (expand-file-name list)))))
154 (add-to-list 'eshell-current-modifiers 'eshell-extended-glob))
155
156 (defun eshell-parse-glob-chars ()
157 "Parse a globbing delimiter.
158 The character is not advanced for ordinary globbing characters, so
159 that other function may have a chance to override the globbing
160 interpretation."
161 (when (memq (char-after) eshell-glob-chars-list)
162 (if (not (memq (char-after) '(?\( ?\[)))
163 (ignore (eshell-add-glob-modifier))
164 (let ((here (point)))
165 (forward-char)
166 (let* ((delim (char-before))
167 (end (eshell-find-delimiter
168 delim (if (eq delim ?\[) ?\] ?\)))))
169 (if (not end)
170 (throw 'eshell-incomplete delim)
171 (if (and (eshell-using-module 'eshell-pred)
172 (eshell-arg-delimiter (1+ end)))
173 (ignore (goto-char here))
174 (eshell-add-glob-modifier)
175 (prog1
176 (buffer-substring-no-properties (1- (point)) (1+ end))
177 (goto-char (1+ end))))))))))
178
179 (defvar eshell-glob-chars-regexp nil)
180
181 (defun eshell-glob-regexp (pattern)
182 "Convert glob-pattern PATTERN to a regular expression.
183 The basic syntax is:
184
185 glob regexp meaning
186 ---- ------ -------
187 ? . matches any single character
188 * .* matches any group of characters (or none)
189 # * matches zero or more occurrences of preceding
190 ## + matches one or more occurrences of preceding
191 (x) \(x\) makes 'x' a regular expression group
192 | \| boolean OR within an expression group
193 [a-b] [a-b] matches a character or range
194 [^a] [^a] excludes a character or range
195
196 If any characters in PATTERN have the text property `eshell-escaped'
197 set to true, then these characters will match themselves in the
198 resulting regular expression."
199 (let ((matched-in-pattern 0) ; How much of PATTERN handled
200 regexp)
201 (while (string-match
202 (or eshell-glob-chars-regexp
203 (set (make-local-variable 'eshell-glob-chars-regexp)
204 (format "[%s]+" (apply 'string eshell-glob-chars-list))))
205 pattern matched-in-pattern)
206 (let* ((op-begin (match-beginning 0))
207 (op-char (aref pattern op-begin)))
208 (setq regexp
209 (concat regexp
210 (regexp-quote
211 (substring pattern matched-in-pattern op-begin))))
212 (if (get-text-property op-begin 'escaped pattern)
213 (setq regexp (concat regexp
214 (regexp-quote (char-to-string op-char)))
215 matched-in-pattern (1+ op-begin))
216 (let ((xlat (assq op-char eshell-glob-translate-alist)))
217 (if (not xlat)
218 (error "Unrecognized globbing character '%c'" op-char)
219 (if (stringp (cdr xlat))
220 (setq regexp (concat regexp (cdr xlat))
221 matched-in-pattern (1+ op-begin))
222 (let ((result (funcall (cdr xlat) pattern op-begin)))
223 (setq regexp (concat regexp (car result))
224 matched-in-pattern (cdr result)))))))))
225 (concat "\\`"
226 regexp
227 (regexp-quote (substring pattern matched-in-pattern))
228 "\\'")))
229
230 (defun eshell-extended-glob (glob)
231 "Return a list of files generated from GLOB, perhaps looking for DIRS-ONLY.
232 This function almost fully supports zsh style filename generation
233 syntax. Things that are not supported are:
234
235 ^foo for matching everything but foo
236 (foo~bar) tilde within a parenthesis group
237 foo<1-10> numeric ranges
238 foo~x(a|b) (a|b) will be interpreted as a predicate/modifier list
239
240 Mainly they are not supported because file matching is done with Emacs
241 regular expressions, and these cannot support the above constructs.
242
243 If this routine fails, it returns nil. Otherwise, it returns a list
244 the form:
245
246 (INCLUDE-REGEXP EXCLUDE-REGEXP (PRED-FUNC-LIST) (MOD-FUNC-LIST))"
247 (let ((paths (eshell-split-path glob))
248 eshell-glob-matches message-shown ange-cache)
249 (unwind-protect
250 (if (and (cdr paths)
251 (file-name-absolute-p (car paths)))
252 (eshell-glob-entries (file-name-as-directory (car paths))
253 (cdr paths))
254 (eshell-glob-entries (file-name-as-directory ".") paths))
255 (if message-shown
256 (message nil)))
257 (or (and eshell-glob-matches (sort eshell-glob-matches #'string<))
258 (if eshell-error-if-no-glob
259 (error "No matches found: %s" glob)
260 glob))))
261
262 (defvar eshell-glob-matches)
263 (defvar message-shown)
264
265 ;; FIXME does this really need to abuse eshell-glob-matches, message-shown?
266 (defun eshell-glob-entries (path globs &optional recurse-p)
267 "Glob the entries in PATHS, possibly recursing if RECURSE-P is non-nil."
268 (let* ((entries (ignore-errors
269 (file-name-all-completions "" path)))
270 (case-fold-search eshell-glob-case-insensitive)
271 (glob (car globs))
272 (len (length glob))
273 dirs rdirs
274 incl excl
275 name isdir pathname)
276 (while (cond
277 ((and (= len 3) (equal glob "**/"))
278 (setq recurse-p 2
279 globs (cdr globs)
280 glob (car globs)
281 len (length glob)))
282 ((and (= len 4) (equal glob "***/"))
283 (setq recurse-p 3
284 globs (cdr globs)
285 glob (car globs)
286 len (length glob)))))
287 (if (and recurse-p (not glob))
288 (error "'**' cannot end a globbing pattern"))
289 (let ((index 1))
290 (setq incl glob)
291 (while (and (eq incl glob)
292 (setq index (string-match "~" glob index)))
293 (if (or (get-text-property index 'escaped glob)
294 (or (= (1+ index) len)))
295 (setq index (1+ index))
296 (setq incl (substring glob 0 index)
297 excl (substring glob (1+ index))))))
298 ;; can't use `directory-file-name' because it strips away text
299 ;; properties in the string
300 (let ((len (1- (length incl))))
301 (if (eq (aref incl len) ?/)
302 (setq incl (substring incl 0 len)))
303 (when excl
304 (setq len (1- (length excl)))
305 (if (eq (aref excl len) ?/)
306 (setq excl (substring excl 0 len)))))
307 (setq incl (eshell-glob-regexp incl)
308 excl (and excl (eshell-glob-regexp excl)))
309 (if (or eshell-glob-include-dot-files
310 (eq (aref glob 0) ?.))
311 (unless (or eshell-glob-include-dot-dot
312 (cdr globs))
313 (setq excl (if excl
314 (concat "\\(\\`\\.\\.?\\'\\|" excl "\\)")
315 "\\`\\.\\.?\\'")))
316 (setq excl (if excl
317 (concat "\\(\\`\\.\\|" excl "\\)")
318 "\\`\\.")))
319 (when (and recurse-p eshell-glob-show-progress)
320 (message "Building file list...%d so far: %s"
321 (length eshell-glob-matches) path)
322 (setq message-shown t))
323 (if (equal path "./") (setq path ""))
324 (while entries
325 (setq name (car entries)
326 len (length name)
327 isdir (eq (aref name (1- len)) ?/))
328 (if (let ((fname (directory-file-name name)))
329 (and (not (and excl (string-match excl fname)))
330 (string-match incl fname)))
331 (if (cdr globs)
332 (if isdir
333 (setq dirs (cons (concat path name) dirs)))
334 (setq eshell-glob-matches
335 (cons (concat path name) eshell-glob-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 (provide 'em-glob)
356
357 ;; Local Variables:
358 ;; generated-autoload-file: "esh-groups.el"
359 ;; End:
360
361 ;;; em-glob.el ends here