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