]> code.delx.au - gnu-emacs/blob - lisp/wdired.el
Fix typos.
[gnu-emacs] / lisp / wdired.el
1 ;;; wdired.el --- Rename files editing their names in dired buffers
2
3 ;; Copyright (C) 2004, 2005, 2006 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
106 (eval-when-compile (require 'cl))
107 (require 'dired)
108 (autoload 'dired-do-create-files-regexp "dired-aux")
109 (autoload 'dired-call-process "dired-aux")
110
111 (defgroup wdired nil
112 "Mode to rename files by editing their names in dired buffers."
113 :group 'dired)
114
115 (defcustom wdired-use-interactive-rename nil
116 "If non-nil, WDired requires confirmation before actually renaming files.
117 If nil, WDired doesn't require confirmation to change the file names,
118 and the variable `wdired-confirm-overwrite' controls whether it is ok
119 to overwrite files without asking."
120 :type 'boolean
121 :group 'wdired)
122
123 (defcustom wdired-confirm-overwrite t
124 "If nil the renames can overwrite files without asking.
125 This variable has no effect at all if `wdired-use-interactive-rename'
126 is not nil."
127 :type 'boolean
128 :group 'wdired)
129
130 (defcustom wdired-use-dired-vertical-movement nil
131 "If t, the \"up\" and \"down\" movement works as in Dired mode.
132 That is, always move the point to the beginning of the filename at line.
133
134 If `sometimes, only move to the beginning of filename if the point is
135 before it, and `track-eol' is honored. This behavior is very handy
136 when editing several filenames.
137
138 If nil, \"up\" and \"down\" movement is done as in any other buffer."
139 :type '(choice (const :tag "As in any other mode" nil)
140 (const :tag "Smart cursor placement" sometimes)
141 (other :tag "As in dired mode" t))
142 :group 'wdired)
143
144 (defcustom wdired-allow-to-redirect-links t
145 "If non-nil, the target of the symbolic links are editable.
146 In systems without symbolic links support, this variable has no effect
147 at all."
148 :type 'boolean
149 :group 'wdired)
150
151 (defcustom wdired-allow-to-change-permissions nil
152 "If non-nil, the permissions bits of the files are editable.
153
154 If t, to change a single bit, put the cursor over it and press the
155 space bar, or left click over it. You can also hit the letter you want
156 to set: if this value is allowed, the character in the buffer will be
157 changed. Anyway, the point is advanced one position, so, for example,
158 you can keep the <x> key pressed to give execution permissions to
159 everybody to that file.
160
161 If `advanced, the bits are freely editable. You can use
162 `string-rectangle', `query-replace', etc. You can put any value (even
163 newlines), but if you want your changes to be useful, you better put a
164 intelligible value.
165
166 Anyway, the real change of the permissions is done by the external
167 program `dired-chmod-program', which must exist."
168 :type '(choice (const :tag "Not allowed" nil)
169 (const :tag "Toggle/set bits" t)
170 (other :tag "Bits freely editable" advanced))
171 :group 'wdired)
172
173 (defvar wdired-mode-map
174 (let ((map (make-sparse-keymap)))
175 (define-key map "\C-x\C-s" 'wdired-finish-edit)
176 (define-key map "\C-c\C-c" 'wdired-finish-edit)
177 (define-key map "\C-c\C-k" 'wdired-abort-changes)
178 (define-key map "\C-c\C-[" 'wdired-abort-changes)
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)
186
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))
198
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)
202
203 map))
204
205 (defvar wdired-mode-hook nil
206 "Hooks run when changing to WDired mode.")
207
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)
212
213
214 (defun wdired-mode ()
215 "\\<wdired-mode-map>File Names Editing mode.
216
217 Press \\[wdired-finish-edit] to make the changes to take effect
218 and exit. To abort the edit, use \\[wdired-abort-changes].
219
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.
223
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)
229
230
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.
238
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")))
271
272
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 (put-text-property (- (point) 2) (1- (point)) 'old-name filename)
287 (put-text-property b-protection (1- (point)) 'read-only t)
288 (setq b-protection (dired-move-to-end-of-filename t)))
289 (put-text-property (point) (1+ (point)) 'end-name t)
290 (forward-line))
291 (put-text-property b-protection (point-max) 'read-only t))))
292
293 ;; This code is a copy of some dired-get-filename lines.
294 (defsubst wdired-normalize-filename (file)
295 (setq file
296 ;; FIXME: shouldn't we check for a `b' argument or somesuch before
297 ;; doing such unquoting? --Stef
298 (read (concat
299 "\"" (replace-regexp-in-string
300 "\\([^\\]\\|\\`\\)\"" "\\1\\\\\"" file)
301 "\"")))
302 (and file buffer-file-coding-system
303 (not file-name-coding-system)
304 (not default-file-name-coding-system)
305 (setq file (encode-coding-string file buffer-file-coding-system)))
306 file)
307
308 (defun wdired-get-filename (&optional no-dir old)
309 "Return the filename at line.
310 Similar to `dired-get-filename' but it doesn't rely on regexps. It
311 relies on WDired buffer's properties. Optional arg NO-DIR with value
312 non-nil means don't include directory. Optional arg OLD with value
313 non-nil means return old filename."
314 ;; FIXME: Use dired-get-filename's new properties.
315 (let* ((end (line-end-position))
316 (beg (next-single-property-change
317 (line-beginning-position) 'old-name nil end)))
318 (unless (eq beg end)
319 (let ((file
320 (if old
321 (get-text-property beg 'old-name)
322 (wdired-normalize-filename
323 (buffer-substring-no-properties
324 (+ 2 beg) (next-single-property-change (1+ beg) 'end-name))))))
325 (if (or no-dir old)
326 file
327 (and file (> (length file) 0)
328 (concat (dired-current-directory) file)))))))
329
330
331 (defun wdired-change-to-dired-mode ()
332 "Change the mode back to dired."
333 (or (eq major-mode 'wdired-mode)
334 (error "Not a Wdired buffer"))
335 (let ((inhibit-read-only t))
336 (remove-text-properties (point-min) (point-max)
337 '(read-only nil local-map nil)))
338 (put-text-property 1 2 'front-sticky nil)
339 (use-local-map dired-mode-map)
340 (force-mode-line-update)
341 (setq buffer-read-only t)
342 (setq major-mode 'dired-mode)
343 (setq mode-name "Dired")
344 (dired-advertise)
345 (remove-hook 'kill-buffer-hook 'wdired-check-kill-buffer t)
346 (set (make-local-variable 'revert-buffer-function) 'dired-revert))
347
348
349 (defun wdired-abort-changes ()
350 "Abort changes and return to dired mode."
351 (interactive)
352 (let ((inhibit-read-only t))
353 (erase-buffer)
354 (insert wdired-old-content)
355 (goto-char wdired-old-point))
356 (wdired-change-to-dired-mode)
357 (set-buffer-modified-p nil)
358 (setq buffer-undo-list nil)
359 (message "Changes aborted"))
360
361 (defun wdired-finish-edit ()
362 "Actually rename files based on your editing in the Dired buffer."
363 (interactive)
364 (wdired-change-to-dired-mode)
365 (let ((overwrite (or (not wdired-confirm-overwrite) 1))
366 (changes nil)
367 (files-deleted nil)
368 (errors 0)
369 file-ori file-new tmp-value)
370 (save-excursion
371 (if (and wdired-allow-to-redirect-links
372 (fboundp 'make-symbolic-link))
373 (progn
374 (setq tmp-value (wdired-do-symlink-changes))
375 (setq errors (cdr tmp-value))
376 (setq changes (car tmp-value))))
377 (if (and wdired-allow-to-change-permissions
378 (boundp 'wdired-col-perm)) ; could have been changed
379 (progn
380 (setq tmp-value (wdired-do-perm-changes))
381 (setq errors (+ errors (cdr tmp-value)))
382 (setq changes (or changes (car tmp-value)))))
383 (goto-char (point-max))
384 (while (not (bobp))
385 (setq file-ori (wdired-get-filename nil t))
386 (if file-ori
387 (setq file-new (wdired-get-filename)))
388 (if (and file-ori (not (equal file-new file-ori)))
389 (progn
390 (setq changes t)
391 (if (not file-new) ;empty filename!
392 (setq files-deleted (cons file-ori files-deleted))
393 (progn
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 (if files-deleted
421 (wdired-flag-for-deletion files-deleted))
422 (if (> 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))
426
427 ;; Rename a file, searching it in a modified dired buffer, in order
428 ;; to be able to use `dired-do-create-files-regexp' and get its
429 ;; "benefits".
430 (defun wdired-search-and-rename (filename-ori filename-new)
431 (save-excursion
432 (goto-char (point-max))
433 (forward-line -1)
434 (let ((exit-while nil)
435 curr-filename)
436 (while (not exit-while)
437 (setq curr-filename (wdired-get-filename))
438 (if (and curr-filename
439 (equal (substitute-in-file-name curr-filename) filename-new))
440 (progn
441 (setq exit-while t)
442 (let ((inhibit-read-only t))
443 (dired-move-to-filename)
444 (search-forward (wdired-get-filename t) nil t)
445 (replace-match (file-name-nondirectory filename-ori) t t))
446 (dired-do-create-files-regexp
447 (function dired-rename-file)
448 "Move" 1 ".*" filename-new nil t))
449 (progn
450 (forward-line -1)
451 (beginning-of-line)
452 (setq exit-while (= 1 (point)))))))))
453
454 ;; marks a list of files for deletion
455 (defun wdired-flag-for-deletion (filenames-ori)
456 (save-excursion
457 (goto-char (point-min))
458 (while (not (eobp))
459 (if (member (dired-get-filename nil t) filenames-ori)
460 (dired-flag-file-deletion 1)
461 (forward-line)))))
462
463 (defun wdired-customize ()
464 "Customize WDired options."
465 (interactive)
466 (customize-apropos "wdired" 'groups))
467
468 (defun wdired-revert (&optional arg noconfirm)
469 "Discard changes in the buffer and update it based on changes on disk.
470 Optional arguments are ignored."
471 (wdired-change-to-dired-mode)
472 (revert-buffer)
473 (wdired-change-to-wdired-mode))
474
475 (defun wdired-check-kill-buffer ()
476 ;; FIXME: Can't we use the normal mechanism for that? --Stef
477 (if (and
478 (buffer-modified-p)
479 (not (y-or-n-p "Buffer changed. Discard changes and kill buffer? ")))
480 (error nil)))
481
482 (defun wdired-next-line (arg)
483 "Move down lines then position at filename or the current column.
484 See `wdired-use-dired-vertical-movement'. Optional prefix ARG
485 says how many lines to move; default is one line."
486 (interactive "p")
487 (next-line arg)
488 (if (or (eq wdired-use-dired-vertical-movement t)
489 (and wdired-use-dired-vertical-movement
490 (< (current-column)
491 (save-excursion (dired-move-to-filename)
492 (current-column)))))
493 (dired-move-to-filename)))
494
495 (defun wdired-previous-line (arg)
496 "Move up lines then position at filename or the current column.
497 See `wdired-use-dired-vertical-movement'. Optional prefix ARG
498 says how many lines to move; default is one line."
499 (interactive "p")
500 (previous-line arg)
501 (if (or (eq wdired-use-dired-vertical-movement t)
502 (and wdired-use-dired-vertical-movement
503 (< (current-column)
504 (save-excursion (dired-move-to-filename)
505 (current-column)))))
506 (dired-move-to-filename)))
507
508 ;; Put the needed properties to allow the user to change links' targets
509 (defun wdired-preprocess-symlinks ()
510 (let ((inhibit-read-only t))
511 (save-excursion
512 (goto-char (point-min))
513 (while (not (eobp))
514 (if (looking-at dired-re-sym)
515 (progn
516 (re-search-forward " -> \\(.*\\)$")
517 (put-text-property (- (match-beginning 1) 2)
518 (1- (match-beginning 1)) 'old-link
519 (match-string-no-properties 1))
520 (put-text-property (match-end 1) (1+ (match-end 1)) 'end-link t)
521 (put-text-property (1- (match-beginning 1))
522 (match-end 1) 'read-only nil)))
523 (forward-line)
524 (beginning-of-line)))))
525
526
527 (defun wdired-get-previous-link (&optional old move)
528 "Return the next symlink target.
529 If OLD, return the old target. If MOVE, move point before it."
530 (let ((beg (previous-single-property-change (point) 'old-link nil)))
531 (when beg
532 (let ((target
533 (if old
534 (get-text-property (1- beg) 'old-link)
535 (buffer-substring-no-properties
536 (1+ beg) (next-single-property-change beg 'end-link)))))
537 (if move (goto-char (1- beg)))
538 (and target (wdired-normalize-filename target))))))
539
540 ;; Perform the changes in the target of the changed links.
541 (defun wdired-do-symlink-changes ()
542 (let ((changes nil)
543 (errors 0)
544 link-to-ori link-to-new link-from)
545 (goto-char (point-max))
546 (while (setq link-to-new (wdired-get-previous-link))
547 (setq link-to-ori (wdired-get-previous-link t t))
548 (setq link-from (wdired-get-filename nil t))
549 (unless (equal link-to-new link-to-ori)
550 (setq changes t)
551 (if (equal link-to-new "") ;empty filename!
552 (setq link-to-new "/dev/null"))
553 (condition-case err
554 (progn
555 (delete-file link-from)
556 (make-symbolic-link
557 (substitute-in-file-name link-to-new) link-from))
558 (error
559 (setq errors (1+ errors))
560 (dired-log (concat "Link `" link-from "' to `"
561 link-to-new "' failed:\n%s\n")
562 err)))))
563 (cons changes errors)))
564
565 ;; Perform a "case command" skipping read-only words.
566 (defun wdired-xcase-word (command arg)
567 (if (< arg 0)
568 (funcall command arg)
569 (while (> arg 0)
570 (condition-case err
571 (progn
572 (funcall command 1)
573 (setq arg (1- arg)))
574 (error
575 (if (not (forward-word 1))
576 (setq arg 0)))))))
577
578 (defun wdired-downcase-word (arg)
579 "WDired version of `downcase-word'.
580 Like original function but it skips read-only words."
581 (interactive "p")
582 (wdired-xcase-word 'downcase-word arg))
583
584 (defun wdired-upcase-word (arg)
585 "WDired version of `upcase-word'.
586 Like original function but it skips read-only words."
587 (interactive "p")
588 (wdired-xcase-word 'upcase-word arg))
589
590 (defun wdired-capitalize-word (arg)
591 "WDired version of `capitalize-word'.
592 Like original function but it skips read-only words."
593 (interactive "p")
594 (wdired-xcase-word 'capitalize-word arg))
595
596
597 ;; The following code deals with changing the access bits (or
598 ;; permissions) of the files.
599
600 (defvar wdired-perm-mode-map
601 (let ((map (make-sparse-keymap)))
602 (define-key map " " 'wdired-toggle-bit)
603 (define-key map "r" 'wdired-set-bit)
604 (define-key map "w" 'wdired-set-bit)
605 (define-key map "x" 'wdired-set-bit)
606 (define-key map "-" 'wdired-set-bit)
607 (define-key map "S" 'wdired-set-bit)
608 (define-key map "s" 'wdired-set-bit)
609 (define-key map "T" 'wdired-set-bit)
610 (define-key map "t" 'wdired-set-bit)
611 (define-key map "s" 'wdired-set-bit)
612 (define-key map "l" 'wdired-set-bit)
613 (define-key map [down-mouse-1] 'wdired-mouse-toggle-bit)
614 map))
615
616 ;; Put a local-map to the permission bits of the files, and store the
617 ;; original name and permissions as a property
618 (defun wdired-preprocess-perms ()
619 (let ((inhibit-read-only t)
620 filename)
621 (set (make-local-variable 'wdired-col-perm) nil)
622 (save-excursion
623 (goto-char (point-min))
624 (while (not (eobp))
625 (if (and (not (looking-at dired-re-sym))
626 (setq filename (wdired-get-filename)))
627 (progn
628 (re-search-forward dired-re-perms)
629 (or wdired-col-perm
630 (setq wdired-col-perm (- (current-column) 9)))
631 (if (eq wdired-allow-to-change-permissions 'advanced)
632 (put-text-property (match-beginning 0) (match-end 0)
633 'read-only nil)
634 (put-text-property (1+ (match-beginning 0)) (match-end 0)
635 'keymap wdired-perm-mode-map))
636 (put-text-property (match-end 0) (1+ (match-end 0)) 'end-perm t)
637 (put-text-property (match-beginning 0) (1+ (match-beginning 0))
638 'old-perm (match-string-no-properties 0))))
639 (forward-line)
640 (beginning-of-line)))))
641
642 (defun wdired-perm-allowed-in-pos (char pos)
643 (cond
644 ((= char ?-) t)
645 ((= char ?r) (= (% pos 3) 0))
646 ((= char ?w) (= (% pos 3) 1))
647 ((= char ?x) (= (% pos 3) 2))
648 ((memq char '(?s ?S)) (memq pos '(2 5)))
649 ((memq char '(?t ?T)) (= pos 8))
650 ((= char ?l) (= pos 5))))
651
652 (defun wdired-set-bit ()
653 "Set a permission bit character."
654 (interactive)
655 (if (wdired-perm-allowed-in-pos last-command-char
656 (- (current-column) wdired-col-perm))
657 (let ((new-bit (char-to-string last-command-char))
658 (inhibit-read-only t)
659 (pos-prop (- (point) (- (current-column) wdired-col-perm))))
660 (put-text-property 0 1 'keymap wdired-perm-mode-map new-bit)
661 (put-text-property 0 1 'read-only t new-bit)
662 (insert new-bit)
663 (delete-char 1)
664 (put-text-property pos-prop (1- pos-prop) 'perm-changed t))
665 (forward-char 1)))
666
667 (defun wdired-toggle-bit ()
668 "Toggle the permission bit at point."
669 (interactive)
670 (let ((inhibit-read-only t)
671 (new-bit (cond
672 ((not (eq (char-after (point)) ?-)) "-")
673 ((= (% (- (current-column) wdired-col-perm) 3) 0) "r")
674 ((= (% (- (current-column) wdired-col-perm) 3) 1) "w")
675 (t "x")))
676 (pos-prop (- (point) (- (current-column) wdired-col-perm))))
677 (put-text-property 0 1 'keymap wdired-perm-mode-map new-bit)
678 (put-text-property 0 1 'read-only t new-bit)
679 (insert new-bit)
680 (delete-char 1)
681 (put-text-property pos-prop (1- pos-prop) 'perm-changed t)))
682
683 (defun wdired-mouse-toggle-bit (event)
684 "Toggle the permission bit that was left clicked."
685 (interactive "e")
686 (mouse-set-point event)
687 (wdired-toggle-bit))
688
689 ;; Allowed chars for 4000 bit are Ss in position 3
690 ;; Allowed chars for 2000 bit are Ssl in position 6
691 ;; Allowed chars for 1000 bit are Tt in position 9
692 (defun wdired-perms-to-number (perms)
693 (+
694 (if (= (elt perms 1) ?-) 0 400)
695 (if (= (elt perms 2) ?-) 0 200)
696 (case (elt perms 3)
697 (?- 0)
698 (?S 4000)
699 (?s 4100)
700 (t 100))
701 (if (= (elt perms 4) ?-) 0 40)
702 (if (= (elt perms 5) ?-) 0 20)
703 (case (elt perms 6)
704 (?- 0)
705 (?S 2000)
706 (?s 2010)
707 (t 10))
708 (if (= (elt perms 7) ?-) 0 4)
709 (if (= (elt perms 8) ?-) 0 2)
710 (case (elt perms 9)
711 (?- 0)
712 (?T 1000)
713 (?t 1001)
714 (t 1))))
715
716 ;; Perform the changes in the permissions of the files that have
717 ;; changed.
718 (defun wdired-do-perm-changes ()
719 (let ((changes nil)
720 (errors 0)
721 (prop-wanted (if (eq wdired-allow-to-change-permissions 'advanced)
722 'old-perm 'perm-changed))
723 filename perms-ori perms-new perm-tmp)
724 (goto-char (next-single-property-change (point-min) prop-wanted
725 nil (point-max)))
726 (while (not (eobp))
727 (setq perms-ori (get-text-property (point) 'old-perm))
728 (setq perms-new (buffer-substring-no-properties
729 (point) (next-single-property-change (point) 'end-perm)))
730 (unless (equal perms-ori perms-new)
731 (setq changes t)
732 (setq filename (wdired-get-filename nil t))
733 (if (= (length perms-new) 10)
734 (progn
735 (setq perm-tmp
736 (int-to-string (wdired-perms-to-number perms-new)))
737 (unless (equal 0 (dired-call-process dired-chmod-program
738 t perm-tmp filename))
739 (setq errors (1+ errors))
740 (dired-log (concat dired-chmod-program " " perm-tmp
741 " `" filename "' failed\n\n"))))
742 (setq errors (1+ errors))
743 (dired-log (concat "Cannot parse permission `" perms-new
744 "' for file `" filename "'\n\n"))))
745 (goto-char (next-single-property-change (1+ (point)) prop-wanted
746 nil (point-max))))
747 (cons changes errors)))
748
749 (provide 'wdired)
750
751 ;; Local Variables:
752 ;; coding: latin-1
753 ;; byte-compile-dynamic: t
754 ;; End:
755
756 ;; arch-tag: bc00902e-526f-4305-bc7f-8862a559184f
757 ;;; wdired.el ends here