(defun add-space-between-chinese-and-english ()
"Automatically add a space between Chinese and English characters."
(let ((current-char (char-before))
(prev-char (char-before (1- (point))))
(next-char (char-after)))
(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))) ; Check if the previous character is a space
(save-excursion
(goto-char (1- (point)))
(insert " ")))
(when (and current-char next-char
(or (and (is-chinese-character current-char) (is-halfwidth-character next-char))
(and (is-halfwidth-character current-char) (is-chinese-character next-char)))
(not (eq current-char ?\s))) ; Check if the current character is a space
(save-excursion
(goto-char (point))
(insert " ")))))
(defun is-halfwidth-character (char)
"Determine if a character is a halfwidth character using char-width."
(and char (not (eq char ?\s)) (= (char-width char) 1)))
2024-05-31 对于一些英文字符不希望插入空格,可以在里面添加
(defun is-halfwidth-character (char)
"Determine if a character is a halfwidth character using char-width."
(and char (not (member char '(?\s ?~ ?“ ?”))) (= (char-width char) 1)))