]> code.delx.au - gnu-emacs/blob - lisp/eshell/em-glob.el
Fix a bunch of custom types (thank you cus-test.el)
[gnu-emacs] / lisp / eshell / em-glob.el
1 ;;; em-glob.el --- extended file name globbing
2
3 ;; Copyright (C) 1999-2013 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 (progn
57 (defgroup eshell-glob nil
58 "This module provides extended globbing syntax, similar what is used
59 by zsh for filename generation."
60 :tag "Extended filename globbing"
61 :group 'eshell-module))
62
63 ;;; User Variables:
64
65 (defcustom eshell-glob-load-hook nil
66 "A list of functions to run when `eshell-glob' is loaded."
67 :version "24.1" ; removed eshell-glob-initialize
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
81 (defcustom eshell-glob-case-insensitive (eshell-under-windows-p)
82 "If non-nil, glob pattern matching will ignore case."
83 :type 'boolean
84 :group 'eshell-glob)
85
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."
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 mimics the behavior of zsh if non-nil, but bash if nil."
95 :type 'boolean
96 :group 'eshell-glob)
97
98 (defcustom eshell-glob-chars-list '(?\] ?\[ ?* ?? ?~ ?\( ?\) ?| ?# ?^)
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 (?\[ . "[")
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 '(alist :key-type character
123 :value-type (choice string function))
124 :group 'eshell-glob)
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 (when (boundp 'eshell-special-chars-outside-quoting)
132 (set (make-local-variable 'eshell-special-chars-outside-quoting)
133 (append eshell-glob-chars-list eshell-special-chars-outside-quoting)))
134 (add-hook 'eshell-parse-argument-hook 'eshell-parse-glob-chars t t)
135 (add-hook 'eshell-pre-rewrite-command-hook
136 'eshell-no-command-globbing nil t))
137
138 (defun eshell-no-command-globbing (terms)
139 "Don't glob the command argument. Reflect this by modifying TERMS."
140 (ignore
141 (when (and (listp (car terms))
142 (eq (caar terms) 'eshell-extended-glob))
143 (setcar terms (cadr (car terms))))))
144
145 (defun eshell-add-glob-modifier ()
146 "Add `eshell-extended-glob' to the argument modifier list."
147 (when (memq 'expand-file-name eshell-current-modifiers)
148 (setq eshell-current-modifiers
149 (delq 'expand-file-name eshell-current-modifiers))
150 ;; if this is a glob pattern than needs to be expanded, then it
151 ;; will need to expand each member of the resulting glob list
152 (add-to-list 'eshell-current-modifiers
153 (lambda (list)
154 (if (listp list)
155 (mapcar 'expand-file-name list)
156 (expand-file-name list)))))
157 (add-to-list 'eshell-current-modifiers 'eshell-extended-glob))
158
159 (defun eshell-parse-glob-chars ()
160 "Parse a globbing delimiter.
161 The character is not advanced for ordinary globbing characters, so
162 that other function may have a chance to override the globbing
163 interpretation."
164 (when (memq (char-after) eshell-glob-chars-list)
165 (if (not (memq (char-after) '(?\( ?\[)))
166 (ignore (eshell-add-glob-modifier))
167 (let ((here (point)))
168 (forward-char)
169 (let* ((delim (char-before))
170 (end (eshell-find-delimiter
171 delim (if (eq delim ?\[) ?\] ?\)))))
172 (if (not end)
173 (throw 'eshell-incomplete delim)
174 (if (and (eshell-using-module 'eshell-pred)
175 (eshell-arg-delimiter (1+ end)))
176 (ignore (goto-char here))
177 (eshell-add-glob-modifier)
178 (prog1
179 (buffer-substring-no-properties (1- (point)) (1+ end))
180 (goto-char (1+ end))))))))))
181
182 (defvar eshell-glob-chars-regexp nil)
183
184 (defun eshell-glob-regexp (pattern)
185 "Convert glob-pattern PATTERN to a regular expression.
186 The basic syntax is:
187
188 glob regexp meaning
189 ---- ------ -------
190 ? . matches any single character
191 * .* matches any group of characters (or none)
192 # * matches zero or more occurrences of preceding
193 ## + matches one or more occurrences of preceding
194 (x) \(x\) makes 'x' a regular expression group
195 | \| boolean OR within an expression group
196 [a-b] [a-b] matches a character or range
197 [^a] [^a] excludes a character or range
198
199 If any characters in PATTERN have the text property `eshell-escaped'
200 set to true, then these characters will match themselves in the
201 resulting regular expression."
202 (let ((matched-in-pattern 0) ; How much of PATTERN handled
203 regexp)
204 (while (string-match
205 (or eshell-glob-chars-regexp
206 (set (make-local-variable 'eshell-glob-chars-regexp)
207 (format "[%s]+" (apply 'string eshell-glob-chars-list))))
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.
235 This function almost fully supports zsh style filename generation
236 syntax. Things that are not supported are:
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
243 Mainly they are not supported because file matching is done with Emacs
244 regular expressions, and these cannot support the above constructs.
245
246 If this routine fails, it returns nil. Otherwise, it returns a list
247 the form:
248
249 (INCLUDE-REGEXP EXCLUDE-REGEXP (PRED-FUNC-LIST) (MOD-FUNC-LIST))"
250 (let ((paths (eshell-split-path glob))
251 eshell-glob-matches message-shown ange-cache)
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 eshell-glob-matches (sort eshell-glob-matches #'string<))
261 (if eshell-error-if-no-glob
262 (error "No matches found: %s" glob)
263 glob))))
264
265 (defvar eshell-glob-matches)
266 (defvar message-shown)
267
268 ;; FIXME does this really need to abuse eshell-glob-matches, message-shown?
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))))
304 (if (eq (aref incl len) ?/)
305 (setq incl (substring incl 0 len)))
306 (when excl
307 (setq len (1- (length excl)))
308 (if (eq (aref excl len) ?/)
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 eshell-glob-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)
330 isdir (eq (aref name (1- len)) ?/))
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 eshell-glob-matches
338 (cons (concat path name) eshell-glob-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 (provide 'em-glob)
359
360 ;; Local Variables:
361 ;; generated-autoload-file: "esh-groups.el"
362 ;; End:
363
364 ;;; em-glob.el ends here