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