wdired.el 32 KB

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