Skip to content

Latest commit

 

History

History
199 lines (171 loc) · 6.54 KB

starter-kit-flycheck.org

File metadata and controls

199 lines (171 loc) · 6.54 KB

Starter Kit Flycheck

Starter Kit Flycheck

Preferences for flycheck

;; (with-eval-after-load 'flycheck
;;   (setq flycheck-display-errors-function
;;         #'flycheck-pos-tip-error-messages))
(with-eval-after-load 'flycheck
  (setq flycheck-display-errors-function
        #'flycheck-display-error-messages))
(setq flycheck-display-errors-delay 0.5)

Only check automatically after saving

  • Don’t always check buffer, it’s too slow.
  • For languages such as Fortran, it’s almost impossible to check syntax correctly without any settings for Compiler (including flycheck-checker and flycheck options). Further, these settings differs from buffer to buffer which means that the ONLY way to set Compiler settings correctly is to use .dir-locals. rather than write in ~/.emacs.d/init.el or ~/.emacs. However, the sequences of loading .dir-locals and enabling flycheck-mode which is defined in f90-mode-hook are unclear. That is, flycheck may has checked syntax of the buffer before loading .dir-locals with incorrect settings. Many errors and warning will occur after find-file. So time will be wasted at fixing non-existing errors.
  • If you want to check syntax immediately after find-file, add flycheck-buffer to ~/.dir-locals after setting other options.
(setq flycheck-check-syntax-automatically '(save))

Fortran

You may work on a large project and has complex dependencies, where module files may locate here and there. In these cases, gfortran can’t find these module files and flycheck will always prompt errors like No such file or directory. The solution is to add a .dir-locals.el at current path and add codes:

((f90-mode . ((flycheck-gfortran-include-path . ("path0" "other/path")))))
(setq flycheck-fortran+-remove-temp-after-check t)
(defun my-flycheck-f90-setup ()
  (when (and
         (buffer-file-name)
         (not (file-remote-p (buffer-file-name))))
    (setq flycheck-fortran+-language-standard 'f08)
    (flycheck-select-checker flycheck-Fortran-checker)
    (flycheck-mode)))
(with-eval-after-load 'f90
  (require 'flycheck-fortran+)
  (add-hook 'f90-mode-hook 'my-flycheck-f90-setup))

(defun my-flycheck-f77-setup ()
  (when (and
         (buffer-file-name)
         (not (file-remote-p (buffer-file-name))))
    (setq flycheck-gfortran-language-standard 'f77)
    (flycheck-select-checker flycheck-Fortran-checker)
    (flycheck-mode)))
(with-eval-after-load 'fortran
  (require 'flycheck-fortran+)
  (add-hook 'fortran-mode-hook 'my-flycheck-f77-setup))

Python

Select checker for that flake8 is not always available.

(defun my-flycheck-python-setup ()
  (when (and
         (buffer-file-name)
         (not (file-remote-p (buffer-file-name)))
         (equal 'python-mode major-mode)
         (not
          (member (file-name-nondirectory (buffer-file-name))
                  '("SConstruct" "SConscript"))))
    (flycheck-select-checker my-flycheck-py-checker)
    (setq-local flycheck-check-syntax-automatically '(mode-enabled save))
    (flycheck-mode)))

(eval-after-load "python"
  `(progn
     (require 'flycheck)
     (setq my-flycheck-py-checker
           (cond ((executable-find "flake8") 'python-flake8)
                 (t 'python-pycompile)))
     (add-hook 'python-mode-hook 'my-flycheck-python-setup)))

Set max line length to be 80:

(setq flycheck-flake8-maximum-line-length 80)

You can install flake8 with its dependencies without direct Internet connection this way:

  1. Download flake8 and its packages pip install flake8 –download=/path/to/download.
  2. Move all the packages where you want.
  3. Install the packages in a dependent sequence pip install /path/to/package.

C

  • Clang is very good at auto completion but doesn’t support OpenMP as well as GCC.
  • Clang always can’t find the default include paths. Maybe the author is not familiar with Clang.
  • Use GCC as the default checker.
(defun my-flycheck-c-setup ()
  (when (and
         (eq major-mode 'c-mode)
         my-flycheck-gcc-found
         (buffer-file-name)
         (not (file-remote-p (buffer-file-name))))
    (flycheck-select-checker 'c/c++-gcc)
    (flycheck-mode)))

(defun my-flycheck-c++-setup ()
  (when (and
         (eq major-mode 'c++-mode)
         my-flycheck-gcc-found
         (buffer-file-name)
         (not (file-remote-p (buffer-file-name))))
    (flycheck-select-checker 'c/c++-gcc)
    (setq-local flycheck-gcc-language-standard "c++11")
    (flycheck-mode)))

(with-eval-after-load "cc-mode"
  (require 'flycheck)
  (setq my-flycheck-gcc-found (executable-find "gcc"))
  (add-hook 'c-mode-hook 'my-flycheck-c-setup)
  (add-hook 'c++-mode-hook 'my-flycheck-c++-setup))

xml

Original flycheck doesn’t support sgml-mode.

(defun my-flycheck-xml-setup ()
  (when (and
         (buffer-file-name)
         (not (file-remote-p (buffer-file-name))))
    (flycheck-mode)
    (setq-local flycheck-highlighting-mode nil)
    (setq-local flycheck-display-errors-function
                #'flycheck-display-error-messages)))
(with-eval-after-load 'sgml-mode
  (require 'flycheck)

  (flycheck-define-checker xml-xmllint
    "A XML syntax checker and validator using the xmllint utility.

The xmllint is part of libxml2, see URL
`http://www.xmlsoft.org/'."
    :command ("xmllint" "--noout" source)
    :error-patterns
    ((error line-start (file-name) ":" line ": " (message) line-end))
    :modes (xml-mode nxml-mode sgml-mode))

  (add-hook 'sgml-mode-hook 'my-flycheck-xml-setup))

shell script

(with-eval-after-load 'sh-script
  (require 'flycheck)
  (add-hook 'sh-mode-hook 'my-flycheck-sh-setup))

(defun my-flycheck-sh-setup ()
  (when (and
         (buffer-file-name)
         (not (file-remote-p (buffer-file-name))))
    (flycheck-mode)))

LaTeX

Use chktex or lacheck to check syntax of LaTeX.

(defun my-flycheck-latex-setup ()
  (when (and (buffer-file-name)
             (not (file-remote-p (buffer-file-name)))
             (or (executable-find "chktex")
                 (executable-find "lacheck")))
    (flycheck-mode)))

(with-eval-after-load "auctex.el"
  (require 'flycheck)
  (add-hook 'LaTeX-mode-hook 'my-flycheck-latex-setup))