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