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