(python-open-block-statement-p): Fix
[emacs.git] / lisp / wdired.el
blob30ba2a3cd4541737b3121639e1715669ee924992
1 ;;; wdired.el --- Rename files editing their names in dired buffers
3 ;; Copyright (C) 2001, 2004 Free Software Foundation, Inc.
5 ;; Filename: wdired.el
6 ;; Author: Juan León Lahoz García <juan-leon.lahoz@tecsidel.es>
7 ;; Version: 1.91
8 ;; Keywords: dired, environment, files, renaming
10 ;; This file is part of GNU Emacs.
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.
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.
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.
27 ;;; Commentary:
29 ;; wdired.el (the "w" is for writable) provides an alternative way of
30 ;; renaming files.
32 ;; Have you ever wished to use C-x r t (string-rectangle), M-%
33 ;; (query-replace), M-c (capitalize-word), etc. to change the name of
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.
43 ;; Another things you can do with wdired:
45 ;; - To move files to another directory (by typing their path,
46 ;; absolute or relative, as a part of the new filename).
48 ;; - To change the target of symbolic links.
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 :-).
61 ;; - To mark files for deletion, by deleting their whole filename.
63 ;; I do not have a URL to hang wdired, but you can use the one below
64 ;; to find the latest version:
66 ;; http://groups.google.com/groups?as_ugroup=gnu.emacs.sources&as_q=wdired
68 ;;; Installation:
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:
73 ;; This is the easy way:
75 ;; (require 'wdired)
76 ;; (define-key dired-mode-map "r" 'wdired-change-to-wdired-mode)
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):
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))))
91 ;; Type "M-x customize-group RET wdired" if you want make changes to
92 ;; the default behavior.
94 ;;; Usage:
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.
104 ;;; Change Log:
106 ;; From 1.9 to 1.91
108 ;; - Fixed a bug (introduced in 1.9) so now files can be marked for
109 ;; deletion again, by deleting their whole filename.
111 ;; From 1.8 to 1.9
113 ;; - Another alternative way of editing permissions allowed, see
114 ;; `wdired-allow-to-change-permissions' for details.
116 ;; - Now wdired doesn't rely on regexp so much. As a consequence of
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.
123 ;; - Now `upcase-word' `capitalize-word' and `downcase-word' are not
124 ;; advised to work better with wdired mode, but the keys bound to
125 ;; them use wdired versions of those commands.
127 ;; - Now "undo" actions are not inherited from wdired mode when
128 ;; changing to dired mode.
130 ;; - Code and documentation cleanups.
132 ;; - Fixed a bug that was making wdired to fail on users with
133 ;; `dired-backup-overwrite' set to t.
135 ;; - C-c C-[ now abort changes.
137 ;; From 1.7 to 1.8
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.
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.
148 ;; From 0.1 to 1.7
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.
153 ;;; TODO:
155 ;; - Make it to work in XEmacs. Any volunteer?
157 ;;; Code:
159 (eval-when-compile
160 (require 'advice)
161 (defvar dired-backup-overwrite) ; Only in emacs 20.x this is a custom var
162 (set (make-local-variable 'byte-compile-dynamic) t))
164 (eval-and-compile
165 (require 'dired)
166 (autoload 'dired-do-create-files-regexp "dired-aux")
167 (autoload 'dired-call-process "dired-aux"))
169 (defgroup wdired nil
170 "Mode to rename files by editing their names in dired buffers."
171 :group 'dired)
173 (defcustom wdired-use-interactive-rename nil
174 "*If t, confirmation is required before actually rename the files.
175 Confirmation is required also for overwriting files. If nil, no
176 confirmation is required for change the file names, and the variable
177 `wdired-is-ok-overwrite' is used to see if it is ok to overwrite files
178 without asking."
179 :type 'boolean
180 :group 'wdired)
182 (defcustom wdired-is-ok-overwrite nil
183 "*If non-nil the renames can overwrite files without asking.
184 This variable is used only if `wdired-use-interactive-rename' is nil."
185 :type 'boolean
186 :group 'wdired)
188 (defcustom wdired-always-move-to-filename-beginning nil
189 "*If t the \"up\" and \"down\" movement is done as in dired mode.
190 That is, always move the point to the beginning of the filename at line.
192 If `sometimes, only move to the beginning of filename if the point is
193 before it, and `track-eol' is honored. This behavior is very handy
194 when editing several filenames.
196 If nil, \"up\" and \"down\" movement is done as in any other buffer."
197 :type '(choice (const :tag "As in any other mode" nil)
198 (const :tag "Smart cursor placement" sometimes)
199 (other :tag "As in dired mode" t))
200 :group 'wdired)
202 (defcustom wdired-advise-functions t
203 "*If t some editing commands are advised when wdired is loaded.
204 The advice only has effect in wdired mode. These commands are
205 `query-replace' `query-replace-regexp' `replace-string', and the
206 advice makes them to ignore read-only regions, so no attempts to
207 modify these regions are done by them, and so they don't end
208 prematurely.
210 Setting this to nil does not unadvise the functions, if they are
211 already advised, but new Emacs will not advise them."
212 :type 'boolean
213 :group 'wdired)
215 (defcustom wdired-allow-to-redirect-links t
216 "*If non-nil, the target of the symbolic links can be changed also.
217 In systems without symbolic links support, this variable has no effect
218 at all."
219 :type 'boolean
220 :group 'wdired)
222 (defcustom wdired-allow-to-change-permissions nil
223 "*If non-nil, the permissions bits of the files can be changed also.
225 If t, to change a single bit, put the cursor over it and press the
226 space bar, or left click over it. You can also hit the letter you want
227 to set: if this value is allowed, the character in the buffer will be
228 changed. Anyway, the point is advanced one position, so, for example,
229 you can keep the \"x\" key pressed to give execution permissions to
230 everybody to that file.
232 If `advanced, the bits are freely editable. You can use
233 `string-rectangle', `query-replace', etc. You can put any value (even
234 newlines), but if you want your changes to be useful, you better put a
235 intelligible value.
237 Anyway, the real change of the permissions is done with the external
238 program `dired-chmod-program', which must exist."
239 :type '(choice (const :tag "Not allowed" nil)
240 (const :tag "Toggle/set bits" t)
241 (other :tag "Bits freely editable" advanced))
242 :group 'wdired)
244 (defvar wdired-mode-map
245 (let ((map (make-sparse-keymap)))
246 (define-key map "\C-x\C-s" 'wdired-finish-edit)
247 (define-key map "\C-c\C-c" 'wdired-finish-edit)
248 (define-key map "\C-c\C-k" 'wdired-abort-changes)
249 (define-key map "\C-c\C-[" 'wdired-abort-changes)
250 (define-key map "\C-m" 'wdired-newline)
251 (define-key map "\C-j" 'wdired-newline)
252 (define-key map "\C-o" 'wdired-newline)
253 (define-key map [up] 'wdired-previous-line)
254 (define-key map "\C-p" 'wdired-previous-line)
255 (define-key map [down] 'wdired-next-line)
256 (define-key map "\C-n" 'wdired-next-line)
258 (define-key map [menu-bar wdired]
259 (cons "WDired" (make-sparse-keymap "WDired")))
260 (define-key map [menu-bar wdired wdired-customize]
261 '("Options" . wdired-customize))
262 (define-key map [menu-bar wdired dashes]
263 '("--"))
264 (define-key map [menu-bar wdired wdired-abort-changes]
265 '("Abort Changes" . wdired-abort-changes))
266 (define-key map [menu-bar wdired wdired-finish-edit]
267 '("Commit Changes" . wdired-finish-edit))
268 ;; FIXME: Use the new remap trick.
269 (substitute-key-definition 'upcase-word 'wdired-upcase-word
270 map global-map)
271 (substitute-key-definition 'capitalize-word 'wdired-capitalize-word
272 map global-map)
273 (substitute-key-definition 'downcase-word 'wdired-downcase-word
274 map global-map)
275 map))
277 (defvar wdired-mode-hook nil
278 "Hook run when changing to wdired mode.")
280 ;; Local variables (put here to avoid compilation gripes)
281 (defvar wdired-col-perm) ;; Column where the permission bits start
282 (defvar wdired-old-content)
285 (defun wdired-mode ()
286 "\\<wdired-mode-map>File Names Editing mode.
288 Press \\[wdired-finish-edit] to make the changes to take effect and
289 exit. To abort the edit, use \\[wdired-abort-changes].
291 In this mode you can edit the names of the files, the target of the
292 links and the permission bits of the files. You can `customize-group'
293 wdired.
295 Editing things out of the filenames, or adding or deleting lines is
296 not allowed, because the rest of the buffer is read-only."
297 (interactive)
298 (error "This mode can be enabled only by `wdired-change-to-wdired-mode'"))
299 (put 'wdired-mode 'mode-class 'special)
302 ;;;###autoload
303 (defun wdired-change-to-wdired-mode ()
304 "Put a dired buffer in a mode in which filenames are editable.
305 In this mode the names of the files can be changed, and after
306 typing C-c C-c the files and directories in disk are renamed.
308 See `wdired-mode'."
309 (interactive)
310 (set (make-local-variable 'wdired-old-content)
311 (buffer-substring (point-min) (point-max)))
312 (use-local-map wdired-mode-map)
313 (force-mode-line-update)
314 (setq buffer-read-only nil)
315 (dired-unadvertise default-directory)
316 (add-hook 'kill-buffer-hook 'wdired-check-kill-buffer nil t)
317 (setq major-mode 'wdired-mode)
318 (setq mode-name "Edit filenames")
319 (setq revert-buffer-function 'wdired-revert)
320 ;; I temp disable undo for performance: since I'm going to clear the
321 ;; undo list, it can save more than a 9% of time with big
322 ;; directories because setting properties modify the undo-list.
323 (buffer-disable-undo)
324 (wdired-preprocess-files)
325 (if wdired-allow-to-change-permissions
326 (wdired-preprocess-perms))
327 (if (and wdired-allow-to-redirect-links (fboundp 'make-symbolic-link))
328 (wdired-preprocess-symlinks))
329 (buffer-enable-undo) ; Performance hack. See above.
330 (set-buffer-modified-p nil)
331 (setq buffer-undo-list nil)
332 (run-hooks 'wdired-mode-hook)
333 (message (substitute-command-keys "Press \\[wdired-finish-edit] when finished")))
336 ;; Protect the buffer so only the filenames can be changed, and put
337 ;; properties so filenames (old and new) can be easily found.
338 (defun wdired-preprocess-files ()
339 (put-text-property 1 2 'front-sticky t)
340 (save-excursion
341 (goto-char (point-min))
342 (let ((b-protection (point))
343 filename)
344 (while (not (eobp))
345 (setq filename (dired-get-filename nil t))
346 (when (and filename
347 (not (member (file-name-nondirectory filename) '("." ".."))))
348 (dired-move-to-filename)
349 (put-text-property (- (point) 2) (1- (point)) 'old-name filename)
350 (put-text-property b-protection (1- (point)) 'read-only t)
351 (setq b-protection (dired-move-to-end-of-filename t)))
352 (put-text-property (point) (1+ (point)) 'end-name t)
353 (forward-line))
354 (put-text-property b-protection (point-max) 'read-only t))))
356 ;; This code is a copy of some dired-get-filename lines.
357 (defsubst wdired-normalize-filename (file)
358 (setq file
359 ;; FIXME: shouldn't we check for a `b' argument or somesuch before
360 ;; doing such unquoting? --Stef
361 (read (concat
362 "\"" (replace-regexp-in-string
363 "\\([^\\]\\|\\`\\)\"" "\\1\\\\\"" file)
364 "\"")))
365 (and file buffer-file-coding-system
366 (not file-name-coding-system)
367 (not default-file-name-coding-system)
368 (setq file (encode-coding-string file buffer-file-coding-system)))
369 file)
371 (defun wdired-get-filename (&optional no-dir old)
372 "Return the filename at line.
373 Similar to `dired-get-filename' but it doesn't rely on regexps. It
374 relies on wdired buffer's properties. Optional arg NO-DIR with value
375 non-nil means don't include directory. Optional arg OLD with value
376 non-nil means return old filename."
377 ;; FIXME: Use dired-get-filename's new properties.
378 (let (beg end file)
379 (save-excursion
380 (setq end (progn (end-of-line) (point)))
381 (beginning-of-line)
382 (setq beg (next-single-property-change (point) 'old-name nil end))
383 (unless (eq beg end)
384 (if old
385 (setq file (get-text-property beg 'old-name))
386 (setq end (next-single-property-change (1+ beg) 'end-name))
387 (setq file (buffer-substring-no-properties (+ 2 beg) end)))
388 (and file (setq file (wdired-normalize-filename file))))
389 (if (or no-dir old)
390 file
391 (and file (> (length file) 0)
392 (concat (dired-current-directory) file))))))
395 (defun wdired-change-to-dired-mode ()
396 "Change the mode back to dired."
397 (let ((inhibit-read-only t))
398 (remove-text-properties (point-min) (point-max)
399 '(read-only nil local-map nil)))
400 (put-text-property 1 2 'front-sticky nil)
401 (use-local-map dired-mode-map)
402 (force-mode-line-update)
403 (setq buffer-read-only t)
404 (setq major-mode 'dired-mode)
405 (setq mode-name "Dired")
406 (dired-advertise)
407 (remove-hook 'kill-buffer-hook 'wdired-check-kill-buffer t)
408 (setq revert-buffer-function 'dired-revert))
411 (defun wdired-abort-changes ()
412 "Abort changes and return to dired mode."
413 (interactive)
414 (let ((inhibit-read-only t))
415 (erase-buffer)
416 (insert wdired-old-content))
417 (wdired-change-to-dired-mode)
418 (set-buffer-modified-p nil)
419 (setq buffer-undo-list nil)
420 (message "Changes aborted"))
422 (defun wdired-finish-edit ()
423 "Actually rename files based on your editing in the Dired buffer."
424 (interactive)
425 (wdired-change-to-dired-mode)
426 (let ((overwrite (or wdired-is-ok-overwrite 1))
427 (changes nil)
428 (files-deleted nil)
429 (errors 0)
430 file-ori file-new tmp-value)
431 (save-excursion
432 (if (and wdired-allow-to-redirect-links
433 (fboundp 'make-symbolic-link))
434 (progn
435 (setq tmp-value (wdired-do-symlink-changes))
436 (setq errors (cdr tmp-value))
437 (setq changes (car tmp-value))))
438 (if (and wdired-allow-to-change-permissions
439 (boundp 'wdired-col-perm)) ; could have been changed
440 (progn
441 (setq tmp-value (wdired-do-perm-changes))
442 (setq errors (+ errors (cdr tmp-value)))
443 (setq changes (or changes (car tmp-value)))))
444 (goto-char (point-max))
445 (while (not (bobp))
446 (setq file-ori (wdired-get-filename nil t))
447 (if file-ori
448 (setq file-new (wdired-get-filename)))
449 (if (and file-ori (not (equal file-new file-ori)))
450 (progn
451 (setq changes t)
452 (if (not file-new) ;empty filename!
453 (setq files-deleted (cons file-ori files-deleted))
454 (progn
455 (setq file-new (substitute-in-file-name file-new))
456 (if wdired-use-interactive-rename
457 (wdired-search-and-rename file-ori file-new)
458 (condition-case err
459 (let ((dired-backup-overwrite nil))
460 (dired-rename-file file-ori file-new
461 overwrite))
462 (error
463 (setq errors (1+ errors))
464 (dired-log (concat "Rename `" file-ori "' to `"
465 file-new "' failed:\n%s\n")
466 err))))))))
467 (forward-line -1)))
468 (if changes
469 (revert-buffer) ;The "revert" is necessary to re-sort the buffer
470 (let ((buffer-read-only nil))
471 (remove-text-properties (point-min) (point-max)
472 '(old-name nil end-name nil old-link nil
473 end-link nil end-perm nil
474 old-perm nil perm-changed nil))
475 (message "(No changes to be performed)")))
476 (if files-deleted
477 (wdired-flag-for-deletion files-deleted))
478 (if (> errors 0)
479 (dired-log-summary (format "%d rename actions failed" errors) nil)))
480 (set-buffer-modified-p nil)
481 (setq buffer-undo-list nil))
483 ;; Renames a file, searching it in a modified dired buffer, in order
484 ;; to be able to use `dired-do-create-files-regexp' and get its
485 ;; "benefits"
486 (defun wdired-search-and-rename (filename-ori filename-new)
487 (save-excursion
488 (goto-char (point-max))
489 (forward-line -1)
490 (let ((exit-while nil)
491 curr-filename)
492 (while (not exit-while)
493 (setq curr-filename (wdired-get-filename))
494 (if (and curr-filename
495 (equal (substitute-in-file-name curr-filename) filename-new))
496 (progn
497 (setq exit-while t)
498 (let ((inhibit-read-only t))
499 (dired-move-to-filename)
500 (search-forward (wdired-get-filename t) nil t)
501 (replace-match (file-name-nondirectory filename-ori) t t))
502 (dired-do-create-files-regexp
503 (function dired-rename-file)
504 "Move" 1 ".*" filename-new nil t))
505 (progn
506 (forward-line -1)
507 (beginning-of-line)
508 (setq exit-while (= 1 (point)))))))))
510 ;; marks a list of files for deletion
511 (defun wdired-flag-for-deletion (filenames-ori)
512 (save-excursion
513 (goto-char (point-min))
514 (while (not (eobp))
515 (if (member (dired-get-filename nil t) filenames-ori)
516 (dired-flag-file-deletion 1)
517 (forward-line)))))
519 (defun wdired-customize ()
520 "Customize wdired options."
521 (interactive)
522 (customize-apropos "wdired" 'groups))
524 (defun wdired-revert (&optional arg noconfirm)
525 "Discard changes in the buffer and update the changes in the disk."
526 (wdired-change-to-dired-mode)
527 (revert-buffer)
528 (wdired-change-to-wdired-mode))
530 (defun wdired-check-kill-buffer ()
531 ;; FIXME: Can't we use the normal mechanism for that? --Stef
532 (if (and
533 (buffer-modified-p)
534 (not (y-or-n-p "Buffer changed. Discard changes and kill buffer? ")))
535 (error nil)))
537 (defun wdired-next-line (arg)
538 "Move down lines then position at filename or the current column.
539 See `wdired-always-move-to-filename-beginning'. Optional prefix ARG
540 says how many lines to move; default is one line."
541 (interactive "p")
542 (next-line arg)
543 (if (or (eq wdired-always-move-to-filename-beginning t)
544 (and wdired-always-move-to-filename-beginning
545 (< (current-column)
546 (save-excursion (dired-move-to-filename)
547 (current-column)))))
548 (dired-move-to-filename)))
550 (defun wdired-previous-line (arg)
551 "Move up lines then position at filename or the current column.
552 See `wdired-always-move-to-filename-beginning'. Optional prefix ARG
553 says how many lines to move; default is one line."
554 (interactive "p")
555 (previous-line arg)
556 (if (or (eq wdired-always-move-to-filename-beginning t)
557 (and wdired-always-move-to-filename-beginning
558 (< (current-column)
559 (save-excursion (dired-move-to-filename)
560 (current-column)))))
561 (dired-move-to-filename)))
563 ;; dired doesn't works well with newlines, so ...
564 (defun wdired-newline ()
565 "Do nothing."
566 (interactive))
568 ;; Put the needed properties to allow the user to change links' targets
569 (defun wdired-preprocess-symlinks ()
570 (let ((inhibit-read-only t))
571 (save-excursion
572 (goto-char (point-min))
573 (while (not (eobp))
574 (if (looking-at dired-re-sym)
575 (progn
576 (re-search-forward " -> \\(.*\\)$")
577 (put-text-property (- (match-beginning 1) 2)
578 (1- (match-beginning 1)) 'old-link
579 (match-string-no-properties 1))
580 (put-text-property (match-end 1) (1+ (match-end 1)) 'end-link t)
581 (put-text-property (1- (match-beginning 1))
582 (match-end 1) 'read-only nil)))
583 (forward-line)
584 (beginning-of-line)))))
587 (defun wdired-get-previous-link (&optional old move)
588 "Return the next symlink target.
589 If OLD, return the old target. If MOVE, move point before it."
590 (let (beg end target)
591 (setq beg (previous-single-property-change (point) 'old-link nil))
592 (if beg
593 (progn
594 (if old
595 (setq target (get-text-property (1- beg) 'old-link))
596 (setq end (next-single-property-change beg 'end-link))
597 (setq target (buffer-substring-no-properties (1+ beg) end)))
598 (if move (goto-char (1- beg)))))
599 (and target (wdired-normalize-filename target))))
603 ;; Perform the changes in the target of the changed links.
604 (defun wdired-do-symlink-changes()
605 (let ((changes nil)
606 (errors 0)
607 link-to-ori link-to-new link-from)
608 (goto-char (point-max))
609 (while (setq link-to-new (wdired-get-previous-link))
610 (setq link-to-ori (wdired-get-previous-link t t))
611 (setq link-from (wdired-get-filename nil t))
612 (if (not (equal link-to-new link-to-ori))
613 (progn
614 (setq changes t)
615 (if (equal link-to-new "") ;empty filename!
616 (setq link-to-new "/dev/null"))
617 (condition-case err
618 (progn
619 (delete-file link-from)
620 (make-symbolic-link
621 (substitute-in-file-name link-to-new) link-from))
622 (error
623 (setq errors (1+ errors))
624 (dired-log (concat "Link `" link-from "' to `"
625 link-to-new "' failed:\n%s\n")
626 err))))))
627 (cons changes errors)))
629 ;; Perform a "case command" skipping read-only words.
630 (defun wdired-xcase-word (command arg)
631 (if (< arg 0)
632 (funcall command arg)
633 (progn
634 (while (> arg 0)
635 (condition-case err
636 (progn
637 (funcall command 1)
638 (setq arg (1- arg)))
639 (error
640 (if (not (forward-word 1))
641 (setq arg 0))))))))
643 (defun wdired-downcase-word (arg)
644 "Wdired version of `downcase-word'.
645 Like original function but it skips read-only words."
646 (interactive "p")
647 (wdired-xcase-word 'downcase-word arg))
649 (defun wdired-upcase-word (arg)
650 "Wdired version of `upcase-word'.
651 Like original function but it skips read-only words."
652 (interactive "p")
653 (wdired-xcase-word 'upcase-word arg))
655 (defun wdired-capitalize-word (arg)
656 "Wdired version of `capitalize-word'.
657 Like original function but it skips read-only words."
658 (interactive "p")
659 (wdired-xcase-word 'capitalize-word arg))
661 ;; The following code is related to advice some interactive functions
662 ;; to make some editing commands in wdired mode not to fail trying to
663 ;; change read-only text. Notice that some advises advice and unadvise
664 ;; them-self to another functions: search-forward and
665 ;; re-search-forward. This is to keep these functions advised only
666 ;; when is necessary. Since they are built-in commands used heavily in
667 ;; lots of places, to have it permanently advised would cause some
668 ;; performance loss.
671 (defun wdired-add-skip-in-replace (command)
672 "Advice COMMAND to skip matches while they have read-only properties.
673 This is useful to avoid \"read-only\" errors in search and replace
674 commands. This advice only has effect in wdired mode."
675 (eval
676 `(defadvice ,command (around wdired-discard-read-only activate)
677 ,(format "Make %s to work better with wdired,\n%s." command
678 "skipping read-only matches when invoked without argument")
679 ad-do-it
680 (if (eq major-mode 'wdired-mode)
681 (while (and ad-return-value
682 (text-property-any
683 (max 1 (1- (match-beginning 0))) (match-end 0)
684 'read-only t))
685 ad-do-it))
686 ad-return-value)))
689 (defun wdired-add-replace-advice (command)
690 "Advice COMMAND to skip matches while they have read-only properties.
691 This is useful to avoid \"read-only\" errors in search and replace
692 commands. This advice only has effect in wdired mode."
693 (eval
694 `(defadvice ,command (around wdired-grok-read-only activate)
695 ,(format "Make %s to work better with wdired,\n%s." command
696 "skipping read-only matches when invoked without argument")
697 (if (eq major-mode 'wdired-mode)
698 (progn
699 (wdired-add-skip-in-replace 'search-forward)
700 (wdired-add-skip-in-replace 're-search-forward)
701 (unwind-protect
702 ad-do-it
703 (progn
704 (ad-remove-advice 'search-forward
705 'around 'wdired-discard-read-only)
706 (ad-remove-advice 're-search-forward
707 'around 'wdired-discard-read-only)
708 (ad-update 'search-forward)
709 (ad-update 're-search-forward))))
710 ad-do-it)
711 ad-return-value)))
714 (if wdired-advise-functions
715 (progn
716 (mapcar 'wdired-add-replace-advice
717 '(query-replace query-replace-regexp replace-string))))
720 ;; The following code deals with changing the access bits (or
721 ;; permissions) of the files.
723 (defvar wdired-perm-mode-map nil)
724 (unless wdired-perm-mode-map
725 (setq wdired-perm-mode-map (copy-keymap wdired-mode-map))
726 (define-key wdired-perm-mode-map " " 'wdired-toggle-bit)
727 (define-key wdired-perm-mode-map "r" 'wdired-set-bit)
728 (define-key wdired-perm-mode-map "w" 'wdired-set-bit)
729 (define-key wdired-perm-mode-map "x" 'wdired-set-bit)
730 (define-key wdired-perm-mode-map "-" 'wdired-set-bit)
731 (define-key wdired-perm-mode-map "S" 'wdired-set-bit)
732 (define-key wdired-perm-mode-map "s" 'wdired-set-bit)
733 (define-key wdired-perm-mode-map "T" 'wdired-set-bit)
734 (define-key wdired-perm-mode-map "t" 'wdired-set-bit)
735 (define-key wdired-perm-mode-map "s" 'wdired-set-bit)
736 (define-key wdired-perm-mode-map "l" 'wdired-set-bit)
737 (define-key wdired-perm-mode-map [down-mouse-1] 'wdired-mouse-toggle-bit))
739 ;; Put a local-map to the permission bits of the files, and store the
740 ;; original name and permissions as a property
741 (defun wdired-preprocess-perms()
742 (let ((inhibit-read-only t)
743 filename)
744 (set (make-local-variable 'wdired-col-perm) nil)
745 (save-excursion
746 (goto-char (point-min))
747 (while (not (eobp))
748 (if (and (not (looking-at dired-re-sym))
749 (setq filename (wdired-get-filename)))
750 (progn
751 (re-search-forward dired-re-perms)
752 (or wdired-col-perm
753 (setq wdired-col-perm (- (current-column) 9)))
754 (if (eq wdired-allow-to-change-permissions 'advanced)
755 (put-text-property (match-beginning 0) (match-end 0)
756 'read-only nil)
757 (put-text-property (1+ (match-beginning 0)) (match-end 0)
758 'local-map wdired-perm-mode-map))
759 (put-text-property (match-end 0) (1+ (match-end 0)) 'end-perm t)
760 (put-text-property (match-beginning 0) (1+ (match-beginning 0))
761 'old-perm (match-string-no-properties 0))))
762 (forward-line)
763 (beginning-of-line)))))
765 (defun wdired-perm-allowed-in-pos (char pos)
766 (cond
767 ((= char ?-) t)
768 ((= char ?r) (= (% pos 3) 0))
769 ((= char ?w) (= (% pos 3) 1))
770 ((= char ?x) (= (% pos 3) 2))
771 ((memq char '(?s ?S)) (memq pos '(2 5)))
772 ((memq char '(?t ?T)) (= pos 8))
773 ((= char ?l) (= pos 5))))
775 (defun wdired-set-bit ()
776 "Set a permission bit character."
777 (interactive)
778 (if (wdired-perm-allowed-in-pos last-command-char
779 (- (current-column) wdired-col-perm))
780 (let ((new-bit (char-to-string last-command-char))
781 (inhibit-read-only t)
782 (pos-prop (- (point) (- (current-column) wdired-col-perm))))
783 (put-text-property 0 1 'local-map wdired-perm-mode-map new-bit)
784 (put-text-property 0 1 'read-only t new-bit)
785 (insert new-bit)
786 (delete-char 1)
787 (put-text-property pos-prop (1- pos-prop) 'perm-changed t))
788 (forward-char 1)))
790 (defun wdired-toggle-bit()
791 "Toggle the permission bit at point."
792 (interactive)
793 (let ((inhibit-read-only t)
794 (new-bit "-")
795 (pos-prop (- (point) (- (current-column) wdired-col-perm))))
796 (if (eq (char-after (point)) ?-)
797 (setq new-bit
798 (if (= (% (- (current-column) wdired-col-perm) 3) 0) "r"
799 (if (= (% (- (current-column) wdired-col-perm) 3) 1) "w"
800 "x"))))
801 (put-text-property 0 1 'local-map wdired-perm-mode-map new-bit)
802 (put-text-property 0 1 'read-only t new-bit)
803 (insert new-bit)
804 (delete-char 1)
805 (put-text-property pos-prop (1- pos-prop) 'perm-changed t)))
807 (defun wdired-mouse-toggle-bit (event)
808 "Toggle the permission bit that was left clicked."
809 (interactive "e")
810 (mouse-set-point event)
811 (wdired-toggle-bit))
813 ;; Allowed chars for 4000 bit are Ss in position 3
814 ;; Allowed chars for 2000 bit are Ssl in position 6
815 ;; Allowed chars for 1000 bit are Tt in position 9
816 (defun wdired-perms-to-number (perms)
817 (let ((nperm 0777))
818 (if (= (elt perms 1) ?-) (setq nperm (- nperm 400)))
819 (if (= (elt perms 2) ?-) (setq nperm (- nperm 200)))
820 (let ((p-bit (elt perms 3)))
821 (if (memq p-bit '(?- ?S)) (setq nperm (- nperm 100)))
822 (if (memq p-bit '(?s ?S)) (setq nperm (+ nperm 4000))))
823 (if (= (elt perms 4) ?-) (setq nperm (- nperm 40)))
824 (if (= (elt perms 5) ?-) (setq nperm (- nperm 20)))
825 (let ((p-bit (elt perms 6)))
826 (if (memq p-bit '(?- ?S ?l)) (setq nperm (- nperm 10)))
827 (if (memq p-bit '(?s ?S ?l)) (setq nperm (+ nperm 2000))))
828 (if (= (elt perms 7) ?-) (setq nperm (- nperm 4)))
829 (if (= (elt perms 8) ?-) (setq nperm (- nperm 2)))
830 (let ((p-bit (elt perms 9)))
831 (if (memq p-bit '(?- ?T)) (setq nperm (- nperm 1)))
832 (if (memq p-bit '(?t ?T)) (setq nperm (+ nperm 1000))))
833 nperm))
835 ;; Perform the changes in the permissions of the files that have
836 ;; changed.
837 (defun wdired-do-perm-changes ()
838 (let ((changes nil)
839 (errors 0)
840 (prop-wanted (if (eq wdired-allow-to-change-permissions 'advanced)
841 'old-perm 'perm-changed))
842 filename perms-ori perms-new perm-tmp)
843 (goto-char (next-single-property-change (point-min) prop-wanted
844 nil (point-max)))
845 (while (not (eobp))
846 (setq perms-ori (get-text-property (point) 'old-perm))
847 (setq perms-new (buffer-substring-no-properties
848 (point) (next-single-property-change (point) 'end-perm)))
849 (if (not (equal perms-ori perms-new))
850 (progn
851 (setq changes t)
852 (setq filename (wdired-get-filename nil t))
853 (if (= (length perms-new) 10)
854 (progn
855 (setq perm-tmp
856 (int-to-string (wdired-perms-to-number perms-new)))
857 (if (not (equal 0 (dired-call-process dired-chmod-program
858 t perm-tmp filename)))
859 (progn
860 (setq errors (1+ errors))
861 (dired-log (concat dired-chmod-program " " perm-tmp
862 " `" filename "' failed\n\n")))))
863 (setq errors (1+ errors))
864 (dired-log (concat "Cannot parse permission `" perms-new
865 "' for file `" filename "'\n\n")))))
866 (goto-char (next-single-property-change (1+ (point)) prop-wanted
867 nil (point-max))))
868 (cons changes errors)))
870 (provide 'wdired)
872 ;; arch-tag: bc00902e-526f-4305-bc7f-8862a559184f
873 ;;; wdired.el ends here