This manual is for GNU Flymake (version 0.3, April 2004), which is a universal on-the-fly syntax checker for GNU Emacs.

Copyright © 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.

     Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, with the Front-Cover texts being ``A GNU Manual,'' and with the Back-Cover Texts as in (a) below.  A copy of the license is included in the section entitled ``GNU Free Documentation License'' in the Emacs manual.

     (a) The FSF's Back-Cover Text is: ``You have freedom to copy and modify this GNU Manual, like GNU software.  Copies published by the Free Software Foundation raise funds for GNU development.''

     This document is part of a collection distributed under the GNU Free Documentation License.  If you want to distribute this document separately from the collection, you can do so by adding a copy of the license to the document, as described in section 6 of the license.

GNU Flymake
***********

Overview
********

Flymake is a universal on-the-fly syntax checker implemented as an Emacs minor mode. Flymake runs the pre-configured syntax check tool (compiler for C++ files, perl for perl files, etc.) in the background, passing it a temporary copy of the current buffer, and parses the output for known error/warning message patterns. Flymake then highlights erroneous lines (i.e. lines for which at least one error or warning has been reported by the syntax check tool), and displays an overall buffer status in the mode line. Status information displayed by Flymake contains total number of errors and warnings reported for the buffer during the last syntax check.

flymake-goto-next-error and flymake-goto-prev-error functions allow for easy navigation to the next/previous erroneous line, respectively.

Calling flymake-display-err-menu-for-current-line will popup a menu containing error messages reported by the syntax check tool for the current line. Errors/warnings belonging to another file, such as a .h header file included by a .c file, are shown in the current buffer as belonging to the first line. Menu items for such messages also contain a filename and a line number. Selecting such a menu item will automatically open the file and jump to the line with error.

Syntax check is done 'on-the-fly'. It is started whenever

  * buffer is loaded

  * a newline character is added to the buffer

  * some changes were made to the buffer more than 0.5 seconds ago (the delay is configurable).

Flymake is a universal syntax checker in the sense that it's easily extended to support new syntax check tools and error message patterns. See Configuring Flymake.

Installing
**********

Flymake is packaged in a single file, flymake.el.

To install/update Flymake, place flymake.el to a directory somewhere on Emacs load path. You might also want to byte-compile flymake.el to improve performance.

Also, place the following line in the .emacs file.

     (require 'flymake)

You might also map the most frequently used Flymake functions, such as flymake-goto-next-error, to some keyboard shortcuts:

     (global-set-key [f3] 'flymake-display-err-menu-for-current-line)
     (global-set-key [f4] 'flymake-goto-next-error)

Using Flymake
*************

Flymake mode
============

Flymake is an Emacs minor mode. To use Flymake, you must first activate flymake-mode by using the flymake-mode function.

Instead of manually activating flymake-mode, you can configure Flymake to automatically enable flymake-mode upon opening any file for which syntax check is possible. To do so, place the following line in .emacs:

     (add-hook 'find-file-hook 'flymake-find-file-hook)

Running the syntax check
========================

When flymake-mode is active, syntax check is started automatically on any of the three conditions mentioned above. Syntax check can also be started manually by using the flymake-start-syntax-check-for-current-buffer function. This can be used, for example, when changes were made to some other buffer affecting the current buffer.

Navigating to error lines
=========================

After syntax check is completed, lines for which at least one error or warning has been reported are highlighted, and total number of errors and warning is shown in the mode line. Use the following functions to navigate the highlighted lines.

flymake-goto-next-error
Moves point to the next erroneous line, if any.

flymake-goto-prev-error
Moves point to the previous erroneous line.

These functions treat erroneous lines as a linked list. Therefore, flymake-goto-next-error will go to the first erroneous line when invoked in the end of the buffer.

Viewing error messages
======================

To view error messages belonging to the current line, use the flymake-display-err-menu-for-current-line function. If there's at least one error or warning reported for the current line, this function will display a popup menu with error/warning texts. Selecting the menu item whose error belongs to another file brings forward that file with the help of the flymake-goto-file-and-line function.

Syntax check statuses
=====================

After syntax check is finished, its status is displayed in the mode line. The following statuses are defined.

Flymake* or Flymake:E/W*
Flymake is currently running. For the second case, E/W contains the
error and warning count for the previous run.

Flymake
Syntax check is not running. Usually this means syntax check was
successfully passed (no errors, no warnings). Other possibilities are:
syntax check was killed as a result of executing
flymake-compile, or syntax check cannot start as compilation
is currently in progress.

Flymake:E/W
Number of errors/warnings found by the syntax check process.

Flymake:!
Flymake was unable to find master file for the current buffer.

The following errors cause a warning message and switch flymake mode OFF for the buffer.

CFGERR
Syntax check process returned nonzero exit code, but no
errors/warnings were reported. This indicates a possible configuration
error (for example, no suitable error message patterns for the
syntax check tool).

NOMASTER
Flymake was unable to find master file for the current buffer.

NOMK
Flymake was unable to find a suitable buildfile for the current buffer.

PROCERR
Flymake was unable to launch a syntax check process.

Troubleshooting
===============

Flymake uses a simple logging facility for indicating important points in the control flow. The logging facility sends logging messages to the *Messages* buffer. The information logged can be used for resolving various problems related to Flymake.

Logging output is controlled by the flymake-log-level variable. 3 is the  most verbose level, and -1 switches logging off.

Configuring and Extending Flymake
*********************************

Flymake was designed to be easily extended for supporting new syntax check tools and error message patterns.

Customizable variables
======================

This section summarizes variables used for Flymake configuration.

flymake-log-level
     Controls logging output, see Troubleshooting.

flymake-allowed-file-name-masks
     A list of (filename-regexp, init-function, cleanup-function getfname-function) for configuring syntax check tools. See Adding support for a new syntax check tool.

flymake-buildfile-dirs
     A list of directories (relative paths) for searching a buildfile. See Locating the buildfile.

flymake-master-file-dirs
     A list of directories for searching a master file. See Locating a master file.

flymake-get-project-include-dirs-function
     A function used for obtaining a list of project include dirs (C/C++ specific). See Getting the include directories.

flymake-master-file-count-limit
flymake-check-file-limit
     Used when looking for a master file. See Locating a master file.

flymake-err-line-patterns
     Patterns for error/warning messages in the form (regexp file-idx line-idx col-idx err-text-idx). See Parsing the output.

flymake-compilation-prevents-syntax-check
     A flag indicating whether compilation and syntax check of the same file cannot be run simultaneously.

flymake-no-changes-timeout
     If any changes are made to the buffer, syntax check is automatically started after flymake-no-changes-timeout seconds.

flymake-gui-warnings-enabled
     A boolean flag indicating whether Flymake will show message boxes for non-recoverable errors. If flymake-gui-warnings-enabled is nil, these errors will only be logged to the *Messages* buffer.

flymake-start-syntax-check-on-newline
     A boolean flag indicating whether to start syntax check after a newline character is added to the buffer.

flymake-errline
     A custom face for highlighting lines for which at least one error has been reported.

flymake-warnline
     A custom face for highlighting lines for which at least one warning and no errors have been reported.

Adding support for a new syntax check tool
==========================================

Syntax check tools are configured using the flymake-allowed-file-name-masks list. Each item of this list has the following format:

     (filename-regexp, init-function, cleanup-function, getfname-function)

filename-regexp
     This field is used as a key for locating init/cleanup/getfname functions for the buffer.  Items in flymake-allowed-file-name-masks are searched sequentially. The first item with filename-regexp matching buffer filename is selected. If no match is found, flymake-mode is switched off.

init-function
     init-function is required to initialize the syntax check, usually by creating a temporary copy of the buffer contents. The function must return (list cmd-name arg-list). If init-function returns null, syntax check is aborted, by flymake-mode is not switched off.

cleanup-function
     cleanup-function is called after the syntax check process is complete and should take care of proper deinitialization, which is usually deleting a temporary copy created by the init-function.

getfname-function
     This function is used for translating filenames reported by the syntax check tool into ``real'' filenames. Filenames reported by the tool will be different from the real ones, as actually the tool works with the temporary copy.  In most cases, the default implementation provided by Flymake, flymake-get-real-file-name, can be used as getfname-function.

To add support for a new syntax check tool, write corresponding init-function, and, optionally cleanup-function and getfname-function.  If the format of error messages reported by the new tool is not yet supported by Flymake, add a new entry to the flymake-err-line-patterns list.

The following sections contain some examples of configuring Flymake support for various syntax check tools.

Example -- Configuring a tool called directly
---------------------------------------------

In this example, we will add support for perl as a syntax check tool. perl supports the -c option which does syntax checking.

First, we write the init-function:

     (defun flymake-perl-init ()
       (let* ((temp-file (flymake-init-create-temp-buffer-copy
                          'flymake-create-temp-inplace))
     	 (local-file (file-relative-name
                           temp-file
                           (file-name-directory buffer-file-name))))
         (list "perl" (list "-wc " local-file))))

flymake-perl-init creates a temporary copy of the buffer contents with the help of flymake-init-create-temp-buffer-copy, and builds an appropriate command line.

Next, we add a new entry to the flymake-allowed-file-name-masks:

     (setq flymake-allowed-file-name-masks
           (cons '(".+\\.pl$"
                   flymake-perl-init
                   flymake-simple-cleanup
                   flymake-get-real-file-name)
                 flymake-allowed-file-name-masks))

Note that we use standard cleanup-function and getfname-function.

Finally, we add an entry to flymake-err-line-patterns:

     (setq flymake-err-line-patterns
           (cons '("\\(.*\\) at \\([^ \n]+\\) line \\([0-9]+\\)[,.\n]"
                   2 3 nil 1)
                 flymake-err-line-patterns))

Example -- Configuring a tool called via make
---------------------------------------------

In this example we will add support for C files syntax checked by gcc called via make.

We're not required to write any new functions, as Flymake already has functions for make. We just add a new entry to the flymake-allowed-file-name-masks:

     (setq flymake-allowed-file-name-masks
           (cons '(".+\\.c$"
                   flymake-simple-make-init
                   flymake-simple-cleanup
                   flymake-get-real-file-name)
                 flymake-allowed-file-name-masks))

flymake-simple-make-init builds the following make command line:

     (list "make"
           (list "-s" "-C"
                 base-dir
                 (concat "CHK_SOURCES=" source)
                 "SYNTAX_CHECK_MODE=1"
                 "check-syntax"))

base-dir is a directory containing Makefile, see Locating the buildfile.

Thus, Makefile must contain the check-syntax target. In our case this target might look like this:

check-syntax:
	gcc -o nul -S ${CHK_SOURCES}

The format of error messages reported by gcc is already supported by Flymake, so we don't have to add a new entry to flymake-err-line-patterns.

Flymake Implementation
**********************

Syntax check is started by calling flymake-start-syntax-check-for-current-buffer. Flymake first determines whether it is able to do syntax check. It then saves a copy of the buffer in a temporary file in the buffer's directory (or in the system temp directory -- for java files), creates a syntax check command and launches a process with this command. The output is parsed using a list of error message patterns, and error information (file name, line number, type and text) is saved. After the process has finished, Flymake highlights erroneous lines in the buffer using the accumulated error information.

Determining whether syntax check is possible
============================================

Syntax check is considered possible if there's an entry in flymake-allowed-file-name-masks matching buffer's filename and its init-function returns non-nil value.

Two syntax check modes are distinguished:

 1. Buffer can be syntax checked in a standalone fashion, that is, the file (its temporary copy, in fact) can be passed over to the compiler to do the syntax check. Examples are C/C++ (.c, .cpp) and Java (.java) sources.

 2. Buffer can be syntax checked, but additional file, called master file, is required to perform this operation. A master file is a file that includes the current file, so that running a syntax check tool on it will also check syntax in the current file. Examples are C/C++ (.h, .hpp) headers.

These modes are handled inside init/cleanup/getfname functions, see Adding support for a new syntax check tool.

Flymake contains implementations of all functionality required to support different syntax check modes described above (making temporary copies, finding master files, etc.), as well as some tool-specific (routines for make, Ant, etc.) code.

Making a temporary copy
=======================

After the possibility of the syntax check has been determined, a temporary copy of the current buffer is made so that the most recent unsaved changes could be seen by the syntax check tool. Making a copy is quite straightforward in a standalone case (mode 1), as it's just saving buffer contents to a temporary file.

Things get trickier, however, when master file is involved, as it requires to

  * locate a master file

  * patch it to include the current file using its new (temporary) name.

Locating a master file is discussed in the following section.

Patching just changes all appropriate lines of the master file so that they use the new (temporary) name of the current file. For example, suppose current file name is file.h, the master file is file.cpp, and it includes current file via #include "file.h". Current file's copy is saved to file file_flymake.h, so the include line must be changed to #include "file_flymake.h". Finally, patched master file is saved to file_flymake_master.cpp, and the last one is passed to the syntax check tool.

Locating a master file
======================

Master file is located in two steps.

First, a list of possible master files is built. A simple name matching is used to find the files. For a C++ header file.h, Flymake searches for all .cpp files in the directories whose relative paths are stored in a customizable variable flymake-master-file-dirs, which usually contains something like ("." "./src"). No more than flymake-master-file-count-limit entries is added to the master file list. The list is then sorted to move files with names file.cpp to the top.

Next, each master file in a list is checked to contain the appropriate include directives. No more than flymake-check-file-limit of each file are parsed.

For file.h, the include directives to look for are #include "file.h", #include "../file.h", etc. Each include is checked against a list of include directories (see Getting the include directories) to be sure it points to the correct file.h.

First matching master file found stops the search. The master file is then patched and saved to disk. In case no master file is found, syntax check is aborted, and corresponding status (!) is reported in the mode line.

Getting the include directories
===============================

Two sets of include directories are distinguished: system include directories and project include directories. The former is just the contents of the INCLUDE environment variable. The latter is not so easy to obtain, and the way it can be obtained can vary greatly for different projects. Therefore, a customizable variable flymake-get-project-include-dirs-function is used to provide the way to implement the desired behavior.

The default implementation, flymake-get-project-include-dirs-imp, uses a make call. This requires a correct base directory, that is, a directory containing a correct Makefile, to be determined.

As obtaining the project include directories might be a costly operation, its return value is cached in the hash table. The cache is cleared in the beginning of every syntax check attempt.

Locating the buildfile
======================

Flymake can be configured to use different tools for performing syntax checks. For example, it can use direct compiler call to syntax check a perl script or a call to make for a more complicated case of a C/C++ source. The general idea is that simple files, like perl scripts and html pages, can be checked by directly invoking a corresponding tool. Files that are usually more complex and generally used as part of larger projects, might require non-trivial options to be passed to the syntax check tool, like include directories for C++. The latter files are syntax checked using some build tool, like make or Ant.

All make configuration data is usually stored in a file called Makefile. To allow for future extensions, flymake uses a notion of buildfile to reference the 'project configuration' file.

Special function, flymake-find-buildfile is provided for locating buildfiles. Searching for a buildfile is done in a manner similar to that of searching for possible master files. A customizable variable flymake-buildfile-dirs holds a list of relative paths to the buildfile. They are checked sequentially until a buildfile is found. In case there's no build file, syntax check is aborted.

Buildfile values are also cached.

Starting the syntax check process
=================================

The command line (command name and the list of arguments) for launching a process is returned by the initialization function. Flymake then just calls start-process to start an asynchronous process and configures process filter and sentinel which is used for processing the output of the syntax check tool.

Parsing the output
==================

The output generated by the syntax check tool is parsed in the process filter/sentinel using the error message patterns stored in the flymake-err-line-patterns variable. This variable contains a list of items of the form (regexp file-idx line-idx err-text-idx), used to determine whether a particular line is an error message and extract file name, line number and error text, respectively. Error type (error/warning) is also guessed by matching error text with the '^[wW]arning' pattern. Anything that was not classified as a warning is considered an error. Type is then used to sort error menu items, which shows error messages first.

Flymake is also able to interpret error message patterns missing err-text-idx information. This is done by merely taking the rest of the matched line ((substring line (match-end 0))) as error text. This trick allows to make use of a huge collection of error message line patterns from compile.el. All these error patterns are appended to the end of flymake-err-line-patterns.

The error information obtained is saved in a buffer local variable. The buffer for which the process output belongs is determined from the process-id->buffer mapping updated after every process launch/exit.

Highlighting erroneous lines
============================

Highlighting is implemented with overlays and happens in the process sentinel, after calling the cleanup function. Two customizable faces are used: flymake-errline and flymake-warnline.  Errors belonging outside the current buffer are considered to belong to line 1 of the current buffer.

Interaction with other modes
============================

The only mode flymake currently knows about is compile.

Flymake can be configured to not start syntax check if it thinks the compilation is in progress. The check is made by the flymake-compilation-is-running, which tests the compilation-in-progress variable. The reason why this might be useful is saving CPU time in case both syntax check and compilation are very CPU intensive. The original reason for adding this feature, though, was working around a locking problem with MS Visual C++ compiler.

Flymake also provides an alternative command for starting compilation, flymake-compile:

     (defun flymake-compile ()
       "Kill all flymake syntax checks then start compilation."
       (interactive)
       (flymake-stop-all-syntax-checks)
       (call-interactively 'compile))

It just kills all the active syntax check processes before calling compile.

GNU Free Documentation License
******************************

     [file doclicense.texi not included]