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