]> code.delx.au - gnu-emacs/blob - lisp/progmodes/compile.el
* progmodes/grep.el (grep-compute-defaults): Keep default values.
[gnu-emacs] / lisp / progmodes / compile.el
1 ;;; compile.el --- run compiler as inferior of Emacs, parse error messages
2
3 ;; Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 ;; 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5
6 ;; Authors: Roland McGrath <roland@gnu.org>,
7 ;; Daniel Pfeiffer <occitan@esperanto.org>
8 ;; Maintainer: FSF
9 ;; Keywords: tools, processes
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
27
28 ;;; Commentary:
29
30 ;; This package provides the compile facilities documented in the Emacs user's
31 ;; manual.
32
33 ;; This mode uses some complex data-structures:
34
35 ;; LOC (or location) is a list of (COLUMN LINE FILE-STRUCTURE)
36
37 ;; COLUMN and LINE are numbers parsed from an error message. COLUMN and maybe
38 ;; LINE will be nil for a message that doesn't contain them. Then the
39 ;; location refers to a indented beginning of line or beginning of file.
40 ;; Once any location in some file has been jumped to, the list is extended to
41 ;; (COLUMN LINE FILE-STRUCTURE MARKER TIMESTAMP . VISITED)
42 ;; for all LOCs pertaining to that file.
43 ;; MARKER initially points to LINE and COLUMN in a buffer visiting that file.
44 ;; Being a marker it sticks to some text, when the buffer grows or shrinks
45 ;; before that point. VISITED is t if we have jumped there, else nil.
46 ;; TIMESTAMP is necessary because of "incremental compilation": `omake -P'
47 ;; polls filesystem for changes and recompiles when a file is modified
48 ;; using the same *compilation* buffer. this necessitates re-parsing markers.
49
50 ;; FILE-STRUCTURE is a list of
51 ;; ((FILENAME . DIRECTORY) FORMATS (LINE LOC ...) ...)
52
53 ;; FILENAME is a string parsed from an error message. DIRECTORY is a string
54 ;; obtained by following directory change messages. DIRECTORY will be nil for
55 ;; an absolute filename. FORMATS is a list of formats to apply to FILENAME if
56 ;; a file of that name can't be found.
57 ;; The rest of the list is an alist of elements with LINE as key. The keys
58 ;; are either nil or line numbers. If present, nil comes first, followed by
59 ;; the numbers in decreasing order. The LOCs for each line are again an alist
60 ;; ordered the same way. Note that the whole file structure is referenced in
61 ;; every LOC.
62
63 ;; MESSAGE is a list of (LOC TYPE END-LOC)
64
65 ;; TYPE is 0 for info or 1 for warning if the message matcher identified it as
66 ;; such, 2 otherwise (for a real error). END-LOC is a LOC pointing to the
67 ;; other end, if the parsed message contained a range. If the end of the
68 ;; range didn't specify a COLUMN, it defaults to -1, meaning end of line.
69 ;; These are the value of the `message' text-properties in the compilation
70 ;; buffer.
71
72 ;;; Code:
73
74 (eval-when-compile (require 'cl))
75
76 (defvar font-lock-extra-managed-props)
77 (defvar font-lock-keywords)
78 (defvar font-lock-maximum-size)
79 (defvar font-lock-support-mode)
80
81
82 (defgroup compilation nil
83 "Run compiler as inferior of Emacs, parse error messages."
84 :group 'tools
85 :group 'processes)
86
87
88 ;;;###autoload
89 (defcustom compilation-mode-hook nil
90 "List of hook functions run by `compilation-mode' (see `run-mode-hooks')."
91 :type 'hook
92 :group 'compilation)
93
94 ;;;###autoload
95 (defcustom compilation-window-height nil
96 "Number of lines in a compilation window. If nil, use Emacs default."
97 :type '(choice (const :tag "Default" nil)
98 integer)
99 :group 'compilation)
100
101 (defvar compilation-first-column 1
102 "*This is how compilers number the first column, usually 1 or 0.")
103
104 (defvar compilation-parse-errors-filename-function nil
105 "Function to call to post-process filenames while parsing error messages.
106 It takes one arg FILENAME which is the name of a file as found
107 in the compilation output, and should return a transformed file name.")
108
109 ;;;###autoload
110 (defvar compilation-process-setup-function nil
111 "*Function to call to customize the compilation process.
112 This function is called immediately before the compilation process is
113 started. It can be used to set any variables or functions that are used
114 while processing the output of the compilation process. The function
115 is called with variables `compilation-buffer' and `compilation-window'
116 bound to the compilation buffer and window, respectively.")
117
118 ;;;###autoload
119 (defvar compilation-buffer-name-function nil
120 "Function to compute the name of a compilation buffer.
121 The function receives one argument, the name of the major mode of the
122 compilation buffer. It should return a string.
123 If nil, compute the name with `(concat \"*\" (downcase major-mode) \"*\")'.")
124
125 ;;;###autoload
126 (defvar compilation-finish-function nil
127 "Function to call when a compilation process finishes.
128 It is called with two arguments: the compilation buffer, and a string
129 describing how the process finished.")
130
131 (make-obsolete-variable 'compilation-finish-function
132 "use `compilation-finish-functions', but it works a little differently."
133 "22.1")
134
135 ;;;###autoload
136 (defvar compilation-finish-functions nil
137 "Functions to call when a compilation process finishes.
138 Each function is called with two arguments: the compilation buffer,
139 and a string describing how the process finished.")
140
141 (defvar compilation-in-progress nil
142 "List of compilation processes now running.")
143 (or (assq 'compilation-in-progress minor-mode-alist)
144 (setq minor-mode-alist (cons '(compilation-in-progress " Compiling")
145 minor-mode-alist)))
146
147 (defvar compilation-error "error"
148 "Stem of message to print when no matches are found.")
149
150 (defvar compilation-arguments nil
151 "Arguments that were given to `compilation-start'.")
152
153 (defvar compilation-num-errors-found)
154
155 (defconst compilation-error-regexp-alist-alist
156 '((absoft
157 "^\\(?:[Ee]rror on \\|[Ww]arning on\\( \\)\\)?[Ll]ine[ \t]+\\([0-9]+\\)[ \t]+\
158 of[ \t]+\"?\\([a-zA-Z]?:?[^\":\n]+\\)\"?:" 3 2 nil (1))
159
160 (ada
161 "\\(warning: .*\\)? at \\([^ \n]+\\):\\([0-9]+\\)$" 2 3 nil (1))
162
163 (aix
164 " in line \\([0-9]+\\) of file \\([^ \n]+[^. \n]\\)\\.? " 2 1)
165
166 (ant
167 "^[ \t]*\\[[^] \n]+\\][ \t]*\\([^: \n]+\\):\\([0-9]+\\):\\(?:\\([0-9]+\\):[0-9]+:[0-9]+:\\)?\
168 \\( warning\\)?" 1 2 3 (4))
169
170 (bash
171 "^\\([^: \n\t]+\\): line \\([0-9]+\\):" 1 2)
172
173 (borland
174 "^\\(?:Error\\|Warnin\\(g\\)\\) \\(?:[FEW][0-9]+ \\)?\
175 \\([a-zA-Z]?:?[^:( \t\n]+\\)\
176 \\([0-9]+\\)\\(?:[) \t]\\|:[^0-9\n]\\)" 2 3 nil (1))
177
178 (caml
179 "^ *File \\(\"?\\)\\([^,\" \n\t<>]+\\)\\1, lines? \\([0-9]+\\)-?\\([0-9]+\\)?\\(?:$\\|,\
180 \\(?: characters? \\([0-9]+\\)-?\\([0-9]+\\)?:\\)?\\([ \n]Warning:\\)?\\)"
181 2 (3 . 4) (5 . 6) (7))
182
183 (comma
184 "^\"\\([^,\" \n\t]+\\)\", line \\([0-9]+\\)\
185 \\(?:[(. pos]+\\([0-9]+\\))?\\)?[:.,; (-]\\( warning:\\|[-0-9 ]*(W)\\)?" 1 2 3 (4))
186
187 (edg-1
188 "^\\([^ \n]+\\)(\\([0-9]+\\)): \\(?:error\\|warnin\\(g\\)\\|remar\\(k\\)\\)"
189 1 2 nil (3 . 4))
190 (edg-2
191 "at line \\([0-9]+\\) of \"\\([^ \n]+\\)\"$"
192 2 1 nil 0)
193
194 (epc
195 "^Error [0-9]+ at (\\([0-9]+\\):\\([^)\n]+\\))" 2 1)
196
197 (ftnchek
198 "\\(^Warning .*\\)? line[ \n]\\([0-9]+\\)[ \n]\\(?:col \\([0-9]+\\)[ \n]\\)?file \\([^ :;\n]+\\)"
199 4 2 3 (1))
200
201 (iar
202 "^\"\\(.*\\)\",\\([0-9]+\\)\\s-+\\(?:Error\\|Warnin\\(g\\)\\)\\[[0-9]+\\]:"
203 1 2 nil (3))
204
205 (ibm
206 "^\\([^( \n\t]+\\)(\\([0-9]+\\):\\([0-9]+\\)) :\
207 \\(?:warnin\\(g\\)\\|informationa\\(l\\)\\)?" 1 2 3 (4 . 5))
208
209 ;; fixme: should be `mips'
210 (irix
211 "^[-[:alnum:]_/ ]+: \\(?:\\(?:[sS]evere\\|[eE]rror\\|[wW]arnin\\(g\\)\\|[iI]nf\\(o\\)\\)[0-9 ]*: \\)?\
212 \\([^,\" \n\t]+\\)\\(?:, line\\|:\\) \\([0-9]+\\):" 3 4 nil (1 . 2))
213
214 (java
215 "^\\(?:[ \t]+at \\|==[0-9]+== +\\(?:at\\|b\\(y\\)\\)\\).+(\\([^()\n]+\\):\\([0-9]+\\))$" 2 3 nil (1))
216
217 (jikes-file
218 "^\\(?:Found\\|Issued\\) .* compiling \"\\(.+\\)\":$" 1 nil nil 0)
219 (jikes-line
220 "^ *\\([0-9]+\\)\\.[ \t]+.*\n +\\(<-*>\n\\*\\*\\* \\(?:Error\\|Warnin\\(g\\)\\)\\)"
221 nil 1 nil 2 0
222 (2 (compilation-face '(3))))
223
224 (gnu
225 ;; I have no idea what this first line is supposed to match, but it
226 ;; makes things ambiguous with output such as "foo:344:50:blabla" since
227 ;; the "foo" part can match this first line (in which case the file
228 ;; name as "344"). To avoid this, the second line disallows filenames
229 ;; exclusively composed of digits. --Stef
230 ;; Similarly, we get lots of false positives with messages including
231 ;; times of the form "HH:MM:SS" where MM is taken as a line number, so
232 ;; the last line tries to rule out message where the info after the
233 ;; line number starts with "SS". --Stef
234 "^\\(?:[[:alpha:]][-[:alnum:].]+: ?\\)?\
235 \\([0-9]*[^0-9\n]\\(?:[^\n ]\\| [^-\n]\\)*?\\): ?\
236 \\([0-9]+\\)\\(?:\\([.:]\\)\\([0-9]+\\)\\)?\
237 \\(?:-\\([0-9]+\\)?\\(?:\\3\\([0-9]+\\)\\)?\\)?:\
238 \\(?: *\\(\\(?:Future\\|Runtime\\)?[Ww]arning\\|W:\\)\\|\
239 *\\([Ii]nfo\\(?:\\>\\|rmationa?l?\\)\\|I:\\|instantiated from\\)\\|\
240 \[0-9]?\\(?:[^0-9\n]\\|$\\)\\|[0-9][0-9][0-9]\\)"
241 1 (2 . 5) (4 . 6) (7 . 8))
242
243 ;; The `gnu' style above can incorrectly match gcc's "In file
244 ;; included from" message, so we process that first. -- cyd
245 (gcc-include
246 "^\\(?:In file included\\| \\) from \
247 \\(.+\\):\\([0-9]+\\)\\(?:\\(:\\)\\|\\(,\\)\\)?" 1 2 nil (3 . 4))
248
249 (lcc
250 "^\\(?:E\\|\\(W\\)\\), \\([^(\n]+\\)(\\([0-9]+\\),[ \t]*\\([0-9]+\\)"
251 2 3 4 (1))
252
253 (makepp
254 "^makepp\\(?:\\(?:: warning\\(:\\).*?\\|\\(: Scanning\\|: [LR]e?l?oading makefile\\|: Imported\\|log:.*?\\) \\|: .*?\\)\
255 `\\(\\(\\S +?\\)\\(?::\\([0-9]+\\)\\)?\\)['(]\\)"
256 4 5 nil (1 . 2) 3
257 ("`\\(\\(\\S +?\\)\\(?::\\([0-9]+\\)\\)?\\)['(]" nil nil
258 (2 compilation-info-face)
259 (3 compilation-line-face nil t)
260 (1 (compilation-error-properties 2 3 nil nil nil 0 nil)
261 append)))
262
263 ;; Should be lint-1, lint-2 (SysV lint)
264 (mips-1
265 " (\\([0-9]+\\)) in \\([^ \n]+\\)" 2 1)
266 (mips-2
267 " in \\([^()\n ]+\\)(\\([0-9]+\\))$" 1 2)
268
269 (msft
270 "^\\([0-9]+>\\)?\\(\\(?:[a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\)) \
271 : \\(?:error\\|warnin\\(g\\)\\) C[0-9]+:" 2 3 nil (4))
272
273 (oracle
274 "^\\(?:Semantic error\\|Error\\|PCC-[0-9]+:\\).* line \\([0-9]+\\)\
275 \\(?:\\(?:,\\| at\\)? column \\([0-9]+\\)\\)?\
276 \\(?:,\\| in\\| of\\)? file \\(.*?\\):?$"
277 3 1 2)
278
279 (perl
280 " at \\([^ \n]+\\) line \\([0-9]+\\)\\(?:[,.]\\|$\\)" 1 2)
281
282 (rxp
283 "^\\(?:Error\\|Warnin\\(g\\)\\):.*\n.* line \\([0-9]+\\) char\
284 \\([0-9]+\\) of file://\\(.+\\)"
285 4 2 3 (1))
286
287 (sparc-pascal-file
288 "^\\w\\w\\w \\w\\w\\w +[0-3]?[0-9] +[0-2][0-9]:[0-5][0-9]:[0-5][0-9]\
289 [12][09][0-9][0-9] +\\(.*\\):$"
290 1 nil nil 0)
291 (sparc-pascal-line
292 "^\\(\\(?:E\\|\\(w\\)\\) +[0-9]+\\) line \\([0-9]+\\) - "
293 nil 3 nil (2) nil (1 (compilation-face '(2))))
294 (sparc-pascal-example
295 "^ +\\([0-9]+\\) +.*\n\\(\\(?:e\\|\\(w\\)\\) [0-9]+\\)-+"
296 nil 1 nil (3) nil (2 (compilation-face '(3))))
297
298 (sun
299 ": \\(?:ERROR\\|WARNIN\\(G\\)\\|REMAR\\(K\\)\\) \\(?:[[:alnum:] ]+, \\)?\
300 File = \\(.+\\), Line = \\([0-9]+\\)\\(?:, Column = \\([0-9]+\\)\\)?"
301 3 4 5 (1 . 2))
302
303 (sun-ada
304 "^\\([^, \n\t]+\\), line \\([0-9]+\\), char \\([0-9]+\\)[:., \(-]" 1 2 3)
305
306 (4bsd
307 "\\(?:^\\|:: \\|\\S ( \\)\\(/[^ \n\t()]+\\)(\\([0-9]+\\))\
308 \\(?:: \\(warning:\\)?\\|$\\| ),\\)" 1 2 nil (3))
309
310 (gcov-file
311 "^ *-: *\\(0\\):Source:\\(.+\\)$"
312 2 1 nil 0 nil
313 (1 compilation-line-face prepend) (2 compilation-info-face prepend))
314 (gcov-header
315 "^ *-: *\\(0\\):\\(?:Object\\|Graph\\|Data\\|Runs\\|Programs\\):.+$"
316 nil 1 nil 0 nil
317 (1 compilation-line-face prepend))
318 ;; Underlines over all lines of gcov output are too uncomfortable to read.
319 ;; However, hyperlinks embedded in the lines are useful.
320 ;; So I put default face on the lines; and then put
321 ;; compilation-*-face by manually to eliminate the underlines.
322 ;; The hyperlinks are still effective.
323 (gcov-nomark
324 "^ *-: *\\([1-9]\\|[0-9]\\{2,\\}\\):.*$"
325 nil 1 nil 0 nil
326 (0 'default t)
327 (1 compilation-line-face prepend))
328 (gcov-called-line
329 "^ *\\([0-9]+\\): *\\([0-9]+\\):.*$"
330 nil 2 nil 0 nil
331 (0 'default t)
332 (1 compilation-info-face prepend) (2 compilation-line-face prepend))
333 (gcov-never-called
334 "^ *\\(#####\\): *\\([0-9]+\\):.*$"
335 nil 2 nil 2 nil
336 (0 'default t)
337 (1 compilation-error-face prepend) (2 compilation-line-face prepend))
338 )
339 "Alist of values for `compilation-error-regexp-alist'.")
340
341 (defcustom compilation-error-regexp-alist
342 (mapcar 'car compilation-error-regexp-alist-alist)
343 "Alist that specifies how to match errors in compiler output.
344 On GNU and Unix, any string is a valid filename, so these
345 matchers must make some common sense assumptions, which catch
346 normal cases. A shorter list will be lighter on resource usage.
347
348 Instead of an alist element, you can use a symbol, which is
349 looked up in `compilation-error-regexp-alist-alist'. You can see
350 the predefined symbols and their effects in the file
351 `etc/compilation.txt' (linked below if you are customizing this).
352
353 Each elt has the form (REGEXP FILE [LINE COLUMN TYPE HYPERLINK
354 HIGHLIGHT...]). If REGEXP matches, the FILE'th subexpression
355 gives the file name, and the LINE'th subexpression gives the line
356 number. The COLUMN'th subexpression gives the column number on
357 that line.
358
359 If FILE, LINE or COLUMN are nil or that index didn't match, that
360 information is not present on the matched line. In that case the
361 file name is assumed to be the same as the previous one in the
362 buffer, line number defaults to 1 and column defaults to
363 beginning of line's indentation.
364
365 FILE can also have the form (FILE FORMAT...), where the FORMATs
366 \(e.g. \"%s.c\") will be applied in turn to the recognized file
367 name, until a file of that name is found. Or FILE can also be a
368 function that returns (FILENAME) or (RELATIVE-FILENAME . DIRNAME).
369 In the former case, FILENAME may be relative or absolute.
370
371 LINE can also be of the form (LINE . END-LINE) meaning a range
372 of lines. COLUMN can also be of the form (COLUMN . END-COLUMN)
373 meaning a range of columns starting on LINE and ending on
374 END-LINE, if that matched.
375
376 TYPE is 2 or nil for a real error or 1 for warning or 0 for info.
377 TYPE can also be of the form (WARNING . INFO). In that case this
378 will be equivalent to 1 if the WARNING'th subexpression matched
379 or else equivalent to 0 if the INFO'th subexpression matched.
380 See `compilation-error-face', `compilation-warning-face',
381 `compilation-info-face' and `compilation-skip-threshold'.
382
383 What matched the HYPERLINK'th subexpression has `mouse-face' and
384 `compilation-message-face' applied. If this is nil, the text
385 matched by the whole REGEXP becomes the hyperlink.
386
387 Additional HIGHLIGHTs as described under `font-lock-keywords' can
388 be added."
389 :type `(set :menu-tag "Pick"
390 ,@(mapcar (lambda (elt)
391 (list 'const (car elt)))
392 compilation-error-regexp-alist-alist))
393 :link `(file-link :tag "example file"
394 ,(expand-file-name "compilation.txt" data-directory))
395 :group 'compilation)
396
397 ;;;###autoload(put 'compilation-directory 'safe-local-variable 'stringp)
398 (defvar compilation-directory nil
399 "Directory to restore to when doing `recompile'.")
400
401 (defvar compilation-directory-matcher
402 '("\\(?:Entering\\|Leavin\\(g\\)\\) directory `\\(.+\\)'$" (2 . 1))
403 "A list for tracking when directories are entered or left.
404 If nil, do not track directories, e.g. if all file names are absolute. The
405 first element is the REGEXP matching these messages. It can match any number
406 of variants, e.g. different languages. The remaining elements are all of the
407 form (DIR . LEAVE). If for any one of these the DIR'th subexpression
408 matches, that is a directory name. If LEAVE is nil or the corresponding
409 LEAVE'th subexpression doesn't match, this message is about going into another
410 directory. If it does match anything, this message is about going back to the
411 directory we were in before the last entering message. If you change this,
412 you may also want to change `compilation-page-delimiter'.")
413
414 (defvar compilation-page-delimiter
415 "^\\(?:\f\\|.*\\(?:Entering\\|Leaving\\) directory `.+'\n\\)+"
416 "Value of `page-delimiter' in Compilation mode.")
417
418 (defvar compilation-mode-font-lock-keywords
419 '(;; configure output lines.
420 ("^[Cc]hecking \\(?:[Ff]or \\|[Ii]f \\|[Ww]hether \\(?:to \\)?\\)?\\(.+\\)\\.\\.\\. *\\(?:(cached) *\\)?\\(\\(yes\\(?: .+\\)?\\)\\|no\\|\\(.*\\)\\)$"
421 (1 font-lock-variable-name-face)
422 (2 (compilation-face '(4 . 3))))
423 ;; Command output lines. Recognize `make[n]:' lines too.
424 ("^\\([[:alnum:]_/.+-]+\\)\\(\\[\\([0-9]+\\)\\]\\)?[ \t]*:"
425 (1 font-lock-function-name-face) (3 compilation-line-face nil t))
426 (" --?o\\(?:utfile\\|utput\\)?[= ]?\\(\\S +\\)" . 1)
427 ("^Compilation \\(finished\\).*"
428 (0 '(face nil message nil help-echo nil mouse-face nil) t)
429 (1 compilation-info-face))
430 ("^Compilation \\(exited abnormally\\|interrupt\\|killed\\|terminated\\|segmentation fault\\)\\(?:.*with code \\([0-9]+\\)\\)?.*"
431 (0 '(face nil message nil help-echo nil mouse-face nil) t)
432 (1 compilation-error-face)
433 (2 compilation-error-face nil t)))
434 "Additional things to highlight in Compilation mode.
435 This gets tacked on the end of the generated expressions.")
436
437 (defvar compilation-highlight-regexp t
438 "Regexp matching part of visited source lines to highlight temporarily.
439 Highlight entire line if t; don't highlight source lines if nil.")
440
441 (defvar compilation-highlight-overlay nil
442 "Overlay used to temporarily highlight compilation matches.")
443
444 (defcustom compilation-error-screen-columns t
445 "If non-nil, column numbers in error messages are screen columns.
446 Otherwise they are interpreted as character positions, with
447 each character occupying one column.
448 The default is to use screen columns, which requires that the compilation
449 program and Emacs agree about the display width of the characters,
450 especially the TAB character."
451 :type 'boolean
452 :group 'compilation
453 :version "20.4")
454
455 (defcustom compilation-read-command t
456 "Non-nil means \\[compile] reads the compilation command to use.
457 Otherwise, \\[compile] just uses the value of `compile-command'."
458 :type 'boolean
459 :group 'compilation)
460
461 ;;;###autoload
462 (defcustom compilation-ask-about-save t
463 "Non-nil means \\[compile] asks which buffers to save before compiling.
464 Otherwise, it saves all modified buffers without asking."
465 :type 'boolean
466 :group 'compilation)
467
468 ;;;###autoload
469 (defcustom compilation-search-path '(nil)
470 "List of directories to search for source files named in error messages.
471 Elements should be directory names, not file names of directories.
472 The value nil as an element means to try the default directory."
473 :type '(repeat (choice (const :tag "Default" nil)
474 (string :tag "Directory")))
475 :group 'compilation)
476
477 ;;;###autoload
478 (defcustom compile-command "make -k "
479 "Last shell command used to do a compilation; default for next compilation.
480
481 Sometimes it is useful for files to supply local values for this variable.
482 You might also use mode hooks to specify it in certain modes, like this:
483
484 (add-hook 'c-mode-hook
485 (lambda ()
486 (unless (or (file-exists-p \"makefile\")
487 (file-exists-p \"Makefile\"))
488 (set (make-local-variable 'compile-command)
489 (concat \"make -k \"
490 (file-name-sans-extension buffer-file-name))))))"
491 :type 'string
492 :group 'compilation)
493 ;;;###autoload(put 'compile-command 'safe-local-variable 'stringp)
494
495 ;;;###autoload
496 (defcustom compilation-disable-input nil
497 "If non-nil, send end-of-file as compilation process input.
498 This only affects platforms that support asynchronous processes (see
499 `start-process'); synchronous compilation processes never accept input."
500 :type 'boolean
501 :group 'compilation
502 :version "22.1")
503
504 ;; A weak per-compilation-buffer hash indexed by (FILENAME . DIRECTORY). Each
505 ;; value is a FILE-STRUCTURE as described above, with the car eq to the hash
506 ;; key. This holds the tree seen from root, for storing new nodes.
507 (defvar compilation-locs ())
508
509 (defvar compilation-debug nil
510 "*Set this to t before creating a *compilation* buffer.
511 Then every error line will have a debug text property with the matcher that
512 fit this line and the match data. Use `describe-text-properties'.")
513
514 (defvar compilation-exit-message-function nil "\
515 If non-nil, called when a compilation process dies to return a status message.
516 This should be a function of three arguments: process status, exit status,
517 and exit message; it returns a cons (MESSAGE . MODELINE) of the strings to
518 write into the compilation buffer, and to put in its mode line.")
519
520 (defvar compilation-environment nil
521 "*List of environment variables for compilation to inherit.
522 Each element should be a string of the form ENVVARNAME=VALUE.
523 This list is temporarily prepended to `process-environment' prior to
524 starting the compilation process.")
525
526 ;; History of compile commands.
527 (defvar compile-history nil)
528
529 (defface compilation-error
530 '((t :inherit font-lock-warning-face))
531 "Face used to highlight compiler errors."
532 :group 'compilation
533 :version "22.1")
534
535 (defface compilation-warning
536 '((((class color) (min-colors 16)) (:foreground "Orange" :weight bold))
537 (((class color)) (:foreground "cyan" :weight bold))
538 (t (:weight bold)))
539 "Face used to highlight compiler warnings."
540 :group 'compilation
541 :version "22.1")
542
543 (defface compilation-info
544 '((((class color) (min-colors 16) (background light))
545 (:foreground "Green3" :weight bold))
546 (((class color) (min-colors 88) (background dark))
547 (:foreground "Green1" :weight bold))
548 (((class color) (min-colors 16) (background dark))
549 (:foreground "Green" :weight bold))
550 (((class color)) (:foreground "green" :weight bold))
551 (t (:weight bold)))
552 "Face used to highlight compiler information."
553 :group 'compilation
554 :version "22.1")
555
556 (defface compilation-line-number
557 '((t :inherit font-lock-variable-name-face))
558 "Face for displaying line numbers in compiler messages."
559 :group 'compilation
560 :version "22.1")
561
562 (defface compilation-column-number
563 '((t :inherit font-lock-type-face))
564 "Face for displaying column numbers in compiler messages."
565 :group 'compilation
566 :version "22.1")
567
568 (defcustom compilation-message-face 'underline
569 "Face name to use for whole messages.
570 Faces `compilation-error-face', `compilation-warning-face',
571 `compilation-info-face', `compilation-line-face' and
572 `compilation-column-face' get prepended to this, when applicable."
573 :type 'face
574 :group 'compilation
575 :version "22.1")
576
577 (defvar compilation-error-face 'compilation-error
578 "Face name to use for file name in error messages.")
579
580 (defvar compilation-warning-face 'compilation-warning
581 "Face name to use for file name in warning messages.")
582
583 (defvar compilation-info-face 'compilation-info
584 "Face name to use for file name in informational messages.")
585
586 (defvar compilation-line-face 'compilation-line-number
587 "Face name to use for line numbers in compiler messages.")
588
589 (defvar compilation-column-face 'compilation-column-number
590 "Face name to use for column numbers in compiler messages.")
591
592 ;; same faces as dired uses
593 (defvar compilation-enter-directory-face 'font-lock-function-name-face
594 "Face name to use for entering directory messages.")
595
596 (defvar compilation-leave-directory-face 'font-lock-type-face
597 "Face name to use for leaving directory messages.")
598
599
600
601 ;; Used for compatibility with the old compile.el.
602 (defvaralias 'compilation-last-buffer 'next-error-last-buffer)
603 (defvar compilation-parsing-end (make-marker))
604 (defvar compilation-parse-errors-function nil)
605 (defvar compilation-error-list nil)
606 (defvar compilation-old-error-list nil)
607
608 (defcustom compilation-auto-jump-to-first-error nil
609 "If non-nil, automatically jump to the first error after `compile'."
610 :type 'boolean
611 :group 'compilation
612 :version "23.1")
613
614 (defvar compilation-auto-jump-to-next nil
615 "If non-nil, automatically jump to the next error encountered.")
616 (make-variable-buffer-local 'compilation-auto-jump-to-next)
617
618 (defun compilation-face (type)
619 (or (and (car type) (match-end (car type)) compilation-warning-face)
620 (and (cdr type) (match-end (cdr type)) compilation-info-face)
621 compilation-error-face))
622
623 ;; Internal function for calculating the text properties of a directory
624 ;; change message. The directory property is important, because it is
625 ;; the stack of nested enter-messages. Relative filenames on the following
626 ;; lines are relative to the top of the stack.
627 (defun compilation-directory-properties (idx leave)
628 (if leave (setq leave (match-end leave)))
629 ;; find previous stack, and push onto it, or if `leave' pop it
630 (let ((dir (previous-single-property-change (point) 'directory)))
631 (setq dir (if dir (or (get-text-property (1- dir) 'directory)
632 (get-text-property dir 'directory))))
633 `(face ,(if leave
634 compilation-leave-directory-face
635 compilation-enter-directory-face)
636 directory ,(if leave
637 (or (cdr dir)
638 '(nil)) ; nil only isn't a property-change
639 (cons (match-string-no-properties idx) dir))
640 mouse-face highlight
641 keymap compilation-button-map
642 help-echo "mouse-2: visit destination directory")))
643
644 ;; Data type `reverse-ordered-alist' retriever. This function retrieves the
645 ;; KEY element from the ALIST, creating it in the right position if not already
646 ;; present. ALIST structure is
647 ;; '(ANCHOR (KEY1 ...) (KEY2 ...)... (KEYn ALIST ...))
648 ;; ANCHOR is ignored, but necessary so that elements can be inserted. KEY1
649 ;; may be nil. The other KEYs are ordered backwards so that growing line
650 ;; numbers can be inserted in front and searching can abort after half the
651 ;; list on average.
652 (eval-when-compile ;Don't keep it at runtime if not needed.
653 (defmacro compilation-assq (key alist)
654 `(let* ((l1 ,alist)
655 (l2 (cdr l1)))
656 (car (if (if (null ,key)
657 (if l2 (null (caar l2)))
658 (while (if l2 (if (caar l2) (< ,key (caar l2)) t))
659 (setq l1 l2
660 l2 (cdr l1)))
661 (if l2 (eq ,key (caar l2))))
662 l2
663 (setcdr l1 (cons (list ,key) l2)))))))
664
665 (defun compilation-auto-jump (buffer pos)
666 (with-current-buffer buffer
667 (goto-char pos)
668 (compile-goto-error)))
669
670 ;; This function is the central driver, called when font-locking to gather
671 ;; all information needed to later jump to corresponding source code.
672 ;; Return a property list with all meta information on this error location.
673
674 (defun compilation-error-properties (file line end-line col end-col type fmt)
675 (unless (< (next-single-property-change (match-beginning 0)
676 'directory nil (point))
677 (point))
678 (if file
679 (if (functionp file)
680 (setq file (funcall file))
681 (let (dir)
682 (setq file (match-string-no-properties file))
683 (unless (file-name-absolute-p file)
684 (setq dir (previous-single-property-change (point) 'directory)
685 dir (if dir (or (get-text-property (1- dir) 'directory)
686 (get-text-property dir 'directory)))))
687 (setq file (cons file (car dir)))))
688 ;; This message didn't mention one, get it from previous
689 (let ((prev-pos
690 ;; Find the previous message.
691 (previous-single-property-change (point) 'message)))
692 (if prev-pos
693 ;; Get the file structure that belongs to it.
694 (let* ((prev
695 (or (get-text-property (1- prev-pos) 'message)
696 (get-text-property prev-pos 'message)))
697 (prev-struct
698 (car (nth 2 (car prev)))))
699 ;; Construct FILE . DIR from that.
700 (if prev-struct
701 (setq file (cons (car prev-struct)
702 (cadr prev-struct))))))
703 (unless file
704 (setq file '("*unknown*")))))
705 ;; All of these fields are optional, get them only if we have an index, and
706 ;; it matched some part of the message.
707 (and line
708 (setq line (match-string-no-properties line))
709 (setq line (string-to-number line)))
710 (and end-line
711 (setq end-line (match-string-no-properties end-line))
712 (setq end-line (string-to-number end-line)))
713 (if col
714 (if (functionp col)
715 (setq col (funcall col))
716 (and
717 (setq col (match-string-no-properties col))
718 (setq col (- (string-to-number col) compilation-first-column)))))
719 (if (and end-col (functionp end-col))
720 (setq end-col (funcall end-col))
721 (if (and end-col (setq end-col (match-string-no-properties end-col)))
722 (setq end-col (- (string-to-number end-col) compilation-first-column -1))
723 (if end-line (setq end-col -1))))
724 (if (consp type) ; not a static type, check what it is.
725 (setq type (or (and (car type) (match-end (car type)) 1)
726 (and (cdr type) (match-end (cdr type)) 0)
727 2)))
728
729 (when (and compilation-auto-jump-to-next
730 (>= type compilation-skip-threshold))
731 (kill-local-variable 'compilation-auto-jump-to-next)
732 (run-with-timer 0 nil 'compilation-auto-jump
733 (current-buffer) (match-beginning 0)))
734
735 (compilation-internal-error-properties file line end-line col end-col type fmt)))
736
737 (defun compilation-move-to-column (col screen)
738 "Go to column COL on the current line.
739 If SCREEN is non-nil, columns are screen columns, otherwise, they are
740 just char-counts."
741 (if screen
742 (move-to-column col)
743 (goto-char (min (+ (line-beginning-position) col) (line-end-position)))))
744
745 (defun compilation-internal-error-properties (file line end-line col end-col type fmts)
746 "Get the meta-info that will be added as text-properties.
747 LINE, END-LINE, COL, END-COL are integers or nil.
748 TYPE can be 0, 1, or 2, meaning error, warning, or just info.
749 FILE should be (FILENAME) or (RELATIVE-FILENAME . DIRNAME) or nil.
750 FMTS is a list of format specs for transforming the file name.
751 (See `compilation-error-regexp-alist'.)"
752 (unless file (setq file '("*unknown*")))
753 (let* ((file-struct (compilation-get-file-structure file fmts))
754 ;; Get first already existing marker (if any has one, all have one).
755 ;; Do this first, as the compilation-assq`s may create new nodes.
756 (marker-line (car (cddr file-struct))) ; a line structure
757 (marker (nth 3 (cadr marker-line))) ; its marker
758 (compilation-error-screen-columns compilation-error-screen-columns)
759 end-marker loc end-loc)
760 (if (not (and marker (marker-buffer marker)))
761 (setq marker nil) ; no valid marker for this file
762 (setq loc (or line 1)) ; normalize no linenumber to line 1
763 (catch 'marker ; find nearest loc, at least one exists
764 (dolist (x (nthcdr 3 file-struct)) ; loop over remaining lines
765 (if (> (car x) loc) ; still bigger
766 (setq marker-line x)
767 (if (> (- (or (car marker-line) 1) loc)
768 (- loc (car x))) ; current line is nearer
769 (setq marker-line x))
770 (throw 'marker t))))
771 (setq marker (nth 3 (cadr marker-line))
772 marker-line (or (car marker-line) 1))
773 (with-current-buffer (marker-buffer marker)
774 (save-excursion
775 (save-restriction
776 (widen)
777 (goto-char (marker-position marker))
778 (when (or end-col end-line)
779 (beginning-of-line (- (or end-line line) marker-line -1))
780 (if (or (null end-col) (< end-col 0))
781 (end-of-line)
782 (compilation-move-to-column
783 end-col compilation-error-screen-columns))
784 (setq end-marker (list (point-marker))))
785 (beginning-of-line (if end-line
786 (- line end-line -1)
787 (- loc marker-line -1)))
788 (if col
789 (compilation-move-to-column
790 col compilation-error-screen-columns)
791 (forward-to-indentation 0))
792 (setq marker (list (point-marker)))))))
793
794 (setq loc (compilation-assq line (cdr file-struct)))
795 (if end-line
796 (setq end-loc (compilation-assq end-line (cdr file-struct))
797 end-loc (compilation-assq end-col end-loc))
798 (if end-col ; use same line element
799 (setq end-loc (compilation-assq end-col loc))))
800 (setq loc (compilation-assq col loc))
801 ;; If they are new, make the loc(s) reference the file they point to.
802 (or (cdr loc) (setcdr loc `(,line ,file-struct ,@marker)))
803 (if end-loc
804 (or (cdr end-loc)
805 (setcdr end-loc `(,(or end-line line) ,file-struct ,@end-marker))))
806
807 ;; Must start with face
808 `(face ,compilation-message-face
809 message (,loc ,type ,end-loc)
810 ,@(if compilation-debug
811 `(debug (,(assoc (with-no-warnings matcher) font-lock-keywords)
812 ,@(match-data))))
813 help-echo ,(if col
814 "mouse-2: visit this file, line and column"
815 (if line
816 "mouse-2: visit this file and line"
817 "mouse-2: visit this file"))
818 keymap compilation-button-map
819 mouse-face highlight)))
820
821 (defun compilation-mode-font-lock-keywords ()
822 "Return expressions to highlight in Compilation mode."
823 (if compilation-parse-errors-function
824 ;; An old package! Try the compatibility code.
825 '((compilation-compat-parse-errors))
826 (append
827 ;; make directory tracking
828 (if compilation-directory-matcher
829 `((,(car compilation-directory-matcher)
830 ,@(mapcar (lambda (elt)
831 `(,(car elt)
832 (compilation-directory-properties
833 ,(car elt) ,(cdr elt))
834 t t))
835 (cdr compilation-directory-matcher)))))
836
837 ;; Compiler warning/error lines.
838 (mapcar
839 (lambda (item)
840 (if (symbolp item)
841 (setq item (cdr (assq item
842 compilation-error-regexp-alist-alist))))
843 (let ((file (nth 1 item))
844 (line (nth 2 item))
845 (col (nth 3 item))
846 (type (nth 4 item))
847 end-line end-col fmt)
848 (if (consp file) (setq fmt (cdr file) file (car file)))
849 (if (consp line) (setq end-line (cdr line) line (car line)))
850 (if (consp col) (setq end-col (cdr col) col (car col)))
851
852 (if (functionp line)
853 ;; The old compile.el had here an undocumented hook that
854 ;; allowed `line' to be a function that computed the actual
855 ;; error location. Let's do our best.
856 `(,(car item)
857 (0 (save-match-data
858 (compilation-compat-error-properties
859 (funcall ',line (cons (match-string ,file)
860 (cons default-directory
861 ',(nthcdr 4 item)))
862 ,(if col `(match-string ,col))))))
863 (,file compilation-error-face t))
864
865 (unless (or (null (nth 5 item)) (integerp (nth 5 item)))
866 (error "HYPERLINK should be an integer: %s" (nth 5 item)))
867
868 `(,(nth 0 item)
869
870 ,@(when (integerp file)
871 `((,file ,(if (consp type)
872 `(compilation-face ',type)
873 (aref [compilation-info-face
874 compilation-warning-face
875 compilation-error-face]
876 (or type 2))))))
877
878 ,@(when line
879 `((,line compilation-line-face nil t)))
880 ,@(when end-line
881 `((,end-line compilation-line-face nil t)))
882
883 ,@(when (integerp col)
884 `((,col compilation-column-face nil t)))
885 ,@(when (integerp end-col)
886 `((,end-col compilation-column-face nil t)))
887
888 ,@(nthcdr 6 item)
889 (,(or (nth 5 item) 0)
890 (compilation-error-properties ',file ,line ,end-line
891 ,col ,end-col ',(or type 2)
892 ',fmt)
893 append))))) ; for compilation-message-face
894 compilation-error-regexp-alist)
895
896 compilation-mode-font-lock-keywords)))
897
898 \f
899 ;;;###autoload
900 (defun compile (command &optional comint)
901 "Compile the program including the current buffer. Default: run `make'.
902 Runs COMMAND, a shell command, in a separate process asynchronously
903 with output going to the buffer `*compilation*'.
904
905 You can then use the command \\[next-error] to find the next error message
906 and move to the source code that caused it.
907
908 If optional second arg COMINT is t the buffer will be in Comint mode with
909 `compilation-shell-minor-mode'.
910
911 Interactively, prompts for the command if `compilation-read-command' is
912 non-nil; otherwise uses `compile-command'. With prefix arg, always prompts.
913 Additionally, with universal prefix arg, compilation buffer will be in
914 comint mode, i.e. interactive.
915
916 To run more than one compilation at once, start one and rename
917 the \`*compilation*' buffer to some other name with
918 \\[rename-buffer]. Then start the next one. On most systems,
919 termination of the main compilation process kills its
920 subprocesses.
921
922 The name used for the buffer is actually whatever is returned by
923 the function in `compilation-buffer-name-function', so you can set that
924 to a function that generates a unique name."
925 (interactive
926 (list
927 (let ((command (eval compile-command)))
928 (if (or compilation-read-command current-prefix-arg)
929 (read-from-minibuffer "Compile command: "
930 command nil nil
931 (if (equal (car compile-history) command)
932 '(compile-history . 1)
933 'compile-history))
934 command))
935 (consp current-prefix-arg)))
936 (unless (equal command (eval compile-command))
937 (setq compile-command command))
938 (save-some-buffers (not compilation-ask-about-save) nil)
939 (setq-default compilation-directory default-directory)
940 (compilation-start command comint))
941
942 ;; run compile with the default command line
943 (defun recompile ()
944 "Re-compile the program including the current buffer.
945 If this is run in a Compilation mode buffer, re-use the arguments from the
946 original use. Otherwise, recompile using `compile-command'."
947 (interactive)
948 (save-some-buffers (not compilation-ask-about-save) nil)
949 (let ((default-directory (or compilation-directory default-directory)))
950 (apply 'compilation-start (or compilation-arguments
951 `(,(eval compile-command))))))
952
953 (defcustom compilation-scroll-output nil
954 "Non-nil to scroll the *compilation* buffer window as output appears.
955
956 Setting it causes the Compilation mode commands to put point at the
957 end of their output window so that the end of the output is always
958 visible rather than the beginning."
959 :type 'boolean
960 :version "20.3"
961 :group 'compilation)
962
963
964 (defun compilation-buffer-name (mode-name mode-command name-function)
965 "Return the name of a compilation buffer to use.
966 If NAME-FUNCTION is non-nil, call it with one argument MODE-NAME
967 to determine the buffer name.
968 Likewise if `compilation-buffer-name-function' is non-nil.
969 If current buffer is the mode MODE-COMMAND,
970 return the name of the current buffer, so that it gets reused.
971 Otherwise, construct a buffer name from MODE-NAME."
972 (cond (name-function
973 (funcall name-function mode-name))
974 (compilation-buffer-name-function
975 (funcall compilation-buffer-name-function mode-name))
976 ((and (eq mode-command major-mode)
977 (eq major-mode (nth 1 compilation-arguments)))
978 (buffer-name))
979 (t
980 (concat "*" (downcase mode-name) "*"))))
981
982 ;; This is a rough emulation of the old hack, until the transition to new
983 ;; compile is complete.
984 (defun compile-internal (command error-message
985 &optional name-of-mode parser
986 error-regexp-alist name-function
987 enter-regexp-alist leave-regexp-alist
988 file-regexp-alist nomessage-regexp-alist
989 no-async highlight-regexp local-map)
990 (if parser
991 (error "Compile now works very differently, see `compilation-error-regexp-alist'"))
992 (let ((compilation-error-regexp-alist
993 (append file-regexp-alist (or error-regexp-alist
994 compilation-error-regexp-alist)))
995 (compilation-error (replace-regexp-in-string "^No more \\(.+\\)s\\.?"
996 "\\1" error-message)))
997 (compilation-start command nil name-function highlight-regexp)))
998 (make-obsolete 'compile-internal 'compilation-start)
999
1000 ;;;###autoload
1001 (defun compilation-start (command &optional mode name-function highlight-regexp)
1002 "Run compilation command COMMAND (low level interface).
1003 If COMMAND starts with a cd command, that becomes the `default-directory'.
1004 The rest of the arguments are optional; for them, nil means use the default.
1005
1006 MODE is the major mode to set in the compilation buffer. Mode
1007 may also be t meaning use `compilation-shell-minor-mode' under `comint-mode'.
1008 If NAME-FUNCTION is non-nil, call it with one argument (the mode name)
1009 to determine the buffer name.
1010
1011 If HIGHLIGHT-REGEXP is non-nil, `next-error' will temporarily highlight
1012 the matching section of the visited source line; the default is to use the
1013 global value of `compilation-highlight-regexp'.
1014
1015 Returns the compilation buffer created."
1016 (or mode (setq mode 'compilation-mode))
1017 (let* ((name-of-mode
1018 (if (eq mode t)
1019 (prog1 "compilation" (require 'comint))
1020 (replace-regexp-in-string "-mode$" "" (symbol-name mode))))
1021 (thisdir default-directory)
1022 outwin outbuf)
1023 (with-current-buffer
1024 (setq outbuf
1025 (get-buffer-create
1026 (compilation-buffer-name name-of-mode mode name-function)))
1027 (let ((comp-proc (get-buffer-process (current-buffer))))
1028 (if comp-proc
1029 (if (or (not (eq (process-status comp-proc) 'run))
1030 (yes-or-no-p
1031 (format "A %s process is running; kill it? "
1032 name-of-mode)))
1033 (condition-case ()
1034 (progn
1035 (interrupt-process comp-proc)
1036 (sit-for 1)
1037 (delete-process comp-proc))
1038 (error nil))
1039 (error "Cannot have two processes in `%s' at once"
1040 (buffer-name)))))
1041 (buffer-disable-undo (current-buffer))
1042 ;; first transfer directory from where M-x compile was called
1043 (setq default-directory thisdir)
1044 ;; Remember the original dir, so we can use it when we recompile.
1045 ;; default-directory' can't be used reliably for that because it may be
1046 ;; affected by the special handling of "cd ...;".
1047 (set (make-local-variable 'compilation-directory) thisdir)
1048 ;; Make compilation buffer read-only. The filter can still write it.
1049 ;; Clear out the compilation buffer.
1050 (let ((inhibit-read-only t)
1051 (default-directory thisdir))
1052 ;; Then evaluate a cd command if any, but don't perform it yet, else
1053 ;; start-command would do it again through the shell: (cd "..") AND
1054 ;; sh -c "cd ..; make"
1055 (cd (if (string-match "^\\s *cd\\(?:\\s +\\(\\S +?\\)\\)?\\s *[;&\n]" command)
1056 (if (match-end 1)
1057 (substitute-env-vars (match-string 1 command))
1058 "~")
1059 default-directory))
1060 (erase-buffer)
1061 ;; Select the desired mode.
1062 (if (not (eq mode t))
1063 (funcall mode)
1064 (setq buffer-read-only nil)
1065 (with-no-warnings (comint-mode))
1066 (compilation-shell-minor-mode))
1067 (if highlight-regexp
1068 (set (make-local-variable 'compilation-highlight-regexp)
1069 highlight-regexp))
1070 (if compilation-auto-jump-to-first-error
1071 (set (make-local-variable 'compilation-auto-jump-to-next) t))
1072 ;; Output a mode setter, for saving and later reloading this buffer.
1073 (insert "-*- mode: " name-of-mode
1074 "; default-directory: " (prin1-to-string default-directory)
1075 " -*-\n"
1076 (format "%s started at %s\n\n"
1077 mode-name
1078 (substring (current-time-string) 0 19))
1079 command "\n")
1080 (setq thisdir default-directory))
1081 (set-buffer-modified-p nil))
1082 ;; If we're already in the compilation buffer, go to the end
1083 ;; of the buffer, so point will track the compilation output.
1084 (if (eq outbuf (current-buffer))
1085 (goto-char (point-max)))
1086 ;; Pop up the compilation buffer.
1087 (setq outwin (display-buffer outbuf nil t))
1088 (with-current-buffer outbuf
1089 (let ((process-environment
1090 (append
1091 compilation-environment
1092 (if (if (boundp 'system-uses-terminfo) ; `if' for compiler warning
1093 system-uses-terminfo)
1094 (list "TERM=dumb" "TERMCAP="
1095 (format "COLUMNS=%d" (window-width)))
1096 (list "TERM=emacs"
1097 (format "TERMCAP=emacs:co#%d:tc=unknown:"
1098 (window-width))))
1099 ;; Set the EMACS variable, but
1100 ;; don't override users' setting of $EMACS.
1101 (unless (getenv "EMACS")
1102 (list "EMACS=t"))
1103 (list "INSIDE_EMACS=t")
1104 (copy-sequence process-environment)))
1105 (start-process (symbol-function 'start-process)))
1106 (set (make-local-variable 'compilation-arguments)
1107 (list command mode name-function highlight-regexp))
1108 (set (make-local-variable 'revert-buffer-function)
1109 'compilation-revert-buffer)
1110 (set-window-start outwin (point-min))
1111 (or (eq outwin (selected-window))
1112 (set-window-point outwin (if compilation-scroll-output
1113 (point)
1114 (point-min))))
1115 ;; The setup function is called before compilation-set-window-height
1116 ;; so it can set the compilation-window-height buffer locally.
1117 (if compilation-process-setup-function
1118 (funcall compilation-process-setup-function))
1119 (compilation-set-window-height outwin)
1120 ;; Start the compilation.
1121 (let ((proc
1122 (if (eq mode t)
1123 ;; comint uses `start-file-process'.
1124 (get-buffer-process
1125 (with-no-warnings
1126 (comint-exec outbuf (downcase mode-name)
1127 shell-file-name nil `("-c" ,command))))
1128 ;; Redefine temporarily `start-process' in order to
1129 ;; handle remote compilation.
1130 (fset 'start-process
1131 (lambda (name buffer program &rest program-args)
1132 (apply
1133 (if (file-remote-p default-directory)
1134 'start-file-process
1135 start-process)
1136 name buffer program program-args)))
1137 (unwind-protect
1138 (start-process-shell-command (downcase mode-name)
1139 outbuf command)
1140 ;; Unwindform: Reset original definition of `start-process'.
1141 (fset 'start-process start-process)))))
1142 ;; Make the buffer's mode line show process state.
1143 (setq mode-line-process '(":%s"))
1144 (set-process-sentinel proc 'compilation-sentinel)
1145 (set-process-filter proc 'compilation-filter)
1146 (set-marker (process-mark proc) (point) outbuf)
1147 (when compilation-disable-input
1148 (condition-case nil
1149 (process-send-eof proc)
1150 ;; The process may have exited already.
1151 (error nil)))
1152 (setq compilation-in-progress
1153 (cons proc compilation-in-progress))))
1154 ;; Now finally cd to where the shell started make/grep/...
1155 (setq default-directory thisdir))
1156 (if (buffer-local-value 'compilation-scroll-output outbuf)
1157 (save-selected-window
1158 (select-window outwin)
1159 (goto-char (point-max))))
1160 ;; Make it so the next C-x ` will use this buffer.
1161 (setq next-error-last-buffer outbuf)))
1162
1163 (defun compilation-set-window-height (window)
1164 "Set the height of WINDOW according to `compilation-window-height'."
1165 (let ((height (buffer-local-value 'compilation-window-height (window-buffer window))))
1166 (and height
1167 (= (window-width window) (frame-width (window-frame window)))
1168 ;; If window is alone in its frame, aside from a minibuffer,
1169 ;; don't change its height.
1170 (not (eq window (frame-root-window (window-frame window))))
1171 ;; Stef said that doing the saves in this order is safer:
1172 (save-excursion
1173 (save-selected-window
1174 (select-window window)
1175 (enlarge-window (- height (window-height))))))))
1176
1177 (defvar compilation-menu-map
1178 (let ((map (make-sparse-keymap "Errors")))
1179 (define-key map [stop-subjob]
1180 '("Stop Compilation" . kill-compilation))
1181 (define-key map [compilation-mode-separator2]
1182 '("----" . nil))
1183 (define-key map [compilation-first-error]
1184 '("First Error" . first-error))
1185 (define-key map [compilation-previous-error]
1186 '("Previous Error" . previous-error))
1187 (define-key map [compilation-next-error]
1188 '("Next Error" . next-error))
1189 map))
1190
1191 (defvar compilation-minor-mode-map
1192 (let ((map (make-sparse-keymap)))
1193 (define-key map [mouse-2] 'compile-goto-error)
1194 (define-key map [follow-link] 'mouse-face)
1195 (define-key map "\C-c\C-c" 'compile-goto-error)
1196 (define-key map "\C-m" 'compile-goto-error)
1197 (define-key map "\C-c\C-k" 'kill-compilation)
1198 (define-key map "\M-n" 'compilation-next-error)
1199 (define-key map "\M-p" 'compilation-previous-error)
1200 (define-key map "\M-{" 'compilation-previous-file)
1201 (define-key map "\M-}" 'compilation-next-file)
1202 ;; Set up the menu-bar
1203 (define-key map [menu-bar compilation]
1204 (cons "Errors" compilation-menu-map))
1205 map)
1206 "Keymap for `compilation-minor-mode'.")
1207
1208 (defvar compilation-shell-minor-mode-map
1209 (let ((map (make-sparse-keymap)))
1210 (define-key map "\M-\C-m" 'compile-goto-error)
1211 (define-key map "\M-\C-n" 'compilation-next-error)
1212 (define-key map "\M-\C-p" 'compilation-previous-error)
1213 (define-key map "\M-{" 'compilation-previous-file)
1214 (define-key map "\M-}" 'compilation-next-file)
1215 ;; Set up the menu-bar
1216 (define-key map [menu-bar compilation]
1217 (cons "Errors" compilation-menu-map))
1218 map)
1219 "Keymap for `compilation-shell-minor-mode'.")
1220
1221 (defvar compilation-button-map
1222 (let ((map (make-sparse-keymap)))
1223 (define-key map [mouse-2] 'compile-goto-error)
1224 (define-key map [follow-link] 'mouse-face)
1225 (define-key map "\C-m" 'compile-goto-error)
1226 map)
1227 "Keymap for compilation-message buttons.")
1228 (fset 'compilation-button-map compilation-button-map)
1229
1230 (defvar compilation-mode-map
1231 (let ((map (make-sparse-keymap)))
1232 ;; Don't inherit from compilation-minor-mode-map,
1233 ;; because that introduces a menu bar item we don't want.
1234 ;; That confuses C-down-mouse-3.
1235 (define-key map [mouse-2] 'compile-goto-error)
1236 (define-key map [follow-link] 'mouse-face)
1237 (define-key map "\C-c\C-c" 'compile-goto-error)
1238 (define-key map "\C-m" 'compile-goto-error)
1239 (define-key map "\C-c\C-k" 'kill-compilation)
1240 (define-key map "\M-n" 'compilation-next-error)
1241 (define-key map "\M-p" 'compilation-previous-error)
1242 (define-key map "\M-{" 'compilation-previous-file)
1243 (define-key map "\M-}" 'compilation-next-file)
1244 (define-key map "\t" 'compilation-next-error)
1245 (define-key map [backtab] 'compilation-previous-error)
1246
1247 (define-key map " " 'scroll-up)
1248 (define-key map "\^?" 'scroll-down)
1249 (define-key map "\C-c\C-f" 'next-error-follow-minor-mode)
1250
1251 ;; Set up the menu-bar
1252 (let ((submap (make-sparse-keymap "Compile")))
1253 (define-key map [menu-bar compilation]
1254 (cons "Compile" submap))
1255 (set-keymap-parent submap compilation-menu-map))
1256 (define-key map [menu-bar compilation compilation-separator2]
1257 '("----" . nil))
1258 (define-key map [menu-bar compilation compilation-grep]
1259 '("Search Files (grep)..." . grep))
1260 (define-key map [menu-bar compilation compilation-recompile]
1261 '("Recompile" . recompile))
1262 (define-key map [menu-bar compilation compilation-compile]
1263 '("Compile..." . compile))
1264 map)
1265 "Keymap for compilation log buffers.
1266 `compilation-minor-mode-map' is a parent of this.")
1267
1268 (put 'compilation-mode 'mode-class 'special)
1269
1270 (defvar compilation-skip-to-next-location t
1271 "*If non-nil, skip multiple error messages for the same source location.")
1272
1273 (defcustom compilation-skip-threshold 1
1274 "Compilation motion commands skip less important messages.
1275 The value can be either 2 -- skip anything less than error, 1 --
1276 skip anything less than warning or 0 -- don't skip any messages.
1277 Note that all messages not positively identified as warning or
1278 info, are considered errors."
1279 :type '(choice (const :tag "Warnings and info" 2)
1280 (const :tag "Info" 1)
1281 (const :tag "None" 0))
1282 :group 'compilation
1283 :version "22.1")
1284
1285 (defcustom compilation-skip-visited nil
1286 "Compilation motion commands skip visited messages if this is t.
1287 Visited messages are ones for which the file, line and column have been jumped
1288 to from the current content in the current compilation buffer, even if it was
1289 from a different message."
1290 :type 'boolean
1291 :group 'compilation
1292 :version "22.1")
1293
1294 ;;;###autoload
1295 (defun compilation-mode (&optional name-of-mode)
1296 "Major mode for compilation log buffers.
1297 \\<compilation-mode-map>To visit the source for a line-numbered error,
1298 move point to the error message line and type \\[compile-goto-error].
1299 To kill the compilation, type \\[kill-compilation].
1300
1301 Runs `compilation-mode-hook' with `run-mode-hooks' (which see).
1302
1303 \\{compilation-mode-map}"
1304 (interactive)
1305 (kill-all-local-variables)
1306 (use-local-map compilation-mode-map)
1307 (setq major-mode 'compilation-mode
1308 mode-name (or name-of-mode "Compilation"))
1309 (set (make-local-variable 'page-delimiter)
1310 compilation-page-delimiter)
1311 (compilation-setup)
1312 (setq buffer-read-only t)
1313 (run-mode-hooks 'compilation-mode-hook))
1314
1315 (defmacro define-compilation-mode (mode name doc &rest body)
1316 "This is like `define-derived-mode' without the PARENT argument.
1317 The parent is always `compilation-mode' and the customizable `compilation-...'
1318 variables are also set from the name of the mode you have chosen,
1319 by replacing the first word, e.g `compilation-scroll-output' from
1320 `grep-scroll-output' if that variable exists."
1321 (let ((mode-name (replace-regexp-in-string "-mode\\'" "" (symbol-name mode))))
1322 `(define-derived-mode ,mode compilation-mode ,name
1323 ,doc
1324 ,@(mapcar (lambda (v)
1325 (setq v (cons v
1326 (intern-soft (replace-regexp-in-string
1327 "^compilation" mode-name
1328 (symbol-name v)))))
1329 (and (cdr v)
1330 (or (boundp (cdr v))
1331 (if (boundp 'byte-compile-bound-variables)
1332 (memq (cdr v) byte-compile-bound-variables)))
1333 `(set (make-local-variable ',(car v)) ,(cdr v))))
1334 '(compilation-buffer-name-function
1335 compilation-directory-matcher
1336 compilation-error
1337 compilation-error-regexp-alist
1338 compilation-error-regexp-alist-alist
1339 compilation-error-screen-columns
1340 compilation-finish-function
1341 compilation-finish-functions
1342 compilation-first-column
1343 compilation-mode-font-lock-keywords
1344 compilation-page-delimiter
1345 compilation-parse-errors-filename-function
1346 compilation-process-setup-function
1347 compilation-scroll-output
1348 compilation-search-path
1349 compilation-skip-threshold
1350 compilation-window-height))
1351 ,@body)))
1352
1353 (defun compilation-revert-buffer (ignore-auto noconfirm)
1354 (if buffer-file-name
1355 (let (revert-buffer-function)
1356 (revert-buffer ignore-auto noconfirm))
1357 (if (or noconfirm (yes-or-no-p (format "Restart compilation? ")))
1358 (apply 'compilation-start compilation-arguments))))
1359
1360 (defvar compilation-current-error nil
1361 "Marker to the location from where the next error will be found.
1362 The global commands next/previous/first-error/goto-error use this.")
1363
1364 (defvar compilation-messages-start nil
1365 "Buffer position of the beginning of the compilation messages.
1366 If nil, use the beginning of buffer.")
1367
1368 ;; A function name can't be a hook, must be something with a value.
1369 (defconst compilation-turn-on-font-lock 'turn-on-font-lock)
1370
1371 (defun compilation-setup (&optional minor)
1372 "Prepare the buffer for the compilation parsing commands to work.
1373 Optional argument MINOR indicates this is called from
1374 `compilation-minor-mode'."
1375 (make-local-variable 'compilation-current-error)
1376 (make-local-variable 'compilation-messages-start)
1377 (make-local-variable 'compilation-error-screen-columns)
1378 (make-local-variable 'overlay-arrow-position)
1379 (set (make-local-variable 'overlay-arrow-string) "")
1380 (setq next-error-overlay-arrow-position nil)
1381 (add-hook 'kill-buffer-hook
1382 (lambda () (setq next-error-overlay-arrow-position nil)) nil t)
1383 ;; Note that compilation-next-error-function is for interfacing
1384 ;; with the next-error function in simple.el, and it's only
1385 ;; coincidentally named similarly to compilation-next-error.
1386 (setq next-error-function 'compilation-next-error-function)
1387 (set (make-local-variable 'comint-file-name-prefix)
1388 (or (file-remote-p default-directory) ""))
1389 (set (make-local-variable 'font-lock-extra-managed-props)
1390 '(directory message help-echo mouse-face debug))
1391 (set (make-local-variable 'compilation-locs)
1392 (make-hash-table :test 'equal :weakness 'value))
1393 ;; lazy-lock would never find the message unless it's scrolled to.
1394 ;; jit-lock might fontify some things too late.
1395 (set (make-local-variable 'font-lock-support-mode) nil)
1396 (set (make-local-variable 'font-lock-maximum-size) nil)
1397 (if minor
1398 (let ((fld font-lock-defaults))
1399 (font-lock-add-keywords nil (compilation-mode-font-lock-keywords))
1400 (if font-lock-mode
1401 (if fld
1402 (font-lock-fontify-buffer)
1403 (font-lock-change-mode)
1404 (turn-on-font-lock))
1405 (turn-on-font-lock)))
1406 (setq font-lock-defaults '(compilation-mode-font-lock-keywords t))
1407 ;; maybe defer font-lock till after derived mode is set up
1408 (run-mode-hooks 'compilation-turn-on-font-lock)))
1409
1410 ;;;###autoload
1411 (define-minor-mode compilation-shell-minor-mode
1412 "Toggle compilation shell minor mode.
1413 With arg, turn compilation mode on if and only if arg is positive.
1414 In this minor mode, all the error-parsing commands of the
1415 Compilation major mode are available but bound to keys that don't
1416 collide with Shell mode. See `compilation-mode'.
1417 Turning the mode on runs the normal hook `compilation-shell-minor-mode-hook'."
1418 nil " Shell-Compile"
1419 :group 'compilation
1420 (if compilation-shell-minor-mode
1421 (compilation-setup t)
1422 (font-lock-remove-keywords nil (compilation-mode-font-lock-keywords))
1423 (font-lock-fontify-buffer)))
1424
1425 ;;;###autoload
1426 (define-minor-mode compilation-minor-mode
1427 "Toggle compilation minor mode.
1428 With arg, turn compilation mode on if and only if arg is positive.
1429 In this minor mode, all the error-parsing commands of the
1430 Compilation major mode are available. See `compilation-mode'.
1431 Turning the mode on runs the normal hook `compilation-minor-mode-hook'."
1432 nil " Compilation"
1433 :group 'compilation
1434 (if compilation-minor-mode
1435 (compilation-setup t)
1436 (font-lock-remove-keywords nil (compilation-mode-font-lock-keywords))
1437 (font-lock-fontify-buffer)))
1438
1439 (defun compilation-handle-exit (process-status exit-status msg)
1440 "Write MSG in the current buffer and hack its `mode-line-process'."
1441 (let ((inhibit-read-only t)
1442 (status (if compilation-exit-message-function
1443 (funcall compilation-exit-message-function
1444 process-status exit-status msg)
1445 (cons msg exit-status)))
1446 (omax (point-max))
1447 (opoint (point))
1448 (cur-buffer (current-buffer)))
1449 ;; Record where we put the message, so we can ignore it later on.
1450 (goto-char omax)
1451 (insert ?\n mode-name " " (car status))
1452 (if (and (numberp compilation-window-height)
1453 (zerop compilation-window-height))
1454 (message "%s" (cdr status)))
1455 (if (bolp)
1456 (forward-char -1))
1457 (insert " at " (substring (current-time-string) 0 19))
1458 (goto-char (point-max))
1459 ;; Prevent that message from being recognized as a compilation error.
1460 (add-text-properties omax (point)
1461 (append '(compilation-handle-exit t) nil))
1462 (setq mode-line-process (format ":%s [%s]" process-status (cdr status)))
1463 ;; Force mode line redisplay soon.
1464 (force-mode-line-update)
1465 (if (and opoint (< opoint omax))
1466 (goto-char opoint))
1467 (with-no-warnings
1468 (if compilation-finish-function
1469 (funcall compilation-finish-function cur-buffer msg)))
1470 (run-hook-with-args 'compilation-finish-functions cur-buffer msg)))
1471
1472 ;; Called when compilation process changes state.
1473 (defun compilation-sentinel (proc msg)
1474 "Sentinel for compilation buffers."
1475 (if (memq (process-status proc) '(exit signal))
1476 (let ((buffer (process-buffer proc)))
1477 (if (null (buffer-name buffer))
1478 ;; buffer killed
1479 (set-process-buffer proc nil)
1480 (with-current-buffer buffer
1481 ;; Write something in the compilation buffer
1482 ;; and hack its mode line.
1483 (compilation-handle-exit (process-status proc)
1484 (process-exit-status proc)
1485 msg)
1486 ;; Since the buffer and mode line will show that the
1487 ;; process is dead, we can delete it now. Otherwise it
1488 ;; will stay around until M-x list-processes.
1489 (delete-process proc)))
1490 (setq compilation-in-progress (delq proc compilation-in-progress)))))
1491
1492 (defun compilation-filter (proc string)
1493 "Process filter for compilation buffers.
1494 Just inserts the text, but uses `insert-before-markers'."
1495 (if (buffer-name (process-buffer proc))
1496 (with-current-buffer (process-buffer proc)
1497 (let ((inhibit-read-only t))
1498 (save-excursion
1499 (goto-char (process-mark proc))
1500 (insert-before-markers string)
1501 (run-hooks 'compilation-filter-hook))))))
1502
1503 ;;; test if a buffer is a compilation buffer, assuming we're in the buffer
1504 (defsubst compilation-buffer-internal-p ()
1505 "Test if inside a compilation buffer."
1506 (local-variable-p 'compilation-locs))
1507
1508 ;;; test if a buffer is a compilation buffer, using compilation-buffer-internal-p
1509 (defsubst compilation-buffer-p (buffer)
1510 "Test if BUFFER is a compilation buffer."
1511 (with-current-buffer buffer
1512 (compilation-buffer-internal-p)))
1513
1514 (defmacro compilation-loop (< property-change 1+ error limit)
1515 `(let (opt)
1516 (while (,< n 0)
1517 (setq opt pt)
1518 (or (setq pt (,property-change pt 'message))
1519 ;; Handle the case where where the first error message is
1520 ;; at the start of the buffer, and n < 0.
1521 (if (or (eq (get-text-property ,limit 'message)
1522 (get-text-property opt 'message))
1523 (eq pt opt))
1524 (error ,error compilation-error)
1525 (setq pt ,limit)))
1526 ;; prop 'message usually has 2 changes, on and off, so
1527 ;; re-search if off
1528 (or (setq msg (get-text-property pt 'message))
1529 (if (setq pt (,property-change pt 'message nil ,limit))
1530 (setq msg (get-text-property pt 'message)))
1531 (error ,error compilation-error))
1532 (or (< (cadr msg) compilation-skip-threshold)
1533 (if different-file
1534 (eq (prog1 last (setq last (nth 2 (car msg))))
1535 last))
1536 (if compilation-skip-visited
1537 (nthcdr 5 (car msg)))
1538 (if compilation-skip-to-next-location
1539 (eq (car msg) loc))
1540 ;; count this message only if none of the above are true
1541 (setq n (,1+ n))))))
1542
1543 (defun compilation-next-error (n &optional different-file pt)
1544 "Move point to the next error in the compilation buffer.
1545 Prefix arg N says how many error messages to move forwards (or
1546 backwards, if negative).
1547 Does NOT find the source line like \\[next-error]."
1548 (interactive "p")
1549 (or (compilation-buffer-p (current-buffer))
1550 (error "Not in a compilation buffer"))
1551 (or pt (setq pt (point)))
1552 (let* ((msg (get-text-property pt 'message))
1553 (loc (car msg))
1554 last)
1555 (if (zerop n)
1556 (unless (or msg ; find message near here
1557 (setq msg (get-text-property (max (1- pt) (point-min))
1558 'message)))
1559 (setq pt (previous-single-property-change pt 'message nil
1560 (line-beginning-position)))
1561 (unless (setq msg (get-text-property (max (1- pt) (point-min)) 'message))
1562 (setq pt (next-single-property-change pt 'message nil
1563 (line-end-position)))
1564 (or (setq msg (get-text-property pt 'message))
1565 (setq pt (point)))))
1566 (setq last (nth 2 (car msg)))
1567 (if (>= n 0)
1568 (compilation-loop > next-single-property-change 1-
1569 (if (get-buffer-process (current-buffer))
1570 "No more %ss yet"
1571 "Moved past last %s")
1572 (point-max))
1573 ;; Don't move "back" to message at or before point.
1574 ;; Pass an explicit (point-min) to make sure pt is non-nil.
1575 (setq pt (previous-single-property-change pt 'message nil (point-min)))
1576 (compilation-loop < previous-single-property-change 1+
1577 "Moved back before first %s" (point-min))))
1578 (goto-char pt)
1579 (or msg
1580 (error "No %s here" compilation-error))))
1581
1582 (defun compilation-previous-error (n)
1583 "Move point to the previous error in the compilation buffer.
1584 Prefix arg N says how many error messages to move backwards (or
1585 forwards, if negative).
1586 Does NOT find the source line like \\[previous-error]."
1587 (interactive "p")
1588 (compilation-next-error (- n)))
1589
1590 (defun compilation-next-file (n)
1591 "Move point to the next error for a different file than the current one.
1592 Prefix arg N says how many files to move forwards (or backwards, if negative)."
1593 (interactive "p")
1594 (compilation-next-error n t))
1595
1596 (defun compilation-previous-file (n)
1597 "Move point to the previous error for a different file than the current one.
1598 Prefix arg N says how many files to move backwards (or forwards, if negative)."
1599 (interactive "p")
1600 (compilation-next-file (- n)))
1601
1602 (defun kill-compilation ()
1603 "Kill the process made by the \\[compile] or \\[grep] commands."
1604 (interactive)
1605 (let ((buffer (compilation-find-buffer)))
1606 (if (get-buffer-process buffer)
1607 (interrupt-process (get-buffer-process buffer))
1608 (error "The %s process is not running" (downcase mode-name)))))
1609
1610 (defalias 'compile-mouse-goto-error 'compile-goto-error)
1611
1612 (defun compile-goto-error (&optional event)
1613 "Visit the source for the error message at point.
1614 Use this command in a compilation log buffer. Sets the mark at point there."
1615 (interactive (list last-input-event))
1616 (if event (posn-set-point (event-end event)))
1617 (or (compilation-buffer-p (current-buffer))
1618 (error "Not in a compilation buffer"))
1619 (if (get-text-property (point) 'directory)
1620 (dired-other-window (car (get-text-property (point) 'directory)))
1621 (push-mark)
1622 (setq compilation-current-error (point))
1623 (next-error-internal)))
1624
1625 ;; Return a compilation buffer.
1626 ;; If the current buffer is a compilation buffer, return it.
1627 ;; Otherwise, look for a compilation buffer and signal an error
1628 ;; if there are none.
1629 (defun compilation-find-buffer (&optional avoid-current)
1630 (next-error-find-buffer avoid-current 'compilation-buffer-internal-p))
1631
1632 ;;;###autoload
1633 (defun compilation-next-error-function (n &optional reset)
1634 "Advance to the next error message and visit the file where the error was.
1635 This is the value of `next-error-function' in Compilation buffers."
1636 (interactive "p")
1637 (when reset
1638 (setq compilation-current-error nil))
1639 (let* ((columns compilation-error-screen-columns) ; buffer's local value
1640 (last 1) timestamp
1641 (loc (compilation-next-error (or n 1) nil
1642 (or compilation-current-error
1643 compilation-messages-start
1644 (point-min))))
1645 (end-loc (nth 2 loc))
1646 (marker (point-marker)))
1647 (setq compilation-current-error (point-marker)
1648 overlay-arrow-position
1649 (if (bolp)
1650 compilation-current-error
1651 (copy-marker (line-beginning-position)))
1652 loc (car loc))
1653 ;; If loc contains no marker, no error in that file has been visited.
1654 ;; If the marker is invalid the buffer has been killed.
1655 ;; If the file is newer than the timestamp, it has been modified
1656 ;; (`omake -P' polls filesystem for changes and recompiles when needed
1657 ;; in the same process and buffer).
1658 ;; So, recalculate all markers for that file.
1659 (unless (and (nth 3 loc) (marker-buffer (nth 3 loc))
1660 (equal (nth 4 loc)
1661 (setq timestamp
1662 (with-current-buffer (marker-buffer (nth 3 loc))
1663 (visited-file-modtime)))))
1664 (with-current-buffer (compilation-find-file marker (caar (nth 2 loc))
1665 (cadr (car (nth 2 loc))))
1666 (save-restriction
1667 (widen)
1668 (goto-char (point-min))
1669 ;; Treat file's found lines in forward order, 1 by 1.
1670 (dolist (line (reverse (cddr (nth 2 loc))))
1671 (when (car line) ; else this is a filename w/o a line#
1672 (beginning-of-line (- (car line) last -1))
1673 (setq last (car line)))
1674 ;; Treat line's found columns and store/update a marker for each.
1675 (dolist (col (cdr line))
1676 (if (car col)
1677 (if (eq (car col) -1) ; special case for range end
1678 (end-of-line)
1679 (compilation-move-to-column (car col) columns))
1680 (beginning-of-line)
1681 (skip-chars-forward " \t"))
1682 (if (nth 3 col)
1683 (set-marker (nth 3 col) (point))
1684 (setcdr (nthcdr 2 col) `(,(point-marker)))))))))
1685 (compilation-goto-locus marker (nth 3 loc) (nth 3 end-loc))
1686 (setcdr (nthcdr 3 loc) (list timestamp))
1687 (setcdr (nthcdr 4 loc) t))) ; Set this one as visited.
1688
1689 (defvar compilation-gcpro nil
1690 "Internal variable used to keep some values from being GC'd.")
1691 (make-variable-buffer-local 'compilation-gcpro)
1692
1693 (defun compilation-fake-loc (marker file &optional line col)
1694 "Preassociate MARKER with FILE.
1695 FILE should be ABSOLUTE-FILENAME or (RELATIVE-FILENAME . DIRNAME).
1696 This is useful when you compile temporary files, but want
1697 automatic translation of the messages to the real buffer from
1698 which the temporary file came. This only works if done before a
1699 message about FILE appears!
1700
1701 Optional args LINE and COL default to 1 and beginning of
1702 indentation respectively. The marker is expected to reflect
1703 this. In the simplest case the marker points to the first line
1704 of the region that was saved to the temp file.
1705
1706 If you concatenate several regions into the temp file (e.g. a
1707 header with variable assignments and a code region), you must
1708 call this several times, once each for the last line of one
1709 region and the first line of the next region."
1710 (or (consp file) (setq file (list file)))
1711 (setq file (compilation-get-file-structure file))
1712 ;; Between the current call to compilation-fake-loc and the first occurrence
1713 ;; of an error message referring to `file', the data is only kept is the
1714 ;; weak hash-table compilation-locs, so we need to prevent this entry
1715 ;; in compilation-locs from being GC'd away. --Stef
1716 (push file compilation-gcpro)
1717 (let ((loc (compilation-assq (or line 1) (cdr file))))
1718 (setq loc (compilation-assq col loc))
1719 (if (cdr loc)
1720 (setcdr (cddr loc) (list marker))
1721 (setcdr loc (list line file marker)))
1722 loc))
1723
1724 (defcustom compilation-context-lines nil
1725 "Display this many lines of leading context before the current message.
1726 If nil and the left fringe is displayed, don't scroll the
1727 compilation output window; an arrow in the left fringe points to
1728 the current message. If nil and there is no left fringe, the message
1729 displays at the top of the window; there is no arrow."
1730 :type '(choice integer (const :tag "No window scrolling" nil))
1731 :group 'compilation
1732 :version "22.1")
1733
1734 (defsubst compilation-set-window (w mk)
1735 "Align the compilation output window W with marker MK near top."
1736 (if (integerp compilation-context-lines)
1737 (set-window-start w (save-excursion
1738 (goto-char mk)
1739 (beginning-of-line
1740 (- 1 compilation-context-lines))
1741 (point)))
1742 ;; If there is no left fringe.
1743 (if (equal (car (window-fringes)) 0)
1744 (set-window-start w (save-excursion
1745 (goto-char mk)
1746 (beginning-of-line 1)
1747 (point)))))
1748 (set-window-point w mk))
1749
1750 (defvar next-error-highlight-timer)
1751
1752 (defun compilation-goto-locus (msg mk end-mk)
1753 "Jump to an error corresponding to MSG at MK.
1754 All arguments are markers. If END-MK is non-nil, mark is set there
1755 and overlay is highlighted between MK and END-MK."
1756 ;; Show compilation buffer in other window, scrolled to this error.
1757 (let* ((from-compilation-buffer (eq (window-buffer (selected-window))
1758 (marker-buffer msg)))
1759 ;; Use an existing window if it is in a visible frame.
1760 (pre-existing (get-buffer-window (marker-buffer msg) 0))
1761 (w (if (and from-compilation-buffer pre-existing)
1762 ;; Calling display-buffer here may end up (partly) hiding
1763 ;; the error location if the two buffers are in two
1764 ;; different frames. So don't do it if it's not necessary.
1765 pre-existing
1766 (let ((display-buffer-reuse-frames t)
1767 (pop-up-windows t))
1768 ;; Pop up a window.
1769 (display-buffer (marker-buffer msg)))))
1770 (highlight-regexp (with-current-buffer (marker-buffer msg)
1771 ;; also do this while we change buffer
1772 (compilation-set-window w msg)
1773 compilation-highlight-regexp)))
1774 ;; Ideally, the window-size should be passed to `display-buffer' (via
1775 ;; something like special-display-buffer) so it's only used when
1776 ;; creating a new window.
1777 (unless pre-existing (compilation-set-window-height w))
1778
1779 (if from-compilation-buffer
1780 ;; If the compilation buffer window was selected,
1781 ;; keep the compilation buffer in this window;
1782 ;; display the source in another window.
1783 (let ((pop-up-windows t))
1784 (pop-to-buffer (marker-buffer mk) 'other-window))
1785 (if (window-dedicated-p (selected-window))
1786 (pop-to-buffer (marker-buffer mk))
1787 (switch-to-buffer (marker-buffer mk))))
1788 ;; If narrowing gets in the way of going to the right place, widen.
1789 (unless (eq (goto-char mk) (point))
1790 (widen)
1791 (goto-char mk))
1792 (if end-mk
1793 (push-mark end-mk t)
1794 (if mark-active (setq mark-active)))
1795 ;; If hideshow got in the way of
1796 ;; seeing the right place, open permanently.
1797 (dolist (ov (overlays-at (point)))
1798 (when (eq 'hs (overlay-get ov 'invisible))
1799 (delete-overlay ov)
1800 (goto-char mk)))
1801
1802 (when highlight-regexp
1803 (if (timerp next-error-highlight-timer)
1804 (cancel-timer next-error-highlight-timer))
1805 (unless compilation-highlight-overlay
1806 (setq compilation-highlight-overlay
1807 (make-overlay (point-min) (point-min)))
1808 (overlay-put compilation-highlight-overlay 'face 'next-error))
1809 (with-current-buffer (marker-buffer mk)
1810 (save-excursion
1811 (if end-mk (goto-char end-mk) (end-of-line))
1812 (let ((end (point)))
1813 (if mk (goto-char mk) (beginning-of-line))
1814 (if (and (stringp highlight-regexp)
1815 (re-search-forward highlight-regexp end t))
1816 (progn
1817 (goto-char (match-beginning 0))
1818 (move-overlay compilation-highlight-overlay
1819 (match-beginning 0) (match-end 0)
1820 (current-buffer)))
1821 (move-overlay compilation-highlight-overlay
1822 (point) end (current-buffer)))
1823 (if (or (eq next-error-highlight t)
1824 (numberp next-error-highlight))
1825 ;; We want highlighting: delete overlay on next input.
1826 (add-hook 'pre-command-hook
1827 'compilation-goto-locus-delete-o)
1828 ;; We don't want highlighting: delete overlay now.
1829 (delete-overlay compilation-highlight-overlay))
1830 ;; We want highlighting for a limited time:
1831 ;; set up a timer to delete it.
1832 (when (numberp next-error-highlight)
1833 (setq next-error-highlight-timer
1834 (run-at-time next-error-highlight nil
1835 'compilation-goto-locus-delete-o)))))))
1836 (when (and (eq next-error-highlight 'fringe-arrow))
1837 ;; We want a fringe arrow (instead of highlighting).
1838 (setq next-error-overlay-arrow-position
1839 (copy-marker (line-beginning-position))))))
1840
1841 (defun compilation-goto-locus-delete-o ()
1842 (delete-overlay compilation-highlight-overlay)
1843 ;; Get rid of timer and hook that would try to do this again.
1844 (if (timerp next-error-highlight-timer)
1845 (cancel-timer next-error-highlight-timer))
1846 (remove-hook 'pre-command-hook
1847 'compilation-goto-locus-delete-o))
1848 \f
1849 (defun compilation-find-file (marker filename directory &rest formats)
1850 "Find a buffer for file FILENAME.
1851 Search the directories in `compilation-search-path'.
1852 A nil in `compilation-search-path' means to try the
1853 \"current\" directory, which is passed in DIRECTORY.
1854 If DIRECTORY is relative, it is combined with `default-directory'.
1855 If DIRECTORY is nil, that means use `default-directory'.
1856 If FILENAME is not found at all, ask the user where to find it.
1857 Pop up the buffer containing MARKER and scroll to MARKER if we ask the user."
1858 (or formats (setq formats '("%s")))
1859 (let ((dirs compilation-search-path)
1860 (spec-dir (if directory
1861 (expand-file-name directory)
1862 default-directory))
1863 buffer thisdir fmts name)
1864 (if (file-name-absolute-p filename)
1865 ;; The file name is absolute. Use its explicit directory as
1866 ;; the first in the search path, and strip it from FILENAME.
1867 (setq filename (abbreviate-file-name (expand-file-name filename))
1868 dirs (cons (file-name-directory filename) dirs)
1869 filename (file-name-nondirectory filename)))
1870 ;; Now search the path.
1871 (while (and dirs (null buffer))
1872 (setq thisdir (or (car dirs) spec-dir)
1873 fmts formats)
1874 ;; For each directory, try each format string.
1875 (while (and fmts (null buffer))
1876 (setq name (expand-file-name (format (car fmts) filename) thisdir)
1877 buffer (and (file-exists-p name)
1878 (find-file-noselect name))
1879 fmts (cdr fmts)))
1880 (setq dirs (cdr dirs)))
1881 (while (null buffer) ;Repeat until the user selects an existing file.
1882 ;; The file doesn't exist. Ask the user where to find it.
1883 (save-excursion ;This save-excursion is probably not right.
1884 (let ((pop-up-windows t))
1885 (compilation-set-window (display-buffer (marker-buffer marker))
1886 marker)
1887 (let* ((name (read-file-name
1888 (format "Find this %s in (default %s): "
1889 compilation-error filename)
1890 spec-dir filename t nil
1891 ;; The predicate below is fine when called from
1892 ;; minibuffer-complete-and-exit, but it's too
1893 ;; restrictive otherwise, since it also prevents the
1894 ;; user from completing "fo" to "foo/" when she
1895 ;; wants to enter "foo/bar".
1896 ;;
1897 ;; Try to make sure the user can only select
1898 ;; a valid answer. This predicate may be ignored,
1899 ;; tho, so we still have to double-check afterwards.
1900 ;; TODO: We should probably fix read-file-name so
1901 ;; that it never ignores this predicate, even when
1902 ;; using popup dialog boxes.
1903 ;; (lambda (name)
1904 ;; (if (file-directory-p name)
1905 ;; (setq name (expand-file-name filename name)))
1906 ;; (file-exists-p name))
1907 ))
1908 (origname name))
1909 (cond
1910 ((not (file-exists-p name))
1911 (message "Cannot find file `%s'" name)
1912 (ding) (sit-for 2))
1913 ((and (file-directory-p name)
1914 (not (file-exists-p
1915 (setq name (expand-file-name filename name)))))
1916 (message "No `%s' in directory %s" filename origname)
1917 (ding) (sit-for 2))
1918 (t
1919 (setq buffer (find-file-noselect name))))))))
1920 ;; Make intangible overlays tangible.
1921 ;; This is weird: it's not even clear which is the current buffer,
1922 ;; so the code below can't be expected to DTRT here. -- Stef
1923 (dolist (ov (overlays-in (point-min) (point-max)))
1924 (when (overlay-get ov 'intangible)
1925 (overlay-put ov 'intangible nil)))
1926 buffer))
1927
1928 (defun compilation-get-file-structure (file &optional fmt)
1929 "Retrieve FILE's file-structure or create a new one.
1930 FILE should be (FILENAME) or (RELATIVE-FILENAME . DIRNAME).
1931 In the former case, FILENAME may be relative or absolute.
1932
1933 The file-structure looks like this:
1934 (list (list FILENAME [DIR-FROM-PREV-MSG]) FMT LINE-STRUCT...)
1935 "
1936 (or (gethash file compilation-locs)
1937 ;; File was not previously encountered, at least not in the form passed.
1938 ;; Let's normalize it and look again.
1939 (let ((filename (car file))
1940 ;; Get the specified directory from FILE.
1941 (spec-directory (if (cdr file)
1942 (file-truename (cdr file)))))
1943
1944 ;; Check for a comint-file-name-prefix and prepend it if appropriate.
1945 ;; (This is very useful for compilation-minor-mode in an rlogin-mode
1946 ;; buffer.)
1947 (when (and (boundp 'comint-file-name-prefix)
1948 (not (equal comint-file-name-prefix "")))
1949 (if (file-name-absolute-p filename)
1950 (setq filename
1951 (concat comint-file-name-prefix filename))
1952 (if spec-directory
1953 (setq spec-directory
1954 (file-truename
1955 (concat comint-file-name-prefix spec-directory))))))
1956
1957 ;; If compilation-parse-errors-filename-function is
1958 ;; defined, use it to process the filename.
1959 (when compilation-parse-errors-filename-function
1960 (setq filename
1961 (funcall compilation-parse-errors-filename-function
1962 filename)))
1963
1964 ;; Some compilers (e.g. Sun's java compiler, reportedly) produce bogus
1965 ;; file names like "./bar//foo.c" for file "bar/foo.c";
1966 ;; expand-file-name will collapse these into "/foo.c" and fail to find
1967 ;; the appropriate file. So we look for doubled slashes in the file
1968 ;; name and fix them.
1969 (setq filename (command-line-normalize-file-name filename))
1970
1971 ;; Store it for the possibly unnormalized name
1972 (puthash file
1973 ;; Retrieve or create file-structure for normalized name
1974 (or (gethash (list filename) compilation-locs)
1975 (puthash (list filename)
1976 (list (list filename spec-directory) fmt)
1977 compilation-locs))
1978 compilation-locs))))
1979
1980 (add-to-list 'debug-ignored-errors "^No more [-a-z ]+s yet$")
1981
1982 ;;; Compatibility with the old compile.el.
1983
1984 (defun compile-buffer-substring (n) (if n (match-string n)))
1985
1986 (defun compilation-compat-error-properties (err)
1987 "Map old-style error ERR to new-style message."
1988 ;; Old-style structure is (MARKER (FILE DIR) LINE COL) or
1989 ;; (MARKER . MARKER).
1990 (let ((dst (cdr err)))
1991 (if (markerp dst)
1992 ;; Must start with a face, for font-lock.
1993 `(face nil
1994 message ,(list (list nil nil nil dst) 2)
1995 help-echo "mouse-2: visit the source location"
1996 keymap compilation-button-map
1997 mouse-face highlight)
1998 ;; Too difficult to do it by hand: dispatch to the normal code.
1999 (let* ((file (pop dst))
2000 (line (pop dst))
2001 (col (pop dst))
2002 (filename (pop file))
2003 (dirname (pop file))
2004 (fmt (pop file)))
2005 (compilation-internal-error-properties
2006 (cons filename dirname) line nil col nil 2 fmt)))))
2007
2008 (defun compilation-compat-parse-errors (limit)
2009 (when compilation-parse-errors-function
2010 ;; FIXME: We should remove the rest of the compilation keywords
2011 ;; but we can't do that from here because font-lock is using
2012 ;; the value right now. --stef
2013 (save-excursion
2014 (setq compilation-error-list nil)
2015 ;; Reset compilation-parsing-end each time because font-lock
2016 ;; might force us the re-parse many times (typically because
2017 ;; some code adds some text-property to the output that we
2018 ;; already parsed). You might say "why reparse", well:
2019 ;; because font-lock has just removed the `message' property so
2020 ;; have to do it all over again.
2021 (if compilation-parsing-end
2022 (set-marker compilation-parsing-end (point))
2023 (setq compilation-parsing-end (point-marker)))
2024 (condition-case nil
2025 ;; Ignore any error: we're calling this function earlier than
2026 ;; in the old compile.el so things might not all be setup yet.
2027 (funcall compilation-parse-errors-function limit nil)
2028 (error nil))
2029 (dolist (err (if (listp compilation-error-list) compilation-error-list))
2030 (let* ((src (car err))
2031 (dst (cdr err))
2032 (loc (cond ((markerp dst) (list nil nil nil dst))
2033 ((consp dst)
2034 (list (nth 2 dst) (nth 1 dst)
2035 (cons (cdar dst) (caar dst)))))))
2036 (when loc
2037 (goto-char src)
2038 ;; (put-text-property src (line-end-position) 'font-lock-face 'font-lock-warning-face)
2039 (put-text-property src (line-end-position)
2040 'message (list loc 2)))))))
2041 (goto-char limit)
2042 nil)
2043
2044 ;; Beware: this is not only compatiblity code. New code stil uses it. --Stef
2045 (defun compilation-forget-errors ()
2046 ;; In case we hit the same file/line specs, we want to recompute a new
2047 ;; marker for them, so flush our cache.
2048 (setq compilation-locs (make-hash-table :test 'equal :weakness 'value))
2049 (setq compilation-gcpro nil)
2050 ;; FIXME: the old code reset the directory-stack, so maybe we should
2051 ;; put a `directory change' marker of some sort, but where? -stef
2052 ;;
2053 ;; FIXME: The old code moved compilation-current-error (which was
2054 ;; virtually represented by a mix of compilation-parsing-end and
2055 ;; compilation-error-list) to point-min, but that was only meaningful for
2056 ;; the internal uses of compilation-forget-errors: all calls from external
2057 ;; packages seem to be followed by a move of compilation-parsing-end to
2058 ;; something equivalent to point-max. So we speculatively move
2059 ;; compilation-current-error to point-max (since the external package
2060 ;; won't know that it should do it). --stef
2061 (setq compilation-current-error nil)
2062 (let* ((proc (get-buffer-process (current-buffer)))
2063 (mark (if proc (process-mark proc)))
2064 (pos (or mark (point-max))))
2065 (setq compilation-messages-start
2066 ;; In the future, ignore the text already present in the buffer.
2067 ;; Since many process filter functions insert before markers,
2068 ;; we need to put ours just before the insertion point rather
2069 ;; than at the insertion point. If that's not possible, then
2070 ;; don't use a marker. --Stef
2071 (if (> pos (point-min)) (copy-marker (1- pos)) pos))))
2072
2073 ;;;###autoload
2074 (add-to-list 'auto-mode-alist '("\\.gcov\\'" . compilation-mode))
2075
2076 (provide 'compile)
2077
2078 ;; arch-tag: 12465727-7382-4f72-b234-79855a00dd8c
2079 ;;; compile.el ends here