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