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