branch: externals/pyim commit c335aa4b92dcda3fcf8c63d1d68599e8091aac36 Author: Feng Shu <tuma...@163.com> Commit: Feng Shu <tuma...@163.com>
Simplify pyim-time-limit-while. * pyim-candidates.el (pyim-candidates-search-buffer): simplify. * pyim-common.el (pyim-time-limit-while): simplify. * tests/pyim-tests.el (pyim-tests-pyim-time-limit-while): New test. --- pyim-candidates.el | 15 ++++++--------- pyim-common.el | 22 +++++++++++----------- tests/pyim-tests.el | 7 +++++++ 3 files changed, 24 insertions(+), 20 deletions(-) diff --git a/pyim-candidates.el b/pyim-candidates.el index afe61de..b30b928 100644 --- a/pyim-candidates.el +++ b/pyim-candidates.el @@ -123,17 +123,14 @@ IMOBJS 获得候选词条。" (defun pyim-candidates-search-buffer (regexp) "在当前 buffer 中使用 REGEXP 搜索词条。" (save-excursion - (let ((start (current-time)) - words) + (let (words) (goto-char (point-min)) - ;; Search after pos. (pyim-time-limit-while (and (not (input-pending-p)) - (re-search-forward regexp nil t)) - start 0.1 25 - (let ((match (match-string-no-properties 0))) - ;; NOTE: 单个汉字我觉得不值得收集。 - (when (>= (length match) 2) - (cl-pushnew match words :test #'equal)))) + (re-search-forward regexp nil t)) 0.1 + (let ((match (match-string-no-properties 0))) + ;; NOTE: 单个汉字我觉得不值得收集。 + (when (>= (length match) 2) + (cl-pushnew match words :test #'equal)))) words))) (defun pyim-candidates-create-quanpin (imobjs scheme-name &optional fast-search) diff --git a/pyim-common.el b/pyim-common.el index 0173c12..80417bf 100644 --- a/pyim-common.el +++ b/pyim-common.el @@ -179,17 +179,17 @@ When CARE-FIRST-ONE is no-nil, ((a b c) (d e)) => (a d)." unread-command-events)))) ;; Fork from `company-dabbrev--time-limit-while' in company-mode." -(defmacro pyim-time-limit-while (test start limit freq &rest body) - (declare (indent 3) (debug t)) - `(let ((pyim-time-limit-while-counter 0)) - (catch 'done - (while ,test - ,@body - (and ,limit - (= (cl-incf pyim-time-limit-while-counter) ,freq) - (setq pyim-time-limit-while-counter 0) - (> (float-time (time-since ,start)) ,limit) - (throw 'done 'pyim-time-out)))))) +(defmacro pyim-time-limit-while (test limit &rest body) + "If TEST non-nil and time consumption < LIMIT, repeat eval BODY." + (declare (indent 2) (debug t)) + (let ((start (make-symbol "start"))) + `(let ((,start (current-time))) + (catch 'done + (while ,test + ,@body + (and ,limit + (> (float-time (time-since ,start)) ,limit) + (throw 'done 'pyim-time-out))))))) ;; * Footer (provide 'pyim-common) diff --git a/tests/pyim-tests.el b/tests/pyim-tests.el index ca85165..0593138 100644 --- a/tests/pyim-tests.el +++ b/tests/pyim-tests.el @@ -93,6 +93,13 @@ '("a-b-c-d" "a-b-c" "a-b"))) (should (equal (pyim-subconcat nil) nil))) +(ert-deftest pyim-tests-pyim-time-limit-while () + (let ((time (current-time)) + (limit 0.1)) + (pyim-time-limit-while t limit + t) + (should (< (float-time (time-since time)) (* limit 1.5))))) + ;; ** pyim-pymap 相关单元测试 (ert-deftest pyim-tests-pyim-pymap () (should-not (cl-find-if-not