-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathflyspell-correct-helm.el
99 lines (88 loc) · 3.56 KB
/
flyspell-correct-helm.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
;;; flyspell-correct-helm.el --- Correcting words with flyspell via helm interface -*- lexical-binding: t; -*-
;;
;; Copyright (c) 2016-2022 Boris Buliga
;;
;; Author: Boris Buliga <[email protected]>
;; URL: https://github.com/d12frosted/flyspell-correct
;; Version: 0.6.1
;; Package-Requires: ((flyspell-correct "0.6.1") (helm "1.9.0") (emacs "24"))
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
;;
;;; Commentary:
;; This package provides helm interface for flyspell-correct package.
;;
;; Points of interest are `flyspell-correct-wrapper',
;; `flyspell-correct-previous' and `flyspell-correct-next'.
;;
;; Example usage:
;;
;; (require 'flyspell-correct-helm)
;; (define-key flyspell-mode-map (kbd "C-;") 'flyspell-correct-wrapper)
;;
;; Or via use-package:
;;
;; (use-package flyspell-correct-helm
;; :bind ("C-M-;" . flyspell-correct-wrapper)
;; :init
;; (setq flyspell-correct-interface #'flyspell-correct-helm))
;;
;;; Code:
;;
;; Requires
(require 'flyspell-correct)
(require 'helm)
;; Interface implementation
(defun flyspell-correct-helm--always-match (_)
"Return non-nil for any CANDIDATE."
t)
(defun flyspell-correct-helm--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))
(cons (format "Skip \"%s\"" word)
(cons 'skip word))
(cons (format "Stop at \"%s\"" word)
(cons 'stop 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 flyspell-correct-helm (candidates word)
"Run `helm' for the given CANDIDATES.
List of CANDIDATES is 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 ()
(flyspell-correct-helm--option-candidates word))
:action 'identity
:candidate-number-limit 9999
:match 'flyspell-correct-helm--always-match
:volatile t))
:buffer "*Helm Flyspell*"
:prompt "Correction: "))
(setq flyspell-correct-interface #'flyspell-correct-helm)
(provide 'flyspell-correct-helm)
;;; flyspell-correct-helm.el ends here