4

I want to load flyspell-mode and execute flyspell-buffer automatically after emacs loaded a tex file. I tried the following which loads flyspell-mode but doesn't seem to execute flyspell-buffer. What would be the proper way to to this? How do I have to change it if I want it on every file not just on LaTeX-files?

(setq ispell-program-name "hunspell")
(setq ispell-local-dictionary "de_DE")
(setq ispell-local-dictionary-alist
      '(("de_DE" "[[:alpha:]]" "[^[:alpha:]]" "[']" nil nil nil utf-8)
    ("en_US" "[[:alpha:]]" "[^[:alpha:]]" "[']" nil nil nil utf-8)
    ))


(add-hook 'LaTeX-mode-hook 'turn-on-flyspell)
(add-hook 'LaTeX-mode-hook 'flyspell-buffer)
student
  • 1,099
  • 9
  • 29
  • 1
    I have similar problems, flyspell checks for errors but asynchronously (it does not immediately flag errors but only if I scroll to a nearby location).

    Maybe you can try one from the following to see if it helps: find-file-hooks, after-save-hook, before-save-hook, and auto-save-hook?

    I have the following but it is often very slow: (add-hook 'before-save-hook #'flyspell-buffer).

    – Swarnendu Biswas Feb 10 '16 at 17:36
  • @SwarnenduBiswas What does the # mean in this context? – student Feb 10 '16 at 17:47
  • @student See http://emacs.stackexchange.com/questions/3595/when-to-sharp-quote-a-lambda-expression – JeanPierre Feb 10 '16 at 19:28
  • @student This post explains why is it beneficial to sharp-quote symbols that are functions http://endlessparentheses.com/get-in-the-habit-of-using-sharp-quote.html. – Swarnendu Biswas Feb 11 '16 at 17:15

1 Answers1

3

It suffices to properly choose and trickily chain hooks.

(add-hook 'text-mode-hook #'flyspell-mode)
(add-hook 'flyspell-mode-hook #'flyspell-local-vars)
(defun flyspell-local-vars ()
  (add-hook 'hack-local-variables-hook #'flyspell-buffer))

(add-hook 'flyspell-mode-hook #'flyspell-buffer) would do the job, but it would use the default dictionary.

vitaminace33
  • 236
  • 1
  • 6
  • The last add-hook should be local otherwise flyspell-buffer will be run on all buffer you open after opening a text-mode buffer. IOW it should be (add-hook 'hack-local-variables #'flyspell-buffer nil 'local). – Stefan Apr 25 '20 at 11:34