em-glob.el 13 KB

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