Commit 3d0d8c13 authored by Yuanle Song's avatar Yuanle Song
Browse files

zero-pinyin: support C-<digit> to DeleteCandidate

- zero-framework.el add support for :shutdown function
- bugfix: zero-backspace-original-func check for nil before setq
- update zero-pinyin-service client, service is renamed
parent e51528b5
Loading
Loading
Loading
Loading
+11 −4
Original line number Diff line number Diff line
@@ -479,7 +479,9 @@ return ch's Chinese punctuation if ch is converted. return nil otherwise"
  (make-local-variable 'zero-backspace-original-func)
  (unless zero-backspace-original-func
    (let ((func (key-binding (kbd "DEL"))))
      (unless (function-equal func 'zero-backspace)
      (when (and (functionp func)
		 (not (function-equal func 'zero-backspace)))
	(zero-debug "set zero-backspace-original-func to %s" func)
	(setq zero-backspace-original-func func))))
  ;; DEL can't be put in zero-mode-map because I need to save the original
  ;; binding in this function.
@@ -557,11 +559,16 @@ registered input method is saved in `zero-ims'"
if im-name is nil, use default empty input method"
  ;; TODO provide auto completion for im-name
  (interactive "SSet input method to: ")
  ;; TODO create a macro to reduce code duplication and human error.
  ;; when switch away from an IM, run last IM's :shutdown function.
  (if zero-im
      (let ((shutdown-func (cdr (assq :shutdown (cdr (assq zero-im zero-ims))))))
	(if (functionp shutdown-func)
	    (funcall shutdown-func))))
  (if im-name
      (let ((im-functions (cdr (assq im-name zero-ims))))
	(if im-functions
	    (progn
	      ;; TODO create a macro to reduce code duplication and human error.
	      (setq zero-build-candidates-func
		    (or (cdr (assq :build-candidates im-functions))
			'zero-build-candidates-default))
@@ -580,6 +587,7 @@ if im-name is nil, use default empty input method"
	      (setq zero-backspace-func
		    (or (cdr (assq :handle-backspace im-functions))
			'zero-backspace-default))
	      ;; when switch to a IM, run its :init function
	      (let ((init-func (cdr (assq :init im-functions))))
		(if (functionp init-func)
		    (funcall init-func)))
@@ -591,8 +599,7 @@ if im-name is nil, use default empty input method"
    (setq zero-can-start-sequence-func 'zero-can-start-sequence-default)
    (setq zero-handle-preedit-char-func 'zero-handle-preedit-char-default)
    (setq zero-get-preedit-str-for-panel-func 'zero-get-preedit-str-for-panel-default)
    (setq zero-backspace-func 'zero-backspace-default)
    ))
    (setq zero-backspace-func 'zero-backspace-default)))

(defun zero-set-default-im (im-name)
  "set given im as default zero input method"
+13 −8
Original line number Diff line number Diff line
@@ -9,9 +9,9 @@

(defun zero-pinyin-service-error-handler (event error)
  "handle dbus errors"
  (when (or (string-equal "com.emacsos.zero.ZeroPinyinService"
  (when (or (string-equal "com.emacsos.zero.ZeroPinyinService1"
			  (dbus-event-interface-name event))
	    (s-contains-p "com.emacsos.zero.ZeroPinyinService" (cadr error)))
	    (s-contains-p "com.emacsos.zero.ZeroPinyinService1" (cadr error)))
    (error "zero-pinyin-service dbus failed: %S" (cadr error))))

(add-hook 'dbus-event-error-functions 'zero-pinyin-service-error-handler)
@@ -19,17 +19,17 @@
(defun zero-pinyin-service-async-call (method handler &rest args)
  "call Method on zero-pinin-service asynchronously. This is a wrapper around `dbus-call-method-asynchronously'"
  (apply 'dbus-call-method-asynchronously
	 :session "com.emacsos.zero.ZeroPinyinService"
	 "/com/emacsos/zero/ZeroPinyinService"
	 "com.emacsos.zero.ZeroPinyinService"
	 :session "com.emacsos.zero.ZeroPinyinService1"
	 "/com/emacsos/zero/ZeroPinyinService1"
	 "com.emacsos.zero.ZeroPinyinService1.ZeroPinyinServiceInterface"
	 method handler :timeout 1000 args))

(defun zero-pinyin-service-call (method &rest args)
  "call Method on zero-pinin-service synchronously. This is a wrapper around `dbus-call-method'"
  (apply 'dbus-call-method
	 :session "com.emacsos.zero.ZeroPinyinService"
	 "/com/emacsos/zero/ZeroPinyinService"
	 "com.emacsos.zero.ZeroPinyinService"
	 :session "com.emacsos.zero.ZeroPinyinService1"
	 "/com/emacsos/zero/ZeroPinyinService1"
	 "com.emacsos.zero.ZeroPinyinService1.ZeroPinyinServiceInterface"
	 method :timeout 1000 args))

;;============
@@ -51,6 +51,11 @@ fetch-size try to fetch this many candidates or more"
  (zero-pinyin-service-async-call
   "GetCandidates" get-candidates-complete :string preedit-str :uint32 fetch-size))

(defun zero-pinyin-service-delete-candidates-async (candidate delete-candidate-complete)
  "delete candidate asynchronously"
  (zero-pinyin-service-async-call
   "DeleteCandidate" delete-candidate-complete :string candidate))

(defun zero-pinyin-service-quit ()
  "quit panel application"
  (zero-pinyin-service-async-call "Quit" nil))
+38 −5
Original line number Diff line number Diff line
@@ -36,9 +36,15 @@
  (setq zero-pinyin-pending-preedit-str ""))

(defun zero-pinyin-init ()
  "called when this im is turned on"
  (define-key zero-mode-map [remap digit-argument] 'zero-digit-argument)
  (make-local-variable 'zero-pinyin-state)
  (zero-pinyin-reset))

(defun zero-pinyin-shutdown ()
  "called when this im is turned off"
  (define-key zero-mode-map [remap digit-argument] nil))

(defvar zero-pinyin--build-candidates-use-test-data nil
  "if t, `zero-pinyin-build-candidates' will use `zero-pinyin-build-candidates-test'")

@@ -198,15 +204,41 @@ This is different from zero-framework because I need to support partial commit"
      (concat zero-pinyin-pending-str zero-pinyin-pending-preedit-str)
    zero-preedit-str))

(defun zero-pinyin-preedit-str-changed ()
  "start over for candidate selection process."
  (setq zero-pinyin-state nil)
  (zero-preedit-str-changed))

(defun zero-pinyin-backspace ()
  "handle backspace key in `*zero-state-im-preediting*' state"
  (if (eq zero-pinyin-state *zero-pinyin-state-im-partial-commit*)
      (progn
	;; start over for candidate selection process.
	(setq zero-pinyin-state nil)
	(zero-preedit-str-changed))
      (zero-pinyin-preedit-str-changed)
    (zero-backspace-default)))

(defun zero-pinyin-delete-candidate (digit)
  "tell backend to delete nth candidate.

n is the digit selection number.
1 means delete 1st candidate.
2 means delete 2st candidate.
0 means delete 10th candidate."
  (let ((candidate (nth (mod (- digit 1) 10)
			(zero-candidates-on-page zero-candidates))))
    (when candidate
      (zero-pinyin-service-delete-candidates-async
       candidate 'zero-pinyin-preedit-str-changed))))

(defun zero-digit-argument (arg)
  "wrapper around `digit-argument' to allow C-<digit> to DeleteCandidate in `*zero-state-im-preediting*' state"
  (interactive "P")
  (if (eq zero-state *zero-state-im-preediting*)
      (let* ((char (if (integerp last-command-event)
		       last-command-event
		     (get last-command-event 'ascii-character)))
	     (digit (- (logand char ?\177) ?0)))
	(zero-pinyin-delete-candidate digit))
    (digit-argument arg)))

;;===============================
;; register IM to zero framework
;;===============================
@@ -220,7 +252,8 @@ This is different from zero-framework because I need to support partial commit"
   (:handle-preedit-char . zero-pinyin-handle-preedit-char)
   (:get-preedit-str-for-panel . zero-pinyin-get-preedit-str-for-panel)
   (:handle-backspace . zero-pinyin-backspace)
   (:init . zero-pinyin-init)))
   (:init . zero-pinyin-init)
   (:shutdown . zero-pinyin-shutdown)))

;;============
;; public API