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