Tweak byte-compile-file-form-autoload warnings
[emacs.git] / lisp / eshell / em-pred.el
blob13bdb2e4a0300bc4e03f0cdfb9e2aacd8d7b70e6
1 ;;; em-pred.el --- argument predicates and modifiers (ala zsh)
3 ;; Copyright (C) 1999-2013 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
7 ;; This file is part of GNU Emacs.
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.
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.
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/>.
22 ;;; Commentary:
24 ;; Argument predication is used to affect which members of a list are
25 ;; selected for use as argument. This is most useful with globbing,
26 ;; but can be used on any list argument, to select certain members.
28 ;; Argument modifiers are used to manipulate argument values. For
29 ;; example, sorting lists, upcasing words, substituting characters,
30 ;; etc.
32 ;; Here are some examples of how to use argument predication. Most of
33 ;; the predicates and modifiers are modeled after those provided by
34 ;; zsh.
36 ;; ls -ld *(/) ; list all directories
37 ;; ls -l *(@u'johnw') ; list all symlinks owned by 'johnw'
38 ;; bzip2 -9v **/*(a+30) ; compress everything which hasn't been
39 ;; accessed in 30 days
40 ;; echo *.c(:o:R) ; a reversed, sorted list of C files
41 ;; *(^@:U^u0) ; all non-symlinks not owned by 'root', upcased
42 ;; chmod u-x *(U*) : remove exec bit on all executables owned by user
44 ;; See the zsh docs for more on the syntax ([(zsh.info)Filename
45 ;; Generation]).
47 ;;; Code:
49 (eval-when-compile (require 'eshell))
51 ;;;###autoload
52 (progn
53 (defgroup eshell-pred nil
54 "This module allows for predicates to be applied to globbing
55 patterns (similar to zsh), in addition to string modifiers which can
56 be applied either to globbing results, variable references, or just
57 ordinary strings."
58 :tag "Value modifiers and predicates"
59 :group 'eshell-module))
61 ;;; User Variables:
63 (defcustom eshell-pred-load-hook nil
64 "A list of functions to run when `eshell-pred' is loaded."
65 :version "24.1" ; removed eshell-pred-initialize
66 :type 'hook
67 :group 'eshell-pred)
69 (defcustom eshell-predicate-alist
70 '((?/ . (eshell-pred-file-type ?d)) ; directories
71 (?. . (eshell-pred-file-type ?-)) ; regular files
72 (?s . (eshell-pred-file-type ?s)) ; sockets
73 (?p . (eshell-pred-file-type ?p)) ; named pipes
74 (?@ . (eshell-pred-file-type ?l)) ; symbolic links
75 (?% . (eshell-pred-file-type ?%)) ; allow user to specify (c def.)
76 (?r . (eshell-pred-file-mode 0400)) ; owner-readable
77 (?w . (eshell-pred-file-mode 0200)) ; owner-writable
78 (?x . (eshell-pred-file-mode 0100)) ; owner-executable
79 (?A . (eshell-pred-file-mode 0040)) ; group-readable
80 (?I . (eshell-pred-file-mode 0020)) ; group-writable
81 (?E . (eshell-pred-file-mode 0010)) ; group-executable
82 (?R . (eshell-pred-file-mode 0004)) ; world-readable
83 (?W . (eshell-pred-file-mode 0002)) ; world-writable
84 (?X . (eshell-pred-file-mode 0001)) ; world-executable
85 (?s . (eshell-pred-file-mode 4000)) ; setuid
86 (?S . (eshell-pred-file-mode 2000)) ; setgid
87 (?t . (eshell-pred-file-mode 1000)) ; sticky bit
88 (?U . #'(lambda (file) ; owned by effective uid
89 (if (file-exists-p file)
90 (= (nth 2 (file-attributes file)) (user-uid)))))
91 ;; (?G . #'(lambda (file) ; owned by effective gid
92 ;; (if (file-exists-p file)
93 ;; (= (nth 2 (file-attributes file)) (user-uid)))))
94 (?* . #'(lambda (file)
95 (and (file-regular-p file)
96 (not (file-symlink-p file))
97 (file-executable-p file))))
98 (?l . (eshell-pred-file-links))
99 (?u . (eshell-pred-user-or-group ?u "user" 2 'eshell-user-id))
100 (?g . (eshell-pred-user-or-group ?g "group" 3 'eshell-group-id))
101 (?a . (eshell-pred-file-time ?a "access" 4))
102 (?m . (eshell-pred-file-time ?m "modification" 5))
103 (?c . (eshell-pred-file-time ?c "change" 6))
104 (?L . (eshell-pred-file-size)))
105 "A list of predicates than can be applied to a globbing pattern.
106 The format of each entry is
108 (CHAR . PREDICATE-FUNC-SEXP)"
109 :type '(repeat (cons character sexp))
110 :group 'eshell-pred)
112 (put 'eshell-predicate-alist 'risky-local-variable t)
114 (defcustom eshell-modifier-alist
115 '((?E . #'(lambda (lst)
116 (mapcar
117 (function
118 (lambda (str)
119 (eshell-stringify
120 (car (eshell-parse-argument str))))) lst)))
121 (?L . #'(lambda (lst) (mapcar 'downcase lst)))
122 (?U . #'(lambda (lst) (mapcar 'upcase lst)))
123 (?C . #'(lambda (lst) (mapcar 'capitalize lst)))
124 (?h . #'(lambda (lst) (mapcar 'file-name-directory lst)))
125 (?i . (eshell-include-members))
126 (?x . (eshell-include-members t))
127 (?r . #'(lambda (lst) (mapcar 'file-name-sans-extension lst)))
128 (?e . #'(lambda (lst) (mapcar 'file-name-extension lst)))
129 (?t . #'(lambda (lst) (mapcar 'file-name-nondirectory lst)))
130 (?q . #'(lambda (lst) (mapcar 'eshell-escape-arg lst)))
131 (?u . #'(lambda (lst) (eshell-uniqify-list lst)))
132 (?o . #'(lambda (lst) (sort lst 'string-lessp)))
133 (?O . #'(lambda (lst) (nreverse (sort lst 'string-lessp))))
134 (?j . (eshell-join-members))
135 (?S . (eshell-split-members))
136 (?R . 'reverse)
137 (?g . (progn
138 (forward-char)
139 (if (eq (char-before) ?s)
140 (eshell-pred-substitute t)
141 (error "`g' modifier cannot be used alone"))))
142 (?s . (eshell-pred-substitute)))
143 "A list of modifiers than can be applied to an argument expansion.
144 The format of each entry is
146 (CHAR ENTRYWISE-P MODIFIER-FUNC-SEXP)"
147 :type '(repeat (cons character sexp))
148 :group 'eshell-pred)
150 (put 'eshell-modifier-alist 'risky-local-variable t)
152 (defvar eshell-predicate-help-string
153 "Eshell predicate quick reference:
155 - follow symbolic references for predicates after the `-'
156 ^ invert sense of predicates after the `^'
158 FILE TYPE:
159 / directories s sockets
160 . regular files p named pipes
161 * executable (files only) @ symbolic links
163 %x file type == `x' (as by ls -l; so `c' = char device, etc.)
165 PERMISSION BITS (for owner/group/world):
166 r/A/R readable s setuid
167 w/I/W writable S setgid
168 x/E/X executable t sticky bit
170 OWNERSHIP:
171 U owned by effective uid
172 u(UID|'user') owned by UID/user
173 g(GID|'group') owned by GID/group
175 FILE ATTRIBUTES:
176 l[+-]N +/-/= N links
177 a[Mwhms][+-](N|'FILE') access time +/-/= N months/weeks/hours/mins/secs
178 (days if unspecified) if FILE specified,
179 use as comparison basis; so a+'file.c'
180 shows files accessed before file.c was
181 last accessed
182 m[Mwhms][+-](N|'FILE') modification time...
183 c[Mwhms][+-](N|'FILE') change time...
184 L[kmp][+-]N file size +/-/= N Kb/Mb/blocks
186 EXAMPLES:
187 *(^@) all non-dot files which are not symlinks
188 .#*(^@) all files which are not symbolic links
189 **/.#*(*) all executable files, searched recursively
190 ***/*~f*(-/) recursively (though not traversing symlinks),
191 find all directories (or symlinks referring to
192 directories) whose names do not begin with f.
193 e*(*Lk+50) executables 50k or larger beginning with 'e'")
195 (defvar eshell-modifier-help-string
196 "Eshell modifier quick reference:
198 FOR SINGLE ARGUMENTS, or each argument of a list of strings:
199 E evaluate again
200 L lowercase
201 U uppercase
202 C capitalize
203 h dirname
204 t basename
205 e file extension
206 r strip file extension
207 q escape special characters
209 S split string at any whitespace character
210 S/PAT/ split string at each occurrence of PAT
212 FOR LISTS OF ARGUMENTS:
213 o sort alphabetically
214 O reverse sort alphabetically
215 u uniq list (typically used after :o or :O)
216 R reverse list
218 j join list members, separated by a space
219 j/PAT/ join list members, separated by PAT
220 i/PAT/ exclude all members not matching PAT
221 x/PAT/ exclude all members matching PAT
223 s/pat/match/ substitute PAT with MATCH
224 g/pat/match/ substitute PAT with MATCH for all occurrences
226 EXAMPLES:
227 *.c(:o) sorted list of .c files")
229 ;;; Functions:
231 (defun eshell-display-predicate-help ()
232 (interactive)
233 (with-electric-help
234 (function
235 (lambda ()
236 (insert eshell-predicate-help-string)))))
238 (defun eshell-display-modifier-help ()
239 (interactive)
240 (with-electric-help
241 (function
242 (lambda ()
243 (insert eshell-modifier-help-string)))))
245 (defun eshell-pred-initialize ()
246 "Initialize the predicate/modifier code."
247 (add-hook 'eshell-parse-argument-hook
248 'eshell-parse-arg-modifier t t)
249 (define-key eshell-command-map [(meta ?q)] 'eshell-display-predicate-help)
250 (define-key eshell-command-map [(meta ?m)] 'eshell-display-modifier-help))
252 (defun eshell-apply-modifiers (lst predicates modifiers)
253 "Apply to LIST a series of PREDICATES and MODIFIERS."
254 (let (stringified)
255 (if (stringp lst)
256 (setq lst (list lst)
257 stringified t))
258 (when (listp lst)
259 (setq lst (eshell-winnow-list lst nil predicates))
260 (while modifiers
261 (setq lst (funcall (car modifiers) lst)
262 modifiers (cdr modifiers)))
263 (if (and stringified
264 (= (length lst) 1))
265 (car lst)
266 lst))))
268 (defun eshell-parse-arg-modifier ()
269 "Parse a modifier that has been specified after an argument.
270 This function is specially for adding onto `eshell-parse-argument-hook'."
271 (when (eq (char-after) ?\()
272 (forward-char)
273 (let ((end (eshell-find-delimiter ?\( ?\))))
274 (if (not end)
275 (throw 'eshell-incomplete ?\()
276 (when (eshell-arg-delimiter (1+ end))
277 (save-restriction
278 (narrow-to-region (point) end)
279 (let* ((modifiers (eshell-parse-modifiers))
280 (preds (car modifiers))
281 (mods (cdr modifiers)))
282 (if (or preds mods)
283 ;; has to go at the end, which is only natural since
284 ;; syntactically it can only occur at the end
285 (setq eshell-current-modifiers
286 (append
287 eshell-current-modifiers
288 (list
289 `(lambda (lst)
290 (eshell-apply-modifiers
291 lst (quote ,preds) (quote ,mods)))))))))
292 (goto-char (1+ end))
293 (eshell-finish-arg))))))
295 (defun eshell-parse-modifiers ()
296 "Parse value modifiers and predicates at point.
297 If ALLOW-PREDS is non-nil, predicates will be parsed as well.
298 Return a cons cell of the form
300 (PRED-FUNC-LIST . MOD-FUNC-LIST)
302 NEW-STRING is STRING minus any modifiers. PRED-FUNC-LIST is a list of
303 predicate functions. MOD-FUNC-LIST is a list of result modifier
304 functions. PRED-FUNCS take a filename and return t if the test
305 succeeds; MOD-FUNCS take any string and preform a modification,
306 returning the resultant string."
307 (let (result negate follow preds mods)
308 (condition-case err
309 (while (not (eobp))
310 (let ((char (char-after)))
311 (cond
312 ((eq char ?')
313 (forward-char)
314 (if (looking-at "[^|':]")
315 (let ((func (read (current-buffer))))
316 (if (and func (functionp func))
317 (setq preds (eshell-add-pred-func func preds
318 negate follow))
319 (error "Invalid function predicate '%s'"
320 (eshell-stringify func))))
321 (error "Invalid function predicate")))
322 ((eq char ?^)
323 (forward-char)
324 (setq negate (not negate)))
325 ((eq char ?-)
326 (forward-char)
327 (setq follow (not follow)))
328 ((eq char ?|)
329 (forward-char)
330 (if (looking-at "[^|':]")
331 (let ((func (read (current-buffer))))
332 (if (and func (functionp func))
333 (setq mods
334 (cons `(lambda (lst)
335 (mapcar (function ,func) lst))
336 mods))
337 (error "Invalid function modifier '%s'"
338 (eshell-stringify func))))
339 (error "Invalid function modifier")))
340 ((eq char ?:)
341 (forward-char)
342 (let ((mod (assq (char-after) eshell-modifier-alist)))
343 (if (not mod)
344 (error "Unknown modifier character '%c'" (char-after))
345 (forward-char)
346 (setq mods (cons (eval (cdr mod)) mods)))))
348 (let ((pred (assq char eshell-predicate-alist)))
349 (if (not pred)
350 (error "Unknown predicate character '%c'" char)
351 (forward-char)
352 (setq preds
353 (eshell-add-pred-func (eval (cdr pred)) preds
354 negate follow))))))))
355 (end-of-buffer
356 (error "Predicate or modifier ended prematurely")))
357 (cons (nreverse preds) (nreverse mods))))
359 (defun eshell-add-pred-func (pred funcs negate follow)
360 "Add the predicate function PRED to FUNCS."
361 (if negate
362 (setq pred `(lambda (file)
363 (not (funcall ,pred file)))))
364 (if follow
365 (setq pred `(lambda (file)
366 (funcall ,pred (file-truename file)))))
367 (cons pred funcs))
369 (defun eshell-pred-user-or-group (mod-char mod-type attr-index get-id-func)
370 "Return a predicate to test whether a file match a given user/group id."
371 (let (ugid open close end)
372 (if (looking-at "[0-9]+")
373 (progn
374 (setq ugid (string-to-number (match-string 0)))
375 (goto-char (match-end 0)))
376 (setq open (char-after))
377 (if (setq close (memq open '(?\( ?\[ ?\< ?\{)))
378 (setq close (car (last '(?\) ?\] ?\> ?\})
379 (length close))))
380 (setq close open))
381 (forward-char)
382 (setq end (eshell-find-delimiter open close))
383 (unless end
384 (error "Malformed %s name string for modifier `%c'"
385 mod-type mod-char))
386 (setq ugid
387 (funcall get-id-func (buffer-substring (point) end)))
388 (goto-char (1+ end)))
389 (unless ugid
390 (error "Unknown %s name specified for modifier `%c'"
391 mod-type mod-char))
392 `(lambda (file)
393 (let ((attrs (file-attributes file)))
394 (if attrs
395 (= (nth ,attr-index attrs) ,ugid))))))
397 (defun eshell-pred-file-time (mod-char mod-type attr-index)
398 "Return a predicate to test whether a file matches a certain time."
399 (let* ((quantum 86400)
400 qual amount when open close end)
401 (when (memq (char-after) '(?M ?w ?h ?m ?s))
402 (setq quantum (char-after))
403 (cond
404 ((eq quantum ?M)
405 (setq quantum (* 60 60 24 30)))
406 ((eq quantum ?w)
407 (setq quantum (* 60 60 24 7)))
408 ((eq quantum ?h)
409 (setq quantum (* 60 60)))
410 ((eq quantum ?m)
411 (setq quantum 60))
412 ((eq quantum ?s)
413 (setq quantum 1)))
414 (forward-char))
415 (when (memq (char-after) '(?+ ?-))
416 (setq qual (char-after))
417 (forward-char))
418 (if (looking-at "[0-9]+")
419 (progn
420 (setq when (- (float-time)
421 (* (string-to-number (match-string 0))
422 quantum)))
423 (goto-char (match-end 0)))
424 (setq open (char-after))
425 (if (setq close (memq open '(?\( ?\[ ?\< ?\{)))
426 (setq close (car (last '(?\) ?\] ?\> ?\})
427 (length close))))
428 (setq close open))
429 (forward-char)
430 (setq end (eshell-find-delimiter open close))
431 (unless end
432 (error "Malformed %s time modifier `%c'" mod-type mod-char))
433 (let* ((file (buffer-substring (point) end))
434 (attrs (file-attributes file)))
435 (unless attrs
436 (error "Cannot stat file `%s'" file))
437 (setq when (float-time (nth attr-index attrs))))
438 (goto-char (1+ end)))
439 `(lambda (file)
440 (let ((attrs (file-attributes file)))
441 (if attrs
442 (,(if (eq qual ?-)
444 (if (eq qual ?+)
446 '=)) ,when (float-time
447 (nth ,attr-index attrs))))))))
449 (defun eshell-pred-file-type (type)
450 "Return a test which tests that the file is of a certain TYPE.
451 TYPE must be a character, and should be one of the possible options
452 that 'ls -l' will show in the first column of its display. "
453 (when (eq type ?%)
454 (setq type (char-after))
455 (if (memq type '(?b ?c))
456 (forward-char)
457 (setq type ?%)))
458 `(lambda (file)
459 (let ((attrs (eshell-file-attributes (directory-file-name file))))
460 (if attrs
461 (memq (aref (nth 8 attrs) 0)
462 ,(if (eq type ?%)
463 '(?b ?c)
464 (list 'quote (list type))))))))
466 (defsubst eshell-pred-file-mode (mode)
467 "Return a test which tests that MODE pertains to the file."
468 `(lambda (file)
469 (let ((modes (file-modes file)))
470 (if modes
471 (logand ,mode modes)))))
473 (defun eshell-pred-file-links ()
474 "Return a predicate to test whether a file has a given number of links."
475 (let (qual amount)
476 (when (memq (char-after) '(?- ?+))
477 (setq qual (char-after))
478 (forward-char))
479 (unless (looking-at "[0-9]+")
480 (error "Invalid file link count modifier `l'"))
481 (setq amount (string-to-number (match-string 0)))
482 (goto-char (match-end 0))
483 `(lambda (file)
484 (let ((attrs (eshell-file-attributes file)))
485 (if attrs
486 (,(if (eq qual ?-)
488 (if (eq qual ?+)
490 '=)) (nth 1 attrs) ,amount))))))
492 (defun eshell-pred-file-size ()
493 "Return a predicate to test whether a file is of a given size."
494 (let ((quantum 1) qual amount)
495 (when (memq (downcase (char-after)) '(?k ?m ?p))
496 (setq qual (downcase (char-after)))
497 (cond
498 ((eq qual ?k)
499 (setq quantum 1024))
500 ((eq qual ?m)
501 (setq quantum (* 1024 1024)))
502 ((eq qual ?p)
503 (setq quantum 512)))
504 (forward-char))
505 (when (memq (char-after) '(?- ?+))
506 (setq qual (char-after))
507 (forward-char))
508 (unless (looking-at "[0-9]+")
509 (error "Invalid file size modifier `L'"))
510 (setq amount (* (string-to-number (match-string 0)) quantum))
511 (goto-char (match-end 0))
512 `(lambda (file)
513 (let ((attrs (eshell-file-attributes file)))
514 (if attrs
515 (,(if (eq qual ?-)
517 (if (eq qual ?+)
519 '=)) (nth 7 attrs) ,amount))))))
521 (defun eshell-pred-substitute (&optional repeat)
522 "Return a modifier function that will substitute matches."
523 (let ((delim (char-after))
524 match replace end)
525 (forward-char)
526 (setq end (eshell-find-delimiter delim delim nil nil t)
527 match (buffer-substring-no-properties (point) end))
528 (goto-char (1+ end))
529 (setq end (eshell-find-delimiter delim delim nil nil t)
530 replace (buffer-substring-no-properties (point) end))
531 (goto-char (1+ end))
532 (if repeat
533 `(lambda (lst)
534 (mapcar
535 (function
536 (lambda (str)
537 (let ((i 0))
538 (while (setq i (string-match ,match str i))
539 (setq str (replace-match ,replace t nil str))))
540 str)) lst))
541 `(lambda (lst)
542 (mapcar
543 (function
544 (lambda (str)
545 (if (string-match ,match str)
546 (setq str (replace-match ,replace t nil str)))
547 str)) lst)))))
549 (defun eshell-include-members (&optional invert-p)
550 "Include only lisp members matching a regexp."
551 (let ((delim (char-after))
552 regexp end)
553 (forward-char)
554 (setq end (eshell-find-delimiter delim delim nil nil t)
555 regexp (buffer-substring-no-properties (point) end))
556 (goto-char (1+ end))
557 `(lambda (lst)
558 (eshell-winnow-list
559 lst nil '((lambda (elem)
560 ,(if invert-p
561 `(not (string-match ,regexp elem))
562 `(string-match ,regexp elem))))))))
564 (defun eshell-join-members ()
565 "Return a modifier function that join matches."
566 (let ((delim (char-after))
567 str end)
568 (if (not (memq delim '(?' ?/)))
569 (setq delim " ")
570 (forward-char)
571 (setq end (eshell-find-delimiter delim delim nil nil t)
572 str (buffer-substring-no-properties (point) end))
573 (goto-char (1+ end)))
574 `(lambda (lst)
575 (mapconcat 'identity lst ,str))))
577 (defun eshell-split-members ()
578 "Return a modifier function that splits members."
579 (let ((delim (char-after))
580 sep end)
581 (when (memq delim '(?' ?/))
582 (forward-char)
583 (setq end (eshell-find-delimiter delim delim nil nil t)
584 sep (buffer-substring-no-properties (point) end))
585 (goto-char (1+ end)))
586 `(lambda (lst)
587 (mapcar
588 (function
589 (lambda (str)
590 (split-string str ,sep))) lst))))
592 (provide 'em-pred)
594 ;; Local Variables:
595 ;; generated-autoload-file: "esh-groups.el"
596 ;; End:
598 ;;; em-pred.el ends here