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