Skip to content

Commit cc9f8aa

Browse files
wumoinda-liii
andauthored
[202_100] 查找替换增加显示当前查找替换位置的功能 (#2284)
Co-authored-by: Da Shen <da@liii.pro>
1 parent 8c548e8 commit cc9f8aa

File tree

6 files changed

+104
-8
lines changed

6 files changed

+104
-8
lines changed

TeXmacs/progs/generic/search-widgets.scm

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@
1515
(:use (generic generic-edit)
1616
(utils library cursor)))
1717

18+
(define search-replace-text "")
19+
20+
21+
(tm-define (update-search-pos-text action)
22+
(let* ((index-str (get-alt-selection-index "alternate" action)))
23+
(if (== index-str "")
24+
(set-auxiliary-widget-title (translate search-replace-text))
25+
(set-auxiliary-widget-title
26+
(string-append (translate search-replace-text) " (" index-str ")")))))
27+
28+
29+
1830
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1931
;; Basic search and replace buffers management
2032
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -153,6 +165,7 @@
153165
(begin
154166
(selection-cancel)
155167
(cancel-alt-selection "alternate")
168+
(update-search-pos-text "new")
156169
(go-to** (get-search-reference #t)))
157170
(let* ((t (buffer-tree))
158171
(sels* (tree-perform-search t what (tree->path t) limit))
@@ -165,13 +178,15 @@
165178
((null? sels)
166179
(selection-cancel)
167180
(cancel-alt-selection "alternate")
181+
(update-search-pos-text "new")
168182
(go-to** (get-search-reference #t))
169183
(set! ok? #f))
170184
(else
171185
(set-alt-selection "alternate" sels)
172186
(with after? (next-search-result #t #f)
173187
(when (not after?)
174-
(selection-cancel))))))))
188+
(selection-cancel))
189+
(update-search-pos-text "new")))))))
175190
(with-buffer (search-buffer)
176191
(if ok?
177192
(init-default "bg-color")
@@ -219,6 +234,7 @@
219234
(selection-set-range-set sel)
220235
(go-to* (car sel))
221236
(when strict? (set-search-reference (car sel)))
237+
(update-search-pos-text (if forward? "next" "previous"))
222238
#t))))
223239

224240
(define (extreme-search-result last?)
@@ -228,7 +244,8 @@
228244
(begin
229245
(selection-set-range-set sel)
230246
(go-to* (car sel))
231-
(set-search-reference (car sel))))))
247+
(set-search-reference (car sel))
248+
(update-search-pos-text (if last? "last" "first"))))))
232249

233250
(tm-define (search-next-match forward?)
234251
(with-buffer (master-buffer)
@@ -368,7 +385,8 @@
368385
(start-editing)
369386
(replace-next by)
370387
(end-editing))
371-
(perform-search*)))
388+
(perform-search*)
389+
(update-search-pos-text "new")))
372390

373391
(tm-define (replace-all)
374392
(and-with by (or (by-tree) current-replace)
@@ -377,7 +395,8 @@
377395
(while (replace-next by)
378396
(perform-search*))
379397
(end-editing))
380-
(perform-search*)))
398+
(perform-search*)
399+
(update-search-pos-text "new")))
381400

382401
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
383402
;; Customized keyboard shortcuts in search and replace modes
@@ -970,6 +989,7 @@
970989

971990
(tm-define (interactive-search)
972991
(:interactive #t)
992+
(set! search-replace-text "search")
973993
(set-boolean-preference "search-and-replace" #f)
974994
(if (and (get-boolean-preference "toolbar search")
975995
(not (buffer-aux? (current-buffer))))
@@ -978,6 +998,7 @@
978998

979999
(tm-define (interactive-replace)
9801000
(:interactive #t)
1001+
(set! search-replace-text "search and replace")
9811002
(set-boolean-preference "search-and-replace" #t)
9821003
(if (and (get-boolean-preference "toolbar replace")
9831004
(not (buffer-aux? (current-buffer))))

devel/202_100.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# 202_100
2+
## 如何测试
3+
1. 打开`帮助->欢迎`
4+
2. 点击`编辑->查找`,输入``,此时能够在标题观察到查找功能所定位的位置,点击更换查找位置,观察标题中所显示的位置是否准确。
5+
3. 关闭查找窗口,点击`编辑->替换`,在查找框中输入``,此时能够在标题观察到查找功能所定位的位置,点击更换查找位置,观察标题中所显示的位置是否准确。
6+
4. 在替换框中输入`1`,并执行替换,观察替换后标题中所显示的位置是否准确
7+
8+
9+
## 2025/12/18 查找替换增加显示当前查找替换位置的功能

src/Edit/Replace/edit_select.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,59 @@ edit_select_rep::get_alt_selection (string name) {
10211021
return alt_sels[name];
10221022
}
10231023

1024+
string
1025+
edit_select_rep::get_alt_selection_index (string name, string action) {
1026+
int n = N (alt_sels[name]);
1027+
int current_total= n / 2;
1028+
if (current_total == 0) {
1029+
index= 0;
1030+
total= 0;
1031+
return "";
1032+
}
1033+
if (action == "new" || total != current_total) {
1034+
total= current_total;
1035+
if (total == 0) return "";
1036+
index = 0;
1037+
const int BINARY_SEARCH_THRESHOLD= 20;
1038+
if (total <= BINARY_SEARCH_THRESHOLD) {
1039+
for (int i= 0; i + 1 < n; i+= 2) {
1040+
if (path_less_eq (alt_sels[name][i], tp)) {
1041+
index++;
1042+
}
1043+
else {
1044+
break;
1045+
}
1046+
}
1047+
}
1048+
else {
1049+
int left= 0, right= total;
1050+
while (left < right) {
1051+
int mid= (left + right) / 2;
1052+
if (path_less_eq (alt_sels[name][2 * mid], tp)) {
1053+
left= mid + 1;
1054+
}
1055+
else {
1056+
right= mid;
1057+
}
1058+
}
1059+
index= left;
1060+
}
1061+
}
1062+
else if (action == "next") {
1063+
index++;
1064+
}
1065+
else if (action == "previous") {
1066+
index--;
1067+
}
1068+
else if (action == "first") {
1069+
index= 1;
1070+
}
1071+
else if (action == "last") {
1072+
index= total;
1073+
}
1074+
return as_string (index) * "/" * as_string (total);
1075+
}
1076+
10241077
range_set
10251078
current_alt_selection (string name) {
10261079
return get_current_editor ()->get_alt_selection (name);

src/Edit/Replace/edit_select.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class edit_select_rep : virtual public editor_rep {
2828
path focus_p;
2929
bool focus_hold;
3030
hashmap<string, range_set> alt_sels;
31+
int total= 0;
32+
int index= 0;
3133

3234
protected:
3335
void get_selection (path& start, path& end);
@@ -106,6 +108,7 @@ class edit_select_rep : virtual public editor_rep {
106108

107109
void set_alt_selection (string s, range_set sel);
108110
range_set get_alt_selection (string s);
111+
string get_alt_selection_index (string s, string action);
109112
void cancel_alt_selection (string s);
110113
void cancel_alt_selections ();
111114
};

src/Edit/editor.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -550,10 +550,11 @@ class editor_rep : public simple_widget_rep {
550550
virtual void manual_focus_release () = 0;
551551
virtual path focus_get (bool skip_flag= true) = 0;
552552

553-
virtual void set_alt_selection (string s, range_set sel)= 0;
554-
virtual range_set get_alt_selection (string s) = 0;
555-
virtual void cancel_alt_selection (string s) = 0;
556-
virtual void cancel_alt_selections () = 0;
553+
virtual void set_alt_selection (string s, range_set sel) = 0;
554+
virtual range_set get_alt_selection (string s) = 0;
555+
virtual string get_alt_selection_index (string s, string action)= 0;
556+
virtual void cancel_alt_selection (string s) = 0;
557+
virtual void cancel_alt_selections () = 0;
557558

558559
/* public routines from edit_replace */
559560
virtual bool inside (string what) = 0;

src/Scheme/Glue/glue_editor.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,15 @@ function main()
15711571
"string"
15721572
}
15731573
},
1574+
{
1575+
scm_name = "get-alt-selection-index",
1576+
cpp_name = "get_alt_selection_index",
1577+
ret_type = "string",
1578+
arg_list = {
1579+
"string",
1580+
"string"
1581+
}
1582+
},
15741583
{
15751584
scm_name = "cancel-alt-selection",
15761585
cpp_name = "cancel_alt_selection",

0 commit comments

Comments
 (0)