(all): Make `indicate-buffer-boundaries' display values set outside
[bpt/emacs.git] / lisp / progmodes / flymake.el
CommitLineData
4bcbcb9d
EZ
1;;; flymake.el -- a universal on-the-fly syntax checker
2
034babe1 3;; Copyright (C) 2003, 2004, 2005 Free Software Foundation
4bcbcb9d
EZ
4
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
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 2, or (at your option)
15;; any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs; see the file COPYING. If not, write to the
3a35cf56
LK
24;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25;; Boston, MA 02110-1301, USA.
4bcbcb9d
EZ
26
27;;; Commentary:
28;;
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)
32
33;;; Code:
34
c92d6023
JB
35;;;; [[ Silence the byte-compiler
36
0ea7c863
SM
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)
c92d6023
JB
43
44;;;; ]]
45
ce721de4
KS
46;;;; [[ Xemacs overlay compatibility
47(if (featurep 'xemacs) (progn
4bcbcb9d
EZ
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)
ce721de4 54))
4bcbcb9d
EZ
55;;;; ]]
56
57;;;; [[ cross-emacs compatibility routines
ce721de4
KS
58(defsubst flymake-makehash (&optional test)
59 (if (fboundp 'make-hash-table)
60 (if test (make-hash-table :test test) (make-hash-table))
adf8a02e
RS
61 (with-no-warnings
62 (makehash test))))
5323b4b0 63
cd2325cd
SM
64(defalias 'flymake-float-time
65 (if (fboundp 'float-time)
66 'float-time
7ea8834e
RS
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)))))))
ce721de4
KS
71
72(defsubst flymake-replace-regexp-in-string (regexp rep str)
7ea8834e
RS
73 (if (fboundp 'replace-in-string)
74 (replace-in-string str regexp rep)
75 (replace-regexp-in-string regexp rep str)))
ce721de4
KS
76
77(defun flymake-split-string (str pattern)
53ec26ed
RS
78 "Split STR into a list of substrings bounded by PATTERN.
79Zero-length substrings at the beginning and end of the list are omitted."
5323b4b0
RS
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))
86
ce721de4 87(defsubst flymake-get-temp-dir ()
cd2325cd 88 (if (fboundp 'temp-directory)
ce721de4
KS
89 (temp-directory)
90 temporary-file-directory))
4bcbcb9d 91
cd2325cd
SM
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)))))
4bcbcb9d 96
cd2325cd
SM
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)))))
5323b4b0
RS
101
102(defun flymake-popup-menu (pos menu-data)
53ec26ed
RS
103 "Pop up the flymake menu at position POS, using the data MENU-DATA.
104POS is a list of the form ((X Y) WINDOW), where X and Y are
105pixels positions from the top left corner of WINDOW's frame.
106MENU-DATA is a list of error and warning messages returned by
107`flymake-make-err-menu-data'."
108 (if (featurep 'xemacs)
ce721de4
KS
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))))
4bcbcb9d 116
5323b4b0 117(defun flymake-make-emacs-menu (menu-data)
53ec26ed
RS
118 "Return a menu specifier using MENU-DATA.
119MENU-DATA is a list of error and warning messages returned by
120`flymake-make-err-menu-data'.
121See `x-popup-menu' for the menu specifier format."
5323b4b0
RS
122 (let* ((menu-title (nth 0 menu-data))
123 (menu-items (nth 1 menu-data))
124 (menu-commands nil))
5323b4b0
RS
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))))
4bcbcb9d 129
ce721de4
KS
130(if (featurep 'xemacs) (progn
131
cd2325cd
SM
132(defun flymake-nop ())
133
2aa13ec4 134(defun flymake-make-xemacs-menu (menu-data)
53ec26ed 135 "Return a menu specifier using MENU-DATA."
5323b4b0
RS
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)))
143
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))
156
ce721de4
KS
157)) ;; xemacs
158
5323b4b0
RS
159(defun flymake-current-row ()
160 "Return current row number in current frame."
cd2325cd
SM
161 (if (fboundp 'window-edges)
162 (+ (car (cdr (window-edges))) (count-lines (window-start) (point)))
163 (count-lines (window-start) (point))))
5323b4b0
RS
164
165(defun flymake-selected-frame ()
cd2325cd
SM
166 (if (fboundp 'window-edges)
167 (selected-frame)
168 (selected-window)))
4bcbcb9d
EZ
169
170;;;; ]]
171
172(defcustom flymake-log-level -1
cd2325cd 173 "Logging level, only messages with level lower or equal will be logged.
4bcbcb9d 174-1 = NONE, 0 = ERROR, 1 = WARNING, 2 = INFO, 3 = DEBUG"
5323b4b0
RS
175 :group 'flymake
176 :type 'integer)
4bcbcb9d 177
2aa13ec4 178(defun flymake-log (level text &rest args)
53ec26ed
RS
179 "Log a message at level LEVEL.
180If LEVEL is higher than `flymake-log-level', the message is
181ignored. Otherwise, it is printed using `message'.
182TEXT is a format control string, and the remaining arguments ARGS
183are the string substitutions (see `format')."
5323b4b0
RS
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
191 ;;)
2aa13ec4 192 )))
4bcbcb9d 193
2aa13ec4 194(defun flymake-ins-after (list pos val)
5323b4b0
RS
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))
4bcbcb9d 199
2aa13ec4 200(defun flymake-set-at (list pos val)
9a686ad2 201 "Set VAL at position POS in LIST."
5323b4b0
RS
202 (let ((tmp (copy-sequence list))) ; (???)
203 (setcar (nthcdr pos tmp) val)
204 tmp))
4bcbcb9d 205
2aa13ec4 206(defvar flymake-pid-to-names (flymake-makehash)
53ec26ed 207 "Hash table mapping PIDs to source buffer names and output files.")
4bcbcb9d 208
2aa13ec4 209(defun flymake-reg-names (pid source-buffer-name)
53ec26ed 210 "Associate PID with SOURCE-BUFFER-NAME in `flymake-pid-to-names'."
5323b4b0
RS
211 (unless (stringp source-buffer-name)
212 (error "Invalid buffer name"))
213 (puthash pid (list source-buffer-name) flymake-pid-to-names))
4bcbcb9d 214
2aa13ec4 215(defun flymake-get-source-buffer-name (pid)
53ec26ed 216 "Return buffer name associated with PID in `flymake-pid-to-names'."
5323b4b0 217 (nth 0 (gethash pid flymake-pid-to-names)))
4bcbcb9d 218
2aa13ec4 219(defun flymake-unreg-names (pid)
53ec26ed 220 "Remove the entry associated with PID from `flymake-pid-to-names'."
5323b4b0 221 (remhash pid flymake-pid-to-names))
4bcbcb9d 222
2aa13ec4
RS
223(defvar flymake-buffer-data (flymake-makehash)
224 "Data specific to syntax check tool, in name-value pairs.")
4bcbcb9d 225
4bcbcb9d 226(make-variable-buffer-local 'flymake-buffer-data)
4bcbcb9d 227
2aa13ec4 228(defun flymake-get-buffer-value (buffer name)
53ec26ed 229 (gethash name (with-current-buffer buffer flymake-buffer-data)))
2aa13ec4
RS
230
231(defun flymake-set-buffer-value (buffer name value)
53ec26ed 232 (puthash name value (with-current-buffer buffer flymake-buffer-data)))
2aa13ec4 233
9a686ad2 234(defvar flymake-output-residual nil)
2aa13ec4 235
4bcbcb9d 236(make-variable-buffer-local 'flymake-output-residual)
2aa13ec4 237
5323b4b0
RS
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)
9a686ad2
SM
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)
5323b4b0
RS
259 )
260 "*Files syntax checking is allowed for."
261 :group 'flymake
262 :type '(repeat (string symbol symbol symbol)))
4bcbcb9d 263
2aa13ec4 264(defun flymake-get-file-name-mode-and-masks (file-name)
9a686ad2 265 "Return the corresponding entry from `flymake-allowed-file-name-masks'."
5323b4b0
RS
266 (unless (stringp file-name)
267 (error "Invalid file-name"))
17404091 268 (let ((fnm flymake-allowed-file-name-masks)
5323b4b0 269 (mode-and-masks nil))
17404091
SM
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)))
5323b4b0
RS
274 (flymake-log 3 "file %s, init=%s" file-name (car mode-and-masks))
275 mode-and-masks))
4bcbcb9d 276
2aa13ec4 277(defun flymake-can-syntax-check-file (file-name)
5323b4b0 278 "Determine whether we can syntax check FILE-NAME.
2aa13ec4 279Return nil if we cannot, non-nil if we can."
5323b4b0 280 (if (flymake-get-init-function file-name) t nil))
4bcbcb9d 281
2aa13ec4 282(defun flymake-get-init-function (file-name)
5323b4b0
RS
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))))
9a686ad2
SM
285 ;;(flymake-log 0 "calling %s" init-f)
286 ;;(funcall init-f (current-buffer))
5323b4b0 287 init-f))
4bcbcb9d 288
2aa13ec4 289(defun flymake-get-cleanup-function (file-name)
5323b4b0
RS
290 "Return cleanup function to be used for the file."
291 (nth 1 (flymake-get-file-name-mode-and-masks file-name)))
4bcbcb9d 292
2aa13ec4 293(defun flymake-get-real-file-name-function (file-name)
5323b4b0 294 (or (nth 2 (flymake-get-file-name-mode-and-masks file-name)) 'flymake-get-real-file-name))
4bcbcb9d
EZ
295
296(defcustom flymake-buildfile-dirs '("." ".." "../.." "../../.." "../../../.." "../../../../.." "../../../../../.." "../../../../../../.." "../../../../../../../.." "../../../../../../../../.." "../../../../../../../../../.." "../../../../../../../../../../..")
5323b4b0
RS
297 "Dirs to look for buildfile."
298 :group 'flymake
299 :type '(repeat (string)))
4bcbcb9d
EZ
300
301(defvar flymake-find-buildfile-cache (flymake-makehash 'equal))
4bcbcb9d 302
2aa13ec4 303(defun flymake-get-buildfile-from-cache (dir-name)
5323b4b0 304 (gethash dir-name flymake-find-buildfile-cache))
2aa13ec4
RS
305
306(defun flymake-add-buildfile-to-cache (dir-name buildfile)
5323b4b0 307 (puthash dir-name buildfile flymake-find-buildfile-cache))
2aa13ec4
RS
308
309(defun flymake-clear-buildfile-cache ()
5323b4b0 310 (clrhash flymake-find-buildfile-cache))
2aa13ec4
RS
311
312(defun flymake-find-buildfile (buildfile-name source-dir-name dirs)
5323b4b0 313 "Find buildfile starting from current directory.
2aa13ec4 314Buildfile includes Makefile, build.xml etc.
5323b4b0
RS
315Return 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)
5323b4b0 321 (found nil))
17404091
SM
322 (while (and (not found) dirs)
323 (setq buildfile-dir (concat source-dir-name (car dirs)))
5323b4b0
RS
324 (setq buildfile (concat buildfile-dir "/" buildfile-name))
325 (when (file-exists-p buildfile)
326 (setq found t))
17404091 327 (setq dirs (cdr dirs)))
5323b4b0
RS
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)
4bcbcb9d 333 (progn
5323b4b0
RS
334 (flymake-log 3 "buildfile for %s not found" source-dir-name)
335 nil)))))
336
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)))
4bcbcb9d 346
2aa13ec4 347(defun flymake-same-files (file-name-one file-name-two)
5323b4b0 348 "Check if FILE-NAME-ONE and FILE-NAME-TWO point to same file.
2aa13ec4 349Return t if so, nil if not."
5323b4b0
RS
350 (equal (flymake-fix-file-name file-name-one)
351 (flymake-fix-file-name file-name-two)))
352
5323b4b0
RS
353(defun flymake-get-common-file-prefix (string-one string-two)
354 "Return common prefix for two file names STRING-ONE and STRING-TWO."
4bd0a5d0
SM
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)))))
5323b4b0
RS
361
362(defun flymake-build-relative-filename (from-dir to-dir)
363 "Return rel: FROM-DIR/rel == TO-DIR."
9a686ad2 364 ;; FIXME: Why not use `file-relative-name'?
5323b4b0
RS
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)
4bd0a5d0
SM
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)))
5323b4b0
RS
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)))
378
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 "./"))))
4bcbcb9d
EZ
389
390(defcustom flymake-master-file-dirs '("." "./src" "./UnitTest")
9a686ad2 391 "Dirs where to look for master files."
5323b4b0
RS
392 :group 'flymake
393 :type '(repeat (string)))
4bcbcb9d
EZ
394
395(defcustom flymake-master-file-count-limit 32
5323b4b0
RS
396 "Max number of master files to check."
397 :group 'flymake
398 :type 'integer)
2aa13ec4 399
ce721de4
KS
400;; This is bound dynamically to pass a parameter to a sort predicate below
401(defvar flymake-included-file-name)
4bcbcb9d 402
2aa13ec4 403(defun flymake-find-possible-master-files (file-name master-file-dirs masks)
9a686ad2 404 "Find (by name and location) all possible master files.
173569aa 405Master files are .cpp and .c for and .h. Files are searched for
2aa13ec4 406starting from the .h directory and max max-level parent dirs.
4bcbcb9d 407File contents are not checked."
17404091 408 (let* ((dirs master-file-dirs)
5323b4b0 409 (files nil)
17404091
SM
410 (done nil))
411
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)))
419
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))
5323b4b0
RS
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)))
17404091
SM
428 (setq dir-files (cdr dir-files))))
429 (setq masks (cdr masks))))
430 (setq dirs (cdr dirs)))
5323b4b0 431 (when files
ce721de4
KS
432 (let ((flymake-included-file-name (file-name-nondirectory file-name)))
433 (setq files (sort files 'flymake-master-file-compare))))
5323b4b0
RS
434 (flymake-log 3 "found %d possible master file(s)" (length files))
435 files))
4bcbcb9d 436
2aa13ec4 437(defun flymake-master-file-compare (file-one file-two)
173569aa 438 "Compare two files specified by FILE-ONE and FILE-TWO.
2aa13ec4 439This function is used in sort to move most possible file names
173569aa 440to the beginning of the list (File.h -> File.cpp moved to top)."
5323b4b0
RS
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))))
4bcbcb9d
EZ
444
445(defcustom flymake-check-file-limit 8192
5323b4b0
RS
446 "Max number of chars to look at when checking possible master file."
447 :group 'flymake
448 :type 'integer)
4bcbcb9d 449
2aa13ec4 450(defun flymake-check-patch-master-file-buffer (master-file-temp-buffer
5323b4b0
RS
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.
2aa13ec4
RS
455For .cpp master file this means it includes SOURCE-FILE-NAME (.h).
456If yes, patch a copy of MASTER-FILE-NAME to include PATCHED-SOURCE-FILE-NAME
457instead of SOURCE-FILE-NAME.
458Whether a buffer for MATER-FILE-NAME exists, use it as a source
459instead of reading master file from disk."
5323b4b0
RS
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)))
479
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
9a686ad2 488 ;; XEmacs with 'last match not a buffer' error as
5323b4b0 489 ;; check-includes calls replace-in-string
4bd0a5d0 490 (flymake-replace-region match-beg match-end
5323b4b0
RS
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)))
9a686ad2 495 ;;+(flymake-log 3 "killing buffer %s" (buffer-name master-file-temp-buffer))
5323b4b0 496 (kill-buffer master-file-temp-buffer)))
9a686ad2 497 ;;+(flymake-log 3 "check-patch master file %s: %s" master-file-name found)
5323b4b0
RS
498 (when found
499 (flymake-log 2 "found master file %s" master-file-name))
500 found))
4bcbcb9d 501
4bd0a5d0 502(defun flymake-replace-region (beg end rep)
5323b4b0
RS
503 "Replace text in BUFFER in region (BEG END) with REP."
504 (save-excursion
4bd0a5d0
SM
505 (goto-char end)
506 ;; Insert before deleting, so as to better preserve markers's positions.
507 (insert rep)
508 (delete-region beg end)))
4bcbcb9d 509
2aa13ec4 510(defun flymake-read-file-to-temp-buffer (file-name)
5323b4b0
RS
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))))))
cd2325cd 513 (with-current-buffer temp-buffer
5323b4b0
RS
514 (insert-file-contents file-name))
515 temp-buffer))
4bcbcb9d 516
2aa13ec4 517(defun flymake-copy-buffer-to-temp-buffer (buffer)
5323b4b0
RS
518 "Copy contents of BUFFER into newly created temp buffer."
519 (let ((contents nil)
520 (temp-buffer nil))
cd2325cd 521 (with-current-buffer buffer
5323b4b0
RS
522 (setq contents (buffer-string))
523
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))
4bcbcb9d 528
2aa13ec4 529(defun flymake-check-include (source-file-name inc-path inc-name include-dirs)
5323b4b0 530 "Check if SOURCE-FILE-NAME can be found in include path.
2aa13ec4 531Return t if it can be found via include path using INC-PATH and INC-NAME."
5323b4b0
RS
532 (if (file-name-absolute-p inc-path)
533 (flymake-same-files source-file-name (concat inc-path "/" inc-name))
17404091 534 (let* ((file-name nil)
5323b4b0 535 (found nil))
17404091
SM
536 (while (and (not found) include-dirs)
537 (setq file-name (concat (file-name-directory source-file-name)
538 "/" (car include-dirs)))
5323b4b0
RS
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))
17404091 544 (setq include-dirs (cdr include-dirs)))
5323b4b0 545 found)))
4bcbcb9d 546
2aa13ec4 547(defun flymake-find-buffer-for-file (file-name)
5323b4b0 548 "Check if there exists a buffer visiting FILE-NAME.
2aa13ec4 549Return t if so, nil if not."
5323b4b0
RS
550 (let ((buffer-name (get-file-buffer file-name)))
551 (if buffer-name
552 (get-buffer buffer-name))))
4bcbcb9d 553
2aa13ec4 554(defun flymake-create-master-file (source-file-name patched-source-file-name get-incl-dirs-f create-temp-f masks include-regexp-list)
5323b4b0 555 "Save SOURCE-FILE-NAME with a different name.
2aa13ec4 556Find master file, patch and save it."
5323b4b0
RS
557 (let* ((possible-master-files (flymake-find-possible-master-files source-file-name flymake-master-file-dirs masks))
558 (master-file-count (length possible-master-files))
559 (idx 0)
560 (temp-buffer nil)
561 (master-file-name nil)
562 (patched-master-file-name nil)
563 (found nil))
564
565 (while (and (not found) (< idx master-file-count))
566 (setq master-file-name (nth idx possible-master-files))
567 (setq patched-master-file-name (funcall create-temp-f master-file-name "flymake_master"))
568 (if (flymake-find-buffer-for-file master-file-name)
569 (setq temp-buffer (flymake-copy-buffer-to-temp-buffer (flymake-find-buffer-for-file master-file-name)))
570 (setq temp-buffer (flymake-read-file-to-temp-buffer master-file-name)))
571 (setq found
572 (flymake-check-patch-master-file-buffer
573 temp-buffer
574 master-file-name
575 patched-master-file-name
576 source-file-name
577 patched-source-file-name
578 (funcall get-incl-dirs-f (file-name-directory master-file-name))
579 include-regexp-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))))
4bcbcb9d 587
2aa13ec4 588(defun flymake-save-buffer-in-file (buffer file-name)
5323b4b0
RS
589 (or buffer
590 (error "Invalid buffer"))
cd2325cd 591 (with-current-buffer buffer
5323b4b0 592 (save-restriction
5323b4b0
RS
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))
4bcbcb9d 597
2aa13ec4 598(defun flymake-save-string-to-file (file-name data)
5323b4b0
RS
599 "Save string DATA to file FILE-NAME."
600 (write-region data nil file-name nil 566))
4bcbcb9d 601
2aa13ec4 602(defun flymake-read-file-to-string (file-name)
5323b4b0
RS
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))))
4bcbcb9d 607
2aa13ec4 608(defun flymake-process-filter (process output)
5323b4b0 609 "Parse OUTPUT and highlight error lines.
2aa13ec4 610It's flymake process filter."
5323b4b0
RS
611 (let* ((pid (process-id process))
612 (source-buffer (get-buffer (flymake-get-source-buffer-name pid))))
4bcbcb9d 613
5323b4b0
RS
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))))
4bcbcb9d 617
2aa13ec4 618(defun flymake-process-sentinel (process event)
5323b4b0
RS
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))))
626
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)
632
633 (flymake-unreg-names pid)
634 (delete-process process)
635
636 (when source-buffer
cd2325cd 637 (with-current-buffer source-buffer
5323b4b0
RS
638
639 (flymake-parse-residual source-buffer)
640 (flymake-post-syntax-check source-buffer exit-status command)
53ec26ed 641 (setq flymake-is-running nil))))
5323b4b0
RS
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)
53ec26ed
RS
646 (with-current-buffer source-buffer
647 (setq flymake-is-running nil))))))))
4bcbcb9d 648
2aa13ec4 649(defun flymake-post-syntax-check (source-buffer exit-status command)
53ec26ed
RS
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))))
5323b4b0 656 (flymake-delete-own-overlays source-buffer)
53ec26ed
RS
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)"
5323b4b0 664 (buffer-name source-buffer) err-count warn-count
53ec26ed
RS
665 (- (flymake-float-time) flymake-check-start-time))
666 (setq flymake-check-start-time nil))
667
5323b4b0
RS
668 (if (and (equal 0 err-count) (equal 0 warn-count))
669 (if (equal 0 exit-status)
670 (flymake-report-status source-buffer "" "") ; PASSED
53ec26ed
RS
671 (if (not (with-current-buffer source-buffer
672 flymake-check-was-interrupted))
5323b4b0
RS
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) ""))))
4bcbcb9d 677
2aa13ec4 678(defun flymake-parse-output-and-residual (source-buffer output)
5323b4b0 679 "Split OUTPUT into lines, merge in residual if necessary."
cd2325cd 680 (with-current-buffer source-buffer
53ec26ed 681 (let* ((buffer-residual flymake-output-residual)
5323b4b0
RS
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)))
53ec26ed
RS
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))))))
4bcbcb9d 692
2aa13ec4 693(defun flymake-parse-residual (source-buffer)
5323b4b0 694 "Parse residual if it's non empty."
cd2325cd 695 (with-current-buffer source-buffer
53ec26ed
RS
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))))
4bcbcb9d
EZ
703
704(defvar flymake-err-info nil
2aa13ec4
RS
705 "Sorted list of line numbers and lists of err info in the form (file, err-text).")
706
4bcbcb9d 707(make-variable-buffer-local 'flymake-err-info)
2aa13ec4 708
2aa13ec4 709(defun flymake-er-make-er (line-no line-err-info-list)
5323b4b0 710 (list line-no line-err-info-list))
2aa13ec4
RS
711
712(defun flymake-er-get-line (err-info)
5323b4b0 713 (nth 0 err-info))
2aa13ec4
RS
714
715(defun flymake-er-get-line-err-info-list (err-info)
5323b4b0 716 (nth 1 err-info))
4bcbcb9d
EZ
717
718(defvar flymake-new-err-info nil
69df8d97 719 "Same as `flymake-err-info', effective when a syntax check is in progress.")
2aa13ec4 720
4bcbcb9d 721(make-variable-buffer-local 'flymake-new-err-info)
2aa13ec4 722
4bcbcb9d 723;; getters/setters for line-err-info: (file, line, type, text).
2aa13ec4 724(defun flymake-ler-make-ler (file line type text &optional full-file)
5323b4b0 725 (list file line type text full-file))
2aa13ec4
RS
726
727(defun flymake-ler-get-file (line-err-info)
5323b4b0 728 (nth 0 line-err-info))
2aa13ec4
RS
729
730(defun flymake-ler-get-line (line-err-info)
5323b4b0 731 (nth 1 line-err-info))
2aa13ec4
RS
732
733(defun flymake-ler-get-type (line-err-info)
5323b4b0 734 (nth 2 line-err-info))
2aa13ec4
RS
735
736(defun flymake-ler-get-text (line-err-info)
5323b4b0 737 (nth 3 line-err-info))
2aa13ec4
RS
738
739(defun flymake-ler-get-full-file (line-err-info)
5323b4b0 740 (nth 4 line-err-info))
2aa13ec4
RS
741
742(defun flymake-ler-set-file (line-err-info file)
5323b4b0 743 (flymake-ler-make-ler file
4bcbcb9d
EZ
744 (flymake-ler-get-line line-err-info)
745 (flymake-ler-get-type line-err-info)
746 (flymake-ler-get-text line-err-info)
2aa13ec4
RS
747 (flymake-ler-get-full-file line-err-info)))
748
749(defun flymake-ler-set-full-file (line-err-info full-file)
5323b4b0 750 (flymake-ler-make-ler (flymake-ler-get-file line-err-info)
4bcbcb9d
EZ
751 (flymake-ler-get-line line-err-info)
752 (flymake-ler-get-type line-err-info)
753 (flymake-ler-get-text line-err-info)
2aa13ec4
RS
754 full-file))
755
756(defun flymake-ler-set-line (line-err-info line)
5323b4b0 757 (flymake-ler-make-ler (flymake-ler-get-file line-err-info)
4bcbcb9d
EZ
758 line
759 (flymake-ler-get-type line-err-info)
760 (flymake-ler-get-text line-err-info)
2aa13ec4 761 (flymake-ler-get-full-file line-err-info)))
4bcbcb9d 762
2aa13ec4 763(defun flymake-get-line-err-count (line-err-info-list type)
5323b4b0 764 "Return number of errors of specified TYPE.
173569aa 765Value of TYPE is either \"e\" or \"w\"."
5323b4b0
RS
766 (let* ((idx 0)
767 (count (length line-err-info-list))
768 (err-count 0))
4bcbcb9d 769
5323b4b0
RS
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))
4bcbcb9d 775
2aa13ec4 776(defun flymake-get-err-count (err-info-list type)
5323b4b0
RS
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))
2aa13ec4
RS
785
786(defun flymake-fix-line-numbers (err-info-list min-line max-line)
5323b4b0 787 "Replace line numbers with fixed value.
2aa13ec4
RS
788If line-numbers is less than MIN-LINE, set line numbers to MIN-LINE.
789If line numbers is greater than MAX-LINE, set line numbers to MAX-LINE.
ce721de4 790The reason for this fix is because some compilers might report
2aa13ec4 791line number outside the file being compiled."
5323b4b0
RS
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)
4bcbcb9d 805
2aa13ec4 806(defun flymake-highlight-err-lines (buffer err-info-list)
5323b4b0 807 "Highlight error lines in BUFFER using info from ERR-INFO-LIST."
cd2325cd 808 (with-current-buffer buffer
5323b4b0
RS
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))))))
4bcbcb9d 814
2aa13ec4 815(defun flymake-overlay-p (ov)
5323b4b0
RS
816 "Determine whether overlay OV was created by flymake."
817 (and (overlayp ov) (overlay-get ov 'flymake-overlay)))
4bcbcb9d 818
2aa13ec4 819(defun flymake-make-overlay (beg end tooltip-text face mouse-face)
5323b4b0
RS
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)
9a686ad2 828 ;;+(flymake-log 3 "created overlay %s" ov)
5323b4b0
RS
829 ov)
830 (flymake-log 3 "created an overlay at (%d-%d)" beg end)))
4bcbcb9d 831
2aa13ec4 832(defun flymake-delete-own-overlays (buffer)
5323b4b0 833 "Delete all flymake overlays in BUFFER."
cd2325cd 834 (with-current-buffer buffer
5323b4b0
RS
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))
9a686ad2 839 ;;+(flymake-log 3 "deleted overlay %s" ov)
5323b4b0
RS
840 )
841 (setq ov (cdr ov))))))
4bcbcb9d 842
2aa13ec4 843(defun flymake-region-has-flymake-overlays (beg end)
5323b4b0 844 "Check if region specified by BEG and END has overlay.
2aa13ec4 845Return t if it has at least one flymake overlay, nil if no overlay."
5323b4b0
RS
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))
cd2325cd
SM
851 (setq ov (cdr ov)))
852 has-flymake-overlays))
4bcbcb9d 853
ded09abd 854(defface flymake-errline
9a686ad2
SM
855 ;;+ '((((class color)) (:foreground "OrangeRed" :bold t :underline t))
856 ;;+ '((((class color)) (:underline "OrangeRed"))
5323b4b0
RS
857 '((((class color)) (:background "LightPink"))
858 (t (:bold t)))
859 "Face used for marking error lines."
860 :group 'flymake)
ded09abd
MB
861;; backward-compatibility alias
862(put 'flymake-errline-face 'face-alias 'flymake-errline)
4bcbcb9d 863
ded09abd 864(defface flymake-warnline
5323b4b0
RS
865 '((((class color)) (:background "LightBlue2"))
866 (t (:bold t)))
867 "Face used for marking warning lines."
868 :group 'flymake)
ded09abd
MB
869;; backward-compatibility alias
870(put 'flymake-warnline-face 'face-alias 'flymake-warnline)
4bcbcb9d 871
2aa13ec4 872(defun flymake-highlight-line (line-no line-err-info-list)
5323b4b0 873 "Highlight line LINE-NO in current buffer.
173569aa 874Perhaps use text from LINE-ERR-INFO-LIST to enhance highlighting."
5323b4b0
RS
875 (goto-line line-no)
876 (let* ((line-beg (flymake-line-beginning-position))
877 (line-end (flymake-line-end-position))
878 (beg line-beg)
879 (end line-end)
880 (tooltip-text (flymake-ler-get-text (nth 0 line-err-info-list)))
881 (face nil))
882
883 (goto-char line-beg)
884 (while (looking-at "[ \t]")
885 (forward-char))
4bcbcb9d 886
5323b4b0 887 (setq beg (point))
4bcbcb9d 888
5323b4b0
RS
889 (goto-char line-end)
890 (while (and (looking-at "[ \t\r\n]") (> (point) 1))
891 (backward-char))
4bcbcb9d 892
5323b4b0 893 (setq end (1+ (point)))
4bcbcb9d 894
5323b4b0
RS
895 (when (<= end beg)
896 (setq beg line-beg)
897 (setq end line-end))
2aa13ec4 898
5323b4b0
RS
899 (when (= end beg)
900 (goto-char end)
901 (forward-line)
902 (setq end (point)))
2aa13ec4 903
5323b4b0 904 (if (> (flymake-get-line-err-count line-err-info-list "e") 0)
ded09abd
MB
905 (setq face 'flymake-errline)
906 (setq face 'flymake-warnline))
2aa13ec4 907
5323b4b0 908 (flymake-make-overlay beg end tooltip-text face nil)))
4bcbcb9d 909
2aa13ec4 910(defun flymake-parse-err-lines (err-info-list source-buffer lines)
5323b4b0
RS
911 "Parse err LINES, store info in ERR-INFO-LIST."
912 (let* ((count (length lines))
913 (idx 0)
914 (line-err-info nil)
915 (real-file-name nil)
916 (source-file-name (buffer-file-name source-buffer))
917 (get-real-file-name-f (flymake-get-real-file-name-function source-file-name)))
918
919 (while (< idx count)
920 (setq line-err-info (flymake-parse-line (nth idx lines)))
921 (when line-err-info
922 (setq real-file-name (funcall get-real-file-name-f source-buffer (flymake-ler-get-file line-err-info)))
923 (setq line-err-info (flymake-ler-set-full-file line-err-info real-file-name))
924
925 (if (flymake-same-files real-file-name source-file-name)
926 (setq line-err-info (flymake-ler-set-file line-err-info nil))
927 (setq line-err-info (flymake-ler-set-file line-err-info (file-name-nondirectory real-file-name))))
928
929 (setq err-info-list (flymake-add-err-info err-info-list line-err-info)))
930 (flymake-log 3 "parsed '%s', %s line-err-info" (nth idx lines) (if line-err-info "got" "no"))
931 (setq idx (1+ idx)))
932 err-info-list))
4bcbcb9d 933
2aa13ec4 934(defun flymake-split-output (output)
5323b4b0 935 "Split OUTPUT into lines.
9a686ad2
SM
936Return last one as residual if it does not end with newline char.
937Returns ((LINES) RESIDUAL)."
5323b4b0
RS
938 (when (and output (> (length output) 0))
939 (let* ((lines (flymake-split-string output "[\n\r]+"))
940 (complete (equal "\n" (char-to-string (aref output (1- (length output))))))
941 (residual nil))
942 (when (not complete)
943 (setq residual (car (last lines)))
944 (setq lines (butlast lines)))
945 (list lines residual))))
4bcbcb9d 946
2aa13ec4 947(defun flymake-reformat-err-line-patterns-from-compile-el (original-list)
5323b4b0 948 "Grab error line patterns from ORIGINAL-LIST in compile.el format.
2aa13ec4 949Convert it to flymake internal format."
5323b4b0 950 (let* ((converted-list '()))
cd2325cd
SM
951 (dolist (item original-list)
952 (setq item (cdr item))
953 (let ((regexp (nth 0 item))
954 (file (nth 1 item))
955 (line (nth 2 item))
956 (col (nth 3 item)))
957 (if (consp file) (setq file (car file)))
958 (if (consp line) (setq line (car line)))
959 (if (consp col) (setq col (car col)))
d799c278 960
cd2325cd
SM
961 (when (not (functionp line))
962 (setq converted-list (cons (list regexp file line col) converted-list)))))
5323b4b0 963 converted-list))
2aa13ec4
RS
964
965(eval-when-compile
5323b4b0
RS
966 (require 'compile))
967
968(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
969 (append
970 '(
9a686ad2 971 ;; MS Visual C++ 6.0
5323b4b0
RS
972 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\)) \: \\(\\(error\\|warning\\|fatal error\\) \\(C[0-9]+\\):[ \t\n]*\\(.+\\)\\)"
973 1 3 nil 4)
9a686ad2 974 ;; jikes
5323b4b0
RS
975 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)\:\\([0-9]+\\)\:[0-9]+\:[0-9]+\:[0-9]+\: \\(\\(Error\\|Warning\\|Caution\\|Semantic Error\\):[ \t\n]*\\(.+\\)\\)"
976 1 3 nil 4)
9a686ad2 977 ;; MS midl
5323b4b0
RS
978 ("midl[ ]*:[ ]*\\(command line error .*\\)"
979 nil nil nil 1)
9a686ad2 980 ;; MS C#
5323b4b0
RS
981 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\),[0-9]+)\: \\(\\(error\\|warning\\|fatal error\\) \\(CS[0-9]+\\):[ \t\n]*\\(.+\\)\\)"
982 1 3 nil 4)
9a686ad2 983 ;; perl
5323b4b0 984 ("\\(.*\\) at \\([^ \n]+\\) line \\([0-9]+\\)[,.\n]" 2 3 nil 1)
9a686ad2
SM
985 ;; LaTeX warnings (fileless) ("\\(LaTeX \\(Warning\\|Error\\): .*\\) on input line \\([0-9]+\\)" 20 3 nil 1)
986 ;; ant/javac
5323b4b0
RS
987 (" *\\(\\[javac\\]\\)? *\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)\:\\([0-9]+\\)\:[ \t\n]*\\(.+\\)"
988 2 4 nil 5))
989 ;; compilation-error-regexp-alist)
ce721de4 990 (flymake-reformat-err-line-patterns-from-compile-el compilation-error-regexp-alist-alist))
9a686ad2
SM
991 "Patterns for matching error/warning lines.
992\(REGEXP FILE-IDX LINE-IDX ERR-TEXT-IDX).
993Use `flymake-reformat-err-line-patterns-from-compile-el' to add patterns
994from compile.el")
995
996;;(defcustom flymake-err-line-patterns
997;; '(
998;; ; MS Visual C++ 6.0
999;; ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\)) \: \\(\\(error\\|warning\\|fatal error\\) \\(C[0-9]+\\):[ \t\n]*\\(.+\\)\\)"
1000;; 1 3 4)
1001;; ; jikes
1002;; ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)\:\\([0-9]+\\)\:[0-9]+\:[0-9]+\:[0-9]+\: \\(\\(Error\\|Warning\\|Caution\\):[ \t\n]*\\(.+\\)\\)"
1003;; 1 3 4))
1004;; "patterns for matching error/warning lines, (regexp file-idx line-idx err-text-idx)"
1005;; :group 'flymake
1006;; :type '(repeat (string number number number))
1007;;)
4bcbcb9d 1008
2aa13ec4 1009(defun flymake-parse-line (line)
173569aa
JB
1010 "Parse LINE to see if it is an error or warning.
1011Return its components if so, nil otherwise."
5323b4b0
RS
1012 (let ((raw-file-name nil)
1013 (line-no 0)
1014 (err-type "e")
1015 (err-text nil)
17404091 1016 (patterns flymake-err-line-patterns)
5323b4b0 1017 (matched nil))
17404091
SM
1018 (while (and patterns (not matched))
1019 (when (string-match (car (car patterns)) line)
1020 (let* ((file-idx (nth 1 (car patterns)))
1021 (line-idx (nth 2 (car patterns))))
5323b4b0
RS
1022
1023 (setq raw-file-name (if file-idx (match-string file-idx line) nil))
027a4b6b 1024 (setq line-no (if line-idx (string-to-number (match-string line-idx line)) 0))
17404091
SM
1025 (setq err-text (if (> (length (car patterns)) 4)
1026 (match-string (nth 4 (car patterns)) line)
5323b4b0
RS
1027 (flymake-patch-err-text (substring line (match-end 0)))))
1028 (or err-text (setq err-text "<no error text>"))
1029 (if (and err-text (string-match "^[wW]arning" err-text))
1030 (setq err-type "w")
1031 )
1032 (flymake-log 3 "parse line: file-idx=%s line-idx=%s file=%s line=%s text=%s" file-idx line-idx
1033 raw-file-name line-no err-text)
1034 (setq matched t)))
17404091 1035 (setq patterns (cdr patterns)))
5323b4b0
RS
1036 (if matched
1037 (flymake-ler-make-ler raw-file-name line-no err-type err-text)
1038 ())))
4bcbcb9d 1039
2aa13ec4 1040(defun flymake-find-err-info (err-info-list line-no)
5323b4b0
RS
1041 "Find (line-err-info-list pos) for specified LINE-NO."
1042 (if err-info-list
1043 (let* ((line-err-info-list nil)
1044 (pos 0)
1045 (count (length err-info-list)))
1046
1047 (while (and (< pos count) (< (car (nth pos err-info-list)) line-no))
1048 (setq pos (1+ pos)))
1049 (when (and (< pos count) (equal (car (nth pos err-info-list)) line-no))
1050 (setq line-err-info-list (flymake-er-get-line-err-info-list (nth pos err-info-list))))
1051 (list line-err-info-list pos))
1052 '(nil 0)))
4bcbcb9d 1053
2aa13ec4 1054(defun flymake-line-err-info-is-less-or-equal (line-one line-two)
5323b4b0
RS
1055 (or (string< (flymake-ler-get-type line-one) (flymake-ler-get-type line-two))
1056 (and (string= (flymake-ler-get-type line-one) (flymake-ler-get-type line-two))
1057 (not (flymake-ler-get-file line-one)) (flymake-ler-get-file line-two))
1058 (and (string= (flymake-ler-get-type line-one) (flymake-ler-get-type line-two))
1059 (or (and (flymake-ler-get-file line-one) (flymake-ler-get-file line-two))
1060 (and (not (flymake-ler-get-file line-one)) (not (flymake-ler-get-file line-two)))))))
4bcbcb9d 1061
2aa13ec4 1062(defun flymake-add-line-err-info (line-err-info-list line-err-info)
53ec26ed
RS
1063 "Update LINE-ERR-INFO-LIST with the error LINE-ERR-INFO.
1064For the format of LINE-ERR-INFO, see `flymake-ler-make-ler'.
1065The new element is inserted in the proper position, according to
1066the predicate `flymake-line-err-info-is-less-or-equal'.
1067The updated value of LINE-ERR-INFO-LIST is returned."
5323b4b0
RS
1068 (if (not line-err-info-list)
1069 (list line-err-info)
1070 (let* ((count (length line-err-info-list))
1071 (idx 0))
1072 (while (and (< idx count) (flymake-line-err-info-is-less-or-equal (nth idx line-err-info-list) line-err-info))
1073 (setq idx (1+ idx)))
1074 (cond ((equal 0 idx) (setq line-err-info-list (cons line-err-info line-err-info-list)))
1075 (t (setq line-err-info-list (flymake-ins-after line-err-info-list (1- idx) line-err-info))))
1076 line-err-info-list)))
4bcbcb9d 1077
2aa13ec4 1078(defun flymake-add-err-info (err-info-list line-err-info)
53ec26ed
RS
1079 "Update ERR-INFO-LIST with the error LINE-ERR-INFO, preserving sort order.
1080Returns the updated value of ERR-INFO-LIST.
1081For the format of ERR-INFO-LIST, see `flymake-err-info'.
1082For the format of LINE-ERR-INFO, see `flymake-ler-make-ler'."
4bd0a5d0 1083 (let* ((line-no (if (flymake-ler-get-file line-err-info) 1 (flymake-ler-get-line line-err-info)))
5323b4b0
RS
1084 (info-and-pos (flymake-find-err-info err-info-list line-no))
1085 (exists (car info-and-pos))
1086 (pos (nth 1 info-and-pos))
1087 (line-err-info-list nil)
1088 (err-info nil))
1089
1090 (if exists
1091 (setq line-err-info-list (flymake-er-get-line-err-info-list (car (nthcdr pos err-info-list)))))
1092 (setq line-err-info-list (flymake-add-line-err-info line-err-info-list line-err-info))
1093
1094 (setq err-info (flymake-er-make-er line-no line-err-info-list))
1095 (cond (exists (setq err-info-list (flymake-set-at err-info-list pos err-info)))
1096 ((equal 0 pos) (setq err-info-list (cons err-info err-info-list)))
1097 (t (setq err-info-list (flymake-ins-after err-info-list (1- pos) err-info))))
1098 err-info-list))
4bcbcb9d 1099
2aa13ec4 1100(defun flymake-get-project-include-dirs-imp (basedir)
5323b4b0
RS
1101 "Include dirs for the project current file belongs to."
1102 (if (flymake-get-project-include-dirs-from-cache basedir)
1103 (progn
1104 (flymake-get-project-include-dirs-from-cache basedir))
9a686ad2 1105 ;;else
5323b4b0
RS
1106 (let* ((command-line (concat "make -C\"" basedir "\" DUMPVARS=INCLUDE_DIRS dumpvars"))
1107 (output (shell-command-to-string command-line))
1108 (lines (flymake-split-string output "\n"))
1109 (count (length lines))
1110 (idx 0)
1111 (inc-dirs nil))
1112 (while (and (< idx count) (not (string-match "^INCLUDE_DIRS=.*" (nth idx lines))))
1113 (setq idx (1+ idx)))
1114 (when (< idx count)
1115 (let* ((inc-lines (flymake-split-string (nth idx lines) " *-I"))
1116 (inc-count (length inc-lines)))
1117 (while (> inc-count 0)
1118 (when (not (string-match "^INCLUDE_DIRS=.*" (nth (1- inc-count) inc-lines)))
1119 (setq inc-dirs (cons (flymake-replace-regexp-in-string "\"" "" (nth (1- inc-count) inc-lines)) inc-dirs)))
1120 (setq inc-count (1- inc-count)))))
1121 (flymake-add-project-include-dirs-to-cache basedir inc-dirs)
1122 inc-dirs)))
4bcbcb9d
EZ
1123
1124(defcustom flymake-get-project-include-dirs-function 'flymake-get-project-include-dirs-imp
173569aa 1125 "Function used to get project include dirs, one parameter: basedir name."
5323b4b0
RS
1126 :group 'flymake
1127 :type 'function)
4bcbcb9d 1128
2aa13ec4 1129(defun flymake-get-project-include-dirs (basedir)
5323b4b0 1130 (funcall flymake-get-project-include-dirs-function basedir))
4bcbcb9d 1131
2aa13ec4 1132(defun flymake-get-system-include-dirs ()
5323b4b0
RS
1133 "System include dirs - from the 'INCLUDE' env setting."
1134 (let* ((includes (getenv "INCLUDE")))
1135 (if includes (flymake-split-string includes path-separator) nil)))
4bcbcb9d
EZ
1136
1137(defvar flymake-project-include-dirs-cache (flymake-makehash 'equal))
4bcbcb9d 1138
2aa13ec4 1139(defun flymake-get-project-include-dirs-from-cache (base-dir)
5323b4b0 1140 (gethash base-dir flymake-project-include-dirs-cache))
2aa13ec4
RS
1141
1142(defun flymake-add-project-include-dirs-to-cache (base-dir include-dirs)
5323b4b0 1143 (puthash base-dir include-dirs flymake-project-include-dirs-cache))
2aa13ec4
RS
1144
1145(defun flymake-clear-project-include-dirs-cache ()
5323b4b0 1146 (clrhash flymake-project-include-dirs-cache))
2aa13ec4
RS
1147
1148(defun flymake-get-include-dirs (base-dir)
5323b4b0
RS
1149 "Get dirs to use when resolving local file names."
1150 (let* ((include-dirs (append '(".") (flymake-get-project-include-dirs base-dir) (flymake-get-system-include-dirs))))
1151 include-dirs))
4bcbcb9d 1152
2aa13ec4 1153(defun flymake-restore-formatting (source-buffer)
5323b4b0
RS
1154 "Remove any formatting made by flymake."
1155 )
4bcbcb9d 1156
2aa13ec4 1157(defun flymake-get-program-dir (buffer)
5323b4b0
RS
1158 "Get dir to start program in."
1159 (unless (bufferp buffer)
cd2325cd
SM
1160 (error "Invalid buffer"))
1161 (with-current-buffer buffer
5323b4b0 1162 default-directory))
4bcbcb9d 1163
2aa13ec4 1164(defun flymake-safe-delete-file (file-name)
5323b4b0
RS
1165 (when (and file-name (file-exists-p file-name))
1166 (delete-file file-name)
1167 (flymake-log 1 "deleted file %s" file-name)))
4bcbcb9d 1168
2aa13ec4 1169(defun flymake-safe-delete-directory (dir-name)
5323b4b0
RS
1170 (condition-case err
1171 (progn
1172 (delete-directory dir-name)
1173 (flymake-log 1 "deleted dir %s" dir-name))
1174 (error
1175 (flymake-log 1 "Failed to delete dir %s, error ignored" dir-name))))
4bcbcb9d 1176
ce721de4 1177(defcustom flymake-compilation-prevents-syntax-check t
5323b4b0
RS
1178 "If non-nil, syntax check won't be started in case compilation is running."
1179 :group 'flymake
1180 :type 'boolean)
4bcbcb9d 1181
2aa13ec4 1182(defun flymake-start-syntax-check (buffer)
5323b4b0
RS
1183 "Start syntax checking for buffer BUFFER."
1184 (unless (bufferp buffer)
1185 (error "Expected a buffer"))
cd2325cd 1186 (with-current-buffer buffer
53ec26ed
RS
1187 (flymake-log 3 "flymake is running: %s" flymake-is-running)
1188 (when (and (not flymake-is-running)
5323b4b0
RS
1189 (flymake-can-syntax-check-file (buffer-file-name buffer)))
1190 (when (or (not flymake-compilation-prevents-syntax-check)
1191 (not (flymake-compilation-is-running))) ;+ (flymake-rep-ort-status buffer "COMP")
1192 (flymake-clear-buildfile-cache)
1193 (flymake-clear-project-include-dirs-cache)
1194
53ec26ed
RS
1195 (setq flymake-check-was-interrupted nil)
1196 (setq flymake-buffer-data (flymake-makehash 'equal))
5323b4b0
RS
1197
1198 (let* ((source-file-name (buffer-file-name buffer))
1199 (init-f (flymake-get-init-function source-file-name))
1200 (cleanup-f (flymake-get-cleanup-function source-file-name))
1201 (cmd-and-args (funcall init-f buffer))
1202 (cmd (nth 0 cmd-and-args))
1203 (args (nth 1 cmd-and-args))
1204 (dir (nth 2 cmd-and-args)))
1205 (if (not cmd-and-args)
1206 (progn
1207 (flymake-log 0 "init function %s for %s failed, cleaning up" init-f source-file-name)
1208 (funcall cleanup-f buffer))
1209 (progn
53ec26ed 1210 (setq flymake-last-change-time nil)
5323b4b0 1211 (flymake-start-syntax-check-process buffer cmd args dir))))))))
4bcbcb9d 1212
2aa13ec4 1213(defun flymake-start-syntax-check-process (buffer cmd args dir)
5323b4b0
RS
1214 "Start syntax check process."
1215 (let* ((process nil))
1216 (condition-case err
1217 (progn
1218 (when dir
1219 (let ((default-directory dir))
1220 (flymake-log 3 "starting process on dir %s" default-directory)))
1221 (setq process (get-process (apply 'start-process "flymake-proc" nil cmd args)))
1222 (set-process-sentinel process 'flymake-process-sentinel)
1223 (set-process-filter process 'flymake-process-filter)
1224
1225 (flymake-reg-names (process-id process) (buffer-name buffer))
1226
53ec26ed
RS
1227 (with-current-buffer buffer
1228 (setq flymake-is-running t)
1229 (setq flymake-last-change-time nil)
1230 (setq flymake-check-start-time (flymake-float-time)))
5323b4b0
RS
1231
1232 (flymake-report-status buffer nil "*")
1233 (flymake-log 2 "started process %d, command=%s, dir=%s"
1234 (process-id process) (process-command process) default-directory)
1235 process)
1236 (error
1237 (let* ((err-str (format "Failed to launch syntax check process '%s' with args %s: %s"
1238 cmd args (error-message-string err)))
1239 (source-file-name (buffer-file-name buffer))
1240 (cleanup-f (flymake-get-cleanup-function source-file-name)))
1241 (flymake-log 0 err-str)
1242 (funcall cleanup-f buffer)
1243 (flymake-report-fatal-status buffer "PROCERR" err-str))))))
4bcbcb9d 1244
2aa13ec4 1245(defun flymake-kill-process (pid &optional rest)
5323b4b0
RS
1246 "Kill process PID."
1247 (signal-process pid 9)
1248 (let* ((buffer-name (flymake-get-source-buffer-name pid)))
1249 (when (and buffer-name (get-buffer buffer-name))
53ec26ed
RS
1250 (with-current-buffer (get-buffer buffer-name)
1251 (setq flymake-check-was-interrupted t))))
5323b4b0 1252 (flymake-log 1 "killed process %d" pid))
4bcbcb9d 1253
2aa13ec4 1254(defun flymake-stop-all-syntax-checks ()
5323b4b0
RS
1255 "Kill all syntax check processes."
1256 (interactive)
1257 (let ((pids (copy-hash-table flymake-pid-to-names)))
1258 (maphash 'flymake-kill-process pids)))
4bcbcb9d 1259
2aa13ec4 1260(defun flymake-compilation-is-running ()
5323b4b0
RS
1261 (and (boundp 'compilation-in-progress)
1262 compilation-in-progress))
4bcbcb9d 1263
2aa13ec4 1264(defun flymake-compile ()
5323b4b0
RS
1265 "Kill all flymake syntax checks, start compilation."
1266 (interactive)
1267 (flymake-stop-all-syntax-checks)
1268 (call-interactively 'compile))
4bcbcb9d
EZ
1269
1270(defvar flymake-is-running nil
9a686ad2 1271 "If t, flymake syntax check process is running for the current buffer.")
2aa13ec4 1272
4bcbcb9d 1273(make-variable-buffer-local 'flymake-is-running)
2aa13ec4 1274
4bcbcb9d 1275(defvar flymake-timer nil
2aa13ec4
RS
1276 "Timer for starting syntax check.")
1277
4bcbcb9d 1278(make-variable-buffer-local 'flymake-timer)
2aa13ec4 1279
4bcbcb9d 1280(defvar flymake-last-change-time nil
2aa13ec4
RS
1281 "Time of last buffer change.")
1282
4bcbcb9d 1283(make-variable-buffer-local 'flymake-last-change-time)
2aa13ec4 1284
4bcbcb9d 1285(defvar flymake-check-start-time nil
2aa13ec4
RS
1286 "Time at which syntax check was started.")
1287
4bcbcb9d 1288(make-variable-buffer-local 'flymake-check-start-time)
2aa13ec4 1289
4bcbcb9d 1290(defvar flymake-check-was-interrupted nil
9a686ad2 1291 "Non-nil if syntax check was killed by `flymake-compile'.")
2aa13ec4 1292
4bcbcb9d 1293(make-variable-buffer-local 'flymake-check-was-interrupted)
2aa13ec4 1294
4bcbcb9d 1295(defcustom flymake-no-changes-timeout 0.5
5323b4b0
RS
1296 "Time to wait after last change before starting compilation."
1297 :group 'flymake
1298 :type 'number)
4bcbcb9d 1299
2aa13ec4 1300(defun flymake-on-timer-event (buffer)
5323b4b0 1301 "Start a syntax check for buffer BUFFER if necessary."
53ec26ed 1302 (when (bufferp buffer)
cd2325cd 1303 (with-current-buffer buffer
53ec26ed
RS
1304 (when (and (not flymake-is-running)
1305 flymake-last-change-time
1306 (> (flymake-float-time) (+ flymake-no-changes-timeout flymake-last-change-time)))
1307
1308 (setq flymake-last-change-time nil)
5323b4b0
RS
1309 (flymake-log 3 "starting syntax check as more than 1 second passed since last change")
1310 (flymake-start-syntax-check buffer)))))
4bcbcb9d 1311
2aa13ec4 1312(defun flymake-start-syntax-check-for-current-buffer ()
69df8d97 1313 "Run `flymake-start-syntax-check' for current buffer if it isn't already running."
5323b4b0
RS
1314 (interactive)
1315 (flymake-start-syntax-check (current-buffer)))
4bcbcb9d 1316
2aa13ec4 1317(defun flymake-current-line-no ()
5323b4b0
RS
1318 "Return number of current line in current buffer."
1319 (interactive)
1320 (let ((beg (point-min))
1321 (end (if (= (point) (point-max)) (point) (1+ (point)))))
1322 (count-lines beg end)))
4bcbcb9d 1323
2aa13ec4 1324(defun flymake-count-lines (buffer)
5323b4b0 1325 "Return number of lines in buffer BUFFER."
cd2325cd 1326 (with-current-buffer buffer
5323b4b0 1327 (count-lines (point-min) (point-max))))
4bcbcb9d 1328
2aa13ec4 1329(defun flymake-get-point-pixel-pos ()
5323b4b0
RS
1330 "Return point position in pixels: (x, y)."
1331 (let ((mouse-pos (mouse-position))
1332 (pixel-pos nil)
1333 (ret nil))
1334 (if (car (cdr mouse-pos))
1335 (progn
1336 (set-mouse-position (flymake-selected-frame) (current-column) (flymake-current-row))
1337 (setq pixel-pos (mouse-pixel-position))
1338 (set-mouse-position (car mouse-pos) (car (cdr mouse-pos)) (cdr (cdr mouse-pos)))
1339 (setq ret (list (car (cdr pixel-pos)) (cdr (cdr pixel-pos)))))
1340 (progn
1341 (setq ret '(0 0))))
1342 (flymake-log 3 "mouse pos is %s" ret)
1343 ret))
4bcbcb9d 1344
2aa13ec4 1345(defun flymake-display-err-menu-for-current-line ()
5323b4b0
RS
1346 "Display a menu with errors/warnings for current line if it has errors and/or warnings."
1347 (interactive)
1348 (let* ((line-no (flymake-current-line-no))
53ec26ed 1349 (line-err-info-list (nth 0 (flymake-find-err-info flymake-err-info line-no)))
5323b4b0
RS
1350 (menu-data (flymake-make-err-menu-data line-no line-err-info-list))
1351 (choice nil)
1352 (mouse-pos (flymake-get-point-pixel-pos))
5323b4b0
RS
1353 (menu-pos (list (flymake-get-point-pixel-pos) (selected-window))))
1354 (if menu-data
1355 (progn
1356 (setq choice (flymake-popup-menu menu-pos menu-data))
1357 (flymake-log 3 "choice=%s" choice)
1358 (when choice
1359 (eval choice)))
1360 (flymake-log 1 "no errors for line %d" line-no))))
4bcbcb9d 1361
2aa13ec4 1362(defun flymake-make-err-menu-data (line-no line-err-info-list)
173569aa 1363 "Make a (menu-title (item-title item-action)*) list with errors/warnings from LINE-ERR-INFO-LIST."
5323b4b0
RS
1364 (let* ((menu-items nil))
1365 (when line-err-info-list
1366 (let* ((count (length line-err-info-list))
1367 (menu-item-text nil))
1368 (while (> count 0)
1369 (setq menu-item-text (flymake-ler-get-text (nth (1- count) line-err-info-list)))
1370 (let* ((file (flymake-ler-get-file (nth (1- count) line-err-info-list)))
1371 (full-file (flymake-ler-get-full-file (nth (1- count) line-err-info-list)))
1372 (line (flymake-ler-get-line (nth (1- count) line-err-info-list))))
1373 (if file
1374 (setq menu-item-text (concat menu-item-text " - " file "(" (format "%d" line) ")")))
1375 (setq menu-items (cons (list menu-item-text
1376 (if file (list 'flymake-goto-file-and-line full-file line) nil))
1377 menu-items)))
1378 (setq count (1- count)))
1379 (flymake-log 3 "created menu-items with %d item(s)" (length menu-items))))
1380 (if menu-items
1381 (let* ((menu-title (format "Line %d: %d error(s), %d warning(s)" line-no
1382 (flymake-get-line-err-count line-err-info-list "e")
1383 (flymake-get-line-err-count line-err-info-list "w"))))
1384 (list menu-title menu-items))
1385 nil)))
4bcbcb9d 1386
2aa13ec4 1387(defun flymake-goto-file-and-line (file line)
9a686ad2 1388 "Try to get buffer for FILE and goto line LINE in it."
5323b4b0
RS
1389 (if (not (file-exists-p file))
1390 (flymake-log 1 "file %s does not exists" file)
1391 (progn
1392 (find-file file)
1393 (goto-line line))))
4bcbcb9d 1394
2aa13ec4 1395;; flymake minor mode declarations
4bd0a5d0 1396(defvar flymake-mode-line nil)
2aa13ec4 1397
4bcbcb9d 1398(make-variable-buffer-local 'flymake-mode-line)
2aa13ec4 1399
4bcbcb9d 1400(defvar flymake-mode-line-e-w nil)
2aa13ec4 1401
4bcbcb9d 1402(make-variable-buffer-local 'flymake-mode-line-e-w)
2aa13ec4 1403
4bcbcb9d 1404(defvar flymake-mode-line-status nil)
2aa13ec4 1405
4bcbcb9d 1406(make-variable-buffer-local 'flymake-mode-line-status)
4bcbcb9d 1407
2aa13ec4 1408(defun flymake-report-status (buffer e-w &optional status)
5323b4b0
RS
1409 "Show status in mode line."
1410 (when (bufferp buffer)
cd2325cd 1411 (with-current-buffer buffer
5323b4b0 1412 (when e-w
53ec26ed 1413 (setq flymake-mode-line-e-w e-w))
5323b4b0 1414 (when status
53ec26ed 1415 (setq flymake-mode-line-status status))
5323b4b0 1416 (let* ((mode-line " Flymake"))
53ec26ed
RS
1417 (when (> (length flymake-mode-line-e-w) 0)
1418 (setq mode-line (concat mode-line ":" flymake-mode-line-e-w)))
1419 (setq mode-line (concat mode-line flymake-mode-line-status))
1420 (setq flymake-mode-line mode-line)
5323b4b0 1421 (force-mode-line-update)))))
4bcbcb9d 1422
2aa13ec4 1423(defun flymake-display-warning (warning)
5323b4b0
RS
1424 "Display a warning to user."
1425 (message-box warning))
4bcbcb9d
EZ
1426
1427(defcustom flymake-gui-warnings-enabled t
173569aa 1428 "Enables/disables GUI warnings."
5323b4b0
RS
1429 :group 'flymake
1430 :type 'boolean)
4bcbcb9d 1431
2aa13ec4 1432(defun flymake-report-fatal-status (buffer status warning)
5323b4b0
RS
1433 "Display a warning and switch flymake mode off."
1434 (when flymake-gui-warnings-enabled
1435 (flymake-display-warning (format "Flymake: %s. Flymake will be switched OFF" warning))
1436 )
cd2325cd 1437 (with-current-buffer buffer
5323b4b0
RS
1438 (flymake-mode 0)
1439 (flymake-log 0 "switched OFF Flymake mode for buffer %s due to fatal status %s, warning %s"
1440 (buffer-name buffer) status warning)))
4bcbcb9d 1441
d57f6b5e
SM
1442(defcustom flymake-start-syntax-check-on-find-file t
1443 "Start syntax check on find file."
1444 :group 'flymake
1445 :type 'boolean)
1446
5ffc943b 1447;;;###autoload
4bd0a5d0
SM
1448(define-minor-mode flymake-mode
1449 "Minor mode to do on-the-fly syntax checking.
1450When called interactively, toggles the minor mode.
1451With arg, turn Flymake mode on if and only if arg is positive."
d799c278 1452 :group 'flymake :lighter flymake-mode-line
d57f6b5e
SM
1453 (cond
1454
1455 ;; Turning the mode ON.
1456 (flymake-mode
1457 (if (not (flymake-can-syntax-check-file buffer-file-name))
1458 (flymake-log 2 "flymake cannot check syntax in buffer %s" (buffer-name))
1459 (add-hook 'after-change-functions 'flymake-after-change-function nil t)
1460 (add-hook 'after-save-hook 'flymake-after-save-hook nil t)
1461 (add-hook 'kill-buffer-hook 'flymake-kill-buffer-hook nil t)
1462 ;;+(add-hook 'find-file-hook 'flymake-find-file-hook)
1463
1464 (flymake-report-status (current-buffer) "" "")
c92d6023 1465
d57f6b5e
SM
1466 (setq flymake-timer
1467 (run-at-time nil 1 'flymake-on-timer-event (current-buffer)))
1468
1469 (when flymake-start-syntax-check-on-find-file
1470 (flymake-start-syntax-check-for-current-buffer))))
1471
1472 ;; Turning the mode OFF.
1473 (t
1474 (remove-hook 'after-change-functions 'flymake-after-change-function t)
1475 (remove-hook 'after-save-hook 'flymake-after-save-hook t)
1476 (remove-hook 'kill-buffer-hook 'flymake-kill-buffer-hook t)
1477 ;;+(remove-hook 'find-file-hook (function flymake-find-file-hook) t)
2aa13ec4 1478
d57f6b5e
SM
1479 (flymake-delete-own-overlays (current-buffer))
1480
1481 (when flymake-timer
1482 (cancel-timer flymake-timer)
1483 (setq flymake-timer nil))
1484
bd6a83d5 1485 (setq flymake-is-running nil))))
4bcbcb9d 1486
4bcbcb9d 1487;;;###autoload
2aa13ec4 1488(defun flymake-mode-on ()
5323b4b0 1489 "Turn flymake mode on."
d57f6b5e
SM
1490 (flymake-mode 1)
1491 (flymake-log 1 "flymake mode turned ON for buffer %s" (buffer-name)))
4bcbcb9d
EZ
1492
1493;;;###autoload
2aa13ec4 1494(defun flymake-mode-off ()
5323b4b0 1495 "Turn flymake mode off."
d57f6b5e 1496 (flymake-mode 0)
bd6a83d5 1497 (flymake-log 1 "flymake mode turned OFF for buffer %s" (buffer-name)))
4bcbcb9d
EZ
1498
1499(defcustom flymake-start-syntax-check-on-newline t
5323b4b0
RS
1500 "Start syntax check if newline char was added/removed from the buffer."
1501 :group 'flymake
1502 :type 'boolean)
4bcbcb9d 1503
2aa13ec4 1504(defun flymake-after-change-function (start stop len)
9a686ad2
SM
1505 "Start syntax check for current buffer if it isn't already running."
1506 ;;+(flymake-log 0 "setting change time to %s" (flymake-float-time))
5323b4b0
RS
1507 (let((new-text (buffer-substring start stop)))
1508 (when (and flymake-start-syntax-check-on-newline (equal new-text "\n"))
1509 (flymake-log 3 "starting syntax check as new-line has been seen")
1510 (flymake-start-syntax-check-for-current-buffer))
53ec26ed 1511 (setq flymake-last-change-time (flymake-float-time))))
4bcbcb9d 1512
2aa13ec4 1513(defun flymake-after-save-hook ()
5323b4b0
RS
1514 (if (local-variable-p 'flymake-mode (current-buffer)) ; (???) other way to determine whether flymake is active in buffer being saved?
1515 (progn
1516 (flymake-log 3 "starting syntax check as buffer was saved")
1517 (flymake-start-syntax-check-for-current-buffer)))) ; no more mode 3. cannot start check if mode 3 (to temp copies) is active - (???)
4bcbcb9d 1518
2aa13ec4 1519(defun flymake-kill-buffer-hook ()
53ec26ed
RS
1520 (when flymake-timer
1521 (cancel-timer flymake-timer)
1522 (setq flymake-timer nil)))
4bcbcb9d 1523
2aa13ec4 1524(defun flymake-find-file-hook ()
9a686ad2
SM
1525 ;;+(when flymake-start-syntax-check-on-find-file
1526 ;;+ (flymake-log 3 "starting syntax check on file open")
1527 ;;+ (flymake-start-syntax-check-for-current-buffer)
1528 ;;+)
5323b4b0 1529 (when (and (not (local-variable-p 'flymake-mode (current-buffer)))
d57f6b5e 1530 (flymake-can-syntax-check-file buffer-file-name))
5323b4b0
RS
1531 (flymake-mode)
1532 (flymake-log 3 "automatically turned ON flymake mode")))
4bcbcb9d 1533
2aa13ec4 1534(defun flymake-get-first-err-line-no (err-info-list)
5323b4b0
RS
1535 "Return first line with error."
1536 (when err-info-list
1537 (flymake-er-get-line (car err-info-list))))
4bcbcb9d 1538
2aa13ec4 1539(defun flymake-get-last-err-line-no (err-info-list)
5323b4b0
RS
1540 "Return last line with error."
1541 (when err-info-list
1542 (flymake-er-get-line (nth (1- (length err-info-list)) err-info-list))))
4bcbcb9d 1543
2aa13ec4 1544(defun flymake-get-next-err-line-no (err-info-list line-no)
5323b4b0
RS
1545 "Return next line with error."
1546 (when err-info-list
1547 (let* ((count (length err-info-list))
1548 (idx 0))
1549 (while (and (< idx count) (>= line-no (flymake-er-get-line (nth idx err-info-list))))
1550 (setq idx (1+ idx)))
1551 (if (< idx count)
1552 (flymake-er-get-line (nth idx err-info-list))))))
4bcbcb9d 1553
2aa13ec4 1554(defun flymake-get-prev-err-line-no (err-info-list line-no)
173569aa 1555 "Return previous line with error."
5323b4b0
RS
1556 (when err-info-list
1557 (let* ((count (length err-info-list)))
1558 (while (and (> count 0) (<= line-no (flymake-er-get-line (nth (1- count) err-info-list))))
1559 (setq count (1- count)))
1560 (if (> count 0)
1561 (flymake-er-get-line (nth (1- count) err-info-list))))))
4bcbcb9d 1562
2aa13ec4 1563(defun flymake-skip-whitespace ()
5323b4b0
RS
1564 "Move forward until non-whitespace is reached."
1565 (while (looking-at "[ \t]")
1566 (forward-char)))
4bcbcb9d 1567
2aa13ec4 1568(defun flymake-goto-line (line-no)
9a686ad2 1569 "Go to line LINE-NO, then skip whitespace."
5323b4b0
RS
1570 (goto-line line-no)
1571 (flymake-skip-whitespace))
4bcbcb9d 1572
2aa13ec4 1573(defun flymake-goto-next-error ()
9a686ad2 1574 "Go to next error in err ring."
5323b4b0 1575 (interactive)
53ec26ed 1576 (let ((line-no (flymake-get-next-err-line-no flymake-err-info (flymake-current-line-no))))
5323b4b0 1577 (when (not line-no)
53ec26ed 1578 (setq line-no (flymake-get-first-err-line-no flymake-err-info))
5323b4b0
RS
1579 (flymake-log 1 "passed end of file"))
1580 (if line-no
1581 (flymake-goto-line line-no)
1582 (flymake-log 1 "no errors in current buffer"))))
4bcbcb9d 1583
2aa13ec4 1584(defun flymake-goto-prev-error ()
173569aa 1585 "Go to previous error in err ring."
5323b4b0 1586 (interactive)
53ec26ed 1587 (let ((line-no (flymake-get-prev-err-line-no flymake-err-info (flymake-current-line-no))))
5323b4b0 1588 (when (not line-no)
53ec26ed 1589 (setq line-no (flymake-get-last-err-line-no flymake-err-info))
5323b4b0
RS
1590 (flymake-log 1 "passed beginning of file"))
1591 (if line-no
1592 (flymake-goto-line line-no)
1593 (flymake-log 1 "no errors in current buffer"))))
4bcbcb9d 1594
2aa13ec4 1595(defun flymake-patch-err-text (string)
5323b4b0
RS
1596 (if (string-match "^[\n\t :0-9]*\\(.*\\)$" string)
1597 (match-string 1 string)
1598 string))
4bcbcb9d
EZ
1599
1600;;;; general init-cleanup and helper routines
2aa13ec4 1601(defun flymake-create-temp-inplace (file-name prefix)
5323b4b0
RS
1602 (unless (stringp file-name)
1603 (error "Invalid file-name"))
1604 (or prefix
1605 (setq prefix "flymake"))
1606 (let* ((temp-name (concat (file-name-sans-extension file-name)
1607 "_" prefix
1608 (and (file-name-extension file-name)
1609 (concat "." (file-name-extension file-name))))))
1610 (flymake-log 3 "create-temp-inplace: file=%s temp=%s" file-name temp-name)
1611 temp-name))
4bcbcb9d 1612
2aa13ec4 1613(defun flymake-create-temp-with-folder-structure (file-name prefix)
5323b4b0
RS
1614 (unless (stringp file-name)
1615 (error "Invalid file-name"))
4bcbcb9d 1616
5323b4b0
RS
1617 (let* ((dir (file-name-directory file-name))
1618 (slash-pos (string-match "/" dir))
4bd0a5d0 1619 (temp-dir (concat (file-name-as-directory (flymake-get-temp-dir)) (substring dir (1+ slash-pos)))))
4bcbcb9d 1620
4bd0a5d0 1621 (file-truename (concat (file-name-as-directory temp-dir)
5323b4b0 1622 (file-name-nondirectory file-name)))))
4bcbcb9d 1623
2aa13ec4 1624(defun flymake-strrchr (str ch)
5323b4b0
RS
1625 (let* ((count (length str))
1626 (pos nil))
1627 (while (and (not pos) (> count 0))
1628 (if (= ch (elt str (1- count)))
1629 (setq pos (1- count)))
1630 (setq count (1- count)))
1631 pos))
4bcbcb9d 1632
2aa13ec4 1633(defun flymake-delete-temp-directory (dir-name)
9a686ad2 1634 "Attempt to delete temp dir created by `flymake-create-temp-with-folder-structure', do not fail on error."
5323b4b0
RS
1635 (let* ((temp-dir (flymake-get-temp-dir))
1636 (suffix (substring dir-name (1+ (length temp-dir))))
1637 (slash-pos nil))
1638
1639 (while (> (length suffix) 0)
9a686ad2 1640 ;;+(flymake-log 0 "suffix=%s" suffix)
4bd0a5d0 1641 (flymake-safe-delete-directory (file-truename (concat (file-name-as-directory temp-dir) suffix)))
5323b4b0
RS
1642 (setq slash-pos (flymake-strrchr suffix (string-to-char "/")))
1643 (if slash-pos
1644 (setq suffix (substring suffix 0 slash-pos))
1645 (setq suffix "")))))
4bcbcb9d 1646
2aa13ec4 1647(defun flymake-init-create-temp-buffer-copy (buffer create-temp-f)
5323b4b0
RS
1648 "Make a temporary copy of the current buffer, save its name in buffer data and return the name."
1649 (let* ((source-file-name (buffer-file-name buffer))
1650 (temp-source-file-name (funcall create-temp-f source-file-name "flymake")))
4bcbcb9d 1651
5323b4b0
RS
1652 (flymake-save-buffer-in-file buffer temp-source-file-name)
1653 (flymake-set-buffer-value buffer "temp-source-file-name" temp-source-file-name)
1654 temp-source-file-name))
4bcbcb9d 1655
2aa13ec4 1656(defun flymake-simple-cleanup (buffer)
69df8d97 1657 "Do cleanup after `flymake-init-create-temp-buffer-copy'.
2aa13ec4 1658Delete temp file."
5323b4b0
RS
1659 (let* ((temp-source-file-name (flymake-get-buffer-value buffer "temp-source-file-name")))
1660 (flymake-safe-delete-file temp-source-file-name)
53ec26ed
RS
1661 (with-current-buffer buffer
1662 (setq flymake-last-change-time nil))))
4bcbcb9d 1663
2aa13ec4 1664(defun flymake-get-real-file-name (buffer file-name-from-err-msg)
9a686ad2
SM
1665 "Translate file name from error message to \"real\" file name.
1666Return full-name. Names are real, not patched."
5323b4b0
RS
1667 (let* ((real-name nil)
1668 (source-file-name (buffer-file-name buffer))
1669 (master-file-name (flymake-get-buffer-value buffer "master-file-name"))
1670 (temp-source-file-name (flymake-get-buffer-value buffer "temp-source-file-name"))
1671 (temp-master-file-name (flymake-get-buffer-value buffer "temp-master-file-name"))
1672 (base-dirs (list (flymake-get-buffer-value buffer "base-dir")
1673 (file-name-directory source-file-name)
1674 (if master-file-name (file-name-directory master-file-name) nil)))
1675 (files (list (list source-file-name source-file-name)
1676 (list temp-source-file-name source-file-name)
1677 (list master-file-name master-file-name)
1678 (list temp-master-file-name master-file-name))))
1679
1680 (when (equal 0 (length file-name-from-err-msg))
1681 (setq file-name-from-err-msg source-file-name))
1682
1683 (setq real-name (flymake-get-full-patched-file-name file-name-from-err-msg base-dirs files))
9a686ad2 1684 ;; if real-name is nil, than file name from err msg is none of the files we've patched
5323b4b0
RS
1685 (if (not real-name)
1686 (setq real-name (flymake-get-full-nonpatched-file-name file-name-from-err-msg base-dirs)))
1687 (if (not real-name)
1688 (setq real-name file-name-from-err-msg))
1689 (setq real-name (flymake-fix-file-name real-name))
1690 (flymake-log 3 "get-real-file-name: file-name=%s real-name=%s" file-name-from-err-msg real-name)
1691 real-name))
4bcbcb9d 1692
2aa13ec4 1693(defun flymake-get-full-patched-file-name (file-name-from-err-msg base-dirs files)
5323b4b0
RS
1694 (let* ((base-dirs-count (length base-dirs))
1695 (file-count (length files))
1696 (real-name nil))
1697
1698 (while (and (not real-name) (> base-dirs-count 0))
1699 (setq file-count (length files))
1700 (while (and (not real-name) (> file-count 0))
1701 (let* ((this-dir (nth (1- base-dirs-count) base-dirs))
1702 (this-file (nth 0 (nth (1- file-count) files)))
1703 (this-real-name (nth 1 (nth (1- file-count) files))))
9a686ad2 1704 ;;+(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)
5323b4b0 1705 (when (and this-dir this-file (flymake-same-files
b8471a02 1706 (expand-file-name file-name-from-err-msg this-dir)
5323b4b0
RS
1707 this-file))
1708 (setq real-name this-real-name)))
1709 (setq file-count (1- file-count)))
1710 (setq base-dirs-count (1- base-dirs-count)))
1711 real-name))
4bcbcb9d 1712
2aa13ec4 1713(defun flymake-get-full-nonpatched-file-name (file-name-from-err-msg base-dirs)
5323b4b0
RS
1714 (let* ((real-name nil))
1715 (if (file-name-absolute-p file-name-from-err-msg)
1716 (setq real-name file-name-from-err-msg)
1717 (let* ((base-dirs-count (length base-dirs)))
1718 (while (and (not real-name) (> base-dirs-count 0))
b8471a02
SM
1719 (let* ((full-name (expand-file-name file-name-from-err-msg
1720 (nth (1- base-dirs-count) base-dirs))))
5323b4b0
RS
1721 (if (file-exists-p full-name)
1722 (setq real-name full-name))
1723 (setq base-dirs-count (1- base-dirs-count))))))
1724 real-name))
4bcbcb9d 1725
2aa13ec4 1726(defun flymake-init-find-buildfile-dir (buffer source-file-name buildfile-name)
5323b4b0
RS
1727 "Find buildfile, store its dir in buffer data and return its dir, if found."
1728 (let* ((buildfile-dir (flymake-find-buildfile buildfile-name
1729 (file-name-directory source-file-name)
1730 flymake-buildfile-dirs)))
1731 (if (not buildfile-dir)
1732 (progn
1733 (flymake-log 1 "no buildfile (%s) for %s" buildfile-name source-file-name)
1734 (flymake-report-fatal-status buffer "NOMK" (format "No buildfile (%s) found for %s" buildfile-name source-file-name))
1735 )
1736 (progn
1737 (flymake-set-buffer-value buffer "base-dir" buildfile-dir)))
1738 buildfile-dir))
4bcbcb9d 1739
2aa13ec4 1740(defun flymake-init-create-temp-source-and-master-buffer-copy (buffer get-incl-dirs-f create-temp-f master-file-masks include-regexp-list)
5323b4b0
RS
1741 "Find master file (or buffer), create it's copy along with a copy of the source file."
1742 (let* ((source-file-name (buffer-file-name buffer))
1743 (temp-source-file-name (flymake-init-create-temp-buffer-copy buffer create-temp-f))
1744 (master-file-name nil)
1745 (temp-master-file-name nil)
1746 (master-and-temp-master (flymake-create-master-file
1747 source-file-name temp-source-file-name
1748 get-incl-dirs-f create-temp-f
1749 master-file-masks include-regexp-list)))
1750
1751 (if (not master-and-temp-master)
1752 (progn
1753 (flymake-log 1 "cannot find master file for %s" source-file-name)
1754 (flymake-report-status buffer "!" "") ; NOMASTER
1755 )
1756 (progn
1757 (setq master-file-name (nth 0 master-and-temp-master))
1758 (setq temp-master-file-name (nth 1 master-and-temp-master))
1759 (flymake-set-buffer-value buffer "master-file-name" master-file-name)
1760 (flymake-set-buffer-value buffer "temp-master-file-name" temp-master-file-name)
1761 ))
1762 temp-master-file-name))
4bcbcb9d 1763
2aa13ec4 1764(defun flymake-master-cleanup (buffer)
5323b4b0
RS
1765 (flymake-simple-cleanup buffer)
1766 (flymake-safe-delete-file (flymake-get-buffer-value buffer "temp-master-file-name")))
4bcbcb9d
EZ
1767
1768;;;; make-specific init-cleanup routines
2aa13ec4 1769(defun flymake-get-syntax-check-program-args (source-file-name base-dir use-relative-base-dir use-relative-source get-cmd-line-f)
5323b4b0
RS
1770 "Create a command line for syntax check using GET-CMD-LINE-F."
1771 (let* ((my-base-dir base-dir)
1772 (my-source source-file-name))
4bcbcb9d 1773
5323b4b0
RS
1774 (when use-relative-base-dir
1775 (setq my-base-dir (flymake-build-relative-filename (file-name-directory source-file-name) base-dir)))
4bcbcb9d 1776
5323b4b0
RS
1777 (when use-relative-source
1778 (setq my-source (concat (flymake-build-relative-filename base-dir (file-name-directory source-file-name))
1779 (file-name-nondirectory source-file-name))))
1780 (funcall get-cmd-line-f my-source my-base-dir)))
4bcbcb9d 1781
2aa13ec4 1782(defun flymake-get-make-cmdline (source base-dir)
5323b4b0
RS
1783 (list "make"
1784 (list "-s"
1785 "-C"
1786 base-dir
1787 (concat "CHK_SOURCES=" source)
1788 "SYNTAX_CHECK_MODE=1"
1789 "check-syntax")))
4bcbcb9d 1790
2aa13ec4 1791(defun flymake-get-ant-cmdline (source base-dir)
5323b4b0
RS
1792 (list "ant"
1793 (list "-buildfile"
1794 (concat base-dir "/" "build.xml")
1795 (concat "-DCHK_SOURCES=" source)
1796 "check-syntax")))
4bcbcb9d 1797
2aa13ec4 1798(defun flymake-simple-make-init-impl (buffer create-temp-f use-relative-base-dir use-relative-source build-file-name get-cmdline-f)
5323b4b0 1799 "Create syntax check command line for a directly checked source file.
2aa13ec4 1800Use CREATE-TEMP-F for creating temp copy."
5323b4b0
RS
1801 (let* ((args nil)
1802 (source-file-name (buffer-file-name buffer))
1803 (buildfile-dir (flymake-init-find-buildfile-dir buffer source-file-name build-file-name)))
1804 (if buildfile-dir
1805 (let* ((temp-source-file-name (flymake-init-create-temp-buffer-copy buffer create-temp-f)))
1806 (setq args (flymake-get-syntax-check-program-args temp-source-file-name buildfile-dir
1807 use-relative-base-dir use-relative-source
1808 get-cmdline-f))))
1809 args))
4bcbcb9d 1810
2aa13ec4 1811(defun flymake-simple-make-init (buffer)
5323b4b0 1812 (flymake-simple-make-init-impl buffer 'flymake-create-temp-inplace t t "Makefile" 'flymake-get-make-cmdline))
4bcbcb9d 1813
2aa13ec4 1814(defun flymake-master-make-init (buffer get-incl-dirs-f master-file-masks include-regexp-list)
9a686ad2 1815 "Create make command line for a source file checked via master file compilation."
5323b4b0
RS
1816 (let* ((make-args nil)
1817 (temp-master-file-name (flymake-init-create-temp-source-and-master-buffer-copy
1818 buffer get-incl-dirs-f 'flymake-create-temp-inplace
1819 master-file-masks include-regexp-list)))
1820 (when temp-master-file-name
1821 (let* ((buildfile-dir (flymake-init-find-buildfile-dir buffer temp-master-file-name "Makefile")))
1822 (if buildfile-dir
1823 (setq make-args (flymake-get-syntax-check-program-args
1824 temp-master-file-name buildfile-dir nil nil 'flymake-get-make-cmdline)))))
1825 make-args))
4bcbcb9d 1826
2aa13ec4 1827(defun flymake-find-make-buildfile (source-dir)
5323b4b0 1828 (flymake-find-buildfile "Makefile" source-dir flymake-buildfile-dirs))
4bcbcb9d
EZ
1829
1830;;;; .h/make specific
2aa13ec4 1831(defun flymake-master-make-header-init (buffer)
5323b4b0
RS
1832 (flymake-master-make-init buffer
1833 'flymake-get-include-dirs
1834 '(".+\\.cpp$" ".+\\.c$")
1835 '("[ \t]*#[ \t]*include[ \t]*\"\\([\w0-9/\\_\.]*[/\\]*\\)\\(%s\\)\"" 1 2)))
4bcbcb9d
EZ
1836
1837;;;; .java/make specific
2aa13ec4 1838(defun flymake-simple-make-java-init (buffer)
5323b4b0 1839 (flymake-simple-make-init-impl buffer 'flymake-create-temp-with-folder-structure nil nil "Makefile" 'flymake-get-make-cmdline))
4bcbcb9d 1840
2aa13ec4 1841(defun flymake-simple-ant-java-init (buffer)
5323b4b0 1842 (flymake-simple-make-init-impl buffer 'flymake-create-temp-with-folder-structure nil nil "build.xml" 'flymake-get-ant-cmdline))
4bcbcb9d 1843
2aa13ec4 1844(defun flymake-simple-java-cleanup (buffer)
9a686ad2 1845 "Cleanup after `flymake-simple-make-java-init' -- delete temp file and dirs."
5323b4b0
RS
1846 (let* ((temp-source-file-name (flymake-get-buffer-value buffer "temp-source-file-name")))
1847 (flymake-safe-delete-file temp-source-file-name)
1848 (when temp-source-file-name
1849 (flymake-delete-temp-directory (file-name-directory temp-source-file-name)))))
4bcbcb9d
EZ
1850
1851;;;; perl-specific init-cleanup routines
2aa13ec4 1852(defun flymake-perl-init (buffer)
4bd0a5d0
SM
1853 (let* ((temp-file (flymake-init-create-temp-buffer-copy
1854 buffer 'flymake-create-temp-inplace))
1855 (local-file (concat (flymake-build-relative-filename
1856 (file-name-directory buffer-file-name)
1857 (file-name-directory temp-file))
5323b4b0
RS
1858 (file-name-nondirectory temp-file))))
1859 (list "perl" (list "-wc " local-file))))
4bcbcb9d
EZ
1860
1861;;;; tex-specific init-cleanup routines
2aa13ec4 1862(defun flymake-get-tex-args (file-name)
9a686ad2 1863 ;;(list "latex" (list "-c-style-errors" file-name))
5323b4b0 1864 (list "texify" (list "--pdf" "--tex-option=-c-style-errors" file-name)))
4bcbcb9d 1865
2aa13ec4 1866(defun flymake-simple-tex-init (buffer)
5323b4b0 1867 (flymake-get-tex-args (flymake-init-create-temp-buffer-copy buffer 'flymake-create-temp-inplace)))
4bcbcb9d 1868
2aa13ec4 1869(defun flymake-master-tex-init (buffer)
5323b4b0
RS
1870 (let* ((temp-master-file-name (flymake-init-create-temp-source-and-master-buffer-copy
1871 buffer 'flymake-get-include-dirs-dot 'flymake-create-temp-inplace
1872 '(".+\\.tex$")
1873 '("[ \t]*\\input[ \t]*{\\(.*\\)\\(%s\\)}" 1 2))))
1874 (when temp-master-file-name
1875 (flymake-get-tex-args temp-master-file-name))))
4bcbcb9d 1876
2aa13ec4 1877(defun flymake-get-include-dirs-dot (base-dir)
5323b4b0 1878 '("."))
4bcbcb9d
EZ
1879
1880;;;; xml-specific init-cleanup routines
4bd0a5d0 1881(defun flymake-xml-init (buffer)
5323b4b0 1882 (list "xml" (list "val" (flymake-init-create-temp-buffer-copy buffer 'flymake-create-temp-inplace))))
2aa13ec4
RS
1883
1884(provide 'flymake)
4bcbcb9d 1885
9a686ad2 1886;; arch-tag: 8f0d6090-061d-4cac-8862-7c151c4a02dd
4bcbcb9d 1887;;; flymake.el ends here