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