0

In my init.el I have

(add-hook 'text-mode-hook 'flyspell-mode)
(add-hook 'prog-mode-hook 'flyspell-prog-mode)
(add-hook 'flyspell-mode-hook 'flyspell-buffer)

but when I find a prog file, buffer isn't spellchachek (only when the cursor go to the word it is).

I would like to spellcheck all comments and strings with flyspell when opening prog files, thanks.

1 Answer 1

1

You need to hook flyspell-buffer to flyspell-prog-mode-hook

(defun my--do-flyspell-prog-mode (buffer)
  (when (buffer-live-p buffer)
    (with-current-buffer buffer
      (flyspell-prog-mode)
      (flyspell-buffer))))

(defun my--schedule-flyspell-prog-mode ()
  (run-with-idle-timer 0 nil #'my--do-flyspell-prog-mode (current-buffer)))

(add-hook 'prog-mode-hook 'my--schedule-flyspell-prog-mode)
      

Just in case anyone is looking for a menu for flyspell ...

  1. Copy the below snippet to a file, say my-flyspell-menu.el
  2. Do M-x load-file my-flyspell-menu.el
  3. Now you will get an entry in the Menu Bar that says My Flyspell
  4. There you can find an entry for M-x flyspell-buffer.

Menu for flyspell library Menu for flyspell-mode


(require 'flyspell)

;;; Menu for Keymaps

;;;; Menu for Keymap `flyspell-mode-map'

(easy-menu-define my-flyspell-mode-map-menu flyspell-mode-map "Menu for Flyspell Mode Map."
  '("My Flyspell Mode Map"
    ("Uncategorized"
     [" Flyspell Correct Word Before Point" flyspell-correct-word-before-point :help "(flyspell-correct-word-before-point &optional EVENT OPOINT)\n\nPop up a menu of possible corrections for misspelled word before point.\nIf EVENT is non-nil, it is the mouse event that invoked this operation;\nthat controls where to put the menu.\nIf OPOINT is non-nil, restore point there after adjusting it for replacement."]
     [" Flyspell Auto Correct Word" flyspell-auto-correct-word :help "(flyspell-auto-correct-word)\n\nCorrect the current word.\nThis command proposes various successive corrections for the\ncurrent word.  If invoked repeatedly on the same position, it\ncycles through the possible corrections of the current word.\n\nSee `flyspell-get-word' for details of how this finds the word to\nspell-check."]
     [" Flyspell Goto Next Error" flyspell-goto-next-error :help "(flyspell-goto-next-error &optional PREVIOUS)\n\nGo to the next error.\nIf PREVIOUS (interactively, the prefix), go to the previous error\ninstead.\n\nIn general FLYSPELL-GOTO-NEXT-ERROR must be used after\nFLYSPELL-BUFFER."]
     [" Flyspell Auto Correct Previous Word" flyspell-auto-correct-previous-word :help "(flyspell-auto-correct-previous-word POSITION)\n\nAuto correct the first misspelled word that occurs before point.\nBut don't look beyond what's visible on the screen."])))


;;;; Menu for Keymap `flyspell-mouse-map'

(easy-menu-define my-flyspell-mouse-map-menu flyspell-mouse-map "Menu for Flyspell Mouse Map."
  '("My Flyspell Mouse Map"
    ("Uncategorized"
     [" Flyspell Correct Word" flyspell-correct-word :help "(flyspell-correct-word EVENT)\n\nPop up a menu of possible corrections for a misspelled word.\nThe word checked is the word at the mouse position."])))
;;; Menu for Libraries

;;;; Menu for Library `flyspell'

(when nil
  (with-eval-after-load "flyspell"
    (easy-menu-define my-flyspell-menu global-map "Menu for Flyspell."
      '("My Flyspell"
        ("Minor Mode P"
         ["     Flyspell Prog Mode" flyspell-prog-mode :style toggle :selected flyspell-prog-mode :help "(flyspell-prog-mode)\n\nTurn on `flyspell-mode' for comments and strings."]
         ["     Flyspell Mode" flyspell-mode :style toggle :selected flyspell-mode :help "(flyspell-mode &optional ARG)\n\nToggle on-the-fly spell checking (Flyspell mode).\n\nFlyspell mode is a buffer-local minor mode.  When enabled, it\nspawns a single Ispell process and checks each word.  The default\nflyspell behavior is to highlight incorrect words.\n\nThis mode is geared toward text modes.  In buffers that contain\ncode, `flyspell-prog-mode' is usually a better choice.\n\nBindings:\n\\[ispell-word]: correct words (using Ispell).\n\\[flyspell-auto-correct-word]: automatically correct word.\n\\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.\n\\[flyspell-correct-word] (or down-mouse-2): popup correct words.\n\nHooks:\nThis runs `flyspell-mode-hook' after flyspell mode is entered or exit.\n\nRemark:\n`flyspell-mode' uses `ispell-mode'.  Thus all Ispell options are\nvalid.  For instance, a different dictionary can be used by\ninvoking `ispell-change-dictionary'.\n\nConsider using the `ispell-parser' to check your text.  For instance\nconsider adding:\n(add-hook \\='tex-mode-hook (lambda () (setq ispell-parser \\='tex)))\nin your init file.\n\n\\[flyspell-region] checks all words inside a region.\n\\[flyspell-buffer] checks the whole buffer.\n\nThis is a minor mode.  If called interactively, toggle the\n`Flyspell mode' mode.  If the prefix argument is positive, enable\nthe mode, and if it is zero or negative, disable the mode.\n\nIf called from Lisp, toggle the mode if ARG is `toggle'.  Enable\nthe mode if ARG is nil, omitted, or is a positive number.\nDisable the mode if ARG is a negative number.\n\nTo check whether the minor mode is enabled in the current buffer,\nevaluate `flyspell-mode'.\n\nThe mode's hook is called both when the mode is enabled and when\nit is disabled."])
        ("Uncategorized"
         ["     Flyspell Auto Correct Previous Hook" flyspell-auto-correct-previous-hook :help "(flyspell-auto-correct-previous-hook)\n\nHook to track successive calls to `flyspell-auto-correct-previous-word'.\nSets `flyspell-auto-correct-previous-pos' to nil."]
         ["     Flyspell Auto Correct Previous Word" flyspell-auto-correct-previous-word :help "(flyspell-auto-correct-previous-word POSITION)\n\nAuto correct the first misspelled word that occurs before point.\nBut don't look beyond what's visible on the screen."]
         ["     Flyspell Auto Correct Word" flyspell-auto-correct-word :help "(flyspell-auto-correct-word)\n\nCorrect the current word.\nThis command proposes various successive corrections for the\ncurrent word.  If invoked repeatedly on the same position, it\ncycles through the possible corrections of the current word.\n\nSee `flyspell-get-word' for details of how this finds the word to\nspell-check."]
         ["     Flyspell Buffer" flyspell-buffer :help "(flyspell-buffer)\n\nFlyspell whole buffer."]
         ["     Flyspell Check Previous Highlighted Word" flyspell-check-previous-highlighted-word :help "(flyspell-check-previous-highlighted-word &optional ARG)\n\nCorrect the closest previous word that is highlighted as misspelled.\nThis function scans for a word which starts before point that has been\nhighlighted by Flyspell as misspelled.  If it finds one, it proposes\na replacement for that word.  With prefix arg N, check the Nth word\nbefore point that's highlighted as misspelled."]
         ["     Flyspell Correct Word" flyspell-correct-word :help "(flyspell-correct-word EVENT)\n\nPop up a menu of possible corrections for a misspelled word.\nThe word checked is the word at the mouse position."]
         ["     Flyspell Correct Word Before Point" flyspell-correct-word-before-point :help "(flyspell-correct-word-before-point &optional EVENT OPOINT)\n\nPop up a menu of possible corrections for misspelled word before point.\nIf EVENT is non-nil, it is the mouse event that invoked this operation;\nthat controls where to put the menu.\nIf OPOINT is non-nil, restore point there after adjusting it for replacement."]
         ["     Flyspell Delay Command" flyspell-delay-command :help "(flyspell-delay-command COMMAND)\n\nSet COMMAND to be delayed, for Flyspell.\nWhen flyspell `post-command-hook' is invoked because a delayed command\nhas been used, the current word is not immediately checked.\nIt will be checked only after `flyspell-delay' seconds."]
         ["     Flyspell Deplacement Command" flyspell-deplacement-command :help "(flyspell-deplacement-command COMMAND)\n\nSet COMMAND that implement cursor movements, for Flyspell.\nWhen flyspell `post-command-hook' is invoked because a deplacement command\nhas been used, the current word is not checked."]
         ["     Flyspell Goto Next Error" flyspell-goto-next-error :help "(flyspell-goto-next-error &optional PREVIOUS)\n\nGo to the next error.\nIf PREVIOUS (interactively, the prefix), go to the previous error\ninstead.\n\nIn general FLYSPELL-GOTO-NEXT-ERROR must be used after\nFLYSPELL-BUFFER."]
         ["     Flyspell Post Command Hook" flyspell-post-command-hook :help "(flyspell-post-command-hook)\n\nThe `post-command-hook' used by flyspell to check a word on-the-fly."]
         ["     Flyspell Pre Command Hook" flyspell-pre-command-hook :help "(flyspell-pre-command-hook)\n\nSave the current buffer and point for Flyspell's post-command hook."]
         ["     Flyspell Region" flyspell-region :help "(flyspell-region BEG END)\n\nFlyspell text between BEG and END.\n\nMake sure `flyspell-mode' is turned on if you want the highlight\nof a misspelled word removed when you've corrected it."]
         ["     Flyspell Word" flyspell-word :help "(flyspell-word &optional FOLLOWING KNOWN-MISSPELLING)\n\nSpell check a word.\nIf the optional argument FOLLOWING, or, when called interactively\n`ispell-following-word', is non-nil, checks the following (rather\nthan preceding) word when the cursor is not over a word.  If\noptional argument KNOWN-MISSPELLING is non-nil considers word a\nmisspelling and skips redundant spell-checking step.\n\nSee `flyspell-get-word' for details of how this finds the word to\nspell-check."])
        ("Flyspell Mode Map"
         ["     Flyspell Correct Word Before Point" flyspell-correct-word-before-point :help "(flyspell-correct-word-before-point &optional EVENT OPOINT)\n\nPop up a menu of possible corrections for misspelled word before point.\nIf EVENT is non-nil, it is the mouse event that invoked this operation;\nthat controls where to put the menu.\nIf OPOINT is non-nil, restore point there after adjusting it for replacement."]
         ["     Flyspell Auto Correct Word" flyspell-auto-correct-word :help "(flyspell-auto-correct-word)\n\nCorrect the current word.\nThis command proposes various successive corrections for the\ncurrent word.  If invoked repeatedly on the same position, it\ncycles through the possible corrections of the current word.\n\nSee `flyspell-get-word' for details of how this finds the word to\nspell-check."]
         ["     Flyspell Goto Next Error" flyspell-goto-next-error :help "(flyspell-goto-next-error &optional PREVIOUS)\n\nGo to the next error.\nIf PREVIOUS (interactively, the prefix), go to the previous error\ninstead.\n\nIn general FLYSPELL-GOTO-NEXT-ERROR must be used after\nFLYSPELL-BUFFER."]
         ["     Flyspell Auto Correct Previous Word" flyspell-auto-correct-previous-word :help "(flyspell-auto-correct-previous-word POSITION)\n\nAuto correct the first misspelled word that occurs before point.\nBut don't look beyond what's visible on the screen."])
        ("Flyspell Mouse Map"
         ["     Flyspell Correct Word" flyspell-correct-word :help "(flyspell-correct-word EVENT)\n\nPop up a menu of possible corrections for a misspelled word.\nThe word checked is the word at the mouse position."])))))


;;; Menu for Package `("flyspell")'

(easy-menu-define my-flyspell-menu global-map "Menu for Flyspell."
  '("My Flyspell"
    ("Flyspell Mode Map"
     [" Flyspell Correct Word Before Point" flyspell-correct-word-before-point :help "(flyspell-correct-word-before-point &optional EVENT OPOINT)\n\nPop up a menu of possible corrections for misspelled word before point.\nIf EVENT is non-nil, it is the mouse event that invoked this operation;\nthat controls where to put the menu.\nIf OPOINT is non-nil, restore point there after adjusting it for replacement."]
     [" Flyspell Auto Correct Word" flyspell-auto-correct-word :help "(flyspell-auto-correct-word)\n\nCorrect the current word.\nThis command proposes various successive corrections for the\ncurrent word.  If invoked repeatedly on the same position, it\ncycles through the possible corrections of the current word.\n\nSee `flyspell-get-word' for details of how this finds the word to\nspell-check."]
     [" Flyspell Goto Next Error" flyspell-goto-next-error :help "(flyspell-goto-next-error &optional PREVIOUS)\n\nGo to the next error.\nIf PREVIOUS (interactively, the prefix), go to the previous error\ninstead.\n\nIn general FLYSPELL-GOTO-NEXT-ERROR must be used after\nFLYSPELL-BUFFER."]
     [" Flyspell Auto Correct Previous Word" flyspell-auto-correct-previous-word :help "(flyspell-auto-correct-previous-word POSITION)\n\nAuto correct the first misspelled word that occurs before point.\nBut don't look beyond what's visible on the screen."])
    ("Flyspell Mouse Map"
     [" Flyspell Correct Word" flyspell-correct-word :help "(flyspell-correct-word EVENT)\n\nPop up a menu of possible corrections for a misspelled word.\nThe word checked is the word at the mouse position."])
    ("Minor Mode P"
     [" Flyspell Prog Mode" flyspell-prog-mode :style toggle :selected flyspell-prog-mode :help "(flyspell-prog-mode)\n\nTurn on `flyspell-mode' for comments and strings."]
     [" Flyspell Mode" flyspell-mode :style toggle :selected flyspell-mode :help "(flyspell-mode &optional ARG)\n\nToggle on-the-fly spell checking (Flyspell mode).\n\nFlyspell mode is a buffer-local minor mode.  When enabled, it\nspawns a single Ispell process and checks each word.  The default\nflyspell behavior is to highlight incorrect words.\n\nThis mode is geared toward text modes.  In buffers that contain\ncode, `flyspell-prog-mode' is usually a better choice.\n\nBindings:\n\\[ispell-word]: correct words (using Ispell).\n\\[flyspell-auto-correct-word]: automatically correct word.\n\\[flyspell-auto-correct-previous-word]: automatically correct the last misspelled word.\n\\[flyspell-correct-word] (or down-mouse-2): popup correct words.\n\nHooks:\nThis runs `flyspell-mode-hook' after flyspell mode is entered or exit.\n\nRemark:\n`flyspell-mode' uses `ispell-mode'.  Thus all Ispell options are\nvalid.  For instance, a different dictionary can be used by\ninvoking `ispell-change-dictionary'.\n\nConsider using the `ispell-parser' to check your text.  For instance\nconsider adding:\n(add-hook \\='tex-mode-hook (lambda () (setq ispell-parser \\='tex)))\nin your init file.\n\n\\[flyspell-region] checks all words inside a region.\n\\[flyspell-buffer] checks the whole buffer.\n\nThis is a minor mode.  If called interactively, toggle the\n`Flyspell mode' mode.  If the prefix argument is positive, enable\nthe mode, and if it is zero or negative, disable the mode.\n\nIf called from Lisp, toggle the mode if ARG is `toggle'.  Enable\nthe mode if ARG is nil, omitted, or is a positive number.\nDisable the mode if ARG is a negative number.\n\nTo check whether the minor mode is enabled in the current buffer,\nevaluate `flyspell-mode'.\n\nThe mode's hook is called both when the mode is enabled and when\nit is disabled."])
    ("Flyspell"
     [" Flyspell Auto Correct Previous Hook" flyspell-auto-correct-previous-hook :help "(flyspell-auto-correct-previous-hook)\n\nHook to track successive calls to `flyspell-auto-correct-previous-word'.\nSets `flyspell-auto-correct-previous-pos' to nil."]
     [" Flyspell Buffer" flyspell-buffer :help "(flyspell-buffer)\n\nFlyspell whole buffer."]
     [" Flyspell Check Previous Highlighted Word" flyspell-check-previous-highlighted-word :help "(flyspell-check-previous-highlighted-word &optional ARG)\n\nCorrect the closest previous word that is highlighted as misspelled.\nThis function scans for a word which starts before point that has been\nhighlighted by Flyspell as misspelled.  If it finds one, it proposes\na replacement for that word.  With prefix arg N, check the Nth word\nbefore point that's highlighted as misspelled."]
     [" Flyspell Delay Command" flyspell-delay-command :help "(flyspell-delay-command COMMAND)\n\nSet COMMAND to be delayed, for Flyspell.\nWhen flyspell `post-command-hook' is invoked because a delayed command\nhas been used, the current word is not immediately checked.\nIt will be checked only after `flyspell-delay' seconds."]
     [" Flyspell Deplacement Command" flyspell-deplacement-command :help "(flyspell-deplacement-command COMMAND)\n\nSet COMMAND that implement cursor movements, for Flyspell.\nWhen flyspell `post-command-hook' is invoked because a deplacement command\nhas been used, the current word is not checked."]
     [" Flyspell Post Command Hook" flyspell-post-command-hook :help "(flyspell-post-command-hook)\n\nThe `post-command-hook' used by flyspell to check a word on-the-fly."]
     [" Flyspell Pre Command Hook" flyspell-pre-command-hook :help "(flyspell-pre-command-hook)\n\nSave the current buffer and point for Flyspell's post-command hook."]
     [" Flyspell Region" flyspell-region :help "(flyspell-region BEG END)\n\nFlyspell text between BEG and END.\n\nMake sure `flyspell-mode' is turned on if you want the highlight\nof a misspelled word removed when you've corrected it."]
     [" Flyspell Word" flyspell-word :help "(flyspell-word &optional FOLLOWING KNOWN-MISSPELLING)\n\nSpell check a word.\nIf the optional argument FOLLOWING, or, when called interactively\n`ispell-following-word', is non-nil, checks the following (rather\nthan preceding) word when the cursor is not over a word.  If\noptional argument KNOWN-MISSPELLING is non-nil considers word a\nmisspelling and skips redundant spell-checking step.\n\nSee `flyspell-get-word' for details of how this finds the word to\nspell-check."])))


(provide 'my-flyspell-menu)


3
  • with (add-hook 'flyspell-prog-mode-hook 'flyspell-buffer) the hook is correct but flyspell-buffer not make the words underlined, only if i call from M-x it does. Sep 22 at 10:46
  • I have reworked the recipe emacs.stackexchange.com/a/73771/31220. It works on my end. Try it and let me know. Sep 22 at 13:46
  • I solved with a simply (add-hook 'flyspell-prog-mode-hook (lambda () (run-with-idle-timer 0 nil 'flyspell-buffer))) Sep 22 at 18:13

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.