Skip to content

Commit

Permalink
feature 中英文输入时的空格
Browse files Browse the repository at this point in the history
  • Loading branch information
chens committed May 24, 2024
1 parent d580555 commit 20a2053
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lisp/init-editing-utils.el
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,46 @@ ORIG is the advised function, which is called with its ARGS."
(remove-it (cdr list)))))


(defun add-space-between-chinese-and-english ()
"在中英文之间自动添加空格。"
(let ((current-char (char-before))
(prev-char (char-before (1- (point)))))
(when (and current-char prev-char
(or (and (is-chinese-character prev-char) (is-halfwidth-character current-char))
(and (is-halfwidth-character prev-char) (is-chinese-character current-char)))
(not (eq prev-char ?\s))) ; 检查前一个字符不是空格
(save-excursion
(goto-char (1- (point)))
(insert " ")))))

(defun is-chinese-character (char)
"判断字符是否为中文字符。"
(and char (or (and (>= char #x4e00) (<= char #x9fff))
(and (>= char #x3400) (<= char #x4dbf))
(and (>= char #x20000) (<= char #x2a6df))
(and (>= char #x2a700) (<= char #x2b73f))
(and (>= char #x2b740) (<= char #x2b81f))
(and (>= char #x2b820) (<= char #x2ceaf)))))

(defun is-halfwidth-character (char)
"判断字符是否为半角字符,包括英文字母、数字和标点符号。"
(and char (or (and (>= char ?a) (<= char ?z))
(and (>= char ?A) (<= char ?Z))
(and (>= char ?0) (<= char ?9))
)))

(defun delayed-add-space-between-chinese-and-english ()
"延迟执行,在中英文之间自动添加空格。"
(run-with-idle-timer 0 nil 'add-space-between-chinese-and-english))

(define-minor-mode auto-space-mode
"在中英文之间自动添加空格的模式。"
:lighter " Auto-Space"
:global t
(if auto-space-mode
(add-hook 'post-self-insert-hook 'add-space-between-chinese-and-english)
(remove-hook 'post-self-insert-hook 'add-space-between-chinese-and-english)))


(use-package restart-emacs)

Expand Down

0 comments on commit 20a2053

Please sign in to comment.