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