diff --git a/lisp/replace.el b/lisp/replace.el index 3a908ac..e3c906b 100644 --- a/lisp/replace.el +++ b/lisp/replace.el @@ -1802,6 +1802,7 @@ query-replace-help C-l to clear the screen, redisplay, and offer same replacement again, ! to replace all remaining matches in this buffer with no more questions, ^ to move point back to previous match, +U to undo previous replacement (queries w/ regexp not supported), E to edit the replacement string. In multi-buffer replacements type `Y' to replace all remaining matches in all remaining buffers with no more questions, @@ -1831,6 +1832,8 @@ query-replace-map (define-key map "\C-l" 'recenter) (define-key map "!" 'automatic) (define-key map "^" 'backup) + (define-key map "u" 'undo) + (define-key map "U" 'undo) (define-key map "\C-h" 'help) (define-key map [f1] 'help) (define-key map [help] 'help) @@ -1856,7 +1859,7 @@ query-replace-map `act-and-exit', `exit', `exit-prefix', `recenter', `scroll-up', `scroll-down', `scroll-other-window', `scroll-other-window-down', `edit', `edit-replacement', `delete-and-edit', `automatic', -`backup', `quit', and `help'. +`backup', `undo', `quit', and `help'. This keymap is used by `y-or-n-p' as well as `query-replace'.") @@ -2332,6 +2335,32 @@ perform-replace (message "No previous match") (ding 'no-terminate) (sit-for 1))) + ((eq def 'undo) + (cond (regexp-flag + (message "Undo not supported in queries with regexp") + (ding 'no-terminate) + (sit-for 1)) + (t + ;; bind locally swapped values (search-string/replacement) + (let* ((search-string-1 next-replacement) + (next-replacement search-string) + (search-string search-string-1) + (elt (if stack (pop stack) (match-data)))) + (goto-char (nth 0 elt)) + (setq replaced (nth 1 elt)) + (and replaced (goto-char (- (point) (length next-replacement)))) + + (replace-match-maybe-edit + next-replacement nocasify literal + noedit real-match-data backward) + + (setq noedit nil + replace-count (1- replace-count) + replaced nil + real-match-data + (replace-match-data + t real-match-data + (nth 2 elt))))))) ((eq def 'act) (or replaced (setq noedit