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