Skip to content

Commit

Permalink
refactor(spell-fu): minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
abougouffa committed Nov 19, 2023
1 parent 82df4cc commit 37e3bfd
Showing 1 changed file with 15 additions and 33 deletions.
48 changes: 15 additions & 33 deletions modules/extras/me-spell-fu.el
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,12 @@
(add-to-list 'ispell-buffer-session-localwords word)
(or ispell-buffer-local-name ; session localwords might conflict
(setq ispell-buffer-local-name (buffer-name)))
(if (null ispell-pdict-modified-p)
(setq ispell-pdict-modified-p
(list ispell-pdict-modified-p)))
(when (null ispell-pdict-modified-p) (setq ispell-pdict-modified-p (list nil)))
(goto-char orig-pt)
(if (eq replace 'buffer)
(ispell-add-per-file-word-list word)))
(when (eq replace 'buffer) (ispell-add-per-file-word-list word)))
(replace
(let ((new-word (if (atom replace)
replace
(car replace)))
(orig-pt (+ (- (length word) (- end start))
orig-pt)))
(let ((new-word (if (atom replace) replace (car replace)))
(orig-pt (+ (- (length word) (- end start)) orig-pt)))
(unless (equal new-word (car poss))
(delete-region start end)
(goto-char start)
Expand All @@ -51,53 +45,41 @@
;; spell-fu fails to initialize correctly if it can't find aspell or a similar
;; program. We want to signal the error, not tell the user that every word is
;; spelled correctly.
(unless (or (and ispell-really-aspell ispell-program-name)
(executable-find "aspell"))
(unless (or (and ispell-really-aspell ispell-program-name) (executable-find "aspell"))
(user-error "Aspell is required for spell checking"))

(ispell-set-spellchecker-params)
(save-current-buffer
(ispell-accept-buffer-local-defs))
(save-current-buffer (ispell-accept-buffer-local-defs))
(if (not (featurep 'vertico))
(call-interactively #'ispell-word)
(cl-destructuring-bind (start . end)
(or (bounds-of-thing-at-point 'word)
(user-error "No word at point"))
(or (bounds-of-thing-at-point 'word) (user-error "No word at point"))
(let ((word (thing-at-point 'word t))
(orig-pt (point))
poss ispell-filter)
(ispell-send-string "%\n")
(ispell-send-string (concat "^" word "\n"))
(while (progn (accept-process-output ispell-process)
(not (string= "" (car ispell-filter)))))
;; Remove leading empty element
(setq ispell-filter (cdr ispell-filter))
;; ispell process should return something after word is sent. Tag word as
;; valid (i.e., skip) otherwise
(unless ispell-filter
(setq ispell-filter '(*)))
(when (consp ispell-filter)
(setq poss (ispell-parse-output (car ispell-filter))))
(setq ispell-filter (cdr ispell-filter)) ; Remove leading empty element
;; ispell process should return something after word is sent. Tag word as valid (i.e., skip) otherwise
(unless ispell-filter (setq ispell-filter '(*)))
(when (consp ispell-filter) (setq poss (ispell-parse-output (car ispell-filter))))
(cond
((or (eq poss t) (stringp poss))
;; don't correct word
((or (eq poss t) (stringp poss)) ; don't correct word
(message "%s is correct" (funcall ispell-format-word-function word))
t)
((null poss)
;; ispell error
((null poss) ; ispell error
(error "Ispell: error in Ispell process"))
(t
;; The word is incorrect, we have to propose a replacement.
(t ; The word is incorrect, we have to propose a replacement
(setq res (completing-read (format "Corrections for %S: " word) (nth 2 poss)))
(unless res (setq res (cons 'break word)))
(cond
((stringp res)
(+spell-fu--correct res poss word orig-pt start end))
((let ((cmd (car res))
(wrd (cdr res)))
(unless (or (eq cmd 'skip)
(eq cmd 'break)
(eq cmd 'stop))
(unless (memq cmd '(skip break stop))
(+spell-fu--correct cmd poss wrd orig-pt start end)
(unless (string-equal wrd word)
(+spell-fu--correct wrd poss word orig-pt start end))))))
Expand Down

0 comments on commit 37e3bfd

Please sign in to comment.