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