1 ;;; em-pred.el --- argument predicates and modifiers (ala zsh)
3 ;; Copyright (C) 1999-2011 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/>.
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,
32 ;; Here are some examples of how to use argument predication. Most of
33 ;; the predicates and modifiers are modeled after those provided by
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
49 (eval-when-compile (require 'eshell))
52 (eshell-defgroup eshell-pred nil
53 "This module allows for predicates to be applied to globbing
54 patterns (similar to zsh), in addition to string modifiers which can
55 be applied either to globbing results, variable references, or just
57 :tag "Value modifiers and predicates"
58 :group 'eshell-module)
62 (defcustom eshell-pred-load-hook '(eshell-pred-initialize)
63 "A list of functions to run when `eshell-pred' is loaded."
67 (defcustom eshell-predicate-alist
68 '((?/ . (eshell-pred-file-type ?d)) ; directories
69 (?. . (eshell-pred-file-type ?-)) ; regular files
70 (?s . (eshell-pred-file-type ?s)) ; sockets
71 (?p . (eshell-pred-file-type ?p)) ; named pipes
72 (?@ . (eshell-pred-file-type ?l)) ; symbolic links
73 (?% . (eshell-pred-file-type ?%)) ; allow user to specify (c def.)
74 (?r . (eshell-pred-file-mode 0400)) ; owner-readable
75 (?w . (eshell-pred-file-mode 0200)) ; owner-writable
76 (?x . (eshell-pred-file-mode 0100)) ; owner-executable
77 (?A . (eshell-pred-file-mode 0040)) ; group-readable
78 (?I . (eshell-pred-file-mode 0020)) ; group-writable
79 (?E . (eshell-pred-file-mode 0010)) ; group-executable
80 (?R . (eshell-pred-file-mode 0004)) ; world-readable
81 (?W . (eshell-pred-file-mode 0002)) ; world-writable
82 (?X . (eshell-pred-file-mode 0001)) ; world-executable
83 (?s . (eshell-pred-file-mode 4000)) ; setuid
84 (?S . (eshell-pred-file-mode 2000)) ; setgid
85 (?t . (eshell-pred-file-mode 1000)) ; sticky bit
86 (?U . '(lambda (file) ; owned by effective uid
87 (if (file-exists-p file)
88 (= (nth 2 (file-attributes file)) (user-uid)))))
89 ;;; (?G . '(lambda (file) ; owned by effective gid
90 ;;; (if (file-exists-p file)
91 ;;; (= (nth 2 (file-attributes file)) (user-uid)))))
93 (and (file-regular-p file)
94 (not (file-symlink-p file))
95 (file-executable-p file))))
96 (?l . (eshell-pred-file-links))
97 (?u . (eshell-pred-user-or-group ?u "user" 2 'eshell-user-id))
98 (?g . (eshell-pred-user-or-group ?g "group" 3 'eshell-group-id))
99 (?a . (eshell-pred-file-time ?a "access" 4))
100 (?m . (eshell-pred-file-time ?m "modification" 5))
101 (?c . (eshell-pred-file-time ?c "change" 6))
102 (?L . (eshell-pred-file-size)))
103 "A list of predicates than can be applied to a globbing pattern.
104 The format of each entry is
106 (CHAR . PREDICATE-FUNC-SEXP)"
107 :type '(repeat (cons character sexp))
110 (put 'eshell-predicate-alist 'risky-local-variable t)
112 (defcustom eshell-modifier-alist
113 '((?E . '(lambda (lst)
118 (car (eshell-parse-argument str))))) lst)))
120 (mapcar 'downcase lst)))
122 (mapcar 'upcase lst)))
124 (mapcar 'capitalize lst)))
126 (mapcar 'file-name-directory lst)))
127 (?i . (eshell-include-members))
128 (?x . (eshell-include-members t))
130 (mapcar 'file-name-sans-extension lst)))
132 (mapcar 'file-name-extension lst)))
134 (mapcar 'file-name-nondirectory lst)))
136 (mapcar 'eshell-escape-arg lst)))
138 (eshell-uniqify-list lst)))
140 (sort lst 'string-lessp)))
142 (nreverse (sort lst 'string-lessp))))
143 (?j . (eshell-join-members))
144 (?S . (eshell-split-members))
148 (if (eq (char-before) ?s)
149 (eshell-pred-substitute t)
150 (error "`g' modifier cannot be used alone"))))
151 (?s . (eshell-pred-substitute)))
152 "A list of modifiers than can be applied to an argument expansion.
153 The format of each entry is
155 (CHAR ENTRYWISE-P MODIFIER-FUNC-SEXP)"
156 :type '(repeat (cons character sexp))
159 (put 'eshell-modifier-alist 'risky-local-variable t)
161 (defvar eshell-predicate-help-string
162 "Eshell predicate quick reference:
164 - follow symbolic references for predicates after the `-'
165 ^ invert sense of predicates after the `^'
168 / directories s sockets
169 . regular files p named pipes
170 * executable (files only) @ symbolic links
172 %x file type == `x' (as by ls -l; so `c' = char device, etc.)
174 PERMISSION BITS (for owner/group/world):
175 r/A/R readable s setuid
176 w/I/W writable S setgid
177 x/E/X executable t sticky bit
180 U owned by effective uid
181 u(UID|'user') owned by UID/user
182 g(GID|'group') owned by GID/group
186 a[Mwhms][+-](N|'FILE') access time +/-/= N mnths/weeks/hours/mins/secs
187 (days if unspecified) if FILE specified,
188 use as comparison basis; so a+'file.c'
189 shows files accessed before file.c was
191 m[Mwhms][+-](N|'FILE') modification time...
192 c[Mwhms][+-](N|'FILE') change time...
193 L[kmp][+-]N file size +/-/= N Kb/Mb/blocks
196 *(^@) all non-dot files which are not symlinks
197 .#*(^@) all files which are not symbolic links
198 **/.#*(*) all executable files, searched recursively
199 ***/*~f*(-/) recursively (though not traversing symlinks),
200 find all directories (or symlinks referring to
201 directories) whose names do not begin with f.
202 e*(*Lk+50) executables 50k or larger beginning with 'e'")
204 (defvar eshell-modifier-help-string
205 "Eshell modifier quick reference:
207 FOR SINGLE ARGUMENTS, or each argument of a list of strings:
215 r strip file extension
216 q escape special characters
218 S split string at any whitespace character
219 S/PAT/ split string at each occurrence of PAT
221 FOR LISTS OF ARGUMENTS:
222 o sort alphabetically
223 O reverse sort alphabetically
224 u uniq list (typically used after :o or :O)
227 j join list members, separated by a space
228 j/PAT/ join list members, separated by PAT
229 i/PAT/ exclude all members not matching PAT
230 x/PAT/ exclude all members matching PAT
232 s/pat/match/ substitute PAT with MATCH
233 g/pat/match/ substitute PAT with MATCH for all occurrences
236 *.c(:o) sorted list of .c files")
240 (defun eshell-display-predicate-help ()
245 (insert eshell-predicate-help-string)))))
247 (defun eshell-display-modifier-help ()
252 (insert eshell-modifier-help-string)))))
254 (defun eshell-pred-initialize ()
255 "Initialize the predicate/modifier code."
256 (add-hook 'eshell-parse-argument-hook
257 'eshell-parse-arg-modifier t t)
258 (define-key eshell-command-map [(meta ?q)] 'eshell-display-predicate-help)
259 (define-key eshell-command-map [(meta ?m)] 'eshell-display-modifier-help))
261 (defun eshell-apply-modifiers (lst predicates modifiers)
262 "Apply to LIST a series of PREDICATES and MODIFIERS."
268 (setq lst (eshell-winnow-list lst nil predicates))
270 (setq lst (funcall (car modifiers) lst)
271 modifiers (cdr modifiers)))
277 (defun eshell-parse-arg-modifier ()
278 "Parse a modifier that has been specified after an argument.
279 This function is specially for adding onto `eshell-parse-argument-hook'."
280 (when (eq (char-after) ?\()
282 (let ((end (eshell-find-delimiter ?\( ?\))))
284 (throw 'eshell-incomplete ?\()
285 (when (eshell-arg-delimiter (1+ end))
287 (narrow-to-region (point) end)
288 (let* ((modifiers (eshell-parse-modifiers))
289 (preds (car modifiers))
290 (mods (cdr modifiers)))
292 ;; has to go at the end, which is only natural since
293 ;; syntactically it can only occur at the end
294 (setq eshell-current-modifiers
296 eshell-current-modifiers
299 (eshell-apply-modifiers
300 lst (quote ,preds) (quote ,mods)))))))))
302 (eshell-finish-arg))))))
304 (defun eshell-parse-modifiers ()
305 "Parse value modifiers and predicates at point.
306 If ALLOW-PREDS is non-nil, predicates will be parsed as well.
307 Return a cons cell of the form
309 (PRED-FUNC-LIST . MOD-FUNC-LIST)
311 NEW-STRING is STRING minus any modifiers. PRED-FUNC-LIST is a list of
312 predicate functions. MOD-FUNC-LIST is a list of result modifier
313 functions. PRED-FUNCS take a filename and return t if the test
314 succeeds; MOD-FUNCS take any string and preform a modification,
315 returning the resultant string."
316 (let (result negate follow preds mods)
319 (let ((char (char-after)))
323 (if (looking-at "[^|':]")
324 (let ((func (read (current-buffer))))
325 (if (and func (functionp func))
326 (setq preds (eshell-add-pred-func func preds
328 (error "Invalid function predicate '%s'"
329 (eshell-stringify func))))
330 (error "Invalid function predicate")))
333 (setq negate (not negate)))
336 (setq follow (not follow)))
339 (if (looking-at "[^|':]")
340 (let ((func (read (current-buffer))))
341 (if (and func (functionp func))
344 (mapcar (function ,func) lst))
346 (error "Invalid function modifier '%s'"
347 (eshell-stringify func))))
348 (error "Invalid function modifier")))
351 (let ((mod (assq (char-after) eshell-modifier-alist)))
353 (error "Unknown modifier character '%c'" (char-after))
355 (setq mods (cons (eval (cdr mod)) mods)))))
357 (let ((pred (assq char eshell-predicate-alist)))
359 (error "Unknown predicate character '%c'" char)
362 (eshell-add-pred-func (eval (cdr pred)) preds
363 negate follow))))))))
365 (error "Predicate or modifier ended prematurely")))
366 (cons (nreverse preds) (nreverse mods))))
368 (defun eshell-add-pred-func (pred funcs negate follow)
369 "Add the predicate function PRED to FUNCS."
371 (setq pred `(lambda (file)
372 (not (funcall ,pred file)))))
374 (setq pred `(lambda (file)
375 (funcall ,pred (file-truename file)))))
378 (defun eshell-pred-user-or-group (mod-char mod-type attr-index get-id-func)
379 "Return a predicate to test whether a file match a given user/group id."
380 (let (ugid open close end)
381 (if (looking-at "[0-9]+")
383 (setq ugid (string-to-number (match-string 0)))
384 (goto-char (match-end 0)))
385 (setq open (char-after))
386 (if (setq close (memq open '(?\( ?\[ ?\< ?\{)))
387 (setq close (car (last '(?\) ?\] ?\> ?\})
391 (setq end (eshell-find-delimiter open close))
393 (error "Malformed %s name string for modifier `%c'"
396 (funcall get-id-func (buffer-substring (point) end)))
397 (goto-char (1+ end)))
399 (error "Unknown %s name specified for modifier `%c'"
402 (let ((attrs (file-attributes file)))
404 (= (nth ,attr-index attrs) ,ugid))))))
406 (defun eshell-pred-file-time (mod-char mod-type attr-index)
407 "Return a predicate to test whether a file matches a certain time."
408 (let* ((quantum 86400)
409 qual amount when open close end)
410 (when (memq (char-after) '(?M ?w ?h ?m ?s))
411 (setq quantum (char-after))
414 (setq quantum (* 60 60 24 30)))
416 (setq quantum (* 60 60 24 7)))
418 (setq quantum (* 60 60)))
424 (when (memq (char-after) '(?+ ?-))
425 (setq qual (char-after))
427 (if (looking-at "[0-9]+")
429 (setq when (- (float-time)
430 (* (string-to-number (match-string 0))
432 (goto-char (match-end 0)))
433 (setq open (char-after))
434 (if (setq close (memq open '(?\( ?\[ ?\< ?\{)))
435 (setq close (car (last '(?\) ?\] ?\> ?\})
439 (setq end (eshell-find-delimiter open close))
441 (error "Malformed %s time modifier `%c'" mod-type mod-char))
442 (let* ((file (buffer-substring (point) end))
443 (attrs (file-attributes file)))
445 (error "Cannot stat file `%s'" file))
446 (setq when (float-time (nth attr-index attrs))))
447 (goto-char (1+ end)))
449 (let ((attrs (file-attributes file)))
455 '=)) ,when (float-time
456 (nth ,attr-index attrs))))))))
458 (defun eshell-pred-file-type (type)
459 "Return a test which tests that the file is of a certain TYPE.
460 TYPE must be a character, and should be one of the possible options
461 that 'ls -l' will show in the first column of its display. "
463 (setq type (char-after))
464 (if (memq type '(?b ?c))
468 (let ((attrs (eshell-file-attributes (directory-file-name file))))
470 (memq (aref (nth 8 attrs) 0)
473 (list 'quote (list type))))))))
475 (defsubst eshell-pred-file-mode (mode)
476 "Return a test which tests that MODE pertains to the file."
478 (let ((modes (file-modes file)))
480 (logand ,mode modes)))))
482 (defun eshell-pred-file-links ()
483 "Return a predicate to test whether a file has a given number of links."
485 (when (memq (char-after) '(?- ?+))
486 (setq qual (char-after))
488 (unless (looking-at "[0-9]+")
489 (error "Invalid file link count modifier `l'"))
490 (setq amount (string-to-number (match-string 0)))
491 (goto-char (match-end 0))
493 (let ((attrs (eshell-file-attributes file)))
499 '=)) (nth 1 attrs) ,amount))))))
501 (defun eshell-pred-file-size ()
502 "Return a predicate to test whether a file is of a given size."
503 (let ((quantum 1) qual amount)
504 (when (memq (downcase (char-after)) '(?k ?m ?p))
505 (setq qual (downcase (char-after)))
510 (setq quantum (* 1024 1024)))
514 (when (memq (char-after) '(?- ?+))
515 (setq qual (char-after))
517 (unless (looking-at "[0-9]+")
518 (error "Invalid file size modifier `L'"))
519 (setq amount (* (string-to-number (match-string 0)) quantum))
520 (goto-char (match-end 0))
522 (let ((attrs (eshell-file-attributes file)))
528 '=)) (nth 7 attrs) ,amount))))))
530 (defun eshell-pred-substitute (&optional repeat)
531 "Return a modifier function that will substitute matches."
532 (let ((delim (char-after))
535 (setq end (eshell-find-delimiter delim delim nil nil t)
536 match (buffer-substring-no-properties (point) end))
538 (setq end (eshell-find-delimiter delim delim nil nil t)
539 replace (buffer-substring-no-properties (point) end))
547 (while (setq i (string-match ,match str i))
548 (setq str (replace-match ,replace t nil str))))
554 (if (string-match ,match str)
555 (setq str (replace-match ,replace t nil str)))
558 (defun eshell-include-members (&optional invert-p)
559 "Include only lisp members matching a regexp."
560 (let ((delim (char-after))
563 (setq end (eshell-find-delimiter delim delim nil nil t)
564 regexp (buffer-substring-no-properties (point) end))
568 lst nil '((lambda (elem)
570 `(not (string-match ,regexp elem))
571 `(string-match ,regexp elem))))))))
573 (defun eshell-join-members ()
574 "Return a modifier function that join matches."
575 (let ((delim (char-after))
577 (if (not (memq delim '(?' ?/)))
580 (setq end (eshell-find-delimiter delim delim nil nil t)
581 str (buffer-substring-no-properties (point) end))
582 (goto-char (1+ end)))
584 (mapconcat 'identity lst ,str))))
586 (defun eshell-split-members ()
587 "Return a modifier function that splits members."
588 (let ((delim (char-after))
590 (when (memq delim '(?' ?/))
592 (setq end (eshell-find-delimiter delim delim nil nil t)
593 sep (buffer-substring-no-properties (point) end))
594 (goto-char (1+ end)))
599 (split-string str ,sep))) lst))))
604 ;; generated-autoload-file: "esh-groups.el"
607 ;;; em-pred.el ends here