]> code.delx.au - gnu-emacs/blob - lisp/progmodes/compile.el
John J Foerch <jjfoerch at earthlink.net> (tiny change)
[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 ;; http://lists.gnu.org/archive/html/emacs-devel/2007-11/msg01638.html
1172 (setq outwin (display-buffer outbuf))
1173 (with-current-buffer outbuf
1174 (let ((process-environment
1175 (append
1176 compilation-environment
1177 (if (if (boundp 'system-uses-terminfo) ; `if' for compiler warning
1178 system-uses-terminfo)
1179 (list "TERM=dumb" "TERMCAP="
1180 (format "COLUMNS=%d" (window-width)))
1181 (list "TERM=emacs"
1182 (format "TERMCAP=emacs:co#%d:tc=unknown:"
1183 (window-width))))
1184 ;; Set the EMACS variable, but
1185 ;; don't override users' setting of $EMACS.
1186 (unless (getenv "EMACS")
1187 (list "EMACS=t"))
1188 (list "INSIDE_EMACS=t")
1189 (copy-sequence process-environment))))
1190 (set (make-local-variable 'compilation-arguments)
1191 (list command mode name-function highlight-regexp))
1192 (set (make-local-variable 'revert-buffer-function)
1193 'compilation-revert-buffer)
1194 (set-window-start outwin (point-min))
1195 (or (eq outwin (selected-window))
1196 (set-window-point outwin (if compilation-scroll-output
1197 (point)
1198 (point-min))))
1199 ;; The setup function is called before compilation-set-window-height
1200 ;; so it can set the compilation-window-height buffer locally.
1201 (if compilation-process-setup-function
1202 (funcall compilation-process-setup-function))
1203 (compilation-set-window-height outwin)
1204 ;; Start the compilation.
1205 (let ((proc
1206 (if (eq mode t)
1207 ;; comint uses `start-file-process'.
1208 (get-buffer-process
1209 (with-no-warnings
1210 (comint-exec
1211 outbuf (downcase mode-name)
1212 (if (file-remote-p default-directory)
1213 "/bin/sh"
1214 shell-file-name)
1215 nil `("-c" ,command))))
1216 (start-file-process-shell-command (downcase mode-name)
1217 outbuf command))))
1218 ;; Make the buffer's mode line show process state.
1219 (setq mode-line-process '(":%s"))
1220 (set-process-sentinel proc 'compilation-sentinel)
1221 (set-process-filter proc 'compilation-filter)
1222 (set-marker (process-mark proc) (point) outbuf)
1223 (when compilation-disable-input
1224 (condition-case nil
1225 (process-send-eof proc)
1226 ;; The process may have exited already.
1227 (error nil)))
1228 (setq compilation-in-progress
1229 (cons proc compilation-in-progress))))
1230 ;; Now finally cd to where the shell started make/grep/...
1231 (setq default-directory thisdir))
1232 (if (buffer-local-value 'compilation-scroll-output outbuf)
1233 (save-selected-window
1234 (select-window outwin)
1235 (goto-char (point-max))))
1236 ;; Make it so the next C-x ` will use this buffer.
1237 (setq next-error-last-buffer outbuf)))
1238
1239 (defun compilation-set-window-height (window)
1240 "Set the height of WINDOW according to `compilation-window-height'."
1241 (let ((height (buffer-local-value 'compilation-window-height (window-buffer window))))
1242 (and height
1243 (window-full-width-p window)
1244 ;; If window is alone in its frame, aside from a minibuffer,
1245 ;; don't change its height.
1246 (not (eq window (frame-root-window (window-frame window))))
1247 ;; Stef said that doing the saves in this order is safer:
1248 (save-excursion
1249 (save-selected-window
1250 (select-window window)
1251 (enlarge-window (- height (window-height))))))))
1252
1253 (defvar compilation-menu-map
1254 (let ((map (make-sparse-keymap "Errors")))
1255 (define-key map [stop-subjob]
1256 '("Stop Compilation" . kill-compilation))
1257 (define-key map [compilation-mode-separator2]
1258 '("----" . nil))
1259 (define-key map [compilation-first-error]
1260 '("First Error" . first-error))
1261 (define-key map [compilation-previous-error]
1262 '("Previous Error" . previous-error))
1263 (define-key map [compilation-next-error]
1264 '("Next Error" . next-error))
1265 map))
1266
1267 (defvar compilation-minor-mode-map
1268 (let ((map (make-sparse-keymap)))
1269 (define-key map [mouse-2] 'compile-goto-error)
1270 (define-key map [follow-link] 'mouse-face)
1271 (define-key map "\C-c\C-c" 'compile-goto-error)
1272 (define-key map "\C-m" 'compile-goto-error)
1273 (define-key map "\C-c\C-k" 'kill-compilation)
1274 (define-key map "\M-n" 'compilation-next-error)
1275 (define-key map "\M-p" 'compilation-previous-error)
1276 (define-key map "\M-{" 'compilation-previous-file)
1277 (define-key map "\M-}" 'compilation-next-file)
1278 ;; Set up the menu-bar
1279 (define-key map [menu-bar compilation]
1280 (cons "Errors" compilation-menu-map))
1281 map)
1282 "Keymap for `compilation-minor-mode'.")
1283
1284 (defvar compilation-shell-minor-mode-map
1285 (let ((map (make-sparse-keymap)))
1286 (define-key map "\M-\C-m" 'compile-goto-error)
1287 (define-key map "\M-\C-n" 'compilation-next-error)
1288 (define-key map "\M-\C-p" 'compilation-previous-error)
1289 (define-key map "\M-{" 'compilation-previous-file)
1290 (define-key map "\M-}" 'compilation-next-file)
1291 ;; Set up the menu-bar
1292 (define-key map [menu-bar compilation]
1293 (cons "Errors" compilation-menu-map))
1294 map)
1295 "Keymap for `compilation-shell-minor-mode'.")
1296
1297 (defvar compilation-button-map
1298 (let ((map (make-sparse-keymap)))
1299 (define-key map [mouse-2] 'compile-goto-error)
1300 (define-key map [follow-link] 'mouse-face)
1301 (define-key map "\C-m" 'compile-goto-error)
1302 map)
1303 "Keymap for compilation-message buttons.")
1304 (fset 'compilation-button-map compilation-button-map)
1305
1306 (defvar compilation-mode-map
1307 (let ((map (make-sparse-keymap)))
1308 ;; Don't inherit from compilation-minor-mode-map,
1309 ;; because that introduces a menu bar item we don't want.
1310 ;; That confuses C-down-mouse-3.
1311 (define-key map [mouse-2] 'compile-goto-error)
1312 (define-key map [follow-link] 'mouse-face)
1313 (define-key map "\C-c\C-c" 'compile-goto-error)
1314 (define-key map "\C-m" 'compile-goto-error)
1315 (define-key map "\C-c\C-k" 'kill-compilation)
1316 (define-key map "\M-n" 'compilation-next-error)
1317 (define-key map "\M-p" 'compilation-previous-error)
1318 (define-key map "\M-{" 'compilation-previous-file)
1319 (define-key map "\M-}" 'compilation-next-file)
1320 (define-key map "\t" 'compilation-next-error)
1321 (define-key map [backtab] 'compilation-previous-error)
1322
1323 (define-key map " " 'scroll-up)
1324 (define-key map "\^?" 'scroll-down)
1325 (define-key map "\C-c\C-f" 'next-error-follow-minor-mode)
1326
1327 ;; Set up the menu-bar
1328 (let ((submap (make-sparse-keymap "Compile")))
1329 (define-key map [menu-bar compilation]
1330 (cons "Compile" submap))
1331 (set-keymap-parent submap compilation-menu-map))
1332 (define-key map [menu-bar compilation compilation-separator2]
1333 '("----" . nil))
1334 (define-key map [menu-bar compilation compilation-grep]
1335 '("Search Files (grep)..." . grep))
1336 (define-key map [menu-bar compilation compilation-recompile]
1337 '("Recompile" . recompile))
1338 (define-key map [menu-bar compilation compilation-compile]
1339 '("Compile..." . compile))
1340 map)
1341 "Keymap for compilation log buffers.
1342 `compilation-minor-mode-map' is a parent of this.")
1343
1344 (put 'compilation-mode 'mode-class 'special)
1345
1346 ;;;###autoload
1347 (defun compilation-mode (&optional name-of-mode)
1348 "Major mode for compilation log buffers.
1349 \\<compilation-mode-map>To visit the source for a line-numbered error,
1350 move point to the error message line and type \\[compile-goto-error].
1351 To kill the compilation, type \\[kill-compilation].
1352
1353 Runs `compilation-mode-hook' with `run-mode-hooks' (which see).
1354
1355 \\{compilation-mode-map}"
1356 (interactive)
1357 (kill-all-local-variables)
1358 (use-local-map compilation-mode-map)
1359 (setq major-mode 'compilation-mode
1360 mode-name (or name-of-mode "Compilation"))
1361 (set (make-local-variable 'page-delimiter)
1362 compilation-page-delimiter)
1363 (compilation-setup)
1364 (setq buffer-read-only t)
1365 (run-mode-hooks 'compilation-mode-hook))
1366
1367 (defmacro define-compilation-mode (mode name doc &rest body)
1368 "This is like `define-derived-mode' without the PARENT argument.
1369 The parent is always `compilation-mode' and the customizable `compilation-...'
1370 variables are also set from the name of the mode you have chosen,
1371 by replacing the first word, e.g `compilation-scroll-output' from
1372 `grep-scroll-output' if that variable exists."
1373 (let ((mode-name (replace-regexp-in-string "-mode\\'" "" (symbol-name mode))))
1374 `(define-derived-mode ,mode compilation-mode ,name
1375 ,doc
1376 ,@(mapcar (lambda (v)
1377 (setq v (cons v
1378 (intern-soft (replace-regexp-in-string
1379 "^compilation" mode-name
1380 (symbol-name v)))))
1381 (and (cdr v)
1382 (or (boundp (cdr v))
1383 (if (boundp 'byte-compile-bound-variables)
1384 (memq (cdr v) byte-compile-bound-variables)))
1385 `(set (make-local-variable ',(car v)) ,(cdr v))))
1386 '(compilation-buffer-name-function
1387 compilation-directory-matcher
1388 compilation-error
1389 compilation-error-regexp-alist
1390 compilation-error-regexp-alist-alist
1391 compilation-error-screen-columns
1392 compilation-finish-function
1393 compilation-finish-functions
1394 compilation-first-column
1395 compilation-mode-font-lock-keywords
1396 compilation-page-delimiter
1397 compilation-parse-errors-filename-function
1398 compilation-process-setup-function
1399 compilation-scroll-output
1400 compilation-search-path
1401 compilation-skip-threshold
1402 compilation-window-height))
1403 ,@body)))
1404
1405 (defun compilation-revert-buffer (ignore-auto noconfirm)
1406 (if buffer-file-name
1407 (let (revert-buffer-function)
1408 (revert-buffer ignore-auto noconfirm))
1409 (if (or noconfirm (yes-or-no-p (format "Restart compilation? ")))
1410 (apply 'compilation-start compilation-arguments))))
1411
1412 (defvar compilation-current-error nil
1413 "Marker to the location from where the next error will be found.
1414 The global commands next/previous/first-error/goto-error use this.")
1415
1416 (defvar compilation-messages-start nil
1417 "Buffer position of the beginning of the compilation messages.
1418 If nil, use the beginning of buffer.")
1419
1420 ;; A function name can't be a hook, must be something with a value.
1421 (defconst compilation-turn-on-font-lock 'turn-on-font-lock)
1422
1423 (defun compilation-setup (&optional minor)
1424 "Prepare the buffer for the compilation parsing commands to work.
1425 Optional argument MINOR indicates this is called from
1426 `compilation-minor-mode'."
1427 (make-local-variable 'compilation-current-error)
1428 (make-local-variable 'compilation-messages-start)
1429 (make-local-variable 'compilation-error-screen-columns)
1430 (make-local-variable 'overlay-arrow-position)
1431 (set (make-local-variable 'overlay-arrow-string) "")
1432 (setq next-error-overlay-arrow-position nil)
1433 (add-hook 'kill-buffer-hook
1434 (lambda () (setq next-error-overlay-arrow-position nil)) nil t)
1435 ;; Note that compilation-next-error-function is for interfacing
1436 ;; with the next-error function in simple.el, and it's only
1437 ;; coincidentally named similarly to compilation-next-error.
1438 (setq next-error-function 'compilation-next-error-function)
1439 (set (make-local-variable 'comint-file-name-prefix)
1440 (or (file-remote-p default-directory) ""))
1441 (set (make-local-variable 'font-lock-extra-managed-props)
1442 '(directory message help-echo mouse-face debug))
1443 (set (make-local-variable 'compilation-locs)
1444 (make-hash-table :test 'equal :weakness 'value))
1445 ;; lazy-lock would never find the message unless it's scrolled to.
1446 ;; jit-lock might fontify some things too late.
1447 (set (make-local-variable 'font-lock-support-mode) nil)
1448 (set (make-local-variable 'font-lock-maximum-size) nil)
1449 (if minor
1450 (let ((fld font-lock-defaults))
1451 (font-lock-add-keywords nil (compilation-mode-font-lock-keywords))
1452 (if font-lock-mode
1453 (if fld
1454 (font-lock-fontify-buffer)
1455 (font-lock-change-mode)
1456 (turn-on-font-lock))
1457 (turn-on-font-lock)))
1458 (setq font-lock-defaults '(compilation-mode-font-lock-keywords t))
1459 ;; maybe defer font-lock till after derived mode is set up
1460 (run-mode-hooks 'compilation-turn-on-font-lock)))
1461
1462 ;;;###autoload
1463 (define-minor-mode compilation-shell-minor-mode
1464 "Toggle compilation shell minor mode.
1465 With arg, turn compilation mode on if and only if arg is positive.
1466 In this minor mode, all the error-parsing commands of the
1467 Compilation major mode are available but bound to keys that don't
1468 collide with Shell mode. See `compilation-mode'.
1469 Turning the mode on runs the normal hook `compilation-shell-minor-mode-hook'."
1470 nil " Shell-Compile"
1471 :group 'compilation
1472 (if compilation-shell-minor-mode
1473 (compilation-setup t)
1474 (font-lock-remove-keywords nil (compilation-mode-font-lock-keywords))
1475 (font-lock-fontify-buffer)))
1476
1477 ;;;###autoload
1478 (define-minor-mode compilation-minor-mode
1479 "Toggle compilation minor mode.
1480 With arg, turn compilation mode on if and only if arg is positive.
1481 In this minor mode, all the error-parsing commands of the
1482 Compilation major mode are available. See `compilation-mode'.
1483 Turning the mode on runs the normal hook `compilation-minor-mode-hook'."
1484 nil " Compilation"
1485 :group 'compilation
1486 (if compilation-minor-mode
1487 (compilation-setup t)
1488 (font-lock-remove-keywords nil (compilation-mode-font-lock-keywords))
1489 (font-lock-fontify-buffer)))
1490
1491 (defun compilation-handle-exit (process-status exit-status msg)
1492 "Write MSG in the current buffer and hack its `mode-line-process'."
1493 (let ((inhibit-read-only t)
1494 (status (if compilation-exit-message-function
1495 (funcall compilation-exit-message-function
1496 process-status exit-status msg)
1497 (cons msg exit-status)))
1498 (omax (point-max))
1499 (opoint (point))
1500 (cur-buffer (current-buffer)))
1501 ;; Record where we put the message, so we can ignore it later on.
1502 (goto-char omax)
1503 (insert ?\n mode-name " " (car status))
1504 (if (and (numberp compilation-window-height)
1505 (zerop compilation-window-height))
1506 (message "%s" (cdr status)))
1507 (if (bolp)
1508 (forward-char -1))
1509 (insert " at " (substring (current-time-string) 0 19))
1510 (goto-char (point-max))
1511 ;; Prevent that message from being recognized as a compilation error.
1512 (add-text-properties omax (point)
1513 (append '(compilation-handle-exit t) nil))
1514 (setq mode-line-process (format ":%s [%s]" process-status (cdr status)))
1515 ;; Force mode line redisplay soon.
1516 (force-mode-line-update)
1517 (if (and opoint (< opoint omax))
1518 (goto-char opoint))
1519 (with-no-warnings
1520 (if compilation-finish-function
1521 (funcall compilation-finish-function cur-buffer msg)))
1522 (run-hook-with-args 'compilation-finish-functions cur-buffer msg)))
1523
1524 ;; Called when compilation process changes state.
1525 (defun compilation-sentinel (proc msg)
1526 "Sentinel for compilation buffers."
1527 (if (memq (process-status proc) '(exit signal))
1528 (let ((buffer (process-buffer proc)))
1529 (if (null (buffer-name buffer))
1530 ;; buffer killed
1531 (set-process-buffer proc nil)
1532 (with-current-buffer buffer
1533 ;; Write something in the compilation buffer
1534 ;; and hack its mode line.
1535 (compilation-handle-exit (process-status proc)
1536 (process-exit-status proc)
1537 msg)
1538 ;; Since the buffer and mode line will show that the
1539 ;; process is dead, we can delete it now. Otherwise it
1540 ;; will stay around until M-x list-processes.
1541 (delete-process proc)))
1542 (setq compilation-in-progress (delq proc compilation-in-progress)))))
1543
1544 (defun compilation-filter (proc string)
1545 "Process filter for compilation buffers.
1546 Just inserts the text, but uses `insert-before-markers'."
1547 (if (buffer-name (process-buffer proc))
1548 (with-current-buffer (process-buffer proc)
1549 (let ((inhibit-read-only t))
1550 (save-excursion
1551 (goto-char (process-mark proc))
1552 (insert-before-markers string)
1553 (run-hooks 'compilation-filter-hook))))))
1554
1555 ;;; test if a buffer is a compilation buffer, assuming we're in the buffer
1556 (defsubst compilation-buffer-internal-p ()
1557 "Test if inside a compilation buffer."
1558 (local-variable-p 'compilation-locs))
1559
1560 ;;; test if a buffer is a compilation buffer, using compilation-buffer-internal-p
1561 (defsubst compilation-buffer-p (buffer)
1562 "Test if BUFFER is a compilation buffer."
1563 (with-current-buffer buffer
1564 (compilation-buffer-internal-p)))
1565
1566 (defmacro compilation-loop (< property-change 1+ error limit)
1567 `(let (opt)
1568 (while (,< n 0)
1569 (setq opt pt)
1570 (or (setq pt (,property-change pt 'message))
1571 ;; Handle the case where where the first error message is
1572 ;; at the start of the buffer, and n < 0.
1573 (if (or (eq (get-text-property ,limit 'message)
1574 (get-text-property opt 'message))
1575 (eq pt opt))
1576 (error ,error compilation-error)
1577 (setq pt ,limit)))
1578 ;; prop 'message usually has 2 changes, on and off, so
1579 ;; re-search if off
1580 (or (setq msg (get-text-property pt 'message))
1581 (if (setq pt (,property-change pt 'message nil ,limit))
1582 (setq msg (get-text-property pt 'message)))
1583 (error ,error compilation-error))
1584 (or (< (cadr msg) compilation-skip-threshold)
1585 (if different-file
1586 (eq (prog1 last (setq last (nth 2 (car msg))))
1587 last))
1588 (if compilation-skip-visited
1589 (nthcdr 5 (car msg)))
1590 (if compilation-skip-to-next-location
1591 (eq (car msg) loc))
1592 ;; count this message only if none of the above are true
1593 (setq n (,1+ n))))))
1594
1595 (defun compilation-next-error (n &optional different-file pt)
1596 "Move point to the next error in the compilation buffer.
1597 Prefix arg N says how many error messages to move forwards (or
1598 backwards, if negative).
1599 Does NOT find the source line like \\[next-error]."
1600 (interactive "p")
1601 (or (compilation-buffer-p (current-buffer))
1602 (error "Not in a compilation buffer"))
1603 (or pt (setq pt (point)))
1604 (let* ((msg (get-text-property pt 'message))
1605 (loc (car msg))
1606 last)
1607 (if (zerop n)
1608 (unless (or msg ; find message near here
1609 (setq msg (get-text-property (max (1- pt) (point-min))
1610 'message)))
1611 (setq pt (previous-single-property-change pt 'message nil
1612 (line-beginning-position)))
1613 (unless (setq msg (get-text-property (max (1- pt) (point-min)) 'message))
1614 (setq pt (next-single-property-change pt 'message nil
1615 (line-end-position)))
1616 (or (setq msg (get-text-property pt 'message))
1617 (setq pt (point)))))
1618 (setq last (nth 2 (car msg)))
1619 (if (>= n 0)
1620 (compilation-loop > next-single-property-change 1-
1621 (if (get-buffer-process (current-buffer))
1622 "No more %ss yet"
1623 "Moved past last %s")
1624 (point-max))
1625 ;; Don't move "back" to message at or before point.
1626 ;; Pass an explicit (point-min) to make sure pt is non-nil.
1627 (setq pt (previous-single-property-change pt 'message nil (point-min)))
1628 (compilation-loop < previous-single-property-change 1+
1629 "Moved back before first %s" (point-min))))
1630 (goto-char pt)
1631 (or msg
1632 (error "No %s here" compilation-error))))
1633
1634 (defun compilation-previous-error (n)
1635 "Move point to the previous error in the compilation buffer.
1636 Prefix arg N says how many error messages to move backwards (or
1637 forwards, if negative).
1638 Does NOT find the source line like \\[previous-error]."
1639 (interactive "p")
1640 (compilation-next-error (- n)))
1641
1642 (defun compilation-next-file (n)
1643 "Move point to the next error for a different file than the current one.
1644 Prefix arg N says how many files to move forwards (or backwards, if negative)."
1645 (interactive "p")
1646 (compilation-next-error n t))
1647
1648 (defun compilation-previous-file (n)
1649 "Move point to the previous error for a different file than the current one.
1650 Prefix arg N says how many files to move backwards (or forwards, if negative)."
1651 (interactive "p")
1652 (compilation-next-file (- n)))
1653
1654 (defun kill-compilation ()
1655 "Kill the process made by the \\[compile] or \\[grep] commands."
1656 (interactive)
1657 (let ((buffer (compilation-find-buffer)))
1658 (if (get-buffer-process buffer)
1659 (interrupt-process (get-buffer-process buffer))
1660 (error "The %s process is not running" (downcase mode-name)))))
1661
1662 (defalias 'compile-mouse-goto-error 'compile-goto-error)
1663
1664 (defun compile-goto-error (&optional event)
1665 "Visit the source for the error message at point.
1666 Use this command in a compilation log buffer. Sets the mark at point there."
1667 (interactive (list last-input-event))
1668 (if event (posn-set-point (event-end event)))
1669 (or (compilation-buffer-p (current-buffer))
1670 (error "Not in a compilation buffer"))
1671 (if (get-text-property (point) 'directory)
1672 (dired-other-window (car (get-text-property (point) 'directory)))
1673 (push-mark)
1674 (setq compilation-current-error (point))
1675 (next-error-internal)))
1676
1677 (defun compilation-find-buffer (&optional avoid-current)
1678 "Return a compilation buffer.
1679 If AVOID-CURRENT is nil, and the current buffer is a compilation buffer,
1680 return it. If AVOID-CURRENT is non-nil, return the current buffer only
1681 as a last resort."
1682 (if (and (compilation-buffer-internal-p) (not avoid-current))
1683 (current-buffer)
1684 (next-error-find-buffer avoid-current 'compilation-buffer-internal-p)))
1685
1686 ;;;###autoload
1687 (defun compilation-next-error-function (n &optional reset)
1688 "Advance to the next error message and visit the file where the error was.
1689 This is the value of `next-error-function' in Compilation buffers."
1690 (interactive "p")
1691 (when reset
1692 (setq compilation-current-error nil))
1693 (let* ((columns compilation-error-screen-columns) ; buffer's local value
1694 (last 1) timestamp
1695 (loc (compilation-next-error (or n 1) nil
1696 (or compilation-current-error
1697 compilation-messages-start
1698 (point-min))))
1699 (end-loc (nth 2 loc))
1700 (marker (point-marker)))
1701 (setq compilation-current-error (point-marker)
1702 overlay-arrow-position
1703 (if (bolp)
1704 compilation-current-error
1705 (copy-marker (line-beginning-position)))
1706 loc (car loc))
1707 ;; If loc contains no marker, no error in that file has been visited.
1708 ;; If the marker is invalid the buffer has been killed.
1709 ;; If the file is newer than the timestamp, it has been modified
1710 ;; (`omake -P' polls filesystem for changes and recompiles when needed
1711 ;; in the same process and buffer).
1712 ;; So, recalculate all markers for that file.
1713 (unless (and (nth 3 loc) (marker-buffer (nth 3 loc))
1714 ;; There may be no timestamp info if the loc is a `fake-loc'.
1715 ;; So we skip the time-check here, although we should maybe
1716 ;; change `compilation-fake-loc' to add timestamp info.
1717 (or (null (nth 4 loc))
1718 (equal (nth 4 loc)
1719 (setq timestamp
1720 (with-current-buffer
1721 (marker-buffer (nth 3 loc))
1722 (visited-file-modtime))))))
1723 (with-current-buffer (compilation-find-file marker (caar (nth 2 loc))
1724 (cadr (car (nth 2 loc))))
1725 (save-restriction
1726 (widen)
1727 (goto-char (point-min))
1728 ;; Treat file's found lines in forward order, 1 by 1.
1729 (dolist (line (reverse (cddr (nth 2 loc))))
1730 (when (car line) ; else this is a filename w/o a line#
1731 (beginning-of-line (- (car line) last -1))
1732 (setq last (car line)))
1733 ;; Treat line's found columns and store/update a marker for each.
1734 (dolist (col (cdr line))
1735 (if (car col)
1736 (if (eq (car col) -1) ; special case for range end
1737 (end-of-line)
1738 (compilation-move-to-column (car col) columns))
1739 (beginning-of-line)
1740 (skip-chars-forward " \t"))
1741 (if (nth 3 col)
1742 (set-marker (nth 3 col) (point))
1743 (setcdr (nthcdr 2 col) `(,(point-marker)))))))))
1744 (compilation-goto-locus marker (nth 3 loc) (nth 3 end-loc))
1745 (setcdr (nthcdr 3 loc) (list timestamp))
1746 (setcdr (nthcdr 4 loc) t))) ; Set this one as visited.
1747
1748 (defvar compilation-gcpro nil
1749 "Internal variable used to keep some values from being GC'd.")
1750 (make-variable-buffer-local 'compilation-gcpro)
1751
1752 (defun compilation-fake-loc (marker file &optional line col)
1753 "Preassociate MARKER with FILE.
1754 FILE should be ABSOLUTE-FILENAME or (RELATIVE-FILENAME . DIRNAME).
1755 This is useful when you compile temporary files, but want
1756 automatic translation of the messages to the real buffer from
1757 which the temporary file came. This only works if done before a
1758 message about FILE appears!
1759
1760 Optional args LINE and COL default to 1 and beginning of
1761 indentation respectively. The marker is expected to reflect
1762 this. In the simplest case the marker points to the first line
1763 of the region that was saved to the temp file.
1764
1765 If you concatenate several regions into the temp file (e.g. a
1766 header with variable assignments and a code region), you must
1767 call this several times, once each for the last line of one
1768 region and the first line of the next region."
1769 (or (consp file) (setq file (list file)))
1770 (setq file (compilation-get-file-structure file))
1771 ;; Between the current call to compilation-fake-loc and the first occurrence
1772 ;; of an error message referring to `file', the data is only kept in the
1773 ;; weak hash-table compilation-locs, so we need to prevent this entry
1774 ;; in compilation-locs from being GC'd away. --Stef
1775 (push file compilation-gcpro)
1776 (let ((loc (compilation-assq (or line 1) (cdr file))))
1777 (setq loc (compilation-assq col loc))
1778 (if (cdr loc)
1779 (setcdr (cddr loc) (list marker))
1780 (setcdr loc (list line file marker)))
1781 loc))
1782
1783 (defcustom compilation-context-lines nil
1784 "Display this many lines of leading context before the current message.
1785 If nil and the left fringe is displayed, don't scroll the
1786 compilation output window; an arrow in the left fringe points to
1787 the current message. If nil and there is no left fringe, the message
1788 displays at the top of the window; there is no arrow."
1789 :type '(choice integer (const :tag "No window scrolling" nil))
1790 :group 'compilation
1791 :version "22.1")
1792
1793 (defsubst compilation-set-window (w mk)
1794 "Align the compilation output window W with marker MK near top."
1795 (if (integerp compilation-context-lines)
1796 (set-window-start w (save-excursion
1797 (goto-char mk)
1798 (beginning-of-line
1799 (- 1 compilation-context-lines))
1800 (point)))
1801 ;; If there is no left fringe.
1802 (if (equal (car (window-fringes)) 0)
1803 (set-window-start w (save-excursion
1804 (goto-char mk)
1805 (beginning-of-line 1)
1806 (point)))))
1807 (set-window-point w mk))
1808
1809 (defvar next-error-highlight-timer)
1810
1811 (defun compilation-goto-locus (msg mk end-mk)
1812 "Jump to an error corresponding to MSG at MK.
1813 All arguments are markers. If END-MK is non-nil, mark is set there
1814 and overlay is highlighted between MK and END-MK."
1815 ;; Show compilation buffer in other window, scrolled to this error.
1816 (let* ((from-compilation-buffer (eq (window-buffer (selected-window))
1817 (marker-buffer msg)))
1818 ;; Use an existing window if it is in a visible frame.
1819 (pre-existing (get-buffer-window (marker-buffer msg) 0))
1820 (w (if (and from-compilation-buffer pre-existing)
1821 ;; Calling display-buffer here may end up (partly) hiding
1822 ;; the error location if the two buffers are in two
1823 ;; different frames. So don't do it if it's not necessary.
1824 pre-existing
1825 (let ((display-buffer-reuse-frames t)
1826 (pop-up-windows t))
1827 ;; Pop up a window.
1828 (display-buffer (marker-buffer msg)))))
1829 (highlight-regexp (with-current-buffer (marker-buffer msg)
1830 ;; also do this while we change buffer
1831 (compilation-set-window w msg)
1832 compilation-highlight-regexp)))
1833 ;; Ideally, the window-size should be passed to `display-buffer' (via
1834 ;; something like special-display-buffer) so it's only used when
1835 ;; creating a new window.
1836 (unless pre-existing (compilation-set-window-height w))
1837
1838 (if from-compilation-buffer
1839 ;; If the compilation buffer window was selected,
1840 ;; keep the compilation buffer in this window;
1841 ;; display the source in another window.
1842 (let ((pop-up-windows t))
1843 (pop-to-buffer (marker-buffer mk) 'other-window))
1844 (if (window-dedicated-p (selected-window))
1845 (pop-to-buffer (marker-buffer mk))
1846 (switch-to-buffer (marker-buffer mk))))
1847 ;; If narrowing gets in the way of going to the right place, widen.
1848 (unless (eq (goto-char mk) (point))
1849 (widen)
1850 (goto-char mk))
1851 (if end-mk
1852 (push-mark end-mk t)
1853 (if mark-active (setq mark-active)))
1854 ;; If hideshow got in the way of
1855 ;; seeing the right place, open permanently.
1856 (dolist (ov (overlays-at (point)))
1857 (when (eq 'hs (overlay-get ov 'invisible))
1858 (delete-overlay ov)
1859 (goto-char mk)))
1860
1861 (when highlight-regexp
1862 (if (timerp next-error-highlight-timer)
1863 (cancel-timer next-error-highlight-timer))
1864 (unless compilation-highlight-overlay
1865 (setq compilation-highlight-overlay
1866 (make-overlay (point-min) (point-min)))
1867 (overlay-put compilation-highlight-overlay 'face 'next-error))
1868 (with-current-buffer (marker-buffer mk)
1869 (save-excursion
1870 (if end-mk (goto-char end-mk) (end-of-line))
1871 (let ((end (point)))
1872 (if mk (goto-char mk) (beginning-of-line))
1873 (if (and (stringp highlight-regexp)
1874 (re-search-forward highlight-regexp end t))
1875 (progn
1876 (goto-char (match-beginning 0))
1877 (move-overlay compilation-highlight-overlay
1878 (match-beginning 0) (match-end 0)
1879 (current-buffer)))
1880 (move-overlay compilation-highlight-overlay
1881 (point) end (current-buffer)))
1882 (if (or (eq next-error-highlight t)
1883 (numberp next-error-highlight))
1884 ;; We want highlighting: delete overlay on next input.
1885 (add-hook 'pre-command-hook
1886 'compilation-goto-locus-delete-o)
1887 ;; We don't want highlighting: delete overlay now.
1888 (delete-overlay compilation-highlight-overlay))
1889 ;; We want highlighting for a limited time:
1890 ;; set up a timer to delete it.
1891 (when (numberp next-error-highlight)
1892 (setq next-error-highlight-timer
1893 (run-at-time next-error-highlight nil
1894 'compilation-goto-locus-delete-o)))))))
1895 (when (and (eq next-error-highlight 'fringe-arrow))
1896 ;; We want a fringe arrow (instead of highlighting).
1897 (setq next-error-overlay-arrow-position
1898 (copy-marker (line-beginning-position))))))
1899
1900 (defun compilation-goto-locus-delete-o ()
1901 (delete-overlay compilation-highlight-overlay)
1902 ;; Get rid of timer and hook that would try to do this again.
1903 (if (timerp next-error-highlight-timer)
1904 (cancel-timer next-error-highlight-timer))
1905 (remove-hook 'pre-command-hook
1906 'compilation-goto-locus-delete-o))
1907 \f
1908 (defun compilation-find-file (marker filename directory &rest formats)
1909 "Find a buffer for file FILENAME.
1910 Search the directories in `compilation-search-path'.
1911 A nil in `compilation-search-path' means to try the
1912 \"current\" directory, which is passed in DIRECTORY.
1913 If DIRECTORY is relative, it is combined with `default-directory'.
1914 If DIRECTORY is nil, that means use `default-directory'.
1915 If FILENAME is not found at all, ask the user where to find it.
1916 Pop up the buffer containing MARKER and scroll to MARKER if we ask the user."
1917 (or formats (setq formats '("%s")))
1918 (let ((dirs compilation-search-path)
1919 (spec-dir (if directory
1920 (expand-file-name directory)
1921 default-directory))
1922 buffer thisdir fmts name)
1923 (if (file-name-absolute-p filename)
1924 ;; The file name is absolute. Use its explicit directory as
1925 ;; the first in the search path, and strip it from FILENAME.
1926 (setq filename (abbreviate-file-name (expand-file-name filename))
1927 dirs (cons (file-name-directory filename) dirs)
1928 filename (file-name-nondirectory filename)))
1929 ;; Now search the path.
1930 (while (and dirs (null buffer))
1931 (setq thisdir (or (car dirs) spec-dir)
1932 fmts formats)
1933 ;; For each directory, try each format string.
1934 (while (and fmts (null buffer))
1935 (setq name (expand-file-name (format (car fmts) filename) thisdir)
1936 buffer (and (file-exists-p name)
1937 (find-file-noselect name))
1938 fmts (cdr fmts)))
1939 (setq dirs (cdr dirs)))
1940 (while (null buffer) ;Repeat until the user selects an existing file.
1941 ;; The file doesn't exist. Ask the user where to find it.
1942 (save-excursion ;This save-excursion is probably not right.
1943 (let ((pop-up-windows t))
1944 (compilation-set-window (display-buffer (marker-buffer marker))
1945 marker)
1946 (let* ((name (read-file-name
1947 (format "Find this %s in (default %s): "
1948 compilation-error filename)
1949 spec-dir filename t nil
1950 ;; The predicate below is fine when called from
1951 ;; minibuffer-complete-and-exit, but it's too
1952 ;; restrictive otherwise, since it also prevents the
1953 ;; user from completing "fo" to "foo/" when she
1954 ;; wants to enter "foo/bar".
1955 ;;
1956 ;; Try to make sure the user can only select
1957 ;; a valid answer. This predicate may be ignored,
1958 ;; tho, so we still have to double-check afterwards.
1959 ;; TODO: We should probably fix read-file-name so
1960 ;; that it never ignores this predicate, even when
1961 ;; using popup dialog boxes.
1962 ;; (lambda (name)
1963 ;; (if (file-directory-p name)
1964 ;; (setq name (expand-file-name filename name)))
1965 ;; (file-exists-p name))
1966 ))
1967 (origname name))
1968 (cond
1969 ((not (file-exists-p name))
1970 (message "Cannot find file `%s'" name)
1971 (ding) (sit-for 2))
1972 ((and (file-directory-p name)
1973 (not (file-exists-p
1974 (setq name (expand-file-name filename name)))))
1975 (message "No `%s' in directory %s" filename origname)
1976 (ding) (sit-for 2))
1977 (t
1978 (setq buffer (find-file-noselect name))))))))
1979 ;; Make intangible overlays tangible.
1980 ;; This is weird: it's not even clear which is the current buffer,
1981 ;; so the code below can't be expected to DTRT here. -- Stef
1982 (dolist (ov (overlays-in (point-min) (point-max)))
1983 (when (overlay-get ov 'intangible)
1984 (overlay-put ov 'intangible nil)))
1985 buffer))
1986
1987 (defun compilation-get-file-structure (file &optional fmt)
1988 "Retrieve FILE's file-structure or create a new one.
1989 FILE should be (FILENAME) or (RELATIVE-FILENAME . DIRNAME).
1990 In the former case, FILENAME may be relative or absolute.
1991
1992 The file-structure looks like this:
1993 (list (list FILENAME [DIR-FROM-PREV-MSG]) FMT LINE-STRUCT...)"
1994 (or (gethash file compilation-locs)
1995 ;; File was not previously encountered, at least not in the form passed.
1996 ;; Let's normalize it and look again.
1997 (let ((filename (car file))
1998 ;; Get the specified directory from FILE.
1999 (spec-directory (if (cdr file)
2000 (file-truename (cdr file)))))
2001
2002 ;; Check for a comint-file-name-prefix and prepend it if appropriate.
2003 ;; (This is very useful for compilation-minor-mode in an rlogin-mode
2004 ;; buffer.)
2005 (when (and (boundp 'comint-file-name-prefix)
2006 (not (equal comint-file-name-prefix "")))
2007 (if (file-name-absolute-p filename)
2008 (setq filename
2009 (concat comint-file-name-prefix filename))
2010 (if spec-directory
2011 (setq spec-directory
2012 (file-truename
2013 (concat comint-file-name-prefix spec-directory))))))
2014
2015 ;; If compilation-parse-errors-filename-function is
2016 ;; defined, use it to process the filename.
2017 (when compilation-parse-errors-filename-function
2018 (setq filename
2019 (funcall compilation-parse-errors-filename-function
2020 filename)))
2021
2022 ;; Some compilers (e.g. Sun's java compiler, reportedly) produce bogus
2023 ;; file names like "./bar//foo.c" for file "bar/foo.c";
2024 ;; expand-file-name will collapse these into "/foo.c" and fail to find
2025 ;; the appropriate file. So we look for doubled slashes in the file
2026 ;; name and fix them.
2027 (setq filename (command-line-normalize-file-name filename))
2028
2029 ;; Store it for the possibly unnormalized name
2030 (puthash file
2031 ;; Retrieve or create file-structure for normalized name
2032 ;; The gethash used to not use spec-directory, but
2033 ;; this leads to errors when files in different
2034 ;; directories have the same name:
2035 ;; http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00463.html
2036 (or (gethash (cons filename spec-directory) compilation-locs)
2037 (puthash (cons filename spec-directory)
2038 (list (list filename spec-directory) fmt)
2039 compilation-locs))
2040 compilation-locs))))
2041
2042 (add-to-list 'debug-ignored-errors "^No more [-a-z ]+s yet$")
2043
2044 ;;; Compatibility with the old compile.el.
2045
2046 (defun compile-buffer-substring (n) (if n (match-string n)))
2047
2048 (defun compilation-compat-error-properties (err)
2049 "Map old-style error ERR to new-style message."
2050 ;; Old-style structure is (MARKER (FILE DIR) LINE COL) or
2051 ;; (MARKER . MARKER).
2052 (let ((dst (cdr err)))
2053 (if (markerp dst)
2054 ;; Must start with a face, for font-lock.
2055 `(face nil
2056 message ,(list (list nil nil nil dst) 2)
2057 help-echo "mouse-2: visit the source location"
2058 keymap compilation-button-map
2059 mouse-face highlight)
2060 ;; Too difficult to do it by hand: dispatch to the normal code.
2061 (let* ((file (pop dst))
2062 (line (pop dst))
2063 (col (pop dst))
2064 (filename (pop file))
2065 (dirname (pop file))
2066 (fmt (pop file)))
2067 (compilation-internal-error-properties
2068 (cons filename dirname) line nil col nil 2 fmt)))))
2069
2070 (defun compilation-compat-parse-errors (limit)
2071 (when compilation-parse-errors-function
2072 ;; FIXME: We should remove the rest of the compilation keywords
2073 ;; but we can't do that from here because font-lock is using
2074 ;; the value right now. --stef
2075 (save-excursion
2076 (setq compilation-error-list nil)
2077 ;; Reset compilation-parsing-end each time because font-lock
2078 ;; might force us the re-parse many times (typically because
2079 ;; some code adds some text-property to the output that we
2080 ;; already parsed). You might say "why reparse", well:
2081 ;; because font-lock has just removed the `message' property so
2082 ;; have to do it all over again.
2083 (if compilation-parsing-end
2084 (set-marker compilation-parsing-end (point))
2085 (setq compilation-parsing-end (point-marker)))
2086 (condition-case nil
2087 ;; Ignore any error: we're calling this function earlier than
2088 ;; in the old compile.el so things might not all be setup yet.
2089 (funcall compilation-parse-errors-function limit nil)
2090 (error nil))
2091 (dolist (err (if (listp compilation-error-list) compilation-error-list))
2092 (let* ((src (car err))
2093 (dst (cdr err))
2094 (loc (cond ((markerp dst) (list nil nil nil dst))
2095 ((consp dst)
2096 (list (nth 2 dst) (nth 1 dst)
2097 (cons (cdar dst) (caar dst)))))))
2098 (when loc
2099 (goto-char src)
2100 ;; (put-text-property src (line-end-position) 'font-lock-face 'font-lock-warning-face)
2101 (put-text-property src (line-end-position)
2102 'message (list loc 2)))))))
2103 (goto-char limit)
2104 nil)
2105
2106 ;; Beware: this is not only compatiblity code. New code stil uses it. --Stef
2107 (defun compilation-forget-errors ()
2108 ;; In case we hit the same file/line specs, we want to recompute a new
2109 ;; marker for them, so flush our cache.
2110 (setq compilation-locs (make-hash-table :test 'equal :weakness 'value))
2111 (setq compilation-gcpro nil)
2112 ;; FIXME: the old code reset the directory-stack, so maybe we should
2113 ;; put a `directory change' marker of some sort, but where? -stef
2114 ;;
2115 ;; FIXME: The old code moved compilation-current-error (which was
2116 ;; virtually represented by a mix of compilation-parsing-end and
2117 ;; compilation-error-list) to point-min, but that was only meaningful for
2118 ;; the internal uses of compilation-forget-errors: all calls from external
2119 ;; packages seem to be followed by a move of compilation-parsing-end to
2120 ;; something equivalent to point-max. So we heuristically move
2121 ;; compilation-current-error to point-max (since the external package
2122 ;; won't know that it should do it). --Stef
2123 (setq compilation-current-error nil)
2124 (let* ((proc (get-buffer-process (current-buffer)))
2125 (mark (if proc (process-mark proc)))
2126 (pos (or mark (point-max))))
2127 (setq compilation-messages-start
2128 ;; In the future, ignore the text already present in the buffer.
2129 ;; Since many process filter functions insert before markers,
2130 ;; we need to put ours just before the insertion point rather
2131 ;; than at the insertion point. If that's not possible, then
2132 ;; don't use a marker. --Stef
2133 (if (> pos (point-min)) (copy-marker (1- pos)) pos)))
2134 ;; Again, since this command is used in buffers that contain several
2135 ;; compilations, to set the beginning of "this compilation", it's a good
2136 ;; place to reset compilation-auto-jump-to-next.
2137 (set (make-local-variable 'compilation-auto-jump-to-next)
2138 compilation-auto-jump-to-first-error))
2139
2140 ;;;###autoload
2141 (add-to-list 'auto-mode-alist '("\\.gcov\\'" . compilation-mode))
2142
2143 (provide 'compile)
2144
2145 ;; arch-tag: 12465727-7382-4f72-b234-79855a00dd8c
2146 ;;; compile.el ends here