Fix bug #13515 with processing DBCS file names on MS-Windows.
[emacs.git] / lisp / wdired.el
blob59a09d6d5a07e4169bb6568ce6903b85e25095a1
1 ;;; wdired.el --- Rename files editing their names in dired buffers
3 ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
5 ;; Filename: wdired.el
6 ;; Author: Juan León Lahoz García <juanleon1@gmail.com>
7 ;; Version: 2.0
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 modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; wdired.el (the "w" is for writable) provides an alternative way of
28 ;; renaming files.
30 ;; Have you ever wished to use C-x r t (string-rectangle), M-%
31 ;; (query-replace), M-c (capitalize-word), etc... to change the name of
32 ;; the files in a "dired" buffer? Now you can do this. All the power
33 ;; of Emacs commands are available to renaming files!
35 ;; This package provides a function that makes the filenames of a
36 ;; dired buffer editable, by changing the buffer mode (which inhibits
37 ;; all of the commands of dired mode). Here you can edit the names of
38 ;; one or more files and directories, and when you press C-c C-c, the
39 ;; renaming takes effect and you are back to dired mode.
41 ;; Another things you can do with WDired:
43 ;; - To move files to another directory (by typing their path,
44 ;; absolute or relative, as a part of the new filename).
46 ;; - To change the target of symbolic links.
48 ;; - To change the permission bits of the filenames (in systems with a
49 ;; working unix-alike `dired-chmod-program'). See and customize the
50 ;; variable `wdired-allow-to-change-permissions'. To change a single
51 ;; char (toggling between its two more usual values) you can press
52 ;; the space bar over it or left-click the mouse. To set any char to
53 ;; an specific value (this includes the SUID, SGID and STI bits) you
54 ;; can use the key labeled as the letter you want. Please note that
55 ;; permissions of the links cannot be changed in that way, because
56 ;; the change would affect to their targets, and this would not be
57 ;; WYSIWYG :-).
59 ;; - To mark files for deletion, by deleting their whole filename.
61 ;;; Usage:
63 ;; You can edit the names of the files by typing C-x C-q or by
64 ;; executing M-x wdired-change-to-wdired-mode. Use C-c C-c when
65 ;; finished or C-c C-k to abort. While editing filenames, a new
66 ;; submenu "WDired" is available at top level. You can customize the
67 ;; behavior of this package from this menu.
69 ;;; Change Log:
71 ;; Google is your friend (previous versions with complete changelogs
72 ;; were posted to gnu.emacs.sources)
74 ;;; Code:
76 (defvar dired-backup-overwrite) ; Only in Emacs 20.x this is a custom var
78 (require 'dired)
79 (autoload 'dired-do-create-files-regexp "dired-aux")
81 (defgroup wdired nil
82 "Mode to rename files by editing their names in dired buffers."
83 :group 'dired)
85 (defcustom wdired-use-interactive-rename nil
86 "If non-nil, WDired requires confirmation before actually renaming files.
87 If nil, WDired doesn't require confirmation to change the file names,
88 and the variable `wdired-confirm-overwrite' controls whether it is ok
89 to overwrite files without asking."
90 :type 'boolean
91 :group 'wdired)
93 (defcustom wdired-confirm-overwrite t
94 "If nil the renames can overwrite files without asking.
95 This variable has no effect at all if `wdired-use-interactive-rename'
96 is not nil."
97 :type 'boolean
98 :group 'wdired)
100 (defcustom wdired-use-dired-vertical-movement nil
101 "If t, the \"up\" and \"down\" movement works as in Dired mode.
102 That is, always move the point to the beginning of the filename at line.
104 If `sometimes', only move to the beginning of filename if the point is
105 before it, and `track-eol' is non-nil. This behavior is very handy
106 when editing several filenames.
108 If nil, \"up\" and \"down\" movement is done as in any other buffer."
109 :type '(choice (const :tag "As in any other mode" nil)
110 (const :tag "Smart cursor placement" sometimes)
111 (other :tag "As in dired mode" t))
112 :group 'wdired)
114 (defcustom wdired-allow-to-redirect-links t
115 "If non-nil, the target of the symbolic links are editable.
116 In systems without symbolic links support, this variable has no effect
117 at all."
118 :type 'boolean
119 :group 'wdired)
121 (defcustom wdired-allow-to-change-permissions nil
122 "If non-nil, the permissions bits of the files are editable.
124 If t, to change a single bit, put the cursor over it and press the
125 space bar, or left click over it. You can also hit the letter you want
126 to set: if this value is allowed, the character in the buffer will be
127 changed. Anyway, the point is advanced one position, so, for example,
128 you can keep the <x> key pressed to give execution permissions to
129 everybody to that file.
131 If `advanced', the bits are freely editable. You can use
132 `string-rectangle', `query-replace', etc. You can put any value (even
133 newlines), but if you want your changes to be useful, you better put a
134 intelligible value.
136 Anyway, the real change of the permissions is done by the external
137 program `dired-chmod-program', which must exist."
138 :type '(choice (const :tag "Not allowed" nil)
139 (const :tag "Toggle/set bits" t)
140 (other :tag "Bits freely editable" advanced))
141 :group 'wdired)
143 (defcustom wdired-keep-marker-rename t
144 ;; Use t as default so that renamed files "take their markers with them".
145 "Controls marking of files renamed in WDired.
146 If t, files keep their previous marks when they are renamed.
147 If a character, renamed files (whether previously marked or not)
148 are afterward marked with that character.
149 This option affects only files renamed by `wdired-finish-edit'.
150 See `dired-keep-marker-rename' if you want to do the same for files
151 renamed by `dired-do-rename' and `dired-do-rename-regexp'."
152 :type '(choice (const :tag "Keep" t)
153 (character :tag "Mark" :value ?R))
154 :version "24.3"
155 :group 'wdired)
157 (defvar wdired-mode-map
158 (let ((map (make-sparse-keymap)))
159 (define-key map "\C-x\C-s" 'wdired-finish-edit)
160 (define-key map "\C-c\C-c" 'wdired-finish-edit)
161 (define-key map "\C-c\C-k" 'wdired-abort-changes)
162 (define-key map "\C-c\C-[" 'wdired-abort-changes)
163 (define-key map "\C-x\C-q" 'wdired-exit)
164 (define-key map "\C-m" 'ignore)
165 (define-key map "\C-j" 'ignore)
166 (define-key map "\C-o" 'ignore)
167 (define-key map [up] 'wdired-previous-line)
168 (define-key map "\C-p" 'wdired-previous-line)
169 (define-key map [down] 'wdired-next-line)
170 (define-key map "\C-n" 'wdired-next-line)
172 (define-key map [menu-bar wdired]
173 (cons "WDired" (make-sparse-keymap "WDired")))
174 (define-key map [menu-bar wdired wdired-customize]
175 '("Options" . wdired-customize))
176 (define-key map [menu-bar wdired dashes]
177 '("--"))
178 (define-key map [menu-bar wdired wdired-abort-changes]
179 '(menu-item "Abort Changes" wdired-abort-changes
180 :help "Abort changes and return to dired mode"))
181 (define-key map [menu-bar wdired wdired-finish-edit]
182 '("Commit Changes" . wdired-finish-edit))
184 (define-key map [remap upcase-word] 'wdired-upcase-word)
185 (define-key map [remap capitalize-word] 'wdired-capitalize-word)
186 (define-key map [remap downcase-word] 'wdired-downcase-word)
188 map))
190 (defvar wdired-mode-hook nil
191 "Hooks run when changing to WDired mode.")
193 ;; Local variables (put here to avoid compilation gripes)
194 (defvar wdired-col-perm) ;; Column where the permission bits start
195 (defvar wdired-old-content)
196 (defvar wdired-old-point)
197 (defvar wdired-old-marks)
199 (defun wdired-mode ()
200 "Writable Dired (WDired) mode.
201 \\<wdired-mode-map>
202 In WDired mode, you can edit the names of the files in the
203 buffer, the target of the links, and the permission bits of the
204 files.
206 Type \\[wdired-finish-edit] to exit WDired mode, returning to
207 Dired mode, and make your edits \"take effect\" by modifying the
208 file and directory names, link targets, and/or file permissions
209 on disk. If you delete the filename of a file, it is flagged for
210 deletion in the Dired buffer.
212 Type \\[wdired-abort-changes] to abort your edits and exit WDired mode.
214 Type \\[customize-group] RET wdired to customize WDired behavior.
216 The only editable texts in a WDired buffer are filenames,
217 symbolic link targets, and filenames permission."
218 (interactive)
219 (error "This mode can be enabled only by `wdired-change-to-wdired-mode'"))
220 (put 'wdired-mode 'mode-class 'special)
223 ;;;###autoload
224 (defun wdired-change-to-wdired-mode ()
225 "Put a Dired buffer in Writable Dired (WDired) mode.
226 \\<wdired-mode-map>
227 In WDired mode, you can edit the names of the files in the
228 buffer, the target of the links, and the permission bits of the
229 files. After typing \\[wdired-finish-edit], Emacs modifies the files and
230 directories to reflect your edits.
232 See `wdired-mode'."
233 (interactive)
234 (unless (eq major-mode 'dired-mode)
235 (error "Not a Dired buffer"))
236 (set (make-local-variable 'wdired-old-content)
237 (buffer-substring (point-min) (point-max)))
238 (set (make-local-variable 'wdired-old-marks)
239 (dired-remember-marks (point-min) (point-max)))
240 (set (make-local-variable 'wdired-old-point) (point))
241 (set (make-local-variable 'query-replace-skip-read-only) t)
242 (set (make-local-variable 'isearch-filter-predicate)
243 'wdired-isearch-filter-read-only)
244 (use-local-map wdired-mode-map)
245 (force-mode-line-update)
246 (setq buffer-read-only nil)
247 (dired-unadvertise default-directory)
248 (add-hook 'kill-buffer-hook 'wdired-check-kill-buffer nil t)
249 (setq major-mode 'wdired-mode)
250 (setq mode-name "Editable Dired")
251 (setq revert-buffer-function 'wdired-revert)
252 ;; I temp disable undo for performance: since I'm going to clear the
253 ;; undo list, it can save more than a 9% of time with big
254 ;; directories because setting properties modify the undo-list.
255 (buffer-disable-undo)
256 (wdired-preprocess-files)
257 (if wdired-allow-to-change-permissions
258 (wdired-preprocess-perms))
259 (if (and wdired-allow-to-redirect-links (fboundp 'make-symbolic-link))
260 (wdired-preprocess-symlinks))
261 (buffer-enable-undo) ; Performance hack. See above.
262 (set-buffer-modified-p nil)
263 (setq buffer-undo-list nil)
264 (run-mode-hooks 'wdired-mode-hook)
265 (message "%s" (substitute-command-keys
266 "Press \\[wdired-finish-edit] when finished \
267 or \\[wdired-abort-changes] to abort changes")))
269 (defun wdired-isearch-filter-read-only (beg end)
270 "Skip matches that have a read-only property."
271 (and (isearch-filter-visible beg end)
272 (not (text-property-not-all (min beg end) (max beg end)
273 'read-only nil))))
275 ;; Protect the buffer so only the filenames can be changed, and put
276 ;; properties so filenames (old and new) can be easily found.
277 (defun wdired-preprocess-files ()
278 (put-text-property (point-min) (1+ (point-min))'front-sticky t)
279 (save-excursion
280 (goto-char (point-min))
281 (let ((b-protection (point))
282 filename)
283 (while (not (eobp))
284 (setq filename (dired-get-filename nil t))
285 (when (and filename
286 (not (member (file-name-nondirectory filename) '("." ".."))))
287 (dired-move-to-filename)
288 ;; The rear-nonsticky property below shall ensure that text preceding
289 ;; the filename can't be modified.
290 (add-text-properties
291 (1- (point)) (point) `(old-name ,filename rear-nonsticky (read-only)))
292 (put-text-property b-protection (point) 'read-only t)
293 (setq b-protection (dired-move-to-end-of-filename t))
294 (put-text-property (point) (1+ (point)) 'end-name t))
295 (forward-line))
296 (put-text-property b-protection (point-max) 'read-only t))))
298 ;; This code is a copy of some dired-get-filename lines.
299 (defsubst wdired-normalize-filename (file)
300 (setq file
301 ;; FIXME: shouldn't we check for a `b' argument or somesuch before
302 ;; doing such unquoting? --Stef
303 (read (concat
304 "\"" (replace-regexp-in-string
305 "\\([^\\]\\|\\`\\)\"" "\\1\\\\\"" file)
306 "\"")))
307 (and file buffer-file-coding-system
308 (not file-name-coding-system)
309 (not default-file-name-coding-system)
310 (setq file (encode-coding-string file buffer-file-coding-system)))
311 file)
313 (defun wdired-get-filename (&optional no-dir old)
314 "Return the filename at line.
315 Similar to `dired-get-filename' but it doesn't rely on regexps. It
316 relies on WDired buffer's properties. Optional arg NO-DIR with value
317 non-nil means don't include directory. Optional arg OLD with value
318 non-nil means return old filename."
319 ;; FIXME: Use dired-get-filename's new properties.
320 (let (beg end file)
321 (save-excursion
322 (setq end (line-end-position))
323 (beginning-of-line)
324 (setq beg (next-single-property-change (point) 'old-name nil end))
325 (unless (eq beg end)
326 (if old
327 (setq file (get-text-property beg 'old-name))
328 ;; In the following form changed `(1+ beg)' to `beg' so that
329 ;; the filename end is found even when the filename is empty.
330 ;; Fixes error and spurious newlines when marking files for
331 ;; deletion.
332 (setq end (next-single-property-change beg 'end-name))
333 (setq file (buffer-substring-no-properties (1+ beg) end)))
334 (and file (setq file (wdired-normalize-filename file))))
335 (if (or no-dir old)
336 file
337 (and file (> (length file) 0)
338 (concat (dired-current-directory) file))))))
341 (defun wdired-change-to-dired-mode ()
342 "Change the mode back to dired."
343 (or (eq major-mode 'wdired-mode)
344 (error "Not a Wdired buffer"))
345 (let ((inhibit-read-only t))
346 (remove-text-properties
347 (point-min) (point-max)
348 '(front-sticky nil rear-nonsticky nil read-only nil keymap nil)))
349 (use-local-map dired-mode-map)
350 (force-mode-line-update)
351 (setq buffer-read-only t)
352 (setq major-mode 'dired-mode)
353 (setq mode-name "Dired")
354 (dired-advertise)
355 (remove-hook 'kill-buffer-hook 'wdired-check-kill-buffer t)
356 (set (make-local-variable 'revert-buffer-function) 'dired-revert))
359 (defun wdired-abort-changes ()
360 "Abort changes and return to dired mode."
361 (interactive)
362 (let ((inhibit-read-only t))
363 (erase-buffer)
364 (insert wdired-old-content)
365 (goto-char wdired-old-point))
366 (wdired-change-to-dired-mode)
367 (set-buffer-modified-p nil)
368 (setq buffer-undo-list nil)
369 (message "Changes aborted"))
371 (defun wdired-finish-edit ()
372 "Actually rename files based on your editing in the Dired buffer."
373 (interactive)
374 (wdired-change-to-dired-mode)
375 (let ((changes nil)
376 (errors 0)
377 files-deleted
378 files-renamed
379 some-file-names-unchanged
380 file-old file-new tmp-value)
381 (save-excursion
382 (when (and wdired-allow-to-redirect-links
383 (fboundp 'make-symbolic-link))
384 (setq tmp-value (wdired-do-symlink-changes))
385 (setq errors (cdr tmp-value))
386 (setq changes (car tmp-value)))
387 (when (and wdired-allow-to-change-permissions
388 (boundp 'wdired-col-perm)) ; could have been changed
389 (setq tmp-value (wdired-do-perm-changes))
390 (setq errors (+ errors (cdr tmp-value)))
391 (setq changes (or changes (car tmp-value))))
392 (goto-char (point-max))
393 (while (not (bobp))
394 (setq file-old (wdired-get-filename nil t))
395 (when file-old
396 (setq file-new (wdired-get-filename))
397 (if (equal file-new file-old)
398 (setq some-file-names-unchanged t)
399 (setq changes t)
400 (if (not file-new) ;empty filename!
401 (push file-old files-deleted)
402 (when wdired-keep-marker-rename
403 (let ((mark (cond ((integerp wdired-keep-marker-rename)
404 wdired-keep-marker-rename)
405 (wdired-keep-marker-rename
406 (cdr (assoc file-old wdired-old-marks)))
407 (t nil))))
408 (when mark
409 (push (cons (substitute-in-file-name file-new) mark)
410 wdired-old-marks))))
411 (push (cons file-old (substitute-in-file-name file-new))
412 files-renamed))))
413 (forward-line -1)))
414 (when files-renamed
415 (setq errors (+ errors (wdired-do-renames files-renamed))))
416 (if changes
417 (progn
418 ;; If we are displaying a single file (rather than the
419 ;; contents of a directory), change dired-directory if that
420 ;; file was renamed. (This ought to be generalized to
421 ;; handle the multiple files case, but that's less trivial).
422 (when (and (stringp dired-directory)
423 (not (file-directory-p dired-directory))
424 (null some-file-names-unchanged)
425 (= (length files-renamed) 1))
426 (setq dired-directory (cdr (car files-renamed))))
427 ;; Re-sort the buffer.
428 (revert-buffer)
429 (let ((inhibit-read-only t))
430 (dired-mark-remembered wdired-old-marks)))
431 (let ((inhibit-read-only t))
432 (remove-text-properties (point-min) (point-max)
433 '(old-name nil end-name nil old-link nil
434 end-link nil end-perm nil
435 old-perm nil perm-changed nil))
436 (message "(No changes to be performed)")))
437 (when files-deleted
438 (wdired-flag-for-deletion files-deleted))
439 (when (> errors 0)
440 (dired-log-summary (format "%d rename actions failed" errors) nil)))
441 (set-buffer-modified-p nil)
442 (setq buffer-undo-list nil))
444 (defun wdired-do-renames (renames)
445 "Perform RENAMES in parallel."
446 (let ((residue ())
447 (progress nil)
448 (errors 0)
449 (overwrite (or (not wdired-confirm-overwrite) 1)))
450 (while (or renames
451 ;; We've done one round through the renames, we have found
452 ;; some residue, but we also made some progress, so maybe
453 ;; some of the residue were resolved: try again.
454 (prog1 (setq renames residue)
455 (setq progress nil)
456 (setq residue nil)))
457 (let* ((rename (pop renames))
458 (file-new (cdr rename)))
459 (cond
460 ((rassoc file-new renames)
461 (error "Trying to rename 2 files to the same name"))
462 ((assoc file-new renames)
463 ;; Renaming to a file name that already exists but will itself be
464 ;; renamed as well. Let's wait until that one gets renamed.
465 (push rename residue))
466 ((and (assoc file-new residue)
467 ;; Make sure the file really exists: if it doesn't it's
468 ;; not really a conflict. It might be a temp-file generated
469 ;; specifically to break a circular renaming.
470 (file-exists-p file-new))
471 ;; Renaming to a file name that already exists, needed to be renamed,
472 ;; but whose renaming could not be performed right away.
473 (if (or progress renames)
474 ;; There's still a chance the conflict will be resolved.
475 (push rename residue)
476 ;; We have not made any progress and we've reached the end of
477 ;; the renames, so we really have a circular conflict, and we
478 ;; have to forcefully break the cycle.
479 (message "Circular renaming: using temporary file name")
480 (let ((tmp (make-temp-name file-new)))
481 (push (cons (car rename) tmp) renames)
482 (push (cons tmp file-new) residue))))
484 (setq progress t)
485 (let ((file-ori (car rename)))
486 (if wdired-use-interactive-rename
487 (wdired-search-and-rename file-ori file-new)
488 ;; If dired-rename-file autoloads dired-aux while
489 ;; dired-backup-overwrite is locally bound,
490 ;; dired-backup-overwrite won't be initialized.
491 ;; So we must ensure dired-aux is loaded.
492 (require 'dired-aux)
493 (condition-case err
494 (let ((dired-backup-overwrite nil))
495 (dired-rename-file file-ori file-new
496 overwrite))
497 (error
498 (setq errors (1+ errors))
499 (dired-log (concat "Rename `" file-ori "' to `"
500 file-new "' failed:\n%s\n")
501 err)))))))))
502 errors))
505 (defun wdired-exit ()
506 "Exit wdired and return to dired mode.
507 Just return to dired mode if there are no changes. Otherwise,
508 ask a yes-or-no question whether to save or cancel changes,
509 and proceed depending on the answer."
510 (interactive)
511 (if (buffer-modified-p)
512 (if (y-or-n-p (format "Buffer %s modified; save changes? "
513 (current-buffer)))
514 (wdired-finish-edit)
515 (wdired-abort-changes))
516 (wdired-change-to-dired-mode)
517 (set-buffer-modified-p nil)
518 (setq buffer-undo-list nil)
519 (message "(No changes need to be saved)")))
521 ;; Rename a file, searching it in a modified dired buffer, in order
522 ;; to be able to use `dired-do-create-files-regexp' and get its
523 ;; "benefits".
524 (defun wdired-search-and-rename (filename-ori filename-new)
525 (save-excursion
526 (goto-char (point-max))
527 (forward-line -1)
528 (let ((done nil)
529 curr-filename)
530 (while (and (not done) (not (bobp)))
531 (setq curr-filename (wdired-get-filename nil t))
532 (if (equal curr-filename filename-ori)
533 (progn
534 (setq done t)
535 (let ((inhibit-read-only t))
536 (dired-move-to-filename)
537 (search-forward (wdired-get-filename t) nil t)
538 (replace-match (file-name-nondirectory filename-ori) t t))
539 (dired-do-create-files-regexp
540 (function dired-rename-file)
541 "Move" 1 ".*" filename-new nil t))
542 (forward-line -1))))))
544 ;; marks a list of files for deletion
545 (defun wdired-flag-for-deletion (filenames-ori)
546 (save-excursion
547 (goto-char (point-min))
548 (while (not (eobp))
549 (if (member (dired-get-filename nil t) filenames-ori)
550 (dired-flag-file-deletion 1)
551 (forward-line)))))
553 (defun wdired-customize ()
554 "Customize WDired options."
555 (interactive)
556 (customize-apropos "wdired" 'groups))
558 (defun wdired-revert (&optional _arg _noconfirm)
559 "Discard changes in the buffer and update it based on changes on disk.
560 Optional arguments are ignored."
561 (wdired-change-to-dired-mode)
562 (revert-buffer)
563 (wdired-change-to-wdired-mode))
565 (defun wdired-check-kill-buffer ()
566 ;; FIXME: Can't we use the normal mechanism for that? --Stef
567 (if (and
568 (buffer-modified-p)
569 (not (y-or-n-p "Buffer changed. Discard changes and kill buffer? ")))
570 (error "Error")))
572 (defun wdired-next-line (arg)
573 "Move down lines then position at filename or the current column.
574 See `wdired-use-dired-vertical-movement'. Optional prefix ARG
575 says how many lines to move; default is one line."
576 (interactive "p")
577 (with-no-warnings (next-line arg))
578 (if (or (eq wdired-use-dired-vertical-movement t)
579 (and wdired-use-dired-vertical-movement
580 (< (current-column)
581 (save-excursion (dired-move-to-filename)
582 (current-column)))))
583 (dired-move-to-filename)))
585 (defun wdired-previous-line (arg)
586 "Move up lines then position at filename or the current column.
587 See `wdired-use-dired-vertical-movement'. Optional prefix ARG
588 says how many lines to move; default is one line."
589 (interactive "p")
590 (with-no-warnings (previous-line arg))
591 (if (or (eq wdired-use-dired-vertical-movement t)
592 (and wdired-use-dired-vertical-movement
593 (< (current-column)
594 (save-excursion (dired-move-to-filename)
595 (current-column)))))
596 (dired-move-to-filename)))
598 ;; Put the needed properties to allow the user to change links' targets
599 (defun wdired-preprocess-symlinks ()
600 (let ((inhibit-read-only t))
601 (save-excursion
602 (goto-char (point-min))
603 (while (not (eobp))
604 (if (looking-at dired-re-sym)
605 (progn
606 (re-search-forward " -> \\(.*\\)$")
607 (put-text-property (- (match-beginning 1) 2)
608 (1- (match-beginning 1)) 'old-link
609 (match-string-no-properties 1))
610 (put-text-property (match-end 1) (1+ (match-end 1)) 'end-link t)
611 (put-text-property (1- (match-beginning 1))
612 (match-beginning 1)
613 'rear-nonsticky '(read-only))
614 (put-text-property (match-beginning 1)
615 (match-end 1) 'read-only nil)))
616 (forward-line)
617 (beginning-of-line)))))
620 (defun wdired-get-previous-link (&optional old move)
621 "Return the next symlink target.
622 If OLD, return the old target. If MOVE, move point before it."
623 (let (beg end target)
624 (setq beg (previous-single-property-change (point) 'old-link nil))
625 (if beg
626 (progn
627 (if old
628 (setq target (get-text-property (1- beg) 'old-link))
629 (setq end (next-single-property-change beg 'end-link))
630 (setq target (buffer-substring-no-properties (1+ beg) end)))
631 (if move (goto-char (1- beg)))))
632 (and target (wdired-normalize-filename target))))
634 (declare-function make-symbolic-link "fileio.c")
636 ;; Perform the changes in the target of the changed links.
637 (defun wdired-do-symlink-changes ()
638 (let ((changes nil)
639 (errors 0)
640 link-to-ori link-to-new link-from)
641 (goto-char (point-max))
642 (while (setq link-to-new (wdired-get-previous-link))
643 (setq link-to-ori (wdired-get-previous-link t t))
644 (setq link-from (wdired-get-filename nil t))
645 (unless (equal link-to-new link-to-ori)
646 (setq changes t)
647 (if (equal link-to-new "") ;empty filename!
648 (setq link-to-new "/dev/null"))
649 (condition-case err
650 (progn
651 (delete-file link-from)
652 (make-symbolic-link
653 (substitute-in-file-name link-to-new) link-from))
654 (error
655 (setq errors (1+ errors))
656 (dired-log (concat "Link `" link-from "' to `"
657 link-to-new "' failed:\n%s\n")
658 err)))))
659 (cons changes errors)))
661 ;; Perform a "case command" skipping read-only words.
662 (defun wdired-xcase-word (command arg)
663 (if (< arg 0)
664 (funcall command arg)
665 (while (> arg 0)
666 (condition-case nil
667 (progn
668 (funcall command 1)
669 (setq arg (1- arg)))
670 (error
671 (if (forward-word)
672 ;; Skip any non-word characters to avoid triggering a read-only
673 ;; error which would cause skipping the next word characters too.
674 (skip-syntax-forward "^w")
675 (setq arg 0)))))))
677 (defun wdired-downcase-word (arg)
678 "WDired version of `downcase-word'.
679 Like original function but it skips read-only words."
680 (interactive "p")
681 (wdired-xcase-word 'downcase-word arg))
683 (defun wdired-upcase-word (arg)
684 "WDired version of `upcase-word'.
685 Like original function but it skips read-only words."
686 (interactive "p")
687 (wdired-xcase-word 'upcase-word arg))
689 (defun wdired-capitalize-word (arg)
690 "WDired version of `capitalize-word'.
691 Like original function but it skips read-only words."
692 (interactive "p")
693 (wdired-xcase-word 'capitalize-word arg))
696 ;; The following code deals with changing the access bits (or
697 ;; permissions) of the files.
699 (defvar wdired-perm-mode-map
700 (let ((map (make-sparse-keymap)))
701 (define-key map " " 'wdired-toggle-bit)
702 (define-key map "r" 'wdired-set-bit)
703 (define-key map "w" 'wdired-set-bit)
704 (define-key map "x" 'wdired-set-bit)
705 (define-key map "-" 'wdired-set-bit)
706 (define-key map "S" 'wdired-set-bit)
707 (define-key map "s" 'wdired-set-bit)
708 (define-key map "T" 'wdired-set-bit)
709 (define-key map "t" 'wdired-set-bit)
710 (define-key map "s" 'wdired-set-bit)
711 (define-key map "l" 'wdired-set-bit)
712 (define-key map [down-mouse-1] 'wdired-mouse-toggle-bit)
713 map))
715 ;; Put a keymap property to the permission bits of the files, and store the
716 ;; original name and permissions as a property
717 (defun wdired-preprocess-perms ()
718 (let ((inhibit-read-only t))
719 (set (make-local-variable 'wdired-col-perm) nil)
720 (save-excursion
721 (goto-char (point-min))
722 (while (not (eobp))
723 (when (and (not (looking-at dired-re-sym))
724 (wdired-get-filename)
725 (re-search-forward dired-re-perms (line-end-position) 'eol))
726 (let ((begin (match-beginning 0))
727 (end (match-end 0)))
728 (unless wdired-col-perm
729 (setq wdired-col-perm (- (current-column) 9)))
730 (if (eq wdired-allow-to-change-permissions 'advanced)
731 (progn
732 (put-text-property begin end 'read-only nil)
733 ;; make first permission bit writable
734 (put-text-property
735 (1- begin) begin 'rear-nonsticky '(read-only)))
736 ;; avoid that keymap applies to text following permissions
737 (add-text-properties
738 (1+ begin) end
739 `(keymap ,wdired-perm-mode-map rear-nonsticky (keymap))))
740 (put-text-property end (1+ end) 'end-perm t)
741 (put-text-property
742 begin (1+ begin) 'old-perm (match-string-no-properties 0))))
743 (forward-line)
744 (beginning-of-line)))))
746 (defun wdired-perm-allowed-in-pos (char pos)
747 (cond
748 ((= char ?-) t)
749 ((= char ?r) (= (% pos 3) 0))
750 ((= char ?w) (= (% pos 3) 1))
751 ((= char ?x) (= (% pos 3) 2))
752 ((memq char '(?s ?S)) (memq pos '(2 5)))
753 ((memq char '(?t ?T)) (= pos 8))
754 ((= char ?l) (= pos 5))))
756 (defun wdired-set-bit ()
757 "Set a permission bit character."
758 (interactive)
759 (if (wdired-perm-allowed-in-pos last-command-event
760 (- (current-column) wdired-col-perm))
761 (let ((new-bit (char-to-string last-command-event))
762 (inhibit-read-only t)
763 (pos-prop (- (point) (- (current-column) wdired-col-perm))))
764 (put-text-property 0 1 'keymap wdired-perm-mode-map new-bit)
765 (put-text-property 0 1 'read-only t new-bit)
766 (insert new-bit)
767 (delete-char 1)
768 (put-text-property (1- pos-prop) pos-prop 'perm-changed t)
769 (put-text-property (1- (point)) (point) 'rear-nonsticky '(keymap)))
770 (forward-char 1)))
772 (defun wdired-toggle-bit ()
773 "Toggle the permission bit at point."
774 (interactive)
775 (let ((inhibit-read-only t)
776 (new-bit "-")
777 (pos-prop (- (point) (- (current-column) wdired-col-perm))))
778 (if (eq (char-after (point)) ?-)
779 (setq new-bit
780 (if (= (% (- (current-column) wdired-col-perm) 3) 0) "r"
781 (if (= (% (- (current-column) wdired-col-perm) 3) 1) "w"
782 "x"))))
783 (put-text-property 0 1 'keymap 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 (1- pos-prop) pos-prop 'perm-changed t)
788 (put-text-property (1- (point)) (point) 'rear-nonsticky '(keymap))))
790 (defun wdired-mouse-toggle-bit (event)
791 "Toggle the permission bit that was left clicked."
792 (interactive "e")
793 (mouse-set-point event)
794 (wdired-toggle-bit))
796 ;; Allowed chars for 4000 bit are Ss in position 3
797 ;; Allowed chars for 2000 bit are Ssl in position 6
798 ;; Allowed chars for 1000 bit are Tt in position 9
799 (defun wdired-perms-to-number (perms)
800 (let ((nperm 0777))
801 (if (= (elt perms 1) ?-) (setq nperm (- nperm 400)))
802 (if (= (elt perms 2) ?-) (setq nperm (- nperm 200)))
803 (let ((p-bit (elt perms 3)))
804 (if (memq p-bit '(?- ?S)) (setq nperm (- nperm 100)))
805 (if (memq p-bit '(?s ?S)) (setq nperm (+ nperm 4000))))
806 (if (= (elt perms 4) ?-) (setq nperm (- nperm 40)))
807 (if (= (elt perms 5) ?-) (setq nperm (- nperm 20)))
808 (let ((p-bit (elt perms 6)))
809 (if (memq p-bit '(?- ?S ?l)) (setq nperm (- nperm 10)))
810 (if (memq p-bit '(?s ?S ?l)) (setq nperm (+ nperm 2000))))
811 (if (= (elt perms 7) ?-) (setq nperm (- nperm 4)))
812 (if (= (elt perms 8) ?-) (setq nperm (- nperm 2)))
813 (let ((p-bit (elt perms 9)))
814 (if (memq p-bit '(?- ?T)) (setq nperm (- nperm 1)))
815 (if (memq p-bit '(?t ?T)) (setq nperm (+ nperm 1000))))
816 nperm))
818 ;; Perform the changes in the permissions of the files that have
819 ;; changed.
820 (defun wdired-do-perm-changes ()
821 (let ((changes nil)
822 (errors 0)
823 (prop-wanted (if (eq wdired-allow-to-change-permissions 'advanced)
824 'old-perm 'perm-changed))
825 filename perms-ori perms-new perm-tmp)
826 (goto-char (next-single-property-change (point-min) prop-wanted
827 nil (point-max)))
828 (while (not (eobp))
829 (setq perms-ori (get-text-property (point) 'old-perm))
830 (setq perms-new (buffer-substring-no-properties
831 (point) (next-single-property-change (point) 'end-perm)))
832 (unless (equal perms-ori perms-new)
833 (setq changes t)
834 (setq filename (wdired-get-filename nil t))
835 (if (= (length perms-new) 10)
836 (progn
837 (setq perm-tmp
838 (int-to-string (wdired-perms-to-number perms-new)))
839 (unless (equal 0 (process-file dired-chmod-program
840 nil nil nil perm-tmp filename))
841 (setq errors (1+ errors))
842 (dired-log (concat dired-chmod-program " " perm-tmp
843 " `" filename "' failed\n\n"))))
844 (setq errors (1+ errors))
845 (dired-log (concat "Cannot parse permission `" perms-new
846 "' for file `" filename "'\n\n"))))
847 (goto-char (next-single-property-change (1+ (point)) prop-wanted
848 nil (point-max))))
849 (cons changes errors)))
851 (provide 'wdired)
853 ;; Local Variables:
854 ;; coding: latin-1
855 ;; byte-compile-dynamic: t
856 ;; End:
858 ;;; wdired.el ends here