Commit 7935ceff authored by Yuanle Song's avatar Yuanle Song
Browse files

code refactoring. make zero-handle-preedit-char-func

easier to read and eaiser to write.
parent 5f6fd432
Loading
Loading
Loading
Loading
+27 −23
Original line number Diff line number Diff line
@@ -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 <backward> 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,24 +275,8 @@ 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-self-insert-command (n)
  "handle character self-insert-command. This includes characters and digits"
  (interactive "p")
  (let ((ch (elt (this-command-keys-vector) 0)))
    (zero-debug "user typed: %c\n" ch)
    (cond
     ((eq zero-state *zero-state-im-waiting-input*)
      (if (zero-can-start-sequence ch)
	  (progn
	    (zero-debug "can start sequence, state=IM_PREEDITING\n")
	    (zero-set-state *zero-state-im-preediting*)
	    (zero-append-char-to-preedit-str ch))
	(zero-debug "cannot start sequence, state=IM_WAITING_INPUT\n")
	(unless (zero-handle-punctuation ch)
	  (self-insert-command n))))
     ((eq zero-state *zero-state-im-preediting*)
      (zero-debug "still preediting\n")
      (unless (funcall zero-handle-preedit-char-func ch)
(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))
@@ -312,7 +297,26 @@ return ch's Chinese punctuation if ch is converted. return nil otherwise"
	      (zero-set-state *zero-state-im-waiting-input*)
	      (zero-commit-first-candidate-or-preedit-str)
	      (insert str))
		(zero-append-char-to-preedit-str ch)))))))
	  (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")
  (let ((ch (elt (this-command-keys-vector) 0)))
    (zero-debug "user typed: %c\n" ch)
    (cond
     ((eq zero-state *zero-state-im-waiting-input*)
      (if (zero-can-start-sequence ch)
	  (progn
	    (zero-debug "can start sequence, state=IM_PREEDITING\n")
	    (zero-set-state *zero-state-im-preediting*)
	    (zero-append-char-to-preedit-str ch))
	(zero-debug "cannot start sequence, state=IM_WAITING_INPUT\n")
	(unless (zero-handle-punctuation ch)
	  (self-insert-command n))))
     ((eq zero-state *zero-state-im-preediting*)
      (zero-debug "still preediting\n")
      (funcall zero-handle-preedit-char-func ch))
     (t
      (zero-debug "unexpected state: %s\n" zero-state)
      (self-insert-command n)))))
+12 −21
Original line number Diff line number Diff line
@@ -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
    (unless (zero-pinyin-commit-nth-candidate (mod (- (- ch ?0) 1) 10))
      (zero-append-char-to-preedit-str ch)
	(setq zero-pinyin-state nil)
	t)))
      (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
	    (when (zero-pinyin-commit-first-candidate-in-full)
	      (zero-set-state *zero-state-im-waiting-input*)
		  (insert str)
		  t)
	      ;; can't commit in full, punctuation key is no-op
	      t)
	      (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*)