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