Elffuss BraidHub

kikocis/company-english-helper public

first commit
Changesetb5c1067d71672ca976bb42b7cf1270ecd13f0fb64c37fd43a557305305cfef92
AuthorAndy Stewart <lazycat.manatee@gmail.com> · 2018-08-18 15:14 +08:00
CommitterAndy Stewart <lazycat.manatee@gmail.com> · 2018-08-18 15:14 +08:00
ParentsNone: the first changeset

Browse the files at this changeset

4 files changed

added README.md +23 −0
@@ -0,0 +1,23 @@
1+# Install
2+1. Copy file [company-english-helper.el](company-english-helper.el) and [company-english-helper-data.el](company-english-helper-data.el) to directory ~/elisp/
3+
4+2. And set in your ~/.emacs like this:
5+```Elisp
6+(add-to-list 'load-path (expand-file-name "~/elisp"))
7+```
8+
9+3. And the following to your ~/.emacs startup file.
10+```Elisp
11+(require 'company-english-helper)
12+```
13+
14+4. Execute command `toggle-company-english-helper' to write english on the fly!
15+
16+# Customize your dict.
17+Default english dictionary is generate from stardict KDict dictionary with below command
18+
19+```Shell
20+python ./stardict.py stardict-kdic-ec-11w-2.4.2/kdic-ec-11w.ifo
21+```
22+
23+You can replace with your favorite stardict dictionary's info filepath to generate your own company-english-helper-data.el .
added company-english-helper-data.el +0 −0

Not shown: the file is larger than the hub shows, or this page holds as much as it shows. View the file.

added company-english-helper.el +155 −0
@@ -0,0 +1,155 @@
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: 0.7
10+;; Last-Updated: 2018-08-10 12:39:24
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+
59+;;; Customize:
60+;;
61+;;
62+;;
63+;; All of the above can customize by:
64+;; M-x customize-group RET company-english-helper RET
65+;;
66+
67+;;; Change log:
68+;;
69+;; 2018/08/10
70+;; * Require `cl' avoid error "Symbol's function definition is void: remove-if-not".
71+;;
72+;; 2018/07/29
73+;; * Calculate maximin length of match candidates dynamically.
74+;; * Adjust require code place.
75+;; * Adjust variable code place.
76+;;
77+;; 2018/07/20
78+;; * Use `string-prefix-p' instead fuzz match, too many wrong candidates in completion result.
79+;; * Use `company-grab-symbol' instead `company-grab-word' to fix that word "good-bye" can't completion, thanks et2010!
80+;;
81+;; 2018/07/06
82+;; * First released.
83+;;
84+
85+;;; Acknowledgements:
86+;;
87+;;
88+;;
89+
90+;;; TODO
91+;;
92+;;
93+;;
94+
95+;;; Require
96+(require 'cl)
97+(require 'cl-lib)
98+(require 'company)
99+(require 'company-english-helper-data)
100+
101+;;; Code:
102+
103+(defvar company-en-words-candidate-max-width 30
104+ "The max width of candidates.
105+Default is 30, it will occur candidate is not alignment if this value too small.")
106+
107+(defvar company-en-words-active-p nil
108+ "The status of company-en-words plugins.
109+Default is disable.")
110+
111+(defun en-words-annotation (s)
112+ (let* ((translation (get-text-property 0 :initials s))
113+ (translation-format-string (replace-regexp-in-string "\\cc" "" translation))
114+ (max-translation-length (+ 1 (apply 'max (mapcar 'length company-candidates))))
115+ (candidate-length (length s))
116+ (translation-length (length translation))
117+ (translation-format-length (length translation-format-string))
118+ (blank-length (max 0 (- max-translation-length candidate-length)))
119+ (dot-length (max 0 (- company-en-words-candidate-max-width (- translation-length translation-format-length)))))
120+ (format "%s" (concat (make-string blank-length ?\ )
121+ translation
122+ (make-string dot-length ?\.)))))
123+
124+(defun company-en-words (command &optional arg &rest ignored)
125+ (interactive (list 'interactive))
126+ (cl-case command
127+ (interactive (company-begin-backend 'company-en-words))
128+ (prefix (company-grab-symbol))
129+ (candidates
130+ (remove-if-not
131+ (lambda (c) (string-prefix-p (downcase arg) c))
132+ en-words-completions))
133+ (annotation (en-words-annotation arg))
134+ (sorted t)
135+ (ignore-case 'keep-prefix)
136+ ))
137+
138+(defun toggle-company-english-helper ()
139+ "Toggle company english helper."
140+ (interactive)
141+ (if company-en-words-active-p
142+ (progn
143+ (setq company-backends (remove 'company-en-words company-backends))
144+ ;; I need remove `company-en-words' with `company-yasnippet',
145+ ;; it's not enough just remove `company-en-words' from `company-backends'
146+ (setq company-backends (remove '(company-en-words :with company-yasnippet) company-backends))
147+ (setq company-en-words-active-p nil)
148+ (message "English helper has disable."))
149+ (add-to-list 'company-backends 'company-en-words)
150+ (setq company-en-words-active-p t)
151+ (message "English helper has enable.")))
152+
153+(provide 'company-english-helper)
154+
155+;;; company-english-helper.el ends here
added stardict.py +35 −0
@@ -0,0 +1,35 @@
1+from pystardict import Dictionary
2+import os
3+import sys
4+import codecs
5+
6+if __name__ == "__main__":
7+ if len(sys.argv) <= 1:
8+ print("Usage: ./stardict.py start_dict_ifo_file_path")
9+ else:
10+ start_dictionary = Dictionary(sys.argv[1].split(".ifo")[0])
11+
12+ f = codecs.open("company-english-helper-data.el","w","utf-8")
13+ f.write(";; -*- mode: fundamental; -*-\n")
14+ f.write(";;\n")
15+ f.write(";; This file is generate by stardict.py, please don't edit this file.\n")
16+ f.write(";;\n\n")
17+ f.write("(defconst en-words-completions\n '(\n")
18+
19+ index = 0
20+ for word in start_dictionary.keys():
21+ is_english_word = all(ord(char) < 128 for char in word)
22+ if is_english_word:
23+ first_line_translation = start_dictionary.dict[word].split()[0]
24+ no_phonetic_translation = first_line_translation.split(">")[-1]
25+
26+ candiate_word = word.lower().replace('\"', ' ')
27+ candiate_translateion = no_phonetic_translation.strip().replace('\"', ' ')
28+ f.write(' #(\"' + candiate_word + '\" ' + '0 1' + '\n' +
29+ ' (:initials \"' + candiate_translateion + '\"))\n')
30+
31+ print(index, candiate_word, candiate_translateion)
32+ index += 1
33+
34+ f.write(" ))\n\n")
35+ f.write("(provide 'company-english-helper-data)\n")