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