From cddace1dae6cb9e4367f1fad3d7bd3745282824d Mon Sep 17 00:00:00 2001 From: Arthur Miller Date: Wed, 17 Mar 2021 23:24:10 +0100 Subject: [PATCH] Make wdired lazy --- lisp/wdired.el | 156 +++++++++++++++++++++++-------------------------- 1 file changed, 74 insertions(+), 82 deletions(-) diff --git a/lisp/wdired.el b/lisp/wdired.el index c495d8de34..ba71306e66 100644 --- a/lisp/wdired.el +++ b/lisp/wdired.el @@ -250,20 +250,11 @@ wdired-change-to-wdired-mode (setq buffer-read-only nil) (dired-unadvertise default-directory) (add-hook 'kill-buffer-hook 'wdired-check-kill-buffer nil t) + (add-hook 'before-change-functions 'wdired--preprocess-line nil t) (add-hook 'after-change-functions 'wdired--restore-properties nil t) (setq major-mode 'wdired-mode) (setq mode-name "Editable Dired") (setq revert-buffer-function 'wdired-revert) - ;; I temp disable undo for performance: since I'm going to clear the - ;; undo list, it can save more than a 9% of time with big - ;; directories because setting properties modify the undo-list. - (buffer-disable-undo) - (wdired-preprocess-files) - (if wdired-allow-to-change-permissions - (wdired-preprocess-perms)) - (if (fboundp 'make-symbolic-link) - (wdired-preprocess-symlinks)) - (buffer-enable-undo) ; Performance hack. See above. (set-buffer-modified-p nil) (setq buffer-undo-list nil) (run-mode-hooks 'wdired-mode-hook) @@ -278,25 +269,33 @@ wdired-isearch-filter-read-only ;; Protect the buffer so only the filenames can be changed, and put ;; properties so filenames (old and new) can be easily found. -(defun wdired-preprocess-files () - (put-text-property (point-min) (1+ (point-min))'front-sticky t) - (save-excursion - (goto-char (point-min)) - (let ((b-protection (point)) - (used-F (dired-check-switches dired-actual-switches "F" "classify")) - filename) - (while (not (eobp)) - (setq filename (dired-get-filename nil t)) - (when (and filename - (not (member (file-name-nondirectory filename) '("." "..")))) - (dired-move-to-filename) - ;; The rear-nonsticky property below shall ensure that text preceding - ;; the filename can't be modified. - (add-text-properties - (1- (point)) (point) `(old-name ,filename rear-nonsticky (read-only))) - (put-text-property b-protection (point) 'read-only t) - (dired-move-to-end-of-filename t) - (put-text-property (point) (1+ (point)) 'end-name t)) +(defun wdired--preprocess-line (beg end) + "Preprocess current line under point to make it writable. " + (let ((inhibit-read-only t)) + (unless (get-text-property (line-beginning-position) 'front-sticky) + (buffer-disable-undo) + (put-text-property (line-beginning-position) (1+ + (line-beginning-position)) + 'front-sticky t) + (save-excursion + (goto-char (line-beginning-position)) + (let ((b-protection (point)) + (used-F (dired-check-switches + dired-actual-switches "F" "classify")) + filename) + (setq filename (dired-get-filename nil t)) + (when (and filename + (not (member (file-name-nondirectory filename) '("." + "..")))) + (dired-move-to-filename) + ;; The rear-nonsticky property below shall ensure that text + ;; preceding the filename can't be modified. + (add-text-properties + (1- (point)) (point) + `(old-name ,filename rear-nonsticky (read-only))) + (put-text-property b-protection (point) 'read-only t) + (dired-move-to-end-of-filename t) + (put-text-property (point) (1+ (point)) 'end-name t)) (when (and used-F (looking-at "[*/@|=>]$")) (forward-char)) (when (save-excursion (and (re-search-backward @@ -304,9 +303,51 @@ wdired-preprocess-files (looking-at "l") (search-forward " -> " (line-end-position) t))) (goto-char (line-end-position))) - (setq b-protection (point)) - (forward-line)) - (put-text-property b-protection (point-max) 'read-only t)))) + (setq b-protection (point)) + (put-text-property b-protection (line-end-position) + 'read-only t)) + + ;; Put a keymap property to the permission bits of the files, + ;; and store the original name and permissions as a property + (when wdired-allow-to-change-permissions + (goto-char (line-beginning-position)) + (setq-local wdired-col-perm nil) + (when (and (not (looking-at dired-re-sym)) + (wdired-get-filename) + (re-search-forward dired-re-perms + (line-end-position) 'eol)) + (let ((begin (match-beginning 0)) + (end (match-end 0))) + (unless wdired-col-perm + (setq wdired-col-perm (- (current-column) 9))) + (if (eq wdired-allow-to-change-permissions 'advanced) + (progn + (put-text-property begin end 'read-only nil) + ;; make first permission bit writable + (put-text-property + (1- begin) begin 'rear-nonsticky '(read-only))) + ;; avoid that keymap applies to text following permissions + (add-text-properties + (1+ begin) end + `(keymap ,wdired-perm-mode-map rear-nonsticky (keymap)))) + (put-text-property end (1+ end) 'end-perm t) + (put-text-property + begin (1+ begin) 'old-perm (match-string-no-properties 0))))) + + ;; Put the needed properties to allow the user to change + ;; links' targets + (when (fboundp 'make-symbolic-link) + (goto-char (line-beginning-position)) + (when (looking-at dired-re-sym) + (re-search-forward " -> \\(.*\\)$") + (put-text-property (1- (match-beginning 1)) + (match-beginning 1) 'old-link + (match-string-no-properties 1)) + (put-text-property (match-end 1) (1+ (match-end 1)) 'end-link t) + (unless wdired-allow-to-redirect-links + (put-text-property (match-beginning 0) + (match-end 1) 'read-only t)))))) + (buffer-enable-undo))) ;; This code is a copy of some dired-get-filename lines. (defsubst wdired-normalize-filename (file unquotep) @@ -369,7 +410,6 @@ wdired-get-filename (and file (> (length file) 0) (concat (dired-current-directory) file)))))) - (defun wdired-change-to-dired-mode () "Change the mode back to dired." (or (eq major-mode 'wdired-mode) @@ -390,10 +430,10 @@ wdired-change-to-dired-mode (remove-hook 'after-change-functions 'wdired--restore-properties t) (setq-local revert-buffer-function 'dired-revert)) - (defun wdired-abort-changes () "Abort changes and return to dired mode." (interactive) + (remove-hook 'before-change-functions 'wdired--preprocess-line t) (let ((inhibit-read-only t)) (erase-buffer) (insert wdired-old-content) @@ -709,23 +749,6 @@ wdired-previous-line (current-column))))) (dired-move-to-filename))) -;; Put the needed properties to allow the user to change links' targets -(defun wdired-preprocess-symlinks () - (let ((inhibit-read-only t)) - (save-excursion - (goto-char (point-min)) - (while (not (eobp)) - (when (looking-at dired-re-sym) - (re-search-forward " -> \\(.*\\)$") - (put-text-property (1- (match-beginning 1)) - (match-beginning 1) 'old-link - (match-string-no-properties 1)) - (put-text-property (match-end 1) (1+ (match-end 1)) 'end-link t) - (unless wdired-allow-to-redirect-links - (put-text-property (match-beginning 0) - (match-end 1) 'read-only t))) - (forward-line))))) - (defun wdired-get-previous-link (&optional old move) "Return the next symlink target. If OLD, return the old target. If MOVE, move point before it." @@ -828,37 +851,6 @@ wdired-perm-mode-map (define-key map [down-mouse-1] 'wdired-mouse-toggle-bit) map)) -;; Put a keymap property to the permission bits of the files, and store the -;; original name and permissions as a property -(defun wdired-preprocess-perms () - (let ((inhibit-read-only t)) - (setq-local wdired-col-perm nil) - (save-excursion - (goto-char (point-min)) - (while (not (eobp)) - (when (and (not (looking-at dired-re-sym)) - (wdired-get-filename) - (re-search-forward dired-re-perms (line-end-position) 'eol)) - (let ((begin (match-beginning 0)) - (end (match-end 0))) - (unless wdired-col-perm - (setq wdired-col-perm (- (current-column) 9))) - (if (eq wdired-allow-to-change-permissions 'advanced) - (progn - (put-text-property begin end 'read-only nil) - ;; make first permission bit writable - (put-text-property - (1- begin) begin 'rear-nonsticky '(read-only))) - ;; avoid that keymap applies to text following permissions - (add-text-properties - (1+ begin) end - `(keymap ,wdired-perm-mode-map rear-nonsticky (keymap)))) - (put-text-property end (1+ end) 'end-perm t) - (put-text-property - begin (1+ begin) 'old-perm (match-string-no-properties 0)))) - (forward-line) - (beginning-of-line))))) - (defun wdired-perm-allowed-in-pos (char pos) (cond ((= char ?-) t) -- 2.31.0