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