diff --git a/zero-framework.el b/zero-framework.el index 6e2cf992a06931f5f6fd943942cfaf914f05c894..7b6e11bf6f7fc4a50780dbe11221ca4b62b35be8 100644 --- a/zero-framework.el +++ b/zero-framework.el @@ -186,7 +186,7 @@ if t, `zero-debug' will output debug msg in *zero-debug* buffer") (zero-panel-show-candidates (funcall zero-get-preedit-str-for-panel-func) (length candidates-on-page) candidates-on-page) - (zero-debug "candidates: %s\n " (s-join "\n " candidates-on-page)) + (zero-debug "candidates:\n %s\n " (s-join "\n " candidates-on-page)) (destructuring-bind (x y) (zero-get-point-position) (zero-panel-move x y)))) diff --git a/zero-pinyin-service.el b/zero-pinyin-service.el index ee63b8898c6680898bae90e5511bed7e8533268f..3f5eb27a0116c10d2af1cda50951a22dbdff0b04 100644 --- a/zero-pinyin-service.el +++ b/zero-pinyin-service.el @@ -36,17 +36,44 @@ ;; public API ;;============ -(defun zero-pinyin-service-get-candidates (preedit-str) - "get candidates for pinyin in preedit-str synchronously" - (zero-pinyin-service-call "GetCandidates" :string preedit-str)) +(defun zero-pinyin-service-get-candidates (preedit-str fetch-size) + "get candidates for pinyin in preedit-str synchronously. -(defun zero-pinyin-service-get-candidates-async (preedit-str get-candidates-complete) - "get candidates for pinyin in preedit-str asynchronously" +preedit-str the preedit-str, should be pure pinyin string +fetch-size try to fetch this many candidates or more" + (zero-pinyin-service-call "GetCandidates" :string preedit-str :uint32 fetch-size)) + +(defun zero-pinyin-service-get-candidates-async (preedit-str fetch-size get-candidates-complete) + "get candidates for pinyin in preedit-str asynchronously. + +preedit-str the preedit-str, should be pure pinyin string +fetch-size try to fetch this many candidates or more" (zero-pinyin-service-async-call - "GetCandidates" get-candidates-complete :string preedit-str)) + "GetCandidates" get-candidates-complete :string preedit-str :uint32 fetch-size)) -(defun zero-panel-quit () +(defun zero-pinyin-service-quit () "quit panel application" (zero-pinyin-service-async-call "Quit" nil)) -(provide 'zero-panel) +;;================ +;; some app test +;;================ + +(ert-deftest zero-pinyin-service-get-candidates () + (destructuring-bind (cs ls) (zero-pinyin-service-get-candidates "liyifeng" 1) + (should (equal (first cs) "李易峰")) + (should (= (first ls) 8))) + (destructuring-bind (cs ls) (zero-pinyin-service-get-candidates "wenti" 1) + (should (equal (first cs) "问题")) + (should (= (first ls) 5))) + (destructuring-bind (cs ls) (zero-pinyin-service-get-candidates "meiyou" 1) + (should (equal (first cs) "没有")) + (should (= (first ls) 6))) + (destructuring-bind (cs ls) (zero-pinyin-service-get-candidates "shi" 1) + (should (equal (first cs) "是")) + (should (= (first ls) 3))) + (destructuring-bind (cs ls) (zero-pinyin-service-get-candidates "de" 1) + (should (equal (first cs) "的")) + (should (= (first ls) 2)))) + +(provide 'zero-pinyin-service) diff --git a/zero-pinyin.el b/zero-pinyin.el index 7da9809a196d2a20e3e6359362368674b40193ea..7e3a3b0ee04af450917969290088dc9d683904ec 100644 --- a/zero-pinyin.el +++ b/zero-pinyin.el @@ -7,6 +7,7 @@ (require 's) (require 'zero-framework) +(require 'zero-pinyin-service) ;;=============================== ;; basic data and emacs facility @@ -19,6 +20,8 @@ "accompany `zero-candidates', marks how many preedit-str chars are used for each candidate") (defvar zero-pinyin-pending-str "") (defvar zero-pinyin-pending-preedit-str "") +(defvar zero-pinyin-initial-fetch-size 20 + "how many candidates to fetch for the first call to GetCandidates") ;;===================== ;; key logic functions @@ -40,7 +43,7 @@ (defun zero-pinyin-build-candidates (preedit-str) (if zero-pinyin--build-candidates-use-test-data (zero-pinyin-build-candidates-test preedit-str) - (let ((result (zero-pinyin-service-get-candidates preedit-str))) + (let ((result (zero-pinyin-service-get-candidates preedit-str zero-pinyin-initial-fetch-size))) (setq zero-pinyin-used-preedit-str-lengths (second result)) (first result)))) @@ -49,6 +52,7 @@ (zero-debug "building candidate list async\n") (zero-pinyin-service-get-candidates-async preedit-str + zero-pinyin-initial-fetch-size (lambda (candidates matched_preedit_str_lengths) (setq zero-pinyin-used-preedit-str-lengths matched_preedit_str_lengths) ;; Note: with dynamic binding, this command result in (void-variable @@ -73,7 +77,7 @@ (should-not (zero-pinyin-can-start-sequence ?v))) (defun zero-pinyin-pending-preedit-str-changed () - (zero-build-candidates-async zero-pinyin-pending-preedit-str)) + (zero-pinyin-build-candidates-async zero-pinyin-pending-preedit-str 'zero-build-candidates-complete)) (defun zero-pinyin-commit-nth-candidate (n) "commit nth candidate and return true if it exists, otherwise, return false"