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