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