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