1 ;;; em-pred --- argument predicates and modifiers (ala zsh)
3 ;; Copyright (C) 1999, 2000 Free Software Foundation
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 2, or (at your option)
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; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
26 (eval-when-compile (require 'esh-maint))
28 (defgroup eshell-pred nil
29 "This module allows for predicates to be applied to globbing
30 patterns (similar to zsh), in addition to string modifiers which can
31 be applied either to globbing results, variable references, or just
33 :tag "Value modifiers and predicates"
34 :group 'eshell-module)
38 ;; Argument predication is used to affect which members of a list are
39 ;; selected for use as argument. This is most useful with globbing,
40 ;; but can be used on any list argument, to select certain members.
42 ;; Argument modifiers are used to manipulate argument values. For
43 ;; example, sorting lists, upcasing words, substituting characters,
46 ;; Here are some examples of how to use argument predication. Most of
47 ;; the predicates and modifiers are modeled after those provided by
50 ;; ls -ld *(/) ; list all directories
51 ;; ls -l *(@u'johnw') ; list all symlinks owned by 'johnw'
52 ;; bzip2 -9v **/*(a+30) ; compress everything which hasn't been
53 ;; accessed in 30 days
54 ;; echo *.c(:o:R) ; a reversed, sorted list of C files
55 ;; *(^@:U^u0) ; all non-symlinks not owned by 'root', upcased
56 ;; chmod u-x *(U*) : remove exec bit on all executables owned by user
58 ;; See the zsh docs for more on the syntax ([(zsh.info)Filename
63 (defcustom eshell-pred-load-hook '(eshell-pred-initialize)
64 "*A list of functions to run when `eshell-pred' is loaded."
68 (defcustom eshell-predicate-alist
69 '((?/ . (eshell-pred-file-type ?d)) ; directories
70 (?. . (eshell-pred-file-type ?-)) ; regular files
71 (?s . (eshell-pred-file-type ?s)) ; sockets
72 (?p . (eshell-pred-file-type ?p)) ; named pipes
73 (?@ . (eshell-pred-file-type ?l)) ; symbolic links
74 (?% . (eshell-pred-file-type ?%)) ; allow user to specify (c def.)
75 (?r . (eshell-pred-file-mode 0400)) ; owner-readable
76 (?w . (eshell-pred-file-mode 0200)) ; owner-writable
77 (?x . (eshell-pred-file-mode 0100)) ; owner-executable
78 (?A . (eshell-pred-file-mode 0040)) ; group-readable
79 (?I . (eshell-pred-file-mode 0020)) ; group-writable
80 (?E . (eshell-pred-file-mode 0010)) ; group-executable
81 (?R . (eshell-pred-file-mode 0004)) ; world-readable
82 (?W . (eshell-pred-file-mode 0002)) ; world-writable
83 (?X . (eshell-pred-file-mode 0001)) ; world-executable
84 (?s . (eshell-pred-file-mode 4000)) ; setuid
85 (?S . (eshell-pred-file-mode 2000)) ; setgid
86 (?t . (eshell-pred-file-mode 1000)) ; sticky bit
87 (?U . '(lambda (file) ; owned by effective uid
88 (if (file-exists-p file)
89 (= (nth 2 (file-attributes file)) (user-uid)))))
90 ;;; (?G . '(lambda (file) ; owned by effective gid
91 ;;; (if (file-exists-p file)
92 ;;; (= (nth 2 (file-attributes file)) (user-uid)))))
94 (and (file-regular-p file)
95 (not (file-symlink-p file))
96 (file-executable-p file))))
97 (?l . (eshell-pred-file-links))
98 (?u . (eshell-pred-user-or-group ?u "user" 2 'eshell-user-id))
99 (?g . (eshell-pred-user-or-group ?g "group" 3 'eshell-group-id))
100 (?a . (eshell-pred-file-time ?a "access" 4))
101 (?m . (eshell-pred-file-time ?m "modification" 5))
102 (?c . (eshell-pred-file-time ?c "change" 6))
103 (?L . (eshell-pred-file-size)))
104 "*A list of predicates than can be applied to a globbing pattern.
105 The format of each entry is
107 (CHAR . PREDICATE-FUNC-SEXP)"
108 :type '(repeat (cons character sexp))
111 (put 'eshell-predicate-alist 'risky-local-variable t)
113 (defcustom eshell-modifier-alist
114 '((?e . '(lambda (lst)
119 (car (eshell-parse-argument str))))) lst)))
121 (mapcar 'downcase lst)))
123 (mapcar 'upcase lst)))
125 (mapcar 'capitalize lst)))
127 (mapcar 'file-name-directory lst)))
128 (?i . (eshell-include-members))
129 (?x . (eshell-include-members t))
131 (mapcar 'file-name-sans-extension lst)))
133 (mapcar 'file-name-extension lst)))
135 (mapcar 'file-name-nondirectory lst)))
137 (mapcar 'eshell-escape-arg lst)))
139 (eshell-uniqify-list lst)))
141 (sort lst 'string-lessp)))
143 (nreverse (sort lst 'string-lessp))))
144 (?j . (eshell-join-members))
145 (?S . (eshell-split-members))
149 (if (eq (char-before) ?s)
150 (eshell-pred-substitute t)
151 (error "`g' modifier cannot be used alone"))))
152 (?s . (eshell-pred-substitute)))
153 "*A list of modifiers than can be applied to an argument expansion.
154 The format of each entry is
156 (CHAR ENTRYWISE-P MODIFIER-FUNC-SEXP)"
157 :type '(repeat (cons character sexp))
160 (put 'eshell-modifier-alist 'risky-local-variable t)
162 (defvar eshell-predicate-help-string
163 "Eshell predicate quick reference:
165 - follow symbolic references for predicates after the `-'
166 ^ invert sense of predicates after the `^'
169 / directories s sockets
170 . regular files p named pipes
171 * executable (files only) @ symbolic links
173 %x file type == `x' (as by ls -l; so `c' = char device, etc.)
175 PERMISSION BITS (for owner/group/world):
176 r/A/R readable s setuid
177 w/I/W writable S setgid
178 x/E/X executable t sticky bit
181 U owned by effective uid
182 u(UID|'user') owned by UID/user
183 g(GID|'group') owned by GID/group
187 a[Mwhm][+-](N|'FILE') access time +/-/= N mnths/weeks/days/mins
188 if FILE specified, use as comparison basis;
189 so a+'file.c' shows files accessed before
190 file.c was last accessed
191 m[Mwhm][+-](N|'FILE') modification time...
192 c[Mwhm][+-](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 occurance 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 occurances
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 (make-local-hook 'eshell-parse-argument-hook)
257 (add-hook 'eshell-parse-argument-hook
258 'eshell-parse-arg-modifier t t)
259 (define-key eshell-command-map [(meta ?q)] 'eshell-display-predicate-help)
260 (define-key eshell-command-map [(meta ?m)] 'eshell-display-modifier-help))
262 (defun eshell-apply-modifiers (lst predicates modifiers)
263 "Apply to LIST a series of PREDICATES and MODIFIERS."
269 (setq lst (eshell-winnow-list lst nil predicates))
271 (setq lst (funcall (car modifiers) lst)
272 modifiers (cdr modifiers)))
278 (defun eshell-parse-arg-modifier ()
279 "Parse a modifier that has been specified after an argument.
280 This function is specially for adding onto `eshell-parse-argument-hook'."
281 (when (eq (char-after) ?\()
283 (let ((end (eshell-find-delimiter ?\( ?\))))
285 (throw 'eshell-incomplete ?\()
286 (when (eshell-arg-delimiter (1+ end))
288 (narrow-to-region (point) end)
289 (let* ((modifiers (eshell-parse-modifiers))
290 (preds (car modifiers))
291 (mods (cdr modifiers)))
293 ;; has to go at the end, which is only natural since
294 ;; syntactically it can only occur at the end
295 (setq eshell-current-modifiers
297 eshell-current-modifiers
300 (eshell-apply-modifiers
301 lst (quote ,preds) (quote ,mods)))))))))
303 (eshell-finish-arg))))))
305 (defun eshell-parse-modifiers ()
306 "Parse value modifiers and predicates at point.
307 If ALLOW-PREDS is non-nil, predicates will be parsed as well.
308 Return a cons cell of the form
310 (PRED-FUNC-LIST . MOD-FUNC-LIST)
312 NEW-STRING is STRING minus any modifiers. PRED-FUNC-LIST is a list of
313 predicate functions. MOD-FUNC-LIST is a list of result modifier
314 functions. PRED-FUNCS take a filename and return t if the test
315 succeeds; MOD-FUNCS take any string and preform a modification,
316 returning the resultant string."
317 (let (result negate follow preds mods)
320 (let ((char (char-after)))
324 (if (looking-at "[^|':]")
325 (let ((func (read (current-buffer))))
326 (if (and func (functionp func))
327 (setq preds (eshell-add-pred-func func preds
329 (error "Invalid function predicate '%s'"
330 (eshell-stringify func))))
331 (error "Invalid function predicate")))
334 (setq negate (not negate)))
337 (setq follow (not follow)))
340 (if (looking-at "[^|':]")
341 (let ((func (read (current-buffer))))
342 (if (and func (functionp func))
345 (mapcar (function ,func) lst))
347 (error "Invalid function modifier '%s'"
348 (eshell-stringify func))))
349 (error "Invalid function modifier")))
352 (let ((mod (assq (char-after) eshell-modifier-alist)))
354 (error "Unknown modifier character '%c'" (char-after))
356 (setq mods (cons (eval (cdr mod)) mods)))))
358 (let ((pred (assq char eshell-predicate-alist)))
360 (error "Unknown predicate character '%c'" char)
363 (eshell-add-pred-func (eval (cdr pred)) preds
364 negate follow))))))))
366 (error "Predicate or modifier ended prematurely")))
367 (cons (nreverse preds) (nreverse mods))))
369 (defun eshell-add-pred-func (pred funcs negate follow)
370 "Add the predicate function PRED to FUNCS."
372 (setq pred `(lambda (file)
373 (not (funcall ,pred file)))))
375 (setq pred `(lambda (file)
376 (funcall ,pred (file-truename file)))))
379 (defun eshell-pred-user-or-group (mod-char mod-type attr-index get-id-func)
380 "Return a predicate to test whether a file match a given user/group id."
381 (let (ugid open close end)
382 (if (looking-at "[0-9]+")
384 (setq ugid (string-to-number (match-string 0)))
385 (goto-char (match-end 0)))
386 (setq open (char-after))
387 (if (setq close (memq open '(?\( ?\[ ?\< ?\{)))
388 (setq close (car (last '(?\) ?\] ?\> ?\})
392 (setq end (eshell-find-delimiter open close))
394 (error "Malformed %s name string for modifier `%c'"
397 (funcall get-id-func (buffer-substring (point) end)))
398 (goto-char (1+ end)))
400 (error "Unknown %s name specified for modifier `%c'"
403 (let ((attrs (file-attributes file)))
405 (= (nth ,attr-index attrs) ,ugid))))))
407 (defun eshell-pred-file-time (mod-char mod-type attr-index)
408 "Return a predicate to test whether a file matches a certain time."
409 (let* ((quantum 86400)
410 qual amount when open close end)
411 (when (memq (char-after) '(?M ?w ?h ?m))
412 (setq quantum (char-after))
415 (setq quantum (* 60 60 24 30)))
417 (setq quantum (* 60 60 24 7)))
419 (setq quantum (* 60 60)))
425 (when (memq (char-after) '(?+ ?-))
426 (setq qual (char-after))
428 (if (looking-at "[0-9]+")
430 (setq when (- (eshell-time-to-seconds (current-time))
431 (* (string-to-number (match-string 0))
433 (goto-char (match-end 0)))
434 (setq open (char-after))
435 (if (setq close (memq open '(?\( ?\[ ?\< ?\{)))
436 (setq close (car (last '(?\) ?\] ?\> ?\})
440 (setq end (eshell-find-delimiter open close))
442 (error "Malformed %s time modifier `%c'" mod-type mod-char))
443 (let* ((file (buffer-substring (point) end))
444 (attrs (file-attributes file)))
446 (error "Cannot stat file `%s'" file))
447 (setq when (eshell-time-to-seconds (nth attr-index attrs))))
448 (goto-char (1+ end)))
450 (let ((attrs (file-attributes file)))
456 '=)) ,when (eshell-time-to-seconds
457 (nth ,attr-index attrs))))))))
459 (defun eshell-pred-file-type (type)
460 "Return a test which tests that the file is of a certain TYPE.
461 TYPE must be a character, and should be one of the possible options
462 that 'ls -l' will show in the first column of its display. "
464 (setq type (char-after))
465 (if (memq type '(?b ?c))
469 (let ((attrs (eshell-file-attributes (directory-file-name file))))
471 (memq (aref (nth 8 attrs) 0)
474 (list 'quote (list type))))))))
476 (defsubst eshell-pred-file-mode (mode)
477 "Return a test which tests that MODE pertains to the file."
479 (let ((modes (file-modes file)))
481 (logand ,mode modes)))))
483 (defun eshell-pred-file-links ()
484 "Return a predicate to test whether a file has a given number of links."
486 (when (memq (char-after) '(?- ?+))
487 (setq qual (char-after))
489 (unless (looking-at "[0-9]+")
490 (error "Invalid file link count modifier `l'"))
491 (setq amount (string-to-number (match-string 0)))
492 (goto-char (match-end 0))
494 (let ((attrs (eshell-file-attributes file)))
500 '=)) (nth 1 attrs) ,amount))))))
502 (defun eshell-pred-file-size ()
503 "Return a predicate to test whether a file is of a given size."
504 (let ((quantum 1) qual amount)
505 (when (memq (downcase (char-after)) '(?k ?m ?p))
506 (setq qual (downcase (char-after)))
511 (setq quantum (* 1024 1024)))
515 (when (memq (char-after) '(?- ?+))
516 (setq qual (char-after))
518 (unless (looking-at "[0-9]+")
519 (error "Invalid file size modifier `L'"))
520 (setq amount (* (string-to-number (match-string 0)) quantum))
521 (goto-char (match-end 0))
523 (let ((attrs (eshell-file-attributes file)))
529 '=)) (nth 7 attrs) ,amount))))))
531 (defun eshell-pred-substitute (&optional repeat)
532 "Return a modifier function that will substitute matches."
533 (let ((delim (char-after))
536 (setq end (eshell-find-delimiter delim delim nil nil t)
537 match (buffer-substring-no-properties (point) end))
539 (setq end (eshell-find-delimiter delim delim nil nil t)
540 replace (buffer-substring-no-properties (point) end))
548 (while (setq i (string-match ,match str i))
549 (setq str (replace-match ,replace t nil str))))
555 (if (string-match ,match str)
556 (setq str (replace-match ,replace t nil str)))
559 (defun eshell-include-members (&optional invert-p)
560 "Include only lisp members matching a regexp."
561 (let ((delim (char-after))
564 (setq end (eshell-find-delimiter delim delim nil nil t)
565 regexp (buffer-substring-no-properties (point) end))
569 lst nil '((lambda (elem)
571 `(not (string-match ,regexp elem))
572 `(string-match ,regexp elem))))))))
574 (defun eshell-join-members ()
575 "Return a modifier function that join matches."
576 (let ((delim (char-after))
578 (if (not (memq delim '(?' ?/)))
581 (setq end (eshell-find-delimiter delim delim nil nil t)
582 str (buffer-substring-no-properties (point) end))
583 (goto-char (1+ end)))
585 (mapconcat 'identity lst ,str))))
587 (defun eshell-split-members ()
588 "Return a modifier function that splits members."
589 (let ((delim (char-after))
591 (when (memq delim '(?' ?/))
593 (setq end (eshell-find-delimiter delim delim nil nil t)
594 sep (buffer-substring-no-properties (point) end))
595 (goto-char (1+ end)))
600 (split-string str ,sep))) lst))))
604 ;;; em-pred.el ends here