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