*** empty log message ***
[emacs.git] / lisp / progmodes / flymake.el
blob6f5d0855e19c4a1a7df75a0c8deea718becfc486
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 "%s" 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 (with-current-buffer
520 (get-buffer-create (generate-new-buffer-name
521 (concat "flymake:" (buffer-name buffer))))
522 (insert-buffer-substring buffer)
523 (current-buffer)))
525 (defun flymake-check-include (source-file-name inc-path inc-name include-dirs)
526 "Check if SOURCE-FILE-NAME can be found in include path.
527 Return t if it can be found via include path using INC-PATH and INC-NAME."
528 (if (file-name-absolute-p inc-path)
529 (flymake-same-files source-file-name (concat inc-path "/" inc-name))
530 (let* ((file-name nil)
531 (found nil))
532 (while (and (not found) include-dirs)
533 (setq file-name (concat (file-name-directory source-file-name)
534 "/" (car include-dirs)))
535 (if (> (length inc-path) 0)
536 (setq file-name (concat file-name "/" inc-path)))
537 (setq file-name (concat file-name "/" inc-name))
538 (when (flymake-same-files source-file-name file-name)
539 (setq found t))
540 (setq include-dirs (cdr include-dirs)))
541 found)))
543 (defun flymake-find-buffer-for-file (file-name)
544 "Check if there exists a buffer visiting FILE-NAME.
545 Return t if so, nil if not."
546 (let ((buffer-name (get-file-buffer file-name)))
547 (if buffer-name
548 (get-buffer buffer-name))))
550 (defun flymake-create-master-file (source-file-name patched-source-file-name get-incl-dirs-f create-temp-f masks include-regexp-list)
551 "Save SOURCE-FILE-NAME with a different name.
552 Find master file, patch and save it."
553 (let* ((possible-master-files (flymake-find-possible-master-files source-file-name flymake-master-file-dirs masks))
554 (master-file-count (length possible-master-files))
555 (idx 0)
556 (temp-buffer nil)
557 (master-file-name nil)
558 (patched-master-file-name nil)
559 (found nil))
561 (while (and (not found) (< idx master-file-count))
562 (setq master-file-name (nth idx possible-master-files))
563 (setq patched-master-file-name (funcall create-temp-f master-file-name "flymake_master"))
564 (if (flymake-find-buffer-for-file master-file-name)
565 (setq temp-buffer (flymake-copy-buffer-to-temp-buffer (flymake-find-buffer-for-file master-file-name)))
566 (setq temp-buffer (flymake-read-file-to-temp-buffer master-file-name)))
567 (setq found
568 (flymake-check-patch-master-file-buffer
569 temp-buffer
570 master-file-name
571 patched-master-file-name
572 source-file-name
573 patched-source-file-name
574 (funcall get-incl-dirs-f (file-name-directory master-file-name))
575 include-regexp-list))
576 (setq idx (1+ idx)))
577 (if found
578 (list master-file-name patched-master-file-name)
579 (progn
580 (flymake-log 3 "none of %d master file(s) checked includes %s" master-file-count
581 (file-name-nondirectory source-file-name))
582 nil))))
584 (defun flymake-save-buffer-in-file (buffer file-name)
585 (or buffer
586 (error "Invalid buffer"))
587 (with-current-buffer buffer
588 (save-restriction
589 (widen)
590 (make-directory (file-name-directory file-name) 1)
591 (write-region (point-min) (point-max) file-name nil 566)))
592 (flymake-log 3 "saved buffer %s in file %s" (buffer-name buffer) file-name))
594 (defun flymake-save-string-to-file (file-name data)
595 "Save string DATA to file FILE-NAME."
596 (write-region data nil file-name nil 566))
598 (defun flymake-read-file-to-string (file-name)
599 "Read contents of file FILE-NAME and return as a string."
600 (with-temp-buffer
601 (insert-file-contents file-name)
602 (buffer-substring (point-min) (point-max))))
604 (defun flymake-process-filter (process output)
605 "Parse OUTPUT and highlight error lines.
606 It's flymake process filter."
607 (let* ((pid (process-id process))
608 (source-buffer (get-buffer (flymake-get-source-buffer-name pid))))
610 (flymake-log 3 "received %d byte(s) of output from process %d" (length output) pid)
611 (when source-buffer
612 (with-current-buffer source-buffer
613 (flymake-parse-output-and-residual output)))))
615 (defun flymake-process-sentinel (process event)
616 "Sentinel for syntax check buffers."
617 (if (memq (process-status process) '(signal exit))
618 (let*((exit-status (process-exit-status process))
619 (command (process-command process))
620 (pid (process-id process))
621 (source-buffer (get-buffer (flymake-get-source-buffer-name pid)))
622 (cleanup-f (flymake-get-cleanup-function (buffer-file-name source-buffer))))
624 (flymake-log 2 "process %d exited with code %d" pid exit-status)
625 (condition-case err
626 (progn
627 (flymake-log 3 "cleaning up using %s" cleanup-f)
628 (funcall cleanup-f source-buffer)
630 (flymake-unreg-names pid)
631 (delete-process process)
633 (when source-buffer
634 (with-current-buffer source-buffer
636 (flymake-parse-residual)
637 (flymake-post-syntax-check exit-status command)
638 (setq flymake-is-running nil))))
639 (error
640 (let ((err-str (format "Error in process sentinel for buffer %s: %s"
641 source-buffer (error-message-string err))))
642 (flymake-log 0 err-str)
643 (with-current-buffer source-buffer
644 (setq flymake-is-running nil))))))))
646 (defun flymake-post-syntax-check (exit-status command)
647 (setq flymake-err-info flymake-new-err-info)
648 (setq flymake-new-err-info nil)
649 (setq flymake-err-info
650 (flymake-fix-line-numbers
651 flymake-err-info 1 (flymake-count-lines)))
652 (flymake-delete-own-overlays)
653 (flymake-highlight-err-lines flymake-err-info)
654 (let (err-count warn-count)
655 (setq err-count (flymake-get-err-count flymake-err-info "e"))
656 (setq warn-count (flymake-get-err-count flymake-err-info "w"))
657 (flymake-log 2 "%s: %d error(s), %d warning(s) in %.2f second(s)"
658 (buffer-name) err-count warn-count
659 (- (flymake-float-time) flymake-check-start-time))
660 (setq flymake-check-start-time nil)
662 (if (and (equal 0 err-count) (equal 0 warn-count))
663 (if (equal 0 exit-status)
664 (flymake-report-status "" "") ; PASSED
665 (if (not flymake-check-was-interrupted)
666 (flymake-report-fatal-status "CFGERR"
667 (format "Configuration error has occured while running %s" command))
668 (flymake-report-status nil ""))) ; "STOPPED"
669 (flymake-report-status (format "%d/%d" err-count warn-count) ""))))
671 (defun flymake-parse-output-and-residual (output)
672 "Split OUTPUT into lines, merge in residual if necessary."
673 (let* ((buffer-residual flymake-output-residual)
674 (total-output (if buffer-residual (concat buffer-residual output) output))
675 (lines-and-residual (flymake-split-output total-output))
676 (lines (nth 0 lines-and-residual))
677 (new-residual (nth 1 lines-and-residual)))
678 (setq flymake-output-residual new-residual)
679 (setq flymake-new-err-info
680 (flymake-parse-err-lines
681 flymake-new-err-info lines))))
683 (defun flymake-parse-residual ()
684 "Parse residual if it's non empty."
685 (when flymake-output-residual
686 (setq flymake-new-err-info
687 (flymake-parse-err-lines
688 flymake-new-err-info
689 (list flymake-output-residual)))
690 (setq flymake-output-residual nil)))
692 (defvar flymake-err-info nil
693 "Sorted list of line numbers and lists of err info in the form (file, err-text).")
695 (make-variable-buffer-local 'flymake-err-info)
697 (defun flymake-er-make-er (line-no line-err-info-list)
698 (list line-no line-err-info-list))
700 (defun flymake-er-get-line (err-info)
701 (nth 0 err-info))
703 (defun flymake-er-get-line-err-info-list (err-info)
704 (nth 1 err-info))
706 (defvar flymake-new-err-info nil
707 "Same as `flymake-err-info', effective when a syntax check is in progress.")
709 (make-variable-buffer-local 'flymake-new-err-info)
711 ;; getters/setters for line-err-info: (file, line, type, text).
712 (defun flymake-ler-make-ler (file line type text &optional full-file)
713 (list file line type text full-file))
715 (defun flymake-ler-get-file (line-err-info)
716 (nth 0 line-err-info))
718 (defun flymake-ler-get-line (line-err-info)
719 (nth 1 line-err-info))
721 (defun flymake-ler-get-type (line-err-info)
722 (nth 2 line-err-info))
724 (defun flymake-ler-get-text (line-err-info)
725 (nth 3 line-err-info))
727 (defun flymake-ler-get-full-file (line-err-info)
728 (nth 4 line-err-info))
730 (defun flymake-ler-set-file (line-err-info file)
731 (flymake-ler-make-ler file
732 (flymake-ler-get-line line-err-info)
733 (flymake-ler-get-type line-err-info)
734 (flymake-ler-get-text line-err-info)
735 (flymake-ler-get-full-file line-err-info)))
737 (defun flymake-ler-set-full-file (line-err-info full-file)
738 (flymake-ler-make-ler (flymake-ler-get-file line-err-info)
739 (flymake-ler-get-line line-err-info)
740 (flymake-ler-get-type line-err-info)
741 (flymake-ler-get-text line-err-info)
742 full-file))
744 (defun flymake-ler-set-line (line-err-info line)
745 (flymake-ler-make-ler (flymake-ler-get-file line-err-info)
746 line
747 (flymake-ler-get-type line-err-info)
748 (flymake-ler-get-text line-err-info)
749 (flymake-ler-get-full-file line-err-info)))
751 (defun flymake-get-line-err-count (line-err-info-list type)
752 "Return number of errors of specified TYPE.
753 Value of TYPE is either \"e\" or \"w\"."
754 (let* ((idx 0)
755 (count (length line-err-info-list))
756 (err-count 0))
758 (while (< idx count)
759 (when (equal type (flymake-ler-get-type (nth idx line-err-info-list)))
760 (setq err-count (1+ err-count)))
761 (setq idx (1+ idx)))
762 err-count))
764 (defun flymake-get-err-count (err-info-list type)
765 "Return number of errors of specified TYPE for ERR-INFO-LIST."
766 (let* ((idx 0)
767 (count (length err-info-list))
768 (err-count 0))
769 (while (< idx count)
770 (setq err-count (+ err-count (flymake-get-line-err-count (nth 1 (nth idx err-info-list)) type)))
771 (setq idx (1+ idx)))
772 err-count))
774 (defun flymake-fix-line-numbers (err-info-list min-line max-line)
775 "Replace line numbers with fixed value.
776 If line-numbers is less than MIN-LINE, set line numbers to MIN-LINE.
777 If line numbers is greater than MAX-LINE, set line numbers to MAX-LINE.
778 The reason for this fix is because some compilers might report
779 line number outside the file being compiled."
780 (let* ((count (length err-info-list))
781 (err-info nil)
782 (line 0))
783 (while (> count 0)
784 (setq err-info (nth (1- count) err-info-list))
785 (setq line (flymake-er-get-line err-info))
786 (when (or (< line min-line) (> line max-line))
787 (setq line (if (< line min-line) min-line max-line))
788 (setq err-info-list (flymake-set-at err-info-list (1- count)
789 (flymake-er-make-er line
790 (flymake-er-get-line-err-info-list err-info)))))
791 (setq count (1- count))))
792 err-info-list)
794 (defun flymake-highlight-err-lines (err-info-list)
795 "Highlight error lines in BUFFER using info from ERR-INFO-LIST."
796 (save-excursion
797 (dolist (err err-info-list)
798 (flymake-highlight-line (car err) (nth 1 err)))))
800 (defun flymake-overlay-p (ov)
801 "Determine whether overlay OV was created by flymake."
802 (and (overlayp ov) (overlay-get ov 'flymake-overlay)))
804 (defun flymake-make-overlay (beg end tooltip-text face mouse-face)
805 "Allocate a flymake overlay in range BEG and END."
806 (when (not (flymake-region-has-flymake-overlays beg end))
807 (let ((ov (make-overlay beg end nil t t)))
808 (overlay-put ov 'face face)
809 (overlay-put ov 'mouse-face mouse-face)
810 (overlay-put ov 'help-echo tooltip-text)
811 (overlay-put ov 'flymake-overlay t)
812 (overlay-put ov 'priority 100)
813 ;;+(flymake-log 3 "created overlay %s" ov)
815 (flymake-log 3 "created an overlay at (%d-%d)" beg end)))
817 (defun flymake-delete-own-overlays ()
818 "Delete all flymake overlays in BUFFER."
819 (dolist (ol (overlays-in (point-min) (point-max)))
820 (when (flymake-overlay-p ol)
821 (delete-overlay ol)
822 ;;+(flymake-log 3 "deleted overlay %s" ol)
825 (defun flymake-region-has-flymake-overlays (beg end)
826 "Check if region specified by BEG and END has overlay.
827 Return t if it has at least one flymake overlay, nil if no overlay."
828 (let ((ov (overlays-in beg end))
829 (has-flymake-overlays nil))
830 (while (consp ov)
831 (when (flymake-overlay-p (car ov))
832 (setq has-flymake-overlays t))
833 (setq ov (cdr ov)))
834 has-flymake-overlays))
836 (defface flymake-errline
837 ;;+ '((((class color)) (:foreground "OrangeRed" :bold t :underline t))
838 ;;+ '((((class color)) (:underline "OrangeRed"))
839 '((((class color)) (:background "LightPink"))
840 (t (:bold t)))
841 "Face used for marking error lines."
842 :group 'flymake)
844 (defface flymake-warnline
845 '((((class color)) (:background "LightBlue2"))
846 (t (:bold t)))
847 "Face used for marking warning lines."
848 :group 'flymake)
850 (defun flymake-highlight-line (line-no line-err-info-list)
851 "Highlight line LINE-NO in current buffer.
852 Perhaps use text from LINE-ERR-INFO-LIST to enhance highlighting."
853 (goto-line line-no)
854 (let* ((line-beg (flymake-line-beginning-position))
855 (line-end (flymake-line-end-position))
856 (beg line-beg)
857 (end line-end)
858 (tooltip-text (flymake-ler-get-text (nth 0 line-err-info-list)))
859 (face nil))
861 (goto-char line-beg)
862 (while (looking-at "[ \t]")
863 (forward-char))
865 (setq beg (point))
867 (goto-char line-end)
868 (while (and (looking-at "[ \t\r\n]") (> (point) 1))
869 (backward-char))
871 (setq end (1+ (point)))
873 (when (<= end beg)
874 (setq beg line-beg)
875 (setq end line-end))
877 (when (= end beg)
878 (goto-char end)
879 (forward-line)
880 (setq end (point)))
882 (if (> (flymake-get-line-err-count line-err-info-list "e") 0)
883 (setq face 'flymake-errline)
884 (setq face 'flymake-warnline))
886 (flymake-make-overlay beg end tooltip-text face nil)))
888 (defun flymake-parse-err-lines (err-info-list lines)
889 "Parse err LINES, store info in ERR-INFO-LIST."
890 (let* ((count (length lines))
891 (idx 0)
892 (line-err-info nil)
893 (real-file-name nil)
894 (source-file-name buffer-file-name)
895 (get-real-file-name-f (flymake-get-real-file-name-function source-file-name)))
897 (while (< idx count)
898 (setq line-err-info (flymake-parse-line (nth idx lines)))
899 (when line-err-info
900 (setq real-file-name (funcall get-real-file-name-f (current-buffer) (flymake-ler-get-file line-err-info)))
901 (setq line-err-info (flymake-ler-set-full-file line-err-info real-file-name))
903 (if (flymake-same-files real-file-name source-file-name)
904 (setq line-err-info (flymake-ler-set-file line-err-info nil))
905 (setq line-err-info (flymake-ler-set-file line-err-info (file-name-nondirectory real-file-name))))
907 (setq err-info-list (flymake-add-err-info err-info-list line-err-info)))
908 (flymake-log 3 "parsed '%s', %s line-err-info" (nth idx lines) (if line-err-info "got" "no"))
909 (setq idx (1+ idx)))
910 err-info-list))
912 (defun flymake-split-output (output)
913 "Split OUTPUT into lines.
914 Return last one as residual if it does not end with newline char.
915 Returns ((LINES) RESIDUAL)."
916 (when (and output (> (length output) 0))
917 (let* ((lines (flymake-split-string output "[\n\r]+"))
918 (complete (equal "\n" (char-to-string (aref output (1- (length output))))))
919 (residual nil))
920 (when (not complete)
921 (setq residual (car (last lines)))
922 (setq lines (butlast lines)))
923 (list lines residual))))
925 (defun flymake-reformat-err-line-patterns-from-compile-el (original-list)
926 "Grab error line patterns from ORIGINAL-LIST in compile.el format.
927 Convert it to flymake internal format."
928 (let* ((converted-list '()))
929 (dolist (item original-list)
930 (setq item (cdr item))
931 (let ((regexp (nth 0 item))
932 (file (nth 1 item))
933 (line (nth 2 item))
934 (col (nth 3 item)))
935 (if (consp file) (setq file (car file)))
936 (if (consp line) (setq line (car line)))
937 (if (consp col) (setq col (car col)))
939 (when (not (functionp line))
940 (setq converted-list (cons (list regexp file line col) converted-list)))))
941 converted-list))
943 (require 'compile)
945 (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
946 (append
948 ;; MS Visual C++ 6.0
949 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\)) \: \\(\\(error\\|warning\\|fatal error\\) \\(C[0-9]+\\):[ \t\n]*\\(.+\\)\\)"
950 1 3 nil 4)
951 ;; jikes
952 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)\:\\([0-9]+\\)\:[0-9]+\:[0-9]+\:[0-9]+\: \\(\\(Error\\|Warning\\|Caution\\|Semantic Error\\):[ \t\n]*\\(.+\\)\\)"
953 1 3 nil 4)
954 ;; MS midl
955 ("midl[ ]*:[ ]*\\(command line error .*\\)"
956 nil nil nil 1)
957 ;; MS C#
958 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\),[0-9]+)\: \\(\\(error\\|warning\\|fatal error\\) \\(CS[0-9]+\\):[ \t\n]*\\(.+\\)\\)"
959 1 3 nil 4)
960 ;; perl
961 ("\\(.*\\) at \\([^ \n]+\\) line \\([0-9]+\\)[,.\n]" 2 3 nil 1)
962 ;; LaTeX warnings (fileless) ("\\(LaTeX \\(Warning\\|Error\\): .*\\) on input line \\([0-9]+\\)" 20 3 nil 1)
963 ;; ant/javac
964 (" *\\(\\[javac\\]\\)? *\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)\:\\([0-9]+\\)\:[ \t\n]*\\(.+\\)"
965 2 4 nil 5))
966 ;; compilation-error-regexp-alist)
967 (flymake-reformat-err-line-patterns-from-compile-el compilation-error-regexp-alist-alist))
968 "Patterns for matching error/warning lines.
969 \(REGEXP FILE-IDX LINE-IDX ERR-TEXT-IDX).
970 Use `flymake-reformat-err-line-patterns-from-compile-el' to add patterns
971 from compile.el")
973 ;;(defcustom flymake-err-line-patterns
974 ;; '(
975 ;; ; MS Visual C++ 6.0
976 ;; ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\)) \: \\(\\(error\\|warning\\|fatal error\\) \\(C[0-9]+\\):[ \t\n]*\\(.+\\)\\)"
977 ;; 1 3 4)
978 ;; ; jikes
979 ;; ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)\:\\([0-9]+\\)\:[0-9]+\:[0-9]+\:[0-9]+\: \\(\\(Error\\|Warning\\|Caution\\):[ \t\n]*\\(.+\\)\\)"
980 ;; 1 3 4))
981 ;; "patterns for matching error/warning lines, (regexp file-idx line-idx err-text-idx)"
982 ;; :group 'flymake
983 ;; :type '(repeat (string number number number))
986 (defun flymake-parse-line (line)
987 "Parse LINE to see if it is an error or warning.
988 Return its components if so, nil otherwise."
989 (let ((raw-file-name nil)
990 (line-no 0)
991 (err-type "e")
992 (err-text nil)
993 (patterns flymake-err-line-patterns)
994 (matched nil))
995 (while (and patterns (not matched))
996 (when (string-match (car (car patterns)) line)
997 (let* ((file-idx (nth 1 (car patterns)))
998 (line-idx (nth 2 (car patterns))))
1000 (setq raw-file-name (if file-idx (match-string file-idx line) nil))
1001 (setq line-no (if line-idx (string-to-number (match-string line-idx line)) 0))
1002 (setq err-text (if (> (length (car patterns)) 4)
1003 (match-string (nth 4 (car patterns)) line)
1004 (flymake-patch-err-text (substring line (match-end 0)))))
1005 (or err-text (setq err-text "<no error text>"))
1006 (if (and err-text (string-match "^[wW]arning" err-text))
1007 (setq err-type "w")
1009 (flymake-log 3 "parse line: file-idx=%s line-idx=%s file=%s line=%s text=%s" file-idx line-idx
1010 raw-file-name line-no err-text)
1011 (setq matched t)))
1012 (setq patterns (cdr patterns)))
1013 (if matched
1014 (flymake-ler-make-ler raw-file-name line-no err-type err-text)
1015 ())))
1017 (defun flymake-find-err-info (err-info-list line-no)
1018 "Find (line-err-info-list pos) for specified LINE-NO."
1019 (if err-info-list
1020 (let* ((line-err-info-list nil)
1021 (pos 0)
1022 (count (length err-info-list)))
1024 (while (and (< pos count) (< (car (nth pos err-info-list)) line-no))
1025 (setq pos (1+ pos)))
1026 (when (and (< pos count) (equal (car (nth pos err-info-list)) line-no))
1027 (setq line-err-info-list (flymake-er-get-line-err-info-list (nth pos err-info-list))))
1028 (list line-err-info-list pos))
1029 '(nil 0)))
1031 (defun flymake-line-err-info-is-less-or-equal (line-one line-two)
1032 (or (string< (flymake-ler-get-type line-one) (flymake-ler-get-type line-two))
1033 (and (string= (flymake-ler-get-type line-one) (flymake-ler-get-type line-two))
1034 (not (flymake-ler-get-file line-one)) (flymake-ler-get-file line-two))
1035 (and (string= (flymake-ler-get-type line-one) (flymake-ler-get-type line-two))
1036 (or (and (flymake-ler-get-file line-one) (flymake-ler-get-file line-two))
1037 (and (not (flymake-ler-get-file line-one)) (not (flymake-ler-get-file line-two)))))))
1039 (defun flymake-add-line-err-info (line-err-info-list line-err-info)
1040 "Update LINE-ERR-INFO-LIST with the error LINE-ERR-INFO.
1041 For the format of LINE-ERR-INFO, see `flymake-ler-make-ler'.
1042 The new element is inserted in the proper position, according to
1043 the predicate `flymake-line-err-info-is-less-or-equal'.
1044 The updated value of LINE-ERR-INFO-LIST is returned."
1045 (if (not line-err-info-list)
1046 (list line-err-info)
1047 (let* ((count (length line-err-info-list))
1048 (idx 0))
1049 (while (and (< idx count) (flymake-line-err-info-is-less-or-equal (nth idx line-err-info-list) line-err-info))
1050 (setq idx (1+ idx)))
1051 (cond ((equal 0 idx) (setq line-err-info-list (cons line-err-info line-err-info-list)))
1052 (t (setq line-err-info-list (flymake-ins-after line-err-info-list (1- idx) line-err-info))))
1053 line-err-info-list)))
1055 (defun flymake-add-err-info (err-info-list line-err-info)
1056 "Update ERR-INFO-LIST with the error LINE-ERR-INFO, preserving sort order.
1057 Returns the updated value of ERR-INFO-LIST.
1058 For the format of ERR-INFO-LIST, see `flymake-err-info'.
1059 For the format of LINE-ERR-INFO, see `flymake-ler-make-ler'."
1060 (let* ((line-no (if (flymake-ler-get-file line-err-info) 1 (flymake-ler-get-line line-err-info)))
1061 (info-and-pos (flymake-find-err-info err-info-list line-no))
1062 (exists (car info-and-pos))
1063 (pos (nth 1 info-and-pos))
1064 (line-err-info-list nil)
1065 (err-info nil))
1067 (if exists
1068 (setq line-err-info-list (flymake-er-get-line-err-info-list (car (nthcdr pos err-info-list)))))
1069 (setq line-err-info-list (flymake-add-line-err-info line-err-info-list line-err-info))
1071 (setq err-info (flymake-er-make-er line-no line-err-info-list))
1072 (cond (exists (setq err-info-list (flymake-set-at err-info-list pos err-info)))
1073 ((equal 0 pos) (setq err-info-list (cons err-info err-info-list)))
1074 (t (setq err-info-list (flymake-ins-after err-info-list (1- pos) err-info))))
1075 err-info-list))
1077 (defun flymake-get-project-include-dirs-imp (basedir)
1078 "Include dirs for the project current file belongs to."
1079 (if (flymake-get-project-include-dirs-from-cache basedir)
1080 (progn
1081 (flymake-get-project-include-dirs-from-cache basedir))
1082 ;;else
1083 (let* ((command-line (concat "make -C\"" basedir "\" DUMPVARS=INCLUDE_DIRS dumpvars"))
1084 (output (shell-command-to-string command-line))
1085 (lines (flymake-split-string output "\n"))
1086 (count (length lines))
1087 (idx 0)
1088 (inc-dirs nil))
1089 (while (and (< idx count) (not (string-match "^INCLUDE_DIRS=.*" (nth idx lines))))
1090 (setq idx (1+ idx)))
1091 (when (< idx count)
1092 (let* ((inc-lines (flymake-split-string (nth idx lines) " *-I"))
1093 (inc-count (length inc-lines)))
1094 (while (> inc-count 0)
1095 (when (not (string-match "^INCLUDE_DIRS=.*" (nth (1- inc-count) inc-lines)))
1096 (setq inc-dirs (cons (flymake-replace-regexp-in-string "\"" "" (nth (1- inc-count) inc-lines)) inc-dirs)))
1097 (setq inc-count (1- inc-count)))))
1098 (flymake-add-project-include-dirs-to-cache basedir inc-dirs)
1099 inc-dirs)))
1101 (defcustom flymake-get-project-include-dirs-function 'flymake-get-project-include-dirs-imp
1102 "Function used to get project include dirs, one parameter: basedir name."
1103 :group 'flymake
1104 :type 'function)
1106 (defun flymake-get-project-include-dirs (basedir)
1107 (funcall flymake-get-project-include-dirs-function basedir))
1109 (defun flymake-get-system-include-dirs ()
1110 "System include dirs - from the 'INCLUDE' env setting."
1111 (let* ((includes (getenv "INCLUDE")))
1112 (if includes (flymake-split-string includes path-separator) nil)))
1114 (defvar flymake-project-include-dirs-cache (flymake-makehash 'equal))
1116 (defun flymake-get-project-include-dirs-from-cache (base-dir)
1117 (gethash base-dir flymake-project-include-dirs-cache))
1119 (defun flymake-add-project-include-dirs-to-cache (base-dir include-dirs)
1120 (puthash base-dir include-dirs flymake-project-include-dirs-cache))
1122 (defun flymake-clear-project-include-dirs-cache ()
1123 (clrhash flymake-project-include-dirs-cache))
1125 (defun flymake-get-include-dirs (base-dir)
1126 "Get dirs to use when resolving local file names."
1127 (let* ((include-dirs (append '(".") (flymake-get-project-include-dirs base-dir) (flymake-get-system-include-dirs))))
1128 include-dirs))
1130 ;; (defun flymake-restore-formatting ()
1131 ;; "Remove any formatting made by flymake."
1132 ;; )
1134 (defun flymake-get-program-dir (buffer)
1135 "Get dir to start program in."
1136 (unless (bufferp buffer)
1137 (error "Invalid buffer"))
1138 (with-current-buffer buffer
1139 default-directory))
1141 (defun flymake-safe-delete-file (file-name)
1142 (when (and file-name (file-exists-p file-name))
1143 (delete-file file-name)
1144 (flymake-log 1 "deleted file %s" file-name)))
1146 (defun flymake-safe-delete-directory (dir-name)
1147 (condition-case err
1148 (progn
1149 (delete-directory dir-name)
1150 (flymake-log 1 "deleted dir %s" dir-name))
1151 (error
1152 (flymake-log 1 "Failed to delete dir %s, error ignored" dir-name))))
1154 (defcustom flymake-compilation-prevents-syntax-check t
1155 "If non-nil, syntax check won't be started in case compilation is running."
1156 :group 'flymake
1157 :type 'boolean)
1159 (defun flymake-start-syntax-check ()
1160 "Start syntax checking for current buffer."
1161 (interactive)
1162 (flymake-log 3 "flymake is running: %s" flymake-is-running)
1163 (when (and (not flymake-is-running)
1164 (flymake-can-syntax-check-file buffer-file-name))
1165 (when (or (not flymake-compilation-prevents-syntax-check)
1166 (not (flymake-compilation-is-running))) ;+ (flymake-rep-ort-status buffer "COMP")
1167 (flymake-clear-buildfile-cache)
1168 (flymake-clear-project-include-dirs-cache)
1170 (setq flymake-check-was-interrupted nil)
1171 (setq flymake-buffer-data (flymake-makehash 'equal))
1173 (let* ((source-file-name buffer-file-name)
1174 (init-f (flymake-get-init-function source-file-name))
1175 (cleanup-f (flymake-get-cleanup-function source-file-name))
1176 (cmd-and-args (funcall init-f (current-buffer)))
1177 (cmd (nth 0 cmd-and-args))
1178 (args (nth 1 cmd-and-args))
1179 (dir (nth 2 cmd-and-args)))
1180 (if (not cmd-and-args)
1181 (progn
1182 (flymake-log 0 "init function %s for %s failed, cleaning up" init-f source-file-name)
1183 (funcall cleanup-f (current-buffer)))
1184 (progn
1185 (setq flymake-last-change-time nil)
1186 (flymake-start-syntax-check-process cmd args dir)))))))
1188 (defun flymake-start-syntax-check-process (cmd args dir)
1189 "Start syntax check process."
1190 (let* ((process nil))
1191 (condition-case err
1192 (progn
1193 (when dir
1194 (let ((default-directory dir))
1195 (flymake-log 3 "starting process on dir %s" default-directory)))
1196 (setq process (get-process (apply 'start-process "flymake-proc" nil cmd args)))
1197 (set-process-sentinel process 'flymake-process-sentinel)
1198 (set-process-filter process 'flymake-process-filter)
1200 (flymake-reg-names (process-id process) (buffer-name))
1202 (setq flymake-is-running t)
1203 (setq flymake-last-change-time nil)
1204 (setq flymake-check-start-time (flymake-float-time))
1206 (flymake-report-status nil "*")
1207 (flymake-log 2 "started process %d, command=%s, dir=%s"
1208 (process-id process) (process-command process) default-directory)
1209 process)
1210 (error
1211 (let* ((err-str (format "Failed to launch syntax check process '%s' with args %s: %s"
1212 cmd args (error-message-string err)))
1213 (source-file-name buffer-file-name)
1214 (cleanup-f (flymake-get-cleanup-function source-file-name)))
1215 (flymake-log 0 err-str)
1216 (funcall cleanup-f (current-buffer))
1217 (flymake-report-fatal-status "PROCERR" err-str))))))
1219 (defun flymake-kill-process (pid &optional rest)
1220 "Kill process PID."
1221 (signal-process pid 9)
1222 (let* ((buffer-name (flymake-get-source-buffer-name pid)))
1223 (when (and buffer-name (get-buffer buffer-name))
1224 (with-current-buffer (get-buffer buffer-name)
1225 (setq flymake-check-was-interrupted t))))
1226 (flymake-log 1 "killed process %d" pid))
1228 (defun flymake-stop-all-syntax-checks ()
1229 "Kill all syntax check processes."
1230 (interactive)
1231 (let ((pids (copy-hash-table flymake-pid-to-names)))
1232 (maphash 'flymake-kill-process pids)))
1234 (defun flymake-compilation-is-running ()
1235 (and (boundp 'compilation-in-progress)
1236 compilation-in-progress))
1238 (defun flymake-compile ()
1239 "Kill all flymake syntax checks, start compilation."
1240 (interactive)
1241 (flymake-stop-all-syntax-checks)
1242 (call-interactively 'compile))
1244 (defvar flymake-is-running nil
1245 "If t, flymake syntax check process is running for the current buffer.")
1247 (make-variable-buffer-local 'flymake-is-running)
1249 (defvar flymake-timer nil
1250 "Timer for starting syntax check.")
1252 (make-variable-buffer-local 'flymake-timer)
1254 (defvar flymake-last-change-time nil
1255 "Time of last buffer change.")
1257 (make-variable-buffer-local 'flymake-last-change-time)
1259 (defvar flymake-check-start-time nil
1260 "Time at which syntax check was started.")
1262 (make-variable-buffer-local 'flymake-check-start-time)
1264 (defvar flymake-check-was-interrupted nil
1265 "Non-nil if syntax check was killed by `flymake-compile'.")
1267 (make-variable-buffer-local 'flymake-check-was-interrupted)
1269 (defcustom flymake-no-changes-timeout 0.5
1270 "Time to wait after last change before starting compilation."
1271 :group 'flymake
1272 :type 'number)
1274 (defun flymake-on-timer-event (buffer)
1275 "Start a syntax check for buffer BUFFER if necessary."
1276 (when (bufferp buffer)
1277 (with-current-buffer buffer
1278 (when (and (not flymake-is-running)
1279 flymake-last-change-time
1280 (> (flymake-float-time) (+ flymake-no-changes-timeout flymake-last-change-time)))
1282 (setq flymake-last-change-time nil)
1283 (flymake-log 3 "starting syntax check as more than 1 second passed since last change")
1284 (flymake-start-syntax-check)))))
1286 (defun flymake-current-line-no ()
1287 "Return number of current line in current buffer."
1288 (interactive)
1289 (let ((beg (point-min))
1290 (end (if (= (point) (point-max)) (point) (1+ (point)))))
1291 (count-lines beg end)))
1293 (defun flymake-count-lines ()
1294 "Return number of lines in buffer BUFFER."
1295 (count-lines (point-min) (point-max)))
1297 (defun flymake-get-point-pixel-pos ()
1298 "Return point position in pixels: (x, y)."
1299 (let ((mouse-pos (mouse-position))
1300 (pixel-pos nil)
1301 (ret nil))
1302 (if (car (cdr mouse-pos))
1303 (progn
1304 (set-mouse-position (flymake-selected-frame) (current-column) (flymake-current-row))
1305 (setq pixel-pos (mouse-pixel-position))
1306 (set-mouse-position (car mouse-pos) (car (cdr mouse-pos)) (cdr (cdr mouse-pos)))
1307 (setq ret (list (car (cdr pixel-pos)) (cdr (cdr pixel-pos)))))
1308 (progn
1309 (setq ret '(0 0))))
1310 (flymake-log 3 "mouse pos is %s" ret)
1311 ret))
1313 (defun flymake-display-err-menu-for-current-line ()
1314 "Display a menu with errors/warnings for current line if it has errors and/or warnings."
1315 (interactive)
1316 (let* ((line-no (flymake-current-line-no))
1317 (line-err-info-list (nth 0 (flymake-find-err-info flymake-err-info line-no)))
1318 (menu-data (flymake-make-err-menu-data line-no line-err-info-list))
1319 (choice nil)
1320 (menu-pos (list (flymake-get-point-pixel-pos) (selected-window))))
1321 (if menu-data
1322 (progn
1323 (setq choice (flymake-popup-menu menu-pos menu-data))
1324 (flymake-log 3 "choice=%s" choice)
1325 (when choice
1326 (eval choice)))
1327 (flymake-log 1 "no errors for line %d" line-no))))
1329 (defun flymake-make-err-menu-data (line-no line-err-info-list)
1330 "Make a (menu-title (item-title item-action)*) list with errors/warnings from LINE-ERR-INFO-LIST."
1331 (let* ((menu-items nil))
1332 (when line-err-info-list
1333 (let* ((count (length line-err-info-list))
1334 (menu-item-text nil))
1335 (while (> count 0)
1336 (setq menu-item-text (flymake-ler-get-text (nth (1- count) line-err-info-list)))
1337 (let* ((file (flymake-ler-get-file (nth (1- count) line-err-info-list)))
1338 (full-file (flymake-ler-get-full-file (nth (1- count) line-err-info-list)))
1339 (line (flymake-ler-get-line (nth (1- count) line-err-info-list))))
1340 (if file
1341 (setq menu-item-text (concat menu-item-text " - " file "(" (format "%d" line) ")")))
1342 (setq menu-items (cons (list menu-item-text
1343 (if file (list 'flymake-goto-file-and-line full-file line) nil))
1344 menu-items)))
1345 (setq count (1- count)))
1346 (flymake-log 3 "created menu-items with %d item(s)" (length menu-items))))
1347 (if menu-items
1348 (let* ((menu-title (format "Line %d: %d error(s), %d warning(s)" line-no
1349 (flymake-get-line-err-count line-err-info-list "e")
1350 (flymake-get-line-err-count line-err-info-list "w"))))
1351 (list menu-title menu-items))
1352 nil)))
1354 (defun flymake-goto-file-and-line (file line)
1355 "Try to get buffer for FILE and goto line LINE in it."
1356 (if (not (file-exists-p file))
1357 (flymake-log 1 "file %s does not exists" file)
1358 (progn
1359 (find-file file)
1360 (goto-line line))))
1362 ;; flymake minor mode declarations
1363 (defvar flymake-mode-line nil)
1365 (make-variable-buffer-local 'flymake-mode-line)
1367 (defvar flymake-mode-line-e-w nil)
1369 (make-variable-buffer-local 'flymake-mode-line-e-w)
1371 (defvar flymake-mode-line-status nil)
1373 (make-variable-buffer-local 'flymake-mode-line-status)
1375 (defun flymake-report-status (e-w &optional status)
1376 "Show status in mode line."
1377 (when e-w
1378 (setq flymake-mode-line-e-w e-w))
1379 (when status
1380 (setq flymake-mode-line-status status))
1381 (let* ((mode-line " Flymake"))
1382 (when (> (length flymake-mode-line-e-w) 0)
1383 (setq mode-line (concat mode-line ":" flymake-mode-line-e-w)))
1384 (setq mode-line (concat mode-line flymake-mode-line-status))
1385 (setq flymake-mode-line mode-line)
1386 (force-mode-line-update)))
1388 (defun flymake-display-warning (warning)
1389 "Display a warning to user."
1390 (message-box warning))
1392 (defcustom flymake-gui-warnings-enabled t
1393 "Enables/disables GUI warnings."
1394 :group 'flymake
1395 :type 'boolean)
1397 (defun flymake-report-fatal-status (status warning)
1398 "Display a warning and switch flymake mode off."
1399 (when flymake-gui-warnings-enabled
1400 (flymake-display-warning (format "Flymake: %s. Flymake will be switched OFF" warning))
1402 (flymake-mode 0)
1403 (flymake-log 0 "switched OFF Flymake mode for buffer %s due to fatal status %s, warning %s"
1404 (buffer-name) status warning))
1406 (defcustom flymake-start-syntax-check-on-find-file t
1407 "Start syntax check on find file."
1408 :group 'flymake
1409 :type 'boolean)
1411 ;;;###autoload
1412 (define-minor-mode flymake-mode
1413 "Minor mode to do on-the-fly syntax checking.
1414 When called interactively, toggles the minor mode.
1415 With arg, turn Flymake mode on if and only if arg is positive."
1416 :group 'flymake :lighter flymake-mode-line
1417 (cond
1419 ;; Turning the mode ON.
1420 (flymake-mode
1421 (if (not (flymake-can-syntax-check-file buffer-file-name))
1422 (flymake-log 2 "flymake cannot check syntax in buffer %s" (buffer-name))
1423 (add-hook 'after-change-functions 'flymake-after-change-function nil t)
1424 (add-hook 'after-save-hook 'flymake-after-save-hook nil t)
1425 (add-hook 'kill-buffer-hook 'flymake-kill-buffer-hook nil t)
1426 ;;+(add-hook 'find-file-hook 'flymake-find-file-hook)
1428 (flymake-report-status "" "")
1430 (setq flymake-timer
1431 (run-at-time nil 1 'flymake-on-timer-event (current-buffer)))
1433 (when flymake-start-syntax-check-on-find-file
1434 (flymake-start-syntax-check))))
1436 ;; Turning the mode OFF.
1438 (remove-hook 'after-change-functions 'flymake-after-change-function t)
1439 (remove-hook 'after-save-hook 'flymake-after-save-hook t)
1440 (remove-hook 'kill-buffer-hook 'flymake-kill-buffer-hook t)
1441 ;;+(remove-hook 'find-file-hook (function flymake-find-file-hook) t)
1443 (flymake-delete-own-overlays)
1445 (when flymake-timer
1446 (cancel-timer flymake-timer)
1447 (setq flymake-timer nil))
1449 (setq flymake-is-running nil))))
1451 ;;;###autoload
1452 (defun flymake-mode-on ()
1453 "Turn flymake mode on."
1454 (flymake-mode 1)
1455 (flymake-log 1 "flymake mode turned ON for buffer %s" (buffer-name)))
1457 ;;;###autoload
1458 (defun flymake-mode-off ()
1459 "Turn flymake mode off."
1460 (flymake-mode 0)
1461 (flymake-log 1 "flymake mode turned OFF for buffer %s" (buffer-name)))
1463 (defcustom flymake-start-syntax-check-on-newline t
1464 "Start syntax check if newline char was added/removed from the buffer."
1465 :group 'flymake
1466 :type 'boolean)
1468 (defun flymake-after-change-function (start stop len)
1469 "Start syntax check for current buffer if it isn't already running."
1470 ;;+(flymake-log 0 "setting change time to %s" (flymake-float-time))
1471 (let((new-text (buffer-substring start stop)))
1472 (when (and flymake-start-syntax-check-on-newline (equal new-text "\n"))
1473 (flymake-log 3 "starting syntax check as new-line has been seen")
1474 (flymake-start-syntax-check))
1475 (setq flymake-last-change-time (flymake-float-time))))
1477 (defun flymake-after-save-hook ()
1478 (if (local-variable-p 'flymake-mode (current-buffer)) ; (???) other way to determine whether flymake is active in buffer being saved?
1479 (progn
1480 (flymake-log 3 "starting syntax check as buffer was saved")
1481 (flymake-start-syntax-check)))) ; no more mode 3. cannot start check if mode 3 (to temp copies) is active - (???)
1483 (defun flymake-kill-buffer-hook ()
1484 (when flymake-timer
1485 (cancel-timer flymake-timer)
1486 (setq flymake-timer nil)))
1488 (defun flymake-find-file-hook ()
1489 ;;+(when flymake-start-syntax-check-on-find-file
1490 ;;+ (flymake-log 3 "starting syntax check on file open")
1491 ;;+ (flymake-start-syntax-check)
1492 ;;+)
1493 (when (and (not (local-variable-p 'flymake-mode (current-buffer)))
1494 (flymake-can-syntax-check-file buffer-file-name))
1495 (flymake-mode)
1496 (flymake-log 3 "automatically turned ON flymake mode")))
1498 (defun flymake-get-first-err-line-no (err-info-list)
1499 "Return first line with error."
1500 (when err-info-list
1501 (flymake-er-get-line (car err-info-list))))
1503 (defun flymake-get-last-err-line-no (err-info-list)
1504 "Return last line with error."
1505 (when err-info-list
1506 (flymake-er-get-line (nth (1- (length err-info-list)) err-info-list))))
1508 (defun flymake-get-next-err-line-no (err-info-list line-no)
1509 "Return next line with error."
1510 (when err-info-list
1511 (let* ((count (length err-info-list))
1512 (idx 0))
1513 (while (and (< idx count) (>= line-no (flymake-er-get-line (nth idx err-info-list))))
1514 (setq idx (1+ idx)))
1515 (if (< idx count)
1516 (flymake-er-get-line (nth idx err-info-list))))))
1518 (defun flymake-get-prev-err-line-no (err-info-list line-no)
1519 "Return previous line with error."
1520 (when err-info-list
1521 (let* ((count (length err-info-list)))
1522 (while (and (> count 0) (<= line-no (flymake-er-get-line (nth (1- count) err-info-list))))
1523 (setq count (1- count)))
1524 (if (> count 0)
1525 (flymake-er-get-line (nth (1- count) err-info-list))))))
1527 (defun flymake-skip-whitespace ()
1528 "Move forward until non-whitespace is reached."
1529 (while (looking-at "[ \t]")
1530 (forward-char)))
1532 (defun flymake-goto-line (line-no)
1533 "Go to line LINE-NO, then skip whitespace."
1534 (goto-line line-no)
1535 (flymake-skip-whitespace))
1537 (defun flymake-goto-next-error ()
1538 "Go to next error in err ring."
1539 (interactive)
1540 (let ((line-no (flymake-get-next-err-line-no flymake-err-info (flymake-current-line-no))))
1541 (when (not line-no)
1542 (setq line-no (flymake-get-first-err-line-no flymake-err-info))
1543 (flymake-log 1 "passed end of file"))
1544 (if line-no
1545 (flymake-goto-line line-no)
1546 (flymake-log 1 "no errors in current buffer"))))
1548 (defun flymake-goto-prev-error ()
1549 "Go to previous error in err ring."
1550 (interactive)
1551 (let ((line-no (flymake-get-prev-err-line-no flymake-err-info (flymake-current-line-no))))
1552 (when (not line-no)
1553 (setq line-no (flymake-get-last-err-line-no flymake-err-info))
1554 (flymake-log 1 "passed beginning of file"))
1555 (if line-no
1556 (flymake-goto-line line-no)
1557 (flymake-log 1 "no errors in current buffer"))))
1559 (defun flymake-patch-err-text (string)
1560 (if (string-match "^[\n\t :0-9]*\\(.*\\)$" string)
1561 (match-string 1 string)
1562 string))
1564 ;;;; general init-cleanup and helper routines
1565 (defun flymake-create-temp-inplace (file-name prefix)
1566 (unless (stringp file-name)
1567 (error "Invalid file-name"))
1568 (or prefix
1569 (setq prefix "flymake"))
1570 (let* ((temp-name (concat (file-name-sans-extension file-name)
1571 "_" prefix
1572 (and (file-name-extension file-name)
1573 (concat "." (file-name-extension file-name))))))
1574 (flymake-log 3 "create-temp-inplace: file=%s temp=%s" file-name temp-name)
1575 temp-name))
1577 (defun flymake-create-temp-with-folder-structure (file-name prefix)
1578 (unless (stringp file-name)
1579 (error "Invalid file-name"))
1581 (let* ((dir (file-name-directory file-name))
1582 (slash-pos (string-match "/" dir))
1583 (temp-dir (concat (file-name-as-directory (flymake-get-temp-dir)) (substring dir (1+ slash-pos)))))
1585 (file-truename (concat (file-name-as-directory temp-dir)
1586 (file-name-nondirectory file-name)))))
1588 (defun flymake-strrchr (str ch)
1589 (let* ((count (length str))
1590 (pos nil))
1591 (while (and (not pos) (> count 0))
1592 (if (= ch (elt str (1- count)))
1593 (setq pos (1- count)))
1594 (setq count (1- count)))
1595 pos))
1597 (defun flymake-delete-temp-directory (dir-name)
1598 "Attempt to delete temp dir created by `flymake-create-temp-with-folder-structure', do not fail on error."
1599 (let* ((temp-dir (flymake-get-temp-dir))
1600 (suffix (substring dir-name (1+ (length temp-dir))))
1601 (slash-pos nil))
1603 (while (> (length suffix) 0)
1604 ;;+(flymake-log 0 "suffix=%s" suffix)
1605 (flymake-safe-delete-directory (file-truename (concat (file-name-as-directory temp-dir) suffix)))
1606 (setq slash-pos (flymake-strrchr suffix (string-to-char "/")))
1607 (if slash-pos
1608 (setq suffix (substring suffix 0 slash-pos))
1609 (setq suffix "")))))
1611 (defun flymake-init-create-temp-buffer-copy (buffer create-temp-f)
1612 "Make a temporary copy of the current buffer, save its name in buffer data and return the name."
1613 (let* ((source-file-name (buffer-file-name buffer))
1614 (temp-source-file-name (funcall create-temp-f source-file-name "flymake")))
1616 (flymake-save-buffer-in-file buffer temp-source-file-name)
1617 (flymake-set-buffer-value buffer "temp-source-file-name" temp-source-file-name)
1618 temp-source-file-name))
1620 (defun flymake-simple-cleanup (buffer)
1621 "Do cleanup after `flymake-init-create-temp-buffer-copy'.
1622 Delete temp file."
1623 (let* ((temp-source-file-name (flymake-get-buffer-value buffer "temp-source-file-name")))
1624 (flymake-safe-delete-file temp-source-file-name)
1625 (with-current-buffer buffer
1626 (setq flymake-last-change-time nil))))
1628 (defun flymake-get-real-file-name (buffer file-name-from-err-msg)
1629 "Translate file name from error message to \"real\" file name.
1630 Return full-name. Names are real, not patched."
1631 (let* ((real-name nil)
1632 (source-file-name (buffer-file-name buffer))
1633 (master-file-name (flymake-get-buffer-value buffer "master-file-name"))
1634 (temp-source-file-name (flymake-get-buffer-value buffer "temp-source-file-name"))
1635 (temp-master-file-name (flymake-get-buffer-value buffer "temp-master-file-name"))
1636 (base-dirs (list (flymake-get-buffer-value buffer "base-dir")
1637 (file-name-directory source-file-name)
1638 (if master-file-name (file-name-directory master-file-name) nil)))
1639 (files (list (list source-file-name source-file-name)
1640 (list temp-source-file-name source-file-name)
1641 (list master-file-name master-file-name)
1642 (list temp-master-file-name master-file-name))))
1644 (when (equal 0 (length file-name-from-err-msg))
1645 (setq file-name-from-err-msg source-file-name))
1647 (setq real-name (flymake-get-full-patched-file-name file-name-from-err-msg base-dirs files))
1648 ;; if real-name is nil, than file name from err msg is none of the files we've patched
1649 (if (not real-name)
1650 (setq real-name (flymake-get-full-nonpatched-file-name file-name-from-err-msg base-dirs)))
1651 (if (not real-name)
1652 (setq real-name file-name-from-err-msg))
1653 (setq real-name (flymake-fix-file-name real-name))
1654 (flymake-log 3 "get-real-file-name: file-name=%s real-name=%s" file-name-from-err-msg real-name)
1655 real-name))
1657 (defun flymake-get-full-patched-file-name (file-name-from-err-msg base-dirs files)
1658 (let* ((base-dirs-count (length base-dirs))
1659 (file-count (length files))
1660 (real-name nil))
1662 (while (and (not real-name) (> base-dirs-count 0))
1663 (setq file-count (length files))
1664 (while (and (not real-name) (> file-count 0))
1665 (let* ((this-dir (nth (1- base-dirs-count) base-dirs))
1666 (this-file (nth 0 (nth (1- file-count) files)))
1667 (this-real-name (nth 1 (nth (1- file-count) files))))
1668 ;;+(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)
1669 (when (and this-dir this-file (flymake-same-files
1670 (expand-file-name file-name-from-err-msg this-dir)
1671 this-file))
1672 (setq real-name this-real-name)))
1673 (setq file-count (1- file-count)))
1674 (setq base-dirs-count (1- base-dirs-count)))
1675 real-name))
1677 (defun flymake-get-full-nonpatched-file-name (file-name-from-err-msg base-dirs)
1678 (let* ((real-name nil))
1679 (if (file-name-absolute-p file-name-from-err-msg)
1680 (setq real-name file-name-from-err-msg)
1681 (let* ((base-dirs-count (length base-dirs)))
1682 (while (and (not real-name) (> base-dirs-count 0))
1683 (let* ((full-name (expand-file-name file-name-from-err-msg
1684 (nth (1- base-dirs-count) base-dirs))))
1685 (if (file-exists-p full-name)
1686 (setq real-name full-name))
1687 (setq base-dirs-count (1- base-dirs-count))))))
1688 real-name))
1690 (defun flymake-init-find-buildfile-dir (buffer source-file-name buildfile-name)
1691 "Find buildfile, store its dir in buffer data and return its dir, if found."
1692 (let* ((buildfile-dir (flymake-find-buildfile buildfile-name
1693 (file-name-directory source-file-name)
1694 flymake-buildfile-dirs)))
1695 (if (not buildfile-dir)
1696 (progn
1697 (flymake-log 1 "no buildfile (%s) for %s" buildfile-name source-file-name)
1698 (with-current-buffer buffer
1699 (flymake-report-fatal-status "NOMK" (format "No buildfile (%s) found for %s" buildfile-name source-file-name)))
1701 (progn
1702 (flymake-set-buffer-value buffer "base-dir" buildfile-dir)))
1703 buildfile-dir))
1705 (defun flymake-init-create-temp-source-and-master-buffer-copy (buffer get-incl-dirs-f create-temp-f master-file-masks include-regexp-list)
1706 "Find master file (or buffer), create it's copy along with a copy of the source file."
1707 (let* ((source-file-name (buffer-file-name buffer))
1708 (temp-source-file-name (flymake-init-create-temp-buffer-copy buffer create-temp-f))
1709 (master-file-name nil)
1710 (temp-master-file-name nil)
1711 (master-and-temp-master (flymake-create-master-file
1712 source-file-name temp-source-file-name
1713 get-incl-dirs-f create-temp-f
1714 master-file-masks include-regexp-list)))
1716 (if (not master-and-temp-master)
1717 (progn
1718 (flymake-log 1 "cannot find master file for %s" source-file-name)
1719 (when (bufferp buffer)
1720 (with-current-buffer buffer
1721 (flymake-report-status "!" ""))) ; NOMASTER
1723 (progn
1724 (setq master-file-name (nth 0 master-and-temp-master))
1725 (setq temp-master-file-name (nth 1 master-and-temp-master))
1726 (flymake-set-buffer-value buffer "master-file-name" master-file-name)
1727 (flymake-set-buffer-value buffer "temp-master-file-name" temp-master-file-name)
1729 temp-master-file-name))
1731 (defun flymake-master-cleanup (buffer)
1732 (flymake-simple-cleanup buffer)
1733 (flymake-safe-delete-file (flymake-get-buffer-value buffer "temp-master-file-name")))
1735 ;;;; make-specific init-cleanup routines
1736 (defun flymake-get-syntax-check-program-args (source-file-name base-dir use-relative-base-dir use-relative-source get-cmd-line-f)
1737 "Create a command line for syntax check using GET-CMD-LINE-F."
1738 (let* ((my-base-dir base-dir)
1739 (my-source source-file-name))
1741 (when use-relative-base-dir
1742 (setq my-base-dir (flymake-build-relative-filename (file-name-directory source-file-name) base-dir)))
1744 (when use-relative-source
1745 (setq my-source (concat (flymake-build-relative-filename base-dir (file-name-directory source-file-name))
1746 (file-name-nondirectory source-file-name))))
1747 (funcall get-cmd-line-f my-source my-base-dir)))
1749 (defun flymake-get-make-cmdline (source base-dir)
1750 (list "make"
1751 (list "-s"
1752 "-C"
1753 base-dir
1754 (concat "CHK_SOURCES=" source)
1755 "SYNTAX_CHECK_MODE=1"
1756 "check-syntax")))
1758 (defun flymake-get-ant-cmdline (source base-dir)
1759 (list "ant"
1760 (list "-buildfile"
1761 (concat base-dir "/" "build.xml")
1762 (concat "-DCHK_SOURCES=" source)
1763 "check-syntax")))
1765 (defun flymake-simple-make-init-impl (buffer create-temp-f use-relative-base-dir use-relative-source build-file-name get-cmdline-f)
1766 "Create syntax check command line for a directly checked source file.
1767 Use CREATE-TEMP-F for creating temp copy."
1768 (let* ((args nil)
1769 (source-file-name (buffer-file-name buffer))
1770 (buildfile-dir (flymake-init-find-buildfile-dir buffer source-file-name build-file-name)))
1771 (if buildfile-dir
1772 (let* ((temp-source-file-name (flymake-init-create-temp-buffer-copy buffer create-temp-f)))
1773 (setq args (flymake-get-syntax-check-program-args temp-source-file-name buildfile-dir
1774 use-relative-base-dir use-relative-source
1775 get-cmdline-f))))
1776 args))
1778 (defun flymake-simple-make-init (buffer)
1779 (flymake-simple-make-init-impl buffer 'flymake-create-temp-inplace t t "Makefile" 'flymake-get-make-cmdline))
1781 (defun flymake-master-make-init (buffer get-incl-dirs-f master-file-masks include-regexp-list)
1782 "Create make command line for a source file checked via master file compilation."
1783 (let* ((make-args nil)
1784 (temp-master-file-name (flymake-init-create-temp-source-and-master-buffer-copy
1785 buffer get-incl-dirs-f 'flymake-create-temp-inplace
1786 master-file-masks include-regexp-list)))
1787 (when temp-master-file-name
1788 (let* ((buildfile-dir (flymake-init-find-buildfile-dir buffer temp-master-file-name "Makefile")))
1789 (if buildfile-dir
1790 (setq make-args (flymake-get-syntax-check-program-args
1791 temp-master-file-name buildfile-dir nil nil 'flymake-get-make-cmdline)))))
1792 make-args))
1794 (defun flymake-find-make-buildfile (source-dir)
1795 (flymake-find-buildfile "Makefile" source-dir flymake-buildfile-dirs))
1797 ;;;; .h/make specific
1798 (defun flymake-master-make-header-init (buffer)
1799 (flymake-master-make-init buffer
1800 'flymake-get-include-dirs
1801 '(".+\\.cpp$" ".+\\.c$")
1802 '("[ \t]*#[ \t]*include[ \t]*\"\\([\w0-9/\\_\.]*[/\\]*\\)\\(%s\\)\"" 1 2)))
1804 ;;;; .java/make specific
1805 (defun flymake-simple-make-java-init (buffer)
1806 (flymake-simple-make-init-impl buffer 'flymake-create-temp-with-folder-structure nil nil "Makefile" 'flymake-get-make-cmdline))
1808 (defun flymake-simple-ant-java-init (buffer)
1809 (flymake-simple-make-init-impl buffer 'flymake-create-temp-with-folder-structure nil nil "build.xml" 'flymake-get-ant-cmdline))
1811 (defun flymake-simple-java-cleanup (buffer)
1812 "Cleanup after `flymake-simple-make-java-init' -- delete temp file and dirs."
1813 (let* ((temp-source-file-name (flymake-get-buffer-value buffer "temp-source-file-name")))
1814 (flymake-safe-delete-file temp-source-file-name)
1815 (when temp-source-file-name
1816 (flymake-delete-temp-directory (file-name-directory temp-source-file-name)))))
1818 ;;;; perl-specific init-cleanup routines
1819 (defun flymake-perl-init (buffer)
1820 (let* ((temp-file (flymake-init-create-temp-buffer-copy
1821 buffer 'flymake-create-temp-inplace))
1822 (local-file (concat (flymake-build-relative-filename
1823 (file-name-directory buffer-file-name)
1824 (file-name-directory temp-file))
1825 (file-name-nondirectory temp-file))))
1826 (list "perl" (list "-wc " local-file))))
1828 ;;;; tex-specific init-cleanup routines
1829 (defun flymake-get-tex-args (file-name)
1830 ;;(list "latex" (list "-c-style-errors" file-name))
1831 (list "texify" (list "--pdf" "--tex-option=-c-style-errors" file-name)))
1833 (defun flymake-simple-tex-init (buffer)
1834 (flymake-get-tex-args (flymake-init-create-temp-buffer-copy buffer 'flymake-create-temp-inplace)))
1836 (defun flymake-master-tex-init (buffer)
1837 (let* ((temp-master-file-name (flymake-init-create-temp-source-and-master-buffer-copy
1838 buffer 'flymake-get-include-dirs-dot 'flymake-create-temp-inplace
1839 '(".+\\.tex$")
1840 '("[ \t]*\\input[ \t]*{\\(.*\\)\\(%s\\)}" 1 2))))
1841 (when temp-master-file-name
1842 (flymake-get-tex-args temp-master-file-name))))
1844 (defun flymake-get-include-dirs-dot (base-dir)
1845 '("."))
1847 ;;;; xml-specific init-cleanup routines
1848 (defun flymake-xml-init (buffer)
1849 (list "xml" (list "val" (flymake-init-create-temp-buffer-copy buffer 'flymake-create-temp-inplace))))
1851 (provide 'flymake)
1853 ;; arch-tag: 8f0d6090-061d-4cac-8862-7c151c4a02dd
1854 ;;; flymake.el ends here