-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhelm-flyspell.el
135 lines (121 loc) · 5.98 KB
/
helm-flyspell.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
;;; helm-flyspell.el --- Helm extension for correcting words with flyspell -*- lexical-binding: t; -*-
;; Copyright (C) 2014 Andrzej Pronobis <[email protected]>
;; Author: Andrzej Pronobis
;; URL: https://github.com/pronobis/helm-flyspell
;; Keywords: convenience
;; Package-Requires: ((helm "1.6.5"))
;; 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:
;;
;; To use, just put your cursor on or after the misspelled word and
;; run helm-flyspell-correct. You can of course bind it to a key as
;; well by adding this to your `~/.emacs` file:
;; (define-key flyspell-mode-map (kbd "C-;") 'helm-flyspell-correct)
;;
;; When invoked, it will show the list of corrections suggested by
;; Flyspell and options to save the word in your personal dictionary
;; or accept it in the buffer or the session. If a pattern is typed,
;; it will be used to filter the corrections. It can also be directly
;; saved to the dictionary, even if it is different from the initial
;; word. The new typed word will also replace the word typed in the
;; buffer.
;;; Code:
;; Requires
(require 'helm)
(require 'flyspell)
(defun helm-flyspell--always-match (_candidate)
"Return true for any CANDIDATE."
t)
(defun helm-flyspell--option-candidates (word)
"Return a set of options for the given WORD."
(let ((opts (list (cons (format "Save \"%s\"" word) (cons 'save word))
(cons (format "Accept (session) \"%s\"" word) (cons 'session word))
(cons (format "Accept (buffer) \"%s\"" word) (cons 'buffer word)))))
(unless (string= helm-pattern "")
(setq opts (append opts (list (cons (format "Save \"%s\"" helm-pattern) (cons 'save helm-pattern))
(cons (format "Accept (session) \"%s\"" helm-pattern) (cons 'session helm-pattern))
(cons (format "Accept (buffer) \"%s\"" helm-pattern) (cons 'buffer helm-pattern))))))
opts))
(defun helm-flyspell (candidates word)
"Run helm for the given CANDIDATES given by flyspell for the WORD.
Return a selected word to use as a replacement or
a tuple of (command, word) to be used by flyspell-do-correct."
(helm :sources (list (helm-build-sync-source (format "Suggestions for \"%s\" in dictionary \"%s\""
word (or ispell-local-dictionary
ispell-dictionary
"Default"))
:candidates candidates
:action 'identity
:candidate-number-limit 9999
:fuzzy-match t)
(helm-build-sync-source "Options"
:candidates (lambda ()
(helm-flyspell--option-candidates word))
:action 'identity
:candidate-number-limit 9999
:match 'helm-flyspell--always-match
:volatile t))
:buffer "*Helm Flyspell*"
:prompt "Correction: "))
;;;###autoload
(defun helm-flyspell-correct ()
"Use helm for flyspell correction.
Adapted from `flyspell-correct-word-before-point'."
(interactive)
;; use the correct dictionary
(flyspell-accept-buffer-local-defs)
(let ((cursor-location (point))
(word (flyspell-get-word)))
(if (consp word)
(let ((start (car (cdr word)))
(end (car (cdr (cdr word))))
(word (car word))
poss ispell-filter)
;; now check spelling of word.
(ispell-send-string "%\n") ;put in verbose mode
(ispell-send-string (concat "^" word "\n"))
;; wait until ispell has processed word
(while (progn
(accept-process-output ispell-process)
(not (string= "" (car ispell-filter)))))
;; Remove leading empty element
(setq ispell-filter (cdr ispell-filter))
;; ispell process should return something after word is sent.
;; Tag word as valid (i.e., skip) otherwise
(or ispell-filter
(setq ispell-filter '(*)))
(if (consp ispell-filter)
(setq poss (ispell-parse-output (car ispell-filter))))
(cond
((or (eq poss t) (stringp poss))
;; don't correct word
(message "%s is correct" (funcall ispell-format-word-function word))
t)
((null poss)
;; ispell error
(error "Ispell: error in Ispell process"))
(t
;; The word is incorrect, we have to propose a replacement.
(let ((res (helm-flyspell (nth 2 poss) word)))
(cond ((stringp res)
(flyspell-do-correct res poss word cursor-location start end cursor-location))
(t
(let ((cmd (car res))
(wrd (cdr res)))
(if (string= wrd word)
(flyspell-do-correct cmd poss wrd cursor-location start end cursor-location)
(progn
(flyspell-do-correct cmd poss wrd cursor-location start end cursor-location)
(flyspell-do-correct wrd poss word cursor-location start end cursor-location)))))))))
(ispell-pdict-save t)))))
(provide 'helm-flyspell)
;;; helm-flyspell.el ends here