基本信息:
- 操作系统:win10 22H2
- emacs version:GNU Emacs 31.0.50 (build 1, x86_64-w64-mingw32) of 2024-11-16
- emacs 配置: spaemacs develop
在李杀那找到如下代码:
(defun xah-convert-english-chinese-punctuation (&optional Begin End ToDirection)
"Convert punctuation from/to English/Chinese characters.
When called interactively, do current line or selection. The conversion direction is automatically determined.
If `universal-argument' is called, ask user for change direction.
When called in lisp code, Begin End are region begin/end positions. ToDirection must be any of the following string values:
chinese
english
auto
URL `http://xahlee.info/emacs/emacs/elisp_convert_chinese_punctuation.html'
Version: 2012-12-10 2022-05-18 2023-08-26 2023-11-25"
(interactive)
(let (xp1 xp2 xinputStr xcontainChinese
(xengToChinesePairs
[
[". " "。"]
[".\n" "。\n"]
[", " ","]
[",\n" ",\n"]
[": " ":"]
["; " ";"]
["? " "?"] ; no space after
["! " "!"]
["& " "&"]
[" (" "("]
[") " ")"]
;; for inside HTML
[".</" "。</"]
["?</" "?</"]
[":</" ":</"]
[" " " "]
]
))
(if (and Begin End)
(setq xp1 Begin xp2 End)
(if (region-active-p)
(setq xp1 (region-beginning) xp2 (region-end))
(setq xp1 (line-beginning-position) xp2 (line-end-position))))
(setq xinputStr (buffer-substring-no-properties xp1 xp2))
(when (not ToDirection)
(setq ToDirection (if current-prefix-arg
(completing-read
"Change to: "
'("english" "chinese")
nil
t)
"auto"
)))
(setq xcontainChinese
(seq-some
(lambda (x) (string-match (aref x 1) xinputStr))
xengToChinesePairs))
(when (string-equal ToDirection "auto")
(setq
ToDirection
(if xcontainChinese
"english"
"chinese")))
(save-restriction
(narrow-to-region xp1 xp2)
(mapc
(lambda (xx)
(progn
(goto-char (point-min))
(while (search-forward (aref xx 0) nil t)
(replace-match (aref xx 1)))))
(cond
((string-equal ToDirection "chinese") xengToChinesePairs)
((string-equal ToDirection "english") (mapcar (lambda (x) (vector (elt x 1) (elt x 0))) xengToChinesePairs))
(t (user-error "Your 3rd argument 「%s」 isn't valid" ToDirection))))
(goto-char (point-max)))))
将上述代码放到 dotspacemacs/user-config ()
后,通过如下代码并定义快捷键,可快速将英文标点符号转换成中文标点符号。
(defun etc ()
(interactive)
(xah-convert-english-chinese-punctuation nil nil "chinese"))
(global-set-key (kbd "M-m o c") 'etc)
问题:
总觉得 (global-set-key (kbd "M-m o c") 'etc)
实现的不好,请求大佬优化下代码?