1 ;;; flymake.el --- a universal on-the-fly syntax checker -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2003-2013 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."
45 (defcustom flymake-error-bitmap
'(exclamation-mark error
)
46 "Bitmap (a symbol) used in the fringe for indicating errors.
47 The value may also be a list of two elements where the second
48 element specifies the face for the bitmap. For possible bitmap
49 symbols, see `fringe-bitmaps'. See also `flymake-warning-bitmap'.
51 The option `flymake-fringe-indicator-position' controls how and where
55 :type
'(choice (symbol :tag
"Bitmap")
56 (list :tag
"Bitmap and face"
57 (symbol :tag
"Bitmap")
60 (defcustom flymake-warning-bitmap
'question-mark
61 "Bitmap (a symbol) used in the fringe for indicating warnings.
62 The value may also be a list of two elements where the second
63 element specifies the face for the bitmap. For possible bitmap
64 symbols, see `fringe-bitmaps'. See also `flymake-error-bitmap'.
66 The option `flymake-fringe-indicator-position' controls how and where
70 :type
'(choice (symbol :tag
"Bitmap")
71 (list :tag
"Bitmap and face"
72 (symbol :tag
"Bitmap")
75 (defcustom flymake-fringe-indicator-position
'left-fringe
76 "The position to put flymake fringe indicator.
77 The value can be nil (do not use indicators), `left-fringe' or `right-fringe'.
78 See `flymake-error-bitmap' and `flymake-warning-bitmap'."
81 :type
'(choice (const left-fringe
)
83 (const :tag
"No fringe indicators" nil
)))
85 (defcustom flymake-compilation-prevents-syntax-check t
86 "If non-nil, don't start syntax check if compilation is running."
90 (defcustom flymake-start-syntax-check-on-newline t
91 "Start syntax check if newline char was added/removed from the buffer."
95 (defcustom flymake-no-changes-timeout
0.5
96 "Time to wait after last change before starting compilation."
100 (defcustom flymake-gui-warnings-enabled t
101 "Enables/disables GUI warnings."
105 (defcustom flymake-start-syntax-check-on-find-file t
106 "Start syntax check on find file."
110 (defcustom flymake-log-level -
1
111 "Logging level, only messages with level lower or equal will be logged.
112 -1 = NONE, 0 = ERROR, 1 = WARNING, 2 = INFO, 3 = DEBUG"
116 (defcustom flymake-xml-program
117 (if (executable-find "xmlstarlet") "xmlstarlet" "xml")
118 "Program to use for XML validation."
123 (defcustom flymake-master-file-dirs
'("." "./src" "./UnitTest")
124 "Dirs where to look for master files."
126 :type
'(repeat (string)))
128 (defcustom flymake-master-file-count-limit
32
129 "Max number of master files to check."
133 (defcustom flymake-allowed-file-name-masks
134 '(("\\.\\(?:c\\(?:pp\\|xx\\|\\+\\+\\)?\\|CC\\)\\'" flymake-simple-make-init
)
135 ("\\.xml\\'" flymake-xml-init
)
136 ("\\.html?\\'" flymake-xml-init
)
137 ("\\.cs\\'" flymake-simple-make-init
)
138 ("\\.p[ml]\\'" flymake-perl-init
)
139 ("\\.php[345]?\\'" flymake-php-init
)
140 ("\\.h\\'" flymake-master-make-header-init flymake-master-cleanup
)
141 ("\\.java\\'" flymake-simple-make-java-init flymake-simple-java-cleanup
)
142 ("[0-9]+\\.tex\\'" flymake-master-tex-init flymake-master-cleanup
)
143 ("\\.tex\\'" flymake-simple-tex-init
)
144 ("\\.idl\\'" flymake-simple-make-init
)
147 ;; ("\\.h\\'" 2 ("\\.cpp\\'" "\\.c\\'")
148 ;; ("[ \t]*#[ \t]*include[ \t]*\"\\([\w0-9/\\_\.]*[/\\]*\\)\\(%s\\)\"" 1 2))
151 ;; ("[0-9]+\\.tex\\'" 2 ("\\.tex\\'")
152 ;; ("[ \t]*\\input[ \t]*{\\(.*\\)\\(%s\\)}" 1 2 ))
155 "Files syntax checking is allowed for.
156 This is an alist with elements of the form:
157 REGEXP INIT [CLEANUP [NAME]]
158 REGEXP is a regular expression that matches a file name.
159 INIT is the init function to use.
160 CLEANUP is the cleanup function to use, default `flymake-simple-cleanup'.
161 NAME is the file name function to use, default `flymake-get-real-file-name'."
163 :type
'(alist :key-type
(regexp :tag
"File regexp")
165 (list :tag
"Handler functions"
166 (function :tag
"Init function")
167 (choice :tag
"Cleanup function"
168 (const :tag
"flymake-simple-cleanup" nil
)
170 (choice :tag
"Name function"
171 (const :tag
"flymake-get-real-file-name" nil
)
174 (defvar-local flymake-is-running nil
175 "If t, flymake syntax check process is running for the current buffer.")
177 (defvar-local flymake-timer nil
178 "Timer for starting syntax check.")
180 (defvar-local flymake-last-change-time nil
181 "Time of last buffer change.")
183 (defvar-local flymake-check-start-time nil
184 "Time at which syntax check was started.")
186 (defvar-local flymake-check-was-interrupted nil
187 "Non-nil if syntax check was killed by `flymake-compile'.")
189 (defvar-local flymake-err-info nil
190 "Sorted list of line numbers and lists of err info in the form (file, err-text).")
192 (defvar-local flymake-new-err-info nil
193 "Same as `flymake-err-info', effective when a syntax check is in progress.")
195 (defun flymake-log (level text
&rest args
)
196 "Log a message at level LEVEL.
197 If LEVEL is higher than `flymake-log-level', the message is
198 ignored. Otherwise, it is printed using `message'.
199 TEXT is a format control string, and the remaining arguments ARGS
200 are the string substitutions (see the function `format')."
201 (if (<= level flymake-log-level
)
202 (let* ((msg (apply 'format text args
)))
203 (message "%s" msg
))))
205 (defun flymake-ins-after (list pos val
)
206 "Insert VAL into LIST after position POS.
207 POS counts from zero."
208 (let ((tmp (copy-sequence list
)))
209 (setcdr (nthcdr pos tmp
) (cons val
(nthcdr (1+ pos
) tmp
)))
212 (defun flymake-set-at (list pos val
)
213 "Set VAL at position POS in LIST.
214 POS counts from zero."
215 (let ((tmp (copy-sequence list
)))
216 (setcar (nthcdr pos tmp
) val
)
219 (defvar flymake-processes nil
220 "List of currently active flymake processes.")
222 (defvar-local flymake-output-residual nil
)
224 (defun flymake-get-file-name-mode-and-masks (file-name)
225 "Return the corresponding entry from `flymake-allowed-file-name-masks'."
226 (unless (stringp file-name
)
227 (error "Invalid file-name"))
228 (let ((fnm flymake-allowed-file-name-masks
)
229 (mode-and-masks nil
))
230 (while (and (not mode-and-masks
) fnm
)
231 (if (string-match (car (car fnm
)) file-name
)
232 (setq mode-and-masks
(cdr (car fnm
))))
233 (setq fnm
(cdr fnm
)))
234 (flymake-log 3 "file %s, init=%s" file-name
(car mode-and-masks
))
237 (defun flymake-can-syntax-check-file (file-name)
238 "Determine whether we can syntax check FILE-NAME.
239 Return nil if we cannot, non-nil if we can."
240 (if (flymake-get-init-function file-name
) t nil
))
242 (defun flymake-get-init-function (file-name)
243 "Return init function to be used for the file."
244 (let* ((init-f (nth 0 (flymake-get-file-name-mode-and-masks file-name
))))
245 ;;(flymake-log 0 "calling %s" init-f)
246 ;;(funcall init-f (current-buffer))
249 (defun flymake-get-cleanup-function (file-name)
250 "Return cleanup function to be used for the file."
251 (or (nth 1 (flymake-get-file-name-mode-and-masks file-name
))
252 'flymake-simple-cleanup
))
254 (defun flymake-get-real-file-name-function (file-name)
255 (or (nth 2 (flymake-get-file-name-mode-and-masks file-name
))
256 'flymake-get-real-file-name
))
258 (defvar flymake-find-buildfile-cache
(make-hash-table :test
#'equal
))
260 (defun flymake-get-buildfile-from-cache (dir-name)
261 "Look up DIR-NAME in cache and return its associated value.
262 If DIR-NAME is not found, return nil."
263 (gethash dir-name flymake-find-buildfile-cache
))
265 (defun flymake-add-buildfile-to-cache (dir-name buildfile
)
266 "Associate DIR-NAME with BUILDFILE in the buildfile cache."
267 (puthash dir-name buildfile flymake-find-buildfile-cache
))
269 (defun flymake-clear-buildfile-cache ()
270 "Clear the buildfile cache."
271 (clrhash flymake-find-buildfile-cache
))
273 (defun flymake-find-buildfile (buildfile-name source-dir-name
)
274 "Find buildfile starting from current directory.
275 Buildfile includes Makefile, build.xml etc.
276 Return its file name if found, or nil if not found."
277 (or (flymake-get-buildfile-from-cache source-dir-name
)
278 (let* ((file (locate-dominating-file source-dir-name buildfile-name
)))
281 (flymake-log 3 "found buildfile at %s" file
)
282 (flymake-add-buildfile-to-cache source-dir-name file
)
285 (flymake-log 3 "buildfile for %s not found" source-dir-name
)
288 (defun flymake-fix-file-name (name)
289 "Replace all occurrences of '\' with '/'."
291 (setq name
(expand-file-name name
))
292 (setq name
(abbreviate-file-name name
))
293 (setq name
(directory-file-name name
))
296 (defun flymake-same-files (file-name-one file-name-two
)
297 "Check if FILE-NAME-ONE and FILE-NAME-TWO point to same file.
298 Return t if so, nil if not."
299 (equal (flymake-fix-file-name file-name-one
)
300 (flymake-fix-file-name file-name-two
)))
302 ;; This is bound dynamically to pass a parameter to a sort predicate below
303 (defvar flymake-included-file-name
)
305 (defun flymake-find-possible-master-files (file-name master-file-dirs masks
)
306 "Find (by name and location) all possible master files.
308 Name is specified by FILE-NAME and location is specified by
309 MASTER-FILE-DIRS. Master files include .cpp and .c for .h.
310 Files are searched for starting from the .h directory and max
311 max-level parent dirs. File contents are not checked."
312 (let* ((dirs master-file-dirs
)
316 (while (and (not done
) dirs
)
317 (let* ((dir (expand-file-name (car dirs
) (file-name-directory file-name
)))
319 (while (and (file-exists-p dir
) (not done
) masks
)
320 (let* ((mask (car masks
))
321 (dir-files (directory-files dir t mask
)))
323 (flymake-log 3 "dir %s, %d file(s) for mask %s"
324 dir
(length dir-files
) mask
)
325 (while (and (not done
) dir-files
)
326 (when (not (file-directory-p (car dir-files
)))
327 (setq files
(cons (car dir-files
) files
))
328 (when (>= (length files
) flymake-master-file-count-limit
)
329 (flymake-log 3 "master file count limit (%d) reached" flymake-master-file-count-limit
)
331 (setq dir-files
(cdr dir-files
))))
332 (setq masks
(cdr masks
))))
333 (setq dirs
(cdr dirs
)))
335 (let ((flymake-included-file-name (file-name-nondirectory file-name
)))
336 (setq files
(sort files
'flymake-master-file-compare
))))
337 (flymake-log 3 "found %d possible master file(s)" (length files
))
340 (defun flymake-master-file-compare (file-one file-two
)
341 "Compare two files specified by FILE-ONE and FILE-TWO.
342 This function is used in sort to move most possible file names
343 to the beginning of the list (File.h -> File.cpp moved to top)."
344 (and (equal (file-name-sans-extension flymake-included-file-name
)
345 (file-name-base file-one
))
346 (not (equal file-one file-two
))))
348 (defvar flymake-check-file-limit
8192
349 "Maximum number of chars to look at when checking possible master file.
350 Nil means search the entire file.")
352 (defun flymake-check-patch-master-file-buffer
353 (master-file-temp-buffer
354 master-file-name patched-master-file-name
355 source-file-name patched-source-file-name
357 "Check if MASTER-FILE-NAME is a master file for SOURCE-FILE-NAME.
358 If yes, patch a copy of MASTER-FILE-NAME to include PATCHED-SOURCE-FILE-NAME
359 instead of SOURCE-FILE-NAME.
361 For example, foo.cpp is a master file if it includes foo.h.
363 Whether a buffer for MATER-FILE-NAME exists, use it as a source
364 instead of reading master file from disk."
365 (let* ((source-file-nondir (file-name-nondirectory source-file-name
))
366 (source-file-extension (file-name-extension source-file-nondir
))
367 (source-file-nonext (file-name-sans-extension source-file-nondir
))
370 (search-limit flymake-check-file-limit
))
372 (format regexp
; "[ \t]*#[ \t]*include[ \t]*\"\\(.*%s\\)\""
373 ;; Hack for tex files, where \include often excludes .tex.
374 ;; Maybe this is safe generally.
375 (if (and (> (length source-file-extension
) 1)
376 (string-equal source-file-extension
"tex"))
377 (format "%s\\(?:\\.%s\\)?"
378 (regexp-quote source-file-nonext
)
379 (regexp-quote source-file-extension
))
380 (regexp-quote source-file-nondir
))))
382 (with-current-buffer master-file-temp-buffer
383 (if (or (not search-limit
)
384 (> search-limit
(point-max)))
385 (setq search-limit
(point-max)))
386 (flymake-log 3 "checking %s against regexp %s"
387 master-file-name regexp
)
388 (goto-char (point-min))
389 (while (and (< (point) search-limit
)
390 (re-search-forward regexp search-limit t
))
391 (let ((match-beg (match-beginning 1))
392 (match-end (match-end 1)))
394 (flymake-log 3 "found possible match for %s" source-file-nondir
)
395 (setq inc-name
(match-string 1))
396 (and (> (length source-file-extension
) 1)
397 (string-equal source-file-extension
"tex")
398 (not (string-match (format "\\.%s\\'" source-file-extension
)
400 (setq inc-name
(concat inc-name
"." source-file-extension
)))
401 (when (eq t
(compare-strings
402 source-file-nondir nil nil
403 inc-name
(- (length inc-name
)
404 (length source-file-nondir
)) nil
))
405 (flymake-log 3 "inc-name=%s" inc-name
)
406 (when (flymake-check-include source-file-name inc-name
409 ;; replace-match is not used here as it fails in
410 ;; XEmacs with 'last match not a buffer' error as
411 ;; check-includes calls replace-in-string
412 (flymake-replace-region
414 (file-name-nondirectory patched-source-file-name
))))
417 (flymake-save-buffer-in-file patched-master-file-name
)))
418 ;;+(flymake-log 3 "killing buffer %s"
419 ;; (buffer-name master-file-temp-buffer))
420 (kill-buffer master-file-temp-buffer
))
421 ;;+(flymake-log 3 "check-patch master file %s: %s" master-file-name found)
423 (flymake-log 2 "found master file %s" master-file-name
))
427 (defun flymake-replace-region (beg end rep
)
428 "Replace text in BUFFER in region (BEG END) with REP."
431 ;; Insert before deleting, so as to better preserve markers's positions.
433 (delete-region beg end
)))
435 (defun flymake-read-file-to-temp-buffer (file-name)
436 "Insert contents of FILE-NAME into newly created temp buffer."
437 (let* ((temp-buffer (get-buffer-create (generate-new-buffer-name (concat "flymake:" (file-name-nondirectory file-name
))))))
438 (with-current-buffer temp-buffer
439 (insert-file-contents file-name
))
442 (defun flymake-copy-buffer-to-temp-buffer (buffer)
443 "Copy contents of BUFFER into newly created temp buffer."
445 (get-buffer-create (generate-new-buffer-name
446 (concat "flymake:" (buffer-name buffer
))))
447 (insert-buffer-substring buffer
)
450 (defun flymake-check-include (source-file-name inc-name include-dirs
)
451 "Check if SOURCE-FILE-NAME can be found in include path.
452 Return t if it can be found via include path using INC-NAME."
453 (if (file-name-absolute-p inc-name
)
454 (flymake-same-files source-file-name inc-name
)
455 (while (and include-dirs
456 (not (flymake-same-files
458 (concat (file-name-directory source-file-name
)
459 "/" (car include-dirs
)
461 (setq include-dirs
(cdr include-dirs
)))
464 (defun flymake-find-buffer-for-file (file-name)
465 "Check if there exists a buffer visiting FILE-NAME.
466 Return t if so, nil if not."
467 (let ((buffer-name (get-file-buffer file-name
)))
469 (get-buffer buffer-name
))))
471 (defun flymake-create-master-file (source-file-name patched-source-file-name get-incl-dirs-f create-temp-f masks include-regexp
)
472 "Save SOURCE-FILE-NAME with a different name.
473 Find master file, patch and save it."
474 (let* ((possible-master-files (flymake-find-possible-master-files source-file-name flymake-master-file-dirs masks
))
475 (master-file-count (length possible-master-files
))
478 (master-file-name nil
)
479 (patched-master-file-name nil
)
482 (while (and (not found
) (< idx master-file-count
))
483 (setq master-file-name
(nth idx possible-master-files
))
484 (setq patched-master-file-name
(funcall create-temp-f master-file-name
"flymake_master"))
485 (if (flymake-find-buffer-for-file master-file-name
)
486 (setq temp-buffer
(flymake-copy-buffer-to-temp-buffer (flymake-find-buffer-for-file master-file-name
)))
487 (setq temp-buffer
(flymake-read-file-to-temp-buffer master-file-name
)))
489 (flymake-check-patch-master-file-buffer
492 patched-master-file-name
494 patched-source-file-name
495 (funcall get-incl-dirs-f
(file-name-directory master-file-name
))
499 (list master-file-name patched-master-file-name
)
501 (flymake-log 3 "none of %d master file(s) checked includes %s" master-file-count
502 (file-name-nondirectory source-file-name
))
505 (defun flymake-save-buffer-in-file (file-name)
506 "Save the entire buffer contents into file FILE-NAME.
507 Create parent directories as needed."
508 (make-directory (file-name-directory file-name
) 1)
509 (write-region nil nil file-name nil
566)
510 (flymake-log 3 "saved buffer %s in file %s" (buffer-name) file-name
))
512 (defun flymake-save-string-to-file (file-name data
)
513 "Save string DATA to file FILE-NAME."
514 (write-region data nil file-name nil
566))
516 (defun flymake-read-file-to-string (file-name)
517 "Read contents of file FILE-NAME and return as a string."
519 (insert-file-contents file-name
)
520 (buffer-substring (point-min) (point-max))))
522 (defun flymake-process-filter (process output
)
523 "Parse OUTPUT and highlight error lines.
524 It's flymake process filter."
525 (let ((source-buffer (process-buffer process
)))
527 (flymake-log 3 "received %d byte(s) of output from process %d"
528 (length output
) (process-id process
))
529 (when (buffer-live-p source-buffer
)
530 (with-current-buffer source-buffer
531 (flymake-parse-output-and-residual output
)))))
533 (defun flymake-process-sentinel (process _event
)
534 "Sentinel for syntax check buffers."
535 (when (memq (process-status process
) '(signal exit
))
536 (let* ((exit-status (process-exit-status process
))
537 (command (process-command process
))
538 (source-buffer (process-buffer process
))
539 (cleanup-f (flymake-get-cleanup-function (buffer-file-name source-buffer
))))
541 (flymake-log 2 "process %d exited with code %d"
542 (process-id process
) exit-status
)
545 (flymake-log 3 "cleaning up using %s" cleanup-f
)
546 (when (buffer-live-p source-buffer
)
547 (with-current-buffer source-buffer
548 (funcall cleanup-f
)))
550 (delete-process process
)
551 (setq flymake-processes
(delq process flymake-processes
))
553 (when (buffer-live-p source-buffer
)
554 (with-current-buffer source-buffer
556 (flymake-parse-residual)
557 (flymake-post-syntax-check exit-status command
)
558 (setq flymake-is-running nil
))))
560 (let ((err-str (format "Error in process sentinel for buffer %s: %s"
561 source-buffer
(error-message-string err
))))
562 (flymake-log 0 err-str
)
563 (with-current-buffer source-buffer
564 (setq flymake-is-running nil
))))))))
566 (defun flymake-post-syntax-check (exit-status command
)
567 (setq flymake-err-info flymake-new-err-info
)
568 (setq flymake-new-err-info nil
)
569 (setq flymake-err-info
570 (flymake-fix-line-numbers
571 flymake-err-info
1 (count-lines (point-min) (point-max))))
572 (flymake-delete-own-overlays)
573 (flymake-highlight-err-lines flymake-err-info
)
574 (let (err-count warn-count
)
575 (setq err-count
(flymake-get-err-count flymake-err-info
"e"))
576 (setq warn-count
(flymake-get-err-count flymake-err-info
"w"))
577 (flymake-log 2 "%s: %d error(s), %d warning(s) in %.2f second(s)"
578 (buffer-name) err-count warn-count
579 (- (float-time) flymake-check-start-time
))
580 (setq flymake-check-start-time nil
)
582 (if (and (equal 0 err-count
) (equal 0 warn-count
))
583 (if (equal 0 exit-status
)
584 (flymake-report-status "" "") ; PASSED
585 (if (not flymake-check-was-interrupted
)
586 (flymake-report-fatal-status "CFGERR"
587 (format "Configuration error has occurred while running %s" command
))
588 (flymake-report-status nil
""))) ; "STOPPED"
589 (flymake-report-status (format "%d/%d" err-count warn-count
) ""))))
591 (defun flymake-parse-output-and-residual (output)
592 "Split OUTPUT into lines, merge in residual if necessary."
593 (let* ((buffer-residual flymake-output-residual
)
594 (total-output (if buffer-residual
(concat buffer-residual output
) output
))
595 (lines-and-residual (flymake-split-output total-output
))
596 (lines (nth 0 lines-and-residual
))
597 (new-residual (nth 1 lines-and-residual
)))
598 (setq flymake-output-residual new-residual
)
599 (setq flymake-new-err-info
600 (flymake-parse-err-lines
601 flymake-new-err-info lines
))))
603 (defun flymake-parse-residual ()
604 "Parse residual if it's non empty."
605 (when flymake-output-residual
606 (setq flymake-new-err-info
607 (flymake-parse-err-lines
609 (list flymake-output-residual
)))
610 (setq flymake-output-residual nil
)))
612 (defun flymake-er-make-er (line-no line-err-info-list
)
613 (list line-no line-err-info-list
))
615 (defun flymake-er-get-line (err-info)
618 (defun flymake-er-get-line-err-info-list (err-info)
621 (cl-defstruct (flymake-ler
623 (:constructor flymake-ler-make-ler
(file line type text
&optional full-file
)))
624 file line type text full-file
)
626 (defun flymake-ler-set-file (line-err-info file
)
627 (flymake-ler-make-ler file
628 (flymake-ler-line line-err-info
)
629 (flymake-ler-type line-err-info
)
630 (flymake-ler-text line-err-info
)
631 (flymake-ler-full-file line-err-info
)))
633 (defun flymake-ler-set-full-file (line-err-info full-file
)
634 (flymake-ler-make-ler (flymake-ler-file line-err-info
)
635 (flymake-ler-line line-err-info
)
636 (flymake-ler-type line-err-info
)
637 (flymake-ler-text line-err-info
)
640 (defun flymake-ler-set-line (line-err-info line
)
641 (flymake-ler-make-ler (flymake-ler-file line-err-info
)
643 (flymake-ler-type line-err-info
)
644 (flymake-ler-text line-err-info
)
645 (flymake-ler-full-file line-err-info
)))
647 (defun flymake-get-line-err-count (line-err-info-list type
)
648 "Return number of errors of specified TYPE.
649 Value of TYPE is either \"e\" or \"w\"."
651 (count (length line-err-info-list
))
655 (when (equal type
(flymake-ler-type (nth idx line-err-info-list
)))
656 (setq err-count
(1+ err-count
)))
660 (defun flymake-get-err-count (err-info-list type
)
661 "Return number of errors of specified TYPE for ERR-INFO-LIST."
663 (count (length err-info-list
))
666 (setq err-count
(+ err-count
(flymake-get-line-err-count (nth 1 (nth idx err-info-list
)) type
)))
670 (defun flymake-fix-line-numbers (err-info-list min-line max-line
)
671 "Replace line numbers with fixed value.
672 If line-numbers is less than MIN-LINE, set line numbers to MIN-LINE.
673 If line numbers is greater than MAX-LINE, set line numbers to MAX-LINE.
674 The reason for this fix is because some compilers might report
675 line number outside the file being compiled."
676 (let* ((count (length err-info-list
))
680 (setq err-info
(nth (1- count
) err-info-list
))
681 (setq line
(flymake-er-get-line err-info
))
682 (when (or (< line min-line
) (> line max-line
))
683 (setq line
(if (< line min-line
) min-line max-line
))
684 (setq err-info-list
(flymake-set-at err-info-list
(1- count
)
685 (flymake-er-make-er line
686 (flymake-er-get-line-err-info-list err-info
)))))
687 (setq count
(1- count
))))
690 (defun flymake-highlight-err-lines (err-info-list)
691 "Highlight error lines in BUFFER using info from ERR-INFO-LIST."
693 (dolist (err err-info-list
)
694 (flymake-highlight-line (car err
) (nth 1 err
)))))
696 (defun flymake-overlay-p (ov)
697 "Determine whether overlay OV was created by flymake."
698 (and (overlayp ov
) (overlay-get ov
'flymake-overlay
)))
700 (defun flymake-make-overlay (beg end tooltip-text face bitmap mouse-face
)
701 "Allocate a flymake overlay in range BEG and END."
702 (when (not (flymake-region-has-flymake-overlays beg end
))
703 (let ((ov (make-overlay beg end nil t t
))
704 (fringe (and flymake-fringe-indicator-position
705 (propertize "!" 'display
706 (cons flymake-fringe-indicator-position
710 (overlay-put ov
'face face
)
711 (overlay-put ov
'mouse-face mouse-face
)
712 (overlay-put ov
'help-echo tooltip-text
)
713 (overlay-put ov
'flymake-overlay t
)
714 (overlay-put ov
'priority
100)
715 (overlay-put ov
'evaporate t
)
716 (overlay-put ov
'before-string fringe
)
717 ;;+(flymake-log 3 "created overlay %s" ov)
719 (flymake-log 3 "created an overlay at (%d-%d)" beg end
)))
721 (defun flymake-delete-own-overlays ()
722 "Delete all flymake overlays in BUFFER."
723 (dolist (ol (overlays-in (point-min) (point-max)))
724 (when (flymake-overlay-p ol
)
726 ;;+(flymake-log 3 "deleted overlay %s" ol)
729 (defun flymake-region-has-flymake-overlays (beg end
)
730 "Check if region specified by BEG and END has overlay.
731 Return t if it has at least one flymake overlay, nil if no overlay."
732 (let ((ov (overlays-in beg end
))
733 (has-flymake-overlays nil
))
735 (when (flymake-overlay-p (car ov
))
736 (setq has-flymake-overlays t
))
738 has-flymake-overlays
))
740 (defface flymake-errline
741 '((((supports :underline
(:style wave
)))
742 :underline
(:style wave
:color
"Red1"))
745 "Face used for marking error lines."
749 (defface flymake-warnline
750 '((((supports :underline
(:style wave
)))
751 :underline
(:style wave
:color
"DarkOrange"))
754 "Face used for marking warning lines."
758 (defun flymake-highlight-line (line-no line-err-info-list
)
759 "Highlight line LINE-NO in current buffer.
760 Perhaps use text from LINE-ERR-INFO-LIST to enhance highlighting."
761 (goto-char (point-min))
762 (forward-line (1- line-no
))
763 (let* ((line-beg (point-at-bol))
764 (line-end (point-at-eol))
767 (tooltip-text (flymake-ler-text (nth 0 line-err-info-list
)))
772 (while (looking-at "[ \t]")
778 (while (and (looking-at "[ \t\r\n]") (> (point) 1))
781 (setq end
(1+ (point)))
792 (if (> (flymake-get-line-err-count line-err-info-list
"e") 0)
793 (setq face
'flymake-errline
794 bitmap flymake-error-bitmap
)
795 (setq face
'flymake-warnline
796 bitmap flymake-warning-bitmap
))
798 (flymake-make-overlay beg end tooltip-text face bitmap nil
)))
800 (defun flymake-parse-err-lines (err-info-list lines
)
801 "Parse err LINES, store info in ERR-INFO-LIST."
802 (let* ((count (length lines
))
806 (source-file-name buffer-file-name
)
807 (get-real-file-name-f (flymake-get-real-file-name-function source-file-name
)))
810 (setq line-err-info
(flymake-parse-line (nth idx lines
)))
812 (setq real-file-name
(funcall get-real-file-name-f
813 (flymake-ler-file line-err-info
)))
814 (setq line-err-info
(flymake-ler-set-full-file line-err-info real-file-name
))
816 (when (flymake-same-files real-file-name source-file-name
)
817 (setq line-err-info
(flymake-ler-set-file line-err-info nil
))
818 (setq err-info-list
(flymake-add-err-info err-info-list line-err-info
))))
819 (flymake-log 3 "parsed '%s', %s line-err-info" (nth idx lines
) (if line-err-info
"got" "no"))
823 (defun flymake-split-output (output)
824 "Split OUTPUT into lines.
825 Return last one as residual if it does not end with newline char.
826 Returns ((LINES) RESIDUAL)."
827 (when (and output
(> (length output
) 0))
828 (let* ((lines (split-string output
"[\n\r]+" t
))
829 (complete (equal "\n" (char-to-string (aref output
(1- (length output
))))))
832 (setq residual
(car (last lines
)))
833 (setq lines
(butlast lines
)))
834 (list lines residual
))))
836 (defun flymake-reformat-err-line-patterns-from-compile-el (original-list)
837 "Grab error line patterns from ORIGINAL-LIST in compile.el format.
838 Convert it to flymake internal format."
839 (let* ((converted-list '()))
840 (dolist (item original-list
)
841 (setq item
(cdr item
))
842 (let ((regexp (nth 0 item
))
846 (if (consp file
) (setq file
(car file
)))
847 (if (consp line
) (setq line
(car line
)))
848 (if (consp col
) (setq col
(car col
)))
850 (when (not (functionp line
))
851 (setq converted-list
(cons (list regexp file line col
) converted-list
)))))
856 (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
860 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\)) \: \\(\\(error\\|warning\\|fatal error\\) \\(C[0-9]+\\):[ \t\n]*\\(.+\\)\\)"
863 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)\:\\([0-9]+\\)\:[0-9]+\:[0-9]+\:[0-9]+\: \\(\\(Error\\|Warning\\|Caution\\|Semantic Error\\):[ \t\n]*\\(.+\\)\\)"
866 ("midl[ ]*:[ ]*\\(command line error .*\\)"
869 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\),[0-9]+)\: \\(\\(error\\|warning\\|fatal error\\) \\(CS[0-9]+\\):[ \t\n]*\\(.+\\)\\)"
872 ("\\(.*\\) at \\([^ \n]+\\) line \\([0-9]+\\)[,.\n]" 2 3 nil
1)
874 ("\\(?:Parse\\|Fatal\\) error: \\(.*\\) in \\(.*\\) on line \\([0-9]+\\)" 2 3 nil
1)
875 ;; LaTeX warnings (fileless) ("\\(LaTeX \\(Warning\\|Error\\): .*\\) on input line \\([0-9]+\\)" 20 3 nil 1)
876 ;; ant/javac. Note this also matches gcc warnings!
877 (" *\\(\\[javac\\] *\\)?\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)\:\\([0-9]+\\)\\(?:\:[0-9]+\\)?\:[ \t\n]*\\(.+\\)"
879 ;; compilation-error-regexp-alist)
880 (flymake-reformat-err-line-patterns-from-compile-el compilation-error-regexp-alist-alist
))
881 "Patterns for matching error/warning lines. Each pattern has the form
882 \(REGEXP FILE-IDX LINE-IDX COL-IDX ERR-TEXT-IDX).
883 Use `flymake-reformat-err-line-patterns-from-compile-el' to add patterns
886 ;;(defcustom flymake-err-line-patterns
888 ;; ; MS Visual C++ 6.0
889 ;; ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\)) \: \\(\\(error\\|warning\\|fatal error\\) \\(C[0-9]+\\):[ \t\n]*\\(.+\\)\\)"
892 ;; ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)\:\\([0-9]+\\)\:[0-9]+\:[0-9]+\:[0-9]+\: \\(\\(Error\\|Warning\\|Caution\\):[ \t\n]*\\(.+\\)\\)"
894 ;; "patterns for matching error/warning lines, (regexp file-idx line-idx err-text-idx)"
896 ;; :type '(repeat (string number number number))
899 (define-obsolete-variable-alias 'flymake-warning-re
'flymake-warning-predicate
"24.4")
900 (defvar flymake-warning-predicate
"^[wW]arning"
901 "Predicate matching against error text to detect a warning.
902 Takes a single argument, the error's text and should return non-nil
904 Instead of a function, it can also be a regular expression.")
906 (defun flymake-parse-line (line)
907 "Parse LINE to see if it is an error or warning.
908 Return its components if so, nil otherwise."
909 (let ((raw-file-name nil
)
913 (patterns flymake-err-line-patterns
)
915 (while (and patterns
(not matched
))
916 (when (string-match (car (car patterns
)) line
)
917 (let* ((file-idx (nth 1 (car patterns
)))
918 (line-idx (nth 2 (car patterns
))))
920 (setq raw-file-name
(if file-idx
(match-string file-idx line
) nil
))
921 (setq line-no
(if line-idx
(string-to-number
922 (match-string line-idx line
)) 0))
923 (setq err-text
(if (> (length (car patterns
)) 4)
924 (match-string (nth 4 (car patterns
)) line
)
925 (flymake-patch-err-text
926 (substring line
(match-end 0)))))
928 (setq err-text
"<no error text>")
929 (when (cond ((stringp flymake-warning-predicate
)
930 (string-match flymake-warning-predicate err-text
))
931 ((functionp flymake-warning-predicate
)
932 (funcall flymake-warning-predicate err-text
)))
933 (setq err-type
"w")))
935 3 "parse line: file-idx=%s line-idx=%s file=%s line=%s text=%s"
936 file-idx line-idx raw-file-name line-no err-text
)
938 (setq patterns
(cdr patterns
)))
940 (flymake-ler-make-ler raw-file-name line-no err-type err-text
)
943 (defun flymake-find-err-info (err-info-list line-no
)
944 "Find (line-err-info-list pos) for specified LINE-NO."
946 (let* ((line-err-info-list nil
)
948 (count (length err-info-list
)))
950 (while (and (< pos count
) (< (car (nth pos err-info-list
)) line-no
))
952 (when (and (< pos count
) (equal (car (nth pos err-info-list
)) line-no
))
953 (setq line-err-info-list
(flymake-er-get-line-err-info-list (nth pos err-info-list
))))
954 (list line-err-info-list pos
))
957 (defun flymake-line-err-info-is-less-or-equal (line-one line-two
)
958 (or (string< (flymake-ler-type line-one
) (flymake-ler-type line-two
))
959 (and (string= (flymake-ler-type line-one
) (flymake-ler-type line-two
))
960 (not (flymake-ler-file line-one
)) (flymake-ler-file line-two
))
961 (and (string= (flymake-ler-type line-one
) (flymake-ler-type line-two
))
962 (or (and (flymake-ler-file line-one
) (flymake-ler-file line-two
))
963 (and (not (flymake-ler-file line-one
)) (not (flymake-ler-file line-two
)))))))
965 (defun flymake-add-line-err-info (line-err-info-list line-err-info
)
966 "Update LINE-ERR-INFO-LIST with the error LINE-ERR-INFO.
967 For the format of LINE-ERR-INFO, see `flymake-ler-make-ler'.
968 The new element is inserted in the proper position, according to
969 the predicate `flymake-line-err-info-is-less-or-equal'.
970 The updated value of LINE-ERR-INFO-LIST is returned."
971 (if (not line-err-info-list
)
973 (let* ((count (length line-err-info-list
))
975 (while (and (< idx count
) (flymake-line-err-info-is-less-or-equal (nth idx line-err-info-list
) line-err-info
))
977 (cond ((equal 0 idx
) (setq line-err-info-list
(cons line-err-info line-err-info-list
)))
978 (t (setq line-err-info-list
(flymake-ins-after line-err-info-list
(1- idx
) line-err-info
))))
979 line-err-info-list
)))
981 (defun flymake-add-err-info (err-info-list line-err-info
)
982 "Update ERR-INFO-LIST with the error LINE-ERR-INFO, preserving sort order.
983 Returns the updated value of ERR-INFO-LIST.
984 For the format of ERR-INFO-LIST, see `flymake-err-info'.
985 For the format of LINE-ERR-INFO, see `flymake-ler-make-ler'."
986 (let* ((line-no (if (flymake-ler-file line-err-info
) 1 (flymake-ler-line line-err-info
)))
987 (info-and-pos (flymake-find-err-info err-info-list line-no
))
988 (exists (car info-and-pos
))
989 (pos (nth 1 info-and-pos
))
990 (line-err-info-list nil
)
994 (setq line-err-info-list
(flymake-er-get-line-err-info-list (car (nthcdr pos err-info-list
)))))
995 (setq line-err-info-list
(flymake-add-line-err-info line-err-info-list line-err-info
))
997 (setq err-info
(flymake-er-make-er line-no line-err-info-list
))
998 (cond (exists (setq err-info-list
(flymake-set-at err-info-list pos err-info
)))
999 ((equal 0 pos
) (setq err-info-list
(cons err-info err-info-list
)))
1000 (t (setq err-info-list
(flymake-ins-after err-info-list
(1- pos
) err-info
))))
1003 (defun flymake-get-project-include-dirs-imp (basedir)
1004 "Include dirs for the project current file belongs to."
1005 (if (flymake-get-project-include-dirs-from-cache basedir
)
1007 (flymake-get-project-include-dirs-from-cache basedir
))
1009 (let* ((command-line (concat "make -C "
1010 (shell-quote-argument basedir
)
1011 " DUMPVARS=INCLUDE_DIRS dumpvars"))
1012 (output (shell-command-to-string command-line
))
1013 (lines (split-string output
"\n" t
))
1014 (count (length lines
))
1017 (while (and (< idx count
) (not (string-match "^INCLUDE_DIRS=.*" (nth idx lines
))))
1018 (setq idx
(1+ idx
)))
1020 (let* ((inc-lines (split-string (nth idx lines
) " *-I" t
))
1021 (inc-count (length inc-lines
)))
1022 (while (> inc-count
0)
1023 (when (not (string-match "^INCLUDE_DIRS=.*" (nth (1- inc-count
) inc-lines
)))
1024 (push (replace-regexp-in-string "\"" "" (nth (1- inc-count
) inc-lines
)) inc-dirs
))
1025 (setq inc-count
(1- inc-count
)))))
1026 (flymake-add-project-include-dirs-to-cache basedir inc-dirs
)
1029 (defvar flymake-get-project-include-dirs-function
#'flymake-get-project-include-dirs-imp
1030 "Function used to get project include dirs, one parameter: basedir name.")
1032 (defun flymake-get-project-include-dirs (basedir)
1033 (funcall flymake-get-project-include-dirs-function basedir
))
1035 (defun flymake-get-system-include-dirs ()
1036 "System include dirs - from the 'INCLUDE' env setting."
1037 (let* ((includes (getenv "INCLUDE")))
1038 (if includes
(split-string includes path-separator t
) nil
)))
1040 (defvar flymake-project-include-dirs-cache
(make-hash-table :test
#'equal
))
1042 (defun flymake-get-project-include-dirs-from-cache (base-dir)
1043 (gethash base-dir flymake-project-include-dirs-cache
))
1045 (defun flymake-add-project-include-dirs-to-cache (base-dir include-dirs
)
1046 (puthash base-dir include-dirs flymake-project-include-dirs-cache
))
1048 (defun flymake-clear-project-include-dirs-cache ()
1049 (clrhash flymake-project-include-dirs-cache
))
1051 (defun flymake-get-include-dirs (base-dir)
1052 "Get dirs to use when resolving local file names."
1053 (let* ((include-dirs (append '(".") (flymake-get-project-include-dirs base-dir
) (flymake-get-system-include-dirs))))
1056 ;; (defun flymake-restore-formatting ()
1057 ;; "Remove any formatting made by flymake."
1060 ;; (defun flymake-get-program-dir (buffer)
1061 ;; "Get dir to start program in."
1062 ;; (unless (bufferp buffer)
1063 ;; (error "Invalid buffer"))
1064 ;; (with-current-buffer buffer
1065 ;; default-directory))
1067 (defun flymake-safe-delete-file (file-name)
1068 (when (and file-name
(file-exists-p file-name
))
1069 (delete-file file-name
)
1070 (flymake-log 1 "deleted file %s" file-name
)))
1072 (defun flymake-safe-delete-directory (dir-name)
1075 (delete-directory dir-name
)
1076 (flymake-log 1 "deleted dir %s" dir-name
))
1078 (flymake-log 1 "Failed to delete dir %s, error ignored" dir-name
))))
1080 (defun flymake-start-syntax-check ()
1081 "Start syntax checking for current buffer."
1083 (flymake-log 3 "flymake is running: %s" flymake-is-running
)
1084 (when (and (not flymake-is-running
)
1085 (flymake-can-syntax-check-file buffer-file-name
))
1086 (when (or (not flymake-compilation-prevents-syntax-check
)
1087 (not (flymake-compilation-is-running))) ;+ (flymake-rep-ort-status buffer "COMP")
1088 (flymake-clear-buildfile-cache)
1089 (flymake-clear-project-include-dirs-cache)
1091 (setq flymake-check-was-interrupted nil
)
1093 (let* ((source-file-name buffer-file-name
)
1094 (init-f (flymake-get-init-function source-file-name
))
1095 (cleanup-f (flymake-get-cleanup-function source-file-name
))
1096 (cmd-and-args (funcall init-f
))
1097 (cmd (nth 0 cmd-and-args
))
1098 (args (nth 1 cmd-and-args
))
1099 (dir (nth 2 cmd-and-args
)))
1100 (if (not cmd-and-args
)
1102 (flymake-log 0 "init function %s for %s failed, cleaning up" init-f source-file-name
)
1103 (funcall cleanup-f
))
1105 (setq flymake-last-change-time nil
)
1106 (flymake-start-syntax-check-process cmd args dir
)))))))
1108 (defun flymake-start-syntax-check-process (cmd args dir
)
1109 "Start syntax check process."
1112 (let ((default-directory (or dir default-directory
)))
1114 (flymake-log 3 "starting process on dir %s" dir
))
1115 (apply 'start-file-process
1116 "flymake-proc" (current-buffer) cmd args
))))
1117 (set-process-sentinel process
'flymake-process-sentinel
)
1118 (set-process-filter process
'flymake-process-filter
)
1119 (push process flymake-processes
)
1121 (setq flymake-is-running t
)
1122 (setq flymake-last-change-time nil
)
1123 (setq flymake-check-start-time
(float-time))
1125 (flymake-report-status nil
"*")
1126 (flymake-log 2 "started process %d, command=%s, dir=%s"
1127 (process-id process
) (process-command process
)
1131 (let* ((err-str (format "Failed to launch syntax check process '%s' with args %s: %s"
1132 cmd args
(error-message-string err
)))
1133 (source-file-name buffer-file-name
)
1134 (cleanup-f (flymake-get-cleanup-function source-file-name
)))
1135 (flymake-log 0 err-str
)
1137 (flymake-report-fatal-status "PROCERR" err-str
)))))
1139 (defun flymake-kill-process (proc)
1140 "Kill process PROC."
1142 (let* ((buf (process-buffer proc
)))
1143 (when (buffer-live-p buf
)
1144 (with-current-buffer buf
1145 (setq flymake-check-was-interrupted t
))))
1146 (flymake-log 1 "killed process %d" (process-id proc
)))
1148 (defun flymake-stop-all-syntax-checks ()
1149 "Kill all syntax check processes."
1151 (while flymake-processes
1152 (flymake-kill-process (pop flymake-processes
))))
1154 (defun flymake-compilation-is-running ()
1155 (and (boundp 'compilation-in-progress
)
1156 compilation-in-progress
))
1158 (defun flymake-compile ()
1159 "Kill all flymake syntax checks, start compilation."
1161 (flymake-stop-all-syntax-checks)
1162 (call-interactively 'compile
))
1164 (defun flymake-on-timer-event (buffer)
1165 "Start a syntax check for buffer BUFFER if necessary."
1166 (when (buffer-live-p buffer
)
1167 (with-current-buffer buffer
1168 (when (and (not flymake-is-running
)
1169 flymake-last-change-time
1170 (> (- (float-time) flymake-last-change-time
)
1171 flymake-no-changes-timeout
))
1173 (setq flymake-last-change-time nil
)
1174 (flymake-log 3 "starting syntax check as more than 1 second passed since last change")
1175 (flymake-start-syntax-check)))))
1177 (define-obsolete-function-alias 'flymake-display-err-menu-for-current-line
1178 'flymake-popup-current-error-menu
"24.4")
1180 (defun flymake-popup-current-error-menu (&optional event
)
1181 "Pop up a menu with errors/warnings for current line."
1182 (interactive (list last-nonmenu-event
))
1183 (let* ((line-no (line-number-at-pos))
1184 (errors (or (car (flymake-find-err-info flymake-err-info line-no
))
1185 (user-error "No errors for current line")))
1186 (menu (mapcar (lambda (x)
1187 (if (flymake-ler-file x
)
1188 (cons (format "%s - %s(%d)"
1189 (flymake-ler-text x
)
1190 (flymake-ler-file x
)
1191 (flymake-ler-line x
))
1193 (list (flymake-ler-text x
))))
1195 (event (if (mouse-event-p event
)
1197 (list 'mouse-1
(posn-at-point))))
1198 (title (format "Line %d: %d error(s), %d warning(s)"
1200 (flymake-get-line-err-count errors
"e")
1201 (flymake-get-line-err-count errors
"w")))
1202 (choice (x-popup-menu event
(list title
(cons "" menu
)))))
1203 (flymake-log 3 "choice=%s" choice
)
1205 (flymake-goto-file-and-line (flymake-ler-full-file choice
)
1206 (flymake-ler-line choice
)))))
1208 (defun flymake-goto-file-and-line (file line
)
1209 "Try to get buffer for FILE and goto line LINE in it."
1210 (if (not (file-exists-p file
))
1211 (flymake-log 1 "File %s does not exist" file
)
1213 (goto-char (point-min))
1214 (forward-line (1- line
))))
1216 ;; flymake minor mode declarations
1217 (defvar-local flymake-mode-line nil
)
1218 (defvar-local flymake-mode-line-e-w nil
)
1219 (defvar-local flymake-mode-line-status nil
)
1221 (defun flymake-report-status (e-w &optional status
)
1222 "Show status in mode line."
1224 (setq flymake-mode-line-e-w e-w
))
1226 (setq flymake-mode-line-status status
))
1227 (let* ((mode-line " Flymake"))
1228 (when (> (length flymake-mode-line-e-w
) 0)
1229 (setq mode-line
(concat mode-line
":" flymake-mode-line-e-w
)))
1230 (setq mode-line
(concat mode-line flymake-mode-line-status
))
1231 (setq flymake-mode-line mode-line
)
1232 (force-mode-line-update)))
1234 (defun flymake-display-warning (warning)
1235 "Display a warning to user."
1236 (message-box warning
))
1238 (defun flymake-report-fatal-status (status warning
)
1239 "Display a warning and switch flymake mode off."
1240 (when flymake-gui-warnings-enabled
1241 (flymake-display-warning (format "Flymake: %s. Flymake will be switched OFF" warning
))
1244 (flymake-log 0 "switched OFF Flymake mode for buffer %s due to fatal status %s, warning %s"
1245 (buffer-name) status warning
))
1248 (define-minor-mode flymake-mode nil
1249 :group
'flymake
:lighter flymake-mode-line
1252 ;; Turning the mode ON.
1255 ((not buffer-file-name
)
1256 (message "Flymake unable to run without a buffer file name"))
1257 ((not (flymake-can-syntax-check-file buffer-file-name
))
1258 (flymake-log 2 "flymake cannot check syntax in buffer %s" (buffer-name)))
1260 (add-hook 'after-change-functions
'flymake-after-change-function nil t
)
1261 (add-hook 'after-save-hook
'flymake-after-save-hook nil t
)
1262 (add-hook 'kill-buffer-hook
'flymake-kill-buffer-hook nil t
)
1263 ;;+(add-hook 'find-file-hook 'flymake-find-file-hook)
1265 (flymake-report-status "" "")
1268 (run-at-time nil
1 'flymake-on-timer-event
(current-buffer)))
1270 (when (and flymake-start-syntax-check-on-find-file
1271 ;; Since we write temp files in current dir, there's no point
1272 ;; trying if the directory is read-only (bug#8954).
1273 (file-writable-p (file-name-directory buffer-file-name
)))
1274 (with-demoted-errors
1275 (flymake-start-syntax-check))))))
1277 ;; Turning the mode OFF.
1279 (remove-hook 'after-change-functions
'flymake-after-change-function t
)
1280 (remove-hook 'after-save-hook
'flymake-after-save-hook t
)
1281 (remove-hook 'kill-buffer-hook
'flymake-kill-buffer-hook t
)
1282 ;;+(remove-hook 'find-file-hook (function flymake-find-file-hook) t)
1284 (flymake-delete-own-overlays)
1287 (cancel-timer flymake-timer
)
1288 (setq flymake-timer nil
))
1290 (setq flymake-is-running nil
))))
1293 (defun flymake-mode-on ()
1294 "Turn flymake mode on."
1296 (flymake-log 1 "flymake mode turned ON for buffer %s" (buffer-name)))
1299 (defun flymake-mode-off ()
1300 "Turn flymake mode off."
1302 (flymake-log 1 "flymake mode turned OFF for buffer %s" (buffer-name)))
1304 (defun flymake-after-change-function (start stop _len
)
1305 "Start syntax check for current buffer if it isn't already running."
1306 ;;+(flymake-log 0 "setting change time to %s" (float-time))
1307 (let((new-text (buffer-substring start stop
)))
1308 (when (and flymake-start-syntax-check-on-newline
(equal new-text
"\n"))
1309 (flymake-log 3 "starting syntax check as new-line has been seen")
1310 (flymake-start-syntax-check))
1311 (setq flymake-last-change-time
(float-time))))
1313 (defun flymake-after-save-hook ()
1314 (if (local-variable-p 'flymake-mode
(current-buffer)) ; (???) other way to determine whether flymake is active in buffer being saved?
1316 (flymake-log 3 "starting syntax check as buffer was saved")
1317 (flymake-start-syntax-check)))) ; no more mode 3. cannot start check if mode 3 (to temp copies) is active - (???)
1319 (defun flymake-kill-buffer-hook ()
1321 (cancel-timer flymake-timer
)
1322 (setq flymake-timer nil
)))
1325 (defun flymake-find-file-hook ()
1326 ;;+(when flymake-start-syntax-check-on-find-file
1327 ;;+ (flymake-log 3 "starting syntax check on file open")
1328 ;;+ (flymake-start-syntax-check)
1330 (when (and (not (local-variable-p 'flymake-mode
(current-buffer)))
1331 (flymake-can-syntax-check-file buffer-file-name
))
1333 (flymake-log 3 "automatically turned ON flymake mode")))
1335 (defun flymake-get-first-err-line-no (err-info-list)
1336 "Return first line with error."
1338 (flymake-er-get-line (car err-info-list
))))
1340 (defun flymake-get-last-err-line-no (err-info-list)
1341 "Return last line with error."
1343 (flymake-er-get-line (nth (1- (length err-info-list
)) err-info-list
))))
1345 (defun flymake-get-next-err-line-no (err-info-list line-no
)
1346 "Return next line with error."
1348 (let* ((count (length err-info-list
))
1350 (while (and (< idx count
) (>= line-no
(flymake-er-get-line (nth idx err-info-list
))))
1351 (setq idx
(1+ idx
)))
1353 (flymake-er-get-line (nth idx err-info-list
))))))
1355 (defun flymake-get-prev-err-line-no (err-info-list line-no
)
1356 "Return previous line with error."
1358 (let* ((count (length err-info-list
)))
1359 (while (and (> count
0) (<= line-no
(flymake-er-get-line (nth (1- count
) err-info-list
))))
1360 (setq count
(1- count
)))
1362 (flymake-er-get-line (nth (1- count
) err-info-list
))))))
1364 (defun flymake-skip-whitespace ()
1365 "Move forward until non-whitespace is reached."
1366 (while (looking-at "[ \t]")
1369 (defun flymake-goto-line (line-no)
1370 "Go to line LINE-NO, then skip whitespace."
1371 (goto-char (point-min))
1372 (forward-line (1- line-no
))
1373 (flymake-skip-whitespace))
1375 (defun flymake-goto-next-error ()
1376 "Go to next error in err ring."
1378 (let ((line-no (flymake-get-next-err-line-no flymake-err-info
(line-number-at-pos))))
1380 (setq line-no
(flymake-get-first-err-line-no flymake-err-info
))
1381 (flymake-log 1 "passed end of file"))
1383 (flymake-goto-line line-no
)
1384 (flymake-log 1 "no errors in current buffer"))))
1386 (defun flymake-goto-prev-error ()
1387 "Go to previous error in err ring."
1389 (let ((line-no (flymake-get-prev-err-line-no flymake-err-info
(line-number-at-pos))))
1391 (setq line-no
(flymake-get-last-err-line-no flymake-err-info
))
1392 (flymake-log 1 "passed beginning of file"))
1394 (flymake-goto-line line-no
)
1395 (flymake-log 1 "no errors in current buffer"))))
1397 (defun flymake-patch-err-text (string)
1398 (if (string-match "^[\n\t :0-9]*\\(.*\\)$" string
)
1399 (match-string 1 string
)
1402 ;;;; general init-cleanup and helper routines
1403 (defun flymake-create-temp-inplace (file-name prefix
)
1404 (unless (stringp file-name
)
1405 (error "Invalid file-name"))
1407 (setq prefix
"flymake"))
1408 (let* ((ext (file-name-extension file-name
))
1409 (temp-name (file-truename
1410 (concat (file-name-sans-extension file-name
)
1412 (and ext
(concat "." ext
))))))
1413 (flymake-log 3 "create-temp-inplace: file=%s temp=%s" file-name temp-name
)
1416 (defun flymake-create-temp-with-folder-structure (file-name _prefix
)
1417 (unless (stringp file-name
)
1418 (error "Invalid file-name"))
1420 (let* ((dir (file-name-directory file-name
))
1421 ;; Not sure what this slash-pos is all about, but I guess it's just
1422 ;; trying to remove the leading / of absolute file names.
1423 (slash-pos (string-match "/" dir
))
1424 (temp-dir (expand-file-name (substring dir
(1+ slash-pos
))
1425 temporary-file-directory
)))
1427 (file-truename (expand-file-name (file-name-nondirectory file-name
)
1430 (defun flymake-delete-temp-directory (dir-name)
1431 "Attempt to delete temp dir created by `flymake-create-temp-with-folder-structure', do not fail on error."
1432 (let* ((temp-dir temporary-file-directory
)
1433 (suffix (substring dir-name
(1+ (length temp-dir
)))))
1435 (while (> (length suffix
) 0)
1436 (setq suffix
(directory-file-name suffix
))
1437 ;;+(flymake-log 0 "suffix=%s" suffix)
1438 (flymake-safe-delete-directory
1439 (file-truename (expand-file-name suffix temp-dir
)))
1440 (setq suffix
(file-name-directory suffix
)))))
1442 (defvar-local flymake-temp-source-file-name nil
)
1443 (defvar-local flymake-master-file-name nil
)
1444 (defvar-local flymake-temp-master-file-name nil
)
1445 (defvar-local flymake-base-dir nil
)
1447 (defun flymake-init-create-temp-buffer-copy (create-temp-f)
1448 "Make a temporary copy of the current buffer, save its name in buffer data and return the name."
1449 (let* ((source-file-name buffer-file-name
)
1450 (temp-source-file-name (funcall create-temp-f source-file-name
"flymake")))
1452 (flymake-save-buffer-in-file temp-source-file-name
)
1453 (setq flymake-temp-source-file-name temp-source-file-name
)
1454 temp-source-file-name
))
1456 (defun flymake-simple-cleanup ()
1457 "Do cleanup after `flymake-init-create-temp-buffer-copy'.
1459 (flymake-safe-delete-file flymake-temp-source-file-name
)
1460 (setq flymake-last-change-time nil
))
1462 (defun flymake-get-real-file-name (file-name-from-err-msg)
1463 "Translate file name from error message to \"real\" file name.
1464 Return full-name. Names are real, not patched."
1465 (let* ((real-name nil
)
1466 (source-file-name buffer-file-name
)
1467 (master-file-name flymake-master-file-name
)
1468 (temp-source-file-name flymake-temp-source-file-name
)
1469 (temp-master-file-name flymake-temp-master-file-name
)
1471 (list flymake-base-dir
1472 (file-name-directory source-file-name
)
1473 (if master-file-name
(file-name-directory master-file-name
))))
1474 (files (list (list source-file-name source-file-name
)
1475 (list temp-source-file-name source-file-name
)
1476 (list master-file-name master-file-name
)
1477 (list temp-master-file-name master-file-name
))))
1479 (when (equal 0 (length file-name-from-err-msg
))
1480 (setq file-name-from-err-msg source-file-name
))
1482 (setq real-name
(flymake-get-full-patched-file-name file-name-from-err-msg base-dirs files
))
1483 ;; if real-name is nil, than file name from err msg is none of the files we've patched
1485 (setq real-name
(flymake-get-full-nonpatched-file-name file-name-from-err-msg base-dirs
)))
1487 (setq real-name file-name-from-err-msg
))
1488 (setq real-name
(flymake-fix-file-name real-name
))
1489 (flymake-log 3 "get-real-file-name: file-name=%s real-name=%s" file-name-from-err-msg real-name
)
1492 (defun flymake-get-full-patched-file-name (file-name-from-err-msg base-dirs files
)
1493 (let* ((base-dirs-count (length base-dirs
))
1494 (file-count (length files
))
1497 (while (and (not real-name
) (> base-dirs-count
0))
1498 (setq file-count
(length files
))
1499 (while (and (not real-name
) (> file-count
0))
1500 (let* ((this-dir (nth (1- base-dirs-count
) base-dirs
))
1501 (this-file (nth 0 (nth (1- file-count
) files
)))
1502 (this-real-name (nth 1 (nth (1- file-count
) files
))))
1503 ;;+(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)
1504 (when (and this-dir this-file
(flymake-same-files
1505 (expand-file-name file-name-from-err-msg this-dir
)
1507 (setq real-name this-real-name
)))
1508 (setq file-count
(1- file-count
)))
1509 (setq base-dirs-count
(1- base-dirs-count
)))
1512 (defun flymake-get-full-nonpatched-file-name (file-name-from-err-msg base-dirs
)
1513 (let* ((real-name nil
))
1514 (if (file-name-absolute-p file-name-from-err-msg
)
1515 (setq real-name file-name-from-err-msg
)
1516 (let* ((base-dirs-count (length base-dirs
)))
1517 (while (and (not real-name
) (> base-dirs-count
0))
1518 (let* ((full-name (expand-file-name file-name-from-err-msg
1519 (nth (1- base-dirs-count
) base-dirs
))))
1520 (if (file-exists-p full-name
)
1521 (setq real-name full-name
))
1522 (setq base-dirs-count
(1- base-dirs-count
))))))
1525 (defun flymake-init-find-buildfile-dir (source-file-name buildfile-name
)
1526 "Find buildfile, store its dir in buffer data and return its dir, if found."
1527 (let* ((buildfile-dir
1528 (flymake-find-buildfile buildfile-name
1529 (file-name-directory source-file-name
))))
1531 (setq flymake-base-dir buildfile-dir
)
1532 (flymake-log 1 "no buildfile (%s) for %s" buildfile-name source-file-name
)
1533 (flymake-report-fatal-status
1534 "NOMK" (format "No buildfile (%s) found for %s"
1535 buildfile-name source-file-name
)))))
1537 (defun flymake-init-create-temp-source-and-master-buffer-copy (get-incl-dirs-f create-temp-f master-file-masks include-regexp
)
1538 "Find master file (or buffer), create its copy along with a copy of the source file."
1539 (let* ((source-file-name buffer-file-name
)
1540 (temp-source-file-name (flymake-init-create-temp-buffer-copy create-temp-f
))
1541 (master-and-temp-master (flymake-create-master-file
1542 source-file-name temp-source-file-name
1543 get-incl-dirs-f create-temp-f
1544 master-file-masks include-regexp
)))
1546 (if (not master-and-temp-master
)
1548 (flymake-log 1 "cannot find master file for %s" source-file-name
)
1549 (flymake-report-status "!" "") ; NOMASTER
1551 (setq flymake-master-file-name
(nth 0 master-and-temp-master
))
1552 (setq flymake-temp-master-file-name
(nth 1 master-and-temp-master
)))))
1554 (defun flymake-master-cleanup ()
1555 (flymake-simple-cleanup)
1556 (flymake-safe-delete-file flymake-temp-master-file-name
))
1558 ;;;; make-specific init-cleanup routines
1559 (defun flymake-get-syntax-check-program-args (source-file-name base-dir use-relative-base-dir use-relative-source get-cmd-line-f
)
1560 "Create a command line for syntax check using GET-CMD-LINE-F."
1561 (funcall get-cmd-line-f
1562 (if use-relative-source
1563 (file-relative-name source-file-name base-dir
)
1565 (if use-relative-base-dir
1566 (file-relative-name base-dir
1567 (file-name-directory source-file-name
))
1570 (defun flymake-get-make-cmdline (source base-dir
)
1575 (concat "CHK_SOURCES=" source
)
1576 "SYNTAX_CHECK_MODE=1"
1579 (defun flymake-get-ant-cmdline (source base-dir
)
1582 (concat base-dir
"/" "build.xml")
1583 (concat "-DCHK_SOURCES=" source
)
1586 (defun flymake-simple-make-init-impl (create-temp-f use-relative-base-dir use-relative-source build-file-name get-cmdline-f
)
1587 "Create syntax check command line for a directly checked source file.
1588 Use CREATE-TEMP-F for creating temp copy."
1590 (source-file-name buffer-file-name
)
1591 (buildfile-dir (flymake-init-find-buildfile-dir source-file-name build-file-name
)))
1593 (let* ((temp-source-file-name (flymake-init-create-temp-buffer-copy create-temp-f
)))
1594 (setq args
(flymake-get-syntax-check-program-args temp-source-file-name buildfile-dir
1595 use-relative-base-dir use-relative-source
1599 (defun flymake-simple-make-init ()
1600 (flymake-simple-make-init-impl 'flymake-create-temp-inplace t t
"Makefile" 'flymake-get-make-cmdline
))
1602 (defun flymake-master-make-init (get-incl-dirs-f master-file-masks include-regexp
)
1603 "Create make command line for a source file checked via master file compilation."
1604 (let* ((make-args nil
)
1605 (temp-master-file-name (flymake-init-create-temp-source-and-master-buffer-copy
1606 get-incl-dirs-f
'flymake-create-temp-inplace
1607 master-file-masks include-regexp
)))
1608 (when temp-master-file-name
1609 (let* ((buildfile-dir (flymake-init-find-buildfile-dir temp-master-file-name
"Makefile")))
1611 (setq make-args
(flymake-get-syntax-check-program-args
1612 temp-master-file-name buildfile-dir nil nil
'flymake-get-make-cmdline
)))))
1615 (defun flymake-find-make-buildfile (source-dir)
1616 (flymake-find-buildfile "Makefile" source-dir
))
1618 ;;;; .h/make specific
1619 (defun flymake-master-make-header-init ()
1620 (flymake-master-make-init
1621 'flymake-get-include-dirs
1622 '("\\.\\(?:c\\(?:pp\\|xx\\|\\+\\+\\)?\\|CC\\)\\'")
1623 "[ \t]*#[ \t]*include[ \t]*\"\\([[:word:]0-9/\\_.]*%s\\)\""))
1625 ;;;; .java/make specific
1626 (defun flymake-simple-make-java-init ()
1627 (flymake-simple-make-init-impl 'flymake-create-temp-with-folder-structure nil nil
"Makefile" 'flymake-get-make-cmdline
))
1629 (defun flymake-simple-ant-java-init ()
1630 (flymake-simple-make-init-impl 'flymake-create-temp-with-folder-structure nil nil
"build.xml" 'flymake-get-ant-cmdline
))
1632 (defun flymake-simple-java-cleanup ()
1633 "Cleanup after `flymake-simple-make-java-init' -- delete temp file and dirs."
1634 (flymake-safe-delete-file flymake-temp-source-file-name
)
1635 (when flymake-temp-source-file-name
1636 (flymake-delete-temp-directory
1637 (file-name-directory flymake-temp-source-file-name
))))
1639 ;;;; perl-specific init-cleanup routines
1640 (defun flymake-perl-init ()
1641 (let* ((temp-file (flymake-init-create-temp-buffer-copy
1642 'flymake-create-temp-inplace
))
1643 (local-file (file-relative-name
1645 (file-name-directory buffer-file-name
))))
1646 (list "perl" (list "-wc " local-file
))))
1648 ;;;; php-specific init-cleanup routines
1649 (defun flymake-php-init ()
1650 (let* ((temp-file (flymake-init-create-temp-buffer-copy
1651 'flymake-create-temp-inplace
))
1652 (local-file (file-relative-name
1654 (file-name-directory buffer-file-name
))))
1655 (list "php" (list "-f" local-file
"-l"))))
1657 ;;;; tex-specific init-cleanup routines
1658 (defun flymake-get-tex-args (file-name)
1659 ;;(list "latex" (list "-c-style-errors" file-name))
1660 (list "texify" (list "--pdf" "--tex-option=-c-style-errors" file-name
)))
1662 (defun flymake-simple-tex-init ()
1663 (flymake-get-tex-args (flymake-init-create-temp-buffer-copy 'flymake-create-temp-inplace
)))
1665 ;; Perhaps there should be a buffer-local variable flymake-master-file
1666 ;; that people can set to override this stuff. Could inherit from
1667 ;; the similar AUCTeX variable.
1668 (defun flymake-master-tex-init ()
1669 (let* ((temp-master-file-name (flymake-init-create-temp-source-and-master-buffer-copy
1670 'flymake-get-include-dirs-dot
'flymake-create-temp-inplace
1672 "[ \t]*\\in\\(?:put\\|clude\\)[ \t]*{\\(.*%s\\)}")))
1673 (when temp-master-file-name
1674 (flymake-get-tex-args temp-master-file-name
))))
1676 (defun flymake-get-include-dirs-dot (_base-dir)
1679 ;;;; xml-specific init-cleanup routines
1680 (defun flymake-xml-init ()
1681 (list flymake-xml-program
1682 (list "val" (flymake-init-create-temp-buffer-copy
1683 'flymake-create-temp-inplace
))))
1686 ;;; flymake.el ends here