Elffuss BraidHub

kikocis/company-english-helper public

master · 8.4 KiBRawPermalink
1;;; company-english-helper.el --- English helper with company interface
2
3;; Filename: company-english-helper.el
4;; Description: English helper with company interface
5;; Author: Andy Stewart <lazycat.manatee@gmail.com>
6;; Maintainer: Andy Stewart <lazycat.manatee@gmail.com>
7;; Copyright (C) 2018, Andy Stewart, all rights reserved.
8;; Created: 2018-07-06 23:22:22
9;; Version: 1.2
10;; Last-Updated: 2018-12-01 23:35:13
11;; By: Andy Stewart
12;; URL: http://www.emacswiki.org/emacs/download/company-english-helper.el
13;; Keywords:
14;; Compatibility: GNU Emacs 27.0.50
15;;
16;; Features that might be required by this library:
17;;
18;; `cl' `cl-lib' `company' `company-english-helper-data'
19;;
20
21;;; This file is NOT part of GNU Emacs
22
23;;; License
24;;
25;; This program is free software; you can redistribute it and/or modify
26;; it under the terms of the GNU General Public License as published by
27;; the Free Software Foundation; either version 3, or (at your option)
28;; any later version.
29
30;; This program is distributed in the hope that it will be useful,
31;; but WITHOUT ANY WARRANTY; without even the implied warranty of
32;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33;; GNU General Public License for more details.
34
35;; You should have received a copy of the GNU General Public License
36;; along with this program; see the file COPYING. If not, write to
37;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
38;; Floor, Boston, MA 02110-1301, USA.
39
40;;; Commentary:
41;;
42;; English helper with company interface.
43;;
44
45;;; Installation:
46;;
47;; Put company-english-helper.el to your load-path.
48;; The load-path is usually ~/elisp/.
49;; It's set in your ~/.emacs like this:
50;; (add-to-list 'load-path (expand-file-name "~/elisp"))
51;;
52;; And the following to your ~/.emacs startup file.
53;;
54;; (require 'company-english-helper)
55;;
56;; And bind your key with function `toggle-company-english-helper'.
57;;
58;; If your computer's performance is good enough, you can enable fuzz search by:
59;;
60;; (setq company-english-helper-fuzz-search-p t)
61;;
62
63;;; Customize:
64;;
65;;
66;;
67;; All of the above can customize by:
68;; M-x customize-group RET company-english-helper RET
69;;
70
71;;; Change log:
72;;
73;; 2018/12/01
74;; * Adjust the case of the completion word according to the string entered by the user.
75;;
76;; 2018/08/29
77;; * Add option `company-english-helper-fuzz-search-p' and turn off it default.
78;;
79;; 2018/08/27
80;; * Add fuzz search algorithm.
81;; * Make prefix-match-candidates as first candidates, then suffix-match-candidates and fuzz-match-candidates.
82;;
83;; 2018/08/10
84;; * Require `cl' avoid error "Symbol's function definition is void: remove-if-not".
85;;
86;; 2018/07/29
87;; * Calculate maximin length of match candidates dynamically.
88;; * Adjust require code place.
89;; * Adjust variable code place.
90;;
91;; 2018/07/20
92;; * Use `string-prefix-p' instead fuzz match, too many wrong candidates in completion result.
93;; * Use `company-grab-symbol' instead `company-grab-word' to fix that word "good-bye" can't completion, thanks et2010!
94;;
95;; 2018/07/06
96;; * First released.
97;;
98
99;;; Acknowledgements:
100;;
101;;
102;;
103
104;;; TODO
105;;
106;;
107;;
108
109;;; Require
110(require 'cl)
111(require 'cl-lib)
112(require 'company)
113(require 'company-english-helper-data)
114
115;;; Code:
116
117(defvar company-english-helper-candidate-max-width 30
118 "The max width of candidates.
119Default is 30, it will occur candidate is not alignment if this value too small.")
120
121(defvar company-english-helper-active-p nil
122 "The status of company-english-helper plugins.
123Default is disable.")
124
125(defvar company-english-helper-match-group-size 10
126 "The max size of match group.")
127
128(defvar company-english-helper-fuzz-search-p nil
129 "Turn on this option to enable fuzz search.
130Default is disable because fuzz search performance.
131
132If your computer's performance is good enough, you can enable this option.")
133
134(defun english-helper-annotation (s)
135 (let* ((translation (get-text-property 0 :initials s))
136 (translation-format-string (replace-regexp-in-string "\\cc" "" translation))
137 (max-translation-length (+ 1 (apply 'max (mapcar 'length company-candidates))))
138 (candidate-length (length s))
139 (translation-length (length translation))
140 (translation-format-length (length translation-format-string))
141 (blank-length (max 0 (- max-translation-length candidate-length)))
142 (dot-length (max 0 (- company-english-helper-candidate-max-width (- translation-length translation-format-length)))))
143 (format "%s" (concat (make-string blank-length ?\ )
144 translation
145 (make-string dot-length ?\.)))))
146
147(defun company-english-helper-search (command &optional arg &rest ignored)
148 (interactive (list 'interactive))
149 (cl-case command
150 (interactive (company-begin-backend 'company-english-helper-search))
151 (prefix (company-grab-symbol))
152 (candidates
153 (let* ((prefix-match-candidates
154 (remove-if-not
155 (lambda (c) (string-prefix-p (downcase arg) c))
156 english-helper-completions))
157 (suffix-match-candidates
158 (if company-english-helper-fuzz-search-p
159 (remove-if-not
160 (lambda (c) (string-suffix-p (downcase arg) c))
161 english-helper-completions)
162 nil))
163 (fuzz-match-candidates
164 (if company-english-helper-fuzz-search-p
165 (remove-if-not
166 (lambda (c)
167 (and (string-match-p "-" (downcase arg))
168 (let* ((split-list (split-string (downcase arg) "-"))
169 (left-string (car split-list))
170 (right-string (cadr split-list)))
171 (and (string-prefix-p left-string c)
172 (string-match-p right-string (string-remove-prefix left-string c))
173 ))))
174 english-helper-completions)
175 nil))
176 (match-candidates
177 ;; Make prefix-match-candidates as first candidates, then suffix-match-candidates and fuzz-match-candidates.
178 (delete-dups (if fuzz-match-candidates
179 (append
180 (subseq prefix-match-candidates 0 (min company-english-helper-match-group-size (length prefix-match-candidates)))
181 (subseq suffix-match-candidates 0 (min company-english-helper-match-group-size (length suffix-match-candidates)))
182 (subseq fuzz-match-candidates 0 (min company-english-helper-match-group-size (length fuzz-match-candidates)))
183 )
184 (append
185 prefix-match-candidates
186 suffix-match-candidates
187 fuzz-match-candidates
188 )))))
189 (company-english-helper-convert-candidates arg match-candidates)
190 ))
191 (annotation (english-helper-annotation arg))
192 (sorted t)))
193
194(defun company-english-helper-convert-candidates (input candidates)
195 (cond ((company-english-helper-upcase-string-p input)
196 (mapcar 'upcase candidates))
197 ((company-english-helper-capitalize-string-p input)
198 (mapcar 'capitalize candidates))
199 (t candidates)))
200
201(defun company-english-helper-upcase-string-p (str)
202 (let ((case-fold-search nil))
203 (and (> (length str) 1)
204 (string-match-p "\\`[A-Z]*\\'" str))))
205
206(defun company-english-helper-capitalize-string-p (str)
207 (let ((case-fold-search nil))
208 (string-match-p "\\`[A-Z][a-z]*\\'" str)))
209
210(defun toggle-company-english-helper ()
211 "Toggle company english helper."
212 (interactive)
213 (if company-english-helper-active-p
214 (progn
215 (setq company-backends (remove 'company-english-helper-search company-backends))
216 ;; I need remove `company-english-helper-search' with `company-yasnippet',
217 ;; it's not enough just remove `company-english-helper-search' from `company-backends'
218 (setq company-backends (remove '(company-english-helper-search :with company-yasnippet) company-backends))
219 (setq company-english-helper-active-p nil)
220 (message "English helper has disable."))
221 (if (not company-mode)
222 (company-mode t))
223 (add-to-list 'company-backends 'company-english-helper-search)
224 (setq company-english-helper-active-p t)
225 (message "English helper has enable.")))
226
227(provide 'company-english-helper)
228
229;;; company-english-helper.el ends here