*** empty log message ***
[bpt/emacs.git] / lisp / progmodes / compile.el
CommitLineData
55535639 1;;; compile.el --- run compiler as inferior of Emacs, parse error messages
fad160d5 2
930a3e46 3;; Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
049dcb6f
GM
4;; 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5;; Free Software Foundation, Inc.
3a801d0c 6
7837c247
SM
7;; Authors: Roland McGrath <roland@gnu.org>,
8;; Daniel Pfeiffer <occitan@esperanto.org>
d1c7011d 9;; Maintainer: FSF
e9571d2a 10;; Keywords: tools, processes
d1c7011d 11
55dfd2c4
RS
12;; This file is part of GNU Emacs.
13
29add8b9
RM
14;; GNU Emacs is free software; you can redistribute it and/or modify
15;; it under the terms of the GNU General Public License as published by
1a484753 16;; the Free Software Foundation; either version 3, or (at your option)
29add8b9
RM
17;; any later version.
18
55dfd2c4 19;; GNU Emacs is distributed in the hope that it will be useful,
29add8b9
RM
20;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22;; GNU General Public License for more details.
23
24;; You should have received a copy of the GNU General Public License
b578f267 25;; along with GNU Emacs; see the file COPYING. If not, write to the
3a35cf56
LK
26;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27;; Boston, MA 02110-1301, USA.
55dfd2c4 28
5cc57841
ER
29;;; Commentary:
30
7837c247
SM
31;; This package provides the compile facilities documented in the Emacs user's
32;; manual.
5cc57841 33
c536bb39 34;; This mode uses some complex data-structures:
7837c247 35
c536bb39 36;; LOC (or location) is a list of (COLUMN LINE FILE-STRUCTURE)
7837c247
SM
37
38;; COLUMN and LINE are numbers parsed from an error message. COLUMN and maybe
39;; LINE will be nil for a message that doesn't contain them. Then the
40;; location refers to a indented beginning of line or beginning of file.
41;; Once any location in some file has been jumped to, the list is extended to
b84acff6
SS
42;; (COLUMN LINE FILE-STRUCTURE MARKER TIMESTAMP . VISITED)
43;; for all LOCs pertaining to that file.
7837c247
SM
44;; MARKER initially points to LINE and COLUMN in a buffer visiting that file.
45;; Being a marker it sticks to some text, when the buffer grows or shrinks
46;; before that point. VISITED is t if we have jumped there, else nil.
b84acff6
SS
47;; TIMESTAMP is necessary because of "incremental compilation": `omake -P'
48;; polls filesystem for changes and recompiles when a file is modified
49;; using the same *compilation* buffer. this necessitates re-parsing markers.
7837c247 50
c536bb39
SM
51;; FILE-STRUCTURE is a list of
52;; ((FILENAME . DIRECTORY) FORMATS (LINE LOC ...) ...)
7837c247
SM
53
54;; FILENAME is a string parsed from an error message. DIRECTORY is a string
55;; obtained by following directory change messages. DIRECTORY will be nil for
56;; an absolute filename. FORMATS is a list of formats to apply to FILENAME if
57;; a file of that name can't be found.
58;; The rest of the list is an alist of elements with LINE as key. The keys
59;; are either nil or line numbers. If present, nil comes first, followed by
60;; the numbers in decreasing order. The LOCs for each line are again an alist
61;; ordered the same way. Note that the whole file structure is referenced in
62;; every LOC.
63
c536bb39 64;; MESSAGE is a list of (LOC TYPE END-LOC)
7837c247
SM
65
66;; TYPE is 0 for info or 1 for warning if the message matcher identified it as
67;; such, 2 otherwise (for a real error). END-LOC is a LOC pointing to the
4fffd73b 68;; other end, if the parsed message contained a range. If the end of the
7837c247
SM
69;; range didn't specify a COLUMN, it defaults to -1, meaning end of line.
70;; These are the value of the `message' text-properties in the compilation
71;; buffer.
72
24299582
DP
73;;; Code:
74
583a15bb
SM
75(eval-when-compile (require 'cl))
76
e623859c
JB
77(defvar font-lock-extra-managed-props)
78(defvar font-lock-keywords)
79(defvar font-lock-maximum-size)
80(defvar font-lock-support-mode)
81
82
c5049fa0
RS
83(defgroup compilation nil
84 "Run compiler as inferior of Emacs, parse error messages."
85 :group 'tools
86 :group 'processes)
87
88
7c163413 89;;;###autoload
c5049fa0 90(defcustom compilation-mode-hook nil
813fb3fe 91 "List of hook functions run by `compilation-mode' (see `run-mode-hooks')."
c5049fa0
RS
92 :type 'hook
93 :group 'compilation)
7c163413
RM
94
95;;;###autoload
c5049fa0 96(defcustom compilation-window-height nil
813fb3fe 97 "Number of lines in a compilation window. If nil, use Emacs default."
c5049fa0
RS
98 :type '(choice (const :tag "Default" nil)
99 integer)
100 :group 'compilation)
d3cb357b 101
7837c247
SM
102(defvar compilation-first-column 1
103 "*This is how compilers number the first column, usually 1 or 0.")
55dfd2c4 104
e335e194
GM
105(defvar compilation-parse-errors-filename-function nil
106 "Function to call to post-process filenames while parsing error messages.
107It takes one arg FILENAME which is the name of a file as found
108in the compilation output, and should return a transformed file name.")
109
49683a13
EZ
110;;;###autoload
111(defvar compilation-process-setup-function nil
112 "*Function to call to customize the compilation process.
6f5b7627 113This function is called immediately before the compilation process is
49683a13 114started. It can be used to set any variables or functions that are used
9ac57479
KS
115while processing the output of the compilation process. The function
116is called with variables `compilation-buffer' and `compilation-window'
7837c247 117bound to the compilation buffer and window, respectively.")
49683a13 118
aa228418 119;;;###autoload
d3cb357b 120(defvar compilation-buffer-name-function nil
6c43f2f9
RS
121 "Function to compute the name of a compilation buffer.
122The function receives one argument, the name of the major mode of the
123compilation buffer. It should return a string.
f441be5b 124If nil, compute the name with `(concat \"*\" (downcase major-mode) \"*\")'.")
55dfd2c4 125
aa228418 126;;;###autoload
d3cb357b 127(defvar compilation-finish-function nil
c5049fa0 128 "Function to call when a compilation process finishes.
d3cb357b 129It is called with two arguments: the compilation buffer, and a string
620d3304 130describing how the process finished.")
3c9e7f42
RS
131
132(make-obsolete-variable 'compilation-finish-function
f441be5b 133 "use `compilation-finish-functions', but it works a little differently."
3c9e7f42 134 "22.1")
55dfd2c4 135
4cc36b17
RS
136;;;###autoload
137(defvar compilation-finish-functions nil
c5049fa0 138 "Functions to call when a compilation process finishes.
4cc36b17
RS
139Each function is called with two arguments: the compilation buffer,
140and a string describing how the process finished.")
141
ebff767c
RM
142(defvar compilation-in-progress nil
143 "List of compilation processes now running.")
144(or (assq 'compilation-in-progress minor-mode-alist)
145 (setq minor-mode-alist (cons '(compilation-in-progress " Compiling")
146 minor-mode-alist)))
147
7837c247
SM
148(defvar compilation-error "error"
149 "Stem of message to print when no matches are found.")
6c43f2f9 150
58856335 151(defvar compilation-arguments nil
7837c247 152 "Arguments that were given to `compilation-start'.")
58856335 153
6c43f2f9 154(defvar compilation-num-errors-found)
d3cb357b 155
7837c247
SM
156(defconst compilation-error-regexp-alist-alist
157 '((absoft
158 "^\\(?:[Ee]rror on \\|[Ww]arning on\\( \\)\\)?[Ll]ine[ \t]+\\([0-9]+\\)[ \t]+\
159of[ \t]+\"?\\([a-zA-Z]?:?[^\":\n]+\\)\"?:" 3 2 nil (1))
5dfa3d35 160
7837c247
SM
161 (ada
162 "\\(warning: .*\\)? at \\([^ \n]+\\):\\([0-9]+\\)$" 2 3 nil (1))
c7c5bbc0 163
7837c247
SM
164 (aix
165 " in line \\([0-9]+\\) of file \\([^ \n]+[^. \n]\\)\\.? " 2 1)
e1f540f2 166
7837c247
SM
167 (ant
168 "^[ \t]*\\[[^] \n]+\\][ \t]*\\([^: \n]+\\):\\([0-9]+\\):\\(?:\\([0-9]+\\):[0-9]+:[0-9]+:\\)?\
169\\( warning\\)?" 1 2 3 (4))
e1f540f2 170
bffa7817
SM
171 (maven
172 ;; Maven is a popular build tool for Java. Maven is Free Software.
173 "\\(.*?\\):\\[\\([0-9]+\\),\\([0-9]+\\)\\]" 1 2 3)
c94d5f81 174
7837c247
SM
175 (bash
176 "^\\([^: \n\t]+\\): line \\([0-9]+\\):" 1 2)
e1f540f2 177
7837c247
SM
178 (borland
179 "^\\(?:Error\\|Warnin\\(g\\)\\) \\(?:[FEW][0-9]+ \\)?\
180\\([a-zA-Z]?:?[^:( \t\n]+\\)\
181 \\([0-9]+\\)\\(?:[) \t]\\|:[^0-9\n]\\)" 2 3 nil (1))
182
183 (caml
f6164cdd
DP
184 "^ *File \\(\"?\\)\\([^,\" \n\t<>]+\\)\\1, lines? \\([0-9]+\\)-?\\([0-9]+\\)?\\(?:$\\|,\
185\\(?: characters? \\([0-9]+\\)-?\\([0-9]+\\)?:\\)?\\([ \n]Warning:\\)?\\)"
186 2 (3 . 4) (5 . 6) (7))
7837c247
SM
187
188 (comma
189 "^\"\\([^,\" \n\t]+\\)\", line \\([0-9]+\\)\
190\\(?:[(. pos]+\\([0-9]+\\))?\\)?[:.,; (-]\\( warning:\\|[-0-9 ]*(W)\\)?" 1 2 3 (4))
191
c39bf546
DP
192 (edg-1
193 "^\\([^ \n]+\\)(\\([0-9]+\\)): \\(?:error\\|warnin\\(g\\)\\|remar\\(k\\)\\)"
194 1 2 nil (3 . 4))
195 (edg-2
196 "at line \\([0-9]+\\) of \"\\([^ \n]+\\)\"$"
197 2 1 nil 0)
198
7837c247 199 (epc
c39bf546 200 "^Error [0-9]+ at (\\([0-9]+\\):\\([^)\n]+\\))" 2 1)
7837c247 201
12221c84
DP
202 (ftnchek
203 "\\(^Warning .*\\)? line[ \n]\\([0-9]+\\)[ \n]\\(?:col \\([0-9]+\\)[ \n]\\)?file \\([^ :;\n]+\\)"
204 4 2 3 (1))
9fcabe79 205
7837c247
SM
206 (iar
207 "^\"\\(.*\\)\",\\([0-9]+\\)\\s-+\\(?:Error\\|Warnin\\(g\\)\\)\\[[0-9]+\\]:"
208 1 2 nil (3))
209
210 (ibm
211 "^\\([^( \n\t]+\\)(\\([0-9]+\\):\\([0-9]+\\)) :\
212 \\(?:warnin\\(g\\)\\|informationa\\(l\\)\\)?" 1 2 3 (4 . 5))
213
6f5b7627 214 ;; fixme: should be `mips'
7837c247 215 (irix
9fcabe79
DP
216 "^[-[:alnum:]_/ ]+: \\(?:\\(?:[sS]evere\\|[eE]rror\\|[wW]arnin\\(g\\)\\|[iI]nf\\(o\\)\\)[0-9 ]*: \\)?\
217\\([^,\" \n\t]+\\)\\(?:, line\\|:\\) \\([0-9]+\\):" 3 4 nil (1 . 2))
7837c247
SM
218
219 (java
220 "^\\(?:[ \t]+at \\|==[0-9]+== +\\(?:at\\|b\\(y\\)\\)\\).+(\\([^()\n]+\\):\\([0-9]+\\))$" 2 3 nil (1))
221
222 (jikes-file
223 "^\\(?:Found\\|Issued\\) .* compiling \"\\(.+\\)\":$" 1 nil nil 0)
224 (jikes-line
225 "^ *\\([0-9]+\\)\\.[ \t]+.*\n +\\(<-*>\n\\*\\*\\* \\(?:Error\\|Warnin\\(g\\)\\)\\)"
226 nil 1 nil 2 0
227 (2 (compilation-face '(3))))
228
7837c247 229 (gnu
b8fa0ffd
SM
230 ;; I have no idea what this first line is supposed to match, but it
231 ;; makes things ambiguous with output such as "foo:344:50:blabla" since
232 ;; the "foo" part can match this first line (in which case the file
0ab31e4a
SM
233 ;; name as "344"). To avoid this, the second line disallows filenames
234 ;; exclusively composed of digits. --Stef
235 ;; Similarly, we get lots of false positives with messages including
236 ;; times of the form "HH:MM:SS" where MM is taken as a line number, so
237 ;; the last line tries to rule out message where the info after the
238 ;; line number starts with "SS". --Stef
6f5b7627 239 "^\\(?:[[:alpha:]][-[:alnum:].]+: ?\\)?\
d25593b7 240\\([0-9]*[^0-9\n]\\(?:[^\n ]\\| [^-\n]\\)*?\\): ?\
8decc87f
SM
241\\([0-9]+\\)\\(?:\\([.:]\\)\\([0-9]+\\)\\)?\
242\\(?:-\\([0-9]+\\)?\\(?:\\3\\([0-9]+\\)\\)?\\)?:\
7837c247 243\\(?: *\\(\\(?:Future\\|Runtime\\)?[Ww]arning\\|W:\\)\\|\
d40a86f9 244 *\\([Ii]nfo\\(?:\\>\\|rmationa?l?\\)\\|I:\\|instantiated from\\|[Nn]ote\\)\\|\
0ab31e4a 245\[0-9]?\\(?:[^0-9\n]\\|$\\)\\|[0-9][0-9][0-9]\\)"
7837c247
SM
246 1 (2 . 5) (4 . 6) (7 . 8))
247
6e1a9597
CY
248 ;; The `gnu' style above can incorrectly match gcc's "In file
249 ;; included from" message, so we process that first. -- cyd
250 (gcc-include
251 "^\\(?:In file included\\| \\) from \
252\\(.+\\):\\([0-9]+\\)\\(?:\\(:\\)\\|\\(,\\)\\)?" 1 2 nil (3 . 4))
253
7837c247
SM
254 (lcc
255 "^\\(?:E\\|\\(W\\)\\), \\([^(\n]+\\)(\\([0-9]+\\),[ \t]*\\([0-9]+\\)"
256 2 3 4 (1))
257
258 (makepp
250540cd 259 "^makepp\\(?:\\(?:: warning\\(:\\).*?\\|\\(: Scanning\\|: [LR]e?l?oading makefile\\|: Imported\\|log:.*?\\) \\|: .*?\\)\
3b79dd20 260`\\(\\(\\S +?\\)\\(?::\\([0-9]+\\)\\)?\\)['(]\\)"
7837c247 261 4 5 nil (1 . 2) 3
3b79dd20 262 ("`\\(\\(\\S +?\\)\\(?::\\([0-9]+\\)\\)?\\)['(]" nil nil
7837c247
SM
263 (2 compilation-info-face)
264 (3 compilation-line-face nil t)
51c8ad03 265 (1 (compilation-error-properties 2 3 nil nil nil 0 nil)
7837c247
SM
266 append)))
267
6f5b7627 268 ;; Should be lint-1, lint-2 (SysV lint)
7837c247
SM
269 (mips-1
270 " (\\([0-9]+\\)) in \\([^ \n]+\\)" 2 1)
271 (mips-2
272 " in \\([^()\n ]+\\)(\\([0-9]+\\))$" 1 2)
273
274 (msft
db0095e0 275 ;; AFAWK, The message may be a "warning", "error", or "fatal error".
28547c1f 276 "^\\([0-9]+>\\)?\\(\\(?:[a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\)) \
db0095e0 277: \\(?:warnin\\(g\\)\\|[a-z ]+\\) C[0-9]+:" 2 3 nil (4))
7837c247
SM
278
279 (oracle
b3ef54c5
DP
280 "^\\(?:Semantic error\\|Error\\|PCC-[0-9]+:\\).* line \\([0-9]+\\)\
281\\(?:\\(?:,\\| at\\)? column \\([0-9]+\\)\\)?\
282\\(?:,\\| in\\| of\\)? file \\(.*?\\):?$"
7837c247 283 3 1 2)
40d63d1f 284
049dcb6f
GM
285 ;; "during global destruction": This comes out under "use
286 ;; warnings" in recent perl when breaking circular references
287 ;; during program or thread exit.
7837c247 288 (perl
049dcb6f
GM
289 " at \\([^ \n]+\\) line \\([0-9]+\\)\\(?:[,.]\\|$\\| \
290during global destruction\\.$\\)" 1 2)
7837c247 291
fa2a4e7d
MH
292 (php
293 "\\(?:Parse\\|Fatal\\) error: \\(.*\\) in \\(.*\\) on line \\([0-9]+\\)"
294 2 3 nil nil)
295
7837c247
SM
296 (rxp
297 "^\\(?:Error\\|Warnin\\(g\\)\\):.*\n.* line \\([0-9]+\\) char\
298 \\([0-9]+\\) of file://\\(.+\\)"
299 4 2 3 (1))
300
301 (sparc-pascal-file
302 "^\\w\\w\\w \\w\\w\\w +[0-3]?[0-9] +[0-2][0-9]:[0-5][0-9]:[0-5][0-9]\
303 [12][09][0-9][0-9] +\\(.*\\):$"
304 1 nil nil 0)
305 (sparc-pascal-line
306 "^\\(\\(?:E\\|\\(w\\)\\) +[0-9]+\\) line \\([0-9]+\\) - "
307 nil 3 nil (2) nil (1 (compilation-face '(2))))
308 (sparc-pascal-example
309 "^ +\\([0-9]+\\) +.*\n\\(\\(?:e\\|\\(w\\)\\) [0-9]+\\)-+"
310 nil 1 nil (3) nil (2 (compilation-face '(3))))
311
312 (sun
6f5b7627 313 ": \\(?:ERROR\\|WARNIN\\(G\\)\\|REMAR\\(K\\)\\) \\(?:[[:alnum:] ]+, \\)?\
7837c247
SM
314File = \\(.+\\), Line = \\([0-9]+\\)\\(?:, Column = \\([0-9]+\\)\\)?"
315 3 4 5 (1 . 2))
316
317 (sun-ada
318 "^\\([^, \n\t]+\\), line \\([0-9]+\\), char \\([0-9]+\\)[:., \(-]" 1 2 3)
319
7837c247
SM
320 (4bsd
321 "\\(?:^\\|:: \\|\\S ( \\)\\(/[^ \n\t()]+\\)(\\([0-9]+\\))\
0c9a01ff
MY
322\\(?:: \\(warning:\\)?\\|$\\| ),\\)" 1 2 nil (3))
323
324 (gcov-file
b8fa0ffd 325 "^ *-: *\\(0\\):Source:\\(.+\\)$"
eb3d9609
MY
326 2 1 nil 0 nil
327 (1 compilation-line-face prepend) (2 compilation-info-face prepend))
328 (gcov-header
329 "^ *-: *\\(0\\):\\(?:Object\\|Graph\\|Data\\|Runs\\|Programs\\):.+$"
330 nil 1 nil 0 nil
331 (1 compilation-line-face prepend))
332 ;; Underlines over all lines of gcov output are too uncomfortable to read.
333 ;; However, hyperlinks embedded in the lines are useful.
334 ;; So I put default face on the lines; and then put
335 ;; compilation-*-face by manually to eliminate the underlines.
336 ;; The hyperlinks are still effective.
337 (gcov-nomark
338 "^ *-: *\\([1-9]\\|[0-9]\\{2,\\}\\):.*$"
339 nil 1 nil 0 nil
340 (0 'default t)
341 (1 compilation-line-face prepend))
0c9a01ff 342 (gcov-called-line
eb3d9609 343 "^ *\\([0-9]+\\): *\\([0-9]+\\):.*$"
b8fa0ffd 344 nil 2 nil 0 nil
eb3d9609
MY
345 (0 'default t)
346 (1 compilation-info-face prepend) (2 compilation-line-face prepend))
347 (gcov-never-called
b8fa0ffd 348 "^ *\\(#####\\): *\\([0-9]+\\):.*$"
eb3d9609
MY
349 nil 2 nil 2 nil
350 (0 'default t)
351 (1 compilation-error-face prepend) (2 compilation-line-face prepend))
ddab7705 352
3dc4febd 353 (perl--Pod::Checker
ddab7705
VJL
354 ;; podchecker error messages, per Pod::Checker.
355 ;; The style is from the Pod::Checker::poderror() function, eg.
356 ;; *** ERROR: Spurious text after =cut at line 193 in file foo.pm
357 ;;
358 ;; Plus end_pod() can give "at line EOF" instead of a
359 ;; number, so for that match "on line N" which is the
360 ;; originating spot, eg.
361 ;; *** ERROR: =over on line 37 without closing =back at line EOF in file bar.pm
362 ;;
363 ;; Plus command() can give both "on line N" and "at line N";
364 ;; the latter is desired and is matched because the .* is
365 ;; greedy.
366 ;; *** ERROR: =over on line 1 without closing =back (at head1) at line 3 in file x.pod
367 ;;
368 "^\\*\\*\\* \\(?:ERROR\\|\\(WARNING\\)\\).* \\(?:at\\|on\\) line \
369\\([0-9]+\\) \\(?:.* \\)?in file \\([^ \t\n]+\\)"
370 3 2 nil (1))
3dc4febd 371 (perl--Test
ddab7705
VJL
372 ;; perl Test module error messages.
373 ;; Style per the ok() function "$context", eg.
374 ;; # Failed test 1 in foo.t at line 6
375 ;;
376 "^# Failed test [0-9]+ in \\([^ \t\r\n]+\\) at line \\([0-9]+\\)"
377 1 2)
3dc4febd 378 (perl--Test2
01f179de
GM
379 ;; Or when comparing got/want values,
380 ;; # Test 2 got: "xx" (t-compilation-perl-2.t at line 10)
381 ;;
382 ;; And under Test::Harness they're preceded by progress stuff with
383 ;; \r and "NOK",
384 ;; ... NOK 1# Test 1 got: "1234" (t/foo.t at line 46)
385 ;;
386 "^\\(.*NOK.*\\)?# Test [0-9]+ got:.* (\\([^ \t\r\n]+\\) at line \
387\\([0-9]+\\))"
388 2 3)
3dc4febd 389 (perl--Test::Harness
ddab7705
VJL
390 ;; perl Test::Harness output, eg.
391 ;; NOK 1# Test 1 got: "1234" (t/foo.t at line 46)
392 ;;
393 ;; Test::Harness is slightly designed for tty output, since
394 ;; it prints CRs to overwrite progress messages, but if you
395 ;; run it in with M-x compile this pattern can at least step
396 ;; through the failures.
397 ;;
398 "^.*NOK.* \\([^ \t\r\n]+\\) at line \\([0-9]+\\)"
399 1 2)
3dc4febd 400 (weblint
ddab7705
VJL
401 ;; The style comes from HTML::Lint::Error::as_string(), eg.
402 ;; index.html (13:1) Unknown element <fdjsk>
403 ;;
404 ;; The pattern only matches filenames without spaces, since that
405 ;; should be usual and should help reduce the chance of a false
406 ;; match of a message from some unrelated program.
407 ;;
408 ;; This message style is quite close to the "ibm" entry which is
409 ;; for IBM C, though that ibm bit doesn't put a space after the
410 ;; filename.
411 ;;
412 "^\\([^ \t\r\n(]+\\) (\\([0-9]+\\):\\([0-9]+\\)) "
413 1 2 3)
eb3d9609 414 )
7837c247
SM
415 "Alist of values for `compilation-error-regexp-alist'.")
416
417(defcustom compilation-error-regexp-alist
418 (mapcar 'car compilation-error-regexp-alist-alist)
6c43f2f9 419 "Alist that specifies how to match errors in compiler output.
7957baea 420On GNU and Unix, any string is a valid filename, so these
7837c247
SM
421matchers must make some common sense assumptions, which catch
422normal cases. A shorter list will be lighter on resource usage.
423
424Instead of an alist element, you can use a symbol, which is
425looked up in `compilation-error-regexp-alist-alist'. You can see
426the predefined symbols and their effects in the file
6f5b7627 427`etc/compilation.txt' (linked below if you are customizing this).
7837c247
SM
428
429Each elt has the form (REGEXP FILE [LINE COLUMN TYPE HYPERLINK
430HIGHLIGHT...]). If REGEXP matches, the FILE'th subexpression
431gives the file name, and the LINE'th subexpression gives the line
432number. The COLUMN'th subexpression gives the column number on
433that line.
434
435If FILE, LINE or COLUMN are nil or that index didn't match, that
436information is not present on the matched line. In that case the
437file name is assumed to be the same as the previous one in the
438buffer, line number defaults to 1 and column defaults to
439beginning of line's indentation.
440
441FILE can also have the form (FILE FORMAT...), where the FORMATs
442\(e.g. \"%s.c\") will be applied in turn to the recognized file
443name, until a file of that name is found. Or FILE can also be a
4aede2f2
RS
444function that returns (FILENAME) or (RELATIVE-FILENAME . DIRNAME).
445In the former case, FILENAME may be relative or absolute.
7837c247
SM
446
447LINE can also be of the form (LINE . END-LINE) meaning a range
448of lines. COLUMN can also be of the form (COLUMN . END-COLUMN)
449meaning a range of columns starting on LINE and ending on
450END-LINE, if that matched.
451
452TYPE is 2 or nil for a real error or 1 for warning or 0 for info.
453TYPE can also be of the form (WARNING . INFO). In that case this
454will be equivalent to 1 if the WARNING'th subexpression matched
455or else equivalent to 0 if the INFO'th subexpression matched.
456See `compilation-error-face', `compilation-warning-face',
457`compilation-info-face' and `compilation-skip-threshold'.
458
459What matched the HYPERLINK'th subexpression has `mouse-face' and
460`compilation-message-face' applied. If this is nil, the text
461matched by the whole REGEXP becomes the hyperlink.
462
463Additional HIGHLIGHTs as described under `font-lock-keywords' can
464be added."
465 :type `(set :menu-tag "Pick"
466 ,@(mapcar (lambda (elt)
467 (list 'const (car elt)))
468 compilation-error-regexp-alist-alist))
469 :link `(file-link :tag "example file"
eeb3ede4 470 ,(expand-file-name "compilation.txt" data-directory))
7837c247 471 :group 'compilation)
55dfd2c4 472
98d4c36f 473;;;###autoload(put 'compilation-directory 'safe-local-variable 'stringp)
ebb8cb68
EZ
474(defvar compilation-directory nil
475 "Directory to restore to when doing `recompile'.")
476
7837c247
SM
477(defvar compilation-directory-matcher
478 '("\\(?:Entering\\|Leavin\\(g\\)\\) directory `\\(.+\\)'$" (2 . 1))
479 "A list for tracking when directories are entered or left.
434520fa 480If nil, do not track directories, e.g. if all file names are absolute. The
7837c247
SM
481first element is the REGEXP matching these messages. It can match any number
482of variants, e.g. different languages. The remaining elements are all of the
483form (DIR . LEAVE). If for any one of these the DIR'th subexpression
484matches, that is a directory name. If LEAVE is nil or the corresponding
485LEAVE'th subexpression doesn't match, this message is about going into another
486directory. If it does match anything, this message is about going back to the
487directory we were in before the last entering message. If you change this,
488you may also want to change `compilation-page-delimiter'.")
489
490(defvar compilation-page-delimiter
491 "^\\(?:\f\\|.*\\(?:Entering\\|Leaving\\) directory `.+'\n\\)+"
492 "Value of `page-delimiter' in Compilation mode.")
493
494(defvar compilation-mode-font-lock-keywords
0ab31e4a 495 '(;; configure output lines.
7837c247
SM
496 ("^[Cc]hecking \\(?:[Ff]or \\|[Ii]f \\|[Ww]hether \\(?:to \\)?\\)?\\(.+\\)\\.\\.\\. *\\(?:(cached) *\\)?\\(\\(yes\\(?: .+\\)?\\)\\|no\\|\\(.*\\)\\)$"
497 (1 font-lock-variable-name-face)
38dbf92b 498 (2 (compilation-face '(4 . 3))))
7837c247 499 ;; Command output lines. Recognize `make[n]:' lines too.
6f5b7627 500 ("^\\([[:alnum:]_/.+-]+\\)\\(\\[\\([0-9]+\\)\\]\\)?[ \t]*:"
7837c247
SM
501 (1 font-lock-function-name-face) (3 compilation-line-face nil t))
502 (" --?o\\(?:utfile\\|utput\\)?[= ]?\\(\\S +\\)" . 1)
47f97084
RF
503 ("^Compilation \\(finished\\).*"
504 (0 '(face nil message nil help-echo nil mouse-face nil) t)
314b410b 505 (1 compilation-info-face))
0ab31e4a 506 ("^Compilation \\(exited abnormally\\|interrupt\\|killed\\|terminated\\|segmentation fault\\)\\(?:.*with code \\([0-9]+\\)\\)?.*"
47f97084 507 (0 '(face nil message nil help-echo nil mouse-face nil) t)
314b410b
JL
508 (1 compilation-error-face)
509 (2 compilation-error-face nil t)))
7837c247
SM
510 "Additional things to highlight in Compilation mode.
511This gets tacked on the end of the generated expressions.")
a24770bc 512
9ac57479
KS
513(defvar compilation-highlight-regexp t
514 "Regexp matching part of visited source lines to highlight temporarily.
515Highlight entire line if t; don't highlight source lines if nil.")
516
517(defvar compilation-highlight-overlay nil
518 "Overlay used to temporarily highlight compilation matches.")
519
f8e03ecb 520(defcustom compilation-error-screen-columns t
813fb3fe 521 "If non-nil, column numbers in error messages are screen columns.
f8e03ecb
AS
522Otherwise they are interpreted as character positions, with
523each character occupying one column.
524The default is to use screen columns, which requires that the compilation
525program and Emacs agree about the display width of the characters,
526especially the TAB character."
527 :type 'boolean
528 :group 'compilation
529 :version "20.4")
530
c5049fa0 531(defcustom compilation-read-command t
813fb3fe 532 "Non-nil means \\[compile] reads the compilation command to use.
851231e9 533Otherwise, \\[compile] just uses the value of `compile-command'."
c5049fa0
RS
534 :type 'boolean
535 :group 'compilation)
90016295 536
e83be080 537;;;###autoload
c5049fa0 538(defcustom compilation-ask-about-save t
813fb3fe 539 "Non-nil means \\[compile] asks which buffers to save before compiling.
c5049fa0
RS
540Otherwise, it saves all modified buffers without asking."
541 :type 'boolean
542 :group 'compilation)
90016295 543
7c163413 544;;;###autoload
c5049fa0 545(defcustom compilation-search-path '(nil)
813fb3fe 546 "List of directories to search for source files named in error messages.
d3cb357b 547Elements should be directory names, not file names of directories.
f441be5b 548The value nil as an element means to try the default directory."
c5049fa0
RS
549 :type '(repeat (choice (const :tag "Default" nil)
550 (string :tag "Directory")))
551 :group 'compilation)
55dfd2c4 552
7957baea 553;;;###autoload
c5049fa0 554(defcustom compile-command "make -k "
813fb3fe 555 "Last shell command used to do a compilation; default for next compilation.
55dfd2c4
RS
556
557Sometimes it is useful for files to supply local values for this variable.
558You might also use mode hooks to specify it in certain modes, like this:
559
61c4aaf8 560 (add-hook 'c-mode-hook
61c4aaf8
RS
561 (lambda ()
562 (unless (or (file-exists-p \"makefile\")
563 (file-exists-p \"Makefile\"))
5b6858da
SM
564 (set (make-local-variable 'compile-command)
565 (concat \"make -k \"
7837c247 566 (file-name-sans-extension buffer-file-name))))))"
c5049fa0
RS
567 :type 'string
568 :group 'compilation)
631c8020 569;;;###autoload(put 'compile-command 'safe-local-variable 'stringp)
55dfd2c4 570
7957baea 571;;;###autoload
93ef35e4 572(defcustom compilation-disable-input nil
813fb3fe 573 "If non-nil, send end-of-file as compilation process input.
3fa37cd1 574This only affects platforms that support asynchronous processes (see
bac3a1c9 575`start-process'); synchronous compilation processes never accept input."
3fa37cd1
EZ
576 :type 'boolean
577 :group 'compilation
578 :version "22.1")
579
7837c247
SM
580;; A weak per-compilation-buffer hash indexed by (FILENAME . DIRECTORY). Each
581;; value is a FILE-STRUCTURE as described above, with the car eq to the hash
4fffd73b 582;; key. This holds the tree seen from root, for storing new nodes.
7837c247
SM
583(defvar compilation-locs ())
584
585(defvar compilation-debug nil
6f5b7627 586 "*Set this to t before creating a *compilation* buffer.
7837c247
SM
587Then every error line will have a debug text property with the matcher that
588fit this line and the match data. Use `describe-text-properties'.")
d3cb357b 589
01f89d11
RM
590(defvar compilation-exit-message-function nil "\
591If non-nil, called when a compilation process dies to return a status message.
fd5e58d7
RM
592This should be a function of three arguments: process status, exit status,
593and exit message; it returns a cons (MESSAGE . MODELINE) of the strings to
594write into the compilation buffer, and to put in its mode line.")
01f89d11 595
c20f961b
JPW
596(defvar compilation-environment nil
597 "*List of environment variables for compilation to inherit.
598Each element should be a string of the form ENVVARNAME=VALUE.
599This list is temporarily prepended to `process-environment' prior to
600starting the compilation process.")
601
770970cb
RS
602;; History of compile commands.
603(defvar compile-history nil)
770970cb 604
fa947ef3
JL
605(defface compilation-error
606 '((t :inherit font-lock-warning-face))
607 "Face used to highlight compiler errors."
da742205 608 :group 'compilation
fa947ef3
JL
609 :version "22.1")
610
541a6d0d 611(defface compilation-warning
bc3621a0
EZ
612 '((((class color) (min-colors 16)) (:foreground "Orange" :weight bold))
613 (((class color)) (:foreground "cyan" :weight bold))
7837c247
SM
614 (t (:weight bold)))
615 "Face used to highlight compiler warnings."
da742205 616 :group 'compilation
bf247b6e 617 :version "22.1")
7837c247 618
541a6d0d 619(defface compilation-info
e4aa3c94 620 '((((class color) (min-colors 16) (background light))
bc3621a0 621 (:foreground "Green3" :weight bold))
ea81d57e
DN
622 (((class color) (min-colors 88) (background dark))
623 (:foreground "Green1" :weight bold))
e4aa3c94 624 (((class color) (min-colors 16) (background dark))
bc3621a0
EZ
625 (:foreground "Green" :weight bold))
626 (((class color)) (:foreground "green" :weight bold))
7837c247 627 (t (:weight bold)))
fa947ef3 628 "Face used to highlight compiler information."
da742205 629 :group 'compilation
bf247b6e 630 :version "22.1")
7837c247 631
a5f1c37e 632(defface compilation-line-number
38dbf92b 633 '((t :inherit font-lock-variable-name-face))
fa947ef3 634 "Face for displaying line numbers in compiler messages."
da742205 635 :group 'compilation
a5f1c37e
RS
636 :version "22.1")
637
638(defface compilation-column-number
38dbf92b 639 '((t :inherit font-lock-type-face))
fa947ef3 640 "Face for displaying column numbers in compiler messages."
da742205 641 :group 'compilation
a5f1c37e
RS
642 :version "22.1")
643
f4beca06 644(defcustom compilation-message-face 'underline
7837c247
SM
645 "Face name to use for whole messages.
646Faces `compilation-error-face', `compilation-warning-face',
647`compilation-info-face', `compilation-line-face' and
f4beca06
RS
648`compilation-column-face' get prepended to this, when applicable."
649 :type 'face
650 :group 'compilation
651 :version "22.1")
7837c247 652
38dbf92b 653(defvar compilation-error-face 'compilation-error
7837c247
SM
654 "Face name to use for file name in error messages.")
655
38dbf92b 656(defvar compilation-warning-face 'compilation-warning
7837c247
SM
657 "Face name to use for file name in warning messages.")
658
38dbf92b 659(defvar compilation-info-face 'compilation-info
7837c247
SM
660 "Face name to use for file name in informational messages.")
661
a5f1c37e 662(defvar compilation-line-face 'compilation-line-number
38dbf92b 663 "Face name to use for line numbers in compiler messages.")
7837c247 664
a5f1c37e 665(defvar compilation-column-face 'compilation-column-number
fa947ef3 666 "Face name to use for column numbers in compiler messages.")
7837c247
SM
667
668;; same faces as dired uses
669(defvar compilation-enter-directory-face 'font-lock-function-name-face
fa947ef3 670 "Face name to use for entering directory messages.")
7837c247
SM
671
672(defvar compilation-leave-directory-face 'font-lock-type-face
fa947ef3 673 "Face name to use for leaving directory messages.")
7837c247
SM
674
675
676
c536bb39 677;; Used for compatibility with the old compile.el.
0ff7f01e 678(defvaralias 'compilation-last-buffer 'next-error-last-buffer)
6f5b7627 679(defvar compilation-parsing-end (make-marker))
c536bb39
SM
680(defvar compilation-parse-errors-function nil)
681(defvar compilation-error-list nil)
682(defvar compilation-old-error-list nil)
683
813fb3fe
SM
684(defcustom compilation-auto-jump-to-first-error nil
685 "If non-nil, automatically jump to the first error after `compile'."
765831a0
DN
686 :type 'boolean
687 :group 'compilation
688 :version "23.1")
813fb3fe
SM
689
690(defvar compilation-auto-jump-to-next nil
691 "If non-nil, automatically jump to the next error encountered.")
692(make-variable-buffer-local 'compilation-auto-jump-to-next)
693
97546017
DN
694
695(defvar compilation-skip-to-next-location t
696 "*If non-nil, skip multiple error messages for the same source location.")
697
698(defcustom compilation-skip-threshold 1
699 "Compilation motion commands skip less important messages.
700The value can be either 2 -- skip anything less than error, 1 --
701skip anything less than warning or 0 -- don't skip any messages.
702Note that all messages not positively identified as warning or
703info, are considered errors."
704 :type '(choice (const :tag "Warnings and info" 2)
705 (const :tag "Info" 1)
706 (const :tag "None" 0))
707 :group 'compilation
708 :version "22.1")
709
710(defcustom compilation-skip-visited nil
711 "Compilation motion commands skip visited messages if this is t.
712Visited messages are ones for which the file, line and column have been jumped
713to from the current content in the current compilation buffer, even if it was
714from a different message."
715 :type 'boolean
716 :group 'compilation
717 :version "22.1")
718
7837c247
SM
719(defun compilation-face (type)
720 (or (and (car type) (match-end (car type)) compilation-warning-face)
721 (and (cdr type) (match-end (cdr type)) compilation-info-face)
722 compilation-error-face))
723
deda0c65
DP
724;; Internal function for calculating the text properties of a directory
725;; change message. The directory property is important, because it is
726;; the stack of nested enter-messages. Relative filenames on the following
727;; lines are relative to the top of the stack.
7837c247
SM
728(defun compilation-directory-properties (idx leave)
729 (if leave (setq leave (match-end leave)))
730 ;; find previous stack, and push onto it, or if `leave' pop it
731 (let ((dir (previous-single-property-change (point) 'directory)))
732 (setq dir (if dir (or (get-text-property (1- dir) 'directory)
733 (get-text-property dir 'directory))))
734 `(face ,(if leave
735 compilation-leave-directory-face
736 compilation-enter-directory-face)
737 directory ,(if leave
738 (or (cdr dir)
739 '(nil)) ; nil only isn't a property-change
740 (cons (match-string-no-properties idx) dir))
741 mouse-face highlight
6f5b7627 742 keymap compilation-button-map
5ae48997 743 help-echo "mouse-2: visit destination directory")))
7837c247 744
4fffd73b 745;; Data type `reverse-ordered-alist' retriever. This function retrieves the
7837c247
SM
746;; KEY element from the ALIST, creating it in the right position if not already
747;; present. ALIST structure is
748;; '(ANCHOR (KEY1 ...) (KEY2 ...)... (KEYn ALIST ...))
749;; ANCHOR is ignored, but necessary so that elements can be inserted. KEY1
4fffd73b 750;; may be nil. The other KEYs are ordered backwards so that growing line
7837c247
SM
751;; numbers can be inserted in front and searching can abort after half the
752;; list on average.
6f5b7627 753(eval-when-compile ;Don't keep it at runtime if not needed.
7837c247
SM
754(defmacro compilation-assq (key alist)
755 `(let* ((l1 ,alist)
756 (l2 (cdr l1)))
757 (car (if (if (null ,key)
758 (if l2 (null (caar l2)))
759 (while (if l2 (if (caar l2) (< ,key (caar l2)) t))
760 (setq l1 l2
761 l2 (cdr l1)))
762 (if l2 (eq ,key (caar l2))))
763 l2
6f5b7627 764 (setcdr l1 (cons (list ,key) l2)))))))
7837c247 765
813fb3fe
SM
766(defun compilation-auto-jump (buffer pos)
767 (with-current-buffer buffer
768 (goto-char pos)
78dc87a2
JL
769 (if compilation-auto-jump-to-first-error
770 (compile-goto-error))))
7837c247
SM
771
772;; This function is the central driver, called when font-locking to gather
773;; all information needed to later jump to corresponding source code.
774;; Return a property list with all meta information on this error location.
f0685ed1 775
7837c247 776(defun compilation-error-properties (file line end-line col end-col type fmt)
813fb3fe
SM
777 (unless (< (next-single-property-change (match-beginning 0)
778 'directory nil (point))
7837c247
SM
779 (point))
780 (if file
781 (if (functionp file)
782 (setq file (funcall file))
783 (let (dir)
784 (setq file (match-string-no-properties file))
785 (unless (file-name-absolute-p file)
786 (setq dir (previous-single-property-change (point) 'directory)
787 dir (if dir (or (get-text-property (1- dir) 'directory)
788 (get-text-property dir 'directory)))))
efb0e677 789 (setq file (cons file (car dir)))))
7837c247 790 ;; This message didn't mention one, get it from previous
f0685ed1
RS
791 (let ((prev-pos
792 ;; Find the previous message.
793 (previous-single-property-change (point) 'message)))
794 (if prev-pos
795 ;; Get the file structure that belongs to it.
796 (let* ((prev
797 (or (get-text-property (1- prev-pos) 'message)
798 (get-text-property prev-pos 'message)))
799 (prev-struct
800 (car (nth 2 (car prev)))))
801 ;; Construct FILE . DIR from that.
802 (if prev-struct
803 (setq file (cons (car prev-struct)
804 (cadr prev-struct))))))
805 (unless file
806 (setq file '("*unknown*")))))
7837c247
SM
807 ;; All of these fields are optional, get them only if we have an index, and
808 ;; it matched some part of the message.
809 (and line
810 (setq line (match-string-no-properties line))
811 (setq line (string-to-number line)))
812 (and end-line
813 (setq end-line (match-string-no-properties end-line))
814 (setq end-line (string-to-number end-line)))
9dc3a46a
JL
815 (if col
816 (if (functionp col)
817 (setq col (funcall col))
818 (and
819 (setq col (match-string-no-properties col))
820 (setq col (- (string-to-number col) compilation-first-column)))))
821 (if (and end-col (functionp end-col))
822 (setq end-col (funcall end-col))
823 (if (and end-col (setq end-col (match-string-no-properties end-col)))
824 (setq end-col (- (string-to-number end-col) compilation-first-column -1))
825 (if end-line (setq end-col -1))))
eb6fb6e2 826 (if (consp type) ; not a static type, check what it is.
7837c247
SM
827 (setq type (or (and (car type) (match-end (car type)) 1)
828 (and (cdr type) (match-end (cdr type)) 0)
829 2)))
813fb3fe
SM
830
831 (when (and compilation-auto-jump-to-next
832 (>= type compilation-skip-threshold))
833 (kill-local-variable 'compilation-auto-jump-to-next)
834 (run-with-timer 0 nil 'compilation-auto-jump
835 (current-buffer) (match-beginning 0)))
836
efb0e677
SM
837 (compilation-internal-error-properties file line end-line col end-col type fmt)))
838
9c8e6c85
SM
839(defun compilation-move-to-column (col screen)
840 "Go to column COL on the current line.
841If SCREEN is non-nil, columns are screen columns, otherwise, they are
842just char-counts."
843 (if screen
844 (move-to-column col)
845 (goto-char (min (+ (line-beginning-position) col) (line-end-position)))))
846
7957baea 847(defun compilation-internal-error-properties (file line end-line col end-col type fmts)
efb0e677
SM
848 "Get the meta-info that will be added as text-properties.
849LINE, END-LINE, COL, END-COL are integers or nil.
7957baea
RS
850TYPE can be 0, 1, or 2, meaning error, warning, or just info.
851FILE should be (FILENAME) or (RELATIVE-FILENAME . DIRNAME) or nil.
852FMTS is a list of format specs for transforming the file name.
853 (See `compilation-error-regexp-alist'.)"
efb0e677 854 (unless file (setq file '("*unknown*")))
7957baea
RS
855 (let* ((file-struct (compilation-get-file-structure file fmts))
856 ;; Get first already existing marker (if any has one, all have one).
857 ;; Do this first, as the compilation-assq`s may create new nodes.
858 (marker-line (car (cddr file-struct))) ; a line structure
efb0e677
SM
859 (marker (nth 3 (cadr marker-line))) ; its marker
860 (compilation-error-screen-columns compilation-error-screen-columns)
861 end-marker loc end-loc)
862 (if (not (and marker (marker-buffer marker)))
7957baea 863 (setq marker nil) ; no valid marker for this file
dbd97672
DP
864 (setq loc (or line 1)) ; normalize no linenumber to line 1
865 (catch 'marker ; find nearest loc, at least one exists
7957baea 866 (dolist (x (nthcdr 3 file-struct)) ; loop over remaining lines
dbd97672 867 (if (> (car x) loc) ; still bigger
efb0e677 868 (setq marker-line x)
dbd97672
DP
869 (if (> (- (or (car marker-line) 1) loc)
870 (- loc (car x))) ; current line is nearer
efb0e677
SM
871 (setq marker-line x))
872 (throw 'marker t))))
873 (setq marker (nth 3 (cadr marker-line))
dbd97672 874 marker-line (or (car marker-line) 1))
efb0e677 875 (with-current-buffer (marker-buffer marker)
8dfb3e16
RS
876 (save-excursion
877 (save-restriction
878 (widen)
879 (goto-char (marker-position marker))
880 (when (or end-col end-line)
881 (beginning-of-line (- (or end-line line) marker-line -1))
882 (if (or (null end-col) (< end-col 0))
883 (end-of-line)
884 (compilation-move-to-column
885 end-col compilation-error-screen-columns))
886 (setq end-marker (list (point-marker))))
887 (beginning-of-line (if end-line
888 (- line end-line -1)
889 (- loc marker-line -1)))
890 (if col
891 (compilation-move-to-column
892 col compilation-error-screen-columns)
893 (forward-to-indentation 0))
894 (setq marker (list (point-marker)))))))
efb0e677 895
7957baea 896 (setq loc (compilation-assq line (cdr file-struct)))
efb0e677 897 (if end-line
7957baea 898 (setq end-loc (compilation-assq end-line (cdr file-struct))
efb0e677
SM
899 end-loc (compilation-assq end-col end-loc))
900 (if end-col ; use same line element
901 (setq end-loc (compilation-assq end-col loc))))
902 (setq loc (compilation-assq col loc))
903 ;; If they are new, make the loc(s) reference the file they point to.
7957baea 904 (or (cdr loc) (setcdr loc `(,line ,file-struct ,@marker)))
efb0e677 905 (if end-loc
7957baea
RS
906 (or (cdr end-loc)
907 (setcdr end-loc `(,(or end-line line) ,file-struct ,@end-marker))))
efb0e677
SM
908
909 ;; Must start with face
910 `(face ,compilation-message-face
911 message (,loc ,type ,end-loc)
912 ,@(if compilation-debug
913 `(debug (,(assoc (with-no-warnings matcher) font-lock-keywords)
914 ,@(match-data))))
915 help-echo ,(if col
916 "mouse-2: visit this file, line and column"
917 (if line
918 "mouse-2: visit this file and line"
919 "mouse-2: visit this file"))
920 keymap compilation-button-map
921 mouse-face highlight)))
7837c247 922
e93b2a55
SM
923(defun compilation-mode-font-lock-keywords ()
924 "Return expressions to highlight in Compilation mode."
91fa27cd
SM
925 (if compilation-parse-errors-function
926 ;; An old package! Try the compatibility code.
927 '((compilation-compat-parse-errors))
928 (append
929 ;; make directory tracking
930 (if compilation-directory-matcher
931 `((,(car compilation-directory-matcher)
932 ,@(mapcar (lambda (elt)
933 `(,(car elt)
934 (compilation-directory-properties
935 ,(car elt) ,(cdr elt))
0ea50e43 936 t t))
91fa27cd
SM
937 (cdr compilation-directory-matcher)))))
938
939 ;; Compiler warning/error lines.
940 (mapcar
941 (lambda (item)
942 (if (symbolp item)
943 (setq item (cdr (assq item
944 compilation-error-regexp-alist-alist))))
945 (let ((file (nth 1 item))
946 (line (nth 2 item))
947 (col (nth 3 item))
948 (type (nth 4 item))
949 end-line end-col fmt)
b3a7f48f
DP
950 (if (consp file) (setq fmt (cdr file) file (car file)))
951 (if (consp line) (setq end-line (cdr line) line (car line)))
91fa27cd 952 (if (consp col) (setq end-col (cdr col) col (car col)))
b3a7f48f 953
544dccaa 954 (if (functionp line)
91fa27cd
SM
955 ;; The old compile.el had here an undocumented hook that
956 ;; allowed `line' to be a function that computed the actual
957 ;; error location. Let's do our best.
958 `(,(car item)
0ea50e43
RS
959 (0 (save-match-data
960 (compilation-compat-error-properties
961 (funcall ',line (cons (match-string ,file)
962 (cons default-directory
963 ',(nthcdr 4 item)))
964 ,(if col `(match-string ,col))))))
91fa27cd 965 (,file compilation-error-face t))
b3a7f48f 966
f71d34b0
SM
967 (unless (or (null (nth 5 item)) (integerp (nth 5 item)))
968 (error "HYPERLINK should be an integer: %s" (nth 5 item)))
969
91fa27cd
SM
970 `(,(nth 0 item)
971
972 ,@(when (integerp file)
973 `((,file ,(if (consp type)
974 `(compilation-face ',type)
975 (aref [compilation-info-face
976 compilation-warning-face
977 compilation-error-face]
978 (or type 2))))))
979
980 ,@(when line
981 `((,line compilation-line-face nil t)))
982 ,@(when end-line
983 `((,end-line compilation-line-face nil t)))
984
9dc3a46a 985 ,@(when (integerp col)
91fa27cd 986 `((,col compilation-column-face nil t)))
9dc3a46a 987 ,@(when (integerp end-col)
91fa27cd
SM
988 `((,end-col compilation-column-face nil t)))
989
990 ,@(nthcdr 6 item)
991 (,(or (nth 5 item) 0)
992 (compilation-error-properties ',file ,line ,end-line
993 ,col ,end-col ',(or type 2)
994 ',fmt)
995 append))))) ; for compilation-message-face
996 compilation-error-regexp-alist)
997
998 compilation-mode-font-lock-keywords)))
7837c247 999
01f89d11 1000\f
d3cb357b 1001;;;###autoload
7837c247 1002(defun compile (command &optional comint)
55dfd2c4
RS
1003 "Compile the program including the current buffer. Default: run `make'.
1004Runs COMMAND, a shell command, in a separate process asynchronously
1005with output going to the buffer `*compilation*'.
d3cb357b 1006
55dfd2c4
RS
1007You can then use the command \\[next-error] to find the next error message
1008and move to the source code that caused it.
1009
8f72171b
RS
1010If optional second arg COMINT is t the buffer will be in Comint mode with
1011`compilation-shell-minor-mode'.
1012
08b1edf4
RM
1013Interactively, prompts for the command if `compilation-read-command' is
1014non-nil; otherwise uses `compile-command'. With prefix arg, always prompts.
c39bf546
DP
1015Additionally, with universal prefix arg, compilation buffer will be in
1016comint mode, i.e. interactive.
08b1edf4 1017
7d1dad0c 1018To run more than one compilation at once, start one then rename
c72095b3 1019the \`*compilation*' buffer to some other name with
7d1dad0c
RS
1020\\[rename-buffer]. Then _switch buffers_ and start the new compilation.
1021It will create a new \`*compilation*' buffer.
1022
1023On most systems, termination of the main compilation process
1024kills its subprocesses.
d3cb357b
RM
1025
1026The name used for the buffer is actually whatever is returned by
1027the function in `compilation-buffer-name-function', so you can set that
1028to a function that generates a unique name."
90016295 1029 (interactive
c39bf546 1030 (list
20320c65
JL
1031 (let ((command (eval compile-command)))
1032 (if (or compilation-read-command current-prefix-arg)
1033 (read-from-minibuffer "Compile command: "
1034 command nil nil
1035 (if (equal (car compile-history) command)
1036 '(compile-history . 1)
1037 'compile-history))
1038 command))
c39bf546 1039 (consp current-prefix-arg)))
5b6858da
SM
1040 (unless (equal command (eval compile-command))
1041 (setq compile-command command))
90016295 1042 (save-some-buffers (not compilation-ask-about-save) nil)
091525d5 1043 (setq-default compilation-directory default-directory)
7837c247 1044 (compilation-start command comint))
55dfd2c4 1045
5b6858da 1046;; run compile with the default command line
e60476ca 1047(defun recompile ()
6867356a 1048 "Re-compile the program including the current buffer.
6f5b7627
SM
1049If this is run in a Compilation mode buffer, re-use the arguments from the
1050original use. Otherwise, recompile using `compile-command'."
e60476ca
RS
1051 (interactive)
1052 (save-some-buffers (not compilation-ask-about-save) nil)
091525d5 1053 (let ((default-directory (or compilation-directory default-directory)))
7837c247
SM
1054 (apply 'compilation-start (or compilation-arguments
1055 `(,(eval compile-command))))))
09843b4a 1056
b5812513 1057(defcustom compilation-scroll-output nil
813fb3fe 1058 "Non-nil to scroll the *compilation* buffer window as output appears.
b5812513 1059
6f5b7627 1060Setting it causes the Compilation mode commands to put point at the
b5812513 1061end of their output window so that the end of the output is always
78dc87a2
JL
1062visible rather than the beginning.
1063
1064The value `first-error' stops scrolling at the first error, and leaves
1065point on its location in the *compilation* buffer."
1066 :type '(choice (const :tag "No scrolling" nil)
1067 (const :tag "Scroll compilation output" t)
1068 (const :tag "Stop scrolling at the first error" first-error))
a46fddeb 1069 :version "20.3"
b5812513
DL
1070 :group 'compilation)
1071
7bdb67b2 1072
ae0cc840 1073(defun compilation-buffer-name (mode-name mode-command name-function)
7bdb67b2
GM
1074 "Return the name of a compilation buffer to use.
1075If NAME-FUNCTION is non-nil, call it with one argument MODE-NAME
1076to determine the buffer name.
1077Likewise if `compilation-buffer-name-function' is non-nil.
7d1dad0c 1078If current buffer has the major mode MODE-COMMAND,
7bdb67b2
GM
1079return the name of the current buffer, so that it gets reused.
1080Otherwise, construct a buffer name from MODE-NAME."
dc2feacf 1081 (cond (name-function
7bdb67b2 1082 (funcall name-function mode-name))
dc2feacf 1083 (compilation-buffer-name-function
7bdb67b2 1084 (funcall compilation-buffer-name-function mode-name))
be947101 1085 ((eq mode-command major-mode)
7bdb67b2
GM
1086 (buffer-name))
1087 (t
1088 (concat "*" (downcase mode-name) "*"))))
1089
7837c247
SM
1090;; This is a rough emulation of the old hack, until the transition to new
1091;; compile is complete.
55dfd2c4 1092(defun compile-internal (command error-message
a24770bc
RS
1093 &optional name-of-mode parser
1094 error-regexp-alist name-function
1095 enter-regexp-alist leave-regexp-alist
23b0c5fc 1096 file-regexp-alist nomessage-regexp-alist
9ac57479 1097 no-async highlight-regexp local-map)
7837c247
SM
1098 (if parser
1099 (error "Compile now works very differently, see `compilation-error-regexp-alist'"))
1100 (let ((compilation-error-regexp-alist
1101 (append file-regexp-alist (or error-regexp-alist
1102 compilation-error-regexp-alist)))
1103 (compilation-error (replace-regexp-in-string "^No more \\(.+\\)s\\.?"
1104 "\\1" error-message)))
1105 (compilation-start command nil name-function highlight-regexp)))
1106(make-obsolete 'compile-internal 'compilation-start)
1107
a24c45d2 1108;;;###autoload
7837c247 1109(defun compilation-start (command &optional mode name-function highlight-regexp)
55dfd2c4 1110 "Run compilation command COMMAND (low level interface).
489a8034 1111If COMMAND starts with a cd command, that becomes the `default-directory'.
9ac57479
KS
1112The rest of the arguments are optional; for them, nil means use the default.
1113
7837c247 1114MODE is the major mode to set in the compilation buffer. Mode
6f5b7627 1115may also be t meaning use `compilation-shell-minor-mode' under `comint-mode'.
7d1dad0c 1116
5b317d74 1117If NAME-FUNCTION is non-nil, call it with one argument (the mode name)
7d1dad0c
RS
1118to determine the buffer name. Otherwise, the default is to
1119reuses the current buffer if it has the proper major mode,
1120else use or create a buffer with name based on the major mode.
23b0c5fc 1121
9ac57479 1122If HIGHLIGHT-REGEXP is non-nil, `next-error' will temporarily highlight
6f5b7627 1123the matching section of the visited source line; the default is to use the
9ac57479
KS
1124global value of `compilation-highlight-regexp'.
1125
646bd331 1126Returns the compilation buffer created."
7837c247 1127 (or mode (setq mode 'compilation-mode))
489a8034
DP
1128 (let* ((name-of-mode
1129 (if (eq mode t)
1130 (prog1 "compilation" (require 'comint))
1131 (replace-regexp-in-string "-mode$" "" (symbol-name mode))))
199143f1 1132 (thisdir default-directory)
489a8034 1133 outwin outbuf)
c536bb39
SM
1134 (with-current-buffer
1135 (setq outbuf
1136 (get-buffer-create
be947101 1137 (compilation-buffer-name name-of-mode mode name-function)))
d3cb357b
RM
1138 (let ((comp-proc (get-buffer-process (current-buffer))))
1139 (if comp-proc
1140 (if (or (not (eq (process-status comp-proc) 'run))
1141 (yes-or-no-p
bea1d57a
JB
1142 (format "A %s process is running; kill it? "
1143 name-of-mode)))
d3cb357b
RM
1144 (condition-case ()
1145 (progn
1146 (interrupt-process comp-proc)
1147 (sit-for 1)
1148 (delete-process comp-proc))
1149 (error nil))
1150 (error "Cannot have two processes in `%s' at once"
7837c247 1151 (buffer-name)))))
7837c247 1152 (buffer-disable-undo (current-buffer))
199143f1
DP
1153 ;; first transfer directory from where M-x compile was called
1154 (setq default-directory thisdir)
091525d5
SM
1155 ;; Remember the original dir, so we can use it when we recompile.
1156 ;; default-directory' can't be used reliably for that because it may be
1157 ;; affected by the special handling of "cd ...;".
1158 (set (make-local-variable 'compilation-directory) thisdir)
d42c87ab
RS
1159 ;; Make compilation buffer read-only. The filter can still write it.
1160 ;; Clear out the compilation buffer.
199143f1
DP
1161 (let ((inhibit-read-only t)
1162 (default-directory thisdir))
813fb3fe
SM
1163 ;; Then evaluate a cd command if any, but don't perform it yet, else
1164 ;; start-command would do it again through the shell: (cd "..") AND
1165 ;; sh -c "cd ..; make"
199143f1
DP
1166 (cd (if (string-match "^\\s *cd\\(?:\\s +\\(\\S +?\\)\\)?\\s *[;&\n]" command)
1167 (if (match-end 1)
12221c84 1168 (substitute-env-vars (match-string 1 command))
199143f1
DP
1169 "~")
1170 default-directory))
abed5267 1171 (erase-buffer)
6d2957c4
RS
1172 ;; Select the desired mode.
1173 (if (not (eq mode t))
1174 (funcall mode)
1175 (setq buffer-read-only nil)
1176 (with-no-warnings (comint-mode))
1177 (compilation-shell-minor-mode))
1178 (if highlight-regexp
1179 (set (make-local-variable 'compilation-highlight-regexp)
1180 highlight-regexp))
78dc87a2
JL
1181 (if (or compilation-auto-jump-to-first-error
1182 (eq compilation-scroll-output 'first-error))
813fb3fe 1183 (set (make-local-variable 'compilation-auto-jump-to-next) t))
6d2957c4 1184 ;; Output a mode setter, for saving and later reloading this buffer.
d42c87ab 1185 (insert "-*- mode: " name-of-mode
4b025f47 1186 "; default-directory: " (prin1-to-string default-directory)
a67e5425 1187 " -*-\n"
314b410b
JL
1188 (format "%s started at %s\n\n"
1189 mode-name
1190 (substring (current-time-string) 0 19))
a67e5425 1191 command "\n")
abed5267 1192 (setq thisdir default-directory))
7837c247 1193 (set-buffer-modified-p nil))
7837c247 1194 ;; Pop up the compilation buffer.
c34dc850
GM
1195 ;; http://lists.gnu.org/archive/html/emacs-devel/2007-11/msg01638.html
1196 (setq outwin (display-buffer outbuf))
7837c247 1197 (with-current-buffer outbuf
577bf5d2
JL
1198 (let ((process-environment
1199 (append
1200 compilation-environment
1201 (if (if (boundp 'system-uses-terminfo) ; `if' for compiler warning
1202 system-uses-terminfo)
1203 (list "TERM=dumb" "TERMCAP="
1204 (format "COLUMNS=%d" (window-width)))
1205 (list "TERM=emacs"
1206 (format "TERMCAP=emacs:co#%d:tc=unknown:"
1207 (window-width))))
1208 ;; Set the EMACS variable, but
1209 ;; don't override users' setting of $EMACS.
4b1aaa8b 1210 (unless (getenv "EMACS")
e725507a
CY
1211 (list "EMACS=t"))
1212 (list "INSIDE_EMACS=t")
a9e11582 1213 (copy-sequence process-environment))))
577bf5d2
JL
1214 (set (make-local-variable 'compilation-arguments)
1215 (list command mode name-function highlight-regexp))
1216 (set (make-local-variable 'revert-buffer-function)
1217 'compilation-revert-buffer)
1218 (set-window-start outwin (point-min))
2d0a22f8
RS
1219
1220 ;; Position point as the user will see it.
1221 (let ((desired-visible-point
1222 ;; Put it at the end if `compilation-scroll-output' is set.
1223 (if compilation-scroll-output
1224 (point-max)
1225 ;; Normally put it at the top.
1226 (point-min))))
1227 (if (eq outwin (selected-window))
1228 (goto-char desired-visible-point)
1229 (set-window-point outwin desired-visible-point)))
1230
577bf5d2
JL
1231 ;; The setup function is called before compilation-set-window-height
1232 ;; so it can set the compilation-window-height buffer locally.
1233 (if compilation-process-setup-function
1234 (funcall compilation-process-setup-function))
1235 (compilation-set-window-height outwin)
1236 ;; Start the compilation.
db8af973
MA
1237 (let ((proc
1238 (if (eq mode t)
1239 ;; comint uses `start-file-process'.
1240 (get-buffer-process
1241 (with-no-warnings
a9e11582
MA
1242 (comint-exec
1243 outbuf (downcase mode-name)
1244 (if (file-remote-p default-directory)
1245 "/bin/sh"
1246 shell-file-name)
c94d5f81 1247 nil `("-c" ,command))))
a9e11582
MA
1248 (start-file-process-shell-command (downcase mode-name)
1249 outbuf command))))
4b29e550 1250 ;; Make the buffer's mode line show process state.
a8bdd228
DN
1251 (setq mode-line-process
1252 (list (propertize ":%s" 'face 'compilation-warning)))
4b29e550
MA
1253 (set-process-sentinel proc 'compilation-sentinel)
1254 (set-process-filter proc 'compilation-filter)
b17f53ab
MB
1255 ;; Use (point-max) here so that output comes in
1256 ;; after the initial text,
1257 ;; regardless of where the user sees point.
1258 (set-marker (process-mark proc) (point-max) outbuf)
4b29e550
MA
1259 (when compilation-disable-input
1260 (condition-case nil
1261 (process-send-eof proc)
1262 ;; The process may have exited already.
1263 (error nil)))
1264 (setq compilation-in-progress
1265 (cons proc compilation-in-progress))))
199143f1
DP
1266 ;; Now finally cd to where the shell started make/grep/...
1267 (setq default-directory thisdir))
7837c247
SM
1268 (if (buffer-local-value 'compilation-scroll-output outbuf)
1269 (save-selected-window
1270 (select-window outwin)
043442b4
KS
1271 (goto-char (point-max))))
1272 ;; Make it so the next C-x ` will use this buffer.
5d9f0de2 1273 (setq next-error-last-buffer outbuf)))
55dfd2c4 1274
c94b02d6 1275(defun compilation-set-window-height (window)
851231e9 1276 "Set the height of WINDOW according to `compilation-window-height'."
9ac57479
KS
1277 (let ((height (buffer-local-value 'compilation-window-height (window-buffer window))))
1278 (and height
0dad3dfe 1279 (window-full-width-p window)
9ac57479
KS
1280 ;; If window is alone in its frame, aside from a minibuffer,
1281 ;; don't change its height.
1282 (not (eq window (frame-root-window (window-frame window))))
b3ef54c5
DP
1283 ;; Stef said that doing the saves in this order is safer:
1284 (save-excursion
9ac57479 1285 (save-selected-window
b3ef54c5
DP
1286 (select-window window)
1287 (enlarge-window (- height (window-height))))))))
c94b02d6 1288
4282eba1
SM
1289(defvar compilation-menu-map
1290 (let ((map (make-sparse-keymap "Errors")))
1291 (define-key map [stop-subjob]
37bf89ab 1292 '("Stop Compilation" . kill-compilation))
4282eba1
SM
1293 (define-key map [compilation-mode-separator2]
1294 '("----" . nil))
9ac57479 1295 (define-key map [compilation-first-error]
4282eba1 1296 '("First Error" . first-error))
9ac57479 1297 (define-key map [compilation-previous-error]
4282eba1 1298 '("Previous Error" . previous-error))
9ac57479 1299 (define-key map [compilation-next-error]
4282eba1
SM
1300 '("Next Error" . next-error))
1301 map))
1302
0b18a8f6 1303(defvar compilation-minor-mode-map
55dfd2c4 1304 (let ((map (make-sparse-keymap)))
9da85ee5 1305 (define-key map [mouse-2] 'compile-goto-error)
0ac804df 1306 (define-key map [follow-link] 'mouse-face)
55dfd2c4 1307 (define-key map "\C-c\C-c" 'compile-goto-error)
4e7ce12e 1308 (define-key map "\C-m" 'compile-goto-error)
d3cb357b 1309 (define-key map "\C-c\C-k" 'kill-compilation)
646bd331
RM
1310 (define-key map "\M-n" 'compilation-next-error)
1311 (define-key map "\M-p" 'compilation-previous-error)
d1ed4475
RM
1312 (define-key map "\M-{" 'compilation-previous-file)
1313 (define-key map "\M-}" 'compilation-next-file)
4282eba1
SM
1314 ;; Set up the menu-bar
1315 (define-key map [menu-bar compilation]
1316 (cons "Errors" compilation-menu-map))
55dfd2c4 1317 map)
4f6e4ad6 1318 "Keymap for `compilation-minor-mode'.")
0b18a8f6 1319
a24770bc
RS
1320(defvar compilation-shell-minor-mode-map
1321 (let ((map (make-sparse-keymap)))
a24770bc
RS
1322 (define-key map "\M-\C-m" 'compile-goto-error)
1323 (define-key map "\M-\C-n" 'compilation-next-error)
1324 (define-key map "\M-\C-p" 'compilation-previous-error)
1325 (define-key map "\M-{" 'compilation-previous-file)
1326 (define-key map "\M-}" 'compilation-next-file)
1327 ;; Set up the menu-bar
4282eba1
SM
1328 (define-key map [menu-bar compilation]
1329 (cons "Errors" compilation-menu-map))
a24770bc
RS
1330 map)
1331 "Keymap for `compilation-shell-minor-mode'.")
1332
60470fd2
SM
1333(defvar compilation-button-map
1334 (let ((map (make-sparse-keymap)))
1335 (define-key map [mouse-2] 'compile-goto-error)
0ac804df 1336 (define-key map [follow-link] 'mouse-face)
60470fd2
SM
1337 (define-key map "\C-m" 'compile-goto-error)
1338 map)
1339 "Keymap for compilation-message buttons.")
1340(fset 'compilation-button-map compilation-button-map)
1341
0b18a8f6 1342(defvar compilation-mode-map
4282eba1 1343 (let ((map (make-sparse-keymap)))
261b01c6
RS
1344 ;; Don't inherit from compilation-minor-mode-map,
1345 ;; because that introduces a menu bar item we don't want.
1346 ;; That confuses C-down-mouse-3.
5988691b 1347 (define-key map [mouse-2] 'compile-goto-error)
0ac804df 1348 (define-key map [follow-link] 'mouse-face)
5988691b
RS
1349 (define-key map "\C-c\C-c" 'compile-goto-error)
1350 (define-key map "\C-m" 'compile-goto-error)
1351 (define-key map "\C-c\C-k" 'kill-compilation)
1352 (define-key map "\M-n" 'compilation-next-error)
1353 (define-key map "\M-p" 'compilation-previous-error)
1354 (define-key map "\M-{" 'compilation-previous-file)
1355 (define-key map "\M-}" 'compilation-next-file)
70fec115
JL
1356 (define-key map "\t" 'compilation-next-error)
1357 (define-key map [backtab] 'compilation-previous-error)
5988691b 1358
0b18a8f6
RM
1359 (define-key map " " 'scroll-up)
1360 (define-key map "\^?" 'scroll-down)
221206e3 1361 (define-key map "\C-c\C-f" 'next-error-follow-minor-mode)
9ac57479 1362
e60476ca 1363 ;; Set up the menu-bar
5988691b
RS
1364 (let ((submap (make-sparse-keymap "Compile")))
1365 (define-key map [menu-bar compilation]
1366 (cons "Compile" submap))
1367 (set-keymap-parent submap compilation-menu-map))
4282eba1 1368 (define-key map [menu-bar compilation compilation-separator2]
e60476ca 1369 '("----" . nil))
9ac57479 1370 (define-key map [menu-bar compilation compilation-grep]
314b410b 1371 '("Search Files (grep)..." . grep))
9ac57479 1372 (define-key map [menu-bar compilation compilation-recompile]
e60476ca 1373 '("Recompile" . recompile))
9ac57479 1374 (define-key map [menu-bar compilation compilation-compile]
32f4ab17 1375 '("Compile..." . compile))
0b18a8f6
RM
1376 map)
1377 "Keymap for compilation log buffers.
4282eba1 1378`compilation-minor-mode-map' is a parent of this.")
55dfd2c4 1379
0a35bd79
RS
1380(put 'compilation-mode 'mode-class 'special)
1381
501cf428 1382;;;###autoload
d42c87ab 1383(defun compilation-mode (&optional name-of-mode)
55dfd2c4
RS
1384 "Major mode for compilation log buffers.
1385\\<compilation-mode-map>To visit the source for a line-numbered error,
d3cb357b 1386move point to the error message line and type \\[compile-goto-error].
7c163413
RM
1387To kill the compilation, type \\[kill-compilation].
1388
2894bd96 1389Runs `compilation-mode-hook' with `run-mode-hooks' (which see).
8ff2ed52
RS
1390
1391\\{compilation-mode-map}"
55dfd2c4 1392 (interactive)
9af0d309 1393 (kill-all-local-variables)
55dfd2c4 1394 (use-local-map compilation-mode-map)
0b18a8f6 1395 (setq major-mode 'compilation-mode
d42c87ab 1396 mode-name (or name-of-mode "Compilation"))
7837c247
SM
1397 (set (make-local-variable 'page-delimiter)
1398 compilation-page-delimiter)
0b18a8f6 1399 (compilation-setup)
bd8c6db5 1400 (setq buffer-read-only t)
7837c247
SM
1401 (run-mode-hooks 'compilation-mode-hook))
1402
1403(defmacro define-compilation-mode (mode name doc &rest body)
1404 "This is like `define-derived-mode' without the PARENT argument.
1405The parent is always `compilation-mode' and the customizable `compilation-...'
bac3a1c9
JL
1406variables are also set from the name of the mode you have chosen,
1407by replacing the first word, e.g `compilation-scroll-output' from
1408`grep-scroll-output' if that variable exists."
7837c247
SM
1409 (let ((mode-name (replace-regexp-in-string "-mode\\'" "" (symbol-name mode))))
1410 `(define-derived-mode ,mode compilation-mode ,name
1411 ,doc
1412 ,@(mapcar (lambda (v)
1413 (setq v (cons v
1414 (intern-soft (replace-regexp-in-string
1415 "^compilation" mode-name
1416 (symbol-name v)))))
1417 (and (cdr v)
1418 (or (boundp (cdr v))
1419 (if (boundp 'byte-compile-bound-variables)
1420 (memq (cdr v) byte-compile-bound-variables)))
1421 `(set (make-local-variable ',(car v)) ,(cdr v))))
1422 '(compilation-buffer-name-function
1423 compilation-directory-matcher
1424 compilation-error
1425 compilation-error-regexp-alist
1426 compilation-error-regexp-alist-alist
1427 compilation-error-screen-columns
1428 compilation-finish-function
1429 compilation-finish-functions
1430 compilation-first-column
1431 compilation-mode-font-lock-keywords
1432 compilation-page-delimiter
1433 compilation-parse-errors-filename-function
1434 compilation-process-setup-function
1435 compilation-scroll-output
1436 compilation-search-path
1437 compilation-skip-threshold
1438 compilation-window-height))
1439 ,@body)))
0b18a8f6 1440
58856335 1441(defun compilation-revert-buffer (ignore-auto noconfirm)
9ed2ab9f
RS
1442 (if buffer-file-name
1443 (let (revert-buffer-function)
9ac57479 1444 (revert-buffer ignore-auto noconfirm))
9ed2ab9f 1445 (if (or noconfirm (yes-or-no-p (format "Restart compilation? ")))
7837c247
SM
1446 (apply 'compilation-start compilation-arguments))))
1447
c536bb39
SM
1448(defvar compilation-current-error nil
1449 "Marker to the location from where the next error will be found.
1450The global commands next/previous/first-error/goto-error use this.")
51c8ad03 1451
9b7b51a2
SM
1452(defvar compilation-messages-start nil
1453 "Buffer position of the beginning of the compilation messages.
1454If nil, use the beginning of buffer.")
1455
7837c247
SM
1456;; A function name can't be a hook, must be something with a value.
1457(defconst compilation-turn-on-font-lock 'turn-on-font-lock)
58856335 1458
7837c247 1459(defun compilation-setup (&optional minor)
6f5b7627
SM
1460 "Prepare the buffer for the compilation parsing commands to work.
1461Optional argument MINOR indicates this is called from
1462`compilation-minor-mode'."
51c8ad03 1463 (make-local-variable 'compilation-current-error)
9b7b51a2 1464 (make-local-variable 'compilation-messages-start)
18aac618 1465 (make-local-variable 'compilation-error-screen-columns)
b3a7f48f 1466 (make-local-variable 'overlay-arrow-position)
8b71e4d1 1467 (set (make-local-variable 'overlay-arrow-string) "")
6a1cdc5b
JL
1468 (setq next-error-overlay-arrow-position nil)
1469 (add-hook 'kill-buffer-hook
1470 (lambda () (setq next-error-overlay-arrow-position nil)) nil t)
9b7b51a2
SM
1471 ;; Note that compilation-next-error-function is for interfacing
1472 ;; with the next-error function in simple.el, and it's only
1473 ;; coincidentally named similarly to compilation-next-error.
1474 (setq next-error-function 'compilation-next-error-function)
00d6fd04
MA
1475 (set (make-local-variable 'comint-file-name-prefix)
1476 (or (file-remote-p default-directory) ""))
7837c247
SM
1477 (set (make-local-variable 'font-lock-extra-managed-props)
1478 '(directory message help-echo mouse-face debug))
1479 (set (make-local-variable 'compilation-locs)
1480 (make-hash-table :test 'equal :weakness 'value))
6f5b7627 1481 ;; lazy-lock would never find the message unless it's scrolled to.
c536bb39
SM
1482 ;; jit-lock might fontify some things too late.
1483 (set (make-local-variable 'font-lock-support-mode) nil)
7837c247 1484 (set (make-local-variable 'font-lock-maximum-size) nil)
963b2040
CY
1485 (if minor
1486 (let ((fld font-lock-defaults))
f6164cdd 1487 (font-lock-add-keywords nil (compilation-mode-font-lock-keywords))
f6164cdd
DP
1488 (if font-lock-mode
1489 (if fld
1490 (font-lock-fontify-buffer)
1491 (font-lock-change-mode)
1492 (turn-on-font-lock))
963b2040 1493 (turn-on-font-lock)))
49d11c49 1494 (setq font-lock-defaults '(compilation-mode-font-lock-keywords t))
963b2040
CY
1495 ;; maybe defer font-lock till after derived mode is set up
1496 (run-mode-hooks 'compilation-turn-on-font-lock)))
0b18a8f6 1497
7052680b 1498;;;###autoload
4282eba1 1499(define-minor-mode compilation-shell-minor-mode
7052680b
RS
1500 "Toggle compilation shell minor mode.
1501With arg, turn compilation mode on if and only if arg is positive.
4282eba1
SM
1502In this minor mode, all the error-parsing commands of the
1503Compilation major mode are available but bound to keys that don't
1504collide with Shell mode. See `compilation-mode'.
7052680b 1505Turning the mode on runs the normal hook `compilation-shell-minor-mode-hook'."
7837c247 1506 nil " Shell-Compile"
d408fed8 1507 :group 'compilation
7837c247
SM
1508 (if compilation-shell-minor-mode
1509 (compilation-setup t)
1510 (font-lock-remove-keywords nil (compilation-mode-font-lock-keywords))
1511 (font-lock-fontify-buffer)))
7052680b 1512
d6bd8dca 1513;;;###autoload
4282eba1 1514(define-minor-mode compilation-minor-mode
0b18a8f6
RM
1515 "Toggle compilation minor mode.
1516With arg, turn compilation mode on if and only if arg is positive.
4282eba1
SM
1517In this minor mode, all the error-parsing commands of the
1518Compilation major mode are available. See `compilation-mode'.
fee3f63a 1519Turning the mode on runs the normal hook `compilation-minor-mode-hook'."
7837c247 1520 nil " Compilation"
d408fed8 1521 :group 'compilation
7837c247
SM
1522 (if compilation-minor-mode
1523 (compilation-setup t)
1524 (font-lock-remove-keywords nil (compilation-mode-font-lock-keywords))
1525 (font-lock-fontify-buffer)))
55dfd2c4 1526
fd5e58d7 1527(defun compilation-handle-exit (process-status exit-status msg)
f441be5b 1528 "Write MSG in the current buffer and hack its `mode-line-process'."
d42c87ab 1529 (let ((inhibit-read-only t)
fd5e58d7
RM
1530 (status (if compilation-exit-message-function
1531 (funcall compilation-exit-message-function
1532 process-status exit-status msg)
1533 (cons msg exit-status)))
1534 (omax (point-max))
c96e025a
GM
1535 (opoint (point))
1536 (cur-buffer (current-buffer)))
b3a7f48f 1537 ;; Record where we put the message, so we can ignore it later on.
fd5e58d7
RM
1538 (goto-char omax)
1539 (insert ?\n mode-name " " (car status))
0eac1faa 1540 (if (and (numberp compilation-window-height)
7837c247
SM
1541 (zerop compilation-window-height))
1542 (message "%s" (cdr status)))
01c50447
RS
1543 (if (bolp)
1544 (forward-char -1))
fd5e58d7 1545 (insert " at " (substring (current-time-string) 0 19))
01c50447 1546 (goto-char (point-max))
47092737
RS
1547 ;; Prevent that message from being recognized as a compilation error.
1548 (add-text-properties omax (point)
1549 (append '(compilation-handle-exit t) nil))
a8bdd228
DN
1550 (setq mode-line-process
1551 (let ((out-string (format ":%s [%s]" process-status (cdr status)))
1552 (tooltip (buffer-substring-no-properties (1+ omax) (point))))
1553 (propertize
1554 out-string
1555 'help-echo tooltip
1556 'face
1557 (if (> exit-status 0) 'font-lock-warning-face 'compilation-info))))
1558 (message (format "exit status: %s %s" exit-status (> 0 exit-status)))
fd5e58d7
RM
1559 ;; Force mode line redisplay soon.
1560 (force-mode-line-update)
1561 (if (and opoint (< opoint omax))
1562 (goto-char opoint))
a5f1c37e
RS
1563 (with-no-warnings
1564 (if compilation-finish-function
c96e025a
GM
1565 (funcall compilation-finish-function cur-buffer msg)))
1566 (run-hook-with-args 'compilation-finish-functions cur-buffer msg)))
fd5e58d7 1567
55dfd2c4 1568;; Called when compilation process changes state.
55dfd2c4 1569(defun compilation-sentinel (proc msg)
d3cb357b 1570 "Sentinel for compilation buffers."
b3a7f48f
DP
1571 (if (memq (process-status proc) '(exit signal))
1572 (let ((buffer (process-buffer proc)))
1573 (if (null (buffer-name buffer))
1574 ;; buffer killed
1575 (set-process-buffer proc nil)
1576 (with-current-buffer buffer
1577 ;; Write something in the compilation buffer
1578 ;; and hack its mode line.
1579 (compilation-handle-exit (process-status proc)
1580 (process-exit-status proc)
1581 msg)
1582 ;; Since the buffer and mode line will show that the
1583 ;; process is dead, we can delete it now. Otherwise it
1584 ;; will stay around until M-x list-processes.
1585 (delete-process proc)))
1586 (setq compilation-in-progress (delq proc compilation-in-progress)))))
55dfd2c4 1587
ad62b7f1
RM
1588(defun compilation-filter (proc string)
1589 "Process filter for compilation buffers.
4f6e4ad6 1590Just inserts the text, but uses `insert-before-markers'."
991c70f8 1591 (if (buffer-name (process-buffer proc))
c536bb39
SM
1592 (with-current-buffer (process-buffer proc)
1593 (let ((inhibit-read-only t))
991c70f8
RS
1594 (save-excursion
1595 (goto-char (process-mark proc))
1596 (insert-before-markers string)
7837c247 1597 (run-hooks 'compilation-filter-hook))))))
b5bb472e 1598
5d9f0de2
KS
1599;;; test if a buffer is a compilation buffer, assuming we're in the buffer
1600(defsubst compilation-buffer-internal-p ()
1601 "Test if inside a compilation buffer."
1602 (local-variable-p 'compilation-locs))
1603
1604;;; test if a buffer is a compilation buffer, using compilation-buffer-internal-p
51ba27e7 1605(defsubst compilation-buffer-p (buffer)
5d9f0de2
KS
1606 "Test if BUFFER is a compilation buffer."
1607 (with-current-buffer buffer
1608 (compilation-buffer-internal-p)))
7837c247 1609
060d5dc1
CY
1610(defmacro compilation-loop (< property-change 1+ error limit)
1611 `(let (opt)
1612 (while (,< n 0)
1613 (setq opt pt)
1614 (or (setq pt (,property-change pt 'message))
1615 ;; Handle the case where where the first error message is
1616 ;; at the start of the buffer, and n < 0.
1617 (if (or (eq (get-text-property ,limit 'message)
1618 (get-text-property opt 'message))
1619 (eq pt opt))
1620 (error ,error compilation-error)
1621 (setq pt ,limit)))
1622 ;; prop 'message usually has 2 changes, on and off, so
1623 ;; re-search if off
1624 (or (setq msg (get-text-property pt 'message))
1625 (if (setq pt (,property-change pt 'message nil ,limit))
1626 (setq msg (get-text-property pt 'message)))
1627 (error ,error compilation-error))
1628 (or (< (cadr msg) compilation-skip-threshold)
1629 (if different-file
1630 (eq (prog1 last (setq last (nth 2 (car msg))))
1631 last))
1632 (if compilation-skip-visited
b84acff6 1633 (nthcdr 5 (car msg)))
060d5dc1
CY
1634 (if compilation-skip-to-next-location
1635 (eq (car msg) loc))
1636 ;; count this message only if none of the above are true
1637 (setq n (,1+ n))))))
7837c247 1638
51c8ad03 1639(defun compilation-next-error (n &optional different-file pt)
646bd331 1640 "Move point to the next error in the compilation buffer.
d0048c60 1641This function does NOT find the source line like \\[next-error].
851231e9
DL
1642Prefix arg N says how many error messages to move forwards (or
1643backwards, if negative).
d0048c60
EZ
1644Optional arg DIFFERENT-FILE, if non-nil, means find next error for a
1645file that is different from the current one.
1646Optional arg PT, if non-nil, specifies the value of point to start
1647looking for the next message."
646bd331
RM
1648 (interactive "p")
1649 (or (compilation-buffer-p (current-buffer))
851231e9 1650 (error "Not in a compilation buffer"))
51c8ad03 1651 (or pt (setq pt (point)))
51c8ad03 1652 (let* ((msg (get-text-property pt 'message))
250962b3 1653 ;; `loc' is used by the compilation-loop macro.
51c8ad03
DP
1654 (loc (car msg))
1655 last)
7837c247
SM
1656 (if (zerop n)
1657 (unless (or msg ; find message near here
c536bb39
SM
1658 (setq msg (get-text-property (max (1- pt) (point-min))
1659 'message)))
7837c247 1660 (setq pt (previous-single-property-change pt 'message nil
c536bb39 1661 (line-beginning-position)))
b3a7f48f 1662 (unless (setq msg (get-text-property (max (1- pt) (point-min)) 'message))
7837c247 1663 (setq pt (next-single-property-change pt 'message nil
c536bb39 1664 (line-end-position)))
b3a7f48f
DP
1665 (or (setq msg (get-text-property pt 'message))
1666 (setq pt (point)))))
7837c247 1667 (setq last (nth 2 (car msg)))
51c8ad03
DP
1668 (if (>= n 0)
1669 (compilation-loop > next-single-property-change 1-
1670 (if (get-buffer-process (current-buffer))
1671 "No more %ss yet"
060d5dc1
CY
1672 "Moved past last %s")
1673 (point-max))
c536bb39
SM
1674 ;; Don't move "back" to message at or before point.
1675 ;; Pass an explicit (point-min) to make sure pt is non-nil.
1676 (setq pt (previous-single-property-change pt 'message nil (point-min)))
51c8ad03 1677 (compilation-loop < previous-single-property-change 1+
060d5dc1 1678 "Moved back before first %s" (point-min))))
7837c247
SM
1679 (goto-char pt)
1680 (or msg
1681 (error "No %s here" compilation-error))))
646bd331
RM
1682
1683(defun compilation-previous-error (n)
1684 "Move point to the previous error in the compilation buffer.
851231e9
DL
1685Prefix arg N says how many error messages to move backwards (or
1686forwards, if negative).
7837c247 1687Does NOT find the source line like \\[previous-error]."
646bd331
RM
1688 (interactive "p")
1689 (compilation-next-error (- n)))
1690
b5bb472e 1691(defun compilation-next-file (n)
24299582
DP
1692 "Move point to the next error for a different file than the current one.
1693Prefix arg N says how many files to move forwards (or backwards, if negative)."
b5bb472e 1694 (interactive "p")
7837c247 1695 (compilation-next-error n t))
b5bb472e
RM
1696
1697(defun compilation-previous-file (n)
24299582
DP
1698 "Move point to the previous error for a different file than the current one.
1699Prefix arg N says how many files to move backwards (or forwards, if negative)."
b5bb472e
RM
1700 (interactive "p")
1701 (compilation-next-file (- n)))
1702
55dfd2c4 1703(defun kill-compilation ()
b765ba64 1704 "Kill the process made by the \\[compile] or \\[grep] commands."
55dfd2c4 1705 (interactive)
d3cb357b 1706 (let ((buffer (compilation-find-buffer)))
55dfd2c4 1707 (if (get-buffer-process buffer)
d3cb357b 1708 (interrupt-process (get-buffer-process buffer))
bac3a1c9 1709 (error "The %s process is not running" (downcase mode-name)))))
d3cb357b 1710
9da85ee5 1711(defalias 'compile-mouse-goto-error 'compile-goto-error)
c536bb39 1712
9da85ee5
SM
1713(defun compile-goto-error (&optional event)
1714 "Visit the source for the error message at point.
7837c247 1715Use this command in a compilation log buffer. Sets the mark at point there."
9da85ee5 1716 (interactive (list last-input-event))
a6cd3f65 1717 (if event (posn-set-point (event-end event)))
d3cb357b 1718 (or (compilation-buffer-p (current-buffer))
851231e9 1719 (error "Not in a compilation buffer"))
b3a7f48f
DP
1720 (if (get-text-property (point) 'directory)
1721 (dired-other-window (car (get-text-property (point) 'directory)))
1722 (push-mark)
1723 (setq compilation-current-error (point))
942c40e3 1724 (next-error-internal)))
55dfd2c4 1725
749354f0 1726(defun compilation-find-buffer (&optional avoid-current)
7d1dad0c 1727 "Return a compilation buffer.
0dad3dfe
MR
1728If AVOID-CURRENT is nil, and the current buffer is a compilation buffer,
1729return it. If AVOID-CURRENT is non-nil, return the current buffer only
1730as a last resort."
1731 (if (and (compilation-buffer-internal-p) (not avoid-current))
7d1dad0c
RS
1732 (current-buffer)
1733 (next-error-find-buffer avoid-current 'compilation-buffer-internal-p)))
d3cb357b
RM
1734
1735;;;###autoload
5d9f0de2 1736(defun compilation-next-error-function (n &optional reset)
a5f1c37e
RS
1737 "Advance to the next error message and visit the file where the error was.
1738This is the value of `next-error-function' in Compilation buffers."
7837c247 1739 (interactive "p")
5d9f0de2
KS
1740 (when reset
1741 (setq compilation-current-error nil))
7837c247 1742 (let* ((columns compilation-error-screen-columns) ; buffer's local value
b84acff6 1743 (last 1) timestamp
51c8ad03 1744 (loc (compilation-next-error (or n 1) nil
9b7b51a2
SM
1745 (or compilation-current-error
1746 compilation-messages-start
1747 (point-min))))
7837c247
SM
1748 (end-loc (nth 2 loc))
1749 (marker (point-marker)))
51c8ad03 1750 (setq compilation-current-error (point-marker)
b3a7f48f
DP
1751 overlay-arrow-position
1752 (if (bolp)
1753 compilation-current-error
9b7b51a2 1754 (copy-marker (line-beginning-position)))
51c8ad03 1755 loc (car loc))
b84acff6
SS
1756 ;; If loc contains no marker, no error in that file has been visited.
1757 ;; If the marker is invalid the buffer has been killed.
1758 ;; If the file is newer than the timestamp, it has been modified
1759 ;; (`omake -P' polls filesystem for changes and recompiles when needed
1760 ;; in the same process and buffer).
1761 ;; So, recalculate all markers for that file.
1762 (unless (and (nth 3 loc) (marker-buffer (nth 3 loc))
77041513
SM
1763 ;; There may be no timestamp info if the loc is a `fake-loc'.
1764 ;; So we skip the time-check here, although we should maybe
1765 ;; change `compilation-fake-loc' to add timestamp info.
1766 (or (null (nth 4 loc))
1767 (equal (nth 4 loc)
1768 (setq timestamp
1769 (with-current-buffer
1770 (marker-buffer (nth 3 loc))
1771 (visited-file-modtime))))))
c536bb39 1772 (with-current-buffer (compilation-find-file marker (caar (nth 2 loc))
7957baea 1773 (cadr (car (nth 2 loc))))
7837c247
SM
1774 (save-restriction
1775 (widen)
c536bb39 1776 (goto-char (point-min))
7837c247
SM
1777 ;; Treat file's found lines in forward order, 1 by 1.
1778 (dolist (line (reverse (cddr (nth 2 loc))))
1779 (when (car line) ; else this is a filename w/o a line#
1780 (beginning-of-line (- (car line) last -1))
1781 (setq last (car line)))
1782 ;; Treat line's found columns and store/update a marker for each.
1783 (dolist (col (cdr line))
1784 (if (car col)
1785 (if (eq (car col) -1) ; special case for range end
1786 (end-of-line)
9c8e6c85 1787 (compilation-move-to-column (car col) columns))
7837c247
SM
1788 (beginning-of-line)
1789 (skip-chars-forward " \t"))
eb6fb6e2 1790 (if (nth 3 col)
7837c247
SM
1791 (set-marker (nth 3 col) (point))
1792 (setcdr (nthcdr 2 col) `(,(point-marker)))))))))
1793 (compilation-goto-locus marker (nth 3 loc) (nth 3 end-loc))
b84acff6
SS
1794 (setcdr (nthcdr 3 loc) (list timestamp))
1795 (setcdr (nthcdr 4 loc) t))) ; Set this one as visited.
7837c247 1796
e3bef839
SM
1797(defvar compilation-gcpro nil
1798 "Internal variable used to keep some values from being GC'd.")
1799(make-variable-buffer-local 'compilation-gcpro)
1800
eb6fb6e2
DP
1801(defun compilation-fake-loc (marker file &optional line col)
1802 "Preassociate MARKER with FILE.
dbd97672 1803FILE should be ABSOLUTE-FILENAME or (RELATIVE-FILENAME . DIRNAME).
eb6fb6e2
DP
1804This is useful when you compile temporary files, but want
1805automatic translation of the messages to the real buffer from
1806which the temporary file came. This only works if done before a
1807message about FILE appears!
1808
1809Optional args LINE and COL default to 1 and beginning of
1810indentation respectively. The marker is expected to reflect
1811this. In the simplest case the marker points to the first line
1812of the region that was saved to the temp file.
1813
1814If you concatenate several regions into the temp file (e.g. a
1815header with variable assignments and a code region), you must
1816call this several times, once each for the last line of one
1817region and the first line of the next region."
1818 (or (consp file) (setq file (list file)))
dbd97672 1819 (setq file (compilation-get-file-structure file))
e3bef839 1820 ;; Between the current call to compilation-fake-loc and the first occurrence
77041513 1821 ;; of an error message referring to `file', the data is only kept in the
e3bef839
SM
1822 ;; weak hash-table compilation-locs, so we need to prevent this entry
1823 ;; in compilation-locs from being GC'd away. --Stef
1824 (push file compilation-gcpro)
eb6fb6e2
DP
1825 (let ((loc (compilation-assq (or line 1) (cdr file))))
1826 (setq loc (compilation-assq col loc))
1827 (if (cdr loc)
1828 (setcdr (cddr loc) (list marker))
dbd97672 1829 (setcdr loc (list line file marker)))
eb6fb6e2
DP
1830 loc))
1831
8b71e4d1
NR
1832(defcustom compilation-context-lines nil
1833 "Display this many lines of leading context before the current message.
1834If nil and the left fringe is displayed, don't scroll the
1835compilation output window; an arrow in the left fringe points to
1836the current message. If nil and there is no left fringe, the message
1837displays at the top of the window; there is no arrow."
a3a9c080 1838 :type '(choice integer (const :tag "No window scrolling" nil))
f6164cdd 1839 :group 'compilation
bf247b6e 1840 :version "22.1")
7837c247
SM
1841
1842(defsubst compilation-set-window (w mk)
6f5b7627 1843 "Align the compilation output window W with marker MK near top."
a3a9c080
JL
1844 (if (integerp compilation-context-lines)
1845 (set-window-start w (save-excursion
8b71e4d1
NR
1846 (goto-char mk)
1847 (beginning-of-line
1848 (- 1 compilation-context-lines))
1849 (point)))
3c9e7f42
RS
1850 ;; If there is no left fringe.
1851 (if (equal (car (window-fringes)) 0)
8b71e4d1
NR
1852 (set-window-start w (save-excursion
1853 (goto-char mk)
1854 (beginning-of-line 1)
1855 (point)))))
1856 (set-window-point w mk))
7837c247 1857
0ea50e43
RS
1858(defvar next-error-highlight-timer)
1859
7837c247 1860(defun compilation-goto-locus (msg mk end-mk)
6f5b7627 1861 "Jump to an error corresponding to MSG at MK.
9dc3a46a
JL
1862All arguments are markers. If END-MK is non-nil, mark is set there
1863and overlay is highlighted between MK and END-MK."
eaa3cac5 1864 ;; Show compilation buffer in other window, scrolled to this error.
e5456e73
SM
1865 (let* ((from-compilation-buffer (eq (window-buffer (selected-window))
1866 (marker-buffer msg)))
1867 ;; Use an existing window if it is in a visible frame.
7bbbd31f 1868 (pre-existing (get-buffer-window (marker-buffer msg) 0))
e5456e73
SM
1869 (w (if (and from-compilation-buffer pre-existing)
1870 ;; Calling display-buffer here may end up (partly) hiding
1871 ;; the error location if the two buffers are in two
1872 ;; different frames. So don't do it if it's not necessary.
1873 pre-existing
1874 (let ((display-buffer-reuse-frames t)
1875 (pop-up-windows t))
1876 ;; Pop up a window.
1877 (display-buffer (marker-buffer msg)))))
7837c247
SM
1878 (highlight-regexp (with-current-buffer (marker-buffer msg)
1879 ;; also do this while we change buffer
1880 (compilation-set-window w msg)
9ac57479 1881 compilation-highlight-regexp)))
7bbbd31f
SM
1882 ;; Ideally, the window-size should be passed to `display-buffer' (via
1883 ;; something like special-display-buffer) so it's only used when
1884 ;; creating a new window.
1885 (unless pre-existing (compilation-set-window-height w))
9ac57479 1886
e5456e73
SM
1887 (if from-compilation-buffer
1888 ;; If the compilation buffer window was selected,
1889 ;; keep the compilation buffer in this window;
1890 ;; display the source in another window.
1891 (let ((pop-up-windows t))
1892 (pop-to-buffer (marker-buffer mk) 'other-window))
1893 (if (window-dedicated-p (selected-window))
1894 (pop-to-buffer (marker-buffer mk))
1895 (switch-to-buffer (marker-buffer mk))))
1896 ;; If narrowing gets in the way of going to the right place, widen.
1897 (unless (eq (goto-char mk) (point))
1898 (widen)
1899 (goto-char mk))
1900 (if end-mk
1901 (push-mark end-mk t)
1902 (if mark-active (setq mark-active)))
1903 ;; If hideshow got in the way of
1904 ;; seeing the right place, open permanently.
1905 (dolist (ov (overlays-at (point)))
1906 (when (eq 'hs (overlay-get ov 'invisible))
1907 (delete-overlay ov)
1908 (goto-char mk)))
1909
9dc3a46a 1910 (when highlight-regexp
073fcaf6
JL
1911 (if (timerp next-error-highlight-timer)
1912 (cancel-timer next-error-highlight-timer))
9ac57479 1913 (unless compilation-highlight-overlay
c536bb39
SM
1914 (setq compilation-highlight-overlay
1915 (make-overlay (point-min) (point-min)))
2beb6aa0 1916 (overlay-put compilation-highlight-overlay 'face 'next-error))
7837c247 1917 (with-current-buffer (marker-buffer mk)
9ac57479 1918 (save-excursion
9dc3a46a 1919 (if end-mk (goto-char end-mk) (end-of-line))
c536bb39 1920 (let ((end (point)))
9dc3a46a 1921 (if mk (goto-char mk) (beginning-of-line))
9ac57479 1922 (if (and (stringp highlight-regexp)
7837c247 1923 (re-search-forward highlight-regexp end t))
9ac57479
KS
1924 (progn
1925 (goto-char (match-beginning 0))
2beb6aa0
JL
1926 (move-overlay compilation-highlight-overlay
1927 (match-beginning 0) (match-end 0)
1928 (current-buffer)))
1929 (move-overlay compilation-highlight-overlay
1930 (point) end (current-buffer)))
63522e3a
RS
1931 (if (or (eq next-error-highlight t)
1932 (numberp next-error-highlight))
1933 ;; We want highlighting: delete overlay on next input.
1934 (add-hook 'pre-command-hook
1935 'compilation-goto-locus-delete-o)
1936 ;; We don't want highlighting: delete overlay now.
1937 (delete-overlay compilation-highlight-overlay))
1938 ;; We want highlighting for a limited time:
1939 ;; set up a timer to delete it.
1940 (when (numberp next-error-highlight)
1941 (setq next-error-highlight-timer
1942 (run-at-time next-error-highlight nil
1943 'compilation-goto-locus-delete-o)))))))
2beb6aa0 1944 (when (and (eq next-error-highlight 'fringe-arrow))
63522e3a 1945 ;; We want a fringe arrow (instead of highlighting).
6a1cdc5b
JL
1946 (setq next-error-overlay-arrow-position
1947 (copy-marker (line-beginning-position))))))
1948
63522e3a
RS
1949(defun compilation-goto-locus-delete-o ()
1950 (delete-overlay compilation-highlight-overlay)
1951 ;; Get rid of timer and hook that would try to do this again.
1952 (if (timerp next-error-highlight-timer)
1953 (cancel-timer next-error-highlight-timer))
1954 (remove-hook 'pre-command-hook
1955 'compilation-goto-locus-delete-o))
eaa3cac5 1956\f
7957baea 1957(defun compilation-find-file (marker filename directory &rest formats)
851231e9 1958 "Find a buffer for file FILENAME.
5099d512
EZ
1959If FILENAME is not found at all, ask the user where to find it.
1960Pop up the buffer containing MARKER and scroll to MARKER if we ask
1961the user where to find the file.
ffb4b7a1
SM
1962Search the directories in `compilation-search-path'.
1963A nil in `compilation-search-path' means to try the
7957baea 1964\"current\" directory, which is passed in DIRECTORY.
f441be5b
JB
1965If DIRECTORY is relative, it is combined with `default-directory'.
1966If DIRECTORY is nil, that means use `default-directory'.
5099d512
EZ
1967FORMATS, if given, is a list of formats to reformat FILENAME when
1968looking for it: for each element FMT in FORMATS, this function
1969attempts to find a file whose name is produced by (format FMT FILENAME)."
20c1daec 1970 (or formats (setq formats '("%s")))
b8fa0ffd
SM
1971 (let ((dirs compilation-search-path)
1972 (spec-dir (if directory
1973 (expand-file-name directory)
1974 default-directory))
1975 buffer thisdir fmts name)
1976 (if (file-name-absolute-p filename)
1977 ;; The file name is absolute. Use its explicit directory as
1978 ;; the first in the search path, and strip it from FILENAME.
1979 (setq filename (abbreviate-file-name (expand-file-name filename))
1980 dirs (cons (file-name-directory filename) dirs)
1981 filename (file-name-nondirectory filename)))
1982 ;; Now search the path.
1983 (while (and dirs (null buffer))
1984 (setq thisdir (or (car dirs) spec-dir)
1985 fmts formats)
1986 ;; For each directory, try each format string.
1987 (while (and fmts (null buffer))
1988 (setq name (expand-file-name (format (car fmts) filename) thisdir)
1989 buffer (and (file-exists-p name)
1990 (find-file-noselect name))
1991 fmts (cdr fmts)))
1992 (setq dirs (cdr dirs)))
f65b9df2
SM
1993 (while (null buffer) ;Repeat until the user selects an existing file.
1994 ;; The file doesn't exist. Ask the user where to find it.
1995 (save-excursion ;This save-excursion is probably not right.
1996 (let ((pop-up-windows t))
1997 (compilation-set-window (display-buffer (marker-buffer marker))
1998 marker)
1999 (let* ((name (read-file-name
2000 (format "Find this %s in (default %s): "
2001 compilation-error filename)
44038dc8
SM
2002 spec-dir filename t nil
2003 ;; The predicate below is fine when called from
2004 ;; minibuffer-complete-and-exit, but it's too
2005 ;; restrictive otherwise, since it also prevents the
2006 ;; user from completing "fo" to "foo/" when she
2007 ;; wants to enter "foo/bar".
f441be5b 2008 ;;
44038dc8
SM
2009 ;; Try to make sure the user can only select
2010 ;; a valid answer. This predicate may be ignored,
2011 ;; tho, so we still have to double-check afterwards.
2012 ;; TODO: We should probably fix read-file-name so
2013 ;; that it never ignores this predicate, even when
2014 ;; using popup dialog boxes.
2015 ;; (lambda (name)
2016 ;; (if (file-directory-p name)
2017 ;; (setq name (expand-file-name filename name)))
2018 ;; (file-exists-p name))
2019 ))
f65b9df2
SM
2020 (origname name))
2021 (cond
2022 ((not (file-exists-p name))
2023 (message "Cannot find file `%s'" name)
2024 (ding) (sit-for 2))
2025 ((and (file-directory-p name)
2026 (not (file-exists-p
2027 (setq name (expand-file-name filename name)))))
2028 (message "No `%s' in directory %s" filename origname)
2029 (ding) (sit-for 2))
2030 (t
2031 (setq buffer (find-file-noselect name))))))))
b8fa0ffd 2032 ;; Make intangible overlays tangible.
f65b9df2
SM
2033 ;; This is weird: it's not even clear which is the current buffer,
2034 ;; so the code below can't be expected to DTRT here. -- Stef
2035 (dolist (ov (overlays-in (point-min) (point-max)))
2036 (when (overlay-get ov 'intangible)
2037 (overlay-put ov 'intangible nil)))
b8fa0ffd 2038 buffer))
721cfafe 2039
dbd97672
DP
2040(defun compilation-get-file-structure (file &optional fmt)
2041 "Retrieve FILE's file-structure or create a new one.
7957baea
RS
2042FILE should be (FILENAME) or (RELATIVE-FILENAME . DIRNAME).
2043In the former case, FILENAME may be relative or absolute.
dbd97672 2044
7957baea 2045The file-structure looks like this:
34a9bf20 2046 (list (list FILENAME [DIR-FROM-PREV-MSG]) FMT LINE-STRUCT...)"
dbd97672
DP
2047 (or (gethash file compilation-locs)
2048 ;; File was not previously encountered, at least not in the form passed.
2049 ;; Let's normalize it and look again.
2050 (let ((filename (car file))
7957baea
RS
2051 ;; Get the specified directory from FILE.
2052 (spec-directory (if (cdr file)
2053 (file-truename (cdr file)))))
dbd97672
DP
2054
2055 ;; Check for a comint-file-name-prefix and prepend it if appropriate.
2056 ;; (This is very useful for compilation-minor-mode in an rlogin-mode
2057 ;; buffer.)
7957baea
RS
2058 (when (and (boundp 'comint-file-name-prefix)
2059 (not (equal comint-file-name-prefix "")))
2060 (if (file-name-absolute-p filename)
2061 (setq filename
2062 (concat comint-file-name-prefix filename))
2063 (if spec-directory
2064 (setq spec-directory
2065 (file-truename
2066 (concat comint-file-name-prefix spec-directory))))))
dbd97672
DP
2067
2068 ;; If compilation-parse-errors-filename-function is
2069 ;; defined, use it to process the filename.
2070 (when compilation-parse-errors-filename-function
2071 (setq filename
2072 (funcall compilation-parse-errors-filename-function
2073 filename)))
2074
2075 ;; Some compilers (e.g. Sun's java compiler, reportedly) produce bogus
2076 ;; file names like "./bar//foo.c" for file "bar/foo.c";
2077 ;; expand-file-name will collapse these into "/foo.c" and fail to find
2078 ;; the appropriate file. So we look for doubled slashes in the file
2079 ;; name and fix them.
2080 (setq filename (command-line-normalize-file-name filename))
2081
dbd97672
DP
2082 ;; Store it for the possibly unnormalized name
2083 (puthash file
2084 ;; Retrieve or create file-structure for normalized name
e68b1841
GM
2085 ;; The gethash used to not use spec-directory, but
2086 ;; this leads to errors when files in different
2087 ;; directories have the same name:
2088 ;; http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00463.html
2089 (or (gethash (cons filename spec-directory) compilation-locs)
34a9bf20 2090 (puthash (cons filename spec-directory)
7957baea
RS
2091 (list (list filename spec-directory) fmt)
2092 compilation-locs))
dbd97672 2093 compilation-locs))))
51501e54 2094
7837c247 2095(add-to-list 'debug-ignored-errors "^No more [-a-z ]+s yet$")
f1ed9461 2096
c536bb39
SM
2097;;; Compatibility with the old compile.el.
2098
2099(defun compile-buffer-substring (n) (if n (match-string n)))
2100
2101(defun compilation-compat-error-properties (err)
6f5b7627 2102 "Map old-style error ERR to new-style message."
efb0e677
SM
2103 ;; Old-style structure is (MARKER (FILE DIR) LINE COL) or
2104 ;; (MARKER . MARKER).
2105 (let ((dst (cdr err)))
2106 (if (markerp dst)
2107 ;; Must start with a face, for font-lock.
2108 `(face nil
2109 message ,(list (list nil nil nil dst) 2)
2110 help-echo "mouse-2: visit the source location"
2111 keymap compilation-button-map
2112 mouse-face highlight)
2113 ;; Too difficult to do it by hand: dispatch to the normal code.
2114 (let* ((file (pop dst))
2115 (line (pop dst))
2116 (col (pop dst))
2117 (filename (pop file))
2118 (dirname (pop file))
2119 (fmt (pop file)))
2120 (compilation-internal-error-properties
2121 (cons filename dirname) line nil col nil 2 fmt)))))
c536bb39
SM
2122
2123(defun compilation-compat-parse-errors (limit)
2124 (when compilation-parse-errors-function
2125 ;; FIXME: We should remove the rest of the compilation keywords
2126 ;; but we can't do that from here because font-lock is using
2127 ;; the value right now. --stef
2128 (save-excursion
2129 (setq compilation-error-list nil)
2130 ;; Reset compilation-parsing-end each time because font-lock
2131 ;; might force us the re-parse many times (typically because
2132 ;; some code adds some text-property to the output that we
2133 ;; already parsed). You might say "why reparse", well:
2134 ;; because font-lock has just removed the `message' property so
2135 ;; have to do it all over again.
2136 (if compilation-parsing-end
2137 (set-marker compilation-parsing-end (point))
2138 (setq compilation-parsing-end (point-marker)))
2139 (condition-case nil
2140 ;; Ignore any error: we're calling this function earlier than
2141 ;; in the old compile.el so things might not all be setup yet.
2142 (funcall compilation-parse-errors-function limit nil)
2143 (error nil))
2144 (dolist (err (if (listp compilation-error-list) compilation-error-list))
2145 (let* ((src (car err))
2146 (dst (cdr err))
2147 (loc (cond ((markerp dst) (list nil nil nil dst))
2148 ((consp dst)
2149 (list (nth 2 dst) (nth 1 dst)
2150 (cons (cdar dst) (caar dst)))))))
2151 (when loc
2152 (goto-char src)
2153 ;; (put-text-property src (line-end-position) 'font-lock-face 'font-lock-warning-face)
2154 (put-text-property src (line-end-position)
2155 'message (list loc 2)))))))
2156 (goto-char limit)
2157 nil)
2158
9b7b51a2 2159;; Beware: this is not only compatiblity code. New code stil uses it. --Stef
c536bb39
SM
2160(defun compilation-forget-errors ()
2161 ;; In case we hit the same file/line specs, we want to recompute a new
2162 ;; marker for them, so flush our cache.
b3a7f48f 2163 (setq compilation-locs (make-hash-table :test 'equal :weakness 'value))
e3bef839 2164 (setq compilation-gcpro nil)
c536bb39
SM
2165 ;; FIXME: the old code reset the directory-stack, so maybe we should
2166 ;; put a `directory change' marker of some sort, but where? -stef
b3a7f48f 2167 ;;
c536bb39
SM
2168 ;; FIXME: The old code moved compilation-current-error (which was
2169 ;; virtually represented by a mix of compilation-parsing-end and
2170 ;; compilation-error-list) to point-min, but that was only meaningful for
2171 ;; the internal uses of compilation-forget-errors: all calls from external
2172 ;; packages seem to be followed by a move of compilation-parsing-end to
b956d64f 2173 ;; something equivalent to point-max. So we heuristically move
c536bb39 2174 ;; compilation-current-error to point-max (since the external package
b956d64f 2175 ;; won't know that it should do it). --Stef
9b7b51a2
SM
2176 (setq compilation-current-error nil)
2177 (let* ((proc (get-buffer-process (current-buffer)))
2178 (mark (if proc (process-mark proc)))
2179 (pos (or mark (point-max))))
2180 (setq compilation-messages-start
2181 ;; In the future, ignore the text already present in the buffer.
2182 ;; Since many process filter functions insert before markers,
2183 ;; we need to put ours just before the insertion point rather
2184 ;; than at the insertion point. If that's not possible, then
2185 ;; don't use a marker. --Stef
b956d64f
SM
2186 (if (> pos (point-min)) (copy-marker (1- pos)) pos)))
2187 ;; Again, since this command is used in buffers that contain several
2188 ;; compilations, to set the beginning of "this compilation", it's a good
2189 ;; place to reset compilation-auto-jump-to-next.
2190 (set (make-local-variable 'compilation-auto-jump-to-next)
78dc87a2
JL
2191 (or compilation-auto-jump-to-first-error
2192 (eq compilation-scroll-output 'first-error))))
c536bb39 2193
0c9a01ff
MY
2194;;;###autoload
2195(add-to-list 'auto-mode-alist '("\\.gcov\\'" . compilation-mode))
2196
4746118a 2197(provide 'compile)
fad160d5 2198
822001db 2199;; arch-tag: 12465727-7382-4f72-b234-79855a00dd8c
fad160d5 2200;;; compile.el ends here