(latexenc-find-file-coding-system): Don't inherit the EOL part of the
[emacs.git] / lisp / eshell / em-pred.el
blobbbb82fc7cd8424c1299b0c21e36b15c06395f8c8
1 ;;; em-pred.el --- argument predicates and modifiers (ala zsh)
3 ;; Copyright (C) 1999, 2000 Free Software Foundation
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 2, or (at your option)
12 ;; 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; 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.
24 (provide 'em-pred)
26 (eval-when-compile (require 'esh-maint))
28 (defgroup eshell-pred nil
29 "This module allows for predicates to be applied to globbing
30 patterns (similar to zsh), in addition to string modifiers which can
31 be applied either to globbing results, variable references, or just
32 ordinary strings."
33 :tag "Value modifiers and predicates"
34 :group 'eshell-module)
36 ;;; Commentary:
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.
42 ;; Argument modifiers are used to manipulate argument values. For
43 ;; example, sorting lists, upcasing words, substituting characters,
44 ;; etc.
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.
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
58 ;; See the zsh docs for more on the syntax ([(zsh.info)Filename
59 ;; Generation]).
61 ;;; Code:
63 ;;; User Variables:
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)
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.
107 The format of each entry is
109 (CHAR . PREDICATE-FUNC-SEXP)"
110 :type '(repeat (cons character sexp))
111 :group 'eshell-pred)
113 (put 'eshell-predicate-alist 'risky-local-variable t)
115 (defcustom eshell-modifier-alist
116 '((?E . '(lambda (lst)
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.
156 The format of each entry is
158 (CHAR ENTRYWISE-P MODIFIER-FUNC-SEXP)"
159 :type '(repeat (cons character sexp))
160 :group 'eshell-pred)
162 (put 'eshell-modifier-alist 'risky-local-variable t)
164 (defvar eshell-predicate-help-string
165 "Eshell predicate quick reference:
167 - follow symbolic references for predicates after the `-'
168 ^ invert sense of predicates after the `^'
170 FILE TYPE:
171 / directories s sockets
172 . regular files p named pipes
173 * executable (files only) @ symbolic links
175 %x file type == `x' (as by ls -l; so `c' = char device, etc.)
177 PERMISSION 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
182 OWNERSHIP:
183 U owned by effective uid
184 u(UID|'user') owned by UID/user
185 g(GID|'group') owned by GID/group
187 FILE ATTRIBUTES:
188 l[+-]N +/-/= N links
189 a[Mwhms][+-](N|'FILE') access time +/-/= N mnths/weeks/hours/mins/secs
190 (days if unspecified) if FILE specified,
191 use as comparison basis; so a+'file.c'
192 shows files accessed before file.c was
193 last accessed
194 m[Mwhms][+-](N|'FILE') modification time...
195 c[Mwhms][+-](N|'FILE') change time...
196 L[kmp][+-]N file size +/-/= N Kb/Mb/blocks
198 EXAMPLES:
199 *(^@) all non-dot files which are not symlinks
200 .#*(^@) all files which are not symbolic links
201 **/.#*(*) all executable files, searched recursively
202 ***/*~f*(-/) recursively (though not traversing symlinks),
203 find all directories (or symlinks referring to
204 directories) whose names do not begin with f.
205 e*(*Lk+50) executables 50k or larger beginning with 'e'")
207 (defvar eshell-modifier-help-string
208 "Eshell modifier quick reference:
210 FOR SINGLE ARGUMENTS, or each argument of a list of strings:
211 E evaluate again
212 L lowercase
213 U uppercase
214 C capitalize
215 h dirname
216 t basename
217 e file extension
218 r strip file extension
219 q escape special characters
221 S split string at any whitespace character
222 S/PAT/ split string at each occurrence of PAT
224 FOR LISTS OF ARGUMENTS:
225 o sort alphabetically
226 O reverse sort alphabetically
227 u uniq list (typically used after :o or :O)
228 R reverse list
230 j join list members, separated by a space
231 j/PAT/ join list members, separated by PAT
232 i/PAT/ exclude all members not matching PAT
233 x/PAT/ exclude all members matching PAT
235 s/pat/match/ substitute PAT with MATCH
236 g/pat/match/ substitute PAT with MATCH for all occurrences
238 EXAMPLES:
239 *.c(:o) sorted list of .c files")
241 ;;; Functions:
243 (defun eshell-display-predicate-help ()
244 (interactive)
245 (with-electric-help
246 (function
247 (lambda ()
248 (insert eshell-predicate-help-string)))))
250 (defun eshell-display-modifier-help ()
251 (interactive)
252 (with-electric-help
253 (function
254 (lambda ()
255 (insert eshell-modifier-help-string)))))
257 (defun eshell-pred-initialize ()
258 "Initialize the predicate/modifier code."
259 (add-hook 'eshell-parse-argument-hook
260 'eshell-parse-arg-modifier t t)
261 (define-key eshell-command-map [(meta ?q)] 'eshell-display-predicate-help)
262 (define-key eshell-command-map [(meta ?m)] 'eshell-display-modifier-help))
264 (defun eshell-apply-modifiers (lst predicates modifiers)
265 "Apply to LIST a series of PREDICATES and MODIFIERS."
266 (let (stringified)
267 (if (stringp lst)
268 (setq lst (list lst)
269 stringified t))
270 (when (listp lst)
271 (setq lst (eshell-winnow-list lst nil predicates))
272 (while modifiers
273 (setq lst (funcall (car modifiers) lst)
274 modifiers (cdr modifiers)))
275 (if (and stringified
276 (= (length lst) 1))
277 (car lst)
278 lst))))
280 (defun eshell-parse-arg-modifier ()
281 "Parse a modifier that has been specified after an argument.
282 This function is specially for adding onto `eshell-parse-argument-hook'."
283 (when (eq (char-after) ?\()
284 (forward-char)
285 (let ((end (eshell-find-delimiter ?\( ?\))))
286 (if (not end)
287 (throw 'eshell-incomplete ?\()
288 (when (eshell-arg-delimiter (1+ end))
289 (save-restriction
290 (narrow-to-region (point) end)
291 (let* ((modifiers (eshell-parse-modifiers))
292 (preds (car modifiers))
293 (mods (cdr modifiers)))
294 (if (or preds mods)
295 ;; has to go at the end, which is only natural since
296 ;; syntactically it can only occur at the end
297 (setq eshell-current-modifiers
298 (append
299 eshell-current-modifiers
300 (list
301 `(lambda (lst)
302 (eshell-apply-modifiers
303 lst (quote ,preds) (quote ,mods)))))))))
304 (goto-char (1+ end))
305 (eshell-finish-arg))))))
307 (defun eshell-parse-modifiers ()
308 "Parse value modifiers and predicates at point.
309 If ALLOW-PREDS is non-nil, predicates will be parsed as well.
310 Return a cons cell of the form
312 (PRED-FUNC-LIST . MOD-FUNC-LIST)
314 NEW-STRING is STRING minus any modifiers. PRED-FUNC-LIST is a list of
315 predicate functions. MOD-FUNC-LIST is a list of result modifier
316 functions. PRED-FUNCS take a filename and return t if the test
317 succeeds; MOD-FUNCS take any string and preform a modification,
318 returning the resultant string."
319 (let (result negate follow preds mods)
320 (condition-case err
321 (while (not (eobp))
322 (let ((char (char-after)))
323 (cond
324 ((eq char ?')
325 (forward-char)
326 (if (looking-at "[^|':]")
327 (let ((func (read (current-buffer))))
328 (if (and func (functionp func))
329 (setq preds (eshell-add-pred-func func preds
330 negate follow))
331 (error "Invalid function predicate '%s'"
332 (eshell-stringify func))))
333 (error "Invalid function predicate")))
334 ((eq char ?^)
335 (forward-char)
336 (setq negate (not negate)))
337 ((eq char ?-)
338 (forward-char)
339 (setq follow (not follow)))
340 ((eq char ?|)
341 (forward-char)
342 (if (looking-at "[^|':]")
343 (let ((func (read (current-buffer))))
344 (if (and func (functionp func))
345 (setq mods
346 (cons `(lambda (lst)
347 (mapcar (function ,func) lst))
348 mods))
349 (error "Invalid function modifier '%s'"
350 (eshell-stringify func))))
351 (error "Invalid function modifier")))
352 ((eq char ?:)
353 (forward-char)
354 (let ((mod (assq (char-after) eshell-modifier-alist)))
355 (if (not mod)
356 (error "Unknown modifier character '%c'" (char-after))
357 (forward-char)
358 (setq mods (cons (eval (cdr mod)) mods)))))
360 (let ((pred (assq char eshell-predicate-alist)))
361 (if (not pred)
362 (error "Unknown predicate character '%c'" char)
363 (forward-char)
364 (setq preds
365 (eshell-add-pred-func (eval (cdr pred)) preds
366 negate follow))))))))
367 (end-of-buffer
368 (error "Predicate or modifier ended prematurely")))
369 (cons (nreverse preds) (nreverse mods))))
371 (defun eshell-add-pred-func (pred funcs negate follow)
372 "Add the predicate function PRED to FUNCS."
373 (if negate
374 (setq pred `(lambda (file)
375 (not (funcall ,pred file)))))
376 (if follow
377 (setq pred `(lambda (file)
378 (funcall ,pred (file-truename file)))))
379 (cons pred funcs))
381 (defun eshell-pred-user-or-group (mod-char mod-type attr-index get-id-func)
382 "Return a predicate to test whether a file match a given user/group id."
383 (let (ugid open close end)
384 (if (looking-at "[0-9]+")
385 (progn
386 (setq ugid (string-to-number (match-string 0)))
387 (goto-char (match-end 0)))
388 (setq open (char-after))
389 (if (setq close (memq open '(?\( ?\[ ?\< ?\{)))
390 (setq close (car (last '(?\) ?\] ?\> ?\})
391 (length close))))
392 (setq close open))
393 (forward-char)
394 (setq end (eshell-find-delimiter open close))
395 (unless end
396 (error "Malformed %s name string for modifier `%c'"
397 mod-type mod-char))
398 (setq ugid
399 (funcall get-id-func (buffer-substring (point) end)))
400 (goto-char (1+ end)))
401 (unless ugid
402 (error "Unknown %s name specified for modifier `%c'"
403 mod-type mod-char))
404 `(lambda (file)
405 (let ((attrs (file-attributes file)))
406 (if attrs
407 (= (nth ,attr-index attrs) ,ugid))))))
409 (defun eshell-pred-file-time (mod-char mod-type attr-index)
410 "Return a predicate to test whether a file matches a certain time."
411 (let* ((quantum 86400)
412 qual amount when open close end)
413 (when (memq (char-after) '(?M ?w ?h ?m ?s))
414 (setq quantum (char-after))
415 (cond
416 ((eq quantum ?M)
417 (setq quantum (* 60 60 24 30)))
418 ((eq quantum ?w)
419 (setq quantum (* 60 60 24 7)))
420 ((eq quantum ?h)
421 (setq quantum (* 60 60)))
422 ((eq quantum ?m)
423 (setq quantum 60))
424 ((eq quantum ?s)
425 (setq quantum 1)))
426 (forward-char))
427 (when (memq (char-after) '(?+ ?-))
428 (setq qual (char-after))
429 (forward-char))
430 (if (looking-at "[0-9]+")
431 (progn
432 (setq when (- (eshell-time-to-seconds (current-time))
433 (* (string-to-number (match-string 0))
434 quantum)))
435 (goto-char (match-end 0)))
436 (setq open (char-after))
437 (if (setq close (memq open '(?\( ?\[ ?\< ?\{)))
438 (setq close (car (last '(?\) ?\] ?\> ?\})
439 (length close))))
440 (setq close open))
441 (forward-char)
442 (setq end (eshell-find-delimiter open close))
443 (unless end
444 (error "Malformed %s time modifier `%c'" mod-type mod-char))
445 (let* ((file (buffer-substring (point) end))
446 (attrs (file-attributes file)))
447 (unless attrs
448 (error "Cannot stat file `%s'" file))
449 (setq when (eshell-time-to-seconds (nth attr-index attrs))))
450 (goto-char (1+ end)))
451 `(lambda (file)
452 (let ((attrs (file-attributes file)))
453 (if attrs
454 (,(if (eq qual ?-)
456 (if (eq qual ?+)
458 '=)) ,when (eshell-time-to-seconds
459 (nth ,attr-index attrs))))))))
461 (defun eshell-pred-file-type (type)
462 "Return a test which tests that the file is of a certain TYPE.
463 TYPE must be a character, and should be one of the possible options
464 that 'ls -l' will show in the first column of its display. "
465 (when (eq type ?%)
466 (setq type (char-after))
467 (if (memq type '(?b ?c))
468 (forward-char)
469 (setq type ?%)))
470 `(lambda (file)
471 (let ((attrs (eshell-file-attributes (directory-file-name file))))
472 (if attrs
473 (memq (aref (nth 8 attrs) 0)
474 ,(if (eq type ?%)
475 '(?b ?c)
476 (list 'quote (list type))))))))
478 (defsubst eshell-pred-file-mode (mode)
479 "Return a test which tests that MODE pertains to the file."
480 `(lambda (file)
481 (let ((modes (file-modes file)))
482 (if modes
483 (logand ,mode modes)))))
485 (defun eshell-pred-file-links ()
486 "Return a predicate to test whether a file has a given number of links."
487 (let (qual amount)
488 (when (memq (char-after) '(?- ?+))
489 (setq qual (char-after))
490 (forward-char))
491 (unless (looking-at "[0-9]+")
492 (error "Invalid file link count modifier `l'"))
493 (setq amount (string-to-number (match-string 0)))
494 (goto-char (match-end 0))
495 `(lambda (file)
496 (let ((attrs (eshell-file-attributes file)))
497 (if attrs
498 (,(if (eq qual ?-)
500 (if (eq qual ?+)
502 '=)) (nth 1 attrs) ,amount))))))
504 (defun eshell-pred-file-size ()
505 "Return a predicate to test whether a file is of a given size."
506 (let ((quantum 1) qual amount)
507 (when (memq (downcase (char-after)) '(?k ?m ?p))
508 (setq qual (downcase (char-after)))
509 (cond
510 ((eq qual ?k)
511 (setq quantum 1024))
512 ((eq qual ?m)
513 (setq quantum (* 1024 1024)))
514 ((eq qual ?p)
515 (setq quantum 512)))
516 (forward-char))
517 (when (memq (char-after) '(?- ?+))
518 (setq qual (char-after))
519 (forward-char))
520 (unless (looking-at "[0-9]+")
521 (error "Invalid file size modifier `L'"))
522 (setq amount (* (string-to-number (match-string 0)) quantum))
523 (goto-char (match-end 0))
524 `(lambda (file)
525 (let ((attrs (eshell-file-attributes file)))
526 (if attrs
527 (,(if (eq qual ?-)
529 (if (eq qual ?+)
531 '=)) (nth 7 attrs) ,amount))))))
533 (defun eshell-pred-substitute (&optional repeat)
534 "Return a modifier function that will substitute matches."
535 (let ((delim (char-after))
536 match replace end)
537 (forward-char)
538 (setq end (eshell-find-delimiter delim delim nil nil t)
539 match (buffer-substring-no-properties (point) end))
540 (goto-char (1+ end))
541 (setq end (eshell-find-delimiter delim delim nil nil t)
542 replace (buffer-substring-no-properties (point) end))
543 (goto-char (1+ end))
544 (if repeat
545 `(lambda (lst)
546 (mapcar
547 (function
548 (lambda (str)
549 (let ((i 0))
550 (while (setq i (string-match ,match str i))
551 (setq str (replace-match ,replace t nil str))))
552 str)) lst))
553 `(lambda (lst)
554 (mapcar
555 (function
556 (lambda (str)
557 (if (string-match ,match str)
558 (setq str (replace-match ,replace t nil str)))
559 str)) lst)))))
561 (defun eshell-include-members (&optional invert-p)
562 "Include only lisp members matching a regexp."
563 (let ((delim (char-after))
564 regexp end)
565 (forward-char)
566 (setq end (eshell-find-delimiter delim delim nil nil t)
567 regexp (buffer-substring-no-properties (point) end))
568 (goto-char (1+ end))
569 `(lambda (lst)
570 (eshell-winnow-list
571 lst nil '((lambda (elem)
572 ,(if invert-p
573 `(not (string-match ,regexp elem))
574 `(string-match ,regexp elem))))))))
576 (defun eshell-join-members ()
577 "Return a modifier function that join matches."
578 (let ((delim (char-after))
579 str end)
580 (if (not (memq delim '(?' ?/)))
581 (setq delim " ")
582 (forward-char)
583 (setq end (eshell-find-delimiter delim delim nil nil t)
584 str (buffer-substring-no-properties (point) end))
585 (goto-char (1+ end)))
586 `(lambda (lst)
587 (mapconcat 'identity lst ,str))))
589 (defun eshell-split-members ()
590 "Return a modifier function that splits members."
591 (let ((delim (char-after))
592 sep end)
593 (when (memq delim '(?' ?/))
594 (forward-char)
595 (setq end (eshell-find-delimiter delim delim nil nil t)
596 sep (buffer-substring-no-properties (point) end))
597 (goto-char (1+ end)))
598 `(lambda (lst)
599 (mapcar
600 (function
601 (lambda (str)
602 (split-string str ,sep))) lst))))
604 ;;; arch-tag: 8b5ce022-17f3-4c40-93c7-5faafaa63f31
605 ;;; em-pred.el ends here