kikocis/company-english-helper public
Changesets · Branches · Tags · Compare
| Changeset | aa289f8aed7ac5032894468e2f10eaf55b32f440bf1a34c94fbe528cad37305d |
|---|---|
| Author | Andy Stewart <lazycat.manatee@gmail.com> · 2018-08-28 23:15 +08:00 |
| Committer | Andy Stewart <lazycat.manatee@gmail.com> · 2018-08-28 23:15 +08:00 |
| Parents | af4f8814e395 |
3 files changed
modified 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.
modified company-english-helper.el +51 −25
| @@ -6,8 +6,8 @@ | ||
| 6 | 6 | ;; Maintainer: Andy Stewart <lazycat.manatee@gmail.com> |
| 7 | 7 | ;; Copyright (C) 2018, Andy Stewart, all rights reserved. |
| 8 | 8 | ;; Created: 2018-07-06 23:22:22 |
| 9 | -;; Version: 0.8 | |
| 10 | -;; Last-Updated: 2018-08-28 21:58:45 | |
| 9 | +;; Version: 1.0 | |
| 10 | +;; Last-Updated: 2018-08-28 22:17:36 | |
| 11 | 11 | ;; By: Andy Stewart |
| 12 | 12 | ;; URL: http://www.emacswiki.org/emacs/download/company-english-helper.el |
| 13 | 13 | ;; Keywords: |
| @@ -68,6 +68,7 @@ | ||
| 68 | 68 | ;; |
| 69 | 69 | ;; 2018/08/27 |
| 70 | 70 | ;; * Add fuzz search algorithm. |
| 71 | +;; * Make prefix-match-candidates as first candidates, then suffix-match-candidates and fuzz-match-candidates. | |
| 71 | 72 | ;; |
| 72 | 73 | ;; 2018/08/10 |
| 73 | 74 | ;; * Require `cl' avoid error "Symbol's function definition is void: remove-if-not". |
| @@ -103,15 +104,18 @@ | ||
| 103 | 104 | |
| 104 | 105 | ;;; Code: |
| 105 | 106 | |
| 106 | -(defvar company-en-words-candidate-max-width 30 | |
| 107 | +(defvar company-english-helper-candidate-max-width 30 | |
| 107 | 108 | "The max width of candidates. |
| 108 | 109 | Default is 30, it will occur candidate is not alignment if this value too small.") |
| 109 | 110 | |
| 110 | -(defvar company-en-words-active-p nil | |
| 111 | - "The status of company-en-words plugins. | |
| 111 | +(defvar company-english-helper-active-p nil | |
| 112 | + "The status of company-english-helper plugins. | |
| 112 | 113 | Default is disable.") |
| 113 | 114 | |
| 114 | -(defun en-words-annotation (s) | |
| 115 | +(defvar company-english-helper-match-group-size 10 | |
| 116 | + "The max size of match group.") | |
| 117 | + | |
| 118 | +(defun english-helper-annotation (s) | |
| 115 | 119 | (let* ((translation (get-text-property 0 :initials s)) |
| 116 | 120 | (translation-format-string (replace-regexp-in-string "\\cc" "" translation)) |
| 117 | 121 | (max-translation-length (+ 1 (apply 'max (mapcar 'length company-candidates)))) |
| @@ -119,42 +123,64 @@ | ||
| 119 | 123 | (translation-length (length translation)) |
| 120 | 124 | (translation-format-length (length translation-format-string)) |
| 121 | 125 | (blank-length (max 0 (- max-translation-length candidate-length))) |
| 122 | - (dot-length (max 0 (- company-en-words-candidate-max-width (- translation-length translation-format-length))))) | |
| 126 | + (dot-length (max 0 (- company-english-helper-candidate-max-width (- translation-length translation-format-length))))) | |
| 123 | 127 | (format "%s" (concat (make-string blank-length ?\ ) |
| 124 | 128 | translation |
| 125 | 129 | (make-string dot-length ?\.))))) |
| 126 | 130 | |
| 127 | -(defun company-en-words (command &optional arg &rest ignored) | |
| 131 | +(defun company-english-helper-search (command &optional arg &rest ignored) | |
| 128 | 132 | (interactive (list 'interactive)) |
| 129 | 133 | (cl-case command |
| 130 | - (interactive (company-begin-backend 'company-en-words)) | |
| 134 | + (interactive (company-begin-backend 'company-english-helper-search)) | |
| 131 | 135 | (prefix (company-grab-symbol)) |
| 132 | 136 | (candidates |
| 133 | - (remove-if-not | |
| 134 | - (lambda (c) (or (string-prefix-p (downcase arg) c) | |
| 135 | - (string-suffix-p (downcase arg) c) | |
| 136 | - (and (string-match-p "-" (downcase arg)) | |
| 137 | - (and (string-prefix-p (car (split-string (downcase arg) "-")) c) | |
| 138 | - (string-suffix-p (cadr (split-string (downcase arg) "-")) c) | |
| 137 | + (progn | |
| 138 | + (setq prefix-match-candidates | |
| 139 | + (remove-if-not | |
| 140 | + (lambda (c) (string-prefix-p (downcase arg) c)) | |
| 141 | + english-helper-completions)) | |
| 142 | + | |
| 143 | + (setq suffix-match-candidates | |
| 144 | + (remove-if-not | |
| 145 | + (lambda (c) (string-suffix-p (downcase arg) c)) | |
| 146 | + english-helper-completions)) | |
| 147 | + | |
| 148 | + (setq fuzz-match-candidates | |
| 149 | + (remove-if-not | |
| 150 | + (lambda (c) | |
| 151 | + (and (string-match-p "-" (downcase arg)) | |
| 152 | + (let* ((split-list (split-string (downcase arg) "-")) | |
| 153 | + (left-string (car split-list)) | |
| 154 | + (right-string (cadr split-list))) | |
| 155 | + (and (string-prefix-p left-string c) | |
| 156 | + (string-match-p right-string (string-remove-prefix left-string c)) | |
| 139 | 157 | )))) |
| 140 | - en-words-completions)) | |
| 141 | - (annotation (en-words-annotation arg)) | |
| 158 | + english-helper-completions)) | |
| 159 | + | |
| 160 | + ;; Make prefix-match-candidates as first candidates, then suffix-match-candidates and fuzz-match-candidates. | |
| 161 | + (delete-dups (append | |
| 162 | + (subseq prefix-match-candidates 0 (min company-english-helper-match-group-size (length prefix-match-candidates))) | |
| 163 | + (subseq suffix-match-candidates 0 (min company-english-helper-match-group-size (length suffix-match-candidates))) | |
| 164 | + (subseq fuzz-match-candidates 0 (min company-english-helper-match-group-size (length fuzz-match-candidates))) | |
| 165 | + )) | |
| 166 | + )) | |
| 167 | + (annotation (english-helper-annotation arg)) | |
| 142 | 168 | (sorted t) |
| 143 | 169 | )) |
| 144 | 170 | |
| 145 | 171 | (defun toggle-company-english-helper () |
| 146 | 172 | "Toggle company english helper." |
| 147 | 173 | (interactive) |
| 148 | - (if company-en-words-active-p | |
| 174 | + (if company-english-helper-active-p | |
| 149 | 175 | (progn |
| 150 | - (setq company-backends (remove 'company-en-words company-backends)) | |
| 151 | - ;; I need remove `company-en-words' with `company-yasnippet', | |
| 152 | - ;; it's not enough just remove `company-en-words' from `company-backends' | |
| 153 | - (setq company-backends (remove '(company-en-words :with company-yasnippet) company-backends)) | |
| 154 | - (setq company-en-words-active-p nil) | |
| 176 | + (setq company-backends (remove 'company-english-helper-search company-backends)) | |
| 177 | + ;; I need remove `company-english-helper-search' with `company-yasnippet', | |
| 178 | + ;; it's not enough just remove `company-english-helper-search' from `company-backends' | |
| 179 | + (setq company-backends (remove '(company-english-helper-search :with company-yasnippet) company-backends)) | |
| 180 | + (setq company-english-helper-active-p nil) | |
| 155 | 181 | (message "English helper has disable.")) |
| 156 | - (add-to-list 'company-backends 'company-en-words) | |
| 157 | - (setq company-en-words-active-p t) | |
| 182 | + (add-to-list 'company-backends 'company-english-helper-search) | |
| 183 | + (setq company-english-helper-active-p t) | |
| 158 | 184 | (message "English helper has enable."))) |
| 159 | 185 | |
| 160 | 186 | (provide 'company-english-helper) |
modified stardict.py +1 −1
| @@ -14,7 +14,7 @@ | ||
| 14 | 14 | f.write(";;\n") |
| 15 | 15 | f.write(";; This file is generate by stardict.py, please don't edit this file.\n") |
| 16 | 16 | f.write(";;\n\n") |
| 17 | - f.write("(defconst en-words-completions\n '(\n") | |
| 17 | + f.write("(defconst english-helper-completions\n '(\n") | |
| 18 | 18 | |
| 19 | 19 | index = 0 |
| 20 | 20 | for word in start_dictionary.keys(): |