4

When using ispell to check spelling and encountering an incorrect word, pressing r takes you to the minibuffer where you can enter a replacement for the misspelled word. By default the minibuffer already contains the incorrectly spelled word and so I need to clear the minibuffer first before being able to type in the replacement.

How do I add a hook to clear the input prompt automatically?

  • 1
    I see I can edit the source ispell.el and change line 2358 (read-string "Replacement for: " word nil) to (read-string "Replacement for: " nil nil) which is a bit of a hack, but does work. Having a limited knowledge of emacs and ispell, I would prefer something I can stick in my .emacs file :) – YoungGrandpa Oct 25 '14 at 13:32
3

Honestly, I don't know if this is a good way to solve this issue, but it works (though fragile) and it shows a method that is applicable in other case.

Test case (since the Young Grandfather (tYG) forgot to post one):

(with-temp-buffer
  (setq ispell-program-name "hunspell")
  (switch-to-buffer (current-buffer))
  (insert "speling")
  (ispell-word))

You see what tYG talks about by evaluating and clicking r. (Leave out the hunspell part if that's better for you).

Since ispell-command-loop is very complicated, it's not easy to mess with its internals. But we can use a fragile advice around it, see (info "(elisp) Advising Functions"). Note that we are using Stefan Monnier's new, hip and super-nice nadvice.el package!

(defun rasmus/ispell-loop-get-rid-of-misspeled-word (orig-fun &rest args)
  "Around-advice to avoid the misspeled word when typing r in ispell."
  (require 'cl)
  (cl-letf* (((symbol-function #'read-string*) (symbol-function #'read-string))
     ((symbol-function #'read-string)
      (lambda (prompt &optional initial-input history default-value
             inherit-input-method)
        (if (equal prompt "Replacement for: ")
        (read-string* prompt)
          (read-string* prompt initial-input history default-value
                inherit-input-method)))))
(apply orig-fun args)))

(advice-add 'ispell-command-loop :around
    #'rasmus/ispell-loop-get-rid-of-misspeled-word)

Run the test again. The misspelled word should be gone.

Bonus: do C-h f ispell-command-loop C-j. It should contain a line like :around advice: `rasmus/ispell-loop-get-rid-of-misspeled-word'.

  • I'd edit your answer quickly if I were at a computer; misspeled is misspelled :) – Kaushal Modi Oct 26 '14 at 1:00
  • Kaushlmodi, it's a homage to speling.org. In the context it works better, no!? – rasmus Oct 26 '14 at 1:22
  • Excellent thanks very much will be using. Just did a copy and paste, all is working. Will get round to figuring out what you did at some other stage ... – YoungGrandpa Oct 26 '14 at 8:20
2

@rasmus provided a nice programmatic solution. Here's another solution that is more interactive - it emulates what you manually do to clear the minibuffer when it's displayed with initial input:

(add-hook 'minibuffer-setup-hook
          (lambda ()
            (when (equal (minibuffer-prompt) "Replacement for: ")
              (delete-minibuffer-contents))))
  • That's certainly nice and simple! r(z)grep suggests that the string/prompt is unique (disregarding site-lisp), so I think it's pretty "safe" from false positives as well. – rasmus Oct 27 '14 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.