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>
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/>.
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
33 ;; - Only uses "Makefile", not "makefile" or "GNUmakefile"
34 ;; (from http://bugs.debian.org/337339).
38 (eval-when-compile (require 'cl-lib))
41 "Universal on-the-fly syntax checker."
43 :link '(custom-manual "(flymake) Top")
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
56 :type '(choice (symbol :tag "Bitmap")
57 (list :tag "Bitmap and face"
58 (symbol :tag "Bitmap")
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
71 :type '(choice (symbol :tag "Bitmap")
72 (list :tag "Bitmap and face"
73 (symbol :tag "Bitmap")
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'."
82 :type '(choice (const left-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."
91 (defcustom flymake-start-syntax-check-on-newline t
92 "Start syntax check if newline char was added/removed from the buffer."
96 (defcustom flymake-no-changes-timeout 0.5
97 "Time to wait after last change before starting compilation."
101 (defcustom flymake-gui-warnings-enabled t
102 "Enables/disables GUI warnings."
106 (defcustom flymake-start-syntax-check-on-find-file t
107 "Start syntax check on find file."
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"
117 (defcustom flymake-xml-program
118 (if (executable-find "xmlstarlet") "xmlstarlet" "xml")
119 "Program to use for XML validation."
124 (defcustom flymake-master-file-dirs '("." "./src" "./UnitTest")
125 "Dirs where to look for master files."
127 :type '(repeat (string)))
129 (defcustom flymake-master-file-count-limit 32
130 "Max number of master files to check."
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)
148 ;; ("\\.h\\'" 2 ("\\.cpp\\'" "\\.c\\'")
149 ;; ("[ \t]*#[ \t]*include[ \t]*\"\\([\w0-9/\\_\.]*[/\\]*\\)\\(%s\\)\"" 1 2))
152 ;; ("[0-9]+\\.tex\\'" 2 ("\\.tex\\'")
153 ;; ("[ \t]*\\input[ \t]*{\\(.*\\)\\(%s\\)}" 1 2 ))
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'."
164 :type '(alist :key-type (regexp :tag "File regexp")
166 (list :tag "Handler functions"
167 (function :tag "Init function")
168 (choice :tag "Cleanup function"
169 (const :tag "flymake-simple-cleanup" nil)
171 (choice :tag "Name function"
172 (const :tag "flymake-get-real-file-name" nil)
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-message 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)))
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)
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))
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))
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)))
282 (flymake-log 3 "found buildfile at %s" file)
283 (flymake-add-buildfile-to-cache source-dir-name file)
286 (flymake-log 3 "buildfile for %s not found" source-dir-name)
289 (defun flymake-fix-file-name (name)
290 "Replace all occurrences of `\\' with `/'."
292 (setq name (expand-file-name name))
293 (setq name (abbreviate-file-name name))
294 (setq name (directory-file-name 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)
317 (while (and (not done) dirs)
318 (let* ((dir (expand-file-name (car dirs) (file-name-directory file-name)))
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)
332 (setq dir-files (cdr dir-files))))
333 (setq masks (cdr masks))))
334 (setq dirs (cdr dirs)))
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))
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
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))
371 (search-limit flymake-check-file-limit))
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))))
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)
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
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
415 (file-name-nondirectory patched-source-file-name))))
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)
424 (flymake-log 2 "found master file %s" master-file-name))
428 (defun flymake-replace-region (beg end rep)
429 "Replace text in BUFFER in region (BEG END) with REP."
432 ;; Insert before deleting, so as to better preserve markers's positions.
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))
443 (defun flymake-copy-buffer-to-temp-buffer (buffer)
444 "Copy contents of BUFFER into newly created temp buffer."
446 (get-buffer-create (generate-new-buffer-name
447 (concat "flymake:" (buffer-name buffer))))
448 (insert-buffer-substring 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
459 (concat (file-name-directory source-file-name)
460 "/" (car include-dirs)
462 (setq include-dirs (cdr 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)))
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))
479 (master-file-name nil)
480 (patched-master-file-name 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)))
490 (flymake-check-patch-master-file-buffer
493 patched-master-file-name
495 patched-source-file-name
496 (funcall get-incl-dirs-f (file-name-directory master-file-name))
500 (list master-file-name patched-master-file-name)
502 (flymake-log 3 "none of %d master file(s) checked includes %s" master-file-count
503 (file-name-nondirectory source-file-name))
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)
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))))
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)
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
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)
611 (defun flymake-er-get-line-err-info-list (err-info)
614 (cl-defstruct (flymake-ler
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)
633 (defun flymake-ler-set-line (line-err-info line)
634 (flymake-ler-make-ler (flymake-ler-file line-err-info)
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\"."
644 (count (length line-err-info-list))
648 (when (equal type (flymake-ler-type (nth idx line-err-info-list)))
649 (setq err-count (1+ err-count)))
653 (defun flymake-get-err-count (err-info-list type)
654 "Return number of errors of specified TYPE for ERR-INFO-LIST."
656 (count (length err-info-list))
659 (setq err-count (+ err-count (flymake-get-line-err-count (nth 1 (nth idx err-info-list)) type)))
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))
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))))
683 (defun flymake-highlight-err-lines (err-info-list)
684 "Highlight error lines in BUFFER using info from ERR-INFO-LIST."
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
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)
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))
727 (when (flymake-overlay-p (car ov))
728 (setq has-flymake-overlays t))
730 has-flymake-overlays))
732 (defface flymake-errline
733 '((((supports :underline (:style wave)))
734 :underline (:style wave :color "Red1"))
737 "Face used for marking error lines."
741 (defface flymake-warnline
742 '((((supports :underline (:style wave)))
743 :underline (:style wave :color "DarkOrange"))
746 "Face used for marking warning lines."
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)))
758 (skip-chars-backward " \t\f\t\n" beg)
760 (line-beginning-position 2)
762 (tooltip-text (mapconcat #'flymake-ler-text line-err-info-list "\n"))
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))
775 (source-file-name buffer-file-name)
776 (get-real-file-name-f (flymake-get-real-file-name-function source-file-name)))
779 (setq line-err-info (flymake-parse-line (nth idx lines)))
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"))
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))))))
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))
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)))))
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
829 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\)) : \\(\\(error\\|warning\\|fatal error\\) \\(C[0-9]+\\):[ \t\n]*\\(.+\\)\\)"
832 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\):\\([0-9]+\\):[0-9]+:[0-9]+:[0-9]+: \\(\\(Error\\|Warning\\|Caution\\|Semantic Error\\):[ \t\n]*\\(.+\\)\\)"
835 ("midl[ ]*:[ ]*\\(command line error .*\\)"
838 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\),[0-9]+): \\(\\(error\\|warning\\|fatal error\\) \\(CS[0-9]+\\):[ \t\n]*\\(.+\\)\\)"
841 ("\\(.*\\) at \\([^ \n]+\\) line \\([0-9]+\\)[,.\n]" 2 3 nil 1)
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]*\\(.+\\)"
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
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
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)
869 (patterns flymake-err-line-patterns)
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)))))
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")))
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)
894 (setq patterns (cdr patterns)))
896 (flymake-ler-make-ler raw-file-name line-no err-type err-text)
899 (defun flymake-find-err-info (err-info-list line-no)
900 "Find (line-err-info-list pos) for specified LINE-NO."
902 (let* ((line-err-info-list nil)
904 (count (length err-info-list)))
906 (while (and (< pos count) (< (car (nth pos err-info-list)) line-no))
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))
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)
929 (let* ((count (length line-err-info-list))
931 (while (and (< idx count) (flymake-line-err-info-is-less-or-equal (nth idx line-err-info-list) line-err-info))
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)
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))))
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)
963 (flymake-get-project-include-dirs-from-cache basedir))
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))
973 (while (and (< idx count) (not (string-match "^INCLUDE_DIRS=.*" (nth idx lines))))
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)
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))))
1012 ;; (defun flymake-restore-formatting ()
1013 ;; "Remove any formatting made by flymake."
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)
1031 (delete-directory dir-name)
1032 (flymake-log 1 "deleted dir %s" dir-name))
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."
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)
1058 (flymake-log 0 "init function %s for %s failed, cleaning up" init-f source-file-name)
1059 (funcall cleanup-f))
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."
1068 (let ((default-directory (or dir default-directory)))
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)
1089 "Failed to launch syntax check process `%s' with args %s: %s"
1090 cmd args (error-message-string err)))
1091 (source-file-name buffer-file-name)
1092 (cleanup-f (flymake-get-cleanup-function source-file-name)))
1093 (flymake-log 0 err-str)
1095 (flymake-report-fatal-status "PROCERR" err-str)))))
1097 (defun flymake-kill-process (proc)
1098 "Kill process PROC."
1100 (let* ((buf (process-buffer proc)))
1101 (when (buffer-live-p buf)
1102 (with-current-buffer buf
1103 (setq flymake-check-was-interrupted t))))
1104 (flymake-log 1 "killed process %d" (process-id proc)))
1106 (defun flymake-stop-all-syntax-checks ()
1107 "Kill all syntax check processes."
1109 (while flymake-processes
1110 (flymake-kill-process (pop flymake-processes))))
1112 (defun flymake-compilation-is-running ()
1113 (and (boundp 'compilation-in-progress)
1114 compilation-in-progress))
1116 (defun flymake-compile ()
1117 "Kill all flymake syntax checks, start compilation."
1119 (flymake-stop-all-syntax-checks)
1120 (call-interactively 'compile))
1122 (defun flymake-on-timer-event (buffer)
1123 "Start a syntax check for buffer BUFFER if necessary."
1124 (when (buffer-live-p buffer)
1125 (with-current-buffer buffer
1126 (when (and (not flymake-is-running)
1127 flymake-last-change-time
1128 (> (- (float-time) flymake-last-change-time)
1129 flymake-no-changes-timeout))
1131 (setq flymake-last-change-time nil)
1132 (flymake-log 3 "starting syntax check as more than 1 second passed since last change")
1133 (flymake-start-syntax-check)))))
1135 (define-obsolete-function-alias 'flymake-display-err-menu-for-current-line
1136 'flymake-popup-current-error-menu "24.4")
1138 (defun flymake-popup-current-error-menu (&optional event)
1139 "Pop up a menu with errors/warnings for current line."
1140 (interactive (list last-nonmenu-event))
1141 (let* ((line-no (line-number-at-pos))
1142 (errors (or (car (flymake-find-err-info flymake-err-info line-no))
1143 (user-error "No errors for current line")))
1144 (menu (mapcar (lambda (x)
1145 (if (flymake-ler-file x)
1146 (cons (format "%s - %s(%d)"
1147 (flymake-ler-text x)
1148 (flymake-ler-file x)
1149 (flymake-ler-line x))
1151 (list (flymake-ler-text x))))
1153 (event (if (mouse-event-p event)
1155 (list 'mouse-1 (posn-at-point))))
1156 (title (format "Line %d: %d error(s), %d warning(s)"
1158 (flymake-get-line-err-count errors "e")
1159 (flymake-get-line-err-count errors "w")))
1160 (choice (x-popup-menu event (list title (cons "" menu)))))
1161 (flymake-log 3 "choice=%s" choice)
1163 (flymake-goto-file-and-line (flymake-ler-full-file choice)
1164 (flymake-ler-line choice)))))
1166 (defun flymake-goto-file-and-line (file line)
1167 "Try to get buffer for FILE and goto line LINE in it."
1168 (if (not (file-exists-p file))
1169 (flymake-log 1 "File %s does not exist" file)
1171 (goto-char (point-min))
1172 (forward-line (1- line))))
1174 ;; flymake minor mode declarations
1175 (defvar-local flymake-mode-line nil)
1176 (defvar-local flymake-mode-line-e-w nil)
1177 (defvar-local flymake-mode-line-status nil)
1179 (defun flymake-report-status (e-w &optional status)
1180 "Show status in mode line."
1182 (setq flymake-mode-line-e-w e-w))
1184 (setq flymake-mode-line-status status))
1185 (let* ((mode-line " Flymake"))
1186 (when (> (length flymake-mode-line-e-w) 0)
1187 (setq mode-line (concat mode-line ":" flymake-mode-line-e-w)))
1188 (setq mode-line (concat mode-line flymake-mode-line-status))
1189 (setq flymake-mode-line mode-line)
1190 (force-mode-line-update)))
1192 (defun flymake-display-warning (warning)
1193 "Display a warning to user."
1194 (message-box warning))
1196 (defun flymake-report-fatal-status (status warning)
1197 "Display a warning and switch flymake mode off."
1198 (when flymake-gui-warnings-enabled
1199 (flymake-display-warning (format "Flymake: %s. Flymake will be switched OFF" warning))
1202 (flymake-log 0 "switched OFF Flymake mode for buffer %s due to fatal status %s, warning %s"
1203 (buffer-name) status warning))
1206 (define-minor-mode flymake-mode nil
1207 :group 'flymake :lighter flymake-mode-line
1210 ;; Turning the mode ON.
1213 ((not buffer-file-name)
1214 (message "Flymake unable to run without a buffer file name"))
1215 ((not (flymake-can-syntax-check-file buffer-file-name))
1216 (flymake-log 2 "flymake cannot check syntax in buffer %s" (buffer-name)))
1218 (add-hook 'after-change-functions 'flymake-after-change-function nil t)
1219 (add-hook 'after-save-hook 'flymake-after-save-hook nil t)
1220 (add-hook 'kill-buffer-hook 'flymake-kill-buffer-hook nil t)
1221 ;;+(add-hook 'find-file-hook 'flymake-find-file-hook)
1223 (flymake-report-status "" "")
1226 (run-at-time nil 1 'flymake-on-timer-event (current-buffer)))
1228 (when (and flymake-start-syntax-check-on-find-file
1229 ;; Since we write temp files in current dir, there's no point
1230 ;; trying if the directory is read-only (bug#8954).
1231 (file-writable-p (file-name-directory buffer-file-name)))
1232 (with-demoted-errors
1233 (flymake-start-syntax-check))))))
1235 ;; Turning the mode OFF.
1237 (remove-hook 'after-change-functions 'flymake-after-change-function t)
1238 (remove-hook 'after-save-hook 'flymake-after-save-hook t)
1239 (remove-hook 'kill-buffer-hook 'flymake-kill-buffer-hook t)
1240 ;;+(remove-hook 'find-file-hook (function flymake-find-file-hook) t)
1242 (flymake-delete-own-overlays)
1245 (cancel-timer flymake-timer)
1246 (setq flymake-timer nil))
1248 (setq flymake-is-running nil))))
1251 (defun flymake-mode-on ()
1252 "Turn flymake mode on."
1254 (flymake-log 1 "flymake mode turned ON for buffer %s" (buffer-name)))
1257 (defun flymake-mode-off ()
1258 "Turn flymake mode off."
1260 (flymake-log 1 "flymake mode turned OFF for buffer %s" (buffer-name)))
1262 (defun flymake-after-change-function (start stop _len)
1263 "Start syntax check for current buffer if it isn't already running."
1264 ;;+(flymake-log 0 "setting change time to %s" (float-time))
1265 (let((new-text (buffer-substring start stop)))
1266 (when (and flymake-start-syntax-check-on-newline (equal new-text "\n"))
1267 (flymake-log 3 "starting syntax check as new-line has been seen")
1268 (flymake-start-syntax-check))
1269 (setq flymake-last-change-time (float-time))))
1271 (defun flymake-after-save-hook ()
1272 (if (local-variable-p 'flymake-mode (current-buffer)) ; (???) other way to determine whether flymake is active in buffer being saved?
1274 (flymake-log 3 "starting syntax check as buffer was saved")
1275 (flymake-start-syntax-check)))) ; no more mode 3. cannot start check if mode 3 (to temp copies) is active - (???)
1277 (defun flymake-kill-buffer-hook ()
1279 (cancel-timer flymake-timer)
1280 (setq flymake-timer nil)))
1283 (defun flymake-find-file-hook ()
1284 ;;+(when flymake-start-syntax-check-on-find-file
1285 ;;+ (flymake-log 3 "starting syntax check on file open")
1286 ;;+ (flymake-start-syntax-check)
1288 (when (and (not (local-variable-p 'flymake-mode (current-buffer)))
1289 (flymake-can-syntax-check-file buffer-file-name))
1291 (flymake-log 3 "automatically turned ON flymake mode")))
1293 (defun flymake-get-first-err-line-no (err-info-list)
1294 "Return first line with error."
1296 (flymake-er-get-line (car err-info-list))))
1298 (defun flymake-get-last-err-line-no (err-info-list)
1299 "Return last line with error."
1301 (flymake-er-get-line (nth (1- (length err-info-list)) err-info-list))))
1303 (defun flymake-get-next-err-line-no (err-info-list line-no)
1304 "Return next line with error."
1306 (let* ((count (length err-info-list))
1308 (while (and (< idx count) (>= line-no (flymake-er-get-line (nth idx err-info-list))))
1309 (setq idx (1+ idx)))
1311 (flymake-er-get-line (nth idx err-info-list))))))
1313 (defun flymake-get-prev-err-line-no (err-info-list line-no)
1314 "Return previous line with error."
1316 (let* ((count (length err-info-list)))
1317 (while (and (> count 0) (<= line-no (flymake-er-get-line (nth (1- count) err-info-list))))
1318 (setq count (1- count)))
1320 (flymake-er-get-line (nth (1- count) err-info-list))))))
1322 (defun flymake-skip-whitespace ()
1323 "Move forward until non-whitespace is reached."
1324 (while (looking-at "[ \t]")
1327 (defun flymake-goto-line (line-no)
1328 "Go to line LINE-NO, then skip whitespace."
1329 (goto-char (point-min))
1330 (forward-line (1- line-no))
1331 (flymake-skip-whitespace))
1333 (defun flymake-goto-next-error ()
1334 "Go to next error in err ring."
1336 (let ((line-no (flymake-get-next-err-line-no flymake-err-info (line-number-at-pos))))
1338 (setq line-no (flymake-get-first-err-line-no flymake-err-info))
1339 (flymake-log 1 "passed end of file"))
1341 (flymake-goto-line line-no)
1342 (flymake-log 1 "no errors in current buffer"))))
1344 (defun flymake-goto-prev-error ()
1345 "Go to previous error in err ring."
1347 (let ((line-no (flymake-get-prev-err-line-no flymake-err-info (line-number-at-pos))))
1349 (setq line-no (flymake-get-last-err-line-no flymake-err-info))
1350 (flymake-log 1 "passed beginning of file"))
1352 (flymake-goto-line line-no)
1353 (flymake-log 1 "no errors in current buffer"))))
1355 (defun flymake-patch-err-text (string)
1356 (if (string-match "^[\n\t :0-9]*\\(.*\\)$" string)
1357 (match-string 1 string)
1360 ;;;; general init-cleanup and helper routines
1361 (defun flymake-create-temp-inplace (file-name prefix)
1362 (unless (stringp file-name)
1363 (error "Invalid file-name"))
1365 (setq prefix "flymake"))
1366 (let* ((ext (file-name-extension file-name))
1367 (temp-name (file-truename
1368 (concat (file-name-sans-extension file-name)
1370 (and ext (concat "." ext))))))
1371 (flymake-log 3 "create-temp-inplace: file=%s temp=%s" file-name temp-name)
1374 (defun flymake-create-temp-with-folder-structure (file-name _prefix)
1375 (unless (stringp file-name)
1376 (error "Invalid file-name"))
1378 (let* ((dir (file-name-directory file-name))
1379 ;; Not sure what this slash-pos is all about, but I guess it's just
1380 ;; trying to remove the leading / of absolute file names.
1381 (slash-pos (string-match "/" dir))
1382 (temp-dir (expand-file-name (substring dir (1+ slash-pos))
1383 temporary-file-directory)))
1385 (file-truename (expand-file-name (file-name-nondirectory file-name)
1388 (defun flymake-delete-temp-directory (dir-name)
1389 "Attempt to delete temp dir created by `flymake-create-temp-with-folder-structure', do not fail on error."
1390 (let* ((temp-dir temporary-file-directory)
1391 (suffix (substring dir-name (1+ (length temp-dir)))))
1393 (while (> (length suffix) 0)
1394 (setq suffix (directory-file-name suffix))
1395 ;;+(flymake-log 0 "suffix=%s" suffix)
1396 (flymake-safe-delete-directory
1397 (file-truename (expand-file-name suffix temp-dir)))
1398 (setq suffix (file-name-directory suffix)))))
1400 (defvar-local flymake-temp-source-file-name nil)
1401 (defvar-local flymake-master-file-name nil)
1402 (defvar-local flymake-temp-master-file-name nil)
1403 (defvar-local flymake-base-dir nil)
1405 (defun flymake-init-create-temp-buffer-copy (create-temp-f)
1406 "Make a temporary copy of the current buffer, save its name in buffer data and return the name."
1407 (let* ((source-file-name buffer-file-name)
1408 (temp-source-file-name (funcall create-temp-f source-file-name "flymake")))
1410 (flymake-save-buffer-in-file temp-source-file-name)
1411 (setq flymake-temp-source-file-name temp-source-file-name)
1412 temp-source-file-name))
1414 (defun flymake-simple-cleanup ()
1415 "Do cleanup after `flymake-init-create-temp-buffer-copy'.
1417 (flymake-safe-delete-file flymake-temp-source-file-name)
1418 (setq flymake-last-change-time nil))
1420 (defun flymake-get-real-file-name (file-name-from-err-msg)
1421 "Translate file name from error message to \"real\" file name.
1422 Return full-name. Names are real, not patched."
1423 (let* ((real-name nil)
1424 (source-file-name buffer-file-name)
1425 (master-file-name flymake-master-file-name)
1426 (temp-source-file-name flymake-temp-source-file-name)
1427 (temp-master-file-name flymake-temp-master-file-name)
1429 (list flymake-base-dir
1430 (file-name-directory source-file-name)
1431 (if master-file-name (file-name-directory master-file-name))))
1432 (files (list (list source-file-name source-file-name)
1433 (list temp-source-file-name source-file-name)
1434 (list master-file-name master-file-name)
1435 (list temp-master-file-name master-file-name))))
1437 (when (equal 0 (length file-name-from-err-msg))
1438 (setq file-name-from-err-msg source-file-name))
1440 (setq real-name (flymake-get-full-patched-file-name file-name-from-err-msg base-dirs files))
1441 ;; if real-name is nil, than file name from err msg is none of the files we've patched
1443 (setq real-name (flymake-get-full-nonpatched-file-name file-name-from-err-msg base-dirs)))
1445 (setq real-name file-name-from-err-msg))
1446 (setq real-name (flymake-fix-file-name real-name))
1447 (flymake-log 3 "get-real-file-name: file-name=%s real-name=%s" file-name-from-err-msg real-name)
1450 (defun flymake-get-full-patched-file-name (file-name-from-err-msg base-dirs files)
1451 (let* ((base-dirs-count (length base-dirs))
1452 (file-count (length files))
1455 (while (and (not real-name) (> base-dirs-count 0))
1456 (setq file-count (length files))
1457 (while (and (not real-name) (> file-count 0))
1458 (let* ((this-dir (nth (1- base-dirs-count) base-dirs))
1459 (this-file (nth 0 (nth (1- file-count) files)))
1460 (this-real-name (nth 1 (nth (1- file-count) files))))
1461 ;;+(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)
1462 (when (and this-dir this-file (flymake-same-files
1463 (expand-file-name file-name-from-err-msg this-dir)
1465 (setq real-name this-real-name)))
1466 (setq file-count (1- file-count)))
1467 (setq base-dirs-count (1- base-dirs-count)))
1470 (defun flymake-get-full-nonpatched-file-name (file-name-from-err-msg base-dirs)
1471 (let* ((real-name nil))
1472 (if (file-name-absolute-p file-name-from-err-msg)
1473 (setq real-name file-name-from-err-msg)
1474 (let* ((base-dirs-count (length base-dirs)))
1475 (while (and (not real-name) (> base-dirs-count 0))
1476 (let* ((full-name (expand-file-name file-name-from-err-msg
1477 (nth (1- base-dirs-count) base-dirs))))
1478 (if (file-exists-p full-name)
1479 (setq real-name full-name))
1480 (setq base-dirs-count (1- base-dirs-count))))))
1483 (defun flymake-init-find-buildfile-dir (source-file-name buildfile-name)
1484 "Find buildfile, store its dir in buffer data and return its dir, if found."
1485 (let* ((buildfile-dir
1486 (flymake-find-buildfile buildfile-name
1487 (file-name-directory source-file-name))))
1489 (setq flymake-base-dir buildfile-dir)
1490 (flymake-log 1 "no buildfile (%s) for %s" buildfile-name source-file-name)
1491 (flymake-report-fatal-status
1492 "NOMK" (format "No buildfile (%s) found for %s"
1493 buildfile-name source-file-name)))))
1495 (defun flymake-init-create-temp-source-and-master-buffer-copy (get-incl-dirs-f create-temp-f master-file-masks include-regexp)
1496 "Find master file (or buffer), create its copy along with a copy of the source file."
1497 (let* ((source-file-name buffer-file-name)
1498 (temp-source-file-name (flymake-init-create-temp-buffer-copy create-temp-f))
1499 (master-and-temp-master (flymake-create-master-file
1500 source-file-name temp-source-file-name
1501 get-incl-dirs-f create-temp-f
1502 master-file-masks include-regexp)))
1504 (if (not master-and-temp-master)
1506 (flymake-log 1 "cannot find master file for %s" source-file-name)
1507 (flymake-report-status "!" "") ; NOMASTER
1509 (setq flymake-master-file-name (nth 0 master-and-temp-master))
1510 (setq flymake-temp-master-file-name (nth 1 master-and-temp-master)))))
1512 (defun flymake-master-cleanup ()
1513 (flymake-simple-cleanup)
1514 (flymake-safe-delete-file flymake-temp-master-file-name))
1516 ;;;; make-specific init-cleanup routines
1517 (defun flymake-get-syntax-check-program-args (source-file-name base-dir use-relative-base-dir use-relative-source get-cmd-line-f)
1518 "Create a command line for syntax check using GET-CMD-LINE-F."
1519 (funcall get-cmd-line-f
1520 (if use-relative-source
1521 (file-relative-name source-file-name base-dir)
1523 (if use-relative-base-dir
1524 (file-relative-name base-dir
1525 (file-name-directory source-file-name))
1528 (defun flymake-get-make-cmdline (source base-dir)
1533 (concat "CHK_SOURCES=" source)
1534 "SYNTAX_CHECK_MODE=1"
1537 (defun flymake-get-ant-cmdline (source base-dir)
1540 (concat base-dir "/" "build.xml")
1541 (concat "-DCHK_SOURCES=" source)
1544 (defun flymake-simple-make-init-impl (create-temp-f use-relative-base-dir use-relative-source build-file-name get-cmdline-f)
1545 "Create syntax check command line for a directly checked source file.
1546 Use CREATE-TEMP-F for creating temp copy."
1548 (source-file-name buffer-file-name)
1549 (buildfile-dir (flymake-init-find-buildfile-dir source-file-name build-file-name)))
1551 (let* ((temp-source-file-name (flymake-init-create-temp-buffer-copy create-temp-f)))
1552 (setq args (flymake-get-syntax-check-program-args temp-source-file-name buildfile-dir
1553 use-relative-base-dir use-relative-source
1557 (defun flymake-simple-make-init ()
1558 (flymake-simple-make-init-impl 'flymake-create-temp-inplace t t "Makefile" 'flymake-get-make-cmdline))
1560 (defun flymake-master-make-init (get-incl-dirs-f master-file-masks include-regexp)
1561 "Create make command line for a source file checked via master file compilation."
1562 (let* ((make-args nil)
1563 (temp-master-file-name (flymake-init-create-temp-source-and-master-buffer-copy
1564 get-incl-dirs-f 'flymake-create-temp-inplace
1565 master-file-masks include-regexp)))
1566 (when temp-master-file-name
1567 (let* ((buildfile-dir (flymake-init-find-buildfile-dir temp-master-file-name "Makefile")))
1569 (setq make-args (flymake-get-syntax-check-program-args
1570 temp-master-file-name buildfile-dir nil nil 'flymake-get-make-cmdline)))))
1573 (defun flymake-find-make-buildfile (source-dir)
1574 (flymake-find-buildfile "Makefile" source-dir))
1576 ;;;; .h/make specific
1577 (defun flymake-master-make-header-init ()
1578 (flymake-master-make-init
1579 'flymake-get-include-dirs
1580 '("\\.\\(?:c\\(?:pp\\|xx\\|\\+\\+\\)?\\|CC\\)\\'")
1581 "[ \t]*#[ \t]*include[ \t]*\"\\([[:word:]0-9/\\_.]*%s\\)\""))
1583 ;;;; .java/make specific
1584 (defun flymake-simple-make-java-init ()
1585 (flymake-simple-make-init-impl 'flymake-create-temp-with-folder-structure nil nil "Makefile" 'flymake-get-make-cmdline))
1587 (defun flymake-simple-ant-java-init ()
1588 (flymake-simple-make-init-impl 'flymake-create-temp-with-folder-structure nil nil "build.xml" 'flymake-get-ant-cmdline))
1590 (defun flymake-simple-java-cleanup ()
1591 "Cleanup after `flymake-simple-make-java-init' -- delete temp file and dirs."
1592 (flymake-safe-delete-file flymake-temp-source-file-name)
1593 (when flymake-temp-source-file-name
1594 (flymake-delete-temp-directory
1595 (file-name-directory flymake-temp-source-file-name))))
1597 ;;;; perl-specific init-cleanup routines
1598 (defun flymake-perl-init ()
1599 (let* ((temp-file (flymake-init-create-temp-buffer-copy
1600 'flymake-create-temp-inplace))
1601 (local-file (file-relative-name
1603 (file-name-directory buffer-file-name))))
1604 (list "perl" (list "-wc " local-file))))
1606 ;;;; php-specific init-cleanup routines
1607 (defun flymake-php-init ()
1608 (let* ((temp-file (flymake-init-create-temp-buffer-copy
1609 'flymake-create-temp-inplace))
1610 (local-file (file-relative-name
1612 (file-name-directory buffer-file-name))))
1613 (list "php" (list "-f" local-file "-l"))))
1615 ;;;; tex-specific init-cleanup routines
1616 (defun flymake-get-tex-args (file-name)
1617 ;;(list "latex" (list "-c-style-errors" file-name))
1618 (list "texify" (list "--pdf" "--tex-option=-c-style-errors" file-name)))
1620 (defun flymake-simple-tex-init ()
1621 (flymake-get-tex-args (flymake-init-create-temp-buffer-copy 'flymake-create-temp-inplace)))
1623 ;; Perhaps there should be a buffer-local variable flymake-master-file
1624 ;; that people can set to override this stuff. Could inherit from
1625 ;; the similar AUCTeX variable.
1626 (defun flymake-master-tex-init ()
1627 (let* ((temp-master-file-name (flymake-init-create-temp-source-and-master-buffer-copy
1628 'flymake-get-include-dirs-dot 'flymake-create-temp-inplace
1630 "[ \t]*\\in\\(?:put\\|clude\\)[ \t]*{\\(.*%s\\)}")))
1631 (when temp-master-file-name
1632 (flymake-get-tex-args temp-master-file-name))))
1634 (defun flymake-get-include-dirs-dot (_base-dir)
1637 ;;;; xml-specific init-cleanup routines
1638 (defun flymake-xml-init ()
1639 (list flymake-xml-program
1640 (list "val" (flymake-init-create-temp-buffer-copy
1641 'flymake-create-temp-inplace))))
1644 ;;; flymake.el ends here