(compilation-error-screen-columns): New
[bpt/emacs.git] / lisp / progmodes / compile.el
1 ;;; compile.el --- run compiler as inferior of Emacs, parse error messages.
2
3 ;; Copyright (C) 1985, 86, 87, 93, 94, 95, 96, 97, 1998 Free Software Foundation, Inc.
4
5 ;; Author: Roland McGrath <roland@prep.ai.mit.edu>
6 ;; Maintainer: FSF
7 ;; Keywords: tools, processes
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;; This package provides the compile and grep facilities documented in
29 ;; the Emacs user's manual.
30
31 ;;; Code:
32
33 (defgroup compilation nil
34 "Run compiler as inferior of Emacs, parse error messages."
35 :group 'tools
36 :group 'processes)
37
38
39 ;;;###autoload
40 (defcustom compilation-mode-hook nil
41 "*List of hook functions run by `compilation-mode' (see `run-hooks')."
42 :type 'hook
43 :group 'compilation)
44
45 ;;;###autoload
46 (defcustom compilation-window-height nil
47 "*Number of lines in a compilation window. If nil, use Emacs default."
48 :type '(choice (const :tag "Default" nil)
49 integer)
50 :group 'compilation)
51
52 (defvar compile-auto-highlight nil
53 "*Specify how many compiler errors to highlight (and parse) initially.
54 \(Highlighting applies to an error message when the mouse is over it.)
55 If this is a number N, all compiler error messages in the first N lines
56 are highlighted and parsed as soon as they arrive in Emacs.
57 If t, highlight and parse the whole compilation output as soon as it arrives.
58 If nil, don't highlight or parse any of the buffer until you try to
59 move to the error messages.
60
61 Those messages which are not parsed and highlighted initially
62 will be parsed and highlighted as soon as you try to move to them.")
63
64 (defvar compilation-error-list nil
65 "List of error message descriptors for visiting erring functions.
66 Each error descriptor is a cons (or nil). Its car is a marker pointing to
67 an error message. If its cdr is a marker, it points to the text of the
68 line the message is about. If its cdr is a cons, it is a list
69 \(\(DIRECTORY . FILE\) LINE [COLUMN]\). Or its cdr may be nil if that
70 error is not interesting.
71
72 The value may be t instead of a list; this means that the buffer of
73 error messages should be reparsed the next time the list of errors is wanted.
74
75 Some other commands (like `diff') use this list to control the error
76 message tracking facilities; if you change its structure, you should make
77 sure you also change those packages. Perhaps it is better not to change
78 it at all.")
79
80 (defvar compilation-old-error-list nil
81 "Value of `compilation-error-list' after errors were parsed.")
82
83 (defvar compilation-parse-errors-function 'compilation-parse-errors
84 "Function to call to parse error messages from a compilation.
85 It takes args LIMIT-SEARCH and FIND-AT-LEAST.
86 If LIMIT-SEARCH is non-nil, don't bother parsing past that location.
87 If FIND-AT-LEAST is non-nil, don't bother parsing after finding that
88 many new errors.
89 It should read in the source files which have errors and set
90 `compilation-error-list' to a list with an element for each error message
91 found. See that variable for more info.")
92
93 ;;;###autoload
94 (defvar compilation-process-setup-function nil
95 "*Function to call to customize the compilation process.
96 This functions is called immediately before the compilation process is
97 started. It can be used to set any variables or functions that are used
98 while processing the output of the compilation process.")
99
100 ;;;###autoload
101 (defvar compilation-buffer-name-function nil
102 "Function to compute the name of a compilation buffer.
103 The function receives one argument, the name of the major mode of the
104 compilation buffer. It should return a string.
105 nil means compute the name with `(concat \"*\" (downcase major-mode) \"*\")'.")
106
107 ;;;###autoload
108 (defvar compilation-finish-function nil
109 "Function to call when a compilation process finishes.
110 It is called with two arguments: the compilation buffer, and a string
111 describing how the process finished.")
112
113 ;;;###autoload
114 (defvar compilation-finish-functions nil
115 "Functions to call when a compilation process finishes.
116 Each function is called with two arguments: the compilation buffer,
117 and a string describing how the process finished.")
118
119 (defvar compilation-last-buffer nil
120 "The most recent compilation buffer.
121 A buffer becomes most recent when its compilation is started
122 or when it is used with \\[next-error] or \\[compile-goto-error].")
123
124 (defvar compilation-in-progress nil
125 "List of compilation processes now running.")
126 (or (assq 'compilation-in-progress minor-mode-alist)
127 (setq minor-mode-alist (cons '(compilation-in-progress " Compiling")
128 minor-mode-alist)))
129
130 (defvar compilation-parsing-end nil
131 "Position of end of buffer when last error messages were parsed.")
132
133 (defvar compilation-error-message "No more errors"
134 "Message to print when no more matches are found.")
135
136 (defvar compilation-arguments nil
137 "Arguments that were given to `compile-internal'.")
138
139 (defvar compilation-num-errors-found)
140
141 (defvar compilation-error-regexp-alist
142 '(
143 ;; NOTE! See also grep-regexp-alist, below.
144
145 ;; 4.3BSD grep, cc, lint pass 1:
146 ;; /usr/src/foo/foo.c(8): warning: w may be used before set
147 ;; or GNU utilities:
148 ;; foo.c:8: error message
149 ;; or HP-UX 7.0 fc:
150 ;; foo.f :16 some horrible error message
151 ;; or GNU utilities with column (GNAT 1.82):
152 ;; foo.adb:2:1: Unit name does not match file name
153 ;; or with column and program name:
154 ;; jade:dbcommon.dsl:133:17:E: missing argument for function call
155 ;;
156 ;; We'll insist that the number be followed by a colon or closing
157 ;; paren, because otherwise this matches just about anything
158 ;; containing a number with spaces around it.
159 ("\\([-a-zA-Z._]+: ?\\)?\\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\
160 \\([) \t]\\|:\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 2 3 6)
161
162 ;; Microsoft C/C++:
163 ;; keyboard.c(537) : warning C4005: 'min' : macro redefinition
164 ;; d:\tmp\test.c(23) : error C2143: syntax error : missing ';' before 'if'
165 ;; This used to be less selective and allow characters other than
166 ;; parens around the line number, but that caused confusion for
167 ;; GNU-style error messages.
168 ;; This used to reject spaces and dashes in file names,
169 ;; but they are valudnow; so I made it more strict about the error
170 ;; message that follows.
171 ("\\(\\([a-zA-Z]:\\)?[^:(\t\n]+\\)(\\([0-9]+\\)) \
172 : \\(error\\|warning\\) C[0-9]+:" 1 3)
173
174 ;; Borland C++:
175 ;; Error ping.c 15: Unable to open include file 'sys/types.h'
176 ;; Warning ping.c 68: Call to function 'func' with no prototype
177 ("\\(Error\\|Warning\\) \\([a-zA-Z]?:?[^:( \t\n]+\\)\
178 \\([0-9]+\\)\\([) \t]\\|:[^0-9\n]\\)" 2 3)
179
180 ;; 4.3BSD lint pass 2
181 ;; strcmp: variable # of args. llib-lc(359) :: /usr/src/foo/foo.c(8)
182 (".*[ \t:]\\([a-zA-Z]?:?[^:( \t\n]+\\)[:(](+[ \t]*\\([0-9]+\\))[:) \t]*$"
183 1 2)
184
185 ;; 4.3BSD lint pass 3
186 ;; bloofle defined( /users/wolfgang/foo.c(4) ), but never used
187 ;; This used to be
188 ;; ("[ \t(]+\\([a-zA-Z]?:?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]+" 1 2)
189 ;; which is regexp Impressionism - it matches almost anything!
190 (".*([ \t]*\\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\))" 1 2)
191
192 ;; MIPS lint pass<n>; looks good for SunPro lint also
193 ;; TrimMask (255) in solomon.c may be indistinguishable from TrimMasks (93) in solomon.c due to truncation
194 ("[^\n ]+ (\\([0-9]+\\)) in \\([^ \n]+\\)" 2 1)
195 ;; name defined but never used: LinInt in cmap_calc.c(199)
196 (".*in \\([^(\n]+\\)(\\([0-9]+\\))$" 1 2)
197
198 ;; Ultrix 3.0 f77:
199 ;; fort: Severe: addstf.f, line 82: Missing operator or delimiter symbol
200 ;; Some SGI cc version:
201 ;; cfe: Warning 835: foo.c, line 2: something
202 ("\\(cfe\\|fort\\): [^:\n]*: \\([^ \n]*\\), line \\([0-9]+\\):" 2 3)
203 ;; Error on line 3 of t.f: Execution error unclassifiable statement
204 ;; Unknown who does this:
205 ;; Line 45 of "foo.c": bloofle undefined
206 ;; Absoft FORTRAN 77 Compiler 3.1.3
207 ;; error on line 19 of fplot.f: spelling error?
208 ;; warning on line 17 of fplot.f: data type is undefined for variable d
209 ("\\(.* on \\)?[Ll]ine[ \t]+\\([0-9]+\\)[ \t]+\
210 of[ \t]+\"?\\([a-zA-Z]?:?[^\":\n]+\\)\"?:" 3 2)
211
212 ;; Apollo cc, 4.3BSD fc:
213 ;; "foo.f", line 3: Error: syntax error near end of statement
214 ;; IBM RS6000:
215 ;; "vvouch.c", line 19.5: 1506-046 (S) Syntax error.
216 ;; Microtec mcc68k:
217 ;; "foo.c", line 32 pos 1; (E) syntax error; unexpected symbol: "lossage"
218 ;; GNAT (as of July 94):
219 ;; "foo.adb", line 2(11): warning: file name does not match ...
220 ;; IBM AIX xlc compiler:
221 ;; "src/swapping.c", line 30.34: 1506-342 (W) "/*" detected in comment.
222 (".*\"\\([^,\" \n\t]+\\)\", lines? \
223 \\([0-9]+\\)\\([\(.]\\([0-9]+\\)\)?\\)?[:., (-]" 1 2 4)
224
225 ;; Caml compiler:
226 ;; File "foobar.ml", lines 5-8, characters 20-155: blah blah
227 ("^File \"\\([^,\" \n\t]+\\)\", lines? \\([0-9]+\\)[-0-9]*, characters? \\([0-9]+\\)" 1 2 3)
228
229 ;; MIPS RISC CC - the one distributed with Ultrix:
230 ;; ccom: Error: foo.c, line 2: syntax error
231 ;; DEC AXP OSF/1 cc
232 ;; /usr/lib/cmplrs/cc/cfe: Error: foo.c: 1: blah blah
233 ("[a-z0-9/]+: \\([eE]rror\\|[wW]arning\\): \\([^,\" \n\t]+\\)[,:] \\(line \\)?\\([0-9]+\\):" 2 4)
234
235 ;; IBM AIX PS/2 C version 1.1:
236 ;; ****** Error number 140 in line 8 of file errors.c ******
237 (".*in line \\([0-9]+\\) of file \\([^ \n]+[^. \n]\\)\\.? " 2 1)
238 ;; IBM AIX lint is too painful to do right this way. File name
239 ;; prefixes entire sections rather than being on each line.
240
241 ;; SPARCcompiler Pascal:
242 ;; 20 linjer : array[1..4] of linje;
243 ;; e 18480-----------^--- Inserted ';'
244 ;; and
245 ;; E 18520 line 61 - 0 is undefined
246 ;; These messages don't contain a file name. Instead the compiler gives
247 ;; a message whenever the file being compiled is changed.
248 (" +\\([0-9]+\\) +.*\n[ew] [0-9]+-+" nil 1)
249 ("[Ew] +[0-9]+ line \\([0-9]+\\) - " nil 1)
250
251 ;; Lucid Compiler, lcc 3.x
252 ;; E, file.cc(35,52) Illegal operation on pointers
253 ("[EW], \\([^(\n]*\\)(\\([0-9]+\\),[ \t]*\\([0-9]+\\)" 1 2 3)
254
255 ;;; This seems to be superfluous because the first pattern matches it.
256 ;;; ;; GNU messages with program name and optional column number.
257 ;;; ("[a-zA-Z]?:?[^0-9 \n\t:]+[^ \n\t:]*:[ \t]*\\([^ \n\t:]+\\):\
258 ;;;\\([0-9]+\\):\\(\\([0-9]+\\)[: \t]\\)?" 1 2 4)
259
260 ;; Cray C compiler error messages
261 ("\\(cc\\| cft\\)-[0-9]+ c\\(c\\|f77\\): ERROR \\([^,\n]+, \\)* File = \
262 \\([^,\n]+\\), Line = \\([0-9]+\\)" 4 5)
263
264 ;; IBM C/C++ Tools 2.01:
265 ;; foo.c(2:0) : informational EDC0804: Function foo is not referenced.
266 ;; foo.c(3:8) : warning EDC0833: Implicit return statement encountered.
267 ;; foo.c(5:5) : error EDC0350: Syntax error.
268 ("\\([^( \n\t]+\\)(\\([0-9]+\\):\\([0-9]+\\)) : " 1 2 3)
269
270 ;; IAR Systems C Compiler:
271 ;; "foo.c",3 Error[32]: Error message
272 ;; "foo.c",3 Warning[32]: Error message
273 ("\"\\(.*\\)\",\\([0-9]+\\)\\s-+\\(Error\\|Warning\\)\\[[0-9]+\\]:" 1 2)
274
275 ;; Sun ada (VADS, Solaris):
276 ;; /home3/xdhar/rcds_rc/main.a, line 361, char 6:syntax error: "," inserted
277 ("\\([^, \n\t]+\\), line \\([0-9]+\\), char \\([0-9]+\\)[:., \(-]" 1 2 3)
278
279 ;; Perl -w:
280 ;; syntax error at automake line 922, near "':'"
281 (".* at \\([^ ]+\\) line \\([0-9]+\\)," 1 2)
282
283 ;; Oracle pro*c:
284 ;; Semantic error at line 528, column 5, file erosacqdb.pc:
285 ("Semantic error at line \\([0-9]+\\), column \\([0-9]+\\), file \\(.*\\):"
286 3 1 2)
287
288 ;; EPC F90 compiler:
289 ;; Error 24 at (2:progran.f90) : syntax error
290 ("Error [0-9]+ at (\\([0-9]*\\):\\([^)\n]+\\))" 2 1)
291
292 ;; Sun F90 error messages:
293 ;; cf90-113 f90comp: ERROR NSE, File = Hoved.f90, Line = 16, Column = 3
294 (".* ERROR [a-zA-Z0-9 ]+, File = \\(.+\\), Line = \\([0-9]+\\), Column = \\([0-9]+\\)"
295 1 2 3)
296 )
297 "Alist that specifies how to match errors in compiler output.
298 Each elt has the form (REGEXP FILE-IDX LINE-IDX [COLUMN-IDX FILE-FORMAT...])
299 If REGEXP matches, the FILE-IDX'th subexpression gives the file name, and
300 the LINE-IDX'th subexpression gives the line number. If COLUMN-IDX is
301 given, the COLUMN-IDX'th subexpression gives the column number on that line.
302 If any FILE-FORMAT is given, each is a format string to produce a file name to
303 try; %s in the string is replaced by the text matching the FILE-IDX'th
304 subexpression.")
305
306 (defvar compilation-enter-directory-regexp-alist
307 '(
308 ;; Matches lines printed by the `-w' option of GNU Make.
309 (".*: Entering directory `\\(.*\\)'$" 1)
310 )
311 "Alist specifying how to match lines that indicate a new current directory.
312 Note that the match is done at the beginning of lines.
313 Each elt has the form (REGEXP IDX).
314 If REGEXP matches, the IDX'th subexpression gives the directory name.
315
316 The default value matches lines printed by the `-w' option of GNU Make.")
317
318 (defvar compilation-leave-directory-regexp-alist
319 '(
320 ;; Matches lines printed by the `-w' option of GNU Make.
321 (".*: Leaving directory `\\(.*\\)'$" 1)
322 )
323 "Alist specifying how to match lines that indicate restoring current directory.
324 Note that the match is done at the beginning of lines.
325 Each elt has the form (REGEXP IDX).
326 If REGEXP matches, the IDX'th subexpression gives the name of the directory
327 being moved from. If IDX is nil, the last directory entered \(by a line
328 matching `compilation-enter-directory-regexp-alist'\) is assumed.
329
330 The default value matches lines printed by the `-w' option of GNU Make.")
331
332 (defvar compilation-file-regexp-alist
333 '(
334 ;; This matches entries with date time year file-name: like
335 ;; Thu May 14 10:46:12 1992 mom3.p:
336 ("\\w\\w\\w \\w\\w\\w +[0-9]+ [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9][0-9][0-9] \\(.*\\):$" 1)
337 )
338 "Alist specifying how to match lines that indicate a new current file.
339 Note that the match is done at the beginning of lines.
340 Each elt has the form (REGEXP IDX).
341 If REGEXP matches, the IDX'th subexpression gives the file name. This is
342 used with compilers that don't indicate file name in every error message.")
343
344 ;; There is no generally useful regexp that will match non messages, but
345 ;; in special cases there might be one. The lines that are not matched by
346 ;; a regexp take much longer time than the ones that are recognized so if
347 ;; you have same regexeps here, parsing is faster.
348 (defvar compilation-nomessage-regexp-alist
349 '(
350 )
351 "Alist specifying how to match lines that have no message.
352 Note that the match is done at the beginning of lines.
353 Each elt has the form (REGEXP). This alist is by default empty, but if
354 you have some good regexps here, the parsing of messages will be faster.")
355
356 (defcustom compilation-error-screen-columns t
357 "*If non-nil, column numbers in error messages are screen columns.
358 Otherwise they are interpreted as character positions, with
359 each character occupying one column.
360 The default is to use screen columns, which requires that the compilation
361 program and Emacs agree about the display width of the characters,
362 especially the TAB character."
363 :type 'boolean
364 :group 'compilation
365 :version "20.4")
366
367 (defcustom compilation-read-command t
368 "*If not nil, M-x compile reads the compilation command to use.
369 Otherwise, M-x compile just uses the value of `compile-command'."
370 :type 'boolean
371 :group 'compilation)
372
373 ;;;###autoload
374 (defcustom compilation-ask-about-save t
375 "*If not nil, M-x compile asks which buffers to save before compiling.
376 Otherwise, it saves all modified buffers without asking."
377 :type 'boolean
378 :group 'compilation)
379
380 (defvar grep-regexp-alist
381 '(("\\([a-zA-Z]?:?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2))
382 "Regexp used to match grep hits. See `compilation-error-regexp-alist'.")
383
384 (defvar grep-program
385 ;; Currently zgrep has trouble. It runs egrep instead of grep,
386 ;; and it doesn't pass along long options right.
387 "grep"
388 ;;; (if (equal (condition-case nil ; in case "zgrep" isn't in exec-path
389 ;;; (call-process "zgrep" nil nil nil
390 ;;; "foo" null-device)
391 ;;; (error nil))
392 ;;; 1)
393 ;;; "zgrep"
394 ;;; "grep")
395 "The default grep program for `grep-command' and `grep-find-command'.
396 This variable's value takes effect when `grep-compute-defaults' is called.")
397
398 ;; Use -e if grep supports it,
399 ;; because that avoids lossage if the pattern starts with `-'.
400 (defvar grep-command nil
401 "The default grep command for \\[grep].
402 The real default value of this variable is set up by `grep-compute-defaults';
403 call that function before using this variable.")
404
405 (defvar grep-find-use-xargs nil
406 "Whether \\[grep-find] uses the `xargs' utility by default.
407
408 If nil, it uses `grep -exec'; if `gnu', it uses `find -print0' and `xargs -0';
409 if not nil and not `gnu', it uses `find -print' and `xargs'.
410
411 This variable's value takes effect when `grep-compute-defaults' is called.")
412
413 (defvar grep-find-command nil
414 "The default find command for \\[grep-find].
415 The default value of this variable is set up by `grep-compute-defaults';
416 call that function before using this variable.")
417
418 ;;;###autoload
419 (defcustom compilation-search-path '(nil)
420 "*List of directories to search for source files named in error messages.
421 Elements should be directory names, not file names of directories.
422 nil as an element means to try the default directory."
423 :type '(repeat (choice (const :tag "Default" nil)
424 (string :tag "Directory")))
425 :group 'compilation)
426
427 (defcustom compile-command "make -k "
428 "*Last shell command used to do a compilation; default for next compilation.
429
430 Sometimes it is useful for files to supply local values for this variable.
431 You might also use mode hooks to specify it in certain modes, like this:
432
433 (add-hook 'c-mode-hook
434 (function
435 (lambda ()
436 (unless (or (file-exists-p \"makefile\")
437 (file-exists-p \"Makefile\"))
438 (make-local-variable 'compile-command)
439 (setq compile-command
440 (concat \"make -k \"
441 (file-name-sans-extension buffer-file-name)))))))"
442 :type 'string
443 :group 'compilation)
444
445 (defvar compilation-directory-stack nil
446 "Stack of previous directories for `compilation-leave-directory-regexp'.
447 The last element is the directory the compilation was started in.")
448
449 (defvar compilation-exit-message-function nil "\
450 If non-nil, called when a compilation process dies to return a status message.
451 This should be a function of three arguments: process status, exit status,
452 and exit message; it returns a cons (MESSAGE . MODELINE) of the strings to
453 write into the compilation buffer, and to put in its mode line.")
454
455 ;; History of compile commands.
456 (defvar compile-history nil)
457 ;; History of grep commands.
458 (defvar grep-history nil)
459 (defvar grep-find-history nil)
460
461 (defun compilation-mode-font-lock-keywords ()
462 "Return expressions to highlight in Compilation mode."
463 (nconc
464 ;;
465 ;; Compiler warning/error lines.
466 (mapcar (function
467 (lambda (item)
468 ;; Prepend "^", adjusting FILE-IDX and LINE-IDX accordingly.
469 (let ((file-idx (nth 1 item))
470 (line-idx (nth 2 item))
471 (col-idx (nth 3 item))
472 keyword)
473 (when (numberp col-idx)
474 (setq keyword
475 (cons (list (1+ col-idx) 'font-lock-type-face nil t)
476 keyword)))
477 (when (numberp line-idx)
478 (setq keyword
479 (cons (list (1+ line-idx) 'font-lock-variable-name-face)
480 keyword)))
481 (when (numberp file-idx)
482 (setq keyword
483 (cons (list (1+ file-idx) 'font-lock-warning-face)
484 keyword)))
485 (cons (concat "^\\(" (nth 0 item) "\\)") keyword))))
486 compilation-error-regexp-alist)
487 (list
488 ;;
489 ;; Compiler output lines. Recognize `make[n]:' lines too.
490 '("^\\([A-Za-z_0-9/\.+-]+\\)\\(\\[\\([0-9]+\\)\\]\\)?[ \t]*:"
491 (1 font-lock-function-name-face) (3 font-lock-comment-face nil t)))
492 ))
493 \f
494 ;;;###autoload
495 (defun compile (command)
496 "Compile the program including the current buffer. Default: run `make'.
497 Runs COMMAND, a shell command, in a separate process asynchronously
498 with output going to the buffer `*compilation*'.
499
500 You can then use the command \\[next-error] to find the next error message
501 and move to the source code that caused it.
502
503 Interactively, prompts for the command if `compilation-read-command' is
504 non-nil; otherwise uses `compile-command'. With prefix arg, always prompts.
505
506 To run more than one compilation at once, start one and rename the
507 \`*compilation*' buffer to some other name with \\[rename-buffer].
508 Then start the next one.
509
510 The name used for the buffer is actually whatever is returned by
511 the function in `compilation-buffer-name-function', so you can set that
512 to a function that generates a unique name."
513 (interactive
514 (if (or compilation-read-command current-prefix-arg)
515 (list (read-from-minibuffer "Compile command: "
516 compile-command nil nil
517 '(compile-history . 1)))
518 (list compile-command)))
519 (setq compile-command command)
520 (save-some-buffers (not compilation-ask-about-save) nil)
521 (compile-internal compile-command "No more errors"))
522
523 ;;; run compile with the default command line
524 (defun recompile ()
525 "Re-compile the program including the current buffer."
526 (interactive)
527 (save-some-buffers (not compilation-ask-about-save) nil)
528 (compile-internal compile-command "No more errors"))
529
530 (defun grep-process-setup ()
531 "Set up `compilation-exit-message-function' for `grep'."
532 (set (make-local-variable 'compilation-exit-message-function)
533 (lambda (status code msg)
534 (if (eq status 'exit)
535 (cond ((zerop code)
536 '("finished (matches found)\n" . "matched"))
537 ((= code 1)
538 '("finished with no matches found\n" . "no match"))
539 (t
540 (cons msg code)))
541 (cons msg code)))))
542
543 (defun grep-compute-defaults ()
544 (setq grep-command
545 (if (equal (condition-case nil ; in case "grep" isn't in exec-path
546 (call-process grep-program nil nil nil
547 "-e" "foo" null-device)
548 (error nil))
549 1)
550 (format "%s -n -e " grep-program)
551 (format "%s -n " grep-program)))
552 (unless grep-find-use-xargs
553 (setq grep-find-use-xargs
554 (if (equal (call-process "find" nil nil nil
555 null-device "-print0")
556 0)
557 'gnu)))
558 (setq grep-find-command
559 (cond ((eq grep-find-use-xargs 'gnu)
560 (format "find . -type f -print0 | xargs -0 -e %s"
561 grep-command))
562 (grep-find-use-xargs
563 (format "find . -type f -print | xargs %s" grep-command))
564 (t (cons (format "find . -type f -exec %s {} /dev/null \\;"
565 grep-command)
566 (+ 22 (length grep-command)))))))
567
568 ;;;###autoload
569 (defun grep (command-args)
570 "Run grep, with user-specified args, and collect output in a buffer.
571 While grep runs asynchronously, you can use \\[next-error] (M-x next-error),
572 or \\<compilation-minor-mode-map>\\[compile-goto-error] in the grep \
573 output buffer, to go to the lines
574 where grep found matches.
575
576 This command uses a special history list for its arguments, so you can
577 easily repeat a grep command.
578
579 A prefix argument says to default the argument based upon the current
580 tag the cursor is over, substituting it into the last grep command
581 in the grep command history (or into `grep-command'
582 if that history list is empty)."
583 (interactive
584 (let (grep-default (arg current-prefix-arg))
585 (unless grep-command
586 (grep-compute-defaults))
587 (when arg
588 (let* ((tag-default
589 (funcall (or find-tag-default-function
590 (get major-mode 'find-tag-default-function)
591 ;; We use grep-tag-default instead of
592 ;; find-tag-default, to avoid loading etags.
593 'grep-tag-default))))
594 (setq grep-default (or (car grep-history) grep-command))
595 ;; Replace the thing matching for with that around cursor
596 (if (string-match "[^ ]+\\s +\\(-[^ ]+\\s +\\)*\\(\"[^\"]+\"\\|[^ ]+\\)" grep-default)
597 (setq grep-default (replace-match tag-default t t
598 grep-default 2)))))
599 (list (read-from-minibuffer "Run grep (like this): "
600 (or grep-default grep-command)
601 nil nil 'grep-history))))
602
603 ;; Setting process-setup-function makes exit-message-function work
604 ;; even when async processes aren't supported.
605 (let* ((compilation-process-setup-function 'grep-process-setup)
606 (buf (compile-internal (if null-device
607 (concat command-args " " null-device)
608 command-args)
609 "No more grep hits" "grep"
610 ;; Give it a simpler regexp to match.
611 nil grep-regexp-alist)))))
612
613 ;; This is a copy of find-tag-default from etags.el.
614 (defun grep-tag-default ()
615 (save-excursion
616 (while (looking-at "\\sw\\|\\s_")
617 (forward-char 1))
618 (when (or (re-search-backward "\\sw\\|\\s_"
619 (save-excursion (beginning-of-line) (point))
620 t)
621 (re-search-forward "\\(\\sw\\|\\s_\\)+"
622 (save-excursion (end-of-line) (point))
623 t))
624 (goto-char (match-end 0))
625 (buffer-substring (point)
626 (progn (forward-sexp -1)
627 (while (looking-at "\\s'")
628 (forward-char 1))
629 (point))))))
630
631 ;;;###autoload
632 (defun grep-find (command-args)
633 "Run grep via find, with user-specified args, and collect output in a buffer.
634 While find runs asynchronously, you can use the \\[next-error] command
635 to find the text that grep hits refer to.
636
637 This command uses a special history list for its arguments, so you can
638 easily repeat a find command."
639 (interactive
640 (progn
641 (unless grep-find-command
642 (grep-compute-defaults))
643 (list (read-from-minibuffer "Run find (like this): "
644 grep-find-command nil nil
645 'grep-find-history))))
646 (let ((null-device nil)) ; see grep
647 (grep command-args)))
648
649 (defcustom compilation-scroll-output nil
650 "*Non-nil to scroll the *compilation* buffer window as output appears.
651
652 Setting it causes the compilation-mode commands to put point at the
653 end of their output window so that the end of the output is always
654 visible rather than the begining."
655 :type 'boolean
656 :version "20.3"
657 :group 'compilation)
658
659 (defun compile-internal (command error-message
660 &optional name-of-mode parser
661 error-regexp-alist name-function
662 enter-regexp-alist leave-regexp-alist
663 file-regexp-alist nomessage-regexp-alist)
664 "Run compilation command COMMAND (low level interface).
665 ERROR-MESSAGE is a string to print if the user asks to see another error
666 and there are no more errors. The rest of the arguments, 3-10 are optional.
667 For them nil means use the default.
668 NAME-OF-MODE is the name to display as the major mode in the compilation
669 buffer. PARSER is the error parser function. ERROR-REGEXP-ALIST is the error
670 message regexp alist to use. NAME-FUNCTION is a function called to name the
671 buffer. ENTER-REGEXP-ALIST is the enter directory message regexp alist to use.
672 LEAVE-REGEXP-ALIST is the leave directory message regexp alist to use.
673 FILE-REGEXP-ALIST is the change current file message regexp alist to use.
674 NOMESSAGE-REGEXP-ALIST is the nomessage regexp alist to use.
675 The defaults for these variables are the global values of
676 \`compilation-parse-errors-function', `compilation-error-regexp-alist',
677 \`compilation-buffer-name-function', `compilation-enter-directory-regexp-alist',
678 \`compilation-leave-directory-regexp-alist', `compilation-file-regexp-alist',
679 \ and `compilation-nomessage-regexp-alist', respectively.
680 For arg 7-10 a value `t' means an empty alist.
681
682 Returns the compilation buffer created."
683 (let (outbuf)
684 (save-excursion
685 (or name-of-mode
686 (setq name-of-mode "Compilation"))
687 (setq outbuf
688 (get-buffer-create
689 (funcall (or name-function compilation-buffer-name-function
690 (function (lambda (mode)
691 (concat "*" (downcase mode) "*"))))
692 name-of-mode)))
693 (set-buffer outbuf)
694 (let ((comp-proc (get-buffer-process (current-buffer))))
695 (if comp-proc
696 (if (or (not (eq (process-status comp-proc) 'run))
697 (yes-or-no-p
698 (format "A %s process is running; kill it? "
699 name-of-mode)))
700 (condition-case ()
701 (progn
702 (interrupt-process comp-proc)
703 (sit-for 1)
704 (delete-process comp-proc))
705 (error nil))
706 (error "Cannot have two processes in `%s' at once"
707 (buffer-name))
708 )))
709 ;; In case the compilation buffer is current, make sure we get the global
710 ;; values of compilation-error-regexp-alist, etc.
711 (kill-all-local-variables))
712 (or error-regexp-alist
713 (setq error-regexp-alist compilation-error-regexp-alist))
714 (or enter-regexp-alist
715 (setq enter-regexp-alist compilation-enter-directory-regexp-alist))
716 (or leave-regexp-alist
717 (setq leave-regexp-alist compilation-leave-directory-regexp-alist))
718 (or file-regexp-alist
719 (setq file-regexp-alist compilation-file-regexp-alist))
720 (or nomessage-regexp-alist
721 (setq nomessage-regexp-alist compilation-nomessage-regexp-alist))
722 (or parser (setq parser compilation-parse-errors-function))
723 (let ((thisdir default-directory)
724 outwin)
725 (save-excursion
726 ;; Clear out the compilation buffer and make it writable.
727 ;; Change its default-directory to the directory where the compilation
728 ;; will happen, and insert a `cd' command to indicate this.
729 (set-buffer outbuf)
730 (setq buffer-read-only nil)
731 (buffer-disable-undo (current-buffer))
732 (erase-buffer)
733 (buffer-enable-undo (current-buffer))
734 (setq default-directory thisdir)
735 (insert "cd " thisdir "\n" command "\n")
736 (set-buffer-modified-p nil))
737 ;; If we're already in the compilation buffer, go to the end
738 ;; of the buffer, so point will track the compilation output.
739 (if (eq outbuf (current-buffer))
740 (goto-char (point-max)))
741 ;; Pop up the compilation buffer.
742 (setq outwin (display-buffer outbuf))
743 (save-excursion
744 (set-buffer outbuf)
745 (compilation-mode name-of-mode)
746 ;; (setq buffer-read-only t) ;;; Non-ergonomic.
747 (set (make-local-variable 'compilation-parse-errors-function) parser)
748 (set (make-local-variable 'compilation-error-message) error-message)
749 (set (make-local-variable 'compilation-error-regexp-alist)
750 error-regexp-alist)
751 (set (make-local-variable 'compilation-enter-directory-regexp-alist)
752 enter-regexp-alist)
753 (set (make-local-variable 'compilation-leave-directory-regexp-alist)
754 leave-regexp-alist)
755 (set (make-local-variable 'compilation-file-regexp-alist)
756 file-regexp-alist)
757 (set (make-local-variable 'compilation-nomessage-regexp-alist)
758 nomessage-regexp-alist)
759 (set (make-local-variable 'compilation-arguments)
760 (list command error-message
761 name-of-mode parser
762 error-regexp-alist name-function
763 enter-regexp-alist leave-regexp-alist
764 file-regexp-alist nomessage-regexp-alist))
765 (make-local-variable 'lazy-lock-defer-on-scrolling)
766 ;; This proves a good idea if the buffer's going to scroll
767 ;; with lazy-lock on.
768 (setq lazy-lock-defer-on-scrolling t)
769 (setq default-directory thisdir
770 compilation-directory-stack (list default-directory))
771 (set-window-start outwin (point-min))
772 (or (eq outwin (selected-window))
773 (set-window-point outwin (point-min)))
774 (compilation-set-window-height outwin)
775 (if compilation-process-setup-function
776 (funcall compilation-process-setup-function))
777 ;; Start the compilation.
778 (if (fboundp 'start-process)
779 (let* ((process-environment (cons "EMACS=t" process-environment))
780 (proc (start-process-shell-command (downcase mode-name)
781 outbuf
782 command)))
783 (set-process-sentinel proc 'compilation-sentinel)
784 (set-process-filter proc 'compilation-filter)
785 (set-marker (process-mark proc) (point) outbuf)
786 (setq compilation-in-progress
787 (cons proc compilation-in-progress)))
788 ;; No asynchronous processes available.
789 (message "Executing `%s'..." command)
790 ;; Fake modeline display as if `start-process' were run.
791 (setq mode-line-process ":run")
792 (force-mode-line-update)
793 (sit-for 0) ; Force redisplay
794 (let ((status (call-process shell-file-name nil outbuf nil "-c"
795 command)))
796 (cond ((numberp status)
797 (compilation-handle-exit 'exit status
798 (if (zerop status)
799 "finished\n"
800 (format "\
801 exited abnormally with code %d\n"
802 status))))
803 ((stringp status)
804 (compilation-handle-exit 'signal status
805 (concat status "\n")))
806 (t
807 (compilation-handle-exit 'bizarre status status))))
808 (message "Executing `%s'...done" command)))
809 (if compilation-scroll-output
810 (let ((currbuf (current-buffer)))
811 (select-window outwin)
812 (goto-char (point-max))
813 (select-window (get-buffer-window currbuf)))))
814 ;; Make it so the next C-x ` will use this buffer.
815 (setq compilation-last-buffer outbuf)))
816
817 ;; Set the height of WINDOW according to compilation-window-height.
818 (defun compilation-set-window-height (window)
819 (and compilation-window-height
820 (= (window-width window) (frame-width (window-frame window)))
821 ;; If window is alone in its frame, aside from a minibuffer,
822 ;; don't change its height.
823 (not (eq window (frame-root-window (window-frame window))))
824 ;; This save-excursion prevents us from changing the current buffer,
825 ;; which might not be the same as the selected window's buffer.
826 (save-excursion
827 (let ((w (selected-window)))
828 (unwind-protect
829 (progn
830 (select-window window)
831 (enlarge-window (- compilation-window-height
832 (window-height))))
833 (select-window w))))))
834
835 (defvar compilation-minor-mode-map
836 (let ((map (make-sparse-keymap)))
837 (define-key map [mouse-2] 'compile-mouse-goto-error)
838 (define-key map "\C-c\C-c" 'compile-goto-error)
839 (define-key map "\C-m" 'compile-goto-error)
840 (define-key map "\C-c\C-k" 'kill-compilation)
841 (define-key map "\M-n" 'compilation-next-error)
842 (define-key map "\M-p" 'compilation-previous-error)
843 (define-key map "\M-{" 'compilation-previous-file)
844 (define-key map "\M-}" 'compilation-next-file)
845 map)
846 "Keymap for `compilation-minor-mode'.")
847
848 (defvar compilation-shell-minor-mode-map
849 (let ((map (make-sparse-keymap)))
850 (define-key map [mouse-2] 'compile-mouse-goto-error)
851 (define-key map "\M-\C-m" 'compile-goto-error)
852 (define-key map "\M-\C-n" 'compilation-next-error)
853 (define-key map "\M-\C-p" 'compilation-previous-error)
854 (define-key map "\M-{" 'compilation-previous-file)
855 (define-key map "\M-}" 'compilation-next-file)
856 ;; Set up the menu-bar
857 (define-key map [menu-bar errors-menu]
858 (cons "Errors" (make-sparse-keymap "Errors")))
859 (define-key map [menu-bar errors-menu stop-subjob]
860 '("Stop" . comint-interrupt-subjob))
861 (define-key map [menu-bar errors-menu compilation-mode-separator2]
862 '("----" . nil))
863 (define-key map [menu-bar errors-menu compilation-mode-first-error]
864 '("First Error" . first-error))
865 (define-key map [menu-bar errors-menu compilation-mode-previous-error]
866 '("Previous Error" . previous-error))
867 (define-key map [menu-bar errors-menu compilation-mode-next-error]
868 '("Next Error" . next-error))
869 map)
870 "Keymap for `compilation-shell-minor-mode'.")
871
872 (defvar compilation-mode-map
873 (let ((map (cons 'keymap compilation-minor-mode-map)))
874 (define-key map " " 'scroll-up)
875 (define-key map "\^?" 'scroll-down)
876 ;; Set up the menu-bar
877 (define-key map [menu-bar compilation-menu]
878 (cons "Compile" (make-sparse-keymap "Compile")))
879
880 (define-key map [menu-bar compilation-menu compilation-mode-kill-compilation]
881 '("Stop Compilation" . kill-compilation))
882 (define-key map [menu-bar compilation-menu compilation-mode-separator2]
883 '("----" . nil))
884 (define-key map [menu-bar compilation-menu compilation-mode-first-error]
885 '("First Error" . first-error))
886 (define-key map [menu-bar compilation-menu compilation-mode-previous-error]
887 '("Previous Error" . previous-error))
888 (define-key map [menu-bar compilation-menu compilation-mode-next-error]
889 '("Next Error" . next-error))
890 (define-key map [menu-bar compilation-menu compilation-separator2]
891 '("----" . nil))
892 (define-key map [menu-bar compilation-menu compilation-mode-grep]
893 '("Search Files (grep)" . grep))
894 (define-key map [menu-bar compilation-menu compilation-mode-recompile]
895 '("Recompile" . recompile))
896 (define-key map [menu-bar compilation-menu compilation-mode-compile]
897 '("Compile..." . compile))
898 map)
899 "Keymap for compilation log buffers.
900 `compilation-minor-mode-map' is a cdr of this.")
901
902 (put 'compilation-mode 'mode-class 'special)
903
904 ;;;###autoload
905 (defun compilation-mode (&optional name-of-mode)
906 "Major mode for compilation log buffers.
907 \\<compilation-mode-map>To visit the source for a line-numbered error,
908 move point to the error message line and type \\[compile-goto-error].
909 To kill the compilation, type \\[kill-compilation].
910
911 Runs `compilation-mode-hook' with `run-hooks' (which see)."
912 (interactive)
913 (kill-all-local-variables)
914 (use-local-map compilation-mode-map)
915 (setq major-mode 'compilation-mode
916 mode-name (or name-of-mode "Compilation"))
917 (compilation-setup)
918 (set (make-local-variable 'font-lock-defaults)
919 '(compilation-mode-font-lock-keywords t))
920 (set (make-local-variable 'revert-buffer-function)
921 'compilation-revert-buffer)
922 (run-hooks 'compilation-mode-hook))
923
924 (defun compilation-revert-buffer (ignore-auto noconfirm)
925 (if (or noconfirm (yes-or-no-p (format "Restart compilation? ")))
926 (apply 'compile-internal compilation-arguments)))
927
928 ;; Prepare the buffer for the compilation parsing commands to work.
929 (defun compilation-setup ()
930 ;; Make the buffer's mode line show process state.
931 (setq mode-line-process '(":%s"))
932 (set (make-local-variable 'compilation-error-list) nil)
933 (set (make-local-variable 'compilation-old-error-list) nil)
934 (set (make-local-variable 'compilation-parsing-end) 1)
935 (set (make-local-variable 'compilation-directory-stack) nil)
936 (setq compilation-last-buffer (current-buffer)))
937
938 (defvar compilation-shell-minor-mode nil
939 "Non-nil when in compilation-shell-minor-mode.
940 In this minor mode, all the error-parsing commands of the
941 Compilation major mode are available but bound to keys that don't
942 collide with Shell mode.")
943 (make-variable-buffer-local 'compilation-shell-minor-mode)
944
945 (or (assq 'compilation-shell-minor-mode minor-mode-alist)
946 (setq minor-mode-alist
947 (cons '(compilation-shell-minor-mode " Shell-Compile")
948 minor-mode-alist)))
949 (or (assq 'compilation-shell-minor-mode minor-mode-map-alist)
950 (setq minor-mode-map-alist (cons (cons 'compilation-shell-minor-mode
951 compilation-shell-minor-mode-map)
952 minor-mode-map-alist)))
953
954 (defvar compilation-minor-mode nil
955 "Non-nil when in compilation-minor-mode.
956 In this minor mode, all the error-parsing commands of the
957 Compilation major mode are available.")
958 (make-variable-buffer-local 'compilation-minor-mode)
959
960 (or (assq 'compilation-minor-mode minor-mode-alist)
961 (setq minor-mode-alist (cons '(compilation-minor-mode " Compilation")
962 minor-mode-alist)))
963 (or (assq 'compilation-minor-mode minor-mode-map-alist)
964 (setq minor-mode-map-alist (cons (cons 'compilation-minor-mode
965 compilation-minor-mode-map)
966 minor-mode-map-alist)))
967
968 ;;;###autoload
969 (defun compilation-minor-mode (&optional arg)
970 "Toggle compilation minor mode.
971 With arg, turn compilation mode on if and only if arg is positive.
972 See `compilation-mode'.
973 Turning the mode on runs the normal hook `compilation-minor-mode-hook'."
974 (interactive "P")
975 (if (setq compilation-minor-mode (if (null arg)
976 (null compilation-minor-mode)
977 (> (prefix-numeric-value arg) 0)))
978 (let ((mode-line-process))
979 (compilation-setup)
980 (run-hooks 'compilation-minor-mode-hook))))
981
982 ;; Write msg in the current buffer and hack its mode-line-process.
983 (defun compilation-handle-exit (process-status exit-status msg)
984 (let ((buffer-read-only nil)
985 (status (if compilation-exit-message-function
986 (funcall compilation-exit-message-function
987 process-status exit-status msg)
988 (cons msg exit-status)))
989 (omax (point-max))
990 (opoint (point)))
991 ;; Record where we put the message, so we can ignore it
992 ;; later on.
993 (goto-char omax)
994 (insert ?\n mode-name " " (car status))
995 (forward-char -1)
996 (insert " at " (substring (current-time-string) 0 19))
997 (forward-char 1)
998 (setq mode-line-process (format ":%s [%s]" process-status (cdr status)))
999 ;; Force mode line redisplay soon.
1000 (force-mode-line-update)
1001 (if (and opoint (< opoint omax))
1002 (goto-char opoint))
1003 ;; Automatically parse (and mouse-highlight) error messages:
1004 (cond ((eq compile-auto-highlight t)
1005 (compile-reinitialize-errors nil (point-max)))
1006 ((numberp compile-auto-highlight)
1007 (compile-reinitialize-errors nil
1008 (save-excursion
1009 (goto-line compile-auto-highlight)
1010 (point)))))
1011 (if compilation-finish-function
1012 (funcall compilation-finish-function (current-buffer) msg))
1013 (let ((functions compilation-finish-functions))
1014 (while functions
1015 (funcall (car functions) (current-buffer) msg)
1016 (setq functions (cdr functions))))))
1017
1018 ;; Called when compilation process changes state.
1019 (defun compilation-sentinel (proc msg)
1020 "Sentinel for compilation buffers."
1021 (let ((buffer (process-buffer proc)))
1022 (if (memq (process-status proc) '(signal exit))
1023 (progn
1024 (if (null (buffer-name buffer))
1025 ;; buffer killed
1026 (set-process-buffer proc nil)
1027 (let ((obuf (current-buffer)))
1028 ;; save-excursion isn't the right thing if
1029 ;; process-buffer is current-buffer
1030 (unwind-protect
1031 (progn
1032 ;; Write something in the compilation buffer
1033 ;; and hack its mode line.
1034 (set-buffer buffer)
1035 (compilation-handle-exit (process-status proc)
1036 (process-exit-status proc)
1037 msg)
1038 ;; Since the buffer and mode line will show that the
1039 ;; process is dead, we can delete it now. Otherwise it
1040 ;; will stay around until M-x list-processes.
1041 (delete-process proc))
1042 (set-buffer obuf))))
1043 (setq compilation-in-progress (delq proc compilation-in-progress))
1044 ))))
1045
1046 (defun compilation-filter (proc string)
1047 "Process filter for compilation buffers.
1048 Just inserts the text, but uses `insert-before-markers'."
1049 (if (buffer-name (process-buffer proc))
1050 (save-excursion
1051 (set-buffer (process-buffer proc))
1052 (let ((buffer-read-only nil))
1053 (save-excursion
1054 (goto-char (process-mark proc))
1055 (insert-before-markers string)
1056 (run-hooks 'compilation-filter-hook)
1057 (set-marker (process-mark proc) (point)))))))
1058
1059 ;; Return the cdr of compilation-old-error-list for the error containing point.
1060 (defun compile-error-at-point ()
1061 (compile-reinitialize-errors nil (point))
1062 (let ((errors compilation-old-error-list))
1063 (while (and errors
1064 (> (point) (car (car errors))))
1065 (setq errors (cdr errors)))
1066 errors))
1067
1068 (defsubst compilation-buffer-p (buffer)
1069 (save-excursion
1070 (set-buffer buffer)
1071 (or compilation-shell-minor-mode compilation-minor-mode
1072 (eq major-mode 'compilation-mode))))
1073
1074 (defun compilation-next-error (n)
1075 "Move point to the next error in the compilation buffer.
1076 Does NOT find the source line like \\[next-error]."
1077 (interactive "p")
1078 (or (compilation-buffer-p (current-buffer))
1079 (error "Not in a compilation buffer."))
1080 (setq compilation-last-buffer (current-buffer))
1081
1082 (let ((errors (compile-error-at-point)))
1083
1084 ;; Move to the error after the one containing point.
1085 (goto-char (car (if (< n 0)
1086 (let ((i 0)
1087 (e compilation-old-error-list))
1088 ;; See how many cdrs away ERRORS is from the start.
1089 (while (not (eq e errors))
1090 (setq i (1+ i)
1091 e (cdr e)))
1092 (if (> (- n) i)
1093 (error "Moved back past first error")
1094 (nth (+ i n) compilation-old-error-list)))
1095 (let ((compilation-error-list (cdr errors)))
1096 (compile-reinitialize-errors nil nil n)
1097 (if compilation-error-list
1098 (nth (1- n) compilation-error-list)
1099 (error "Moved past last error"))))))))
1100
1101 (defun compilation-previous-error (n)
1102 "Move point to the previous error in the compilation buffer.
1103 Does NOT find the source line like \\[next-error]."
1104 (interactive "p")
1105 (compilation-next-error (- n)))
1106
1107
1108 ;; Given an elt of `compilation-error-list', return an object representing
1109 ;; the referenced file which is equal to (but not necessarily eq to) what
1110 ;; this function would return for another error in the same file.
1111 (defsubst compilation-error-filedata (data)
1112 (setq data (cdr data))
1113 (if (markerp data)
1114 (marker-buffer data)
1115 (car data)))
1116
1117 ;; Return a string describing a value from compilation-error-filedata.
1118 ;; This value is not necessarily useful as a file name, but should be
1119 ;; indicative to the user of what file's errors are being referred to.
1120 (defsubst compilation-error-filedata-file-name (filedata)
1121 (if (bufferp filedata)
1122 (buffer-file-name filedata)
1123 (car filedata)))
1124
1125 (defun compilation-next-file (n)
1126 "Move point to the next error for a different file than the current one."
1127 (interactive "p")
1128 (or (compilation-buffer-p (current-buffer))
1129 (error "Not in a compilation buffer."))
1130 (setq compilation-last-buffer (current-buffer))
1131
1132 (let ((reversed (< n 0))
1133 errors filedata)
1134
1135 (if (not reversed)
1136 (setq errors (or (compile-error-at-point)
1137 (error "Moved past last error")))
1138
1139 ;; Get a reversed list of the errors up through the one containing point.
1140 (compile-reinitialize-errors nil (point))
1141 (setq errors (reverse compilation-old-error-list)
1142 n (- n))
1143
1144 ;; Ignore errors after point. (car ERRORS) will be the error
1145 ;; containing point, (cadr ERRORS) the one before it.
1146 (while (and errors
1147 (< (point) (car (car errors))))
1148 (setq errors (cdr errors))))
1149
1150 (while (> n 0)
1151 (setq filedata (compilation-error-filedata (car errors)))
1152
1153 ;; Skip past the following errors for this file.
1154 (while (equal filedata
1155 (compilation-error-filedata
1156 (car (or errors
1157 (if reversed
1158 (error "%s the first erring file"
1159 (compilation-error-filedata-file-name
1160 filedata))
1161 (let ((compilation-error-list nil))
1162 ;; Parse some more.
1163 (compile-reinitialize-errors nil nil 2)
1164 (setq errors compilation-error-list)))
1165 (error "%s is the last erring file"
1166 (compilation-error-filedata-file-name
1167 filedata))))))
1168 (setq errors (cdr errors)))
1169
1170 (setq n (1- n)))
1171
1172 ;; Move to the following error.
1173 (goto-char (car (car (or errors
1174 (if reversed
1175 (error "This is the first erring file")
1176 (let ((compilation-error-list nil))
1177 ;; Parse the last one.
1178 (compile-reinitialize-errors nil nil 1)
1179 compilation-error-list))))))))
1180
1181 (defun compilation-previous-file (n)
1182 "Move point to the previous error for a different file than the current one."
1183 (interactive "p")
1184 (compilation-next-file (- n)))
1185
1186
1187 (defun kill-compilation ()
1188 "Kill the process made by the \\[compile] command."
1189 (interactive)
1190 (let ((buffer (compilation-find-buffer)))
1191 (if (get-buffer-process buffer)
1192 (interrupt-process (get-buffer-process buffer))
1193 (error "The compilation process is not running."))))
1194
1195
1196 ;; Parse any new errors in the compilation buffer,
1197 ;; or reparse from the beginning if the user has asked for that.
1198 (defun compile-reinitialize-errors (reparse
1199 &optional limit-search find-at-least)
1200 (save-excursion
1201 (set-buffer compilation-last-buffer)
1202 ;; If we are out of errors, or if user says "reparse",
1203 ;; discard the info we have, to force reparsing.
1204 (if (or (eq compilation-error-list t)
1205 reparse)
1206 (compilation-forget-errors))
1207 (if (and compilation-error-list
1208 (or (not limit-search)
1209 (> compilation-parsing-end limit-search))
1210 (or (not find-at-least)
1211 (>= (length compilation-error-list) find-at-least)))
1212 ;; Since compilation-error-list is non-nil, it points to a specific
1213 ;; error the user wanted. So don't move it around.
1214 nil
1215 ;; This was here for a long time (before my rewrite); why? --roland
1216 ;;(switch-to-buffer compilation-last-buffer)
1217 (set-buffer-modified-p nil)
1218 (if (< compilation-parsing-end (point-max))
1219 ;; compilation-error-list might be non-nil if we have a non-nil
1220 ;; LIMIT-SEARCH or FIND-AT-LEAST arg. In that case its value
1221 ;; records the current position in the error list, and we must
1222 ;; preserve that after reparsing.
1223 (let ((error-list-pos compilation-error-list))
1224 (funcall compilation-parse-errors-function
1225 limit-search
1226 (and find-at-least
1227 ;; We only need enough new parsed errors to reach
1228 ;; FIND-AT-LEAST errors past the current
1229 ;; position.
1230 (- find-at-least (length compilation-error-list))))
1231 ;; Remember the entire list for compilation-forget-errors. If
1232 ;; this is an incremental parse, append to previous list. If
1233 ;; we are parsing anew, compilation-forget-errors cleared
1234 ;; compilation-old-error-list above.
1235 (setq compilation-old-error-list
1236 (nconc compilation-old-error-list compilation-error-list))
1237 (if error-list-pos
1238 ;; We started in the middle of an existing list of parsed
1239 ;; errors before parsing more; restore that position.
1240 (setq compilation-error-list error-list-pos))
1241 ;; Mouse-Highlight (the first line of) each error message when the
1242 ;; mouse pointer moves over it:
1243 (let ((inhibit-read-only t)
1244 (error-list compilation-error-list))
1245 (while error-list
1246 (save-excursion
1247 (put-text-property (goto-char (car (car error-list)))
1248 (progn (end-of-line) (point))
1249 'mouse-face 'highlight))
1250 (setq error-list (cdr error-list))))
1251 )))))
1252
1253 (defun compile-mouse-goto-error (event)
1254 (interactive "e")
1255 (save-excursion
1256 (set-buffer (window-buffer (posn-window (event-end event))))
1257 (goto-char (posn-point (event-end event)))
1258
1259 (or (compilation-buffer-p (current-buffer))
1260 (error "Not in a compilation buffer."))
1261 (setq compilation-last-buffer (current-buffer))
1262 ;; `compile-reinitialize-errors' needs to see the complete filename
1263 ;; on the line where they clicked the mouse. Since it only looks
1264 ;; up to point, moving point to eol makes sure the filename is
1265 ;; visible to `compile-reinitialize-errors'.
1266 (end-of-line)
1267 (compile-reinitialize-errors nil (point))
1268
1269 ;; Move to bol; the marker for the error on this line will point there.
1270 (beginning-of-line)
1271
1272 ;; Move compilation-error-list to the elt of compilation-old-error-list
1273 ;; we want.
1274 (setq compilation-error-list compilation-old-error-list)
1275 (while (and compilation-error-list
1276 (> (point) (car (car compilation-error-list))))
1277 (setq compilation-error-list (cdr compilation-error-list)))
1278 (or compilation-error-list
1279 (error "No error to go to")))
1280 (select-window (posn-window (event-end event)))
1281 ;; Move to another window, so that next-error's window changes
1282 ;; result in the desired setup.
1283 (or (one-window-p)
1284 (progn
1285 (other-window -1)
1286 ;; other-window changed the selected buffer,
1287 ;; but we didn't want to do that.
1288 (set-buffer compilation-last-buffer)))
1289
1290 (push-mark)
1291 (next-error 1))
1292
1293 (defun compile-goto-error (&optional argp)
1294 "Visit the source for the error message point is on.
1295 Use this command in a compilation log buffer. Sets the mark at point there.
1296 \\[universal-argument] as a prefix arg means to reparse the buffer's error messages first;
1297 other kinds of prefix arguments are ignored."
1298 (interactive "P")
1299 (or (compilation-buffer-p (current-buffer))
1300 (error "Not in a compilation buffer."))
1301 (setq compilation-last-buffer (current-buffer))
1302 (compile-reinitialize-errors (consp argp) (point))
1303
1304 ;; Move to bol; the marker for the error on this line will point there.
1305 (beginning-of-line)
1306
1307 ;; Move compilation-error-list to the elt of compilation-old-error-list
1308 ;; we want.
1309 (setq compilation-error-list compilation-old-error-list)
1310 (while (and compilation-error-list
1311 (> (point) (car (car compilation-error-list))))
1312 (setq compilation-error-list (cdr compilation-error-list)))
1313
1314 ;; Move to another window, so that next-error's window changes
1315 ;; result in the desired setup.
1316 (or (one-window-p)
1317 (progn
1318 (other-window -1)
1319 ;; other-window changed the selected buffer,
1320 ;; but we didn't want to do that.
1321 (set-buffer compilation-last-buffer)))
1322
1323 (push-mark)
1324 (next-error 1))
1325
1326 ;; Return a compilation buffer.
1327 ;; If the current buffer is a compilation buffer, return it.
1328 ;; If compilation-last-buffer is set to a live buffer, use that.
1329 ;; Otherwise, look for a compilation buffer and signal an error
1330 ;; if there are none.
1331 (defun compilation-find-buffer (&optional other-buffer)
1332 (if (and (not other-buffer)
1333 (compilation-buffer-p (current-buffer)))
1334 ;; The current buffer is a compilation buffer.
1335 (current-buffer)
1336 (if (and compilation-last-buffer (buffer-name compilation-last-buffer)
1337 (compilation-buffer-p compilation-last-buffer)
1338 (or (not other-buffer) (not (eq compilation-last-buffer
1339 (current-buffer)))))
1340 compilation-last-buffer
1341 (let ((buffers (buffer-list)))
1342 (while (and buffers (or (not (compilation-buffer-p (car buffers)))
1343 (and other-buffer
1344 (eq (car buffers) (current-buffer)))))
1345 (setq buffers (cdr buffers)))
1346 (if buffers
1347 (car buffers)
1348 (or (and other-buffer
1349 (compilation-buffer-p (current-buffer))
1350 ;; The current buffer is a compilation buffer.
1351 (progn
1352 (if other-buffer
1353 (message "This is the only compilation buffer."))
1354 (current-buffer)))
1355 (error "No compilation started!")))))))
1356
1357 ;;;###autoload
1358 (defun next-error (&optional argp)
1359 "Visit next compilation error message and corresponding source code.
1360
1361 If all the error messages parsed so far have been processed already,
1362 the message buffer is checked for new ones.
1363
1364 A prefix arg specifies how many error messages to move;
1365 negative means move back to previous error messages.
1366 Just C-u as a prefix means reparse the error message buffer
1367 and start at the first error.
1368
1369 \\[next-error] normally uses the most recently started compilation or
1370 grep buffer. However, it can operate on any buffer with output from
1371 the \\[compile] and \\[grep] commands, or, more generally, on any
1372 buffer in Compilation mode or with Compilation Minor mode enabled. To
1373 specify use of a particular buffer for error messages, type
1374 \\[next-error] in that buffer.
1375
1376 Once \\[next-error] has chosen the buffer for error messages,
1377 it stays with that buffer until you use it in some other buffer which
1378 uses Compilation mode or Compilation Minor mode.
1379
1380 See variables `compilation-parse-errors-function' and
1381 \`compilation-error-regexp-alist' for customization ideas."
1382 (interactive "P")
1383 (setq compilation-last-buffer (compilation-find-buffer))
1384 (compilation-goto-locus (compilation-next-error-locus
1385 ;; We want to pass a number here only if
1386 ;; we got a numeric prefix arg, not just C-u.
1387 (and (not (consp argp))
1388 (prefix-numeric-value argp))
1389 (consp argp))))
1390 ;;;###autoload (define-key ctl-x-map "`" 'next-error)
1391
1392 (defun previous-error ()
1393 "Visit previous compilation error message and corresponding source code.
1394 This operates on the output from the \\[compile] command."
1395 (interactive)
1396 (next-error -1))
1397
1398 (defun first-error ()
1399 "Reparse the error message buffer and start at the first error.
1400 Visit corresponding source code.
1401 This operates on the output from the \\[compile] command."
1402 (interactive)
1403 (next-error '(4)))
1404
1405 (defvar compilation-skip-to-next-location nil
1406 "*If non-nil, skip multiple error messages for the same source location.")
1407
1408 (defun compilation-next-error-locus (&optional move reparse silent)
1409 "Visit next compilation error and return locus in corresponding source code.
1410 This operates on the output from the \\[compile] command.
1411 If all preparsed error messages have been processed,
1412 the error message buffer is checked for new ones.
1413
1414 Returns a cons (ERROR . SOURCE) of two markers: ERROR is a marker at the
1415 location of the error message in the compilation buffer, and SOURCE is a
1416 marker at the location in the source code indicated by the error message.
1417
1418 Optional first arg MOVE says how many error messages to move forwards (or
1419 backwards, if negative); default is 1. Optional second arg REPARSE, if
1420 non-nil, says to reparse the error message buffer and reset to the first
1421 error (plus MOVE - 1). If optional third argument SILENT is non-nil, return
1422 nil instead of raising an error if there are no more errors.
1423
1424 The current buffer should be the desired compilation output buffer."
1425 (or move (setq move 1))
1426 (compile-reinitialize-errors reparse nil (and (not reparse)
1427 (if (< move 1) 0 (1- move))))
1428 (let (next-errors next-error)
1429 (catch 'no-next-error
1430 (save-excursion
1431 (set-buffer compilation-last-buffer)
1432 ;; compilation-error-list points to the "current" error.
1433 (setq next-errors
1434 (if (> move 0)
1435 (nthcdr (1- move)
1436 compilation-error-list)
1437 ;; Zero or negative arg; we need to move back in the list.
1438 (let ((n (1- move))
1439 (i 0)
1440 (e compilation-old-error-list))
1441 ;; See how many cdrs away the current error is from the start.
1442 (while (not (eq e compilation-error-list))
1443 (setq i (1+ i)
1444 e (cdr e)))
1445 (if (> (- n) i)
1446 (error "Moved back past first error")
1447 (nthcdr (+ i n) compilation-old-error-list))))
1448 next-error (car next-errors))
1449 (while
1450 (if (null next-error)
1451 (progn
1452 (and move (/= move 1)
1453 (error (if (> move 0)
1454 "Moved past last error")
1455 "Moved back past first error"))
1456 ;; Forget existing error messages if compilation has finished.
1457 (if (not (and (get-buffer-process (current-buffer))
1458 (eq (process-status
1459 (get-buffer-process
1460 (current-buffer)))
1461 'run)))
1462 (compilation-forget-errors))
1463 (if silent
1464 (throw 'no-next-error nil)
1465 (error (concat compilation-error-message
1466 (and (get-buffer-process (current-buffer))
1467 (eq (process-status
1468 (get-buffer-process
1469 (current-buffer)))
1470 'run)
1471 " yet")))))
1472 (setq compilation-error-list (cdr next-errors))
1473 (if (null (cdr next-error))
1474 ;; This error is boring. Go to the next.
1475 t
1476 (or (markerp (cdr next-error))
1477 ;; This error has a filename/lineno pair.
1478 ;; Find the file and turn it into a marker.
1479 (let* ((fileinfo (car (cdr next-error)))
1480 (buffer (apply 'compilation-find-file
1481 (car next-error) fileinfo)))
1482 (if (null buffer)
1483 ;; We can't find this error's file.
1484 ;; Remove all errors in the same file.
1485 (progn
1486 (setq next-errors compilation-old-error-list)
1487 (while next-errors
1488 (and (consp (cdr (car next-errors)))
1489 (equal (car (cdr (car next-errors)))
1490 fileinfo)
1491 (progn
1492 (set-marker (car (car next-errors)) nil)
1493 (setcdr (car next-errors) nil)))
1494 (setq next-errors (cdr next-errors)))
1495 ;; Look for the next error.
1496 t)
1497 ;; We found the file. Get a marker for this error.
1498 ;; compilation-old-error-list is a buffer-local
1499 ;; variable, so we must be careful to extract its value
1500 ;; before switching to the source file buffer.
1501 (let ((errors compilation-old-error-list)
1502 (last-line (nth 1 (cdr next-error)))
1503 (column (nth 2 (cdr next-error))))
1504 (set-buffer buffer)
1505 (save-excursion
1506 (save-restriction
1507 (widen)
1508 (goto-line last-line)
1509 (if (and column (> column 0))
1510 ;; Columns in error msgs are 1-origin.
1511 (if compilation-error-screen-columns
1512 (move-to-column (1- column))
1513 (forward-char (1- column)))
1514 (beginning-of-line))
1515 (setcdr next-error (point-marker))
1516 ;; Make all the other error messages referring
1517 ;; to the same file have markers into the buffer.
1518 (while errors
1519 (and (consp (cdr (car errors)))
1520 (equal (car (cdr (car errors))) fileinfo)
1521 (let* ((this (nth 1 (cdr (car errors))))
1522 (column (nth 2 (cdr (car errors))))
1523 (lines (- this last-line)))
1524 (if (eq selective-display t)
1525 ;; When selective-display is t,
1526 ;; each C-m is a line boundary,
1527 ;; as well as each newline.
1528 (if (< lines 0)
1529 (re-search-backward "[\n\C-m]"
1530 nil 'end
1531 (- lines))
1532 (re-search-forward "[\n\C-m]"
1533 nil 'end
1534 lines))
1535 (forward-line lines))
1536 (if (and column (> column 1))
1537 (if compilation-error-screen-columns
1538 (move-to-column (1- column))
1539 (forward-char (1- column)))
1540 (beginning-of-line))
1541 (setq last-line this)
1542 (setcdr (car errors) (point-marker))))
1543 (setq errors (cdr errors)))))))))
1544 ;; If we didn't get a marker for this error, or this
1545 ;; marker's buffer was killed, go on to the next one.
1546 (or (not (markerp (cdr next-error)))
1547 (not (marker-buffer (cdr next-error))))))
1548 (setq next-errors compilation-error-list
1549 next-error (car next-errors)))))
1550
1551 (if compilation-skip-to-next-location
1552 ;; Skip over multiple error messages for the same source location,
1553 ;; so the next C-x ` won't go to an error in the same place.
1554 (while (and compilation-error-list
1555 (equal (cdr (car compilation-error-list))
1556 (cdr next-error)))
1557 (setq compilation-error-list (cdr compilation-error-list))))
1558
1559 ;; We now have a marker for the position of the error source code.
1560 ;; NEXT-ERROR is a cons (ERROR . SOURCE) of two markers.
1561 next-error))
1562
1563 (defun compilation-goto-locus (next-error)
1564 "Jump to an error locus returned by `compilation-next-error-locus'.
1565 Takes one argument, a cons (ERROR . SOURCE) of two markers.
1566 Selects a window with point at SOURCE, with another window displaying ERROR."
1567 (if (and (window-dedicated-p (selected-window))
1568 (eq (selected-window) (frame-root-window)))
1569 (switch-to-buffer-other-frame (marker-buffer (cdr next-error)))
1570 (switch-to-buffer (marker-buffer (cdr next-error))))
1571 (goto-char (cdr next-error))
1572 ;; If narrowing got in the way of
1573 ;; going to the right place, widen.
1574 (or (= (point) (marker-position (cdr next-error)))
1575 (progn
1576 (widen)
1577 (goto-char (cdr next-error))))
1578
1579 ;; Show compilation buffer in other window, scrolled to this error.
1580 (let* ((pop-up-windows t)
1581 ;; Use an existing window if it is in a visible frame.
1582 (w (or (get-buffer-window (marker-buffer (car next-error)) 'visible)
1583 ;; Pop up a window.
1584 (display-buffer (marker-buffer (car next-error))))))
1585 (set-window-point w (car next-error))
1586 (set-window-start w (car next-error))
1587 (compilation-set-window-height w)))
1588 \f
1589 ;; Find a buffer for file FILENAME.
1590 ;; Search the directories in compilation-search-path.
1591 ;; A nil in compilation-search-path means to try the
1592 ;; current directory, which is passed in DIR.
1593 ;; If FILENAME is not found at all, ask the user where to find it.
1594 ;; Pop up the buffer containing MARKER and scroll to MARKER if we ask the user.
1595 (defun compilation-find-file (marker filename dir &rest formats)
1596 (or formats (setq formats '("%s")))
1597 (let ((dirs compilation-search-path)
1598 buffer thisdir fmts name)
1599 (if (file-name-absolute-p filename)
1600 ;; The file name is absolute. Use its explicit directory as
1601 ;; the first in the search path, and strip it from FILENAME.
1602 (setq filename (abbreviate-file-name (expand-file-name filename))
1603 dirs (cons (file-name-directory filename) dirs)
1604 filename (file-name-nondirectory filename)))
1605 ;; Now search the path.
1606 (while (and dirs (null buffer))
1607 (setq thisdir (or (car dirs) dir)
1608 fmts formats)
1609 ;; For each directory, try each format string.
1610 (while (and fmts (null buffer))
1611 (setq name (expand-file-name (format (car fmts) filename) thisdir)
1612 buffer (and (file-exists-p name)
1613 (find-file-noselect name))
1614 fmts (cdr fmts)))
1615 (setq dirs (cdr dirs)))
1616 (or buffer
1617 ;; The file doesn't exist.
1618 ;; Ask the user where to find it.
1619 ;; If he hits C-g, then the next time he does
1620 ;; next-error, he'll skip past it.
1621 (let* ((pop-up-windows t)
1622 (w (display-buffer (marker-buffer marker))))
1623 (set-window-point w marker)
1624 (set-window-start w marker)
1625 (let ((name (expand-file-name
1626 (read-file-name
1627 (format "Find this error in: (default %s) "
1628 filename)
1629 dir filename t))))
1630 (if (file-directory-p name)
1631 (setq name (expand-file-name filename name)))
1632 (and (file-exists-p name)
1633 (find-file-noselect name)))))))
1634
1635 ;; Set compilation-error-list to nil, and unchain the markers that point to the
1636 ;; error messages and their text, so that they no longer slow down gap motion.
1637 ;; This would happen anyway at the next garbage collection, but it is better to
1638 ;; do it right away.
1639 (defun compilation-forget-errors ()
1640 (while compilation-old-error-list
1641 (let ((next-error (car compilation-old-error-list)))
1642 (set-marker (car next-error) nil)
1643 (if (markerp (cdr next-error))
1644 (set-marker (cdr next-error) nil)))
1645 (setq compilation-old-error-list (cdr compilation-old-error-list)))
1646 (setq compilation-error-list nil
1647 compilation-directory-stack (list default-directory)
1648 compilation-parsing-end 1)
1649 ;; Remove the highlighting added by compile-reinitialize-errors:
1650 (let ((inhibit-read-only t))
1651 (remove-text-properties (point-min) (point-max) '(mouse-face highlight)))
1652 )
1653
1654
1655 ;; This function is not needed any more by compilation mode.
1656 ;; Does anyone else need it or can it be deleted?
1657 (defun count-regexp-groupings (regexp)
1658 "Return the number of \\( ... \\) groupings in REGEXP (a string)."
1659 (let ((groupings 0)
1660 (len (length regexp))
1661 (i 0)
1662 c)
1663 (while (< i len)
1664 (setq c (aref regexp i)
1665 i (1+ i))
1666 (cond ((= c ?\[)
1667 ;; Find the end of this [...].
1668 (while (and (< i len)
1669 (not (= (aref regexp i) ?\])))
1670 (setq i (1+ i))))
1671 ((= c ?\\)
1672 (if (< i len)
1673 (progn
1674 (setq c (aref regexp i)
1675 i (1+ i))
1676 (if (= c ?\))
1677 ;; We found the end of a grouping,
1678 ;; so bump our counter.
1679 (setq groupings (1+ groupings))))))))
1680 groupings))
1681
1682 (defvar compilation-current-file nil
1683 "Used by compilation-parse-errors to store filename for file being compiled")
1684
1685 ;; This variable is not used as a global variable. It's defined here just to
1686 ;; shut up the byte compiler. It's bound and used by compilation-parse-errors
1687 ;; and set by compile-collect-regexps.
1688 (defvar compilation-regexps nil)
1689
1690 (defun compilation-parse-errors (limit-search find-at-least)
1691 "Parse the current buffer as grep, cc, lint or other error messages.
1692 See variable `compilation-parse-errors-function' for the interface it uses."
1693 (setq compilation-error-list nil)
1694 (message "Parsing error messages...")
1695 (if (null compilation-error-regexp-alist)
1696 (error "compilation-error-regexp-alist is empty!"))
1697 (let* ((compilation-regexps nil) ; Variable set by compile-collect-regexps.
1698 (default-directory (car compilation-directory-stack))
1699 (found-desired nil)
1700 (compilation-num-errors-found 0)
1701 ;; Set up now the expanded, abbreviated directory variables
1702 ;; that compile-abbreviate-directory will need, so we can
1703 ;; compute them just once here.
1704 (orig (abbreviate-file-name default-directory))
1705 (orig-expanded (abbreviate-file-name
1706 (file-truename default-directory)))
1707 (parent-expanded (abbreviate-file-name
1708 (expand-file-name "../" orig-expanded))))
1709
1710 ;; Make a list of all the regexps. Each element has the form
1711 ;; (REGEXP TYPE IDX1 IDX2 ...)
1712 ;; where TYPE is one of leave, enter, file, error or nomessage.
1713 (compile-collect-regexps 'leave compilation-leave-directory-regexp-alist)
1714 (compile-collect-regexps 'enter compilation-enter-directory-regexp-alist)
1715 (compile-collect-regexps 'file compilation-file-regexp-alist)
1716 (compile-collect-regexps 'nomessage compilation-nomessage-regexp-alist)
1717 (compile-collect-regexps 'error compilation-error-regexp-alist)
1718
1719 ;; Don't reparse messages already seen at last parse.
1720 (goto-char compilation-parsing-end)
1721 (when (and (bobp)
1722 (eq major-mode 'compilation-mode))
1723 (setq compilation-current-file nil) ; No current file at start.
1724 ;; Don't parse the first two lines as error messages.
1725 ;; This matters for grep.
1726 (forward-line 2))
1727
1728 ;; Parse messages.
1729 (while (not (or found-desired (eobp)))
1730 (let ((this compilation-regexps) (prev nil) (alist nil) type)
1731 ;; Go through the regular expressions. If a match is found,
1732 ;; variable alist is set to the corresponding alist and the
1733 ;; matching regexp is moved to the front of compilation-regexps
1734 ;; to make it match faster next time.
1735 (while (and this (null alist))
1736 (if (not (looking-at (car (car this))))
1737 (progn (setq prev this) ; No match, go to next.
1738 (setq this (cdr this)))
1739 (setq alist (cdr (car this))) ; Got a match.
1740 ;;; (if prev ; If not the first regexp,
1741 ;;; (progn ; move it to the front.
1742 ;;; (setcdr prev (cdr this))
1743 ;;; (setcdr this compilation-regexps)
1744 ;;; (setq compilation-regexps this)))
1745 ))
1746 (if (and alist ; Seen a match and not to
1747 (not (eq (setq type (car alist)) 'nomessage))) ; be ignored.
1748 (let* ((end-of-match (match-end 0))
1749 (filename
1750 (compile-buffer-substring (car (setq alist (cdr alist)))))
1751 stack)
1752 (if (eq type 'error) ; error message
1753 (let* ((linenum (if (numberp (car (setq alist (cdr alist))))
1754 (string-to-int
1755 (compile-buffer-substring (car alist)))
1756 ;; (car alist) is not a number, must be a
1757 ;; function that is called below to return
1758 ;; an error position descriptor.
1759 (car alist)))
1760 ;; Convert to integer later if linenum not a function.
1761 (column (compile-buffer-substring
1762 (car (setq alist (cdr alist)))))
1763 this-error)
1764
1765 ;; Check that we have a file name.
1766 (or filename
1767 ;; No file name in message, we must have seen it before
1768 (setq filename compilation-current-file)
1769 (error "\
1770 An error message with no file name and no file name has been seen earlier."))
1771
1772 ;; Check for a comint-file-name-prefix and prepend it if
1773 ;; appropriate. (This is very useful for
1774 ;; compilation-minor-mode in an rlogin-mode buffer.)
1775 (and (boundp 'comint-file-name-prefix)
1776 ;; If file name is relative, default-directory will
1777 ;; already contain the comint-file-name-prefix (done
1778 ;; by compile-abbreviate-directory).
1779 (file-name-absolute-p filename)
1780 (setq filename
1781 (concat comint-file-name-prefix filename)))
1782
1783 ;; Some compilers (e.g. Sun's java compiler, reportedly)
1784 ;; produce bogus file names like "./bar//foo.c" for file
1785 ;; "bar/foo.c"; expand-file-name will collapse these into
1786 ;; "/foo.c" and fail to find the appropriate file. So we
1787 ;; look for doubled slashes in the file name and fix them
1788 ;; up in the buffer.
1789 (setq filename (command-line-normalize-file-name filename))
1790
1791 (setq filename
1792 (cons filename (cons default-directory (cdr alist))))
1793
1794 ;; Locate the erring file and line.
1795 ;; Make this-error a new elt for compilation-error-list,
1796 ;; giving a marker for the current compilation buffer
1797 ;; location, and the file and line number of the error.
1798 ;; Save, as the start of the error, the beginning of the
1799 ;; line containing the match.
1800 (setq this-error
1801 (if (numberp linenum)
1802 (list (point-marker) filename linenum
1803 (and column (string-to-int column)))
1804 ;; If linenum is not a number then it must be
1805 ;; a function returning an error position
1806 ;; descriptor or nil (meaning no position).
1807 (save-excursion
1808 (funcall linenum filename column))))
1809
1810 ;; We have an error position descriptor.
1811 ;; If we have found as many new errors as the user
1812 ;; wants, or if we are past the buffer position he
1813 ;; indicated, then we continue to parse until we have
1814 ;; seen all consecutive errors in the same file. This
1815 ;; means that all the errors of a source file will be
1816 ;; seen in one parsing run, so that the error positions
1817 ;; will be recorded as markers in the source file
1818 ;; buffer that will move when the buffer is changed.
1819 (if (and this-error
1820 compilation-error-list ; At least one previous.
1821 (or (and find-at-least
1822 (>= compilation-num-errors-found
1823 find-at-least))
1824 (and limit-search
1825 (>= end-of-match limit-search)))
1826 (not (equal ; Same filename?
1827 (car (cdr (car compilation-error-list)))
1828 (car (cdr this-error)))))
1829 ;; We are past the limits and the last error
1830 ;; parsed, didn't belong to the same source file
1831 ;; as the earlier ones i.e. we have seen all the
1832 ;; errors belonging to the earlier file. We don't
1833 ;; add the error just parsed so that the next
1834 ;; parsing run can get it and the following errors
1835 ;; in the same file all at once.
1836 (setq found-desired t)
1837
1838 (goto-char end-of-match) ; Prepare for next message.
1839 ;; Don't add the same source line more than once.
1840 (and this-error
1841 (not (and
1842 compilation-error-list
1843 (equal (cdr (car compilation-error-list))
1844 (cdr this-error))))
1845 (setq compilation-error-list
1846 (cons this-error compilation-error-list)
1847 compilation-num-errors-found
1848 (1+ compilation-num-errors-found)))))
1849
1850 ;; Not an error message.
1851 (if (eq type `file) ; Change current file.
1852 (and filename (setq compilation-current-file filename))
1853 ;; Enter or leave directory.
1854 (setq stack compilation-directory-stack)
1855 (and filename
1856 (file-directory-p
1857 (setq filename
1858 ;; The directory name in the message
1859 ;; is a truename. Try to convert it to a form
1860 ;; like what the user typed in.
1861 (compile-abbreviate-directory
1862 (file-name-as-directory
1863 (expand-file-name filename))
1864 orig orig-expanded parent-expanded)))
1865 (if (eq type 'leave)
1866 (while (and stack
1867 (not (string-equal (car stack)
1868 filename)))
1869 (setq stack (cdr stack)))
1870 (setq compilation-directory-stack
1871 (cons filename compilation-directory-stack)
1872 default-directory filename)))
1873 (and (eq type 'leave)
1874 stack
1875 (setq compilation-directory-stack (cdr stack))
1876 (setq stack (car compilation-directory-stack))
1877 (setq default-directory stack)))
1878 (goto-char end-of-match) ; Prepare to look at next message.
1879 (and limit-search (>= end-of-match limit-search)
1880 ;; The user wanted a specific error, and we're past it.
1881 ;; We do this check here rather than at the end of the
1882 ;; loop because if the last thing seen is an error
1883 ;; message, we must carefully discard the last error
1884 ;; when it is the first in a new file (see above in
1885 ;; the error-message case)
1886 (setq found-desired t)))
1887
1888 ;; Go to before the last character in the message so that we will
1889 ;; see the next line also when the message ended at end of line.
1890 ;; When we ignore the last error message above, this will
1891 ;; cancel the effect of forward-line below so that point
1892 ;; doesn't move.
1893 (forward-char -1)
1894
1895 ;; Is this message necessary any more? Parsing is now so fast
1896 ;; that you might not need to know how it proceeds.
1897 (message
1898 "Parsing error messages...%d found. %.0f%% of buffer seen."
1899 compilation-num-errors-found
1900 ;; Use floating-point because (* 100 (point)) frequently
1901 ;; exceeds the range of Emacs Lisp integers.
1902 (/ (* 100.0 (point)) (point-max)))
1903 ))
1904
1905 (forward-line 1))) ; End of while loop. Look at next line.
1906
1907 (setq compilation-parsing-end (point))
1908 (setq compilation-error-list (nreverse compilation-error-list))
1909 ;;; (message "Parsing error messages...done. %d found. %.0f%% of buffer seen."
1910 ;;; compilation-num-errors-found
1911 ;;; (/ (* 100.0 (point)) (point-max)))
1912 (message "Parsing error messages...done.")))
1913
1914 (defun compile-collect-regexps (type this)
1915 ;; Add elements to variable compilation-regexps that is bound in
1916 ;; compilation-parse-errors.
1917 (and (not (eq this t))
1918 (while this
1919 (setq compilation-regexps
1920 (cons (cons (car (car this)) (cons type (cdr (car this))))
1921 compilation-regexps))
1922 (setq this (cdr this)))))
1923
1924 (defun compile-buffer-substring (index)
1925 ;; Get substring matched by INDEXth subexpression.
1926 (if index
1927 (let ((beg (match-beginning index)))
1928 (if beg (buffer-substring beg (match-end index))))))
1929
1930 ;; If directory DIR is a subdir of ORIG or of ORIG's parent,
1931 ;; return a relative name for it starting from ORIG or its parent.
1932 ;; ORIG-EXPANDED is an expanded version of ORIG.
1933 ;; PARENT-EXPANDED is an expanded version of ORIG's parent.
1934 ;; Those two args could be computed here, but we run faster by
1935 ;; having the caller compute them just once.
1936 (defun compile-abbreviate-directory (dir orig orig-expanded parent-expanded)
1937 ;; Apply canonical abbreviations to DIR first thing.
1938 ;; Those abbreviations are already done in the other arguments passed.
1939 (setq dir (abbreviate-file-name dir))
1940
1941 ;; Check for a comint-file-name-prefix and prepend it if appropriate.
1942 ;; (This is very useful for compilation-minor-mode in an rlogin-mode
1943 ;; buffer.)
1944 (if (boundp 'comint-file-name-prefix)
1945 (setq dir (concat comint-file-name-prefix dir)))
1946
1947 (if (and (> (length dir) (length orig-expanded))
1948 (string= orig-expanded
1949 (substring dir 0 (length orig-expanded))))
1950 (setq dir
1951 (concat orig
1952 (substring dir (length orig-expanded)))))
1953 (if (and (> (length dir) (length parent-expanded))
1954 (string= parent-expanded
1955 (substring dir 0 (length parent-expanded))))
1956 (setq dir
1957 (concat (file-name-directory
1958 (directory-file-name orig))
1959 (substring dir (length parent-expanded)))))
1960 dir)
1961
1962 (provide 'compile)
1963
1964 ;;; compile.el ends here