1 ;;; em-pred.el --- argument predicates and modifiers (ala zsh)
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: John Wiegley <johnw@gnu.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
27 ;; Argument predication is used to affect which members of a list are
28 ;; selected for use as argument. This is most useful with globbing,
29 ;; but can be used on any list argument, to select certain members.
31 ;; Argument modifiers are used to manipulate argument values. For
32 ;; example, sorting lists, upcasing words, substituting characters,
35 ;; Here are some examples of how to use argument predication. Most of
36 ;; the predicates and modifiers are modeled after those provided by
39 ;; ls -ld *(/) ; list all directories
40 ;; ls -l *(@u'johnw') ; list all symlinks owned by 'johnw'
41 ;; bzip2 -9v **/*(a+30) ; compress everything which hasn't been
42 ;; accessed in 30 days
43 ;; echo *.c(:o:R) ; a reversed, sorted list of C files
44 ;; *(^@:U^u0) ; all non-symlinks not owned by 'root', upcased
45 ;; chmod u-x *(U*) : remove exec bit on all executables owned by user
47 ;; See the zsh docs for more on the syntax ([(zsh.info)Filename
52 (eval-when-compile (require 'eshell
))
54 (defgroup eshell-pred nil
55 "This module allows for predicates to be applied to globbing
56 patterns (similar to zsh), in addition to string modifiers which can
57 be applied either to globbing results, variable references, or just
59 :tag
"Value modifiers and predicates"
60 :group
'eshell-module
)
64 (defcustom eshell-pred-load-hook
'(eshell-pred-initialize)
65 "*A list of functions to run when `eshell-pred' is loaded."
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)))))
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
))
112 (put 'eshell-predicate-alist
'risky-local-variable t
)
114 (defcustom eshell-modifier-alist
115 '((?E .
'(lambda (lst)
120 (car (eshell-parse-argument str
))))) lst
)))
122 (mapcar 'downcase lst
)))
124 (mapcar 'upcase lst
)))
126 (mapcar 'capitalize lst
)))
128 (mapcar 'file-name-directory lst
)))
129 (?i .
(eshell-include-members))
130 (?x .
(eshell-include-members t
))
132 (mapcar 'file-name-sans-extension lst
)))
134 (mapcar 'file-name-extension lst
)))
136 (mapcar 'file-name-nondirectory lst
)))
138 (mapcar 'eshell-escape-arg lst
)))
140 (eshell-uniqify-list lst
)))
142 (sort lst
'string-lessp
)))
144 (nreverse (sort lst
'string-lessp
))))
145 (?j .
(eshell-join-members))
146 (?S .
(eshell-split-members))
150 (if (eq (char-before) ?s
)
151 (eshell-pred-substitute t
)
152 (error "`g' modifier cannot be used alone"))))
153 (?s .
(eshell-pred-substitute)))
154 "*A list of modifiers than can be applied to an argument expansion.
155 The format of each entry is
157 (CHAR ENTRYWISE-P MODIFIER-FUNC-SEXP)"
158 :type
'(repeat (cons character sexp
))
161 (put 'eshell-modifier-alist
'risky-local-variable t
)
163 (defvar eshell-predicate-help-string
164 "Eshell predicate quick reference:
166 - follow symbolic references for predicates after the `-'
167 ^ invert sense of predicates after the `^'
170 / directories s sockets
171 . regular files p named pipes
172 * executable (files only) @ symbolic links
174 %x file type == `x' (as by ls -l; so `c' = char device, etc.)
176 PERMISSION BITS (for owner/group/world):
177 r/A/R readable s setuid
178 w/I/W writable S setgid
179 x/E/X executable t sticky bit
182 U owned by effective uid
183 u(UID|'user') owned by UID/user
184 g(GID|'group') owned by GID/group
188 a[Mwhms][+-](N|'FILE') access time +/-/= N mnths/weeks/hours/mins/secs
189 (days if unspecified) if FILE specified,
190 use as comparison basis; so a+'file.c'
191 shows files accessed before file.c was
193 m[Mwhms][+-](N|'FILE') modification time...
194 c[Mwhms][+-](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 occurrence 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 occurrences
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 ?s
))
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
))))
605 ;; arch-tag: 8b5ce022-17f3-4c40-93c7-5faafaa63f31
606 ;;; em-pred.el ends here