Emacs: flymake package

This article will describe using flymake package which provides dynamic syntax checker. flaymake is builtin package and needs Makefile.


1 Install flymake-cursor package

The flymake-cursor package outputs flymake's error and warning on mini buffer. Install flymake-cursor package with M-x package-list-packages.

flymake-cursor  1.0.2  installed  Show flymake messages in the minibuffer after delay

2 ${HOME}/.emacs

Load flymake with require.

(require 'flymake)

Change colors of flymake's warning and error.

  '(flymake-errline ((((class color)) (:background "red"))))
  '(flymake-warnline ((((class color)) (:background "red")))))

flymake does not check header files by default. Add header files to check list.

(push '("\\.h\\'" flymake-simple-make-init flymake-master-cleanup)

Add flymake-mode to major mode. This article adds flymake-mode to c++-mode-hook.

(add-hook 'c++-mode-hook
             (flymake-mode t)
             (flymake-cursor-mode t)
             (setq flymake-check-was-interrupted t)
             (local-set-key "\C-c\C-v" 'flymake-start-syntax-check)
             (local-set-key "\C-c\C-p" 'flymake-goto-prev-error)
             (local-set-key "\C-c\C-n" 'flymake-goto-next-error)

This will output error when move cursor to error location.

(defun flymake-show-help ()
  (when (get-char-property (point) 'flymake-overlay)
    (let ((help (get-char-property (point) 'help-echo)))
      (if help (message "%s" help)))))
(add-hook 'post-command-hook 'flymake-show-help)

2.1 Makefile

flymake needs Makefile which defines check-syntax target. Add include path to CFLAGS.

  $(CC) $(CFLAGS) -Wall -Wextra -fsyntax-only $(CHK_SOURCES)

In case of C++, use $(CXX) and $(CXXFLAGS).

3 key bindings

This article defines the following key bindings in ${HOME}/.emacs. The syntax check is run dynamically and run manually by C-c C-v.

C-c C-v Run syntax check
C-c C-p Move previous error and warning
C-c C-n Move next error and warning

4 Execution result

bar is not static function but is not called. Arguments of main is not used.