Skip to content

Commit

Permalink
stop action for leaving the point at misspelled word
Browse files Browse the repository at this point in the history
Required for #58 and #69
  • Loading branch information
d12frosted committed Feb 15, 2020
1 parent f6a641e commit 747e1a9
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.org
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
when any of these functions is called with point at misspelled word.
- Rewrite all tests using [[https://github.com/jorgenschaefer/emacs-buttercup][buttercup]] library.
- Allow to correct and save the word in one pass (see #66).
- New action - =stop= allowing to leave the point at the misspelled word.

* v0.6.1

Expand Down
3 changes: 2 additions & 1 deletion flyspell-correct-avy-menu.el
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ of (command, word) to be used by `flyspell-do-correct'."
(base-menu (let ((save `(("Save word" (save . ,word))
("Accept (session)" (session . ,word))
("Accept (buffer)" (buffer . ,word))
("Skip" (skip . ,word)))))
("Skip" (skip . ,word))
("Stop" (stop . ,word)))))
(if (consp cor-menu)
(append cor-menu (cons "" save))
save)))
Expand Down
4 changes: 3 additions & 1 deletion flyspell-correct-helm.el
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@
(cons (format "Accept (buffer) \"%s\"" word)
(cons 'buffer word))
(cons (format "Skip \"%s\"" word)
(cons 'skip word)))))
(cons 'skip word))
(cons (format "Stop at \"%s\"" word)
(cons 'stop word)))))
(unless (string= helm-pattern "")
(setq opts
(append opts
Expand Down
4 changes: 3 additions & 1 deletion flyspell-correct-ivy.el
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@ of (command, word) to be used by `flyspell-do-correct'."
(action-accept-session (lambda (_) (setq result (cons 'session word))))
(action-accept-buffer (lambda (_) (setq result (cons 'buffer word))))
(action-skip-word (lambda (_) (setq result (cons 'skip word))))
(action-stop (lambda (_) (setq result (cons 'stop word))))
(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"))))
("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
Expand Down
29 changes: 20 additions & 9 deletions flyspell-correct.el
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ Adapted from `flyspell-correct-word-before-point'."
(let ((cmd (car res))
(wrd (cdr res)))
(unless (or (eq cmd 'skip)
(eq cmd 'break))
(eq cmd 'break)
(eq cmd 'stop))
(flyspell-do-correct
cmd poss wrd cursor-location start end opoint)
(unless (string-equal wrd word)
Expand Down Expand Up @@ -260,7 +261,8 @@ until all errors in buffer have been addressed."
;; `flyspell-correct-word-before-point'.
(interactive "d")
(let ((original-pos (point))
(hard-reset-pos))
(target-pos (point))
(hard-move-point))
(save-excursion
(let ((incorrect-word-pos))

Expand Down Expand Up @@ -296,26 +298,35 @@ until all errors in buffer have been addressed."
;; But with rapid mode, `hard-reset-pos' will be set to nil
;; eventually. Which gives more predictable point location in
;; general.
(setq hard-reset-pos
(setq hard-move-point
(and (>= original-pos (overlay-start overlay))
(<= original-pos (overlay-end overlay))))

;; Correct a word using `flyspell-correct-at-point'.
(let ((res (flyspell-correct-at-point)))
(when res
;; stop at misspelled word
(when (eq (car-safe res) 'stop)
(setq target-pos incorrect-word-pos
hard-move-point t))

;; break from rapid mode
(when (or (not rapid)
(eq (car-safe res) 'break)
(eq (car-safe res) 'stop))
(setq overlay nil))

;; push mark
(when (or (not (mark t))
(/= (mark t) (point)))
(push-mark (point) t))
(when (or (not rapid)
(eq (car-safe res) 'break))
(setq overlay nil)))))))
(push-mark (point) t)))))))

(when incorrect-word-pos
(goto-char incorrect-word-pos)
(forward-word)
(when (= (mark t) (point)) (pop-mark)))))
(when hard-reset-pos
(goto-char original-pos))))
(when hard-move-point
(goto-char target-pos))))

;;; Overlays

Expand Down
84 changes: 81 additions & 3 deletions test/test-flyspell-correct.el
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ License for most of our software; it applies also to any other
werk released this way by its authors. You can apply it to your
programs, too." ,@body))

(defmacro with-mistakes|stop|cursor-before (&rest body)
"Execute BODY in temporary buffer that has a mistake.
Cursor is placed somewhere before the misspelled word."
`(with-flyspell-buffer
"The licenses for most software and other practical works are
designed to take away your freedom to share and change the works.
By contrast, the GNU Generel Public License |is intended to
guarantee your freedom to share and change all †versiuns of a
program--to make sure it remains free software for all its users.
We, the Free Software Foundation, use the GNU General Public
License for most of our software; it applies also to any other
werk released this way by its authors. You can apply it to your
programs, too." ,@body))

(defmacro with-mistakes|cursor-inside (&rest body)
"Execute BODY in temporary buffer that has a mistake.
Expand Down Expand Up @@ -144,6 +159,21 @@ Public License for most of our software; it applies also to any
other werk released this way by its authors. You can apply it to
your programs, too." ,@body))

(defmacro with-mistakes|stop|cursor-after (&rest body)
"Execute BODY in temporary buffer that has a mistake.
Cursor is placed somewhere after the misspelled word."
`(with-flyspell-buffer
"The licenses for most software and other practical works are
designed to take away your freedom to share and change the works.
By contrast, the GNU Generel Public License is intended to
guarantee your freedom to share and change all †versiuns of a
program--to make sure it remains free software for all| its
users. We, the Free Software Foundation, use the GNU General
Public License for most of our software; it applies also to any
other werk released this way by its authors. You can apply it to
your programs, too." ,@body))

(defmacro with-mistakes|cursor-after-all (&rest body)
"Execute BODY in temporary buffer that has a mistake.
Expand Down Expand Up @@ -366,6 +396,19 @@ Simply passed WORD to `correct-word' mock."

(expect-no-correction "versiuns"))))

(describe "action - stop"

(before-each
(spy-on 'flyspell-do-correct)
(spy-on 'correct-interface :and-call-through)
(spy-on 'correct-word :and-call-fake (lambda (word) (cons 'stop word))))

(it "stop at the beginning of misspelled word"
(with-mistakes|stop|cursor-before
(flyspell-correct-next (point))

(expect-no-correction "versiuns"))))

(describe "action - fix"

(before-each
Expand Down Expand Up @@ -445,6 +488,19 @@ Simply passed WORD to `correct-word' mock."

(expect-no-correction "versiuns"))))

(describe "action - stop"

(before-each
(spy-on 'flyspell-do-correct)
(spy-on 'correct-interface :and-call-through)
(spy-on 'correct-word :and-call-fake (lambda (word) (cons 'stop word))))

(it "stop at the beginning of misspelled word"
(with-mistakes|stop|cursor-after
(flyspell-correct-previous (point))

(expect-no-correction "versiuns"))))

(describe "action - fix"

(before-each
Expand Down Expand Up @@ -528,9 +584,9 @@ Simply passed WORD to `correct-word' mock."
(describe "break"

(before-each
(spy-on 'correct-interface :and-call-through)
;; imitate C-g
(spy-on 'correct-word :and-return-value nil))
(spy-on 'correct-interface :and-call-through)
;; imitate C-g
(spy-on 'correct-word :and-return-value nil))

(it "call correct interface only once with backward direction"
(with-mistakes|cursor-after
Expand All @@ -542,6 +598,28 @@ Simply passed WORD to `correct-word' mock."

(it "call correct interface only once with forward direction"
(with-mistakes|cursor-before
(let ((current-prefix-arg '(64)))
(flyspell-correct-wrapper)

(expect 'correct-interface :to-have-been-called-times 1)
(expect 'correct-word :to-have-been-called-with "versiuns")))))

(describe "stop"

(before-each
(spy-on 'correct-interface :and-call-through)
(spy-on 'correct-word :and-call-fake (lambda (word) (cons 'stop word))))

(it "stop at the first misspelled word with backward direction"
(with-mistakes|stop|cursor-after
(let ((current-prefix-arg '(4)))
(flyspell-correct-wrapper)

(expect 'correct-interface :to-have-been-called-times 1)
(expect 'correct-word :to-have-been-called-with "versiuns"))))

(it "stop at the first misspelled word with forward direction"
(with-mistakes|stop|cursor-before
(let ((current-prefix-arg '(64)))
(flyspell-correct-wrapper)

Expand Down

0 comments on commit 747e1a9

Please sign in to comment.