1 ;;; em-pred.el --- argument predicates and modifiers (ala zsh)
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006 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 2, 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 (eval-when-compile (require 'esh-maint
))
29 (defgroup eshell-pred nil
30 "This module allows for predicates to be applied to globbing
31 patterns (similar to zsh), in addition to string modifiers which can
32 be applied either to globbing results, variable references, or just
34 :tag
"Value modifiers and predicates"
35 :group
'eshell-module
)
39 ;; Argument predication is used to affect which members of a list are
40 ;; selected for use as argument. This is most useful with globbing,
41 ;; but can be used on any list argument, to select certain members.
43 ;; Argument modifiers are used to manipulate argument values. For
44 ;; example, sorting lists, upcasing words, substituting characters,
47 ;; Here are some examples of how to use argument predication. Most of
48 ;; the predicates and modifiers are modeled after those provided by
51 ;; ls -ld *(/) ; list all directories
52 ;; ls -l *(@u'johnw') ; list all symlinks owned by 'johnw'
53 ;; bzip2 -9v **/*(a+30) ; compress everything which hasn't been
54 ;; accessed in 30 days
55 ;; echo *.c(:o:R) ; a reversed, sorted list of C files
56 ;; *(^@:U^u0) ; all non-symlinks not owned by 'root', upcased
57 ;; chmod u-x *(U*) : remove exec bit on all executables owned by user
59 ;; See the zsh docs for more on the syntax ([(zsh.info)Filename
66 (defcustom eshell-pred-load-hook
'(eshell-pred-initialize)
67 "*A list of functions to run when `eshell-pred' is loaded."
71 (defcustom eshell-predicate-alist
72 '((?
/ .
(eshell-pred-file-type ?d
)) ; directories
73 (?. .
(eshell-pred-file-type ?-
)) ; regular files
74 (?s .
(eshell-pred-file-type ?s
)) ; sockets
75 (?p .
(eshell-pred-file-type ?p
)) ; named pipes
76 (?
@ .
(eshell-pred-file-type ?l
)) ; symbolic links
77 (?% .
(eshell-pred-file-type ?%
)) ; allow user to specify (c def.)
78 (?r .
(eshell-pred-file-mode 0400)) ; owner-readable
79 (?w .
(eshell-pred-file-mode 0200)) ; owner-writable
80 (?x .
(eshell-pred-file-mode 0100)) ; owner-executable
81 (?A .
(eshell-pred-file-mode 0040)) ; group-readable
82 (?I .
(eshell-pred-file-mode 0020)) ; group-writable
83 (?E .
(eshell-pred-file-mode 0010)) ; group-executable
84 (?R .
(eshell-pred-file-mode 0004)) ; world-readable
85 (?W .
(eshell-pred-file-mode 0002)) ; world-writable
86 (?X .
(eshell-pred-file-mode 0001)) ; world-executable
87 (?s .
(eshell-pred-file-mode 4000)) ; setuid
88 (?S .
(eshell-pred-file-mode 2000)) ; setgid
89 (?t .
(eshell-pred-file-mode 1000)) ; sticky bit
90 (?U .
'(lambda (file) ; owned by effective uid
91 (if (file-exists-p file
)
92 (= (nth 2 (file-attributes file
)) (user-uid)))))
93 ;;; (?G . '(lambda (file) ; owned by effective gid
94 ;;; (if (file-exists-p file)
95 ;;; (= (nth 2 (file-attributes file)) (user-uid)))))
97 (and (file-regular-p file
)
98 (not (file-symlink-p file
))
99 (file-executable-p file
))))
100 (?l .
(eshell-pred-file-links))
101 (?u .
(eshell-pred-user-or-group ?u
"user" 2 'eshell-user-id
))
102 (?g .
(eshell-pred-user-or-group ?g
"group" 3 'eshell-group-id
))
103 (?a .
(eshell-pred-file-time ?a
"access" 4))
104 (?m .
(eshell-pred-file-time ?m
"modification" 5))
105 (?c .
(eshell-pred-file-time ?c
"change" 6))
106 (?L .
(eshell-pred-file-size)))
107 "*A list of predicates than can be applied to a globbing pattern.
108 The format of each entry is
110 (CHAR . PREDICATE-FUNC-SEXP)"
111 :type
'(repeat (cons character sexp
))
114 (put 'eshell-predicate-alist
'risky-local-variable t
)
116 (defcustom eshell-modifier-alist
117 '((?E .
'(lambda (lst)
122 (car (eshell-parse-argument str
))))) lst
)))
124 (mapcar 'downcase lst
)))
126 (mapcar 'upcase lst
)))
128 (mapcar 'capitalize lst
)))
130 (mapcar 'file-name-directory lst
)))
131 (?i .
(eshell-include-members))
132 (?x .
(eshell-include-members t
))
134 (mapcar 'file-name-sans-extension lst
)))
136 (mapcar 'file-name-extension lst
)))
138 (mapcar 'file-name-nondirectory lst
)))
140 (mapcar 'eshell-escape-arg lst
)))
142 (eshell-uniqify-list lst
)))
144 (sort lst
'string-lessp
)))
146 (nreverse (sort lst
'string-lessp
))))
147 (?j .
(eshell-join-members))
148 (?S .
(eshell-split-members))
152 (if (eq (char-before) ?s
)
153 (eshell-pred-substitute t
)
154 (error "`g' modifier cannot be used alone"))))
155 (?s .
(eshell-pred-substitute)))
156 "*A list of modifiers than can be applied to an argument expansion.
157 The format of each entry is
159 (CHAR ENTRYWISE-P MODIFIER-FUNC-SEXP)"
160 :type
'(repeat (cons character sexp
))
163 (put 'eshell-modifier-alist
'risky-local-variable t
)
165 (defvar eshell-predicate-help-string
166 "Eshell predicate quick reference:
168 - follow symbolic references for predicates after the `-'
169 ^ invert sense of predicates after the `^'
172 / directories s sockets
173 . regular files p named pipes
174 * executable (files only) @ symbolic links
176 %x file type == `x' (as by ls -l; so `c' = char device, etc.)
178 PERMISSION BITS (for owner/group/world):
179 r/A/R readable s setuid
180 w/I/W writable S setgid
181 x/E/X executable t sticky bit
184 U owned by effective uid
185 u(UID|'user') owned by UID/user
186 g(GID|'group') owned by GID/group
190 a[Mwhms][+-](N|'FILE') access time +/-/= N mnths/weeks/hours/mins/secs
191 (days if unspecified) if FILE specified,
192 use as comparison basis; so a+'file.c'
193 shows files accessed before file.c was
195 m[Mwhms][+-](N|'FILE') modification time...
196 c[Mwhms][+-](N|'FILE') change time...
197 L[kmp][+-]N file size +/-/= N Kb/Mb/blocks
200 *(^@) all non-dot files which are not symlinks
201 .#*(^@) all files which are not symbolic links
202 **/.#*(*) all executable files, searched recursively
203 ***/*~f*(-/) recursively (though not traversing symlinks),
204 find all directories (or symlinks referring to
205 directories) whose names do not begin with f.
206 e*(*Lk+50) executables 50k or larger beginning with 'e'")
208 (defvar eshell-modifier-help-string
209 "Eshell modifier quick reference:
211 FOR SINGLE ARGUMENTS, or each argument of a list of strings:
219 r strip file extension
220 q escape special characters
222 S split string at any whitespace character
223 S/PAT/ split string at each occurrence of PAT
225 FOR LISTS OF ARGUMENTS:
226 o sort alphabetically
227 O reverse sort alphabetically
228 u uniq list (typically used after :o or :O)
231 j join list members, separated by a space
232 j/PAT/ join list members, separated by PAT
233 i/PAT/ exclude all members not matching PAT
234 x/PAT/ exclude all members matching PAT
236 s/pat/match/ substitute PAT with MATCH
237 g/pat/match/ substitute PAT with MATCH for all occurrences
240 *.c(:o) sorted list of .c files")
244 (defun eshell-display-predicate-help ()
249 (insert eshell-predicate-help-string
)))))
251 (defun eshell-display-modifier-help ()
256 (insert eshell-modifier-help-string
)))))
258 (defun eshell-pred-initialize ()
259 "Initialize the predicate/modifier code."
260 (add-hook 'eshell-parse-argument-hook
261 'eshell-parse-arg-modifier t t
)
262 (define-key eshell-command-map
[(meta ?q
)] 'eshell-display-predicate-help
)
263 (define-key eshell-command-map
[(meta ?m
)] 'eshell-display-modifier-help
))
265 (defun eshell-apply-modifiers (lst predicates modifiers
)
266 "Apply to LIST a series of PREDICATES and MODIFIERS."
272 (setq lst
(eshell-winnow-list lst nil predicates
))
274 (setq lst
(funcall (car modifiers
) lst
)
275 modifiers
(cdr modifiers
)))
281 (defun eshell-parse-arg-modifier ()
282 "Parse a modifier that has been specified after an argument.
283 This function is specially for adding onto `eshell-parse-argument-hook'."
284 (when (eq (char-after) ?\
()
286 (let ((end (eshell-find-delimiter ?\
( ?\
))))
288 (throw 'eshell-incomplete ?\
()
289 (when (eshell-arg-delimiter (1+ end
))
291 (narrow-to-region (point) end
)
292 (let* ((modifiers (eshell-parse-modifiers))
293 (preds (car modifiers
))
294 (mods (cdr modifiers
)))
296 ;; has to go at the end, which is only natural since
297 ;; syntactically it can only occur at the end
298 (setq eshell-current-modifiers
300 eshell-current-modifiers
303 (eshell-apply-modifiers
304 lst
(quote ,preds
) (quote ,mods
)))))))))
306 (eshell-finish-arg))))))
308 (defun eshell-parse-modifiers ()
309 "Parse value modifiers and predicates at point.
310 If ALLOW-PREDS is non-nil, predicates will be parsed as well.
311 Return a cons cell of the form
313 (PRED-FUNC-LIST . MOD-FUNC-LIST)
315 NEW-STRING is STRING minus any modifiers. PRED-FUNC-LIST is a list of
316 predicate functions. MOD-FUNC-LIST is a list of result modifier
317 functions. PRED-FUNCS take a filename and return t if the test
318 succeeds; MOD-FUNCS take any string and preform a modification,
319 returning the resultant string."
320 (let (result negate follow preds mods
)
323 (let ((char (char-after)))
327 (if (looking-at "[^|':]")
328 (let ((func (read (current-buffer))))
329 (if (and func
(functionp func
))
330 (setq preds
(eshell-add-pred-func func preds
332 (error "Invalid function predicate '%s'"
333 (eshell-stringify func
))))
334 (error "Invalid function predicate")))
337 (setq negate
(not negate
)))
340 (setq follow
(not follow
)))
343 (if (looking-at "[^|':]")
344 (let ((func (read (current-buffer))))
345 (if (and func
(functionp func
))
348 (mapcar (function ,func
) lst
))
350 (error "Invalid function modifier '%s'"
351 (eshell-stringify func
))))
352 (error "Invalid function modifier")))
355 (let ((mod (assq (char-after) eshell-modifier-alist
)))
357 (error "Unknown modifier character '%c'" (char-after))
359 (setq mods
(cons (eval (cdr mod
)) mods
)))))
361 (let ((pred (assq char eshell-predicate-alist
)))
363 (error "Unknown predicate character '%c'" char
)
366 (eshell-add-pred-func (eval (cdr pred
)) preds
367 negate follow
))))))))
369 (error "Predicate or modifier ended prematurely")))
370 (cons (nreverse preds
) (nreverse mods
))))
372 (defun eshell-add-pred-func (pred funcs negate follow
)
373 "Add the predicate function PRED to FUNCS."
375 (setq pred
`(lambda (file)
376 (not (funcall ,pred file
)))))
378 (setq pred
`(lambda (file)
379 (funcall ,pred
(file-truename file
)))))
382 (defun eshell-pred-user-or-group (mod-char mod-type attr-index get-id-func
)
383 "Return a predicate to test whether a file match a given user/group id."
384 (let (ugid open close end
)
385 (if (looking-at "[0-9]+")
387 (setq ugid
(string-to-number (match-string 0)))
388 (goto-char (match-end 0)))
389 (setq open
(char-after))
390 (if (setq close
(memq open
'(?\
( ?\
[ ?\
< ?\
{)))
391 (setq close
(car (last '(?\
) ?\
] ?\
> ?\
})
395 (setq end
(eshell-find-delimiter open close
))
397 (error "Malformed %s name string for modifier `%c'"
400 (funcall get-id-func
(buffer-substring (point) end
)))
401 (goto-char (1+ end
)))
403 (error "Unknown %s name specified for modifier `%c'"
406 (let ((attrs (file-attributes file
)))
408 (= (nth ,attr-index attrs
) ,ugid
))))))
410 (defun eshell-pred-file-time (mod-char mod-type attr-index
)
411 "Return a predicate to test whether a file matches a certain time."
412 (let* ((quantum 86400)
413 qual amount when open close end
)
414 (when (memq (char-after) '(?M ?w ?h ?m ?s
))
415 (setq quantum
(char-after))
418 (setq quantum
(* 60 60 24 30)))
420 (setq quantum
(* 60 60 24 7)))
422 (setq quantum
(* 60 60)))
428 (when (memq (char-after) '(?
+ ?-
))
429 (setq qual
(char-after))
431 (if (looking-at "[0-9]+")
433 (setq when
(- (eshell-time-to-seconds (current-time))
434 (* (string-to-number (match-string 0))
436 (goto-char (match-end 0)))
437 (setq open
(char-after))
438 (if (setq close
(memq open
'(?\
( ?\
[ ?\
< ?\
{)))
439 (setq close
(car (last '(?\
) ?\
] ?\
> ?\
})
443 (setq end
(eshell-find-delimiter open close
))
445 (error "Malformed %s time modifier `%c'" mod-type mod-char
))
446 (let* ((file (buffer-substring (point) end
))
447 (attrs (file-attributes file
)))
449 (error "Cannot stat file `%s'" file
))
450 (setq when
(eshell-time-to-seconds (nth attr-index attrs
))))
451 (goto-char (1+ end
)))
453 (let ((attrs (file-attributes file
)))
459 '=)) ,when
(eshell-time-to-seconds
460 (nth ,attr-index attrs
))))))))
462 (defun eshell-pred-file-type (type)
463 "Return a test which tests that the file is of a certain TYPE.
464 TYPE must be a character, and should be one of the possible options
465 that 'ls -l' will show in the first column of its display. "
467 (setq type
(char-after))
468 (if (memq type
'(?b ?c
))
472 (let ((attrs (eshell-file-attributes (directory-file-name file
))))
474 (memq (aref (nth 8 attrs
) 0)
477 (list 'quote
(list type
))))))))
479 (defsubst eshell-pred-file-mode
(mode)
480 "Return a test which tests that MODE pertains to the file."
482 (let ((modes (file-modes file
)))
484 (logand ,mode modes
)))))
486 (defun eshell-pred-file-links ()
487 "Return a predicate to test whether a file has a given number of links."
489 (when (memq (char-after) '(?- ?
+))
490 (setq qual
(char-after))
492 (unless (looking-at "[0-9]+")
493 (error "Invalid file link count modifier `l'"))
494 (setq amount
(string-to-number (match-string 0)))
495 (goto-char (match-end 0))
497 (let ((attrs (eshell-file-attributes file
)))
503 '=)) (nth 1 attrs
) ,amount
))))))
505 (defun eshell-pred-file-size ()
506 "Return a predicate to test whether a file is of a given size."
507 (let ((quantum 1) qual amount
)
508 (when (memq (downcase (char-after)) '(?k ?m ?p
))
509 (setq qual
(downcase (char-after)))
514 (setq quantum
(* 1024 1024)))
518 (when (memq (char-after) '(?- ?
+))
519 (setq qual
(char-after))
521 (unless (looking-at "[0-9]+")
522 (error "Invalid file size modifier `L'"))
523 (setq amount
(* (string-to-number (match-string 0)) quantum
))
524 (goto-char (match-end 0))
526 (let ((attrs (eshell-file-attributes file
)))
532 '=)) (nth 7 attrs
) ,amount
))))))
534 (defun eshell-pred-substitute (&optional repeat
)
535 "Return a modifier function that will substitute matches."
536 (let ((delim (char-after))
539 (setq end
(eshell-find-delimiter delim delim nil nil t
)
540 match
(buffer-substring-no-properties (point) end
))
542 (setq end
(eshell-find-delimiter delim delim nil nil t
)
543 replace
(buffer-substring-no-properties (point) end
))
551 (while (setq i
(string-match ,match str i
))
552 (setq str
(replace-match ,replace t nil str
))))
558 (if (string-match ,match str
)
559 (setq str
(replace-match ,replace t nil str
)))
562 (defun eshell-include-members (&optional invert-p
)
563 "Include only lisp members matching a regexp."
564 (let ((delim (char-after))
567 (setq end
(eshell-find-delimiter delim delim nil nil t
)
568 regexp
(buffer-substring-no-properties (point) end
))
572 lst nil
'((lambda (elem)
574 `(not (string-match ,regexp elem
))
575 `(string-match ,regexp elem
))))))))
577 (defun eshell-join-members ()
578 "Return a modifier function that join matches."
579 (let ((delim (char-after))
581 (if (not (memq delim
'(?
' ?
/)))
584 (setq end
(eshell-find-delimiter delim delim nil nil t
)
585 str
(buffer-substring-no-properties (point) end
))
586 (goto-char (1+ end
)))
588 (mapconcat 'identity lst
,str
))))
590 (defun eshell-split-members ()
591 "Return a modifier function that splits members."
592 (let ((delim (char-after))
594 (when (memq delim
'(?
' ?
/))
596 (setq end
(eshell-find-delimiter delim delim nil nil t
)
597 sep
(buffer-substring-no-properties (point) end
))
598 (goto-char (1+ end
)))
603 (split-string str
,sep
))) lst
))))
605 ;;; arch-tag: 8b5ce022-17f3-4c40-93c7-5faafaa63f31
606 ;;; em-pred.el ends here