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