Merge branch 'master' into comment-cache
[emacs.git] / lisp / progmodes / flymake.el
blobe488c196fef8806df520f01fbca5d20145b25db2
1 ;;; flymake.el --- a universal on-the-fly syntax checker -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2003-2017 Free Software Foundation, Inc.
5 ;; Author: Pavel Kobyakov <pk_at_work@yahoo.com>
6 ;; Maintainer: Leo Liu <sdl.web@gmail.com>
7 ;; Version: 0.3
8 ;; Keywords: c languages tools
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Flymake is a minor Emacs mode performing on-the-fly syntax checks
28 ;; using the external syntax check tool (for C/C++ this is usually the
29 ;; compiler).
31 ;;; Bugs/todo:
33 ;; - Only uses "Makefile", not "makefile" or "GNUmakefile"
34 ;; (from http://bugs.debian.org/337339).
36 ;;; Code:
38 (eval-when-compile (require 'cl-lib))
40 (defgroup flymake nil
41 "Universal on-the-fly syntax checker."
42 :version "23.1"
43 :link '(custom-manual "(flymake) Top")
44 :group 'tools)
46 (defcustom flymake-error-bitmap '(exclamation-mark error)
47 "Bitmap (a symbol) used in the fringe for indicating errors.
48 The value may also be a list of two elements where the second
49 element specifies the face for the bitmap. For possible bitmap
50 symbols, see `fringe-bitmaps'. See also `flymake-warning-bitmap'.
52 The option `flymake-fringe-indicator-position' controls how and where
53 this is used."
54 :group 'flymake
55 :version "24.3"
56 :type '(choice (symbol :tag "Bitmap")
57 (list :tag "Bitmap and face"
58 (symbol :tag "Bitmap")
59 (face :tag "Face"))))
61 (defcustom flymake-warning-bitmap 'question-mark
62 "Bitmap (a symbol) used in the fringe for indicating warnings.
63 The value may also be a list of two elements where the second
64 element specifies the face for the bitmap. For possible bitmap
65 symbols, see `fringe-bitmaps'. See also `flymake-error-bitmap'.
67 The option `flymake-fringe-indicator-position' controls how and where
68 this is used."
69 :group 'flymake
70 :version "24.3"
71 :type '(choice (symbol :tag "Bitmap")
72 (list :tag "Bitmap and face"
73 (symbol :tag "Bitmap")
74 (face :tag "Face"))))
76 (defcustom flymake-fringe-indicator-position 'left-fringe
77 "The position to put flymake fringe indicator.
78 The value can be nil (do not use indicators), `left-fringe' or `right-fringe'.
79 See `flymake-error-bitmap' and `flymake-warning-bitmap'."
80 :group 'flymake
81 :version "24.3"
82 :type '(choice (const left-fringe)
83 (const right-fringe)
84 (const :tag "No fringe indicators" nil)))
86 (defcustom flymake-compilation-prevents-syntax-check t
87 "If non-nil, don't start syntax check if compilation is running."
88 :group 'flymake
89 :type 'boolean)
91 (defcustom flymake-start-syntax-check-on-newline t
92 "Start syntax check if newline char was added/removed from the buffer."
93 :group 'flymake
94 :type 'boolean)
96 (defcustom flymake-no-changes-timeout 0.5
97 "Time to wait after last change before starting compilation."
98 :group 'flymake
99 :type 'number)
101 (defcustom flymake-gui-warnings-enabled t
102 "Enables/disables GUI warnings."
103 :group 'flymake
104 :type 'boolean)
105 (make-obsolete-variable 'flymake-gui-warnings-enabled
106 "it no longer has any effect." "26.1")
108 (defcustom flymake-start-syntax-check-on-find-file t
109 "Start syntax check on find file."
110 :group 'flymake
111 :type 'boolean)
113 (defcustom flymake-log-level -1
114 "Logging level, only messages with level lower or equal will be logged.
115 -1 = NONE, 0 = ERROR, 1 = WARNING, 2 = INFO, 3 = DEBUG"
116 :group 'flymake
117 :type 'integer)
119 (defcustom flymake-xml-program
120 (if (executable-find "xmlstarlet") "xmlstarlet" "xml")
121 "Program to use for XML validation."
122 :type 'file
123 :group 'flymake
124 :version "24.4")
126 (defcustom flymake-master-file-dirs '("." "./src" "./UnitTest")
127 "Dirs where to look for master files."
128 :group 'flymake
129 :type '(repeat (string)))
131 (defcustom flymake-master-file-count-limit 32
132 "Max number of master files to check."
133 :group 'flymake
134 :type 'integer)
136 (defcustom flymake-allowed-file-name-masks
137 '(("\\.\\(?:c\\(?:pp\\|xx\\|\\+\\+\\)?\\|CC\\)\\'" flymake-simple-make-init)
138 ("\\.xml\\'" flymake-xml-init)
139 ("\\.html?\\'" flymake-xml-init)
140 ("\\.cs\\'" flymake-simple-make-init)
141 ("\\.p[ml]\\'" flymake-perl-init)
142 ("\\.php[345]?\\'" flymake-php-init)
143 ("\\.h\\'" flymake-master-make-header-init flymake-master-cleanup)
144 ("\\.java\\'" flymake-simple-make-java-init flymake-simple-java-cleanup)
145 ("[0-9]+\\.tex\\'" flymake-master-tex-init flymake-master-cleanup)
146 ("\\.tex\\'" flymake-simple-tex-init)
147 ("\\.idl\\'" flymake-simple-make-init)
148 ;; ("\\.cpp\\'" 1)
149 ;; ("\\.java\\'" 3)
150 ;; ("\\.h\\'" 2 ("\\.cpp\\'" "\\.c\\'")
151 ;; ("[ \t]*#[ \t]*include[ \t]*\"\\([\w0-9/\\_\.]*[/\\]*\\)\\(%s\\)\"" 1 2))
152 ;; ("\\.idl\\'" 1)
153 ;; ("\\.odl\\'" 1)
154 ;; ("[0-9]+\\.tex\\'" 2 ("\\.tex\\'")
155 ;; ("[ \t]*\\input[ \t]*{\\(.*\\)\\(%s\\)}" 1 2 ))
156 ;; ("\\.tex\\'" 1)
158 "Files syntax checking is allowed for.
159 This is an alist with elements of the form:
160 REGEXP INIT [CLEANUP [NAME]]
161 REGEXP is a regular expression that matches a file name.
162 INIT is the init function to use.
163 CLEANUP is the cleanup function to use, default `flymake-simple-cleanup'.
164 NAME is the file name function to use, default `flymake-get-real-file-name'."
165 :group 'flymake
166 :type '(alist :key-type (regexp :tag "File regexp")
167 :value-type
168 (list :tag "Handler functions"
169 (function :tag "Init function")
170 (choice :tag "Cleanup function"
171 (const :tag "flymake-simple-cleanup" nil)
172 function)
173 (choice :tag "Name function"
174 (const :tag "flymake-get-real-file-name" nil)
175 function))))
177 (defvar-local flymake-is-running nil
178 "If t, flymake syntax check process is running for the current buffer.")
180 (defvar-local flymake-timer nil
181 "Timer for starting syntax check.")
183 (defvar-local flymake-last-change-time nil
184 "Time of last buffer change.")
186 (defvar-local flymake-check-start-time nil
187 "Time at which syntax check was started.")
189 (defvar-local flymake-check-was-interrupted nil
190 "Non-nil if syntax check was killed by `flymake-compile'.")
192 (defvar-local flymake-err-info nil
193 "Sorted list of line numbers and lists of err info in the form (file, err-text).")
195 (defvar-local flymake-new-err-info nil
196 "Same as `flymake-err-info', effective when a syntax check is in progress.")
198 (defun flymake-log (level text &rest args)
199 "Log a message at level LEVEL.
200 If LEVEL is higher than `flymake-log-level', the message is
201 ignored. Otherwise, it is printed using `message'.
202 TEXT is a format control string, and the remaining arguments ARGS
203 are the string substitutions (see the function `format')."
204 (if (<= level flymake-log-level)
205 (let* ((msg (apply #'format-message text args)))
206 (message "%s" msg))))
208 (defun flymake-ins-after (list pos val)
209 "Insert VAL into LIST after position POS.
210 POS counts from zero."
211 (let ((tmp (copy-sequence list)))
212 (setcdr (nthcdr pos tmp) (cons val (nthcdr (1+ pos) tmp)))
213 tmp))
215 (defun flymake-set-at (list pos val)
216 "Set VAL at position POS in LIST.
217 POS counts from zero."
218 (let ((tmp (copy-sequence list)))
219 (setcar (nthcdr pos tmp) val)
220 tmp))
222 (defvar flymake-processes nil
223 "List of currently active flymake processes.")
225 (defvar-local flymake-output-residual nil)
227 (defun flymake-get-file-name-mode-and-masks (file-name)
228 "Return the corresponding entry from `flymake-allowed-file-name-masks'."
229 (unless (stringp file-name)
230 (error "Invalid file-name"))
231 (let ((fnm flymake-allowed-file-name-masks)
232 (mode-and-masks nil))
233 (while (and (not mode-and-masks) fnm)
234 (if (string-match (car (car fnm)) file-name)
235 (setq mode-and-masks (cdr (car fnm))))
236 (setq fnm (cdr fnm)))
237 (flymake-log 3 "file %s, init=%s" file-name (car mode-and-masks))
238 mode-and-masks))
240 (defun flymake-can-syntax-check-file (file-name)
241 "Determine whether we can syntax check FILE-NAME.
242 Return nil if we cannot, non-nil if we can."
243 (if (flymake-get-init-function file-name) t nil))
245 (defun flymake-get-init-function (file-name)
246 "Return init function to be used for the file."
247 (let* ((init-f (nth 0 (flymake-get-file-name-mode-and-masks file-name))))
248 ;;(flymake-log 0 "calling %s" init-f)
249 ;;(funcall init-f (current-buffer))
250 init-f))
252 (defun flymake-get-cleanup-function (file-name)
253 "Return cleanup function to be used for the file."
254 (or (nth 1 (flymake-get-file-name-mode-and-masks file-name))
255 'flymake-simple-cleanup))
257 (defun flymake-get-real-file-name-function (file-name)
258 (or (nth 2 (flymake-get-file-name-mode-and-masks file-name))
259 'flymake-get-real-file-name))
261 (defvar flymake-find-buildfile-cache (make-hash-table :test #'equal))
263 (defun flymake-get-buildfile-from-cache (dir-name)
264 "Look up DIR-NAME in cache and return its associated value.
265 If DIR-NAME is not found, return nil."
266 (gethash dir-name flymake-find-buildfile-cache))
268 (defun flymake-add-buildfile-to-cache (dir-name buildfile)
269 "Associate DIR-NAME with BUILDFILE in the buildfile cache."
270 (puthash dir-name buildfile flymake-find-buildfile-cache))
272 (defun flymake-clear-buildfile-cache ()
273 "Clear the buildfile cache."
274 (clrhash flymake-find-buildfile-cache))
276 (defun flymake-find-buildfile (buildfile-name source-dir-name)
277 "Find buildfile starting from current directory.
278 Buildfile includes Makefile, build.xml etc.
279 Return its file name if found, or nil if not found."
280 (or (flymake-get-buildfile-from-cache source-dir-name)
281 (let* ((file (locate-dominating-file source-dir-name buildfile-name)))
282 (if file
283 (progn
284 (flymake-log 3 "found buildfile at %s" file)
285 (flymake-add-buildfile-to-cache source-dir-name file)
286 file)
287 (progn
288 (flymake-log 3 "buildfile for %s not found" source-dir-name)
289 nil)))))
291 (defun flymake-fix-file-name (name)
292 "Replace all occurrences of `\\' with `/'."
293 (when name
294 (setq name (expand-file-name name))
295 (setq name (abbreviate-file-name name))
296 (setq name (directory-file-name name))
297 name))
299 (defun flymake-same-files (file-name-one file-name-two)
300 "Check if FILE-NAME-ONE and FILE-NAME-TWO point to same file.
301 Return t if so, nil if not."
302 (equal (flymake-fix-file-name file-name-one)
303 (flymake-fix-file-name file-name-two)))
305 ;; This is bound dynamically to pass a parameter to a sort predicate below
306 (defvar flymake-included-file-name)
308 (defun flymake-find-possible-master-files (file-name master-file-dirs masks)
309 "Find (by name and location) all possible master files.
311 Name is specified by FILE-NAME and location is specified by
312 MASTER-FILE-DIRS. Master files include .cpp and .c for .h.
313 Files are searched for starting from the .h directory and max
314 max-level parent dirs. File contents are not checked."
315 (let* ((dirs master-file-dirs)
316 (files nil)
317 (done nil))
319 (while (and (not done) dirs)
320 (let* ((dir (expand-file-name (car dirs) (file-name-directory file-name)))
321 (masks masks))
322 (while (and (file-exists-p dir) (not done) masks)
323 (let* ((mask (car masks))
324 (dir-files (directory-files dir t mask)))
326 (flymake-log 3 "dir %s, %d file(s) for mask %s"
327 dir (length dir-files) mask)
328 (while (and (not done) dir-files)
329 (when (not (file-directory-p (car dir-files)))
330 (setq files (cons (car dir-files) files))
331 (when (>= (length files) flymake-master-file-count-limit)
332 (flymake-log 3 "master file count limit (%d) reached" flymake-master-file-count-limit)
333 (setq done t)))
334 (setq dir-files (cdr dir-files))))
335 (setq masks (cdr masks))))
336 (setq dirs (cdr dirs)))
337 (when files
338 (let ((flymake-included-file-name (file-name-nondirectory file-name)))
339 (setq files (sort files 'flymake-master-file-compare))))
340 (flymake-log 3 "found %d possible master file(s)" (length files))
341 files))
343 (defun flymake-master-file-compare (file-one file-two)
344 "Compare two files specified by FILE-ONE and FILE-TWO.
345 This function is used in sort to move most possible file names
346 to the beginning of the list (File.h -> File.cpp moved to top)."
347 (and (equal (file-name-sans-extension flymake-included-file-name)
348 (file-name-base file-one))
349 (not (equal file-one file-two))))
351 (defvar flymake-check-file-limit 8192
352 "Maximum number of chars to look at when checking possible master file.
353 Nil means search the entire file.")
355 (defun flymake-check-patch-master-file-buffer
356 (master-file-temp-buffer
357 master-file-name patched-master-file-name
358 source-file-name patched-source-file-name
359 include-dirs regexp)
360 "Check if MASTER-FILE-NAME is a master file for SOURCE-FILE-NAME.
361 If yes, patch a copy of MASTER-FILE-NAME to include PATCHED-SOURCE-FILE-NAME
362 instead of SOURCE-FILE-NAME.
364 For example, foo.cpp is a master file if it includes foo.h.
366 Whether a buffer for MATER-FILE-NAME exists, use it as a source
367 instead of reading master file from disk."
368 (let* ((source-file-nondir (file-name-nondirectory source-file-name))
369 (source-file-extension (file-name-extension source-file-nondir))
370 (source-file-nonext (file-name-sans-extension source-file-nondir))
371 (found nil)
372 (inc-name nil)
373 (search-limit flymake-check-file-limit))
374 (setq regexp
375 (format regexp ; "[ \t]*#[ \t]*include[ \t]*\"\\(.*%s\\)\""
376 ;; Hack for tex files, where \include often excludes .tex.
377 ;; Maybe this is safe generally.
378 (if (and (> (length source-file-extension) 1)
379 (string-equal source-file-extension "tex"))
380 (format "%s\\(?:\\.%s\\)?"
381 (regexp-quote source-file-nonext)
382 (regexp-quote source-file-extension))
383 (regexp-quote source-file-nondir))))
384 (unwind-protect
385 (with-current-buffer master-file-temp-buffer
386 (if (or (not search-limit)
387 (> search-limit (point-max)))
388 (setq search-limit (point-max)))
389 (flymake-log 3 "checking %s against regexp %s"
390 master-file-name regexp)
391 (goto-char (point-min))
392 (while (and (< (point) search-limit)
393 (re-search-forward regexp search-limit t))
394 (let ((match-beg (match-beginning 1))
395 (match-end (match-end 1)))
397 (flymake-log 3 "found possible match for %s" source-file-nondir)
398 (setq inc-name (match-string 1))
399 (and (> (length source-file-extension) 1)
400 (string-equal source-file-extension "tex")
401 (not (string-match (format "\\.%s\\'" source-file-extension)
402 inc-name))
403 (setq inc-name (concat inc-name "." source-file-extension)))
404 (when (eq t (compare-strings
405 source-file-nondir nil nil
406 inc-name (- (length inc-name)
407 (length source-file-nondir)) nil))
408 (flymake-log 3 "inc-name=%s" inc-name)
409 (when (flymake-check-include source-file-name inc-name
410 include-dirs)
411 (setq found t)
412 ;; replace-match is not used here as it fails in
413 ;; XEmacs with 'last match not a buffer' error as
414 ;; check-includes calls replace-in-string
415 (flymake-replace-region
416 match-beg match-end
417 (file-name-nondirectory patched-source-file-name))))
418 (forward-line 1)))
419 (when found
420 (flymake-save-buffer-in-file patched-master-file-name)))
421 ;;+(flymake-log 3 "killing buffer %s"
422 ;; (buffer-name master-file-temp-buffer))
423 (kill-buffer master-file-temp-buffer))
424 ;;+(flymake-log 3 "check-patch master file %s: %s" master-file-name found)
425 (when found
426 (flymake-log 2 "found master file %s" master-file-name))
427 found))
429 ;;; XXX: remove
430 (defun flymake-replace-region (beg end rep)
431 "Replace text in BUFFER in region (BEG END) with REP."
432 (save-excursion
433 (goto-char end)
434 ;; Insert before deleting, so as to better preserve markers's positions.
435 (insert rep)
436 (delete-region beg end)))
438 (defun flymake-read-file-to-temp-buffer (file-name)
439 "Insert contents of FILE-NAME into newly created temp buffer."
440 (let* ((temp-buffer (get-buffer-create (generate-new-buffer-name (concat "flymake:" (file-name-nondirectory file-name))))))
441 (with-current-buffer temp-buffer
442 (insert-file-contents file-name))
443 temp-buffer))
445 (defun flymake-copy-buffer-to-temp-buffer (buffer)
446 "Copy contents of BUFFER into newly created temp buffer."
447 (with-current-buffer
448 (get-buffer-create (generate-new-buffer-name
449 (concat "flymake:" (buffer-name buffer))))
450 (insert-buffer-substring buffer)
451 (current-buffer)))
453 (defun flymake-check-include (source-file-name inc-name include-dirs)
454 "Check if SOURCE-FILE-NAME can be found in include path.
455 Return t if it can be found via include path using INC-NAME."
456 (if (file-name-absolute-p inc-name)
457 (flymake-same-files source-file-name inc-name)
458 (while (and include-dirs
459 (not (flymake-same-files
460 source-file-name
461 (concat (file-name-directory source-file-name)
462 "/" (car include-dirs)
463 "/" inc-name))))
464 (setq include-dirs (cdr include-dirs)))
465 include-dirs))
467 (defun flymake-find-buffer-for-file (file-name)
468 "Check if there exists a buffer visiting FILE-NAME.
469 Return t if so, nil if not."
470 (let ((buffer-name (get-file-buffer file-name)))
471 (if buffer-name
472 (get-buffer buffer-name))))
474 (defun flymake-create-master-file (source-file-name patched-source-file-name get-incl-dirs-f create-temp-f masks include-regexp)
475 "Save SOURCE-FILE-NAME with a different name.
476 Find master file, patch and save it."
477 (let* ((possible-master-files (flymake-find-possible-master-files source-file-name flymake-master-file-dirs masks))
478 (master-file-count (length possible-master-files))
479 (idx 0)
480 (temp-buffer nil)
481 (master-file-name nil)
482 (patched-master-file-name nil)
483 (found nil))
485 (while (and (not found) (< idx master-file-count))
486 (setq master-file-name (nth idx possible-master-files))
487 (setq patched-master-file-name (funcall create-temp-f master-file-name "flymake_master"))
488 (if (flymake-find-buffer-for-file master-file-name)
489 (setq temp-buffer (flymake-copy-buffer-to-temp-buffer (flymake-find-buffer-for-file master-file-name)))
490 (setq temp-buffer (flymake-read-file-to-temp-buffer master-file-name)))
491 (setq found
492 (flymake-check-patch-master-file-buffer
493 temp-buffer
494 master-file-name
495 patched-master-file-name
496 source-file-name
497 patched-source-file-name
498 (funcall get-incl-dirs-f (file-name-directory master-file-name))
499 include-regexp))
500 (setq idx (1+ idx)))
501 (if found
502 (list master-file-name patched-master-file-name)
503 (progn
504 (flymake-log 3 "none of %d master file(s) checked includes %s" master-file-count
505 (file-name-nondirectory source-file-name))
506 nil))))
508 (defun flymake-save-buffer-in-file (file-name)
509 "Save the entire buffer contents into file FILE-NAME.
510 Create parent directories as needed."
511 (make-directory (file-name-directory file-name) 1)
512 (write-region nil nil file-name nil 566)
513 (flymake-log 3 "saved buffer %s in file %s" (buffer-name) file-name))
515 (defun flymake-process-filter (process output)
516 "Parse OUTPUT and highlight error lines.
517 It's flymake process filter."
518 (let ((source-buffer (process-buffer process)))
520 (flymake-log 3 "received %d byte(s) of output from process %d"
521 (length output) (process-id process))
522 (when (buffer-live-p source-buffer)
523 (with-current-buffer source-buffer
524 (flymake-parse-output-and-residual output)))))
526 (defun flymake-process-sentinel (process _event)
527 "Sentinel for syntax check buffers."
528 (when (memq (process-status process) '(signal exit))
529 (let* ((exit-status (process-exit-status process))
530 (command (process-command process))
531 (source-buffer (process-buffer process))
532 (cleanup-f (flymake-get-cleanup-function (buffer-file-name source-buffer))))
534 (flymake-log 2 "process %d exited with code %d"
535 (process-id process) exit-status)
536 (condition-case err
537 (progn
538 (flymake-log 3 "cleaning up using %s" cleanup-f)
539 (when (buffer-live-p source-buffer)
540 (with-current-buffer source-buffer
541 (funcall cleanup-f)))
543 (delete-process process)
544 (setq flymake-processes (delq process flymake-processes))
546 (when (buffer-live-p source-buffer)
547 (with-current-buffer source-buffer
549 (flymake-parse-residual)
550 (flymake-post-syntax-check exit-status command)
551 (setq flymake-is-running nil))))
552 (error
553 (let ((err-str (format "Error in process sentinel for buffer %s: %s"
554 source-buffer (error-message-string err))))
555 (flymake-log 0 err-str)
556 (with-current-buffer source-buffer
557 (setq flymake-is-running nil))))))))
559 (defun flymake-post-syntax-check (exit-status command)
560 (save-restriction
561 (widen)
562 (setq flymake-err-info flymake-new-err-info)
563 (setq flymake-new-err-info nil)
564 (setq flymake-err-info
565 (flymake-fix-line-numbers
566 flymake-err-info 1 (count-lines (point-min) (point-max))))
567 (flymake-delete-own-overlays)
568 (flymake-highlight-err-lines flymake-err-info)
569 (let (err-count warn-count)
570 (setq err-count (flymake-get-err-count flymake-err-info "e"))
571 (setq warn-count (flymake-get-err-count flymake-err-info "w"))
572 (flymake-log 2 "%s: %d error(s), %d warning(s) in %.2f second(s)"
573 (buffer-name) err-count warn-count
574 (- (float-time) flymake-check-start-time))
575 (setq flymake-check-start-time nil)
577 (if (and (equal 0 err-count) (equal 0 warn-count))
578 (if (equal 0 exit-status)
579 (flymake-report-status "" "") ; PASSED
580 (if (not flymake-check-was-interrupted)
581 (flymake-report-fatal-status "CFGERR"
582 (format "Configuration error has occurred while running %s" command))
583 (flymake-report-status nil ""))) ; "STOPPED"
584 (flymake-report-status (format "%d/%d" err-count warn-count) "")))))
586 (defun flymake-parse-output-and-residual (output)
587 "Split OUTPUT into lines, merge in residual if necessary."
588 (let* ((buffer-residual flymake-output-residual)
589 (total-output (if buffer-residual (concat buffer-residual output) output))
590 (lines-and-residual (flymake-split-output total-output))
591 (lines (nth 0 lines-and-residual))
592 (new-residual (nth 1 lines-and-residual)))
593 (setq flymake-output-residual new-residual)
594 (setq flymake-new-err-info
595 (flymake-parse-err-lines
596 flymake-new-err-info lines))))
598 (defun flymake-parse-residual ()
599 "Parse residual if it's non empty."
600 (when flymake-output-residual
601 (setq flymake-new-err-info
602 (flymake-parse-err-lines
603 flymake-new-err-info
604 (list flymake-output-residual)))
605 (setq flymake-output-residual nil)))
607 (defun flymake-er-make-er (line-no line-err-info-list)
608 (list line-no line-err-info-list))
610 (defun flymake-er-get-line (err-info)
611 (nth 0 err-info))
613 (defun flymake-er-get-line-err-info-list (err-info)
614 (nth 1 err-info))
616 (cl-defstruct (flymake-ler
617 (:constructor nil)
618 (:constructor flymake-ler-make-ler (file line type text &optional full-file)))
619 file line type text full-file)
621 (defun flymake-ler-set-file (line-err-info file)
622 (flymake-ler-make-ler file
623 (flymake-ler-line line-err-info)
624 (flymake-ler-type line-err-info)
625 (flymake-ler-text line-err-info)
626 (flymake-ler-full-file line-err-info)))
628 (defun flymake-ler-set-full-file (line-err-info full-file)
629 (flymake-ler-make-ler (flymake-ler-file line-err-info)
630 (flymake-ler-line line-err-info)
631 (flymake-ler-type line-err-info)
632 (flymake-ler-text line-err-info)
633 full-file))
635 (defun flymake-ler-set-line (line-err-info line)
636 (flymake-ler-make-ler (flymake-ler-file line-err-info)
637 line
638 (flymake-ler-type line-err-info)
639 (flymake-ler-text line-err-info)
640 (flymake-ler-full-file line-err-info)))
642 (defun flymake-get-line-err-count (line-err-info-list type)
643 "Return number of errors of specified TYPE.
644 Value of TYPE is either \"e\" or \"w\"."
645 (let* ((idx 0)
646 (count (length line-err-info-list))
647 (err-count 0))
649 (while (< idx count)
650 (when (equal type (flymake-ler-type (nth idx line-err-info-list)))
651 (setq err-count (1+ err-count)))
652 (setq idx (1+ idx)))
653 err-count))
655 (defun flymake-get-err-count (err-info-list type)
656 "Return number of errors of specified TYPE for ERR-INFO-LIST."
657 (let* ((idx 0)
658 (count (length err-info-list))
659 (err-count 0))
660 (while (< idx count)
661 (setq err-count (+ err-count (flymake-get-line-err-count (nth 1 (nth idx err-info-list)) type)))
662 (setq idx (1+ idx)))
663 err-count))
665 (defun flymake-fix-line-numbers (err-info-list min-line max-line)
666 "Replace line numbers with fixed value.
667 If line-numbers is less than MIN-LINE, set line numbers to MIN-LINE.
668 If line numbers is greater than MAX-LINE, set line numbers to MAX-LINE.
669 The reason for this fix is because some compilers might report
670 line number outside the file being compiled."
671 (let* ((count (length err-info-list))
672 (err-info nil)
673 (line 0))
674 (while (> count 0)
675 (setq err-info (nth (1- count) err-info-list))
676 (setq line (flymake-er-get-line err-info))
677 (when (or (< line min-line) (> line max-line))
678 (setq line (if (< line min-line) min-line max-line))
679 (setq err-info-list (flymake-set-at err-info-list (1- count)
680 (flymake-er-make-er line
681 (flymake-er-get-line-err-info-list err-info)))))
682 (setq count (1- count))))
683 err-info-list)
685 (defun flymake-highlight-err-lines (err-info-list)
686 "Highlight error lines in BUFFER using info from ERR-INFO-LIST."
687 (save-excursion
688 (dolist (err err-info-list)
689 (flymake-highlight-line (car err) (nth 1 err)))))
691 (defun flymake-overlay-p (ov)
692 "Determine whether overlay OV was created by flymake."
693 (and (overlayp ov) (overlay-get ov 'flymake-overlay)))
695 (defun flymake-make-overlay (beg end tooltip-text face bitmap)
696 "Allocate a flymake overlay in range BEG and END."
697 (when (not (flymake-region-has-flymake-overlays beg end))
698 (let ((ov (make-overlay beg end nil t))
699 (fringe (and flymake-fringe-indicator-position
700 (propertize "!" 'display
701 (cons flymake-fringe-indicator-position
702 (if (listp bitmap)
703 bitmap
704 (list bitmap)))))))
705 (overlay-put ov 'face face)
706 (overlay-put ov 'help-echo tooltip-text)
707 (overlay-put ov 'flymake-overlay t)
708 (overlay-put ov 'priority 100)
709 (overlay-put ov 'evaporate t)
710 (overlay-put ov 'before-string fringe)
711 ;;+(flymake-log 3 "created overlay %s" ov)
713 (flymake-log 3 "created an overlay at (%d-%d)" beg end)))
715 (defun flymake-delete-own-overlays ()
716 "Delete all flymake overlays in BUFFER."
717 (dolist (ol (overlays-in (point-min) (point-max)))
718 (when (flymake-overlay-p ol)
719 (delete-overlay ol)
720 ;;+(flymake-log 3 "deleted overlay %s" ol)
723 (defun flymake-region-has-flymake-overlays (beg end)
724 "Check if region specified by BEG and END has overlay.
725 Return t if it has at least one flymake overlay, nil if no overlay."
726 (let ((ov (overlays-in beg end))
727 (has-flymake-overlays nil))
728 (while (consp ov)
729 (when (flymake-overlay-p (car ov))
730 (setq has-flymake-overlays t))
731 (setq ov (cdr ov)))
732 has-flymake-overlays))
734 (defface flymake-errline
735 '((((supports :underline (:style wave)))
736 :underline (:style wave :color "Red1"))
738 :inherit error))
739 "Face used for marking error lines."
740 :version "24.4"
741 :group 'flymake)
743 (defface flymake-warnline
744 '((((supports :underline (:style wave)))
745 :underline (:style wave :color "DarkOrange"))
747 :inherit warning))
748 "Face used for marking warning lines."
749 :version "24.4"
750 :group 'flymake)
752 (defun flymake-highlight-line (line-no line-err-info-list)
753 "Highlight line LINE-NO in current buffer.
754 Perhaps use text from LINE-ERR-INFO-LIST to enhance highlighting."
755 (goto-char (point-min))
756 (forward-line (1- line-no))
757 (pcase-let* ((beg (progn (back-to-indentation) (point)))
758 (end (progn
759 (end-of-line)
760 (skip-chars-backward " \t\f\t\n" beg)
761 (if (eq (point) beg)
762 (line-beginning-position 2)
763 (point))))
764 (tooltip-text (mapconcat #'flymake-ler-text line-err-info-list "\n"))
765 (`(,face ,bitmap)
766 (if (> (flymake-get-line-err-count line-err-info-list "e") 0)
767 (list 'flymake-errline flymake-error-bitmap)
768 (list 'flymake-warnline flymake-warning-bitmap))))
769 (flymake-make-overlay beg end tooltip-text face bitmap)))
771 (defun flymake-parse-err-lines (err-info-list lines)
772 "Parse err LINES, store info in ERR-INFO-LIST."
773 (let* ((count (length lines))
774 (idx 0)
775 (line-err-info nil)
776 (real-file-name nil)
777 (source-file-name buffer-file-name)
778 (get-real-file-name-f (flymake-get-real-file-name-function source-file-name)))
780 (while (< idx count)
781 (setq line-err-info (flymake-parse-line (nth idx lines)))
782 (when line-err-info
783 (setq real-file-name (funcall get-real-file-name-f
784 (flymake-ler-file line-err-info)))
785 (setq line-err-info (flymake-ler-set-full-file line-err-info real-file-name))
787 (when (flymake-same-files real-file-name source-file-name)
788 (setq line-err-info (flymake-ler-set-file line-err-info nil))
789 (setq err-info-list (flymake-add-err-info err-info-list line-err-info))))
790 (flymake-log 3 "parsed `%s', %s line-err-info" (nth idx lines) (if line-err-info "got" "no"))
791 (setq idx (1+ idx)))
792 err-info-list))
794 (defun flymake-split-output (output)
795 "Split OUTPUT into lines.
796 Return last one as residual if it does not end with newline char.
797 Returns ((LINES) RESIDUAL)."
798 (when (and output (> (length output) 0))
799 (let* ((lines (split-string output "[\n\r]+" t))
800 (complete (equal "\n" (char-to-string (aref output (1- (length output))))))
801 (residual nil))
802 (when (not complete)
803 (setq residual (car (last lines)))
804 (setq lines (butlast lines)))
805 (list lines residual))))
807 (defun flymake-reformat-err-line-patterns-from-compile-el (original-list)
808 "Grab error line patterns from ORIGINAL-LIST in compile.el format.
809 Convert it to flymake internal format."
810 (let* ((converted-list '()))
811 (dolist (item original-list)
812 (setq item (cdr item))
813 (let ((regexp (nth 0 item))
814 (file (nth 1 item))
815 (line (nth 2 item))
816 (col (nth 3 item)))
817 (if (consp file) (setq file (car file)))
818 (if (consp line) (setq line (car line)))
819 (if (consp col) (setq col (car col)))
821 (when (not (functionp line))
822 (setq converted-list (cons (list regexp file line col) converted-list)))))
823 converted-list))
825 (require 'compile)
827 (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
828 (append
830 ;; MS Visual C++ 6.0
831 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\)) : \\(\\(error\\|warning\\|fatal error\\) \\(C[0-9]+\\):[ \t\n]*\\(.+\\)\\)"
832 1 3 nil 4)
833 ;; jikes
834 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\):\\([0-9]+\\):[0-9]+:[0-9]+:[0-9]+: \\(\\(Error\\|Warning\\|Caution\\|Semantic Error\\):[ \t\n]*\\(.+\\)\\)"
835 1 3 nil 4)
836 ;; MS midl
837 ("midl[ ]*:[ ]*\\(command line error .*\\)"
838 nil nil nil 1)
839 ;; MS C#
840 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\),[0-9]+): \\(\\(error\\|warning\\|fatal error\\) \\(CS[0-9]+\\):[ \t\n]*\\(.+\\)\\)"
841 1 3 nil 4)
842 ;; perl
843 ("\\(.*\\) at \\([^ \n]+\\) line \\([0-9]+\\)[,.\n]" 2 3 nil 1)
844 ;; PHP
845 ("\\(?:Parse\\|Fatal\\) error: \\(.*\\) in \\(.*\\) on line \\([0-9]+\\)" 2 3 nil 1)
846 ;; LaTeX warnings (fileless) ("\\(LaTeX \\(Warning\\|Error\\): .*\\) on input line \\([0-9]+\\)" 20 3 nil 1)
847 ;; ant/javac. Note this also matches gcc warnings!
848 (" *\\(\\[javac\\] *\\)?\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\):\\([0-9]+\\)\\(?::[0-9]+\\)?:[ \t\n]*\\(.+\\)"
849 2 4 nil 5))
850 ;; compilation-error-regexp-alist)
851 (flymake-reformat-err-line-patterns-from-compile-el compilation-error-regexp-alist-alist))
852 "Patterns for matching error/warning lines. Each pattern has the form
853 \(REGEXP FILE-IDX LINE-IDX COL-IDX ERR-TEXT-IDX).
854 Use `flymake-reformat-err-line-patterns-from-compile-el' to add patterns
855 from compile.el")
857 (define-obsolete-variable-alias 'flymake-warning-re 'flymake-warning-predicate "24.4")
858 (defvar flymake-warning-predicate "^[wW]arning"
859 "Predicate matching against error text to detect a warning.
860 Takes a single argument, the error's text and should return non-nil
861 if it's a warning.
862 Instead of a function, it can also be a regular expression.")
864 (defun flymake-parse-line (line)
865 "Parse LINE to see if it is an error or warning.
866 Return its components if so, nil otherwise."
867 (let ((raw-file-name nil)
868 (line-no 0)
869 (err-type "e")
870 (err-text nil)
871 (patterns flymake-err-line-patterns)
872 (matched nil))
873 (while (and patterns (not matched))
874 (when (string-match (car (car patterns)) line)
875 (let* ((file-idx (nth 1 (car patterns)))
876 (line-idx (nth 2 (car patterns))))
878 (setq raw-file-name (if file-idx (match-string file-idx line) nil))
879 (setq line-no (if line-idx (string-to-number
880 (match-string line-idx line)) 0))
881 (setq err-text (if (> (length (car patterns)) 4)
882 (match-string (nth 4 (car patterns)) line)
883 (flymake-patch-err-text
884 (substring line (match-end 0)))))
885 (if (null err-text)
886 (setq err-text "<no error text>")
887 (when (cond ((stringp flymake-warning-predicate)
888 (string-match flymake-warning-predicate err-text))
889 ((functionp flymake-warning-predicate)
890 (funcall flymake-warning-predicate err-text)))
891 (setq err-type "w")))
892 (flymake-log
893 3 "parse line: file-idx=%s line-idx=%s file=%s line=%s text=%s"
894 file-idx line-idx raw-file-name line-no err-text)
895 (setq matched t)))
896 (setq patterns (cdr patterns)))
897 (if matched
898 (flymake-ler-make-ler raw-file-name line-no err-type err-text)
899 ())))
901 (defun flymake-find-err-info (err-info-list line-no)
902 "Find (line-err-info-list pos) for specified LINE-NO."
903 (if err-info-list
904 (let* ((line-err-info-list nil)
905 (pos 0)
906 (count (length err-info-list)))
908 (while (and (< pos count) (< (car (nth pos err-info-list)) line-no))
909 (setq pos (1+ pos)))
910 (when (and (< pos count) (equal (car (nth pos err-info-list)) line-no))
911 (setq line-err-info-list (flymake-er-get-line-err-info-list (nth pos err-info-list))))
912 (list line-err-info-list pos))
913 '(nil 0)))
915 (defun flymake-line-err-info-is-less-or-equal (line-one line-two)
916 (or (string< (flymake-ler-type line-one) (flymake-ler-type line-two))
917 (and (string= (flymake-ler-type line-one) (flymake-ler-type line-two))
918 (not (flymake-ler-file line-one)) (flymake-ler-file line-two))
919 (and (string= (flymake-ler-type line-one) (flymake-ler-type line-two))
920 (or (and (flymake-ler-file line-one) (flymake-ler-file line-two))
921 (and (not (flymake-ler-file line-one)) (not (flymake-ler-file line-two)))))))
923 (defun flymake-add-line-err-info (line-err-info-list line-err-info)
924 "Update LINE-ERR-INFO-LIST with the error LINE-ERR-INFO.
925 For the format of LINE-ERR-INFO, see `flymake-ler-make-ler'.
926 The new element is inserted in the proper position, according to
927 the predicate `flymake-line-err-info-is-less-or-equal'.
928 The updated value of LINE-ERR-INFO-LIST is returned."
929 (if (not line-err-info-list)
930 (list line-err-info)
931 (let* ((count (length line-err-info-list))
932 (idx 0))
933 (while (and (< idx count) (flymake-line-err-info-is-less-or-equal (nth idx line-err-info-list) line-err-info))
934 (setq idx (1+ idx)))
935 (cond ((equal 0 idx) (setq line-err-info-list (cons line-err-info line-err-info-list)))
936 (t (setq line-err-info-list (flymake-ins-after line-err-info-list (1- idx) line-err-info))))
937 line-err-info-list)))
939 (defun flymake-add-err-info (err-info-list line-err-info)
940 "Update ERR-INFO-LIST with the error LINE-ERR-INFO, preserving sort order.
941 Returns the updated value of ERR-INFO-LIST.
942 For the format of ERR-INFO-LIST, see `flymake-err-info'.
943 For the format of LINE-ERR-INFO, see `flymake-ler-make-ler'."
944 (let* ((line-no (if (flymake-ler-file line-err-info) 1 (flymake-ler-line line-err-info)))
945 (info-and-pos (flymake-find-err-info err-info-list line-no))
946 (exists (car info-and-pos))
947 (pos (nth 1 info-and-pos))
948 (line-err-info-list nil)
949 (err-info nil))
951 (if exists
952 (setq line-err-info-list (flymake-er-get-line-err-info-list (car (nthcdr pos err-info-list)))))
953 (setq line-err-info-list (flymake-add-line-err-info line-err-info-list line-err-info))
955 (setq err-info (flymake-er-make-er line-no line-err-info-list))
956 (cond (exists (setq err-info-list (flymake-set-at err-info-list pos err-info)))
957 ((equal 0 pos) (setq err-info-list (cons err-info err-info-list)))
958 (t (setq err-info-list (flymake-ins-after err-info-list (1- pos) err-info))))
959 err-info-list))
961 (defun flymake-get-project-include-dirs-imp (basedir)
962 "Include dirs for the project current file belongs to."
963 (if (flymake-get-project-include-dirs-from-cache basedir)
964 (progn
965 (flymake-get-project-include-dirs-from-cache basedir))
966 ;;else
967 (let* ((command-line (concat "make -C "
968 (shell-quote-argument basedir)
969 " DUMPVARS=INCLUDE_DIRS dumpvars"))
970 (output (shell-command-to-string command-line))
971 (lines (split-string output "\n" t))
972 (count (length lines))
973 (idx 0)
974 (inc-dirs nil))
975 (while (and (< idx count) (not (string-match "^INCLUDE_DIRS=.*" (nth idx lines))))
976 (setq idx (1+ idx)))
977 (when (< idx count)
978 (let* ((inc-lines (split-string (nth idx lines) " *-I" t))
979 (inc-count (length inc-lines)))
980 (while (> inc-count 0)
981 (when (not (string-match "^INCLUDE_DIRS=.*" (nth (1- inc-count) inc-lines)))
982 (push (replace-regexp-in-string "\"" "" (nth (1- inc-count) inc-lines)) inc-dirs))
983 (setq inc-count (1- inc-count)))))
984 (flymake-add-project-include-dirs-to-cache basedir inc-dirs)
985 inc-dirs)))
987 (defvar flymake-get-project-include-dirs-function #'flymake-get-project-include-dirs-imp
988 "Function used to get project include dirs, one parameter: basedir name.")
990 (defun flymake-get-project-include-dirs (basedir)
991 (funcall flymake-get-project-include-dirs-function basedir))
993 (defun flymake-get-system-include-dirs ()
994 "System include dirs - from the `INCLUDE' env setting."
995 (let* ((includes (getenv "INCLUDE")))
996 (if includes (split-string includes path-separator t) nil)))
998 (defvar flymake-project-include-dirs-cache (make-hash-table :test #'equal))
1000 (defun flymake-get-project-include-dirs-from-cache (base-dir)
1001 (gethash base-dir flymake-project-include-dirs-cache))
1003 (defun flymake-add-project-include-dirs-to-cache (base-dir include-dirs)
1004 (puthash base-dir include-dirs flymake-project-include-dirs-cache))
1006 (defun flymake-clear-project-include-dirs-cache ()
1007 (clrhash flymake-project-include-dirs-cache))
1009 (defun flymake-get-include-dirs (base-dir)
1010 "Get dirs to use when resolving local file names."
1011 (let* ((include-dirs (append '(".") (flymake-get-project-include-dirs base-dir) (flymake-get-system-include-dirs))))
1012 include-dirs))
1014 ;; (defun flymake-restore-formatting ()
1015 ;; "Remove any formatting made by flymake."
1016 ;; )
1018 ;; (defun flymake-get-program-dir (buffer)
1019 ;; "Get dir to start program in."
1020 ;; (unless (bufferp buffer)
1021 ;; (error "Invalid buffer"))
1022 ;; (with-current-buffer buffer
1023 ;; default-directory))
1025 (defun flymake-safe-delete-file (file-name)
1026 (when (and file-name (file-exists-p file-name))
1027 (delete-file file-name)
1028 (flymake-log 1 "deleted file %s" file-name)))
1030 (defun flymake-safe-delete-directory (dir-name)
1031 (condition-case nil
1032 (progn
1033 (delete-directory dir-name)
1034 (flymake-log 1 "deleted dir %s" dir-name))
1035 (error
1036 (flymake-log 1 "Failed to delete dir %s, error ignored" dir-name))))
1038 (defun flymake-start-syntax-check ()
1039 "Start syntax checking for current buffer."
1040 (interactive)
1041 (flymake-log 3 "flymake is running: %s" flymake-is-running)
1042 (when (and (not flymake-is-running)
1043 (flymake-can-syntax-check-file buffer-file-name))
1044 (when (or (not flymake-compilation-prevents-syntax-check)
1045 (not (flymake-compilation-is-running))) ;+ (flymake-rep-ort-status buffer "COMP")
1046 (flymake-clear-buildfile-cache)
1047 (flymake-clear-project-include-dirs-cache)
1049 (setq flymake-check-was-interrupted nil)
1051 (let* ((source-file-name buffer-file-name)
1052 (init-f (flymake-get-init-function source-file-name))
1053 (cleanup-f (flymake-get-cleanup-function source-file-name))
1054 (cmd-and-args (funcall init-f))
1055 (cmd (nth 0 cmd-and-args))
1056 (args (nth 1 cmd-and-args))
1057 (dir (nth 2 cmd-and-args)))
1058 (if (not cmd-and-args)
1059 (progn
1060 (flymake-log 0 "init function %s for %s failed, cleaning up" init-f source-file-name)
1061 (funcall cleanup-f))
1062 (progn
1063 (setq flymake-last-change-time nil)
1064 (flymake-start-syntax-check-process cmd args dir)))))))
1066 (defun flymake-start-syntax-check-process (cmd args dir)
1067 "Start syntax check process."
1068 (condition-case err
1069 (let* ((process
1070 (let ((default-directory (or dir default-directory)))
1071 (when dir
1072 (flymake-log 3 "starting process on dir %s" dir))
1073 (apply 'start-file-process
1074 "flymake-proc" (current-buffer) cmd args))))
1075 (set-process-sentinel process 'flymake-process-sentinel)
1076 (set-process-filter process 'flymake-process-filter)
1077 (set-process-query-on-exit-flag process nil)
1078 (push process flymake-processes)
1080 (setq flymake-is-running t)
1081 (setq flymake-last-change-time nil)
1082 (setq flymake-check-start-time (float-time))
1084 (flymake-report-status nil "*")
1085 (flymake-log 2 "started process %d, command=%s, dir=%s"
1086 (process-id process) (process-command process)
1087 default-directory)
1088 process)
1089 (error
1090 (let* ((err-str
1091 (format-message
1092 "Failed to launch syntax check process `%s' with args %s: %s"
1093 cmd args (error-message-string err)))
1094 (source-file-name buffer-file-name)
1095 (cleanup-f (flymake-get-cleanup-function source-file-name)))
1096 (flymake-log 0 err-str)
1097 (funcall cleanup-f)
1098 (flymake-report-fatal-status "PROCERR" err-str)))))
1100 (defun flymake-kill-process (proc)
1101 "Kill process PROC."
1102 (kill-process proc)
1103 (let* ((buf (process-buffer proc)))
1104 (when (buffer-live-p buf)
1105 (with-current-buffer buf
1106 (setq flymake-check-was-interrupted t))))
1107 (flymake-log 1 "killed process %d" (process-id proc)))
1109 (defun flymake-stop-all-syntax-checks ()
1110 "Kill all syntax check processes."
1111 (interactive)
1112 (while flymake-processes
1113 (flymake-kill-process (pop flymake-processes))))
1115 (defun flymake-compilation-is-running ()
1116 (and (boundp 'compilation-in-progress)
1117 compilation-in-progress))
1119 (defun flymake-compile ()
1120 "Kill all flymake syntax checks, start compilation."
1121 (interactive)
1122 (flymake-stop-all-syntax-checks)
1123 (call-interactively 'compile))
1125 (defun flymake-on-timer-event (buffer)
1126 "Start a syntax check for buffer BUFFER if necessary."
1127 (when (buffer-live-p buffer)
1128 (with-current-buffer buffer
1129 (when (and (not flymake-is-running)
1130 flymake-last-change-time
1131 (> (- (float-time) flymake-last-change-time)
1132 flymake-no-changes-timeout))
1134 (setq flymake-last-change-time nil)
1135 (flymake-log 3 "starting syntax check as more than 1 second passed since last change")
1136 (flymake-start-syntax-check)))))
1138 (define-obsolete-function-alias 'flymake-display-err-menu-for-current-line
1139 'flymake-popup-current-error-menu "24.4")
1141 (defun flymake-popup-current-error-menu (&optional event)
1142 "Pop up a menu with errors/warnings for current line."
1143 (interactive (list last-nonmenu-event))
1144 (let* ((line-no (line-number-at-pos))
1145 (errors (or (car (flymake-find-err-info flymake-err-info line-no))
1146 (user-error "No errors for current line")))
1147 (menu (mapcar (lambda (x)
1148 (if (flymake-ler-file x)
1149 (cons (format "%s - %s(%d)"
1150 (flymake-ler-text x)
1151 (flymake-ler-file x)
1152 (flymake-ler-line x))
1154 (list (flymake-ler-text x))))
1155 errors))
1156 (event (if (mouse-event-p event)
1157 event
1158 (list 'mouse-1 (posn-at-point))))
1159 (title (format "Line %d: %d error(s), %d warning(s)"
1160 line-no
1161 (flymake-get-line-err-count errors "e")
1162 (flymake-get-line-err-count errors "w")))
1163 (choice (x-popup-menu event (list title (cons "" menu)))))
1164 (flymake-log 3 "choice=%s" choice)
1165 (when choice
1166 (flymake-goto-file-and-line (flymake-ler-full-file choice)
1167 (flymake-ler-line choice)))))
1169 (defun flymake-goto-file-and-line (file line)
1170 "Try to get buffer for FILE and goto line LINE in it."
1171 (if (not (file-exists-p file))
1172 (flymake-log 1 "File %s does not exist" file)
1173 (find-file file)
1174 (goto-char (point-min))
1175 (forward-line (1- line))))
1177 ;; flymake minor mode declarations
1178 (defvar-local flymake-mode-line nil)
1179 (defvar-local flymake-mode-line-e-w nil)
1180 (defvar-local flymake-mode-line-status nil)
1182 (defun flymake-report-status (e-w &optional status)
1183 "Show status in mode line."
1184 (when e-w
1185 (setq flymake-mode-line-e-w e-w))
1186 (when status
1187 (setq flymake-mode-line-status status))
1188 (let* ((mode-line " Flymake"))
1189 (when (> (length flymake-mode-line-e-w) 0)
1190 (setq mode-line (concat mode-line ":" flymake-mode-line-e-w)))
1191 (setq mode-line (concat mode-line flymake-mode-line-status))
1192 (setq flymake-mode-line mode-line)
1193 (force-mode-line-update)))
1195 ;; Nothing in flymake uses this at all any more, so this is just for
1196 ;; third-party compatibility.
1197 (define-obsolete-function-alias 'flymake-display-warning 'message-box "26.1")
1199 (defun flymake-report-fatal-status (status warning)
1200 "Display a warning and switch flymake mode off."
1201 ;; This first message was always shown by default, and flymake-log
1202 ;; does nothing by default, hence the use of message.
1203 ;; Another option is display-warning.
1204 (if (< flymake-log-level 0)
1205 (message "Flymake: %s. Flymake will be switched OFF" warning))
1206 (flymake-mode 0)
1207 (flymake-log 0 "switched OFF Flymake mode for buffer %s due to fatal status %s, warning %s"
1208 (buffer-name) status warning))
1210 ;;;###autoload
1211 (define-minor-mode flymake-mode nil
1212 :group 'flymake :lighter flymake-mode-line
1213 (cond
1215 ;; Turning the mode ON.
1216 (flymake-mode
1217 (cond
1218 ((not buffer-file-name)
1219 (message "Flymake unable to run without a buffer file name"))
1220 ((not (flymake-can-syntax-check-file buffer-file-name))
1221 (flymake-log 2 "flymake cannot check syntax in buffer %s" (buffer-name)))
1223 (add-hook 'after-change-functions 'flymake-after-change-function nil t)
1224 (add-hook 'after-save-hook 'flymake-after-save-hook nil t)
1225 (add-hook 'kill-buffer-hook 'flymake-kill-buffer-hook nil t)
1226 ;;+(add-hook 'find-file-hook 'flymake-find-file-hook)
1228 (flymake-report-status "" "")
1230 (setq flymake-timer
1231 (run-at-time nil 1 'flymake-on-timer-event (current-buffer)))
1233 (when (and flymake-start-syntax-check-on-find-file
1234 ;; Since we write temp files in current dir, there's no point
1235 ;; trying if the directory is read-only (bug#8954).
1236 (file-writable-p (file-name-directory buffer-file-name)))
1237 (with-demoted-errors
1238 (flymake-start-syntax-check))))))
1240 ;; Turning the mode OFF.
1242 (remove-hook 'after-change-functions 'flymake-after-change-function t)
1243 (remove-hook 'after-save-hook 'flymake-after-save-hook t)
1244 (remove-hook 'kill-buffer-hook 'flymake-kill-buffer-hook t)
1245 ;;+(remove-hook 'find-file-hook (function flymake-find-file-hook) t)
1247 (flymake-delete-own-overlays)
1249 (when flymake-timer
1250 (cancel-timer flymake-timer)
1251 (setq flymake-timer nil))
1253 (setq flymake-is-running nil))))
1255 ;;;###autoload
1256 (defun flymake-mode-on ()
1257 "Turn flymake mode on."
1258 (flymake-mode 1)
1259 (flymake-log 1 "flymake mode turned ON for buffer %s" (buffer-name)))
1261 ;;;###autoload
1262 (defun flymake-mode-off ()
1263 "Turn flymake mode off."
1264 (flymake-mode 0)
1265 (flymake-log 1 "flymake mode turned OFF for buffer %s" (buffer-name)))
1267 (defun flymake-after-change-function (start stop _len)
1268 "Start syntax check for current buffer if it isn't already running."
1269 ;;+(flymake-log 0 "setting change time to %s" (float-time))
1270 (let((new-text (buffer-substring start stop)))
1271 (when (and flymake-start-syntax-check-on-newline (equal new-text "\n"))
1272 (flymake-log 3 "starting syntax check as new-line has been seen")
1273 (flymake-start-syntax-check))
1274 (setq flymake-last-change-time (float-time))))
1276 (defun flymake-after-save-hook ()
1277 (if (local-variable-p 'flymake-mode (current-buffer)) ; (???) other way to determine whether flymake is active in buffer being saved?
1278 (progn
1279 (flymake-log 3 "starting syntax check as buffer was saved")
1280 (flymake-start-syntax-check)))) ; no more mode 3. cannot start check if mode 3 (to temp copies) is active - (???)
1282 (defun flymake-kill-buffer-hook ()
1283 (when flymake-timer
1284 (cancel-timer flymake-timer)
1285 (setq flymake-timer nil)))
1287 ;;;###autoload
1288 (defun flymake-find-file-hook ()
1289 ;;+(when flymake-start-syntax-check-on-find-file
1290 ;;+ (flymake-log 3 "starting syntax check on file open")
1291 ;;+ (flymake-start-syntax-check)
1292 ;;+)
1293 (when (and (not (local-variable-p 'flymake-mode (current-buffer)))
1294 (flymake-can-syntax-check-file buffer-file-name))
1295 (flymake-mode)
1296 (flymake-log 3 "automatically turned ON flymake mode")))
1298 (defun flymake-get-first-err-line-no (err-info-list)
1299 "Return first line with error."
1300 (when err-info-list
1301 (flymake-er-get-line (car err-info-list))))
1303 (defun flymake-get-last-err-line-no (err-info-list)
1304 "Return last line with error."
1305 (when err-info-list
1306 (flymake-er-get-line (nth (1- (length err-info-list)) err-info-list))))
1308 (defun flymake-get-next-err-line-no (err-info-list line-no)
1309 "Return next line with error."
1310 (when err-info-list
1311 (let* ((count (length err-info-list))
1312 (idx 0))
1313 (while (and (< idx count) (>= line-no (flymake-er-get-line (nth idx err-info-list))))
1314 (setq idx (1+ idx)))
1315 (if (< idx count)
1316 (flymake-er-get-line (nth idx err-info-list))))))
1318 (defun flymake-get-prev-err-line-no (err-info-list line-no)
1319 "Return previous line with error."
1320 (when err-info-list
1321 (let* ((count (length err-info-list)))
1322 (while (and (> count 0) (<= line-no (flymake-er-get-line (nth (1- count) err-info-list))))
1323 (setq count (1- count)))
1324 (if (> count 0)
1325 (flymake-er-get-line (nth (1- count) err-info-list))))))
1327 (defun flymake-skip-whitespace ()
1328 "Move forward until non-whitespace is reached."
1329 (while (looking-at "[ \t]")
1330 (forward-char)))
1332 (defun flymake-goto-line (line-no)
1333 "Go to line LINE-NO, then skip whitespace."
1334 (goto-char (point-min))
1335 (forward-line (1- line-no))
1336 (flymake-skip-whitespace))
1338 (defun flymake-goto-next-error ()
1339 "Go to next error in err ring."
1340 (interactive)
1341 (let ((line-no (flymake-get-next-err-line-no flymake-err-info (line-number-at-pos))))
1342 (when (not line-no)
1343 (setq line-no (flymake-get-first-err-line-no flymake-err-info))
1344 (flymake-log 1 "passed end of file"))
1345 (if line-no
1346 (flymake-goto-line line-no)
1347 (flymake-log 1 "no errors in current buffer"))))
1349 (defun flymake-goto-prev-error ()
1350 "Go to previous error in err ring."
1351 (interactive)
1352 (let ((line-no (flymake-get-prev-err-line-no flymake-err-info (line-number-at-pos))))
1353 (when (not line-no)
1354 (setq line-no (flymake-get-last-err-line-no flymake-err-info))
1355 (flymake-log 1 "passed beginning of file"))
1356 (if line-no
1357 (flymake-goto-line line-no)
1358 (flymake-log 1 "no errors in current buffer"))))
1360 (defun flymake-patch-err-text (string)
1361 (if (string-match "^[\n\t :0-9]*\\(.*\\)$" string)
1362 (match-string 1 string)
1363 string))
1365 ;;;; general init-cleanup and helper routines
1366 (defun flymake-create-temp-inplace (file-name prefix)
1367 (unless (stringp file-name)
1368 (error "Invalid file-name"))
1369 (or prefix
1370 (setq prefix "flymake"))
1371 (let* ((ext (file-name-extension file-name))
1372 (temp-name (file-truename
1373 (concat (file-name-sans-extension file-name)
1374 "_" prefix
1375 (and ext (concat "." ext))))))
1376 (flymake-log 3 "create-temp-inplace: file=%s temp=%s" file-name temp-name)
1377 temp-name))
1379 (defun flymake-create-temp-with-folder-structure (file-name _prefix)
1380 (unless (stringp file-name)
1381 (error "Invalid file-name"))
1383 (let* ((dir (file-name-directory file-name))
1384 ;; Not sure what this slash-pos is all about, but I guess it's just
1385 ;; trying to remove the leading / of absolute file names.
1386 (slash-pos (string-match "/" dir))
1387 (temp-dir (expand-file-name (substring dir (1+ slash-pos))
1388 temporary-file-directory)))
1390 (file-truename (expand-file-name (file-name-nondirectory file-name)
1391 temp-dir))))
1393 (defun flymake-delete-temp-directory (dir-name)
1394 "Attempt to delete temp dir created by `flymake-create-temp-with-folder-structure', do not fail on error."
1395 (let* ((temp-dir temporary-file-directory)
1396 (suffix (substring dir-name (1+ (length temp-dir)))))
1398 (while (> (length suffix) 0)
1399 (setq suffix (directory-file-name suffix))
1400 ;;+(flymake-log 0 "suffix=%s" suffix)
1401 (flymake-safe-delete-directory
1402 (file-truename (expand-file-name suffix temp-dir)))
1403 (setq suffix (file-name-directory suffix)))))
1405 (defvar-local flymake-temp-source-file-name nil)
1406 (defvar-local flymake-master-file-name nil)
1407 (defvar-local flymake-temp-master-file-name nil)
1408 (defvar-local flymake-base-dir nil)
1410 (defun flymake-init-create-temp-buffer-copy (create-temp-f)
1411 "Make a temporary copy of the current buffer, save its name in buffer data and return the name."
1412 (let* ((source-file-name buffer-file-name)
1413 (temp-source-file-name (funcall create-temp-f source-file-name "flymake")))
1415 (flymake-save-buffer-in-file temp-source-file-name)
1416 (setq flymake-temp-source-file-name temp-source-file-name)
1417 temp-source-file-name))
1419 (defun flymake-simple-cleanup ()
1420 "Do cleanup after `flymake-init-create-temp-buffer-copy'.
1421 Delete temp file."
1422 (flymake-safe-delete-file flymake-temp-source-file-name)
1423 (setq flymake-last-change-time nil))
1425 (defun flymake-get-real-file-name (file-name-from-err-msg)
1426 "Translate file name from error message to \"real\" file name.
1427 Return full-name. Names are real, not patched."
1428 (let* ((real-name nil)
1429 (source-file-name buffer-file-name)
1430 (master-file-name flymake-master-file-name)
1431 (temp-source-file-name flymake-temp-source-file-name)
1432 (temp-master-file-name flymake-temp-master-file-name)
1433 (base-dirs
1434 (list flymake-base-dir
1435 (file-name-directory source-file-name)
1436 (if master-file-name (file-name-directory master-file-name))))
1437 (files (list (list source-file-name source-file-name)
1438 (list temp-source-file-name source-file-name)
1439 (list master-file-name master-file-name)
1440 (list temp-master-file-name master-file-name))))
1442 (when (equal 0 (length file-name-from-err-msg))
1443 (setq file-name-from-err-msg source-file-name))
1445 (setq real-name (flymake-get-full-patched-file-name file-name-from-err-msg base-dirs files))
1446 ;; if real-name is nil, than file name from err msg is none of the files we've patched
1447 (if (not real-name)
1448 (setq real-name (flymake-get-full-nonpatched-file-name file-name-from-err-msg base-dirs)))
1449 (if (not real-name)
1450 (setq real-name file-name-from-err-msg))
1451 (setq real-name (flymake-fix-file-name real-name))
1452 (flymake-log 3 "get-real-file-name: file-name=%s real-name=%s" file-name-from-err-msg real-name)
1453 real-name))
1455 (defun flymake-get-full-patched-file-name (file-name-from-err-msg base-dirs files)
1456 (let* ((base-dirs-count (length base-dirs))
1457 (file-count (length files))
1458 (real-name nil))
1460 (while (and (not real-name) (> base-dirs-count 0))
1461 (setq file-count (length files))
1462 (while (and (not real-name) (> file-count 0))
1463 (let* ((this-dir (nth (1- base-dirs-count) base-dirs))
1464 (this-file (nth 0 (nth (1- file-count) files)))
1465 (this-real-name (nth 1 (nth (1- file-count) files))))
1466 ;;+(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)
1467 (when (and this-dir this-file (flymake-same-files
1468 (expand-file-name file-name-from-err-msg this-dir)
1469 this-file))
1470 (setq real-name this-real-name)))
1471 (setq file-count (1- file-count)))
1472 (setq base-dirs-count (1- base-dirs-count)))
1473 real-name))
1475 (defun flymake-get-full-nonpatched-file-name (file-name-from-err-msg base-dirs)
1476 (let* ((real-name nil))
1477 (if (file-name-absolute-p file-name-from-err-msg)
1478 (setq real-name file-name-from-err-msg)
1479 (let* ((base-dirs-count (length base-dirs)))
1480 (while (and (not real-name) (> base-dirs-count 0))
1481 (let* ((full-name (expand-file-name file-name-from-err-msg
1482 (nth (1- base-dirs-count) base-dirs))))
1483 (if (file-exists-p full-name)
1484 (setq real-name full-name))
1485 (setq base-dirs-count (1- base-dirs-count))))))
1486 real-name))
1488 (defun flymake-init-find-buildfile-dir (source-file-name buildfile-name)
1489 "Find buildfile, store its dir in buffer data and return its dir, if found."
1490 (let* ((buildfile-dir
1491 (flymake-find-buildfile buildfile-name
1492 (file-name-directory source-file-name))))
1493 (if buildfile-dir
1494 (setq flymake-base-dir buildfile-dir)
1495 (flymake-log 1 "no buildfile (%s) for %s" buildfile-name source-file-name)
1496 (flymake-report-fatal-status
1497 "NOMK" (format "No buildfile (%s) found for %s"
1498 buildfile-name source-file-name)))))
1500 (defun flymake-init-create-temp-source-and-master-buffer-copy (get-incl-dirs-f create-temp-f master-file-masks include-regexp)
1501 "Find master file (or buffer), create its copy along with a copy of the source file."
1502 (let* ((source-file-name buffer-file-name)
1503 (temp-source-file-name (flymake-init-create-temp-buffer-copy create-temp-f))
1504 (master-and-temp-master (flymake-create-master-file
1505 source-file-name temp-source-file-name
1506 get-incl-dirs-f create-temp-f
1507 master-file-masks include-regexp)))
1509 (if (not master-and-temp-master)
1510 (progn
1511 (flymake-log 1 "cannot find master file for %s" source-file-name)
1512 (flymake-report-status "!" "") ; NOMASTER
1513 nil)
1514 (setq flymake-master-file-name (nth 0 master-and-temp-master))
1515 (setq flymake-temp-master-file-name (nth 1 master-and-temp-master)))))
1517 (defun flymake-master-cleanup ()
1518 (flymake-simple-cleanup)
1519 (flymake-safe-delete-file flymake-temp-master-file-name))
1521 ;;;; make-specific init-cleanup routines
1522 (defun flymake-get-syntax-check-program-args (source-file-name base-dir use-relative-base-dir use-relative-source get-cmd-line-f)
1523 "Create a command line for syntax check using GET-CMD-LINE-F."
1524 (funcall get-cmd-line-f
1525 (if use-relative-source
1526 (file-relative-name source-file-name base-dir)
1527 source-file-name)
1528 (if use-relative-base-dir
1529 (file-relative-name base-dir
1530 (file-name-directory source-file-name))
1531 base-dir)))
1533 (defun flymake-get-make-cmdline (source base-dir)
1534 (list "make"
1535 (list "-s"
1536 "-C"
1537 base-dir
1538 (concat "CHK_SOURCES=" source)
1539 "SYNTAX_CHECK_MODE=1"
1540 "check-syntax")))
1542 (defun flymake-get-ant-cmdline (source base-dir)
1543 (list "ant"
1544 (list "-buildfile"
1545 (concat base-dir "/" "build.xml")
1546 (concat "-DCHK_SOURCES=" source)
1547 "check-syntax")))
1549 (defun flymake-simple-make-init-impl (create-temp-f use-relative-base-dir use-relative-source build-file-name get-cmdline-f)
1550 "Create syntax check command line for a directly checked source file.
1551 Use CREATE-TEMP-F for creating temp copy."
1552 (let* ((args nil)
1553 (source-file-name buffer-file-name)
1554 (buildfile-dir (flymake-init-find-buildfile-dir source-file-name build-file-name)))
1555 (if buildfile-dir
1556 (let* ((temp-source-file-name (flymake-init-create-temp-buffer-copy create-temp-f)))
1557 (setq args (flymake-get-syntax-check-program-args temp-source-file-name buildfile-dir
1558 use-relative-base-dir use-relative-source
1559 get-cmdline-f))))
1560 args))
1562 (defun flymake-simple-make-init ()
1563 (flymake-simple-make-init-impl 'flymake-create-temp-inplace t t "Makefile" 'flymake-get-make-cmdline))
1565 (defun flymake-master-make-init (get-incl-dirs-f master-file-masks include-regexp)
1566 "Create make command line for a source file checked via master file compilation."
1567 (let* ((make-args nil)
1568 (temp-master-file-name (flymake-init-create-temp-source-and-master-buffer-copy
1569 get-incl-dirs-f 'flymake-create-temp-inplace
1570 master-file-masks include-regexp)))
1571 (when temp-master-file-name
1572 (let* ((buildfile-dir (flymake-init-find-buildfile-dir temp-master-file-name "Makefile")))
1573 (if buildfile-dir
1574 (setq make-args (flymake-get-syntax-check-program-args
1575 temp-master-file-name buildfile-dir nil nil 'flymake-get-make-cmdline)))))
1576 make-args))
1578 (defun flymake-find-make-buildfile (source-dir)
1579 (flymake-find-buildfile "Makefile" source-dir))
1581 ;;;; .h/make specific
1582 (defun flymake-master-make-header-init ()
1583 (flymake-master-make-init
1584 'flymake-get-include-dirs
1585 '("\\.\\(?:c\\(?:pp\\|xx\\|\\+\\+\\)?\\|CC\\)\\'")
1586 "[ \t]*#[ \t]*include[ \t]*\"\\([[:word:]0-9/\\_.]*%s\\)\""))
1588 ;;;; .java/make specific
1589 (defun flymake-simple-make-java-init ()
1590 (flymake-simple-make-init-impl 'flymake-create-temp-with-folder-structure nil nil "Makefile" 'flymake-get-make-cmdline))
1592 (defun flymake-simple-ant-java-init ()
1593 (flymake-simple-make-init-impl 'flymake-create-temp-with-folder-structure nil nil "build.xml" 'flymake-get-ant-cmdline))
1595 (defun flymake-simple-java-cleanup ()
1596 "Cleanup after `flymake-simple-make-java-init' -- delete temp file and dirs."
1597 (flymake-safe-delete-file flymake-temp-source-file-name)
1598 (when flymake-temp-source-file-name
1599 (flymake-delete-temp-directory
1600 (file-name-directory flymake-temp-source-file-name))))
1602 ;;;; perl-specific init-cleanup routines
1603 (defun flymake-perl-init ()
1604 (let* ((temp-file (flymake-init-create-temp-buffer-copy
1605 'flymake-create-temp-inplace))
1606 (local-file (file-relative-name
1607 temp-file
1608 (file-name-directory buffer-file-name))))
1609 (list "perl" (list "-wc " local-file))))
1611 ;;;; php-specific init-cleanup routines
1612 (defun flymake-php-init ()
1613 (let* ((temp-file (flymake-init-create-temp-buffer-copy
1614 'flymake-create-temp-inplace))
1615 (local-file (file-relative-name
1616 temp-file
1617 (file-name-directory buffer-file-name))))
1618 (list "php" (list "-f" local-file "-l"))))
1620 ;;;; tex-specific init-cleanup routines
1621 (defun flymake-get-tex-args (file-name)
1622 ;;(list "latex" (list "-c-style-errors" file-name))
1623 (list "texify" (list "--pdf" "--tex-option=-c-style-errors" file-name)))
1625 (defun flymake-simple-tex-init ()
1626 (flymake-get-tex-args (flymake-init-create-temp-buffer-copy 'flymake-create-temp-inplace)))
1628 ;; Perhaps there should be a buffer-local variable flymake-master-file
1629 ;; that people can set to override this stuff. Could inherit from
1630 ;; the similar AUCTeX variable.
1631 (defun flymake-master-tex-init ()
1632 (let* ((temp-master-file-name (flymake-init-create-temp-source-and-master-buffer-copy
1633 'flymake-get-include-dirs-dot 'flymake-create-temp-inplace
1634 '("\\.tex\\'")
1635 "[ \t]*\\in\\(?:put\\|clude\\)[ \t]*{\\(.*%s\\)}")))
1636 (when temp-master-file-name
1637 (flymake-get-tex-args temp-master-file-name))))
1639 (defun flymake-get-include-dirs-dot (_base-dir)
1640 '("."))
1642 ;;;; xml-specific init-cleanup routines
1643 (defun flymake-xml-init ()
1644 (list flymake-xml-program
1645 (list "val" (flymake-init-create-temp-buffer-copy
1646 'flymake-create-temp-inplace))))
1648 (provide 'flymake)
1649 ;;; flymake.el ends here