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