Commit 42ef83fb authored by Yuanle Song's avatar Yuanle Song
Browse files

fix zero-fetch-size; zero-panel add hints support.

- zero-framework.el and zero-pinyin.el when updating zero-fetch-size,
  should update it to

    (max (length candidates) fetch-size)

  because service can return more than asked and it still counts.

- zero-panel.el add hints support and use it for pass has_next_page,
  has_previous_page indicator.
parent 029a5600
Loading
Loading
Loading
Loading
+10 −3
Original line number Diff line number Diff line
@@ -191,7 +191,13 @@ if t, `zero-debug' will output debug msg in *zero-debug* buffer")
							 zero-candidates))))
    (zero-panel-show-candidates
     (funcall zero-get-preedit-str-for-panel-func)
     (length candidates-on-page) candidates-on-page)
     (length candidates-on-page)
     candidates-on-page
     `(("in_emacs" t)
       ("filename" ,(or (buffer-file-name) ""))
       ("page_number" ,zero-current-page)
       ("has_next_page" ,(or (> (length (or candidates zero-candidates)) (* zero-candidates-per-page (1+ zero-current-page))) (< zero-fetch-size (* zero-candidates-per-page (+ 2 zero-current-page)))))
       ("has_previous_page" ,(> zero-current-page 0))))
    (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))))
@@ -202,7 +208,7 @@ if t, `zero-debug' will output debug msg in *zero-debug* buffer")
  (unless (functionp zero-build-candidates-func)
    (signal 'wrong-type-argument (list 'functionp zero-build-candidates-func)))
  (prog1 (funcall zero-build-candidates-func preedit-str fetch-size)
    (setq zero-fetch-size fetch-size)))
    (setq zero-fetch-size (max fetch-size (length zero-candidates)))))

(defun zero-build-candidates-complete (candidates)
  "called when `zero-build-candidates-async' returns"
@@ -300,7 +306,8 @@ return ch's Chinese punctuation if ch is converted. return nil otherwise"
		 new-fetch-size
		 (lambda (candidates)
		   (zero-build-candidates-complete candidates)
		   (setq zero-fetch-size new-fetch-size)
		   (setq zero-fetch-size (max new-fetch-size
					      (length candidates)))
		   (zero-just-page-down)))
      (zero-just-page-down))))

+41 −5
Original line number Diff line number Diff line
@@ -19,11 +19,45 @@
(defun zero-panel-async-call (method _handler &rest args)
  "call Method on zero-panel service asynchronously. This is a wrapper around `dbus-call-method-asynchronously'"
  (apply 'dbus-call-method-asynchronously
	 :session "com.emacsos.zero.Panel"
	 "/com/emacsos/zero/Panel"
	 "com.emacsos.zero.Panel"
	 :session
	 "com.emacsos.zero.Panel1"	; well known name
	 "/com/emacsos/zero/Panel1"	; object path
	 "com.emacsos.zero.Panel1.PanelInterface" ; interface name
	 method nil :timeout 500 args))

;;=========================
;; public utility function
;;=========================

(defun zero-alist-to-asv (hints)
  "convert lisp alist to dbus a{sv} data structure.
alist should be of form '((k1 [v1type] v1) (k2 [v2type] v2)).

For example,
(zero-alist-to-asv
  '((\"name\" \"foo\")
    (\"timeout\" :int32 10)))
=>
'(:array
  (:dict-entry \"name\" (:variant \"foo\"))
  (:dict-entry \"timeout\" (:variant :int32 10)))
"
  (if (null hints)
      '(:array :signature "{sv}")
    (let ((result '(:array)))
      (dolist (item hints)
	(push (list :dict-entry (car item) (cons :variant (cdr item))) result))
      (reverse result))))

(ert-deftest zero-alist-to-asv ()
  (should (equal (zero-alist-to-asv nil) '(:array :signature "{sv}")))
  (should (equal (zero-alist-to-asv
		  '(("name" "foo")
		    ("timeout" :int32 10)))
		 '(:array
		   (:dict-entry "name" (:variant "foo"))
		   (:dict-entry "timeout" (:variant :int32 10))))))

;;============
;; public API
;;============
@@ -33,12 +67,13 @@
(x, y) are coordinates, (0, 0) is at screen top left corner"
  (zero-panel-async-call "Move" nil :int32 x :int32 y))

(defun zero-panel-show-candidates (preedit_str candidate_length candidates)
(defun zero-panel-show-candidates (preedit_str candidate_length candidates &optional hints)
  "show candidates"
  (zero-panel-async-call "ShowCandidates" nil
			 :string preedit_str
			 :int32 candidate_length
			 (or candidates '(:array))))
			 (or candidates '(:array))
			 (zero-alist-to-asv hints)))

(defun zero-panel-show ()
  "show panel"
@@ -50,6 +85,7 @@

(defun zero-panel-quit ()
  "quit panel application"
  (interactive)
  (zero-panel-async-call "Quit" nil))

(provide 'zero-panel)
+3 −3
Original line number Diff line number Diff line
@@ -47,10 +47,10 @@
  (if zero-pinyin--build-candidates-use-test-data
      (progn
	(zero-pinyin-build-candidates-test preedit-str)
	(setq zero-fetch-size fetch-size))
	(setq zero-fetch-size (max fetch-size (length zero-candidates))))
    (zero-debug "zero-pinyin building candidate list synchronously\n")
    (let ((result (zero-pinyin-service-get-candidates preedit-str fetch-size)))
      (setq zero-fetch-size fetch-size)
      (setq zero-fetch-size (max fetch-size (length (first result))))
      (setq zero-pinyin-used-preedit-str-lengths (second result))
      (first result))))

@@ -62,7 +62,7 @@
   fetch-size
   (lambda (candidates matched_preedit_str_lengths)
     (setq zero-pinyin-used-preedit-str-lengths matched_preedit_str_lengths)
     (setq zero-fetch-size fetch-size)
     (setq zero-fetch-size (max fetch-size (length candidates)))
     ;; Note: with dynamic binding, this command result in (void-variable
     ;; complete-func) error.
     (funcall complete-func candidates))))