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