Fix up comment convention on the arch-tag lines.
[bpt/emacs.git] / lisp / eshell / em-pred.el
1 ;;; em-pred.el --- argument predicates and modifiers (ala zsh)
2
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: John Wiegley <johnw@gnu.org>
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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.
24
25 ;;; Commentary:
26
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.
30 ;;
31 ;; Argument modifiers are used to manipulate argument values. For
32 ;; example, sorting lists, upcasing words, substituting characters,
33 ;; etc.
34 ;;
35 ;; Here are some examples of how to use argument predication. Most of
36 ;; the predicates and modifiers are modeled after those provided by
37 ;; zsh.
38 ;;
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
46 ;;
47 ;; See the zsh docs for more on the syntax ([(zsh.info)Filename
48 ;; Generation]).
49
50 ;;; Code:
51
52 (eval-when-compile (require 'eshell))
53
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
58 ordinary strings."
59 :tag "Value modifiers and predicates"
60 :group 'eshell-module)
61
62 ;;; User Variables:
63
64 (defcustom eshell-pred-load-hook '(eshell-pred-initialize)
65 "*A list of functions to run when `eshell-pred' is loaded."
66 :type 'hook
67 :group 'eshell-pred)
68
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
107
108 (CHAR . PREDICATE-FUNC-SEXP)"
109 :type '(repeat (cons character sexp))
110 :group 'eshell-pred)
111
112 (put 'eshell-predicate-alist 'risky-local-variable t)
113
114 (defcustom eshell-modifier-alist
115 '((?E . '(lambda (lst)
116 (mapcar
117 (function
118 (lambda (str)
119 (eshell-stringify
120 (car (eshell-parse-argument str))))) lst)))
121 (?L . '(lambda (lst)
122 (mapcar 'downcase lst)))
123 (?U . '(lambda (lst)
124 (mapcar 'upcase lst)))
125 (?C . '(lambda (lst)
126 (mapcar 'capitalize lst)))
127 (?h . '(lambda (lst)
128 (mapcar 'file-name-directory lst)))
129 (?i . (eshell-include-members))
130 (?x . (eshell-include-members t))
131 (?r . '(lambda (lst)
132 (mapcar 'file-name-sans-extension lst)))
133 (?e . '(lambda (lst)
134 (mapcar 'file-name-extension lst)))
135 (?t . '(lambda (lst)
136 (mapcar 'file-name-nondirectory lst)))
137 (?q . '(lambda (lst)
138 (mapcar 'eshell-escape-arg lst)))
139 (?u . '(lambda (lst)
140 (eshell-uniqify-list lst)))
141 (?o . '(lambda (lst)
142 (sort lst 'string-lessp)))
143 (?O . '(lambda (lst)
144 (nreverse (sort lst 'string-lessp))))
145 (?j . (eshell-join-members))
146 (?S . (eshell-split-members))
147 (?R . 'reverse)
148 (?g . (progn
149 (forward-char)
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
156
157 (CHAR ENTRYWISE-P MODIFIER-FUNC-SEXP)"
158 :type '(repeat (cons character sexp))
159 :group 'eshell-pred)
160
161 (put 'eshell-modifier-alist 'risky-local-variable t)
162
163 (defvar eshell-predicate-help-string
164 "Eshell predicate quick reference:
165
166 - follow symbolic references for predicates after the `-'
167 ^ invert sense of predicates after the `^'
168
169 FILE TYPE:
170 / directories s sockets
171 . regular files p named pipes
172 * executable (files only) @ symbolic links
173
174 %x file type == `x' (as by ls -l; so `c' = char device, etc.)
175
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
180
181 OWNERSHIP:
182 U owned by effective uid
183 u(UID|'user') owned by UID/user
184 g(GID|'group') owned by GID/group
185
186 FILE ATTRIBUTES:
187 l[+-]N +/-/= N links
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
192 last accessed
193 m[Mwhms][+-](N|'FILE') modification time...
194 c[Mwhms][+-](N|'FILE') change time...
195 L[kmp][+-]N file size +/-/= N Kb/Mb/blocks
196
197 EXAMPLES:
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
209 FOR SINGLE ARGUMENTS, or each argument of a list of strings:
210 E evaluate again
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
221 S/PAT/ split string at each occurrence of PAT
222
223 FOR 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
235 g/pat/match/ substitute PAT with MATCH for all occurrences
236
237 EXAMPLES:
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."
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.
281 This 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.
308 If ALLOW-PREDS is non-nil, predicates will be parsed as well.
309 Return a cons cell of the form
310
311 (PRED-FUNC-LIST . MOD-FUNC-LIST)
312
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)
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 ?s))
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.
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. "
464 (when (eq type ?%)
465 (setq type (char-after))
466 (if (memq type '(?b ?c))
467 (forward-char)
468 (setq type ?%)))
469 `(lambda (file)
470 (let ((attrs (eshell-file-attributes (directory-file-name file))))
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)
495 (let ((attrs (eshell-file-attributes file)))
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)
524 (let ((attrs (eshell-file-attributes file)))
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
603 (provide 'em-pred)
604
605 ;; arch-tag: 8b5ce022-17f3-4c40-93c7-5faafaa63f31
606 ;;; em-pred.el ends here