-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompany-emojify.el
178 lines (152 loc) · 6.43 KB
/
company-emojify.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
;;; company-emojify.el --- Company completion for Emojify -*- lexical-binding: t; -*-
;; Copyright (C) 2021-2025 Shen, Jen-Chieh
;; Created date 2021-07-16 13:41:28
;; Author: Shen, Jen-Chieh <[email protected]>
;; URL: https://github.com/jcs-elpa/company-emojify
;; Version: 0.1.0
;; Package-Requires: ((emacs "26.1") (company "0.8.0") (emojify "1.2.1") (ht "2.0"))
;; Keywords: convenience emoji company emojify
;; This file is NOT part of GNU Emacs.
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Company completion for Emojify
;;
;;; Code:
(require 'cl-lib)
(require 'company)
(require 'emojify)
(require 'ht)
(defgroup company-emojify nil
"Company completion for Emojify."
:prefix "company-emojify-"
:group 'tool
:link '(url-link :tag "Repository" "https://github.com/jcs-elpa/company-emojify"))
(defcustom company-emojify-insert-unicode t
"Replace the :shortcode: with the real Unicode character upon completion."
:type 'boolean
:group 'company-emojify)
(defcustom company-emojify-emoji-styles emojify-emoji-styles
"Styles same with variable `emojify-emoji-styles' but limit to this package."
:type '(set
(const :tag "Display only ascii emojis" ascii)
(const :tag "Display only github emojis" github)
(const :tag "Display only unicode codepoints" unicode))
:group 'company-emojify)
(defcustom company-emojify-annotation 'image
"Option to display emoji in annotation."
:type '(choice (const :tag "Don't display" nil)
(const :tag "Display with unicode" unicode)
(const :tag "Display with image" image))
:group 'company-emojify)
(defcustom company-emojify-document t
"If non-nil, display emoji information."
:type 'boolean
:group 'company-emojify)
;;
;; (@* "Core" )
;;
(defun company-emojify--display-image-p ()
"Return non-nil, if we can display image."
(and (display-graphic-p) (eq company-emojify-annotation 'image)))
(defun company-emojify--display-image (file selected)
"Display emoji icon in annotation.
FILE is the emoji png file. If SELECTED is non-nil means the current candidate
is the selected one."
(let ((image-file (expand-file-name file (emojify-image-dir)))
bkg dfw icon-size spec)
(when (file-exists-p image-file)
(setq bkg (face-attribute (if selected
'company-tooltip-selection
'company-tooltip)
:background)
dfw (default-font-width)
icon-size (cond
((integerp company-icon-size)
company-icon-size)
;; XXX: Also consider smooth scaling, e.g. using
;; (aref (font-info (face-font 'default)) 2)
((and (consp company-icon-size)
(eq 'auto-scale (car company-icon-size)))
(let ((base-size (cdr company-icon-size))
(dfh (default-font-height)))
(min
(if (> dfh (* 2 base-size))
(* 2 base-size)
base-size)
(* 2 dfw)))))
spec (list 'image
:file image-file
:type 'png
:width icon-size
:height icon-size
:ascent 'center
:background (unless (eq bkg 'unspecified)
bkg)))
(propertize "-" 'display spec))))
(defun company-emojify--annotation (candidate)
"Return annotation for CANDIDATE."
(when company-emojify-annotation
(let* ((display-with-image (company-emojify--display-image-p))
(type (if display-with-image "image" "unicode"))
(data (emojify-get-emoji candidate))
(display (when (hash-table-p data) (ht-get data type)))
(selected (equal (nth company-selection company-candidates) candidate)))
(if display
(if display-with-image
(or (company-emojify--display-image display selected) "")
display)
""))))
(defun company-emojify--candidates ()
"Return a list of valid candidates."
(let ((user (when (hash-table-p emojify--user-emojis) (ht-keys emojify--user-emojis)))
(const (when (hash-table-p emojify-emojis) (ht-keys emojify-emojis))))
(cl-remove-if-not
(lambda (candidate)
(let* ((data (emojify-get-emoji candidate))
(style (ht-get data "style")))
(memq (intern style) company-emojify-emoji-styles)))
(append user const))))
(defun company-emojify--doc-buffer (candidate)
"Return document for CANDIDATE."
(company-doc-buffer
(if company-emojify-document
(let* ((data (emojify-get-emoji candidate))
(name (ht-get data "name"))
(style (ht-get data "style")))
(format "%s (%s)" name style))
"")))
;;
;; (@* "Entry" )
;;
;;;###autoload
(defun company-emojify (command &optional arg &rest ignored)
"Company backend for Emojify.
Arguments COMMAND, ARG and IGNORED are standard arguments from `company-mode`."
(interactive (list 'interactive))
(emojify-create-emojify-emojis)
(cl-case command
(interactive (company-begin-backend 'company-emojify))
(prefix (company-grab "\:[a-zA-Z0-9-_+]*"))
(candidates (company-emojify--candidates))
(annotation (company-emojify--annotation arg))
(doc-buffer (company-emojify--doc-buffer arg))
(post-completion
(delete-region (- (point) (length arg)) (point))
(if company-emojify-insert-unicode
(let* ((data (emojify-get-emoji arg))
(type "unicode")
(display (when (hash-table-p data) (ht-get data type))))
(insert display))
(insert arg)))))
(provide 'company-emojify)
;;; company-emojify.el ends here