Sync to HEAD
[bpt/emacs.git] / lisp / eshell / em-pred.el
CommitLineData
60370d40 1;;; em-pred.el --- argument predicates and modifiers (ala zsh)
affbf647 2
faadfb0a 3;; Copyright (C) 1999, 2000 Free Software Foundation
affbf647 4
7de5b421
GM
5;; Author: John Wiegley <johnw@gnu.org>
6
affbf647
GM
7;; This file is part of GNU Emacs.
8
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)
12;; any later version.
13
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.
18
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.
23
24(provide 'em-pred)
25
26(eval-when-compile (require 'esh-maint))
27
28(defgroup eshell-pred nil
29 "This module allows for predicates to be applied to globbing
30patterns (similar to zsh), in addition to string modifiers which can
31be applied either to globbing results, variable references, or just
32ordinary strings."
33 :tag "Value modifiers and predicates"
34 :group 'eshell-module)
35
36;;; Commentary:
37
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.
41;;
42;; Argument modifiers are used to manipulate argument values. For
43;; example, sorting lists, upcasing words, substituting characters,
44;; etc.
45;;
46;; Here are some examples of how to use argument predication. Most of
47;; the predicates and modifiers are modeled after those provided by
48;; zsh.
49;;
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
57;;
58;; See the zsh docs for more on the syntax ([(zsh.info)Filename
59;; Generation]).
60
60370d40
PJ
61;;; Code:
62
affbf647
GM
63;;; User Variables:
64
65(defcustom eshell-pred-load-hook '(eshell-pred-initialize)
66 "*A list of functions to run when `eshell-pred' is loaded."
67 :type 'hook
68 :group 'eshell-pred)
69
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)))))
95 (?* . '(lambda (file)
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.
107The format of each entry is
108
109 (CHAR . PREDICATE-FUNC-SEXP)"
110 :type '(repeat (cons character sexp))
111 :group 'eshell-pred)
112
113(put 'eshell-predicate-alist 'risky-local-variable t)
114
115(defcustom eshell-modifier-alist
6b61353c 116 '((?E . '(lambda (lst)
affbf647
GM
117 (mapcar
118 (function
119 (lambda (str)
120 (eshell-stringify
121 (car (eshell-parse-argument str))))) lst)))
122 (?L . '(lambda (lst)
123 (mapcar 'downcase lst)))
124 (?U . '(lambda (lst)
125 (mapcar 'upcase lst)))
126 (?C . '(lambda (lst)
127 (mapcar 'capitalize lst)))
128 (?h . '(lambda (lst)
129 (mapcar 'file-name-directory lst)))
130 (?i . (eshell-include-members))
131 (?x . (eshell-include-members t))
132 (?r . '(lambda (lst)
133 (mapcar 'file-name-sans-extension lst)))
134 (?e . '(lambda (lst)
135 (mapcar 'file-name-extension lst)))
136 (?t . '(lambda (lst)
137 (mapcar 'file-name-nondirectory lst)))
138 (?q . '(lambda (lst)
139 (mapcar 'eshell-escape-arg lst)))
140 (?u . '(lambda (lst)
141 (eshell-uniqify-list lst)))
142 (?o . '(lambda (lst)
143 (sort lst 'string-lessp)))
144 (?O . '(lambda (lst)
145 (nreverse (sort lst 'string-lessp))))
146 (?j . (eshell-join-members))
147 (?S . (eshell-split-members))
148 (?R . 'reverse)
149 (?g . (progn
150 (forward-char)
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.
156The format of each entry is
157
158 (CHAR ENTRYWISE-P MODIFIER-FUNC-SEXP)"
159 :type '(repeat (cons character sexp))
160 :group 'eshell-pred)
161
162(put 'eshell-modifier-alist 'risky-local-variable t)
163
164(defvar eshell-predicate-help-string
165 "Eshell predicate quick reference:
166
167 - follow symbolic references for predicates after the `-'
168 ^ invert sense of predicates after the `^'
169
170FILE TYPE:
171 / directories s sockets
172 . regular files p named pipes
173 * executable (files only) @ symbolic links
174
175 %x file type == `x' (as by ls -l; so `c' = char device, etc.)
176
177PERMISSION 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
181
182OWNERSHIP:
183 U owned by effective uid
184 u(UID|'user') owned by UID/user
185 g(GID|'group') owned by GID/group
186
187FILE ATTRIBUTES:
188 l[+-]N +/-/= N links
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
196
197EXAMPLES:
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'")
205
206(defvar eshell-modifier-help-string
207 "Eshell modifier quick reference:
208
209FOR SINGLE ARGUMENTS, or each argument of a list of strings:
6b61353c 210 E evaluate again
affbf647
GM
211 L lowercase
212 U uppercase
213 C capitalize
214 h dirname
215 t basename
216 e file extension
217 r strip file extension
218 q escape special characters
219
220 S split string at any whitespace character
f9f196c6 221 S/PAT/ split string at each occurrence of PAT
affbf647
GM
222
223FOR LISTS OF ARGUMENTS:
224 o sort alphabetically
225 O reverse sort alphabetically
226 u uniq list (typically used after :o or :O)
227 R reverse list
228
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
233
234 s/pat/match/ substitute PAT with MATCH
f9f196c6 235 g/pat/match/ substitute PAT with MATCH for all occurrences
affbf647
GM
236
237EXAMPLES:
238 *.c(:o) sorted list of .c files")
239
240;;; Functions:
241
242(defun eshell-display-predicate-help ()
243 (interactive)
244 (with-electric-help
245 (function
246 (lambda ()
247 (insert eshell-predicate-help-string)))))
248
249(defun eshell-display-modifier-help ()
250 (interactive)
251 (with-electric-help
252 (function
253 (lambda ()
254 (insert eshell-modifier-help-string)))))
255
256(defun eshell-pred-initialize ()
257 "Initialize the predicate/modifier code."
affbf647
GM
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))
262
263(defun eshell-apply-modifiers (lst predicates modifiers)
264 "Apply to LIST a series of PREDICATES and MODIFIERS."
265 (let (stringified)
266 (if (stringp lst)
267 (setq lst (list lst)
268 stringified t))
269 (when (listp lst)
270 (setq lst (eshell-winnow-list lst nil predicates))
271 (while modifiers
272 (setq lst (funcall (car modifiers) lst)
273 modifiers (cdr modifiers)))
274 (if (and stringified
275 (= (length lst) 1))
276 (car lst)
277 lst))))
278
279(defun eshell-parse-arg-modifier ()
280 "Parse a modifier that has been specified after an argument.
281This function is specially for adding onto `eshell-parse-argument-hook'."
282 (when (eq (char-after) ?\()
283 (forward-char)
284 (let ((end (eshell-find-delimiter ?\( ?\))))
285 (if (not end)
286 (throw 'eshell-incomplete ?\()
287 (when (eshell-arg-delimiter (1+ end))
288 (save-restriction
289 (narrow-to-region (point) end)
290 (let* ((modifiers (eshell-parse-modifiers))
291 (preds (car modifiers))
292 (mods (cdr modifiers)))
293 (if (or preds mods)
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
297 (append
298 eshell-current-modifiers
299 (list
300 `(lambda (lst)
301 (eshell-apply-modifiers
302 lst (quote ,preds) (quote ,mods)))))))))
303 (goto-char (1+ end))
304 (eshell-finish-arg))))))
305
306(defun eshell-parse-modifiers ()
307 "Parse value modifiers and predicates at point.
308If ALLOW-PREDS is non-nil, predicates will be parsed as well.
309Return a cons cell of the form
310
311 (PRED-FUNC-LIST . MOD-FUNC-LIST)
312
313NEW-STRING is STRING minus any modifiers. PRED-FUNC-LIST is a list of
314predicate functions. MOD-FUNC-LIST is a list of result modifier
315functions. PRED-FUNCS take a filename and return t if the test
316succeeds; MOD-FUNCS take any string and preform a modification,
317returning the resultant string."
318 (let (result negate follow preds mods)
319 (condition-case err
320 (while (not (eobp))
321 (let ((char (char-after)))
322 (cond
323 ((eq char ?')
324 (forward-char)
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
329 negate follow))
330 (error "Invalid function predicate '%s'"
331 (eshell-stringify func))))
332 (error "Invalid function predicate")))
333 ((eq char ?^)
334 (forward-char)
335 (setq negate (not negate)))
336 ((eq char ?-)
337 (forward-char)
338 (setq follow (not follow)))
339 ((eq char ?|)
340 (forward-char)
341 (if (looking-at "[^|':]")
342 (let ((func (read (current-buffer))))
343 (if (and func (functionp func))
344 (setq mods
345 (cons `(lambda (lst)
346 (mapcar (function ,func) lst))
347 mods))
348 (error "Invalid function modifier '%s'"
349 (eshell-stringify func))))
350 (error "Invalid function modifier")))
351 ((eq char ?:)
352 (forward-char)
353 (let ((mod (assq (char-after) eshell-modifier-alist)))
354 (if (not mod)
355 (error "Unknown modifier character '%c'" (char-after))
356 (forward-char)
357 (setq mods (cons (eval (cdr mod)) mods)))))
358 (t
359 (let ((pred (assq char eshell-predicate-alist)))
360 (if (not pred)
361 (error "Unknown predicate character '%c'" char)
362 (forward-char)
363 (setq preds
364 (eshell-add-pred-func (eval (cdr pred)) preds
365 negate follow))))))))
366 (end-of-buffer
367 (error "Predicate or modifier ended prematurely")))
368 (cons (nreverse preds) (nreverse mods))))
369
370(defun eshell-add-pred-func (pred funcs negate follow)
371 "Add the predicate function PRED to FUNCS."
372 (if negate
373 (setq pred `(lambda (file)
374 (not (funcall ,pred file)))))
375 (if follow
376 (setq pred `(lambda (file)
377 (funcall ,pred (file-truename file)))))
378 (cons pred funcs))
379
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]+")
384 (progn
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 '(?\) ?\] ?\> ?\})
390 (length close))))
391 (setq close open))
392 (forward-char)
393 (setq end (eshell-find-delimiter open close))
394 (unless end
395 (error "Malformed %s name string for modifier `%c'"
396 mod-type mod-char))
397 (setq ugid
398 (funcall get-id-func (buffer-substring (point) end)))
399 (goto-char (1+ end)))
400 (unless ugid
401 (error "Unknown %s name specified for modifier `%c'"
402 mod-type mod-char))
403 `(lambda (file)
404 (let ((attrs (file-attributes file)))
405 (if attrs
406 (= (nth ,attr-index attrs) ,ugid))))))
407
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))
414 (cond
415 ((eq quantum ?M)
416 (setq quantum (* 60 60 24 30)))
417 ((eq quantum ?w)
418 (setq quantum (* 60 60 24 7)))
419 ((eq quantum ?h)
420 (setq quantum (* 60 60)))
421 ((eq quantum ?m)
422 (setq quantum 60))
423 ((eq quantum ?s)
424 (setq quantum 1)))
425 (forward-char))
426 (when (memq (char-after) '(?+ ?-))
427 (setq qual (char-after))
428 (forward-char))
429 (if (looking-at "[0-9]+")
430 (progn
431 (setq when (- (eshell-time-to-seconds (current-time))
432 (* (string-to-number (match-string 0))
433 quantum)))
434 (goto-char (match-end 0)))
435 (setq open (char-after))
436 (if (setq close (memq open '(?\( ?\[ ?\< ?\{)))
437 (setq close (car (last '(?\) ?\] ?\> ?\})
438 (length close))))
439 (setq close open))
440 (forward-char)
441 (setq end (eshell-find-delimiter open close))
442 (unless end
443 (error "Malformed %s time modifier `%c'" mod-type mod-char))
444 (let* ((file (buffer-substring (point) end))
445 (attrs (file-attributes file)))
446 (unless attrs
447 (error "Cannot stat file `%s'" file))
448 (setq when (eshell-time-to-seconds (nth attr-index attrs))))
449 (goto-char (1+ end)))
450 `(lambda (file)
451 (let ((attrs (file-attributes file)))
452 (if attrs
453 (,(if (eq qual ?-)
454 '<
455 (if (eq qual ?+)
456 '>
457 '=)) ,when (eshell-time-to-seconds
458 (nth ,attr-index attrs))))))))
459
460(defun eshell-pred-file-type (type)
461 "Return a test which tests that the file is of a certain TYPE.
462TYPE must be a character, and should be one of the possible options
463that 'ls -l' will show in the first column of its display. "
464 (when (eq type ?%)
465 (setq type (char-after))
466 (if (memq type '(?b ?c))
467 (forward-char)
468 (setq type ?%)))
469 `(lambda (file)
8c6b1d83 470 (let ((attrs (eshell-file-attributes (directory-file-name file))))
affbf647
GM
471 (if attrs
472 (memq (aref (nth 8 attrs) 0)
473 ,(if (eq type ?%)
474 '(?b ?c)
475 (list 'quote (list type))))))))
476
477(defsubst eshell-pred-file-mode (mode)
478 "Return a test which tests that MODE pertains to the file."
479 `(lambda (file)
480 (let ((modes (file-modes file)))
481 (if modes
482 (logand ,mode modes)))))
483
484(defun eshell-pred-file-links ()
485 "Return a predicate to test whether a file has a given number of links."
486 (let (qual amount)
487 (when (memq (char-after) '(?- ?+))
488 (setq qual (char-after))
489 (forward-char))
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))
494 `(lambda (file)
8c6b1d83 495 (let ((attrs (eshell-file-attributes file)))
affbf647
GM
496 (if attrs
497 (,(if (eq qual ?-)
498 '<
499 (if (eq qual ?+)
500 '>
501 '=)) (nth 1 attrs) ,amount))))))
502
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)))
508 (cond
509 ((eq qual ?k)
510 (setq quantum 1024))
511 ((eq qual ?m)
512 (setq quantum (* 1024 1024)))
513 ((eq qual ?p)
514 (setq quantum 512)))
515 (forward-char))
516 (when (memq (char-after) '(?- ?+))
517 (setq qual (char-after))
518 (forward-char))
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))
523 `(lambda (file)
8c6b1d83 524 (let ((attrs (eshell-file-attributes file)))
affbf647
GM
525 (if attrs
526 (,(if (eq qual ?-)
527 '<
528 (if (eq qual ?+)
529 '>
530 '=)) (nth 7 attrs) ,amount))))))
531
532(defun eshell-pred-substitute (&optional repeat)
533 "Return a modifier function that will substitute matches."
534 (let ((delim (char-after))
535 match replace end)
536 (forward-char)
537 (setq end (eshell-find-delimiter delim delim nil nil t)
538 match (buffer-substring-no-properties (point) end))
539 (goto-char (1+ end))
540 (setq end (eshell-find-delimiter delim delim nil nil t)
541 replace (buffer-substring-no-properties (point) end))
542 (goto-char (1+ end))
543 (if repeat
544 `(lambda (lst)
545 (mapcar
546 (function
547 (lambda (str)
548 (let ((i 0))
549 (while (setq i (string-match ,match str i))
550 (setq str (replace-match ,replace t nil str))))
551 str)) lst))
552 `(lambda (lst)
553 (mapcar
554 (function
555 (lambda (str)
556 (if (string-match ,match str)
557 (setq str (replace-match ,replace t nil str)))
558 str)) lst)))))
559
560(defun eshell-include-members (&optional invert-p)
561 "Include only lisp members matching a regexp."
562 (let ((delim (char-after))
563 regexp end)
564 (forward-char)
565 (setq end (eshell-find-delimiter delim delim nil nil t)
566 regexp (buffer-substring-no-properties (point) end))
567 (goto-char (1+ end))
568 `(lambda (lst)
569 (eshell-winnow-list
570 lst nil '((lambda (elem)
571 ,(if invert-p
572 `(not (string-match ,regexp elem))
573 `(string-match ,regexp elem))))))))
574
575(defun eshell-join-members ()
576 "Return a modifier function that join matches."
577 (let ((delim (char-after))
578 str end)
579 (if (not (memq delim '(?' ?/)))
580 (setq delim " ")
581 (forward-char)
582 (setq end (eshell-find-delimiter delim delim nil nil t)
583 str (buffer-substring-no-properties (point) end))
584 (goto-char (1+ end)))
585 `(lambda (lst)
586 (mapconcat 'identity lst ,str))))
587
588(defun eshell-split-members ()
589 "Return a modifier function that splits members."
590 (let ((delim (char-after))
591 sep end)
592 (when (memq delim '(?' ?/))
593 (forward-char)
594 (setq end (eshell-find-delimiter delim delim nil nil t)
595 sep (buffer-substring-no-properties (point) end))
596 (goto-char (1+ end)))
597 `(lambda (lst)
598 (mapcar
599 (function
600 (lambda (str)
601 (split-string str ,sep))) lst))))
602
6b61353c 603;;; arch-tag: 8b5ce022-17f3-4c40-93c7-5faafaa63f31
affbf647 604;;; em-pred.el ends here