Skip to content

Commit 3e115e6

Browse files
committed
!473 [201_11] 数学环境 Tab Cycling 补全项获取
* doc on test * doc * 数学环境 Tab Cycling 补全项获取
1 parent 7eb8bc5 commit 3e115e6

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

TeXmacs/progs/math/math-edit.scm

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,3 +756,79 @@
756756
(set-message "Only implemented for complete subtrees"
757757
"correct formula"))
758758
(math-manually-correct-tree (buffer-tree))))
759+
760+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
761+
;; Tab cycling completion
762+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
763+
764+
(define (tabcycle-symbols comb)
765+
;; 根据按键序列获取数学符号Tab循环展示的列表
766+
;; 输入:
767+
;; comb: 按键序列,类型为string,如"b tab"
768+
;; 处理过程:
769+
;; 1. 裁剪comb获得最基本的按键序列,例如“space b tab” -> "b"
770+
;; 2. 查找该按键序列对应的符号绑定
771+
;; 3. 如果绑定不存在,返回空列表
772+
;; 4. 如果绑定存在,继续查找所有以该按键序列为前缀的符号绑定
773+
;; 5. 返回所有找到的符号绑定列表,例如((symbol "<alpha>"))
774+
;; 输出:
775+
;; 符号列表,格式为((symbol "符号") ...)
776+
(if (not (kbd-find-key-binding comb)) '()
777+
(let* ((pre (string-replace (string-replace comb " tab" "") "space " ""))
778+
(kbd-res (kbd-find-key-binding pre))
779+
(kbd-sym (and (pair? kbd-res) (car kbd-res))))
780+
(append
781+
(if kbd-sym `((symbol ,kbd-sym)) '())
782+
(reverse
783+
(map (lambda (x) `(symbol ,(and (pair? x) (pair? (cdr x)) (cadadr x))))
784+
(list-sort
785+
(filter
786+
(lambda (x)
787+
(and (pair? x)
788+
(string? (car x))
789+
(string-ends? (car x) "tab")
790+
(string=? (string-replace (car x) " tab" "") pre)))
791+
(kbd-find-prefix pre))
792+
(lambda (x y)
793+
(and (pair? x) (pair? y)
794+
(string>? (car x) (car y)))))))))))
795+
796+
(define (highlight-tabcycle-symbols lst comb)
797+
;; 高亮显示Tab循环符号列表中符合按键序列的符号
798+
;; 输入:
799+
;; lst: 符号列表,格式为((symbol "符号") ...)
800+
;; comb: 按键序列,类型为string,如"b tab"
801+
;; 输出:
802+
;; 符号列表,格式为((symbol "符号") (symbol* "符号") ...),
803+
;; 其中 symbol* 表示高亮显示的符号,参见 make-menu-symbol 中的定义
804+
(let ((bind (let ((res (kbd-find-key-binding comb)))
805+
(and res (car res)))))
806+
(if (string? bind)
807+
(map (lambda (x) (if (and bind
808+
(pair? x)
809+
(eq? (car x) 'symbol)
810+
(string=? (cadr x) bind))
811+
(list 'symbol* bind) x))
812+
lst)
813+
lst)))
814+
815+
(tm-define (math-tabcycle-symbols comb)
816+
;; 根据按键序列获取数学符号Tab循环展示的列表
817+
;; 输入:
818+
;; comb: 按键序列,类型为string,如"b tab"
819+
;; 输出:
820+
;; 符号列表,格式为((symbol "符号") (symbol* "符号") ...)
821+
(if (in-math?)
822+
(highlight-tabcycle-symbols (tabcycle-symbols comb) comb)
823+
'()))
824+
825+
(tm-define (math-variant comb)
826+
;; 触发数学符号Tab循环的展示
827+
;; 输入:
828+
;; comb: 按键序列,类型为string,如"b tab"
829+
(if (in-math?)
830+
(set-auxiliary-widget
831+
(make-menu-widget
832+
`((tile 99 (link (lambda () (math-tabcycle-symbols ,comb))))) 0)
833+
"Math"))
834+
(noop))

TeXmacs/tests/201_11.scm

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
33
;;
44
;; MODULE : 201_11.scm
5-
;; DESCRIPTION : Tests for kbd-find-prefix
5+
;; DESCRIPTION : Tests for tab-cycling completion
66
;; COPYRIGHT : (C) 2025 JimZhouZZY
77
;;
88
;; This software falls under the GNU general public license version 3 or later.
@@ -39,6 +39,23 @@
3939
("e tab tab tab" ((#<lambda ()>) "<epsilon>" ""))
4040
("e tab tab tab tab" ((#<lambda ()>) "<backepsilon>" ""))))))
4141

42+
(define (test-math-tabcycle-symbols)
43+
(make 'math)
44+
(kbd-insert "now we are in math mode")
45+
;; 注意 symbol* 高亮的位置也是随着 tab 的次数变化的
46+
(check (math-tabcycle-symbols "a") => '((symbol* "a") (symbol "<alpha>")))
47+
(check (math-tabcycle-symbols "a tab")
48+
=> '((symbol "a") (symbol* "<alpha>")))
49+
(check (math-tabcycle-symbols "a tab tab") => '())
50+
(check (math-tabcycle-symbols "= >")
51+
=> '((symbol* "<Rightarrow>") (symbol "<Downarrow>") (symbol "<Uparrow>") (symbol "<Rrightarrow>")))
52+
(check (math-tabcycle-symbols "= > tab")
53+
=> '((symbol "<Rightarrow>") (symbol* "<Downarrow>") (symbol "<Uparrow>") (symbol "<Rrightarrow>")))
54+
(check (math-tabcycle-symbols "= > tab tab")
55+
=> '((symbol "<Rightarrow>") (symbol "<Downarrow>") (symbol* "<Uparrow>") (symbol "<Rrightarrow>")))
56+
)
57+
4258
(tm-define (test_201_11)
4359
(test-kbd-find-prefix)
60+
(test-math-tabcycle-symbols)
4461
(check-report))

devel/201_11.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@
1717
```
1818
4. 检查:右侧辅助窗口中正常出现数学符号的按钮
1919

20+
### 测试数学环境 Tab Cycling 补全项获取
21+
注:这部分也有自动测试,在`201_11.scm`
22+
1. 插入一个 Scheme 块
23+
2. run
24+
```
25+
((lambda () (make 'math) (insert "in math") (math-variant "b") (show-auxiliary-widget #t))))))
26+
```
27+
3. 检查结果
28+
29+
## 2025/07/31 数学环境 Tab Cycling 补全项获取
30+
### What
31+
-`math-edit.scm`中添加了函数`math-tabcycle-symbols`用于在数学环境下获取 TabCycling 补全项
32+
-`math-edit.scm`中添加了函数`math-variant`用于展示 TabCycling 补全项
33+
34+
目前TabCycling补全项由辅助窗口展示。将来会改成新的UI组件。
35+
2036
## 2025/07/29 支持转化box_widget内容为qwidget
2137
### What
2238
修改了as_qwidget中case menu_button情况下的逻辑,在没有精确匹配到type=xpm_widget或者text_widget的时候默认包装此widget的as_qaction为一个QToolButton作为返回的qwidget。

0 commit comments

Comments
 (0)