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