*** empty log message ***
[bpt/emacs.git] / lisp / eshell / em-glob.el
CommitLineData
60370d40 1;;; em-glob.el --- extended file name globbing
affbf647 2
faadfb0a 3;; Copyright (C) 1999, 2000 Free Software Foundation
affbf647 4
7de5b421
GM
5;; Author: John Wiegley <johnw@gnu.org>
6
affbf647
GM
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
6d736b08
SM
24;;; Code:
25
affbf647
GM
26(provide 'em-glob)
27
28(eval-when-compile (require 'esh-maint))
6d736b08 29(require 'esh-util)
affbf647
GM
30
31(defgroup eshell-glob nil
32 "This module provides extended globbing syntax, similar what is used
33by 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
70a06174 82(defcustom eshell-glob-case-insensitive (eshell-under-windows-p)
affbf647
GM
83 "*If non-nil, glob pattern matching will ignore case."
84 :type 'boolean
85 :group 'eshell-glob)
86
dace60cf
JW
87(defcustom eshell-glob-show-progress nil
88 "*If non-nil, display progress messages during a recursive glob.
89This option slows down recursive glob processing by quite a bit."
affbf647
GM
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
affbf647
GM
125;;; Functions:
126
127(defun eshell-glob-initialize ()
128 "Initialize the extended globbing code."
129 ;; it's important that `eshell-glob-chars-list' come first
6d736b08
SM
130 (when (boundp 'eshell-special-chars-outside-quoting)
131 (set (make-local-variable 'eshell-special-chars-outside-quoting)
132 (append eshell-glob-chars-list eshell-special-chars-outside-quoting)))
affbf647 133 (add-hook 'eshell-parse-argument-hook 'eshell-parse-glob-chars t t)
affbf647
GM
134 (add-hook 'eshell-pre-rewrite-command-hook
135 'eshell-no-command-globbing nil t))
136
137(defun eshell-no-command-globbing (terms)
138 "Don't glob the command argument. Reflect this by modifying TERMS."
139 (ignore
140 (when (and (listp (car terms))
141 (eq (caar terms) 'eshell-extended-glob))
142 (setcar terms (cadr (car terms))))))
143
144(defun eshell-add-glob-modifier ()
145 "Add `eshell-extended-glob' to the argument modifier list."
146 (when (memq 'expand-file-name eshell-current-modifiers)
147 (setq eshell-current-modifiers
148 (delq 'expand-file-name eshell-current-modifiers))
149 ;; if this is a glob pattern than needs to be expanded, then it
150 ;; will need to expand each member of the resulting glob list
151 (add-to-list 'eshell-current-modifiers
152 '(lambda (list)
153 (if (listp list)
154 (mapcar 'expand-file-name list)
155 (expand-file-name list)))))
156 (add-to-list 'eshell-current-modifiers 'eshell-extended-glob))
157
158(defun eshell-parse-glob-chars ()
159 "Parse a globbing delimiter.
160The character is not advanced for ordinary globbing characters, so
161that other function may have a chance to override the globbing
162interpretation."
163 (when (memq (char-after) eshell-glob-chars-list)
164 (if (not (memq (char-after) '(?\( ?\[)))
165 (ignore (eshell-add-glob-modifier))
166 (let ((here (point)))
167 (forward-char)
168 (let* ((delim (char-before))
169 (end (eshell-find-delimiter
170 delim (if (eq delim ?\[) ?\] ?\)))))
171 (if (not end)
172 (throw 'eshell-incomplete delim)
173 (if (and (eshell-using-module 'eshell-pred)
174 (eshell-arg-delimiter (1+ end)))
175 (ignore (goto-char here))
176 (eshell-add-glob-modifier)
177 (prog1
178 (buffer-substring-no-properties (1- (point)) (1+ end))
179 (goto-char (1+ end))))))))))
180
4403b1e1
JW
181(defvar eshell-glob-chars-regexp nil)
182
affbf647
GM
183(defun eshell-glob-regexp (pattern)
184 "Convert glob-pattern PATTERN to a regular expression.
185The 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
198If any characters in PATTERN have the text property `eshell-escaped'
199set to true, then these characters will match themselves in the
200resulting regular expression."
201 (let ((matched-in-pattern 0) ; How much of PATTERN handled
202 regexp)
4403b1e1
JW
203 (while (string-match
204 (or eshell-glob-chars-regexp
205 (set (make-local-variable 'eshell-glob-chars-regexp)
206 (format "[%s]+" (apply 'string eshell-glob-chars-list))))
207 pattern matched-in-pattern)
affbf647
GM
208 (let* ((op-begin (match-beginning 0))
209 (op-char (aref pattern op-begin)))
210 (setq regexp
211 (concat regexp
212 (regexp-quote
213 (substring pattern matched-in-pattern op-begin))))
214 (if (get-text-property op-begin 'escaped pattern)
215 (setq regexp (concat regexp
216 (regexp-quote (char-to-string op-char)))
217 matched-in-pattern (1+ op-begin))
218 (let ((xlat (assq op-char eshell-glob-translate-alist)))
219 (if (not xlat)
220 (error "Unrecognized globbing character '%c'" op-char)
221 (if (stringp (cdr xlat))
222 (setq regexp (concat regexp (cdr xlat))
223 matched-in-pattern (1+ op-begin))
224 (let ((result (funcall (cdr xlat) pattern op-begin)))
225 (setq regexp (concat regexp (car result))
226 matched-in-pattern (cdr result)))))))))
227 (concat "\\`"
228 regexp
229 (regexp-quote (substring pattern matched-in-pattern))
230 "\\'")))
231
232(defun eshell-extended-glob (glob)
233 "Return a list of files generated from GLOB, perhaps looking for DIRS-ONLY.
6d736b08
SM
234This function almost fully supports zsh style filename generation
235syntax. Things that are not supported are:
affbf647
GM
236
237 ^foo for matching everything but foo
238 (foo~bar) tilde within a parenthesis group
239 foo<1-10> numeric ranges
240 foo~x(a|b) (a|b) will be interpreted as a predicate/modifier list
241
6d736b08
SM
242Mainly they are not supported because file matching is done with Emacs
243regular expressions, and these cannot support the above constructs.
affbf647 244
6d736b08
SM
245If this routine fails, it returns nil. Otherwise, it returns a list
246the form:
affbf647
GM
247
248 (INCLUDE-REGEXP EXCLUDE-REGEXP (PRED-FUNC-LIST) (MOD-FUNC-LIST))"
249 (let ((paths (eshell-split-path glob))
8c6b1d83 250 matches message-shown ange-cache)
affbf647
GM
251 (unwind-protect
252 (if (and (cdr paths)
253 (file-name-absolute-p (car paths)))
254 (eshell-glob-entries (file-name-as-directory (car paths))
255 (cdr paths))
256 (eshell-glob-entries (file-name-as-directory ".") paths))
257 (if message-shown
258 (message nil)))
259 (or (and matches (nreverse matches))
260 (if eshell-error-if-no-glob
261 (error "No matches found: %s" glob)
262 glob))))
263
264(eval-when-compile
265 (defvar matches)
266 (defvar message-shown))
267
268;; jww (1999-11-18): this function assumes that directory-sep-char is
269;; a forward slash (/)
270
271(defun eshell-glob-entries (path globs &optional recurse-p)
272 "Glob the entries in PATHS, possibly recursing if RECURSE-P is non-nil."
273 (let* ((entries (ignore-errors
274 (file-name-all-completions "" path)))
275 (case-fold-search eshell-glob-case-insensitive)
276 (glob (car globs))
277 (len (length glob))
278 dirs rdirs
279 incl excl
280 name isdir pathname)
281 (while (cond
282 ((and (= len 3) (equal glob "**/"))
283 (setq recurse-p 2
284 globs (cdr globs)
285 glob (car globs)
286 len (length glob)))
287 ((and (= len 4) (equal glob "***/"))
288 (setq recurse-p 3
289 globs (cdr globs)
290 glob (car globs)
291 len (length glob)))))
292 (if (and recurse-p (not glob))
293 (error "'**' cannot end a globbing pattern"))
294 (let ((index 1))
295 (setq incl glob)
296 (while (and (eq incl glob)
297 (setq index (string-match "~" glob index)))
298 (if (or (get-text-property index 'escaped glob)
299 (or (= (1+ index) len)))
300 (setq index (1+ index))
301 (setq incl (substring glob 0 index)
302 excl (substring glob (1+ index))))))
303 ;; can't use `directory-file-name' because it strips away text
304 ;; properties in the string
305 (let ((len (1- (length incl))))
306 (if (eq (aref incl len) directory-sep-char)
307 (setq incl (substring incl 0 len)))
308 (when excl
309 (setq len (1- (length excl)))
310 (if (eq (aref excl len) directory-sep-char)
311 (setq excl (substring excl 0 len)))))
312 (setq incl (eshell-glob-regexp incl)
313 excl (and excl (eshell-glob-regexp excl)))
314 (if (or eshell-glob-include-dot-files
315 (eq (aref glob 0) ?.))
316 (unless (or eshell-glob-include-dot-dot
317 (cdr globs))
318 (setq excl (if excl
319 (concat "\\(\\`\\.\\.?\\'\\|" excl "\\)")
320 "\\`\\.\\.?\\'")))
321 (setq excl (if excl
322 (concat "\\(\\`\\.\\|" excl "\\)")
323 "\\`\\.")))
324 (when (and recurse-p eshell-glob-show-progress)
325 (message "Building file list...%d so far: %s"
326 (length matches) path)
327 (setq message-shown t))
328 (if (equal path "./") (setq path ""))
329 (while entries
330 (setq name (car entries)
331 len (length name)
332 isdir (eq (aref name (1- len)) directory-sep-char))
333 (if (let ((fname (directory-file-name name)))
334 (and (not (and excl (string-match excl fname)))
335 (string-match incl fname)))
336 (if (cdr globs)
337 (if isdir
338 (setq dirs (cons (concat path name) dirs)))
339 (setq matches (cons (concat path name) matches))))
340 (if (and recurse-p isdir
341 (or (> len 3)
342 (not (or (and (= len 2) (equal name "./"))
343 (and (= len 3) (equal name "../")))))
344 (setq pathname (concat path name))
345 (not (and (= recurse-p 2)
346 (file-symlink-p
347 (directory-file-name pathname)))))
348 (setq rdirs (cons pathname rdirs)))
349 (setq entries (cdr entries)))
350 (setq dirs (nreverse dirs)
351 rdirs (nreverse rdirs))
352 (while dirs
353 (eshell-glob-entries (car dirs) (cdr globs))
354 (setq dirs (cdr dirs)))
355 (while rdirs
356 (eshell-glob-entries (car rdirs) globs recurse-p)
357 (setq rdirs (cdr rdirs)))))
358
ab5796a9 359;;; arch-tag: d0548f54-fb7c-4978-a88e-f7c26f7f68ca
affbf647 360;;; em-glob.el ends here