Spotlight: Flycheck, a Flymake replacement
Today’s spotlight is Flycheck, an excellent drop-in replacement for Flymake.
If you’re a regular Emacs user you may have heard of Flymake, a built-in package in Emacs that adds you as-you-type syntax checking and error highlighting. It’s been a part of Emacs for a long time (more than ten years) and, while it is useful, it is starting to show its age. Thankfully, there is a drop-in replacement called Flycheck by Sebastian Wiesner, and this article is here to convince you to switch to Flycheck.
Both Flymake and Flycheck use the same, simple, principle of calling out to an external tool – be it a linter, compiler or interpreter – with a file copy of the active buffer you’re editing, parsing the resulting errors and warnings, and highlighting them in your buffer.
So why switch away from Flymake? Well, for one thing, its limited support, out-of-the-box, for most programming languages means you’ll end up writing your Flymake handlers for each language or tool that you use. For most people, especially people who are not intimately familiar with elisp, it is a daunting task.
Flycheck works with most major and minor programming languages and environments – both C and C++, with or without Clang, for instance – out-of-the-box. For most Emacs users it’s easy to download and install and have it up and running in minutes. You simply invoke
M-x flycheck-mode in a buffer, or globally with
M-x global-flycheck-mode. Another benefit of Flycheck is a wider array of third-party plugins to Flycheck – available on your local Emacs package repository – if your favorite language lacks support.
Switching to Flycheck was extremely easy for me. One moment I was using my own duct taped Flymake handlers, and the next I was simply using Flychecks’. Very seamless.
Another major advantage is speed. Flycheck is much quicker and more efficient, and you can control when syntax checking takes place (by customizing with
M-x customize-group flycheck.)
Flycheck also has its own keybinding,
C-c ! by default, with a handful of utility commands you may find useful:
|Describe a Flycheck Checker|
|Compile using checker|
|Copy error point is on to kill ring|
|Clear all highlights from buffer|
|Report Flycheck version|
|Start syntax checking current buffer|
|Change Flycheck executable|
|Open Flycheck info manual|
|List all Flycheck errors|
|Jump to next error|
|Jump to previous error|
|Change Flycheck checker|
|Verifies the Flycheck checker works|
|Disable Flycheck checker in buffer|
Of particular note is
C-c ! v as a diagnostic tool. When you run the command Flycheck will display diagnostic information about the active checker in your current buffer. Useful if you have
$PATH issues or are missing the checker entirely on your system.
C-c ! C-c is also handy. Instead of checking the buffer source code you can also instruct Flycheck to compile the file; whether that works or not depends entirely on your programming language and the checker used.
Another major benefit is the ease of which you can add your own checkers. Using an elisp macro called
flycheck-define-checker you can construct a checker with just a few arguments and Flycheck will handle the rest. I particularly like the use of LISP macros’ pattern matching and
rx instead of the far more unreadable and brittle string-based regular expressions Flymake used.
Take a look at the XML linter. It’s very easy to understand and modify:
(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))
All in all, Flycheck is a solid improvement over the original Flymake, and if you are not using it already I encourage you to download it right now.