Fix triple-quoting electricity in python-mode
[bpt/emacs.git] / lisp / progmodes / flymake.el
CommitLineData
a92fa5f1 1;;; flymake.el --- a universal on-the-fly syntax checker -*- lexical-binding: t; -*-
4bcbcb9d 2
ba318903 3;; Copyright (C) 2003-2014 Free Software Foundation, Inc.
4bcbcb9d 4
1d9a4930 5;; Author: Pavel Kobyakov <pk_at_work@yahoo.com>
a92fa5f1 6;; Maintainer: Leo Liu <sdl.web@gmail.com>
4bcbcb9d
EZ
7;; Version: 0.3
8;; Keywords: c languages tools
9
10;; This file is part of GNU Emacs.
11
b1fc2b50 12;; GNU Emacs is free software: you can redistribute it and/or modify
4bcbcb9d 13;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
4bcbcb9d
EZ
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
b1fc2b50 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
4bcbcb9d
EZ
24
25;;; Commentary:
26;;
a92fa5f1
LL
27;; Flymake is a minor Emacs mode performing on-the-fly syntax checks
28;; using the external syntax check tool (for C/C++ this is usually the
29;; compiler).
4bcbcb9d 30
9f812d23
SM
31;;; Bugs/todo:
32
33;; - Only uses "Makefile", not "makefile" or "GNUmakefile"
34;; (from http://bugs.debian.org/337339).
35
4bcbcb9d
EZ
36;;; Code:
37
a464a6c7 38(eval-when-compile (require 'cl-lib))
6fee12e6 39
a92fa5f1
LL
40(defgroup flymake nil
41 "Universal on-the-fly syntax checker."
42 :version "23.1"
ed85dee6 43 :link '(custom-manual "(flymake) Top")
a92fa5f1
LL
44 :group 'tools)
45
46(defcustom flymake-error-bitmap '(exclamation-mark error)
47 "Bitmap (a symbol) used in the fringe for indicating errors.
48The value may also be a list of two elements where the second
49element specifies the face for the bitmap. For possible bitmap
50symbols, see `fringe-bitmaps'. See also `flymake-warning-bitmap'.
51
52The option `flymake-fringe-indicator-position' controls how and where
53this is used."
54 :group 'flymake
55 :version "24.3"
56 :type '(choice (symbol :tag "Bitmap")
57 (list :tag "Bitmap and face"
58 (symbol :tag "Bitmap")
59 (face :tag "Face"))))
60
61(defcustom flymake-warning-bitmap 'question-mark
62 "Bitmap (a symbol) used in the fringe for indicating warnings.
63The value may also be a list of two elements where the second
64element specifies the face for the bitmap. For possible bitmap
65symbols, see `fringe-bitmaps'. See also `flymake-error-bitmap'.
66
67The option `flymake-fringe-indicator-position' controls how and where
68this is used."
69 :group 'flymake
70 :version "24.3"
71 :type '(choice (symbol :tag "Bitmap")
72 (list :tag "Bitmap and face"
73 (symbol :tag "Bitmap")
74 (face :tag "Face"))))
75
76(defcustom flymake-fringe-indicator-position 'left-fringe
77 "The position to put flymake fringe indicator.
78The value can be nil (do not use indicators), `left-fringe' or `right-fringe'.
79See `flymake-error-bitmap' and `flymake-warning-bitmap'."
80 :group 'flymake
81 :version "24.3"
82 :type '(choice (const left-fringe)
83 (const right-fringe)
84 (const :tag "No fringe indicators" nil)))
85
86(defcustom flymake-compilation-prevents-syntax-check t
87 "If non-nil, don't start syntax check if compilation is running."
88 :group 'flymake
89 :type 'boolean)
90
91(defcustom flymake-start-syntax-check-on-newline t
92 "Start syntax check if newline char was added/removed from the buffer."
93 :group 'flymake
94 :type 'boolean)
95
96(defcustom flymake-no-changes-timeout 0.5
97 "Time to wait after last change before starting compilation."
98 :group 'flymake
99 :type 'number)
100
101(defcustom flymake-gui-warnings-enabled t
102 "Enables/disables GUI warnings."
103 :group 'flymake
104 :type 'boolean)
105
106(defcustom flymake-start-syntax-check-on-find-file t
107 "Start syntax check on find file."
108 :group 'flymake
109 :type 'boolean)
110
111(defcustom flymake-log-level -1
112 "Logging level, only messages with level lower or equal will be logged.
113-1 = NONE, 0 = ERROR, 1 = WARNING, 2 = INFO, 3 = DEBUG"
114 :group 'flymake
115 :type 'integer)
116
117(defcustom flymake-xml-program
118 (if (executable-find "xmlstarlet") "xmlstarlet" "xml")
119 "Program to use for XML validation."
120 :type 'file
121 :group 'flymake
122 :version "24.4")
123
124(defcustom flymake-master-file-dirs '("." "./src" "./UnitTest")
125 "Dirs where to look for master files."
126 :group 'flymake
127 :type '(repeat (string)))
128
129(defcustom flymake-master-file-count-limit 32
130 "Max number of master files to check."
131 :group 'flymake
132 :type 'integer)
133
134(defcustom flymake-allowed-file-name-masks
135 '(("\\.\\(?:c\\(?:pp\\|xx\\|\\+\\+\\)?\\|CC\\)\\'" flymake-simple-make-init)
136 ("\\.xml\\'" flymake-xml-init)
137 ("\\.html?\\'" flymake-xml-init)
138 ("\\.cs\\'" flymake-simple-make-init)
139 ("\\.p[ml]\\'" flymake-perl-init)
140 ("\\.php[345]?\\'" flymake-php-init)
141 ("\\.h\\'" flymake-master-make-header-init flymake-master-cleanup)
142 ("\\.java\\'" flymake-simple-make-java-init flymake-simple-java-cleanup)
143 ("[0-9]+\\.tex\\'" flymake-master-tex-init flymake-master-cleanup)
144 ("\\.tex\\'" flymake-simple-tex-init)
145 ("\\.idl\\'" flymake-simple-make-init)
146 ;; ("\\.cpp\\'" 1)
147 ;; ("\\.java\\'" 3)
148 ;; ("\\.h\\'" 2 ("\\.cpp\\'" "\\.c\\'")
149 ;; ("[ \t]*#[ \t]*include[ \t]*\"\\([\w0-9/\\_\.]*[/\\]*\\)\\(%s\\)\"" 1 2))
150 ;; ("\\.idl\\'" 1)
151 ;; ("\\.odl\\'" 1)
152 ;; ("[0-9]+\\.tex\\'" 2 ("\\.tex\\'")
153 ;; ("[ \t]*\\input[ \t]*{\\(.*\\)\\(%s\\)}" 1 2 ))
154 ;; ("\\.tex\\'" 1)
155 )
156 "Files syntax checking is allowed for.
157This is an alist with elements of the form:
158 REGEXP INIT [CLEANUP [NAME]]
159REGEXP is a regular expression that matches a file name.
160INIT is the init function to use.
161CLEANUP is the cleanup function to use, default `flymake-simple-cleanup'.
162NAME is the file name function to use, default `flymake-get-real-file-name'."
163 :group 'flymake
164 :type '(alist :key-type (regexp :tag "File regexp")
165 :value-type
166 (list :tag "Handler functions"
167 (function :tag "Init function")
168 (choice :tag "Cleanup function"
169 (const :tag "flymake-simple-cleanup" nil)
170 function)
171 (choice :tag "Name function"
172 (const :tag "flymake-get-real-file-name" nil)
173 function))))
174
175(defvar-local flymake-is-running nil
43ed65ac 176 "If t, flymake syntax check process is running for the current buffer.")
c92d6023 177
a92fa5f1 178(defvar-local flymake-timer nil
43ed65ac 179 "Timer for starting syntax check.")
c92d6023 180
a92fa5f1 181(defvar-local flymake-last-change-time nil
43ed65ac 182 "Time of last buffer change.")
43ed65ac 183
a92fa5f1 184(defvar-local flymake-check-start-time nil
43ed65ac 185 "Time at which syntax check was started.")
43ed65ac 186
a92fa5f1 187(defvar-local flymake-check-was-interrupted nil
43ed65ac 188 "Non-nil if syntax check was killed by `flymake-compile'.")
43ed65ac 189
a92fa5f1 190(defvar-local flymake-err-info nil
43ed65ac 191 "Sorted list of line numbers and lists of err info in the form (file, err-text).")
43ed65ac 192
a92fa5f1 193(defvar-local flymake-new-err-info nil
43ed65ac 194 "Same as `flymake-err-info', effective when a syntax check is in progress.")
4bcbcb9d 195
2aa13ec4 196(defun flymake-log (level text &rest args)
53ec26ed
RS
197 "Log a message at level LEVEL.
198If LEVEL is higher than `flymake-log-level', the message is
199ignored. Otherwise, it is printed using `message'.
200TEXT is a format control string, and the remaining arguments ARGS
5d028165 201are the string substitutions (see the function `format')."
5323b4b0
RS
202 (if (<= level flymake-log-level)
203 (let* ((msg (apply 'format text args)))
a92fa5f1 204 (message "%s" msg))))
4bcbcb9d 205
2aa13ec4 206(defun flymake-ins-after (list pos val)
5d028165
XF
207 "Insert VAL into LIST after position POS.
208POS counts from zero."
17e5c0cc 209 (let ((tmp (copy-sequence list)))
5323b4b0
RS
210 (setcdr (nthcdr pos tmp) (cons val (nthcdr (1+ pos) tmp)))
211 tmp))
4bcbcb9d 212
2aa13ec4 213(defun flymake-set-at (list pos val)
17e5c0cc 214 "Set VAL at position POS in LIST.
5d028165 215POS counts from zero."
17e5c0cc 216 (let ((tmp (copy-sequence list)))
5323b4b0
RS
217 (setcar (nthcdr pos tmp) val)
218 tmp))
4bcbcb9d 219
43ed65ac
SM
220(defvar flymake-processes nil
221 "List of currently active flymake processes.")
4bcbcb9d 222
a92fa5f1 223(defvar-local flymake-output-residual nil)
4bcbcb9d 224
2aa13ec4 225(defun flymake-get-file-name-mode-and-masks (file-name)
9a686ad2 226 "Return the corresponding entry from `flymake-allowed-file-name-masks'."
5323b4b0
RS
227 (unless (stringp file-name)
228 (error "Invalid file-name"))
17404091 229 (let ((fnm flymake-allowed-file-name-masks)
5d028165 230 (mode-and-masks nil))
17404091
SM
231 (while (and (not mode-and-masks) fnm)
232 (if (string-match (car (car fnm)) file-name)
233 (setq mode-and-masks (cdr (car fnm))))
234 (setq fnm (cdr fnm)))
5323b4b0
RS
235 (flymake-log 3 "file %s, init=%s" file-name (car mode-and-masks))
236 mode-and-masks))
4bcbcb9d 237
2aa13ec4 238(defun flymake-can-syntax-check-file (file-name)
5323b4b0 239 "Determine whether we can syntax check FILE-NAME.
2aa13ec4 240Return nil if we cannot, non-nil if we can."
5323b4b0 241 (if (flymake-get-init-function file-name) t nil))
4bcbcb9d 242
2aa13ec4 243(defun flymake-get-init-function (file-name)
5323b4b0
RS
244 "Return init function to be used for the file."
245 (let* ((init-f (nth 0 (flymake-get-file-name-mode-and-masks file-name))))
9a686ad2
SM
246 ;;(flymake-log 0 "calling %s" init-f)
247 ;;(funcall init-f (current-buffer))
5323b4b0 248 init-f))
4bcbcb9d 249
2aa13ec4 250(defun flymake-get-cleanup-function (file-name)
5323b4b0 251 "Return cleanup function to be used for the file."
c07fa030
SM
252 (or (nth 1 (flymake-get-file-name-mode-and-masks file-name))
253 'flymake-simple-cleanup))
4bcbcb9d 254
2aa13ec4 255(defun flymake-get-real-file-name-function (file-name)
eed99101 256 (or (nth 2 (flymake-get-file-name-mode-and-masks file-name))
6df19241 257 'flymake-get-real-file-name))
4bcbcb9d 258
a92fa5f1 259(defvar flymake-find-buildfile-cache (make-hash-table :test #'equal))
4bcbcb9d 260
2aa13ec4 261(defun flymake-get-buildfile-from-cache (dir-name)
5d028165
XF
262 "Look up DIR-NAME in cache and return its associated value.
263If DIR-NAME is not found, return nil."
5323b4b0 264 (gethash dir-name flymake-find-buildfile-cache))
2aa13ec4
RS
265
266(defun flymake-add-buildfile-to-cache (dir-name buildfile)
5d028165 267 "Associate DIR-NAME with BUILDFILE in the buildfile cache."
5323b4b0 268 (puthash dir-name buildfile flymake-find-buildfile-cache))
2aa13ec4
RS
269
270(defun flymake-clear-buildfile-cache ()
5d028165 271 "Clear the buildfile cache."
5323b4b0 272 (clrhash flymake-find-buildfile-cache))
2aa13ec4 273
5f73367d 274(defun flymake-find-buildfile (buildfile-name source-dir-name)
5323b4b0 275 "Find buildfile starting from current directory.
2aa13ec4 276Buildfile includes Makefile, build.xml etc.
5323b4b0 277Return its file name if found, or nil if not found."
5f73367d 278 (or (flymake-get-buildfile-from-cache source-dir-name)
8cd56959 279 (let* ((file (locate-dominating-file source-dir-name buildfile-name)))
8ebbfc80 280 (if file
5f73367d 281 (progn
8ebbfc80 282 (flymake-log 3 "found buildfile at %s" file)
8ebbfc80
SM
283 (flymake-add-buildfile-to-cache source-dir-name file)
284 file)
5f73367d
SM
285 (progn
286 (flymake-log 3 "buildfile for %s not found" source-dir-name)
287 nil)))))
5323b4b0
RS
288
289(defun flymake-fix-file-name (name)
57d3a1f3 290 "Replace all occurrences of '\' with '/'."
5323b4b0 291 (when name
587d108e
SM
292 (setq name (expand-file-name name))
293 (setq name (abbreviate-file-name name))
294 (setq name (directory-file-name name))
295 name))
4bcbcb9d 296
2aa13ec4 297(defun flymake-same-files (file-name-one file-name-two)
5323b4b0 298 "Check if FILE-NAME-ONE and FILE-NAME-TWO point to same file.
2aa13ec4 299Return t if so, nil if not."
5323b4b0
RS
300 (equal (flymake-fix-file-name file-name-one)
301 (flymake-fix-file-name file-name-two)))
302
ce721de4
KS
303;; This is bound dynamically to pass a parameter to a sort predicate below
304(defvar flymake-included-file-name)
4bcbcb9d 305
2aa13ec4 306(defun flymake-find-possible-master-files (file-name master-file-dirs masks)
9a686ad2 307 "Find (by name and location) all possible master files.
5d028165
XF
308
309Name is specified by FILE-NAME and location is specified by
310MASTER-FILE-DIRS. Master files include .cpp and .c for .h.
311Files are searched for starting from the .h directory and max
312max-level parent dirs. File contents are not checked."
17404091 313 (let* ((dirs master-file-dirs)
5323b4b0 314 (files nil)
17404091
SM
315 (done nil))
316
317 (while (and (not done) dirs)
587d108e 318 (let* ((dir (expand-file-name (car dirs) (file-name-directory file-name)))
17404091
SM
319 (masks masks))
320 (while (and (file-exists-p dir) (not done) masks)
321 (let* ((mask (car masks))
322 (dir-files (directory-files dir t mask)))
323
324 (flymake-log 3 "dir %s, %d file(s) for mask %s"
325 dir (length dir-files) mask)
326 (while (and (not done) dir-files)
327 (when (not (file-directory-p (car dir-files)))
328 (setq files (cons (car dir-files) files))
5323b4b0
RS
329 (when (>= (length files) flymake-master-file-count-limit)
330 (flymake-log 3 "master file count limit (%d) reached" flymake-master-file-count-limit)
331 (setq done t)))
17404091
SM
332 (setq dir-files (cdr dir-files))))
333 (setq masks (cdr masks))))
334 (setq dirs (cdr dirs)))
5323b4b0 335 (when files
ce721de4
KS
336 (let ((flymake-included-file-name (file-name-nondirectory file-name)))
337 (setq files (sort files 'flymake-master-file-compare))))
5323b4b0
RS
338 (flymake-log 3 "found %d possible master file(s)" (length files))
339 files))
4bcbcb9d 340
2aa13ec4 341(defun flymake-master-file-compare (file-one file-two)
173569aa 342 "Compare two files specified by FILE-ONE and FILE-TWO.
2aa13ec4 343This function is used in sort to move most possible file names
173569aa 344to the beginning of the list (File.h -> File.cpp moved to top)."
5323b4b0 345 (and (equal (file-name-sans-extension flymake-included-file-name)
d2c32364 346 (file-name-base file-one))
5323b4b0 347 (not (equal file-one file-two))))
4bcbcb9d 348
a92fa5f1 349(defvar flymake-check-file-limit 8192
bd77c2ef 350 "Maximum number of chars to look at when checking possible master file.
a92fa5f1 351Nil means search the entire file.")
4bcbcb9d 352
587d108e
SM
353(defun flymake-check-patch-master-file-buffer
354 (master-file-temp-buffer
355 master-file-name patched-master-file-name
356 source-file-name patched-source-file-name
357 include-dirs regexp)
5323b4b0 358 "Check if MASTER-FILE-NAME is a master file for SOURCE-FILE-NAME.
2aa13ec4
RS
359If yes, patch a copy of MASTER-FILE-NAME to include PATCHED-SOURCE-FILE-NAME
360instead of SOURCE-FILE-NAME.
fd09a83f
CY
361
362For example, foo.cpp is a master file if it includes foo.h.
363
2aa13ec4
RS
364Whether a buffer for MATER-FILE-NAME exists, use it as a source
365instead of reading master file from disk."
587d108e 366 (let* ((source-file-nondir (file-name-nondirectory source-file-name))
bd77c2ef
GM
367 (source-file-extension (file-name-extension source-file-nondir))
368 (source-file-nonext (file-name-sans-extension source-file-nondir))
587d108e 369 (found nil)
5323b4b0
RS
370 (inc-name nil)
371 (search-limit flymake-check-file-limit))
587d108e
SM
372 (setq regexp
373 (format regexp ; "[ \t]*#[ \t]*include[ \t]*\"\\(.*%s\\)\""
bd77c2ef
GM
374 ;; Hack for tex files, where \include often excludes .tex.
375 ;; Maybe this is safe generally.
376 (if (and (> (length source-file-extension) 1)
377 (string-equal source-file-extension "tex"))
378 (format "%s\\(?:\\.%s\\)?"
379 (regexp-quote source-file-nonext)
380 (regexp-quote source-file-extension))
381 (regexp-quote source-file-nondir))))
587d108e
SM
382 (unwind-protect
383 (with-current-buffer master-file-temp-buffer
bd77c2ef
GM
384 (if (or (not search-limit)
385 (> search-limit (point-max)))
386 (setq search-limit (point-max)))
587d108e
SM
387 (flymake-log 3 "checking %s against regexp %s"
388 master-file-name regexp)
389 (goto-char (point-min))
390 (while (and (< (point) search-limit)
391 (re-search-forward regexp search-limit t))
392 (let ((match-beg (match-beginning 1))
393 (match-end (match-end 1)))
394
395 (flymake-log 3 "found possible match for %s" source-file-nondir)
396 (setq inc-name (match-string 1))
bd77c2ef
GM
397 (and (> (length source-file-extension) 1)
398 (string-equal source-file-extension "tex")
399 (not (string-match (format "\\.%s\\'" source-file-extension)
400 inc-name))
401 (setq inc-name (concat inc-name "." source-file-extension)))
587d108e
SM
402 (when (eq t (compare-strings
403 source-file-nondir nil nil
404 inc-name (- (length inc-name)
405 (length source-file-nondir)) nil))
406 (flymake-log 3 "inc-name=%s" inc-name)
407 (when (flymake-check-include source-file-name inc-name
408 include-dirs)
409 (setq found t)
410 ;; replace-match is not used here as it fails in
411 ;; XEmacs with 'last match not a buffer' error as
412 ;; check-includes calls replace-in-string
413 (flymake-replace-region
414 match-beg match-end
415 (file-name-nondirectory patched-source-file-name))))
416 (forward-line 1)))
417 (when found
418 (flymake-save-buffer-in-file patched-master-file-name)))
419 ;;+(flymake-log 3 "killing buffer %s"
420 ;; (buffer-name master-file-temp-buffer))
421 (kill-buffer master-file-temp-buffer))
9a686ad2 422 ;;+(flymake-log 3 "check-patch master file %s: %s" master-file-name found)
5323b4b0
RS
423 (when found
424 (flymake-log 2 "found master file %s" master-file-name))
425 found))
4bcbcb9d 426
a92fa5f1 427;;; XXX: remove
4bd0a5d0 428(defun flymake-replace-region (beg end rep)
5323b4b0
RS
429 "Replace text in BUFFER in region (BEG END) with REP."
430 (save-excursion
4bd0a5d0
SM
431 (goto-char end)
432 ;; Insert before deleting, so as to better preserve markers's positions.
433 (insert rep)
434 (delete-region beg end)))
4bcbcb9d 435
2aa13ec4 436(defun flymake-read-file-to-temp-buffer (file-name)
5323b4b0
RS
437 "Insert contents of FILE-NAME into newly created temp buffer."
438 (let* ((temp-buffer (get-buffer-create (generate-new-buffer-name (concat "flymake:" (file-name-nondirectory file-name))))))
cd2325cd 439 (with-current-buffer temp-buffer
5323b4b0
RS
440 (insert-file-contents file-name))
441 temp-buffer))
4bcbcb9d 442
2aa13ec4 443(defun flymake-copy-buffer-to-temp-buffer (buffer)
5323b4b0 444 "Copy contents of BUFFER into newly created temp buffer."
4dd68f44
SM
445 (with-current-buffer
446 (get-buffer-create (generate-new-buffer-name
447 (concat "flymake:" (buffer-name buffer))))
448 (insert-buffer-substring buffer)
449 (current-buffer)))
4bcbcb9d 450
587d108e 451(defun flymake-check-include (source-file-name inc-name include-dirs)
5323b4b0 452 "Check if SOURCE-FILE-NAME can be found in include path.
587d108e
SM
453Return t if it can be found via include path using INC-NAME."
454 (if (file-name-absolute-p inc-name)
455 (flymake-same-files source-file-name inc-name)
456 (while (and include-dirs
457 (not (flymake-same-files
458 source-file-name
459 (concat (file-name-directory source-file-name)
460 "/" (car include-dirs)
461 "/" inc-name))))
462 (setq include-dirs (cdr include-dirs)))
463 include-dirs))
4bcbcb9d 464
2aa13ec4 465(defun flymake-find-buffer-for-file (file-name)
5323b4b0 466 "Check if there exists a buffer visiting FILE-NAME.
2aa13ec4 467Return t if so, nil if not."
5323b4b0
RS
468 (let ((buffer-name (get-file-buffer file-name)))
469 (if buffer-name
470 (get-buffer buffer-name))))
4bcbcb9d 471
587d108e 472(defun flymake-create-master-file (source-file-name patched-source-file-name get-incl-dirs-f create-temp-f masks include-regexp)
5323b4b0 473 "Save SOURCE-FILE-NAME with a different name.
2aa13ec4 474Find master file, patch and save it."
5323b4b0
RS
475 (let* ((possible-master-files (flymake-find-possible-master-files source-file-name flymake-master-file-dirs masks))
476 (master-file-count (length possible-master-files))
477 (idx 0)
478 (temp-buffer nil)
479 (master-file-name nil)
480 (patched-master-file-name nil)
481 (found nil))
482
483 (while (and (not found) (< idx master-file-count))
484 (setq master-file-name (nth idx possible-master-files))
485 (setq patched-master-file-name (funcall create-temp-f master-file-name "flymake_master"))
486 (if (flymake-find-buffer-for-file master-file-name)
487 (setq temp-buffer (flymake-copy-buffer-to-temp-buffer (flymake-find-buffer-for-file master-file-name)))
488 (setq temp-buffer (flymake-read-file-to-temp-buffer master-file-name)))
489 (setq found
490 (flymake-check-patch-master-file-buffer
491 temp-buffer
492 master-file-name
493 patched-master-file-name
494 source-file-name
495 patched-source-file-name
496 (funcall get-incl-dirs-f (file-name-directory master-file-name))
587d108e 497 include-regexp))
5323b4b0
RS
498 (setq idx (1+ idx)))
499 (if found
500 (list master-file-name patched-master-file-name)
501 (progn
502 (flymake-log 3 "none of %d master file(s) checked includes %s" master-file-count
503 (file-name-nondirectory source-file-name))
504 nil))))
4bcbcb9d 505
6df19241 506(defun flymake-save-buffer-in-file (file-name)
5d028165
XF
507 "Save the entire buffer contents into file FILE-NAME.
508Create parent directories as needed."
ab1d3835
SM
509 (make-directory (file-name-directory file-name) 1)
510 (write-region nil nil file-name nil 566)
6df19241 511 (flymake-log 3 "saved buffer %s in file %s" (buffer-name) file-name))
4bcbcb9d 512
2aa13ec4 513(defun flymake-process-filter (process output)
5323b4b0 514 "Parse OUTPUT and highlight error lines.
2aa13ec4 515It's flymake process filter."
43ed65ac 516 (let ((source-buffer (process-buffer process)))
4bcbcb9d 517
43ed65ac
SM
518 (flymake-log 3 "received %d byte(s) of output from process %d"
519 (length output) (process-id process))
466a09b5 520 (when (buffer-live-p source-buffer)
4dd68f44
SM
521 (with-current-buffer source-buffer
522 (flymake-parse-output-and-residual output)))))
4bcbcb9d 523
e02f48d7 524(defun flymake-process-sentinel (process _event)
5323b4b0 525 "Sentinel for syntax check buffers."
c07fa030
SM
526 (when (memq (process-status process) '(signal exit))
527 (let* ((exit-status (process-exit-status process))
528 (command (process-command process))
529 (source-buffer (process-buffer process))
530 (cleanup-f (flymake-get-cleanup-function (buffer-file-name source-buffer))))
531
532 (flymake-log 2 "process %d exited with code %d"
533 (process-id process) exit-status)
534 (condition-case err
535 (progn
536 (flymake-log 3 "cleaning up using %s" cleanup-f)
537 (when (buffer-live-p source-buffer)
538 (with-current-buffer source-buffer
539 (funcall cleanup-f)))
540
541 (delete-process process)
542 (setq flymake-processes (delq process flymake-processes))
543
544 (when (buffer-live-p source-buffer)
545 (with-current-buffer source-buffer
546
547 (flymake-parse-residual)
548 (flymake-post-syntax-check exit-status command)
549 (setq flymake-is-running nil))))
550 (error
551 (let ((err-str (format "Error in process sentinel for buffer %s: %s"
552 source-buffer (error-message-string err))))
553 (flymake-log 0 err-str)
554 (with-current-buffer source-buffer
555 (setq flymake-is-running nil))))))))
4bcbcb9d 556
4dd68f44 557(defun flymake-post-syntax-check (exit-status command)
0b87142f
DC
558 (save-restriction
559 (widen)
560 (setq flymake-err-info flymake-new-err-info)
561 (setq flymake-new-err-info nil)
562 (setq flymake-err-info
563 (flymake-fix-line-numbers
564 flymake-err-info 1 (count-lines (point-min) (point-max))))
565 (flymake-delete-own-overlays)
566 (flymake-highlight-err-lines flymake-err-info)
567 (let (err-count warn-count)
568 (setq err-count (flymake-get-err-count flymake-err-info "e"))
569 (setq warn-count (flymake-get-err-count flymake-err-info "w"))
570 (flymake-log 2 "%s: %d error(s), %d warning(s) in %.2f second(s)"
571 (buffer-name) err-count warn-count
572 (- (float-time) flymake-check-start-time))
573 (setq flymake-check-start-time nil)
574
575 (if (and (equal 0 err-count) (equal 0 warn-count))
576 (if (equal 0 exit-status)
577 (flymake-report-status "" "") ; PASSED
578 (if (not flymake-check-was-interrupted)
579 (flymake-report-fatal-status "CFGERR"
580 (format "Configuration error has occurred while running %s" command))
581 (flymake-report-status nil ""))) ; "STOPPED"
582 (flymake-report-status (format "%d/%d" err-count warn-count) "")))))
4bcbcb9d 583
4dd68f44 584(defun flymake-parse-output-and-residual (output)
5323b4b0 585 "Split OUTPUT into lines, merge in residual if necessary."
4dd68f44
SM
586 (let* ((buffer-residual flymake-output-residual)
587 (total-output (if buffer-residual (concat buffer-residual output) output))
588 (lines-and-residual (flymake-split-output total-output))
589 (lines (nth 0 lines-and-residual))
590 (new-residual (nth 1 lines-and-residual)))
591 (setq flymake-output-residual new-residual)
592 (setq flymake-new-err-info
593 (flymake-parse-err-lines
594 flymake-new-err-info lines))))
595
596(defun flymake-parse-residual ()
5323b4b0 597 "Parse residual if it's non empty."
4dd68f44
SM
598 (when flymake-output-residual
599 (setq flymake-new-err-info
600 (flymake-parse-err-lines
601 flymake-new-err-info
602 (list flymake-output-residual)))
603 (setq flymake-output-residual nil)))
4bcbcb9d 604
2aa13ec4 605(defun flymake-er-make-er (line-no line-err-info-list)
5323b4b0 606 (list line-no line-err-info-list))
2aa13ec4
RS
607
608(defun flymake-er-get-line (err-info)
5323b4b0 609 (nth 0 err-info))
2aa13ec4
RS
610
611(defun flymake-er-get-line-err-info-list (err-info)
5323b4b0 612 (nth 1 err-info))
4bcbcb9d 613
a464a6c7 614(cl-defstruct (flymake-ler
587d108e
SM
615 (:constructor nil)
616 (:constructor flymake-ler-make-ler (file line type text &optional full-file)))
617 file line type text full-file)
2aa13ec4
RS
618
619(defun flymake-ler-set-file (line-err-info file)
5323b4b0 620 (flymake-ler-make-ler file
587d108e
SM
621 (flymake-ler-line line-err-info)
622 (flymake-ler-type line-err-info)
623 (flymake-ler-text line-err-info)
624 (flymake-ler-full-file line-err-info)))
2aa13ec4
RS
625
626(defun flymake-ler-set-full-file (line-err-info full-file)
587d108e
SM
627 (flymake-ler-make-ler (flymake-ler-file line-err-info)
628 (flymake-ler-line line-err-info)
629 (flymake-ler-type line-err-info)
630 (flymake-ler-text line-err-info)
2aa13ec4
RS
631 full-file))
632
633(defun flymake-ler-set-line (line-err-info line)
587d108e 634 (flymake-ler-make-ler (flymake-ler-file line-err-info)
4bcbcb9d 635 line
587d108e
SM
636 (flymake-ler-type line-err-info)
637 (flymake-ler-text line-err-info)
638 (flymake-ler-full-file line-err-info)))
4bcbcb9d 639
2aa13ec4 640(defun flymake-get-line-err-count (line-err-info-list type)
5323b4b0 641 "Return number of errors of specified TYPE.
173569aa 642Value of TYPE is either \"e\" or \"w\"."
5323b4b0
RS
643 (let* ((idx 0)
644 (count (length line-err-info-list))
645 (err-count 0))
4bcbcb9d 646
5323b4b0 647 (while (< idx count)
587d108e 648 (when (equal type (flymake-ler-type (nth idx line-err-info-list)))
5323b4b0
RS
649 (setq err-count (1+ err-count)))
650 (setq idx (1+ idx)))
651 err-count))
4bcbcb9d 652
2aa13ec4 653(defun flymake-get-err-count (err-info-list type)
5323b4b0
RS
654 "Return number of errors of specified TYPE for ERR-INFO-LIST."
655 (let* ((idx 0)
656 (count (length err-info-list))
657 (err-count 0))
658 (while (< idx count)
659 (setq err-count (+ err-count (flymake-get-line-err-count (nth 1 (nth idx err-info-list)) type)))
660 (setq idx (1+ idx)))
661 err-count))
2aa13ec4
RS
662
663(defun flymake-fix-line-numbers (err-info-list min-line max-line)
5323b4b0 664 "Replace line numbers with fixed value.
2aa13ec4
RS
665If line-numbers is less than MIN-LINE, set line numbers to MIN-LINE.
666If line numbers is greater than MAX-LINE, set line numbers to MAX-LINE.
ce721de4 667The reason for this fix is because some compilers might report
2aa13ec4 668line number outside the file being compiled."
5323b4b0
RS
669 (let* ((count (length err-info-list))
670 (err-info nil)
671 (line 0))
672 (while (> count 0)
673 (setq err-info (nth (1- count) err-info-list))
674 (setq line (flymake-er-get-line err-info))
675 (when (or (< line min-line) (> line max-line))
676 (setq line (if (< line min-line) min-line max-line))
677 (setq err-info-list (flymake-set-at err-info-list (1- count)
678 (flymake-er-make-er line
679 (flymake-er-get-line-err-info-list err-info)))))
680 (setq count (1- count))))
681 err-info-list)
4bcbcb9d 682
4dd68f44 683(defun flymake-highlight-err-lines (err-info-list)
5323b4b0 684 "Highlight error lines in BUFFER using info from ERR-INFO-LIST."
4dd68f44
SM
685 (save-excursion
686 (dolist (err err-info-list)
687 (flymake-highlight-line (car err) (nth 1 err)))))
4bcbcb9d 688
2aa13ec4 689(defun flymake-overlay-p (ov)
5323b4b0
RS
690 "Determine whether overlay OV was created by flymake."
691 (and (overlayp ov) (overlay-get ov 'flymake-overlay)))
4bcbcb9d 692
974ebc9c 693(defun flymake-make-overlay (beg end tooltip-text face bitmap)
5323b4b0
RS
694 "Allocate a flymake overlay in range BEG and END."
695 (when (not (flymake-region-has-flymake-overlays beg end))
e0c67e1d 696 (let ((ov (make-overlay beg end nil t))
28ca98ac
LL
697 (fringe (and flymake-fringe-indicator-position
698 (propertize "!" 'display
699 (cons flymake-fringe-indicator-position
700 (if (listp bitmap)
701 bitmap
702 (list bitmap)))))))
5323b4b0 703 (overlay-put ov 'face face)
5323b4b0
RS
704 (overlay-put ov 'help-echo tooltip-text)
705 (overlay-put ov 'flymake-overlay t)
706 (overlay-put ov 'priority 100)
28ca98ac
LL
707 (overlay-put ov 'evaporate t)
708 (overlay-put ov 'before-string fringe)
9a686ad2 709 ;;+(flymake-log 3 "created overlay %s" ov)
5323b4b0
RS
710 ov)
711 (flymake-log 3 "created an overlay at (%d-%d)" beg end)))
4bcbcb9d 712
4dd68f44 713(defun flymake-delete-own-overlays ()
5323b4b0 714 "Delete all flymake overlays in BUFFER."
4dd68f44
SM
715 (dolist (ol (overlays-in (point-min) (point-max)))
716 (when (flymake-overlay-p ol)
717 (delete-overlay ol)
718 ;;+(flymake-log 3 "deleted overlay %s" ol)
719 )))
4bcbcb9d 720
2aa13ec4 721(defun flymake-region-has-flymake-overlays (beg end)
5323b4b0 722 "Check if region specified by BEG and END has overlay.
2aa13ec4 723Return t if it has at least one flymake overlay, nil if no overlay."
5323b4b0
RS
724 (let ((ov (overlays-in beg end))
725 (has-flymake-overlays nil))
726 (while (consp ov)
727 (when (flymake-overlay-p (car ov))
728 (setq has-flymake-overlays t))
cd2325cd
SM
729 (setq ov (cdr ov)))
730 has-flymake-overlays))
4bcbcb9d 731
ded09abd 732(defface flymake-errline
073ca75b
JL
733 '((((supports :underline (:style wave)))
734 :underline (:style wave :color "Red1"))
735 (t
736 :inherit error))
5323b4b0 737 "Face used for marking error lines."
073ca75b 738 :version "24.4"
5323b4b0 739 :group 'flymake)
4bcbcb9d 740
ded09abd 741(defface flymake-warnline
073ca75b
JL
742 '((((supports :underline (:style wave)))
743 :underline (:style wave :color "DarkOrange"))
744 (t
745 :inherit warning))
5323b4b0 746 "Face used for marking warning lines."
073ca75b 747 :version "24.4"
5323b4b0 748 :group 'flymake)
4bcbcb9d 749
2aa13ec4 750(defun flymake-highlight-line (line-no line-err-info-list)
5323b4b0 751 "Highlight line LINE-NO in current buffer.
173569aa 752Perhaps use text from LINE-ERR-INFO-LIST to enhance highlighting."
5f68c1b7
GM
753 (goto-char (point-min))
754 (forward-line (1- line-no))
974ebc9c
LL
755 (pcase-let* ((beg (progn (back-to-indentation) (point)))
756 (end (progn
757 (end-of-line)
758 (skip-chars-backward " \t\f\t\n" beg)
759 (if (eq (point) beg)
760 (line-beginning-position 2)
761 (point))))
762 (tooltip-text (mapconcat #'flymake-ler-text line-err-info-list "\n"))
763 (`(,face ,bitmap)
764 (if (> (flymake-get-line-err-count line-err-info-list "e") 0)
765 (list 'flymake-errline flymake-error-bitmap)
766 (list 'flymake-warnline flymake-warning-bitmap))))
767 (flymake-make-overlay beg end tooltip-text face bitmap)))
4bcbcb9d 768
4dd68f44 769(defun flymake-parse-err-lines (err-info-list lines)
5323b4b0
RS
770 "Parse err LINES, store info in ERR-INFO-LIST."
771 (let* ((count (length lines))
772 (idx 0)
773 (line-err-info nil)
774 (real-file-name nil)
4dd68f44 775 (source-file-name buffer-file-name)
5323b4b0
RS
776 (get-real-file-name-f (flymake-get-real-file-name-function source-file-name)))
777
778 (while (< idx count)
779 (setq line-err-info (flymake-parse-line (nth idx lines)))
780 (when line-err-info
c07fa030 781 (setq real-file-name (funcall get-real-file-name-f
587d108e 782 (flymake-ler-file line-err-info)))
5323b4b0
RS
783 (setq line-err-info (flymake-ler-set-full-file line-err-info real-file-name))
784
b89b385c
CY
785 (when (flymake-same-files real-file-name source-file-name)
786 (setq line-err-info (flymake-ler-set-file line-err-info nil))
787 (setq err-info-list (flymake-add-err-info err-info-list line-err-info))))
5323b4b0
RS
788 (flymake-log 3 "parsed '%s', %s line-err-info" (nth idx lines) (if line-err-info "got" "no"))
789 (setq idx (1+ idx)))
790 err-info-list))
4bcbcb9d 791
2aa13ec4 792(defun flymake-split-output (output)
5323b4b0 793 "Split OUTPUT into lines.
9a686ad2
SM
794Return last one as residual if it does not end with newline char.
795Returns ((LINES) RESIDUAL)."
5323b4b0 796 (when (and output (> (length output) 0))
a92fa5f1 797 (let* ((lines (split-string output "[\n\r]+" t))
5323b4b0
RS
798 (complete (equal "\n" (char-to-string (aref output (1- (length output))))))
799 (residual nil))
800 (when (not complete)
801 (setq residual (car (last lines)))
802 (setq lines (butlast lines)))
803 (list lines residual))))
4bcbcb9d 804
2aa13ec4 805(defun flymake-reformat-err-line-patterns-from-compile-el (original-list)
5323b4b0 806 "Grab error line patterns from ORIGINAL-LIST in compile.el format.
2aa13ec4 807Convert it to flymake internal format."
5323b4b0 808 (let* ((converted-list '()))
cd2325cd
SM
809 (dolist (item original-list)
810 (setq item (cdr item))
811 (let ((regexp (nth 0 item))
812 (file (nth 1 item))
813 (line (nth 2 item))
814 (col (nth 3 item)))
815 (if (consp file) (setq file (car file)))
816 (if (consp line) (setq line (car line)))
817 (if (consp col) (setq col (car col)))
d799c278 818
cd2325cd
SM
819 (when (not (functionp line))
820 (setq converted-list (cons (list regexp file line col) converted-list)))))
5323b4b0 821 converted-list))
2aa13ec4 822
c3b8339f 823(require 'compile)
5323b4b0
RS
824
825(defvar flymake-err-line-patterns ; regexp file-idx line-idx col-idx (optional) text-idx(optional), match-end to end of string is error text
826 (append
827 '(
9a686ad2 828 ;; MS Visual C++ 6.0
5323b4b0
RS
829 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\)) \: \\(\\(error\\|warning\\|fatal error\\) \\(C[0-9]+\\):[ \t\n]*\\(.+\\)\\)"
830 1 3 nil 4)
9a686ad2 831 ;; jikes
5323b4b0
RS
832 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)\:\\([0-9]+\\)\:[0-9]+\:[0-9]+\:[0-9]+\: \\(\\(Error\\|Warning\\|Caution\\|Semantic Error\\):[ \t\n]*\\(.+\\)\\)"
833 1 3 nil 4)
9a686ad2 834 ;; MS midl
5323b4b0
RS
835 ("midl[ ]*:[ ]*\\(command line error .*\\)"
836 nil nil nil 1)
9a686ad2 837 ;; MS C#
5323b4b0
RS
838 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\),[0-9]+)\: \\(\\(error\\|warning\\|fatal error\\) \\(CS[0-9]+\\):[ \t\n]*\\(.+\\)\\)"
839 1 3 nil 4)
9a686ad2 840 ;; perl
5323b4b0 841 ("\\(.*\\) at \\([^ \n]+\\) line \\([0-9]+\\)[,.\n]" 2 3 nil 1)
eabd11d4
MH
842 ;; PHP
843 ("\\(?:Parse\\|Fatal\\) error: \\(.*\\) in \\(.*\\) on line \\([0-9]+\\)" 2 3 nil 1)
9a686ad2 844 ;; LaTeX warnings (fileless) ("\\(LaTeX \\(Warning\\|Error\\): .*\\) on input line \\([0-9]+\\)" 20 3 nil 1)
819a6054
GM
845 ;; ant/javac. Note this also matches gcc warnings!
846 (" *\\(\\[javac\\] *\\)?\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)\:\\([0-9]+\\)\\(?:\:[0-9]+\\)?\:[ \t\n]*\\(.+\\)"
5323b4b0
RS
847 2 4 nil 5))
848 ;; compilation-error-regexp-alist)
ce721de4 849 (flymake-reformat-err-line-patterns-from-compile-el compilation-error-regexp-alist-alist))
8c01db22
EZ
850 "Patterns for matching error/warning lines. Each pattern has the form
851\(REGEXP FILE-IDX LINE-IDX COL-IDX ERR-TEXT-IDX).
9a686ad2
SM
852Use `flymake-reformat-err-line-patterns-from-compile-el' to add patterns
853from compile.el")
854
c5b0993e
EW
855(define-obsolete-variable-alias 'flymake-warning-re 'flymake-warning-predicate "24.4")
856(defvar flymake-warning-predicate "^[wW]arning"
857 "Predicate matching against error text to detect a warning.
858Takes a single argument, the error's text and should return non-nil
859if it's a warning.
860Instead of a function, it can also be a regular expression.")
3aca1291 861
2aa13ec4 862(defun flymake-parse-line (line)
173569aa
JB
863 "Parse LINE to see if it is an error or warning.
864Return its components if so, nil otherwise."
5323b4b0
RS
865 (let ((raw-file-name nil)
866 (line-no 0)
867 (err-type "e")
868 (err-text nil)
17404091 869 (patterns flymake-err-line-patterns)
5323b4b0 870 (matched nil))
17404091
SM
871 (while (and patterns (not matched))
872 (when (string-match (car (car patterns)) line)
873 (let* ((file-idx (nth 1 (car patterns)))
874 (line-idx (nth 2 (car patterns))))
5323b4b0
RS
875
876 (setq raw-file-name (if file-idx (match-string file-idx line) nil))
c5b0993e
EW
877 (setq line-no (if line-idx (string-to-number
878 (match-string line-idx line)) 0))
17404091
SM
879 (setq err-text (if (> (length (car patterns)) 4)
880 (match-string (nth 4 (car patterns)) line)
c5b0993e
EW
881 (flymake-patch-err-text
882 (substring line (match-end 0)))))
883 (if (null err-text)
884 (setq err-text "<no error text>")
885 (when (cond ((stringp flymake-warning-predicate)
886 (string-match flymake-warning-predicate err-text))
887 ((functionp flymake-warning-predicate)
888 (funcall flymake-warning-predicate err-text)))
889 (setq err-type "w")))
890 (flymake-log
891 3 "parse line: file-idx=%s line-idx=%s file=%s line=%s text=%s"
892 file-idx line-idx raw-file-name line-no err-text)
5323b4b0 893 (setq matched t)))
17404091 894 (setq patterns (cdr patterns)))
5323b4b0
RS
895 (if matched
896 (flymake-ler-make-ler raw-file-name line-no err-type err-text)
897 ())))
4bcbcb9d 898
2aa13ec4 899(defun flymake-find-err-info (err-info-list line-no)
5323b4b0
RS
900 "Find (line-err-info-list pos) for specified LINE-NO."
901 (if err-info-list
902 (let* ((line-err-info-list nil)
903 (pos 0)
904 (count (length err-info-list)))
905
906 (while (and (< pos count) (< (car (nth pos err-info-list)) line-no))
907 (setq pos (1+ pos)))
908 (when (and (< pos count) (equal (car (nth pos err-info-list)) line-no))
909 (setq line-err-info-list (flymake-er-get-line-err-info-list (nth pos err-info-list))))
910 (list line-err-info-list pos))
911 '(nil 0)))
4bcbcb9d 912
2aa13ec4 913(defun flymake-line-err-info-is-less-or-equal (line-one line-two)
587d108e
SM
914 (or (string< (flymake-ler-type line-one) (flymake-ler-type line-two))
915 (and (string= (flymake-ler-type line-one) (flymake-ler-type line-two))
916 (not (flymake-ler-file line-one)) (flymake-ler-file line-two))
917 (and (string= (flymake-ler-type line-one) (flymake-ler-type line-two))
918 (or (and (flymake-ler-file line-one) (flymake-ler-file line-two))
919 (and (not (flymake-ler-file line-one)) (not (flymake-ler-file line-two)))))))
4bcbcb9d 920
2aa13ec4 921(defun flymake-add-line-err-info (line-err-info-list line-err-info)
53ec26ed
RS
922 "Update LINE-ERR-INFO-LIST with the error LINE-ERR-INFO.
923For the format of LINE-ERR-INFO, see `flymake-ler-make-ler'.
924The new element is inserted in the proper position, according to
925the predicate `flymake-line-err-info-is-less-or-equal'.
926The updated value of LINE-ERR-INFO-LIST is returned."
5323b4b0
RS
927 (if (not line-err-info-list)
928 (list line-err-info)
929 (let* ((count (length line-err-info-list))
930 (idx 0))
931 (while (and (< idx count) (flymake-line-err-info-is-less-or-equal (nth idx line-err-info-list) line-err-info))
932 (setq idx (1+ idx)))
933 (cond ((equal 0 idx) (setq line-err-info-list (cons line-err-info line-err-info-list)))
934 (t (setq line-err-info-list (flymake-ins-after line-err-info-list (1- idx) line-err-info))))
935 line-err-info-list)))
4bcbcb9d 936
2aa13ec4 937(defun flymake-add-err-info (err-info-list line-err-info)
53ec26ed
RS
938 "Update ERR-INFO-LIST with the error LINE-ERR-INFO, preserving sort order.
939Returns the updated value of ERR-INFO-LIST.
940For the format of ERR-INFO-LIST, see `flymake-err-info'.
941For the format of LINE-ERR-INFO, see `flymake-ler-make-ler'."
587d108e 942 (let* ((line-no (if (flymake-ler-file line-err-info) 1 (flymake-ler-line line-err-info)))
5323b4b0
RS
943 (info-and-pos (flymake-find-err-info err-info-list line-no))
944 (exists (car info-and-pos))
945 (pos (nth 1 info-and-pos))
946 (line-err-info-list nil)
947 (err-info nil))
948
949 (if exists
950 (setq line-err-info-list (flymake-er-get-line-err-info-list (car (nthcdr pos err-info-list)))))
951 (setq line-err-info-list (flymake-add-line-err-info line-err-info-list line-err-info))
952
953 (setq err-info (flymake-er-make-er line-no line-err-info-list))
954 (cond (exists (setq err-info-list (flymake-set-at err-info-list pos err-info)))
955 ((equal 0 pos) (setq err-info-list (cons err-info err-info-list)))
956 (t (setq err-info-list (flymake-ins-after err-info-list (1- pos) err-info))))
957 err-info-list))
4bcbcb9d 958
2aa13ec4 959(defun flymake-get-project-include-dirs-imp (basedir)
5323b4b0
RS
960 "Include dirs for the project current file belongs to."
961 (if (flymake-get-project-include-dirs-from-cache basedir)
962 (progn
963 (flymake-get-project-include-dirs-from-cache basedir))
9a686ad2 964 ;;else
ff4e2883
KS
965 (let* ((command-line (concat "make -C "
966 (shell-quote-argument basedir)
967 " DUMPVARS=INCLUDE_DIRS dumpvars"))
5323b4b0 968 (output (shell-command-to-string command-line))
a92fa5f1 969 (lines (split-string output "\n" t))
5323b4b0
RS
970 (count (length lines))
971 (idx 0)
972 (inc-dirs nil))
973 (while (and (< idx count) (not (string-match "^INCLUDE_DIRS=.*" (nth idx lines))))
974 (setq idx (1+ idx)))
975 (when (< idx count)
a92fa5f1 976 (let* ((inc-lines (split-string (nth idx lines) " *-I" t))
5323b4b0
RS
977 (inc-count (length inc-lines)))
978 (while (> inc-count 0)
979 (when (not (string-match "^INCLUDE_DIRS=.*" (nth (1- inc-count) inc-lines)))
a92fa5f1 980 (push (replace-regexp-in-string "\"" "" (nth (1- inc-count) inc-lines)) inc-dirs))
5323b4b0
RS
981 (setq inc-count (1- inc-count)))))
982 (flymake-add-project-include-dirs-to-cache basedir inc-dirs)
983 inc-dirs)))
4bcbcb9d 984
a92fa5f1
LL
985(defvar flymake-get-project-include-dirs-function #'flymake-get-project-include-dirs-imp
986 "Function used to get project include dirs, one parameter: basedir name.")
4bcbcb9d 987
2aa13ec4 988(defun flymake-get-project-include-dirs (basedir)
5323b4b0 989 (funcall flymake-get-project-include-dirs-function basedir))
4bcbcb9d 990
2aa13ec4 991(defun flymake-get-system-include-dirs ()
5323b4b0
RS
992 "System include dirs - from the 'INCLUDE' env setting."
993 (let* ((includes (getenv "INCLUDE")))
a92fa5f1 994 (if includes (split-string includes path-separator t) nil)))
4bcbcb9d 995
a92fa5f1 996(defvar flymake-project-include-dirs-cache (make-hash-table :test #'equal))
4bcbcb9d 997
2aa13ec4 998(defun flymake-get-project-include-dirs-from-cache (base-dir)
5323b4b0 999 (gethash base-dir flymake-project-include-dirs-cache))
2aa13ec4
RS
1000
1001(defun flymake-add-project-include-dirs-to-cache (base-dir include-dirs)
5323b4b0 1002 (puthash base-dir include-dirs flymake-project-include-dirs-cache))
2aa13ec4
RS
1003
1004(defun flymake-clear-project-include-dirs-cache ()
5323b4b0 1005 (clrhash flymake-project-include-dirs-cache))
2aa13ec4
RS
1006
1007(defun flymake-get-include-dirs (base-dir)
5323b4b0
RS
1008 "Get dirs to use when resolving local file names."
1009 (let* ((include-dirs (append '(".") (flymake-get-project-include-dirs base-dir) (flymake-get-system-include-dirs))))
1010 include-dirs))
4bcbcb9d 1011
4dd68f44
SM
1012;; (defun flymake-restore-formatting ()
1013;; "Remove any formatting made by flymake."
1014;; )
4bcbcb9d 1015
c07fa030
SM
1016;; (defun flymake-get-program-dir (buffer)
1017;; "Get dir to start program in."
1018;; (unless (bufferp buffer)
1019;; (error "Invalid buffer"))
1020;; (with-current-buffer buffer
1021;; default-directory))
4bcbcb9d 1022
2aa13ec4 1023(defun flymake-safe-delete-file (file-name)
5323b4b0
RS
1024 (when (and file-name (file-exists-p file-name))
1025 (delete-file file-name)
1026 (flymake-log 1 "deleted file %s" file-name)))
4bcbcb9d 1027
2aa13ec4 1028(defun flymake-safe-delete-directory (dir-name)
e02f48d7 1029 (condition-case nil
5323b4b0
RS
1030 (progn
1031 (delete-directory dir-name)
1032 (flymake-log 1 "deleted dir %s" dir-name))
1033 (error
1034 (flymake-log 1 "Failed to delete dir %s, error ignored" dir-name))))
4bcbcb9d 1035
4dd68f44
SM
1036(defun flymake-start-syntax-check ()
1037 "Start syntax checking for current buffer."
1038 (interactive)
1039 (flymake-log 3 "flymake is running: %s" flymake-is-running)
1040 (when (and (not flymake-is-running)
1041 (flymake-can-syntax-check-file buffer-file-name))
1042 (when (or (not flymake-compilation-prevents-syntax-check)
1043 (not (flymake-compilation-is-running))) ;+ (flymake-rep-ort-status buffer "COMP")
1044 (flymake-clear-buildfile-cache)
1045 (flymake-clear-project-include-dirs-cache)
1046
1047 (setq flymake-check-was-interrupted nil)
4dd68f44
SM
1048
1049 (let* ((source-file-name buffer-file-name)
1050 (init-f (flymake-get-init-function source-file-name))
1051 (cleanup-f (flymake-get-cleanup-function source-file-name))
6df19241 1052 (cmd-and-args (funcall init-f))
4dd68f44
SM
1053 (cmd (nth 0 cmd-and-args))
1054 (args (nth 1 cmd-and-args))
1055 (dir (nth 2 cmd-and-args)))
1056 (if (not cmd-and-args)
1057 (progn
1058 (flymake-log 0 "init function %s for %s failed, cleaning up" init-f source-file-name)
c07fa030 1059 (funcall cleanup-f))
4dd68f44
SM
1060 (progn
1061 (setq flymake-last-change-time nil)
1062 (flymake-start-syntax-check-process cmd args dir)))))))
1063
1064(defun flymake-start-syntax-check-process (cmd args dir)
5323b4b0 1065 "Start syntax check process."
c79a6f38
SM
1066 (condition-case err
1067 (let* ((process
1068 (let ((default-directory (or dir default-directory)))
1069 (when dir
1070 (flymake-log 3 "starting process on dir %s" dir))
1071 (apply 'start-file-process
1072 "flymake-proc" (current-buffer) cmd args))))
1073 (set-process-sentinel process 'flymake-process-sentinel)
1074 (set-process-filter process 'flymake-process-filter)
1075 (push process flymake-processes)
1076
1077 (setq flymake-is-running t)
1078 (setq flymake-last-change-time nil)
a92fa5f1 1079 (setq flymake-check-start-time (float-time))
c79a6f38
SM
1080
1081 (flymake-report-status nil "*")
1082 (flymake-log 2 "started process %d, command=%s, dir=%s"
1083 (process-id process) (process-command process)
1084 default-directory)
1085 process)
1086 (error
1087 (let* ((err-str (format "Failed to launch syntax check process '%s' with args %s: %s"
1088 cmd args (error-message-string err)))
1089 (source-file-name buffer-file-name)
1090 (cleanup-f (flymake-get-cleanup-function source-file-name)))
1091 (flymake-log 0 err-str)
1092 (funcall cleanup-f)
1093 (flymake-report-fatal-status "PROCERR" err-str)))))
4bcbcb9d 1094
43ed65ac
SM
1095(defun flymake-kill-process (proc)
1096 "Kill process PROC."
1097 (kill-process proc)
1098 (let* ((buf (process-buffer proc)))
1099 (when (buffer-live-p buf)
1100 (with-current-buffer buf
53ec26ed 1101 (setq flymake-check-was-interrupted t))))
43ed65ac 1102 (flymake-log 1 "killed process %d" (process-id proc)))
4bcbcb9d 1103
2aa13ec4 1104(defun flymake-stop-all-syntax-checks ()
5323b4b0
RS
1105 "Kill all syntax check processes."
1106 (interactive)
43ed65ac
SM
1107 (while flymake-processes
1108 (flymake-kill-process (pop flymake-processes))))
4bcbcb9d 1109
2aa13ec4 1110(defun flymake-compilation-is-running ()
5323b4b0
RS
1111 (and (boundp 'compilation-in-progress)
1112 compilation-in-progress))
4bcbcb9d 1113
2aa13ec4 1114(defun flymake-compile ()
5323b4b0
RS
1115 "Kill all flymake syntax checks, start compilation."
1116 (interactive)
1117 (flymake-stop-all-syntax-checks)
1118 (call-interactively 'compile))
4bcbcb9d 1119
2aa13ec4 1120(defun flymake-on-timer-event (buffer)
5323b4b0 1121 "Start a syntax check for buffer BUFFER if necessary."
587d108e 1122 (when (buffer-live-p buffer)
cd2325cd 1123 (with-current-buffer buffer
53ec26ed
RS
1124 (when (and (not flymake-is-running)
1125 flymake-last-change-time
a92fa5f1 1126 (> (- (float-time) flymake-last-change-time)
587d108e 1127 flymake-no-changes-timeout))
53ec26ed
RS
1128
1129 (setq flymake-last-change-time nil)
5323b4b0 1130 (flymake-log 3 "starting syntax check as more than 1 second passed since last change")
4dd68f44 1131 (flymake-start-syntax-check)))))
4bcbcb9d 1132
6407822c
LL
1133(define-obsolete-function-alias 'flymake-display-err-menu-for-current-line
1134 'flymake-popup-current-error-menu "24.4")
1135
1136(defun flymake-popup-current-error-menu (&optional event)
1137 "Pop up a menu with errors/warnings for current line."
1138 (interactive (list last-nonmenu-event))
1139 (let* ((line-no (line-number-at-pos))
1140 (errors (or (car (flymake-find-err-info flymake-err-info line-no))
1141 (user-error "No errors for current line")))
1142 (menu (mapcar (lambda (x)
1143 (if (flymake-ler-file x)
1144 (cons (format "%s - %s(%d)"
1145 (flymake-ler-text x)
1146 (flymake-ler-file x)
1147 (flymake-ler-line x))
1148 x)
1149 (list (flymake-ler-text x))))
1150 errors))
1151 (event (if (mouse-event-p event)
1152 event
1153 (list 'mouse-1 (posn-at-point))))
1154 (title (format "Line %d: %d error(s), %d warning(s)"
1155 line-no
1156 (flymake-get-line-err-count errors "e")
1157 (flymake-get-line-err-count errors "w")))
1158 (choice (x-popup-menu event (list title (cons "" menu)))))
1159 (flymake-log 3 "choice=%s" choice)
1160 (when choice
1161 (flymake-goto-file-and-line (flymake-ler-full-file choice)
1162 (flymake-ler-line choice)))))
4bcbcb9d 1163
2aa13ec4 1164(defun flymake-goto-file-and-line (file line)
9a686ad2 1165 "Try to get buffer for FILE and goto line LINE in it."
5323b4b0 1166 (if (not (file-exists-p file))
caf790b6
MR
1167 (flymake-log 1 "File %s does not exist" file)
1168 (find-file file)
5f68c1b7
GM
1169 (goto-char (point-min))
1170 (forward-line (1- line))))
4bcbcb9d 1171
2aa13ec4 1172;; flymake minor mode declarations
a92fa5f1
LL
1173(defvar-local flymake-mode-line nil)
1174(defvar-local flymake-mode-line-e-w nil)
1175(defvar-local flymake-mode-line-status nil)
4bcbcb9d 1176
4dd68f44 1177(defun flymake-report-status (e-w &optional status)
5323b4b0 1178 "Show status in mode line."
4dd68f44
SM
1179 (when e-w
1180 (setq flymake-mode-line-e-w e-w))
1181 (when status
1182 (setq flymake-mode-line-status status))
1183 (let* ((mode-line " Flymake"))
1184 (when (> (length flymake-mode-line-e-w) 0)
1185 (setq mode-line (concat mode-line ":" flymake-mode-line-e-w)))
1186 (setq mode-line (concat mode-line flymake-mode-line-status))
1187 (setq flymake-mode-line mode-line)
1188 (force-mode-line-update)))
4bcbcb9d 1189
2aa13ec4 1190(defun flymake-display-warning (warning)
5323b4b0
RS
1191 "Display a warning to user."
1192 (message-box warning))
4bcbcb9d 1193
4dd68f44 1194(defun flymake-report-fatal-status (status warning)
5323b4b0
RS
1195 "Display a warning and switch flymake mode off."
1196 (when flymake-gui-warnings-enabled
1197 (flymake-display-warning (format "Flymake: %s. Flymake will be switched OFF" warning))
1198 )
4dd68f44
SM
1199 (flymake-mode 0)
1200 (flymake-log 0 "switched OFF Flymake mode for buffer %s due to fatal status %s, warning %s"
1201 (buffer-name) status warning))
4bcbcb9d 1202
5ffc943b 1203;;;###autoload
a92fa5f1 1204(define-minor-mode flymake-mode nil
d799c278 1205 :group 'flymake :lighter flymake-mode-line
d57f6b5e
SM
1206 (cond
1207
1208 ;; Turning the mode ON.
1209 (flymake-mode
83319045
LMI
1210 (cond
1211 ((not buffer-file-name)
1212 (message "Flymake unable to run without a buffer file name"))
1213 ((not (flymake-can-syntax-check-file buffer-file-name))
1214 (flymake-log 2 "flymake cannot check syntax in buffer %s" (buffer-name)))
1215 (t
d57f6b5e
SM
1216 (add-hook 'after-change-functions 'flymake-after-change-function nil t)
1217 (add-hook 'after-save-hook 'flymake-after-save-hook nil t)
1218 (add-hook 'kill-buffer-hook 'flymake-kill-buffer-hook nil t)
1219 ;;+(add-hook 'find-file-hook 'flymake-find-file-hook)
1220
4dd68f44 1221 (flymake-report-status "" "")
c92d6023 1222
d57f6b5e
SM
1223 (setq flymake-timer
1224 (run-at-time nil 1 'flymake-on-timer-event (current-buffer)))
1225
0221e323
SM
1226 (when (and flymake-start-syntax-check-on-find-file
1227 ;; Since we write temp files in current dir, there's no point
1228 ;; trying if the directory is read-only (bug#8954).
1229 (file-writable-p (file-name-directory buffer-file-name)))
1230 (with-demoted-errors
1231 (flymake-start-syntax-check))))))
d57f6b5e
SM
1232
1233 ;; Turning the mode OFF.
1234 (t
1235 (remove-hook 'after-change-functions 'flymake-after-change-function t)
1236 (remove-hook 'after-save-hook 'flymake-after-save-hook t)
1237 (remove-hook 'kill-buffer-hook 'flymake-kill-buffer-hook t)
1238 ;;+(remove-hook 'find-file-hook (function flymake-find-file-hook) t)
2aa13ec4 1239
4dd68f44 1240 (flymake-delete-own-overlays)
d57f6b5e
SM
1241
1242 (when flymake-timer
1243 (cancel-timer flymake-timer)
1244 (setq flymake-timer nil))
1245
bd6a83d5 1246 (setq flymake-is-running nil))))
4bcbcb9d 1247
4bcbcb9d 1248;;;###autoload
2aa13ec4 1249(defun flymake-mode-on ()
5323b4b0 1250 "Turn flymake mode on."
d57f6b5e
SM
1251 (flymake-mode 1)
1252 (flymake-log 1 "flymake mode turned ON for buffer %s" (buffer-name)))
4bcbcb9d
EZ
1253
1254;;;###autoload
2aa13ec4 1255(defun flymake-mode-off ()
5323b4b0 1256 "Turn flymake mode off."
d57f6b5e 1257 (flymake-mode 0)
bd6a83d5 1258 (flymake-log 1 "flymake mode turned OFF for buffer %s" (buffer-name)))
4bcbcb9d 1259
e02f48d7 1260(defun flymake-after-change-function (start stop _len)
9a686ad2 1261 "Start syntax check for current buffer if it isn't already running."
a92fa5f1 1262 ;;+(flymake-log 0 "setting change time to %s" (float-time))
5323b4b0
RS
1263 (let((new-text (buffer-substring start stop)))
1264 (when (and flymake-start-syntax-check-on-newline (equal new-text "\n"))
1265 (flymake-log 3 "starting syntax check as new-line has been seen")
4dd68f44 1266 (flymake-start-syntax-check))
a92fa5f1 1267 (setq flymake-last-change-time (float-time))))
4bcbcb9d 1268
2aa13ec4 1269(defun flymake-after-save-hook ()
5323b4b0
RS
1270 (if (local-variable-p 'flymake-mode (current-buffer)) ; (???) other way to determine whether flymake is active in buffer being saved?
1271 (progn
1272 (flymake-log 3 "starting syntax check as buffer was saved")
4dd68f44 1273 (flymake-start-syntax-check)))) ; no more mode 3. cannot start check if mode 3 (to temp copies) is active - (???)
4bcbcb9d 1274
2aa13ec4 1275(defun flymake-kill-buffer-hook ()
53ec26ed
RS
1276 (when flymake-timer
1277 (cancel-timer flymake-timer)
1278 (setq flymake-timer nil)))
4bcbcb9d 1279
5bd35902 1280;;;###autoload
2aa13ec4 1281(defun flymake-find-file-hook ()
9a686ad2
SM
1282 ;;+(when flymake-start-syntax-check-on-find-file
1283 ;;+ (flymake-log 3 "starting syntax check on file open")
4dd68f44 1284 ;;+ (flymake-start-syntax-check)
9a686ad2 1285 ;;+)
5323b4b0 1286 (when (and (not (local-variable-p 'flymake-mode (current-buffer)))
d57f6b5e 1287 (flymake-can-syntax-check-file buffer-file-name))
5323b4b0
RS
1288 (flymake-mode)
1289 (flymake-log 3 "automatically turned ON flymake mode")))
4bcbcb9d 1290
2aa13ec4 1291(defun flymake-get-first-err-line-no (err-info-list)
5323b4b0
RS
1292 "Return first line with error."
1293 (when err-info-list
1294 (flymake-er-get-line (car err-info-list))))
4bcbcb9d 1295
2aa13ec4 1296(defun flymake-get-last-err-line-no (err-info-list)
5323b4b0
RS
1297 "Return last line with error."
1298 (when err-info-list
1299 (flymake-er-get-line (nth (1- (length err-info-list)) err-info-list))))
4bcbcb9d 1300
2aa13ec4 1301(defun flymake-get-next-err-line-no (err-info-list line-no)
5323b4b0
RS
1302 "Return next line with error."
1303 (when err-info-list
1304 (let* ((count (length err-info-list))
1305 (idx 0))
1306 (while (and (< idx count) (>= line-no (flymake-er-get-line (nth idx err-info-list))))
1307 (setq idx (1+ idx)))
1308 (if (< idx count)
1309 (flymake-er-get-line (nth idx err-info-list))))))
4bcbcb9d 1310
2aa13ec4 1311(defun flymake-get-prev-err-line-no (err-info-list line-no)
173569aa 1312 "Return previous line with error."
5323b4b0
RS
1313 (when err-info-list
1314 (let* ((count (length err-info-list)))
1315 (while (and (> count 0) (<= line-no (flymake-er-get-line (nth (1- count) err-info-list))))
1316 (setq count (1- count)))
1317 (if (> count 0)
1318 (flymake-er-get-line (nth (1- count) err-info-list))))))
4bcbcb9d 1319
2aa13ec4 1320(defun flymake-skip-whitespace ()
5323b4b0
RS
1321 "Move forward until non-whitespace is reached."
1322 (while (looking-at "[ \t]")
1323 (forward-char)))
4bcbcb9d 1324
2aa13ec4 1325(defun flymake-goto-line (line-no)
9a686ad2 1326 "Go to line LINE-NO, then skip whitespace."
5f68c1b7
GM
1327 (goto-char (point-min))
1328 (forward-line (1- line-no))
5323b4b0 1329 (flymake-skip-whitespace))
4bcbcb9d 1330
2aa13ec4 1331(defun flymake-goto-next-error ()
9a686ad2 1332 "Go to next error in err ring."
5323b4b0 1333 (interactive)
a92fa5f1 1334 (let ((line-no (flymake-get-next-err-line-no flymake-err-info (line-number-at-pos))))
5323b4b0 1335 (when (not line-no)
53ec26ed 1336 (setq line-no (flymake-get-first-err-line-no flymake-err-info))
5323b4b0
RS
1337 (flymake-log 1 "passed end of file"))
1338 (if line-no
1339 (flymake-goto-line line-no)
1340 (flymake-log 1 "no errors in current buffer"))))
4bcbcb9d 1341
2aa13ec4 1342(defun flymake-goto-prev-error ()
173569aa 1343 "Go to previous error in err ring."
5323b4b0 1344 (interactive)
a92fa5f1 1345 (let ((line-no (flymake-get-prev-err-line-no flymake-err-info (line-number-at-pos))))
5323b4b0 1346 (when (not line-no)
53ec26ed 1347 (setq line-no (flymake-get-last-err-line-no flymake-err-info))
5323b4b0
RS
1348 (flymake-log 1 "passed beginning of file"))
1349 (if line-no
1350 (flymake-goto-line line-no)
1351 (flymake-log 1 "no errors in current buffer"))))
4bcbcb9d 1352
2aa13ec4 1353(defun flymake-patch-err-text (string)
5323b4b0
RS
1354 (if (string-match "^[\n\t :0-9]*\\(.*\\)$" string)
1355 (match-string 1 string)
1356 string))
4bcbcb9d
EZ
1357
1358;;;; general init-cleanup and helper routines
2aa13ec4 1359(defun flymake-create-temp-inplace (file-name prefix)
5323b4b0
RS
1360 (unless (stringp file-name)
1361 (error "Invalid file-name"))
1362 (or prefix
1363 (setq prefix "flymake"))
79e1997a
AJ
1364 (let* ((ext (file-name-extension file-name))
1365 (temp-name (file-truename
1366 (concat (file-name-sans-extension file-name)
1367 "_" prefix
1368 (and ext (concat "." ext))))))
5323b4b0
RS
1369 (flymake-log 3 "create-temp-inplace: file=%s temp=%s" file-name temp-name)
1370 temp-name))
4bcbcb9d 1371
e02f48d7 1372(defun flymake-create-temp-with-folder-structure (file-name _prefix)
5323b4b0
RS
1373 (unless (stringp file-name)
1374 (error "Invalid file-name"))
4bcbcb9d 1375
5323b4b0 1376 (let* ((dir (file-name-directory file-name))
6df19241
SM
1377 ;; Not sure what this slash-pos is all about, but I guess it's just
1378 ;; trying to remove the leading / of absolute file names.
5323b4b0 1379 (slash-pos (string-match "/" dir))
6df19241 1380 (temp-dir (expand-file-name (substring dir (1+ slash-pos))
a92fa5f1 1381 temporary-file-directory)))
4bcbcb9d 1382
6df19241
SM
1383 (file-truename (expand-file-name (file-name-nondirectory file-name)
1384 temp-dir))))
4bcbcb9d 1385
2aa13ec4 1386(defun flymake-delete-temp-directory (dir-name)
9a686ad2 1387 "Attempt to delete temp dir created by `flymake-create-temp-with-folder-structure', do not fail on error."
a92fa5f1 1388 (let* ((temp-dir temporary-file-directory)
587d108e 1389 (suffix (substring dir-name (1+ (length temp-dir)))))
5323b4b0
RS
1390
1391 (while (> (length suffix) 0)
6df19241 1392 (setq suffix (directory-file-name suffix))
9a686ad2 1393 ;;+(flymake-log 0 "suffix=%s" suffix)
6df19241
SM
1394 (flymake-safe-delete-directory
1395 (file-truename (expand-file-name suffix temp-dir)))
1396 (setq suffix (file-name-directory suffix)))))
4bcbcb9d 1397
a92fa5f1
LL
1398(defvar-local flymake-temp-source-file-name nil)
1399(defvar-local flymake-master-file-name nil)
1400(defvar-local flymake-temp-master-file-name nil)
1401(defvar-local flymake-base-dir nil)
5bcef417 1402
6df19241 1403(defun flymake-init-create-temp-buffer-copy (create-temp-f)
5323b4b0 1404 "Make a temporary copy of the current buffer, save its name in buffer data and return the name."
6df19241 1405 (let* ((source-file-name buffer-file-name)
5323b4b0 1406 (temp-source-file-name (funcall create-temp-f source-file-name "flymake")))
4bcbcb9d 1407
6df19241
SM
1408 (flymake-save-buffer-in-file temp-source-file-name)
1409 (setq flymake-temp-source-file-name temp-source-file-name)
5323b4b0 1410 temp-source-file-name))
4bcbcb9d 1411
c07fa030 1412(defun flymake-simple-cleanup ()
69df8d97 1413 "Do cleanup after `flymake-init-create-temp-buffer-copy'.
2aa13ec4 1414Delete temp file."
c07fa030
SM
1415 (flymake-safe-delete-file flymake-temp-source-file-name)
1416 (setq flymake-last-change-time nil))
4bcbcb9d 1417
c07fa030 1418(defun flymake-get-real-file-name (file-name-from-err-msg)
9a686ad2
SM
1419 "Translate file name from error message to \"real\" file name.
1420Return full-name. Names are real, not patched."
c07fa030
SM
1421 (let* ((real-name nil)
1422 (source-file-name buffer-file-name)
1423 (master-file-name flymake-master-file-name)
1424 (temp-source-file-name flymake-temp-source-file-name)
1425 (temp-master-file-name flymake-temp-master-file-name)
5bcef417 1426 (base-dirs
c07fa030 1427 (list flymake-base-dir
5bcef417
SM
1428 (file-name-directory source-file-name)
1429 (if master-file-name (file-name-directory master-file-name))))
c07fa030
SM
1430 (files (list (list source-file-name source-file-name)
1431 (list temp-source-file-name source-file-name)
1432 (list master-file-name master-file-name)
1433 (list temp-master-file-name master-file-name))))
5323b4b0
RS
1434
1435 (when (equal 0 (length file-name-from-err-msg))
1436 (setq file-name-from-err-msg source-file-name))
1437
1438 (setq real-name (flymake-get-full-patched-file-name file-name-from-err-msg base-dirs files))
9a686ad2 1439 ;; if real-name is nil, than file name from err msg is none of the files we've patched
5323b4b0
RS
1440 (if (not real-name)
1441 (setq real-name (flymake-get-full-nonpatched-file-name file-name-from-err-msg base-dirs)))
1442 (if (not real-name)
1443 (setq real-name file-name-from-err-msg))
1444 (setq real-name (flymake-fix-file-name real-name))
1445 (flymake-log 3 "get-real-file-name: file-name=%s real-name=%s" file-name-from-err-msg real-name)
1446 real-name))
4bcbcb9d 1447
2aa13ec4 1448(defun flymake-get-full-patched-file-name (file-name-from-err-msg base-dirs files)
5323b4b0
RS
1449 (let* ((base-dirs-count (length base-dirs))
1450 (file-count (length files))
1451 (real-name nil))
1452
1453 (while (and (not real-name) (> base-dirs-count 0))
1454 (setq file-count (length files))
1455 (while (and (not real-name) (> file-count 0))
1456 (let* ((this-dir (nth (1- base-dirs-count) base-dirs))
1457 (this-file (nth 0 (nth (1- file-count) files)))
1458 (this-real-name (nth 1 (nth (1- file-count) files))))
9a686ad2 1459 ;;+(flymake-log 0 "this-dir=%s this-file=%s this-real=%s msg-file=%s" this-dir this-file this-real-name file-name-from-err-msg)
5323b4b0 1460 (when (and this-dir this-file (flymake-same-files
b8471a02 1461 (expand-file-name file-name-from-err-msg this-dir)
5323b4b0
RS
1462 this-file))
1463 (setq real-name this-real-name)))
1464 (setq file-count (1- file-count)))
1465 (setq base-dirs-count (1- base-dirs-count)))
1466 real-name))
4bcbcb9d 1467
2aa13ec4 1468(defun flymake-get-full-nonpatched-file-name (file-name-from-err-msg base-dirs)
5323b4b0
RS
1469 (let* ((real-name nil))
1470 (if (file-name-absolute-p file-name-from-err-msg)
1471 (setq real-name file-name-from-err-msg)
1472 (let* ((base-dirs-count (length base-dirs)))
1473 (while (and (not real-name) (> base-dirs-count 0))
b8471a02
SM
1474 (let* ((full-name (expand-file-name file-name-from-err-msg
1475 (nth (1- base-dirs-count) base-dirs))))
5323b4b0
RS
1476 (if (file-exists-p full-name)
1477 (setq real-name full-name))
1478 (setq base-dirs-count (1- base-dirs-count))))))
1479 real-name))
4bcbcb9d 1480
6df19241 1481(defun flymake-init-find-buildfile-dir (source-file-name buildfile-name)
5323b4b0 1482 "Find buildfile, store its dir in buffer data and return its dir, if found."
5bcef417
SM
1483 (let* ((buildfile-dir
1484 (flymake-find-buildfile buildfile-name
5f73367d 1485 (file-name-directory source-file-name))))
5bcef417 1486 (if buildfile-dir
6df19241 1487 (setq flymake-base-dir buildfile-dir)
5bcef417 1488 (flymake-log 1 "no buildfile (%s) for %s" buildfile-name source-file-name)
6df19241
SM
1489 (flymake-report-fatal-status
1490 "NOMK" (format "No buildfile (%s) found for %s"
1491 buildfile-name source-file-name)))))
4bcbcb9d 1492
587d108e 1493(defun flymake-init-create-temp-source-and-master-buffer-copy (get-incl-dirs-f create-temp-f master-file-masks include-regexp)
84bb5a0c 1494 "Find master file (or buffer), create its copy along with a copy of the source file."
c07fa030 1495 (let* ((source-file-name buffer-file-name)
6df19241 1496 (temp-source-file-name (flymake-init-create-temp-buffer-copy create-temp-f))
5323b4b0
RS
1497 (master-and-temp-master (flymake-create-master-file
1498 source-file-name temp-source-file-name
1499 get-incl-dirs-f create-temp-f
587d108e 1500 master-file-masks include-regexp)))
5323b4b0
RS
1501
1502 (if (not master-and-temp-master)
1503 (progn
1504 (flymake-log 1 "cannot find master file for %s" source-file-name)
6df19241 1505 (flymake-report-status "!" "") ; NOMASTER
5bcef417 1506 nil)
6df19241
SM
1507 (setq flymake-master-file-name (nth 0 master-and-temp-master))
1508 (setq flymake-temp-master-file-name (nth 1 master-and-temp-master)))))
4bcbcb9d 1509
c07fa030
SM
1510(defun flymake-master-cleanup ()
1511 (flymake-simple-cleanup)
1512 (flymake-safe-delete-file flymake-temp-master-file-name))
4bcbcb9d
EZ
1513
1514;;;; make-specific init-cleanup routines
2aa13ec4 1515(defun flymake-get-syntax-check-program-args (source-file-name base-dir use-relative-base-dir use-relative-source get-cmd-line-f)
5323b4b0 1516 "Create a command line for syntax check using GET-CMD-LINE-F."
6fee12e6
SM
1517 (funcall get-cmd-line-f
1518 (if use-relative-source
1519 (file-relative-name source-file-name base-dir)
1520 source-file-name)
1521 (if use-relative-base-dir
1522 (file-relative-name base-dir
1523 (file-name-directory source-file-name))
1524 base-dir)))
4bcbcb9d 1525
2aa13ec4 1526(defun flymake-get-make-cmdline (source base-dir)
5323b4b0
RS
1527 (list "make"
1528 (list "-s"
1529 "-C"
1530 base-dir
1531 (concat "CHK_SOURCES=" source)
1532 "SYNTAX_CHECK_MODE=1"
1533 "check-syntax")))
4bcbcb9d 1534
2aa13ec4 1535(defun flymake-get-ant-cmdline (source base-dir)
5323b4b0
RS
1536 (list "ant"
1537 (list "-buildfile"
1538 (concat base-dir "/" "build.xml")
1539 (concat "-DCHK_SOURCES=" source)
1540 "check-syntax")))
4bcbcb9d 1541
6df19241 1542(defun flymake-simple-make-init-impl (create-temp-f use-relative-base-dir use-relative-source build-file-name get-cmdline-f)
5323b4b0 1543 "Create syntax check command line for a directly checked source file.
2aa13ec4 1544Use CREATE-TEMP-F for creating temp copy."
5323b4b0 1545 (let* ((args nil)
6df19241
SM
1546 (source-file-name buffer-file-name)
1547 (buildfile-dir (flymake-init-find-buildfile-dir source-file-name build-file-name)))
5323b4b0 1548 (if buildfile-dir
6df19241 1549 (let* ((temp-source-file-name (flymake-init-create-temp-buffer-copy create-temp-f)))
5323b4b0
RS
1550 (setq args (flymake-get-syntax-check-program-args temp-source-file-name buildfile-dir
1551 use-relative-base-dir use-relative-source
1552 get-cmdline-f))))
1553 args))
4bcbcb9d 1554
6df19241
SM
1555(defun flymake-simple-make-init ()
1556 (flymake-simple-make-init-impl 'flymake-create-temp-inplace t t "Makefile" 'flymake-get-make-cmdline))
4bcbcb9d 1557
587d108e 1558(defun flymake-master-make-init (get-incl-dirs-f master-file-masks include-regexp)
9a686ad2 1559 "Create make command line for a source file checked via master file compilation."
5323b4b0
RS
1560 (let* ((make-args nil)
1561 (temp-master-file-name (flymake-init-create-temp-source-and-master-buffer-copy
6df19241 1562 get-incl-dirs-f 'flymake-create-temp-inplace
587d108e 1563 master-file-masks include-regexp)))
5323b4b0 1564 (when temp-master-file-name
6df19241 1565 (let* ((buildfile-dir (flymake-init-find-buildfile-dir temp-master-file-name "Makefile")))
5323b4b0
RS
1566 (if buildfile-dir
1567 (setq make-args (flymake-get-syntax-check-program-args
1568 temp-master-file-name buildfile-dir nil nil 'flymake-get-make-cmdline)))))
1569 make-args))
4bcbcb9d 1570
2aa13ec4 1571(defun flymake-find-make-buildfile (source-dir)
5f73367d 1572 (flymake-find-buildfile "Makefile" source-dir))
4bcbcb9d
EZ
1573
1574;;;; .h/make specific
6df19241 1575(defun flymake-master-make-header-init ()
fd09a83f
CY
1576 (flymake-master-make-init
1577 'flymake-get-include-dirs
1578 '("\\.\\(?:c\\(?:pp\\|xx\\|\\+\\+\\)?\\|CC\\)\\'")
1579 "[ \t]*#[ \t]*include[ \t]*\"\\([[:word:]0-9/\\_.]*%s\\)\""))
4bcbcb9d
EZ
1580
1581;;;; .java/make specific
6df19241
SM
1582(defun flymake-simple-make-java-init ()
1583 (flymake-simple-make-init-impl 'flymake-create-temp-with-folder-structure nil nil "Makefile" 'flymake-get-make-cmdline))
4bcbcb9d 1584
6df19241
SM
1585(defun flymake-simple-ant-java-init ()
1586 (flymake-simple-make-init-impl 'flymake-create-temp-with-folder-structure nil nil "build.xml" 'flymake-get-ant-cmdline))
4bcbcb9d 1587
c07fa030 1588(defun flymake-simple-java-cleanup ()
9a686ad2 1589 "Cleanup after `flymake-simple-make-java-init' -- delete temp file and dirs."
c07fa030
SM
1590 (flymake-safe-delete-file flymake-temp-source-file-name)
1591 (when flymake-temp-source-file-name
1592 (flymake-delete-temp-directory
1593 (file-name-directory flymake-temp-source-file-name))))
4bcbcb9d
EZ
1594
1595;;;; perl-specific init-cleanup routines
6df19241 1596(defun flymake-perl-init ()
4bd0a5d0 1597 (let* ((temp-file (flymake-init-create-temp-buffer-copy
6df19241 1598 'flymake-create-temp-inplace))
6fee12e6
SM
1599 (local-file (file-relative-name
1600 temp-file
1601 (file-name-directory buffer-file-name))))
5323b4b0 1602 (list "perl" (list "-wc " local-file))))
eabd11d4
MH
1603
1604;;;; php-specific init-cleanup routines
1605(defun flymake-php-init ()
1606 (let* ((temp-file (flymake-init-create-temp-buffer-copy
1607 'flymake-create-temp-inplace))
1608 (local-file (file-relative-name
1609 temp-file
1610 (file-name-directory buffer-file-name))))
1611 (list "php" (list "-f" local-file "-l"))))
4bcbcb9d
EZ
1612
1613;;;; tex-specific init-cleanup routines
2aa13ec4 1614(defun flymake-get-tex-args (file-name)
9a686ad2 1615 ;;(list "latex" (list "-c-style-errors" file-name))
5323b4b0 1616 (list "texify" (list "--pdf" "--tex-option=-c-style-errors" file-name)))
4bcbcb9d 1617
6df19241
SM
1618(defun flymake-simple-tex-init ()
1619 (flymake-get-tex-args (flymake-init-create-temp-buffer-copy 'flymake-create-temp-inplace)))
4bcbcb9d 1620
bd77c2ef
GM
1621;; Perhaps there should be a buffer-local variable flymake-master-file
1622;; that people can set to override this stuff. Could inherit from
1623;; the similar AUCTeX variable.
6df19241 1624(defun flymake-master-tex-init ()
5323b4b0 1625 (let* ((temp-master-file-name (flymake-init-create-temp-source-and-master-buffer-copy
6df19241
SM
1626 'flymake-get-include-dirs-dot 'flymake-create-temp-inplace
1627 '("\\.tex\\'")
bd77c2ef 1628 "[ \t]*\\in\\(?:put\\|clude\\)[ \t]*{\\(.*%s\\)}")))
5323b4b0
RS
1629 (when temp-master-file-name
1630 (flymake-get-tex-args temp-master-file-name))))
4bcbcb9d 1631
e02f48d7 1632(defun flymake-get-include-dirs-dot (_base-dir)
5323b4b0 1633 '("."))
4bcbcb9d
EZ
1634
1635;;;; xml-specific init-cleanup routines
6df19241 1636(defun flymake-xml-init ()
61aaeb01
GM
1637 (list flymake-xml-program
1638 (list "val" (flymake-init-create-temp-buffer-copy
1639 'flymake-create-temp-inplace))))
2aa13ec4
RS
1640
1641(provide 'flymake)
4bcbcb9d 1642;;; flymake.el ends here