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