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