From 66910f7e7070fb849c7d53e3e785a9ed44e52bfa Mon Sep 17 00:00:00 2001 From: divyansharma001 Date: Wed, 25 Feb 2026 13:38:14 +0530 Subject: [PATCH] Add separate 'Replace all occurrences' button, keep original 'Replace all further occurrences' --- TeXmacs/progs/generic/search-widgets.scm | 19 ++++++++++++ devel/202_105.md | 39 ++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/TeXmacs/progs/generic/search-widgets.scm b/TeXmacs/progs/generic/search-widgets.scm index 728d7d3aba..1d73d10873 100644 --- a/TeXmacs/progs/generic/search-widgets.scm +++ b/TeXmacs/progs/generic/search-widgets.scm @@ -723,6 +723,19 @@ tree 或 #f (perform-search*) (set! isreplace? #t)))) +(tm-define (replace-all-occurrences . args) + (let ((u (if (null? args) (master-buffer) (car args))) + (raux (if (null? args) (replace-buffer) (cadr args)))) + (and-with by (or (by-tree raux) current-replace) + (with-buffer u + (search-extreme-match #f) + (start-editing) + (while (replace-next by) + (perform-search*)) + (end-editing)) + (perform-search*) + (set! isreplace? #t)))) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Customized keyboard shortcuts in search and replace modes ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -957,6 +970,8 @@ tree 或 #f "Replace all further occurrences (Command+Enter)" "Replace all further occurrences (Ctrl+Enter)"))) (replace-all u raux)) + ((balloon (icon "tm_replace_all.xpm") "Replace all occurrences") + (replace-all-occurrences u raux)) >>> (=> (balloon (icon "tm_preferences.xpm") "Search and replace preferences") @@ -996,6 +1011,8 @@ tree 或 #f (replace-one)) ((balloon (icon "tm_replace_all.xpm") "Replace all further occurrences") (replace-all)) + ((balloon (icon "tm_replace_all.xpm") "Replace all occurrences") + (replace-all-occurrences)) >>> (=> (balloon (icon "tm_preferences.xpm") "Search and replace preferences") @@ -1197,6 +1214,8 @@ tree 或 #f (replace-one)) ((balloon (icon "tm_replace_all.xpm") "Replace all further occurrences") (replace-all)) + ((balloon (icon "tm_replace_all.xpm") "Replace all occurrences") + (replace-all-occurrences)) >>> ((check (balloon (icon "tm_filter.xpm") "Only show paragraphs with hits") "v" (search-filter-enabled?)) diff --git a/devel/202_105.md b/devel/202_105.md index 7da3a7a26f..84e9b46225 100644 --- a/devel/202_105.md +++ b/devel/202_105.md @@ -31,3 +31,42 @@ else ``` Also removed the now-unused `("meta::keyboard" "")` entry from `TeXmacs/plugins/lang/dic/en_US/zh_CN.scm`. + +# [202_105] Search and replace: add "Replace all occurrences" button + +## How to test +1. Open a document with multiple occurrences of a word (e.g. "hello" appearing 5 times) +2. Open search and replace (Edit → Search and replace) +3. Place cursor in the middle of the document (e.g. after the 3rd occurrence) +4. Enter the search term "hello" and the replacement "world" +5. Click "Replace all further occurrences" (or press Ctrl+Enter / Command+Enter) +6. Confirm that only the occurrences **after the cursor** are replaced (original behavior preserved) +7. Undo, then click "Replace all occurrences" +8. Confirm that **all 5 occurrences** are replaced, including those before the cursor + +## 2026/02/25 +### What +Add a separate "Replace all occurrences" button that replaces every match in the entire document, while keeping the original "Replace all further occurrences" button and behavior. + +### Why +Users expect a way to replace every occurrence in the document regardless of cursor position (issue #2857). The maintainer requested this as a separate button so the original "Replace all further occurrences" behavior is preserved. + +### How +Added a new `replace-all-occurrences` function that calls `(search-extreme-match #f)` before the replace loop to navigate to the first match, so replacement starts from the beginning of the document. The original `replace-all` function is kept unchanged. + +```scheme +(tm-define (replace-all-occurrences . args) + (let ((u (if (null? args) (master-buffer) (car args))) + (raux (if (null? args) (replace-buffer) (cadr args)))) + (and-with by (or (by-tree raux) current-replace) + (with-buffer u + (search-extreme-match #f) ;; Go to the first match + (start-editing) + (while (replace-next by) + (perform-search*)) + (end-editing)) + (perform-search*) + (set! isreplace? #t)))) +``` + +Added a new "Replace all occurrences" button in all 3 replace UI locations (dialog, side panel, toolbar) alongside the existing "Replace all further occurrences" button.