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