1 ;;; flymake.el --- a universal on-the-fly syntax checker -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2003-2014 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-process-filter (process output
)
513 "Parse OUTPUT and highlight error lines.
514 It's flymake process filter."
515 (let ((source-buffer (process-buffer process
)))
517 (flymake-log 3 "received %d byte(s) of output from process %d"
518 (length output
) (process-id process
))
519 (when (buffer-live-p source-buffer
)
520 (with-current-buffer source-buffer
521 (flymake-parse-output-and-residual output
)))))
523 (defun flymake-process-sentinel (process _event
)
524 "Sentinel for syntax check buffers."
525 (when (memq (process-status process
) '(signal exit
))
526 (let* ((exit-status (process-exit-status process
))
527 (command (process-command process
))
528 (source-buffer (process-buffer process
))
529 (cleanup-f (flymake-get-cleanup-function (buffer-file-name source-buffer
))))
531 (flymake-log 2 "process %d exited with code %d"
532 (process-id process
) exit-status
)
535 (flymake-log 3 "cleaning up using %s" cleanup-f
)
536 (when (buffer-live-p source-buffer
)
537 (with-current-buffer source-buffer
538 (funcall cleanup-f
)))
540 (delete-process process
)
541 (setq flymake-processes
(delq process flymake-processes
))
543 (when (buffer-live-p source-buffer
)
544 (with-current-buffer source-buffer
546 (flymake-parse-residual)
547 (flymake-post-syntax-check exit-status command
)
548 (setq flymake-is-running nil
))))
550 (let ((err-str (format "Error in process sentinel for buffer %s: %s"
551 source-buffer
(error-message-string err
))))
552 (flymake-log 0 err-str
)
553 (with-current-buffer source-buffer
554 (setq flymake-is-running nil
))))))))
556 (defun flymake-post-syntax-check (exit-status command
)
557 (setq flymake-err-info flymake-new-err-info
)
558 (setq flymake-new-err-info nil
)
559 (setq flymake-err-info
560 (flymake-fix-line-numbers
561 flymake-err-info
1 (count-lines (point-min) (point-max))))
562 (flymake-delete-own-overlays)
563 (flymake-highlight-err-lines flymake-err-info
)
564 (let (err-count warn-count
)
565 (setq err-count
(flymake-get-err-count flymake-err-info
"e"))
566 (setq warn-count
(flymake-get-err-count flymake-err-info
"w"))
567 (flymake-log 2 "%s: %d error(s), %d warning(s) in %.2f second(s)"
568 (buffer-name) err-count warn-count
569 (- (float-time) flymake-check-start-time
))
570 (setq flymake-check-start-time nil
)
572 (if (and (equal 0 err-count
) (equal 0 warn-count
))
573 (if (equal 0 exit-status
)
574 (flymake-report-status "" "") ; PASSED
575 (if (not flymake-check-was-interrupted
)
576 (flymake-report-fatal-status "CFGERR"
577 (format "Configuration error has occurred while running %s" command
))
578 (flymake-report-status nil
""))) ; "STOPPED"
579 (flymake-report-status (format "%d/%d" err-count warn-count
) ""))))
581 (defun flymake-parse-output-and-residual (output)
582 "Split OUTPUT into lines, merge in residual if necessary."
583 (let* ((buffer-residual flymake-output-residual
)
584 (total-output (if buffer-residual
(concat buffer-residual output
) output
))
585 (lines-and-residual (flymake-split-output total-output
))
586 (lines (nth 0 lines-and-residual
))
587 (new-residual (nth 1 lines-and-residual
)))
588 (setq flymake-output-residual new-residual
)
589 (setq flymake-new-err-info
590 (flymake-parse-err-lines
591 flymake-new-err-info lines
))))
593 (defun flymake-parse-residual ()
594 "Parse residual if it's non empty."
595 (when flymake-output-residual
596 (setq flymake-new-err-info
597 (flymake-parse-err-lines
599 (list flymake-output-residual
)))
600 (setq flymake-output-residual nil
)))
602 (defun flymake-er-make-er (line-no line-err-info-list
)
603 (list line-no line-err-info-list
))
605 (defun flymake-er-get-line (err-info)
608 (defun flymake-er-get-line-err-info-list (err-info)
611 (cl-defstruct (flymake-ler
613 (:constructor flymake-ler-make-ler
(file line type text
&optional full-file
)))
614 file line type text full-file
)
616 (defun flymake-ler-set-file (line-err-info file
)
617 (flymake-ler-make-ler file
618 (flymake-ler-line line-err-info
)
619 (flymake-ler-type line-err-info
)
620 (flymake-ler-text line-err-info
)
621 (flymake-ler-full-file line-err-info
)))
623 (defun flymake-ler-set-full-file (line-err-info full-file
)
624 (flymake-ler-make-ler (flymake-ler-file line-err-info
)
625 (flymake-ler-line line-err-info
)
626 (flymake-ler-type line-err-info
)
627 (flymake-ler-text line-err-info
)
630 (defun flymake-ler-set-line (line-err-info line
)
631 (flymake-ler-make-ler (flymake-ler-file line-err-info
)
633 (flymake-ler-type line-err-info
)
634 (flymake-ler-text line-err-info
)
635 (flymake-ler-full-file line-err-info
)))
637 (defun flymake-get-line-err-count (line-err-info-list type
)
638 "Return number of errors of specified TYPE.
639 Value of TYPE is either \"e\" or \"w\"."
641 (count (length line-err-info-list
))
645 (when (equal type
(flymake-ler-type (nth idx line-err-info-list
)))
646 (setq err-count
(1+ err-count
)))
650 (defun flymake-get-err-count (err-info-list type
)
651 "Return number of errors of specified TYPE for ERR-INFO-LIST."
653 (count (length err-info-list
))
656 (setq err-count
(+ err-count
(flymake-get-line-err-count (nth 1 (nth idx err-info-list
)) type
)))
660 (defun flymake-fix-line-numbers (err-info-list min-line max-line
)
661 "Replace line numbers with fixed value.
662 If line-numbers is less than MIN-LINE, set line numbers to MIN-LINE.
663 If line numbers is greater than MAX-LINE, set line numbers to MAX-LINE.
664 The reason for this fix is because some compilers might report
665 line number outside the file being compiled."
666 (let* ((count (length err-info-list
))
670 (setq err-info
(nth (1- count
) err-info-list
))
671 (setq line
(flymake-er-get-line err-info
))
672 (when (or (< line min-line
) (> line max-line
))
673 (setq line
(if (< line min-line
) min-line max-line
))
674 (setq err-info-list
(flymake-set-at err-info-list
(1- count
)
675 (flymake-er-make-er line
676 (flymake-er-get-line-err-info-list err-info
)))))
677 (setq count
(1- count
))))
680 (defun flymake-highlight-err-lines (err-info-list)
681 "Highlight error lines in BUFFER using info from ERR-INFO-LIST."
683 (dolist (err err-info-list
)
684 (flymake-highlight-line (car err
) (nth 1 err
)))))
686 (defun flymake-overlay-p (ov)
687 "Determine whether overlay OV was created by flymake."
688 (and (overlayp ov
) (overlay-get ov
'flymake-overlay
)))
690 (defun flymake-make-overlay (beg end tooltip-text face bitmap
)
691 "Allocate a flymake overlay in range BEG and END."
692 (when (not (flymake-region-has-flymake-overlays beg end
))
693 (let ((ov (make-overlay beg end nil t t
))
694 (fringe (and flymake-fringe-indicator-position
695 (propertize "!" 'display
696 (cons flymake-fringe-indicator-position
700 (overlay-put ov
'face face
)
701 (overlay-put ov
'help-echo tooltip-text
)
702 (overlay-put ov
'flymake-overlay t
)
703 (overlay-put ov
'priority
100)
704 (overlay-put ov
'evaporate t
)
705 (overlay-put ov
'before-string fringe
)
706 ;;+(flymake-log 3 "created overlay %s" ov)
708 (flymake-log 3 "created an overlay at (%d-%d)" beg end
)))
710 (defun flymake-delete-own-overlays ()
711 "Delete all flymake overlays in BUFFER."
712 (dolist (ol (overlays-in (point-min) (point-max)))
713 (when (flymake-overlay-p ol
)
715 ;;+(flymake-log 3 "deleted overlay %s" ol)
718 (defun flymake-region-has-flymake-overlays (beg end
)
719 "Check if region specified by BEG and END has overlay.
720 Return t if it has at least one flymake overlay, nil if no overlay."
721 (let ((ov (overlays-in beg end
))
722 (has-flymake-overlays nil
))
724 (when (flymake-overlay-p (car ov
))
725 (setq has-flymake-overlays t
))
727 has-flymake-overlays
))
729 (defface flymake-errline
730 '((((supports :underline
(:style wave
)))
731 :underline
(:style wave
:color
"Red1"))
734 "Face used for marking error lines."
738 (defface flymake-warnline
739 '((((supports :underline
(:style wave
)))
740 :underline
(:style wave
:color
"DarkOrange"))
743 "Face used for marking warning lines."
747 (defun flymake-highlight-line (line-no line-err-info-list
)
748 "Highlight line LINE-NO in current buffer.
749 Perhaps use text from LINE-ERR-INFO-LIST to enhance highlighting."
750 (goto-char (point-min))
751 (forward-line (1- line-no
))
752 (pcase-let* ((beg (progn (back-to-indentation) (point)))
755 (skip-chars-backward " \t\f\t\n" beg
)
757 (line-beginning-position 2)
759 (tooltip-text (mapconcat #'flymake-ler-text line-err-info-list
"\n"))
761 (if (> (flymake-get-line-err-count line-err-info-list
"e") 0)
762 (list 'flymake-errline flymake-error-bitmap
)
763 (list 'flymake-warnline flymake-warning-bitmap
))))
764 (flymake-make-overlay beg end tooltip-text face bitmap
)))
766 (defun flymake-parse-err-lines (err-info-list lines
)
767 "Parse err LINES, store info in ERR-INFO-LIST."
768 (let* ((count (length lines
))
772 (source-file-name buffer-file-name
)
773 (get-real-file-name-f (flymake-get-real-file-name-function source-file-name
)))
776 (setq line-err-info
(flymake-parse-line (nth idx lines
)))
778 (setq real-file-name
(funcall get-real-file-name-f
779 (flymake-ler-file line-err-info
)))
780 (setq line-err-info
(flymake-ler-set-full-file line-err-info real-file-name
))
782 (when (flymake-same-files real-file-name source-file-name
)
783 (setq line-err-info
(flymake-ler-set-file line-err-info nil
))
784 (setq err-info-list
(flymake-add-err-info err-info-list line-err-info
))))
785 (flymake-log 3 "parsed '%s', %s line-err-info" (nth idx lines
) (if line-err-info
"got" "no"))
789 (defun flymake-split-output (output)
790 "Split OUTPUT into lines.
791 Return last one as residual if it does not end with newline char.
792 Returns ((LINES) RESIDUAL)."
793 (when (and output
(> (length output
) 0))
794 (let* ((lines (split-string output
"[\n\r]+" t
))
795 (complete (equal "\n" (char-to-string (aref output
(1- (length output
))))))
798 (setq residual
(car (last lines
)))
799 (setq lines
(butlast lines
)))
800 (list lines residual
))))
802 (defun flymake-reformat-err-line-patterns-from-compile-el (original-list)
803 "Grab error line patterns from ORIGINAL-LIST in compile.el format.
804 Convert it to flymake internal format."
805 (let* ((converted-list '()))
806 (dolist (item original-list
)
807 (setq item
(cdr item
))
808 (let ((regexp (nth 0 item
))
812 (if (consp file
) (setq file
(car file
)))
813 (if (consp line
) (setq line
(car line
)))
814 (if (consp col
) (setq col
(car col
)))
816 (when (not (functionp line
))
817 (setq converted-list
(cons (list regexp file line col
) converted-list
)))))
822 (defvar flymake-err-line-patterns
; regexp file-idx line-idx col-idx (optional) text-idx(optional), match-end to end of string is error text
826 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\)) \: \\(\\(error\\|warning\\|fatal error\\) \\(C[0-9]+\\):[ \t\n]*\\(.+\\)\\)"
829 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)\:\\([0-9]+\\)\:[0-9]+\:[0-9]+\:[0-9]+\: \\(\\(Error\\|Warning\\|Caution\\|Semantic Error\\):[ \t\n]*\\(.+\\)\\)"
832 ("midl[ ]*:[ ]*\\(command line error .*\\)"
835 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\),[0-9]+)\: \\(\\(error\\|warning\\|fatal error\\) \\(CS[0-9]+\\):[ \t\n]*\\(.+\\)\\)"
838 ("\\(.*\\) at \\([^ \n]+\\) line \\([0-9]+\\)[,.\n]" 2 3 nil
1)
840 ("\\(?:Parse\\|Fatal\\) error: \\(.*\\) in \\(.*\\) on line \\([0-9]+\\)" 2 3 nil
1)
841 ;; LaTeX warnings (fileless) ("\\(LaTeX \\(Warning\\|Error\\): .*\\) on input line \\([0-9]+\\)" 20 3 nil 1)
842 ;; ant/javac. Note this also matches gcc warnings!
843 (" *\\(\\[javac\\] *\\)?\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)\:\\([0-9]+\\)\\(?:\:[0-9]+\\)?\:[ \t\n]*\\(.+\\)"
845 ;; compilation-error-regexp-alist)
846 (flymake-reformat-err-line-patterns-from-compile-el compilation-error-regexp-alist-alist
))
847 "Patterns for matching error/warning lines. Each pattern has the form
848 \(REGEXP FILE-IDX LINE-IDX COL-IDX ERR-TEXT-IDX).
849 Use `flymake-reformat-err-line-patterns-from-compile-el' to add patterns
852 (define-obsolete-variable-alias 'flymake-warning-re
'flymake-warning-predicate
"24.4")
853 (defvar flymake-warning-predicate
"^[wW]arning"
854 "Predicate matching against error text to detect a warning.
855 Takes a single argument, the error's text and should return non-nil
857 Instead of a function, it can also be a regular expression.")
859 (defun flymake-parse-line (line)
860 "Parse LINE to see if it is an error or warning.
861 Return its components if so, nil otherwise."
862 (let ((raw-file-name nil
)
866 (patterns flymake-err-line-patterns
)
868 (while (and patterns
(not matched
))
869 (when (string-match (car (car patterns
)) line
)
870 (let* ((file-idx (nth 1 (car patterns
)))
871 (line-idx (nth 2 (car patterns
))))
873 (setq raw-file-name
(if file-idx
(match-string file-idx line
) nil
))
874 (setq line-no
(if line-idx
(string-to-number
875 (match-string line-idx line
)) 0))
876 (setq err-text
(if (> (length (car patterns
)) 4)
877 (match-string (nth 4 (car patterns
)) line
)
878 (flymake-patch-err-text
879 (substring line
(match-end 0)))))
881 (setq err-text
"<no error text>")
882 (when (cond ((stringp flymake-warning-predicate
)
883 (string-match flymake-warning-predicate err-text
))
884 ((functionp flymake-warning-predicate
)
885 (funcall flymake-warning-predicate err-text
)))
886 (setq err-type
"w")))
888 3 "parse line: file-idx=%s line-idx=%s file=%s line=%s text=%s"
889 file-idx line-idx raw-file-name line-no err-text
)
891 (setq patterns
(cdr patterns
)))
893 (flymake-ler-make-ler raw-file-name line-no err-type err-text
)
896 (defun flymake-find-err-info (err-info-list line-no
)
897 "Find (line-err-info-list pos) for specified LINE-NO."
899 (let* ((line-err-info-list nil
)
901 (count (length err-info-list
)))
903 (while (and (< pos count
) (< (car (nth pos err-info-list
)) line-no
))
905 (when (and (< pos count
) (equal (car (nth pos err-info-list
)) line-no
))
906 (setq line-err-info-list
(flymake-er-get-line-err-info-list (nth pos err-info-list
))))
907 (list line-err-info-list pos
))
910 (defun flymake-line-err-info-is-less-or-equal (line-one line-two
)
911 (or (string< (flymake-ler-type line-one
) (flymake-ler-type line-two
))
912 (and (string= (flymake-ler-type line-one
) (flymake-ler-type line-two
))
913 (not (flymake-ler-file line-one
)) (flymake-ler-file line-two
))
914 (and (string= (flymake-ler-type line-one
) (flymake-ler-type line-two
))
915 (or (and (flymake-ler-file line-one
) (flymake-ler-file line-two
))
916 (and (not (flymake-ler-file line-one
)) (not (flymake-ler-file line-two
)))))))
918 (defun flymake-add-line-err-info (line-err-info-list line-err-info
)
919 "Update LINE-ERR-INFO-LIST with the error LINE-ERR-INFO.
920 For the format of LINE-ERR-INFO, see `flymake-ler-make-ler'.
921 The new element is inserted in the proper position, according to
922 the predicate `flymake-line-err-info-is-less-or-equal'.
923 The updated value of LINE-ERR-INFO-LIST is returned."
924 (if (not line-err-info-list
)
926 (let* ((count (length line-err-info-list
))
928 (while (and (< idx count
) (flymake-line-err-info-is-less-or-equal (nth idx line-err-info-list
) line-err-info
))
930 (cond ((equal 0 idx
) (setq line-err-info-list
(cons line-err-info line-err-info-list
)))
931 (t (setq line-err-info-list
(flymake-ins-after line-err-info-list
(1- idx
) line-err-info
))))
932 line-err-info-list
)))
934 (defun flymake-add-err-info (err-info-list line-err-info
)
935 "Update ERR-INFO-LIST with the error LINE-ERR-INFO, preserving sort order.
936 Returns the updated value of ERR-INFO-LIST.
937 For the format of ERR-INFO-LIST, see `flymake-err-info'.
938 For the format of LINE-ERR-INFO, see `flymake-ler-make-ler'."
939 (let* ((line-no (if (flymake-ler-file line-err-info
) 1 (flymake-ler-line line-err-info
)))
940 (info-and-pos (flymake-find-err-info err-info-list line-no
))
941 (exists (car info-and-pos
))
942 (pos (nth 1 info-and-pos
))
943 (line-err-info-list nil
)
947 (setq line-err-info-list
(flymake-er-get-line-err-info-list (car (nthcdr pos err-info-list
)))))
948 (setq line-err-info-list
(flymake-add-line-err-info line-err-info-list line-err-info
))
950 (setq err-info
(flymake-er-make-er line-no line-err-info-list
))
951 (cond (exists (setq err-info-list
(flymake-set-at err-info-list pos err-info
)))
952 ((equal 0 pos
) (setq err-info-list
(cons err-info err-info-list
)))
953 (t (setq err-info-list
(flymake-ins-after err-info-list
(1- pos
) err-info
))))
956 (defun flymake-get-project-include-dirs-imp (basedir)
957 "Include dirs for the project current file belongs to."
958 (if (flymake-get-project-include-dirs-from-cache basedir
)
960 (flymake-get-project-include-dirs-from-cache basedir
))
962 (let* ((command-line (concat "make -C "
963 (shell-quote-argument basedir
)
964 " DUMPVARS=INCLUDE_DIRS dumpvars"))
965 (output (shell-command-to-string command-line
))
966 (lines (split-string output
"\n" t
))
967 (count (length lines
))
970 (while (and (< idx count
) (not (string-match "^INCLUDE_DIRS=.*" (nth idx lines
))))
973 (let* ((inc-lines (split-string (nth idx lines
) " *-I" t
))
974 (inc-count (length inc-lines
)))
975 (while (> inc-count
0)
976 (when (not (string-match "^INCLUDE_DIRS=.*" (nth (1- inc-count
) inc-lines
)))
977 (push (replace-regexp-in-string "\"" "" (nth (1- inc-count
) inc-lines
)) inc-dirs
))
978 (setq inc-count
(1- inc-count
)))))
979 (flymake-add-project-include-dirs-to-cache basedir inc-dirs
)
982 (defvar flymake-get-project-include-dirs-function
#'flymake-get-project-include-dirs-imp
983 "Function used to get project include dirs, one parameter: basedir name.")
985 (defun flymake-get-project-include-dirs (basedir)
986 (funcall flymake-get-project-include-dirs-function basedir
))
988 (defun flymake-get-system-include-dirs ()
989 "System include dirs - from the 'INCLUDE' env setting."
990 (let* ((includes (getenv "INCLUDE")))
991 (if includes
(split-string includes path-separator t
) nil
)))
993 (defvar flymake-project-include-dirs-cache
(make-hash-table :test
#'equal
))
995 (defun flymake-get-project-include-dirs-from-cache (base-dir)
996 (gethash base-dir flymake-project-include-dirs-cache
))
998 (defun flymake-add-project-include-dirs-to-cache (base-dir include-dirs
)
999 (puthash base-dir include-dirs flymake-project-include-dirs-cache
))
1001 (defun flymake-clear-project-include-dirs-cache ()
1002 (clrhash flymake-project-include-dirs-cache
))
1004 (defun flymake-get-include-dirs (base-dir)
1005 "Get dirs to use when resolving local file names."
1006 (let* ((include-dirs (append '(".") (flymake-get-project-include-dirs base-dir
) (flymake-get-system-include-dirs))))
1009 ;; (defun flymake-restore-formatting ()
1010 ;; "Remove any formatting made by flymake."
1013 ;; (defun flymake-get-program-dir (buffer)
1014 ;; "Get dir to start program in."
1015 ;; (unless (bufferp buffer)
1016 ;; (error "Invalid buffer"))
1017 ;; (with-current-buffer buffer
1018 ;; default-directory))
1020 (defun flymake-safe-delete-file (file-name)
1021 (when (and file-name
(file-exists-p file-name
))
1022 (delete-file file-name
)
1023 (flymake-log 1 "deleted file %s" file-name
)))
1025 (defun flymake-safe-delete-directory (dir-name)
1028 (delete-directory dir-name
)
1029 (flymake-log 1 "deleted dir %s" dir-name
))
1031 (flymake-log 1 "Failed to delete dir %s, error ignored" dir-name
))))
1033 (defun flymake-start-syntax-check ()
1034 "Start syntax checking for current buffer."
1036 (flymake-log 3 "flymake is running: %s" flymake-is-running
)
1037 (when (and (not flymake-is-running
)
1038 (flymake-can-syntax-check-file buffer-file-name
))
1039 (when (or (not flymake-compilation-prevents-syntax-check
)
1040 (not (flymake-compilation-is-running))) ;+ (flymake-rep-ort-status buffer "COMP")
1041 (flymake-clear-buildfile-cache)
1042 (flymake-clear-project-include-dirs-cache)
1044 (setq flymake-check-was-interrupted nil
)
1046 (let* ((source-file-name buffer-file-name
)
1047 (init-f (flymake-get-init-function source-file-name
))
1048 (cleanup-f (flymake-get-cleanup-function source-file-name
))
1049 (cmd-and-args (funcall init-f
))
1050 (cmd (nth 0 cmd-and-args
))
1051 (args (nth 1 cmd-and-args
))
1052 (dir (nth 2 cmd-and-args
)))
1053 (if (not cmd-and-args
)
1055 (flymake-log 0 "init function %s for %s failed, cleaning up" init-f source-file-name
)
1056 (funcall cleanup-f
))
1058 (setq flymake-last-change-time nil
)
1059 (flymake-start-syntax-check-process cmd args dir
)))))))
1061 (defun flymake-start-syntax-check-process (cmd args dir
)
1062 "Start syntax check process."
1065 (let ((default-directory (or dir default-directory
)))
1067 (flymake-log 3 "starting process on dir %s" dir
))
1068 (apply 'start-file-process
1069 "flymake-proc" (current-buffer) cmd args
))))
1070 (set-process-sentinel process
'flymake-process-sentinel
)
1071 (set-process-filter process
'flymake-process-filter
)
1072 (push process flymake-processes
)
1074 (setq flymake-is-running t
)
1075 (setq flymake-last-change-time nil
)
1076 (setq flymake-check-start-time
(float-time))
1078 (flymake-report-status nil
"*")
1079 (flymake-log 2 "started process %d, command=%s, dir=%s"
1080 (process-id process
) (process-command process
)
1084 (let* ((err-str (format "Failed to launch syntax check process '%s' with args %s: %s"
1085 cmd args
(error-message-string err
)))
1086 (source-file-name buffer-file-name
)
1087 (cleanup-f (flymake-get-cleanup-function source-file-name
)))
1088 (flymake-log 0 err-str
)
1090 (flymake-report-fatal-status "PROCERR" err-str
)))))
1092 (defun flymake-kill-process (proc)
1093 "Kill process PROC."
1095 (let* ((buf (process-buffer proc
)))
1096 (when (buffer-live-p buf
)
1097 (with-current-buffer buf
1098 (setq flymake-check-was-interrupted t
))))
1099 (flymake-log 1 "killed process %d" (process-id proc
)))
1101 (defun flymake-stop-all-syntax-checks ()
1102 "Kill all syntax check processes."
1104 (while flymake-processes
1105 (flymake-kill-process (pop flymake-processes
))))
1107 (defun flymake-compilation-is-running ()
1108 (and (boundp 'compilation-in-progress
)
1109 compilation-in-progress
))
1111 (defun flymake-compile ()
1112 "Kill all flymake syntax checks, start compilation."
1114 (flymake-stop-all-syntax-checks)
1115 (call-interactively 'compile
))
1117 (defun flymake-on-timer-event (buffer)
1118 "Start a syntax check for buffer BUFFER if necessary."
1119 (when (buffer-live-p buffer
)
1120 (with-current-buffer buffer
1121 (when (and (not flymake-is-running
)
1122 flymake-last-change-time
1123 (> (- (float-time) flymake-last-change-time
)
1124 flymake-no-changes-timeout
))
1126 (setq flymake-last-change-time nil
)
1127 (flymake-log 3 "starting syntax check as more than 1 second passed since last change")
1128 (flymake-start-syntax-check)))))
1130 (define-obsolete-function-alias 'flymake-display-err-menu-for-current-line
1131 'flymake-popup-current-error-menu
"24.4")
1133 (defun flymake-popup-current-error-menu (&optional event
)
1134 "Pop up a menu with errors/warnings for current line."
1135 (interactive (list last-nonmenu-event
))
1136 (let* ((line-no (line-number-at-pos))
1137 (errors (or (car (flymake-find-err-info flymake-err-info line-no
))
1138 (user-error "No errors for current line")))
1139 (menu (mapcar (lambda (x)
1140 (if (flymake-ler-file x
)
1141 (cons (format "%s - %s(%d)"
1142 (flymake-ler-text x
)
1143 (flymake-ler-file x
)
1144 (flymake-ler-line x
))
1146 (list (flymake-ler-text x
))))
1148 (event (if (mouse-event-p event
)
1150 (list 'mouse-1
(posn-at-point))))
1151 (title (format "Line %d: %d error(s), %d warning(s)"
1153 (flymake-get-line-err-count errors
"e")
1154 (flymake-get-line-err-count errors
"w")))
1155 (choice (x-popup-menu event
(list title
(cons "" menu
)))))
1156 (flymake-log 3 "choice=%s" choice
)
1158 (flymake-goto-file-and-line (flymake-ler-full-file choice
)
1159 (flymake-ler-line choice
)))))
1161 (defun flymake-goto-file-and-line (file line
)
1162 "Try to get buffer for FILE and goto line LINE in it."
1163 (if (not (file-exists-p file
))
1164 (flymake-log 1 "File %s does not exist" file
)
1166 (goto-char (point-min))
1167 (forward-line (1- line
))))
1169 ;; flymake minor mode declarations
1170 (defvar-local flymake-mode-line nil
)
1171 (defvar-local flymake-mode-line-e-w nil
)
1172 (defvar-local flymake-mode-line-status nil
)
1174 (defun flymake-report-status (e-w &optional status
)
1175 "Show status in mode line."
1177 (setq flymake-mode-line-e-w e-w
))
1179 (setq flymake-mode-line-status status
))
1180 (let* ((mode-line " Flymake"))
1181 (when (> (length flymake-mode-line-e-w
) 0)
1182 (setq mode-line
(concat mode-line
":" flymake-mode-line-e-w
)))
1183 (setq mode-line
(concat mode-line flymake-mode-line-status
))
1184 (setq flymake-mode-line mode-line
)
1185 (force-mode-line-update)))
1187 (defun flymake-display-warning (warning)
1188 "Display a warning to user."
1189 (message-box warning
))
1191 (defun flymake-report-fatal-status (status warning
)
1192 "Display a warning and switch flymake mode off."
1193 (when flymake-gui-warnings-enabled
1194 (flymake-display-warning (format "Flymake: %s. Flymake will be switched OFF" warning
))
1197 (flymake-log 0 "switched OFF Flymake mode for buffer %s due to fatal status %s, warning %s"
1198 (buffer-name) status warning
))
1201 (define-minor-mode flymake-mode nil
1202 :group
'flymake
:lighter flymake-mode-line
1205 ;; Turning the mode ON.
1208 ((not buffer-file-name
)
1209 (message "Flymake unable to run without a buffer file name"))
1210 ((not (flymake-can-syntax-check-file buffer-file-name
))
1211 (flymake-log 2 "flymake cannot check syntax in buffer %s" (buffer-name)))
1213 (add-hook 'after-change-functions
'flymake-after-change-function nil t
)
1214 (add-hook 'after-save-hook
'flymake-after-save-hook nil t
)
1215 (add-hook 'kill-buffer-hook
'flymake-kill-buffer-hook nil t
)
1216 ;;+(add-hook 'find-file-hook 'flymake-find-file-hook)
1218 (flymake-report-status "" "")
1221 (run-at-time nil
1 'flymake-on-timer-event
(current-buffer)))
1223 (when (and flymake-start-syntax-check-on-find-file
1224 ;; Since we write temp files in current dir, there's no point
1225 ;; trying if the directory is read-only (bug#8954).
1226 (file-writable-p (file-name-directory buffer-file-name
)))
1227 (with-demoted-errors
1228 (flymake-start-syntax-check))))))
1230 ;; Turning the mode OFF.
1232 (remove-hook 'after-change-functions
'flymake-after-change-function t
)
1233 (remove-hook 'after-save-hook
'flymake-after-save-hook t
)
1234 (remove-hook 'kill-buffer-hook
'flymake-kill-buffer-hook t
)
1235 ;;+(remove-hook 'find-file-hook (function flymake-find-file-hook) t)
1237 (flymake-delete-own-overlays)
1240 (cancel-timer flymake-timer
)
1241 (setq flymake-timer nil
))
1243 (setq flymake-is-running nil
))))
1246 (defun flymake-mode-on ()
1247 "Turn flymake mode on."
1249 (flymake-log 1 "flymake mode turned ON for buffer %s" (buffer-name)))
1252 (defun flymake-mode-off ()
1253 "Turn flymake mode off."
1255 (flymake-log 1 "flymake mode turned OFF for buffer %s" (buffer-name)))
1257 (defun flymake-after-change-function (start stop _len
)
1258 "Start syntax check for current buffer if it isn't already running."
1259 ;;+(flymake-log 0 "setting change time to %s" (float-time))
1260 (let((new-text (buffer-substring start stop
)))
1261 (when (and flymake-start-syntax-check-on-newline
(equal new-text
"\n"))
1262 (flymake-log 3 "starting syntax check as new-line has been seen")
1263 (flymake-start-syntax-check))
1264 (setq flymake-last-change-time
(float-time))))
1266 (defun flymake-after-save-hook ()
1267 (if (local-variable-p 'flymake-mode
(current-buffer)) ; (???) other way to determine whether flymake is active in buffer being saved?
1269 (flymake-log 3 "starting syntax check as buffer was saved")
1270 (flymake-start-syntax-check)))) ; no more mode 3. cannot start check if mode 3 (to temp copies) is active - (???)
1272 (defun flymake-kill-buffer-hook ()
1274 (cancel-timer flymake-timer
)
1275 (setq flymake-timer nil
)))
1278 (defun flymake-find-file-hook ()
1279 ;;+(when flymake-start-syntax-check-on-find-file
1280 ;;+ (flymake-log 3 "starting syntax check on file open")
1281 ;;+ (flymake-start-syntax-check)
1283 (when (and (not (local-variable-p 'flymake-mode
(current-buffer)))
1284 (flymake-can-syntax-check-file buffer-file-name
))
1286 (flymake-log 3 "automatically turned ON flymake mode")))
1288 (defun flymake-get-first-err-line-no (err-info-list)
1289 "Return first line with error."
1291 (flymake-er-get-line (car err-info-list
))))
1293 (defun flymake-get-last-err-line-no (err-info-list)
1294 "Return last line with error."
1296 (flymake-er-get-line (nth (1- (length err-info-list
)) err-info-list
))))
1298 (defun flymake-get-next-err-line-no (err-info-list line-no
)
1299 "Return next line with error."
1301 (let* ((count (length err-info-list
))
1303 (while (and (< idx count
) (>= line-no
(flymake-er-get-line (nth idx err-info-list
))))
1304 (setq idx
(1+ idx
)))
1306 (flymake-er-get-line (nth idx err-info-list
))))))
1308 (defun flymake-get-prev-err-line-no (err-info-list line-no
)
1309 "Return previous line with error."
1311 (let* ((count (length err-info-list
)))
1312 (while (and (> count
0) (<= line-no
(flymake-er-get-line (nth (1- count
) err-info-list
))))
1313 (setq count
(1- count
)))
1315 (flymake-er-get-line (nth (1- count
) err-info-list
))))))
1317 (defun flymake-skip-whitespace ()
1318 "Move forward until non-whitespace is reached."
1319 (while (looking-at "[ \t]")
1322 (defun flymake-goto-line (line-no)
1323 "Go to line LINE-NO, then skip whitespace."
1324 (goto-char (point-min))
1325 (forward-line (1- line-no
))
1326 (flymake-skip-whitespace))
1328 (defun flymake-goto-next-error ()
1329 "Go to next error in err ring."
1331 (let ((line-no (flymake-get-next-err-line-no flymake-err-info
(line-number-at-pos))))
1333 (setq line-no
(flymake-get-first-err-line-no flymake-err-info
))
1334 (flymake-log 1 "passed end of file"))
1336 (flymake-goto-line line-no
)
1337 (flymake-log 1 "no errors in current buffer"))))
1339 (defun flymake-goto-prev-error ()
1340 "Go to previous error in err ring."
1342 (let ((line-no (flymake-get-prev-err-line-no flymake-err-info
(line-number-at-pos))))
1344 (setq line-no
(flymake-get-last-err-line-no flymake-err-info
))
1345 (flymake-log 1 "passed beginning of file"))
1347 (flymake-goto-line line-no
)
1348 (flymake-log 1 "no errors in current buffer"))))
1350 (defun flymake-patch-err-text (string)
1351 (if (string-match "^[\n\t :0-9]*\\(.*\\)$" string
)
1352 (match-string 1 string
)
1355 ;;;; general init-cleanup and helper routines
1356 (defun flymake-create-temp-inplace (file-name prefix
)
1357 (unless (stringp file-name
)
1358 (error "Invalid file-name"))
1360 (setq prefix
"flymake"))
1361 (let* ((ext (file-name-extension file-name
))
1362 (temp-name (file-truename
1363 (concat (file-name-sans-extension file-name
)
1365 (and ext
(concat "." ext
))))))
1366 (flymake-log 3 "create-temp-inplace: file=%s temp=%s" file-name temp-name
)
1369 (defun flymake-create-temp-with-folder-structure (file-name _prefix
)
1370 (unless (stringp file-name
)
1371 (error "Invalid file-name"))
1373 (let* ((dir (file-name-directory file-name
))
1374 ;; Not sure what this slash-pos is all about, but I guess it's just
1375 ;; trying to remove the leading / of absolute file names.
1376 (slash-pos (string-match "/" dir
))
1377 (temp-dir (expand-file-name (substring dir
(1+ slash-pos
))
1378 temporary-file-directory
)))
1380 (file-truename (expand-file-name (file-name-nondirectory file-name
)
1383 (defun flymake-delete-temp-directory (dir-name)
1384 "Attempt to delete temp dir created by `flymake-create-temp-with-folder-structure', do not fail on error."
1385 (let* ((temp-dir temporary-file-directory
)
1386 (suffix (substring dir-name
(1+ (length temp-dir
)))))
1388 (while (> (length suffix
) 0)
1389 (setq suffix
(directory-file-name suffix
))
1390 ;;+(flymake-log 0 "suffix=%s" suffix)
1391 (flymake-safe-delete-directory
1392 (file-truename (expand-file-name suffix temp-dir
)))
1393 (setq suffix
(file-name-directory suffix
)))))
1395 (defvar-local flymake-temp-source-file-name nil
)
1396 (defvar-local flymake-master-file-name nil
)
1397 (defvar-local flymake-temp-master-file-name nil
)
1398 (defvar-local flymake-base-dir nil
)
1400 (defun flymake-init-create-temp-buffer-copy (create-temp-f)
1401 "Make a temporary copy of the current buffer, save its name in buffer data and return the name."
1402 (let* ((source-file-name buffer-file-name
)
1403 (temp-source-file-name (funcall create-temp-f source-file-name
"flymake")))
1405 (flymake-save-buffer-in-file temp-source-file-name
)
1406 (setq flymake-temp-source-file-name temp-source-file-name
)
1407 temp-source-file-name
))
1409 (defun flymake-simple-cleanup ()
1410 "Do cleanup after `flymake-init-create-temp-buffer-copy'.
1412 (flymake-safe-delete-file flymake-temp-source-file-name
)
1413 (setq flymake-last-change-time nil
))
1415 (defun flymake-get-real-file-name (file-name-from-err-msg)
1416 "Translate file name from error message to \"real\" file name.
1417 Return full-name. Names are real, not patched."
1418 (let* ((real-name nil
)
1419 (source-file-name buffer-file-name
)
1420 (master-file-name flymake-master-file-name
)
1421 (temp-source-file-name flymake-temp-source-file-name
)
1422 (temp-master-file-name flymake-temp-master-file-name
)
1424 (list flymake-base-dir
1425 (file-name-directory source-file-name
)
1426 (if master-file-name
(file-name-directory master-file-name
))))
1427 (files (list (list source-file-name source-file-name
)
1428 (list temp-source-file-name source-file-name
)
1429 (list master-file-name master-file-name
)
1430 (list temp-master-file-name master-file-name
))))
1432 (when (equal 0 (length file-name-from-err-msg
))
1433 (setq file-name-from-err-msg source-file-name
))
1435 (setq real-name
(flymake-get-full-patched-file-name file-name-from-err-msg base-dirs files
))
1436 ;; if real-name is nil, than file name from err msg is none of the files we've patched
1438 (setq real-name
(flymake-get-full-nonpatched-file-name file-name-from-err-msg base-dirs
)))
1440 (setq real-name file-name-from-err-msg
))
1441 (setq real-name
(flymake-fix-file-name real-name
))
1442 (flymake-log 3 "get-real-file-name: file-name=%s real-name=%s" file-name-from-err-msg real-name
)
1445 (defun flymake-get-full-patched-file-name (file-name-from-err-msg base-dirs files
)
1446 (let* ((base-dirs-count (length base-dirs
))
1447 (file-count (length files
))
1450 (while (and (not real-name
) (> base-dirs-count
0))
1451 (setq file-count
(length files
))
1452 (while (and (not real-name
) (> file-count
0))
1453 (let* ((this-dir (nth (1- base-dirs-count
) base-dirs
))
1454 (this-file (nth 0 (nth (1- file-count
) files
)))
1455 (this-real-name (nth 1 (nth (1- file-count
) files
))))
1456 ;;+(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)
1457 (when (and this-dir this-file
(flymake-same-files
1458 (expand-file-name file-name-from-err-msg this-dir
)
1460 (setq real-name this-real-name
)))
1461 (setq file-count
(1- file-count
)))
1462 (setq base-dirs-count
(1- base-dirs-count
)))
1465 (defun flymake-get-full-nonpatched-file-name (file-name-from-err-msg base-dirs
)
1466 (let* ((real-name nil
))
1467 (if (file-name-absolute-p file-name-from-err-msg
)
1468 (setq real-name file-name-from-err-msg
)
1469 (let* ((base-dirs-count (length base-dirs
)))
1470 (while (and (not real-name
) (> base-dirs-count
0))
1471 (let* ((full-name (expand-file-name file-name-from-err-msg
1472 (nth (1- base-dirs-count
) base-dirs
))))
1473 (if (file-exists-p full-name
)
1474 (setq real-name full-name
))
1475 (setq base-dirs-count
(1- base-dirs-count
))))))
1478 (defun flymake-init-find-buildfile-dir (source-file-name buildfile-name
)
1479 "Find buildfile, store its dir in buffer data and return its dir, if found."
1480 (let* ((buildfile-dir
1481 (flymake-find-buildfile buildfile-name
1482 (file-name-directory source-file-name
))))
1484 (setq flymake-base-dir buildfile-dir
)
1485 (flymake-log 1 "no buildfile (%s) for %s" buildfile-name source-file-name
)
1486 (flymake-report-fatal-status
1487 "NOMK" (format "No buildfile (%s) found for %s"
1488 buildfile-name source-file-name
)))))
1490 (defun flymake-init-create-temp-source-and-master-buffer-copy (get-incl-dirs-f create-temp-f master-file-masks include-regexp
)
1491 "Find master file (or buffer), create its copy along with a copy of the source file."
1492 (let* ((source-file-name buffer-file-name
)
1493 (temp-source-file-name (flymake-init-create-temp-buffer-copy create-temp-f
))
1494 (master-and-temp-master (flymake-create-master-file
1495 source-file-name temp-source-file-name
1496 get-incl-dirs-f create-temp-f
1497 master-file-masks include-regexp
)))
1499 (if (not master-and-temp-master
)
1501 (flymake-log 1 "cannot find master file for %s" source-file-name
)
1502 (flymake-report-status "!" "") ; NOMASTER
1504 (setq flymake-master-file-name
(nth 0 master-and-temp-master
))
1505 (setq flymake-temp-master-file-name
(nth 1 master-and-temp-master
)))))
1507 (defun flymake-master-cleanup ()
1508 (flymake-simple-cleanup)
1509 (flymake-safe-delete-file flymake-temp-master-file-name
))
1511 ;;;; make-specific init-cleanup routines
1512 (defun flymake-get-syntax-check-program-args (source-file-name base-dir use-relative-base-dir use-relative-source get-cmd-line-f
)
1513 "Create a command line for syntax check using GET-CMD-LINE-F."
1514 (funcall get-cmd-line-f
1515 (if use-relative-source
1516 (file-relative-name source-file-name base-dir
)
1518 (if use-relative-base-dir
1519 (file-relative-name base-dir
1520 (file-name-directory source-file-name
))
1523 (defun flymake-get-make-cmdline (source base-dir
)
1528 (concat "CHK_SOURCES=" source
)
1529 "SYNTAX_CHECK_MODE=1"
1532 (defun flymake-get-ant-cmdline (source base-dir
)
1535 (concat base-dir
"/" "build.xml")
1536 (concat "-DCHK_SOURCES=" source
)
1539 (defun flymake-simple-make-init-impl (create-temp-f use-relative-base-dir use-relative-source build-file-name get-cmdline-f
)
1540 "Create syntax check command line for a directly checked source file.
1541 Use CREATE-TEMP-F for creating temp copy."
1543 (source-file-name buffer-file-name
)
1544 (buildfile-dir (flymake-init-find-buildfile-dir source-file-name build-file-name
)))
1546 (let* ((temp-source-file-name (flymake-init-create-temp-buffer-copy create-temp-f
)))
1547 (setq args
(flymake-get-syntax-check-program-args temp-source-file-name buildfile-dir
1548 use-relative-base-dir use-relative-source
1552 (defun flymake-simple-make-init ()
1553 (flymake-simple-make-init-impl 'flymake-create-temp-inplace t t
"Makefile" 'flymake-get-make-cmdline
))
1555 (defun flymake-master-make-init (get-incl-dirs-f master-file-masks include-regexp
)
1556 "Create make command line for a source file checked via master file compilation."
1557 (let* ((make-args nil
)
1558 (temp-master-file-name (flymake-init-create-temp-source-and-master-buffer-copy
1559 get-incl-dirs-f
'flymake-create-temp-inplace
1560 master-file-masks include-regexp
)))
1561 (when temp-master-file-name
1562 (let* ((buildfile-dir (flymake-init-find-buildfile-dir temp-master-file-name
"Makefile")))
1564 (setq make-args
(flymake-get-syntax-check-program-args
1565 temp-master-file-name buildfile-dir nil nil
'flymake-get-make-cmdline
)))))
1568 (defun flymake-find-make-buildfile (source-dir)
1569 (flymake-find-buildfile "Makefile" source-dir
))
1571 ;;;; .h/make specific
1572 (defun flymake-master-make-header-init ()
1573 (flymake-master-make-init
1574 'flymake-get-include-dirs
1575 '("\\.\\(?:c\\(?:pp\\|xx\\|\\+\\+\\)?\\|CC\\)\\'")
1576 "[ \t]*#[ \t]*include[ \t]*\"\\([[:word:]0-9/\\_.]*%s\\)\""))
1578 ;;;; .java/make specific
1579 (defun flymake-simple-make-java-init ()
1580 (flymake-simple-make-init-impl 'flymake-create-temp-with-folder-structure nil nil
"Makefile" 'flymake-get-make-cmdline
))
1582 (defun flymake-simple-ant-java-init ()
1583 (flymake-simple-make-init-impl 'flymake-create-temp-with-folder-structure nil nil
"build.xml" 'flymake-get-ant-cmdline
))
1585 (defun flymake-simple-java-cleanup ()
1586 "Cleanup after `flymake-simple-make-java-init' -- delete temp file and dirs."
1587 (flymake-safe-delete-file flymake-temp-source-file-name
)
1588 (when flymake-temp-source-file-name
1589 (flymake-delete-temp-directory
1590 (file-name-directory flymake-temp-source-file-name
))))
1592 ;;;; perl-specific init-cleanup routines
1593 (defun flymake-perl-init ()
1594 (let* ((temp-file (flymake-init-create-temp-buffer-copy
1595 'flymake-create-temp-inplace
))
1596 (local-file (file-relative-name
1598 (file-name-directory buffer-file-name
))))
1599 (list "perl" (list "-wc " local-file
))))
1601 ;;;; php-specific init-cleanup routines
1602 (defun flymake-php-init ()
1603 (let* ((temp-file (flymake-init-create-temp-buffer-copy
1604 'flymake-create-temp-inplace
))
1605 (local-file (file-relative-name
1607 (file-name-directory buffer-file-name
))))
1608 (list "php" (list "-f" local-file
"-l"))))
1610 ;;;; tex-specific init-cleanup routines
1611 (defun flymake-get-tex-args (file-name)
1612 ;;(list "latex" (list "-c-style-errors" file-name))
1613 (list "texify" (list "--pdf" "--tex-option=-c-style-errors" file-name
)))
1615 (defun flymake-simple-tex-init ()
1616 (flymake-get-tex-args (flymake-init-create-temp-buffer-copy 'flymake-create-temp-inplace
)))
1618 ;; Perhaps there should be a buffer-local variable flymake-master-file
1619 ;; that people can set to override this stuff. Could inherit from
1620 ;; the similar AUCTeX variable.
1621 (defun flymake-master-tex-init ()
1622 (let* ((temp-master-file-name (flymake-init-create-temp-source-and-master-buffer-copy
1623 'flymake-get-include-dirs-dot
'flymake-create-temp-inplace
1625 "[ \t]*\\in\\(?:put\\|clude\\)[ \t]*{\\(.*%s\\)}")))
1626 (when temp-master-file-name
1627 (flymake-get-tex-args temp-master-file-name
))))
1629 (defun flymake-get-include-dirs-dot (_base-dir)
1632 ;;;; xml-specific init-cleanup routines
1633 (defun flymake-xml-init ()
1634 (list flymake-xml-program
1635 (list "val" (flymake-init-create-temp-buffer-copy
1636 'flymake-create-temp-inplace
))))
1639 ;;; flymake.el ends here