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