Add "(require 'eshell)", to get necessary features
[emacs.git] / lisp / wdired.el
bloba8c36c2066feb68c333cceae3014e190cd145968
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 "Press C-c C-c 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))
421 (defun wdired-finish-edit ()
422 "Actually rename files based on your editing in the Dired buffer."
423 (interactive)
424 (wdired-change-to-dired-mode)
425 (let ((overwrite (or wdired-is-ok-overwrite 1))
426 (changes nil)
427 (files-deleted nil)
428 (errors 0)
429 file-ori file-new tmp-value)
430 (save-excursion
431 (if (and wdired-allow-to-redirect-links
432 (fboundp 'make-symbolic-link))
433 (progn
434 (setq tmp-value (wdired-do-symlink-changes))
435 (setq errors (cdr tmp-value))
436 (setq changes (car tmp-value))))
437 (if (and wdired-allow-to-change-permissions
438 (boundp 'wdired-col-perm)) ; could have been changed
439 (progn
440 (setq tmp-value (wdired-do-perm-changes))
441 (setq errors (+ errors (cdr tmp-value)))
442 (setq changes (or changes (car tmp-value)))))
443 (goto-char (point-max))
444 (while (not (bobp))
445 (setq file-ori (wdired-get-filename nil t))
446 (if file-ori
447 (setq file-new (wdired-get-filename)))
448 (if (and file-ori (not (equal file-new file-ori)))
449 (progn
450 (setq changes t)
451 (if (not file-new) ;empty filename!
452 (setq files-deleted (cons file-ori files-deleted))
453 (progn
454 (setq file-new (substitute-in-file-name file-new))
455 (if wdired-use-interactive-rename
456 (wdired-search-and-rename file-ori file-new)
457 (condition-case err
458 (let ((dired-backup-overwrite nil))
459 (dired-rename-file file-ori file-new
460 overwrite))
461 (error
462 (setq errors (1+ errors))
463 (dired-log (concat "Rename `" file-ori "' to `"
464 file-new "' failed:\n%s\n")
465 err))))))))
466 (forward-line -1)))
467 (if changes
468 (revert-buffer) ;The "revert" is necessary to re-sort the buffer
469 (let ((buffer-read-only nil))
470 (remove-text-properties (point-min) (point-max)
471 '(old-name nil end-name nil old-link nil
472 end-link nil end-perm nil
473 old-perm nil perm-changed nil))
474 (message "(No changes to be performed)")))
475 (if files-deleted
476 (wdired-flag-for-deletion files-deleted))
477 (if (> errors 0)
478 (dired-log-summary (format "%d rename actions failed" errors) nil)))
479 (set-buffer-modified-p nil)
480 (setq buffer-undo-list nil))
482 ;; Renames a file, searching it in a modified dired buffer, in order
483 ;; to be able to use `dired-do-create-files-regexp' and get its
484 ;; "benefits"
485 (defun wdired-search-and-rename (filename-ori filename-new)
486 (save-excursion
487 (goto-char (point-max))
488 (forward-line -1)
489 (let ((exit-while nil)
490 curr-filename)
491 (while (not exit-while)
492 (setq curr-filename (wdired-get-filename))
493 (if (and curr-filename
494 (equal (substitute-in-file-name curr-filename) filename-new))
495 (progn
496 (setq exit-while t)
497 (let ((inhibit-read-only t))
498 (dired-move-to-filename)
499 (search-forward (wdired-get-filename t) nil t)
500 (replace-match (file-name-nondirectory filename-ori) t t))
501 (dired-do-create-files-regexp
502 (function dired-rename-file)
503 "Move" 1 ".*" filename-new nil t))
504 (progn
505 (forward-line -1)
506 (beginning-of-line)
507 (setq exit-while (= 1 (point)))))))))
509 ;; marks a list of files for deletion
510 (defun wdired-flag-for-deletion (filenames-ori)
511 (save-excursion
512 (goto-char (point-min))
513 (while (not (eobp))
514 (if (member (dired-get-filename nil t) filenames-ori)
515 (dired-flag-file-deletion 1)
516 (forward-line)))))
518 (defun wdired-customize ()
519 "Customize wdired options."
520 (interactive)
521 (customize-apropos "wdired" 'groups))
523 (defun wdired-revert (&optional arg noconfirm)
524 "Discard changes in the buffer and update the changes in the disk."
525 (wdired-change-to-dired-mode)
526 (revert-buffer)
527 (wdired-change-to-wdired-mode))
529 (defun wdired-check-kill-buffer ()
530 ;; FIXME: Can't we use the normal mechanism for that? --Stef
531 (if (and
532 (buffer-modified-p)
533 (not (y-or-n-p "Buffer changed. Discard changes and kill buffer? ")))
534 (error nil)))
536 (defun wdired-next-line (arg)
537 "Move down lines then position at filename or the current column.
538 See `wdired-always-move-to-filename-beginning'. Optional prefix ARG
539 says how many lines to move; default is one line."
540 (interactive "p")
541 (next-line arg)
542 (if (or (eq wdired-always-move-to-filename-beginning t)
543 (and wdired-always-move-to-filename-beginning
544 (< (current-column)
545 (save-excursion (dired-move-to-filename)
546 (current-column)))))
547 (dired-move-to-filename)))
549 (defun wdired-previous-line (arg)
550 "Move up lines then position at filename or the current column.
551 See `wdired-always-move-to-filename-beginning'. Optional prefix ARG
552 says how many lines to move; default is one line."
553 (interactive "p")
554 (previous-line arg)
555 (if (or (eq wdired-always-move-to-filename-beginning t)
556 (and wdired-always-move-to-filename-beginning
557 (< (current-column)
558 (save-excursion (dired-move-to-filename)
559 (current-column)))))
560 (dired-move-to-filename)))
562 ;; dired doesn't works well with newlines, so ...
563 (defun wdired-newline ()
564 "Do nothing."
565 (interactive))
567 ;; Put the needed properties to allow the user to change links' targets
568 (defun wdired-preprocess-symlinks ()
569 (let ((inhibit-read-only t))
570 (save-excursion
571 (goto-char (point-min))
572 (while (not (eobp))
573 (if (looking-at dired-re-sym)
574 (progn
575 (re-search-forward " -> \\(.*\\)$")
576 (put-text-property (- (match-beginning 1) 2)
577 (1- (match-beginning 1)) 'old-link
578 (match-string-no-properties 1))
579 (put-text-property (match-end 1) (1+ (match-end 1)) 'end-link t)
580 (put-text-property (1- (match-beginning 1))
581 (match-end 1) 'read-only nil)))
582 (forward-line)
583 (beginning-of-line)))))
586 (defun wdired-get-previous-link (&optional old move)
587 "Return the next symlink target.
588 If OLD, return the old target. If MOVE, move point before it."
589 (let (beg end target)
590 (setq beg (previous-single-property-change (point) 'old-link nil))
591 (if beg
592 (progn
593 (if old
594 (setq target (get-text-property (1- beg) 'old-link))
595 (setq end (next-single-property-change beg 'end-link))
596 (setq target (buffer-substring-no-properties (1+ beg) end)))
597 (if move (goto-char (1- beg)))))
598 (and target (wdired-normalize-filename target))))
602 ;; Perform the changes in the target of the changed links.
603 (defun wdired-do-symlink-changes()
604 (let ((changes nil)
605 (errors 0)
606 link-to-ori link-to-new link-from)
607 (goto-char (point-max))
608 (while (setq link-to-new (wdired-get-previous-link))
609 (setq link-to-ori (wdired-get-previous-link t t))
610 (setq link-from (wdired-get-filename nil t))
611 (if (not (equal link-to-new link-to-ori))
612 (progn
613 (setq changes t)
614 (if (equal link-to-new "") ;empty filename!
615 (setq link-to-new "/dev/null"))
616 (condition-case err
617 (progn
618 (delete-file link-from)
619 (make-symbolic-link
620 (substitute-in-file-name link-to-new) link-from))
621 (error
622 (setq errors (1+ errors))
623 (dired-log (concat "Link `" link-from "' to `"
624 link-to-new "' failed:\n%s\n")
625 err))))))
626 (cons changes errors)))
628 ;; Perform a "case command" skipping read-only words.
629 (defun wdired-xcase-word (command arg)
630 (if (< arg 0)
631 (funcall command arg)
632 (progn
633 (while (> arg 0)
634 (condition-case err
635 (progn
636 (funcall command 1)
637 (setq arg (1- arg)))
638 (error
639 (if (not (forward-word 1))
640 (setq arg 0))))))))
642 (defun wdired-downcase-word (arg)
643 "Wdired version of `downcase-word'.
644 Like original function but it skips read-only words."
645 (interactive "p")
646 (wdired-xcase-word 'downcase-word arg))
648 (defun wdired-upcase-word (arg)
649 "Wdired version of `upcase-word'.
650 Like original function but it skips read-only words."
651 (interactive "p")
652 (wdired-xcase-word 'upcase-word arg))
654 (defun wdired-capitalize-word (arg)
655 "Wdired version of `capitalize-word'.
656 Like original function but it skips read-only words."
657 (interactive "p")
658 (wdired-xcase-word 'capitalize-word arg))
660 ;; The following code is related to advice some interactive functions
661 ;; to make some editing commands in wdired mode not to fail trying to
662 ;; change read-only text. Notice that some advises advice and unadvise
663 ;; them-self to another functions: search-forward and
664 ;; re-search-forward. This is to keep these functions advised only
665 ;; when is necessary. Since they are built-in commands used heavily in
666 ;; lots of places, to have it permanently advised would cause some
667 ;; performance loss.
670 (defun wdired-add-skip-in-replace (command)
671 "Advice COMMAND to skip matches while they have read-only properties.
672 This is useful to avoid \"read-only\" errors in search and replace
673 commands. This advice only has effect in wdired mode."
674 (eval
675 `(defadvice ,command (around wdired-discard-read-only activate)
676 ,(format "Make %s to work better with wdired,\n%s." command
677 "skipping read-only matches when invoked without argument")
678 ad-do-it
679 (if (eq major-mode 'wdired-mode)
680 (while (and ad-return-value
681 (text-property-any
682 (max 1 (1- (match-beginning 0))) (match-end 0)
683 'read-only t))
684 ad-do-it))
685 ad-return-value)))
688 (defun wdired-add-replace-advice (command)
689 "Advice COMMAND to skip matches while they have read-only properties.
690 This is useful to avoid \"read-only\" errors in search and replace
691 commands. This advice only has effect in wdired mode."
692 (eval
693 `(defadvice ,command (around wdired-grok-read-only activate)
694 ,(format "Make %s to work better with wdired,\n%s." command
695 "skipping read-only matches when invoked without argument")
696 (if (eq major-mode 'wdired-mode)
697 (progn
698 (wdired-add-skip-in-replace 'search-forward)
699 (wdired-add-skip-in-replace 're-search-forward)
700 (unwind-protect
701 ad-do-it
702 (progn
703 (ad-remove-advice 'search-forward
704 'around 'wdired-discard-read-only)
705 (ad-remove-advice 're-search-forward
706 'around 'wdired-discard-read-only)
707 (ad-update 'search-forward)
708 (ad-update 're-search-forward))))
709 ad-do-it)
710 ad-return-value)))
713 (if wdired-advise-functions
714 (progn
715 (mapcar 'wdired-add-replace-advice
716 '(query-replace query-replace-regexp replace-string))))
719 ;; The following code deals with changing the access bits (or
720 ;; permissions) of the files.
722 (defvar wdired-perm-mode-map nil)
723 (unless wdired-perm-mode-map
724 (setq wdired-perm-mode-map (copy-keymap wdired-mode-map))
725 (define-key wdired-perm-mode-map " " 'wdired-toggle-bit)
726 (define-key wdired-perm-mode-map "r" 'wdired-set-bit)
727 (define-key wdired-perm-mode-map "w" 'wdired-set-bit)
728 (define-key wdired-perm-mode-map "x" 'wdired-set-bit)
729 (define-key wdired-perm-mode-map "-" 'wdired-set-bit)
730 (define-key wdired-perm-mode-map "S" 'wdired-set-bit)
731 (define-key wdired-perm-mode-map "s" 'wdired-set-bit)
732 (define-key wdired-perm-mode-map "T" 'wdired-set-bit)
733 (define-key wdired-perm-mode-map "t" 'wdired-set-bit)
734 (define-key wdired-perm-mode-map "s" 'wdired-set-bit)
735 (define-key wdired-perm-mode-map "l" 'wdired-set-bit)
736 (define-key wdired-perm-mode-map [down-mouse-1] 'wdired-mouse-toggle-bit))
738 ;; Put a local-map to the permission bits of the files, and store the
739 ;; original name and permissions as a property
740 (defun wdired-preprocess-perms()
741 (let ((inhibit-read-only t)
742 filename)
743 (set (make-local-variable 'wdired-col-perm) nil)
744 (save-excursion
745 (goto-char (point-min))
746 (while (not (eobp))
747 (if (and (not (looking-at dired-re-sym))
748 (setq filename (wdired-get-filename)))
749 (progn
750 (re-search-forward dired-re-perms)
751 (or wdired-col-perm
752 (setq wdired-col-perm (- (current-column) 9)))
753 (if (eq wdired-allow-to-change-permissions 'advanced)
754 (put-text-property (match-beginning 0) (match-end 0)
755 'read-only nil)
756 (put-text-property (1+ (match-beginning 0)) (match-end 0)
757 'local-map wdired-perm-mode-map))
758 (put-text-property (match-end 0) (1+ (match-end 0)) 'end-perm t)
759 (put-text-property (match-beginning 0) (1+ (match-beginning 0))
760 'old-perm (match-string-no-properties 0))))
761 (forward-line)
762 (beginning-of-line)))))
764 (defun wdired-perm-allowed-in-pos (char pos)
765 (cond
766 ((= char ?-) t)
767 ((= char ?r) (= (% pos 3) 0))
768 ((= char ?w) (= (% pos 3) 1))
769 ((= char ?x) (= (% pos 3) 2))
770 ((memq char '(?s ?S)) (memq pos '(2 5)))
771 ((memq char '(?t ?T)) (= pos 8))
772 ((= char ?l) (= pos 5))))
774 (defun wdired-set-bit ()
775 "Set a permission bit character."
776 (interactive)
777 (if (wdired-perm-allowed-in-pos last-command-char
778 (- (current-column) wdired-col-perm))
779 (let ((new-bit (char-to-string last-command-char))
780 (inhibit-read-only t)
781 (pos-prop (- (point) (- (current-column) wdired-col-perm))))
782 (put-text-property 0 1 'local-map wdired-perm-mode-map new-bit)
783 (put-text-property 0 1 'read-only t new-bit)
784 (insert new-bit)
785 (delete-char 1)
786 (put-text-property pos-prop (1- pos-prop) 'perm-changed t))
787 (forward-char 1)))
789 (defun wdired-toggle-bit()
790 "Toggle the permission bit at point."
791 (interactive)
792 (let ((inhibit-read-only t)
793 (new-bit "-")
794 (pos-prop (- (point) (- (current-column) wdired-col-perm))))
795 (if (eq (char-after (point)) ?-)
796 (setq new-bit
797 (if (= (% (- (current-column) wdired-col-perm) 3) 0) "r"
798 (if (= (% (- (current-column) wdired-col-perm) 3) 1) "w"
799 "x"))))
800 (put-text-property 0 1 'local-map wdired-perm-mode-map new-bit)
801 (put-text-property 0 1 'read-only t new-bit)
802 (insert new-bit)
803 (delete-char 1)
804 (put-text-property pos-prop (1- pos-prop) 'perm-changed t)))
806 (defun wdired-mouse-toggle-bit (event)
807 "Toggle the permission bit that was left clicked."
808 (interactive "e")
809 (mouse-set-point event)
810 (wdired-toggle-bit))
812 ;; Allowed chars for 4000 bit are Ss in position 3
813 ;; Allowed chars for 2000 bit are Ssl in position 6
814 ;; Allowed chars for 1000 bit are Tt in position 9
815 (defun wdired-perms-to-number (perms)
816 (let ((nperm 0777))
817 (if (= (elt perms 1) ?-) (setq nperm (- nperm 400)))
818 (if (= (elt perms 2) ?-) (setq nperm (- nperm 200)))
819 (let ((p-bit (elt perms 3)))
820 (if (memq p-bit '(?- ?S)) (setq nperm (- nperm 100)))
821 (if (memq p-bit '(?s ?S)) (setq nperm (+ nperm 4000))))
822 (if (= (elt perms 4) ?-) (setq nperm (- nperm 40)))
823 (if (= (elt perms 5) ?-) (setq nperm (- nperm 20)))
824 (let ((p-bit (elt perms 6)))
825 (if (memq p-bit '(?- ?S ?l)) (setq nperm (- nperm 10)))
826 (if (memq p-bit '(?s ?S ?l)) (setq nperm (+ nperm 2000))))
827 (if (= (elt perms 7) ?-) (setq nperm (- nperm 4)))
828 (if (= (elt perms 8) ?-) (setq nperm (- nperm 2)))
829 (let ((p-bit (elt perms 9)))
830 (if (memq p-bit '(?- ?T)) (setq nperm (- nperm 1)))
831 (if (memq p-bit '(?t ?T)) (setq nperm (+ nperm 1000))))
832 nperm))
834 ;; Perform the changes in the permissions of the files that have
835 ;; changed.
836 (defun wdired-do-perm-changes ()
837 (let ((changes nil)
838 (errors 0)
839 (prop-wanted (if (eq wdired-allow-to-change-permissions 'advanced)
840 'old-perm 'perm-changed))
841 filename perms-ori perms-new perm-tmp)
842 (goto-char (next-single-property-change (point-min) prop-wanted
843 nil (point-max)))
844 (while (not (eobp))
845 (setq perms-ori (get-text-property (point) 'old-perm))
846 (setq perms-new (buffer-substring-no-properties
847 (point) (next-single-property-change (point) 'end-perm)))
848 (if (not (equal perms-ori perms-new))
849 (progn
850 (setq changes t)
851 (setq filename (wdired-get-filename nil t))
852 (if (= (length perms-new) 10)
853 (progn
854 (setq perm-tmp
855 (int-to-string (wdired-perms-to-number perms-new)))
856 (if (not (equal 0 (dired-call-process dired-chmod-program
857 t perm-tmp filename)))
858 (progn
859 (setq errors (1+ errors))
860 (dired-log (concat dired-chmod-program " " perm-tmp
861 " `" filename "' failed\n\n")))))
862 (setq errors (1+ errors))
863 (dired-log (concat "Cannot parse permission `" perms-new
864 "' for file `" filename "'\n\n")))))
865 (goto-char (next-single-property-change (1+ (point)) prop-wanted
866 nil (point-max))))
867 (cons changes errors)))
869 (provide 'wdired)
871 ;; arch-tag: bc00902e-526f-4305-bc7f-8862a559184f
872 ;;; wdired.el ends here