1 ;;; em-pred.el --- argument predicates and modifiers (ala zsh)
3 ;; Copyright (C) 1999-2013 Free Software Foundation, Inc.
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 3 of the License, or
12 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
24 ;; Argument predication is used to affect which members of a list are
25 ;; selected for use as argument. This is most useful with globbing,
26 ;; but can be used on any list argument, to select certain members.
28 ;; Argument modifiers are used to manipulate argument values. For
29 ;; example, sorting lists, upcasing words, substituting characters,
32 ;; Here are some examples of how to use argument predication. Most of
33 ;; the predicates and modifiers are modeled after those provided by
36 ;; ls -ld *(/) ; list all directories
37 ;; ls -l *(@u'johnw') ; list all symlinks owned by 'johnw'
38 ;; bzip2 -9v **/*(a+30) ; compress everything which hasn't been
39 ;; accessed in 30 days
40 ;; echo *.c(:o:R) ; a reversed, sorted list of C files
41 ;; *(^@:U^u0) ; all non-symlinks not owned by 'root', upcased
42 ;; chmod u-x *(U*) : remove exec bit on all executables owned by user
44 ;; See the zsh docs for more on the syntax ([(zsh.info)Filename
49 (eval-when-compile (require 'eshell
))
53 (defgroup eshell-pred nil
54 "This module allows for predicates to be applied to globbing
55 patterns (similar to zsh), in addition to string modifiers which can
56 be applied either to globbing results, variable references, or just
58 :tag
"Value modifiers and predicates"
59 :group
'eshell-module
))
63 (defcustom eshell-pred-load-hook nil
64 "A list of functions to run when `eshell-pred' is loaded."
65 :version
"24.1" ; removed eshell-pred-initialize
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)))))
94 (?
* .
#'(lambda (file)
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
)))
121 (?L .
#'(lambda (lst) (mapcar 'downcase lst
)))
122 (?U .
#'(lambda (lst) (mapcar 'upcase lst
)))
123 (?C .
#'(lambda (lst) (mapcar 'capitalize lst
)))
124 (?h .
#'(lambda (lst) (mapcar 'file-name-directory lst
)))
125 (?i .
(eshell-include-members))
126 (?x .
(eshell-include-members t
))
127 (?r .
#'(lambda (lst) (mapcar 'file-name-sans-extension lst
)))
128 (?e .
#'(lambda (lst) (mapcar 'file-name-extension lst
)))
129 (?t .
#'(lambda (lst) (mapcar 'file-name-nondirectory lst
)))
130 (?q .
#'(lambda (lst) (mapcar 'eshell-escape-arg lst
)))
131 (?u .
#'(lambda (lst) (eshell-uniqify-list lst
)))
132 (?o .
#'(lambda (lst) (sort lst
'string-lessp
)))
133 (?O .
#'(lambda (lst) (nreverse (sort lst
'string-lessp
))))
134 (?j .
(eshell-join-members))
135 (?S .
(eshell-split-members))
139 (if (eq (char-before) ?s
)
140 (eshell-pred-substitute t
)
141 (error "`g' modifier cannot be used alone"))))
142 (?s .
(eshell-pred-substitute)))
143 "A list of modifiers than can be applied to an argument expansion.
144 The format of each entry is
146 (CHAR ENTRYWISE-P MODIFIER-FUNC-SEXP)"
147 :type
'(repeat (cons character sexp
))
150 (put 'eshell-modifier-alist
'risky-local-variable t
)
152 (defvar eshell-predicate-help-string
153 "Eshell predicate quick reference:
155 - follow symbolic references for predicates after the `-'
156 ^ invert sense of predicates after the `^'
159 / directories s sockets
160 . regular files p named pipes
161 * executable (files only) @ symbolic links
163 %x file type == `x' (as by ls -l; so `c' = char device, etc.)
165 PERMISSION BITS (for owner/group/world):
166 r/A/R readable s setuid
167 w/I/W writable S setgid
168 x/E/X executable t sticky bit
171 U owned by effective uid
172 u(UID|'user') owned by UID/user
173 g(GID|'group') owned by GID/group
177 a[Mwhms][+-](N|'FILE') access time +/-/= N months/weeks/hours/mins/secs
178 (days if unspecified) if FILE specified,
179 use as comparison basis; so a+'file.c'
180 shows files accessed before file.c was
182 m[Mwhms][+-](N|'FILE') modification time...
183 c[Mwhms][+-](N|'FILE') change time...
184 L[kmp][+-]N file size +/-/= N Kb/Mb/blocks
187 *(^@) all non-dot files which are not symlinks
188 .#*(^@) all files which are not symbolic links
189 **/.#*(*) all executable files, searched recursively
190 ***/*~f*(-/) recursively (though not traversing symlinks),
191 find all directories (or symlinks referring to
192 directories) whose names do not begin with f.
193 e*(*Lk+50) executables 50k or larger beginning with 'e'")
195 (defvar eshell-modifier-help-string
196 "Eshell modifier quick reference:
198 FOR SINGLE ARGUMENTS, or each argument of a list of strings:
206 r strip file extension
207 q escape special characters
209 S split string at any whitespace character
210 S/PAT/ split string at each occurrence of PAT
212 FOR LISTS OF ARGUMENTS:
213 o sort alphabetically
214 O reverse sort alphabetically
215 u uniq list (typically used after :o or :O)
218 j join list members, separated by a space
219 j/PAT/ join list members, separated by PAT
220 i/PAT/ exclude all members not matching PAT
221 x/PAT/ exclude all members matching PAT
223 s/pat/match/ substitute PAT with MATCH
224 g/pat/match/ substitute PAT with MATCH for all occurrences
227 *.c(:o) sorted list of .c files")
231 (defun eshell-display-predicate-help ()
236 (insert eshell-predicate-help-string
)))))
238 (defun eshell-display-modifier-help ()
243 (insert eshell-modifier-help-string
)))))
245 (defun eshell-pred-initialize ()
246 "Initialize the predicate/modifier code."
247 (add-hook 'eshell-parse-argument-hook
248 'eshell-parse-arg-modifier t t
)
249 (define-key eshell-command-map
[(meta ?q
)] 'eshell-display-predicate-help
)
250 (define-key eshell-command-map
[(meta ?m
)] 'eshell-display-modifier-help
))
252 (defun eshell-apply-modifiers (lst predicates modifiers
)
253 "Apply to LIST a series of PREDICATES and MODIFIERS."
259 (setq lst
(eshell-winnow-list lst nil predicates
))
261 (setq lst
(funcall (car modifiers
) lst
)
262 modifiers
(cdr modifiers
)))
268 (defun eshell-parse-arg-modifier ()
269 "Parse a modifier that has been specified after an argument.
270 This function is specially for adding onto `eshell-parse-argument-hook'."
271 (when (eq (char-after) ?\
()
273 (let ((end (eshell-find-delimiter ?\
( ?\
))))
275 (throw 'eshell-incomplete ?\
()
276 (when (eshell-arg-delimiter (1+ end
))
278 (narrow-to-region (point) end
)
279 (let* ((modifiers (eshell-parse-modifiers))
280 (preds (car modifiers
))
281 (mods (cdr modifiers
)))
283 ;; has to go at the end, which is only natural since
284 ;; syntactically it can only occur at the end
285 (setq eshell-current-modifiers
287 eshell-current-modifiers
290 (eshell-apply-modifiers
291 lst
(quote ,preds
) (quote ,mods
)))))))))
293 (eshell-finish-arg))))))
295 (defun eshell-parse-modifiers ()
296 "Parse value modifiers and predicates at point.
297 If ALLOW-PREDS is non-nil, predicates will be parsed as well.
298 Return a cons cell of the form
300 (PRED-FUNC-LIST . MOD-FUNC-LIST)
302 NEW-STRING is STRING minus any modifiers. PRED-FUNC-LIST is a list of
303 predicate functions. MOD-FUNC-LIST is a list of result modifier
304 functions. PRED-FUNCS take a filename and return t if the test
305 succeeds; MOD-FUNCS take any string and preform a modification,
306 returning the resultant string."
307 (let (result negate follow preds mods
)
310 (let ((char (char-after)))
314 (if (looking-at "[^|':]")
315 (let ((func (read (current-buffer))))
316 (if (and func
(functionp func
))
317 (setq preds
(eshell-add-pred-func func preds
319 (error "Invalid function predicate '%s'"
320 (eshell-stringify func
))))
321 (error "Invalid function predicate")))
324 (setq negate
(not negate
)))
327 (setq follow
(not follow
)))
330 (if (looking-at "[^|':]")
331 (let ((func (read (current-buffer))))
332 (if (and func
(functionp func
))
335 (mapcar (function ,func
) lst
))
337 (error "Invalid function modifier '%s'"
338 (eshell-stringify func
))))
339 (error "Invalid function modifier")))
342 (let ((mod (assq (char-after) eshell-modifier-alist
)))
344 (error "Unknown modifier character '%c'" (char-after))
346 (setq mods
(cons (eval (cdr mod
)) mods
)))))
348 (let ((pred (assq char eshell-predicate-alist
)))
350 (error "Unknown predicate character '%c'" char
)
353 (eshell-add-pred-func (eval (cdr pred
)) preds
354 negate follow
))))))))
356 (error "Predicate or modifier ended prematurely")))
357 (cons (nreverse preds
) (nreverse mods
))))
359 (defun eshell-add-pred-func (pred funcs negate follow
)
360 "Add the predicate function PRED to FUNCS."
362 (setq pred
`(lambda (file)
363 (not (funcall ,pred file
)))))
365 (setq pred
`(lambda (file)
366 (funcall ,pred
(file-truename file
)))))
369 (defun eshell-pred-user-or-group (mod-char mod-type attr-index get-id-func
)
370 "Return a predicate to test whether a file match a given user/group id."
371 (let (ugid open close end
)
372 (if (looking-at "[0-9]+")
374 (setq ugid
(string-to-number (match-string 0)))
375 (goto-char (match-end 0)))
376 (setq open
(char-after))
377 (if (setq close
(memq open
'(?\
( ?\
[ ?\
< ?\
{)))
378 (setq close
(car (last '(?\
) ?\
] ?\
> ?\
})
382 (setq end
(eshell-find-delimiter open close
))
384 (error "Malformed %s name string for modifier `%c'"
387 (funcall get-id-func
(buffer-substring (point) end
)))
388 (goto-char (1+ end
)))
390 (error "Unknown %s name specified for modifier `%c'"
393 (let ((attrs (file-attributes file
)))
395 (= (nth ,attr-index attrs
) ,ugid
))))))
397 (defun eshell-pred-file-time (mod-char mod-type attr-index
)
398 "Return a predicate to test whether a file matches a certain time."
399 (let* ((quantum 86400)
400 qual amount when open close end
)
401 (when (memq (char-after) '(?M ?w ?h ?m ?s
))
402 (setq quantum
(char-after))
405 (setq quantum
(* 60 60 24 30)))
407 (setq quantum
(* 60 60 24 7)))
409 (setq quantum
(* 60 60)))
415 (when (memq (char-after) '(?
+ ?-
))
416 (setq qual
(char-after))
418 (if (looking-at "[0-9]+")
420 (setq when
(- (float-time)
421 (* (string-to-number (match-string 0))
423 (goto-char (match-end 0)))
424 (setq open
(char-after))
425 (if (setq close
(memq open
'(?\
( ?\
[ ?\
< ?\
{)))
426 (setq close
(car (last '(?\
) ?\
] ?\
> ?\
})
430 (setq end
(eshell-find-delimiter open close
))
432 (error "Malformed %s time modifier `%c'" mod-type mod-char
))
433 (let* ((file (buffer-substring (point) end
))
434 (attrs (file-attributes file
)))
436 (error "Cannot stat file `%s'" file
))
437 (setq when
(float-time (nth attr-index attrs
))))
438 (goto-char (1+ end
)))
440 (let ((attrs (file-attributes file
)))
446 '=)) ,when
(float-time
447 (nth ,attr-index attrs
))))))))
449 (defun eshell-pred-file-type (type)
450 "Return a test which tests that the file is of a certain TYPE.
451 TYPE must be a character, and should be one of the possible options
452 that 'ls -l' will show in the first column of its display. "
454 (setq type
(char-after))
455 (if (memq type
'(?b ?c
))
459 (let ((attrs (eshell-file-attributes (directory-file-name file
))))
461 (memq (aref (nth 8 attrs
) 0)
464 (list 'quote
(list type
))))))))
466 (defsubst eshell-pred-file-mode
(mode)
467 "Return a test which tests that MODE pertains to the file."
469 (let ((modes (file-modes file
)))
471 (logand ,mode modes
)))))
473 (defun eshell-pred-file-links ()
474 "Return a predicate to test whether a file has a given number of links."
476 (when (memq (char-after) '(?- ?
+))
477 (setq qual
(char-after))
479 (unless (looking-at "[0-9]+")
480 (error "Invalid file link count modifier `l'"))
481 (setq amount
(string-to-number (match-string 0)))
482 (goto-char (match-end 0))
484 (let ((attrs (eshell-file-attributes file
)))
490 '=)) (nth 1 attrs
) ,amount
))))))
492 (defun eshell-pred-file-size ()
493 "Return a predicate to test whether a file is of a given size."
494 (let ((quantum 1) qual amount
)
495 (when (memq (downcase (char-after)) '(?k ?m ?p
))
496 (setq qual
(downcase (char-after)))
501 (setq quantum
(* 1024 1024)))
505 (when (memq (char-after) '(?- ?
+))
506 (setq qual
(char-after))
508 (unless (looking-at "[0-9]+")
509 (error "Invalid file size modifier `L'"))
510 (setq amount
(* (string-to-number (match-string 0)) quantum
))
511 (goto-char (match-end 0))
513 (let ((attrs (eshell-file-attributes file
)))
519 '=)) (nth 7 attrs
) ,amount
))))))
521 (defun eshell-pred-substitute (&optional repeat
)
522 "Return a modifier function that will substitute matches."
523 (let ((delim (char-after))
526 (setq end
(eshell-find-delimiter delim delim nil nil t
)
527 match
(buffer-substring-no-properties (point) end
))
529 (setq end
(eshell-find-delimiter delim delim nil nil t
)
530 replace
(buffer-substring-no-properties (point) end
))
538 (while (setq i
(string-match ,match str i
))
539 (setq str
(replace-match ,replace t nil str
))))
545 (if (string-match ,match str
)
546 (setq str
(replace-match ,replace t nil str
)))
549 (defun eshell-include-members (&optional invert-p
)
550 "Include only lisp members matching a regexp."
551 (let ((delim (char-after))
554 (setq end
(eshell-find-delimiter delim delim nil nil t
)
555 regexp
(buffer-substring-no-properties (point) end
))
559 lst nil
'((lambda (elem)
561 `(not (string-match ,regexp elem
))
562 `(string-match ,regexp elem
))))))))
564 (defun eshell-join-members ()
565 "Return a modifier function that join matches."
566 (let ((delim (char-after))
568 (if (not (memq delim
'(?
' ?
/)))
571 (setq end
(eshell-find-delimiter delim delim nil nil t
)
572 str
(buffer-substring-no-properties (point) end
))
573 (goto-char (1+ end
)))
575 (mapconcat 'identity lst
,str
))))
577 (defun eshell-split-members ()
578 "Return a modifier function that splits members."
579 (let ((delim (char-after))
581 (when (memq delim
'(?
' ?
/))
583 (setq end
(eshell-find-delimiter delim delim nil nil t
)
584 sep
(buffer-substring-no-properties (point) end
))
585 (goto-char (1+ end
)))
590 (split-string str
,sep
))) lst
))))
595 ;; generated-autoload-file: "esh-groups.el"
598 ;;; em-pred.el ends here