* process.h (PSET): Remove.
[emacs.git] / lisp / wdired.el
blobb893e8f6f2b73754e79863ce40e660489c56c0fd
1 ;;; wdired.el --- Rename files editing their names in dired buffers
3 ;; Copyright (C) 2004-2012 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 (defvar wdired-mode-map
144 (let ((map (make-sparse-keymap)))
145 (define-key map "\C-x\C-s" 'wdired-finish-edit)
146 (define-key map "\C-c\C-c" 'wdired-finish-edit)
147 (define-key map "\C-c\C-k" 'wdired-abort-changes)
148 (define-key map "\C-c\C-[" 'wdired-abort-changes)
149 (define-key map "\C-x\C-q" 'wdired-exit)
150 (define-key map "\C-m" 'ignore)
151 (define-key map "\C-j" 'ignore)
152 (define-key map "\C-o" 'ignore)
153 (define-key map [up] 'wdired-previous-line)
154 (define-key map "\C-p" 'wdired-previous-line)
155 (define-key map [down] 'wdired-next-line)
156 (define-key map "\C-n" 'wdired-next-line)
158 (define-key map [menu-bar wdired]
159 (cons "WDired" (make-sparse-keymap "WDired")))
160 (define-key map [menu-bar wdired wdired-customize]
161 '("Options" . wdired-customize))
162 (define-key map [menu-bar wdired dashes]
163 '("--"))
164 (define-key map [menu-bar wdired wdired-abort-changes]
165 '(menu-item "Abort Changes" wdired-abort-changes
166 :help "Abort changes and return to dired mode"))
167 (define-key map [menu-bar wdired wdired-finish-edit]
168 '("Commit Changes" . wdired-finish-edit))
170 (define-key map [remap upcase-word] 'wdired-upcase-word)
171 (define-key map [remap capitalize-word] 'wdired-capitalize-word)
172 (define-key map [remap downcase-word] 'wdired-downcase-word)
174 map))
176 (defvar wdired-mode-hook nil
177 "Hooks run when changing to WDired mode.")
179 ;; Local variables (put here to avoid compilation gripes)
180 (defvar wdired-col-perm) ;; Column where the permission bits start
181 (defvar wdired-old-content)
182 (defvar wdired-old-point)
185 (defun wdired-mode ()
186 "Writable Dired (WDired) mode.
187 \\<wdired-mode-map>
188 In WDired mode, you can edit the names of the files in the
189 buffer, the target of the links, and the permission bits of the
190 files.
192 Type \\[wdired-finish-edit] to exit WDired mode, returning to
193 Dired mode, and make your edits \"take effect\" by modifying the
194 file and directory names, link targets, and/or file permissions
195 on disk. If you delete the filename of a file, it is flagged for
196 deletion in the Dired buffer.
198 Type \\[wdired-abort-changes] to abort your edits and exit WDired mode.
200 Type \\[customize-group] RET wdired to customize WDired behavior.
202 The only editable texts in a WDired buffer are filenames,
203 symbolic link targets, and filenames permission."
204 (interactive)
205 (error "This mode can be enabled only by `wdired-change-to-wdired-mode'"))
206 (put 'wdired-mode 'mode-class 'special)
209 ;;;###autoload
210 (defun wdired-change-to-wdired-mode ()
211 "Put a Dired buffer in Writable Dired (WDired) mode.
212 \\<wdired-mode-map>
213 In WDired mode, you can edit the names of the files in the
214 buffer, the target of the links, and the permission bits of the
215 files. After typing \\[wdired-finish-edit], Emacs modifies the files and
216 directories to reflect your edits.
218 See `wdired-mode'."
219 (interactive)
220 (unless (eq major-mode 'dired-mode)
221 (error "Not a Dired buffer"))
222 (set (make-local-variable 'wdired-old-content)
223 (buffer-substring (point-min) (point-max)))
224 (set (make-local-variable 'wdired-old-point) (point))
225 (set (make-local-variable 'query-replace-skip-read-only) t)
226 (set (make-local-variable 'isearch-filter-predicate)
227 'wdired-isearch-filter-read-only)
228 (use-local-map wdired-mode-map)
229 (force-mode-line-update)
230 (setq buffer-read-only nil)
231 (dired-unadvertise default-directory)
232 (add-hook 'kill-buffer-hook 'wdired-check-kill-buffer nil t)
233 (setq major-mode 'wdired-mode)
234 (setq mode-name "Editable Dired")
235 (setq revert-buffer-function 'wdired-revert)
236 ;; I temp disable undo for performance: since I'm going to clear the
237 ;; undo list, it can save more than a 9% of time with big
238 ;; directories because setting properties modify the undo-list.
239 (buffer-disable-undo)
240 (wdired-preprocess-files)
241 (if wdired-allow-to-change-permissions
242 (wdired-preprocess-perms))
243 (if (and wdired-allow-to-redirect-links (fboundp 'make-symbolic-link))
244 (wdired-preprocess-symlinks))
245 (buffer-enable-undo) ; Performance hack. See above.
246 (set-buffer-modified-p nil)
247 (setq buffer-undo-list nil)
248 (run-mode-hooks 'wdired-mode-hook)
249 (message "%s" (substitute-command-keys
250 "Press \\[wdired-finish-edit] when finished \
251 or \\[wdired-abort-changes] to abort changes")))
253 (defun wdired-isearch-filter-read-only (beg end)
254 "Skip matches that have a read-only property."
255 (and (isearch-filter-visible beg end)
256 (not (text-property-not-all (min beg end) (max beg end)
257 'read-only nil))))
259 ;; Protect the buffer so only the filenames can be changed, and put
260 ;; properties so filenames (old and new) can be easily found.
261 (defun wdired-preprocess-files ()
262 (put-text-property (point-min) (1+ (point-min))'front-sticky t)
263 (save-excursion
264 (goto-char (point-min))
265 (let ((b-protection (point))
266 filename)
267 (while (not (eobp))
268 (setq filename (dired-get-filename nil t))
269 (when (and filename
270 (not (member (file-name-nondirectory filename) '("." ".."))))
271 (dired-move-to-filename)
272 ;; The rear-nonsticky property below shall ensure that text preceding
273 ;; the filename can't be modified.
274 (add-text-properties
275 (1- (point)) (point) `(old-name ,filename rear-nonsticky (read-only)))
276 (put-text-property b-protection (point) 'read-only t)
277 (setq b-protection (dired-move-to-end-of-filename t))
278 (put-text-property (point) (1+ (point)) 'end-name t))
279 (forward-line))
280 (put-text-property b-protection (point-max) 'read-only t))))
282 ;; This code is a copy of some dired-get-filename lines.
283 (defsubst wdired-normalize-filename (file)
284 (setq file
285 ;; FIXME: shouldn't we check for a `b' argument or somesuch before
286 ;; doing such unquoting? --Stef
287 (read (concat
288 "\"" (replace-regexp-in-string
289 "\\([^\\]\\|\\`\\)\"" "\\1\\\\\"" file)
290 "\"")))
291 (and file buffer-file-coding-system
292 (not file-name-coding-system)
293 (not default-file-name-coding-system)
294 (setq file (encode-coding-string file buffer-file-coding-system)))
295 file)
297 (defun wdired-get-filename (&optional no-dir old)
298 "Return the filename at line.
299 Similar to `dired-get-filename' but it doesn't rely on regexps. It
300 relies on WDired buffer's properties. Optional arg NO-DIR with value
301 non-nil means don't include directory. Optional arg OLD with value
302 non-nil means return old filename."
303 ;; FIXME: Use dired-get-filename's new properties.
304 (let (beg end file)
305 (save-excursion
306 (setq end (line-end-position))
307 (beginning-of-line)
308 (setq beg (next-single-property-change (point) 'old-name nil end))
309 (unless (eq beg end)
310 (if old
311 (setq file (get-text-property beg 'old-name))
312 ;; In the following form changed `(1+ beg)' to `beg' so that
313 ;; the filename end is found even when the filename is empty.
314 ;; Fixes error and spurious newlines when marking files for
315 ;; deletion.
316 (setq end (next-single-property-change beg 'end-name))
317 (setq file (buffer-substring-no-properties (1+ beg) end)))
318 (and file (setq file (wdired-normalize-filename file))))
319 (if (or no-dir old)
320 file
321 (and file (> (length file) 0)
322 (concat (dired-current-directory) file))))))
325 (defun wdired-change-to-dired-mode ()
326 "Change the mode back to dired."
327 (or (eq major-mode 'wdired-mode)
328 (error "Not a Wdired buffer"))
329 (let ((inhibit-read-only t))
330 (remove-text-properties
331 (point-min) (point-max)
332 '(front-sticky nil rear-nonsticky nil read-only nil keymap nil)))
333 (use-local-map dired-mode-map)
334 (force-mode-line-update)
335 (setq buffer-read-only t)
336 (setq major-mode 'dired-mode)
337 (setq mode-name "Dired")
338 (dired-advertise)
339 (remove-hook 'kill-buffer-hook 'wdired-check-kill-buffer t)
340 (set (make-local-variable 'revert-buffer-function) 'dired-revert))
343 (defun wdired-abort-changes ()
344 "Abort changes and return to dired mode."
345 (interactive)
346 (let ((inhibit-read-only t))
347 (erase-buffer)
348 (insert wdired-old-content)
349 (goto-char wdired-old-point))
350 (wdired-change-to-dired-mode)
351 (set-buffer-modified-p nil)
352 (setq buffer-undo-list nil)
353 (message "Changes aborted"))
355 (defun wdired-finish-edit ()
356 "Actually rename files based on your editing in the Dired buffer."
357 (interactive)
358 (wdired-change-to-dired-mode)
359 (let ((changes nil)
360 (errors 0)
361 files-deleted
362 files-renamed
363 some-file-names-unchanged
364 file-old file-new tmp-value)
365 (save-excursion
366 (when (and wdired-allow-to-redirect-links
367 (fboundp 'make-symbolic-link))
368 (setq tmp-value (wdired-do-symlink-changes))
369 (setq errors (cdr tmp-value))
370 (setq changes (car tmp-value)))
371 (when (and wdired-allow-to-change-permissions
372 (boundp 'wdired-col-perm)) ; could have been changed
373 (setq tmp-value (wdired-do-perm-changes))
374 (setq errors (+ errors (cdr tmp-value)))
375 (setq changes (or changes (car tmp-value))))
376 (goto-char (point-max))
377 (while (not (bobp))
378 (setq file-old (wdired-get-filename nil t))
379 (when file-old
380 (setq file-new (wdired-get-filename))
381 (if (equal file-new file-old)
382 (setq some-file-names-unchanged t)
383 (setq changes t)
384 (if (not file-new) ;empty filename!
385 (push file-old files-deleted)
386 (push (cons file-old (substitute-in-file-name file-new))
387 files-renamed))))
388 (forward-line -1)))
389 (when files-renamed
390 (setq errors (+ errors (wdired-do-renames files-renamed))))
391 (if changes
392 (progn
393 ;; If we are displaying a single file (rather than the
394 ;; contents of a directory), change dired-directory if that
395 ;; file was renamed. (This ought to be generalized to
396 ;; handle the multiple files case, but that's less trivial).
397 (when (and (stringp dired-directory)
398 (not (file-directory-p dired-directory))
399 (null some-file-names-unchanged)
400 (= (length files-renamed) 1))
401 (setq dired-directory (cdr (car files-renamed))))
402 ;; Re-sort the buffer.
403 (revert-buffer))
404 (let ((inhibit-read-only t))
405 (remove-text-properties (point-min) (point-max)
406 '(old-name nil end-name nil old-link nil
407 end-link nil end-perm nil
408 old-perm nil perm-changed nil))
409 (message "(No changes to be performed)")))
410 (when files-deleted
411 (wdired-flag-for-deletion files-deleted))
412 (when (> errors 0)
413 (dired-log-summary (format "%d rename actions failed" errors) nil)))
414 (set-buffer-modified-p nil)
415 (setq buffer-undo-list nil))
417 (defun wdired-do-renames (renames)
418 "Perform RENAMES in parallel."
419 (let ((residue ())
420 (progress nil)
421 (errors 0)
422 (overwrite (or (not wdired-confirm-overwrite) 1)))
423 (while (or renames
424 ;; We've done one round through the renames, we have found
425 ;; some residue, but we also made some progress, so maybe
426 ;; some of the residue were resolved: try again.
427 (prog1 (setq renames residue)
428 (setq progress nil)
429 (setq residue nil)))
430 (let* ((rename (pop renames))
431 (file-new (cdr rename)))
432 (cond
433 ((rassoc file-new renames)
434 (error "Trying to rename 2 files to the same name"))
435 ((assoc file-new renames)
436 ;; Renaming to a file name that already exists but will itself be
437 ;; renamed as well. Let's wait until that one gets renamed.
438 (push rename residue))
439 ((and (assoc file-new residue)
440 ;; Make sure the file really exists: if it doesn't it's
441 ;; not really a conflict. It might be a temp-file generated
442 ;; specifically to break a circular renaming.
443 (file-exists-p file-new))
444 ;; Renaming to a file name that already exists, needed to be renamed,
445 ;; but whose renaming could not be performed right away.
446 (if (or progress renames)
447 ;; There's still a chance the conflict will be resolved.
448 (push rename residue)
449 ;; We have not made any progress and we've reached the end of
450 ;; the renames, so we really have a circular conflict, and we
451 ;; have to forcefully break the cycle.
452 (message "Circular renaming: using temporary file name")
453 (let ((tmp (make-temp-name file-new)))
454 (push (cons (car rename) tmp) renames)
455 (push (cons tmp file-new) residue))))
457 (setq progress t)
458 (let ((file-ori (car rename)))
459 (if wdired-use-interactive-rename
460 (wdired-search-and-rename file-ori file-new)
461 ;; If dired-rename-file autoloads dired-aux while
462 ;; dired-backup-overwrite is locally bound,
463 ;; dired-backup-overwrite won't be initialized.
464 ;; So we must ensure dired-aux is loaded.
465 (require 'dired-aux)
466 (condition-case err
467 (let ((dired-backup-overwrite nil))
468 (dired-rename-file file-ori file-new
469 overwrite))
470 (error
471 (setq errors (1+ errors))
472 (dired-log (concat "Rename `" file-ori "' to `"
473 file-new "' failed:\n%s\n")
474 err)))))))))
475 errors))
478 (defun wdired-exit ()
479 "Exit wdired and return to dired mode.
480 Just return to dired mode if there are no changes. Otherwise,
481 ask a yes-or-no question whether to save or cancel changes,
482 and proceed depending on the answer."
483 (interactive)
484 (if (buffer-modified-p)
485 (if (y-or-n-p (format "Buffer %s modified; save changes? "
486 (current-buffer)))
487 (wdired-finish-edit)
488 (wdired-abort-changes))
489 (wdired-change-to-dired-mode)
490 (set-buffer-modified-p nil)
491 (setq buffer-undo-list nil)
492 (message "(No changes need to be saved)")))
494 ;; Rename a file, searching it in a modified dired buffer, in order
495 ;; to be able to use `dired-do-create-files-regexp' and get its
496 ;; "benefits".
497 (defun wdired-search-and-rename (filename-ori filename-new)
498 (save-excursion
499 (goto-char (point-max))
500 (forward-line -1)
501 (let ((done nil)
502 curr-filename)
503 (while (and (not done) (not (bobp)))
504 (setq curr-filename (wdired-get-filename nil t))
505 (if (equal curr-filename filename-ori)
506 (progn
507 (setq done t)
508 (let ((inhibit-read-only t))
509 (dired-move-to-filename)
510 (search-forward (wdired-get-filename t) nil t)
511 (replace-match (file-name-nondirectory filename-ori) t t))
512 (dired-do-create-files-regexp
513 (function dired-rename-file)
514 "Move" 1 ".*" filename-new nil t))
515 (forward-line -1))))))
517 ;; marks a list of files for deletion
518 (defun wdired-flag-for-deletion (filenames-ori)
519 (save-excursion
520 (goto-char (point-min))
521 (while (not (eobp))
522 (if (member (dired-get-filename nil t) filenames-ori)
523 (dired-flag-file-deletion 1)
524 (forward-line)))))
526 (defun wdired-customize ()
527 "Customize WDired options."
528 (interactive)
529 (customize-apropos "wdired" 'groups))
531 (defun wdired-revert (&optional _arg _noconfirm)
532 "Discard changes in the buffer and update it based on changes on disk.
533 Optional arguments are ignored."
534 (wdired-change-to-dired-mode)
535 (revert-buffer)
536 (wdired-change-to-wdired-mode))
538 (defun wdired-check-kill-buffer ()
539 ;; FIXME: Can't we use the normal mechanism for that? --Stef
540 (if (and
541 (buffer-modified-p)
542 (not (y-or-n-p "Buffer changed. Discard changes and kill buffer? ")))
543 (error "Error")))
545 (defun wdired-next-line (arg)
546 "Move down lines then position at filename or the current column.
547 See `wdired-use-dired-vertical-movement'. Optional prefix ARG
548 says how many lines to move; default is one line."
549 (interactive "p")
550 (with-no-warnings (next-line arg))
551 (if (or (eq wdired-use-dired-vertical-movement t)
552 (and wdired-use-dired-vertical-movement
553 (< (current-column)
554 (save-excursion (dired-move-to-filename)
555 (current-column)))))
556 (dired-move-to-filename)))
558 (defun wdired-previous-line (arg)
559 "Move up lines then position at filename or the current column.
560 See `wdired-use-dired-vertical-movement'. Optional prefix ARG
561 says how many lines to move; default is one line."
562 (interactive "p")
563 (with-no-warnings (previous-line arg))
564 (if (or (eq wdired-use-dired-vertical-movement t)
565 (and wdired-use-dired-vertical-movement
566 (< (current-column)
567 (save-excursion (dired-move-to-filename)
568 (current-column)))))
569 (dired-move-to-filename)))
571 ;; Put the needed properties to allow the user to change links' targets
572 (defun wdired-preprocess-symlinks ()
573 (let ((inhibit-read-only t))
574 (save-excursion
575 (goto-char (point-min))
576 (while (not (eobp))
577 (if (looking-at dired-re-sym)
578 (progn
579 (re-search-forward " -> \\(.*\\)$")
580 (put-text-property (- (match-beginning 1) 2)
581 (1- (match-beginning 1)) 'old-link
582 (match-string-no-properties 1))
583 (put-text-property (match-end 1) (1+ (match-end 1)) 'end-link t)
584 (put-text-property (1- (match-beginning 1))
585 (match-beginning 1)
586 'rear-nonsticky '(read-only))
587 (put-text-property (match-beginning 1)
588 (match-end 1) 'read-only nil)))
589 (forward-line)
590 (beginning-of-line)))))
593 (defun wdired-get-previous-link (&optional old move)
594 "Return the next symlink target.
595 If OLD, return the old target. If MOVE, move point before it."
596 (let (beg end target)
597 (setq beg (previous-single-property-change (point) 'old-link nil))
598 (if beg
599 (progn
600 (if old
601 (setq target (get-text-property (1- beg) 'old-link))
602 (setq end (next-single-property-change beg 'end-link))
603 (setq target (buffer-substring-no-properties (1+ beg) end)))
604 (if move (goto-char (1- beg)))))
605 (and target (wdired-normalize-filename target))))
607 (declare-function make-symbolic-link "fileio.c")
609 ;; Perform the changes in the target of the changed links.
610 (defun wdired-do-symlink-changes ()
611 (let ((changes nil)
612 (errors 0)
613 link-to-ori link-to-new link-from)
614 (goto-char (point-max))
615 (while (setq link-to-new (wdired-get-previous-link))
616 (setq link-to-ori (wdired-get-previous-link t t))
617 (setq link-from (wdired-get-filename nil t))
618 (unless (equal link-to-new link-to-ori)
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 (while (> arg 0)
639 (condition-case nil
640 (progn
641 (funcall command 1)
642 (setq arg (1- arg)))
643 (error
644 (if (forward-word)
645 ;; Skip any non-word characters to avoid triggering a read-only
646 ;; error which would cause skipping the next word characters too.
647 (skip-syntax-forward "^w")
648 (setq arg 0)))))))
650 (defun wdired-downcase-word (arg)
651 "WDired version of `downcase-word'.
652 Like original function but it skips read-only words."
653 (interactive "p")
654 (wdired-xcase-word 'downcase-word arg))
656 (defun wdired-upcase-word (arg)
657 "WDired version of `upcase-word'.
658 Like original function but it skips read-only words."
659 (interactive "p")
660 (wdired-xcase-word 'upcase-word arg))
662 (defun wdired-capitalize-word (arg)
663 "WDired version of `capitalize-word'.
664 Like original function but it skips read-only words."
665 (interactive "p")
666 (wdired-xcase-word 'capitalize-word arg))
669 ;; The following code deals with changing the access bits (or
670 ;; permissions) of the files.
672 (defvar wdired-perm-mode-map
673 (let ((map (make-sparse-keymap)))
674 (define-key map " " 'wdired-toggle-bit)
675 (define-key map "r" 'wdired-set-bit)
676 (define-key map "w" 'wdired-set-bit)
677 (define-key map "x" 'wdired-set-bit)
678 (define-key map "-" 'wdired-set-bit)
679 (define-key map "S" 'wdired-set-bit)
680 (define-key map "s" 'wdired-set-bit)
681 (define-key map "T" 'wdired-set-bit)
682 (define-key map "t" 'wdired-set-bit)
683 (define-key map "s" 'wdired-set-bit)
684 (define-key map "l" 'wdired-set-bit)
685 (define-key map [down-mouse-1] 'wdired-mouse-toggle-bit)
686 map))
688 ;; Put a keymap property to the permission bits of the files, and store the
689 ;; original name and permissions as a property
690 (defun wdired-preprocess-perms ()
691 (let ((inhibit-read-only t))
692 (set (make-local-variable 'wdired-col-perm) nil)
693 (save-excursion
694 (goto-char (point-min))
695 (while (not (eobp))
696 (when (and (not (looking-at dired-re-sym))
697 (wdired-get-filename)
698 (re-search-forward dired-re-perms (line-end-position) 'eol))
699 (let ((begin (match-beginning 0))
700 (end (match-end 0)))
701 (unless wdired-col-perm
702 (setq wdired-col-perm (- (current-column) 9)))
703 (if (eq wdired-allow-to-change-permissions 'advanced)
704 (progn
705 (put-text-property begin end 'read-only nil)
706 ;; make first permission bit writable
707 (put-text-property
708 (1- begin) begin 'rear-nonsticky '(read-only)))
709 ;; avoid that keymap applies to text following permissions
710 (add-text-properties
711 (1+ begin) end
712 `(keymap ,wdired-perm-mode-map rear-nonsticky (keymap))))
713 (put-text-property end (1+ end) 'end-perm t)
714 (put-text-property
715 begin (1+ begin) 'old-perm (match-string-no-properties 0))))
716 (forward-line)
717 (beginning-of-line)))))
719 (defun wdired-perm-allowed-in-pos (char pos)
720 (cond
721 ((= char ?-) t)
722 ((= char ?r) (= (% pos 3) 0))
723 ((= char ?w) (= (% pos 3) 1))
724 ((= char ?x) (= (% pos 3) 2))
725 ((memq char '(?s ?S)) (memq pos '(2 5)))
726 ((memq char '(?t ?T)) (= pos 8))
727 ((= char ?l) (= pos 5))))
729 (defun wdired-set-bit ()
730 "Set a permission bit character."
731 (interactive)
732 (if (wdired-perm-allowed-in-pos last-command-event
733 (- (current-column) wdired-col-perm))
734 (let ((new-bit (char-to-string last-command-event))
735 (inhibit-read-only t)
736 (pos-prop (- (point) (- (current-column) wdired-col-perm))))
737 (put-text-property 0 1 'keymap wdired-perm-mode-map new-bit)
738 (put-text-property 0 1 'read-only t new-bit)
739 (insert new-bit)
740 (delete-char 1)
741 (put-text-property (1- pos-prop) pos-prop 'perm-changed t)
742 (put-text-property (1- (point)) (point) 'rear-nonsticky '(keymap)))
743 (forward-char 1)))
745 (defun wdired-toggle-bit ()
746 "Toggle the permission bit at point."
747 (interactive)
748 (let ((inhibit-read-only t)
749 (new-bit "-")
750 (pos-prop (- (point) (- (current-column) wdired-col-perm))))
751 (if (eq (char-after (point)) ?-)
752 (setq new-bit
753 (if (= (% (- (current-column) wdired-col-perm) 3) 0) "r"
754 (if (= (% (- (current-column) wdired-col-perm) 3) 1) "w"
755 "x"))))
756 (put-text-property 0 1 'keymap wdired-perm-mode-map new-bit)
757 (put-text-property 0 1 'read-only t new-bit)
758 (insert new-bit)
759 (delete-char 1)
760 (put-text-property (1- pos-prop) pos-prop 'perm-changed t)
761 (put-text-property (1- (point)) (point) 'rear-nonsticky '(keymap))))
763 (defun wdired-mouse-toggle-bit (event)
764 "Toggle the permission bit that was left clicked."
765 (interactive "e")
766 (mouse-set-point event)
767 (wdired-toggle-bit))
769 ;; Allowed chars for 4000 bit are Ss in position 3
770 ;; Allowed chars for 2000 bit are Ssl in position 6
771 ;; Allowed chars for 1000 bit are Tt in position 9
772 (defun wdired-perms-to-number (perms)
773 (let ((nperm 0777))
774 (if (= (elt perms 1) ?-) (setq nperm (- nperm 400)))
775 (if (= (elt perms 2) ?-) (setq nperm (- nperm 200)))
776 (let ((p-bit (elt perms 3)))
777 (if (memq p-bit '(?- ?S)) (setq nperm (- nperm 100)))
778 (if (memq p-bit '(?s ?S)) (setq nperm (+ nperm 4000))))
779 (if (= (elt perms 4) ?-) (setq nperm (- nperm 40)))
780 (if (= (elt perms 5) ?-) (setq nperm (- nperm 20)))
781 (let ((p-bit (elt perms 6)))
782 (if (memq p-bit '(?- ?S ?l)) (setq nperm (- nperm 10)))
783 (if (memq p-bit '(?s ?S ?l)) (setq nperm (+ nperm 2000))))
784 (if (= (elt perms 7) ?-) (setq nperm (- nperm 4)))
785 (if (= (elt perms 8) ?-) (setq nperm (- nperm 2)))
786 (let ((p-bit (elt perms 9)))
787 (if (memq p-bit '(?- ?T)) (setq nperm (- nperm 1)))
788 (if (memq p-bit '(?t ?T)) (setq nperm (+ nperm 1000))))
789 nperm))
791 ;; Perform the changes in the permissions of the files that have
792 ;; changed.
793 (defun wdired-do-perm-changes ()
794 (let ((changes nil)
795 (errors 0)
796 (prop-wanted (if (eq wdired-allow-to-change-permissions 'advanced)
797 'old-perm 'perm-changed))
798 filename perms-ori perms-new perm-tmp)
799 (goto-char (next-single-property-change (point-min) prop-wanted
800 nil (point-max)))
801 (while (not (eobp))
802 (setq perms-ori (get-text-property (point) 'old-perm))
803 (setq perms-new (buffer-substring-no-properties
804 (point) (next-single-property-change (point) 'end-perm)))
805 (unless (equal perms-ori perms-new)
806 (setq changes t)
807 (setq filename (wdired-get-filename nil t))
808 (if (= (length perms-new) 10)
809 (progn
810 (setq perm-tmp
811 (int-to-string (wdired-perms-to-number perms-new)))
812 (unless (equal 0 (process-file dired-chmod-program
813 nil nil nil perm-tmp filename))
814 (setq errors (1+ errors))
815 (dired-log (concat dired-chmod-program " " perm-tmp
816 " `" filename "' failed\n\n"))))
817 (setq errors (1+ errors))
818 (dired-log (concat "Cannot parse permission `" perms-new
819 "' for file `" filename "'\n\n"))))
820 (goto-char (next-single-property-change (1+ (point)) prop-wanted
821 nil (point-max))))
822 (cons changes errors)))
824 (provide 'wdired)
826 ;; Local Variables:
827 ;; coding: latin-1
828 ;; byte-compile-dynamic: t
829 ;; End:
831 ;;; wdired.el ends here