1 ;;; em-pred --- argument predicates and modifiers (ala zsh)
3 ;; Copyright (C) 1999, 2000 Free Sofware Foundation
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
24 (eval-when-compile (require 'esh-maint
))
26 (defgroup eshell-pred nil
27 "This module allows for predicates to be applied to globbing
28 patterns (similar to zsh), in addition to string modifiers which can
29 be applied either to globbing results, variable references, or just
31 :tag
"Value modifiers and predicates"
32 :group
'eshell-module
)
36 ;; Argument predication is used to affect which members of a list are
37 ;; selected for use as argument. This is most useful with globbing,
38 ;; but can be used on any list argument, to select certain members.
40 ;; Argument modifiers are used to manipulate argument values. For
41 ;; example, sorting lists, upcasing words, substituting characters,
44 ;; Here are some examples of how to use argument predication. Most of
45 ;; the predicates and modifiers are modeled after those provided by
48 ;; ls -ld *(/) ; list all directories
49 ;; ls -l *(@u'johnw') ; list all symlinks owned by 'johnw'
50 ;; bzip2 -9v **/*(a+30) ; compress everything which hasn't been
51 ;; accessed in 30 days
52 ;; echo *.c(:o:R) ; a reversed, sorted list of C files
53 ;; *(^@:U^u0) ; all non-symlinks not owned by 'root', upcased
54 ;; chmod u-x *(U*) : remove exec bit on all executables owned by user
56 ;; See the zsh docs for more on the syntax ([(zsh.info)Filename
61 (defcustom eshell-pred-load-hook
'(eshell-pred-initialize)
62 "*A list of functions to run when `eshell-pred' is loaded."
66 (defcustom eshell-predicate-alist
67 '((?
/ .
(eshell-pred-file-type ?d
)) ; directories
68 (?. .
(eshell-pred-file-type ?-
)) ; regular files
69 (?s .
(eshell-pred-file-type ?s
)) ; sockets
70 (?p .
(eshell-pred-file-type ?p
)) ; named pipes
71 (?
@ .
(eshell-pred-file-type ?l
)) ; symbolic links
72 (?% .
(eshell-pred-file-type ?%
)) ; allow user to specify (c def.)
73 (?r .
(eshell-pred-file-mode 0400)) ; owner-readable
74 (?w .
(eshell-pred-file-mode 0200)) ; owner-writable
75 (?x .
(eshell-pred-file-mode 0100)) ; owner-executable
76 (?A .
(eshell-pred-file-mode 0040)) ; group-readable
77 (?I .
(eshell-pred-file-mode 0020)) ; group-writable
78 (?E .
(eshell-pred-file-mode 0010)) ; group-executable
79 (?R .
(eshell-pred-file-mode 0004)) ; world-readable
80 (?W .
(eshell-pred-file-mode 0002)) ; world-writable
81 (?X .
(eshell-pred-file-mode 0001)) ; world-executable
82 (?s .
(eshell-pred-file-mode 4000)) ; setuid
83 (?S .
(eshell-pred-file-mode 2000)) ; setgid
84 (?t .
(eshell-pred-file-mode 1000)) ; sticky bit
85 (?U .
'(lambda (file) ; owned by effective uid
86 (if (file-exists-p file
)
87 (= (nth 2 (file-attributes file
)) (user-uid)))))
88 ;;; (?G . '(lambda (file) ; owned by effective gid
89 ;;; (if (file-exists-p file)
90 ;;; (= (nth 2 (file-attributes file)) (user-uid)))))
92 (and (file-regular-p file
)
93 (not (file-symlink-p file
))
94 (file-executable-p file
))))
95 (?l .
(eshell-pred-file-links))
96 (?u .
(eshell-pred-user-or-group ?u
"user" 2 'eshell-user-id
))
97 (?g .
(eshell-pred-user-or-group ?g
"group" 3 'eshell-group-id
))
98 (?a .
(eshell-pred-file-time ?a
"access" 4))
99 (?m .
(eshell-pred-file-time ?m
"modification" 5))
100 (?c .
(eshell-pred-file-time ?c
"change" 6))
101 (?L .
(eshell-pred-file-size)))
102 "*A list of predicates than can be applied to a globbing pattern.
103 The format of each entry is
105 (CHAR . PREDICATE-FUNC-SEXP)"
106 :type
'(repeat (cons character sexp
))
109 (put 'eshell-predicate-alist
'risky-local-variable t
)
111 (defcustom eshell-modifier-alist
112 '((?e .
'(lambda (lst)
117 (car (eshell-parse-argument str
))))) lst
)))
119 (mapcar 'downcase lst
)))
121 (mapcar 'upcase lst
)))
123 (mapcar 'capitalize lst
)))
125 (mapcar 'file-name-directory lst
)))
126 (?i .
(eshell-include-members))
127 (?x .
(eshell-include-members t
))
129 (mapcar 'file-name-sans-extension lst
)))
131 (mapcar 'file-name-extension lst
)))
133 (mapcar 'file-name-nondirectory lst
)))
135 (mapcar 'eshell-escape-arg lst
)))
137 (eshell-uniqify-list lst
)))
139 (sort lst
'string-lessp
)))
141 (nreverse (sort lst
'string-lessp
))))
142 (?j .
(eshell-join-members))
143 (?S .
(eshell-split-members))
147 (if (eq (char-before) ?s
)
148 (eshell-pred-substitute t
)
149 (error "`g' modifier cannot be used alone"))))
150 (?s .
(eshell-pred-substitute)))
151 "*A list of modifiers than can be applied to an argument expansion.
152 The format of each entry is
154 (CHAR ENTRYWISE-P MODIFIER-FUNC-SEXP)"
155 :type
'(repeat (cons character sexp
))
158 (put 'eshell-modifier-alist
'risky-local-variable t
)
160 (defvar eshell-predicate-help-string
161 "Eshell predicate quick reference:
163 - follow symbolic references for predicates after the `-'
164 ^ invert sense of predicates after the `^'
167 / directories s sockets
168 . regular files p named pipes
169 * executable (files only) @ symbolic links
171 %x file type == `x' (as by ls -l; so `c' = char device, etc.)
173 PERMISSION BITS (for owner/group/world):
174 r/A/R readable s setuid
175 w/I/W writable S setgid
176 x/E/X executable t sticky bit
179 U owned by effective uid
180 u(UID|'user') owned by UID/user
181 g(GID|'group') owned by GID/group
185 a[Mwhm][+-](N|'FILE') access time +/-/= N mnths/weeks/days/mins
186 if FILE specified, use as comparison basis;
187 so a+'file.c' shows files accessed before
188 file.c was last accessed
189 m[Mwhm][+-](N|'FILE') modification time...
190 c[Mwhm][+-](N|'FILE') change time...
191 L[kmp][+-]N file size +/-/= N Kb/Mb/blocks
194 *(^@) all non-dot files which are not symlinks
195 .#*(^@) all files which are not symbolic links
196 **/.#*(*) all executable files, searched recursively
197 ***/*~f*(-/) recursively (though not traversing symlinks),
198 find all directories (or symlinks referring to
199 directories) whose names do not begin with f.
200 e*(*Lk+50) executables 50k or larger beginning with 'e'")
202 (defvar eshell-modifier-help-string
203 "Eshell modifier quick reference:
205 FOR SINGLE ARGUMENTS, or each argument of a list of strings:
213 r strip file extension
214 q escape special characters
216 S split string at any whitespace character
217 S/PAT/ split string at each occurance of PAT
219 FOR LISTS OF ARGUMENTS:
220 o sort alphabetically
221 O reverse sort alphabetically
222 u uniq list (typically used after :o or :O)
225 j join list members, separated by a space
226 j/PAT/ join list members, separated by PAT
227 i/PAT/ exclude all members not matching PAT
228 x/PAT/ exclude all members matching PAT
230 s/pat/match/ substitute PAT with MATCH
231 g/pat/match/ substitute PAT with MATCH for all occurances
234 *.c(:o) sorted list of .c files")
238 (defun eshell-display-predicate-help ()
243 (insert eshell-predicate-help-string
)))))
245 (defun eshell-display-modifier-help ()
250 (insert eshell-modifier-help-string
)))))
252 (defun eshell-pred-initialize ()
253 "Initialize the predicate/modifier code."
254 (make-local-hook 'eshell-parse-argument-hook
)
255 (add-hook 'eshell-parse-argument-hook
256 'eshell-parse-arg-modifier t t
)
257 (define-key eshell-command-map
[(meta ?q
)] 'eshell-display-predicate-help
)
258 (define-key eshell-command-map
[(meta ?m
)] 'eshell-display-modifier-help
))
260 (defun eshell-apply-modifiers (lst predicates modifiers
)
261 "Apply to LIST a series of PREDICATES and MODIFIERS."
267 (setq lst
(eshell-winnow-list lst nil predicates
))
269 (setq lst
(funcall (car modifiers
) lst
)
270 modifiers
(cdr modifiers
)))
276 (defun eshell-parse-arg-modifier ()
277 "Parse a modifier that has been specified after an argument.
278 This function is specially for adding onto `eshell-parse-argument-hook'."
279 (when (eq (char-after) ?\
()
281 (let ((end (eshell-find-delimiter ?\
( ?\
))))
283 (throw 'eshell-incomplete ?\
()
284 (when (eshell-arg-delimiter (1+ end
))
286 (narrow-to-region (point) end
)
287 (let* ((modifiers (eshell-parse-modifiers))
288 (preds (car modifiers
))
289 (mods (cdr modifiers
)))
291 ;; has to go at the end, which is only natural since
292 ;; syntactically it can only occur at the end
293 (setq eshell-current-modifiers
295 eshell-current-modifiers
298 (eshell-apply-modifiers
299 lst
(quote ,preds
) (quote ,mods
)))))))))
301 (eshell-finish-arg))))))
303 (defun eshell-parse-modifiers ()
304 "Parse value modifiers and predicates at point.
305 If ALLOW-PREDS is non-nil, predicates will be parsed as well.
306 Return a cons cell of the form
308 (PRED-FUNC-LIST . MOD-FUNC-LIST)
310 NEW-STRING is STRING minus any modifiers. PRED-FUNC-LIST is a list of
311 predicate functions. MOD-FUNC-LIST is a list of result modifier
312 functions. PRED-FUNCS take a filename and return t if the test
313 succeeds; MOD-FUNCS take any string and preform a modification,
314 returning the resultant string."
315 (let (result negate follow preds mods
)
318 (let ((char (char-after)))
322 (if (looking-at "[^|':]")
323 (let ((func (read (current-buffer))))
324 (if (and func
(functionp func
))
325 (setq preds
(eshell-add-pred-func func preds
327 (error "Invalid function predicate '%s'"
328 (eshell-stringify func
))))
329 (error "Invalid function predicate")))
332 (setq negate
(not negate
)))
335 (setq follow
(not follow
)))
338 (if (looking-at "[^|':]")
339 (let ((func (read (current-buffer))))
340 (if (and func
(functionp func
))
343 (mapcar (function ,func
) lst
))
345 (error "Invalid function modifier '%s'"
346 (eshell-stringify func
))))
347 (error "Invalid function modifier")))
350 (let ((mod (assq (char-after) eshell-modifier-alist
)))
352 (error "Unknown modifier character '%c'" (char-after))
354 (setq mods
(cons (eval (cdr mod
)) mods
)))))
356 (let ((pred (assq char eshell-predicate-alist
)))
358 (error "Unknown predicate character '%c'" char
)
361 (eshell-add-pred-func (eval (cdr pred
)) preds
362 negate follow
))))))))
364 (error "Predicate or modifier ended prematurely")))
365 (cons (nreverse preds
) (nreverse mods
))))
367 (defun eshell-add-pred-func (pred funcs negate follow
)
368 "Add the predicate function PRED to FUNCS."
370 (setq pred
`(lambda (file)
371 (not (funcall ,pred file
)))))
373 (setq pred
`(lambda (file)
374 (funcall ,pred
(file-truename file
)))))
377 (defun eshell-pred-user-or-group (mod-char mod-type attr-index get-id-func
)
378 "Return a predicate to test whether a file match a given user/group id."
379 (let (ugid open close end
)
380 (if (looking-at "[0-9]+")
382 (setq ugid
(string-to-number (match-string 0)))
383 (goto-char (match-end 0)))
384 (setq open
(char-after))
385 (if (setq close
(memq open
'(?\
( ?\
[ ?\
< ?\
{)))
386 (setq close
(car (last '(?\
) ?\
] ?\
> ?\
})
390 (setq end
(eshell-find-delimiter open close
))
392 (error "Malformed %s name string for modifier `%c'"
395 (funcall get-id-func
(buffer-substring (point) end
)))
396 (goto-char (1+ end
)))
398 (error "Unknown %s name specified for modifier `%c'"
401 (let ((attrs (file-attributes file
)))
403 (= (nth ,attr-index attrs
) ,ugid
))))))
405 (defun eshell-pred-file-time (mod-char mod-type attr-index
)
406 "Return a predicate to test whether a file matches a certain time."
407 (let* ((quantum 86400)
408 qual amount when open close end
)
409 (when (memq (char-after) '(?M ?w ?h ?m
))
410 (setq quantum
(char-after))
413 (setq quantum
(* 60 60 24 30)))
415 (setq quantum
(* 60 60 24 7)))
417 (setq quantum
(* 60 60)))
423 (when (memq (char-after) '(?
+ ?-
))
424 (setq qual
(char-after))
426 (if (looking-at "[0-9]+")
428 (setq when
(- (eshell-time-to-seconds (current-time))
429 (* (string-to-number (match-string 0))
431 (goto-char (match-end 0)))
432 (setq open
(char-after))
433 (if (setq close
(memq open
'(?\
( ?\
[ ?\
< ?\
{)))
434 (setq close
(car (last '(?\
) ?\
] ?\
> ?\
})
438 (setq end
(eshell-find-delimiter open close
))
440 (error "Malformed %s time modifier `%c'" mod-type mod-char
))
441 (let* ((file (buffer-substring (point) end
))
442 (attrs (file-attributes file
)))
444 (error "Cannot stat file `%s'" file
))
445 (setq when
(eshell-time-to-seconds (nth attr-index attrs
))))
446 (goto-char (1+ end
)))
448 (let ((attrs (file-attributes file
)))
454 '=)) ,when
(eshell-time-to-seconds
455 (nth ,attr-index attrs
))))))))
457 (defun eshell-pred-file-type (type)
458 "Return a test which tests that the file is of a certain TYPE.
459 TYPE must be a character, and should be one of the possible options
460 that 'ls -l' will show in the first column of its display. "
462 (setq type
(char-after))
463 (if (memq type
'(?b ?c
))
467 (let ((attrs (file-attributes (directory-file-name file
))))
469 (memq (aref (nth 8 attrs
) 0)
472 (list 'quote
(list type
))))))))
474 (defsubst eshell-pred-file-mode
(mode)
475 "Return a test which tests that MODE pertains to the file."
477 (let ((modes (file-modes file
)))
479 (logand ,mode modes
)))))
481 (defun eshell-pred-file-links ()
482 "Return a predicate to test whether a file has a given number of links."
484 (when (memq (char-after) '(?- ?
+))
485 (setq qual
(char-after))
487 (unless (looking-at "[0-9]+")
488 (error "Invalid file link count modifier `l'"))
489 (setq amount
(string-to-number (match-string 0)))
490 (goto-char (match-end 0))
492 (let ((attrs (file-attributes file
)))
498 '=)) (nth 1 attrs
) ,amount
))))))
500 (defun eshell-pred-file-size ()
501 "Return a predicate to test whether a file is of a given size."
502 (let ((quantum 1) qual amount
)
503 (when (memq (downcase (char-after)) '(?k ?m ?p
))
504 (setq qual
(downcase (char-after)))
509 (setq quantum
(* 1024 1024)))
513 (when (memq (char-after) '(?- ?
+))
514 (setq qual
(char-after))
516 (unless (looking-at "[0-9]+")
517 (error "Invalid file size modifier `L'"))
518 (setq amount
(* (string-to-number (match-string 0)) quantum
))
519 (goto-char (match-end 0))
521 (let ((attrs (file-attributes file
)))
527 '=)) (nth 7 attrs
) ,amount
))))))
529 (defun eshell-pred-substitute (&optional repeat
)
530 "Return a modifier function that will substitute matches."
531 (let ((delim (char-after))
534 (setq end
(eshell-find-delimiter delim delim nil nil t
)
535 match
(buffer-substring-no-properties (point) end
))
537 (setq end
(eshell-find-delimiter delim delim nil nil t
)
538 replace
(buffer-substring-no-properties (point) end
))
546 (while (setq i
(string-match ,match str i
))
547 (setq str
(replace-match ,replace t nil str
))))
553 (if (string-match ,match str
)
554 (setq str
(replace-match ,replace t nil str
)))
557 (defun eshell-include-members (&optional invert-p
)
558 "Include only lisp members matching a regexp."
559 (let ((delim (char-after))
562 (setq end
(eshell-find-delimiter delim delim nil nil t
)
563 regexp
(buffer-substring-no-properties (point) end
))
567 lst nil
'((lambda (elem)
569 `(not (string-match ,regexp elem
))
570 `(string-match ,regexp elem
))))))))
572 (defun eshell-join-members ()
573 "Return a modifier function that join matches."
574 (let ((delim (char-after))
576 (if (not (memq delim
'(?
' ?
/)))
579 (setq end
(eshell-find-delimiter delim delim nil nil t
)
580 str
(buffer-substring-no-properties (point) end
))
581 (goto-char (1+ end
)))
583 (mapconcat 'identity lst
,str
))))
585 (defun eshell-split-members ()
586 "Return a modifier function that splits members."
587 (let ((delim (char-after))
589 (when (memq delim
'(?
' ?
/))
591 (setq end
(eshell-find-delimiter delim delim nil nil t
)
592 sep
(buffer-substring-no-properties (point) end
))
593 (goto-char (1+ end
)))
598 (split-string str
,sep
))) lst
))))
602 ;;; em-pred.el ends here