Skip to content

Commit ab78f9c

Browse files
authored
[1199] 使用 utf8-kbd-map 支持 ¥、等非 ASCII 键 (#4322)
1 parent 3c25cc9 commit ab78f9c

5 files changed

Lines changed: 163 additions & 0 deletions

File tree

TeXmacs/progs/generic/generic-kbd.scm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@
229229
("std j" (toggle-chat-sidebar))
230230
) ;kbd-map
231231

232+
(utf8-kbd-map ("" (if (or (inside? 'hybrid) (in-prog?)) (insert "<#3001>") (make-hybrid)))
233+
("、 var" "<#3001>")
234+
("" (make 'math))
235+
("¥ var" "<#FFE5>")
236+
) ;utf8-kbd-map
237+
232238
(kbd-map (:require (list-structured-insert-context?))
233239
("structured:insert delete" (structured-remove-down))
234240
("structured:insert backspace" (structured-remove-up))

TeXmacs/progs/kernel/gui/kbd-define.scm

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,11 +512,64 @@
512512
`(begin ,@(kbd-map-body '() l))
513513
) ;tm-define-macro
514514

515+
(tm-define (utf8-kbd-binding conds key2 cmd help)
516+
(:synopsis "Helper routine for utf8-kbd-map macro")
517+
(with key
518+
(kbd-pre-rewrite (string-convert key2 "UTF-8" "Cork"))
519+
(kbd-sub-bindings conds key)
520+
(kbd-insert-key-binding conds key (list cmd help))
521+
) ;with
522+
) ;tm-define
523+
524+
(define (utf8-kbd-map-one conds l)
525+
(if (not (and (pair? l) (string? (car l)) (pair? (cdr l))))
526+
(texmacs-error "utf8-kbd-map-pre-one" "Bad keymap in: ~S" l)
527+
) ;if
528+
(with (key action . opt)
529+
l
530+
(if (string? action)
531+
(with help
532+
(if (null? opt) "" (car opt))
533+
`(utf8-kbd-binding (list ,@conds) ,key ,action ,help)
534+
) ;with
535+
`(utf8-kbd-binding (list ,@conds) ,key (lambda ,() ,action ,@opt) ,"")
536+
) ;if
537+
) ;with
538+
) ;define
539+
540+
(define (utf8-kbd-map-body conds l)
541+
(cond ((null? l) '())
542+
((symbol? (car l)) (utf8-kbd-map-body (list 0 (car l)) (cdr l)))
543+
((and (pair? (car l)) (== (caar l) :profile))
544+
(if (not (has-look-and-feel? (cdar l)))
545+
'((noop))
546+
(utf8-kbd-map-body conds (cdr l))
547+
) ;if
548+
) ;
549+
((and (pair? (car l)) (keyword? (caar l)))
550+
(utf8-kbd-map-body (kbd-add-condition conds (car l)) (cdr l))
551+
) ;
552+
(else (map (lambda (x) (utf8-kbd-map-one conds x)) l))
553+
) ;cond
554+
) ;define
555+
556+
(tm-define-macro (utf8-kbd-map . l)
557+
(:synopsis "Add entries with UTF-8 key names to the keyboard mapping")
558+
`(begin ,@(utf8-kbd-map-body '() l))
559+
) ;tm-define-macro
560+
515561
(tm-define-macro (delayed-kbd-map . l)
516562
(:synopsis "Like kbd-map, but register bindings in chunks via the event loop")
517563
`(kbd-enqueue (list ,@(map (lambda (x) `(lambda ,() ,x)) (kbd-map-body '() l))))
518564
) ;tm-define-macro
519565

566+
(tm-define-macro (utf8-delayed-kbd-map . l)
567+
(:synopsis "Like utf8-kbd-map, but register bindings in chunks via the event loop"
568+
) ;:synopsis
569+
`(kbd-enqueue (list ,@(map (lambda (x) `(lambda ,() ,x))
570+
(utf8-kbd-map-body '() l))))
571+
) ;tm-define-macro
572+
520573
(define (kbd-remove-one conds key)
521574
`(kbd-delete-key-binding2 (list ,@conds) ,key)
522575
) ;define

TeXmacs/progs/math/math-kbd.scm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,8 @@
12291229
("~ = /" "<ncong>")
12301230
) ;delayed-kbd-map
12311231

1232+
(utf8-delayed-kbd-map (:mode in-math?) ("" (math-make-math)))
1233+
12321234
(delayed-kbd-map (:mode in-math-or-hybrid?)
12331235
("/ \\" "<wedge>")
12341236
("/ \\ var " "<cap>")

TeXmacs/tests/1199.scm

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2+
;;
3+
;; MODULE : 1199.scm
4+
;; DESCRIPTION : 回归测试:UTF-8 键名通过 utf8-kbd-map 注册,不改变旧 kbd-map。
5+
;;
6+
;; This software falls under the GNU general public license version 3 or later.
7+
;; It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
8+
;; in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
9+
;;
10+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11+
12+
(texmacs-module (texmacs tests 1199))
13+
14+
(import (liii check))
15+
(check-set-mode! 'report-failed)
16+
17+
(define (test-1199-encoding)
18+
(check (string-convert "" "UTF-8" "Cork") => "<#FFE5>")
19+
(check (string-convert "" "UTF-8" "Cork") => "<#3001>")
20+
) ;define
21+
22+
(define (test-1199-bindings)
23+
(kbd-flush-pending)
24+
(check-true (pair? (kbd-find-key-binding "<#FFE5>")))
25+
(check-true (pair? (kbd-find-key-binding "<#3001>")))
26+
(check-true (pair? (kbd-find-key-binding "<#FFE5> tab")))
27+
(check-true (pair? (kbd-find-key-binding "<#3001> tab")))
28+
) ;define
29+
30+
(define step-delay-ms 300)
31+
32+
(define (run-chain steps)
33+
(let loop
34+
((rest steps) (t (+ (texmacs-time) step-delay-ms)))
35+
(when (pair? rest)
36+
(let ((act (cdar rest)))
37+
(exec-delayed-at (lambda () (act) (loop (cdr rest) (+ (texmacs-time) step-delay-ms)))
38+
t
39+
) ;exec-delayed-at
40+
) ;let
41+
) ;when
42+
) ;let
43+
) ;define
44+
45+
(tm-define (test_1199)
46+
(test-1199-encoding)
47+
(test-1199-bindings)
48+
(run-chain (list (cons "new document"
49+
(lambda () (new-document) (check (get-env "mode") => "text"))
50+
) ;cons
51+
(cons "¥ enters math"
52+
(lambda () (keyboard-press "<#FFE5>" 0) (check (get-env "mode") => "math"))
53+
) ;cons
54+
(cons "¥ leaves math"
55+
(lambda () (keyboard-press "<#FFE5>" 0) (check (get-env "mode") => "text"))
56+
) ;cons
57+
(cons "、 enters hybrid"
58+
(lambda () (keyboard-press "<#3001>" 0) (check-true (inside? 'hybrid)))
59+
) ;cons
60+
(cons "report + quit" (lambda () (check-report) (quit-TeXmacs)))
61+
) ;list
62+
) ;run-chain
63+
) ;tm-define

devel/1199.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# [1199] kbd-map 支持 UTF-8 键名
2+
3+
## What
4+
5+
新增 `utf8-kbd-map`,支持中文输入法提交的 ```` 键名。
6+
7+
## Why
8+
9+
保留旧 `kbd-map` 的注册逻辑,避免全局 UTF-8 到 Cork 转换影响已有快捷键。
10+
11+
## How
12+
13+
UTF-8 入口在注册键名时转换为 Cork;运行时查找仍使用已有 Cork 键。````分别复用 `$``\\` 的编辑命令。
14+
15+
## 测试
16+
17+
```powershell
18+
xmake b -y stem
19+
xmake install -y stem
20+
xmake r 1199
21+
```
22+
23+
已验证:`xmake b -y stem``xmake install -y stem``xmake r 1199` 均通过。
24+
headless 回归覆盖 UTF-8 到 Cork 的转换、``/``及其 Tab 变体的注册可达性;
25+
实际输入法和编辑状态行为需手动验证。
26+
27+
## 手动验证
28+
29+
1. 中文输入法激活,文本模式下提交 ``,确认进入数学模式;再次提交,确认回到文本模式。
30+
2. 中文输入法激活,文本模式下提交 ``,确认进入 hybrid;紧接 Tab,确认插入 `` 本身。
31+
3. 中文输入法激活,提交普通中文候选词,确认预编辑与候选提交不受影响。
32+
4. 英文输入法下 `$``\\` 及其既有 Tab 变体保持原行为。
33+
34+
## 涉及文件
35+
36+
- `TeXmacs/progs/kernel/gui/kbd-define.scm`
37+
- `TeXmacs/progs/generic/generic-kbd.scm`
38+
- `TeXmacs/progs/math/math-kbd.scm`
39+
- `TeXmacs/tests/1199.scm`

0 commit comments

Comments
 (0)