Skip to content

Commit cf15bd8

Browse files
committed
enhance(el): replace 'looking-at' and 'looking-back' for better performance
1 parent e663036 commit cf15bd8

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

emt.el

+12-12
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@
6363
(defvar emt--root (file-name-directory (or load-file-name buffer-file-name))
6464
"The path to the root of the package.")
6565

66-
(defvar emt--cjk-char-regex-forward (format "\\W*\\(\\cc\\|\\cj\\|\\ch\\)")
67-
"Regex for CJK char.")
68-
69-
(defvar emt--cjk-char-regex-backward (format "\\(\\cc\\|\\cj\\|\\ch\\)\\W*")
66+
(defvar emt--cjk-char-regex (format "^\\W*\\(\\cc\\|\\cj\\|\\ch\\)+\\W*$")
7067
"Regex for CJK char.")
7168

7269
(defvar emt--lib-loaded nil
@@ -107,17 +104,20 @@
107104
If DIRECTION is `'forward', return the bounds of the string forward.
108105
If DIRECTION is `'backward', return the bounds of the string backward.
109106
If DIRECTION is `'all', return the bounds of the string forward and backward."
110-
(let ((beg (point))
111-
(end (point)))
107+
(let* ((pos (point))
108+
(beg pos)
109+
(end pos))
112110
(when (or (eq direction 'forward) (eq direction 'all))
113-
(when (looking-at emt--cjk-char-regex-forward)
114-
(save-excursion
115-
(forward-word)
111+
(save-excursion
112+
(forward-word)
113+
(when (string-match-p emt--cjk-char-regex
114+
(buffer-substring-no-properties pos (point)))
116115
(setq end (point)))))
117116
(when (or (eq direction 'backward) (eq direction 'all))
118-
(when (looking-back emt--cjk-char-regex-backward nil)
119-
(save-excursion
120-
(backward-word)
117+
(save-excursion
118+
(backward-word)
119+
(when (string-match-p emt--cjk-char-regex
120+
(buffer-substring-no-properties (point) pos))
121121
(setq beg (point)))))
122122
(cons beg end)))
123123

0 commit comments

Comments
 (0)