annotate lisp/dired/dired-trns.el @ 0:376386a54a3c r19-14

Import from CVS: tag r19-14
author cvs
date Mon, 13 Aug 2007 08:45:50 +0200
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;; dired-trns.el - file transformers for dired shell commands.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 ;; Id: dired-trns.el,v 1.6 1991/07/05 13:36:01 sk RelBeta
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;; Code contributed by Hans Chalupsky <hans@cs.Buffalo.EDU>.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ;; Integrated with my dired.el sk@sparc0 11-Jan-1991 14:38.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;; And hacked up a bit.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 ;; LISPDIR ENTRY for the Elisp Archive ===============================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 ;; LCD Archive Entry:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 ;; dired-trns|Hans Chalupsky|hans@cs.Buffalo.EDU
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;; |Filename Transformation for Tree Dired Shell Commands
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;; |Date: 1991/07/05 13:36:01 |Revision: 1.6 |
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 ;; INSTALLATION ======================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 ;; Put this file into your load-path and add (load "dired-trns") to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ;; your dired-load-hook, e.g.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 ;; (setq dired-load-hook '(lambda ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 ;; ;; possibly more statements here
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 ;; (load "dired-trns")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 ;; Transformers are functions that take a file (a string) as an argument
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 ;; and transform it into some other string (e.g., a filename without an
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 ;; extension).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 ;; Each transformer is associated with a dispatch character. The associations
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 ;; are stored in a keymap for fast and easy lookup. The dispatch character
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ;; is used to activate the associated transformer function at a particular
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 ;; position in a shell command issued in dired.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 ;; Transformers can be used to construct complicated shell commands that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 ;; operate on a large number of files, for example, they allow to create
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 ;; functionality such as "mv *.lsp *.lisp" where each .lsp file is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 ;; renamed into a a file with same name but new extension .lisp.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 (defvar dired-trans-map (make-keymap)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 "Array that associates keys with file transformer functions")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 (defmacro dired-trans-define (char &rest body)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 "Macro that assigns the transformer function (lambda (file) BODY) to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 CHAR (a character or string). BODY must return a string (the transformed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 file or whatever. This macro allows easy definition of user specific
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 transformation functions."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 (if (not (stringp char)) (setq char (char-to-string char)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 (list 'define-key 'dired-trans-map char
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 (list 'function (append '(lambda (file)) body))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 (defun dired-trans-run (transformers file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 "Applies each transformer supplied in the string TRANSFORMERS in sequence
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 to FILE and returns the concatenation of the results."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 (mapconcat (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 (lambda (transformer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 (setq transformer (char-to-string transformer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 (funcall (or (lookup-key dired-trans-map transformer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 (error "Undefined transfomer: %s" transformer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 file)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 transformers nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 (defvar dired-trans-re-ext "\\.[^.]*\\(\\.\\(\\(g?z\\)\\|Z\\)\\)?$"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 "The part of a filename matching this regexp will be viewed as extension")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 (defun dired-trans-init ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 "Defines a basic set of useful transformers.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 * is a noop that returns the unmodified filename (equivalent to [dbe]).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 n returns the Name component of a filename without directory information
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 d returns the Directory component of a filename
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 b returns the Basename of a filename, i.e., the name of the file without
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 directory and extension (see dired-trans-re-ext)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 A basename with directory component can be obtained by [db].
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 e returns the Extension of a filename (i.e., whatever
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 dired-trans-re-ext splits off)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 v returns a file without directory and without ,v suffixes if any.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 z returns a file without directory and without .Z .z .gz suffixes if any."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 (dired-trans-define
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 "*" file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 (dired-trans-define
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 "n" (or (file-name-nondirectory file) ""))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 (dired-trans-define
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 "d" (or (file-name-directory file) ""))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 (dired-trans-define
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 "b" (setq file (dired-trans-run "n" file))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 (substring file 0 (string-match dired-trans-re-ext file)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 (dired-trans-define
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 "e" (let ((e (string-match dired-trans-re-ext file)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 (if e
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 (substring file e)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 "")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 (dired-trans-define
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 "v" (setq file (dired-trans-run "n" file))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 (substring file 0 (string-match ",v$" file)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 (dired-trans-define
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 "z" (setq file (dired-trans-run "n" file))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 (substring file 0 (string-match "\\.\\(\\(g?z\\)\\|Z\\)$" file)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 (dired-trans-init)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 (defun dired-trans-mklist (files &optional transformers)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 "Takes a list of FILES and applies the sequence of TRANSFORMERS to each
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 of them. The transformed results are concatenated, separated by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 dired-mark-separator, prefixed by dired-mark-prefix and postfixed by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 dired-mark-postfix to generate a file list suitable for a particular shell."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (if (not (consp files))(setq files (list files)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 (if (null transformers) (setq transformers "*"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 (let ((file-list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 (mapconcat (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 (lambda (file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 (shell-quote
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 (dired-trans-run transformers file))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 files dired-mark-separator)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 (if (> (length files) 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 (concat dired-mark-prefix file-list dired-mark-postfix)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 file-list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 ;; By default, transformations can be specified like this:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 ;; [db] or [dv] or #z# or #dbe# or #dbe (blank at the end).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 (defvar dired-trans-starters "[#[]"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 "User definable set of characters to be used to indicate the start of a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 transformer sequence")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 (defvar dired-trans-enders "[]# ]"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 "User definable set of characters to be used to indicate the end of a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 transformer sequence")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 (defun dired-trans-expand (command files)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 "Takes a shell COMMAND and a list of FILES and substitutes each occurance
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 of a transformer sequence by an accordingly transformed file list. Special
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 characters such as [,] or * can be quoted with a backslash."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 (let ((quoted nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 (collect-transformers nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 (transformers ""))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 (mapconcat (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 (lambda (char)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 (setq char (char-to-string char))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 (cond (quoted (setq quoted nil) char)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 ((equal char "\\") (setq quoted t) nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 (collect-transformers
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 (cond ((string-match dired-trans-enders char)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 (setq collect-transformers nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 (prog1 (dired-trans-mklist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 files transformers)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 (setq transformers "")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 (t (setq transformers
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 (concat transformers char))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 ((string-match dired-trans-starters char)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 (setq collect-transformers t) nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 ;; for compatibility and as a special case that should
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 ;; not be redefinable by the user (used below)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 ((equal char "*")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 (dired-trans-mklist files "*"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 (t char))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 command nil)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 (defun dired-trans-make (command files &optional all-at-once)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 "Takes a shell COMMAND and a list of FILES and returns a command operating
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 on the list of files (transformed if COMMAND contains transformers). If
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 ALL-AT-ONCE is t the resulting command will be of the form
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 cmd file1 file2 ... fileN
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 otherwise it will be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 cmd file1; cmd file2; ... cmd fileN;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 Both examples assume a single reference to the file list."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 (let (fns expanded-command)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 (cond (all-at-once
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 (setq expanded-command (dired-trans-expand command files))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 (if (equal command expanded-command)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 (concat command (dired-trans-expand " *" files))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 expanded-command))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 (t (mapconcat
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 (function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 (lambda (file)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 (dired-trans-make command file t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 files ";")))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 ;; Redefine this function from dired.el:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 (defun dired-shell-stuff-it (command file-list on-each &optional raw-arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 "Make up a shell command line from COMMAND and FILE-LIST.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 If ON-EACH is t, COMMAND should be applied to each file, else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 simply concat all files.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 The list of marked files is appended to the command string unless asterisks
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 `*' or transformer sequences enclosed in `[]' indicate the place(s) where
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 the (transformed) list should go. See documentation of function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 dired-trans-init for a list of transformers.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 With a zero argument the resulting command will be of the form
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 cmd file1; cmd file2; ... cmd fileN assuming only one reference to the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 file list. E.g., to rename all .lsp files into .lisp files mark all the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 .lsp files and issue the command `mv * [db].lisp' ."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 (dired-trans-make command file-list (not on-each)))