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