Mercurial > emacs
annotate lisp/wdired.el @ 61462:7296705f27fd
(message-make-date): Handle byte-compiler warnings differently.
author | Lute Kamstra <lute@gnu.org> |
---|---|
date | Mon, 11 Apr 2005 16:09:59 +0000 |
parents | 796b7fe4bfd8 |
children | 496c7a7bdacc |
rev | line source |
---|---|
55098 | 1 ;;; wdired.el --- Rename files editing their names in dired buffers |
2 | |
61350
796b7fe4bfd8
(wdired-advise-functions, wdired-add-skip-in-replace)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
61030
diff
changeset
|
3 ;; Copyright (C) 2001, 2004, 2005 Free Software Foundation, Inc. |
55098 | 4 |
5 ;; Filename: wdired.el | |
61350
796b7fe4bfd8
(wdired-advise-functions, wdired-add-skip-in-replace)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
61030
diff
changeset
|
6 ;; Author: Juan León Lahoz García <juanleon1@gmail.com> |
796b7fe4bfd8
(wdired-advise-functions, wdired-add-skip-in-replace)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
61030
diff
changeset
|
7 ;; Version: 2.0 |
55098 | 8 ;; Keywords: dired, environment, files, renaming |
9 | |
10 ;; This file is part of GNU Emacs. | |
11 | |
12 ;; GNU Emacs is free software; you can redistribute it and/or | |
13 ;; modify it under the terms of the GNU General Public License as | |
14 ;; published by the Free Software Foundation; either version 2, or (at | |
15 ;; your option) any later version. | |
16 | |
17 ;; This program is distributed in the hope that it will be useful, but | |
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 ;; General Public License for more details. | |
21 | |
22 ;; You should have received a copy of the GNU General Public License | |
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
25 ;; Boston, MA 02111-1307, USA. | |
26 | |
27 ;;; Commentary: | |
28 | |
29 ;; wdired.el (the "w" is for writable) provides an alternative way of | |
30 ;; renaming files. | |
31 ;; | |
32 ;; Have you ever wished to use C-x r t (string-rectangle), M-% | |
55109
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
33 ;; (query-replace), M-c (capitalize-word), etc. to change the name of |
55098 | 34 ;; the files in a "dired" buffer? Now you can do this. All the power |
35 ;; of emacs commands are available to renaming files! | |
36 ;; | |
37 ;; This package provides a function that makes the filenames of a a | |
38 ;; dired buffer editable, by changing the buffer mode (which inhibits | |
39 ;; all of the commands of dired mode). Here you can edit the names of | |
40 ;; one or more files and directories, and when you press C-c C-c, the | |
41 ;; renaming takes effect and you are back to dired mode. | |
42 ;; | |
43 ;; Another things you can do with wdired: | |
44 ;; | |
45 ;; - To move files to another directory (by typing their path, | |
46 ;; absolute or relative, as a part of the new filename). | |
47 ;; | |
48 ;; - To change the target of symbolic links. | |
49 ;; | |
50 ;; - To change the permission bits of the filenames (in systems with a | |
51 ;; working unix-alike `dired-chmod-program'). See and customize the | |
52 ;; variable `wdired-allow-to-change-permissions'. To change a single | |
53 ;; char (toggling between its two more usual values) you can press | |
54 ;; the space bar over it or left-click the mouse. To set any char to | |
55 ;; an specific value (this includes the SUID, SGID and STI bits) you | |
56 ;; can use the key labeled as the letter you want. Please note that | |
57 ;; permissions of the links cannot be changed in that way, because | |
58 ;; the change would affect to their targets, and this would not be | |
59 ;; WYSIWYG :-). | |
60 ;; | |
61 ;; - To mark files for deletion, by deleting their whole filename. | |
62 ;; | |
63 ;; I do not have a URL to hang wdired, but you can use the one below | |
64 ;; to find the latest version: | |
65 ;; | |
66 ;; http://groups.google.com/groups?as_ugroup=gnu.emacs.sources&as_q=wdired | |
67 | |
68 ;;; Installation: | |
69 | |
70 ;; Add this file (byte-compiling it is recommended) to your load-path. | |
71 ;; Then add one of these set of lines (or similar ones) to your config: | |
72 ;; | |
73 ;; This is the easy way: | |
74 ;; | |
75 ;; (require 'wdired) | |
76 ;; (define-key dired-mode-map "r" 'wdired-change-to-wdired-mode) | |
77 ;; | |
78 ;; This is recommended way for faster emacs startup time and lower | |
79 ;; memory consumption, but remind to add these lines before dired.el | |
80 ;; gets loaded (i.e., near the beginning of your .emacs file): | |
81 ;; | |
82 ;; (autoload 'wdired-change-to-wdired-mode "wdired") | |
83 ;; (add-hook 'dired-load-hook | |
84 ;; '(lambda () | |
85 ;; (define-key dired-mode-map "r" 'wdired-change-to-wdired-mode) | |
86 ;; (define-key dired-mode-map | |
87 ;; [menu-bar immediate wdired-change-to-wdired-mode] | |
88 ;; '("Edit File Names" . wdired-change-to-wdired-mode)))) | |
89 ;; | |
90 ;; | |
91 ;; Type "M-x customize-group RET wdired" if you want make changes to | |
92 ;; the default behavior. | |
93 | |
94 ;;; Usage: | |
95 | |
96 ;; Then, you can start editing the names of the files by typing "r" | |
97 ;; (or whatever key you choose, or M-x wdired-change-to-wdired-mode). | |
98 ;; Use C-c C-c when finished or C-c C-k to abort. You can use also the | |
99 ;; menu options: in dired mode, "Edit File Names" under "Immediate". | |
100 ;; While editing the names, a new submenu "WDired" is available at top | |
101 ;; level. You can customize the behavior of this package from this | |
102 ;; menu. | |
103 | |
104 ;;; Change Log: | |
105 | |
106 ;; From 1.9 to 1.91 | |
107 ;; | |
108 ;; - Fixed a bug (introduced in 1.9) so now files can be marked for | |
109 ;; deletion again, by deleting their whole filename. | |
110 | |
111 ;; From 1.8 to 1.9 | |
112 ;; | |
113 ;; - Another alternative way of editing permissions allowed, see | |
114 ;; `wdired-allow-to-change-permissions' for details. | |
115 ;; | |
55109
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
116 ;; - Now wdired doesn't rely on regexp so much. As a consequence of |
55098 | 117 ;; this, you can add newlines to filenames and symlinks targets |
118 ;; (although this is not very usual, IMHO). Please note that dired | |
119 ;; (at least in Emacs 21.1 and previous) does not work very well | |
120 ;; with filenames with newlines in them, so RET is deactivated in | |
121 ;; wdired mode. But you can activate it if you want. | |
122 ;; | |
123 ;; - Now `upcase-word' `capitalize-word' and `downcase-word' are not | |
55109
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
124 ;; advised to work better with wdired mode, but the keys bound to |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
125 ;; them use wdired versions of those commands. |
55098 | 126 ;; |
127 ;; - Now "undo" actions are not inherited from wdired mode when | |
128 ;; changing to dired mode. | |
129 ;; | |
130 ;; - Code and documentation cleanups. | |
131 ;; | |
132 ;; - Fixed a bug that was making wdired to fail on users with | |
133 ;; `dired-backup-overwrite' set to t. | |
134 ;; | |
135 ;; - C-c C-[ now abort changes. | |
136 | |
137 ;; From 1.7 to 1.8 | |
138 ;; | |
139 ;; - Now permission (access-control) bits of the files can be changed. | |
140 ;; Please see the commentary section and the custom variable | |
141 ;; `wdired-allow-to-change-permissions' for details. | |
142 ;; | |
143 ;; - Added another possible value for the variable | |
144 ;; `wdired-always-move-to-filename-beginning', useful to change | |
145 ;; permission bits of several files without the cursor jumping to | |
146 ;; filenames when changing lines. | |
147 | |
148 ;; From 0.1 to 1.7 | |
149 | |
150 ;; - I've moved the list of changes to another file, because it was | |
151 ;; huge. Ask me for it or search older versions in google. | |
152 | |
153 ;;; TODO: | |
154 | |
155 ;; - Make it to work in XEmacs. Any volunteer? | |
156 | |
157 ;;; Code: | |
158 | |
61350
796b7fe4bfd8
(wdired-advise-functions, wdired-add-skip-in-replace)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
61030
diff
changeset
|
159 (defvar dired-backup-overwrite) ; Only in emacs 20.x this is a custom var |
55098 | 160 (eval-when-compile |
161 (set (make-local-variable 'byte-compile-dynamic) t)) | |
162 | |
163 (eval-and-compile | |
164 (require 'dired) | |
165 (autoload 'dired-do-create-files-regexp "dired-aux") | |
166 (autoload 'dired-call-process "dired-aux")) | |
167 | |
168 (defgroup wdired nil | |
169 "Mode to rename files by editing their names in dired buffers." | |
170 :group 'dired) | |
171 | |
172 (defcustom wdired-use-interactive-rename nil | |
173 "*If t, confirmation is required before actually rename the files. | |
174 Confirmation is required also for overwriting files. If nil, no | |
175 confirmation is required for change the file names, and the variable | |
176 `wdired-is-ok-overwrite' is used to see if it is ok to overwrite files | |
177 without asking." | |
178 :type 'boolean | |
179 :group 'wdired) | |
180 | |
181 (defcustom wdired-is-ok-overwrite nil | |
182 "*If non-nil the renames can overwrite files without asking. | |
183 This variable is used only if `wdired-use-interactive-rename' is nil." | |
184 :type 'boolean | |
185 :group 'wdired) | |
186 | |
187 (defcustom wdired-always-move-to-filename-beginning nil | |
188 "*If t the \"up\" and \"down\" movement is done as in dired mode. | |
189 That is, always move the point to the beginning of the filename at line. | |
190 | |
191 If `sometimes, only move to the beginning of filename if the point is | |
192 before it, and `track-eol' is honored. This behavior is very handy | |
193 when editing several filenames. | |
194 | |
195 If nil, \"up\" and \"down\" movement is done as in any other buffer." | |
196 :type '(choice (const :tag "As in any other mode" nil) | |
197 (const :tag "Smart cursor placement" sometimes) | |
198 (other :tag "As in dired mode" t)) | |
199 :group 'wdired) | |
200 | |
201 (defcustom wdired-allow-to-redirect-links t | |
202 "*If non-nil, the target of the symbolic links can be changed also. | |
203 In systems without symbolic links support, this variable has no effect | |
204 at all." | |
205 :type 'boolean | |
206 :group 'wdired) | |
207 | |
208 (defcustom wdired-allow-to-change-permissions nil | |
209 "*If non-nil, the permissions bits of the files can be changed also. | |
210 | |
211 If t, to change a single bit, put the cursor over it and press the | |
212 space bar, or left click over it. You can also hit the letter you want | |
213 to set: if this value is allowed, the character in the buffer will be | |
214 changed. Anyway, the point is advanced one position, so, for example, | |
215 you can keep the \"x\" key pressed to give execution permissions to | |
216 everybody to that file. | |
217 | |
218 If `advanced, the bits are freely editable. You can use | |
219 `string-rectangle', `query-replace', etc. You can put any value (even | |
220 newlines), but if you want your changes to be useful, you better put a | |
221 intelligible value. | |
222 | |
223 Anyway, the real change of the permissions is done with the external | |
224 program `dired-chmod-program', which must exist." | |
225 :type '(choice (const :tag "Not allowed" nil) | |
55109
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
226 (const :tag "Toggle/set bits" t) |
55098 | 227 (other :tag "Bits freely editable" advanced)) |
228 :group 'wdired) | |
229 | |
55109
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
230 (defvar wdired-mode-map |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
231 (let ((map (make-sparse-keymap))) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
232 (define-key map "\C-x\C-s" 'wdired-finish-edit) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
233 (define-key map "\C-c\C-c" 'wdired-finish-edit) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
234 (define-key map "\C-c\C-k" 'wdired-abort-changes) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
235 (define-key map "\C-c\C-[" 'wdired-abort-changes) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
236 (define-key map "\C-m" 'wdired-newline) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
237 (define-key map "\C-j" 'wdired-newline) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
238 (define-key map "\C-o" 'wdired-newline) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
239 (define-key map [up] 'wdired-previous-line) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
240 (define-key map "\C-p" 'wdired-previous-line) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
241 (define-key map [down] 'wdired-next-line) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
242 (define-key map "\C-n" 'wdired-next-line) |
55098 | 243 |
55109
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
244 (define-key map [menu-bar wdired] |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
245 (cons "WDired" (make-sparse-keymap "WDired"))) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
246 (define-key map [menu-bar wdired wdired-customize] |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
247 '("Options" . wdired-customize)) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
248 (define-key map [menu-bar wdired dashes] |
55098 | 249 '("--")) |
55109
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
250 (define-key map [menu-bar wdired wdired-abort-changes] |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
251 '("Abort Changes" . wdired-abort-changes)) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
252 (define-key map [menu-bar wdired wdired-finish-edit] |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
253 '("Commit Changes" . wdired-finish-edit)) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
254 ;; FIXME: Use the new remap trick. |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
255 (substitute-key-definition 'upcase-word 'wdired-upcase-word |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
256 map global-map) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
257 (substitute-key-definition 'capitalize-word 'wdired-capitalize-word |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
258 map global-map) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
259 (substitute-key-definition 'downcase-word 'wdired-downcase-word |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
260 map global-map) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
261 map)) |
55098 | 262 |
55109
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
263 (defvar wdired-mode-hook nil |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
264 "Hook run when changing to wdired mode.") |
55098 | 265 |
266 ;; Local variables (put here to avoid compilation gripes) | |
267 (defvar wdired-col-perm) ;; Column where the permission bits start | |
268 (defvar wdired-old-content) | |
269 | |
270 | |
271 (defun wdired-mode () | |
272 "\\<wdired-mode-map>File Names Editing mode. | |
273 | |
274 Press \\[wdired-finish-edit] to make the changes to take effect and | |
275 exit. To abort the edit, use \\[wdired-abort-changes]. | |
276 | |
277 In this mode you can edit the names of the files, the target of the | |
278 links and the permission bits of the files. You can `customize-group' | |
279 wdired. | |
280 | |
281 Editing things out of the filenames, or adding or deleting lines is | |
282 not allowed, because the rest of the buffer is read-only." | |
283 (interactive) | |
284 (error "This mode can be enabled only by `wdired-change-to-wdired-mode'")) | |
285 (put 'wdired-mode 'mode-class 'special) | |
286 | |
287 | |
288 ;;;###autoload | |
289 (defun wdired-change-to-wdired-mode () | |
290 "Put a dired buffer in a mode in which filenames are editable. | |
55109
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
291 In this mode the names of the files can be changed, and after |
55098 | 292 typing C-c C-c the files and directories in disk are renamed. |
293 | |
294 See `wdired-mode'." | |
295 (interactive) | |
296 (set (make-local-variable 'wdired-old-content) | |
297 (buffer-substring (point-min) (point-max))) | |
61350
796b7fe4bfd8
(wdired-advise-functions, wdired-add-skip-in-replace)
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
61030
diff
changeset
|
298 (set (make-local-variable 'query-replace-skip-read-only) t) |
55098 | 299 (use-local-map wdired-mode-map) |
55109
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
300 (force-mode-line-update) |
55098 | 301 (setq buffer-read-only nil) |
302 (dired-unadvertise default-directory) | |
303 (add-hook 'kill-buffer-hook 'wdired-check-kill-buffer nil t) | |
304 (setq major-mode 'wdired-mode) | |
305 (setq mode-name "Edit filenames") | |
306 (setq revert-buffer-function 'wdired-revert) | |
307 ;; I temp disable undo for performance: since I'm going to clear the | |
308 ;; undo list, it can save more than a 9% of time with big | |
309 ;; directories because setting properties modify the undo-list. | |
310 (buffer-disable-undo) | |
311 (wdired-preprocess-files) | |
312 (if wdired-allow-to-change-permissions | |
313 (wdired-preprocess-perms)) | |
314 (if (and wdired-allow-to-redirect-links (fboundp 'make-symbolic-link)) | |
315 (wdired-preprocess-symlinks)) | |
316 (buffer-enable-undo) ; Performance hack. See above. | |
317 (set-buffer-modified-p nil) | |
318 (setq buffer-undo-list nil) | |
55371
a96e9558b1ea
(wdired-change-to-wdired-mode): Quote wdired-mode-hook in run-hooks.
Lars Hansen <larsh@soem.dk>
parents:
55109
diff
changeset
|
319 (run-hooks 'wdired-mode-hook) |
61030
6b0506554db8
(wdired-change-to-wdired-mode):
Juri Linkov <juri@jurta.org>
parents:
60141
diff
changeset
|
320 (message (substitute-command-keys "Press \\[wdired-finish-edit] when finished \ |
6b0506554db8
(wdired-change-to-wdired-mode):
Juri Linkov <juri@jurta.org>
parents:
60141
diff
changeset
|
321 or \\[wdired-abort-changes] to abort changes"))) |
55098 | 322 |
323 | |
324 ;; Protect the buffer so only the filenames can be changed, and put | |
325 ;; properties so filenames (old and new) can be easily found. | |
326 (defun wdired-preprocess-files () | |
327 (put-text-property 1 2 'front-sticky t) | |
328 (save-excursion | |
329 (goto-char (point-min)) | |
330 (let ((b-protection (point)) | |
331 filename) | |
332 (while (not (eobp)) | |
333 (setq filename (dired-get-filename nil t)) | |
55109
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
334 (when (and filename |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
335 (not (member (file-name-nondirectory filename) '("." "..")))) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
336 (dired-move-to-filename) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
337 (put-text-property (- (point) 2) (1- (point)) 'old-name filename) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
338 (put-text-property b-protection (1- (point)) 'read-only t) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
339 (setq b-protection (dired-move-to-end-of-filename t))) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
340 (put-text-property (point) (1+ (point)) 'end-name t) |
55098 | 341 (forward-line)) |
342 (put-text-property b-protection (point-max) 'read-only t)))) | |
343 | |
344 ;; This code is a copy of some dired-get-filename lines. | |
345 (defsubst wdired-normalize-filename (file) | |
346 (setq file | |
55109
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
347 ;; FIXME: shouldn't we check for a `b' argument or somesuch before |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
348 ;; doing such unquoting? --Stef |
55098 | 349 (read (concat |
55109
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
350 "\"" (replace-regexp-in-string |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
351 "\\([^\\]\\|\\`\\)\"" "\\1\\\\\"" file) |
55098 | 352 "\""))) |
353 (and file buffer-file-coding-system | |
354 (not file-name-coding-system) | |
355 (not default-file-name-coding-system) | |
356 (setq file (encode-coding-string file buffer-file-coding-system))) | |
357 file) | |
358 | |
359 (defun wdired-get-filename (&optional no-dir old) | |
360 "Return the filename at line. | |
55109
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
361 Similar to `dired-get-filename' but it doesn't rely on regexps. It |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
362 relies on wdired buffer's properties. Optional arg NO-DIR with value |
55098 | 363 non-nil means don't include directory. Optional arg OLD with value |
364 non-nil means return old filename." | |
55109
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
365 ;; FIXME: Use dired-get-filename's new properties. |
55098 | 366 (let (beg end file) |
367 (save-excursion | |
368 (setq end (progn (end-of-line) (point))) | |
369 (beginning-of-line) | |
370 (setq beg (next-single-property-change (point) 'old-name nil end)) | |
55109
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
371 (unless (eq beg end) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
372 (if old |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
373 (setq file (get-text-property beg 'old-name)) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
374 (setq end (next-single-property-change (1+ beg) 'end-name)) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
375 (setq file (buffer-substring-no-properties (+ 2 beg) end))) |
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
376 (and file (setq file (wdired-normalize-filename file)))) |
55098 | 377 (if (or no-dir old) |
378 file | |
379 (and file (> (length file) 0) | |
380 (concat (dired-current-directory) file)))))) | |
381 | |
382 | |
383 (defun wdired-change-to-dired-mode () | |
384 "Change the mode back to dired." | |
385 (let ((inhibit-read-only t)) | |
386 (remove-text-properties (point-min) (point-max) | |
387 '(read-only nil local-map nil))) | |
388 (put-text-property 1 2 'front-sticky nil) | |
389 (use-local-map dired-mode-map) | |
55109
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
390 (force-mode-line-update) |
55098 | 391 (setq buffer-read-only t) |
392 (setq major-mode 'dired-mode) | |
393 (setq mode-name "Dired") | |
394 (dired-advertise) | |
395 (remove-hook 'kill-buffer-hook 'wdired-check-kill-buffer t) | |
396 (setq revert-buffer-function 'dired-revert)) | |
397 | |
398 | |
399 (defun wdired-abort-changes () | |
400 "Abort changes and return to dired mode." | |
401 (interactive) | |
402 (let ((inhibit-read-only t)) | |
403 (erase-buffer) | |
404 (insert wdired-old-content)) | |
405 (wdired-change-to-dired-mode) | |
406 (set-buffer-modified-p nil) | |
55371
a96e9558b1ea
(wdired-change-to-wdired-mode): Quote wdired-mode-hook in run-hooks.
Lars Hansen <larsh@soem.dk>
parents:
55109
diff
changeset
|
407 (setq buffer-undo-list nil) |
a96e9558b1ea
(wdired-change-to-wdired-mode): Quote wdired-mode-hook in run-hooks.
Lars Hansen <larsh@soem.dk>
parents:
55109
diff
changeset
|
408 (message "Changes aborted")) |
55098 | 409 |
410 (defun wdired-finish-edit () | |
411 "Actually rename files based on your editing in the Dired buffer." | |
412 (interactive) | |
413 (wdired-change-to-dired-mode) | |
414 (let ((overwrite (or wdired-is-ok-overwrite 1)) | |
415 (changes nil) | |
416 (files-deleted nil) | |
417 (errors 0) | |
418 file-ori file-new tmp-value) | |
419 (save-excursion | |
420 (if (and wdired-allow-to-redirect-links | |
421 (fboundp 'make-symbolic-link)) | |
422 (progn | |
423 (setq tmp-value (wdired-do-symlink-changes)) | |
424 (setq errors (cdr tmp-value)) | |
425 (setq changes (car tmp-value)))) | |
426 (if (and wdired-allow-to-change-permissions | |
427 (boundp 'wdired-col-perm)) ; could have been changed | |
428 (progn | |
429 (setq tmp-value (wdired-do-perm-changes)) | |
430 (setq errors (+ errors (cdr tmp-value))) | |
431 (setq changes (or changes (car tmp-value))))) | |
432 (goto-char (point-max)) | |
433 (while (not (bobp)) | |
434 (setq file-ori (wdired-get-filename nil t)) | |
435 (if file-ori | |
436 (setq file-new (wdired-get-filename))) | |
437 (if (and file-ori (not (equal file-new file-ori))) | |
438 (progn | |
439 (setq changes t) | |
440 (if (not file-new) ;empty filename! | |
441 (setq files-deleted (cons file-ori files-deleted)) | |
442 (progn | |
443 (setq file-new (substitute-in-file-name file-new)) | |
444 (if wdired-use-interactive-rename | |
445 (wdired-search-and-rename file-ori file-new) | |
56545
c3fa830eb56c
(wdired-finish-edit): Require dired-aux before locally binding dired-backup-overwrite.
Lars Hansen <larsh@soem.dk>
parents:
55371
diff
changeset
|
446 ;; If dired-rename-file autoloads dired-aux while |
c3fa830eb56c
(wdired-finish-edit): Require dired-aux before locally binding dired-backup-overwrite.
Lars Hansen <larsh@soem.dk>
parents:
55371
diff
changeset
|
447 ;; dired-backup-overwrite is locally bound, |
c3fa830eb56c
(wdired-finish-edit): Require dired-aux before locally binding dired-backup-overwrite.
Lars Hansen <larsh@soem.dk>
parents:
55371
diff
changeset
|
448 ;; dired-backup-overwrite won't be initialized. |
c3fa830eb56c
(wdired-finish-edit): Require dired-aux before locally binding dired-backup-overwrite.
Lars Hansen <larsh@soem.dk>
parents:
55371
diff
changeset
|
449 ;; So we must ensure dired-aux is loaded. |
c3fa830eb56c
(wdired-finish-edit): Require dired-aux before locally binding dired-backup-overwrite.
Lars Hansen <larsh@soem.dk>
parents:
55371
diff
changeset
|
450 (require 'dired-aux) |
55098 | 451 (condition-case err |
452 (let ((dired-backup-overwrite nil)) | |
453 (dired-rename-file file-ori file-new | |
454 overwrite)) | |
455 (error | |
456 (setq errors (1+ errors)) | |
457 (dired-log (concat "Rename `" file-ori "' to `" | |
458 file-new "' failed:\n%s\n") | |
459 err)))))))) | |
460 (forward-line -1))) | |
461 (if changes | |
462 (revert-buffer) ;The "revert" is necessary to re-sort the buffer | |
463 (let ((buffer-read-only nil)) | |
464 (remove-text-properties (point-min) (point-max) | |
465 '(old-name nil end-name nil old-link nil | |
466 end-link nil end-perm nil | |
467 old-perm nil perm-changed nil)) | |
468 (message "(No changes to be performed)"))) | |
469 (if files-deleted | |
470 (wdired-flag-for-deletion files-deleted)) | |
471 (if (> errors 0) | |
472 (dired-log-summary (format "%d rename actions failed" errors) nil))) | |
473 (set-buffer-modified-p nil) | |
474 (setq buffer-undo-list nil)) | |
475 | |
476 ;; Renames a file, searching it in a modified dired buffer, in order | |
477 ;; to be able to use `dired-do-create-files-regexp' and get its | |
478 ;; "benefits" | |
479 (defun wdired-search-and-rename (filename-ori filename-new) | |
480 (save-excursion | |
481 (goto-char (point-max)) | |
482 (forward-line -1) | |
483 (let ((exit-while nil) | |
484 curr-filename) | |
485 (while (not exit-while) | |
486 (setq curr-filename (wdired-get-filename)) | |
487 (if (and curr-filename | |
488 (equal (substitute-in-file-name curr-filename) filename-new)) | |
489 (progn | |
490 (setq exit-while t) | |
491 (let ((inhibit-read-only t)) | |
492 (dired-move-to-filename) | |
493 (search-forward (wdired-get-filename t) nil t) | |
494 (replace-match (file-name-nondirectory filename-ori) t t)) | |
495 (dired-do-create-files-regexp | |
496 (function dired-rename-file) | |
497 "Move" 1 ".*" filename-new nil t)) | |
498 (progn | |
499 (forward-line -1) | |
500 (beginning-of-line) | |
501 (setq exit-while (= 1 (point))))))))) | |
502 | |
503 ;; marks a list of files for deletion | |
504 (defun wdired-flag-for-deletion (filenames-ori) | |
505 (save-excursion | |
506 (goto-char (point-min)) | |
507 (while (not (eobp)) | |
508 (if (member (dired-get-filename nil t) filenames-ori) | |
509 (dired-flag-file-deletion 1) | |
510 (forward-line))))) | |
511 | |
512 (defun wdired-customize () | |
513 "Customize wdired options." | |
514 (interactive) | |
515 (customize-apropos "wdired" 'groups)) | |
516 | |
517 (defun wdired-revert (&optional arg noconfirm) | |
518 "Discard changes in the buffer and update the changes in the disk." | |
519 (wdired-change-to-dired-mode) | |
520 (revert-buffer) | |
521 (wdired-change-to-wdired-mode)) | |
522 | |
523 (defun wdired-check-kill-buffer () | |
55109
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
524 ;; FIXME: Can't we use the normal mechanism for that? --Stef |
55098 | 525 (if (and |
526 (buffer-modified-p) | |
527 (not (y-or-n-p "Buffer changed. Discard changes and kill buffer? "))) | |
528 (error nil))) | |
529 | |
530 (defun wdired-next-line (arg) | |
531 "Move down lines then position at filename or the current column. | |
532 See `wdired-always-move-to-filename-beginning'. Optional prefix ARG | |
533 says how many lines to move; default is one line." | |
534 (interactive "p") | |
535 (next-line arg) | |
536 (if (or (eq wdired-always-move-to-filename-beginning t) | |
537 (and wdired-always-move-to-filename-beginning | |
538 (< (current-column) | |
539 (save-excursion (dired-move-to-filename) | |
540 (current-column))))) | |
541 (dired-move-to-filename))) | |
542 | |
543 (defun wdired-previous-line (arg) | |
544 "Move up lines then position at filename or the current column. | |
545 See `wdired-always-move-to-filename-beginning'. Optional prefix ARG | |
546 says how many lines to move; default is one line." | |
547 (interactive "p") | |
548 (previous-line arg) | |
549 (if (or (eq wdired-always-move-to-filename-beginning t) | |
550 (and wdired-always-move-to-filename-beginning | |
551 (< (current-column) | |
552 (save-excursion (dired-move-to-filename) | |
553 (current-column))))) | |
554 (dired-move-to-filename))) | |
555 | |
556 ;; dired doesn't works well with newlines, so ... | |
557 (defun wdired-newline () | |
558 "Do nothing." | |
559 (interactive)) | |
560 | |
561 ;; Put the needed properties to allow the user to change links' targets | |
562 (defun wdired-preprocess-symlinks () | |
563 (let ((inhibit-read-only t)) | |
564 (save-excursion | |
565 (goto-char (point-min)) | |
566 (while (not (eobp)) | |
567 (if (looking-at dired-re-sym) | |
568 (progn | |
569 (re-search-forward " -> \\(.*\\)$") | |
570 (put-text-property (- (match-beginning 1) 2) | |
571 (1- (match-beginning 1)) 'old-link | |
572 (match-string-no-properties 1)) | |
573 (put-text-property (match-end 1) (1+ (match-end 1)) 'end-link t) | |
574 (put-text-property (1- (match-beginning 1)) | |
575 (match-end 1) 'read-only nil))) | |
576 (forward-line) | |
577 (beginning-of-line))))) | |
578 | |
579 | |
580 (defun wdired-get-previous-link (&optional old move) | |
581 "Return the next symlink target. | |
582 If OLD, return the old target. If MOVE, move point before it." | |
583 (let (beg end target) | |
584 (setq beg (previous-single-property-change (point) 'old-link nil)) | |
585 (if beg | |
586 (progn | |
587 (if old | |
588 (setq target (get-text-property (1- beg) 'old-link)) | |
589 (setq end (next-single-property-change beg 'end-link)) | |
590 (setq target (buffer-substring-no-properties (1+ beg) end))) | |
591 (if move (goto-char (1- beg))))) | |
592 (and target (wdired-normalize-filename target)))) | |
593 | |
594 | |
595 | |
596 ;; Perform the changes in the target of the changed links. | |
597 (defun wdired-do-symlink-changes() | |
598 (let ((changes nil) | |
599 (errors 0) | |
600 link-to-ori link-to-new link-from) | |
601 (goto-char (point-max)) | |
602 (while (setq link-to-new (wdired-get-previous-link)) | |
603 (setq link-to-ori (wdired-get-previous-link t t)) | |
604 (setq link-from (wdired-get-filename nil t)) | |
605 (if (not (equal link-to-new link-to-ori)) | |
606 (progn | |
607 (setq changes t) | |
608 (if (equal link-to-new "") ;empty filename! | |
609 (setq link-to-new "/dev/null")) | |
610 (condition-case err | |
611 (progn | |
612 (delete-file link-from) | |
613 (make-symbolic-link | |
614 (substitute-in-file-name link-to-new) link-from)) | |
615 (error | |
616 (setq errors (1+ errors)) | |
617 (dired-log (concat "Link `" link-from "' to `" | |
618 link-to-new "' failed:\n%s\n") | |
619 err)))))) | |
620 (cons changes errors))) | |
621 | |
622 ;; Perform a "case command" skipping read-only words. | |
623 (defun wdired-xcase-word (command arg) | |
624 (if (< arg 0) | |
625 (funcall command arg) | |
626 (progn | |
627 (while (> arg 0) | |
628 (condition-case err | |
629 (progn | |
630 (funcall command 1) | |
631 (setq arg (1- arg))) | |
632 (error | |
633 (if (not (forward-word 1)) | |
634 (setq arg 0)))))))) | |
635 | |
636 (defun wdired-downcase-word (arg) | |
637 "Wdired version of `downcase-word'. | |
638 Like original function but it skips read-only words." | |
639 (interactive "p") | |
640 (wdired-xcase-word 'downcase-word arg)) | |
641 | |
642 (defun wdired-upcase-word (arg) | |
643 "Wdired version of `upcase-word'. | |
644 Like original function but it skips read-only words." | |
645 (interactive "p") | |
646 (wdired-xcase-word 'upcase-word arg)) | |
647 | |
648 (defun wdired-capitalize-word (arg) | |
649 "Wdired version of `capitalize-word'. | |
650 Like original function but it skips read-only words." | |
651 (interactive "p") | |
652 (wdired-xcase-word 'capitalize-word arg)) | |
653 | |
654 | |
655 ;; The following code deals with changing the access bits (or | |
656 ;; permissions) of the files. | |
657 | |
658 (defvar wdired-perm-mode-map nil) | |
659 (unless wdired-perm-mode-map | |
660 (setq wdired-perm-mode-map (copy-keymap wdired-mode-map)) | |
661 (define-key wdired-perm-mode-map " " 'wdired-toggle-bit) | |
662 (define-key wdired-perm-mode-map "r" 'wdired-set-bit) | |
663 (define-key wdired-perm-mode-map "w" 'wdired-set-bit) | |
664 (define-key wdired-perm-mode-map "x" 'wdired-set-bit) | |
665 (define-key wdired-perm-mode-map "-" 'wdired-set-bit) | |
666 (define-key wdired-perm-mode-map "S" 'wdired-set-bit) | |
667 (define-key wdired-perm-mode-map "s" 'wdired-set-bit) | |
668 (define-key wdired-perm-mode-map "T" 'wdired-set-bit) | |
669 (define-key wdired-perm-mode-map "t" 'wdired-set-bit) | |
670 (define-key wdired-perm-mode-map "s" 'wdired-set-bit) | |
671 (define-key wdired-perm-mode-map "l" 'wdired-set-bit) | |
672 (define-key wdired-perm-mode-map [down-mouse-1] 'wdired-mouse-toggle-bit)) | |
673 | |
674 ;; Put a local-map to the permission bits of the files, and store the | |
675 ;; original name and permissions as a property | |
676 (defun wdired-preprocess-perms() | |
677 (let ((inhibit-read-only t) | |
678 filename) | |
679 (set (make-local-variable 'wdired-col-perm) nil) | |
680 (save-excursion | |
681 (goto-char (point-min)) | |
682 (while (not (eobp)) | |
683 (if (and (not (looking-at dired-re-sym)) | |
684 (setq filename (wdired-get-filename))) | |
685 (progn | |
686 (re-search-forward dired-re-perms) | |
687 (or wdired-col-perm | |
688 (setq wdired-col-perm (- (current-column) 9))) | |
689 (if (eq wdired-allow-to-change-permissions 'advanced) | |
690 (put-text-property (match-beginning 0) (match-end 0) | |
691 'read-only nil) | |
692 (put-text-property (1+ (match-beginning 0)) (match-end 0) | |
693 'local-map wdired-perm-mode-map)) | |
694 (put-text-property (match-end 0) (1+ (match-end 0)) 'end-perm t) | |
695 (put-text-property (match-beginning 0) (1+ (match-beginning 0)) | |
696 'old-perm (match-string-no-properties 0)))) | |
697 (forward-line) | |
698 (beginning-of-line))))) | |
699 | |
700 (defun wdired-perm-allowed-in-pos (char pos) | |
701 (cond | |
702 ((= char ?-) t) | |
703 ((= char ?r) (= (% pos 3) 0)) | |
704 ((= char ?w) (= (% pos 3) 1)) | |
705 ((= char ?x) (= (% pos 3) 2)) | |
706 ((memq char '(?s ?S)) (memq pos '(2 5))) | |
707 ((memq char '(?t ?T)) (= pos 8)) | |
708 ((= char ?l) (= pos 5)))) | |
709 | |
710 (defun wdired-set-bit () | |
711 "Set a permission bit character." | |
712 (interactive) | |
713 (if (wdired-perm-allowed-in-pos last-command-char | |
714 (- (current-column) wdired-col-perm)) | |
715 (let ((new-bit (char-to-string last-command-char)) | |
716 (inhibit-read-only t) | |
717 (pos-prop (- (point) (- (current-column) wdired-col-perm)))) | |
718 (put-text-property 0 1 'local-map wdired-perm-mode-map new-bit) | |
719 (put-text-property 0 1 'read-only t new-bit) | |
720 (insert new-bit) | |
721 (delete-char 1) | |
722 (put-text-property pos-prop (1- pos-prop) 'perm-changed t)) | |
723 (forward-char 1))) | |
724 | |
725 (defun wdired-toggle-bit() | |
726 "Toggle the permission bit at point." | |
727 (interactive) | |
728 (let ((inhibit-read-only t) | |
729 (new-bit "-") | |
730 (pos-prop (- (point) (- (current-column) wdired-col-perm)))) | |
731 (if (eq (char-after (point)) ?-) | |
732 (setq new-bit | |
733 (if (= (% (- (current-column) wdired-col-perm) 3) 0) "r" | |
734 (if (= (% (- (current-column) wdired-col-perm) 3) 1) "w" | |
735 "x")))) | |
736 (put-text-property 0 1 'local-map wdired-perm-mode-map new-bit) | |
737 (put-text-property 0 1 'read-only t new-bit) | |
738 (insert new-bit) | |
739 (delete-char 1) | |
740 (put-text-property pos-prop (1- pos-prop) 'perm-changed t))) | |
741 | |
742 (defun wdired-mouse-toggle-bit (event) | |
743 "Toggle the permission bit that was left clicked." | |
744 (interactive "e") | |
745 (mouse-set-point event) | |
746 (wdired-toggle-bit)) | |
747 | |
748 ;; Allowed chars for 4000 bit are Ss in position 3 | |
749 ;; Allowed chars for 2000 bit are Ssl in position 6 | |
750 ;; Allowed chars for 1000 bit are Tt in position 9 | |
751 (defun wdired-perms-to-number (perms) | |
752 (let ((nperm 0777)) | |
753 (if (= (elt perms 1) ?-) (setq nperm (- nperm 400))) | |
754 (if (= (elt perms 2) ?-) (setq nperm (- nperm 200))) | |
755 (let ((p-bit (elt perms 3))) | |
756 (if (memq p-bit '(?- ?S)) (setq nperm (- nperm 100))) | |
757 (if (memq p-bit '(?s ?S)) (setq nperm (+ nperm 4000)))) | |
758 (if (= (elt perms 4) ?-) (setq nperm (- nperm 40))) | |
759 (if (= (elt perms 5) ?-) (setq nperm (- nperm 20))) | |
760 (let ((p-bit (elt perms 6))) | |
761 (if (memq p-bit '(?- ?S ?l)) (setq nperm (- nperm 10))) | |
762 (if (memq p-bit '(?s ?S ?l)) (setq nperm (+ nperm 2000)))) | |
763 (if (= (elt perms 7) ?-) (setq nperm (- nperm 4))) | |
764 (if (= (elt perms 8) ?-) (setq nperm (- nperm 2))) | |
765 (let ((p-bit (elt perms 9))) | |
766 (if (memq p-bit '(?- ?T)) (setq nperm (- nperm 1))) | |
767 (if (memq p-bit '(?t ?T)) (setq nperm (+ nperm 1000)))) | |
768 nperm)) | |
769 | |
770 ;; Perform the changes in the permissions of the files that have | |
771 ;; changed. | |
772 (defun wdired-do-perm-changes () | |
773 (let ((changes nil) | |
774 (errors 0) | |
775 (prop-wanted (if (eq wdired-allow-to-change-permissions 'advanced) | |
776 'old-perm 'perm-changed)) | |
777 filename perms-ori perms-new perm-tmp) | |
778 (goto-char (next-single-property-change (point-min) prop-wanted | |
779 nil (point-max))) | |
780 (while (not (eobp)) | |
781 (setq perms-ori (get-text-property (point) 'old-perm)) | |
782 (setq perms-new (buffer-substring-no-properties | |
783 (point) (next-single-property-change (point) 'end-perm))) | |
784 (if (not (equal perms-ori perms-new)) | |
785 (progn | |
786 (setq changes t) | |
787 (setq filename (wdired-get-filename nil t)) | |
788 (if (= (length perms-new) 10) | |
789 (progn | |
790 (setq perm-tmp | |
791 (int-to-string (wdired-perms-to-number perms-new))) | |
792 (if (not (equal 0 (dired-call-process dired-chmod-program | |
793 t perm-tmp filename))) | |
794 (progn | |
795 (setq errors (1+ errors)) | |
796 (dired-log (concat dired-chmod-program " " perm-tmp | |
797 " `" filename "' failed\n\n"))))) | |
798 (setq errors (1+ errors)) | |
799 (dired-log (concat "Cannot parse permission `" perms-new | |
800 "' for file `" filename "'\n\n"))))) | |
801 (goto-char (next-single-property-change (1+ (point)) prop-wanted | |
802 nil (point-max)))) | |
803 (cons changes errors))) | |
804 | |
805 (provide 'wdired) | |
806 | |
55109
043dd9a4d1b4
(wdired-mode-map): Move init into declaration. Fix `return' binding.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
55107
diff
changeset
|
807 ;; arch-tag: bc00902e-526f-4305-bc7f-8862a559184f |
55098 | 808 ;;; wdired.el ends here |