em-glob.el 12 KB

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