; * etc/NEWS: Fix last change.
[emacs.git] / lisp / progmodes / compile.el
blob4cce47e5d8c7ede10158f0891d3269a1e38867a1
1 ;;; compile.el --- run compiler as inferior of Emacs, parse error messages -*- lexical-binding:t -*-
3 ;; Copyright (C) 1985-1987, 1993-1999, 2001-2017 Free Software
4 ;; Foundation, Inc.
6 ;; Authors: Roland McGrath <roland@gnu.org>,
7 ;; Daniel Pfeiffer <occitan@esperanto.org>
8 ;; Maintainer: emacs-devel@gnu.org
9 ;; Keywords: tools, processes
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; This package provides the compile facilities documented in the Emacs user's
29 ;; manual.
31 ;;; Code:
33 (eval-when-compile (require 'cl-lib))
34 (require 'tool-bar)
35 (require 'comint)
37 (defgroup compilation nil
38 "Run compiler as inferior of Emacs, parse error messages."
39 :group 'tools
40 :group 'processes)
43 ;;;###autoload
44 (defcustom compilation-mode-hook nil
45 "List of hook functions run by `compilation-mode'."
46 :type 'hook
47 :group 'compilation)
49 ;;;###autoload
50 (defcustom compilation-start-hook nil
51 "Hook run after starting a new compilation process.
52 The hook is run with one argument, the new process."
53 :type 'hook
54 :group 'compilation)
56 ;;;###autoload
57 (defcustom compilation-window-height nil
58 "Number of lines in a compilation window.
59 If nil, use Emacs default."
60 :type '(choice (const :tag "Default" nil)
61 integer)
62 :group 'compilation)
64 (defvar compilation-filter-hook nil
65 "Hook run after `compilation-filter' has inserted a string into the buffer.
66 It is called with the variable `compilation-filter-start' bound
67 to the position of the start of the inserted text, and point at
68 its end.
70 If Emacs lacks asynchronous process support, this hook is run
71 after `call-process' inserts the grep output into the buffer.")
73 (defvar compilation-filter-start nil
74 "Position of the start of the text inserted by `compilation-filter'.
75 This is bound before running `compilation-filter-hook'.")
77 (defvar compilation-first-column 1
78 "This is how compilers number the first column, usually 1 or 0.
79 If this is buffer-local in the destination buffer, Emacs obeys
80 that value, otherwise it uses the value in the *compilation*
81 buffer. This enables a major-mode to specify its own value.")
83 (defvar compilation-parse-errors-filename-function nil
84 "Function to call to post-process filenames while parsing error messages.
85 It takes one arg FILENAME which is the name of a file as found
86 in the compilation output, and should return a transformed file name.")
88 ;;;###autoload
89 (defvar compilation-process-setup-function nil
90 "Function to call to customize the compilation process.
91 This function is called immediately before the compilation process is
92 started. It can be used to set any variables or functions that are used
93 while processing the output of the compilation process.")
95 ;;;###autoload
96 (defvar compilation-buffer-name-function nil
97 "Function to compute the name of a compilation buffer.
98 The function receives one argument, the name of the major mode of the
99 compilation buffer. It should return a string.
100 If nil, compute the name with `(concat \"*\" (downcase major-mode) \"*\")'.")
102 ;;;###autoload
103 (defvar compilation-finish-function nil
104 "Function to call when a compilation process finishes.
105 It is called with two arguments: the compilation buffer, and a string
106 describing how the process finished.")
108 (make-obsolete-variable 'compilation-finish-function
109 "use `compilation-finish-functions', but it works a little differently."
110 "22.1")
112 ;;;###autoload
113 (defvar compilation-finish-functions nil
114 "Functions to call when a compilation process finishes.
115 Each function is called with two arguments: the compilation buffer,
116 and a string describing how the process finished.")
118 (defvar compilation-in-progress nil
119 "List of compilation processes now running.")
120 (or (assq 'compilation-in-progress minor-mode-alist)
121 (setq minor-mode-alist (cons '(compilation-in-progress " Compiling")
122 minor-mode-alist)))
124 (defvar compilation-error "error"
125 "Stem of message to print when no matches are found.")
127 (defvar compilation-arguments nil
128 "Arguments that were given to `compilation-start'.")
130 (defvar compilation-num-errors-found 0)
131 (defvar compilation-num-warnings-found 0)
132 (defvar compilation-num-infos-found 0)
134 (defconst compilation-mode-line-errors
135 '(" [" (:propertize (:eval (int-to-string compilation-num-errors-found))
136 face compilation-error
137 help-echo "Number of errors so far")
138 " " (:propertize (:eval (int-to-string compilation-num-warnings-found))
139 face compilation-warning
140 help-echo "Number of warnings so far")
141 " " (:propertize (:eval (int-to-string compilation-num-infos-found))
142 face compilation-info
143 help-echo "Number of informational messages so far")
144 "]"))
146 ;; If you make any changes to `compilation-error-regexp-alist-alist',
147 ;; be sure to run the ERT test in test/lisp/progmodes/compile-tests.el.
148 ;; emacs -batch -l compile-tests.el -f ert-run-tests-batch-and-exit
150 (defvar compilation-error-regexp-alist-alist
151 `((absoft
152 "^\\(?:[Ee]rror on \\|[Ww]arning on\\( \\)\\)?[Ll]ine[ \t]+\\([0-9]+\\)[ \t]+\
153 of[ \t]+\"?\\([a-zA-Z]?:?[^\":\n]+\\)\"?:" 3 2 nil (1))
155 (ada
156 "\\(warning: .*\\)? at \\([^ \n]+\\):\\([0-9]+\\)$" 2 3 nil (1))
158 (aix
159 " in line \\([0-9]+\\) of file \\([^ \n]+[^. \n]\\)\\.? " 2 1)
161 (ant
162 "^[ \t]*\\[[^] \n]+\\][ \t]*\\(\\(?:[A-Za-z]:\\\\\\)?[^: \n]+\\):\\([0-9]+\\):\\(?:\\([0-9]+\\):\\([0-9]+\\):\\([0-9]+\\):\\)?\
163 \\( warning\\)?" 1 (2 . 4) (3 . 5) (6))
165 (bash
166 "^\\([^: \n\t]+\\): line \\([0-9]+\\):" 1 2)
168 (borland
169 "^\\(?:Error\\|Warnin\\(g\\)\\) \\(?:[FEW][0-9]+ \\)?\
170 \\([a-zA-Z]?:?[^:( \t\n]+\\)\
171 \\([0-9]+\\)\\(?:[) \t]\\|:[^0-9\n]\\)" 2 3 nil (1))
173 (python-tracebacks-and-caml
174 "^[ \t]*File \\(\"?\\)\\([^,\" \n\t<>]+\\)\\1, lines? \\([0-9]+\\)-?\\([0-9]+\\)?\\(?:$\\|,\
175 \\(?: characters? \\([0-9]+\\)-?\\([0-9]+\\)?:\\)?\\([ \n]Warning\\(?: [0-9]+\\)?:\\)?\\)"
176 2 (3 . 4) (5 . 6) (7))
178 (cmake
179 "^CMake \\(?:Error\\|\\(Warning\\)\\) at \\(.*\\):\\([1-9][0-9]*\\) ([^)]+):$"
180 2 3 nil (1))
181 (cmake-info
182 "^ \\(?: \\*\\)?\\(.*\\):\\([1-9][0-9]*\\) ([^)]+)$"
183 1 2 nil 0)
185 (comma
186 "^\"\\([^,\" \n\t]+\\)\", line \\([0-9]+\\)\
187 \\(?:[(. pos]+\\([0-9]+\\))?\\)?[:.,; (-]\\( warning:\\|[-0-9 ]*(W)\\)?" 1 2 3 (4))
189 (cucumber
190 "\\(?:^cucumber\\(?: -p [^[:space:]]+\\)?\\|#\\)\
191 \\(?: \\)\\([^(].*\\):\\([1-9][0-9]*\\)" 1 2)
193 (msft
194 ;; Must be before edg-1, so that MSVC's longer messages are
195 ;; considered before EDG.
196 ;; The message may be a "warning", "error", or "fatal error" with
197 ;; an error code, or "see declaration of" without an error code.
198 "^ *\\([0-9]+>\\)?\\(\\(?:[a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\)) ?\
199 : \\(?:see declaration\\|\\(?:warnin\\(g\\)\\|[a-z ]+\\) C[0-9]+:\\)"
200 2 3 nil (4))
202 (edg-1
203 "^\\([^ \n]+\\)(\\([0-9]+\\)): \\(?:error\\|warnin\\(g\\)\\|remar\\(k\\)\\)"
204 1 2 nil (3 . 4))
205 (edg-2
206 "at line \\([0-9]+\\) of \"\\([^ \n]+\\)\"$"
207 2 1 nil 0)
209 (epc
210 "^Error [0-9]+ at (\\([0-9]+\\):\\([^)\n]+\\))" 2 1)
212 (ftnchek
213 "\\(^Warning .*\\)? line[ \n]\\([0-9]+\\)[ \n]\\(?:col \\([0-9]+\\)[ \n]\\)?file \\([^ :;\n]+\\)"
214 4 2 3 (1))
216 (iar
217 "^\"\\(.*\\)\",\\([0-9]+\\)\\s-+\\(?:Error\\|Warnin\\(g\\)\\)\\[[0-9]+\\]:"
218 1 2 nil (3))
220 (ibm
221 "^\\([^( \n\t]+\\)(\\([0-9]+\\):\\([0-9]+\\)) :\
222 \\(?:warnin\\(g\\)\\|informationa\\(l\\)\\)?" 1 2 3 (4 . 5))
224 ;; fixme: should be `mips'
225 (irix
226 "^[-[:alnum:]_/ ]+: \\(?:\\(?:[sS]evere\\|[eE]rror\\|[wW]arnin\\(g\\)\\|[iI]nf\\(o\\)\\)[0-9 ]*: \\)?\
227 \\([^,\" \n\t]+\\)\\(?:, line\\|:\\) \\([0-9]+\\):" 3 4 nil (1 . 2))
229 (java
230 "^\\(?:[ \t]+at \\|==[0-9]+== +\\(?:at\\|b\\(y\\)\\)\\).+(\\([^()\n]+\\):\\([0-9]+\\))$" 2 3 nil (1))
232 (jikes-file
233 "^\\(?:Found\\|Issued\\) .* compiling \"\\(.+\\)\":$" 1 nil nil 0)
236 ;; This used to be pathologically slow on long lines (Bug#3441),
237 ;; due to matching filenames via \\(.*?\\). This might be faster.
238 (maven
239 ;; Maven is a popular free software build tool for Java.
240 "\\(\\[WARNING\\] *\\)?\\([^ \n]\\(?:[^\n :]\\| [^-/\n]\\|:[^ \n]\\)*?\\):\\[\\([0-9]+\\),\\([0-9]+\\)\\] " 2 3 4 (1))
242 (jikes-line
243 "^ *\\([0-9]+\\)\\.[ \t]+.*\n +\\(<-*>\n\\*\\*\\* \\(?:Error\\|Warnin\\(g\\)\\)\\)"
244 nil 1 nil 2 0
245 (2 (compilation-face '(3))))
247 (clang-include
248 ,(rx bol "In file included from "
249 (group (+ (not (any ?\n ?:)))) ?:
250 (group (+ (any (?0 . ?9)))) ?:
251 eol)
252 1 2 nil 0)
254 (gcc-include
255 "^\\(?:In file included \\| \\|\t\\)from \
256 \\([0-9]*[^0-9\n]\\(?:[^\n :]\\| [^-/\n]\\|:[^ \n]\\)*?\\):\
257 \\([0-9]+\\)\\(?::\\([0-9]+\\)\\)?\\(?:\\(:\\)\\|\\(,\\|$\\)\\)?"
258 1 2 3 (4 . 5))
260 (ruby-Test::Unit
261 "^[\t ]*\\[\\([^(].*\\):\\([1-9][0-9]*\\)\\(\\]\\)?:in " 1 2)
263 (gnu
264 ;; The first line matches the program name for
266 ;; PROGRAM:SOURCE-FILE-NAME:LINENO: MESSAGE
268 ;; format, which is used for non-interactive programs other than
269 ;; compilers (e.g. the "jade:" entry in compilation.txt).
271 ;; This first line makes things ambiguous with output such as
272 ;; "foo:344:50:blabla" since the "foo" part can match this first
273 ;; line (in which case the file name as "344"). To avoid this,
274 ;; the second line disallows filenames exclusively composed of
275 ;; digits.
277 ;; Similarly, we get lots of false positives with messages including
278 ;; times of the form "HH:MM:SS" where MM is taken as a line number, so
279 ;; the last line tries to rule out message where the info after the
280 ;; line number starts with "SS". --Stef
282 ;; The core of the regexp is the one with *?. It says that a file name
283 ;; can be composed of any non-newline char, but it also rules out some
284 ;; valid but unlikely cases, such as a trailing space or a space
285 ;; followed by a -, or a colon followed by a space.
287 ;; The "in \\|from " exception was added to handle messages from Ruby.
288 ,(rx
290 (? (| (regexp "[[:alpha:]][-[:alnum:].]+: ?")
291 (regexp "[ \t]+\\(?:in \\|from\\)")))
292 (group-n 1 (: (regexp "[0-9]*[^0-9\n]")
293 (*? (| (regexp "[^\n :]")
294 (regexp " [^-/\n]")
295 (regexp ":[^ \n]")))))
296 (regexp ": ?")
297 (group-n 2 (regexp "[0-9]+"))
298 (? (| (: "-"
299 (group-n 4 (regexp "[0-9]+"))
300 (? "." (group-n 5 (regexp "[0-9]+"))))
301 (: (in ".:")
302 (group-n 3 (regexp "[0-9]+"))
303 (? "-"
304 (? (group-n 4 (regexp "[0-9]+")) ".")
305 (group-n 5 (regexp "[0-9]+"))))))
307 (| (: (* " ")
308 (group-n 6 (| "FutureWarning"
309 "RuntimeWarning"
310 "Warning"
311 "warning"
312 "W:")))
313 (: (* " ")
314 (group-n 7 (| (regexp "[Ii]nfo\\(?:\\>\\|rmationa?l?\\)")
315 "I:"
316 (: "[ skipping " (+ ".") " ]")
317 "instantiated from"
318 "required from"
319 (regexp "[Nn]ote"))))
320 (: (* " ")
321 (regexp "[Ee]rror"))
322 (: (regexp "[0-9]?")
323 (| (regexp "[^0-9\n]")
324 eol))
325 (regexp "[0-9][0-9][0-9]")))
326 1 (2 . 4) (3 . 5) (6 . 7))
328 (lcc
329 "^\\(?:E\\|\\(W\\)\\), \\([^(\n]+\\)(\\([0-9]+\\),[ \t]*\\([0-9]+\\)"
330 2 3 4 (1))
332 (makepp
333 "^makepp\\(?:\\(?:: warning\\(:\\).*?\\|\\(: Scanning\\|: [LR]e?l?oading makefile\\|: Imported\\|log:.*?\\) \\|: .*?\\)\
334 `\\(\\(\\S +?\\)\\(?::\\([0-9]+\\)\\)?\\)['(]\\)"
335 4 5 nil (1 . 2) 3
336 (0 (progn (save-match-data
337 (compilation-parse-errors
338 (match-end 0) (line-end-position)
339 `("`\\(\\(\\S +?\\)\\(?::\\([0-9]+\\)\\)?\\)['(]"
340 2 3 nil
341 ,(cond ((match-end 1) 1) ((match-end 2) 0) (t 2))
342 1)))
343 (end-of-line)
344 nil)))
346 ;; Should be lint-1, lint-2 (SysV lint)
347 (mips-1
348 " (\\([0-9]+\\)) in \\([^ \n]+\\)" 2 1)
349 (mips-2
350 " in \\([^()\n ]+\\)(\\([0-9]+\\))$" 1 2)
352 (msft
353 ;; The message may be a "warning", "error", or "fatal error" with
354 ;; an error code, or "see declaration of" without an error code.
355 "^ *\\([0-9]+>\\)?\\(\\(?:[a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\)) \
356 : \\(?:see declaration\\|\\(?:warnin\\(g\\)\\|[a-z ]+\\) C[0-9]+:\\)"
357 2 3 nil (4))
359 (omake
360 ;; "omake -P" reports "file foo changed"
361 ;; (useful if you do "cvs up" and want to see what has changed)
362 "omake: file \\(.*\\) changed" 1 nil nil nil nil
363 ;; FIXME-omake: This tries to prevent reusing pre-existing markers
364 ;; for subsequent messages, since those messages's line numbers
365 ;; are about another version of the file.
366 (0 (progn (compilation--flush-file-structure (match-string 1))
367 nil)))
369 (oracle
370 "^\\(?:Semantic error\\|Error\\|PCC-[0-9]+:\\).* line \\([0-9]+\\)\
371 \\(?:\\(?:,\\| at\\)? column \\([0-9]+\\)\\)?\
372 \\(?:,\\| in\\| of\\)? file \\(.*?\\):?$"
373 3 1 2)
375 ;; "during global destruction": This comes out under "use
376 ;; warnings" in recent perl when breaking circular references
377 ;; during program or thread exit.
378 (perl
379 " at \\([^ \n]+\\) line \\([0-9]+\\)\\(?:[,.]\\|$\\| \
380 during global destruction\\.$\\)" 1 2)
382 (php
383 "\\(?:Parse\\|Fatal\\) error: \\(.*\\) in \\(.*\\) on line \\([0-9]+\\)"
384 2 3 nil nil)
386 (rxp
387 "^\\(?:Error\\|Warnin\\(g\\)\\):.*\n.* line \\([0-9]+\\) char\
388 \\([0-9]+\\) of file://\\(.+\\)"
389 4 2 3 (1))
391 (sparc-pascal-file
392 "^\\w\\w\\w \\w\\w\\w +[0-3]?[0-9] +[0-2][0-9]:[0-5][0-9]:[0-5][0-9]\
393 [12][09][0-9][0-9] +\\(.*\\):$"
394 1 nil nil 0)
395 (sparc-pascal-line
396 "^\\(\\(?:E\\|\\(w\\)\\) +[0-9]+\\) line \\([0-9]+\\) - "
397 nil 3 nil (2) nil (1 (compilation-face '(2))))
398 (sparc-pascal-example
399 "^ +\\([0-9]+\\) +.*\n\\(\\(?:e\\|\\(w\\)\\) [0-9]+\\)-+"
400 nil 1 nil (3) nil (2 (compilation-face '(3))))
402 (sun
403 ": \\(?:ERROR\\|WARNIN\\(G\\)\\|REMAR\\(K\\)\\) \\(?:[[:alnum:] ]+, \\)?\
404 File = \\(.+\\), Line = \\([0-9]+\\)\\(?:, Column = \\([0-9]+\\)\\)?"
405 3 4 5 (1 . 2))
407 (sun-ada
408 "^\\([^, \n\t]+\\), line \\([0-9]+\\), char \\([0-9]+\\)[:., (-]" 1 2 3)
410 (watcom
411 "^[ \t]*\\(\\(?:[a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\)): ?\
412 \\(?:\\(Error! E[0-9]+\\)\\|\\(Warning! W[0-9]+\\)\\):"
413 1 2 nil (4))
415 (4bsd
416 "\\(?:^\\|:: \\|\\S ( \\)\\(/[^ \n\t()]+\\)(\\([0-9]+\\))\
417 \\(?:: \\(warning:\\)?\\|$\\| ),\\)" 1 2 nil (3))
419 (gcov-file
420 "^ *-: *\\(0\\):Source:\\(.+\\)$"
421 2 1 nil 0 nil)
422 (gcov-header
423 "^ *-: *\\(0\\):\\(?:Object\\|Graph\\|Data\\|Runs\\|Programs\\):.+$"
424 nil 1 nil 0 nil)
425 ;; Underlines over all lines of gcov output are too uncomfortable to read.
426 ;; However, hyperlinks embedded in the lines are useful.
427 ;; So I put default face on the lines; and then put
428 ;; compilation-*-face by manually to eliminate the underlines.
429 ;; The hyperlinks are still effective.
430 (gcov-nomark
431 "^ *-: *\\([1-9]\\|[0-9]\\{2,\\}\\):.*$"
432 nil 1 nil 0 nil
433 (0 'default)
434 (1 compilation-line-face))
435 (gcov-called-line
436 "^ *\\([0-9]+\\): *\\([0-9]+\\):.*$"
437 nil 2 nil 0 nil
438 (0 'default)
439 (1 compilation-info-face) (2 compilation-line-face))
440 (gcov-never-called
441 "^ *\\(#####\\): *\\([0-9]+\\):.*$"
442 nil 2 nil 2 nil
443 (0 'default)
444 (1 compilation-error-face) (2 compilation-line-face))
446 (perl--Pod::Checker
447 ;; podchecker error messages, per Pod::Checker.
448 ;; The style is from the Pod::Checker::poderror() function, eg.
449 ;; *** ERROR: Spurious text after =cut at line 193 in file foo.pm
451 ;; Plus end_pod() can give "at line EOF" instead of a
452 ;; number, so for that match "on line N" which is the
453 ;; originating spot, eg.
454 ;; *** ERROR: =over on line 37 without closing =back at line EOF in file bar.pm
456 ;; Plus command() can give both "on line N" and "at line N";
457 ;; the latter is desired and is matched because the .* is
458 ;; greedy.
459 ;; *** ERROR: =over on line 1 without closing =back (at head1) at line 3 in file x.pod
461 "^\\*\\*\\* \\(?:ERROR\\|\\(WARNING\\)\\).* \\(?:at\\|on\\) line \
462 \\([0-9]+\\) \\(?:.* \\)?in file \\([^ \t\n]+\\)"
463 3 2 nil (1))
464 (perl--Test
465 ;; perl Test module error messages.
466 ;; Style per the ok() function "$context", eg.
467 ;; # Failed test 1 in foo.t at line 6
469 "^# Failed test [0-9]+ in \\([^ \t\r\n]+\\) at line \\([0-9]+\\)"
470 1 2)
471 (perl--Test2
472 ;; Or when comparing got/want values, with a "fail #n" if repeated
473 ;; # Test 2 got: "xx" (t-compilation-perl-2.t at line 10)
474 ;; # Test 3 got: "xx" (t-compilation-perl-2.t at line 10 fail #2)
476 ;; And under Test::Harness they're preceded by progress stuff with
477 ;; \r and "NOK",
478 ;; ... NOK 1# Test 1 got: "1234" (t/foo.t at line 46)
480 "^\\(.*NOK.*\\)?# Test [0-9]+ got:.* (\\([^ \t\r\n]+\\) at line \
481 \\([0-9]+\\)\\( fail #[0-9]+\\)?)"
482 2 3)
483 (perl--Test::Harness
484 ;; perl Test::Harness output, eg.
485 ;; NOK 1# Test 1 got: "1234" (t/foo.t at line 46)
487 ;; Test::Harness is slightly designed for tty output, since
488 ;; it prints CRs to overwrite progress messages, but if you
489 ;; run it in with M-x compile this pattern can at least step
490 ;; through the failures.
492 "^.*NOK.* \\([^ \t\r\n]+\\) at line \\([0-9]+\\)"
493 1 2)
494 (weblint
495 ;; The style comes from HTML::Lint::Error::as_string(), eg.
496 ;; index.html (13:1) Unknown element <fdjsk>
498 ;; The pattern only matches filenames without spaces, since that
499 ;; should be usual and should help reduce the chance of a false
500 ;; match of a message from some unrelated program.
502 ;; This message style is quite close to the "ibm" entry which is
503 ;; for IBM C, though that ibm bit doesn't put a space after the
504 ;; filename.
506 "^\\([^ \t\r\n(]+\\) (\\([0-9]+\\):\\([0-9]+\\)) "
507 1 2 3)
509 ;; Guile compilation yields file-headers in the following format:
511 ;; In sourcefile.scm:
513 ;; We need to catch those, but we also need to be aware that Emacs
514 ;; byte-compilation yields compiler headers in similar form of
515 ;; those:
517 ;; In toplevel form:
518 ;; In end of data:
520 ;; We want to catch the Guile file-headers but not the Emacs
521 ;; byte-compilation headers, because that will cause next-error
522 ;; and prev-error to break, because the files "toplevel form" and
523 ;; "end of data" does not exist.
525 ;; To differentiate between these two cases, we require that the
526 ;; file-match must always contain an extension.
528 ;; We should also only treat this as "info", not "error", because
529 ;; we do not know what lines will follow.
530 (guile-file "^In \\(.+\\..+\\):\n" 1 nil nil 0)
531 (guile-line "^ *\\([0-9]+\\): *\\([0-9]+\\)" nil 1 2)
533 "Alist of values for `compilation-error-regexp-alist'.")
535 (defcustom compilation-error-regexp-alist
536 (mapcar 'car compilation-error-regexp-alist-alist)
537 "Alist that specifies how to match errors in compiler output.
538 On GNU and Unix, any string is a valid filename, so these
539 matchers must make some common sense assumptions, which catch
540 normal cases. A shorter list will be lighter on resource usage.
542 Instead of an alist element, you can use a symbol, which is
543 looked up in `compilation-error-regexp-alist-alist'. You can see
544 the predefined symbols and their effects in the file
545 `etc/compilation.txt' (linked below if you are customizing this).
547 Each elt has the form (REGEXP FILE [LINE COLUMN TYPE HYPERLINK
548 HIGHLIGHT...]). If REGEXP matches, the FILE'th subexpression
549 gives the file name, and the LINE'th subexpression gives the line
550 number. The COLUMN'th subexpression gives the column number on
551 that line.
553 If FILE, LINE or COLUMN are nil or that index didn't match, that
554 information is not present on the matched line. In that case the
555 file name is assumed to be the same as the previous one in the
556 buffer, line number defaults to 1 and column defaults to
557 beginning of line's indentation.
559 FILE can also have the form (FILE FORMAT...), where the FORMATs
560 \(e.g. \"%s.c\") will be applied in turn to the recognized file
561 name, until a file of that name is found. Or FILE can also be a
562 function that returns (FILENAME) or (RELATIVE-FILENAME . DIRNAME).
563 In the former case, FILENAME may be relative or absolute.
565 LINE can also be of the form (LINE . END-LINE) meaning a range
566 of lines. COLUMN can also be of the form (COLUMN . END-COLUMN)
567 meaning a range of columns starting on LINE and ending on
568 END-LINE, if that matched.
570 TYPE is 2 or nil for a real error or 1 for warning or 0 for info.
571 TYPE can also be of the form (WARNING . INFO). In that case this
572 will be equivalent to 1 if the WARNING'th subexpression matched
573 or else equivalent to 0 if the INFO'th subexpression matched.
574 See `compilation-error-face', `compilation-warning-face',
575 `compilation-info-face' and `compilation-skip-threshold'.
577 What matched the HYPERLINK'th subexpression has `mouse-face' and
578 `compilation-message-face' applied. If this is nil, the text
579 matched by the whole REGEXP becomes the hyperlink.
581 Additional HIGHLIGHTs take the shape (SUBMATCH FACE), where
582 SUBMATCH is the number of a submatch and FACE is an expression
583 which evaluates to a face name (a symbol or string).
584 Alternatively, FACE can evaluate to a property list of the
585 form (face FACE PROP1 VAL1 PROP2 VAL2 ...), in which case all the
586 listed text properties PROP# are given values VAL# as well."
587 :type '(repeat (choice (symbol :tag "Predefined symbol")
588 (sexp :tag "Error specification")))
589 :link `(file-link :tag "example file"
590 ,(expand-file-name "compilation.txt" data-directory))
591 :group 'compilation)
593 ;;;###autoload(put 'compilation-directory 'safe-local-variable 'stringp)
594 (defvar compilation-directory nil
595 "Directory to restore to when doing `recompile'.")
597 (defvar compilation-directory-matcher
598 '("\\(?:Entering\\|Leavin\\(g\\)\\) directory [`']\\(.+\\)'$" (2 . 1))
599 "A list for tracking when directories are entered or left.
600 If nil, do not track directories, e.g. if all file names are absolute. The
601 first element is the REGEXP matching these messages. It can match any number
602 of variants, e.g. different languages. The remaining elements are all of the
603 form (DIR . LEAVE). If for any one of these the DIR'th subexpression
604 matches, that is a directory name. If LEAVE is nil or the corresponding
605 LEAVE'th subexpression doesn't match, this message is about going into another
606 directory. If it does match anything, this message is about going back to the
607 directory we were in before the last entering message. If you change this,
608 you may also want to change `compilation-page-delimiter'.")
610 (defvar compilation-page-delimiter
611 "^\\(?:\f\\|.*\\(?:Entering\\|Leaving\\) directory [`'].+'\n\\)+"
612 "Value of `page-delimiter' in Compilation mode.")
614 (defvar compilation-mode-font-lock-keywords
615 '(;; configure output lines.
616 ("^[Cc]hecking \\(?:[Ff]or \\|[Ii]f \\|[Ww]hether \\(?:to \\)?\\)?\\(.+\\)\\.\\.\\. *\\(?:(cached) *\\)?\\(\\(yes\\(?: .+\\)?\\)\\|no\\|\\(.*\\)\\)$"
617 (1 font-lock-variable-name-face)
618 (2 (compilation-face '(4 . 3))))
619 ;; Command output lines. Recognize `make[n]:' lines too.
620 ("^\\([[:alnum:]_/.+-]+\\)\\(\\[\\([0-9]+\\)\\]\\)?[ \t]*:"
621 (1 font-lock-function-name-face) (3 compilation-line-face nil t))
622 (" --?o\\(?:utfile\\|utput\\)?[= ]\\(\\S +\\)" . 1)
623 ("^Compilation \\(finished\\).*"
624 (0 '(face nil compilation-message nil help-echo nil mouse-face nil) t)
625 (1 compilation-info-face))
626 ("^Compilation \\(exited abnormally\\|interrupt\\|killed\\|terminated\\|segmentation fault\\)\\(?:.*with code \\([0-9]+\\)\\)?.*"
627 (0 '(face nil compilation-message nil help-echo nil mouse-face nil) t)
628 (1 compilation-error-face)
629 (2 compilation-error-face nil t)))
630 "Additional things to highlight in Compilation mode.
631 This gets tacked on the end of the generated expressions.")
633 (defvar compilation-highlight-regexp t
634 "Regexp matching part of visited source lines to highlight temporarily.
635 Highlight entire line if t; don't highlight source lines if nil.")
637 (defvar compilation-highlight-overlay nil
638 "Overlay used to temporarily highlight compilation matches.")
640 (defcustom compilation-error-screen-columns t
641 "If non-nil, column numbers in error messages are screen columns.
642 Otherwise they are interpreted as character positions, with
643 each character occupying one column.
644 The default is to use screen columns, which requires that the compilation
645 program and Emacs agree about the display width of the characters,
646 especially the TAB character.
647 If this is buffer-local in the destination buffer, Emacs obeys
648 that value, otherwise it uses the value in the *compilation*
649 buffer. This enables a major-mode to specify its own value."
650 :type 'boolean
651 :group 'compilation
652 :version "20.4")
654 (defcustom compilation-read-command t
655 "Non-nil means \\[compile] reads the compilation command to use.
656 Otherwise, \\[compile] just uses the value of `compile-command'.
658 Note that changing this to nil may be a security risk, because a
659 file might define a malicious `compile-command' as a file local
660 variable, and you might not notice. Therefore, `compile-command'
661 is considered unsafe if this variable is nil."
662 :type 'boolean
663 :group 'compilation)
665 ;;;###autoload
666 (defcustom compilation-ask-about-save t
667 "Non-nil means \\[compile] asks which buffers to save before compiling.
668 Otherwise, it saves all modified buffers without asking."
669 :type 'boolean
670 :group 'compilation)
672 (defcustom compilation-save-buffers-predicate nil
673 "The second argument (PRED) passed to `save-some-buffers' before compiling.
674 E.g., one can set this to
675 (lambda ()
676 (string-prefix-p my-compilation-root (file-truename (buffer-file-name))))
677 to limit saving to files located under `my-compilation-root'.
678 Note, that, in general, `compilation-directory' cannot be used instead
679 of `my-compilation-root' here."
680 :type '(choice
681 (const :tag "Default (save all file-visiting buffers)" nil)
682 (const :tag "Save all buffers" t)
683 function)
684 :group 'compilation
685 :version "24.1")
687 ;;;###autoload
688 (defcustom compilation-search-path '(nil)
689 "List of directories to search for source files named in error messages.
690 Elements should be directory names, not file names of directories.
691 The value nil as an element means to try the default directory."
692 :type '(repeat (choice (const :tag "Default" nil)
693 (string :tag "Directory")))
694 :group 'compilation)
696 ;;;###autoload
697 (defcustom compile-command (purecopy "make -k ")
698 "Last shell command used to do a compilation; default for next compilation.
700 Sometimes it is useful for files to supply local values for this variable.
701 You might also use mode hooks to specify it in certain modes, like this:
703 (add-hook \\='c-mode-hook
704 (lambda ()
705 (unless (or (file-exists-p \"makefile\")
706 (file-exists-p \"Makefile\"))
707 (set (make-local-variable \\='compile-command)
708 (concat \"make -k \"
709 (if buffer-file-name
710 (shell-quote-argument
711 (file-name-sans-extension buffer-file-name))))))))
713 It's often useful to leave a space at the end of the value."
714 :type 'string
715 :group 'compilation)
716 ;;;###autoload(put 'compile-command 'safe-local-variable (lambda (a) (and (stringp a) (or (not (boundp 'compilation-read-command)) compilation-read-command))))
718 ;;;###autoload
719 (defcustom compilation-disable-input nil
720 "If non-nil, send end-of-file as compilation process input.
721 This only affects platforms that support asynchronous processes (see
722 `start-process'); synchronous compilation processes never accept input."
723 :type 'boolean
724 :group 'compilation
725 :version "22.1")
727 ;; A weak per-compilation-buffer hash indexed by (FILENAME . DIRECTORY). Each
728 ;; value is a FILE-STRUCTURE as described above, with the car eq to the hash
729 ;; key. This holds the tree seen from root, for storing new nodes.
730 (defvar compilation-locs ())
732 (defvar compilation-debug nil
733 "Set this to t before creating a *compilation* buffer.
734 Then every error line will have a debug text property with the matcher that
735 fit this line and the match data. Use `describe-text-properties'.")
737 (defvar compilation-exit-message-function nil "\
738 If non-nil, called when a compilation process dies to return a status message.
739 This should be a function of three arguments: process status, exit status,
740 and exit message; it returns a cons (MESSAGE . MODELINE) of the strings to
741 write into the compilation buffer, and to put in its mode line.")
743 (defcustom compilation-environment nil
744 "List of environment variables for compilation to inherit.
745 Each element should be a string of the form ENVVARNAME=VALUE.
746 This list is temporarily prepended to `process-environment' prior to
747 starting the compilation process."
748 :type '(repeat (string :tag "ENVVARNAME=VALUE"))
749 :options '(("LANG=C"))
750 :group 'compilation
751 :version "24.1")
753 ;; History of compile commands.
754 (defvar compile-history nil)
756 (defface compilation-error
757 '((t :inherit error))
758 "Face used to highlight compiler errors."
759 :group 'compilation
760 :version "22.1")
762 (defface compilation-warning
763 '((t :inherit warning))
764 "Face used to highlight compiler warnings."
765 :group 'compilation
766 :version "22.1")
768 (defface compilation-info
769 '((t :inherit success))
770 "Face used to highlight compiler information."
771 :group 'compilation
772 :version "22.1")
774 ;; The next three faces must be able to stand out against the
775 ;; `mode-line' and `mode-line-inactive' faces.
777 (defface compilation-mode-line-fail
778 '((default :inherit compilation-error)
779 (((class color) (min-colors 16)) (:foreground "Red1" :weight bold))
780 (((class color) (min-colors 8)) (:foreground "red"))
781 (t (:inverse-video t :weight bold)))
782 "Face for Compilation mode's \"error\" mode line indicator."
783 :group 'compilation
784 :version "24.3")
786 (defface compilation-mode-line-run
787 '((t :inherit compilation-warning))
788 "Face for Compilation mode's \"running\" mode line indicator."
789 :group 'compilation
790 :version "24.3")
792 (defface compilation-mode-line-exit
793 '((default :inherit compilation-info)
794 (((class color) (min-colors 16))
795 (:foreground "ForestGreen" :weight bold))
796 (((class color)) (:foreground "green" :weight bold))
797 (t (:weight bold)))
798 "Face for Compilation mode's \"exit\" mode line indicator."
799 :group 'compilation
800 :version "24.3")
802 (defface compilation-line-number
803 '((t :inherit font-lock-keyword-face))
804 "Face for displaying line numbers in compiler messages."
805 :group 'compilation
806 :version "22.1")
808 (defface compilation-column-number
809 '((t :inherit font-lock-doc-face))
810 "Face for displaying column numbers in compiler messages."
811 :group 'compilation
812 :version "22.1")
814 (defcustom compilation-message-face 'underline
815 "Face name to use for whole messages.
816 Faces `compilation-error-face', `compilation-warning-face',
817 `compilation-info-face', `compilation-line-face' and
818 `compilation-column-face' get prepended to this, when applicable."
819 :type 'face
820 :group 'compilation
821 :version "22.1")
823 (defvar compilation-error-face 'compilation-error
824 "Face name to use for file name in error messages.")
826 (defvar compilation-warning-face 'compilation-warning
827 "Face name to use for file name in warning messages.")
829 (defvar compilation-info-face 'compilation-info
830 "Face name to use for file name in informational messages.")
832 (defvar compilation-line-face 'compilation-line-number
833 "Face name to use for line numbers in compiler messages.")
835 (defvar compilation-column-face 'compilation-column-number
836 "Face name to use for column numbers in compiler messages.")
838 ;; same faces as dired uses
839 (defvar compilation-enter-directory-face 'font-lock-function-name-face
840 "Face name to use for entering directory messages.")
842 (defvar compilation-leave-directory-face 'font-lock-builtin-face
843 "Face name to use for leaving directory messages.")
845 ;; Used for compatibility with the old compile.el.
846 (defvar compilation-parse-errors-function nil)
847 (make-obsolete-variable 'compilation-parse-errors-function
848 'compilation-error-regexp-alist "24.1")
850 (defcustom compilation-auto-jump-to-first-error nil
851 "If non-nil, automatically jump to the first error during compilation."
852 :type 'boolean
853 :group 'compilation
854 :version "23.1")
856 (defvar compilation-auto-jump-to-next nil
857 "If non-nil, automatically jump to the next error encountered.")
858 (make-variable-buffer-local 'compilation-auto-jump-to-next)
860 ;; (defvar compilation-buffer-modtime nil
861 ;; "The buffer modification time, for buffers not associated with files.")
862 ;; (make-variable-buffer-local 'compilation-buffer-modtime)
864 (defvar compilation-skip-to-next-location t
865 "If non-nil, skip multiple error messages for the same source location.")
867 (defcustom compilation-skip-threshold 1
868 "Compilation motion commands skip less important messages.
869 The value can be either 2 -- skip anything less than error, 1 --
870 skip anything less than warning or 0 -- don't skip any messages.
871 Note that all messages not positively identified as warning or
872 info, are considered errors."
873 :type '(choice (const :tag "Skip warnings and info" 2)
874 (const :tag "Skip info" 1)
875 (const :tag "No skip" 0))
876 :group 'compilation
877 :version "22.1")
879 (defun compilation-set-skip-threshold (level)
880 "Switch the `compilation-skip-threshold' level."
881 (interactive
882 (list
883 (mod (if current-prefix-arg
884 (prefix-numeric-value current-prefix-arg)
885 (1+ compilation-skip-threshold))
886 3)))
887 (setq compilation-skip-threshold level)
888 (message "Skipping %s"
889 (pcase compilation-skip-threshold
890 (0 "Nothing")
891 (1 "Info messages")
892 (2 "Warnings and info"))))
894 (defcustom compilation-skip-visited nil
895 "Compilation motion commands skip visited messages if this is t.
896 Visited messages are ones for which the file, line and column have been jumped
897 to from the current content in the current compilation buffer, even if it was
898 from a different message."
899 :type 'boolean
900 :group 'compilation
901 :version "22.1")
903 (defun compilation-type (type)
904 (or (and (car type) (match-end (car type)) 1)
905 (and (cdr type) (match-end (cdr type)) 0)
908 (defun compilation-face (type)
909 (let ((typ (compilation-type type)))
910 (cond
911 ((eq typ 1)
912 compilation-warning-face)
913 ((eq typ 0)
914 compilation-info-face)
915 ((eq typ 2)
916 compilation-error-face))))
918 ;; LOC (or location) is a list of (COLUMN LINE FILE-STRUCTURE nil nil)
920 ;; COLUMN and LINE are numbers parsed from an error message. COLUMN and maybe
921 ;; LINE will be nil for a message that doesn't contain them. Then the
922 ;; location refers to a indented beginning of line or beginning of file.
923 ;; Once any location in some file has been jumped to, the list is extended to
924 ;; (COLUMN LINE FILE-STRUCTURE MARKER TIMESTAMP . VISITED)
925 ;; for all LOCs pertaining to that file.
926 ;; MARKER initially points to LINE and COLUMN in a buffer visiting that file.
927 ;; Being a marker it sticks to some text, when the buffer grows or shrinks
928 ;; before that point. VISITED is t if we have jumped there, else nil.
929 ;; FIXME-omake: TIMESTAMP was used to try and handle "incremental compilation":
930 ;; `omake -P' polls filesystem for changes and recompiles when a file is
931 ;; modified using the same *compilation* buffer. this necessitates
932 ;; re-parsing markers.
934 ;; (cl-defstruct (compilation--loc
935 ;; (:constructor nil)
936 ;; (:copier nil)
937 ;; (:constructor compilation--make-loc
938 ;; (file-struct line col marker))
939 ;; (:conc-name compilation--loc->))
940 ;; col line file-struct marker timestamp visited)
942 ;; FIXME: We don't use a defstruct because of compilation-assq which looks up
943 ;; and creates part of the LOC (only the first cons cell containing the COL).
945 (defmacro compilation--make-cdrloc (line file-struct marker)
946 `(list ,line ,file-struct ,marker nil))
947 (defmacro compilation--loc->col (loc) `(car ,loc))
948 (defmacro compilation--loc->line (loc) `(cadr ,loc))
949 (defmacro compilation--loc->file-struct (loc) `(nth 2 ,loc))
950 (defmacro compilation--loc->marker (loc) `(nth 3 ,loc))
951 ;; (defmacro compilation--loc->timestamp (loc) `(nth 4 ,loc))
952 (defmacro compilation--loc->visited (loc) `(nthcdr 5 ,loc))
954 ;; FILE-STRUCTURE is a list of
955 ;; ((FILENAME DIRECTORY) FORMATS (LINE LOC ...) ...)
957 ;; FILENAME is a string parsed from an error message. DIRECTORY is a string
958 ;; obtained by following directory change messages. DIRECTORY will be nil for
959 ;; an absolute filename. FORMATS is a list of formats to apply to FILENAME if
960 ;; a file of that name can't be found.
961 ;; The rest of the list is an alist of elements with LINE as key. The keys
962 ;; are either nil or line numbers. If present, nil comes first, followed by
963 ;; the numbers in decreasing order. The LOCs for each line are again an alist
964 ;; ordered the same way. Note that the whole file structure is referenced in
965 ;; every LOC.
967 (defmacro compilation--make-file-struct (file-spec formats &optional loc-tree)
968 `(cons ,file-spec (cons ,formats ,loc-tree)))
969 (defmacro compilation--file-struct->file-spec (fs) `(car ,fs))
970 (defmacro compilation--file-struct->formats (fs) `(cadr ,fs))
971 ;; The FORMATS field plays the role of ANCHOR in the loc-tree.
972 (defmacro compilation--file-struct->loc-tree (fs) `(cdr ,fs))
974 ;; MESSAGE is a list of (LOC TYPE END-LOC)
976 ;; TYPE is 0 for info or 1 for warning if the message matcher identified it as
977 ;; such, 2 otherwise (for a real error). END-LOC is a LOC pointing to the
978 ;; other end, if the parsed message contained a range. If the end of the
979 ;; range didn't specify a COLUMN, it defaults to -1, meaning end of line.
980 ;; These are the value of the `compilation-message' text-properties in the
981 ;; compilation buffer.
983 (cl-defstruct (compilation--message
984 (:constructor nil)
985 (:copier nil)
986 ;; (:type list) ;Old representation.
987 (:constructor compilation--make-message (loc type end-loc))
988 (:conc-name compilation--message->))
989 loc type end-loc)
991 (defvar compilation--previous-directory-cache nil
992 "A pair (POS . RES) caching the result of previous directory search.
993 Basically, this pair says that calling
994 (previous-single-property-change POS \\='compilation-directory)
995 returned RES, i.e. there is no change of `compilation-directory' between
996 POS and RES.")
997 (make-variable-buffer-local 'compilation--previous-directory-cache)
999 (defun compilation--flush-directory-cache (start _end)
1000 (cond
1001 ((or (not compilation--previous-directory-cache)
1002 (<= (car compilation--previous-directory-cache) start)))
1003 ((or (not (cdr compilation--previous-directory-cache))
1004 (null (marker-buffer (cdr compilation--previous-directory-cache)))
1005 (<= (cdr compilation--previous-directory-cache) start))
1006 (set-marker (car compilation--previous-directory-cache) start))
1007 (t (setq compilation--previous-directory-cache nil))))
1009 (defun compilation--previous-directory (pos)
1010 "Like (previous-single-property-change POS \\='compilation-directory), but faster."
1011 ;; This avoids an N² behavior when there's no/few compilation-directory
1012 ;; entries, in which case each call to previous-single-property-change
1013 ;; ends up having to walk very far back to find the last change.
1014 (if (and compilation--previous-directory-cache
1015 (< pos (car compilation--previous-directory-cache))
1016 (or (null (cdr compilation--previous-directory-cache))
1017 (< (cdr compilation--previous-directory-cache) pos)))
1018 ;; No need to call previous-single-property-change.
1019 (cdr compilation--previous-directory-cache)
1021 (let* ((cache (and compilation--previous-directory-cache
1022 (<= (car compilation--previous-directory-cache) pos)
1023 (car compilation--previous-directory-cache)))
1024 (prev
1025 (previous-single-property-change
1026 pos 'compilation-directory nil cache))
1027 (res
1028 (cond
1029 ((null cache)
1030 (setq compilation--previous-directory-cache
1031 (cons (copy-marker pos) (if prev (copy-marker prev))))
1032 prev)
1033 ((and prev (= prev cache))
1034 (set-marker (car compilation--previous-directory-cache) pos)
1035 (cdr compilation--previous-directory-cache))
1037 (set-marker cache pos)
1038 (setcdr compilation--previous-directory-cache
1039 (copy-marker prev))
1040 prev))))
1041 (if (markerp res) (marker-position res) res))))
1043 ;; Internal function for calculating the text properties of a directory
1044 ;; change message. The compilation-directory property is important, because it
1045 ;; is the stack of nested enter-messages. Relative filenames on the following
1046 ;; lines are relative to the top of the stack.
1047 (defun compilation-directory-properties (idx leave)
1048 (if leave (setq leave (match-end leave)))
1049 ;; find previous stack, and push onto it, or if `leave' pop it
1050 (let ((dir (compilation--previous-directory (match-beginning 0))))
1051 (setq dir (if dir (or (get-text-property (1- dir) 'compilation-directory)
1052 (get-text-property dir 'compilation-directory))))
1053 `(font-lock-face ,(if leave
1054 compilation-leave-directory-face
1055 compilation-enter-directory-face)
1056 compilation-directory ,(if leave
1057 (or (cdr dir)
1058 '(nil)) ; nil only isn't a property-change
1059 (cons (match-string-no-properties idx) dir))
1060 ;; Place a `compilation-message' everywhere we change text-properties
1061 ;; so compilation--remove-properties can know what to remove.
1062 compilation-message ,(compilation--make-message nil 0 nil)
1063 mouse-face highlight
1064 keymap compilation-button-map
1065 help-echo "mouse-2: visit destination directory")))
1067 ;; Data type `reverse-ordered-alist' retriever. This function retrieves the
1068 ;; KEY element from the ALIST, creating it in the right position if not already
1069 ;; present. ALIST structure is
1070 ;; '(ANCHOR (KEY1 ...) (KEY2 ...)... (KEYn ALIST ...))
1071 ;; ANCHOR is ignored, but necessary so that elements can be inserted. KEY1
1072 ;; may be nil. The other KEYs are ordered backwards so that growing line
1073 ;; numbers can be inserted in front and searching can abort after half the
1074 ;; list on average.
1075 (eval-when-compile ;Don't keep it at runtime if not needed.
1076 (defmacro compilation-assq (key alist)
1077 `(let* ((l1 ,alist)
1078 (l2 (cdr l1)))
1079 (car (if (if (null ,key)
1080 (if l2 (null (caar l2)))
1081 (while (if l2 (if (caar l2) (< ,key (caar l2)) t))
1082 (setq l1 l2
1083 l2 (cdr l1)))
1084 (if l2 (eq ,key (caar l2))))
1086 (setcdr l1 (cons (list ,key) l2)))))))
1088 (defun compilation-auto-jump (buffer pos)
1089 (with-current-buffer buffer
1090 (goto-char pos)
1091 (let ((win (get-buffer-window buffer 0)))
1092 (if win (set-window-point win pos)))
1093 (if compilation-auto-jump-to-first-error
1094 (compile-goto-error))))
1096 ;; This function is the central driver, called when font-locking to gather
1097 ;; all information needed to later jump to corresponding source code.
1098 ;; Return a property list with all meta information on this error location.
1100 (defun compilation-error-properties (file line end-line col end-col type fmt)
1101 (unless (text-property-not-all (match-beginning 0) (point)
1102 'compilation-message nil)
1103 (if file
1104 (when (stringp
1105 (setq file (if (functionp file) (funcall file)
1106 (match-string-no-properties file))))
1107 (let ((dir
1108 (unless (file-name-absolute-p file)
1109 (let ((pos (compilation--previous-directory
1110 (match-beginning 0))))
1111 (when pos
1112 (or (get-text-property (1- pos) 'compilation-directory)
1113 (get-text-property pos 'compilation-directory)))))))
1114 (setq file (cons file (car dir)))))
1115 ;; This message didn't mention one, get it from previous
1116 (let ((prev-pos
1117 ;; Find the previous message.
1118 (previous-single-property-change (point) 'compilation-message)))
1119 (if prev-pos
1120 ;; Get the file structure that belongs to it.
1121 (let* ((prev
1122 (or (get-text-property (1- prev-pos) 'compilation-message)
1123 (get-text-property prev-pos 'compilation-message)))
1124 (prev-file-struct
1125 (and prev
1126 (compilation--loc->file-struct
1127 (compilation--message->loc prev)))))
1129 ;; Construct FILE . DIR from that.
1130 (if prev-file-struct
1131 (setq file (cons (caar prev-file-struct)
1132 (cadr (car prev-file-struct)))))))
1133 (unless file
1134 (setq file '("*unknown*")))))
1135 ;; All of these fields are optional, get them only if we have an index, and
1136 ;; it matched some part of the message.
1137 (and line
1138 (setq line (match-string-no-properties line))
1139 (setq line (string-to-number line)))
1140 (and end-line
1141 (setq end-line (match-string-no-properties end-line))
1142 (setq end-line (string-to-number end-line)))
1143 (if col
1144 (if (functionp col)
1145 (setq col (funcall col))
1146 (and
1147 (setq col (match-string-no-properties col))
1148 (setq col (string-to-number col)))))
1149 (if (and end-col (functionp end-col))
1150 (setq end-col (funcall end-col))
1151 (if (and end-col (setq end-col (match-string-no-properties end-col)))
1152 (setq end-col (- (string-to-number end-col) -1))
1153 (if end-line (setq end-col -1))))
1154 (if (consp type) ; not a static type, check what it is.
1155 (setq type (or (and (car type) (match-end (car type)) 1)
1156 (and (cdr type) (match-end (cdr type)) 0)
1157 2)))
1159 (when (and compilation-auto-jump-to-next
1160 (>= type compilation-skip-threshold))
1161 (kill-local-variable 'compilation-auto-jump-to-next)
1162 (run-with-timer 0 nil 'compilation-auto-jump
1163 (current-buffer) (match-beginning 0)))
1165 (compilation-internal-error-properties
1166 file line end-line col end-col type fmt)))
1168 (defun compilation-beginning-of-line (&optional n)
1169 "Like `beginning-of-line', but accounts for lines hidden by `selective-display'."
1170 (if (or (not (eq selective-display t))
1171 (null n)
1172 (= n 1))
1173 (beginning-of-line n)
1174 (re-search-forward "[\n\r]" nil 'end (1- n))
1175 (if (< n 0)
1176 (beginning-of-line))))
1178 (defun compilation-move-to-column (col screen)
1179 "Go to column COL on the current line.
1180 If SCREEN is non-nil, columns are screen columns, otherwise, they are
1181 just char-counts."
1182 (setq col (- col compilation-first-column))
1183 (if screen
1184 ;; Presumably, the compilation tool doesn't know about our current
1185 ;; `tab-width' setting, so it probably assumed 8-wide TABs (bug#21038).
1186 (let ((tab-width 8)) (move-to-column (max col 0)))
1187 (goto-char (min (+ (line-beginning-position) col) (line-end-position)))))
1189 (defun compilation-internal-error-properties (file line end-line col end-col type fmts)
1190 "Get the meta-info that will be added as text-properties.
1191 LINE, END-LINE, COL, END-COL are integers or nil.
1192 TYPE can be 0, 1, or 2, meaning error, warning, or just info.
1193 FILE should be (FILENAME) or (RELATIVE-FILENAME . DIRNAME) or nil.
1194 FMTS is a list of format specs for transforming the file name.
1195 (See `compilation-error-regexp-alist'.)"
1196 (unless file (setq file '("*unknown*")))
1197 (let* ((file-struct (compilation-get-file-structure file fmts))
1198 ;; Get first already existing marker (if any has one, all have one).
1199 ;; Do this first, as the compilation-assq`s may create new nodes.
1200 (marker-line ; a line structure
1201 (cadr (compilation--file-struct->loc-tree file-struct)))
1202 (marker
1203 (if marker-line (compilation--loc->marker (cadr marker-line))))
1204 (screen-columns compilation-error-screen-columns)
1205 (first-column compilation-first-column)
1206 end-marker loc end-loc)
1207 (if (not (and marker (marker-buffer marker)))
1208 (setq marker nil) ; no valid marker for this file
1209 (unless line (setq line 1)) ; normalize no linenumber to line 1
1210 (catch 'marker ; find nearest loc, at least one exists
1211 (dolist (x (cddr (compilation--file-struct->loc-tree
1212 file-struct))) ; Loop over remaining lines.
1213 (if (> (car x) line) ; Still bigger.
1214 (setq marker-line x)
1215 (if (> (- (or (car marker-line) 1) line)
1216 (- line (car x))) ; Current line is nearer.
1217 (setq marker-line x))
1218 (throw 'marker t))))
1219 (setq marker (compilation--loc->marker (cadr marker-line))
1220 marker-line (or (car marker-line) 1))
1221 (with-current-buffer (marker-buffer marker)
1222 (let ((screen-columns
1223 ;; Obey the compilation-error-screen-columns of the target
1224 ;; buffer if its major mode set it buffer-locally.
1225 (if (local-variable-p 'compilation-error-screen-columns)
1226 compilation-error-screen-columns screen-columns))
1227 (compilation-first-column
1228 (if (local-variable-p 'compilation-first-column)
1229 compilation-first-column first-column)))
1230 (save-excursion
1231 (save-restriction
1232 (widen)
1233 (goto-char (marker-position marker))
1234 ;; Set end-marker if appropriate and go to line.
1235 (if (not (or end-col end-line))
1236 (compilation-beginning-of-line (- line marker-line -1))
1237 (compilation-beginning-of-line (- (or end-line line)
1238 marker-line -1))
1239 (if (or (null end-col) (< end-col 0))
1240 (end-of-line)
1241 (compilation-move-to-column end-col screen-columns))
1242 (setq end-marker (point-marker))
1243 (when end-line
1244 (compilation-beginning-of-line (- line end-line -1))))
1245 (if col
1246 (compilation-move-to-column col screen-columns)
1247 (forward-to-indentation 0))
1248 (setq marker (point-marker)))))))
1250 (setq loc (compilation-assq line (compilation--file-struct->loc-tree
1251 file-struct)))
1252 (setq end-loc
1253 (if end-line
1254 (compilation-assq
1255 end-col (compilation-assq
1256 end-line (compilation--file-struct->loc-tree
1257 file-struct)))
1258 (if end-col ; use same line element
1259 (compilation-assq end-col loc))))
1260 (setq loc (compilation-assq col loc))
1261 ;; If they are new, make the loc(s) reference the file they point to.
1262 ;; FIXME-omake: there's a problem with timestamps here: the markers
1263 ;; relative to which we computed the current `marker' have a timestamp
1264 ;; almost guaranteed to be different from compilation-buffer-modtime, so if
1265 ;; we use their timestamp, we'll never use `loc' since the timestamp won't
1266 ;; match compilation-buffer-modtime, and if we use
1267 ;; compilation-buffer-modtime then we have different timestamps for
1268 ;; locations that were computed together, which doesn't make sense either.
1269 ;; I think this points to a fundamental problem in our approach to the
1270 ;; "omake -P" problem. --Stef
1271 (or (cdr loc)
1272 (setcdr loc (compilation--make-cdrloc line file-struct marker)))
1273 (if end-loc
1274 (or (cdr end-loc)
1275 (setcdr end-loc
1276 (compilation--make-cdrloc (or end-line line) file-struct
1277 end-marker))))
1279 ;; Must start with face
1280 `(font-lock-face ,compilation-message-face
1281 compilation-message ,(compilation--make-message loc type end-loc)
1282 help-echo ,(if col
1283 "mouse-2: visit this file, line and column"
1284 (if line
1285 "mouse-2: visit this file and line"
1286 "mouse-2: visit this file"))
1287 keymap compilation-button-map
1288 mouse-face highlight)))
1290 (defun compilation--put-prop (matchnum prop val)
1291 (when (and (integerp matchnum) (match-beginning matchnum))
1292 (put-text-property
1293 (match-beginning matchnum) (match-end matchnum)
1294 prop val)))
1296 (defun compilation--remove-properties (&optional start end)
1297 (with-silent-modifications
1298 ;; When compile.el used font-lock directly, we could just remove all
1299 ;; our text-properties in one go, but now that we manually place
1300 ;; font-lock-face, we have to be careful to only remove the font-lock-face
1301 ;; we placed.
1302 ;; (remove-list-of-text-properties
1303 ;; (or start (point-min)) (or end (point-max))
1304 ;; '(compilation-debug compilation-directory compilation-message
1305 ;; font-lock-face help-echo mouse-face))
1306 (let (next)
1307 (unless start (setq start (point-min)))
1308 (unless end (setq end (point-max)))
1309 (compilation--flush-directory-cache start end)
1310 (while
1311 (progn
1312 (setq next (or (next-single-property-change
1313 start 'compilation-message nil end)
1314 end))
1315 (when (get-text-property start 'compilation-message)
1316 (remove-list-of-text-properties
1317 start next
1318 '(compilation-debug compilation-directory compilation-message
1319 font-lock-face help-echo mouse-face)))
1320 (< next end))
1321 (setq start next)))))
1323 (defun compilation--parse-region (start end)
1324 (goto-char end)
1325 (unless (bolp)
1326 ;; We generally don't like to parse partial lines.
1327 (cl-assert (eobp))
1328 (when (let ((proc (get-buffer-process (current-buffer))))
1329 (and proc (memq (process-status proc) '(run open))))
1330 (setq end (line-beginning-position))))
1331 (compilation--remove-properties start end)
1332 (if compilation-parse-errors-function
1333 ;; An old package! Try the compatibility code.
1334 (progn
1335 (goto-char start)
1336 (compilation--compat-parse-errors end))
1338 ;; compilation-directory-matcher is the only part that really needs to be
1339 ;; parsed sequentially. So we could split it out, handle directories
1340 ;; like syntax-propertize, and the rest as font-lock-keywords. But since
1341 ;; we want to have it work even when font-lock is off, we'd then need to
1342 ;; use our own compilation-parsed text-property to keep track of the parts
1343 ;; that have already been parsed.
1344 (goto-char start)
1345 (while (re-search-forward (car compilation-directory-matcher)
1346 end t)
1347 (compilation--flush-directory-cache (match-beginning 0) (match-end 0))
1348 (when compilation-debug
1349 (font-lock-append-text-property
1350 (match-beginning 0) (match-end 0)
1351 'compilation-debug
1352 (vector 'directory compilation-directory-matcher)))
1353 (dolist (elt (cdr compilation-directory-matcher))
1354 (add-text-properties (match-beginning (car elt))
1355 (match-end (car elt))
1356 (compilation-directory-properties
1357 (car elt) (cdr elt)))))
1359 (compilation-parse-errors start end)))
1361 (defun compilation--note-type (type)
1362 "Note that a new message with severity TYPE was seen.
1363 This updates the appropriate variable used by the mode-line."
1364 (cl-case type
1365 (0 (cl-incf compilation-num-infos-found))
1366 (1 (cl-incf compilation-num-warnings-found))
1367 (2 (cl-incf compilation-num-errors-found))))
1369 (defun compilation-parse-errors (start end &rest rules)
1370 "Parse errors between START and END.
1371 The errors recognized are the ones specified in RULES which default
1372 to `compilation-error-regexp-alist' if RULES is nil."
1373 (dolist (item (or rules compilation-error-regexp-alist))
1374 (if (symbolp item)
1375 (setq item (cdr (assq item
1376 compilation-error-regexp-alist-alist))))
1377 (let ((file (nth 1 item))
1378 (line (nth 2 item))
1379 (col (nth 3 item))
1380 (type (nth 4 item))
1381 (pat (car item))
1382 end-line end-col fmt
1383 props)
1385 ;; omake reports some error indented, so skip the indentation.
1386 ;; another solution is to modify (some?) regexps in
1387 ;; `compilation-error-regexp-alist'.
1388 ;; note that omake usage is not limited to ocaml and C (for stubs).
1389 ;; FIXME-omake: Doing it here seems wrong, at least it should depend on
1390 ;; whether or not omake's own error messages are recognized.
1391 (cond
1392 ((not (memq 'omake compilation-error-regexp-alist)) nil)
1393 ((string-match "\\`\\([^^]\\|\\^\\( \\*\\|\\[\\)\\)" pat)
1394 nil) ;; Not anchored or anchored but already allows empty spaces.
1395 (t (setq pat (concat "^ *" (substring pat 1)))))
1397 (if (consp file) (setq fmt (cdr file) file (car file)))
1398 (if (consp line) (setq end-line (cdr line) line (car line)))
1399 (if (consp col) (setq end-col (cdr col) col (car col)))
1401 (if (functionp line)
1402 ;; The old compile.el had here an undocumented hook that
1403 ;; allowed `line' to be a function that computed the actual
1404 ;; error location. Let's do our best.
1405 (progn
1406 (goto-char start)
1407 (while (re-search-forward pat end t)
1408 (save-match-data
1409 (when compilation-debug
1410 (font-lock-append-text-property
1411 (match-beginning 0) (match-end 0)
1412 'compilation-debug (vector 'functionp item)))
1413 (add-text-properties
1414 (match-beginning 0) (match-end 0)
1415 (compilation--compat-error-properties
1416 (funcall line (cons (match-string file)
1417 (cons default-directory
1418 (nthcdr 4 item)))
1419 (if col (match-string col))))))
1420 (compilation--put-prop
1421 file 'font-lock-face compilation-error-face)))
1423 (unless (or (null (nth 5 item)) (integerp (nth 5 item)))
1424 (error "HYPERLINK should be an integer: %s" (nth 5 item)))
1426 (goto-char start)
1427 (while (re-search-forward pat end t)
1428 (when (setq props (compilation-error-properties
1429 file line end-line col end-col (or type 2) fmt))
1431 (when (integerp file)
1432 (setq type (if (consp type)
1433 (compilation-type type)
1434 (or type 2)))
1435 (compilation--note-type type)
1437 (compilation--put-prop
1438 file 'font-lock-face
1439 (symbol-value (aref [compilation-info-face
1440 compilation-warning-face
1441 compilation-error-face]
1442 type))))
1444 (compilation--put-prop
1445 line 'font-lock-face compilation-line-face)
1446 (compilation--put-prop
1447 end-line 'font-lock-face compilation-line-face)
1449 (compilation--put-prop
1450 col 'font-lock-face compilation-column-face)
1451 (compilation--put-prop
1452 end-col 'font-lock-face compilation-column-face)
1454 ;; Obey HIGHLIGHT.
1455 (dolist (extra-item (nthcdr 6 item))
1456 (let ((mn (pop extra-item)))
1457 (when (match-beginning mn)
1458 (let ((face (eval (car extra-item))))
1459 (cond
1460 ((null face))
1461 ((or (symbolp face) (stringp face))
1462 (put-text-property
1463 (match-beginning mn) (match-end mn)
1464 'font-lock-face face))
1465 ((and (listp face)
1466 (eq (car face) 'face)
1467 (or (symbolp (cadr face))
1468 (stringp (cadr face))))
1469 (compilation--put-prop mn 'font-lock-face (cadr face))
1470 (add-text-properties
1471 (match-beginning mn) (match-end mn)
1472 (nthcdr 2 face)))
1474 (error "Don't know how to handle face %S"
1475 face)))))))
1476 (let ((mn (or (nth 5 item) 0)))
1477 (when compilation-debug
1478 (font-lock-append-text-property
1479 (match-beginning 0) (match-end 0)
1480 'compilation-debug (vector 'std item props)))
1481 (add-text-properties
1482 (match-beginning mn) (match-end mn)
1483 (cddr props))
1484 (font-lock-append-text-property
1485 (match-beginning mn) (match-end mn)
1486 'font-lock-face (cadr props)))))))))
1488 (defvar compilation--parsed -1)
1489 (make-variable-buffer-local 'compilation--parsed)
1491 (defun compilation--ensure-parse (limit)
1492 "Make sure the text has been parsed up to LIMIT."
1493 (save-excursion
1494 (goto-char limit)
1495 (setq limit (line-beginning-position 2))
1496 (unless (markerp compilation--parsed)
1497 ;; We use a marker for compilation--parsed so that users (such as
1498 ;; grep.el) don't need to flush-parse when they modify the buffer
1499 ;; in a way that impacts buffer positions but does not require
1500 ;; re-parsing.
1501 (setq compilation--parsed (point-min-marker)))
1502 (when (< compilation--parsed limit)
1503 (let ((start (max compilation--parsed (point-min))))
1504 (move-marker compilation--parsed limit)
1505 (goto-char start)
1506 (forward-line 0) ;Not line-beginning-position: ignore (comint) fields.
1507 (while (and (not (bobp))
1508 (get-text-property (1- (point)) 'compilation-multiline))
1509 (forward-line -1))
1510 (with-silent-modifications
1511 (compilation--parse-region (point) compilation--parsed)))))
1512 nil)
1514 (defun compilation--flush-parse (start _end)
1515 "Mark the region between START and END for re-parsing."
1516 (if (markerp compilation--parsed)
1517 (move-marker compilation--parsed (min start compilation--parsed))))
1519 (defun compilation-mode-font-lock-keywords ()
1520 "Return expressions to highlight in Compilation mode."
1521 (append
1522 '((compilation--ensure-parse))
1523 compilation-mode-font-lock-keywords))
1525 (defun compilation-read-command (command)
1526 (read-shell-command "Compile command: " command
1527 (if (equal (car compile-history) command)
1528 '(compile-history . 1)
1529 'compile-history)))
1532 ;;;###autoload
1533 (defun compile (command &optional comint)
1534 "Compile the program including the current buffer. Default: run `make'.
1535 Runs COMMAND, a shell command, in a separate process asynchronously
1536 with output going to the buffer `*compilation*'.
1538 You can then use the command \\[next-error] to find the next error message
1539 and move to the source code that caused it.
1541 If optional second arg COMINT is t the buffer will be in Comint mode with
1542 `compilation-shell-minor-mode'.
1544 Interactively, prompts for the command if the variable
1545 `compilation-read-command' is non-nil; otherwise uses `compile-command'.
1546 With prefix arg, always prompts.
1547 Additionally, with universal prefix arg, compilation buffer will be in
1548 comint mode, i.e. interactive.
1550 To run more than one compilation at once, start one then rename
1551 the `*compilation*' buffer to some other name with
1552 \\[rename-buffer]. Then _switch buffers_ and start the new compilation.
1553 It will create a new `*compilation*' buffer.
1555 On most systems, termination of the main compilation process
1556 kills its subprocesses.
1558 The name used for the buffer is actually whatever is returned by
1559 the function in `compilation-buffer-name-function', so you can set that
1560 to a function that generates a unique name."
1561 (interactive
1562 (list
1563 (let ((command (eval compile-command)))
1564 (if (or compilation-read-command current-prefix-arg)
1565 (compilation-read-command command)
1566 command))
1567 (consp current-prefix-arg)))
1568 (unless (equal command (eval compile-command))
1569 (setq compile-command command))
1570 (save-some-buffers (not compilation-ask-about-save)
1571 compilation-save-buffers-predicate)
1572 (setq-default compilation-directory default-directory)
1573 (compilation-start command comint))
1575 ;; run compile with the default command line
1576 (defun recompile (&optional edit-command)
1577 "Re-compile the program including the current buffer.
1578 If this is run in a Compilation mode buffer, re-use the arguments from the
1579 original use. Otherwise, recompile using `compile-command'.
1580 If the optional argument `edit-command' is non-nil, the command can be edited."
1581 (interactive "P")
1582 (save-some-buffers (not compilation-ask-about-save)
1583 compilation-save-buffers-predicate)
1584 (let ((default-directory (or compilation-directory default-directory))
1585 (command (eval compile-command)))
1586 (when edit-command
1587 (setq command (compilation-read-command (or (car compilation-arguments)
1588 command)))
1589 (if compilation-arguments (setcar compilation-arguments command)))
1590 (apply 'compilation-start (or compilation-arguments (list command)))))
1592 (defcustom compilation-scroll-output nil
1593 "Non-nil to scroll the *compilation* buffer window as output appears.
1595 Setting it causes the Compilation mode commands to put point at the
1596 end of their output window so that the end of the output is always
1597 visible rather than the beginning.
1599 The value `first-error' stops scrolling at the first error, and leaves
1600 point on its location in the *compilation* buffer."
1601 :type '(choice (const :tag "No scrolling" nil)
1602 (const :tag "Scroll compilation output" t)
1603 (const :tag "Stop scrolling at the first error" first-error))
1604 :version "20.3"
1605 :group 'compilation)
1608 (defun compilation-buffer-name (name-of-mode mode-command name-function)
1609 "Return the name of a compilation buffer to use.
1610 If NAME-FUNCTION is non-nil, call it with one argument NAME-OF-MODE
1611 to determine the buffer name.
1612 Likewise if `compilation-buffer-name-function' is non-nil.
1613 If current buffer has the major mode MODE-COMMAND,
1614 return the name of the current buffer, so that it gets reused.
1615 Otherwise, construct a buffer name from NAME-OF-MODE."
1616 (cond (name-function
1617 (funcall name-function name-of-mode))
1618 (compilation-buffer-name-function
1619 (funcall compilation-buffer-name-function name-of-mode))
1620 ((eq mode-command major-mode)
1621 (buffer-name))
1623 (concat "*" (downcase name-of-mode) "*"))))
1625 (defcustom compilation-always-kill nil
1626 "If t, always kill a running compilation process before starting a new one.
1627 If nil, ask to kill it."
1628 :type 'boolean
1629 :version "24.3"
1630 :group 'compilation)
1632 ;;;###autoload
1633 (defun compilation-start (command &optional mode name-function highlight-regexp)
1634 "Run compilation command COMMAND (low level interface).
1635 If COMMAND starts with a cd command, that becomes the `default-directory'.
1636 The rest of the arguments are optional; for them, nil means use the default.
1638 MODE is the major mode to set in the compilation buffer. Mode
1639 may also be t meaning use `compilation-shell-minor-mode' under `comint-mode'.
1641 If NAME-FUNCTION is non-nil, call it with one argument (the mode name)
1642 to determine the buffer name. Otherwise, the default is to
1643 reuses the current buffer if it has the proper major mode,
1644 else use or create a buffer with name based on the major mode.
1646 If HIGHLIGHT-REGEXP is non-nil, `next-error' will temporarily highlight
1647 the matching section of the visited source line; the default is to use the
1648 global value of `compilation-highlight-regexp'.
1650 Returns the compilation buffer created."
1651 (or mode (setq mode 'compilation-mode))
1652 (let* ((name-of-mode
1653 (if (eq mode t)
1654 "compilation"
1655 (replace-regexp-in-string "-mode\\'" "" (symbol-name mode))))
1656 (thisdir default-directory)
1657 (thisenv compilation-environment)
1658 outwin outbuf)
1659 (with-current-buffer
1660 (setq outbuf
1661 (get-buffer-create
1662 (compilation-buffer-name name-of-mode mode name-function)))
1663 (let ((comp-proc (get-buffer-process (current-buffer))))
1664 (if comp-proc
1665 (if (or (not (eq (process-status comp-proc) 'run))
1666 (eq (process-query-on-exit-flag comp-proc) nil)
1667 (yes-or-no-p
1668 (format "A %s process is running; kill it? "
1669 name-of-mode)))
1670 (condition-case ()
1671 (progn
1672 (interrupt-process comp-proc)
1673 (sit-for 1)
1674 (delete-process comp-proc))
1675 (error nil))
1676 (error "Cannot have two processes in `%s' at once"
1677 (buffer-name)))))
1678 ;; first transfer directory from where M-x compile was called
1679 (setq default-directory thisdir)
1680 ;; Make compilation buffer read-only. The filter can still write it.
1681 ;; Clear out the compilation buffer.
1682 (let ((inhibit-read-only t)
1683 (default-directory thisdir))
1684 ;; Then evaluate a cd command if any, but don't perform it yet, else
1685 ;; start-command would do it again through the shell: (cd "..") AND
1686 ;; sh -c "cd ..; make"
1687 (cd (cond
1688 ((not (string-match "\\`\\s *cd\\(?:\\s +\\(\\S +?\\|'[^']*'\\|\"\\(?:[^\"`$\\]\\|\\\\.\\)*\"\\)\\)?\\s *[;&\n]"
1689 command))
1690 default-directory)
1691 ((not (match-end 1)) "~")
1692 ((eq (aref command (match-beginning 1)) ?\')
1693 (substring command (1+ (match-beginning 1))
1694 (1- (match-end 1))))
1695 ((eq (aref command (match-beginning 1)) ?\")
1696 (replace-regexp-in-string
1697 "\\\\\\(.\\)" "\\1"
1698 (substring command (1+ (match-beginning 1))
1699 (1- (match-end 1)))))
1700 ;; Try globbing as well (bug#15417).
1701 (t (let* ((substituted-dir
1702 (substitute-env-vars (match-string 1 command)))
1703 ;; FIXME: This also tries to expand `*' that were
1704 ;; introduced by the envvar expansion!
1705 (expanded-dir
1706 (file-expand-wildcards substituted-dir)))
1707 (if (= (length expanded-dir) 1)
1708 (car expanded-dir)
1709 substituted-dir)))))
1710 (erase-buffer)
1711 ;; Select the desired mode.
1712 (if (not (eq mode t))
1713 (progn
1714 (buffer-disable-undo)
1715 (funcall mode))
1716 (setq buffer-read-only nil)
1717 (with-no-warnings (comint-mode))
1718 (compilation-shell-minor-mode))
1719 ;; Remember the original dir, so we can use it when we recompile.
1720 ;; default-directory' can't be used reliably for that because it may be
1721 ;; affected by the special handling of "cd ...;".
1722 ;; NB: must be done after (funcall mode) as that resets local variables
1723 (set (make-local-variable 'compilation-directory) thisdir)
1724 (set (make-local-variable 'compilation-environment) thisenv)
1725 (if highlight-regexp
1726 (set (make-local-variable 'compilation-highlight-regexp)
1727 highlight-regexp))
1728 (if (or compilation-auto-jump-to-first-error
1729 (eq compilation-scroll-output 'first-error))
1730 (set (make-local-variable 'compilation-auto-jump-to-next) t))
1731 ;; Output a mode setter, for saving and later reloading this buffer.
1732 (insert "-*- mode: " name-of-mode
1733 "; default-directory: "
1734 (prin1-to-string (abbreviate-file-name default-directory))
1735 " -*-\n"
1736 (format "%s started at %s\n\n"
1737 mode-name
1738 (substring (current-time-string) 0 19))
1739 command "\n")
1740 (setq thisdir default-directory))
1741 (set-buffer-modified-p nil))
1742 ;; Pop up the compilation buffer.
1743 ;; https://lists.gnu.org/r/emacs-devel/2007-11/msg01638.html
1744 (setq outwin (display-buffer outbuf '(nil (allow-no-window . t))))
1745 (with-current-buffer outbuf
1746 (let ((process-environment
1747 (append
1748 compilation-environment
1749 (if (if (boundp 'system-uses-terminfo);`If' for compiler warning.
1750 system-uses-terminfo)
1751 (list "TERM=dumb" "TERMCAP="
1752 (format "COLUMNS=%d" (window-width)))
1753 (list "TERM=emacs"
1754 (format "TERMCAP=emacs:co#%d:tc=unknown:"
1755 (window-width))))
1756 (list (format "INSIDE_EMACS=%s,compile" emacs-version))
1757 (copy-sequence process-environment))))
1758 (set (make-local-variable 'compilation-arguments)
1759 (list command mode name-function highlight-regexp))
1760 (set (make-local-variable 'revert-buffer-function)
1761 'compilation-revert-buffer)
1762 (and outwin
1763 ;; Forcing the window-start overrides the usual redisplay
1764 ;; feature of bringing point into view, so setting the
1765 ;; window-start to top of the buffer risks losing the
1766 ;; effect of moving point to EOB below, per
1767 ;; compilation-scroll-output, if the command is long
1768 ;; enough to push point outside of the window. This
1769 ;; could happen, e.g., in `rgrep'.
1770 (not compilation-scroll-output)
1771 (set-window-start outwin (point-min)))
1773 ;; Position point as the user will see it.
1774 (let ((desired-visible-point
1775 ;; Put it at the end if `compilation-scroll-output' is set.
1776 (if compilation-scroll-output
1777 (point-max)
1778 ;; Normally put it at the top.
1779 (point-min))))
1780 (goto-char desired-visible-point)
1781 (when (and outwin (not (eq outwin (selected-window))))
1782 (set-window-point outwin desired-visible-point)))
1784 ;; The setup function is called before compilation-set-window-height
1785 ;; so it can set the compilation-window-height buffer locally.
1786 (if compilation-process-setup-function
1787 (funcall compilation-process-setup-function))
1788 (and outwin (compilation-set-window-height outwin))
1789 ;; Start the compilation.
1790 (if (fboundp 'make-process)
1791 (let ((proc
1792 (if (eq mode t)
1793 ;; comint uses `start-file-process'.
1794 (get-buffer-process
1795 (with-no-warnings
1796 (comint-exec
1797 outbuf (downcase mode-name)
1798 (if (file-remote-p default-directory)
1799 "/bin/sh"
1800 shell-file-name)
1801 nil `("-c" ,command))))
1802 (start-file-process-shell-command (downcase mode-name)
1803 outbuf command))))
1804 ;; Make the buffer's mode line show process state.
1805 (setq mode-line-process
1806 '((:propertize ":%s" face compilation-mode-line-run)
1807 compilation-mode-line-errors))
1809 ;; Set the process as killable without query by default.
1810 ;; This allows us to start a new compilation without
1811 ;; getting prompted.
1812 (when compilation-always-kill
1813 (set-process-query-on-exit-flag proc nil))
1815 (set-process-sentinel proc 'compilation-sentinel)
1816 (unless (eq mode t)
1817 ;; Keep the comint filter, since it's needed for proper
1818 ;; handling of the prompts.
1819 (set-process-filter proc 'compilation-filter))
1820 ;; Use (point-max) here so that output comes in
1821 ;; after the initial text,
1822 ;; regardless of where the user sees point.
1823 (set-marker (process-mark proc) (point-max) outbuf)
1824 (when compilation-disable-input
1825 (condition-case nil
1826 (process-send-eof proc)
1827 ;; The process may have exited already.
1828 (error nil)))
1829 (run-hook-with-args 'compilation-start-hook proc)
1830 (setq compilation-in-progress
1831 (cons proc compilation-in-progress)))
1832 ;; No asynchronous processes available.
1833 (message "Executing `%s'..." command)
1834 ;; Fake mode line display as if `start-process' were run.
1835 (setq mode-line-process
1836 '((:propertize ":run" face compilation-mode-line-run)
1837 compilation-mode-line-errors))
1838 (force-mode-line-update)
1839 (sit-for 0) ; Force redisplay
1840 (save-excursion
1841 ;; Insert the output at the end, after the initial text,
1842 ;; regardless of where the user sees point.
1843 (goto-char (point-max))
1844 (let* ((inhibit-read-only t) ; call-process needs to modify outbuf
1845 (compilation-filter-start (point))
1846 (status (call-process shell-file-name nil outbuf nil "-c"
1847 command)))
1848 (run-hooks 'compilation-filter-hook)
1849 (cond ((numberp status)
1850 (compilation-handle-exit
1851 'exit status
1852 (if (zerop status)
1853 "finished\n"
1854 (format "exited abnormally with code %d\n" status))))
1855 ((stringp status)
1856 (compilation-handle-exit 'signal status
1857 (concat status "\n")))
1859 (compilation-handle-exit 'bizarre status status)))))
1860 (set-buffer-modified-p nil)
1861 (message "Executing `%s'...done" command)))
1862 ;; Now finally cd to where the shell started make/grep/...
1863 (setq default-directory thisdir)
1864 ;; The following form selected outwin ever since revision 1.183,
1865 ;; so possibly messing up point in some other window (bug#1073).
1866 ;; Moved into the scope of with-current-buffer, though still with
1867 ;; complete disregard for the case when compilation-scroll-output
1868 ;; equals 'first-error (martin 2008-10-04).
1869 (when compilation-scroll-output
1870 (goto-char (point-max))))
1872 ;; Make it so the next C-x ` will use this buffer.
1873 (setq next-error-last-buffer outbuf)))
1875 (defun compilation-set-window-height (window)
1876 "Set the height of WINDOW according to `compilation-window-height'."
1877 (let ((height (buffer-local-value 'compilation-window-height (window-buffer window))))
1878 (and height
1879 (window-full-width-p window)
1880 ;; If window is alone in its frame, aside from a minibuffer,
1881 ;; don't change its height.
1882 (not (eq window (frame-root-window (window-frame window))))
1883 ;; Stef said that doing the saves in this order is safer:
1884 (save-excursion
1885 (save-selected-window
1886 (select-window window)
1887 (enlarge-window (- height (window-height))))))))
1889 (defvar compilation-menu-map
1890 (let ((map (make-sparse-keymap "Errors"))
1891 (opt-map (make-sparse-keymap "Skip")))
1892 (define-key map [stop-subjob]
1893 '(menu-item "Stop Compilation" kill-compilation
1894 :help "Kill the process made by the M-x compile or M-x grep commands"))
1895 (define-key map [compilation-mode-separator3]
1896 '("----" . nil))
1897 (define-key map [compilation-next-error-follow-minor-mode]
1898 '(menu-item
1899 "Auto Error Display" next-error-follow-minor-mode
1900 :help "Display the error under cursor when moving the cursor"
1901 :button (:toggle . next-error-follow-minor-mode)))
1902 (define-key map [compilation-skip]
1903 (cons "Skip Less Important Messages" opt-map))
1904 (define-key opt-map [compilation-skip-none]
1905 '(menu-item "Don't Skip Any Messages"
1906 (lambda ()
1907 (interactive)
1908 (customize-set-variable 'compilation-skip-threshold 0))
1909 :help "Do not skip any type of messages"
1910 :button (:radio . (eq compilation-skip-threshold 0))))
1911 (define-key opt-map [compilation-skip-info]
1912 '(menu-item "Skip Info"
1913 (lambda ()
1914 (interactive)
1915 (customize-set-variable 'compilation-skip-threshold 1))
1916 :help "Skip anything less than warning"
1917 :button (:radio . (eq compilation-skip-threshold 1))))
1918 (define-key opt-map [compilation-skip-warning-and-info]
1919 '(menu-item "Skip Warnings and Info"
1920 (lambda ()
1921 (interactive)
1922 (customize-set-variable 'compilation-skip-threshold 2))
1923 :help "Skip over Warnings and Info, stop for errors"
1924 :button (:radio . (eq compilation-skip-threshold 2))))
1925 (define-key map [compilation-mode-separator2]
1926 '("----" . nil))
1927 (define-key map [compilation-first-error]
1928 '(menu-item "First Error" first-error
1929 :help "Restart at the first error, visit corresponding source code"))
1930 (define-key map [compilation-previous-error]
1931 '(menu-item "Previous Error" previous-error
1932 :help "Visit previous `next-error' message and corresponding source code"))
1933 (define-key map [compilation-next-error]
1934 '(menu-item "Next Error" next-error
1935 :help "Visit next `next-error' message and corresponding source code"))
1936 map))
1938 (defvar compilation-minor-mode-map
1939 (let ((map (make-sparse-keymap)))
1940 (set-keymap-parent map special-mode-map)
1941 (define-key map [mouse-2] 'compile-goto-error)
1942 (define-key map [follow-link] 'mouse-face)
1943 (define-key map "\C-c\C-c" 'compile-goto-error)
1944 (define-key map "\C-m" 'compile-goto-error)
1945 (define-key map "\C-o" 'compilation-display-error)
1946 (define-key map "\C-c\C-k" 'kill-compilation)
1947 (define-key map "\M-n" 'compilation-next-error)
1948 (define-key map "\M-p" 'compilation-previous-error)
1949 (define-key map "\M-{" 'compilation-previous-file)
1950 (define-key map "\M-}" 'compilation-next-file)
1951 (define-key map "g" 'recompile) ; revert
1952 ;; Set up the menu-bar
1953 (define-key map [menu-bar compilation]
1954 (cons "Errors" compilation-menu-map))
1955 map)
1956 "Keymap for `compilation-minor-mode'.")
1958 (defvar compilation-shell-minor-mode-map
1959 (let ((map (make-sparse-keymap)))
1960 (define-key map "\M-\C-m" 'compile-goto-error)
1961 (define-key map "\M-\C-n" 'compilation-next-error)
1962 (define-key map "\M-\C-p" 'compilation-previous-error)
1963 (define-key map "\M-{" 'compilation-previous-file)
1964 (define-key map "\M-}" 'compilation-next-file)
1965 ;; Set up the menu-bar
1966 (define-key map [menu-bar compilation]
1967 (cons "Errors" compilation-menu-map))
1968 map)
1969 "Keymap for `compilation-shell-minor-mode'.")
1971 (defvar compilation-button-map
1972 (let ((map (make-sparse-keymap)))
1973 (define-key map [mouse-2] 'compile-goto-error)
1974 (define-key map [follow-link] 'mouse-face)
1975 (define-key map "\C-m" 'compile-goto-error)
1976 map)
1977 "Keymap for compilation-message buttons.")
1978 (fset 'compilation-button-map compilation-button-map)
1980 (defvar compilation-mode-map
1981 (let ((map (make-sparse-keymap)))
1982 ;; Don't inherit from compilation-minor-mode-map,
1983 ;; because that introduces a menu bar item we don't want.
1984 ;; That confuses C-down-mouse-3.
1985 (set-keymap-parent map special-mode-map)
1986 (define-key map [mouse-2] 'compile-goto-error)
1987 (define-key map [follow-link] 'mouse-face)
1988 (define-key map "\C-c\C-c" 'compile-goto-error)
1989 (define-key map "\C-m" 'compile-goto-error)
1990 (define-key map "\C-o" 'compilation-display-error)
1991 (define-key map "\C-c\C-k" 'kill-compilation)
1992 (define-key map "\M-n" 'compilation-next-error)
1993 (define-key map "\M-p" 'compilation-previous-error)
1994 (define-key map "\M-{" 'compilation-previous-file)
1995 (define-key map "\M-}" 'compilation-next-file)
1996 (define-key map "\t" 'compilation-next-error)
1997 (define-key map [backtab] 'compilation-previous-error)
1998 (define-key map "g" 'recompile) ; revert
2000 (define-key map "\C-c\C-f" 'next-error-follow-minor-mode)
2002 ;; Set up the menu-bar
2003 (let ((submap (make-sparse-keymap "Compile")))
2004 (define-key map [menu-bar compilation]
2005 (cons "Compile" submap))
2006 (set-keymap-parent submap compilation-menu-map))
2007 (define-key map [menu-bar compilation compilation-separator2]
2008 '("----" . nil))
2009 (define-key map [menu-bar compilation compilation-grep]
2010 '(menu-item "Search Files (grep)..." grep
2011 :help "Run grep, with user-specified args, and collect output in a buffer"))
2012 (define-key map [menu-bar compilation compilation-recompile]
2013 '(menu-item "Recompile" recompile
2014 :help "Re-compile the program including the current buffer"))
2015 (define-key map [menu-bar compilation compilation-compile]
2016 '(menu-item "Compile..." compile
2017 :help "Compile the program including the current buffer. Default: run `make'"))
2018 map)
2019 "Keymap for compilation log buffers.
2020 `compilation-minor-mode-map' is a parent of this.")
2022 (defvar compilation-mode-tool-bar-map
2023 ;; When bootstrapping, tool-bar-map is not properly initialized yet,
2024 ;; so don't do anything.
2025 (when (keymapp tool-bar-map)
2026 (let ((map (copy-keymap tool-bar-map)))
2027 (define-key map [undo] nil)
2028 (define-key map [separator-2] nil)
2029 (define-key-after map [separator-compile] menu-bar-separator)
2030 (tool-bar-local-item
2031 "left-arrow" 'previous-error-no-select 'previous-error-no-select map
2032 :rtl "right-arrow"
2033 :help "Goto previous error")
2034 (tool-bar-local-item
2035 "right-arrow" 'next-error-no-select 'next-error-no-select map
2036 :rtl "left-arrow"
2037 :help "Goto next error")
2038 (tool-bar-local-item
2039 "cancel" 'kill-compilation 'kill-compilation map
2040 :enable '(let ((buffer (compilation-find-buffer)))
2041 (get-buffer-process buffer))
2042 :help "Stop compilation")
2043 (tool-bar-local-item
2044 "refresh" 'recompile 'recompile map
2045 :help "Restart compilation")
2046 map)))
2048 (put 'compilation-mode 'mode-class 'special)
2050 ;;;###autoload
2051 (defun compilation-mode (&optional name-of-mode)
2052 "Major mode for compilation log buffers.
2053 \\<compilation-mode-map>To visit the source for a line-numbered error,
2054 move point to the error message line and type \\[compile-goto-error].
2055 To kill the compilation, type \\[kill-compilation].
2057 Runs `compilation-mode-hook' with `run-mode-hooks' (which see).
2059 \\{compilation-mode-map}"
2060 (interactive)
2061 (kill-all-local-variables)
2062 (use-local-map compilation-mode-map)
2063 ;; Let windows scroll along with the output.
2064 (set (make-local-variable 'window-point-insertion-type) t)
2065 (set (make-local-variable 'tool-bar-map) compilation-mode-tool-bar-map)
2066 (setq major-mode 'compilation-mode ; FIXME: Use define-derived-mode.
2067 mode-name (or name-of-mode "Compilation"))
2068 (set (make-local-variable 'page-delimiter)
2069 compilation-page-delimiter)
2070 ;; (set (make-local-variable 'compilation-buffer-modtime) nil)
2071 (compilation-setup)
2072 ;; Turn off deferred fontifications in the compilation buffer, if
2073 ;; the user turned them on globally. This is because idle timers
2074 ;; aren't re-run after receiving input from a subprocess, so the
2075 ;; buffer is left unfontified after the compilation exits, until
2076 ;; some other input event happens.
2077 (set (make-local-variable 'jit-lock-defer-time) nil)
2078 (setq buffer-read-only t)
2079 (run-mode-hooks 'compilation-mode-hook))
2081 ;;;###autoload
2082 (put 'define-compilation-mode 'doc-string-elt 3)
2084 (defmacro define-compilation-mode (mode name doc &rest body)
2085 "This is like `define-derived-mode' without the PARENT argument.
2086 The parent is always `compilation-mode' and the customizable `compilation-...'
2087 variables are also set from the name of the mode you have chosen,
2088 by replacing the first word, e.g., `compilation-scroll-output' from
2089 `grep-scroll-output' if that variable exists."
2090 (let ((mode-name (replace-regexp-in-string "-mode\\'" "" (symbol-name mode))))
2091 `(define-derived-mode ,mode compilation-mode ,name
2092 ,doc
2093 ,@(mapcar (lambda (v)
2094 (setq v (cons v
2095 (intern-soft (replace-regexp-in-string
2096 "^compilation" mode-name
2097 (symbol-name v)))))
2098 (and (cdr v)
2099 (or (boundp (cdr v))
2100 ;; FIXME: This is hackish, using undocumented info.
2101 (if (boundp 'byte-compile-bound-variables)
2102 (memq (cdr v) byte-compile-bound-variables)))
2103 `(set (make-local-variable ',(car v)) ,(cdr v))))
2104 '(compilation-buffer-name-function
2105 compilation-directory-matcher
2106 compilation-error
2107 compilation-error-regexp-alist
2108 compilation-error-regexp-alist-alist
2109 compilation-error-screen-columns
2110 compilation-finish-function
2111 compilation-finish-functions
2112 compilation-first-column
2113 compilation-mode-font-lock-keywords
2114 compilation-page-delimiter
2115 compilation-parse-errors-filename-function
2116 compilation-process-setup-function
2117 compilation-scroll-output
2118 compilation-search-path
2119 compilation-skip-threshold
2120 compilation-window-height))
2121 ,@body)))
2123 (defun compilation-revert-buffer (ignore-auto noconfirm)
2124 (if buffer-file-name
2125 (let (revert-buffer-function)
2126 (revert-buffer ignore-auto noconfirm))
2127 (if (or noconfirm (yes-or-no-p (format "Restart compilation? ")))
2128 (apply 'compilation-start compilation-arguments))))
2130 (defvar compilation-current-error nil
2131 "Marker to the location from where the next error will be found.
2132 The global commands next/previous/first-error/goto-error use this.")
2134 (defvar compilation-messages-start nil
2135 "Buffer position of the beginning of the compilation messages.
2136 If nil, use the beginning of buffer.")
2138 (defun compilation-setup (&optional minor)
2139 "Prepare the buffer for the compilation parsing commands to work.
2140 Optional argument MINOR indicates this is called from
2141 `compilation-minor-mode'."
2142 (make-local-variable 'compilation-current-error)
2143 (make-local-variable 'compilation-messages-start)
2144 (make-local-variable 'compilation-error-screen-columns)
2145 (make-local-variable 'overlay-arrow-position)
2146 (setq-local compilation-num-errors-found 0)
2147 (setq-local compilation-num-warnings-found 0)
2148 (setq-local compilation-num-infos-found 0)
2149 (set (make-local-variable 'overlay-arrow-string) "")
2150 (setq next-error-overlay-arrow-position nil)
2151 (add-hook 'kill-buffer-hook
2152 (lambda () (setq next-error-overlay-arrow-position nil)) nil t)
2153 ;; Note that compilation-next-error-function is for interfacing
2154 ;; with the next-error function in simple.el, and it's only
2155 ;; coincidentally named similarly to compilation-next-error.
2156 (setq next-error-function 'compilation-next-error-function)
2157 (set (make-local-variable 'comint-file-name-prefix)
2158 (or (file-remote-p default-directory) ""))
2159 (set (make-local-variable 'compilation-locs)
2160 (make-hash-table :test 'equal :weakness 'value))
2161 ;; It's generally preferable to use after-change-functions since they
2162 ;; can be subject to combine-after-change-calls, but if we do that, we risk
2163 ;; running our hook after font-lock, resulting in incorrect refontification.
2164 (add-hook 'before-change-functions 'compilation--flush-parse nil t)
2165 ;; Also for minor mode, since it's not permanent-local.
2166 (add-hook 'change-major-mode-hook #'compilation--remove-properties nil t)
2167 (if minor
2168 (progn
2169 (font-lock-add-keywords nil (compilation-mode-font-lock-keywords))
2170 (font-lock-flush))
2171 (setq font-lock-defaults '(compilation-mode-font-lock-keywords t))))
2173 (defun compilation--unsetup ()
2174 ;; Only for minor mode.
2175 (font-lock-remove-keywords nil (compilation-mode-font-lock-keywords))
2176 (remove-hook 'before-change-functions 'compilation--flush-parse t)
2177 (kill-local-variable 'compilation--parsed)
2178 (compilation--remove-properties)
2179 (font-lock-flush))
2181 ;;;###autoload
2182 (define-minor-mode compilation-shell-minor-mode
2183 "Toggle Compilation Shell minor mode.
2184 With a prefix argument ARG, enable Compilation Shell minor mode
2185 if ARG is positive, and disable it otherwise. If called from
2186 Lisp, enable the mode if ARG is omitted or nil.
2188 When Compilation Shell minor mode is enabled, all the
2189 error-parsing commands of the Compilation major mode are
2190 available but bound to keys that don't collide with Shell mode.
2191 See `compilation-mode'."
2192 nil " Shell-Compile"
2193 :group 'compilation
2194 (if compilation-shell-minor-mode
2195 (compilation-setup t)
2196 (compilation--unsetup)))
2198 ;;;###autoload
2199 (define-minor-mode compilation-minor-mode
2200 "Toggle Compilation minor mode.
2201 With a prefix argument ARG, enable Compilation minor mode if ARG
2202 is positive, and disable it otherwise. If called from Lisp,
2203 enable the mode if ARG is omitted or nil.
2205 When Compilation minor mode is enabled, all the error-parsing
2206 commands of Compilation major mode are available. See
2207 `compilation-mode'."
2208 nil " Compilation"
2209 :group 'compilation
2210 (if compilation-minor-mode
2211 (compilation-setup t)
2212 (compilation--unsetup)))
2214 (defun compilation-handle-exit (process-status exit-status msg)
2215 "Write MSG in the current buffer and hack its `mode-line-process'."
2216 (let ((inhibit-read-only t)
2217 (status (if compilation-exit-message-function
2218 (funcall compilation-exit-message-function
2219 process-status exit-status msg)
2220 (cons msg exit-status)))
2221 (omax (point-max))
2222 (opoint (point))
2223 (cur-buffer (current-buffer)))
2224 ;; Record where we put the message, so we can ignore it later on.
2225 (goto-char omax)
2226 (insert ?\n mode-name " " (car status))
2227 (if (and (numberp compilation-window-height)
2228 (zerop compilation-window-height))
2229 (message "%s" (cdr status)))
2230 (if (bolp)
2231 (forward-char -1))
2232 (insert " at " (substring (current-time-string) 0 19))
2233 (goto-char (point-max))
2234 ;; Prevent that message from being recognized as a compilation error.
2235 (add-text-properties omax (point)
2236 (append '(compilation-handle-exit t) nil))
2237 (setq mode-line-process
2238 (list
2239 (let ((out-string (format ":%s [%s]" process-status (cdr status)))
2240 (msg (format "%s %s" mode-name
2241 (replace-regexp-in-string "\n?$" ""
2242 (car status)))))
2243 (message "%s" msg)
2244 (propertize out-string
2245 'help-echo msg
2246 'face (if (> exit-status 0)
2247 'compilation-mode-line-fail
2248 'compilation-mode-line-exit)))
2249 compilation-mode-line-errors))
2250 ;; Force mode line redisplay soon.
2251 (force-mode-line-update)
2252 (if (and opoint (< opoint omax))
2253 (goto-char opoint))
2254 (with-no-warnings
2255 (if compilation-finish-function
2256 (funcall compilation-finish-function cur-buffer msg)))
2257 (run-hook-with-args 'compilation-finish-functions cur-buffer msg)))
2259 ;; Called when compilation process changes state.
2260 (defun compilation-sentinel (proc msg)
2261 "Sentinel for compilation buffers."
2262 (if (memq (process-status proc) '(exit signal))
2263 (let ((buffer (process-buffer proc)))
2264 (if (null (buffer-name buffer))
2265 ;; buffer killed
2266 (set-process-buffer proc nil)
2267 (with-current-buffer buffer
2268 ;; Write something in the compilation buffer
2269 ;; and hack its mode line.
2270 (compilation-handle-exit (process-status proc)
2271 (process-exit-status proc)
2272 msg)
2273 ;; Since the buffer and mode line will show that the
2274 ;; process is dead, we can delete it now. Otherwise it
2275 ;; will stay around until M-x list-processes.
2276 (delete-process proc)))
2277 (setq compilation-in-progress (delq proc compilation-in-progress)))))
2279 (defun compilation-filter (proc string)
2280 "Process filter for compilation buffers.
2281 Just inserts the text,
2282 handles carriage motion (see `comint-inhibit-carriage-motion'),
2283 and runs `compilation-filter-hook'."
2284 (when (buffer-live-p (process-buffer proc))
2285 (with-current-buffer (process-buffer proc)
2286 (let ((inhibit-read-only t)
2287 ;; `save-excursion' doesn't use the right insertion-type for us.
2288 (pos (copy-marker (point) t))
2289 ;; `save-restriction' doesn't use the right insertion type either:
2290 ;; If we are inserting at the end of the accessible part of the
2291 ;; buffer, keep the inserted text visible.
2292 (min (point-min-marker))
2293 (max (copy-marker (point-max) t))
2294 (compilation-filter-start (marker-position (process-mark proc))))
2295 (unwind-protect
2296 (progn
2297 (widen)
2298 (goto-char compilation-filter-start)
2299 ;; We used to use `insert-before-markers', so that windows with
2300 ;; point at `process-mark' scroll along with the output, but we
2301 ;; now use window-point-insertion-type instead.
2302 (insert string)
2303 (unless comint-inhibit-carriage-motion
2304 (comint-carriage-motion (process-mark proc) (point)))
2305 (set-marker (process-mark proc) (point))
2306 ;; (set (make-local-variable 'compilation-buffer-modtime)
2307 ;; (current-time))
2308 (run-hooks 'compilation-filter-hook))
2309 (goto-char pos)
2310 (narrow-to-region min max)
2311 (set-marker pos nil)
2312 (set-marker min nil)
2313 (set-marker max nil))))))
2315 ;;; test if a buffer is a compilation buffer, assuming we're in the buffer
2316 (defsubst compilation-buffer-internal-p ()
2317 "Test if inside a compilation buffer."
2318 (local-variable-p 'compilation-locs))
2320 ;;; test if a buffer is a compilation buffer, using compilation-buffer-internal-p
2321 (defsubst compilation-buffer-p (buffer)
2322 "Test if BUFFER is a compilation buffer."
2323 (with-current-buffer buffer
2324 (compilation-buffer-internal-p)))
2326 (defmacro compilation-loop (< property-change 1+ error limit)
2327 `(let (opt)
2328 (while (,< n 0)
2329 (setq opt pt)
2330 (or (setq pt (,property-change pt 'compilation-message))
2331 ;; Handle the case where the first error message is
2332 ;; at the start of the buffer, and n < 0.
2333 (if (or (eq (get-text-property ,limit 'compilation-message)
2334 (get-text-property opt 'compilation-message))
2335 (eq pt opt))
2336 (user-error ,error compilation-error)
2337 (setq pt ,limit)))
2338 ;; prop 'compilation-message usually has 2 changes, on and off, so
2339 ;; re-search if off
2340 (or (setq msg (get-text-property pt 'compilation-message))
2341 (if (setq pt (,property-change pt 'compilation-message nil ,limit))
2342 (setq msg (get-text-property pt 'compilation-message)))
2343 (user-error ,error compilation-error))
2344 (or (< (compilation--message->type msg) compilation-skip-threshold)
2345 (if different-file
2346 (eq (prog1 last
2347 (setq last (compilation--loc->file-struct
2348 (compilation--message->loc msg))))
2349 last))
2350 (if compilation-skip-visited
2351 (compilation--loc->visited (compilation--message->loc msg)))
2352 (if compilation-skip-to-next-location
2353 (eq (compilation--message->loc msg) loc))
2354 ;; count this message only if none of the above are true
2355 (setq n (,1+ n))))))
2357 (defun compilation-next-single-property-change (position prop
2358 &optional object limit)
2359 (let (parsed res)
2360 (while (progn
2361 ;; We parse the buffer here "on-demand" by chunks of 500 chars.
2362 ;; But we could also just parse the whole buffer.
2363 (compilation--ensure-parse
2364 (setq parsed (max compilation--parsed
2365 (min (+ position 500)
2366 (or limit (point-max))))))
2367 (and (or (not (setq res (next-single-property-change
2368 position prop object limit)))
2369 (eq res limit))
2370 (< position (or limit (point-max)))))
2371 (setq position parsed))
2372 res))
2374 (defun compilation-next-error (n &optional different-file pt)
2375 "Move point to the next error in the compilation buffer.
2376 This function does NOT find the source line like \\[next-error].
2377 Prefix arg N says how many error messages to move forwards (or
2378 backwards, if negative).
2379 Optional arg DIFFERENT-FILE, if non-nil, means find next error for a
2380 file that is different from the current one.
2381 Optional arg PT, if non-nil, specifies the value of point to start
2382 looking for the next message."
2383 (interactive "p")
2384 (or (compilation-buffer-p (current-buffer))
2385 (error "Not in a compilation buffer"))
2386 (or pt (setq pt (point)))
2387 (compilation--ensure-parse pt)
2388 (let* ((msg (get-text-property pt 'compilation-message))
2389 ;; `loc', `msg', and `last' are used by the compilation-loop macro.
2390 (loc (and msg (compilation--message->loc msg)))
2391 last)
2392 (if (zerop n)
2393 (unless (or msg ; find message near here
2394 (setq msg (get-text-property (max (1- pt) (point-min))
2395 'compilation-message)))
2396 (setq pt (previous-single-property-change pt 'compilation-message nil
2397 (line-beginning-position)))
2398 (unless (setq msg (get-text-property (max (1- pt) (point-min))
2399 'compilation-message))
2400 (setq pt (compilation-next-single-property-change
2401 pt 'compilation-message nil
2402 (line-end-position)))
2403 (or (setq msg (get-text-property pt 'compilation-message))
2404 (setq pt (point)))))
2405 (setq last (compilation--loc->file-struct loc))
2406 (if (>= n 0)
2407 (compilation-loop > compilation-next-single-property-change 1-
2408 (if (get-buffer-process (current-buffer))
2409 "No more %ss yet"
2410 "Moved past last %s")
2411 (point-max))
2412 ;; Don't move "back" to message at or before point.
2413 ;; Pass an explicit (point-min) to make sure pt is non-nil.
2414 (setq pt (previous-single-property-change
2415 pt 'compilation-message nil (point-min)))
2416 (compilation-loop < previous-single-property-change 1+
2417 "Moved back before first %s" (point-min))))
2418 (goto-char pt)
2419 (or msg
2420 (error "No %s here" compilation-error))))
2422 (defun compilation-previous-error (n)
2423 "Move point to the previous error in the compilation buffer.
2424 Prefix arg N says how many error messages to move backwards (or
2425 forwards, if negative).
2426 Does NOT find the source line like \\[previous-error]."
2427 (interactive "p")
2428 (compilation-next-error (- n)))
2430 (defun compilation-next-file (n)
2431 "Move point to the next error for a different file than the current one.
2432 Prefix arg N says how many files to move forwards (or backwards, if negative)."
2433 (interactive "p")
2434 (compilation-next-error n t))
2436 (defun compilation-previous-file (n)
2437 "Move point to the previous error for a different file than the current one.
2438 Prefix arg N says how many files to move backwards (or forwards, if negative)."
2439 (interactive "p")
2440 (compilation-next-file (- n)))
2442 (defun compilation-display-error ()
2443 "Display the source for current error in another window."
2444 (interactive)
2445 (setq compilation-current-error (point))
2446 (next-error-no-select 0))
2448 (defun kill-compilation ()
2449 "Kill the process made by the \\[compile] or \\[grep] commands."
2450 (interactive)
2451 (let ((buffer (compilation-find-buffer)))
2452 (if (get-buffer-process buffer)
2453 (interrupt-process (get-buffer-process buffer))
2454 (error "The %s process is not running" (downcase mode-name)))))
2456 (defalias 'compile-mouse-goto-error 'compile-goto-error)
2458 (defun compile-goto-error (&optional event)
2459 "Visit the source for the error message at point.
2460 Use this command in a compilation log buffer."
2461 (interactive (list last-input-event))
2462 (if event (posn-set-point (event-end event)))
2463 (or (compilation-buffer-p (current-buffer))
2464 (error "Not in a compilation buffer"))
2465 (compilation--ensure-parse (point))
2466 (if (get-text-property (point) 'compilation-directory)
2467 (dired-other-window
2468 (car (get-text-property (point) 'compilation-directory)))
2469 (setq compilation-current-error (point))
2470 (next-error-internal)))
2472 ;; This is mostly unused, but we keep it for the sake of some external
2473 ;; packages which seem to make use of it.
2474 (defun compilation-find-buffer (&optional avoid-current)
2475 "Return a compilation buffer.
2476 If AVOID-CURRENT is nil, and the current buffer is a compilation buffer,
2477 return it. If AVOID-CURRENT is non-nil, return the current buffer only
2478 as a last resort."
2479 (if (and (compilation-buffer-internal-p) (not avoid-current))
2480 (current-buffer)
2481 (next-error-find-buffer avoid-current 'compilation-buffer-internal-p)))
2483 ;;;###autoload
2484 (defun compilation-next-error-function (n &optional reset)
2485 "Advance to the next error message and visit the file where the error was.
2486 This is the value of `next-error-function' in Compilation buffers."
2487 (interactive "p")
2488 (when reset
2489 (setq compilation-current-error nil))
2490 (let* ((screen-columns compilation-error-screen-columns)
2491 (first-column compilation-first-column)
2492 (last 1)
2493 (msg (compilation-next-error (or n 1) nil
2494 (or compilation-current-error
2495 compilation-messages-start
2496 (point-min))))
2497 (loc (compilation--message->loc msg))
2498 (end-loc (compilation--message->end-loc msg))
2499 (marker (point-marker)))
2500 (setq compilation-current-error (point-marker)
2501 overlay-arrow-position
2502 (if (bolp)
2503 compilation-current-error
2504 (copy-marker (line-beginning-position))))
2505 ;; If loc contains no marker, no error in that file has been visited.
2506 ;; If the marker is invalid the buffer has been killed.
2507 ;; So, recalculate all markers for that file.
2508 (unless (and (compilation--loc->marker loc)
2509 (marker-buffer (compilation--loc->marker loc))
2510 ;; FIXME-omake: For "omake -P", which automatically recompiles
2511 ;; when the file is modified, the line numbers of new output
2512 ;; may not be related to line numbers from earlier output
2513 ;; (earlier markers), so we used to try to detect it here and
2514 ;; force a reparse. But that caused more problems elsewhere,
2515 ;; so instead we now flush the file-structure when we see
2516 ;; omake's message telling it's about to recompile a file.
2517 ;; (or (null (compilation--loc->timestamp loc)) ;A fake-loc
2518 ;; (equal (compilation--loc->timestamp loc)
2519 ;; (setq timestamp compilation-buffer-modtime)))
2521 (with-current-buffer
2522 (apply #'compilation-find-file
2523 marker
2524 (caar (compilation--loc->file-struct loc))
2525 (cadr (car (compilation--loc->file-struct loc)))
2526 (compilation--file-struct->formats
2527 (compilation--loc->file-struct loc)))
2528 (let ((screen-columns
2529 ;; Obey the compilation-error-screen-columns of the target
2530 ;; buffer if its major mode set it buffer-locally.
2531 (if (local-variable-p 'compilation-error-screen-columns)
2532 compilation-error-screen-columns screen-columns))
2533 (compilation-first-column
2534 (if (local-variable-p 'compilation-first-column)
2535 compilation-first-column first-column)))
2536 (save-restriction
2537 (widen)
2538 (goto-char (point-min))
2539 ;; Treat file's found lines in forward order, 1 by 1.
2540 (dolist (line (reverse (cddr (compilation--loc->file-struct loc))))
2541 (when (car line) ; else this is a filename w/o a line#
2542 (compilation-beginning-of-line (- (car line) last -1))
2543 (setq last (car line)))
2544 ;; Treat line's found columns and store/update a marker for each.
2545 (dolist (col (cdr line))
2546 (if (compilation--loc->col col)
2547 (if (eq (compilation--loc->col col) -1)
2548 ;; Special case for range end.
2549 (end-of-line)
2550 (compilation-move-to-column (compilation--loc->col col)
2551 screen-columns))
2552 (beginning-of-line)
2553 (skip-chars-forward " \t"))
2554 (if (compilation--loc->marker col)
2555 (set-marker (compilation--loc->marker col) (point))
2556 (setf (compilation--loc->marker col) (point-marker)))
2557 ;; (setf (compilation--loc->timestamp col) timestamp)
2558 ))))))
2559 (compilation-goto-locus marker (compilation--loc->marker loc)
2560 (compilation--loc->marker end-loc))
2561 (setf (compilation--loc->visited loc) t)))
2563 (defvar compilation-gcpro nil
2564 "Internal variable used to keep some values from being GC'd.")
2565 (make-variable-buffer-local 'compilation-gcpro)
2567 (defun compilation-fake-loc (marker file &optional line col)
2568 "Preassociate MARKER with FILE.
2569 FILE should be ABSOLUTE-FILENAME or (RELATIVE-FILENAME . DIRNAME).
2570 This is useful when you compile temporary files, but want
2571 automatic translation of the messages to the real buffer from
2572 which the temporary file came. This may also affect previous messages
2573 about FILE.
2575 Optional args LINE and COL default to 1 and beginning of
2576 indentation respectively. The marker is expected to reflect
2577 this. In the simplest case the marker points to the first line
2578 of the region that was saved to the temp file.
2580 If you concatenate several regions into the temp file (e.g. a
2581 header with variable assignments and a code region), you must
2582 call this several times, once each for the last line of one
2583 region and the first line of the next region."
2584 (or (consp file) (setq file (list file)))
2585 (compilation--flush-file-structure file)
2586 (let ((fs (compilation-get-file-structure file)))
2587 ;; Between the current call to compilation-fake-loc and the first
2588 ;; occurrence of an error message referring to `file', the data is
2589 ;; only kept in the weak hash-table compilation-locs, so we need
2590 ;; to prevent this entry in compilation-locs from being GC'd
2591 ;; away. --Stef
2592 (push fs compilation-gcpro)
2593 (let ((loc (compilation-assq (or line 1) (cdr fs))))
2594 (setq loc (compilation-assq col loc))
2595 (cl-assert (null (cdr loc)))
2596 (setcdr loc (compilation--make-cdrloc line fs marker))
2597 loc)))
2599 (defcustom compilation-context-lines nil
2600 "Display this many lines of leading context before the current message.
2601 If nil and the left fringe is displayed, don't scroll the
2602 compilation output window; an arrow in the left fringe points to
2603 the current message. If nil and there is no left fringe, the message
2604 displays at the top of the window; there is no arrow."
2605 :type '(choice integer (const :tag "No window scrolling" nil))
2606 :group 'compilation
2607 :version "22.1")
2609 (defsubst compilation-set-window (w mk)
2610 "Align the compilation output window W with marker MK near top."
2611 (if (integerp compilation-context-lines)
2612 (set-window-start w (save-excursion
2613 (goto-char mk)
2614 (compilation-beginning-of-line
2615 (- 1 compilation-context-lines))
2616 (point)))
2617 ;; If there is no left fringe.
2618 (when (equal (car (window-fringes w)) 0)
2619 (set-window-start w (save-excursion
2620 (goto-char mk)
2621 (beginning-of-line 1)
2622 (point)))))
2623 (set-window-point w mk))
2625 (defvar next-error-highlight-timer)
2627 (defun compilation-goto-locus (msg mk end-mk)
2628 "Jump to an error corresponding to MSG at MK.
2629 All arguments are markers. If END-MK is non-nil, mark is set there
2630 and overlay is highlighted between MK and END-MK."
2631 ;; Show compilation buffer in other window, scrolled to this error.
2632 (let* ((from-compilation-buffer (eq (window-buffer)
2633 (marker-buffer msg)))
2634 ;; Use an existing window if it is in a visible frame.
2635 (pre-existing (get-buffer-window (marker-buffer msg) 0))
2636 (w (if (and from-compilation-buffer pre-existing)
2637 ;; Calling display-buffer here may end up (partly) hiding
2638 ;; the error location if the two buffers are in two
2639 ;; different frames. So don't do it if it's not necessary.
2640 pre-existing
2641 (display-buffer (marker-buffer msg) '(nil (allow-no-window . t)))))
2642 (highlight-regexp (with-current-buffer (marker-buffer msg)
2643 ;; also do this while we change buffer
2644 (goto-char (marker-position msg))
2645 (and w (compilation-set-window w msg))
2646 compilation-highlight-regexp)))
2647 ;; Ideally, the window-size should be passed to `display-buffer'
2648 ;; so it's only used when creating a new window.
2649 (when (and (not pre-existing) w)
2650 (compilation-set-window-height w))
2652 (if from-compilation-buffer
2653 ;; If the compilation buffer window was selected,
2654 ;; keep the compilation buffer in this window;
2655 ;; display the source in another window.
2656 (let ((pop-up-windows t))
2657 (pop-to-buffer (marker-buffer mk) 'other-window))
2658 (switch-to-buffer (marker-buffer mk)))
2659 (unless (eq (goto-char mk) (point))
2660 ;; If narrowing gets in the way of going to the right place, widen.
2661 (widen)
2662 (if next-error-move-function
2663 (funcall next-error-move-function msg mk)
2664 (goto-char mk)))
2665 (if end-mk
2666 (push-mark end-mk t)
2667 (if mark-active (setq mark-active nil)))
2668 ;; If hideshow got in the way of
2669 ;; seeing the right place, open permanently.
2670 (dolist (ov (overlays-at (point)))
2671 (when (eq 'hs (overlay-get ov 'invisible))
2672 (delete-overlay ov)
2673 (goto-char mk)))
2675 (when highlight-regexp
2676 (if (timerp next-error-highlight-timer)
2677 (cancel-timer next-error-highlight-timer))
2678 (unless compilation-highlight-overlay
2679 (setq compilation-highlight-overlay
2680 (make-overlay (point-min) (point-min)))
2681 (overlay-put compilation-highlight-overlay 'face 'next-error))
2682 (with-current-buffer (marker-buffer mk)
2683 (save-excursion
2684 (if end-mk (goto-char end-mk) (end-of-line))
2685 (let ((end (point)))
2686 (if mk (goto-char mk) (beginning-of-line))
2687 (if (and (stringp highlight-regexp)
2688 (re-search-forward highlight-regexp end t))
2689 (progn
2690 (goto-char (match-beginning 0))
2691 (move-overlay compilation-highlight-overlay
2692 (match-beginning 0) (match-end 0)
2693 (current-buffer)))
2694 (move-overlay compilation-highlight-overlay
2695 (point) end (current-buffer)))
2696 (if (or (eq next-error-highlight t)
2697 (numberp next-error-highlight))
2698 ;; We want highlighting: delete overlay on next input.
2699 (add-hook 'pre-command-hook
2700 'compilation-goto-locus-delete-o)
2701 ;; We don't want highlighting: delete overlay now.
2702 (delete-overlay compilation-highlight-overlay))
2703 ;; We want highlighting for a limited time:
2704 ;; set up a timer to delete it.
2705 (when (numberp next-error-highlight)
2706 (setq next-error-highlight-timer
2707 (run-at-time next-error-highlight nil
2708 'compilation-goto-locus-delete-o)))))))
2709 (when (and (eq next-error-highlight 'fringe-arrow))
2710 ;; We want a fringe arrow (instead of highlighting).
2711 (setq next-error-overlay-arrow-position
2712 (copy-marker (line-beginning-position))))))
2714 (defun compilation-goto-locus-delete-o ()
2715 (delete-overlay compilation-highlight-overlay)
2716 ;; Get rid of timer and hook that would try to do this again.
2717 (if (timerp next-error-highlight-timer)
2718 (cancel-timer next-error-highlight-timer))
2719 (remove-hook 'pre-command-hook
2720 'compilation-goto-locus-delete-o))
2722 (defun compilation-find-file (marker filename directory &rest formats)
2723 "Find a buffer for file FILENAME.
2724 If FILENAME is not found at all, ask the user where to find it.
2725 Pop up the buffer containing MARKER and scroll to MARKER if we ask
2726 the user where to find the file.
2727 Search the directories in `compilation-search-path'.
2728 A nil in `compilation-search-path' means to try the
2729 \"current\" directory, which is passed in DIRECTORY.
2730 If DIRECTORY is relative, it is combined with `default-directory'.
2731 If DIRECTORY is nil, that means use `default-directory'.
2732 FORMATS, if given, is a list of formats to reformat FILENAME when
2733 looking for it: for each element FMT in FORMATS, this function
2734 attempts to find a file whose name is produced by (format FMT FILENAME)."
2735 (or formats (setq formats '("%s")))
2736 (let ((dirs compilation-search-path)
2737 (spec-dir (if directory
2738 (expand-file-name directory)
2739 default-directory))
2740 buffer thisdir fmts name)
2741 (if (file-name-absolute-p filename)
2742 ;; The file name is absolute. Use its explicit directory as
2743 ;; the first in the search path, and strip it from FILENAME.
2744 (setq filename (abbreviate-file-name (expand-file-name filename))
2745 dirs (cons (file-name-directory filename) dirs)
2746 filename (file-name-nondirectory filename)))
2747 ;; Now search the path.
2748 (while (and dirs (null buffer))
2749 (setq thisdir (or (car dirs) spec-dir)
2750 fmts formats)
2751 ;; For each directory, try each format string.
2752 (while (and fmts (null buffer))
2753 (setq name (expand-file-name (format (car fmts) filename) thisdir)
2754 buffer (and (file-exists-p name)
2755 (find-file-noselect name))
2756 fmts (cdr fmts)))
2757 (setq dirs (cdr dirs)))
2758 (while (null buffer) ;Repeat until the user selects an existing file.
2759 ;; The file doesn't exist. Ask the user where to find it.
2760 (save-excursion ;This save-excursion is probably not right.
2761 (let ((w (let ((pop-up-windows t))
2762 (display-buffer (marker-buffer marker)
2763 '(nil (allow-no-window . t))))))
2764 (with-current-buffer (marker-buffer marker)
2765 (goto-char marker)
2766 (and w (compilation-set-window w marker)))
2767 (let* ((name (read-file-name
2768 (format "Find this %s in (default %s): "
2769 compilation-error filename)
2770 spec-dir filename t nil
2771 ;; The predicate below is fine when called from
2772 ;; minibuffer-complete-and-exit, but it's too
2773 ;; restrictive otherwise, since it also prevents the
2774 ;; user from completing "fo" to "foo/" when she
2775 ;; wants to enter "foo/bar".
2777 ;; Try to make sure the user can only select
2778 ;; a valid answer. This predicate may be ignored,
2779 ;; tho, so we still have to double-check afterwards.
2780 ;; TODO: We should probably fix read-file-name so
2781 ;; that it never ignores this predicate, even when
2782 ;; using popup dialog boxes.
2783 ;; (lambda (name)
2784 ;; (if (file-directory-p name)
2785 ;; (setq name (expand-file-name filename name)))
2786 ;; (file-exists-p name))
2788 (origname name))
2789 (cond
2790 ((not (file-exists-p name))
2791 (message "Cannot find file `%s'" name)
2792 (ding) (sit-for 2))
2793 ((and (file-directory-p name)
2794 (not (file-exists-p
2795 (setq name (expand-file-name filename name)))))
2796 (message "No `%s' in directory %s" filename origname)
2797 (ding) (sit-for 2))
2799 (setq buffer (find-file-noselect name))))))))
2800 ;; Make intangible overlays tangible.
2801 ;; This is weird: it's not even clear which is the current buffer,
2802 ;; so the code below can't be expected to DTRT here. -- Stef
2803 (dolist (ov (overlays-in (point-min) (point-max)))
2804 (when (overlay-get ov 'intangible)
2805 (overlay-put ov 'intangible nil)))
2806 buffer))
2808 (defun compilation-get-file-structure (file &optional fmt)
2809 "Retrieve FILE's file-structure or create a new one.
2810 FILE should be (FILENAME) or (RELATIVE-FILENAME . DIRNAME).
2811 In the former case, FILENAME may be relative or absolute.
2813 The file-structure looks like this:
2814 ((FILENAME [TRUE-DIRNAME]) FMT ...)
2816 TRUE-DIRNAME is the `file-truename' of DIRNAME, if given."
2817 (or (gethash file compilation-locs)
2818 ;; File was not previously encountered, at least not in the form passed.
2819 ;; Let's normalize it and look again.
2820 (let ((filename (car file))
2821 ;; Get the specified directory from FILE.
2822 (spec-directory (if (cdr file)
2823 (file-truename (cdr file)))))
2825 ;; Check for a comint-file-name-prefix and prepend it if appropriate.
2826 ;; (This is very useful for compilation-minor-mode in an rlogin-mode
2827 ;; buffer.)
2828 (when (and (boundp 'comint-file-name-prefix)
2829 (not (equal comint-file-name-prefix "")))
2830 (if (file-name-absolute-p filename)
2831 (setq filename
2832 (concat comint-file-name-prefix filename))
2833 (if spec-directory
2834 (setq spec-directory
2835 (file-truename
2836 (concat comint-file-name-prefix spec-directory))))))
2838 ;; If compilation-parse-errors-filename-function is
2839 ;; defined, use it to process the filename.
2840 (when compilation-parse-errors-filename-function
2841 (setq filename
2842 (funcall compilation-parse-errors-filename-function
2843 filename)))
2845 ;; Some compilers (e.g. Sun's java compiler, reportedly) produce bogus
2846 ;; file names like "./bar//foo.c" for file "bar/foo.c";
2847 ;; expand-file-name will collapse these into "/foo.c" and fail to find
2848 ;; the appropriate file. So we look for doubled slashes in the file
2849 ;; name and fix them.
2850 (setq filename (command-line-normalize-file-name filename))
2852 ;; Store it for the possibly unnormalized name
2853 (puthash file
2854 ;; Retrieve or create file-structure for normalized name
2855 ;; The gethash used to not use spec-directory, but
2856 ;; this leads to errors when files in different
2857 ;; directories have the same name:
2858 ;; https://lists.gnu.org/r/emacs-devel/2007-08/msg00463.html
2859 (or (gethash (cons filename spec-directory) compilation-locs)
2860 (puthash (cons filename spec-directory)
2861 (compilation--make-file-struct
2862 (list filename spec-directory) fmt)
2863 compilation-locs))
2864 compilation-locs))))
2866 (defun compilation--flush-file-structure (file)
2867 (or (consp file) (setq file (list file)))
2868 (let ((fs (compilation-get-file-structure file)))
2869 (cl-assert (eq fs (gethash file compilation-locs)))
2870 (cl-assert (eq fs (gethash (cons (caar fs) (cadr (car fs)))
2871 compilation-locs)))
2872 (maphash (lambda (k v)
2873 (if (eq v fs) (remhash k compilation-locs)))
2874 compilation-locs)))
2876 ;;; Compatibility with the old compile.el.
2878 (defvaralias 'compilation-last-buffer 'next-error-last-buffer)
2879 (defvar compilation-parsing-end (make-marker))
2880 (defvar compilation-error-list nil)
2881 (defvar compilation-old-error-list nil)
2883 (defun compilation--compat-error-properties (err)
2884 "Map old-style error ERR to new-style message."
2885 ;; Old-style structure is (MARKER (FILE DIR) LINE COL) or
2886 ;; (MARKER . MARKER).
2887 (let ((dst (cdr err)))
2888 (if (markerp dst)
2889 `(compilation-message ,(compilation--make-message
2890 (cons nil (compilation--make-cdrloc
2891 nil nil dst))
2892 2 nil)
2893 help-echo "mouse-2: visit the source location"
2894 keymap compilation-button-map
2895 mouse-face highlight)
2896 ;; Too difficult to do it by hand: dispatch to the normal code.
2897 (let* ((file (pop dst))
2898 (line (pop dst))
2899 (col (pop dst))
2900 (filename (pop file))
2901 (dirname (pop file))
2902 (fmt (pop file)))
2903 (compilation-internal-error-properties
2904 (cons filename dirname) line nil col nil 2 fmt)))))
2906 (defun compilation--compat-parse-errors (limit)
2907 (when compilation-parse-errors-function
2908 ;; FIXME: We should remove the rest of the compilation keywords
2909 ;; but we can't do that from here because font-lock is using
2910 ;; the value right now. --Stef
2911 (save-excursion
2912 (setq compilation-error-list nil)
2913 ;; Reset compilation-parsing-end each time because font-lock
2914 ;; might force us the re-parse many times (typically because
2915 ;; some code adds some text-property to the output that we
2916 ;; already parsed). You might say "why reparse", well:
2917 ;; because font-lock has just removed the `compilation-message' property
2918 ;; so have to do it all over again.
2919 (if compilation-parsing-end
2920 (set-marker compilation-parsing-end (point))
2921 (setq compilation-parsing-end (point-marker)))
2922 (condition-case nil
2923 ;; Ignore any error: we're calling this function earlier than
2924 ;; in the old compile.el so things might not all be setup yet.
2925 (funcall compilation-parse-errors-function limit nil)
2926 (error nil))
2927 (dolist (err (if (listp compilation-error-list) compilation-error-list))
2928 (let* ((src (car err))
2929 (dst (cdr err))
2930 (loc (cond ((markerp dst)
2931 (cons nil
2932 (compilation--make-cdrloc nil nil dst)))
2933 ((consp dst)
2934 (cons (nth 2 dst)
2935 (compilation--make-cdrloc
2936 (nth 1 dst)
2937 (cons (cdar dst) (caar dst))
2938 nil))))))
2939 (when loc
2940 (goto-char src)
2941 ;; (put-text-property src (line-end-position)
2942 ;; 'font-lock-face 'font-lock-warning-face)
2943 (put-text-property src (line-end-position)
2944 'compilation-message
2945 (compilation--make-message loc 2 nil)))))))
2946 (goto-char limit)
2947 nil)
2949 ;; Beware! this is not only compatibility code. New code also uses it. --Stef
2950 (defun compilation-forget-errors ()
2951 ;; In case we hit the same file/line specs, we want to recompute a new
2952 ;; marker for them, so flush our cache.
2953 (clrhash compilation-locs)
2954 (setq compilation-gcpro nil)
2955 ;; FIXME: the old code reset the directory-stack, so maybe we should
2956 ;; put a `directory change' marker of some sort, but where? -stef
2958 ;; FIXME: The old code moved compilation-current-error (which was
2959 ;; virtually represented by a mix of compilation-parsing-end and
2960 ;; compilation-error-list) to point-min, but that was only meaningful for
2961 ;; the internal uses of compilation-forget-errors: all calls from external
2962 ;; packages seem to be followed by a move of compilation-parsing-end to
2963 ;; something equivalent to point-max. So we heuristically move
2964 ;; compilation-current-error to point-max (since the external package
2965 ;; won't know that it should do it). --Stef
2966 (setq compilation-current-error nil)
2967 (let* ((proc (get-buffer-process (current-buffer)))
2968 (mark (if proc (process-mark proc)))
2969 (pos (or mark (point-max))))
2970 (setq compilation-messages-start
2971 ;; In the future, ignore the text already present in the buffer.
2972 ;; Since many process filter functions insert before markers,
2973 ;; we need to put ours just before the insertion point rather
2974 ;; than at the insertion point. If that's not possible, then
2975 ;; don't use a marker. --Stef
2976 (if (> pos (point-min)) (copy-marker (1- pos)) pos)))
2977 ;; Again, since this command is used in buffers that contain several
2978 ;; compilations, to set the beginning of "this compilation", it's a good
2979 ;; place to reset compilation-auto-jump-to-next.
2980 (set (make-local-variable 'compilation-auto-jump-to-next)
2981 (or compilation-auto-jump-to-first-error
2982 (eq compilation-scroll-output 'first-error))))
2984 (provide 'compile)
2986 ;;; compile.el ends here