-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathflyspell-correct-ivy.el
119 lines (104 loc) · 3.93 KB
/
flyspell-correct-ivy.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
;;; flyspell-correct-ivy.el --- Correcting words with flyspell via ivy 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") (ivy "0.8.0") (emacs "24.4"))
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
;;
;;; Commentary:
;; This package provides ivy interface for flyspell-correct package.
;;
;; Points of interest are `flyspell-correct-wrapper',
;; `flyspell-correct-previous' and `flyspell-correct-next'.
;;
;; Example usage:
;;
;; (require 'flyspell-correct-ivy)
;; (define-key flyspell-mode-map (kbd "C-;") 'flyspell-correct-wrapper)
;;
;; Or via use-package:
;;
;; (use-package flyspell-correct-ivy
;; :bind ("C-M-;" . flyspell-correct-wrapper)
;; :init
;; (setq flyspell-correct-interface #'flyspell-correct-ivy))
;;
;;; Code:
;;
;; Requires
(require 'flyspell-correct)
(require 'ivy)
(require 'subr-x)
;; Interface implementation
(defvar flyspell-correct-ivy-map (make-sparse-keymap)
"Keymap used in the `flyspell-correct-ivy' minibuffer.")
(defvar-local flyspell-correct-ivy--result nil
"Result of `flyspell-correct-ivy'.
See `flyspell-correct-interface' for more information.")
(defun flyspell-correct-ivy--process-input (candidates word input)
"Calculate resulting string based on INPUT.
It should allow to:
1. Save misspelled WORD when CANDIDATES list is non-empty.
2. Save misspelled WORD when CANDIDATES list is empty.
3. Correct and save WORD in one pass when CANDIDATES list is non-empty.
4. Correct and save WORD in one pass when CANDIDATES list is empty."
(if (or (member input candidates))
word
(if (string-empty-p input)
word
input)))
;;;###autoload
(defun flyspell-correct-ivy (candidates word)
"Run `ivy-read' for the given CANDIDATES.
List of CANDIDATES is given by flyspell for the WORD.
Return result according to `flyspell-correct-interface'
specification."
(setq flyspell-correct-ivy--result nil)
(let* ((action-default
(lambda (x)
(setq flyspell-correct-ivy--result x)))
(action-save-word
(lambda (x)
(setq flyspell-correct-ivy--result
(cons 'save (flyspell-correct-ivy--process-input candidates word x)))))
(action-accept-session
(lambda (x)
(setq flyspell-correct-ivy--result
(cons 'session (flyspell-correct-ivy--process-input candidates word x)))))
(action-accept-buffer
(lambda (x)
(setq flyspell-correct-ivy--result
(cons 'buffer (flyspell-correct-ivy--process-input candidates word x)))))
(action-skip-word
(lambda (x)
(setq flyspell-correct-ivy--result
(cons 'skip (flyspell-correct-ivy--process-input candidates word x)))))
(action-stop
(lambda (x)
(setq flyspell-correct-ivy--result
(cons 'stop (flyspell-correct-ivy--process-input candidates word x)))))
(action `(1
("o" ,action-default "correct")
("s" ,action-save-word "Save")
("S" ,action-accept-session "Accept (session)")
("b" ,action-accept-buffer "Accept (buffer)")
("k" ,action-skip-word "Skip")
("p" ,action-stop "Stop"))))
(ivy-read (format "Suggestions for \"%s\" in dictionary \"%s\": "
word (or ispell-local-dictionary
ispell-dictionary
"Default"))
candidates
:action action
:keymap flyspell-correct-ivy-map
:caller 'flyspell-correct-ivy)
flyspell-correct-ivy--result))
(setq flyspell-correct-interface #'flyspell-correct-ivy)
(provide 'flyspell-correct-ivy)
;;; flyspell-correct-ivy.el ends here