From 7935ceff038e7001e5f4ac9ca58e042dcf4716cc Mon Sep 17 00:00:00 2001 From: Yuanle Song Date: Thu, 4 Apr 2019 11:41:44 +0800 Subject: [PATCH] code refactoring. make zero-handle-preedit-char-func easier to read and eaiser to write. --- zero-framework.el | 50 +++++++++++++++++++++++++---------------------- zero-pinyin.el | 33 ++++++++++++------------------- 2 files changed, 39 insertions(+), 44 deletions(-) diff --git a/zero-framework.el b/zero-framework.el index 7d0b29b..cfc98bf 100644 --- a/zero-framework.el +++ b/zero-framework.el @@ -97,7 +97,6 @@ if item is not in lst, return nil" ;;; corresponding *-func variable. (defun zero-build-candidates-default (preedit-str) nil) (defun zero-can-start-sequence-default (ch) nil) -(defun zero-handle-preedit-char-default (ch) nil) (defun zero-get-preedit-str-for-panel-default () zero-preedit-str) (defvar zero-build-candidates-func 'zero-build-candidates-default "contains a function to build candidates from preedit-str") @@ -111,6 +110,8 @@ This allow input method to override default logic.") "contains a function that return preedit-str to show in zero-panel") (defvar zero-backspace-func 'zero-backspace-default "contains a function to handle char") +(defvar zero-handle-preedit-char-func 'zero-handle-preedit-char-default + "hanlde character insert in `*zero-state-im-preediting*' mode") (defvar zero-im nil "current input method. if nil, the empty input method will be used. @@ -274,6 +275,30 @@ return ch's Chinese punctuation if ch is converted. return nil otherwise" (setq zero-current-page (1+ zero-current-page)) (zero-show-candidates)))) +(defun zero-handle-preedit-char-default (ch) + "hanlde character insert in `*zero-state-im-preediting*' mode" + (cond + ((= ch ?\s) + (zero-commit-first-candidate-or-preedit-str)) + ((and (>= ch ?0) (<= ch ?9)) + ;; 1 commit the 0th candidate + ;; 2 commit the 1st candidate + ;; ... + ;; 0 commit the 9th candidate + (unless (zero-commit-nth-candidate (mod (- (- ch ?0) 1) 10)) + (zero-append-char-to-preedit-str ch))) + ((= ch zero-previous-page-key) + (zero-page-up)) + ((= ch zero-next-page-key) + (zero-page-down)) + (t (let ((str (zero-convert-punctuation ch))) + (if str + (progn + (zero-set-state *zero-state-im-waiting-input*) + (zero-commit-first-candidate-or-preedit-str) + (insert str)) + (zero-append-char-to-preedit-str ch)))))) + (defun zero-self-insert-command (n) "handle character self-insert-command. This includes characters and digits" (interactive "p") @@ -291,28 +316,7 @@ return ch's Chinese punctuation if ch is converted. return nil otherwise" (self-insert-command n)))) ((eq zero-state *zero-state-im-preediting*) (zero-debug "still preediting\n") - (unless (funcall zero-handle-preedit-char-func ch) - (cond - ((= ch ?\s) - (zero-commit-first-candidate-or-preedit-str)) - ((and (>= ch ?0) (<= ch ?9)) - ;; 1 commit the 0th candidate - ;; 2 commit the 1st candidate - ;; ... - ;; 0 commit the 9th candidate - (unless (zero-commit-nth-candidate (mod (- (- ch ?0) 1) 10)) - (zero-append-char-to-preedit-str ch))) - ((= ch zero-previous-page-key) - (zero-page-up)) - ((= ch zero-next-page-key) - (zero-page-down)) - (t (let ((str (zero-convert-punctuation ch))) - (if str - (progn - (zero-set-state *zero-state-im-waiting-input*) - (zero-commit-first-candidate-or-preedit-str) - (insert str)) - (zero-append-char-to-preedit-str ch))))))) + (funcall zero-handle-preedit-char-func ch)) (t (zero-debug "unexpected state: %s\n" zero-state) (self-insert-command n))))) diff --git a/zero-pinyin.el b/zero-pinyin.el index 0302c55..727d506 100644 --- a/zero-pinyin.el +++ b/zero-pinyin.el @@ -127,37 +127,28 @@ otherwise, just return nil" (t (error "unexpected zero-pinyin-state: %s" zero-pinyin-state)))))) (defun zero-pinyin-handle-preedit-char (ch) - "handle IM-PREEDITING state char insert. return t if char is handled." + "handle IM-PREEDITING state char insert. overrides `zero-handle-preedit-char-default'" (cond - ((or (= ch zero-previous-page-key) - (= ch zero-next-page-key)) - nil) ((= ch ?\s) - (zero-pinyin-commit-first-candidate-or-preedit-str) - t) + (zero-pinyin-commit-first-candidate-or-preedit-str)) ((and (>= ch ?0) (<= ch ?9)) ;; 1 commit the 0th candidate ;; 2 commit the 1st candidate ;; ... ;; 0 commit the 9th candidate - (if (zero-pinyin-commit-nth-candidate (mod (- (- ch ?0) 1) 10)) - t - (progn - (zero-append-char-to-preedit-str ch) - (setq zero-pinyin-state nil) - t))) + (unless (zero-pinyin-commit-nth-candidate (mod (- (- ch ?0) 1) 10)) + (zero-append-char-to-preedit-str ch) + (setq zero-pinyin-state nil))) + ((or (= ch zero-previous-page-key) + (= ch zero-next-page-key)) + (zero-handle-preedit-char-default ch)) (t (let ((str (zero-convert-punctuation ch))) (if str - (if (zero-pinyin-commit-first-candidate-in-full) - (progn - (zero-set-state *zero-state-im-waiting-input*) - (insert str) - t) - ;; can't commit in full, punctuation key is no-op - t) + (when (zero-pinyin-commit-first-candidate-in-full) + (zero-set-state *zero-state-im-waiting-input*) + (insert str)) (setq zero-pinyin-state nil) - (zero-append-char-to-preedit-str ch) - t))))) + (zero-append-char-to-preedit-str ch)))))) (defun zero-pinyin-get-preedit-str-for-panel () (if (eq zero-pinyin-state *zero-pinyin-state-im-partial-commit*) -- GitLab