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