2007-08-19 Michael Kifer <kifer@cs.stonybrook.edu>
[emacs.git] / lisp / wdired.el
blob960d89909835d7cadd6be42ab7c3bf2b90a9ef10
1 ;;; wdired.el --- Rename files editing their names in dired buffers
3 ;; Copyright (C) 2004, 2005, 2006, 2007 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
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 3, or (at
15 ;; your option) any later version.
17 ;; This program is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
27 ;;; Commentary:
29 ;; wdired.el (the "w" is for writable) provides an alternative way of
30 ;; renaming files.
32 ;; Have you ever wished to use C-x r t (string-rectangle), M-%
33 ;; (query-replace), M-c (capitalize-word), etc... to change the name of
34 ;; the files in a "dired" buffer? Now you can do this. All the power
35 ;; of Emacs commands are available to renaming files!
37 ;; This package provides a function that makes the filenames of a a
38 ;; dired buffer editable, by changing the buffer mode (which inhibits
39 ;; all of the commands of dired mode). Here you can edit the names of
40 ;; one or more files and directories, and when you press C-c C-c, the
41 ;; renaming takes effect and you are back to dired mode.
43 ;; Another things you can do with WDired:
45 ;; - To move files to another directory (by typing their path,
46 ;; absolute or relative, as a part of the new filename).
48 ;; - To change the target of symbolic links.
50 ;; - To change the permission bits of the filenames (in systems with a
51 ;; working unix-alike `dired-chmod-program'). See and customize the
52 ;; variable `wdired-allow-to-change-permissions'. To change a single
53 ;; char (toggling between its two more usual values) you can press
54 ;; the space bar over it or left-click the mouse. To set any char to
55 ;; an specific value (this includes the SUID, SGID and STI bits) you
56 ;; can use the key labeled as the letter you want. Please note that
57 ;; permissions of the links cannot be changed in that way, because
58 ;; the change would affect to their targets, and this would not be
59 ;; WYSIWYG :-).
61 ;; - To mark files for deletion, by deleting their whole filename.
63 ;;; Installation:
65 ;; Add this file (byte-compiling it is recommended) to your load-path.
66 ;; Then add one of these set of lines (or similar ones) to your config:
68 ;; This is the easy way:
70 ;; (require 'wdired)
71 ;; (define-key dired-mode-map "r" 'wdired-change-to-wdired-mode)
73 ;; This is the recommended way for faster Emacs startup time and lower
74 ;; memory consumption:
76 ;; (autoload 'wdired-change-to-wdired-mode "wdired")
77 ;; (eval-after-load "dired"
78 ;; '(lambda ()
79 ;; (define-key dired-mode-map "r" 'wdired-change-to-wdired-mode)
80 ;; (define-key dired-mode-map
81 ;; [menu-bar immediate wdired-change-to-wdired-mode]
82 ;; '("Edit File Names" . wdired-change-to-wdired-mode))))
84 ;; Type "M-x customize-group RET wdired" if you want to make changes
85 ;; to the default behavior.
87 ;;; Usage:
89 ;; Then, you can start editing the names of the files by typing "r"
90 ;; (or whatever key you choose, or M-x wdired-change-to-wdired-mode).
91 ;; Use C-c C-c when finished or C-c C-k to abort. You can use also the
92 ;; menu options: in dired mode, "Edit File Names" under "Immediate".
93 ;; While editing the names, a new submenu "WDired" is available at top
94 ;; level. You can customize the behavior of this package from this
95 ;; menu.
97 ;;; Change Log:
99 ;; Google is your friend (previous versions with complete changelogs
100 ;; were posted to gnu.emacs.sources)
102 ;;; Code:
104 (defvar dired-backup-overwrite) ; Only in Emacs 20.x this is a custom var
106 (eval-when-compile (require 'cl))
107 (require 'dired)
108 (autoload 'dired-do-create-files-regexp "dired-aux")
110 (defgroup wdired nil
111 "Mode to rename files by editing their names in dired buffers."
112 :group 'dired)
114 (defcustom wdired-use-interactive-rename nil
115 "If non-nil, WDired requires confirmation before actually renaming files.
116 If nil, WDired doesn't require confirmation to change the file names,
117 and the variable `wdired-confirm-overwrite' controls whether it is ok
118 to overwrite files without asking."
119 :type 'boolean
120 :group 'wdired)
122 (defcustom wdired-confirm-overwrite t
123 "If nil the renames can overwrite files without asking.
124 This variable has no effect at all if `wdired-use-interactive-rename'
125 is not nil."
126 :type 'boolean
127 :group 'wdired)
129 (defcustom wdired-use-dired-vertical-movement nil
130 "If t, the \"up\" and \"down\" movement works as in Dired mode.
131 That is, always move the point to the beginning of the filename at line.
133 If `sometimes, only move to the beginning of filename if the point is
134 before it, and `track-eol' is honored. This behavior is very handy
135 when editing several filenames.
137 If nil, \"up\" and \"down\" movement is done as in any other buffer."
138 :type '(choice (const :tag "As in any other mode" nil)
139 (const :tag "Smart cursor placement" sometimes)
140 (other :tag "As in dired mode" t))
141 :group 'wdired)
143 (defcustom wdired-allow-to-redirect-links t
144 "If non-nil, the target of the symbolic links are editable.
145 In systems without symbolic links support, this variable has no effect
146 at all."
147 :type 'boolean
148 :group 'wdired)
150 (defcustom wdired-allow-to-change-permissions nil
151 "If non-nil, the permissions bits of the files are editable.
153 If t, to change a single bit, put the cursor over it and press the
154 space bar, or left click over it. You can also hit the letter you want
155 to set: if this value is allowed, the character in the buffer will be
156 changed. Anyway, the point is advanced one position, so, for example,
157 you can keep the <x> key pressed to give execution permissions to
158 everybody to that file.
160 If `advanced, the bits are freely editable. You can use
161 `string-rectangle', `query-replace', etc. You can put any value (even
162 newlines), but if you want your changes to be useful, you better put a
163 intelligible value.
165 Anyway, the real change of the permissions is done by the external
166 program `dired-chmod-program', which must exist."
167 :type '(choice (const :tag "Not allowed" nil)
168 (const :tag "Toggle/set bits" t)
169 (other :tag "Bits freely editable" advanced))
170 :group 'wdired)
172 (defvar wdired-mode-map
173 (let ((map (make-sparse-keymap)))
174 (define-key map "\C-x\C-s" 'wdired-finish-edit)
175 (define-key map "\C-c\C-c" 'wdired-finish-edit)
176 (define-key map "\C-c\C-k" 'wdired-abort-changes)
177 (define-key map "\C-c\C-[" 'wdired-abort-changes)
178 (define-key map "\C-x\C-q" 'wdired-exit)
179 (define-key map "\C-m" 'ignore)
180 (define-key map "\C-j" 'ignore)
181 (define-key map "\C-o" 'ignore)
182 (define-key map [up] 'wdired-previous-line)
183 (define-key map "\C-p" 'wdired-previous-line)
184 (define-key map [down] 'wdired-next-line)
185 (define-key map "\C-n" 'wdired-next-line)
187 (define-key map [menu-bar wdired]
188 (cons "WDired" (make-sparse-keymap "WDired")))
189 (define-key map [menu-bar wdired wdired-customize]
190 '("Options" . wdired-customize))
191 (define-key map [menu-bar wdired dashes]
192 '("--"))
193 (define-key map [menu-bar wdired wdired-abort-changes]
194 '(menu-item "Abort Changes" wdired-abort-changes
195 :help "Abort changes and return to dired mode"))
196 (define-key map [menu-bar wdired wdired-finish-edit]
197 '("Commit Changes" . wdired-finish-edit))
199 (define-key map [remap upcase-word] 'wdired-upcase-word)
200 (define-key map [remap capitalize-word] 'wdired-capitalize-word)
201 (define-key map [remap downcase-word] 'wdired-downcase-word)
203 map))
205 (defvar wdired-mode-hook nil
206 "Hooks run when changing to WDired mode.")
208 ;; Local variables (put here to avoid compilation gripes)
209 (defvar wdired-col-perm) ;; Column where the permission bits start
210 (defvar wdired-old-content)
211 (defvar wdired-old-point)
214 (defun wdired-mode ()
215 "\\<wdired-mode-map>File Names Editing mode.
217 Press \\[wdired-finish-edit] to make the changes to take effect
218 and exit. To abort the edit, use \\[wdired-abort-changes].
220 In this mode you can edit the names of the files, the target of
221 the links and the permission bits of the files. You can use
222 \\[customize-group] RET wdired to customize WDired behavior.
224 The only editable texts in a WDired buffer are filenames,
225 symbolic link targets, and filenames permission."
226 (interactive)
227 (error "This mode can be enabled only by `wdired-change-to-wdired-mode'"))
228 (put 'wdired-mode 'mode-class 'special)
231 ;;;###autoload
232 (defun wdired-change-to-wdired-mode ()
233 "Put a dired buffer in a mode in which filenames are editable.
234 \\<wdired-mode-map>
235 This mode allows the user to change the names of the files, and after
236 typing \\[wdired-finish-edit] Emacs renames the files and directories
237 in disk.
239 See `wdired-mode'."
240 (interactive)
241 (or (eq major-mode 'dired-mode)
242 (error "Not a Dired buffer"))
243 (set (make-local-variable 'wdired-old-content)
244 (buffer-substring (point-min) (point-max)))
245 (set (make-local-variable 'wdired-old-point) (point))
246 (set (make-local-variable 'query-replace-skip-read-only) t)
247 (use-local-map wdired-mode-map)
248 (force-mode-line-update)
249 (setq buffer-read-only nil)
250 (dired-unadvertise default-directory)
251 (add-hook 'kill-buffer-hook 'wdired-check-kill-buffer nil t)
252 (setq major-mode 'wdired-mode)
253 (setq mode-name "Editable Dired")
254 (setq revert-buffer-function 'wdired-revert)
255 ;; I temp disable undo for performance: since I'm going to clear the
256 ;; undo list, it can save more than a 9% of time with big
257 ;; directories because setting properties modify the undo-list.
258 (buffer-disable-undo)
259 (wdired-preprocess-files)
260 (if wdired-allow-to-change-permissions
261 (wdired-preprocess-perms))
262 (if (and wdired-allow-to-redirect-links (fboundp 'make-symbolic-link))
263 (wdired-preprocess-symlinks))
264 (buffer-enable-undo) ; Performance hack. See above.
265 (set-buffer-modified-p nil)
266 (setq buffer-undo-list nil)
267 (run-mode-hooks 'wdired-mode-hook)
268 (message "%s" (substitute-command-keys
269 "Press \\[wdired-finish-edit] when finished \
270 or \\[wdired-abort-changes] to abort changes")))
273 ;; Protect the buffer so only the filenames can be changed, and put
274 ;; properties so filenames (old and new) can be easily found.
275 (defun wdired-preprocess-files ()
276 (put-text-property 1 2 'front-sticky t)
277 (save-excursion
278 (goto-char (point-min))
279 (let ((b-protection (point))
280 filename)
281 (while (not (eobp))
282 (setq filename (dired-get-filename nil t))
283 (when (and filename
284 (not (member (file-name-nondirectory filename) '("." ".."))))
285 (dired-move-to-filename)
286 ;; The rear-nonsticky property below shall ensure that text preceding
287 ;; the filename can't be modified.
288 (add-text-properties
289 (1- (point)) (point) `(old-name ,filename rear-nonsticky (read-only)))
290 (put-text-property b-protection (point) 'read-only t)
291 (setq b-protection (dired-move-to-end-of-filename t))
292 (put-text-property (point) (1+ (point)) 'end-name t))
293 (forward-line))
294 (put-text-property b-protection (point-max) 'read-only t))))
296 ;; This code is a copy of some dired-get-filename lines.
297 (defsubst wdired-normalize-filename (file)
298 (setq file
299 ;; FIXME: shouldn't we check for a `b' argument or somesuch before
300 ;; doing such unquoting? --Stef
301 (read (concat
302 "\"" (replace-regexp-in-string
303 "\\([^\\]\\|\\`\\)\"" "\\1\\\\\"" file)
304 "\"")))
305 (and file buffer-file-coding-system
306 (not file-name-coding-system)
307 (not default-file-name-coding-system)
308 (setq file (encode-coding-string file buffer-file-coding-system)))
309 file)
311 (defun wdired-get-filename (&optional no-dir old)
312 "Return the filename at line.
313 Similar to `dired-get-filename' but it doesn't rely on regexps. It
314 relies on WDired buffer's properties. Optional arg NO-DIR with value
315 non-nil means don't include directory. Optional arg OLD with value
316 non-nil means return old filename."
317 ;; FIXME: Use dired-get-filename's new properties.
318 (let (beg end file)
319 (save-excursion
320 (setq end (line-end-position))
321 (beginning-of-line)
322 (setq beg (next-single-property-change (point) 'old-name nil end))
323 (unless (eq beg end)
324 (if old
325 (setq file (get-text-property beg 'old-name))
326 (setq end (next-single-property-change (1+ beg) 'end-name))
327 (setq file (buffer-substring-no-properties (1+ beg) end)))
328 (and file (setq file (wdired-normalize-filename file))))
329 (if (or no-dir old)
330 file
331 (and file (> (length file) 0)
332 (concat (dired-current-directory) file))))))
335 (defun wdired-change-to-dired-mode ()
336 "Change the mode back to dired."
337 (or (eq major-mode 'wdired-mode)
338 (error "Not a Wdired buffer"))
339 (let ((inhibit-read-only t))
340 (remove-text-properties
341 (point-min) (point-max)
342 '(front-sticky nil rear-nonsticky nil read-only nil keymap nil)))
343 (use-local-map dired-mode-map)
344 (force-mode-line-update)
345 (setq buffer-read-only t)
346 (setq major-mode 'dired-mode)
347 (setq mode-name "Dired")
348 (dired-advertise)
349 (remove-hook 'kill-buffer-hook 'wdired-check-kill-buffer t)
350 (set (make-local-variable 'revert-buffer-function) 'dired-revert))
353 (defun wdired-abort-changes ()
354 "Abort changes and return to dired mode."
355 (interactive)
356 (let ((inhibit-read-only t))
357 (erase-buffer)
358 (insert wdired-old-content)
359 (goto-char wdired-old-point))
360 (wdired-change-to-dired-mode)
361 (set-buffer-modified-p nil)
362 (setq buffer-undo-list nil)
363 (message "Changes aborted"))
365 (defun wdired-finish-edit ()
366 "Actually rename files based on your editing in the Dired buffer."
367 (interactive)
368 (wdired-change-to-dired-mode)
369 (let ((overwrite (or (not wdired-confirm-overwrite) 1))
370 (changes nil)
371 (files-deleted nil)
372 (errors 0)
373 file-ori file-new tmp-value)
374 (save-excursion
375 (when (and wdired-allow-to-redirect-links
376 (fboundp 'make-symbolic-link))
377 (setq tmp-value (wdired-do-symlink-changes))
378 (setq errors (cdr tmp-value))
379 (setq changes (car tmp-value)))
380 (when (and wdired-allow-to-change-permissions
381 (boundp 'wdired-col-perm)) ; could have been changed
382 (setq tmp-value (wdired-do-perm-changes))
383 (setq errors (+ errors (cdr tmp-value)))
384 (setq changes (or changes (car tmp-value))))
385 (goto-char (point-max))
386 (while (not (bobp))
387 (setq file-ori (wdired-get-filename nil t))
388 (when file-ori
389 (setq file-new (wdired-get-filename)))
390 (when (and file-ori (not (equal file-new file-ori)))
391 (setq changes t)
392 (if (not file-new) ;empty filename!
393 (setq files-deleted (cons file-ori files-deleted))
394 (setq file-new (substitute-in-file-name file-new))
395 (if wdired-use-interactive-rename
396 (wdired-search-and-rename file-ori file-new)
397 ;; If dired-rename-file autoloads dired-aux while
398 ;; dired-backup-overwrite is locally bound,
399 ;; dired-backup-overwrite won't be initialized.
400 ;; So we must ensure dired-aux is loaded.
401 (require 'dired-aux)
402 (condition-case err
403 (let ((dired-backup-overwrite nil))
404 (dired-rename-file file-ori file-new
405 overwrite))
406 (error
407 (setq errors (1+ errors))
408 (dired-log (concat "Rename `" file-ori "' to `"
409 file-new "' failed:\n%s\n")
410 err))))))
411 (forward-line -1)))
412 (if changes
413 (revert-buffer) ;The "revert" is necessary to re-sort the buffer
414 (let ((inhibit-read-only t))
415 (remove-text-properties (point-min) (point-max)
416 '(old-name nil end-name nil old-link nil
417 end-link nil end-perm nil
418 old-perm nil perm-changed nil))
419 (message "(No changes to be performed)")))
420 (when files-deleted
421 (wdired-flag-for-deletion files-deleted))
422 (when (> errors 0)
423 (dired-log-summary (format "%d rename actions failed" errors) nil)))
424 (set-buffer-modified-p nil)
425 (setq buffer-undo-list nil))
427 (defun wdired-exit ()
428 "Exit wdired and return to dired mode.
429 Just return to dired mode if there are no changes. Otherwise,
430 ask a yes-or-no question whether to save or cancel changes,
431 and proceed depending on the answer."
432 (interactive)
433 (if (buffer-modified-p)
434 (if (y-or-n-p (format "Buffer %s modified; save changes? "
435 (current-buffer)))
436 (wdired-finish-edit)
437 (wdired-abort-changes))
438 (wdired-change-to-dired-mode)
439 (set-buffer-modified-p nil)
440 (setq buffer-undo-list nil)
441 (message "(No changes need to be saved)")))
443 ;; Rename a file, searching it in a modified dired buffer, in order
444 ;; to be able to use `dired-do-create-files-regexp' and get its
445 ;; "benefits".
446 (defun wdired-search-and-rename (filename-ori filename-new)
447 (save-excursion
448 (goto-char (point-max))
449 (forward-line -1)
450 (let ((exit-while nil)
451 curr-filename)
452 (while (not exit-while)
453 (setq curr-filename (wdired-get-filename))
454 (if (and curr-filename
455 (equal (substitute-in-file-name curr-filename) filename-new))
456 (progn
457 (setq exit-while t)
458 (let ((inhibit-read-only t))
459 (dired-move-to-filename)
460 (search-forward (wdired-get-filename t) nil t)
461 (replace-match (file-name-nondirectory filename-ori) t t))
462 (dired-do-create-files-regexp
463 (function dired-rename-file)
464 "Move" 1 ".*" filename-new nil t))
465 (forward-line -1)
466 (beginning-of-line)
467 (setq exit-while (bobp)))))))
469 ;; marks a list of files for deletion
470 (defun wdired-flag-for-deletion (filenames-ori)
471 (save-excursion
472 (goto-char (point-min))
473 (while (not (eobp))
474 (if (member (dired-get-filename nil t) filenames-ori)
475 (dired-flag-file-deletion 1)
476 (forward-line)))))
478 (defun wdired-customize ()
479 "Customize WDired options."
480 (interactive)
481 (customize-apropos "wdired" 'groups))
483 (defun wdired-revert (&optional arg noconfirm)
484 "Discard changes in the buffer and update it based on changes on disk.
485 Optional arguments are ignored."
486 (wdired-change-to-dired-mode)
487 (revert-buffer)
488 (wdired-change-to-wdired-mode))
490 (defun wdired-check-kill-buffer ()
491 ;; FIXME: Can't we use the normal mechanism for that? --Stef
492 (if (and
493 (buffer-modified-p)
494 (not (y-or-n-p "Buffer changed. Discard changes and kill buffer? ")))
495 (error nil)))
497 (defun wdired-next-line (arg)
498 "Move down lines then position at filename or the current column.
499 See `wdired-use-dired-vertical-movement'. Optional prefix ARG
500 says how many lines to move; default is one line."
501 (interactive "p")
502 (next-line arg)
503 (if (or (eq wdired-use-dired-vertical-movement t)
504 (and wdired-use-dired-vertical-movement
505 (< (current-column)
506 (save-excursion (dired-move-to-filename)
507 (current-column)))))
508 (dired-move-to-filename)))
510 (defun wdired-previous-line (arg)
511 "Move up lines then position at filename or the current column.
512 See `wdired-use-dired-vertical-movement'. Optional prefix ARG
513 says how many lines to move; default is one line."
514 (interactive "p")
515 (previous-line arg)
516 (if (or (eq wdired-use-dired-vertical-movement t)
517 (and wdired-use-dired-vertical-movement
518 (< (current-column)
519 (save-excursion (dired-move-to-filename)
520 (current-column)))))
521 (dired-move-to-filename)))
523 ;; Put the needed properties to allow the user to change links' targets
524 (defun wdired-preprocess-symlinks ()
525 (let ((inhibit-read-only t))
526 (save-excursion
527 (goto-char (point-min))
528 (while (not (eobp))
529 (if (looking-at dired-re-sym)
530 (progn
531 (re-search-forward " -> \\(.*\\)$")
532 (put-text-property (- (match-beginning 1) 2)
533 (1- (match-beginning 1)) 'old-link
534 (match-string-no-properties 1))
535 (put-text-property (match-end 1) (1+ (match-end 1)) 'end-link t)
536 (put-text-property (1- (match-beginning 1))
537 (match-beginning 1)
538 'rear-nonsticky '(read-only))
539 (put-text-property (match-beginning 1)
540 (match-end 1) 'read-only nil)))
541 (forward-line)
542 (beginning-of-line)))))
545 (defun wdired-get-previous-link (&optional old move)
546 "Return the next symlink target.
547 If OLD, return the old target. If MOVE, move point before it."
548 (let (beg end target)
549 (setq beg (previous-single-property-change (point) 'old-link nil))
550 (if beg
551 (progn
552 (if old
553 (setq target (get-text-property (1- beg) 'old-link))
554 (setq end (next-single-property-change beg 'end-link))
555 (setq target (buffer-substring-no-properties (1+ beg) end)))
556 (if move (goto-char (1- beg)))))
557 (and target (wdired-normalize-filename target))))
560 ;; Perform the changes in the target of the changed links.
561 (defun wdired-do-symlink-changes ()
562 (let ((changes nil)
563 (errors 0)
564 link-to-ori link-to-new link-from)
565 (goto-char (point-max))
566 (while (setq link-to-new (wdired-get-previous-link))
567 (setq link-to-ori (wdired-get-previous-link t t))
568 (setq link-from (wdired-get-filename nil t))
569 (unless (equal link-to-new link-to-ori)
570 (setq changes t)
571 (if (equal link-to-new "") ;empty filename!
572 (setq link-to-new "/dev/null"))
573 (condition-case err
574 (progn
575 (delete-file link-from)
576 (make-symbolic-link
577 (substitute-in-file-name link-to-new) link-from))
578 (error
579 (setq errors (1+ errors))
580 (dired-log (concat "Link `" link-from "' to `"
581 link-to-new "' failed:\n%s\n")
582 err)))))
583 (cons changes errors)))
585 ;; Perform a "case command" skipping read-only words.
586 (defun wdired-xcase-word (command arg)
587 (if (< arg 0)
588 (funcall command arg)
589 (while (> arg 0)
590 (condition-case err
591 (progn
592 (funcall command 1)
593 (setq arg (1- arg)))
594 (error
595 (if (forward-word)
596 ;; Skip any non-word characters to avoid triggering a read-only
597 ;; error which would cause skipping the next word characters too.
598 (skip-syntax-forward "^w")
599 (setq arg 0)))))))
601 (defun wdired-downcase-word (arg)
602 "WDired version of `downcase-word'.
603 Like original function but it skips read-only words."
604 (interactive "p")
605 (wdired-xcase-word 'downcase-word arg))
607 (defun wdired-upcase-word (arg)
608 "WDired version of `upcase-word'.
609 Like original function but it skips read-only words."
610 (interactive "p")
611 (wdired-xcase-word 'upcase-word arg))
613 (defun wdired-capitalize-word (arg)
614 "WDired version of `capitalize-word'.
615 Like original function but it skips read-only words."
616 (interactive "p")
617 (wdired-xcase-word 'capitalize-word arg))
620 ;; The following code deals with changing the access bits (or
621 ;; permissions) of the files.
623 (defvar wdired-perm-mode-map
624 (let ((map (make-sparse-keymap)))
625 (define-key map " " 'wdired-toggle-bit)
626 (define-key map "r" 'wdired-set-bit)
627 (define-key map "w" 'wdired-set-bit)
628 (define-key map "x" 'wdired-set-bit)
629 (define-key map "-" 'wdired-set-bit)
630 (define-key map "S" 'wdired-set-bit)
631 (define-key map "s" 'wdired-set-bit)
632 (define-key map "T" 'wdired-set-bit)
633 (define-key map "t" 'wdired-set-bit)
634 (define-key map "s" 'wdired-set-bit)
635 (define-key map "l" 'wdired-set-bit)
636 (define-key map [down-mouse-1] 'wdired-mouse-toggle-bit)
637 map))
639 ;; Put a keymap property to the permission bits of the files, and store the
640 ;; original name and permissions as a property
641 (defun wdired-preprocess-perms ()
642 (let ((inhibit-read-only t))
643 (set (make-local-variable 'wdired-col-perm) nil)
644 (save-excursion
645 (goto-char (point-min))
646 (while (not (eobp))
647 (when (and (not (looking-at dired-re-sym))
648 (wdired-get-filename)
649 (re-search-forward dired-re-perms (line-end-position) 'eol))
650 (let ((begin (match-beginning 0))
651 (end (match-end 0)))
652 (unless wdired-col-perm
653 (setq wdired-col-perm (- (current-column) 9)))
654 (if (eq wdired-allow-to-change-permissions 'advanced)
655 (progn
656 (put-text-property begin end 'read-only nil)
657 ;; make first permission bit writable
658 (put-text-property
659 (1- begin) begin 'rear-nonsticky '(read-only)))
660 ;; avoid that keymap applies to text following permissions
661 (add-text-properties
662 (1+ begin) end
663 `(keymap ,wdired-perm-mode-map rear-nonsticky (keymap))))
664 (put-text-property end (1+ end) 'end-perm t)
665 (put-text-property
666 begin (1+ begin) 'old-perm (match-string-no-properties 0))))
667 (forward-line)
668 (beginning-of-line)))))
670 (defun wdired-perm-allowed-in-pos (char pos)
671 (cond
672 ((= char ?-) t)
673 ((= char ?r) (= (% pos 3) 0))
674 ((= char ?w) (= (% pos 3) 1))
675 ((= char ?x) (= (% pos 3) 2))
676 ((memq char '(?s ?S)) (memq pos '(2 5)))
677 ((memq char '(?t ?T)) (= pos 8))
678 ((= char ?l) (= pos 5))))
680 (defun wdired-set-bit ()
681 "Set a permission bit character."
682 (interactive)
683 (if (wdired-perm-allowed-in-pos last-command-char
684 (- (current-column) wdired-col-perm))
685 (let ((new-bit (char-to-string last-command-char))
686 (inhibit-read-only t)
687 (pos-prop (- (point) (- (current-column) wdired-col-perm))))
688 (put-text-property 0 1 'keymap wdired-perm-mode-map new-bit)
689 (put-text-property 0 1 'read-only t new-bit)
690 (insert new-bit)
691 (delete-char 1)
692 (put-text-property (1- pos-prop) pos-prop 'perm-changed t)
693 (put-text-property (1- (point)) (point) 'rear-nonsticky '(keymap)))
694 (forward-char 1)))
696 (defun wdired-toggle-bit ()
697 "Toggle the permission bit at point."
698 (interactive)
699 (let ((inhibit-read-only t)
700 (new-bit "-")
701 (pos-prop (- (point) (- (current-column) wdired-col-perm))))
702 (if (eq (char-after (point)) ?-)
703 (setq new-bit
704 (if (= (% (- (current-column) wdired-col-perm) 3) 0) "r"
705 (if (= (% (- (current-column) wdired-col-perm) 3) 1) "w"
706 "x"))))
707 (put-text-property 0 1 'keymap wdired-perm-mode-map new-bit)
708 (put-text-property 0 1 'read-only t new-bit)
709 (insert new-bit)
710 (delete-char 1)
711 (put-text-property (1- pos-prop) pos-prop 'perm-changed t)
712 (put-text-property (1- (point)) (point) 'rear-nonsticky '(keymap))))
714 (defun wdired-mouse-toggle-bit (event)
715 "Toggle the permission bit that was left clicked."
716 (interactive "e")
717 (mouse-set-point event)
718 (wdired-toggle-bit))
720 ;; Allowed chars for 4000 bit are Ss in position 3
721 ;; Allowed chars for 2000 bit are Ssl in position 6
722 ;; Allowed chars for 1000 bit are Tt in position 9
723 (defun wdired-perms-to-number (perms)
724 (let ((nperm 0777))
725 (if (= (elt perms 1) ?-) (setq nperm (- nperm 400)))
726 (if (= (elt perms 2) ?-) (setq nperm (- nperm 200)))
727 (let ((p-bit (elt perms 3)))
728 (if (memq p-bit '(?- ?S)) (setq nperm (- nperm 100)))
729 (if (memq p-bit '(?s ?S)) (setq nperm (+ nperm 4000))))
730 (if (= (elt perms 4) ?-) (setq nperm (- nperm 40)))
731 (if (= (elt perms 5) ?-) (setq nperm (- nperm 20)))
732 (let ((p-bit (elt perms 6)))
733 (if (memq p-bit '(?- ?S ?l)) (setq nperm (- nperm 10)))
734 (if (memq p-bit '(?s ?S ?l)) (setq nperm (+ nperm 2000))))
735 (if (= (elt perms 7) ?-) (setq nperm (- nperm 4)))
736 (if (= (elt perms 8) ?-) (setq nperm (- nperm 2)))
737 (let ((p-bit (elt perms 9)))
738 (if (memq p-bit '(?- ?T)) (setq nperm (- nperm 1)))
739 (if (memq p-bit '(?t ?T)) (setq nperm (+ nperm 1000))))
740 nperm))
742 ;; Perform the changes in the permissions of the files that have
743 ;; changed.
744 (defun wdired-do-perm-changes ()
745 (let ((changes nil)
746 (errors 0)
747 (prop-wanted (if (eq wdired-allow-to-change-permissions 'advanced)
748 'old-perm 'perm-changed))
749 filename perms-ori perms-new perm-tmp)
750 (goto-char (next-single-property-change (point-min) prop-wanted
751 nil (point-max)))
752 (while (not (eobp))
753 (setq perms-ori (get-text-property (point) 'old-perm))
754 (setq perms-new (buffer-substring-no-properties
755 (point) (next-single-property-change (point) 'end-perm)))
756 (unless (equal perms-ori perms-new)
757 (setq changes t)
758 (setq filename (wdired-get-filename nil t))
759 (if (= (length perms-new) 10)
760 (progn
761 (setq perm-tmp
762 (int-to-string (wdired-perms-to-number perms-new)))
763 (unless (equal 0 (process-file dired-chmod-program
764 nil nil nil perm-tmp filename))
765 (setq errors (1+ errors))
766 (dired-log (concat dired-chmod-program " " perm-tmp
767 " `" filename "' failed\n\n"))))
768 (setq errors (1+ errors))
769 (dired-log (concat "Cannot parse permission `" perms-new
770 "' for file `" filename "'\n\n"))))
771 (goto-char (next-single-property-change (1+ (point)) prop-wanted
772 nil (point-max))))
773 (cons changes errors)))
775 (provide 'wdired)
777 ;; Local Variables:
778 ;; coding: latin-1
779 ;; byte-compile-dynamic: t
780 ;; End:
782 ;; arch-tag: bc00902e-526f-4305-bc7f-8862a559184f
783 ;;; wdired.el ends here