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