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