-
Notifications
You must be signed in to change notification settings - Fork 1
/
mozc-posframe.el
196 lines (163 loc) · 7.71 KB
/
mozc-posframe.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
;;; mozc-posframe.el --- Mozc with posframe
;; Copyright (C) 2019 Yuya Takahashi
;; Author: Yuya Takahashi <derutakayu@gmail.com>
;; Version: 0.2
;; Keywords: i18n, extentions
;; Package-Requires: ((posframe "0.4.3") (mozc "0"))
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This package implements posframe-style for candidates displaying by
;; `posframe' for' `mozc'.
;; This package is based on `mozc-popup.el' written by 'Daisuke Kobayashi <d5884jp@gmail.com>'
;;; Usage:
;; (require 'mozc-posframe)
;; (setq mozc-candidate-style 'posframe) ; select posframe style.
;;
;;; Code:
(require 'mozc)
(require 'posframe)
(defvar mozc-posframe-buffer-name " *Mozc Posframe*"
"Name of the buffer to render candidates")
(defface mozc-cand-overlay-description-face
'((t (:inherit mozc-cand-overlay-odd-face)))
"Face for description part of overlay candidate window."
:group 'mozc-faces)
(defvar mozc-cand-posframe-position nil)
(make-variable-buffer-local 'mozc-cand-posframe-position)
(defconst mozc-cand-posframe-shortcut-spacer ". ")
(defconst mozc-cand-posframe-description-space 3)
(defun mozc-posframe--get-buffer ()
(get-buffer-create mozc-posframe-buffer-name))
(defun mozc-posframe--apply-face (str face)
(put-text-property 0 (length str) 'face face str)
str)
(defun mozc-posframe--get-item-face (index focused-index)
(if (and focused-index (= focused-index index))
'mozc-cand-overlay-focused-face
(if (zerop (logand index 1))
'mozc-cand-overlay-even-face
'mozc-cand-overlay-odd-face)))
(defun mozc-posframe--render (candidates footer-label focused-index max-width)
"render candidates to posframe's buffer"
;; render candidates
(with-current-buffer (mozc-posframe--get-buffer)
(mapc
(lambda (candidate)
(let* ((index (mozc-protobuf-get candidate 'index))
(value (mozc-protobuf-get candidate 'value))
(description (mozc-protobuf-get candidate 'annotation 'description))
(shortcut (mozc-protobuf-get candidate 'annotation 'shortcut))
(label (if shortcut
(concat shortcut
mozc-cand-posframe-shortcut-spacer
value
(if description
(concat
(cl-loop repeat mozc-cand-posframe-description-space
concat " ")
description)
""))
value)))
(insert
(mozc-posframe--apply-face (concat label (cl-loop repeat (max 0 (- max-width (string-width label)))
concat " "))
(mozc-posframe--get-item-face index focused-index)))
(newline)))
candidates)
(when footer-label
(insert (mozc-posframe--apply-face footer-label 'mozc-cand-overlay-footer-face)))))
;;;###autoload
(defun mozc-cand-posframe-draw (candidates)
(let ((footer-label (mozc-protobuf-get candidates 'footer 'label))
(focused-index (mozc-protobuf-get candidates 'focused-index))
(sub-candidates (mozc-protobuf-get candidates 'subcandidates))
(max-width 0))
(when sub-candidates
(setq footer-label
(catch 'find-focused-value
(dolist (candidate (mozc-protobuf-get candidates 'candidate))
(let ((index (mozc-protobuf-get candidate 'index))
(value (mozc-protobuf-get candidate 'value))
(shortcut (mozc-protobuf-get candidate 'annotation 'shortcut)))
(when (eq index focused-index)
(throw 'find-focused-value
(concat (if shortcut (concat shortcut mozc-cand-posframe-shortcut-spacer value)
value))))))))
(setq focused-index (mozc-protobuf-get sub-candidates 'focused-index))
(setq candidates sub-candidates))
(mapc
(lambda (candidate)
(let ((index (mozc-protobuf-get candidate 'index))
(value (mozc-protobuf-get candidate 'value))
(description (mozc-protobuf-get candidate 'annotation 'description))
(shortcut (mozc-protobuf-get candidate 'annotation 'shortcut)))
(setq max-width (max (+ (string-width value)
(if shortcut
(+ (string-width
mozc-cand-posframe-shortcut-spacer)
(string-width shortcut)) 0)
(if description
(+ mozc-cand-posframe-description-space
(string-width description)) 0))
max-width))))
(mozc-protobuf-get candidates 'candidate))
(let ((candidates-size (mozc-protobuf-get candidates 'size))
(index-visible (mozc-protobuf-get candidates 'footer 'index-visible)))
(if (and index-visible focused-index candidates-size)
(let ((index-label (format "%d/%d" (1+ focused-index) candidates-size)))
(setq footer-label
(format (concat "%" (number-to-string
(max max-width (string-width index-label))) "s")
index-label)))
(setq footer-label
(concat
footer-label
(cl-loop repeat (max 0 (- max-width (string-width footer-label)))
concat " "))))
(mozc-cand-posframe-clear)
(mozc-posframe--render (mozc-protobuf-get candidates 'candidate) footer-label focused-index
(string-width footer-label))
(posframe-show (mozc-posframe--get-buffer)
:position mozc-cand-posframe-position))))
;;;###autoload
(defun mozc-cand-posframe-update (candidates)
(unless mozc-cand-posframe-position
(setq mozc-cand-posframe-position (posn-point mozc-preedit-posn-origin)))
(condition-case nil
(when (posframe-workable-p)
(mozc-with-buffer-modified-p-unchanged
(mozc-cand-posframe-draw candidates)))
(error
(mozc-cand-posframe-clear)
;; Fall back to the echo area version.
(mozc-cand-echo-area-update candidates))))
;;;###autoload
(defun mozc-cand-posframe-clear ()
(with-current-buffer (mozc-posframe--get-buffer)
(erase-buffer)))
;;;###autoload
(defun mozc-cand-posframe-clean-up ()
(with-current-buffer (mozc-posframe--get-buffer)
(erase-buffer))
(setq mozc-cand-posframe-position nil)
(posframe-hide mozc-posframe-buffer-name))
;;;###autoload
(defun mozc-posframe-register ()
"Register mozc-posframe to mozc candidate dispatch table"
(unless (assoc 'posframe mozc-candidate-dispatch-table)
(add-to-list 'mozc-candidate-dispatch-table
'(posframe
(clean-up . mozc-cand-posframe-clean-up)
(clear . mozc-cand-posframe-clear)
(update . mozc-cand-posframe-update)))))
(provide 'mozc-posframe)
;;; mozc-posframe.el ends here