(decode_any_window): New function.
[bpt/emacs.git] / lisp / progmodes / compile.el
CommitLineData
55535639 1;;; compile.el --- run compiler as inferior of Emacs, parse error messages
fad160d5 2
812fade8 3;; Copyright (C) 1985, 86, 87, 93, 94, 95, 96, 97, 98, 1999, 2001, 2003
e335e194 4;; Free Software Foundation, Inc.
3a801d0c 5
c4d7c00a 6;; Author: Roland McGrath <roland@gnu.org>
d1c7011d 7;; Maintainer: FSF
e9571d2a 8;; Keywords: tools, processes
d1c7011d 9
55dfd2c4
RS
10;; This file is part of GNU Emacs.
11
29add8b9
RM
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 2, or (at your option)
15;; any later version.
16
55dfd2c4 17;; GNU Emacs is distributed in the hope that it will be useful,
29add8b9
RM
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
b578f267
EN
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25;; Boston, MA 02111-1307, USA.
55dfd2c4 26
5cc57841
ER
27;;; Commentary:
28
29;; This package provides the compile and grep facilities documented in
30;; the Emacs user's manual.
31
d1c7011d
ER
32;;; Code:
33
c5049fa0
RS
34(defgroup compilation nil
35 "Run compiler as inferior of Emacs, parse error messages."
36 :group 'tools
37 :group 'processes)
38
39
7c163413 40;;;###autoload
c5049fa0
RS
41(defcustom compilation-mode-hook nil
42 "*List of hook functions run by `compilation-mode' (see `run-hooks')."
43 :type 'hook
44 :group 'compilation)
7c163413
RM
45
46;;;###autoload
c5049fa0
RS
47(defcustom compilation-window-height nil
48 "*Number of lines in a compilation window. If nil, use Emacs default."
49 :type '(choice (const :tag "Default" nil)
50 integer)
51 :group 'compilation)
d3cb357b 52
59bb4146 53(defcustom compile-auto-highlight nil
37f5f978 54 "*Specify how many compiler errors to highlight (and parse) initially.
a24770bc 55\(Highlighting applies to an error message when the mouse is over it.)
37f5f978
RS
56If this is a number N, all compiler error messages in the first N lines
57are highlighted and parsed as soon as they arrive in Emacs.
01e6e8c9 58If t, highlight and parse the whole compilation output as soon as it arrives.
37f5f978
RS
59If nil, don't highlight or parse any of the buffer until you try to
60move to the error messages.
61
01e6e8c9 62Those messages which are not parsed and highlighted initially
4315d68e
RS
63will be parsed and highlighted as soon as you try to move to them."
64 :type '(choice (const :tag "All" t)
65 (const :tag "None" nil)
66 (integer :tag "First N lines"))
67 :group 'compilation)
37f5f978 68
55dfd2c4
RS
69(defvar compilation-error-list nil
70 "List of error message descriptors for visiting erring functions.
fc0094d7
RM
71Each error descriptor is a cons (or nil). Its car is a marker pointing to
72an error message. If its cdr is a marker, it points to the text of the
533304f8
RM
73line the message is about. If its cdr is a cons, it is a list
74\(\(DIRECTORY . FILE\) LINE [COLUMN]\). Or its cdr may be nil if that
75error is not interesting.
d3cb357b
RM
76
77The value may be t instead of a list; this means that the buffer of
29478cc5
JB
78error messages should be reparsed the next time the list of errors is wanted.
79
80Some other commands (like `diff') use this list to control the error
9c09c008 81message tracking facilities; if you change its structure, you should make
29478cc5
JB
82sure you also change those packages. Perhaps it is better not to change
83it at all.")
55dfd2c4
RS
84
85(defvar compilation-old-error-list nil
86 "Value of `compilation-error-list' after errors were parsed.")
87
501cf428 88(defvar compilation-parse-errors-function 'compilation-parse-errors
b2bb6ec8 89 "Function to call to parse error messages from a compilation.
c540863c
RM
90It takes args LIMIT-SEARCH and FIND-AT-LEAST.
91If LIMIT-SEARCH is non-nil, don't bother parsing past that location.
501cf428 92If FIND-AT-LEAST is non-nil, don't bother parsing after finding that
e78e10ca 93many new errors.
d3cb357b
RM
94It should read in the source files which have errors and set
95`compilation-error-list' to a list with an element for each error message
96found. See that variable for more info.")
55dfd2c4 97
e335e194
GM
98(defvar compilation-parse-errors-filename-function nil
99 "Function to call to post-process filenames while parsing error messages.
100It takes one arg FILENAME which is the name of a file as found
101in the compilation output, and should return a transformed file name.")
102
49683a13
EZ
103;;;###autoload
104(defvar compilation-process-setup-function nil
105 "*Function to call to customize the compilation process.
106This functions is called immediately before the compilation process is
107started. It can be used to set any variables or functions that are used
9ac57479
KS
108while processing the output of the compilation process. The function
109is called with variables `compilation-buffer' and `compilation-window'
110bound to the compilation buffer and window, respectively.")
49683a13 111
aa228418 112;;;###autoload
d3cb357b 113(defvar compilation-buffer-name-function nil
6c43f2f9
RS
114 "Function to compute the name of a compilation buffer.
115The function receives one argument, the name of the major mode of the
116compilation buffer. It should return a string.
117nil means compute the name with `(concat \"*\" (downcase major-mode) \"*\")'.")
55dfd2c4 118
aa228418 119;;;###autoload
d3cb357b 120(defvar compilation-finish-function nil
c5049fa0 121 "Function to call when a compilation process finishes.
d3cb357b
RM
122It is called with two arguments: the compilation buffer, and a string
123describing how the process finished.")
55dfd2c4 124
4cc36b17
RS
125;;;###autoload
126(defvar compilation-finish-functions nil
c5049fa0 127 "Functions to call when a compilation process finishes.
4cc36b17
RS
128Each function is called with two arguments: the compilation buffer,
129and a string describing how the process finished.")
130
d3cb357b 131(defvar compilation-last-buffer nil
6c43f2f9
RS
132 "The most recent compilation buffer.
133A buffer becomes most recent when its compilation is started
134or when it is used with \\[next-error] or \\[compile-goto-error].")
55dfd2c4 135
ebff767c
RM
136(defvar compilation-in-progress nil
137 "List of compilation processes now running.")
138(or (assq 'compilation-in-progress minor-mode-alist)
139 (setq minor-mode-alist (cons '(compilation-in-progress " Compiling")
140 minor-mode-alist)))
141
d3cb357b 142(defvar compilation-parsing-end nil
c4d7c00a 143 "Marker position of end of buffer when last error messages were parsed.")
d3cb357b
RM
144
145(defvar compilation-error-message "No more errors"
6c43f2f9
RS
146 "Message to print when no more matches are found.")
147
58856335
RS
148(defvar compilation-arguments nil
149 "Arguments that were given to `compile-internal'.")
150
6c43f2f9 151(defvar compilation-num-errors-found)
d3cb357b
RM
152
153(defvar compilation-error-regexp-alist
154 '(
d86bdede 155 ;; NOTE! See also grep-regexp-alist, below.
7ee790ac 156
d3cb357b 157 ;; 4.3BSD grep, cc, lint pass 1:
7ee790ac
RM
158 ;; /usr/src/foo/foo.c(8): warning: w may be used before set
159 ;; or GNU utilities:
160 ;; foo.c:8: error message
161 ;; or HP-UX 7.0 fc:
162 ;; foo.f :16 some horrible error message
08b1edf4
RM
163 ;; or GNU utilities with column (GNAT 1.82):
164 ;; foo.adb:2:1: Unit name does not match file name
9b7dee86
DL
165 ;; or with column and program name:
166 ;; jade:dbcommon.dsl:133:17:E: missing argument for function call
501cf428 167 ;;
57c8389c
JB
168 ;; We'll insist that the number be followed by a colon or closing
169 ;; paren, because otherwise this matches just about anything
170 ;; containing a number with spaces around it.
d914bed5
KH
171
172 ;; We insist on a non-digit in the file name
173 ;; so that we don't mistake the file name for a command name
174 ;; and take the line number as the file name.
a0730fbd 175 ("\\([a-zA-Z][-a-zA-Z._0-9]+: ?\\)?\
d914bed5 176\\([a-zA-Z]?:?[^:( \t\n]*[^:( \t\n0-9][^:( \t\n]*\\)[:(][ \t]*\\([0-9]+\\)\
e228c7e2 177\\([) \t]\\|:\\(\\([0-9]+:\\)\\|[0-9]*[^:0-9]\\)\\)" 2 3 6)
69d4d27f 178
fb49f36a
RS
179 ;; GNU utilities with precise locations (line and columns),
180 ;; possibly ranges:
181 ;; foo.c:8.23-9.1: error message
ccc41714 182 ("\\([a-zA-Z][-a-zA-Z._0-9]+\\): ?\
fb49f36a
RS
183\\([0-9]+\\)\\.\\([0-9]+\\)\
184-\\([0-9]+\\)\\.\\([0-9]+\\)\
185:" 1 2 3) ;; When ending points are supported, add line = 4 and col = 5.
186 ;; foo.c:8.23-45: error message
ccc41714 187 ("\\([a-zA-Z][-a-zA-Z._0-9]+\\): ?\
fb49f36a
RS
188\\([0-9]+\\)\\.\\([0-9]+\\)\
189-\\([0-9]+\\)\
190:" 1 2 3) ;; When ending points are supported, add line = 2 and col = 4.
191 ;; foo.c:8-45.3: error message
ccc41714 192 ("\\([a-zA-Z][-a-zA-Z._0-9]+\\): ?\
fb49f36a
RS
193\\([0-9]+\\)\
194-\\([0-9]+\\)\\.\\([0-9]+\\)\
195:" 1 2 nil) ;; When ending points are supported, add line = 2 and col = 4.
196 ;; foo.c:8.23: error message
ccc41714 197 ("\\([a-zA-Z][-a-zA-Z._0-9]+\\): ?\
fb49f36a
RS
198\\([0-9]+\\)\\.\\([0-9]+\\)\
199:" 1 2 3)
200 ;; foo.c:8-23: error message
ccc41714 201 ("\\([a-zA-Z][-a-zA-Z._0-9]+\\): ?\
fb49f36a
RS
202\\([0-9]+\\)\
203-\\([0-9]+\\)\
204:" 1 2 nil);; When ending points are supported, add line = 3.
205
0c43cc89
RS
206 ;; Microsoft C/C++:
207 ;; keyboard.c(537) : warning C4005: 'min' : macro redefinition
208 ;; d:\tmp\test.c(23) : error C2143: syntax error : missing ';' before 'if'
9cab952d
RS
209 ;; This used to be less selective and allow characters other than
210 ;; parens around the line number, but that caused confusion for
211 ;; GNU-style error messages.
d2705a18 212 ;; This used to reject spaces and dashes in file names,
717149ec 213 ;; but they are valid now; so I made it more strict about the error
d2705a18
RS
214 ;; message that follows.
215 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\)) \
216: \\(error\\|warning\\) C[0-9]+:" 1 3)
0c43cc89 217
717149ec 218 ;; Borland C++, C++Builder:
69d4d27f
RS
219 ;; Error ping.c 15: Unable to open include file 'sys/types.h'
220 ;; Warning ping.c 68: Call to function 'func' with no prototype
717149ec
KH
221 ;; Error E2010 ping.c 15: Unable to open include file 'sys/types.h'
222 ;; Warning W1022 ping.c 68: Call to function 'func' with no prototype
223 ("\\(Error\\|Warning\\) \\(\\([FEW][0-9]+\\) \\)?\
224\\([a-zA-Z]?:?[^:( \t\n]+\\)\
225 \\([0-9]+\\)\\([) \t]\\|:[^0-9\n]\\)" 4 5)
7ee790ac 226
56ce04a3
RS
227 ;; Valgrind (memory debugger for x86 GNU/Linux):
228 ;; ==1332== at 0x8008621: main (vtest.c:180)
229 ;; Currently this regexp only matches the first error.
230 ;; Thanks to Hans Petter Jansson <hpj@ximian.com> for his regexp wisdom.
231 ("^==[0-9]+==[^(]+\(([^:]+):([0-9]+)" 1 2)
232
d3cb357b 233 ;; 4.3BSD lint pass 2
7ee790ac 234 ;; strcmp: variable # of args. llib-lc(359) :: /usr/src/foo/foo.c(8)
2fa55e20 235 (".*[ \t:]\\([a-zA-Z]?:?[^:( \t\n]+\\)[:(](+[ \t]*\\([0-9]+\\))[:) \t]*$"
3e8258e7 236 1 2)
7ee790ac 237
d3cb357b 238 ;; 4.3BSD lint pass 3
7ee790ac 239 ;; bloofle defined( /users/wolfgang/foo.c(4) ), but never used
daa37602 240 ;; This used to be
3e8258e7 241 ;; ("[ \t(]+\\([a-zA-Z]?:?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]+" 1 2)
daa37602 242 ;; which is regexp Impressionism - it matches almost anything!
2fa55e20 243 (".*([ \t]*\\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\))" 1 2)
7ee790ac 244
369d3cdc
RM
245 ;; MIPS lint pass<n>; looks good for SunPro lint also
246 ;; TrimMask (255) in solomon.c may be indistinguishable from TrimMasks (93) in solomon.c due to truncation
2fa55e20 247 ("[^\n ]+ (\\([0-9]+\\)) in \\([^ \n]+\\)" 2 1)
369d3cdc 248 ;; name defined but never used: LinInt in cmap_calc.c(199)
2fa55e20 249 (".*in \\([^(\n]+\\)(\\([0-9]+\\))$" 1 2)
369d3cdc 250
ff735134 251 ;; Ultrix 3.0 f77:
f89fdaed 252 ;; fort: Severe: addstf.f, line 82: Missing operator or delimiter symbol
984ae4ed
RM
253 ;; Some SGI cc version:
254 ;; cfe: Warning 835: foo.c, line 2: something
2fa55e20 255 ("\\(cfe\\|fort\\): [^:\n]*: \\([^ \n]*\\), line \\([0-9]+\\):" 2 3)
501cf428 256 ;; Error on line 3 of t.f: Execution error unclassifiable statement
ff735134 257 ;; Unknown who does this:
9c09c008 258 ;; Line 45 of "foo.c": bloofle undefined
5ff3b093
RM
259 ;; Absoft FORTRAN 77 Compiler 3.1.3
260 ;; error on line 19 of fplot.f: spelling error?
261 ;; warning on line 17 of fplot.f: data type is undefined for variable d
a24770bc 262 ("\\(.* on \\)?[Ll]ine[ \t]+\\([0-9]+\\)[ \t]+\
3e8258e7 263of[ \t]+\"?\\([a-zA-Z]?:?[^\":\n]+\\)\"?:" 3 2)
7ee790ac
RM
264
265 ;; Apollo cc, 4.3BSD fc:
266 ;; "foo.f", line 3: Error: syntax error near end of statement
6d68d793
RS
267 ;; IBM RS6000:
268 ;; "vvouch.c", line 19.5: 1506-046 (S) Syntax error.
a0c567b5
RM
269 ;; Microtec mcc68k:
270 ;; "foo.c", line 32 pos 1; (E) syntax error; unexpected symbol: "lossage"
501cf428 271 ;; GNAT (as of July 94):
37fd2208 272 ;; "foo.adb", line 2(11): warning: file name does not match ...
b1707ae4
RM
273 ;; IBM AIX xlc compiler:
274 ;; "src/swapping.c", line 30.34: 1506-342 (W) "/*" detected in comment.
2fa55e20 275 (".*\"\\([^,\" \n\t]+\\)\", lines? \
b1707ae4 276\\([0-9]+\\)\\([\(.]\\([0-9]+\\)\)?\\)?[:., (-]" 1 2 4)
18710add 277
154b2b10
RS
278 ;; Python:
279 ;; File "foobar.py", line 5, blah blah
280 ("^File \"\\([^,\" \n\t]+\\)\", line \\([0-9]+\\)," 1 2)
281
96db060f
RS
282 ;; Caml compiler:
283 ;; File "foobar.ml", lines 5-8, characters 20-155: blah blah
284 ("^File \"\\([^,\" \n\t]+\\)\", lines? \\([0-9]+\\)[-0-9]*, characters? \\([0-9]+\\)" 1 2 3)
285
18710add 286 ;; MIPS RISC CC - the one distributed with Ultrix:
7ee790ac 287 ;; ccom: Error: foo.c, line 2: syntax error
dfb89664 288 ;; DEC AXP OSF/1 cc
501cf428 289 ;; /usr/lib/cmplrs/cc/cfe: Error: foo.c: 1: blah blah
4d60e093 290 ("[a-z0-9/]+: \\([eE]rror\\|[wW]arning\\): \\([^,\" \n\t]+\\)[,:] \\(line \\)?\\([0-9]+\\):" 2 4)
7ee790ac
RM
291
292 ;; IBM AIX PS/2 C version 1.1:
293 ;; ****** Error number 140 in line 8 of file errors.c ******
2fa55e20 294 (".*in line \\([0-9]+\\) of file \\([^ \n]+[^. \n]\\)\\.? " 2 1)
d3cb357b
RM
295 ;; IBM AIX lint is too painful to do right this way. File name
296 ;; prefixes entire sections rather than being on each line.
57c8389c 297
a24770bc
RS
298 ;; SPARCcompiler Pascal:
299 ;; 20 linjer : array[1..4] of linje;
300 ;; e 18480-----------^--- Inserted ';'
301 ;; and
302 ;; E 18520 line 61 - 0 is undefined
303 ;; These messages don't contain a file name. Instead the compiler gives
304 ;; a message whenever the file being compiled is changed.
305 (" +\\([0-9]+\\) +.*\n[ew] [0-9]+-+" nil 1)
306 ("[Ew] +[0-9]+ line \\([0-9]+\\) - " nil 1)
307
72244bea
RM
308 ;; Lucid Compiler, lcc 3.x
309 ;; E, file.cc(35,52) Illegal operation on pointers
2fa55e20 310 ("[EW], \\([^(\n]*\\)(\\([0-9]+\\),[ \t]*\\([0-9]+\\)" 1 2 3)
72244bea 311
5b6858da
SM
312 ;; This seems to be superfluous because the first pattern matches it.
313 ;; ;; GNU messages with program name and optional column number.
314 ;; ("[a-zA-Z]?:?[^0-9 \n\t:]+[^ \n\t:]*:[ \t]*\\([^ \n\t:]+\\):\
315 ;;\\([0-9]+\\):\\(\\([0-9]+\\)[: \t]\\)?" 1 2 4)
4dbf8a2c
RM
316
317 ;; Cray C compiler error messages
a24770bc
RS
318 ("\\(cc\\| cft\\)-[0-9]+ c\\(c\\|f77\\): ERROR \\([^,\n]+, \\)* File = \
319\\([^,\n]+\\), Line = \\([0-9]+\\)" 4 5)
4dbf8a2c
RM
320
321 ;; IBM C/C++ Tools 2.01:
322 ;; foo.c(2:0) : informational EDC0804: Function foo is not referenced.
323 ;; foo.c(3:8) : warning EDC0833: Implicit return statement encountered.
324 ;; foo.c(5:5) : error EDC0350: Syntax error.
2fa55e20 325 ("\\([^( \n\t]+\\)(\\([0-9]+\\):\\([0-9]+\\)) : " 1 2 3)
cfda7e6d 326
b1b56e01
RS
327 ;; IAR Systems C Compiler:
328 ;; "foo.c",3 Error[32]: Error message
329 ;; "foo.c",3 Warning[32]: Error message
330 ("\"\\(.*\\)\",\\([0-9]+\\)\\s-+\\(Error\\|Warning\\)\\[[0-9]+\\]:" 1 2)
331
cfda7e6d
RM
332 ;; Sun ada (VADS, Solaris):
333 ;; /home3/xdhar/rcds_rc/main.a, line 361, char 6:syntax error: "," inserted
2fa55e20 334 ("\\([^, \n\t]+\\), line \\([0-9]+\\), char \\([0-9]+\\)[:., \(-]" 1 2 3)
cbf7f476
RS
335
336 ;; Perl -w:
337 ;; syntax error at automake line 922, near "':'"
3d8be0c9
RS
338 ;; Perl debugging traces
339 ;; store::odrecall('File_A', 'x2') called at store.pm line 90
340 (".* at \\([^ \n]+\\) line \\([0-9]+\\)[,.\n]" 1 2)
7ce22b41
RS
341
342 ;; Oracle pro*c:
343 ;; Semantic error at line 528, column 5, file erosacqdb.pc:
344 ("Semantic error at line \\([0-9]+\\), column \\([0-9]+\\), file \\(.*\\):"
345 3 1 2)
5dfa3d35
RS
346
347 ;; EPC F90 compiler:
348 ;; Error 24 at (2:progran.f90) : syntax error
349 ("Error [0-9]+ at (\\([0-9]*\\):\\([^)\n]+\\))" 2 1)
c7c5bbc0 350
04f12e57 351 ;; SGI IRIX MipsPro 7.3 compilers:
e3c0f9de
RS
352 ;; cc-1070 cc: ERROR File = linkl.c, Line = 38
353 (".*: ERROR File = \\(.+\\), Line = \\([0-9]+\\)" 1 2)
354 (".*: WARNING File = \\(.+\\), Line = \\([0-9]+\\)" 1 2)
355
c7c5bbc0 356 ;; Sun F90 error messages:
851231e9 357 ;; cf90-113 f90comp: ERROR NSE, File = Hoved.f90, Line = 16, Column = 3
c7c5bbc0
KH
358 (".* ERROR [a-zA-Z0-9 ]+, File = \\(.+\\), Line = \\([0-9]+\\), Column = \\([0-9]+\\)"
359 1 2 3)
c299126f
MB
360
361 ;; RXP - GPL XML validator at http://www.cogsci.ed.ac.uk/~richard/rxp.html:
362 ;; Error: Mismatched end tag: expected </geroup>, got </group>
363 ;; in unnamed entity at line 71 char 8 of file:///home/reto/test/group.xml
364 ("Error:.*\n.* line \\([0-9]+\\) char \\([0-9]+\\) of file://\\(.+\\)"
365 3 1 2)
366 ;; Warning: Start tag for undeclared element geroup
367 ;; in unnamed entity at line 4 char 8 of file:///home/reto/test/group.xml
368 ("Warning:.*\n.* line \\([0-9]+\\) char \\([0-9]+\\) of file://\\(.+\\)"
369 3 1 2)
e1f540f2
AS
370
371 ;; See http://ant.apache.org/faq.html
372 ;; Ant Java: works for jikes
373 ("^\\s-*\\[[^]]*\\]\\s-*\\(.+\\):\\([0-9]+\\):\\([0-9]+\\):[0-9]+:[0-9]+:" 1 2 3)
374
375 ;; Ant Java: works for javac
376 ("^\\s-*\\[[^]]*\\]\\s-*\\(.+\\):\\([0-9]+\\):" 1 2)
377
d3cb357b 378 )
40d63d1f 379
6c43f2f9 380 "Alist that specifies how to match errors in compiler output.
20c1daec 381Each elt has the form (REGEXP FILE-IDX LINE-IDX [COLUMN-IDX FILE-FORMAT...])
72244bea
RM
382If REGEXP matches, the FILE-IDX'th subexpression gives the file name, and
383the LINE-IDX'th subexpression gives the line number. If COLUMN-IDX is
20c1daec
RM
384given, the COLUMN-IDX'th subexpression gives the column number on that line.
385If any FILE-FORMAT is given, each is a format string to produce a file name to
386try; %s in the string is replaced by the text matching the FILE-IDX'th
387subexpression.")
55dfd2c4 388
a24770bc
RS
389(defvar compilation-enter-directory-regexp-alist
390 '(
391 ;; Matches lines printed by the `-w' option of GNU Make.
392 (".*: Entering directory `\\(.*\\)'$" 1)
3b5fb045
RS
393 ;; Matches lines made by Emacs byte compiler.
394 ("^Entering directory `\\(.*\\)'$" 1)
a24770bc
RS
395 )
396 "Alist specifying how to match lines that indicate a new current directory.
397Note that the match is done at the beginning of lines.
398Each elt has the form (REGEXP IDX).
399If REGEXP matches, the IDX'th subexpression gives the directory name.
400
401The default value matches lines printed by the `-w' option of GNU Make.")
402
403(defvar compilation-leave-directory-regexp-alist
404 '(
405 ;; Matches lines printed by the `-w' option of GNU Make.
406 (".*: Leaving directory `\\(.*\\)'$" 1)
3b5fb045
RS
407 ;; Matches lines made by Emacs byte compiler.
408 ("^Leaving directory `\\(.*\\)'$" 1)
a24770bc
RS
409 )
410"Alist specifying how to match lines that indicate restoring current directory.
411Note that the match is done at the beginning of lines.
412Each elt has the form (REGEXP IDX).
413If REGEXP matches, the IDX'th subexpression gives the name of the directory
851231e9 414being moved from. If IDX is nil, the last directory entered \(by a line
a24770bc
RS
415matching `compilation-enter-directory-regexp-alist'\) is assumed.
416
417The default value matches lines printed by the `-w' option of GNU Make.")
418
419(defvar compilation-file-regexp-alist
420 '(
421 ;; This matches entries with date time year file-name: like
422 ;; Thu May 14 10:46:12 1992 mom3.p:
423 ("\\w\\w\\w \\w\\w\\w +[0-9]+ [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9][0-9][0-9] \\(.*\\):$" 1)
424 )
425 "Alist specifying how to match lines that indicate a new current file.
426Note that the match is done at the beginning of lines.
427Each elt has the form (REGEXP IDX).
851231e9 428If REGEXP matches, the IDX'th subexpression gives the file name. This is
a24770bc
RS
429used with compilers that don't indicate file name in every error message.")
430
431;; There is no generally useful regexp that will match non messages, but
432;; in special cases there might be one. The lines that are not matched by
433;; a regexp take much longer time than the ones that are recognized so if
434;; you have same regexeps here, parsing is faster.
435(defvar compilation-nomessage-regexp-alist
436 '(
437 )
438 "Alist specifying how to match lines that have no message.
439Note that the match is done at the beginning of lines.
851231e9 440Each elt has the form (REGEXP). This alist is by default empty, but if
a24770bc
RS
441you have some good regexps here, the parsing of messages will be faster.")
442
9ac57479
KS
443(defvar compilation-highlight-regexp t
444 "Regexp matching part of visited source lines to highlight temporarily.
445Highlight entire line if t; don't highlight source lines if nil.")
446
447(defvar compilation-highlight-overlay nil
448 "Overlay used to temporarily highlight compilation matches.")
449
f8e03ecb
AS
450(defcustom compilation-error-screen-columns t
451 "*If non-nil, column numbers in error messages are screen columns.
452Otherwise they are interpreted as character positions, with
453each character occupying one column.
454The default is to use screen columns, which requires that the compilation
455program and Emacs agree about the display width of the characters,
456especially the TAB character."
457 :type 'boolean
458 :group 'compilation
459 :version "20.4")
460
c5049fa0 461(defcustom compilation-read-command t
851231e9
DL
462 "*Non-nil means \\[compile] reads the compilation command to use.
463Otherwise, \\[compile] just uses the value of `compile-command'."
c5049fa0
RS
464 :type 'boolean
465 :group 'compilation)
90016295 466
e83be080 467;;;###autoload
c5049fa0 468(defcustom compilation-ask-about-save t
851231e9 469 "*Non-nil means \\[compile] asks which buffers to save before compiling.
c5049fa0
RS
470Otherwise, it saves all modified buffers without asking."
471 :type 'boolean
472 :group 'compilation)
90016295 473
7c163413 474;;;###autoload
c5049fa0 475(defcustom compilation-search-path '(nil)
7c163413 476 "*List of directories to search for source files named in error messages.
d3cb357b 477Elements should be directory names, not file names of directories.
c5049fa0
RS
478nil as an element means to try the default directory."
479 :type '(repeat (choice (const :tag "Default" nil)
480 (string :tag "Directory")))
481 :group 'compilation)
55dfd2c4 482
c5049fa0
RS
483(defcustom compile-command "make -k "
484 "*Last shell command used to do a compilation; default for next compilation.
55dfd2c4
RS
485
486Sometimes it is useful for files to supply local values for this variable.
487You might also use mode hooks to specify it in certain modes, like this:
488
61c4aaf8 489 (add-hook 'c-mode-hook
61c4aaf8
RS
490 (lambda ()
491 (unless (or (file-exists-p \"makefile\")
492 (file-exists-p \"Makefile\"))
5b6858da
SM
493 (set (make-local-variable 'compile-command)
494 (concat \"make -k \"
495 (file-name-sans-extension buffer-file-name))))))"
c5049fa0
RS
496 :type 'string
497 :group 'compilation)
55dfd2c4 498
d3cb357b 499(defvar compilation-directory-stack nil
6c43f2f9 500 "Stack of previous directories for `compilation-leave-directory-regexp'.
aa53db6a 501The last element is the directory the compilation was started in.")
d3cb357b 502
01f89d11
RM
503(defvar compilation-exit-message-function nil "\
504If non-nil, called when a compilation process dies to return a status message.
fd5e58d7
RM
505This should be a function of three arguments: process status, exit status,
506and exit message; it returns a cons (MESSAGE . MODELINE) of the strings to
507write into the compilation buffer, and to put in its mode line.")
01f89d11 508
c20f961b
JPW
509(defvar compilation-environment nil
510 "*List of environment variables for compilation to inherit.
511Each element should be a string of the form ENVVARNAME=VALUE.
512This list is temporarily prepended to `process-environment' prior to
513starting the compilation process.")
514
770970cb
RS
515;; History of compile commands.
516(defvar compile-history nil)
770970cb 517
e93b2a55
SM
518(defun compilation-mode-font-lock-keywords ()
519 "Return expressions to highlight in Compilation mode."
520 (nconc
521 ;;
522 ;; Compiler warning/error lines.
5dfa3d35
RS
523 (mapcar (function
524 (lambda (item)
525 ;; Prepend "^", adjusting FILE-IDX and LINE-IDX accordingly.
22052f1d
KH
526 (let ((file-idx (nth 1 item))
527 (line-idx (nth 2 item))
528 (col-idx (nth 3 item))
529 keyword)
530 (when (numberp col-idx)
531 (setq keyword
532 (cons (list (1+ col-idx) 'font-lock-type-face nil t)
533 keyword)))
5dfa3d35
RS
534 (when (numberp line-idx)
535 (setq keyword
536 (cons (list (1+ line-idx) 'font-lock-variable-name-face)
537 keyword)))
538 (when (numberp file-idx)
539 (setq keyword
540 (cons (list (1+ file-idx) 'font-lock-warning-face)
541 keyword)))
542 (cons (concat "^\\(" (nth 0 item) "\\)") keyword))))
e93b2a55
SM
543 compilation-error-regexp-alist)
544 (list
545 ;;
a24770bc 546 ;; Compiler output lines. Recognize `make[n]:' lines too.
e93b2a55
SM
547 '("^\\([A-Za-z_0-9/\.+-]+\\)\\(\\[\\([0-9]+\\)\\]\\)?[ \t]*:"
548 (1 font-lock-function-name-face) (3 font-lock-comment-face nil t)))
549 ))
01f89d11 550\f
d3cb357b 551;;;###autoload
55dfd2c4
RS
552(defun compile (command)
553 "Compile the program including the current buffer. Default: run `make'.
554Runs COMMAND, a shell command, in a separate process asynchronously
555with output going to the buffer `*compilation*'.
d3cb357b 556
55dfd2c4
RS
557You can then use the command \\[next-error] to find the next error message
558and move to the source code that caused it.
559
08b1edf4
RM
560Interactively, prompts for the command if `compilation-read-command' is
561non-nil; otherwise uses `compile-command'. With prefix arg, always prompts.
562
c72095b3
RS
563To run more than one compilation at once, start one and rename
564the \`*compilation*' buffer to some other name with
565\\[rename-buffer]. Then start the next one. On most systems,
566termination of the main compilation process kills its
567subprocesses.
d3cb357b
RM
568
569The name used for the buffer is actually whatever is returned by
570the function in `compilation-buffer-name-function', so you can set that
571to a function that generates a unique name."
90016295 572 (interactive
08b1edf4 573 (if (or compilation-read-command current-prefix-arg)
90016295 574 (list (read-from-minibuffer "Compile command: "
5b6858da 575 (eval compile-command) nil nil
90016295 576 '(compile-history . 1)))
5b6858da
SM
577 (list (eval compile-command))))
578 (unless (equal command (eval compile-command))
579 (setq compile-command command))
90016295 580 (save-some-buffers (not compilation-ask-about-save) nil)
5b6858da 581 (compile-internal command "No more errors"))
55dfd2c4 582
5b6858da 583;; run compile with the default command line
e60476ca 584(defun recompile ()
6867356a
RS
585 "Re-compile the program including the current buffer.
586If this is run in a compilation-mode buffer, re-use the arguments from the
587original use. Otherwise, it recompiles using `compile-command'."
e60476ca
RS
588 (interactive)
589 (save-some-buffers (not compilation-ask-about-save) nil)
6867356a
RS
590 (apply 'compile-internal (or compilation-arguments
591 `(,(eval compile-command) "No more errors"))))
09843b4a 592
b5812513
DL
593(defcustom compilation-scroll-output nil
594 "*Non-nil to scroll the *compilation* buffer window as output appears.
595
596Setting it causes the compilation-mode commands to put point at the
597end of their output window so that the end of the output is always
598visible rather than the begining."
599 :type 'boolean
a46fddeb 600 :version "20.3"
b5812513
DL
601 :group 'compilation)
602
7bdb67b2
GM
603
604(defun compilation-buffer-name (mode-name name-function)
605 "Return the name of a compilation buffer to use.
606If NAME-FUNCTION is non-nil, call it with one argument MODE-NAME
607to determine the buffer name.
608Likewise if `compilation-buffer-name-function' is non-nil.
609If current buffer is in Compilation mode for the same mode name
610return the name of the current buffer, so that it gets reused.
611Otherwise, construct a buffer name from MODE-NAME."
dc2feacf 612 (cond (name-function
7bdb67b2 613 (funcall name-function mode-name))
dc2feacf 614 (compilation-buffer-name-function
7bdb67b2
GM
615 (funcall compilation-buffer-name-function mode-name))
616 ((and (eq major-mode 'compilation-mode)
617 (equal mode-name (nth 2 compilation-arguments)))
618 (buffer-name))
619 (t
620 (concat "*" (downcase mode-name) "*"))))
621
622
55dfd2c4 623(defun compile-internal (command error-message
a24770bc
RS
624 &optional name-of-mode parser
625 error-regexp-alist name-function
626 enter-regexp-alist leave-regexp-alist
23b0c5fc 627 file-regexp-alist nomessage-regexp-alist
9ac57479 628 no-async highlight-regexp local-map)
55dfd2c4
RS
629 "Run compilation command COMMAND (low level interface).
630ERROR-MESSAGE is a string to print if the user asks to see another error
9ac57479
KS
631and there are no more errors.
632
633The rest of the arguments are optional; for them, nil means use the default.
634
a24770bc 635NAME-OF-MODE is the name to display as the major mode in the compilation
9ac57479
KS
636buffer.
637
638PARSER is the error parser function.
639ERROR-REGEXP-ALIST is the error message regexp alist to use.
640NAME-FUNCTION is a function called to name the buffer.
641ENTER-REGEXP-ALIST is the enter directory message regexp alist to use.
a24770bc
RS
642LEAVE-REGEXP-ALIST is the leave directory message regexp alist to use.
643FILE-REGEXP-ALIST is the change current file message regexp alist to use.
644NOMESSAGE-REGEXP-ALIST is the nomessage regexp alist to use.
645 The defaults for these variables are the global values of
646\`compilation-parse-errors-function', `compilation-error-regexp-alist',
647\`compilation-buffer-name-function', `compilation-enter-directory-regexp-alist',
648\`compilation-leave-directory-regexp-alist', `compilation-file-regexp-alist',
649\ and `compilation-nomessage-regexp-alist', respectively.
08815e25 650For arg 7-10 a value t means an empty alist.
646bd331 651
23b0c5fc
CW
652If NO-ASYNC is non-nil, start the compilation process synchronously.
653
9ac57479
KS
654If HIGHLIGHT-REGEXP is non-nil, `next-error' will temporarily highlight
655matching section of the visited source line; the default is to use the
656global value of `compilation-highlight-regexp'.
657
658If LOCAL-MAP is non-nil, use the given keymap instead of `compilation-mode-map'.
659
646bd331 660Returns the compilation buffer created."
23b0c5fc
CW
661 (unless no-async
662 (setq no-async (not (fboundp 'start-process))))
d3cb357b 663 (let (outbuf)
55dfd2c4 664 (save-excursion
d3cb357b
RM
665 (or name-of-mode
666 (setq name-of-mode "Compilation"))
667 (setq outbuf
7bdb67b2
GM
668 (get-buffer-create (compilation-buffer-name name-of-mode
669 name-function)))
d3cb357b
RM
670 (set-buffer outbuf)
671 (let ((comp-proc (get-buffer-process (current-buffer))))
672 (if comp-proc
673 (if (or (not (eq (process-status comp-proc) 'run))
674 (yes-or-no-p
bea1d57a
JB
675 (format "A %s process is running; kill it? "
676 name-of-mode)))
d3cb357b
RM
677 (condition-case ()
678 (progn
679 (interrupt-process comp-proc)
680 (sit-for 1)
681 (delete-process comp-proc))
682 (error nil))
683 (error "Cannot have two processes in `%s' at once"
684 (buffer-name))
685 )))
686 ;; In case the compilation buffer is current, make sure we get the global
687 ;; values of compilation-error-regexp-alist, etc.
688 (kill-all-local-variables))
a24770bc
RS
689 (or error-regexp-alist
690 (setq error-regexp-alist compilation-error-regexp-alist))
691 (or enter-regexp-alist
692 (setq enter-regexp-alist compilation-enter-directory-regexp-alist))
693 (or leave-regexp-alist
694 (setq leave-regexp-alist compilation-leave-directory-regexp-alist))
695 (or file-regexp-alist
696 (setq file-regexp-alist compilation-file-regexp-alist))
697 (or nomessage-regexp-alist
698 (setq nomessage-regexp-alist compilation-nomessage-regexp-alist))
9ac57479
KS
699 (or highlight-regexp
700 (setq highlight-regexp compilation-highlight-regexp))
a24770bc
RS
701 (or parser (setq parser compilation-parse-errors-function))
702 (let ((thisdir default-directory)
501cf428 703 outwin)
d3cb357b
RM
704 (save-excursion
705 ;; Clear out the compilation buffer and make it writable.
706 ;; Change its default-directory to the directory where the compilation
707 ;; will happen, and insert a `cd' command to indicate this.
708 (set-buffer outbuf)
709 (setq buffer-read-only nil)
5d5ab7ac 710 (buffer-disable-undo (current-buffer))
d3cb357b 711 (erase-buffer)
5d5ab7ac 712 (buffer-enable-undo (current-buffer))
d3cb357b
RM
713 (setq default-directory thisdir)
714 (insert "cd " thisdir "\n" command "\n")
715 (set-buffer-modified-p nil))
716 ;; If we're already in the compilation buffer, go to the end
717 ;; of the buffer, so point will track the compilation output.
718 (if (eq outbuf (current-buffer))
719 (goto-char (point-max)))
720 ;; Pop up the compilation buffer.
92d8844e 721 (setq outwin (display-buffer outbuf nil t))
812fade8 722 (with-current-buffer outbuf
aa53db6a 723 (compilation-mode name-of-mode)
9ac57479
KS
724 (if local-map
725 (use-local-map local-map))
5b6858da
SM
726 ;; In what way is it non-ergonomic ? -stef
727 ;; (toggle-read-only 1) ;;; Non-ergonomic.
d4ece679
RS
728 (set (make-local-variable 'compilation-parse-errors-function) parser)
729 (set (make-local-variable 'compilation-error-message) error-message)
a24770bc
RS
730 (set (make-local-variable 'compilation-error-regexp-alist)
731 error-regexp-alist)
732 (set (make-local-variable 'compilation-enter-directory-regexp-alist)
733 enter-regexp-alist)
734 (set (make-local-variable 'compilation-leave-directory-regexp-alist)
735 leave-regexp-alist)
736 (set (make-local-variable 'compilation-file-regexp-alist)
737 file-regexp-alist)
738 (set (make-local-variable 'compilation-nomessage-regexp-alist)
739 nomessage-regexp-alist)
9ac57479
KS
740 (set (make-local-variable 'compilation-highlight-regexp)
741 highlight-regexp)
58856335
RS
742 (set (make-local-variable 'compilation-arguments)
743 (list command error-message
744 name-of-mode parser
745 error-regexp-alist name-function
746 enter-regexp-alist leave-regexp-alist
9ac57479
KS
747 file-regexp-alist nomessage-regexp-alist
748 nil ; or no-async ??
749 highlight-regexp local-map))
b5812513
DL
750 ;; This proves a good idea if the buffer's going to scroll
751 ;; with lazy-lock on.
32d6fffb 752 (set (make-local-variable 'lazy-lock-defer-on-scrolling) t)
d4ece679
RS
753 (setq default-directory thisdir
754 compilation-directory-stack (list default-directory))
755 (set-window-start outwin (point-min))
d4ece679
RS
756 (or (eq outwin (selected-window))
757 (set-window-point outwin (point-min)))
9ac57479
KS
758 ;; The setup function is called before compilation-set-window-height
759 ;; so it can set the compilation-window-height buffer locally.
49683a13
EZ
760 (if compilation-process-setup-function
761 (funcall compilation-process-setup-function))
9ac57479 762 (compilation-set-window-height outwin)
d4ece679 763 ;; Start the compilation.
23b0c5fc 764 (if (not no-async)
75da8302
RS
765 (let* ((process-environment
766 (append
c20f961b 767 compilation-environment
75da8302
RS
768 (if (and (boundp 'system-uses-terminfo)
769 system-uses-terminfo)
770 (list "TERM=dumb" "TERMCAP="
771 (format "COLUMNS=%d" (window-width)))
772 (list "TERM=emacs"
773 (format "TERMCAP=emacs:co#%d:tc=unknown:"
774 (window-width))))
775 ;; Set the EMACS variable, but
776 ;; don't override users' setting of $EMACS.
777 (if (getenv "EMACS")
778 process-environment
779 (cons "EMACS=t" process-environment))))
2bb240a9
RS
780 (proc (start-process-shell-command (downcase mode-name)
781 outbuf
782 command)))
35f7e85b
RS
783 (set-process-sentinel proc 'compilation-sentinel)
784 (set-process-filter proc 'compilation-filter)
785 (set-marker (process-mark proc) (point) outbuf)
501cf428 786 (setq compilation-in-progress
35f7e85b 787 (cons proc compilation-in-progress)))
fd5e58d7
RM
788 ;; No asynchronous processes available.
789 (message "Executing `%s'..." command)
d3e56891
RS
790 ;; Fake modeline display as if `start-process' were run.
791 (setq mode-line-process ":run")
fd5e58d7
RM
792 (force-mode-line-update)
793 (sit-for 0) ; Force redisplay
35f7e85b 794 (let ((status (call-process shell-file-name nil outbuf nil "-c"
fd5e58d7
RM
795 command)))
796 (cond ((numberp status)
797 (compilation-handle-exit 'exit status
798 (if (zerop status)
799 "finished\n"
800 (format "\
801exited abnormally with code %d\n"
802 status))))
803 ((stringp status)
804 (compilation-handle-exit 'signal status
805 (concat status "\n")))
806 (t
807 (compilation-handle-exit 'bizarre status status))))
b5812513 808 (message "Executing `%s'...done" command)))
9ac57479 809 (if (buffer-local-value 'compilation-scroll-output outbuf)
bccc6e3d 810 (save-selected-window
9ac57479
KS
811 (select-window outwin)
812 (goto-char (point-max)))))
d3cb357b
RM
813 ;; Make it so the next C-x ` will use this buffer.
814 (setq compilation-last-buffer outbuf)))
55dfd2c4 815
c94b02d6 816(defun compilation-set-window-height (window)
851231e9 817 "Set the height of WINDOW according to `compilation-window-height'."
9ac57479
KS
818 (let ((height (buffer-local-value 'compilation-window-height (window-buffer window))))
819 (and height
820 (= (window-width window) (frame-width (window-frame window)))
821 ;; If window is alone in its frame, aside from a minibuffer,
822 ;; don't change its height.
823 (not (eq window (frame-root-window (window-frame window))))
824 ;; This save-current-buffer prevents us from changing the current
825 ;; buffer, which might not be the same as the selected window's buffer.
826 (save-current-buffer
827 (save-selected-window
828 (select-window window)
829 (enlarge-window (- height (window-height))))))))
c94b02d6 830
4282eba1
SM
831(defvar compilation-menu-map
832 (let ((map (make-sparse-keymap "Errors")))
833 (define-key map [stop-subjob]
37bf89ab 834 '("Stop Compilation" . kill-compilation))
4282eba1
SM
835 (define-key map [compilation-mode-separator2]
836 '("----" . nil))
9ac57479 837 (define-key map [compilation-first-error]
4282eba1 838 '("First Error" . first-error))
9ac57479 839 (define-key map [compilation-previous-error]
4282eba1 840 '("Previous Error" . previous-error))
9ac57479 841 (define-key map [compilation-next-error]
4282eba1
SM
842 '("Next Error" . next-error))
843 map))
844
0b18a8f6 845(defvar compilation-minor-mode-map
55dfd2c4 846 (let ((map (make-sparse-keymap)))
f98494a4 847 (define-key map [mouse-2] 'compile-mouse-goto-error)
55dfd2c4 848 (define-key map "\C-c\C-c" 'compile-goto-error)
4e7ce12e 849 (define-key map "\C-m" 'compile-goto-error)
d3cb357b 850 (define-key map "\C-c\C-k" 'kill-compilation)
646bd331
RM
851 (define-key map "\M-n" 'compilation-next-error)
852 (define-key map "\M-p" 'compilation-previous-error)
d1ed4475
RM
853 (define-key map "\M-{" 'compilation-previous-file)
854 (define-key map "\M-}" 'compilation-next-file)
4282eba1
SM
855 ;; Set up the menu-bar
856 (define-key map [menu-bar compilation]
857 (cons "Errors" compilation-menu-map))
55dfd2c4 858 map)
4f6e4ad6 859 "Keymap for `compilation-minor-mode'.")
0b18a8f6 860
a24770bc
RS
861(defvar compilation-shell-minor-mode-map
862 (let ((map (make-sparse-keymap)))
863 (define-key map [mouse-2] 'compile-mouse-goto-error)
864 (define-key map "\M-\C-m" 'compile-goto-error)
865 (define-key map "\M-\C-n" 'compilation-next-error)
866 (define-key map "\M-\C-p" 'compilation-previous-error)
867 (define-key map "\M-{" 'compilation-previous-file)
868 (define-key map "\M-}" 'compilation-next-file)
869 ;; Set up the menu-bar
4282eba1
SM
870 (define-key map [menu-bar compilation]
871 (cons "Errors" compilation-menu-map))
a24770bc
RS
872 map)
873 "Keymap for `compilation-shell-minor-mode'.")
874
0b18a8f6 875(defvar compilation-mode-map
4282eba1
SM
876 (let ((map (make-sparse-keymap)))
877 (set-keymap-parent map compilation-minor-mode-map)
0b18a8f6
RM
878 (define-key map " " 'scroll-up)
879 (define-key map "\^?" 'scroll-down)
9ac57479 880
e60476ca 881 ;; Set up the menu-bar
4282eba1 882 (define-key map [menu-bar compilation]
e60476ca 883 (cons "Compile" (make-sparse-keymap "Compile")))
4282eba1 884 (define-key map [menu-bar compilation compilation-separator2]
e60476ca 885 '("----" . nil))
9ac57479 886 (define-key map [menu-bar compilation compilation-grep]
7933678b 887 '("Search Files (grep)" . grep))
9ac57479 888 (define-key map [menu-bar compilation compilation-recompile]
e60476ca 889 '("Recompile" . recompile))
9ac57479 890 (define-key map [menu-bar compilation compilation-compile]
32f4ab17 891 '("Compile..." . compile))
0b18a8f6
RM
892 map)
893 "Keymap for compilation log buffers.
4282eba1 894`compilation-minor-mode-map' is a parent of this.")
55dfd2c4 895
0a35bd79
RS
896(put 'compilation-mode 'mode-class 'special)
897
501cf428 898;;;###autoload
aa53db6a 899(defun compilation-mode (&optional name-of-mode)
55dfd2c4
RS
900 "Major mode for compilation log buffers.
901\\<compilation-mode-map>To visit the source for a line-numbered error,
d3cb357b 902move point to the error message line and type \\[compile-goto-error].
7c163413
RM
903To kill the compilation, type \\[kill-compilation].
904
905Runs `compilation-mode-hook' with `run-hooks' (which see)."
55dfd2c4 906 (interactive)
9af0d309 907 (kill-all-local-variables)
55dfd2c4 908 (use-local-map compilation-mode-map)
0b18a8f6 909 (setq major-mode 'compilation-mode
aa53db6a 910 mode-name (or name-of-mode "Compilation"))
0b18a8f6 911 (compilation-setup)
63a2daf6
SM
912 (set (make-local-variable 'font-lock-defaults)
913 '(compilation-mode-font-lock-keywords t))
58856335
RS
914 (set (make-local-variable 'revert-buffer-function)
915 'compilation-revert-buffer)
0b18a8f6
RM
916 (run-hooks 'compilation-mode-hook))
917
58856335 918(defun compilation-revert-buffer (ignore-auto noconfirm)
9ed2ab9f
RS
919 (if buffer-file-name
920 (let (revert-buffer-function)
9ac57479 921 (revert-buffer ignore-auto noconfirm))
9ed2ab9f
RS
922 (if (or noconfirm (yes-or-no-p (format "Restart compilation? ")))
923 (apply 'compile-internal compilation-arguments))))
58856335 924
0b18a8f6 925(defun compilation-setup ()
851231e9 926 "Prepare the buffer for the compilation parsing commands to work."
0b18a8f6 927 ;; Make the buffer's mode line show process state.
ebf0b439 928 (setq mode-line-process '(":%s"))
d3cb357b
RM
929 (set (make-local-variable 'compilation-error-list) nil)
930 (set (make-local-variable 'compilation-old-error-list) nil)
c4d7c00a 931 (set (make-local-variable 'compilation-parsing-end) (copy-marker 1))
474d71ae
RS
932 (set (make-local-variable 'compilation-directory-stack)
933 (list default-directory))
18aac618 934 (make-local-variable 'compilation-error-screen-columns)
0b18a8f6
RM
935 (setq compilation-last-buffer (current-buffer)))
936
7052680b 937;;;###autoload
4282eba1 938(define-minor-mode compilation-shell-minor-mode
7052680b
RS
939 "Toggle compilation shell minor mode.
940With arg, turn compilation mode on if and only if arg is positive.
4282eba1
SM
941In this minor mode, all the error-parsing commands of the
942Compilation major mode are available but bound to keys that don't
943collide with Shell mode. See `compilation-mode'.
7052680b 944Turning the mode on runs the normal hook `compilation-shell-minor-mode-hook'."
4282eba1 945 nil " Shell-Compile" nil
d408fed8 946 :group 'compilation
4282eba1
SM
947 (let (mode-line-process)
948 (compilation-setup)))
7052680b 949
d6bd8dca 950;;;###autoload
4282eba1 951(define-minor-mode compilation-minor-mode
0b18a8f6
RM
952 "Toggle compilation minor mode.
953With arg, turn compilation mode on if and only if arg is positive.
4282eba1
SM
954In this minor mode, all the error-parsing commands of the
955Compilation major mode are available. See `compilation-mode'.
fee3f63a 956Turning the mode on runs the normal hook `compilation-minor-mode-hook'."
4282eba1 957 nil " Compilation" nil
d408fed8 958 :group 'compilation
4282eba1
SM
959 (let ((mode-line-process))
960 (compilation-setup)))
55dfd2c4 961
fd5e58d7 962(defun compilation-handle-exit (process-status exit-status msg)
851231e9 963 "Write msg in the current buffer and hack its mode-line-process."
fd5e58d7
RM
964 (let ((buffer-read-only nil)
965 (status (if compilation-exit-message-function
966 (funcall compilation-exit-message-function
967 process-status exit-status msg)
968 (cons msg exit-status)))
969 (omax (point-max))
970 (opoint (point)))
971 ;; Record where we put the message, so we can ignore it
972 ;; later on.
973 (goto-char omax)
974 (insert ?\n mode-name " " (car status))
0eac1faa
RS
975 (if (and (numberp compilation-window-height)
976 (zerop compilation-window-height))
977 (message "%s" (cdr status)))
01c50447
RS
978 (if (bolp)
979 (forward-char -1))
fd5e58d7 980 (insert " at " (substring (current-time-string) 0 19))
01c50447 981 (goto-char (point-max))
47092737
RS
982 ;; Prevent that message from being recognized as a compilation error.
983 (add-text-properties omax (point)
984 (append '(compilation-handle-exit t) nil))
52f84622 985 (setq mode-line-process (format ":%s [%s]" process-status (cdr status)))
fd5e58d7
RM
986 ;; Force mode line redisplay soon.
987 (force-mode-line-update)
988 (if (and opoint (< opoint omax))
989 (goto-char opoint))
01e6e8c9
RS
990 ;; Automatically parse (and mouse-highlight) error messages:
991 (cond ((eq compile-auto-highlight t)
37f5f978 992 (compile-reinitialize-errors nil (point-max)))
01e6e8c9
RS
993 ((numberp compile-auto-highlight)
994 (compile-reinitialize-errors nil
995 (save-excursion
996 (goto-line compile-auto-highlight)
997 (point)))))
fd5e58d7 998 (if compilation-finish-function
4cc36b17
RS
999 (funcall compilation-finish-function (current-buffer) msg))
1000 (let ((functions compilation-finish-functions))
1001 (while functions
1002 (funcall (car functions) (current-buffer) msg)
1003 (setq functions (cdr functions))))))
fd5e58d7 1004
55dfd2c4 1005;; Called when compilation process changes state.
55dfd2c4 1006(defun compilation-sentinel (proc msg)
d3cb357b
RM
1007 "Sentinel for compilation buffers."
1008 (let ((buffer (process-buffer proc)))
ebff767c
RM
1009 (if (memq (process-status proc) '(signal exit))
1010 (progn
1011 (if (null (buffer-name buffer))
1012 ;; buffer killed
1013 (set-process-buffer proc nil)
fd5e58d7 1014 (let ((obuf (current-buffer)))
ebff767c
RM
1015 ;; save-excursion isn't the right thing if
1016 ;; process-buffer is current-buffer
1017 (unwind-protect
1018 (progn
1019 ;; Write something in the compilation buffer
1020 ;; and hack its mode line.
1021 (set-buffer buffer)
fd5e58d7
RM
1022 (compilation-handle-exit (process-status proc)
1023 (process-exit-status proc)
1024 msg)
1025 ;; Since the buffer and mode line will show that the
1026 ;; process is dead, we can delete it now. Otherwise it
1027 ;; will stay around until M-x list-processes.
1028 (delete-process proc))
6c43f2f9 1029 (set-buffer obuf))))
ebff767c
RM
1030 (setq compilation-in-progress (delq proc compilation-in-progress))
1031 ))))
55dfd2c4 1032
ad62b7f1
RM
1033(defun compilation-filter (proc string)
1034 "Process filter for compilation buffers.
4f6e4ad6 1035Just inserts the text, but uses `insert-before-markers'."
991c70f8 1036 (if (buffer-name (process-buffer proc))
ad62b7f1 1037 (save-excursion
991c70f8 1038 (set-buffer (process-buffer proc))
c4d7c00a
RS
1039 (let ((buffer-read-only nil)
1040 (end (marker-position compilation-parsing-end)))
991c70f8
RS
1041 (save-excursion
1042 (goto-char (process-mark proc))
1043 (insert-before-markers string)
c4d7c00a 1044 (set-marker compilation-parsing-end end) ;don't move it
d89793b7 1045 (run-hooks 'compilation-filter-hook)
c4d7c00a
RS
1046 ;; this seems redundant since we insert-before-marks -stefan
1047 ;;(set-marker (process-mark proc) (point))
1048 )))))
ad62b7f1 1049
b5bb472e 1050(defun compile-error-at-point ()
851231e9 1051 "Return the cdr of `compilation-old-error-list' for error containing point."
b5bb472e
RM
1052 (compile-reinitialize-errors nil (point))
1053 (let ((errors compilation-old-error-list))
1054 (while (and errors
1055 (> (point) (car (car errors))))
1056 (setq errors (cdr errors)))
1057 errors))
1058
51ba27e7 1059(defsubst compilation-buffer-p (buffer)
20c1daec
RM
1060 (save-excursion
1061 (set-buffer buffer)
a24770bc
RS
1062 (or compilation-shell-minor-mode compilation-minor-mode
1063 (eq major-mode 'compilation-mode))))
51ba27e7 1064
646bd331
RM
1065(defun compilation-next-error (n)
1066 "Move point to the next error in the compilation buffer.
851231e9
DL
1067Prefix arg N says how many error messages to move forwards (or
1068backwards, if negative).
646bd331
RM
1069Does NOT find the source line like \\[next-error]."
1070 (interactive "p")
1071 (or (compilation-buffer-p (current-buffer))
851231e9 1072 (error "Not in a compilation buffer"))
646bd331 1073 (setq compilation-last-buffer (current-buffer))
b5bb472e
RM
1074
1075 (let ((errors (compile-error-at-point)))
1076
1077 ;; Move to the error after the one containing point.
1078 (goto-char (car (if (< n 0)
1079 (let ((i 0)
1080 (e compilation-old-error-list))
1081 ;; See how many cdrs away ERRORS is from the start.
1082 (while (not (eq e errors))
1083 (setq i (1+ i)
1084 e (cdr e)))
1085 (if (> (- n) i)
1086 (error "Moved back past first error")
1087 (nth (+ i n) compilation-old-error-list)))
83b5c8e0 1088 (save-excursion
c72095b3 1089 (while (and (> n 0) errors)
83b5c8e0 1090 ;; Discard the current error and any previous.
c72095b3 1091 (while (and errors (>= (point) (car (car errors))))
83b5c8e0
RS
1092 (setq errors (cdr errors)))
1093 ;; Now (car errors) is the next error.
1094 ;; If we want to move down more errors,
1095 ;; put point at this one and start again.
1096 (setq n (1- n))
1097 (if (and errors (> n 0))
1098 (goto-char (car (car errors))))))
1099 (let ((compilation-error-list errors))
b5bb472e
RM
1100 (compile-reinitialize-errors nil nil n)
1101 (if compilation-error-list
1102 (nth (1- n) compilation-error-list)
1103 (error "Moved past last error"))))))))
646bd331
RM
1104
1105(defun compilation-previous-error (n)
1106 "Move point to the previous error in the compilation buffer.
851231e9
DL
1107Prefix arg N says how many error messages to move backwards (or
1108forwards, if negative).
646bd331
RM
1109Does NOT find the source line like \\[next-error]."
1110 (interactive "p")
1111 (compilation-next-error (- n)))
1112
9ac57479
KS
1113(defun next-error-no-select (n)
1114 "Move point to the next error in the compilation buffer and highlight match.
1115Prefix arg N says how many error messages to move forwards.
1116Finds and highlights the source line like \\[next-error], but does not
1117select the source buffer."
1118 (interactive "p")
1119 (next-error n)
1120 (pop-to-buffer compilation-last-buffer))
1121
1122(defun previous-error-no-select (n)
1123 "Move point to the previous error in the compilation buffer and highlight match.
1124Prefix arg N says how many error messages to move forwards.
1125Finds and highlights the source line like \\[next-error], but does not
1126select the source buffer."
1127 (interactive "p")
1128 (next-error (- n))
1129 (pop-to-buffer compilation-last-buffer))
646bd331 1130
fc0094d7
RM
1131;; Given an elt of `compilation-error-list', return an object representing
1132;; the referenced file which is equal to (but not necessarily eq to) what
1133;; this function would return for another error in the same file.
1134(defsubst compilation-error-filedata (data)
b5bb472e
RM
1135 (setq data (cdr data))
1136 (if (markerp data)
fc0094d7 1137 (marker-buffer data)
aead2f9f 1138 (car data)))
b5bb472e 1139
fc0094d7
RM
1140;; Return a string describing a value from compilation-error-filedata.
1141;; This value is not necessarily useful as a file name, but should be
1142;; indicative to the user of what file's errors are being referred to.
1143(defsubst compilation-error-filedata-file-name (filedata)
1144 (if (bufferp filedata)
1145 (buffer-file-name filedata)
1146 (car filedata)))
1147
b5bb472e
RM
1148(defun compilation-next-file (n)
1149 "Move point to the next error for a different file than the current one."
1150 (interactive "p")
1151 (or (compilation-buffer-p (current-buffer))
851231e9 1152 (error "Not in a compilation buffer"))
b5bb472e
RM
1153 (setq compilation-last-buffer (current-buffer))
1154
1155 (let ((reversed (< n 0))
fc0094d7 1156 errors filedata)
b5bb472e
RM
1157
1158 (if (not reversed)
1159 (setq errors (or (compile-error-at-point)
1160 (error "Moved past last error")))
1161
1162 ;; Get a reversed list of the errors up through the one containing point.
1163 (compile-reinitialize-errors nil (point))
1164 (setq errors (reverse compilation-old-error-list)
1165 n (- n))
1166
1167 ;; Ignore errors after point. (car ERRORS) will be the error
1168 ;; containing point, (cadr ERRORS) the one before it.
1169 (while (and errors
1170 (< (point) (car (car errors))))
1171 (setq errors (cdr errors))))
1172
1173 (while (> n 0)
fc0094d7
RM
1174 (setq filedata (compilation-error-filedata (car errors)))
1175
1176 ;; Skip past the following errors for this file.
1177 (while (equal filedata
1178 (compilation-error-filedata
1179 (car (or errors
1180 (if reversed
1181 (error "%s the first erring file"
1182 (compilation-error-filedata-file-name
1183 filedata))
1184 (let ((compilation-error-list nil))
1185 ;; Parse some more.
1186 (compile-reinitialize-errors nil nil 2)
1187 (setq errors compilation-error-list)))
501cf428 1188 (error "%s is the last erring file"
fc0094d7
RM
1189 (compilation-error-filedata-file-name
1190 filedata))))))
b5bb472e
RM
1191 (setq errors (cdr errors)))
1192
1193 (setq n (1- n)))
1194
1195 ;; Move to the following error.
1196 (goto-char (car (car (or errors
1197 (if reversed
1198 (error "This is the first erring file")
1199 (let ((compilation-error-list nil))
1200 ;; Parse the last one.
1201 (compile-reinitialize-errors nil nil 1)
1202 compilation-error-list))))))))
1203
1204(defun compilation-previous-file (n)
1205 "Move point to the previous error for a different file than the current one."
1206 (interactive "p")
1207 (compilation-next-file (- n)))
1208
55dfd2c4 1209(defun kill-compilation ()
b765ba64 1210 "Kill the process made by the \\[compile] or \\[grep] commands."
55dfd2c4 1211 (interactive)
d3cb357b 1212 (let ((buffer (compilation-find-buffer)))
55dfd2c4 1213 (if (get-buffer-process buffer)
d3cb357b 1214 (interrupt-process (get-buffer-process buffer))
851231e9 1215 (error "The compilation process is not running"))))
d3cb357b 1216
b765ba64 1217(defalias 'kill-grep 'kill-compilation)
55dfd2c4 1218
d3cb357b
RM
1219;; Parse any new errors in the compilation buffer,
1220;; or reparse from the beginning if the user has asked for that.
eaa3cac5
RM
1221(defun compile-reinitialize-errors (reparse
1222 &optional limit-search find-at-least)
d3cb357b
RM
1223 (save-excursion
1224 (set-buffer compilation-last-buffer)
1225 ;; If we are out of errors, or if user says "reparse",
1226 ;; discard the info we have, to force reparsing.
1227 (if (or (eq compilation-error-list t)
eaa3cac5 1228 reparse)
51ba27e7 1229 (compilation-forget-errors))
c540863c 1230 (if (and compilation-error-list
b5bb472e
RM
1231 (or (not limit-search)
1232 (> compilation-parsing-end limit-search))
c540863c 1233 (or (not find-at-least)
a72ba929 1234 (>= (length compilation-error-list) find-at-least)))
d3cb357b
RM
1235 ;; Since compilation-error-list is non-nil, it points to a specific
1236 ;; error the user wanted. So don't move it around.
1237 nil
51ba27e7
RM
1238 ;; This was here for a long time (before my rewrite); why? --roland
1239 ;;(switch-to-buffer compilation-last-buffer)
55dfd2c4 1240 (set-buffer-modified-p nil)
b5bb472e 1241 (if (< compilation-parsing-end (point-max))
51ba27e7 1242 ;; compilation-error-list might be non-nil if we have a non-nil
676a14e1 1243 ;; LIMIT-SEARCH or FIND-AT-LEAST arg. In that case its value
51ba27e7
RM
1244 ;; records the current position in the error list, and we must
1245 ;; preserve that after reparsing.
1246 (let ((error-list-pos compilation-error-list))
b5bb472e 1247 (funcall compilation-parse-errors-function
51ba27e7
RM
1248 limit-search
1249 (and find-at-least
1250 ;; We only need enough new parsed errors to reach
1251 ;; FIND-AT-LEAST errors past the current
1252 ;; position.
1253 (- find-at-least (length compilation-error-list))))
1254 ;; Remember the entire list for compilation-forget-errors. If
1255 ;; this is an incremental parse, append to previous list. If
1256 ;; we are parsing anew, compilation-forget-errors cleared
1257 ;; compilation-old-error-list above.
1258 (setq compilation-old-error-list
1259 (nconc compilation-old-error-list compilation-error-list))
1260 (if error-list-pos
1261 ;; We started in the middle of an existing list of parsed
1262 ;; errors before parsing more; restore that position.
1263 (setq compilation-error-list error-list-pos))
01e6e8c9 1264 ;; Mouse-Highlight (the first line of) each error message when the
37f5f978
RS
1265 ;; mouse pointer moves over it:
1266 (let ((inhibit-read-only t)
474d71ae
RS
1267 (buffer-undo-list t)
1268 deactivate-mark
311ff235 1269 (buffer-was-modified (buffer-modified-p))
37f5f978
RS
1270 (error-list compilation-error-list))
1271 (while error-list
1272 (save-excursion
08de70d7
EZ
1273 (add-text-properties (goto-char (car (car error-list)))
1274 (progn (end-of-line) (point))
1275 '(mouse-face highlight help-echo "\
1276mouse-2: visit this file and line")))
311ff235
JB
1277 (setq error-list (cdr error-list)))
1278 (set-buffer-modified-p buffer-was-modified))
b5bb472e 1279 )))))
55dfd2c4 1280
f98494a4 1281(defun compile-mouse-goto-error (event)
d28701c7
RS
1282 "Visit the source for the error message the mouse is pointing at.
1283This is like `compile-goto-error' called without prefix arg
1284at the end of the line."
f98494a4 1285 (interactive "e")
e78e10ca
KH
1286 (save-excursion
1287 (set-buffer (window-buffer (posn-window (event-end event))))
1288 (goto-char (posn-point (event-end event)))
1289
1290 (or (compilation-buffer-p (current-buffer))
851231e9 1291 (error "Not in a compilation buffer"))
e78e10ca 1292 (setq compilation-last-buffer (current-buffer))
167d1418
EZ
1293 ;; `compile-reinitialize-errors' needs to see the complete filename
1294 ;; on the line where they clicked the mouse. Since it only looks
a24770bc 1295 ;; up to point, moving point to eol makes sure the filename is
167d1418
EZ
1296 ;; visible to `compile-reinitialize-errors'.
1297 (end-of-line)
e78e10ca
KH
1298 (compile-reinitialize-errors nil (point))
1299
1300 ;; Move to bol; the marker for the error on this line will point there.
1301 (beginning-of-line)
1302
1303 ;; Move compilation-error-list to the elt of compilation-old-error-list
1304 ;; we want.
1305 (setq compilation-error-list compilation-old-error-list)
1306 (while (and compilation-error-list
41a3354e
DL
1307 ;; The marker can point nowhere if we previously
1308 ;; failed to find the relevant file. See
1309 ;; compilation-next-error-locus.
1310 (or (null (marker-buffer (caar compilation-error-list)))
8f36a284 1311 (and (> (point) (caar compilation-error-list))
2827a3c1
RS
1312 (>= (point)
1313 ;; Don't skip too far: the text between
1314 ;; two errors belongs to the first. This
1315 ;; in-between text might be other errors
1316 ;; on the same line (see
1317 ;; compilation-skip-to-next-location).
1318 (if (null (cdr compilation-error-list))
1319 compilation-parsing-end
1320 (caar (cdr compilation-error-list)))))))
e78e10ca
KH
1321 (setq compilation-error-list (cdr compilation-error-list)))
1322 (or compilation-error-list
1323 (error "No error to go to")))
1324 (select-window (posn-window (event-end event)))
e78e10ca
KH
1325
1326 (push-mark)
1327 (next-error 1))
f98494a4 1328
55dfd2c4
RS
1329(defun compile-goto-error (&optional argp)
1330 "Visit the source for the error message point is on.
31efa7c9 1331Use this command in a compilation log buffer. Sets the mark at point there.
b2bb6ec8 1332\\[universal-argument] as a prefix arg means to reparse the buffer's error messages first;
55dfd2c4
RS
1333other kinds of prefix arguments are ignored."
1334 (interactive "P")
d3cb357b 1335 (or (compilation-buffer-p (current-buffer))
851231e9 1336 (error "Not in a compilation buffer"))
d3cb357b 1337 (setq compilation-last-buffer (current-buffer))
eaa3cac5 1338 (compile-reinitialize-errors (consp argp) (point))
646bd331 1339
15d1a8da
RM
1340 ;; Move to bol; the marker for the error on this line will point there.
1341 (beginning-of-line)
1342
646bd331 1343 ;; Move compilation-error-list to the elt of compilation-old-error-list
643f763f 1344 ;; we want.
646bd331 1345 (setq compilation-error-list compilation-old-error-list)
643f763f 1346 (while (and compilation-error-list
41a3354e
DL
1347 ;; The marker can point nowhere if we previously
1348 ;; failed to find the relevant file. See
1349 ;; compilation-next-error-locus.
1350 (or (null (marker-buffer (caar compilation-error-list)))
8f36a284 1351 (and (> (point) (caar compilation-error-list))
2827a3c1
RS
1352 (>= (point)
1353 ;; Don't skip too far: the text between
1354 ;; two errors belongs to the first. This
1355 ;; in-between text might be other errors
1356 ;; on the same line (see
1357 ;; compilation-skip-to-next-location).
1358 (if (null (cdr compilation-error-list))
1359 compilation-parsing-end
1360 (caar (cdr compilation-error-list)))))))
646bd331
RM
1361 (setq compilation-error-list (cdr compilation-error-list)))
1362
31efa7c9 1363 (push-mark)
643f763f 1364 (next-error 1))
55dfd2c4 1365
d3cb357b
RM
1366;; Return a compilation buffer.
1367;; If the current buffer is a compilation buffer, return it.
1368;; If compilation-last-buffer is set to a live buffer, use that.
1369;; Otherwise, look for a compilation buffer and signal an error
1370;; if there are none.
4746118a
JB
1371(defun compilation-find-buffer (&optional other-buffer)
1372 (if (and (not other-buffer)
1373 (compilation-buffer-p (current-buffer)))
d3cb357b
RM
1374 ;; The current buffer is a compilation buffer.
1375 (current-buffer)
4746118a 1376 (if (and compilation-last-buffer (buffer-name compilation-last-buffer)
95d219b5 1377 (compilation-buffer-p compilation-last-buffer)
4746118a
JB
1378 (or (not other-buffer) (not (eq compilation-last-buffer
1379 (current-buffer)))))
d3cb357b
RM
1380 compilation-last-buffer
1381 (let ((buffers (buffer-list)))
4746118a
JB
1382 (while (and buffers (or (not (compilation-buffer-p (car buffers)))
1383 (and other-buffer
1384 (eq (car buffers) (current-buffer)))))
d3cb357b
RM
1385 (setq buffers (cdr buffers)))
1386 (if buffers
1387 (car buffers)
4746118a
JB
1388 (or (and other-buffer
1389 (compilation-buffer-p (current-buffer))
1390 ;; The current buffer is a compilation buffer.
1391 (progn
1392 (if other-buffer
1393 (message "This is the only compilation buffer."))
1394 (current-buffer)))
1395 (error "No compilation started!")))))))
d3cb357b
RM
1396
1397;;;###autoload
55dfd2c4
RS
1398(defun next-error (&optional argp)
1399 "Visit next compilation error message and corresponding source code.
966c0a72
RS
1400
1401If all the error messages parsed so far have been processed already,
1402the message buffer is checked for new ones.
55dfd2c4 1403
ffb4b7a1 1404A prefix ARGP specifies how many error messages to move;
55dfd2c4 1405negative means move back to previous error messages.
ffb4b7a1 1406Just \\[universal-argument] as a prefix means reparse the error message buffer
55dfd2c4
RS
1407and start at the first error.
1408
966c0a72
RS
1409\\[next-error] normally uses the most recently started compilation or
1410grep buffer. However, it can operate on any buffer with output from
1411the \\[compile] and \\[grep] commands, or, more generally, on any
1412buffer in Compilation mode or with Compilation Minor mode enabled. To
1413specify use of a particular buffer for error messages, type
1414\\[next-error] in that buffer.
55dfd2c4 1415
966c0a72
RS
1416Once \\[next-error] has chosen the buffer for error messages,
1417it stays with that buffer until you use it in some other buffer which
1418uses Compilation mode or Compilation Minor mode.
55dfd2c4 1419
d3cb357b
RM
1420See variables `compilation-parse-errors-function' and
1421\`compilation-error-regexp-alist' for customization ideas."
55dfd2c4 1422 (interactive "P")
d3cb357b 1423 (setq compilation-last-buffer (compilation-find-buffer))
eaa3cac5
RM
1424 (compilation-goto-locus (compilation-next-error-locus
1425 ;; We want to pass a number here only if
1426 ;; we got a numeric prefix arg, not just C-u.
1427 (and (not (consp argp))
1428 (prefix-numeric-value argp))
1429 (consp argp))))
1430;;;###autoload (define-key ctl-x-map "`" 'next-error)
1431
00c8f418 1432(defun previous-error (argp)
e60476ca 1433 "Visit previous compilation error message and corresponding source code.
00c8f418
EZ
1434
1435A prefix ARGP specifies how many error messages to move;
1436negative means move forward to next error messages.
1437
1438This operates on the output from the \\[compile] and \\[grep] commands."
1439 (interactive "P")
1440 (next-error (- (prefix-numeric-value argp))))
e60476ca
RS
1441
1442(defun first-error ()
d9b73ccc 1443 "Reparse the error message buffer and start at the first error.
e60476ca
RS
1444Visit corresponding source code.
1445This operates on the output from the \\[compile] command."
1446 (interactive)
eae2c972 1447 (next-error '(4)))
e60476ca 1448
c41fe446
KH
1449(defvar compilation-skip-to-next-location nil
1450 "*If non-nil, skip multiple error messages for the same source location.")
1451
b4300a1a 1452(defun compilation-next-error-locus (&optional move reparse silent)
eaa3cac5
RM
1453 "Visit next compilation error and return locus in corresponding source code.
1454This operates on the output from the \\[compile] command.
1455If all preparsed error messages have been processed,
1456the error message buffer is checked for new ones.
1457
1458Returns a cons (ERROR . SOURCE) of two markers: ERROR is a marker at the
1459location of the error message in the compilation buffer, and SOURCE is a
1460marker at the location in the source code indicated by the error message.
1461
1462Optional first arg MOVE says how many error messages to move forwards (or
1463backwards, if negative); default is 1. Optional second arg REPARSE, if
1464non-nil, says to reparse the error message buffer and reset to the first
501cf428 1465error (plus MOVE - 1). If optional third argument SILENT is non-nil, return
b4300a1a 1466nil instead of raising an error if there are no more errors.
eaa3cac5
RM
1467
1468The current buffer should be the desired compilation output buffer."
1469 (or move (setq move 1))
667aec94 1470 (compile-reinitialize-errors reparse nil (and (not reparse) (max 0 move)))
d3cb357b 1471 (let (next-errors next-error)
7fb63acd
RS
1472 (catch 'no-next-error
1473 (save-excursion
1474 (set-buffer compilation-last-buffer)
1475 ;; compilation-error-list points to the "current" error.
501cf428 1476 (setq next-errors
7fb63acd
RS
1477 (if (> move 0)
1478 (nthcdr (1- move)
1479 compilation-error-list)
1480 ;; Zero or negative arg; we need to move back in the list.
1481 (let ((n (1- move))
1482 (i 0)
1483 (e compilation-old-error-list))
1484 ;; See how many cdrs away the current error is from the start.
1485 (while (not (eq e compilation-error-list))
1486 (setq i (1+ i)
1487 e (cdr e)))
1488 (if (> (- n) i)
1489 (error "Moved back past first error")
1490 (nthcdr (+ i n) compilation-old-error-list))))
1491 next-error (car next-errors))
1492 (while
1493 (if (null next-error)
1494 (progn
1495 (and move (/= move 1)
1496 (error (if (> move 0)
1497 "Moved past last error")
1498 "Moved back past first error"))
1499 ;; Forget existing error messages if compilation has finished.
1500 (if (not (and (get-buffer-process (current-buffer))
1501 (eq (process-status
1502 (get-buffer-process
1503 (current-buffer)))
1504 'run)))
1505 (compilation-forget-errors))
1506 (if silent
1507 (throw 'no-next-error nil)
1508 (error (concat compilation-error-message
1509 (and (get-buffer-process (current-buffer))
1510 (eq (process-status
1511 (get-buffer-process
1512 (current-buffer)))
1513 'run)
b4300a1a 1514 " yet")))))
7fb63acd
RS
1515 (setq compilation-error-list (cdr next-errors))
1516 (if (null (cdr next-error))
1517 ;; This error is boring. Go to the next.
1518 t
1519 (or (markerp (cdr next-error))
1520 ;; This error has a filename/lineno pair.
1521 ;; Find the file and turn it into a marker.
1522 (let* ((fileinfo (car (cdr next-error)))
20c1daec
RM
1523 (buffer (apply 'compilation-find-file
1524 (car next-error) fileinfo)))
7fb63acd
RS
1525 (if (null buffer)
1526 ;; We can't find this error's file.
1527 ;; Remove all errors in the same file.
1528 (progn
1529 (setq next-errors compilation-old-error-list)
1530 (while next-errors
1531 (and (consp (cdr (car next-errors)))
1532 (equal (car (cdr (car next-errors)))
1533 fileinfo)
1534 (progn
1535 (set-marker (car (car next-errors)) nil)
1536 (setcdr (car next-errors) nil)))
1537 (setq next-errors (cdr next-errors)))
1538 ;; Look for the next error.
1539 t)
1540 ;; We found the file. Get a marker for this error.
47d1bc29
KH
1541 ;; compilation-old-error-list and
1542 ;; compilation-error-screen-columns are buffer-local
1543 ;; so we must be careful to extract their value
7fb63acd
RS
1544 ;; before switching to the source file buffer.
1545 (let ((errors compilation-old-error-list)
47d1bc29 1546 (columns compilation-error-screen-columns)
7fb63acd
RS
1547 (last-line (nth 1 (cdr next-error)))
1548 (column (nth 2 (cdr next-error))))
1549 (set-buffer buffer)
1550 (save-excursion
1551 (save-restriction
1552 (widen)
1553 (goto-line last-line)
4dbf8a2c 1554 (if (and column (> column 0))
4ad26d84 1555 ;; Columns in error msgs are 1-origin.
47d1bc29 1556 (if columns
f8e03ecb
AS
1557 (move-to-column (1- column))
1558 (forward-char (1- column)))
7fb63acd
RS
1559 (beginning-of-line))
1560 (setcdr next-error (point-marker))
1561 ;; Make all the other error messages referring
1562 ;; to the same file have markers into the buffer.
1563 (while errors
1564 (and (consp (cdr (car errors)))
1565 (equal (car (cdr (car errors))) fileinfo)
1566 (let* ((this (nth 1 (cdr (car errors))))
1567 (column (nth 2 (cdr (car errors))))
1568 (lines (- this last-line)))
1569 (if (eq selective-display t)
1570 ;; When selective-display is t,
1571 ;; each C-m is a line boundary,
1572 ;; as well as each newline.
1573 (if (< lines 0)
1574 (re-search-backward "[\n\C-m]"
1575 nil 'end
1576 (- lines))
1577 (re-search-forward "[\n\C-m]"
1578 nil 'end
1579 lines))
1580 (forward-line lines))
eae2c972 1581 (if (and column (> column 1))
47d1bc29 1582 (if columns
f8e03ecb
AS
1583 (move-to-column (1- column))
1584 (forward-char (1- column)))
eae2c972 1585 (beginning-of-line))
7fb63acd
RS
1586 (setq last-line this)
1587 (setcdr (car errors) (point-marker))))
1588 (setq errors (cdr errors)))))))))
1589 ;; If we didn't get a marker for this error, or this
1590 ;; marker's buffer was killed, go on to the next one.
1591 (or (not (markerp (cdr next-error)))
1592 (not (marker-buffer (cdr next-error))))))
1593 (setq next-errors compilation-error-list
b4300a1a 1594 next-error (car next-errors)))))
d3cb357b 1595
c41fe446
KH
1596 (if compilation-skip-to-next-location
1597 ;; Skip over multiple error messages for the same source location,
1598 ;; so the next C-x ` won't go to an error in the same place.
1599 (while (and compilation-error-list
a24770bc
RS
1600 (equal (cdr (car compilation-error-list))
1601 (cdr next-error)))
c41fe446 1602 (setq compilation-error-list (cdr compilation-error-list))))
d3cb357b 1603
eaa3cac5
RM
1604 ;; We now have a marker for the position of the error source code.
1605 ;; NEXT-ERROR is a cons (ERROR . SOURCE) of two markers.
1606 next-error))
1607
1608(defun compilation-goto-locus (next-error)
1609 "Jump to an error locus returned by `compilation-next-error-locus'.
1610Takes one argument, a cons (ERROR . SOURCE) of two markers.
1611Selects a window with point at SOURCE, with another window displaying ERROR."
d28701c7
RS
1612 (if (eq (window-buffer (selected-window))
1613 (marker-buffer (car next-error)))
1614 ;; If the compilation buffer window is selected,
1615 ;; keep the compilation buffer in this window;
1616 ;; display the source in another window.
1617 (let ((pop-up-windows t))
1618 (pop-to-buffer (marker-buffer (cdr next-error))))
8f36a284
SM
1619 (if (window-dedicated-p (selected-window))
1620 (pop-to-buffer (marker-buffer (cdr next-error)))
d28701c7 1621 (switch-to-buffer (marker-buffer (cdr next-error)))))
eaa3cac5
RM
1622 (goto-char (cdr next-error))
1623 ;; If narrowing got in the way of
1624 ;; going to the right place, widen.
1625 (or (= (point) (marker-position (cdr next-error)))
1626 (progn
1627 (widen)
1628 (goto-char (cdr next-error))))
721cfafe
TTN
1629 ;; If hideshow got in the way of
1630 ;; seeing the right place, open permanently.
1631 (mapcar (function (lambda (ov)
1632 (when (eq 'hs (overlay-get ov 'invisible))
1633 (delete-overlay ov)
1634 (goto-char (cdr next-error)))))
1635 (overlays-at (point)))
eaa3cac5
RM
1636
1637 ;; Show compilation buffer in other window, scrolled to this error.
1638 (let* ((pop-up-windows t)
eae2c972 1639 ;; Use an existing window if it is in a visible frame.
c94b02d6 1640 (w (or (get-buffer-window (marker-buffer (car next-error)) 'visible)
eae2c972 1641 ;; Pop up a window.
9ac57479
KS
1642 (display-buffer (marker-buffer (car next-error)))))
1643 (highlight-regexp (with-current-buffer (marker-buffer (car next-error))
1644 compilation-highlight-regexp)))
eaa3cac5 1645 (set-window-point w (car next-error))
c94b02d6 1646 (set-window-start w (car next-error))
9ac57479
KS
1647 (compilation-set-window-height w)
1648
1649 (when highlight-regexp
1650 (unless compilation-highlight-overlay
1651 (setq compilation-highlight-overlay (make-overlay 1 1))
1652 (overlay-put compilation-highlight-overlay 'face 'region))
1653 (with-current-buffer (marker-buffer (cdr next-error))
1654 (save-excursion
1655 (end-of-line)
1656 (let ((end (point)) olay)
1657 (beginning-of-line)
1658 (if (and (stringp highlight-regexp)
1659 (re-search-forward highlight-regexp end t))
1660 (progn
1661 (goto-char (match-beginning 0))
1662 (move-overlay compilation-highlight-overlay (match-beginning 0) (match-end 0)))
1663 (move-overlay compilation-highlight-overlay (point) end))
1664 (sit-for 0 500)
1665 (delete-overlay compilation-highlight-overlay)))))))
1666
eaa3cac5 1667\f
20c1daec 1668(defun compilation-find-file (marker filename dir &rest formats)
851231e9 1669 "Find a buffer for file FILENAME.
ffb4b7a1
SM
1670Search the directories in `compilation-search-path'.
1671A nil in `compilation-search-path' means to try the
851231e9
DL
1672current directory, which is passed in DIR.
1673If FILENAME is not found at all, ask the user where to find it.
1674Pop up the buffer containing MARKER and scroll to MARKER if we ask the user."
20c1daec 1675 (or formats (setq formats '("%s")))
721cfafe
TTN
1676 (save-excursion
1677 (let ((dirs compilation-search-path)
1678 buffer thisdir fmts name)
1679 (if (file-name-absolute-p filename)
5b6858da
SM
1680 ;; The file name is absolute. Use its explicit directory as
1681 ;; the first in the search path, and strip it from FILENAME.
1682 (setq filename (abbreviate-file-name (expand-file-name filename))
1683 dirs (cons (file-name-directory filename) dirs)
1684 filename (file-name-nondirectory filename)))
721cfafe
TTN
1685 ;; Now search the path.
1686 (while (and dirs (null buffer))
1687 (setq thisdir (or (car dirs) dir)
1688 fmts formats)
1689 ;; For each directory, try each format string.
1690 (while (and fmts (null buffer))
1691 (setq name (expand-file-name (format (car fmts) filename) thisdir)
1692 buffer (and (file-exists-p name)
ffb4b7a1 1693 (find-file-noselect name))
721cfafe
TTN
1694 fmts (cdr fmts)))
1695 (setq dirs (cdr dirs)))
1696 (or buffer
1697 ;; The file doesn't exist.
1698 ;; Ask the user where to find it.
1699 ;; If he hits C-g, then the next time he does
1700 ;; next-error, he'll skip past it.
1701 (let* ((pop-up-windows t)
1702 (w (display-buffer (marker-buffer marker))))
1703 (set-window-point w marker)
1704 (set-window-start w marker)
1705 (let ((name (expand-file-name
1706 (read-file-name
1707 (format "Find this error in: (default %s) "
1708 filename)
1709 dir filename t))))
1710 (if (file-directory-p name)
1711 (setq name (expand-file-name filename name)))
1712 (setq buffer (and (file-exists-p name)
1713 (find-file name))))))
1714 ;; Make intangible overlays tangible.
1715 (mapcar (function (lambda (ov)
1716 (when (overlay-get ov 'intangible)
1717 (overlay-put ov 'intangible nil))))
1718 (overlays-in (point-min) (point-max)))
1719 buffer)))
1720
56ce04a3
RS
1721(defun compilation-normalize-filename (filename)
1722 "Convert a filename string found in an error message to make it usable."
1723
1724 ;; Check for a comint-file-name-prefix and prepend it if
1725 ;; appropriate. (This is very useful for
1726 ;; compilation-minor-mode in an rlogin-mode buffer.)
1727 (and (boundp 'comint-file-name-prefix)
1728 ;; If file name is relative, default-directory will
1729 ;; already contain the comint-file-name-prefix (done
1730 ;; by compile-abbreviate-directory).
1731 (file-name-absolute-p filename)
1732 (setq filename
1733 (concat comint-file-name-prefix filename)))
1734
1735 ;; If compilation-parse-errors-filename-function is
1736 ;; defined, use it to process the filename.
1737 (when compilation-parse-errors-filename-function
1738 (setq filename
1739 (funcall compilation-parse-errors-filename-function
1740 filename)))
1741
1742 ;; Some compilers (e.g. Sun's java compiler, reportedly)
1743 ;; produce bogus file names like "./bar//foo.c" for file
1744 ;; "bar/foo.c"; expand-file-name will collapse these into
1745 ;; "/foo.c" and fail to find the appropriate file. So we
1746 ;; look for doubled slashes in the file name and fix them
1747 ;; up in the buffer.
1748 (setq filename (command-line-normalize-file-name filename)))
d3cb357b
RM
1749
1750;; Set compilation-error-list to nil, and unchain the markers that point to the
1751;; error messages and their text, so that they no longer slow down gap motion.
1752;; This would happen anyway at the next garbage collection, but it is better to
646bd331 1753;; do it right away.
e7512168 1754(defun compilation-forget-errors ()
55dfd2c4
RS
1755 (while compilation-old-error-list
1756 (let ((next-error (car compilation-old-error-list)))
1757 (set-marker (car next-error) nil)
d3cb357b
RM
1758 (if (markerp (cdr next-error))
1759 (set-marker (cdr next-error) nil)))
55dfd2c4 1760 (setq compilation-old-error-list (cdr compilation-old-error-list)))
bd3910fe 1761 (setq compilation-error-list nil
c4d7c00a 1762 compilation-directory-stack (list default-directory))
347c9140
RS
1763 (if compilation-parsing-end
1764 (set-marker compilation-parsing-end 1))
37f5f978 1765 ;; Remove the highlighting added by compile-reinitialize-errors:
474d71ae
RS
1766 (let ((inhibit-read-only t)
1767 (buffer-undo-list t)
1768 deactivate-mark)
08de70d7
EZ
1769 (remove-text-properties (point-min) (point-max)
1770 '(mouse-face highlight help-echo nil))))
d3cb357b
RM
1771
1772
a24770bc
RS
1773;; This function is not needed any more by compilation mode.
1774;; Does anyone else need it or can it be deleted?
d3cb357b
RM
1775(defun count-regexp-groupings (regexp)
1776 "Return the number of \\( ... \\) groupings in REGEXP (a string)."
1777 (let ((groupings 0)
1778 (len (length regexp))
1779 (i 0)
1780 c)
1781 (while (< i len)
1782 (setq c (aref regexp i)
1783 i (1+ i))
1784 (cond ((= c ?\[)
1785 ;; Find the end of this [...].
1786 (while (and (< i len)
1787 (not (= (aref regexp i) ?\])))
1788 (setq i (1+ i))))
1789 ((= c ?\\)
1790 (if (< i len)
1791 (progn
1792 (setq c (aref regexp i)
1793 i (1+ i))
1794 (if (= c ?\))
1795 ;; We found the end of a grouping,
1796 ;; so bump our counter.
1797 (setq groupings (1+ groupings))))))))
1798 groupings))
55dfd2c4 1799
a24770bc 1800(defvar compilation-current-file nil
851231e9 1801 "Used by `compilation-parse-errors' to store filename for file being compiled.")
a24770bc
RS
1802
1803;; This variable is not used as a global variable. It's defined here just to
1804;; shut up the byte compiler. It's bound and used by compilation-parse-errors
1805;; and set by compile-collect-regexps.
1806(defvar compilation-regexps nil)
1807
c540863c 1808(defun compilation-parse-errors (limit-search find-at-least)
a24770bc 1809 "Parse the current buffer as grep, cc, lint or other error messages.
d3cb357b 1810See variable `compilation-parse-errors-function' for the interface it uses."
55dfd2c4
RS
1811 (setq compilation-error-list nil)
1812 (message "Parsing error messages...")
a24770bc
RS
1813 (if (null compilation-error-regexp-alist)
1814 (error "compilation-error-regexp-alist is empty!"))
1815 (let* ((compilation-regexps nil) ; Variable set by compile-collect-regexps.
aa53db6a 1816 (default-directory (car compilation-directory-stack))
a24770bc
RS
1817 (found-desired nil)
1818 (compilation-num-errors-found 0)
1819 ;; Set up now the expanded, abbreviated directory variables
1820 ;; that compile-abbreviate-directory will need, so we can
1821 ;; compute them just once here.
1822 (orig (abbreviate-file-name default-directory))
1823 (orig-expanded (abbreviate-file-name
1824 (file-truename default-directory)))
1825 (parent-expanded (abbreviate-file-name
1826 (expand-file-name "../" orig-expanded))))
1827
1828 ;; Make a list of all the regexps. Each element has the form
1829 ;; (REGEXP TYPE IDX1 IDX2 ...)
1830 ;; where TYPE is one of leave, enter, file, error or nomessage.
1831 (compile-collect-regexps 'leave compilation-leave-directory-regexp-alist)
1832 (compile-collect-regexps 'enter compilation-enter-directory-regexp-alist)
1833 (compile-collect-regexps 'file compilation-file-regexp-alist)
a24770bc 1834 (compile-collect-regexps 'error compilation-error-regexp-alist)
2497076e 1835 (compile-collect-regexps 'nomessage compilation-nomessage-regexp-alist)
d3cb357b 1836
55dfd2c4
RS
1837 ;; Don't reparse messages already seen at last parse.
1838 (goto-char compilation-parsing-end)
9cab952d 1839 (when (and (bobp)
9dd1cf67 1840 (compilation-buffer-p (current-buffer)))
9cab952d
RS
1841 (setq compilation-current-file nil) ; No current file at start.
1842 ;; Don't parse the first two lines as error messages.
1843 ;; This matters for grep.
1844 (forward-line 2))
a24770bc
RS
1845
1846 ;; Parse messages.
47092737
RS
1847 (while (not (or found-desired (eobp)
1848 ;; Don't parse the "compilation finished" message
1849 ;; as a compilation error.
1850 (get-text-property (point) 'compilation-handle-exit)))
a24770bc
RS
1851 (let ((this compilation-regexps) (prev nil) (alist nil) type)
1852 ;; Go through the regular expressions. If a match is found,
1853 ;; variable alist is set to the corresponding alist and the
1854 ;; matching regexp is moved to the front of compilation-regexps
1855 ;; to make it match faster next time.
1856 (while (and this (null alist))
1857 (if (not (looking-at (car (car this))))
1858 (progn (setq prev this) ; No match, go to next.
1859 (setq this (cdr this)))
1860 (setq alist (cdr (car this))) ; Got a match.
1861;;; (if prev ; If not the first regexp,
1862;;; (progn ; move it to the front.
1863;;; (setcdr prev (cdr this))
1864;;; (setcdr this compilation-regexps)
1865;;; (setq compilation-regexps this)))
1866 ))
1867 (if (and alist ; Seen a match and not to
1868 (not (eq (setq type (car alist)) 'nomessage))) ; be ignored.
1869 (let* ((end-of-match (match-end 0))
1870 (filename
1871 (compile-buffer-substring (car (setq alist (cdr alist)))))
1872 stack)
1873 (if (eq type 'error) ; error message
1874 (let* ((linenum (if (numberp (car (setq alist (cdr alist))))
1875 (string-to-int
1876 (compile-buffer-substring (car alist)))
1877 ;; (car alist) is not a number, must be a
1878 ;; function that is called below to return
1879 ;; an error position descriptor.
1880 (car alist)))
1881 ;; Convert to integer later if linenum not a function.
1882 (column (compile-buffer-substring
1883 (car (setq alist (cdr alist)))))
1884 this-error)
1885
1886 ;; Check that we have a file name.
1887 (or filename
1888 ;; No file name in message, we must have seen it before
1889 (setq filename compilation-current-file)
1890 (error "\
851231e9 1891An error message with no file name and no file name has been seen earlier"))
a24770bc 1892
56ce04a3
RS
1893 ;; Clean up the file name string in several ways.
1894 (setq filename (compilation-normalize-filename filename))
a24770bc
RS
1895
1896 (setq filename
1897 (cons filename (cons default-directory (cdr alist))))
1898
1899 ;; Locate the erring file and line.
1900 ;; Make this-error a new elt for compilation-error-list,
1901 ;; giving a marker for the current compilation buffer
1902 ;; location, and the file and line number of the error.
1903 ;; Save, as the start of the error, the beginning of the
1904 ;; line containing the match.
bb40e751
RS
1905 (setq this-error
1906 (if (numberp linenum)
1907 (list (point-marker) filename linenum
1908 (and column (string-to-int column)))
1909 ;; If linenum is not a number then it must be
1910 ;; a function returning an error position
1911 ;; descriptor or nil (meaning no position).
1912 (save-excursion
1913 (funcall linenum filename column))))
721cfafe 1914
bb40e751
RS
1915 ;; We have an error position descriptor.
1916 ;; If we have found as many new errors as the user
1917 ;; wants, or if we are past the buffer position he
1918 ;; indicated, then we continue to parse until we have
1919 ;; seen all consecutive errors in the same file. This
1920 ;; means that all the errors of a source file will be
1921 ;; seen in one parsing run, so that the error positions
1922 ;; will be recorded as markers in the source file
1923 ;; buffer that will move when the buffer is changed.
1924 (if (and this-error
1925 compilation-error-list ; At least one previous.
1926 (or (and find-at-least
1927 (>= compilation-num-errors-found
1928 find-at-least))
1929 (and limit-search
1930 (>= end-of-match limit-search)))
5b6858da
SM
1931 ;; `this-error' could contain a pair of
1932 ;; markers already.
1933 (let ((thispos (cdr this-error))
1934 (lastpos (cdar compilation-error-list)))
1935 (not (equal
1936 (if (markerp thispos)
1937 (marker-buffer thispos)
1938 (car thispos))
1939 (if (markerp lastpos)
1940 (marker-buffer lastpos)
1941 (car lastpos))))))
bb40e751
RS
1942 ;; We are past the limits and the last error
1943 ;; parsed, didn't belong to the same source file
1944 ;; as the earlier ones i.e. we have seen all the
1945 ;; errors belonging to the earlier file. We don't
1946 ;; add the error just parsed so that the next
1947 ;; parsing run can get it and the following errors
1948 ;; in the same file all at once.
1949 (setq found-desired t)
1950
1951 (goto-char end-of-match) ; Prepare for next message.
1952 ;; Don't add the same source line more than once.
1953 (and this-error
1954 (not (and
1955 compilation-error-list
1956 (equal (cdr (car compilation-error-list))
1957 (cdr this-error))))
1958 (setq compilation-error-list
1959 (cons this-error compilation-error-list)
1960 compilation-num-errors-found
1961 (1+ compilation-num-errors-found)))))
a24770bc
RS
1962
1963 ;; Not an error message.
1964 (if (eq type `file) ; Change current file.
56ce04a3
RS
1965 (when filename
1966 (setq compilation-current-file
1967 ;; Clean up the file name string in several ways.
1968 (compilation-normalize-filename filename)))
a24770bc
RS
1969 ;; Enter or leave directory.
1970 (setq stack compilation-directory-stack)
56ce04a3
RS
1971 ;; Don't check if it is really a directory.
1972 ;; Let the code to search and clean up file names
1973 ;; try to use it in any case.
1974 (when filename
1975 ;; Clean up the directory name string in several ways.
1976 (setq filename (compilation-normalize-filename filename))
1977 (setq filename
1978 ;; The directory name in the message
1979 ;; is a truename. Try to convert it to a form
1980 ;; like what the user typed in.
1981 (compile-abbreviate-directory
1982 (file-name-as-directory
1983 (expand-file-name filename))
1984 orig orig-expanded parent-expanded))
1985 (if (eq type 'leave)
1986 ;; If we are leaving a specific directory,
1987 ;; as preparation, pop out of all other directories
1988 ;; that we entered nested within it.
1989 (while (and stack
1990 (not (string-equal (car stack)
1991 filename)))
1992 (setq stack (cdr stack)))
1993 (setq compilation-directory-stack
1994 (cons filename compilation-directory-stack)
1995 default-directory filename)))
58856335
RS
1996 (and (eq type 'leave)
1997 stack
1998 (setq compilation-directory-stack (cdr stack))
1999 (setq stack (car compilation-directory-stack))
bb40e751
RS
2000 (setq default-directory stack)))
2001 (goto-char end-of-match) ; Prepare to look at next message.
2002 (and limit-search (>= end-of-match limit-search)
2003 ;; The user wanted a specific error, and we're past it.
2004 ;; We do this check here rather than at the end of the
2005 ;; loop because if the last thing seen is an error
2006 ;; message, we must carefully discard the last error
2007 ;; when it is the first in a new file (see above in
2008 ;; the error-message case)
2009 (setq found-desired t)))
a24770bc
RS
2010
2011 ;; Go to before the last character in the message so that we will
2012 ;; see the next line also when the message ended at end of line.
2013 ;; When we ignore the last error message above, this will
2014 ;; cancel the effect of forward-line below so that point
2015 ;; doesn't move.
2016 (forward-char -1)
2017
2018 ;; Is this message necessary any more? Parsing is now so fast
2019 ;; that you might not need to know how it proceeds.
2020 (message
2021 "Parsing error messages...%d found. %.0f%% of buffer seen."
6c43f2f9 2022 compilation-num-errors-found
51ba27e7
RM
2023 ;; Use floating-point because (* 100 (point)) frequently
2024 ;; exceeds the range of Emacs Lisp integers.
2025 (/ (* 100.0 (point)) (point-max)))
47092737 2026 )))
a24770bc 2027
47092737 2028 (forward-line 1)) ; End of while loop. Look at next line.
a24770bc 2029
c4d7c00a 2030 (set-marker compilation-parsing-end (point))
a24770bc 2031 (setq compilation-error-list (nreverse compilation-error-list))
5b6858da
SM
2032 ;; (message "Parsing error messages...done. %d found. %.0f%% of buffer seen."
2033 ;; compilation-num-errors-found
2034 ;; (/ (* 100.0 (point)) (point-max)))
a24770bc
RS
2035 (message "Parsing error messages...done.")))
2036
2037(defun compile-collect-regexps (type this)
2038 ;; Add elements to variable compilation-regexps that is bound in
2039 ;; compilation-parse-errors.
2040 (and (not (eq this t))
02c1bd08
SM
2041 (dolist (el this)
2042 (push (cons (car el) (cons type (cdr el))) compilation-regexps))))
a24770bc
RS
2043
2044(defun compile-buffer-substring (index)
851231e9 2045 "Get substring matched by INDEXth subexpression."
a24770bc
RS
2046 (if index
2047 (let ((beg (match-beginning index)))
2048 (if beg (buffer-substring beg (match-end index))))))
55dfd2c4 2049
51501e54
RS
2050;; If directory DIR is a subdir of ORIG or of ORIG's parent,
2051;; return a relative name for it starting from ORIG or its parent.
2052;; ORIG-EXPANDED is an expanded version of ORIG.
2053;; PARENT-EXPANDED is an expanded version of ORIG's parent.
2054;; Those two args could be computed here, but we run faster by
2055;; having the caller compute them just once.
2056(defun compile-abbreviate-directory (dir orig orig-expanded parent-expanded)
cd494de4
RM
2057 ;; Apply canonical abbreviations to DIR first thing.
2058 ;; Those abbreviations are already done in the other arguments passed.
2059 (setq dir (abbreviate-file-name dir))
2060
1e0e614d
RM
2061 ;; Check for a comint-file-name-prefix and prepend it if appropriate.
2062 ;; (This is very useful for compilation-minor-mode in an rlogin-mode
2063 ;; buffer.)
2064 (if (boundp 'comint-file-name-prefix)
2065 (setq dir (concat comint-file-name-prefix dir)))
2066
51501e54
RS
2067 (if (and (> (length dir) (length orig-expanded))
2068 (string= orig-expanded
2069 (substring dir 0 (length orig-expanded))))
2070 (setq dir
2071 (concat orig
2072 (substring dir (length orig-expanded)))))
2073 (if (and (> (length dir) (length parent-expanded))
2074 (string= parent-expanded
2075 (substring dir 0 (length parent-expanded))))
2076 (setq dir
2077 (concat (file-name-directory
2078 (directory-file-name orig))
2079 (substring dir (length parent-expanded)))))
2080 dir)
2081
f1ed9461
DL
2082(add-to-list 'debug-ignored-errors "^No more errors\\( yet\\|\\)$")
2083
4746118a 2084(provide 'compile)
fad160d5 2085
ab5796a9 2086;;; arch-tag: 12465727-7382-4f72-b234-79855a00dd8c
fad160d5 2087;;; compile.el ends here