Merge changes from emacs-23 branch
[emacs.git] / lisp / progmodes / compile.el
blob40383c6bc31def148a482c0d4a65df0f66c8306d
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 (null (marker-buffer (cdr compilation--previous-directory-cache)))
842 (<= (cdr compilation--previous-directory-cache) start))
843 (set-marker (car compilation--previous-directory-cache) start))
844 (t (setq compilation--previous-directory-cache nil))))
846 (defun compilation--previous-directory (pos)
847 "Like (previous-single-property-change POS 'compilation-directory), but faster."
848 ;; This avoids an N² behavior when there's no/few compilation-directory
849 ;; entries, in which case each call to previous-single-property-change
850 ;; ends up having to walk very far back to find the last change.
851 (if (and compilation--previous-directory-cache
852 (< pos (car compilation--previous-directory-cache))
853 (or (null (cdr compilation--previous-directory-cache))
854 (< (cdr compilation--previous-directory-cache) pos)))
855 ;; No need to call previous-single-property-change.
856 (cdr compilation--previous-directory-cache)
858 (let* ((cache (and compilation--previous-directory-cache
859 (<= (car compilation--previous-directory-cache) pos)
860 (car compilation--previous-directory-cache)))
861 (prev
862 (previous-single-property-change
863 pos 'compilation-directory nil cache))
864 (res
865 (cond
866 ((null cache)
867 (setq compilation--previous-directory-cache
868 (cons (copy-marker pos) (if prev (copy-marker prev))))
869 prev)
870 ((and prev (= prev cache))
871 (if cache
872 (set-marker (car compilation--previous-directory-cache) pos)
873 (setq compilation--previous-directory-cache
874 (cons (copy-marker pos) nil)))
875 (cdr compilation--previous-directory-cache))
877 (if cache
878 (progn
879 (set-marker cache pos)
880 (setcdr compilation--previous-directory-cache
881 (copy-marker prev)))
882 (setq compilation--previous-directory-cache
883 (cons (copy-marker pos) (if prev (copy-marker prev)))))
884 prev))))
885 (if (markerp res) (marker-position res) res))))
887 ;; Internal function for calculating the text properties of a directory
888 ;; change message. The compilation-directory property is important, because it
889 ;; is the stack of nested enter-messages. Relative filenames on the following
890 ;; lines are relative to the top of the stack.
891 (defun compilation-directory-properties (idx leave)
892 (if leave (setq leave (match-end leave)))
893 ;; find previous stack, and push onto it, or if `leave' pop it
894 (let ((dir (compilation--previous-directory (match-beginning 0))))
895 (setq dir (if dir (or (get-text-property (1- dir) 'compilation-directory)
896 (get-text-property dir 'compilation-directory))))
897 `(font-lock-face ,(if leave
898 compilation-leave-directory-face
899 compilation-enter-directory-face)
900 compilation-directory ,(if leave
901 (or (cdr dir)
902 '(nil)) ; nil only isn't a property-change
903 (cons (match-string-no-properties idx) dir))
904 ;; Place a `compilation-message' everywhere we change text-properties
905 ;; so compilation--remove-properties can know what to remove.
906 compilation-message ,(compilation--make-message nil 0 nil)
907 mouse-face highlight
908 keymap compilation-button-map
909 help-echo "mouse-2: visit destination directory")))
911 ;; Data type `reverse-ordered-alist' retriever. This function retrieves the
912 ;; KEY element from the ALIST, creating it in the right position if not already
913 ;; present. ALIST structure is
914 ;; '(ANCHOR (KEY1 ...) (KEY2 ...)... (KEYn ALIST ...))
915 ;; ANCHOR is ignored, but necessary so that elements can be inserted. KEY1
916 ;; may be nil. The other KEYs are ordered backwards so that growing line
917 ;; numbers can be inserted in front and searching can abort after half the
918 ;; list on average.
919 (eval-when-compile ;Don't keep it at runtime if not needed.
920 (defmacro compilation-assq (key alist)
921 `(let* ((l1 ,alist)
922 (l2 (cdr l1)))
923 (car (if (if (null ,key)
924 (if l2 (null (caar l2)))
925 (while (if l2 (if (caar l2) (< ,key (caar l2)) t))
926 (setq l1 l2
927 l2 (cdr l1)))
928 (if l2 (eq ,key (caar l2))))
930 (setcdr l1 (cons (list ,key) l2)))))))
932 (defun compilation-auto-jump (buffer pos)
933 (with-current-buffer buffer
934 (goto-char pos)
935 (let ((win (get-buffer-window buffer 0)))
936 (if win (set-window-point win pos)))
937 (if compilation-auto-jump-to-first-error
938 (compile-goto-error))))
940 ;; This function is the central driver, called when font-locking to gather
941 ;; all information needed to later jump to corresponding source code.
942 ;; Return a property list with all meta information on this error location.
944 (defun compilation-error-properties (file line end-line col end-col type fmt)
945 (unless (text-property-not-all (match-beginning 0) (point)
946 'compilation-message nil)
947 (if file
948 (when (stringp
949 (setq file (if (functionp file) (funcall file)
950 (match-string-no-properties file))))
951 (let ((dir
952 (unless (file-name-absolute-p file)
953 (let ((pos (compilation--previous-directory
954 (match-beginning 0))))
955 (when pos
956 (or (get-text-property (1- pos) 'compilation-directory)
957 (get-text-property pos 'compilation-directory)))))))
958 (setq file (cons file (car dir)))))
959 ;; This message didn't mention one, get it from previous
960 (let ((prev-pos
961 ;; Find the previous message.
962 (previous-single-property-change (point) 'compilation-message)))
963 (if prev-pos
964 ;; Get the file structure that belongs to it.
965 (let* ((prev
966 (or (get-text-property (1- prev-pos) 'compilation-message)
967 (get-text-property prev-pos 'compilation-message)))
968 (prev-struct
969 (car (nth 2 (car prev)))))
970 ;; Construct FILE . DIR from that.
971 (if prev-struct
972 (setq file (cons (car prev-struct)
973 (cadr prev-struct))))))
974 (unless file
975 (setq file '("*unknown*")))))
976 ;; All of these fields are optional, get them only if we have an index, and
977 ;; it matched some part of the message.
978 (and line
979 (setq line (match-string-no-properties line))
980 (setq line (string-to-number line)))
981 (and end-line
982 (setq end-line (match-string-no-properties end-line))
983 (setq end-line (string-to-number end-line)))
984 (if col
985 (if (functionp col)
986 (setq col (funcall col))
987 (and
988 (setq col (match-string-no-properties col))
989 (setq col (- (string-to-number col) compilation-first-column)))))
990 (if (and end-col (functionp end-col))
991 (setq end-col (funcall end-col))
992 (if (and end-col (setq end-col (match-string-no-properties end-col)))
993 (setq end-col (- (string-to-number end-col) compilation-first-column -1))
994 (if end-line (setq end-col -1))))
995 (if (consp type) ; not a static type, check what it is.
996 (setq type (or (and (car type) (match-end (car type)) 1)
997 (and (cdr type) (match-end (cdr type)) 0)
998 2)))
1000 (when (and compilation-auto-jump-to-next
1001 (>= type compilation-skip-threshold))
1002 (kill-local-variable 'compilation-auto-jump-to-next)
1003 (run-with-timer 0 nil 'compilation-auto-jump
1004 (current-buffer) (match-beginning 0)))
1006 (compilation-internal-error-properties
1007 file line end-line col end-col type fmt)))
1009 (defun compilation-move-to-column (col screen)
1010 "Go to column COL on the current line.
1011 If SCREEN is non-nil, columns are screen columns, otherwise, they are
1012 just char-counts."
1013 (if screen
1014 (move-to-column (max col 0))
1015 (goto-char (min (+ (line-beginning-position) col) (line-end-position)))))
1017 (defun compilation-internal-error-properties (file line end-line col end-col type fmts)
1018 "Get the meta-info that will be added as text-properties.
1019 LINE, END-LINE, COL, END-COL are integers or nil.
1020 TYPE can be 0, 1, or 2, meaning error, warning, or just info.
1021 FILE should be (FILENAME) or (RELATIVE-FILENAME . DIRNAME) or nil.
1022 FMTS is a list of format specs for transforming the file name.
1023 (See `compilation-error-regexp-alist'.)"
1024 (unless file (setq file '("*unknown*")))
1025 (let* ((file-struct (compilation-get-file-structure file fmts))
1026 ;; Get first already existing marker (if any has one, all have one).
1027 ;; Do this first, as the compilation-assq`s may create new nodes.
1028 (marker-line ; a line structure
1029 (cadr (compilation--file-struct->loc-tree file-struct)))
1030 (marker
1031 (if marker-line (compilation--loc->marker (cadr marker-line))))
1032 (compilation-error-screen-columns compilation-error-screen-columns)
1033 end-marker loc end-loc)
1034 (if (not (and marker (marker-buffer marker)))
1035 (setq marker nil) ; no valid marker for this file
1036 (setq loc (or line 1)) ; normalize no linenumber to line 1
1037 (catch 'marker ; find nearest loc, at least one exists
1038 (dolist (x (cddr (compilation--file-struct->loc-tree
1039 file-struct))) ; Loop over remaining lines.
1040 (if (> (car x) loc) ; still bigger
1041 (setq marker-line x)
1042 (if (> (- (or (car marker-line) 1) loc)
1043 (- loc (car x))) ; current line is nearer
1044 (setq marker-line x))
1045 (throw 'marker t))))
1046 (setq marker (compilation--loc->marker (cadr marker-line))
1047 marker-line (or (car marker-line) 1))
1048 (with-current-buffer (marker-buffer marker)
1049 (save-excursion
1050 (save-restriction
1051 (widen)
1052 (goto-char (marker-position marker))
1053 (when (or end-col end-line)
1054 (beginning-of-line (- (or end-line line) marker-line -1))
1055 (if (or (null end-col) (< end-col 0))
1056 (end-of-line)
1057 (compilation-move-to-column
1058 end-col compilation-error-screen-columns))
1059 (setq end-marker (point-marker)))
1060 (beginning-of-line (if end-line
1061 (- line end-line -1)
1062 (- loc marker-line -1)))
1063 (if col
1064 (compilation-move-to-column
1065 col compilation-error-screen-columns)
1066 (forward-to-indentation 0))
1067 (setq marker (point-marker))))))
1069 (setq loc (compilation-assq line (compilation--file-struct->loc-tree
1070 file-struct)))
1071 (setq end-loc
1072 (if end-line
1073 (compilation-assq
1074 end-col (compilation-assq
1075 end-line (compilation--file-struct->loc-tree
1076 file-struct)))
1077 (if end-col ; use same line element
1078 (compilation-assq end-col loc))))
1079 (setq loc (compilation-assq col loc))
1080 ;; If they are new, make the loc(s) reference the file they point to.
1081 ;; FIXME-omake: there's a problem with timestamps here: the markers
1082 ;; relative to which we computed the current `marker' have a timestamp
1083 ;; almost guaranteed to be different from compilation-buffer-modtime, so if
1084 ;; we use their timestamp, we'll never use `loc' since the timestamp won't
1085 ;; match compilation-buffer-modtime, and if we use
1086 ;; compilation-buffer-modtime then we have different timestamps for
1087 ;; locations that were computed together, which doesn't make sense either.
1088 ;; I think this points to a fundamental problem in our approach to the
1089 ;; "omake -P" problem. --Stef
1090 (or (cdr loc)
1091 (setcdr loc (compilation--make-cdrloc line file-struct marker)))
1092 (if end-loc
1093 (or (cdr end-loc)
1094 (setcdr end-loc
1095 (compilation--make-cdrloc (or end-line line) file-struct
1096 end-marker))))
1098 ;; Must start with face
1099 `(font-lock-face ,compilation-message-face
1100 compilation-message ,(compilation--make-message loc type end-loc)
1101 help-echo ,(if col
1102 "mouse-2: visit this file, line and column"
1103 (if line
1104 "mouse-2: visit this file and line"
1105 "mouse-2: visit this file"))
1106 keymap compilation-button-map
1107 mouse-face highlight)))
1109 (defun compilation--put-prop (matchnum prop val)
1110 (when (and (integerp matchnum) (match-beginning matchnum))
1111 (put-text-property
1112 (match-beginning matchnum) (match-end matchnum)
1113 prop val)))
1115 (defun compilation--remove-properties (&optional start end)
1116 (with-silent-modifications
1117 ;; When compile.el used font-lock directly, we could just remove all
1118 ;; our text-properties in one go, but now that we manually place
1119 ;; font-lock-face, we have to be careful to only remove the font-lock-face
1120 ;; we placed.
1121 ;; (remove-list-of-text-properties
1122 ;; (or start (point-min)) (or end (point-max))
1123 ;; '(compilation-debug compilation-directory compilation-message
1124 ;; font-lock-face help-echo mouse-face))
1125 (let (next)
1126 (unless start (setq start (point-min)))
1127 (unless end (setq end (point-max)))
1128 (compilation--flush-directory-cache start end)
1129 (while
1130 (progn
1131 (setq next (or (next-single-property-change
1132 start 'compilation-message nil end)
1133 end))
1134 (when (get-text-property start 'compilation-message)
1135 (remove-list-of-text-properties
1136 start next
1137 '(compilation-debug compilation-directory compilation-message
1138 font-lock-face help-echo mouse-face)))
1139 (< next end))
1140 (setq start next)))))
1142 (defun compilation--parse-region (start end)
1143 (goto-char end)
1144 (unless (bolp)
1145 ;; We generally don't like to parse partial lines.
1146 (assert (eobp))
1147 (when (let ((proc (get-buffer-process (current-buffer))))
1148 (and proc (memq (process-status proc) '(run open))))
1149 (setq end (line-beginning-position))))
1150 (compilation--remove-properties start end)
1151 (if compilation-parse-errors-function
1152 ;; An old package! Try the compatibility code.
1153 (progn
1154 (goto-char start)
1155 (compilation--compat-parse-errors end))
1157 ;; compilation-directory-matcher is the only part that really needs to be
1158 ;; parsed sequentially. So we could split it out, handle directories
1159 ;; like syntax-propertize, and the rest as font-lock-keywords. But since
1160 ;; we want to have it work even when font-lock is off, we'd then need to
1161 ;; use our own compilation-parsed text-property to keep track of the parts
1162 ;; that have already been parsed.
1163 (goto-char start)
1164 (while (re-search-forward (car compilation-directory-matcher)
1165 end t)
1166 (compilation--flush-directory-cache (match-beginning 0) (match-end 0))
1167 (when compilation-debug
1168 (font-lock-append-text-property
1169 (match-beginning 0) (match-end 0)
1170 'compilation-debug
1171 (vector 'directory compilation-directory-matcher)))
1172 (dolist (elt (cdr compilation-directory-matcher))
1173 (add-text-properties (match-beginning (car elt))
1174 (match-end (car elt))
1175 (compilation-directory-properties
1176 (car elt) (cdr elt)))))
1178 (compilation-parse-errors start end)))
1180 (defun compilation-parse-errors (start end &rest rules)
1181 "Parse errors between START and END.
1182 The errors recognized are the ones specified in RULES which default
1183 to `compilation-error-regexp-alist' if RULES is nil."
1184 (dolist (item (or rules compilation-error-regexp-alist))
1185 (if (symbolp item)
1186 (setq item (cdr (assq item
1187 compilation-error-regexp-alist-alist))))
1188 (let ((file (nth 1 item))
1189 (line (nth 2 item))
1190 (col (nth 3 item))
1191 (type (nth 4 item))
1192 (pat (car item))
1193 end-line end-col fmt
1194 props)
1196 ;; omake reports some error indented, so skip the indentation.
1197 ;; another solution is to modify (some?) regexps in
1198 ;; `compilation-error-regexp-alist'.
1199 ;; note that omake usage is not limited to ocaml and C (for stubs).
1200 ;; FIXME-omake: Doing it here seems wrong, at least it should depend on
1201 ;; whether or not omake's own error messages are recognized.
1202 (cond
1203 ((not (memq 'omake compilation-error-regexp-alist)) nil)
1204 ((string-match "\\`\\([^^]\\|^\\( \\*\\|\\[\\)\\)" pat)
1205 nil) ;; Not anchored or anchored but already allows empty spaces.
1206 (t (setq pat (concat "^ *" (substring pat 1)))))
1208 (if (consp file) (setq fmt (cdr file) file (car file)))
1209 (if (consp line) (setq end-line (cdr line) line (car line)))
1210 (if (consp col) (setq end-col (cdr col) col (car col)))
1212 (if (functionp line)
1213 ;; The old compile.el had here an undocumented hook that
1214 ;; allowed `line' to be a function that computed the actual
1215 ;; error location. Let's do our best.
1216 (progn
1217 (goto-char start)
1218 (while (re-search-forward pat end t)
1219 (save-match-data
1220 (when compilation-debug
1221 (font-lock-append-text-property
1222 (match-beginning 0) (match-end 0)
1223 'compilation-debug (vector 'functionp item)))
1224 (add-text-properties
1225 (match-beginning 0) (match-end 0)
1226 (compilation--compat-error-properties
1227 (funcall line (cons (match-string file)
1228 (cons default-directory
1229 (nthcdr 4 item)))
1230 (if col (match-string col))))))
1231 (compilation--put-prop
1232 file 'font-lock-face compilation-error-face)))
1234 (unless (or (null (nth 5 item)) (integerp (nth 5 item)))
1235 (error "HYPERLINK should be an integer: %s" (nth 5 item)))
1237 (goto-char start)
1238 (while (re-search-forward pat end t)
1239 (when (setq props (compilation-error-properties
1240 file line end-line col end-col (or type 2) fmt))
1242 (when (integerp file)
1243 (compilation--put-prop
1244 file 'font-lock-face
1245 (if (consp type)
1246 (compilation-face type)
1247 (symbol-value (aref [compilation-info-face
1248 compilation-warning-face
1249 compilation-error-face]
1250 (or type 2))))))
1252 (compilation--put-prop
1253 line 'font-lock-face compilation-line-face)
1254 (compilation--put-prop
1255 end-line 'font-lock-face compilation-line-face)
1257 (compilation--put-prop
1258 col 'font-lock-face compilation-column-face)
1259 (compilation--put-prop
1260 end-col 'font-lock-face compilation-column-face)
1262 (dolist (extra-item (nthcdr 6 item))
1263 (let ((mn (pop extra-item)))
1264 (when (match-beginning mn)
1265 (let ((face (eval (car extra-item))))
1266 (cond
1267 ((null face))
1268 ((symbolp face)
1269 (put-text-property
1270 (match-beginning mn) (match-end mn)
1271 'font-lock-face face))
1273 (error "Don't know how to handle face %S"
1274 face)))))))
1275 (let ((mn (or (nth 5 item) 0)))
1276 (when compilation-debug
1277 (font-lock-append-text-property
1278 (match-beginning 0) (match-end 0)
1279 'compilation-debug (vector 'std item props)))
1280 (add-text-properties
1281 (match-beginning mn) (match-end mn)
1282 (cddr props))
1283 (font-lock-append-text-property
1284 (match-beginning mn) (match-end mn)
1285 'font-lock-face (cadr props)))))))))
1287 (defvar compilation--parsed -1)
1288 (make-variable-buffer-local 'compilation--parsed)
1290 (defun compilation--ensure-parse (limit)
1291 "Make sure the text has been parsed up to LIMIT."
1292 (save-excursion
1293 (goto-char limit)
1294 (setq limit (line-beginning-position 2))
1295 (unless (markerp compilation--parsed)
1296 ;; We use a marker for compilation--parsed so that users (such as
1297 ;; grep.el) don't need to flush-parse when they modify the buffer
1298 ;; in a way that impacts buffer positions but does not require
1299 ;; re-parsing.
1300 (setq compilation--parsed (point-min-marker)))
1301 (when (< compilation--parsed limit)
1302 (let ((start (max compilation--parsed (point-min))))
1303 (move-marker compilation--parsed limit)
1304 (goto-char start)
1305 (forward-line 0) ;Not line-beginning-position: ignore (comint) fields.
1306 (with-silent-modifications
1307 (compilation--parse-region (point) compilation--parsed)))))
1308 nil)
1310 (defun compilation--flush-parse (start end)
1311 "Mark the region between START and END for re-parsing."
1312 (if (markerp compilation--parsed)
1313 (move-marker compilation--parsed (min start compilation--parsed))))
1315 (defun compilation-mode-font-lock-keywords ()
1316 "Return expressions to highlight in Compilation mode."
1317 (append
1318 '((compilation--ensure-parse))
1319 compilation-mode-font-lock-keywords))
1321 (defun compilation-read-command (command)
1322 (read-shell-command "Compile command: " command
1323 (if (equal (car compile-history) command)
1324 '(compile-history . 1)
1325 'compile-history)))
1328 ;;;###autoload
1329 (defun compile (command &optional comint)
1330 "Compile the program including the current buffer. Default: run `make'.
1331 Runs COMMAND, a shell command, in a separate process asynchronously
1332 with output going to the buffer `*compilation*'.
1334 You can then use the command \\[next-error] to find the next error message
1335 and move to the source code that caused it.
1337 If optional second arg COMINT is t the buffer will be in Comint mode with
1338 `compilation-shell-minor-mode'.
1340 Interactively, prompts for the command if `compilation-read-command' is
1341 non-nil; otherwise uses `compile-command'. With prefix arg, always prompts.
1342 Additionally, with universal prefix arg, compilation buffer will be in
1343 comint mode, i.e. interactive.
1345 To run more than one compilation at once, start one then rename
1346 the \`*compilation*' buffer to some other name with
1347 \\[rename-buffer]. Then _switch buffers_ and start the new compilation.
1348 It will create a new \`*compilation*' buffer.
1350 On most systems, termination of the main compilation process
1351 kills its subprocesses.
1353 The name used for the buffer is actually whatever is returned by
1354 the function in `compilation-buffer-name-function', so you can set that
1355 to a function that generates a unique name."
1356 (interactive
1357 (list
1358 (let ((command (eval compile-command)))
1359 (if (or compilation-read-command current-prefix-arg)
1360 (compilation-read-command command)
1361 command))
1362 (consp current-prefix-arg)))
1363 (unless (equal command (eval compile-command))
1364 (setq compile-command command))
1365 (save-some-buffers (not compilation-ask-about-save)
1366 compilation-save-buffers-predicate)
1367 (setq-default compilation-directory default-directory)
1368 (compilation-start command comint))
1370 ;; run compile with the default command line
1371 (defun recompile (&optional edit-command)
1372 "Re-compile the program including the current buffer.
1373 If this is run in a Compilation mode buffer, re-use the arguments from the
1374 original use. Otherwise, recompile using `compile-command'.
1375 If the optional argument `edit-command' is non-nil, the command can be edited."
1376 (interactive "P")
1377 (save-some-buffers (not compilation-ask-about-save)
1378 compilation-save-buffers-predicate)
1379 (let ((default-directory (or compilation-directory default-directory)))
1380 (when edit-command
1381 (setcar compilation-arguments
1382 (compilation-read-command (car compilation-arguments))))
1383 (apply 'compilation-start (or compilation-arguments
1384 `(,(eval compile-command))))))
1386 (defcustom compilation-scroll-output nil
1387 "Non-nil to scroll the *compilation* buffer window as output appears.
1389 Setting it causes the Compilation mode commands to put point at the
1390 end of their output window so that the end of the output is always
1391 visible rather than the beginning.
1393 The value `first-error' stops scrolling at the first error, and leaves
1394 point on its location in the *compilation* buffer."
1395 :type '(choice (const :tag "No scrolling" nil)
1396 (const :tag "Scroll compilation output" t)
1397 (const :tag "Stop scrolling at the first error" first-error))
1398 :version "20.3"
1399 :group 'compilation)
1402 (defun compilation-buffer-name (mode-name mode-command name-function)
1403 "Return the name of a compilation buffer to use.
1404 If NAME-FUNCTION is non-nil, call it with one argument MODE-NAME
1405 to determine the buffer name.
1406 Likewise if `compilation-buffer-name-function' is non-nil.
1407 If current buffer has the major mode MODE-COMMAND,
1408 return the name of the current buffer, so that it gets reused.
1409 Otherwise, construct a buffer name from MODE-NAME."
1410 (cond (name-function
1411 (funcall name-function mode-name))
1412 (compilation-buffer-name-function
1413 (funcall compilation-buffer-name-function mode-name))
1414 ((eq mode-command major-mode)
1415 (buffer-name))
1417 (concat "*" (downcase mode-name) "*"))))
1419 ;; This is a rough emulation of the old hack, until the transition to new
1420 ;; compile is complete.
1421 (defun compile-internal (command error-message
1422 &optional name-of-mode parser
1423 error-regexp-alist name-function
1424 enter-regexp-alist leave-regexp-alist
1425 file-regexp-alist nomessage-regexp-alist
1426 no-async highlight-regexp local-map)
1427 (if parser
1428 (error "Compile now works very differently, see `compilation-error-regexp-alist'"))
1429 (let ((compilation-error-regexp-alist
1430 (append file-regexp-alist (or error-regexp-alist
1431 compilation-error-regexp-alist)))
1432 (compilation-error (replace-regexp-in-string "^No more \\(.+\\)s\\.?"
1433 "\\1" error-message)))
1434 (compilation-start command nil name-function highlight-regexp)))
1435 (make-obsolete 'compile-internal 'compilation-start "22.1")
1437 ;;;###autoload
1438 (defun compilation-start (command &optional mode name-function highlight-regexp)
1439 "Run compilation command COMMAND (low level interface).
1440 If COMMAND starts with a cd command, that becomes the `default-directory'.
1441 The rest of the arguments are optional; for them, nil means use the default.
1443 MODE is the major mode to set in the compilation buffer. Mode
1444 may also be t meaning use `compilation-shell-minor-mode' under `comint-mode'.
1446 If NAME-FUNCTION is non-nil, call it with one argument (the mode name)
1447 to determine the buffer name. Otherwise, the default is to
1448 reuses the current buffer if it has the proper major mode,
1449 else use or create a buffer with name based on the major mode.
1451 If HIGHLIGHT-REGEXP is non-nil, `next-error' will temporarily highlight
1452 the matching section of the visited source line; the default is to use the
1453 global value of `compilation-highlight-regexp'.
1455 Returns the compilation buffer created."
1456 (or mode (setq mode 'compilation-mode))
1457 (let* ((name-of-mode
1458 (if (eq mode t)
1459 "compilation"
1460 (replace-regexp-in-string "-mode\\'" "" (symbol-name mode))))
1461 (thisdir default-directory)
1462 outwin outbuf)
1463 (with-current-buffer
1464 (setq outbuf
1465 (get-buffer-create
1466 (compilation-buffer-name name-of-mode mode name-function)))
1467 (let ((comp-proc (get-buffer-process (current-buffer))))
1468 (if comp-proc
1469 (if (or (not (eq (process-status comp-proc) 'run))
1470 (yes-or-no-p
1471 (format "A %s process is running; kill it? "
1472 name-of-mode)))
1473 (condition-case ()
1474 (progn
1475 (interrupt-process comp-proc)
1476 (sit-for 1)
1477 (delete-process comp-proc))
1478 (error nil))
1479 (error "Cannot have two processes in `%s' at once"
1480 (buffer-name)))))
1481 ;; first transfer directory from where M-x compile was called
1482 (setq default-directory thisdir)
1483 ;; Make compilation buffer read-only. The filter can still write it.
1484 ;; Clear out the compilation buffer.
1485 (let ((inhibit-read-only t)
1486 (default-directory thisdir))
1487 ;; Then evaluate a cd command if any, but don't perform it yet, else
1488 ;; start-command would do it again through the shell: (cd "..") AND
1489 ;; sh -c "cd ..; make"
1490 (cd (if (string-match "\\`\\s *cd\\(?:\\s +\\(\\S +?\\)\\)?\\s *[;&\n]"
1491 command)
1492 (if (match-end 1)
1493 (substitute-env-vars (match-string 1 command))
1494 "~")
1495 default-directory))
1496 (erase-buffer)
1497 ;; Select the desired mode.
1498 (if (not (eq mode t))
1499 (progn
1500 (buffer-disable-undo)
1501 (funcall mode))
1502 (setq buffer-read-only nil)
1503 (with-no-warnings (comint-mode))
1504 (compilation-shell-minor-mode))
1505 ;; Remember the original dir, so we can use it when we recompile.
1506 ;; default-directory' can't be used reliably for that because it may be
1507 ;; affected by the special handling of "cd ...;".
1508 ;; NB: must be fone after (funcall mode) as that resets local variables
1509 (set (make-local-variable 'compilation-directory) thisdir)
1510 (if highlight-regexp
1511 (set (make-local-variable 'compilation-highlight-regexp)
1512 highlight-regexp))
1513 (if (or compilation-auto-jump-to-first-error
1514 (eq compilation-scroll-output 'first-error))
1515 (set (make-local-variable 'compilation-auto-jump-to-next) t))
1516 ;; Output a mode setter, for saving and later reloading this buffer.
1517 (insert "-*- mode: " name-of-mode
1518 "; default-directory: "
1519 (prin1-to-string (abbreviate-file-name default-directory))
1520 " -*-\n"
1521 (format "%s started at %s\n\n"
1522 mode-name
1523 (substring (current-time-string) 0 19))
1524 command "\n")
1525 (setq thisdir default-directory))
1526 (set-buffer-modified-p nil))
1527 ;; Pop up the compilation buffer.
1528 ;; http://lists.gnu.org/archive/html/emacs-devel/2007-11/msg01638.html
1529 (setq outwin (display-buffer outbuf))
1530 (with-current-buffer outbuf
1531 (let ((process-environment
1532 (append
1533 compilation-environment
1534 (if (if (boundp 'system-uses-terminfo) ; `if' for compiler warning
1535 system-uses-terminfo)
1536 (list "TERM=dumb" "TERMCAP="
1537 (format "COLUMNS=%d" (window-width)))
1538 (list "TERM=emacs"
1539 (format "TERMCAP=emacs:co#%d:tc=unknown:"
1540 (window-width))))
1541 ;; Set the EMACS variable, but
1542 ;; don't override users' setting of $EMACS.
1543 (unless (getenv "EMACS")
1544 (list "EMACS=t"))
1545 (list "INSIDE_EMACS=t")
1546 (copy-sequence process-environment))))
1547 (set (make-local-variable 'compilation-arguments)
1548 (list command mode name-function highlight-regexp))
1549 (set (make-local-variable 'revert-buffer-function)
1550 'compilation-revert-buffer)
1551 (set-window-start outwin (point-min))
1553 ;; Position point as the user will see it.
1554 (let ((desired-visible-point
1555 ;; Put it at the end if `compilation-scroll-output' is set.
1556 (if compilation-scroll-output
1557 (point-max)
1558 ;; Normally put it at the top.
1559 (point-min))))
1560 (if (eq outwin (selected-window))
1561 (goto-char desired-visible-point)
1562 (set-window-point outwin desired-visible-point)))
1564 ;; The setup function is called before compilation-set-window-height
1565 ;; so it can set the compilation-window-height buffer locally.
1566 (if compilation-process-setup-function
1567 (funcall compilation-process-setup-function))
1568 (compilation-set-window-height outwin)
1569 ;; Start the compilation.
1570 (if (fboundp 'start-process)
1571 (let ((proc
1572 (if (eq mode t)
1573 ;; comint uses `start-file-process'.
1574 (get-buffer-process
1575 (with-no-warnings
1576 (comint-exec
1577 outbuf (downcase mode-name)
1578 (if (file-remote-p default-directory)
1579 "/bin/sh"
1580 shell-file-name)
1581 nil `("-c" ,command))))
1582 (start-file-process-shell-command (downcase mode-name)
1583 outbuf command))))
1584 ;; Make the buffer's mode line show process state.
1585 (setq mode-line-process
1586 (list (propertize ":%s" 'face 'compilation-warning)))
1587 (set-process-sentinel proc 'compilation-sentinel)
1588 (unless (eq mode t)
1589 ;; Keep the comint filter, since it's needed for proper handling
1590 ;; of the prompts.
1591 (set-process-filter proc 'compilation-filter))
1592 ;; Use (point-max) here so that output comes in
1593 ;; after the initial text,
1594 ;; regardless of where the user sees point.
1595 (set-marker (process-mark proc) (point-max) outbuf)
1596 (when compilation-disable-input
1597 (condition-case nil
1598 (process-send-eof proc)
1599 ;; The process may have exited already.
1600 (error nil)))
1601 (run-hook-with-args 'compilation-start-hook proc)
1602 (setq compilation-in-progress
1603 (cons proc compilation-in-progress)))
1604 ;; No asynchronous processes available.
1605 (message "Executing `%s'..." command)
1606 ;; Fake modeline display as if `start-process' were run.
1607 (setq mode-line-process
1608 (list (propertize ":run" 'face 'compilation-warning)))
1609 (force-mode-line-update)
1610 (sit-for 0) ; Force redisplay
1611 (save-excursion
1612 ;; Insert the output at the end, after the initial text,
1613 ;; regardless of where the user sees point.
1614 (goto-char (point-max))
1615 (let* ((inhibit-read-only t) ; call-process needs to modify outbuf
1616 (status (call-process shell-file-name nil outbuf nil "-c"
1617 command)))
1618 (cond ((numberp status)
1619 (compilation-handle-exit
1620 'exit status
1621 (if (zerop status)
1622 "finished\n"
1623 (format "exited abnormally with code %d\n" status))))
1624 ((stringp status)
1625 (compilation-handle-exit 'signal status
1626 (concat status "\n")))
1628 (compilation-handle-exit 'bizarre status status)))))
1629 (set-buffer-modified-p nil)
1630 (message "Executing `%s'...done" command)))
1631 ;; Now finally cd to where the shell started make/grep/...
1632 (setq default-directory thisdir)
1633 ;; The following form selected outwin ever since revision 1.183,
1634 ;; so possibly messing up point in some other window (bug#1073).
1635 ;; Moved into the scope of with-current-buffer, though still with
1636 ;; complete disregard for the case when compilation-scroll-output
1637 ;; equals 'first-error (martin 2008-10-04).
1638 (when compilation-scroll-output
1639 (goto-char (point-max))))
1641 ;; Make it so the next C-x ` will use this buffer.
1642 (setq next-error-last-buffer outbuf)))
1644 (defun compilation-set-window-height (window)
1645 "Set the height of WINDOW according to `compilation-window-height'."
1646 (let ((height (buffer-local-value 'compilation-window-height (window-buffer window))))
1647 (and height
1648 (window-full-width-p window)
1649 ;; If window is alone in its frame, aside from a minibuffer,
1650 ;; don't change its height.
1651 (not (eq window (frame-root-window (window-frame window))))
1652 ;; Stef said that doing the saves in this order is safer:
1653 (save-excursion
1654 (save-selected-window
1655 (select-window window)
1656 (enlarge-window (- height (window-height))))))))
1658 (defvar compilation-menu-map
1659 (let ((map (make-sparse-keymap "Errors"))
1660 (opt-map (make-sparse-keymap "Skip")))
1661 (define-key map [stop-subjob]
1662 '(menu-item "Stop Compilation" kill-compilation
1663 :help "Kill the process made by the M-x compile or M-x grep commands"))
1664 (define-key map [compilation-mode-separator3]
1665 '("----" . nil))
1666 (define-key map [compilation-next-error-follow-minor-mode]
1667 '(menu-item
1668 "Auto Error Display" next-error-follow-minor-mode
1669 :help "Display the error under cursor when moving the cursor"
1670 :button (:toggle . next-error-follow-minor-mode)))
1671 (define-key map [compilation-skip]
1672 (cons "Skip Less Important Messages" opt-map))
1673 (define-key opt-map [compilation-skip-none]
1674 '(menu-item "Don't Skip Any Messages"
1675 (lambda ()
1676 (interactive)
1677 (customize-set-variable 'compilation-skip-threshold 0))
1678 :help "Do not skip any type of messages"
1679 :button (:radio . (eq compilation-skip-threshold 0))))
1680 (define-key opt-map [compilation-skip-info]
1681 '(menu-item "Skip Info"
1682 (lambda ()
1683 (interactive)
1684 (customize-set-variable 'compilation-skip-threshold 1))
1685 :help "Skip anything less than warning"
1686 :button (:radio . (eq compilation-skip-threshold 1))))
1687 (define-key opt-map [compilation-skip-warning-and-info]
1688 '(menu-item "Skip Warnings and Info"
1689 (lambda ()
1690 (interactive)
1691 (customize-set-variable 'compilation-skip-threshold 2))
1692 :help "Skip over Warnings and Info, stop for errors"
1693 :button (:radio . (eq compilation-skip-threshold 2))))
1694 (define-key map [compilation-mode-separator2]
1695 '("----" . nil))
1696 (define-key map [compilation-first-error]
1697 '(menu-item "First Error" first-error
1698 :help "Restart at the first error, visit corresponding source code"))
1699 (define-key map [compilation-previous-error]
1700 '(menu-item "Previous Error" previous-error
1701 :help "Visit previous `next-error' message and corresponding source code"))
1702 (define-key map [compilation-next-error]
1703 '(menu-item "Next Error" next-error
1704 :help "Visit next `next-error' message and corresponding source code"))
1705 map))
1707 (defvar compilation-minor-mode-map
1708 (let ((map (make-sparse-keymap)))
1709 (set-keymap-parent map special-mode-map)
1710 (define-key map [mouse-2] 'compile-goto-error)
1711 (define-key map [follow-link] 'mouse-face)
1712 (define-key map "\C-c\C-c" 'compile-goto-error)
1713 (define-key map "\C-m" 'compile-goto-error)
1714 (define-key map "\C-c\C-k" 'kill-compilation)
1715 (define-key map "\M-n" 'compilation-next-error)
1716 (define-key map "\M-p" 'compilation-previous-error)
1717 (define-key map "\M-{" 'compilation-previous-file)
1718 (define-key map "\M-}" 'compilation-next-file)
1719 (define-key map "g" 'recompile) ; revert
1720 ;; Set up the menu-bar
1721 (define-key map [menu-bar compilation]
1722 (cons "Errors" compilation-menu-map))
1723 map)
1724 "Keymap for `compilation-minor-mode'.")
1726 (defvar compilation-shell-minor-mode-map
1727 (let ((map (make-sparse-keymap)))
1728 (define-key map "\M-\C-m" 'compile-goto-error)
1729 (define-key map "\M-\C-n" 'compilation-next-error)
1730 (define-key map "\M-\C-p" 'compilation-previous-error)
1731 (define-key map "\M-{" 'compilation-previous-file)
1732 (define-key map "\M-}" 'compilation-next-file)
1733 ;; Set up the menu-bar
1734 (define-key map [menu-bar compilation]
1735 (cons "Errors" compilation-menu-map))
1736 map)
1737 "Keymap for `compilation-shell-minor-mode'.")
1739 (defvar compilation-button-map
1740 (let ((map (make-sparse-keymap)))
1741 (define-key map [mouse-2] 'compile-goto-error)
1742 (define-key map [follow-link] 'mouse-face)
1743 (define-key map "\C-m" 'compile-goto-error)
1744 map)
1745 "Keymap for compilation-message buttons.")
1746 (fset 'compilation-button-map compilation-button-map)
1748 (defvar compilation-mode-map
1749 (let ((map (make-sparse-keymap)))
1750 ;; Don't inherit from compilation-minor-mode-map,
1751 ;; because that introduces a menu bar item we don't want.
1752 ;; That confuses C-down-mouse-3.
1753 (set-keymap-parent map special-mode-map)
1754 (define-key map [mouse-2] 'compile-goto-error)
1755 (define-key map [follow-link] 'mouse-face)
1756 (define-key map "\C-c\C-c" 'compile-goto-error)
1757 (define-key map "\C-m" 'compile-goto-error)
1758 (define-key map "\C-c\C-k" 'kill-compilation)
1759 (define-key map "\M-n" 'compilation-next-error)
1760 (define-key map "\M-p" 'compilation-previous-error)
1761 (define-key map "\M-{" 'compilation-previous-file)
1762 (define-key map "\M-}" 'compilation-next-file)
1763 (define-key map "\t" 'compilation-next-error)
1764 (define-key map [backtab] 'compilation-previous-error)
1765 (define-key map "g" 'recompile) ; revert
1767 (define-key map "\C-c\C-f" 'next-error-follow-minor-mode)
1769 ;; Set up the menu-bar
1770 (let ((submap (make-sparse-keymap "Compile")))
1771 (define-key map [menu-bar compilation]
1772 (cons "Compile" submap))
1773 (set-keymap-parent submap compilation-menu-map))
1774 (define-key map [menu-bar compilation compilation-separator2]
1775 '("----" . nil))
1776 (define-key map [menu-bar compilation compilation-grep]
1777 '(menu-item "Search Files (grep)..." grep
1778 :help "Run grep, with user-specified args, and collect output in a buffer"))
1779 (define-key map [menu-bar compilation compilation-recompile]
1780 '(menu-item "Recompile" recompile
1781 :help "Re-compile the program including the current buffer"))
1782 (define-key map [menu-bar compilation compilation-compile]
1783 '(menu-item "Compile..." compile
1784 :help "Compile the program including the current buffer. Default: run `make'"))
1785 map)
1786 "Keymap for compilation log buffers.
1787 `compilation-minor-mode-map' is a parent of this.")
1789 (defvar compilation-mode-tool-bar-map
1790 ;; When bootstrapping, tool-bar-map is not properly initialized yet,
1791 ;; so don't do anything.
1792 (when (keymapp tool-bar-map)
1793 (let ((map (copy-keymap tool-bar-map)))
1794 (define-key map [undo] nil)
1795 (define-key map [separator-2] nil)
1796 (define-key-after map [separator-compile] menu-bar-separator)
1797 (tool-bar-local-item
1798 "left-arrow" 'previous-error-no-select 'previous-error-no-select map
1799 :rtl "right-arrow"
1800 :help "Goto previous error")
1801 (tool-bar-local-item
1802 "right-arrow" 'next-error-no-select 'next-error-no-select map
1803 :rtl "left-arrow"
1804 :help "Goto next error")
1805 (tool-bar-local-item
1806 "cancel" 'kill-compilation 'kill-compilation map
1807 :enable '(let ((buffer (compilation-find-buffer)))
1808 (get-buffer-process buffer))
1809 :help "Stop compilation")
1810 (tool-bar-local-item
1811 "refresh" 'recompile 'recompile map
1812 :help "Restart compilation")
1813 map)))
1815 (put 'compilation-mode 'mode-class 'special)
1817 ;;;###autoload
1818 (defun compilation-mode (&optional name-of-mode)
1819 "Major mode for compilation log buffers.
1820 \\<compilation-mode-map>To visit the source for a line-numbered error,
1821 move point to the error message line and type \\[compile-goto-error].
1822 To kill the compilation, type \\[kill-compilation].
1824 Runs `compilation-mode-hook' with `run-mode-hooks' (which see).
1826 \\{compilation-mode-map}"
1827 (interactive)
1828 (kill-all-local-variables)
1829 (use-local-map compilation-mode-map)
1830 ;; Let windows scroll along with the output.
1831 (set (make-local-variable 'window-point-insertion-type) t)
1832 (set (make-local-variable 'tool-bar-map) compilation-mode-tool-bar-map)
1833 (setq major-mode 'compilation-mode ; FIXME: Use define-derived-mode.
1834 mode-name (or name-of-mode "Compilation"))
1835 (set (make-local-variable 'page-delimiter)
1836 compilation-page-delimiter)
1837 ;; (set (make-local-variable 'compilation-buffer-modtime) nil)
1838 (compilation-setup)
1839 (setq buffer-read-only t)
1840 (run-mode-hooks 'compilation-mode-hook))
1842 (defmacro define-compilation-mode (mode name doc &rest body)
1843 "This is like `define-derived-mode' without the PARENT argument.
1844 The parent is always `compilation-mode' and the customizable `compilation-...'
1845 variables are also set from the name of the mode you have chosen,
1846 by replacing the first word, e.g `compilation-scroll-output' from
1847 `grep-scroll-output' if that variable exists."
1848 (let ((mode-name (replace-regexp-in-string "-mode\\'" "" (symbol-name mode))))
1849 `(define-derived-mode ,mode compilation-mode ,name
1850 ,doc
1851 ,@(mapcar (lambda (v)
1852 (setq v (cons v
1853 (intern-soft (replace-regexp-in-string
1854 "^compilation" mode-name
1855 (symbol-name v)))))
1856 (and (cdr v)
1857 (or (boundp (cdr v))
1858 ;; FIXME: This is hackish, using undocumented info.
1859 (if (boundp 'byte-compile-bound-variables)
1860 (memq (cdr v) byte-compile-bound-variables)))
1861 `(set (make-local-variable ',(car v)) ,(cdr v))))
1862 '(compilation-buffer-name-function
1863 compilation-directory-matcher
1864 compilation-error
1865 compilation-error-regexp-alist
1866 compilation-error-regexp-alist-alist
1867 compilation-error-screen-columns
1868 compilation-finish-function
1869 compilation-finish-functions
1870 compilation-first-column
1871 compilation-mode-font-lock-keywords
1872 compilation-page-delimiter
1873 compilation-parse-errors-filename-function
1874 compilation-process-setup-function
1875 compilation-scroll-output
1876 compilation-search-path
1877 compilation-skip-threshold
1878 compilation-window-height))
1879 ,@body)))
1881 (defun compilation-revert-buffer (ignore-auto noconfirm)
1882 (if buffer-file-name
1883 (let (revert-buffer-function)
1884 (revert-buffer ignore-auto noconfirm))
1885 (if (or noconfirm (yes-or-no-p (format "Restart compilation? ")))
1886 (apply 'compilation-start compilation-arguments))))
1888 (defvar compilation-current-error nil
1889 "Marker to the location from where the next error will be found.
1890 The global commands next/previous/first-error/goto-error use this.")
1892 (defvar compilation-messages-start nil
1893 "Buffer position of the beginning of the compilation messages.
1894 If nil, use the beginning of buffer.")
1896 (defun compilation-setup (&optional minor)
1897 "Prepare the buffer for the compilation parsing commands to work.
1898 Optional argument MINOR indicates this is called from
1899 `compilation-minor-mode'."
1900 (make-local-variable 'compilation-current-error)
1901 (make-local-variable 'compilation-messages-start)
1902 (make-local-variable 'compilation-error-screen-columns)
1903 (make-local-variable 'overlay-arrow-position)
1904 (set (make-local-variable 'overlay-arrow-string) "")
1905 (setq next-error-overlay-arrow-position nil)
1906 (add-hook 'kill-buffer-hook
1907 (lambda () (setq next-error-overlay-arrow-position nil)) nil t)
1908 ;; Note that compilation-next-error-function is for interfacing
1909 ;; with the next-error function in simple.el, and it's only
1910 ;; coincidentally named similarly to compilation-next-error.
1911 (setq next-error-function 'compilation-next-error-function)
1912 (set (make-local-variable 'comint-file-name-prefix)
1913 (or (file-remote-p default-directory) ""))
1914 (set (make-local-variable 'compilation-locs)
1915 (make-hash-table :test 'equal :weakness 'value))
1916 ;; It's generally preferable to use after-change-functions since they
1917 ;; can be subject to combine-after-change-calls, but if we do that, we risk
1918 ;; running our hook after font-lock, resulting in incorrect refontification.
1919 (add-hook 'before-change-functions 'compilation--flush-parse nil t)
1920 ;; Also for minor mode, since it's not permanent-local.
1921 (add-hook 'change-major-mode-hook #'compilation--remove-properties nil t)
1922 (if minor
1923 (progn
1924 (font-lock-add-keywords nil (compilation-mode-font-lock-keywords))
1925 (if font-lock-mode
1926 (font-lock-fontify-buffer)))
1927 (setq font-lock-defaults '(compilation-mode-font-lock-keywords t))))
1929 (defun compilation--unsetup ()
1930 ;; Only for minor mode.
1931 (font-lock-remove-keywords nil (compilation-mode-font-lock-keywords))
1932 (remove-hook 'before-change-functions 'compilation--flush-parse t)
1933 (kill-local-variable 'compilation--parsed)
1934 (compilation--remove-properties)
1935 (if font-lock-mode
1936 (font-lock-fontify-buffer)))
1938 ;;;###autoload
1939 (define-minor-mode compilation-shell-minor-mode
1940 "Toggle compilation shell minor mode.
1941 With arg, turn compilation mode on if and only if arg is positive.
1942 In this minor mode, all the error-parsing commands of the
1943 Compilation major mode are available but bound to keys that don't
1944 collide with Shell mode. See `compilation-mode'.
1945 Turning the mode on runs the normal hook `compilation-shell-minor-mode-hook'."
1946 nil " Shell-Compile"
1947 :group 'compilation
1948 (if compilation-shell-minor-mode
1949 (compilation-setup t)
1950 (compilation--unsetup)))
1952 ;;;###autoload
1953 (define-minor-mode compilation-minor-mode
1954 "Toggle compilation minor mode.
1955 With arg, turn compilation mode on if and only if arg is positive.
1956 In this minor mode, all the error-parsing commands of the
1957 Compilation major mode are available. See `compilation-mode'.
1958 Turning the mode on runs the normal hook `compilation-minor-mode-hook'."
1959 nil " Compilation"
1960 :group 'compilation
1961 (if compilation-minor-mode
1962 (compilation-setup t)
1963 (compilation--unsetup)))
1965 (defun compilation-handle-exit (process-status exit-status msg)
1966 "Write MSG in the current buffer and hack its `mode-line-process'."
1967 (let ((inhibit-read-only t)
1968 (status (if compilation-exit-message-function
1969 (funcall compilation-exit-message-function
1970 process-status exit-status msg)
1971 (cons msg exit-status)))
1972 (omax (point-max))
1973 (opoint (point))
1974 (cur-buffer (current-buffer)))
1975 ;; Record where we put the message, so we can ignore it later on.
1976 (goto-char omax)
1977 (insert ?\n mode-name " " (car status))
1978 (if (and (numberp compilation-window-height)
1979 (zerop compilation-window-height))
1980 (message "%s" (cdr status)))
1981 (if (bolp)
1982 (forward-char -1))
1983 (insert " at " (substring (current-time-string) 0 19))
1984 (goto-char (point-max))
1985 ;; Prevent that message from being recognized as a compilation error.
1986 (add-text-properties omax (point)
1987 (append '(compilation-handle-exit t) nil))
1988 (setq mode-line-process
1989 (let ((out-string (format ":%s [%s]" process-status (cdr status)))
1990 (msg (format "%s %s" mode-name
1991 (replace-regexp-in-string "\n?$" ""
1992 (car status)))))
1993 (message "%s" msg)
1994 (propertize out-string
1995 'help-echo msg 'face (if (> exit-status 0)
1996 'compilation-error
1997 'compilation-info))))
1998 ;; Force mode line redisplay soon.
1999 (force-mode-line-update)
2000 (if (and opoint (< opoint omax))
2001 (goto-char opoint))
2002 (with-no-warnings
2003 (if compilation-finish-function
2004 (funcall compilation-finish-function cur-buffer msg)))
2005 (run-hook-with-args 'compilation-finish-functions cur-buffer msg)))
2007 ;; Called when compilation process changes state.
2008 (defun compilation-sentinel (proc msg)
2009 "Sentinel for compilation buffers."
2010 (if (memq (process-status proc) '(exit signal))
2011 (let ((buffer (process-buffer proc)))
2012 (if (null (buffer-name buffer))
2013 ;; buffer killed
2014 (set-process-buffer proc nil)
2015 (with-current-buffer buffer
2016 ;; Write something in the compilation buffer
2017 ;; and hack its mode line.
2018 (compilation-handle-exit (process-status proc)
2019 (process-exit-status proc)
2020 msg)
2021 ;; Since the buffer and mode line will show that the
2022 ;; process is dead, we can delete it now. Otherwise it
2023 ;; will stay around until M-x list-processes.
2024 (delete-process proc)))
2025 (setq compilation-in-progress (delq proc compilation-in-progress)))))
2027 (defun compilation-filter (proc string)
2028 "Process filter for compilation buffers.
2029 Just inserts the text,
2030 handles carriage motion (see `comint-inhibit-carriage-motion'),
2031 and runs `compilation-filter-hook'."
2032 (when (buffer-live-p (process-buffer proc))
2033 (with-current-buffer (process-buffer proc)
2034 (let ((inhibit-read-only t)
2035 ;; `save-excursion' doesn't use the right insertion-type for us.
2036 (pos (copy-marker (point) t))
2037 ;; `save-restriction' doesn't use the right insertion type either:
2038 ;; If we are inserting at the end of the accessible part of the
2039 ;; buffer, keep the inserted text visible.
2040 (min (point-min-marker))
2041 (max (copy-marker (point-max) t)))
2042 (unwind-protect
2043 (progn
2044 (widen)
2045 (goto-char (process-mark proc))
2046 ;; We used to use `insert-before-markers', so that windows with
2047 ;; point at `process-mark' scroll along with the output, but we
2048 ;; now use window-point-insertion-type instead.
2049 (insert string)
2050 (unless comint-inhibit-carriage-motion
2051 (comint-carriage-motion (process-mark proc) (point)))
2052 (set-marker (process-mark proc) (point))
2053 ;; (set (make-local-variable 'compilation-buffer-modtime)
2054 ;; (current-time))
2055 (run-hooks 'compilation-filter-hook))
2056 (goto-char pos)
2057 (narrow-to-region min max)
2058 (set-marker pos nil)
2059 (set-marker min nil)
2060 (set-marker max nil))))))
2062 ;;; test if a buffer is a compilation buffer, assuming we're in the buffer
2063 (defsubst compilation-buffer-internal-p ()
2064 "Test if inside a compilation buffer."
2065 (local-variable-p 'compilation-locs))
2067 ;;; test if a buffer is a compilation buffer, using compilation-buffer-internal-p
2068 (defsubst compilation-buffer-p (buffer)
2069 "Test if BUFFER is a compilation buffer."
2070 (with-current-buffer buffer
2071 (compilation-buffer-internal-p)))
2073 (defmacro compilation-loop (< property-change 1+ error limit)
2074 `(let (opt)
2075 (while (,< n 0)
2076 (setq opt pt)
2077 (or (setq pt (,property-change pt 'compilation-message))
2078 ;; Handle the case where where the first error message is
2079 ;; at the start of the buffer, and n < 0.
2080 (if (or (eq (get-text-property ,limit 'compilation-message)
2081 (get-text-property opt 'compilation-message))
2082 (eq pt opt))
2083 (error ,error compilation-error)
2084 (setq pt ,limit)))
2085 ;; prop 'compilation-message usually has 2 changes, on and off, so
2086 ;; re-search if off
2087 (or (setq msg (get-text-property pt 'compilation-message))
2088 (if (setq pt (,property-change pt 'compilation-message nil ,limit))
2089 (setq msg (get-text-property pt 'compilation-message)))
2090 (error ,error compilation-error))
2091 (or (< (compilation--message->type msg) compilation-skip-threshold)
2092 (if different-file
2093 (eq (prog1 last
2094 (setq last (compilation--loc->file-struct
2095 (compilation--message->loc msg))))
2096 last))
2097 (if compilation-skip-visited
2098 (compilation--loc->visited (compilation--message->loc msg)))
2099 (if compilation-skip-to-next-location
2100 (eq (compilation--message->loc msg) loc))
2101 ;; count this message only if none of the above are true
2102 (setq n (,1+ n))))))
2104 (defun compilation-next-single-property-change (position prop
2105 &optional object limit)
2106 (let (parsed res)
2107 (while (progn
2108 ;; We parse the buffer here "on-demand" by chunks of 500 chars.
2109 ;; But we could also just parse the whole buffer.
2110 (compilation--ensure-parse
2111 (setq parsed (max compilation--parsed
2112 (min (+ position 500)
2113 (or limit (point-max))))))
2114 (and (or (not (setq res (next-single-property-change
2115 position prop object limit)))
2116 (eq res limit))
2117 (< position (or limit (point-max)))))
2118 (setq position parsed))
2119 res))
2121 (defun compilation-next-error (n &optional different-file pt)
2122 "Move point to the next error in the compilation buffer.
2123 This function does NOT find the source line like \\[next-error].
2124 Prefix arg N says how many error messages to move forwards (or
2125 backwards, if negative).
2126 Optional arg DIFFERENT-FILE, if non-nil, means find next error for a
2127 file that is different from the current one.
2128 Optional arg PT, if non-nil, specifies the value of point to start
2129 looking for the next message."
2130 (interactive "p")
2131 (or (compilation-buffer-p (current-buffer))
2132 (error "Not in a compilation buffer"))
2133 (or pt (setq pt (point)))
2134 (let* ((msg (get-text-property pt 'compilation-message))
2135 ;; `loc', `msg', and `last' are used by the compilation-loop macro.
2136 (loc (and msg (compilation--message->loc msg)))
2137 last)
2138 (if (zerop n)
2139 (unless (or msg ; find message near here
2140 (setq msg (get-text-property (max (1- pt) (point-min))
2141 'compilation-message)))
2142 (setq pt (previous-single-property-change pt 'compilation-message nil
2143 (line-beginning-position)))
2144 (unless (setq msg (get-text-property (max (1- pt) (point-min))
2145 'compilation-message))
2146 (setq pt (next-single-property-change pt 'compilation-message nil
2147 (line-end-position)))
2148 (or (setq msg (get-text-property pt 'compilation-message))
2149 (setq pt (point)))))
2150 (setq last (compilation--loc->file-struct loc))
2151 (if (>= n 0)
2152 (compilation-loop > compilation-next-single-property-change 1-
2153 (if (get-buffer-process (current-buffer))
2154 "No more %ss yet"
2155 "Moved past last %s")
2156 (point-max))
2157 (compilation--ensure-parse pt)
2158 ;; Don't move "back" to message at or before point.
2159 ;; Pass an explicit (point-min) to make sure pt is non-nil.
2160 (setq pt (previous-single-property-change
2161 pt 'compilation-message nil (point-min)))
2162 (compilation-loop < previous-single-property-change 1+
2163 "Moved back before first %s" (point-min))))
2164 (goto-char pt)
2165 (or msg
2166 (error "No %s here" compilation-error))))
2168 (defun compilation-previous-error (n)
2169 "Move point to the previous error in the compilation buffer.
2170 Prefix arg N says how many error messages to move backwards (or
2171 forwards, if negative).
2172 Does NOT find the source line like \\[previous-error]."
2173 (interactive "p")
2174 (compilation-next-error (- n)))
2176 (defun compilation-next-file (n)
2177 "Move point to the next error for a different file than the current one.
2178 Prefix arg N says how many files to move forwards (or backwards, if negative)."
2179 (interactive "p")
2180 (compilation-next-error n t))
2182 (defun compilation-previous-file (n)
2183 "Move point to the previous error for a different file than the current one.
2184 Prefix arg N says how many files to move backwards (or forwards, if negative)."
2185 (interactive "p")
2186 (compilation-next-file (- n)))
2188 (defun kill-compilation ()
2189 "Kill the process made by the \\[compile] or \\[grep] commands."
2190 (interactive)
2191 (let ((buffer (compilation-find-buffer)))
2192 (if (get-buffer-process buffer)
2193 (interrupt-process (get-buffer-process buffer))
2194 (error "The %s process is not running" (downcase mode-name)))))
2196 (defalias 'compile-mouse-goto-error 'compile-goto-error)
2198 (defun compile-goto-error (&optional event)
2199 "Visit the source for the error message at point.
2200 Use this command in a compilation log buffer. Sets the mark at point there."
2201 (interactive (list last-input-event))
2202 (if event (posn-set-point (event-end event)))
2203 (or (compilation-buffer-p (current-buffer))
2204 (error "Not in a compilation buffer"))
2205 (compilation--ensure-parse (point))
2206 (if (get-text-property (point) 'compilation-directory)
2207 (dired-other-window
2208 (car (get-text-property (point) 'compilation-directory)))
2209 (push-mark)
2210 (setq compilation-current-error (point))
2211 (next-error-internal)))
2213 ;; This is mostly unused, but we keep it for the sake of some external
2214 ;; packages which seem to make use of it.
2215 (defun compilation-find-buffer (&optional avoid-current)
2216 "Return a compilation buffer.
2217 If AVOID-CURRENT is nil, and the current buffer is a compilation buffer,
2218 return it. If AVOID-CURRENT is non-nil, return the current buffer only
2219 as a last resort."
2220 (if (and (compilation-buffer-internal-p) (not avoid-current))
2221 (current-buffer)
2222 (next-error-find-buffer avoid-current 'compilation-buffer-internal-p)))
2224 ;;;###autoload
2225 (defun compilation-next-error-function (n &optional reset)
2226 "Advance to the next error message and visit the file where the error was.
2227 This is the value of `next-error-function' in Compilation buffers."
2228 (interactive "p")
2229 (when reset
2230 (setq compilation-current-error nil))
2231 (let* ((columns compilation-error-screen-columns) ; buffer's local value
2232 (last 1) timestamp
2233 (msg (compilation-next-error (or n 1) nil
2234 (or compilation-current-error
2235 compilation-messages-start
2236 (point-min))))
2237 (loc (compilation--message->loc msg))
2238 (end-loc (compilation--message->end-loc msg))
2239 (marker (point-marker)))
2240 (setq compilation-current-error (point-marker)
2241 overlay-arrow-position
2242 (if (bolp)
2243 compilation-current-error
2244 (copy-marker (line-beginning-position))))
2245 ;; If loc contains no marker, no error in that file has been visited.
2246 ;; If the marker is invalid the buffer has been killed.
2247 ;; So, recalculate all markers for that file.
2248 (unless (and (compilation--loc->marker loc)
2249 (marker-buffer (compilation--loc->marker loc))
2250 ;; FIXME-omake: For "omake -P", which automatically recompiles
2251 ;; when the file is modified, the line numbers of new output
2252 ;; may not be related to line numbers from earlier output
2253 ;; (earlier markers), so we used to try to detect it here and
2254 ;; force a reparse. But that caused more problems elsewhere,
2255 ;; so instead we now flush the file-structure when we see
2256 ;; omake's message telling it's about to recompile a file.
2257 ;; (or (null (compilation--loc->timestamp loc)) ;A fake-loc
2258 ;; (equal (compilation--loc->timestamp loc)
2259 ;; (setq timestamp compilation-buffer-modtime)))
2261 (with-current-buffer
2262 (compilation-find-file
2263 marker
2264 (caar (compilation--loc->file-struct loc))
2265 (cadr (car (compilation--loc->file-struct loc))))
2266 (save-restriction
2267 (widen)
2268 (goto-char (point-min))
2269 ;; Treat file's found lines in forward order, 1 by 1.
2270 (dolist (line (reverse (cddr (compilation--loc->file-struct loc))))
2271 (when (car line) ; else this is a filename w/o a line#
2272 (beginning-of-line (- (car line) last -1))
2273 (setq last (car line)))
2274 ;; Treat line's found columns and store/update a marker for each.
2275 (dolist (col (cdr line))
2276 (if (compilation--loc->col col)
2277 (if (eq (compilation--loc->col col) -1)
2278 ;; Special case for range end.
2279 (end-of-line)
2280 (compilation-move-to-column (compilation--loc->col col)
2281 columns))
2282 (beginning-of-line)
2283 (skip-chars-forward " \t"))
2284 (if (compilation--loc->marker col)
2285 (set-marker (compilation--loc->marker col) (point))
2286 (setf (compilation--loc->marker col) (point-marker)))
2287 ;; (setf (compilation--loc->timestamp col) timestamp)
2288 )))))
2289 (compilation-goto-locus marker (compilation--loc->marker loc)
2290 (compilation--loc->marker end-loc))
2291 (setf (compilation--loc->visited loc) t)))
2293 (defvar compilation-gcpro nil
2294 "Internal variable used to keep some values from being GC'd.")
2295 (make-variable-buffer-local 'compilation-gcpro)
2297 (defun compilation-fake-loc (marker file &optional line col)
2298 "Preassociate MARKER with FILE.
2299 FILE should be ABSOLUTE-FILENAME or (RELATIVE-FILENAME . DIRNAME).
2300 This is useful when you compile temporary files, but want
2301 automatic translation of the messages to the real buffer from
2302 which the temporary file came. This may also affect previous messages
2303 about FILE.
2305 Optional args LINE and COL default to 1 and beginning of
2306 indentation respectively. The marker is expected to reflect
2307 this. In the simplest case the marker points to the first line
2308 of the region that was saved to the temp file.
2310 If you concatenate several regions into the temp file (e.g. a
2311 header with variable assignments and a code region), you must
2312 call this several times, once each for the last line of one
2313 region and the first line of the next region."
2314 (or (consp file) (setq file (list file)))
2315 (compilation--flush-file-structure file)
2316 (let ((fs (compilation-get-file-structure file)))
2317 ;; Between the current call to compilation-fake-loc and the first
2318 ;; occurrence of an error message referring to `file', the data is
2319 ;; only kept in the weak hash-table compilation-locs, so we need
2320 ;; to prevent this entry in compilation-locs from being GC'd
2321 ;; away. --Stef
2322 (push fs compilation-gcpro)
2323 (let ((loc (compilation-assq (or line 1) (cdr fs))))
2324 (setq loc (compilation-assq col loc))
2325 (assert (null (cdr loc)))
2326 (setcdr loc (compilation--make-cdrloc line fs marker))
2327 loc)))
2329 (defcustom compilation-context-lines nil
2330 "Display this many lines of leading context before the current message.
2331 If nil and the left fringe is displayed, don't scroll the
2332 compilation output window; an arrow in the left fringe points to
2333 the current message. If nil and there is no left fringe, the message
2334 displays at the top of the window; there is no arrow."
2335 :type '(choice integer (const :tag "No window scrolling" nil))
2336 :group 'compilation
2337 :version "22.1")
2339 (defsubst compilation-set-window (w mk)
2340 "Align the compilation output window W with marker MK near top."
2341 (if (integerp compilation-context-lines)
2342 (set-window-start w (save-excursion
2343 (goto-char mk)
2344 (beginning-of-line
2345 (- 1 compilation-context-lines))
2346 (point)))
2347 ;; If there is no left fringe.
2348 (if (equal (car (window-fringes)) 0)
2349 (set-window-start w (save-excursion
2350 (goto-char mk)
2351 (beginning-of-line 1)
2352 (point)))))
2353 (set-window-point w mk))
2355 (defvar next-error-highlight-timer)
2357 (defun compilation-goto-locus (msg mk end-mk)
2358 "Jump to an error corresponding to MSG at MK.
2359 All arguments are markers. If END-MK is non-nil, mark is set there
2360 and overlay is highlighted between MK and END-MK."
2361 ;; Show compilation buffer in other window, scrolled to this error.
2362 (let* ((from-compilation-buffer (eq (window-buffer (selected-window))
2363 (marker-buffer msg)))
2364 ;; Use an existing window if it is in a visible frame.
2365 (pre-existing (get-buffer-window (marker-buffer msg) 0))
2366 (w (if (and from-compilation-buffer pre-existing)
2367 ;; Calling display-buffer here may end up (partly) hiding
2368 ;; the error location if the two buffers are in two
2369 ;; different frames. So don't do it if it's not necessary.
2370 pre-existing
2371 (let ((display-buffer-reuse-frames t)
2372 (pop-up-windows t))
2373 ;; Pop up a window.
2374 (display-buffer (marker-buffer msg)))))
2375 (highlight-regexp (with-current-buffer (marker-buffer msg)
2376 ;; also do this while we change buffer
2377 (compilation-set-window w msg)
2378 compilation-highlight-regexp)))
2379 ;; Ideally, the window-size should be passed to `display-buffer' (via
2380 ;; something like special-display-buffer) so it's only used when
2381 ;; creating a new window.
2382 (unless pre-existing (compilation-set-window-height w))
2384 (if from-compilation-buffer
2385 ;; If the compilation buffer window was selected,
2386 ;; keep the compilation buffer in this window;
2387 ;; display the source in another window.
2388 (let ((pop-up-windows t))
2389 (pop-to-buffer (marker-buffer mk) 'other-window))
2390 (if (window-dedicated-p (selected-window))
2391 (pop-to-buffer (marker-buffer mk))
2392 (switch-to-buffer (marker-buffer mk))))
2393 (unless (eq (goto-char mk) (point))
2394 ;; If narrowing gets in the way of going to the right place, widen.
2395 (widen)
2396 (if next-error-move-function
2397 (funcall next-error-move-function msg mk)
2398 (goto-char mk)))
2399 (if end-mk
2400 (push-mark end-mk t)
2401 (if mark-active (setq mark-active)))
2402 ;; If hideshow got in the way of
2403 ;; seeing the right place, open permanently.
2404 (dolist (ov (overlays-at (point)))
2405 (when (eq 'hs (overlay-get ov 'invisible))
2406 (delete-overlay ov)
2407 (goto-char mk)))
2409 (when highlight-regexp
2410 (if (timerp next-error-highlight-timer)
2411 (cancel-timer next-error-highlight-timer))
2412 (unless compilation-highlight-overlay
2413 (setq compilation-highlight-overlay
2414 (make-overlay (point-min) (point-min)))
2415 (overlay-put compilation-highlight-overlay 'face 'next-error))
2416 (with-current-buffer (marker-buffer mk)
2417 (save-excursion
2418 (if end-mk (goto-char end-mk) (end-of-line))
2419 (let ((end (point)))
2420 (if mk (goto-char mk) (beginning-of-line))
2421 (if (and (stringp highlight-regexp)
2422 (re-search-forward highlight-regexp end t))
2423 (progn
2424 (goto-char (match-beginning 0))
2425 (move-overlay compilation-highlight-overlay
2426 (match-beginning 0) (match-end 0)
2427 (current-buffer)))
2428 (move-overlay compilation-highlight-overlay
2429 (point) end (current-buffer)))
2430 (if (or (eq next-error-highlight t)
2431 (numberp next-error-highlight))
2432 ;; We want highlighting: delete overlay on next input.
2433 (add-hook 'pre-command-hook
2434 'compilation-goto-locus-delete-o)
2435 ;; We don't want highlighting: delete overlay now.
2436 (delete-overlay compilation-highlight-overlay))
2437 ;; We want highlighting for a limited time:
2438 ;; set up a timer to delete it.
2439 (when (numberp next-error-highlight)
2440 (setq next-error-highlight-timer
2441 (run-at-time next-error-highlight nil
2442 'compilation-goto-locus-delete-o)))))))
2443 (when (and (eq next-error-highlight 'fringe-arrow))
2444 ;; We want a fringe arrow (instead of highlighting).
2445 (setq next-error-overlay-arrow-position
2446 (copy-marker (line-beginning-position))))))
2448 (defun compilation-goto-locus-delete-o ()
2449 (delete-overlay compilation-highlight-overlay)
2450 ;; Get rid of timer and hook that would try to do this again.
2451 (if (timerp next-error-highlight-timer)
2452 (cancel-timer next-error-highlight-timer))
2453 (remove-hook 'pre-command-hook
2454 'compilation-goto-locus-delete-o))
2456 (defun compilation-find-file (marker filename directory &rest formats)
2457 "Find a buffer for file FILENAME.
2458 If FILENAME is not found at all, ask the user where to find it.
2459 Pop up the buffer containing MARKER and scroll to MARKER if we ask
2460 the user where to find the file.
2461 Search the directories in `compilation-search-path'.
2462 A nil in `compilation-search-path' means to try the
2463 \"current\" directory, which is passed in DIRECTORY.
2464 If DIRECTORY is relative, it is combined with `default-directory'.
2465 If DIRECTORY is nil, that means use `default-directory'.
2466 FORMATS, if given, is a list of formats to reformat FILENAME when
2467 looking for it: for each element FMT in FORMATS, this function
2468 attempts to find a file whose name is produced by (format FMT FILENAME)."
2469 (or formats (setq formats '("%s")))
2470 (let ((dirs compilation-search-path)
2471 (spec-dir (if directory
2472 (expand-file-name directory)
2473 default-directory))
2474 buffer thisdir fmts name)
2475 (if (file-name-absolute-p filename)
2476 ;; The file name is absolute. Use its explicit directory as
2477 ;; the first in the search path, and strip it from FILENAME.
2478 (setq filename (abbreviate-file-name (expand-file-name filename))
2479 dirs (cons (file-name-directory filename) dirs)
2480 filename (file-name-nondirectory filename)))
2481 ;; Now search the path.
2482 (while (and dirs (null buffer))
2483 (setq thisdir (or (car dirs) spec-dir)
2484 fmts formats)
2485 ;; For each directory, try each format string.
2486 (while (and fmts (null buffer))
2487 (setq name (expand-file-name (format (car fmts) filename) thisdir)
2488 buffer (and (file-exists-p name)
2489 (find-file-noselect name))
2490 fmts (cdr fmts)))
2491 (setq dirs (cdr dirs)))
2492 (while (null buffer) ;Repeat until the user selects an existing file.
2493 ;; The file doesn't exist. Ask the user where to find it.
2494 (save-excursion ;This save-excursion is probably not right.
2495 (let ((pop-up-windows t))
2496 (compilation-set-window (display-buffer (marker-buffer marker))
2497 marker)
2498 (let* ((name (read-file-name
2499 (format "Find this %s in (default %s): "
2500 compilation-error filename)
2501 spec-dir filename t nil
2502 ;; The predicate below is fine when called from
2503 ;; minibuffer-complete-and-exit, but it's too
2504 ;; restrictive otherwise, since it also prevents the
2505 ;; user from completing "fo" to "foo/" when she
2506 ;; wants to enter "foo/bar".
2508 ;; Try to make sure the user can only select
2509 ;; a valid answer. This predicate may be ignored,
2510 ;; tho, so we still have to double-check afterwards.
2511 ;; TODO: We should probably fix read-file-name so
2512 ;; that it never ignores this predicate, even when
2513 ;; using popup dialog boxes.
2514 ;; (lambda (name)
2515 ;; (if (file-directory-p name)
2516 ;; (setq name (expand-file-name filename name)))
2517 ;; (file-exists-p name))
2519 (origname name))
2520 (cond
2521 ((not (file-exists-p name))
2522 (message "Cannot find file `%s'" name)
2523 (ding) (sit-for 2))
2524 ((and (file-directory-p name)
2525 (not (file-exists-p
2526 (setq name (expand-file-name filename name)))))
2527 (message "No `%s' in directory %s" filename origname)
2528 (ding) (sit-for 2))
2530 (setq buffer (find-file-noselect name))))))))
2531 ;; Make intangible overlays tangible.
2532 ;; This is weird: it's not even clear which is the current buffer,
2533 ;; so the code below can't be expected to DTRT here. -- Stef
2534 (dolist (ov (overlays-in (point-min) (point-max)))
2535 (when (overlay-get ov 'intangible)
2536 (overlay-put ov 'intangible nil)))
2537 buffer))
2539 (defun compilation-get-file-structure (file &optional fmt)
2540 "Retrieve FILE's file-structure or create a new one.
2541 FILE should be (FILENAME) or (RELATIVE-FILENAME . DIRNAME).
2542 In the former case, FILENAME may be relative or absolute.
2544 The file-structure looks like this:
2545 ((FILENAME [DIR-FROM-PREV-MSG]) FMT LINE-STRUCT...)"
2546 (or (gethash file compilation-locs)
2547 ;; File was not previously encountered, at least not in the form passed.
2548 ;; Let's normalize it and look again.
2549 (let ((filename (car file))
2550 ;; Get the specified directory from FILE.
2551 (spec-directory (if (cdr file)
2552 (file-truename (cdr file)))))
2554 ;; Check for a comint-file-name-prefix and prepend it if appropriate.
2555 ;; (This is very useful for compilation-minor-mode in an rlogin-mode
2556 ;; buffer.)
2557 (when (and (boundp 'comint-file-name-prefix)
2558 (not (equal comint-file-name-prefix "")))
2559 (if (file-name-absolute-p filename)
2560 (setq filename
2561 (concat comint-file-name-prefix filename))
2562 (if spec-directory
2563 (setq spec-directory
2564 (file-truename
2565 (concat comint-file-name-prefix spec-directory))))))
2567 ;; If compilation-parse-errors-filename-function is
2568 ;; defined, use it to process the filename.
2569 (when compilation-parse-errors-filename-function
2570 (setq filename
2571 (funcall compilation-parse-errors-filename-function
2572 filename)))
2574 ;; Some compilers (e.g. Sun's java compiler, reportedly) produce bogus
2575 ;; file names like "./bar//foo.c" for file "bar/foo.c";
2576 ;; expand-file-name will collapse these into "/foo.c" and fail to find
2577 ;; the appropriate file. So we look for doubled slashes in the file
2578 ;; name and fix them.
2579 (setq filename (command-line-normalize-file-name filename))
2581 ;; Store it for the possibly unnormalized name
2582 (puthash file
2583 ;; Retrieve or create file-structure for normalized name
2584 ;; The gethash used to not use spec-directory, but
2585 ;; this leads to errors when files in different
2586 ;; directories have the same name:
2587 ;; http://lists.gnu.org/archive/html/emacs-devel/2007-08/msg00463.html
2588 (or (gethash (cons filename spec-directory) compilation-locs)
2589 (puthash (cons filename spec-directory)
2590 (compilation--make-file-struct
2591 (list filename spec-directory) fmt)
2592 compilation-locs))
2593 compilation-locs))))
2595 (defun compilation--flush-file-structure (file)
2596 (or (consp file) (setq file (list file)))
2597 (let ((fs (compilation-get-file-structure file)))
2598 (assert (eq fs (gethash file compilation-locs)))
2599 (assert (eq fs (gethash (cons (caar fs) (cadr (car fs)))
2600 compilation-locs)))
2601 (maphash (lambda (k v)
2602 (if (eq v fs) (remhash k compilation-locs)))
2603 compilation-locs)))
2605 (add-to-list 'debug-ignored-errors "\\`No more [-a-z ]+s yet\\'")
2606 (add-to-list 'debug-ignored-errors "\\`Moved past last .*")
2608 ;;; Compatibility with the old compile.el.
2610 (defvaralias 'compilation-last-buffer 'next-error-last-buffer)
2611 (defvar compilation-parsing-end (make-marker))
2612 (defvar compilation-error-list nil)
2613 (defvar compilation-old-error-list nil)
2615 (defun compilation--compat-error-properties (err)
2616 "Map old-style error ERR to new-style message."
2617 ;; Old-style structure is (MARKER (FILE DIR) LINE COL) or
2618 ;; (MARKER . MARKER).
2619 (let ((dst (cdr err)))
2620 (if (markerp dst)
2621 `(compilation-message ,(compilation--make-message
2622 (cons nil (compilation--make-cdrloc
2623 nil nil dst))
2624 2 nil)
2625 help-echo "mouse-2: visit the source location"
2626 keymap compilation-button-map
2627 mouse-face highlight)
2628 ;; Too difficult to do it by hand: dispatch to the normal code.
2629 (let* ((file (pop dst))
2630 (line (pop dst))
2631 (col (pop dst))
2632 (filename (pop file))
2633 (dirname (pop file))
2634 (fmt (pop file)))
2635 (compilation-internal-error-properties
2636 (cons filename dirname) line nil col nil 2 fmt)))))
2638 (defun compilation--compat-parse-errors (limit)
2639 (when compilation-parse-errors-function
2640 ;; FIXME: We should remove the rest of the compilation keywords
2641 ;; but we can't do that from here because font-lock is using
2642 ;; the value right now. --Stef
2643 (save-excursion
2644 (setq compilation-error-list nil)
2645 ;; Reset compilation-parsing-end each time because font-lock
2646 ;; might force us the re-parse many times (typically because
2647 ;; some code adds some text-property to the output that we
2648 ;; already parsed). You might say "why reparse", well:
2649 ;; because font-lock has just removed the `compilation-message' property
2650 ;; so have to do it all over again.
2651 (if compilation-parsing-end
2652 (set-marker compilation-parsing-end (point))
2653 (setq compilation-parsing-end (point-marker)))
2654 (condition-case nil
2655 ;; Ignore any error: we're calling this function earlier than
2656 ;; in the old compile.el so things might not all be setup yet.
2657 (funcall compilation-parse-errors-function limit nil)
2658 (error nil))
2659 (dolist (err (if (listp compilation-error-list) compilation-error-list))
2660 (let* ((src (car err))
2661 (dst (cdr err))
2662 (loc (cond ((markerp dst)
2663 (cons nil
2664 (compilation--make-cdrloc nil nil dst)))
2665 ((consp dst)
2666 (cons (nth 2 dst)
2667 (compilation--make-cdrloc
2668 (nth 1 dst)
2669 (cons (cdar dst) (caar dst))
2670 nil))))))
2671 (when loc
2672 (goto-char src)
2673 ;; (put-text-property src (line-end-position)
2674 ;; 'font-lock-face 'font-lock-warning-face)
2675 (put-text-property src (line-end-position)
2676 'compilation-message
2677 (compilation--make-message loc 2 nil)))))))
2678 (goto-char limit)
2679 nil)
2681 ;; Beware! this is not only compatibility code. New code also uses it. --Stef
2682 (defun compilation-forget-errors ()
2683 ;; In case we hit the same file/line specs, we want to recompute a new
2684 ;; marker for them, so flush our cache.
2685 (clrhash compilation-locs)
2686 (setq compilation-gcpro nil)
2687 ;; FIXME: the old code reset the directory-stack, so maybe we should
2688 ;; put a `directory change' marker of some sort, but where? -stef
2690 ;; FIXME: The old code moved compilation-current-error (which was
2691 ;; virtually represented by a mix of compilation-parsing-end and
2692 ;; compilation-error-list) to point-min, but that was only meaningful for
2693 ;; the internal uses of compilation-forget-errors: all calls from external
2694 ;; packages seem to be followed by a move of compilation-parsing-end to
2695 ;; something equivalent to point-max. So we heuristically move
2696 ;; compilation-current-error to point-max (since the external package
2697 ;; won't know that it should do it). --Stef
2698 (setq compilation-current-error nil)
2699 (let* ((proc (get-buffer-process (current-buffer)))
2700 (mark (if proc (process-mark proc)))
2701 (pos (or mark (point-max))))
2702 (setq compilation-messages-start
2703 ;; In the future, ignore the text already present in the buffer.
2704 ;; Since many process filter functions insert before markers,
2705 ;; we need to put ours just before the insertion point rather
2706 ;; than at the insertion point. If that's not possible, then
2707 ;; don't use a marker. --Stef
2708 (if (> pos (point-min)) (copy-marker (1- pos)) pos)))
2709 ;; Again, since this command is used in buffers that contain several
2710 ;; compilations, to set the beginning of "this compilation", it's a good
2711 ;; place to reset compilation-auto-jump-to-next.
2712 (set (make-local-variable 'compilation-auto-jump-to-next)
2713 (or compilation-auto-jump-to-first-error
2714 (eq compilation-scroll-output 'first-error))))
2716 (provide 'compile)
2718 ;;; compile.el ends here