1 ;;; em-pred.el --- 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
65 (defcustom eshell-pred-load-hook
'(eshell-pred-initialize)
66 "*A list of functions to run when `eshell-pred' is loaded."
70 (defcustom eshell-predicate-alist
71 '((?
/ .
(eshell-pred-file-type ?d
)) ; directories
72 (?. .
(eshell-pred-file-type ?-
)) ; regular files
73 (?s .
(eshell-pred-file-type ?s
)) ; sockets
74 (?p .
(eshell-pred-file-type ?p
)) ; named pipes
75 (?
@ .
(eshell-pred-file-type ?l
)) ; symbolic links
76 (?% .
(eshell-pred-file-type ?%
)) ; allow user to specify (c def.)
77 (?r .
(eshell-pred-file-mode 0400)) ; owner-readable
78 (?w .
(eshell-pred-file-mode 0200)) ; owner-writable
79 (?x .
(eshell-pred-file-mode 0100)) ; owner-executable
80 (?A .
(eshell-pred-file-mode 0040)) ; group-readable
81 (?I .
(eshell-pred-file-mode 0020)) ; group-writable
82 (?E .
(eshell-pred-file-mode 0010)) ; group-executable
83 (?R .
(eshell-pred-file-mode 0004)) ; world-readable
84 (?W .
(eshell-pred-file-mode 0002)) ; world-writable
85 (?X .
(eshell-pred-file-mode 0001)) ; world-executable
86 (?s .
(eshell-pred-file-mode 4000)) ; setuid
87 (?S .
(eshell-pred-file-mode 2000)) ; setgid
88 (?t .
(eshell-pred-file-mode 1000)) ; sticky bit
89 (?U .
'(lambda (file) ; owned by effective uid
90 (if (file-exists-p file
)
91 (= (nth 2 (file-attributes file
)) (user-uid)))))
92 ;;; (?G . '(lambda (file) ; owned by effective gid
93 ;;; (if (file-exists-p file)
94 ;;; (= (nth 2 (file-attributes file)) (user-uid)))))
96 (and (file-regular-p file
)
97 (not (file-symlink-p file
))
98 (file-executable-p file
))))
99 (?l .
(eshell-pred-file-links))
100 (?u .
(eshell-pred-user-or-group ?u
"user" 2 'eshell-user-id
))
101 (?g .
(eshell-pred-user-or-group ?g
"group" 3 'eshell-group-id
))
102 (?a .
(eshell-pred-file-time ?a
"access" 4))
103 (?m .
(eshell-pred-file-time ?m
"modification" 5))
104 (?c .
(eshell-pred-file-time ?c
"change" 6))
105 (?L .
(eshell-pred-file-size)))
106 "*A list of predicates than can be applied to a globbing pattern.
107 The format of each entry is
109 (CHAR . PREDICATE-FUNC-SEXP)"
110 :type
'(repeat (cons character sexp
))
113 (put 'eshell-predicate-alist
'risky-local-variable t
)
115 (defcustom eshell-modifier-alist
116 '((?e .
'(lambda (lst)
121 (car (eshell-parse-argument str
))))) lst
)))
123 (mapcar 'downcase lst
)))
125 (mapcar 'upcase lst
)))
127 (mapcar 'capitalize lst
)))
129 (mapcar 'file-name-directory lst
)))
130 (?i .
(eshell-include-members))
131 (?x .
(eshell-include-members t
))
133 (mapcar 'file-name-sans-extension lst
)))
135 (mapcar 'file-name-extension lst
)))
137 (mapcar 'file-name-nondirectory lst
)))
139 (mapcar 'eshell-escape-arg lst
)))
141 (eshell-uniqify-list lst
)))
143 (sort lst
'string-lessp
)))
145 (nreverse (sort lst
'string-lessp
))))
146 (?j .
(eshell-join-members))
147 (?S .
(eshell-split-members))
151 (if (eq (char-before) ?s
)
152 (eshell-pred-substitute t
)
153 (error "`g' modifier cannot be used alone"))))
154 (?s .
(eshell-pred-substitute)))
155 "*A list of modifiers than can be applied to an argument expansion.
156 The format of each entry is
158 (CHAR ENTRYWISE-P MODIFIER-FUNC-SEXP)"
159 :type
'(repeat (cons character sexp
))
162 (put 'eshell-modifier-alist
'risky-local-variable t
)
164 (defvar eshell-predicate-help-string
165 "Eshell predicate quick reference:
167 - follow symbolic references for predicates after the `-'
168 ^ invert sense of predicates after the `^'
171 / directories s sockets
172 . regular files p named pipes
173 * executable (files only) @ symbolic links
175 %x file type == `x' (as by ls -l; so `c' = char device, etc.)
177 PERMISSION BITS (for owner/group/world):
178 r/A/R readable s setuid
179 w/I/W writable S setgid
180 x/E/X executable t sticky bit
183 U owned by effective uid
184 u(UID|'user') owned by UID/user
185 g(GID|'group') owned by GID/group
189 a[Mwhm][+-](N|'FILE') access time +/-/= N mnths/weeks/days/mins
190 if FILE specified, use as comparison basis;
191 so a+'file.c' shows files accessed before
192 file.c was last accessed
193 m[Mwhm][+-](N|'FILE') modification time...
194 c[Mwhm][+-](N|'FILE') change time...
195 L[kmp][+-]N file size +/-/= N Kb/Mb/blocks
198 *(^@) all non-dot files which are not symlinks
199 .#*(^@) all files which are not symbolic links
200 **/.#*(*) all executable files, searched recursively
201 ***/*~f*(-/) recursively (though not traversing symlinks),
202 find all directories (or symlinks referring to
203 directories) whose names do not begin with f.
204 e*(*Lk+50) executables 50k or larger beginning with 'e'")
206 (defvar eshell-modifier-help-string
207 "Eshell modifier quick reference:
209 FOR SINGLE ARGUMENTS, or each argument of a list of strings:
217 r strip file extension
218 q escape special characters
220 S split string at any whitespace character
221 S/PAT/ split string at each occurance of PAT
223 FOR LISTS OF ARGUMENTS:
224 o sort alphabetically
225 O reverse sort alphabetically
226 u uniq list (typically used after :o or :O)
229 j join list members, separated by a space
230 j/PAT/ join list members, separated by PAT
231 i/PAT/ exclude all members not matching PAT
232 x/PAT/ exclude all members matching PAT
234 s/pat/match/ substitute PAT with MATCH
235 g/pat/match/ substitute PAT with MATCH for all occurances
238 *.c(:o) sorted list of .c files")
242 (defun eshell-display-predicate-help ()
247 (insert eshell-predicate-help-string
)))))
249 (defun eshell-display-modifier-help ()
254 (insert eshell-modifier-help-string
)))))
256 (defun eshell-pred-initialize ()
257 "Initialize the predicate/modifier code."
258 (add-hook 'eshell-parse-argument-hook
259 'eshell-parse-arg-modifier t t
)
260 (define-key eshell-command-map
[(meta ?q
)] 'eshell-display-predicate-help
)
261 (define-key eshell-command-map
[(meta ?m
)] 'eshell-display-modifier-help
))
263 (defun eshell-apply-modifiers (lst predicates modifiers
)
264 "Apply to LIST a series of PREDICATES and MODIFIERS."
270 (setq lst
(eshell-winnow-list lst nil predicates
))
272 (setq lst
(funcall (car modifiers
) lst
)
273 modifiers
(cdr modifiers
)))
279 (defun eshell-parse-arg-modifier ()
280 "Parse a modifier that has been specified after an argument.
281 This function is specially for adding onto `eshell-parse-argument-hook'."
282 (when (eq (char-after) ?\
()
284 (let ((end (eshell-find-delimiter ?\
( ?\
))))
286 (throw 'eshell-incomplete ?\
()
287 (when (eshell-arg-delimiter (1+ end
))
289 (narrow-to-region (point) end
)
290 (let* ((modifiers (eshell-parse-modifiers))
291 (preds (car modifiers
))
292 (mods (cdr modifiers
)))
294 ;; has to go at the end, which is only natural since
295 ;; syntactically it can only occur at the end
296 (setq eshell-current-modifiers
298 eshell-current-modifiers
301 (eshell-apply-modifiers
302 lst
(quote ,preds
) (quote ,mods
)))))))))
304 (eshell-finish-arg))))))
306 (defun eshell-parse-modifiers ()
307 "Parse value modifiers and predicates at point.
308 If ALLOW-PREDS is non-nil, predicates will be parsed as well.
309 Return a cons cell of the form
311 (PRED-FUNC-LIST . MOD-FUNC-LIST)
313 NEW-STRING is STRING minus any modifiers. PRED-FUNC-LIST is a list of
314 predicate functions. MOD-FUNC-LIST is a list of result modifier
315 functions. PRED-FUNCS take a filename and return t if the test
316 succeeds; MOD-FUNCS take any string and preform a modification,
317 returning the resultant string."
318 (let (result negate follow preds mods
)
321 (let ((char (char-after)))
325 (if (looking-at "[^|':]")
326 (let ((func (read (current-buffer))))
327 (if (and func
(functionp func
))
328 (setq preds
(eshell-add-pred-func func preds
330 (error "Invalid function predicate '%s'"
331 (eshell-stringify func
))))
332 (error "Invalid function predicate")))
335 (setq negate
(not negate
)))
338 (setq follow
(not follow
)))
341 (if (looking-at "[^|':]")
342 (let ((func (read (current-buffer))))
343 (if (and func
(functionp func
))
346 (mapcar (function ,func
) lst
))
348 (error "Invalid function modifier '%s'"
349 (eshell-stringify func
))))
350 (error "Invalid function modifier")))
353 (let ((mod (assq (char-after) eshell-modifier-alist
)))
355 (error "Unknown modifier character '%c'" (char-after))
357 (setq mods
(cons (eval (cdr mod
)) mods
)))))
359 (let ((pred (assq char eshell-predicate-alist
)))
361 (error "Unknown predicate character '%c'" char
)
364 (eshell-add-pred-func (eval (cdr pred
)) preds
365 negate follow
))))))))
367 (error "Predicate or modifier ended prematurely")))
368 (cons (nreverse preds
) (nreverse mods
))))
370 (defun eshell-add-pred-func (pred funcs negate follow
)
371 "Add the predicate function PRED to FUNCS."
373 (setq pred
`(lambda (file)
374 (not (funcall ,pred file
)))))
376 (setq pred
`(lambda (file)
377 (funcall ,pred
(file-truename file
)))))
380 (defun eshell-pred-user-or-group (mod-char mod-type attr-index get-id-func
)
381 "Return a predicate to test whether a file match a given user/group id."
382 (let (ugid open close end
)
383 (if (looking-at "[0-9]+")
385 (setq ugid
(string-to-number (match-string 0)))
386 (goto-char (match-end 0)))
387 (setq open
(char-after))
388 (if (setq close
(memq open
'(?\
( ?\
[ ?\
< ?\
{)))
389 (setq close
(car (last '(?\
) ?\
] ?\
> ?\
})
393 (setq end
(eshell-find-delimiter open close
))
395 (error "Malformed %s name string for modifier `%c'"
398 (funcall get-id-func
(buffer-substring (point) end
)))
399 (goto-char (1+ end
)))
401 (error "Unknown %s name specified for modifier `%c'"
404 (let ((attrs (file-attributes file
)))
406 (= (nth ,attr-index attrs
) ,ugid
))))))
408 (defun eshell-pred-file-time (mod-char mod-type attr-index
)
409 "Return a predicate to test whether a file matches a certain time."
410 (let* ((quantum 86400)
411 qual amount when open close end
)
412 (when (memq (char-after) '(?M ?w ?h ?m
))
413 (setq quantum
(char-after))
416 (setq quantum
(* 60 60 24 30)))
418 (setq quantum
(* 60 60 24 7)))
420 (setq quantum
(* 60 60)))
426 (when (memq (char-after) '(?
+ ?-
))
427 (setq qual
(char-after))
429 (if (looking-at "[0-9]+")
431 (setq when
(- (eshell-time-to-seconds (current-time))
432 (* (string-to-number (match-string 0))
434 (goto-char (match-end 0)))
435 (setq open
(char-after))
436 (if (setq close
(memq open
'(?\
( ?\
[ ?\
< ?\
{)))
437 (setq close
(car (last '(?\
) ?\
] ?\
> ?\
})
441 (setq end
(eshell-find-delimiter open close
))
443 (error "Malformed %s time modifier `%c'" mod-type mod-char
))
444 (let* ((file (buffer-substring (point) end
))
445 (attrs (file-attributes file
)))
447 (error "Cannot stat file `%s'" file
))
448 (setq when
(eshell-time-to-seconds (nth attr-index attrs
))))
449 (goto-char (1+ end
)))
451 (let ((attrs (file-attributes file
)))
457 '=)) ,when
(eshell-time-to-seconds
458 (nth ,attr-index attrs
))))))))
460 (defun eshell-pred-file-type (type)
461 "Return a test which tests that the file is of a certain TYPE.
462 TYPE must be a character, and should be one of the possible options
463 that 'ls -l' will show in the first column of its display. "
465 (setq type
(char-after))
466 (if (memq type
'(?b ?c
))
470 (let ((attrs (eshell-file-attributes (directory-file-name file
))))
472 (memq (aref (nth 8 attrs
) 0)
475 (list 'quote
(list type
))))))))
477 (defsubst eshell-pred-file-mode
(mode)
478 "Return a test which tests that MODE pertains to the file."
480 (let ((modes (file-modes file
)))
482 (logand ,mode modes
)))))
484 (defun eshell-pred-file-links ()
485 "Return a predicate to test whether a file has a given number of links."
487 (when (memq (char-after) '(?- ?
+))
488 (setq qual
(char-after))
490 (unless (looking-at "[0-9]+")
491 (error "Invalid file link count modifier `l'"))
492 (setq amount
(string-to-number (match-string 0)))
493 (goto-char (match-end 0))
495 (let ((attrs (eshell-file-attributes file
)))
501 '=)) (nth 1 attrs
) ,amount
))))))
503 (defun eshell-pred-file-size ()
504 "Return a predicate to test whether a file is of a given size."
505 (let ((quantum 1) qual amount
)
506 (when (memq (downcase (char-after)) '(?k ?m ?p
))
507 (setq qual
(downcase (char-after)))
512 (setq quantum
(* 1024 1024)))
516 (when (memq (char-after) '(?- ?
+))
517 (setq qual
(char-after))
519 (unless (looking-at "[0-9]+")
520 (error "Invalid file size modifier `L'"))
521 (setq amount
(* (string-to-number (match-string 0)) quantum
))
522 (goto-char (match-end 0))
524 (let ((attrs (eshell-file-attributes file
)))
530 '=)) (nth 7 attrs
) ,amount
))))))
532 (defun eshell-pred-substitute (&optional repeat
)
533 "Return a modifier function that will substitute matches."
534 (let ((delim (char-after))
537 (setq end
(eshell-find-delimiter delim delim nil nil t
)
538 match
(buffer-substring-no-properties (point) end
))
540 (setq end
(eshell-find-delimiter delim delim nil nil t
)
541 replace
(buffer-substring-no-properties (point) end
))
549 (while (setq i
(string-match ,match str i
))
550 (setq str
(replace-match ,replace t nil str
))))
556 (if (string-match ,match str
)
557 (setq str
(replace-match ,replace t nil str
)))
560 (defun eshell-include-members (&optional invert-p
)
561 "Include only lisp members matching a regexp."
562 (let ((delim (char-after))
565 (setq end
(eshell-find-delimiter delim delim nil nil t
)
566 regexp
(buffer-substring-no-properties (point) end
))
570 lst nil
'((lambda (elem)
572 `(not (string-match ,regexp elem
))
573 `(string-match ,regexp elem
))))))))
575 (defun eshell-join-members ()
576 "Return a modifier function that join matches."
577 (let ((delim (char-after))
579 (if (not (memq delim
'(?
' ?
/)))
582 (setq end
(eshell-find-delimiter delim delim nil nil t
)
583 str
(buffer-substring-no-properties (point) end
))
584 (goto-char (1+ end
)))
586 (mapconcat 'identity lst
,str
))))
588 (defun eshell-split-members ()
589 "Return a modifier function that splits members."
590 (let ((delim (char-after))
592 (when (memq delim
'(?
' ?
/))
594 (setq end
(eshell-find-delimiter delim delim nil nil t
)
595 sep
(buffer-substring-no-properties (point) end
))
596 (goto-char (1+ end
)))
601 (split-string str
,sep
))) lst
))))
603 ;;; em-pred.el ends here