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