Spelling fixes
[emacs.git] / doc / misc / flymake.texi
blob99ab1271ac9ff3129a0ea8f236e8c3dc206a2b5f
1 \input texinfo   @c -*-texinfo; coding: utf-8 -*-
2 @comment %**start of header
3 @setfilename ../../info/flymake.info
4 @set VERSION 0.3
5 @set UPDATED April 2004
6 @settitle GNU Flymake @value{VERSION}
7 @include docstyle.texi
8 @syncodeindex pg cp
9 @syncodeindex vr cp
10 @syncodeindex fn cp
11 @comment %**end of header
13 @copying
14 This manual is for GNU Flymake (version @value{VERSION}, @value{UPDATED}),
15 which is a universal on-the-fly syntax checker for GNU Emacs.
17 Copyright @copyright{} 2004--2017 Free Software Foundation, Inc.
19 @quotation
20 Permission is granted to copy, distribute and/or modify this document
21 under the terms of the GNU Free Documentation License, Version 1.3 or
22 any later version published by the Free Software Foundation; with no
23 Invariant Sections, with the Front-Cover Texts being ``A GNU Manual,''
24 and with the Back-Cover Texts as in (a) below.  A copy of the license
25 is included in the section entitled ``GNU Free Documentation License''.
27 (a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
28 modify this GNU manual.''
29 @end quotation
30 @end copying
32 @dircategory Emacs misc features
33 @direntry
34 * Flymake: (flymake).           A universal on-the-fly syntax checker.
35 @end direntry
37 @titlepage
38 @title GNU Flymake
39 @subtitle for version @value{VERSION}, @value{UPDATED}
40 @author Pavel Kobiakov(@email{pk_at_work@@yahoo.com}) and João Távora.
41 @page
42 @vskip 0pt plus 1filll
43 @insertcopying
44 @end titlepage
46 @contents
48 @ifnottex
49 @node Top
50 @top GNU Flymake
51 @insertcopying
52 @end ifnottex
54 @menu
55 * Using Flymake::
56 * Extending Flymake::
57 * The legacy Proc backend::
58 * GNU Free Documentation License::
59 * Index::
60 @end menu
62 @node Using Flymake
63 @chapter Using Flymake
64 @cindex overview of flymake
65 @cindex using flymake
67 Flymake is a universal on-the-fly buffer checker implemented as an
68 Emacs minor minor.  To use Flymake, you must first activate
69 @code{flymake-mode} by using the command @kbd{flymake-mode}.
71 When enabled, Flymake collects information about problems in the
72 buffer, called @dfn{diagnostics}, from one or more different sources,
73 or @dfn{backends}, and then visually annotates the buffer by
74 highlighting problematic buffer regions with a special face.
76 It also displays an overall buffer status in the mode line containing
77 totals for different types of diagnostics.
79 Syntax check is done ``on-the-fly''.  It is started whenever
81 @itemize @bullet
82 @item
83 @code{flymake-mode} is started, unless
84 @code{flymake-start-on-flymake-mode} is nil;
86 @item
87 a newline character is added to the buffer, unless
88 @code{flymake-start-syntax-check-on-newline} is nil;
90 @item
91 some changes were made to the buffer more than @code{0.5} seconds ago
92 (the delay is configurable in @code{flymake-no-changes-timeout}).
93 @end itemize
95 Syntax check can also be started manually by typing the @kbd{M-x
96 flymake-start @key{RET}} command.
98 @code{flymake-goto-next-error} and @code{flymake-goto-prev-error} are
99 commands that allow easy navigation to the next/previous erroneous
100 line, respectively.  If might be a good idea to map them to @kbd{M-n}
101 and @kbd{M-p} in @code{flymake-mode}, by adding to your init file:
103 @lisp
104 (define-key flymake-mode-map (kbd "M-n") 'flymake-goto-next-error)
105 (define-key flymake-mode-map (kbd "M-p") 'flymake-goto-prev-error)
106 @end lisp
108 Flymake is a universal syntax checker in the sense that it's easily
109 extended to support new backends (@pxref{Extending Flymake}).
111 Historically, Flymake used to accept diagnostics from a single
112 backend, albeit a reasonably flexible one.
114 This backend isn't (yet) obsolete, and so is still available as a
115 fallback and active by default (@pxref{The legacy Proc backend}).  It works by
116 selecting a syntax check tool from a preconfigured list (compiler for
117 C@t{++} files, @command{perl} for Perl files, etc.), and executing it in the
118 background, passing it a temporary file which is a copy of the current
119 buffer, and parsing the output for known error/warning message
120 patterns.
122 @menu
123 * Syntax check statuses::
124 * Backend exceptions::
125 * Customizable variables::
126 @end menu
128 @node Syntax check statuses
129 @section Syntax check statuses
130 @cindex Syntax check statuses
132 When enabled, Flymake displays its status in the mode line, which
133 provides a visual summary of diagnostic collection.  It may also hint
134 at certain exceptional situations encountered when communicating with
135 backends.
137 The following statuses are defined:
139 @multitable @columnfractions 0.25 0.75
140 @item [@var{nerrors} @var{nwarnings} ...]
141 @tab Normal operation. @var{nerrors} and @var{nwarnings} are, respectively,
142 the total number of errors and warnings found during the last buffer
143 check, for all backends. They may be followed by other totals for
144 other types of diagnostics (@pxref{Flymake error types}).
146 @item @code{Wait}
147 @tab  Some Flymake backends haven't reported since the last time they
148 where questioned.  It is reasonable to assume that this is a temporary
149 delay and Flymake will resume normal operation soon.
151 @item @code{!}
152 @tab All the configured Flymake backends have disabled themselves: Flymake
153 cannot annotate the buffer and action from the user is needed to
154 investigate and remedy the situation (@pxref{Backend exceptions}).
156 @item @code{?}
157 @tab There are no applicable Flymake backends for this buffer, thus Flymake
158 cannot annotate it.  To fix this, a user may look to extending Flymake
159 and add a new backend (@pxref{Extending Flymake}).
161 @end multitable
163 @node Backend exceptions
164 @section Backend exceptions
165 @cindex backend exceptions
167 @cindex disabled backends
168 @cindex backends, disabled
169 Some backends may take longer than others to respond or complete, and
170 some may decide to @emph{disable} themselves if they are not suitable
171 for the current buffer or encounter some unavoidable problem.  A
172 disabled backend is not tried again for future checks of the current
173 buffer.
175 @findex flymake-reporting-backends
176 @findex flymake-running-backends
177 @findex flymake-disabled-backends
178 The commands @code{flymake-reporting-backends},
179 @code{flymake-running-backends} and @code{flymake-disabled-backends}
180 show the backends currently used and those which are disabled.
182 @cindex reset disabled backends
183 Toggling @code{flymake-mode} off and on again, or invoking
184 @code{flymake-start} with a prefix argument is one way to reset the
185 disabled backend list, so that they will be tried again in the next check.
187 @cindex logging
188 @cindex flymake logging
189 Flymake also uses a simple logging facility for indicating important
190 points in the control flow.  The logging facility sends logging
191 messages to the @file{*Flymake log*} buffer.  The information logged
192 can be used for resolving various problems related to Flymake.  For
193 convenience, a shortcut to this buffer can be found in Flymake's menu,
194 accessible from the top menu bar or just left of the status indicator.
196 @vindex warning-minimum-log-level
197 @vindex warning-minimum-level
198 Logging output is controlled by the Emacs @code{warning-minimum-log-level}
199 and @code{warning-minimum-level} variables.
201 @node Customizable variables
202 @section Customizable variables
203 @cindex customizable variables
204 @cindex variables for customizing flymake
206 This section summarizes customization variables used for the
207 configuration of the Flymake user interface.
209 @vtable @code
210 @item flymake-no-changes-timeout
211 If any changes are made to the buffer, syntax check is automatically
212 started after this many seconds, unless the user makes another change,
213 which resets the timer.
215 @item flymake-start-syntax-check-on-newline
216 A boolean flag indicating whether to start syntax check immediately
217 after a newline character is inserted into the buffer.
219 @item flymake-start-on-flymake-mode
220 A boolean flag indicating whether to start syntax check immediately
221 after enabling @code{flymake-mode}.
223 @item flymake-error
224 A custom face for highlighting regions for which an error has been
225 reported.
227 @item flymake-warning
228 A custom face for highlighting regions for which a warning has been
229 reported.
231 @item flymake-note
232 A custom face for highlighting regions for which a note has been
233 reported.
235 @item flymake-error-bitmap
236 A bitmap used in the fringe to mark lines for which an error has
237 been reported.
239 @item flymake-warning-bitmap
240 A bitmap used in the fringe to mark lines for which a warning has
241 been reported.
243 @item flymake-fringe-indicator-position
244 Which fringe (if any) should show the warning/error bitmaps.
246 @item flymake-wrap-around
247 If non-nil, moving to errors with @code{flymake-goto-next-error} and
248 @code{flymake-goto-prev-error} wraps around buffer boundaries.
249 @end vtable
251 @node Extending Flymake
252 @chapter Extending Flymake
253 @cindex extending flymake
255 Flymake can primarily be extended in one of two ways:
257 @enumerate
258 @item
259 By changing the look and feel of the annotations produced by the
260 different backends.
262 @item
263 By adding a new buffer-checking backend.
264 @end enumerate
266 The following sections discuss each approach in detail.
268 @menu
269 * Flymake error types::
270 * Backend functions::
271 @end menu
273 @node Flymake error types
274 @section Customizing Flymake error types
275 @cindex customizing error types
276 @cindex error types, customization
278 @vindex flymake-diagnostic-types-alist
279 The variable @code{flymake-diagnostic-types-alist} is looked up by
280 Flymake every time an annotation for a diagnostic is created in the
281 buffer.  Specifically, this variable holds a table of correspondence
282 between symbols designating diagnostic types and an additional
283 sub-table of properties pertaining to each diagnostic type.
285 Both tables are laid out in association list (@pxref{Association
286 Lists,,, elisp, The Emacs Lisp Reference Manual}) format, and thus can
287 be conveniently accessed with the functions of the @code{assoc}
288 family.
290 You can use any symbol-value association in the properties sub-table,
291 but some symbols have special meaning as to where and how Flymake
292 presents the diagnostic:
294 @itemize
296 @item
297 @cindex bitmap of diagnostic
298 @code{bitmap}, an image displayed in the fringe according to
299 @code{flymake-fringe-indicator-position}.  The value actually follows
300 the syntax of @code{flymake-error-bitmap} (@pxref{Customizable
301 variables}).  It is overridden by any @code{before-string} overlay
302 property.
304 @item
305 @cindex severity of diagnostic
306 @code{severity} is a non-negative integer specifying the diagnostic's
307 severity.  The higher the value, the more serious is the error.  If
308 the overlay property @code{priority} is not specified, @code{severity}
309 is used to set it and help sort overlapping overlays.
311 @item
312 Every property pertaining to overlays (@pxref{Overlay Properties,,,
313 elisp, The Emacs Lisp Reference Manual}), except @code{category} and
314 @code{evaporate}.  These properties are used to affect the appearance
315 of Flymake annotations.
317 As an example, here's how to make errors (diagnostics of the type
318 @code{:error}) stand out even more prominently in the buffer, by
319 raising the characters using a @code{display} overlay property.
321 @example
322 (push '(display . (raise 1.2))
323       (cdr (assoc :error flymake-diagnostic-types-alist)))
324 @end example
326 @item
327 @vindex flymake-category
328 @code{flymake-category} is a symbol whose property list is considered
329 the default for missing values of any other properties.
330 @end itemize
332 @cindex predefined diagnostic types
333 @vindex flymake-error
334 @vindex flymake-warning
335 @vindex flymake-note
336 Three default diagnostic types, @code{:error}, @code{:warning} and
337 @code{:note} are predefined in
338 @code{flymake-diagnostic-types-alist}.  By default each lists a single
339 @code{flymake-category} property whose value is, respectively, the
340 symbols @code{flymake-error}, @code{flymake-warning} and
341 @code{flymake-note}.
343 These category symbols' plists is where the values of customizable
344 variables and faces such as @code{flymake-error-bitmap} are found.
345 Thus, if you change their plists, Flymake may stop honoring these
346 user customizations.
348 The @code{flymake-category} special property is also especially useful
349 for backends which create diagnostics objects with non-default
350 types that differ from an existing type by only a few properties
351 (@pxref{Flymake utility functions}).
353 As an example, consider configuring a new diagnostic type
354 @code{:low-priority-note} that behaves much like the @code{:note}
355 priority but without an overlay face.
357 @example
358 (add-to-list
359  'flymake-diagnostic-types-alist
360  `(:low-priority-note . ((face . nil)
361                          (flymake-category . flymake-note))))
362 @end example
364 @vindex flymake-text
365 As you might have guessed, Flymake's annotations are implemented as
366 overlays (@pxref{Overlays,,, elisp, The Emacs Lisp Reference Manual}).
367 Along with the properties that you specify for the specific type of
368 diagnostic, Flymake adds the property @code{flymake-text} to these
369 overlays, and sets it to the message string that the backend used to
370 describe the diagnostic.
372 Since overlays also support arbitrary keymaps, you can use this
373 property @code{flymake-text} to create interactive annotations, such
374 as in the following example of binding a @kbd{mouse-3} event (middle
375 mouse button click) to an Internet search for the text of a
376 @code{:warning} or @code{:error}.
378 @example
379 (defun my-search-for-message (event)
380   (interactive "e")
381   (let ((ovs (overlays-at (posn-point (event-start event))))
382         ov)
383     ;; loop until flymake overlay we clicked on is recovered
384     (while (not (overlay-get (setq ov (pop ovs)) 'flymake-text)))
385     (when ov
386       (eww-browse-url
387        (concat "https://duckduckgo.com/?q="
388                (replace-regexp-in-string " "
389                                          "+"
390                                          (overlay-get ov 'flymake-text)))
391        t))))
393 (dolist (type '(:warning :error))
394   (let ((a (assoc type flymake-diagnostic-types-alist)))
395     (setf (cdr a)
396           (append `((mouse-face . highlight)
397                     (keymap . ,(let ((map (make-sparse-keymap)))
398                                  (define-key map [mouse-2]
399                                    'my-search-for-message)
400                                  map)))
401                   (cdr a)))))
402 @end example
404 @node Backend functions
405 @section Backend functions
406 @cindex backend functions
408 @vindex flymake-diagnostic-functions
409 Flymake backends are Lisp functions placed in the special hook
410 @code{flymake-diagnostic-functions}.
412 A backend's responsibility is to diagnose the contents of a buffer for
413 problems, registering the problem's positions, type, and summary
414 description.  This information is collected in the form of diagnostic
415 objects created by the function @code{flymake-make-diagnostic}
416 (@pxref{Flymake utility functions}), and
417 then handed over to Flymake, which proceeds to annotate the
418 buffer.
420 A request for a buffer check, and the subsequent delivery of
421 diagnostics, are two key events of the interaction between Flymake
422 and backend.  Each such event corresponds to a well-defined function
423 calling convention: one for calls made by Flymake into the backend via
424 the backend function, the other in the reverse direction via a
425 callback.  To be usable, backends must adhere to both.
427 Backend functions must accept an arbitrary number of arguments:
429 @itemize
430 @item
431 the first argument is always @var{report-fn}, a callback function
432 detailed below;
434 @item
435 the remaining arguments are keyword-value pairs of the
436 form @w{@code{(@var{:key} @var{value} @var{:key2} @var{value2}...)}}.  Currently,
437 Flymake provides no such arguments, but backend functions must be
438 prepared to accept (and possibly ignore) any number of them.
439 @end itemize
441 Whenever Flymake or the user decide to re-check the buffer, backend
442 functions are called as detailed above, and are expected to initiate
443 this check, but aren't in any way required to complete it before
444 exiting: if the computation involved is expensive, as
445 is often the case with large buffers, that slower task should be
446 scheduled for the future using asynchronous sub-processes
447 (@pxref{Asynchronous Processes,,, elisp, The Emacs Lisp reference
448 manual}) or other asynchronous mechanisms.
450 In any case, backend functions are expected to return quickly or
451 signal an error, in which case the backend is disabled
452 (@pxref{Backend exceptions}).
454 If the function returns, Flymake considers the backend to be
455 @dfn{running}.  If it has not done so already, the backend is expected
456 to call the function @var{report-fn} passed to it, at which point
457 Flymake considers the backend to be @dfn{reporting}.  Backends call
458 @var{report-fn} by passing it a single argument @var{report-action}
459 followed by an optional list of keyword-value pairs of the form
460 @w{@code{(@var{:report-key} @var{value} @var{:report-key2} @var{value2}...)}}.
462 Currently accepted values for @var{report-action} are:
464 @itemize
465 @item
466 A (possibly empty) list of diagnostic objects created by
467 @code{flymake-make-diagnostic}, causing Flymake to annotate the
468 buffer with this information.
470 A backend may call @var{report-fn} repeatedly in this manner, but only
471 until Flymake considers that the most recently requested buffer check
472 is now obsolete, because, say, buffer contents have changed in the
473 meantime.  The backend is only given notice of this via a renewed call
474 to the backend function.  Thus, to prevent making obsolete reports and
475 wasting resources, backend functions should first cancel any ongoing
476 processing from previous calls.
478 @item
479 The symbol @code{:panic}, signaling that the backend has encountered
480 an exceptional situation and should be disabled.
481 @end itemize
483 Currently accepted @var{report-key} arguments are:
485 @itemize
486 @item
487 @code{:explanation}, whose value should give user-readable
488 details of the situation encountered, if any.
490 @item
491 @code{:force}, whose value should be a boolean suggesting
492 that Flymake consider the report even if it was somehow
493 unexpected.
494 @end itemize
496 @menu
497 * Flymake utility functions::
498 * An annotated example backend::
499 @end menu
501 @node Flymake utility functions
502 @subsection Flymake utility functions
503 @cindex utility functions
505 @cindex create diagnostic object
506 Before delivering them to Flymake, backends create diagnostic objects
507 by calling the function @code{flymake-make-diagnostic}.
509 @deffn Function flymake-make-diagnostic buffer beg end type text
510 Make a Flymake diagnostic for @var{buffer}'s region from @var{beg} to
511 @var{end}.  @var{type} is a key to
512 @code{flymake-diagnostic-types-alist} and @var{text} is a description
513 of the problem detected in this region.
514 @end deffn
516 @cindex buffer position from line and column number
517 It is often the case with external syntax tools that a diagnostic's
518 position is reported in terms of a line number, and sometimes a column
519 number.  To convert this information into a buffer position, backends
520 can use the following function:
522 @deffn Function flymake-diag-region buffer line &optional col
523 Compute @var{buffer}'s region (@var{beg} .  @var{end}) corresponding to
524 @var{line} and @var{col}.  If @var{col} is nil, return a region just
525 for @var{line}.  Return nil if the region is invalid.
526 @end deffn
528 @cindex add a log message
529 For troubleshooting purposes, backends may record arbitrary
530 exceptional or erroneous situations into the Flymake log
531 buffer (@pxref{Backend exceptions}):
533 @deffn Macro flymake-log level msg &optional args
534 Log, at level @var{level}, the message @var{msg} formatted with
535 @var{args}.  @var{level} is passed to @code{display-warning}
536 (@pxref{Warning Basics,,, elisp, The Emacs Lisp reference Manual}), which is
537 used to display the warning in Flymake's log buffer.
538 @end deffn
540 @node An annotated example backend
541 @subsection An annotated example backend
542 @cindex example of backend
543 @cindex backend, annotated example
545 This section presents an annotated example of a complete working
546 Flymake backend.  The example illustrates the process of writing a
547 backend as outlined above.
549 The backend in question is used for checking Ruby source files.  It
550 uses asynchronous sub-processes (@pxref{Asynchronous Processes,,, elisp,
551 The Emacs Lisp Reference Manual}), a common technique for performing
552 parallel processing in Emacs.
554 The following code needs lexical binding (@pxref{Using Lexical
555 Binding,,, elisp, The Emacs Lisp Reference Manual}) to be active.
557 @example
558 ;;; ruby-flymake.el --- A ruby Flymake backend  -*- lexical-binding: t; -*-
559 (defvar-local ruby--flymake-proc nil)
561 (defun ruby-flymake (report-fn &rest _args)
562   ;; Not having a ruby interpreter is a serious problem which should cause
563   ;; the backend to disable itself, so an @code{error} is signaled.
564   ;;
565   (unless (executable-find
566            "ruby") (error "Cannot find a suitable ruby"))
567   ;; If a live process launched in an earlier check was found, that
568   ;; process is killed.  When that process's sentinel eventually runs,
569   ;; it will notice its obsoletion, since it have since reset
570   ;; `ruby-flymake-proc' to a different value
571   ;;
572   (when (process-live-p ruby--flymake-proc)
573     (kill-process ruby--flymake-proc))
575   ;; Save the current buffer, the narrowing restriction, remove any
576   ;; narrowing restriction.
577   ;;
578   (let ((source (current-buffer)))
579     (save-restriction
580       (widen)
581       ;; Reset the `ruby--flymake-proc' process to a new process
582       ;; calling the ruby tool.
583       ;;
584       (setq
585        ruby--flymake-proc
586        (make-process
587         :name "ruby-flymake" :noquery t :connection-type 'pipe
588         ;; Make output go to a temporary buffer.
589         ;;
590         :buffer (generate-new-buffer " *ruby-flymake*")
591         :command '("ruby" "-w" "-c")
592         :sentinel
593         (lambda (proc _event)
594           ;; Check that the process has indeed exited, as it might
595           ;; be simply suspended.
596           ;;
597           (when (eq 'exit (process-status proc))
598             (unwind-protect
599                 ;; Only proceed if `proc' is the same as
600                 ;; `ruby--flymake-proc', which indicates that
601                 ;; `proc' is not an obsolete process.
602                 ;;
603                 (if (eq proc ruby--flymake-proc)
604                     (with-current-buffer (process-buffer proc)
605                       (goto-char (point-min))
606                       ;; Parse the output buffer for diagnostic's
607                       ;; messages and locations, collect them in a list
608                       ;; of objects, and call `report-fn'.
609                       ;;
610                       (cl-loop
611                        while (search-forward-regexp
612                               "^\\(?:.*.rb\\|-\\):\\([0-9]+\\): \\(.*\\)$"
613                               nil t)
614                        for msg = (match-string 2)
615                        for (beg . end) = (flymake-diag-region
616                                           source
617                                           (string-to-number (match-string 1)))
618                        for type = (if (string-match "^warning" msg)
619                                       :warning
620                                     :error)
621                        collect (flymake-make-diagnostic source
622                                                         beg
623                                                         end
624                                                         type
625                                                         msg)
626                        into diags
627                        finally (funcall report-fn diags)))
628                   (flymake-log :warning "Canceling obsolete check %s"
629                                proc))
630               ;; Cleanup the temporary buffer used to hold the
631               ;; check's output.
632               ;;
633               (kill-buffer (process-buffer proc)))))))
634       ;; Send the buffer contents to the process's stdin, followed by
635       ;; an EOF.
636       ;;
637       (process-send-region ruby--flymake-proc (point-min) (point-max))
638       (process-send-eof ruby--flymake-proc))))
640 (defun ruby-setup-flymake-backend ()
641   (add-hook 'flymake-diagnostic-functions 'ruby-flymake nil t))
643 (add-hook 'ruby-mode-hook 'ruby-setup-flymake-backend)
644 @end example
646 @node The legacy Proc backend
647 @chapter The legacy ``Proc'' backend
648 @cindex legacy proc backend
650 @menu
651 * Proc customization variables::
652 * Adding support for a new syntax check tool::
653 * Implementation overview::
654 * Making a temporary copy::
655 * Locating a master file::
656 * Getting the include directories::
657 * Locating the buildfile::
658 * Starting the syntax check process::
659 * Parsing the output::
660 * Interaction with other modes::
661 @end menu
663 @findex flymake-proc-legacy-backend
664 The backend @code{flymake-proc-legacy-backend} was originally designed
665 to be extended for supporting new syntax check tools and error message
666 patterns.  It is also controlled by its own set of customization variables
668 @node Proc customization variables
669 @section Customization variables for the Proc backend
670 @cindex proc backend customization variables
672 @vtable @code
673 @item flymake-proc-allowed-file-name-masks
674 A list of @code{(filename-regexp, init-function, cleanup-function
675 getfname-function)} for configuring syntax check tools.  @xref{Adding
676 support for a new syntax check tool}.
678 @item flymake-proc-master-file-dirs
679 A list of directories for searching a master file.  @xref{Locating a
680 master file}.
682 @item flymake-proc-get-project-include-dirs-function
683 A function used for obtaining a list of project include dirs (C/C++
684 specific).  @xref{Getting the include directories}.
686 @item flymake-proc-master-file-count-limit
687 @itemx flymake-proc-check-file-limit
688 Used when looking for a master file.  @xref{Locating a master file}.
690 @item flymake-proc-err-line-patterns
691 Patterns for error/warning messages in the form @code{(regexp file-idx
692 line-idx col-idx err-text-idx)}.  @xref{Parsing the output}.
694 @item flymake-proc-diagnostic-type-pred
695 A function to classify a diagnostic text as particular type of
696 error.  Should be a function taking an error text and returning one of
697 the symbols indexing @code{flymake-diagnostic-types-alist}.  If non-nil
698 is returned but there is no such symbol in that table, a warning is
699 assumed.  If nil is returned, an error is assumed.  Can also be a
700 regular expression that should match only warnings.  This variable
701 replaces the old @code{flymake-warning-re} and
702 @code{flymake-warning-predicate}.
704 @item flymake-proc-compilation-prevents-syntax-check
705 A flag indicating whether compilation and syntax check of the same
706 file cannot be run simultaneously.  @xref{Interaction with other modes}.
707 @end vtable
709 @node Adding support for a new syntax check tool
710 @section Adding support for a new syntax check tool
711 @cindex adding support for a new syntax check tool
713 @menu
714 * Example---Configuring a tool called directly::
715 * Example---Configuring a tool called via make::
716 @end menu
718 Syntax check tools are configured using the
719 @code{flymake-proc-allowed-file-name-masks} list.  Each item of this list
720 has the following format:
722 @lisp
723 (filename-regexp, init-function, cleanup-function, getfname-function)
724 @end lisp
726 @table @code
727 @item filename-regexp
728 This field is used as a key for locating init/cleanup/getfname
729 functions for the buffer.  Items in
730 @code{flymake-proc-allowed-file-name-masks} are searched sequentially.
731 The first item with @code{filename-regexp} matching buffer filename is
732 selected.  If no match is found, @code{flymake-mode} is switched off.
734 @item init-function
735 @code{init-function} is required to initialize the syntax check,
736 usually by creating a temporary copy of the buffer contents.  The
737 function must return @code{(list cmd-name arg-list)}.  If
738 @code{init-function} returns null, syntax check is aborted, but
739 @code{flymake-mode} is not switched off.
741 @item cleanup-function
742 @code{cleanup-function} is called after the syntax check process is
743 complete and should take care of proper deinitialization, which is
744 usually deleting a temporary copy created by the @code{init-function}.
746 @item getfname-function
747 This function is used for translating filenames reported by the syntax
748 check tool into ``real'' filenames.  Filenames reported by the tool
749 will be different from the real ones, as actually the tool works with
750 the temporary copy.  In most cases, the default implementation
751 provided by Flymake, @code{flymake-proc-get-real-file-name}, can be
752 used as @code{getfname-function}.
753 @end table
755 To add support for a new syntax check tool, write the corresponding
756 @code{init-function} and, optionally, @code{cleanup-function} and
757 @code{getfname-function}.  If the format of error messages reported by
758 the new tool is not yet supported by Flymake, add a new entry to
759 the @code{flymake-proc-err-line-patterns} list.
761 The following sections contain some examples of configuring Flymake
762 support for various syntax check tools.
764 @node Example---Configuring a tool called directly
765 @subsection Example---Configuring a tool called directly
766 @cindex adding support for perl
768 In this example, we will add support for @command{perl} as a syntax check
769 tool.  @command{perl} supports the @option{-c} option which does syntax
770 checking.
772 First, we write the @code{init-function}:
774 @lisp
775 (defun flymake-proc-perl-init ()
776   (let* ((temp-file (flymake-proc-init-create-temp-buffer-copy
777                      'flymake-proc-create-temp-inplace))
778          (local-file (file-relative-name
779                       temp-file
780                       (file-name-directory buffer-file-name))))
781     (list "perl" (list "-wc " local-file))))
782 @end lisp
784 @code{flymake-proc-perl-init} creates a temporary copy of the buffer
785 contents with the help of
786 @code{flymake-proc-init-create-temp-buffer-copy}, and builds an appropriate
787 command line.
789 Next, we add a new entry to the
790 @code{flymake-proc-allowed-file-name-masks}:
792 @lisp
793 (setq flymake-proc-allowed-file-name-masks
794       (cons '(".+\\.pl$"
795               flymake-proc-perl-init
796               flymake-proc-simple-cleanup
797               flymake-proc-get-real-file-name)
798             flymake-proc-allowed-file-name-masks))
799 @end lisp
801 Note that we use standard @code{cleanup-function} and
802 @code{getfname-function}.
804 Finally, we add an entry to @code{flymake-proc-err-line-patterns}:
806 @lisp
807 (setq flymake-proc-err-line-patterns
808       (cons '("\\(.*\\) at \\([^ \n]+\\) line \\([0-9]+\\)[,.\n]"
809               2 3 nil 1)
810             flymake-proc-err-line-patterns))
811 @end lisp
813 @node Example---Configuring a tool called via make
814 @subsection Example---Configuring a tool called via make
815 @cindex adding support for C (gcc+make)
817 In this example we will add support for C files syntax checked by
818 @command{gcc} called via @command{make}.
820 We're not required to write any new functions, as Flymake already has
821 functions for @command{make}.  We just add a new entry to the
822 @code{flymake-proc-allowed-file-name-masks}:
824 @lisp
825 (setq flymake-proc-allowed-file-name-masks
826       (cons '(".+\\.c$"
827               flymake-proc-simple-make-init
828               flymake-proc-simple-cleanup
829               flymake-proc-get-real-file-name)
830             flymake-proc-allowed-file-name-masks))
831 @end lisp
833 @code{flymake-proc-simple-make-init} builds the following @command{make}
834 command line:
836 @lisp
837 (list "make"
838       (list "-s" "-C"
839             base-dir
840             (concat "CHK_SOURCES=" source)
841             "SYNTAX_CHECK_MODE=1"
842             "check-syntax"))
843 @end lisp
845 @code{base-dir} is a directory containing the @file{Makefile}, see
846 @ref{Locating the buildfile}.
848 Thus, @file{Makefile} must contain the @code{check-syntax} target.  In
849 our case this target might look like this:
851 @verbatim
852 check-syntax:
853         gcc -o /dev/null -S ${CHK_SOURCES} || true
854 @end verbatim
856 @noindent
857 The format of error messages reported by @command{gcc} is already
858 supported by Flymake, so we don't have to add a new entry to
859 @code{flymake-err-line-patterns}.  Note that if you are using
860 Automake, you may want to replace @code{gcc} with the standard
861 Automake variable @code{COMPILE}:
863 @verbatim
864 check-syntax:
865         $(COMPILE) -o /dev/null -S ${CHK_SOURCES} || true
866 @end verbatim
868 @node Implementation overview
869 @section Implementation overview
870 @cindex syntax check models
871 @cindex master file
873 @code{flymake-proc-legacy-backend} saves a copy of the buffer in a
874 temporary file in the buffer's directory (or in the system temporary
875 directory, for Java files), creates a syntax check command and
876 launches a process with this command.  The output is parsed using a
877 list of error message patterns, and error information (file name, line
878 number, type and text) is saved.  After the process has finished,
879 Flymake highlights erroneous lines in the buffer using the accumulated
880 error information.
882 Syntax check is considered possible if there's an entry in
883 @code{flymake-proc-allowed-file-name-masks} matching buffer's filename and
884 its @code{init-function} returns non-@code{nil} value.
886 Two syntax check modes are distinguished:
888 @enumerate
890 @item
891 Buffer can be syntax checked in a standalone fashion, that is, the
892 file (its temporary copy, in fact) can be passed over to the compiler to
893 do the syntax check.  Examples are C/C@t{++} sources (@file{.c},
894 @file{.cpp}) and Java (@file{.java}).
896 @item
897 Buffer can be syntax checked, but additional file, called master file,
898 is required to perform this operation.  A master file is a file that
899 includes the current file, so that running a syntax check tool on it
900 will also check syntax in the current file.  Examples are C/C@t{++}
901 headers (@file{.h}, @file{.hpp}).
903 @end enumerate
905 These modes are handled inside init/cleanup/getfname functions, see
906 @ref{Adding support for a new syntax check tool}.
908 The Proc backend contains implementations of all functionality
909 required to support different syntax check modes described above
910 (making temporary copies, finding master files, etc.), as well as some
911 tool-specific (routines for Make, Ant, etc.)@: code.
914 @node Making a temporary copy
915 @section Making a temporary copy
916 @cindex temporary copy of the buffer
918 After the possibility of the syntax check has been determined, a
919 temporary copy of the current buffer is made so that the most recent
920 unsaved changes could be seen by the syntax check tool.  Making a copy
921 is quite straightforward in a standalone case (mode @code{1}), as it's
922 just saving buffer contents to a temporary file.
924 Things get trickier, however, when master file is involved, as it
925 requires to
927 @itemize @bullet
928 @item
929 locate a master file
930 @item
931 patch it to include the current file using its new (temporary) name.
932 @end itemize
934 Locating a master file is discussed in the following section.
936 Patching just changes all appropriate lines of the master file so that they
937 use the new (temporary) name of the current file.  For example, suppose current
938 file name is @code{file.h}, the master file is @code{file.cpp}, and
939 it includes current file via @code{#include "file.h"}.  Current file's copy
940 is saved to file @code{file_flymake.h}, so the include line must be
941 changed to @code{#include "file_flymake.h"}.  Finally, patched master file
942 is saved to @code{file_flymake_master.cpp}, and the last one is passed to
943 the syntax check tool.
945 @node Locating a master file
946 @section Locating a master file
947 @cindex locating a master file
948 @cindex master file, locating
950 Master file is located in two steps.
952 First, a list of possible master files is built.  A simple name
953 matching is used to find the files.  For a C++ header @file{file.h},
954 the Proc backend searches for all @file{.cpp} files in the directories
955 whose relative paths are stored in a customizable variable
956 @code{flymake-proc-master-file-dirs}, which usually contains something
957 like @code{("." "./src")}.  No more than
958 @code{flymake-proc-master-file-count-limit} entries is added to the
959 master file list.  The list is then sorted to move files with names
960 @file{file.cpp} to the top.
962 Next, each master file in a list is checked to contain the appropriate
963 include directives.  No more than @code{flymake-proc-check-file-limit} of each
964 file are parsed.
966 For @file{file.h}, the include directives to look for are
967 @code{#include "file.h"}, @code{#include "../file.h"}, etc.  Each
968 include is checked against a list of include directories
969 (see @ref{Getting the include directories}) to be sure it points to the
970 correct @file{file.h}.
972 First matching master file found stops the search.  The master file is then
973 patched and saved to disk.  In case no master file is found, syntax check is
974 aborted, and corresponding status (@samp{!}) is reported in the mode line.
975 @xref{Syntax check statuses}.
977 @node Getting the include directories
978 @section Getting the include directories
979 @cindex include directories (C/C++ specific)
981 Two sets of include directories are distinguished: system include directories
982 and project include directories.  The former is just the contents of the
983 @code{INCLUDE} environment variable.  The latter is not so easy to obtain,
984 and the way it can be obtained can vary greatly for different projects.
985 Therefore, a customizable variable
986 @code{flymake-proc-get-project-include-dirs-function} is used to provide the
987 way to implement the desired behavior.
989 The default implementation, @code{flymake-proc-get-project-include-dirs-imp},
990 uses a @command{make} call.  This requires a correct base directory, that is, a
991 directory containing a correct @file{Makefile}, to be determined.
993 As obtaining the project include directories might be a costly operation, its
994 return value is cached in the hash table.  The cache is cleared in the beginning
995 of every syntax check attempt.
997 @node Locating the buildfile
998 @section Locating the buildfile
999 @cindex locating the buildfile
1000 @cindex buildfile, locating
1001 @cindex Makefile, locating
1003 The Proc backend can be configured to use different tools for
1004 performing syntax checks.  For example, it can use direct compiler
1005 call to syntax check a perl script or a call to @command{make} for a
1006 more complicated case of a C/C@t{++} source.  The general idea is
1007 that simple files, like Perl scripts and @acronym{HTML} pages, can be checked by
1008 directly invoking a corresponding tool.  Files that are usually more
1009 complex and generally used as part of larger projects, might require
1010 non-trivial options to be passed to the syntax check tool, like
1011 include directories for C@t{++}.  The latter files are syntax checked
1012 using some build tool, like Make or Ant.
1014 All Make configuration data is usually stored in a file called
1015 @file{Makefile}.  To allow for future extensions, Flymake uses a notion of
1016 buildfile to reference the @dfn{project configuration} file.
1018 @findex flymake-proc-find-buildfile
1019 Special function, @code{flymake-proc-find-buildfile} is provided for locating buildfiles.
1020 Searching for a buildfile is done in a manner similar to that of searching
1021 for possible master files.
1022 @ignore
1023 A customizable variable
1024 @code{flymake-proc-buildfile-dirs} holds a list of relative paths to the
1025 buildfile.  They are checked sequentially until a buildfile is found.
1026 @end ignore
1027 In case there's no build file, the syntax check is aborted.
1029 Buildfile values are also cached.
1031 @node Starting the syntax check process
1032 @section Starting the syntax check process
1033 @cindex syntax check process, legacy proc backend
1035 The command line (command name and the list of arguments) for
1036 launching a process is returned by the initialization function.  The
1037 Proc backend then just starts an asynchronous process and configures a
1038 process filter and sentinel, which are used for processing the output
1039 of the syntax check tool.  When exiting Emacs, running processes will
1040 be killed without prompting the user.
1042 @node Parsing the output
1043 @section Parsing the output
1044 @cindex parsing the output, legacy proc backend
1046 The output generated by the syntax check tool is parsed in the process
1047 filter/sentinel using the error message patterns stored in the
1048 @code{flymake-proc-err-line-patterns} variable.  This variable contains a
1049 list of items of the form @w{@code{(regexp file-idx line-idx
1050 err-text-idx)}}, used to determine whether a particular line is an
1051 error message and extract file name, line number and error text,
1052 respectively.  Error type (error/warning) is also guessed by matching
1053 error text with the @samp{^[wW]arning} pattern.  Anything that was not
1054 classified as a warning is considered an error.  Type is then used to
1055 sort error menu items, which shows error messages first.
1057 The Proc backend is also able to interpret error message patterns
1058 missing err-text-idx information.  This is done by merely taking the
1059 rest of the matched line (@code{(substring line (match-end 0))}) as
1060 error text.  This trick allows making use of a huge collection of
1061 error message line patterns from @file{compile.el}.  All these error
1062 patterns are appended to the end of
1063 @code{flymake-proc-err-line-patterns}.
1065 The error information obtained is saved in a buffer local
1066 variable.  The buffer for which the process output belongs is
1067 determined from the process-id@w{}->@w{}buffer mapping updated
1068 after every process launch/exit.
1070 @node Interaction with other modes
1071 @section Interaction with other modes
1072 @cindex interaction with other modes, legacy proc backend
1073 @cindex interaction with compile mode, legacy proc backend
1075 The only mode the Proc backend currently knows about is
1076 @code{compile}.
1078 The Proc backend can be configured to not start syntax check if it
1079 thinks the compilation is in progress, by testing the
1080 @code{compilation-in-progress} variable.  The reason why this might be
1081 useful is saving CPU time in case both syntax check and compilation
1082 are very CPU intensive.  The original reason for adding this feature,
1083 though, was working around a locking problem with MS Visual C++
1084 compiler.  The variable in question is
1085 @code{flymake-proc-compilation-prevents-syntax-check}.
1087 @findex flymake-proc-compile
1088 The Proc backend also provides an alternative command for starting
1089 compilation, @code{flymake-proc-compile}.  It just kills all the active
1090 syntax check processes before calling @code{compile}.
1092 @node GNU Free Documentation License
1093 @appendix GNU Free Documentation License
1094 @include doclicense.texi
1096 @node Index
1097 @unnumbered Index
1099 @printindex cp
1101 @bye