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