Add unattended spell-checking to checkdoc

This commit makes checkdoc capable of spell-checking even when the
user isn't using it interactively.  When TAKE-NOTES is non-nil,
checkdoc will run spell-checking (with ispell) and report spelling
mistakes.

Fixes: (bug#38583).

* lisp/textmodes/ispell.el (ispell-word): Extract part of it to
`ispell--run-on-word`.
(ispell--run-on-word): New function, extracted from `ispell-word`.
(ispell-error-checking-word): New function.
(ispell-correct-p): New function.  Use `ispell--run-on-word` and
`ispell-error-checking-word`.
* lisp/emacs-lisp/checkdoc.el (checkdoc-current-buffer): Pass
TAKE-NOTES to `checkdoc-start`.
(checkdoc-continue): Pass TAKE-NOTES to `checkdoc-this-string-valid`.
(checkdoc-this-string-valid): Add optional argument TAKE-NOTES and
pass it to `checkdoc-this-string-valid-engine`.
(checkdoc-this-string-valid-engine): Add optional argument TAKE-NOTES
and pass it to `checkdoc-ispell-docstring-engine`.
(checkdoc-ispell-init): Call `ispell-set-spellchecker-params` and
`ispell-accept-buffer-local-defs`.  These calls are required to
properly use ispell.  The problem went unnoticed until now because
checkdoc was only using ispell through the high-level command
`ispell-word` which takes care of all the initialization for the user.
(checkdoc-ispell-docstring-engine): Add optional argument TAKE-NOTES
to force reporting of spell-checking errors.  Throw error
when (checkdoc-ispell-init) fails configuring ispell.  Replace a
few (if cond nil body) with (unless cond body). Replace (let ((var
nil))) with (let (var)).  Replace (if (not (eq checkdoc-autofix-flag
'never)) body) with just body because `checkdoc-autofix-flag` is
checked at the beginning of the function.

(cherry picked from commit 25adbc4a5e)
This commit is contained in:
Damien Cassou 2019-12-27 15:35:52 +01:00 committed by Eli Zaretskii
parent 4453acbdc9
commit 6338f69102
2 changed files with 106 additions and 62 deletions

View file

@ -1951,18 +1951,7 @@ quit spell session exited."
(or quietly
(message "Checking spelling of %s..."
(funcall ispell-format-word-function word)))
(ispell-send-string "%\n") ; put in verbose mode
(ispell-send-string (concat "^" word "\n"))
;; wait until ispell has processed word
(while (progn
(ispell-accept-output)
(not (string= "" (car ispell-filter)))))
;;(ispell-send-string "!\n") ;back to terse mode.
(setq ispell-filter (cdr ispell-filter)) ; remove extra \n
(if (and ispell-filter (listp ispell-filter))
(if (> (length ispell-filter) 1)
(error "Ispell and its process have different character maps")
(setq poss (ispell-parse-output (car ispell-filter)))))
(setq poss (ispell--run-on-word word))
(cond ((eq poss t)
(or quietly
(message "%s is correct"
@ -2024,6 +2013,43 @@ quit spell session exited."
(goto-char cursor-location) ; return to original location
replace))))
(defun ispell--run-on-word (word)
"Run ispell on WORD."
(ispell-send-string "%\n") ; put in verbose mode
(ispell-send-string (concat "^" word "\n"))
;; wait until ispell has processed word
(while (progn
(ispell-accept-output)
(not (string= "" (car ispell-filter)))))
(setq ispell-filter (cdr ispell-filter))
(when (and ispell-filter (listp ispell-filter))
(if (> (length ispell-filter) 1)
(error "Ispell and its processs have different character maps: %s" ispell-filter)
(ispell-parse-output (car ispell-filter)))))
(defun ispell-error-checking-word (word)
"Return a string describing that checking for WORD failed."
(format "Error checking word %s using %s with %s dictionary"
(funcall ispell-format-word-function word)
(file-name-nondirectory ispell-program-name)
(or ispell-current-dictionary "default")))
(defun ispell-correct-p (&optional following)
"Return t if the word at point is correct. Nil otherwise.
If optional argument FOLLOWING is non-nil then the following
word (rather than preceding) is checked when the cursor is not
over a word."
(save-excursion
;; reset ispell-filter so it only contains the result of
;; spell-checking the current-word:
(setq ispell-filter nil)
(let* ((word-and-boundaries (ispell-get-word following))
(word (car word-and-boundaries))
(poss (ispell--run-on-word word)))
(unless poss (error (ispell-error-checking-word word)))
(or (eq poss t)
(stringp poss)))))
(defun ispell-get-word (following &optional extra-otherchars)
"Return the word for spell-checking according to ispell syntax.