* lisp/emacs-lisp/package.el: Revert async package transactions
[emacs.git] / lisp / progmodes / flymake.el
blob696ab5273b791c96f3866b2327dbaa6d305b87e4
1 ;;; flymake.el --- a universal on-the-fly syntax checker -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2003-2015 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)
106 (defcustom flymake-start-syntax-check-on-find-file t
107 "Start syntax check on find file."
108 :group 'flymake
109 :type 'boolean)
111 (defcustom flymake-log-level -1
112 "Logging level, only messages with level lower or equal will be logged.
113 -1 = NONE, 0 = ERROR, 1 = WARNING, 2 = INFO, 3 = DEBUG"
114 :group 'flymake
115 :type 'integer)
117 (defcustom flymake-xml-program
118 (if (executable-find "xmlstarlet") "xmlstarlet" "xml")
119 "Program to use for XML validation."
120 :type 'file
121 :group 'flymake
122 :version "24.4")
124 (defcustom flymake-master-file-dirs '("." "./src" "./UnitTest")
125 "Dirs where to look for master files."
126 :group 'flymake
127 :type '(repeat (string)))
129 (defcustom flymake-master-file-count-limit 32
130 "Max number of master files to check."
131 :group 'flymake
132 :type 'integer)
134 (defcustom flymake-allowed-file-name-masks
135 '(("\\.\\(?:c\\(?:pp\\|xx\\|\\+\\+\\)?\\|CC\\)\\'" flymake-simple-make-init)
136 ("\\.xml\\'" flymake-xml-init)
137 ("\\.html?\\'" flymake-xml-init)
138 ("\\.cs\\'" flymake-simple-make-init)
139 ("\\.p[ml]\\'" flymake-perl-init)
140 ("\\.php[345]?\\'" flymake-php-init)
141 ("\\.h\\'" flymake-master-make-header-init flymake-master-cleanup)
142 ("\\.java\\'" flymake-simple-make-java-init flymake-simple-java-cleanup)
143 ("[0-9]+\\.tex\\'" flymake-master-tex-init flymake-master-cleanup)
144 ("\\.tex\\'" flymake-simple-tex-init)
145 ("\\.idl\\'" flymake-simple-make-init)
146 ;; ("\\.cpp\\'" 1)
147 ;; ("\\.java\\'" 3)
148 ;; ("\\.h\\'" 2 ("\\.cpp\\'" "\\.c\\'")
149 ;; ("[ \t]*#[ \t]*include[ \t]*\"\\([\w0-9/\\_\.]*[/\\]*\\)\\(%s\\)\"" 1 2))
150 ;; ("\\.idl\\'" 1)
151 ;; ("\\.odl\\'" 1)
152 ;; ("[0-9]+\\.tex\\'" 2 ("\\.tex\\'")
153 ;; ("[ \t]*\\input[ \t]*{\\(.*\\)\\(%s\\)}" 1 2 ))
154 ;; ("\\.tex\\'" 1)
156 "Files syntax checking is allowed for.
157 This is an alist with elements of the form:
158 REGEXP INIT [CLEANUP [NAME]]
159 REGEXP is a regular expression that matches a file name.
160 INIT is the init function to use.
161 CLEANUP is the cleanup function to use, default `flymake-simple-cleanup'.
162 NAME is the file name function to use, default `flymake-get-real-file-name'."
163 :group 'flymake
164 :type '(alist :key-type (regexp :tag "File regexp")
165 :value-type
166 (list :tag "Handler functions"
167 (function :tag "Init function")
168 (choice :tag "Cleanup function"
169 (const :tag "flymake-simple-cleanup" nil)
170 function)
171 (choice :tag "Name function"
172 (const :tag "flymake-get-real-file-name" nil)
173 function))))
175 (defvar-local flymake-is-running nil
176 "If t, flymake syntax check process is running for the current buffer.")
178 (defvar-local flymake-timer nil
179 "Timer for starting syntax check.")
181 (defvar-local flymake-last-change-time nil
182 "Time of last buffer change.")
184 (defvar-local flymake-check-start-time nil
185 "Time at which syntax check was started.")
187 (defvar-local flymake-check-was-interrupted nil
188 "Non-nil if syntax check was killed by `flymake-compile'.")
190 (defvar-local flymake-err-info nil
191 "Sorted list of line numbers and lists of err info in the form (file, err-text).")
193 (defvar-local flymake-new-err-info nil
194 "Same as `flymake-err-info', effective when a syntax check is in progress.")
196 (defun flymake-log (level text &rest args)
197 "Log a message at level LEVEL.
198 If LEVEL is higher than `flymake-log-level', the message is
199 ignored. Otherwise, it is printed using `message'.
200 TEXT is a format control string, and the remaining arguments ARGS
201 are the string substitutions (see the function `format')."
202 (if (<= level flymake-log-level)
203 (let* ((msg (apply 'format text args)))
204 (message "%s" msg))))
206 (defun flymake-ins-after (list pos val)
207 "Insert VAL into LIST after position POS.
208 POS counts from zero."
209 (let ((tmp (copy-sequence list)))
210 (setcdr (nthcdr pos tmp) (cons val (nthcdr (1+ pos) tmp)))
211 tmp))
213 (defun flymake-set-at (list pos val)
214 "Set VAL at position POS in LIST.
215 POS counts from zero."
216 (let ((tmp (copy-sequence list)))
217 (setcar (nthcdr pos tmp) val)
218 tmp))
220 (defvar flymake-processes nil
221 "List of currently active flymake processes.")
223 (defvar-local flymake-output-residual nil)
225 (defun flymake-get-file-name-mode-and-masks (file-name)
226 "Return the corresponding entry from `flymake-allowed-file-name-masks'."
227 (unless (stringp file-name)
228 (error "Invalid file-name"))
229 (let ((fnm flymake-allowed-file-name-masks)
230 (mode-and-masks nil))
231 (while (and (not mode-and-masks) fnm)
232 (if (string-match (car (car fnm)) file-name)
233 (setq mode-and-masks (cdr (car fnm))))
234 (setq fnm (cdr fnm)))
235 (flymake-log 3 "file %s, init=%s" file-name (car mode-and-masks))
236 mode-and-masks))
238 (defun flymake-can-syntax-check-file (file-name)
239 "Determine whether we can syntax check FILE-NAME.
240 Return nil if we cannot, non-nil if we can."
241 (if (flymake-get-init-function file-name) t nil))
243 (defun flymake-get-init-function (file-name)
244 "Return init function to be used for the file."
245 (let* ((init-f (nth 0 (flymake-get-file-name-mode-and-masks file-name))))
246 ;;(flymake-log 0 "calling %s" init-f)
247 ;;(funcall init-f (current-buffer))
248 init-f))
250 (defun flymake-get-cleanup-function (file-name)
251 "Return cleanup function to be used for the file."
252 (or (nth 1 (flymake-get-file-name-mode-and-masks file-name))
253 'flymake-simple-cleanup))
255 (defun flymake-get-real-file-name-function (file-name)
256 (or (nth 2 (flymake-get-file-name-mode-and-masks file-name))
257 'flymake-get-real-file-name))
259 (defvar flymake-find-buildfile-cache (make-hash-table :test #'equal))
261 (defun flymake-get-buildfile-from-cache (dir-name)
262 "Look up DIR-NAME in cache and return its associated value.
263 If DIR-NAME is not found, return nil."
264 (gethash dir-name flymake-find-buildfile-cache))
266 (defun flymake-add-buildfile-to-cache (dir-name buildfile)
267 "Associate DIR-NAME with BUILDFILE in the buildfile cache."
268 (puthash dir-name buildfile flymake-find-buildfile-cache))
270 (defun flymake-clear-buildfile-cache ()
271 "Clear the buildfile cache."
272 (clrhash flymake-find-buildfile-cache))
274 (defun flymake-find-buildfile (buildfile-name source-dir-name)
275 "Find buildfile starting from current directory.
276 Buildfile includes Makefile, build.xml etc.
277 Return its file name if found, or nil if not found."
278 (or (flymake-get-buildfile-from-cache source-dir-name)
279 (let* ((file (locate-dominating-file source-dir-name buildfile-name)))
280 (if file
281 (progn
282 (flymake-log 3 "found buildfile at %s" file)
283 (flymake-add-buildfile-to-cache source-dir-name file)
284 file)
285 (progn
286 (flymake-log 3 "buildfile for %s not found" source-dir-name)
287 nil)))))
289 (defun flymake-fix-file-name (name)
290 "Replace all occurrences of '\' with '/'."
291 (when name
292 (setq name (expand-file-name name))
293 (setq name (abbreviate-file-name name))
294 (setq name (directory-file-name name))
295 name))
297 (defun flymake-same-files (file-name-one file-name-two)
298 "Check if FILE-NAME-ONE and FILE-NAME-TWO point to same file.
299 Return t if so, nil if not."
300 (equal (flymake-fix-file-name file-name-one)
301 (flymake-fix-file-name file-name-two)))
303 ;; This is bound dynamically to pass a parameter to a sort predicate below
304 (defvar flymake-included-file-name)
306 (defun flymake-find-possible-master-files (file-name master-file-dirs masks)
307 "Find (by name and location) all possible master files.
309 Name is specified by FILE-NAME and location is specified by
310 MASTER-FILE-DIRS. Master files include .cpp and .c for .h.
311 Files are searched for starting from the .h directory and max
312 max-level parent dirs. File contents are not checked."
313 (let* ((dirs master-file-dirs)
314 (files nil)
315 (done nil))
317 (while (and (not done) dirs)
318 (let* ((dir (expand-file-name (car dirs) (file-name-directory file-name)))
319 (masks masks))
320 (while (and (file-exists-p dir) (not done) masks)
321 (let* ((mask (car masks))
322 (dir-files (directory-files dir t mask)))
324 (flymake-log 3 "dir %s, %d file(s) for mask %s"
325 dir (length dir-files) mask)
326 (while (and (not done) dir-files)
327 (when (not (file-directory-p (car dir-files)))
328 (setq files (cons (car dir-files) files))
329 (when (>= (length files) flymake-master-file-count-limit)
330 (flymake-log 3 "master file count limit (%d) reached" flymake-master-file-count-limit)
331 (setq done t)))
332 (setq dir-files (cdr dir-files))))
333 (setq masks (cdr masks))))
334 (setq dirs (cdr dirs)))
335 (when files
336 (let ((flymake-included-file-name (file-name-nondirectory file-name)))
337 (setq files (sort files 'flymake-master-file-compare))))
338 (flymake-log 3 "found %d possible master file(s)" (length files))
339 files))
341 (defun flymake-master-file-compare (file-one file-two)
342 "Compare two files specified by FILE-ONE and FILE-TWO.
343 This function is used in sort to move most possible file names
344 to the beginning of the list (File.h -> File.cpp moved to top)."
345 (and (equal (file-name-sans-extension flymake-included-file-name)
346 (file-name-base file-one))
347 (not (equal file-one file-two))))
349 (defvar flymake-check-file-limit 8192
350 "Maximum number of chars to look at when checking possible master file.
351 Nil means search the entire file.")
353 (defun flymake-check-patch-master-file-buffer
354 (master-file-temp-buffer
355 master-file-name patched-master-file-name
356 source-file-name patched-source-file-name
357 include-dirs regexp)
358 "Check if MASTER-FILE-NAME is a master file for SOURCE-FILE-NAME.
359 If yes, patch a copy of MASTER-FILE-NAME to include PATCHED-SOURCE-FILE-NAME
360 instead of SOURCE-FILE-NAME.
362 For example, foo.cpp is a master file if it includes foo.h.
364 Whether a buffer for MATER-FILE-NAME exists, use it as a source
365 instead of reading master file from disk."
366 (let* ((source-file-nondir (file-name-nondirectory source-file-name))
367 (source-file-extension (file-name-extension source-file-nondir))
368 (source-file-nonext (file-name-sans-extension source-file-nondir))
369 (found nil)
370 (inc-name nil)
371 (search-limit flymake-check-file-limit))
372 (setq regexp
373 (format regexp ; "[ \t]*#[ \t]*include[ \t]*\"\\(.*%s\\)\""
374 ;; Hack for tex files, where \include often excludes .tex.
375 ;; Maybe this is safe generally.
376 (if (and (> (length source-file-extension) 1)
377 (string-equal source-file-extension "tex"))
378 (format "%s\\(?:\\.%s\\)?"
379 (regexp-quote source-file-nonext)
380 (regexp-quote source-file-extension))
381 (regexp-quote source-file-nondir))))
382 (unwind-protect
383 (with-current-buffer master-file-temp-buffer
384 (if (or (not search-limit)
385 (> search-limit (point-max)))
386 (setq search-limit (point-max)))
387 (flymake-log 3 "checking %s against regexp %s"
388 master-file-name regexp)
389 (goto-char (point-min))
390 (while (and (< (point) search-limit)
391 (re-search-forward regexp search-limit t))
392 (let ((match-beg (match-beginning 1))
393 (match-end (match-end 1)))
395 (flymake-log 3 "found possible match for %s" source-file-nondir)
396 (setq inc-name (match-string 1))
397 (and (> (length source-file-extension) 1)
398 (string-equal source-file-extension "tex")
399 (not (string-match (format "\\.%s\\'" source-file-extension)
400 inc-name))
401 (setq inc-name (concat inc-name "." source-file-extension)))
402 (when (eq t (compare-strings
403 source-file-nondir nil nil
404 inc-name (- (length inc-name)
405 (length source-file-nondir)) nil))
406 (flymake-log 3 "inc-name=%s" inc-name)
407 (when (flymake-check-include source-file-name inc-name
408 include-dirs)
409 (setq found t)
410 ;; replace-match is not used here as it fails in
411 ;; XEmacs with 'last match not a buffer' error as
412 ;; check-includes calls replace-in-string
413 (flymake-replace-region
414 match-beg match-end
415 (file-name-nondirectory patched-source-file-name))))
416 (forward-line 1)))
417 (when found
418 (flymake-save-buffer-in-file patched-master-file-name)))
419 ;;+(flymake-log 3 "killing buffer %s"
420 ;; (buffer-name master-file-temp-buffer))
421 (kill-buffer master-file-temp-buffer))
422 ;;+(flymake-log 3 "check-patch master file %s: %s" master-file-name found)
423 (when found
424 (flymake-log 2 "found master file %s" master-file-name))
425 found))
427 ;;; XXX: remove
428 (defun flymake-replace-region (beg end rep)
429 "Replace text in BUFFER in region (BEG END) with REP."
430 (save-excursion
431 (goto-char end)
432 ;; Insert before deleting, so as to better preserve markers's positions.
433 (insert rep)
434 (delete-region beg end)))
436 (defun flymake-read-file-to-temp-buffer (file-name)
437 "Insert contents of FILE-NAME into newly created temp buffer."
438 (let* ((temp-buffer (get-buffer-create (generate-new-buffer-name (concat "flymake:" (file-name-nondirectory file-name))))))
439 (with-current-buffer temp-buffer
440 (insert-file-contents file-name))
441 temp-buffer))
443 (defun flymake-copy-buffer-to-temp-buffer (buffer)
444 "Copy contents of BUFFER into newly created temp buffer."
445 (with-current-buffer
446 (get-buffer-create (generate-new-buffer-name
447 (concat "flymake:" (buffer-name buffer))))
448 (insert-buffer-substring buffer)
449 (current-buffer)))
451 (defun flymake-check-include (source-file-name inc-name include-dirs)
452 "Check if SOURCE-FILE-NAME can be found in include path.
453 Return t if it can be found via include path using INC-NAME."
454 (if (file-name-absolute-p inc-name)
455 (flymake-same-files source-file-name inc-name)
456 (while (and include-dirs
457 (not (flymake-same-files
458 source-file-name
459 (concat (file-name-directory source-file-name)
460 "/" (car include-dirs)
461 "/" inc-name))))
462 (setq include-dirs (cdr include-dirs)))
463 include-dirs))
465 (defun flymake-find-buffer-for-file (file-name)
466 "Check if there exists a buffer visiting FILE-NAME.
467 Return t if so, nil if not."
468 (let ((buffer-name (get-file-buffer file-name)))
469 (if buffer-name
470 (get-buffer buffer-name))))
472 (defun flymake-create-master-file (source-file-name patched-source-file-name get-incl-dirs-f create-temp-f masks include-regexp)
473 "Save SOURCE-FILE-NAME with a different name.
474 Find master file, patch and save it."
475 (let* ((possible-master-files (flymake-find-possible-master-files source-file-name flymake-master-file-dirs masks))
476 (master-file-count (length possible-master-files))
477 (idx 0)
478 (temp-buffer nil)
479 (master-file-name nil)
480 (patched-master-file-name nil)
481 (found nil))
483 (while (and (not found) (< idx master-file-count))
484 (setq master-file-name (nth idx possible-master-files))
485 (setq patched-master-file-name (funcall create-temp-f master-file-name "flymake_master"))
486 (if (flymake-find-buffer-for-file master-file-name)
487 (setq temp-buffer (flymake-copy-buffer-to-temp-buffer (flymake-find-buffer-for-file master-file-name)))
488 (setq temp-buffer (flymake-read-file-to-temp-buffer master-file-name)))
489 (setq found
490 (flymake-check-patch-master-file-buffer
491 temp-buffer
492 master-file-name
493 patched-master-file-name
494 source-file-name
495 patched-source-file-name
496 (funcall get-incl-dirs-f (file-name-directory master-file-name))
497 include-regexp))
498 (setq idx (1+ idx)))
499 (if found
500 (list master-file-name patched-master-file-name)
501 (progn
502 (flymake-log 3 "none of %d master file(s) checked includes %s" master-file-count
503 (file-name-nondirectory source-file-name))
504 nil))))
506 (defun flymake-save-buffer-in-file (file-name)
507 "Save the entire buffer contents into file FILE-NAME.
508 Create parent directories as needed."
509 (make-directory (file-name-directory file-name) 1)
510 (write-region nil nil file-name nil 566)
511 (flymake-log 3 "saved buffer %s in file %s" (buffer-name) file-name))
513 (defun flymake-process-filter (process output)
514 "Parse OUTPUT and highlight error lines.
515 It's flymake process filter."
516 (let ((source-buffer (process-buffer process)))
518 (flymake-log 3 "received %d byte(s) of output from process %d"
519 (length output) (process-id process))
520 (when (buffer-live-p source-buffer)
521 (with-current-buffer source-buffer
522 (flymake-parse-output-and-residual output)))))
524 (defun flymake-process-sentinel (process _event)
525 "Sentinel for syntax check buffers."
526 (when (memq (process-status process) '(signal exit))
527 (let* ((exit-status (process-exit-status process))
528 (command (process-command process))
529 (source-buffer (process-buffer process))
530 (cleanup-f (flymake-get-cleanup-function (buffer-file-name source-buffer))))
532 (flymake-log 2 "process %d exited with code %d"
533 (process-id process) exit-status)
534 (condition-case err
535 (progn
536 (flymake-log 3 "cleaning up using %s" cleanup-f)
537 (when (buffer-live-p source-buffer)
538 (with-current-buffer source-buffer
539 (funcall cleanup-f)))
541 (delete-process process)
542 (setq flymake-processes (delq process flymake-processes))
544 (when (buffer-live-p source-buffer)
545 (with-current-buffer source-buffer
547 (flymake-parse-residual)
548 (flymake-post-syntax-check exit-status command)
549 (setq flymake-is-running nil))))
550 (error
551 (let ((err-str (format "Error in process sentinel for buffer %s: %s"
552 source-buffer (error-message-string err))))
553 (flymake-log 0 err-str)
554 (with-current-buffer source-buffer
555 (setq flymake-is-running nil))))))))
557 (defun flymake-post-syntax-check (exit-status command)
558 (save-restriction
559 (widen)
560 (setq flymake-err-info flymake-new-err-info)
561 (setq flymake-new-err-info nil)
562 (setq flymake-err-info
563 (flymake-fix-line-numbers
564 flymake-err-info 1 (count-lines (point-min) (point-max))))
565 (flymake-delete-own-overlays)
566 (flymake-highlight-err-lines flymake-err-info)
567 (let (err-count warn-count)
568 (setq err-count (flymake-get-err-count flymake-err-info "e"))
569 (setq warn-count (flymake-get-err-count flymake-err-info "w"))
570 (flymake-log 2 "%s: %d error(s), %d warning(s) in %.2f second(s)"
571 (buffer-name) err-count warn-count
572 (- (float-time) flymake-check-start-time))
573 (setq flymake-check-start-time nil)
575 (if (and (equal 0 err-count) (equal 0 warn-count))
576 (if (equal 0 exit-status)
577 (flymake-report-status "" "") ; PASSED
578 (if (not flymake-check-was-interrupted)
579 (flymake-report-fatal-status "CFGERR"
580 (format "Configuration error has occurred while running %s" command))
581 (flymake-report-status nil ""))) ; "STOPPED"
582 (flymake-report-status (format "%d/%d" err-count warn-count) "")))))
584 (defun flymake-parse-output-and-residual (output)
585 "Split OUTPUT into lines, merge in residual if necessary."
586 (let* ((buffer-residual flymake-output-residual)
587 (total-output (if buffer-residual (concat buffer-residual output) output))
588 (lines-and-residual (flymake-split-output total-output))
589 (lines (nth 0 lines-and-residual))
590 (new-residual (nth 1 lines-and-residual)))
591 (setq flymake-output-residual new-residual)
592 (setq flymake-new-err-info
593 (flymake-parse-err-lines
594 flymake-new-err-info lines))))
596 (defun flymake-parse-residual ()
597 "Parse residual if it's non empty."
598 (when flymake-output-residual
599 (setq flymake-new-err-info
600 (flymake-parse-err-lines
601 flymake-new-err-info
602 (list flymake-output-residual)))
603 (setq flymake-output-residual nil)))
605 (defun flymake-er-make-er (line-no line-err-info-list)
606 (list line-no line-err-info-list))
608 (defun flymake-er-get-line (err-info)
609 (nth 0 err-info))
611 (defun flymake-er-get-line-err-info-list (err-info)
612 (nth 1 err-info))
614 (cl-defstruct (flymake-ler
615 (:constructor nil)
616 (:constructor flymake-ler-make-ler (file line type text &optional full-file)))
617 file line type text full-file)
619 (defun flymake-ler-set-file (line-err-info file)
620 (flymake-ler-make-ler file
621 (flymake-ler-line line-err-info)
622 (flymake-ler-type line-err-info)
623 (flymake-ler-text line-err-info)
624 (flymake-ler-full-file line-err-info)))
626 (defun flymake-ler-set-full-file (line-err-info full-file)
627 (flymake-ler-make-ler (flymake-ler-file line-err-info)
628 (flymake-ler-line line-err-info)
629 (flymake-ler-type line-err-info)
630 (flymake-ler-text line-err-info)
631 full-file))
633 (defun flymake-ler-set-line (line-err-info line)
634 (flymake-ler-make-ler (flymake-ler-file line-err-info)
635 line
636 (flymake-ler-type line-err-info)
637 (flymake-ler-text line-err-info)
638 (flymake-ler-full-file line-err-info)))
640 (defun flymake-get-line-err-count (line-err-info-list type)
641 "Return number of errors of specified TYPE.
642 Value of TYPE is either \"e\" or \"w\"."
643 (let* ((idx 0)
644 (count (length line-err-info-list))
645 (err-count 0))
647 (while (< idx count)
648 (when (equal type (flymake-ler-type (nth idx line-err-info-list)))
649 (setq err-count (1+ err-count)))
650 (setq idx (1+ idx)))
651 err-count))
653 (defun flymake-get-err-count (err-info-list type)
654 "Return number of errors of specified TYPE for ERR-INFO-LIST."
655 (let* ((idx 0)
656 (count (length err-info-list))
657 (err-count 0))
658 (while (< idx count)
659 (setq err-count (+ err-count (flymake-get-line-err-count (nth 1 (nth idx err-info-list)) type)))
660 (setq idx (1+ idx)))
661 err-count))
663 (defun flymake-fix-line-numbers (err-info-list min-line max-line)
664 "Replace line numbers with fixed value.
665 If line-numbers is less than MIN-LINE, set line numbers to MIN-LINE.
666 If line numbers is greater than MAX-LINE, set line numbers to MAX-LINE.
667 The reason for this fix is because some compilers might report
668 line number outside the file being compiled."
669 (let* ((count (length err-info-list))
670 (err-info nil)
671 (line 0))
672 (while (> count 0)
673 (setq err-info (nth (1- count) err-info-list))
674 (setq line (flymake-er-get-line err-info))
675 (when (or (< line min-line) (> line max-line))
676 (setq line (if (< line min-line) min-line max-line))
677 (setq err-info-list (flymake-set-at err-info-list (1- count)
678 (flymake-er-make-er line
679 (flymake-er-get-line-err-info-list err-info)))))
680 (setq count (1- count))))
681 err-info-list)
683 (defun flymake-highlight-err-lines (err-info-list)
684 "Highlight error lines in BUFFER using info from ERR-INFO-LIST."
685 (save-excursion
686 (dolist (err err-info-list)
687 (flymake-highlight-line (car err) (nth 1 err)))))
689 (defun flymake-overlay-p (ov)
690 "Determine whether overlay OV was created by flymake."
691 (and (overlayp ov) (overlay-get ov 'flymake-overlay)))
693 (defun flymake-make-overlay (beg end tooltip-text face bitmap)
694 "Allocate a flymake overlay in range BEG and END."
695 (when (not (flymake-region-has-flymake-overlays beg end))
696 (let ((ov (make-overlay beg end nil t))
697 (fringe (and flymake-fringe-indicator-position
698 (propertize "!" 'display
699 (cons flymake-fringe-indicator-position
700 (if (listp bitmap)
701 bitmap
702 (list bitmap)))))))
703 (overlay-put ov 'face face)
704 (overlay-put ov 'help-echo tooltip-text)
705 (overlay-put ov 'flymake-overlay t)
706 (overlay-put ov 'priority 100)
707 (overlay-put ov 'evaporate t)
708 (overlay-put ov 'before-string fringe)
709 ;;+(flymake-log 3 "created overlay %s" ov)
711 (flymake-log 3 "created an overlay at (%d-%d)" beg end)))
713 (defun flymake-delete-own-overlays ()
714 "Delete all flymake overlays in BUFFER."
715 (dolist (ol (overlays-in (point-min) (point-max)))
716 (when (flymake-overlay-p ol)
717 (delete-overlay ol)
718 ;;+(flymake-log 3 "deleted overlay %s" ol)
721 (defun flymake-region-has-flymake-overlays (beg end)
722 "Check if region specified by BEG and END has overlay.
723 Return t if it has at least one flymake overlay, nil if no overlay."
724 (let ((ov (overlays-in beg end))
725 (has-flymake-overlays nil))
726 (while (consp ov)
727 (when (flymake-overlay-p (car ov))
728 (setq has-flymake-overlays t))
729 (setq ov (cdr ov)))
730 has-flymake-overlays))
732 (defface flymake-errline
733 '((((supports :underline (:style wave)))
734 :underline (:style wave :color "Red1"))
736 :inherit error))
737 "Face used for marking error lines."
738 :version "24.4"
739 :group 'flymake)
741 (defface flymake-warnline
742 '((((supports :underline (:style wave)))
743 :underline (:style wave :color "DarkOrange"))
745 :inherit warning))
746 "Face used for marking warning lines."
747 :version "24.4"
748 :group 'flymake)
750 (defun flymake-highlight-line (line-no line-err-info-list)
751 "Highlight line LINE-NO in current buffer.
752 Perhaps use text from LINE-ERR-INFO-LIST to enhance highlighting."
753 (goto-char (point-min))
754 (forward-line (1- line-no))
755 (pcase-let* ((beg (progn (back-to-indentation) (point)))
756 (end (progn
757 (end-of-line)
758 (skip-chars-backward " \t\f\t\n" beg)
759 (if (eq (point) beg)
760 (line-beginning-position 2)
761 (point))))
762 (tooltip-text (mapconcat #'flymake-ler-text line-err-info-list "\n"))
763 (`(,face ,bitmap)
764 (if (> (flymake-get-line-err-count line-err-info-list "e") 0)
765 (list 'flymake-errline flymake-error-bitmap)
766 (list 'flymake-warnline flymake-warning-bitmap))))
767 (flymake-make-overlay beg end tooltip-text face bitmap)))
769 (defun flymake-parse-err-lines (err-info-list lines)
770 "Parse err LINES, store info in ERR-INFO-LIST."
771 (let* ((count (length lines))
772 (idx 0)
773 (line-err-info nil)
774 (real-file-name nil)
775 (source-file-name buffer-file-name)
776 (get-real-file-name-f (flymake-get-real-file-name-function source-file-name)))
778 (while (< idx count)
779 (setq line-err-info (flymake-parse-line (nth idx lines)))
780 (when line-err-info
781 (setq real-file-name (funcall get-real-file-name-f
782 (flymake-ler-file line-err-info)))
783 (setq line-err-info (flymake-ler-set-full-file line-err-info real-file-name))
785 (when (flymake-same-files real-file-name source-file-name)
786 (setq line-err-info (flymake-ler-set-file line-err-info nil))
787 (setq err-info-list (flymake-add-err-info err-info-list line-err-info))))
788 (flymake-log 3 "parsed '%s', %s line-err-info" (nth idx lines) (if line-err-info "got" "no"))
789 (setq idx (1+ idx)))
790 err-info-list))
792 (defun flymake-split-output (output)
793 "Split OUTPUT into lines.
794 Return last one as residual if it does not end with newline char.
795 Returns ((LINES) RESIDUAL)."
796 (when (and output (> (length output) 0))
797 (let* ((lines (split-string output "[\n\r]+" t))
798 (complete (equal "\n" (char-to-string (aref output (1- (length output))))))
799 (residual nil))
800 (when (not complete)
801 (setq residual (car (last lines)))
802 (setq lines (butlast lines)))
803 (list lines residual))))
805 (defun flymake-reformat-err-line-patterns-from-compile-el (original-list)
806 "Grab error line patterns from ORIGINAL-LIST in compile.el format.
807 Convert it to flymake internal format."
808 (let* ((converted-list '()))
809 (dolist (item original-list)
810 (setq item (cdr item))
811 (let ((regexp (nth 0 item))
812 (file (nth 1 item))
813 (line (nth 2 item))
814 (col (nth 3 item)))
815 (if (consp file) (setq file (car file)))
816 (if (consp line) (setq line (car line)))
817 (if (consp col) (setq col (car col)))
819 (when (not (functionp line))
820 (setq converted-list (cons (list regexp file line col) converted-list)))))
821 converted-list))
823 (require 'compile)
825 (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
826 (append
828 ;; MS Visual C++ 6.0
829 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\)) \: \\(\\(error\\|warning\\|fatal error\\) \\(C[0-9]+\\):[ \t\n]*\\(.+\\)\\)"
830 1 3 nil 4)
831 ;; jikes
832 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)\:\\([0-9]+\\)\:[0-9]+\:[0-9]+\:[0-9]+\: \\(\\(Error\\|Warning\\|Caution\\|Semantic Error\\):[ \t\n]*\\(.+\\)\\)"
833 1 3 nil 4)
834 ;; MS midl
835 ("midl[ ]*:[ ]*\\(command line error .*\\)"
836 nil nil nil 1)
837 ;; MS C#
838 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\),[0-9]+)\: \\(\\(error\\|warning\\|fatal error\\) \\(CS[0-9]+\\):[ \t\n]*\\(.+\\)\\)"
839 1 3 nil 4)
840 ;; perl
841 ("\\(.*\\) at \\([^ \n]+\\) line \\([0-9]+\\)[,.\n]" 2 3 nil 1)
842 ;; PHP
843 ("\\(?:Parse\\|Fatal\\) error: \\(.*\\) in \\(.*\\) on line \\([0-9]+\\)" 2 3 nil 1)
844 ;; LaTeX warnings (fileless) ("\\(LaTeX \\(Warning\\|Error\\): .*\\) on input line \\([0-9]+\\)" 20 3 nil 1)
845 ;; ant/javac. Note this also matches gcc warnings!
846 (" *\\(\\[javac\\] *\\)?\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)\:\\([0-9]+\\)\\(?:\:[0-9]+\\)?\:[ \t\n]*\\(.+\\)"
847 2 4 nil 5))
848 ;; compilation-error-regexp-alist)
849 (flymake-reformat-err-line-patterns-from-compile-el compilation-error-regexp-alist-alist))
850 "Patterns for matching error/warning lines. Each pattern has the form
851 \(REGEXP FILE-IDX LINE-IDX COL-IDX ERR-TEXT-IDX).
852 Use `flymake-reformat-err-line-patterns-from-compile-el' to add patterns
853 from compile.el")
855 (define-obsolete-variable-alias 'flymake-warning-re 'flymake-warning-predicate "24.4")
856 (defvar flymake-warning-predicate "^[wW]arning"
857 "Predicate matching against error text to detect a warning.
858 Takes a single argument, the error's text and should return non-nil
859 if it's a warning.
860 Instead of a function, it can also be a regular expression.")
862 (defun flymake-parse-line (line)
863 "Parse LINE to see if it is an error or warning.
864 Return its components if so, nil otherwise."
865 (let ((raw-file-name nil)
866 (line-no 0)
867 (err-type "e")
868 (err-text nil)
869 (patterns flymake-err-line-patterns)
870 (matched nil))
871 (while (and patterns (not matched))
872 (when (string-match (car (car patterns)) line)
873 (let* ((file-idx (nth 1 (car patterns)))
874 (line-idx (nth 2 (car patterns))))
876 (setq raw-file-name (if file-idx (match-string file-idx line) nil))
877 (setq line-no (if line-idx (string-to-number
878 (match-string line-idx line)) 0))
879 (setq err-text (if (> (length (car patterns)) 4)
880 (match-string (nth 4 (car patterns)) line)
881 (flymake-patch-err-text
882 (substring line (match-end 0)))))
883 (if (null err-text)
884 (setq err-text "<no error text>")
885 (when (cond ((stringp flymake-warning-predicate)
886 (string-match flymake-warning-predicate err-text))
887 ((functionp flymake-warning-predicate)
888 (funcall flymake-warning-predicate err-text)))
889 (setq err-type "w")))
890 (flymake-log
891 3 "parse line: file-idx=%s line-idx=%s file=%s line=%s text=%s"
892 file-idx line-idx raw-file-name line-no err-text)
893 (setq matched t)))
894 (setq patterns (cdr patterns)))
895 (if matched
896 (flymake-ler-make-ler raw-file-name line-no err-type err-text)
897 ())))
899 (defun flymake-find-err-info (err-info-list line-no)
900 "Find (line-err-info-list pos) for specified LINE-NO."
901 (if err-info-list
902 (let* ((line-err-info-list nil)
903 (pos 0)
904 (count (length err-info-list)))
906 (while (and (< pos count) (< (car (nth pos err-info-list)) line-no))
907 (setq pos (1+ pos)))
908 (when (and (< pos count) (equal (car (nth pos err-info-list)) line-no))
909 (setq line-err-info-list (flymake-er-get-line-err-info-list (nth pos err-info-list))))
910 (list line-err-info-list pos))
911 '(nil 0)))
913 (defun flymake-line-err-info-is-less-or-equal (line-one line-two)
914 (or (string< (flymake-ler-type line-one) (flymake-ler-type line-two))
915 (and (string= (flymake-ler-type line-one) (flymake-ler-type line-two))
916 (not (flymake-ler-file line-one)) (flymake-ler-file line-two))
917 (and (string= (flymake-ler-type line-one) (flymake-ler-type line-two))
918 (or (and (flymake-ler-file line-one) (flymake-ler-file line-two))
919 (and (not (flymake-ler-file line-one)) (not (flymake-ler-file line-two)))))))
921 (defun flymake-add-line-err-info (line-err-info-list line-err-info)
922 "Update LINE-ERR-INFO-LIST with the error LINE-ERR-INFO.
923 For the format of LINE-ERR-INFO, see `flymake-ler-make-ler'.
924 The new element is inserted in the proper position, according to
925 the predicate `flymake-line-err-info-is-less-or-equal'.
926 The updated value of LINE-ERR-INFO-LIST is returned."
927 (if (not line-err-info-list)
928 (list line-err-info)
929 (let* ((count (length line-err-info-list))
930 (idx 0))
931 (while (and (< idx count) (flymake-line-err-info-is-less-or-equal (nth idx line-err-info-list) line-err-info))
932 (setq idx (1+ idx)))
933 (cond ((equal 0 idx) (setq line-err-info-list (cons line-err-info line-err-info-list)))
934 (t (setq line-err-info-list (flymake-ins-after line-err-info-list (1- idx) line-err-info))))
935 line-err-info-list)))
937 (defun flymake-add-err-info (err-info-list line-err-info)
938 "Update ERR-INFO-LIST with the error LINE-ERR-INFO, preserving sort order.
939 Returns the updated value of ERR-INFO-LIST.
940 For the format of ERR-INFO-LIST, see `flymake-err-info'.
941 For the format of LINE-ERR-INFO, see `flymake-ler-make-ler'."
942 (let* ((line-no (if (flymake-ler-file line-err-info) 1 (flymake-ler-line line-err-info)))
943 (info-and-pos (flymake-find-err-info err-info-list line-no))
944 (exists (car info-and-pos))
945 (pos (nth 1 info-and-pos))
946 (line-err-info-list nil)
947 (err-info nil))
949 (if exists
950 (setq line-err-info-list (flymake-er-get-line-err-info-list (car (nthcdr pos err-info-list)))))
951 (setq line-err-info-list (flymake-add-line-err-info line-err-info-list line-err-info))
953 (setq err-info (flymake-er-make-er line-no line-err-info-list))
954 (cond (exists (setq err-info-list (flymake-set-at err-info-list pos err-info)))
955 ((equal 0 pos) (setq err-info-list (cons err-info err-info-list)))
956 (t (setq err-info-list (flymake-ins-after err-info-list (1- pos) err-info))))
957 err-info-list))
959 (defun flymake-get-project-include-dirs-imp (basedir)
960 "Include dirs for the project current file belongs to."
961 (if (flymake-get-project-include-dirs-from-cache basedir)
962 (progn
963 (flymake-get-project-include-dirs-from-cache basedir))
964 ;;else
965 (let* ((command-line (concat "make -C "
966 (shell-quote-argument basedir)
967 " DUMPVARS=INCLUDE_DIRS dumpvars"))
968 (output (shell-command-to-string command-line))
969 (lines (split-string output "\n" t))
970 (count (length lines))
971 (idx 0)
972 (inc-dirs nil))
973 (while (and (< idx count) (not (string-match "^INCLUDE_DIRS=.*" (nth idx lines))))
974 (setq idx (1+ idx)))
975 (when (< idx count)
976 (let* ((inc-lines (split-string (nth idx lines) " *-I" t))
977 (inc-count (length inc-lines)))
978 (while (> inc-count 0)
979 (when (not (string-match "^INCLUDE_DIRS=.*" (nth (1- inc-count) inc-lines)))
980 (push (replace-regexp-in-string "\"" "" (nth (1- inc-count) inc-lines)) inc-dirs))
981 (setq inc-count (1- inc-count)))))
982 (flymake-add-project-include-dirs-to-cache basedir inc-dirs)
983 inc-dirs)))
985 (defvar flymake-get-project-include-dirs-function #'flymake-get-project-include-dirs-imp
986 "Function used to get project include dirs, one parameter: basedir name.")
988 (defun flymake-get-project-include-dirs (basedir)
989 (funcall flymake-get-project-include-dirs-function basedir))
991 (defun flymake-get-system-include-dirs ()
992 "System include dirs - from the 'INCLUDE' env setting."
993 (let* ((includes (getenv "INCLUDE")))
994 (if includes (split-string includes path-separator t) nil)))
996 (defvar flymake-project-include-dirs-cache (make-hash-table :test #'equal))
998 (defun flymake-get-project-include-dirs-from-cache (base-dir)
999 (gethash base-dir flymake-project-include-dirs-cache))
1001 (defun flymake-add-project-include-dirs-to-cache (base-dir include-dirs)
1002 (puthash base-dir include-dirs flymake-project-include-dirs-cache))
1004 (defun flymake-clear-project-include-dirs-cache ()
1005 (clrhash flymake-project-include-dirs-cache))
1007 (defun flymake-get-include-dirs (base-dir)
1008 "Get dirs to use when resolving local file names."
1009 (let* ((include-dirs (append '(".") (flymake-get-project-include-dirs base-dir) (flymake-get-system-include-dirs))))
1010 include-dirs))
1012 ;; (defun flymake-restore-formatting ()
1013 ;; "Remove any formatting made by flymake."
1014 ;; )
1016 ;; (defun flymake-get-program-dir (buffer)
1017 ;; "Get dir to start program in."
1018 ;; (unless (bufferp buffer)
1019 ;; (error "Invalid buffer"))
1020 ;; (with-current-buffer buffer
1021 ;; default-directory))
1023 (defun flymake-safe-delete-file (file-name)
1024 (when (and file-name (file-exists-p file-name))
1025 (delete-file file-name)
1026 (flymake-log 1 "deleted file %s" file-name)))
1028 (defun flymake-safe-delete-directory (dir-name)
1029 (condition-case nil
1030 (progn
1031 (delete-directory dir-name)
1032 (flymake-log 1 "deleted dir %s" dir-name))
1033 (error
1034 (flymake-log 1 "Failed to delete dir %s, error ignored" dir-name))))
1036 (defun flymake-start-syntax-check ()
1037 "Start syntax checking for current buffer."
1038 (interactive)
1039 (flymake-log 3 "flymake is running: %s" flymake-is-running)
1040 (when (and (not flymake-is-running)
1041 (flymake-can-syntax-check-file buffer-file-name))
1042 (when (or (not flymake-compilation-prevents-syntax-check)
1043 (not (flymake-compilation-is-running))) ;+ (flymake-rep-ort-status buffer "COMP")
1044 (flymake-clear-buildfile-cache)
1045 (flymake-clear-project-include-dirs-cache)
1047 (setq flymake-check-was-interrupted nil)
1049 (let* ((source-file-name buffer-file-name)
1050 (init-f (flymake-get-init-function source-file-name))
1051 (cleanup-f (flymake-get-cleanup-function source-file-name))
1052 (cmd-and-args (funcall init-f))
1053 (cmd (nth 0 cmd-and-args))
1054 (args (nth 1 cmd-and-args))
1055 (dir (nth 2 cmd-and-args)))
1056 (if (not cmd-and-args)
1057 (progn
1058 (flymake-log 0 "init function %s for %s failed, cleaning up" init-f source-file-name)
1059 (funcall cleanup-f))
1060 (progn
1061 (setq flymake-last-change-time nil)
1062 (flymake-start-syntax-check-process cmd args dir)))))))
1064 (defun flymake-start-syntax-check-process (cmd args dir)
1065 "Start syntax check process."
1066 (condition-case err
1067 (let* ((process
1068 (let ((default-directory (or dir default-directory)))
1069 (when dir
1070 (flymake-log 3 "starting process on dir %s" dir))
1071 (apply 'start-file-process
1072 "flymake-proc" (current-buffer) cmd args))))
1073 (set-process-sentinel process 'flymake-process-sentinel)
1074 (set-process-filter process 'flymake-process-filter)
1075 (push process flymake-processes)
1077 (setq flymake-is-running t)
1078 (setq flymake-last-change-time nil)
1079 (setq flymake-check-start-time (float-time))
1081 (flymake-report-status nil "*")
1082 (flymake-log 2 "started process %d, command=%s, dir=%s"
1083 (process-id process) (process-command process)
1084 default-directory)
1085 process)
1086 (error
1087 (let* ((err-str (format "Failed to launch syntax check process '%s' with args %s: %s"
1088 cmd args (error-message-string err)))
1089 (source-file-name buffer-file-name)
1090 (cleanup-f (flymake-get-cleanup-function source-file-name)))
1091 (flymake-log 0 err-str)
1092 (funcall cleanup-f)
1093 (flymake-report-fatal-status "PROCERR" err-str)))))
1095 (defun flymake-kill-process (proc)
1096 "Kill process PROC."
1097 (kill-process proc)
1098 (let* ((buf (process-buffer proc)))
1099 (when (buffer-live-p buf)
1100 (with-current-buffer buf
1101 (setq flymake-check-was-interrupted t))))
1102 (flymake-log 1 "killed process %d" (process-id proc)))
1104 (defun flymake-stop-all-syntax-checks ()
1105 "Kill all syntax check processes."
1106 (interactive)
1107 (while flymake-processes
1108 (flymake-kill-process (pop flymake-processes))))
1110 (defun flymake-compilation-is-running ()
1111 (and (boundp 'compilation-in-progress)
1112 compilation-in-progress))
1114 (defun flymake-compile ()
1115 "Kill all flymake syntax checks, start compilation."
1116 (interactive)
1117 (flymake-stop-all-syntax-checks)
1118 (call-interactively 'compile))
1120 (defun flymake-on-timer-event (buffer)
1121 "Start a syntax check for buffer BUFFER if necessary."
1122 (when (buffer-live-p buffer)
1123 (with-current-buffer buffer
1124 (when (and (not flymake-is-running)
1125 flymake-last-change-time
1126 (> (- (float-time) flymake-last-change-time)
1127 flymake-no-changes-timeout))
1129 (setq flymake-last-change-time nil)
1130 (flymake-log 3 "starting syntax check as more than 1 second passed since last change")
1131 (flymake-start-syntax-check)))))
1133 (define-obsolete-function-alias 'flymake-display-err-menu-for-current-line
1134 'flymake-popup-current-error-menu "24.4")
1136 (defun flymake-popup-current-error-menu (&optional event)
1137 "Pop up a menu with errors/warnings for current line."
1138 (interactive (list last-nonmenu-event))
1139 (let* ((line-no (line-number-at-pos))
1140 (errors (or (car (flymake-find-err-info flymake-err-info line-no))
1141 (user-error "No errors for current line")))
1142 (menu (mapcar (lambda (x)
1143 (if (flymake-ler-file x)
1144 (cons (format "%s - %s(%d)"
1145 (flymake-ler-text x)
1146 (flymake-ler-file x)
1147 (flymake-ler-line x))
1149 (list (flymake-ler-text x))))
1150 errors))
1151 (event (if (mouse-event-p event)
1152 event
1153 (list 'mouse-1 (posn-at-point))))
1154 (title (format "Line %d: %d error(s), %d warning(s)"
1155 line-no
1156 (flymake-get-line-err-count errors "e")
1157 (flymake-get-line-err-count errors "w")))
1158 (choice (x-popup-menu event (list title (cons "" menu)))))
1159 (flymake-log 3 "choice=%s" choice)
1160 (when choice
1161 (flymake-goto-file-and-line (flymake-ler-full-file choice)
1162 (flymake-ler-line choice)))))
1164 (defun flymake-goto-file-and-line (file line)
1165 "Try to get buffer for FILE and goto line LINE in it."
1166 (if (not (file-exists-p file))
1167 (flymake-log 1 "File %s does not exist" file)
1168 (find-file file)
1169 (goto-char (point-min))
1170 (forward-line (1- line))))
1172 ;; flymake minor mode declarations
1173 (defvar-local flymake-mode-line nil)
1174 (defvar-local flymake-mode-line-e-w nil)
1175 (defvar-local flymake-mode-line-status nil)
1177 (defun flymake-report-status (e-w &optional status)
1178 "Show status in mode line."
1179 (when e-w
1180 (setq flymake-mode-line-e-w e-w))
1181 (when status
1182 (setq flymake-mode-line-status status))
1183 (let* ((mode-line " Flymake"))
1184 (when (> (length flymake-mode-line-e-w) 0)
1185 (setq mode-line (concat mode-line ":" flymake-mode-line-e-w)))
1186 (setq mode-line (concat mode-line flymake-mode-line-status))
1187 (setq flymake-mode-line mode-line)
1188 (force-mode-line-update)))
1190 (defun flymake-display-warning (warning)
1191 "Display a warning to user."
1192 (message-box warning))
1194 (defun flymake-report-fatal-status (status warning)
1195 "Display a warning and switch flymake mode off."
1196 (when flymake-gui-warnings-enabled
1197 (flymake-display-warning (format "Flymake: %s. Flymake will be switched OFF" warning))
1199 (flymake-mode 0)
1200 (flymake-log 0 "switched OFF Flymake mode for buffer %s due to fatal status %s, warning %s"
1201 (buffer-name) status warning))
1203 ;;;###autoload
1204 (define-minor-mode flymake-mode nil
1205 :group 'flymake :lighter flymake-mode-line
1206 (cond
1208 ;; Turning the mode ON.
1209 (flymake-mode
1210 (cond
1211 ((not buffer-file-name)
1212 (message "Flymake unable to run without a buffer file name"))
1213 ((not (flymake-can-syntax-check-file buffer-file-name))
1214 (flymake-log 2 "flymake cannot check syntax in buffer %s" (buffer-name)))
1216 (add-hook 'after-change-functions 'flymake-after-change-function nil t)
1217 (add-hook 'after-save-hook 'flymake-after-save-hook nil t)
1218 (add-hook 'kill-buffer-hook 'flymake-kill-buffer-hook nil t)
1219 ;;+(add-hook 'find-file-hook 'flymake-find-file-hook)
1221 (flymake-report-status "" "")
1223 (setq flymake-timer
1224 (run-at-time nil 1 'flymake-on-timer-event (current-buffer)))
1226 (when (and flymake-start-syntax-check-on-find-file
1227 ;; Since we write temp files in current dir, there's no point
1228 ;; trying if the directory is read-only (bug#8954).
1229 (file-writable-p (file-name-directory buffer-file-name)))
1230 (with-demoted-errors
1231 (flymake-start-syntax-check))))))
1233 ;; Turning the mode OFF.
1235 (remove-hook 'after-change-functions 'flymake-after-change-function t)
1236 (remove-hook 'after-save-hook 'flymake-after-save-hook t)
1237 (remove-hook 'kill-buffer-hook 'flymake-kill-buffer-hook t)
1238 ;;+(remove-hook 'find-file-hook (function flymake-find-file-hook) t)
1240 (flymake-delete-own-overlays)
1242 (when flymake-timer
1243 (cancel-timer flymake-timer)
1244 (setq flymake-timer nil))
1246 (setq flymake-is-running nil))))
1248 ;;;###autoload
1249 (defun flymake-mode-on ()
1250 "Turn flymake mode on."
1251 (flymake-mode 1)
1252 (flymake-log 1 "flymake mode turned ON for buffer %s" (buffer-name)))
1254 ;;;###autoload
1255 (defun flymake-mode-off ()
1256 "Turn flymake mode off."
1257 (flymake-mode 0)
1258 (flymake-log 1 "flymake mode turned OFF for buffer %s" (buffer-name)))
1260 (defun flymake-after-change-function (start stop _len)
1261 "Start syntax check for current buffer if it isn't already running."
1262 ;;+(flymake-log 0 "setting change time to %s" (float-time))
1263 (let((new-text (buffer-substring start stop)))
1264 (when (and flymake-start-syntax-check-on-newline (equal new-text "\n"))
1265 (flymake-log 3 "starting syntax check as new-line has been seen")
1266 (flymake-start-syntax-check))
1267 (setq flymake-last-change-time (float-time))))
1269 (defun flymake-after-save-hook ()
1270 (if (local-variable-p 'flymake-mode (current-buffer)) ; (???) other way to determine whether flymake is active in buffer being saved?
1271 (progn
1272 (flymake-log 3 "starting syntax check as buffer was saved")
1273 (flymake-start-syntax-check)))) ; no more mode 3. cannot start check if mode 3 (to temp copies) is active - (???)
1275 (defun flymake-kill-buffer-hook ()
1276 (when flymake-timer
1277 (cancel-timer flymake-timer)
1278 (setq flymake-timer nil)))
1280 ;;;###autoload
1281 (defun flymake-find-file-hook ()
1282 ;;+(when flymake-start-syntax-check-on-find-file
1283 ;;+ (flymake-log 3 "starting syntax check on file open")
1284 ;;+ (flymake-start-syntax-check)
1285 ;;+)
1286 (when (and (not (local-variable-p 'flymake-mode (current-buffer)))
1287 (flymake-can-syntax-check-file buffer-file-name))
1288 (flymake-mode)
1289 (flymake-log 3 "automatically turned ON flymake mode")))
1291 (defun flymake-get-first-err-line-no (err-info-list)
1292 "Return first line with error."
1293 (when err-info-list
1294 (flymake-er-get-line (car err-info-list))))
1296 (defun flymake-get-last-err-line-no (err-info-list)
1297 "Return last line with error."
1298 (when err-info-list
1299 (flymake-er-get-line (nth (1- (length err-info-list)) err-info-list))))
1301 (defun flymake-get-next-err-line-no (err-info-list line-no)
1302 "Return next line with error."
1303 (when err-info-list
1304 (let* ((count (length err-info-list))
1305 (idx 0))
1306 (while (and (< idx count) (>= line-no (flymake-er-get-line (nth idx err-info-list))))
1307 (setq idx (1+ idx)))
1308 (if (< idx count)
1309 (flymake-er-get-line (nth idx err-info-list))))))
1311 (defun flymake-get-prev-err-line-no (err-info-list line-no)
1312 "Return previous line with error."
1313 (when err-info-list
1314 (let* ((count (length err-info-list)))
1315 (while (and (> count 0) (<= line-no (flymake-er-get-line (nth (1- count) err-info-list))))
1316 (setq count (1- count)))
1317 (if (> count 0)
1318 (flymake-er-get-line (nth (1- count) err-info-list))))))
1320 (defun flymake-skip-whitespace ()
1321 "Move forward until non-whitespace is reached."
1322 (while (looking-at "[ \t]")
1323 (forward-char)))
1325 (defun flymake-goto-line (line-no)
1326 "Go to line LINE-NO, then skip whitespace."
1327 (goto-char (point-min))
1328 (forward-line (1- line-no))
1329 (flymake-skip-whitespace))
1331 (defun flymake-goto-next-error ()
1332 "Go to next error in err ring."
1333 (interactive)
1334 (let ((line-no (flymake-get-next-err-line-no flymake-err-info (line-number-at-pos))))
1335 (when (not line-no)
1336 (setq line-no (flymake-get-first-err-line-no flymake-err-info))
1337 (flymake-log 1 "passed end of file"))
1338 (if line-no
1339 (flymake-goto-line line-no)
1340 (flymake-log 1 "no errors in current buffer"))))
1342 (defun flymake-goto-prev-error ()
1343 "Go to previous error in err ring."
1344 (interactive)
1345 (let ((line-no (flymake-get-prev-err-line-no flymake-err-info (line-number-at-pos))))
1346 (when (not line-no)
1347 (setq line-no (flymake-get-last-err-line-no flymake-err-info))
1348 (flymake-log 1 "passed beginning of file"))
1349 (if line-no
1350 (flymake-goto-line line-no)
1351 (flymake-log 1 "no errors in current buffer"))))
1353 (defun flymake-patch-err-text (string)
1354 (if (string-match "^[\n\t :0-9]*\\(.*\\)$" string)
1355 (match-string 1 string)
1356 string))
1358 ;;;; general init-cleanup and helper routines
1359 (defun flymake-create-temp-inplace (file-name prefix)
1360 (unless (stringp file-name)
1361 (error "Invalid file-name"))
1362 (or prefix
1363 (setq prefix "flymake"))
1364 (let* ((ext (file-name-extension file-name))
1365 (temp-name (file-truename
1366 (concat (file-name-sans-extension file-name)
1367 "_" prefix
1368 (and ext (concat "." ext))))))
1369 (flymake-log 3 "create-temp-inplace: file=%s temp=%s" file-name temp-name)
1370 temp-name))
1372 (defun flymake-create-temp-with-folder-structure (file-name _prefix)
1373 (unless (stringp file-name)
1374 (error "Invalid file-name"))
1376 (let* ((dir (file-name-directory file-name))
1377 ;; Not sure what this slash-pos is all about, but I guess it's just
1378 ;; trying to remove the leading / of absolute file names.
1379 (slash-pos (string-match "/" dir))
1380 (temp-dir (expand-file-name (substring dir (1+ slash-pos))
1381 temporary-file-directory)))
1383 (file-truename (expand-file-name (file-name-nondirectory file-name)
1384 temp-dir))))
1386 (defun flymake-delete-temp-directory (dir-name)
1387 "Attempt to delete temp dir created by `flymake-create-temp-with-folder-structure', do not fail on error."
1388 (let* ((temp-dir temporary-file-directory)
1389 (suffix (substring dir-name (1+ (length temp-dir)))))
1391 (while (> (length suffix) 0)
1392 (setq suffix (directory-file-name suffix))
1393 ;;+(flymake-log 0 "suffix=%s" suffix)
1394 (flymake-safe-delete-directory
1395 (file-truename (expand-file-name suffix temp-dir)))
1396 (setq suffix (file-name-directory suffix)))))
1398 (defvar-local flymake-temp-source-file-name nil)
1399 (defvar-local flymake-master-file-name nil)
1400 (defvar-local flymake-temp-master-file-name nil)
1401 (defvar-local flymake-base-dir nil)
1403 (defun flymake-init-create-temp-buffer-copy (create-temp-f)
1404 "Make a temporary copy of the current buffer, save its name in buffer data and return the name."
1405 (let* ((source-file-name buffer-file-name)
1406 (temp-source-file-name (funcall create-temp-f source-file-name "flymake")))
1408 (flymake-save-buffer-in-file temp-source-file-name)
1409 (setq flymake-temp-source-file-name temp-source-file-name)
1410 temp-source-file-name))
1412 (defun flymake-simple-cleanup ()
1413 "Do cleanup after `flymake-init-create-temp-buffer-copy'.
1414 Delete temp file."
1415 (flymake-safe-delete-file flymake-temp-source-file-name)
1416 (setq flymake-last-change-time nil))
1418 (defun flymake-get-real-file-name (file-name-from-err-msg)
1419 "Translate file name from error message to \"real\" file name.
1420 Return full-name. Names are real, not patched."
1421 (let* ((real-name nil)
1422 (source-file-name buffer-file-name)
1423 (master-file-name flymake-master-file-name)
1424 (temp-source-file-name flymake-temp-source-file-name)
1425 (temp-master-file-name flymake-temp-master-file-name)
1426 (base-dirs
1427 (list flymake-base-dir
1428 (file-name-directory source-file-name)
1429 (if master-file-name (file-name-directory master-file-name))))
1430 (files (list (list source-file-name source-file-name)
1431 (list temp-source-file-name source-file-name)
1432 (list master-file-name master-file-name)
1433 (list temp-master-file-name master-file-name))))
1435 (when (equal 0 (length file-name-from-err-msg))
1436 (setq file-name-from-err-msg source-file-name))
1438 (setq real-name (flymake-get-full-patched-file-name file-name-from-err-msg base-dirs files))
1439 ;; if real-name is nil, than file name from err msg is none of the files we've patched
1440 (if (not real-name)
1441 (setq real-name (flymake-get-full-nonpatched-file-name file-name-from-err-msg base-dirs)))
1442 (if (not real-name)
1443 (setq real-name file-name-from-err-msg))
1444 (setq real-name (flymake-fix-file-name real-name))
1445 (flymake-log 3 "get-real-file-name: file-name=%s real-name=%s" file-name-from-err-msg real-name)
1446 real-name))
1448 (defun flymake-get-full-patched-file-name (file-name-from-err-msg base-dirs files)
1449 (let* ((base-dirs-count (length base-dirs))
1450 (file-count (length files))
1451 (real-name nil))
1453 (while (and (not real-name) (> base-dirs-count 0))
1454 (setq file-count (length files))
1455 (while (and (not real-name) (> file-count 0))
1456 (let* ((this-dir (nth (1- base-dirs-count) base-dirs))
1457 (this-file (nth 0 (nth (1- file-count) files)))
1458 (this-real-name (nth 1 (nth (1- file-count) files))))
1459 ;;+(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)
1460 (when (and this-dir this-file (flymake-same-files
1461 (expand-file-name file-name-from-err-msg this-dir)
1462 this-file))
1463 (setq real-name this-real-name)))
1464 (setq file-count (1- file-count)))
1465 (setq base-dirs-count (1- base-dirs-count)))
1466 real-name))
1468 (defun flymake-get-full-nonpatched-file-name (file-name-from-err-msg base-dirs)
1469 (let* ((real-name nil))
1470 (if (file-name-absolute-p file-name-from-err-msg)
1471 (setq real-name file-name-from-err-msg)
1472 (let* ((base-dirs-count (length base-dirs)))
1473 (while (and (not real-name) (> base-dirs-count 0))
1474 (let* ((full-name (expand-file-name file-name-from-err-msg
1475 (nth (1- base-dirs-count) base-dirs))))
1476 (if (file-exists-p full-name)
1477 (setq real-name full-name))
1478 (setq base-dirs-count (1- base-dirs-count))))))
1479 real-name))
1481 (defun flymake-init-find-buildfile-dir (source-file-name buildfile-name)
1482 "Find buildfile, store its dir in buffer data and return its dir, if found."
1483 (let* ((buildfile-dir
1484 (flymake-find-buildfile buildfile-name
1485 (file-name-directory source-file-name))))
1486 (if buildfile-dir
1487 (setq flymake-base-dir buildfile-dir)
1488 (flymake-log 1 "no buildfile (%s) for %s" buildfile-name source-file-name)
1489 (flymake-report-fatal-status
1490 "NOMK" (format "No buildfile (%s) found for %s"
1491 buildfile-name source-file-name)))))
1493 (defun flymake-init-create-temp-source-and-master-buffer-copy (get-incl-dirs-f create-temp-f master-file-masks include-regexp)
1494 "Find master file (or buffer), create its copy along with a copy of the source file."
1495 (let* ((source-file-name buffer-file-name)
1496 (temp-source-file-name (flymake-init-create-temp-buffer-copy create-temp-f))
1497 (master-and-temp-master (flymake-create-master-file
1498 source-file-name temp-source-file-name
1499 get-incl-dirs-f create-temp-f
1500 master-file-masks include-regexp)))
1502 (if (not master-and-temp-master)
1503 (progn
1504 (flymake-log 1 "cannot find master file for %s" source-file-name)
1505 (flymake-report-status "!" "") ; NOMASTER
1506 nil)
1507 (setq flymake-master-file-name (nth 0 master-and-temp-master))
1508 (setq flymake-temp-master-file-name (nth 1 master-and-temp-master)))))
1510 (defun flymake-master-cleanup ()
1511 (flymake-simple-cleanup)
1512 (flymake-safe-delete-file flymake-temp-master-file-name))
1514 ;;;; make-specific init-cleanup routines
1515 (defun flymake-get-syntax-check-program-args (source-file-name base-dir use-relative-base-dir use-relative-source get-cmd-line-f)
1516 "Create a command line for syntax check using GET-CMD-LINE-F."
1517 (funcall get-cmd-line-f
1518 (if use-relative-source
1519 (file-relative-name source-file-name base-dir)
1520 source-file-name)
1521 (if use-relative-base-dir
1522 (file-relative-name base-dir
1523 (file-name-directory source-file-name))
1524 base-dir)))
1526 (defun flymake-get-make-cmdline (source base-dir)
1527 (list "make"
1528 (list "-s"
1529 "-C"
1530 base-dir
1531 (concat "CHK_SOURCES=" source)
1532 "SYNTAX_CHECK_MODE=1"
1533 "check-syntax")))
1535 (defun flymake-get-ant-cmdline (source base-dir)
1536 (list "ant"
1537 (list "-buildfile"
1538 (concat base-dir "/" "build.xml")
1539 (concat "-DCHK_SOURCES=" source)
1540 "check-syntax")))
1542 (defun flymake-simple-make-init-impl (create-temp-f use-relative-base-dir use-relative-source build-file-name get-cmdline-f)
1543 "Create syntax check command line for a directly checked source file.
1544 Use CREATE-TEMP-F for creating temp copy."
1545 (let* ((args nil)
1546 (source-file-name buffer-file-name)
1547 (buildfile-dir (flymake-init-find-buildfile-dir source-file-name build-file-name)))
1548 (if buildfile-dir
1549 (let* ((temp-source-file-name (flymake-init-create-temp-buffer-copy create-temp-f)))
1550 (setq args (flymake-get-syntax-check-program-args temp-source-file-name buildfile-dir
1551 use-relative-base-dir use-relative-source
1552 get-cmdline-f))))
1553 args))
1555 (defun flymake-simple-make-init ()
1556 (flymake-simple-make-init-impl 'flymake-create-temp-inplace t t "Makefile" 'flymake-get-make-cmdline))
1558 (defun flymake-master-make-init (get-incl-dirs-f master-file-masks include-regexp)
1559 "Create make command line for a source file checked via master file compilation."
1560 (let* ((make-args nil)
1561 (temp-master-file-name (flymake-init-create-temp-source-and-master-buffer-copy
1562 get-incl-dirs-f 'flymake-create-temp-inplace
1563 master-file-masks include-regexp)))
1564 (when temp-master-file-name
1565 (let* ((buildfile-dir (flymake-init-find-buildfile-dir temp-master-file-name "Makefile")))
1566 (if buildfile-dir
1567 (setq make-args (flymake-get-syntax-check-program-args
1568 temp-master-file-name buildfile-dir nil nil 'flymake-get-make-cmdline)))))
1569 make-args))
1571 (defun flymake-find-make-buildfile (source-dir)
1572 (flymake-find-buildfile "Makefile" source-dir))
1574 ;;;; .h/make specific
1575 (defun flymake-master-make-header-init ()
1576 (flymake-master-make-init
1577 'flymake-get-include-dirs
1578 '("\\.\\(?:c\\(?:pp\\|xx\\|\\+\\+\\)?\\|CC\\)\\'")
1579 "[ \t]*#[ \t]*include[ \t]*\"\\([[:word:]0-9/\\_.]*%s\\)\""))
1581 ;;;; .java/make specific
1582 (defun flymake-simple-make-java-init ()
1583 (flymake-simple-make-init-impl 'flymake-create-temp-with-folder-structure nil nil "Makefile" 'flymake-get-make-cmdline))
1585 (defun flymake-simple-ant-java-init ()
1586 (flymake-simple-make-init-impl 'flymake-create-temp-with-folder-structure nil nil "build.xml" 'flymake-get-ant-cmdline))
1588 (defun flymake-simple-java-cleanup ()
1589 "Cleanup after `flymake-simple-make-java-init' -- delete temp file and dirs."
1590 (flymake-safe-delete-file flymake-temp-source-file-name)
1591 (when flymake-temp-source-file-name
1592 (flymake-delete-temp-directory
1593 (file-name-directory flymake-temp-source-file-name))))
1595 ;;;; perl-specific init-cleanup routines
1596 (defun flymake-perl-init ()
1597 (let* ((temp-file (flymake-init-create-temp-buffer-copy
1598 'flymake-create-temp-inplace))
1599 (local-file (file-relative-name
1600 temp-file
1601 (file-name-directory buffer-file-name))))
1602 (list "perl" (list "-wc " local-file))))
1604 ;;;; php-specific init-cleanup routines
1605 (defun flymake-php-init ()
1606 (let* ((temp-file (flymake-init-create-temp-buffer-copy
1607 'flymake-create-temp-inplace))
1608 (local-file (file-relative-name
1609 temp-file
1610 (file-name-directory buffer-file-name))))
1611 (list "php" (list "-f" local-file "-l"))))
1613 ;;;; tex-specific init-cleanup routines
1614 (defun flymake-get-tex-args (file-name)
1615 ;;(list "latex" (list "-c-style-errors" file-name))
1616 (list "texify" (list "--pdf" "--tex-option=-c-style-errors" file-name)))
1618 (defun flymake-simple-tex-init ()
1619 (flymake-get-tex-args (flymake-init-create-temp-buffer-copy 'flymake-create-temp-inplace)))
1621 ;; Perhaps there should be a buffer-local variable flymake-master-file
1622 ;; that people can set to override this stuff. Could inherit from
1623 ;; the similar AUCTeX variable.
1624 (defun flymake-master-tex-init ()
1625 (let* ((temp-master-file-name (flymake-init-create-temp-source-and-master-buffer-copy
1626 'flymake-get-include-dirs-dot 'flymake-create-temp-inplace
1627 '("\\.tex\\'")
1628 "[ \t]*\\in\\(?:put\\|clude\\)[ \t]*{\\(.*%s\\)}")))
1629 (when temp-master-file-name
1630 (flymake-get-tex-args temp-master-file-name))))
1632 (defun flymake-get-include-dirs-dot (_base-dir)
1633 '("."))
1635 ;;;; xml-specific init-cleanup routines
1636 (defun flymake-xml-init ()
1637 (list flymake-xml-program
1638 (list "val" (flymake-init-create-temp-buffer-copy
1639 'flymake-create-temp-inplace))))
1641 (provide 'flymake)
1642 ;;; flymake.el ends here