wdired.el 31 KB

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