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