(compilation-error-regexp-alist): Fix bug in Borland
[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 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
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
25 ;;; Commentary:
26
27 ;; This package provides the compile and grep facilities documented in
28 ;; the Emacs user's manual.
29
30 ;;; Code:
31
32 ;;;###autoload
33 (defvar compilation-mode-hook nil
34 "*List of hook functions run by `compilation-mode' (see `run-hooks').")
35
36 ;;;###autoload
37 (defvar compilation-window-height nil
38 "*Number of lines in a compilation window. If nil, use Emacs default.")
39
40 (defvar compilation-error-list nil
41 "List of error message descriptors for visiting erring functions.
42 Each error descriptor is a cons (or nil). Its car is a marker pointing to
43 an error message. If its cdr is a marker, it points to the text of the
44 line the message is about. If its cdr is a cons, it is a list
45 \(\(DIRECTORY . FILE\) LINE [COLUMN]\). Or its cdr may be nil if that
46 error is not interesting.
47
48 The value may be t instead of a list; this means that the buffer of
49 error messages should be reparsed the next time the list of errors is wanted.
50
51 Some other commands (like `diff') use this list to control the error
52 message tracking facilites; if you change its structure, you should make
53 sure you also change those packages. Perhaps it is better not to change
54 it at all.")
55
56 (defvar compilation-old-error-list nil
57 "Value of `compilation-error-list' after errors were parsed.")
58
59 (defvar compilation-parse-errors-function 'compilation-parse-errors
60 "Function to call to parse error messages from a compilation.
61 It takes args LIMIT-SEARCH and FIND-AT-LEAST.
62 If LIMIT-SEARCH is non-nil, don't bother parsing past that location.
63 If FIND-AT-LEAST is non-nil, don't bother parsing after finding that
64 many new errors.
65 It should read in the source files which have errors and set
66 `compilation-error-list' to a list with an element for each error message
67 found. See that variable for more info.")
68
69 ;;;###autoload
70 (defvar compilation-buffer-name-function nil
71 "Function to compute the name of a compilation buffer.
72 The function receives one argument, the name of the major mode of the
73 compilation buffer. It should return a string.
74 nil means compute the name with `(concat \"*\" (downcase major-mode) \"*\")'.")
75
76 ;;;###autoload
77 (defvar compilation-finish-function nil
78 "*Function to call when a compilation process finishes.
79 It is called with two arguments: the compilation buffer, and a string
80 describing how the process finished.")
81
82 (defvar compilation-last-buffer nil
83 "The most recent compilation buffer.
84 A buffer becomes most recent when its compilation is started
85 or when it is used with \\[next-error] or \\[compile-goto-error].")
86
87 (defvar compilation-in-progress nil
88 "List of compilation processes now running.")
89 (or (assq 'compilation-in-progress minor-mode-alist)
90 (setq minor-mode-alist (cons '(compilation-in-progress " Compiling")
91 minor-mode-alist)))
92
93 (defvar compilation-parsing-end nil
94 "Position of end of buffer when last error messages were parsed.")
95
96 (defvar compilation-error-message "No more errors"
97 "Message to print when no more matches are found.")
98
99 (defvar compilation-num-errors-found)
100
101 (defvar compilation-error-regexp-alist
102 '(
103 ;; NOTE! See also grep-regexp-alist, below.
104
105 ;; 4.3BSD grep, cc, lint pass 1:
106 ;; /usr/src/foo/foo.c(8): warning: w may be used before set
107 ;; or GNU utilities:
108 ;; foo.c:8: error message
109 ;; or HP-UX 7.0 fc:
110 ;; foo.f :16 some horrible error message
111 ;; or Borland C++:
112 ;; Error ping.c 15: Unable to open include file 'sys/types.h'
113 ;; Warning ping.c 68: Call to function 'func' with no prototype
114 ;;
115 ;; We refuse to match file:number:number
116 ;; because we want to leave that for another case (see below, GNAT).
117 ;;
118 ;; We'll insist that the number be followed by a colon or closing
119 ;; paren, because otherwise this matches just about anything
120 ;; containing a number with spaces around it.
121 ("\n\\(Error \\|Warning \\)?[ \t]*\\([^:( \t\n]+\\)\
122 \[:(]?[ \t]*\\([0-9]+\\)\\([) \t]\\|:[^0-9\n]\\)" 2 3)
123
124 ;; 4.3BSD lint pass 2
125 ;; strcmp: variable # of args. llib-lc(359) :: /usr/src/foo/foo.c(8)
126 ("[ \t:]\\([^:( \t\n]+\\)[:(](+[ \t]*\\([0-9]+\\))[:) \t]*$" 1 2)
127
128 ;; 4.3BSD lint pass 3
129 ;; bloofle defined( /users/wolfgang/foo.c(4) ), but never used
130 ;; This used to be
131 ;; ("[ \t(]+\\([^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]+" 1 2)
132 ;; which is regexp Impressionism - it matches almost anything!
133 ("([ \t]*\\([^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\))" 1 2)
134
135 ;; Ultrix 3.0 f77:
136 ;; fort: Severe: addstf.f, line 82: Missing operator or delimiter symbol
137 ("\nfort: [^:\n]*: \\([^ \n]*\\), line \\([0-9]+\\):" 1 2)
138 ;; Error on line 3 of t.f: Execution error unclassifiable statement
139 ;; Unknown who does this:
140 ;; Line 45 of "foo.c": bloofel undefined
141 ;; Absoft FORTRAN 77 Compiler 3.1.3
142 ;; error on line 19 of fplot.f: spelling error?
143 ;; warning on line 17 of fplot.f: data type is undefined for variable d
144 ("\\(\n\\|on \\)[Ll]ine[ \t]+\\([0-9]+\\)[ \t]+\
145 of[ \t]+\"?\\([^\":\n]+\\)\"?:" 3 2)
146
147 ;; Apollo cc, 4.3BSD fc:
148 ;; "foo.f", line 3: Error: syntax error near end of statement
149 ;; IBM RS6000:
150 ;; "vvouch.c", line 19.5: 1506-046 (S) Syntax error.
151 ;; Unknown compiler:
152 ;; File "foobar.ml", lines 5-8, characters 20-155: blah blah
153 ;; Microtec mcc68k:
154 ;; "foo.c", line 32 pos 1; (E) syntax error; unexpected symbol: "lossage"
155 ;; GNAT (as of July 94):
156 ;; "foo.adb", line 2(11): warning: file name does not match ...
157 ("\"\\([^,\" \n\t]+\\)\", lines? \\([0-9]+\\)[:., (-]" 1 2)
158
159 ;; MIPS RISC CC - the one distributed with Ultrix:
160 ;; ccom: Error: foo.c, line 2: syntax error
161 ;; DEC AXP OSF/1 cc
162 ;; /usr/lib/cmplrs/cc/cfe: Error: foo.c: 1: blah blah
163 ("rror: \\([^,\" \n\t]+\\)[,:] \\(line \\)?\\([0-9]+\\):" 1 3)
164
165 ;; IBM AIX PS/2 C version 1.1:
166 ;; ****** Error number 140 in line 8 of file errors.c ******
167 ("in line \\([0-9]+\\) of file \\([^ \n]+[^. \n]\\)\\.? " 2 1)
168 ;; IBM AIX lint is too painful to do right this way. File name
169 ;; prefixes entire sections rather than being on each line.
170
171 ;; Lucid Compiler, lcc 3.x
172 ;; E, file.cc(35,52) Illegal operation on pointers
173 ("\n[EW], \\([^(\n]*\\)(\\([0-9]+\\),[ \t]*\\([0-9]+\\)" 1 2 3)
174
175 ;; GNAT compiler v1.82
176 ;; foo.adb:2:1: Unit name does not match file name
177 ("\n\\([^ \n\t:]+\\):\\([0-9]+\\):\\([0-9]+\\)[: \t]" 1 2 3)
178 )
179 "Alist that specifies how to match errors in compiler output.
180 Each element has the form (REGEXP FILE-IDX LINE-IDX [COLUMN-IDX]).
181 If REGEXP matches, the FILE-IDX'th subexpression gives the file name, and
182 the LINE-IDX'th subexpression gives the line number. If COLUMN-IDX is
183 given, the COLUMN-IDX'th subexpression gives the column number on that line.")
184
185 (defvar compilation-read-command t
186 "If not nil, M-x compile reads the compilation command to use.
187 Otherwise, M-x compile just uses the value of `compile-command'.")
188
189 (defvar compilation-ask-about-save t
190 "If not nil, M-x compile asks which buffers to save before compiling.
191 Otherwise, it saves all modified buffers without asking.")
192
193 (defvar grep-regexp-alist
194 '(("^\\([^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2))
195 "Regexp used to match grep hits. See `compilation-error-regexp-alist'.")
196
197 (defvar grep-command "grep -n "
198 "Last grep command used in \\{grep}; default for next grep.")
199
200 ;;;###autoload
201 (defvar compilation-search-path '(nil)
202 "*List of directories to search for source files named in error messages.
203 Elements should be directory names, not file names of directories.
204 nil as an element means to try the default directory.")
205
206 (defvar compile-command "make -k "
207 "Last shell command used to do a compilation; default for next compilation.
208
209 Sometimes it is useful for files to supply local values for this variable.
210 You might also use mode hooks to specify it in certain modes, like this:
211
212 (setq c-mode-hook
213 '(lambda () (or (file-exists-p \"makefile\") (file-exists-p \"Makefile\")
214 (progn (make-local-variable 'compile-command)
215 (setq compile-command
216 (concat \"make -k \"
217 buffer-file-name))))))")
218
219 (defvar compilation-enter-directory-regexp
220 ": Entering directory `\\(.*\\)'$"
221 "Regular expression matching lines that indicate a new current directory.
222 This must contain one \\(, \\) pair around the directory name.
223
224 The default value matches lines printed by the `-w' option of GNU Make.")
225
226 (defvar compilation-leave-directory-regexp
227 ": Leaving directory `\\(.*\\)'$"
228 "Regular expression matching lines that indicate restoring current directory.
229 This may contain one \\(, \\) pair around the name of the directory
230 being moved from. If it does not, the last directory entered \(by a
231 line matching `compilation-enter-directory-regexp'\) is assumed.
232
233 The default value matches lines printed by the `-w' option of GNU Make.")
234
235 (defvar compilation-directory-stack nil
236 "Stack of previous directories for `compilation-leave-directory-regexp'.
237 The head element is the directory the compilation was started in.")
238
239 ;; History of compile commands.
240 (defvar compile-history nil)
241 ;; History of grep commands.
242 (defvar grep-history nil)
243
244 ;;;###autoload
245 (defun compile (command)
246 "Compile the program including the current buffer. Default: run `make'.
247 Runs COMMAND, a shell command, in a separate process asynchronously
248 with output going to the buffer `*compilation*'.
249
250 You can then use the command \\[next-error] to find the next error message
251 and move to the source code that caused it.
252
253 To run more than one compilation at once, start one and rename the
254 \`*compilation*' buffer to some other name with \\[rename-buffer].
255 Then start the next one.
256
257 The name used for the buffer is actually whatever is returned by
258 the function in `compilation-buffer-name-function', so you can set that
259 to a function that generates a unique name."
260 (interactive
261 (if compilation-read-command
262 (list (read-from-minibuffer "Compile command: "
263 compile-command nil nil
264 '(compile-history . 1)))
265 (list compile-command)))
266 (setq compile-command command)
267 (save-some-buffers (not compilation-ask-about-save) nil)
268 (compile-internal compile-command "No more errors"))
269
270 ;;;###autoload
271 (defun grep (command-args)
272 "Run grep, with user-specified args, and collect output in a buffer.
273 While grep runs asynchronously, you can use the \\[next-error] command
274 to find the text that grep hits refer to.
275
276 This command uses a special history list for its arguments, so you can
277 easily repeat a grep command."
278 (interactive
279 (list (read-from-minibuffer "Run grep (like this): "
280 grep-command nil nil 'grep-history)))
281 (compile-internal (concat command-args " /dev/null")
282 "No more grep hits" "grep"
283 ;; Give it a simpler regexp to match.
284 nil grep-regexp-alist))
285
286 (defun compile-internal (command error-message
287 &optional name-of-mode parser regexp-alist
288 name-function)
289 "Run compilation command COMMAND (low level interface).
290 ERROR-MESSAGE is a string to print if the user asks to see another error
291 and there are no more errors. Third argument NAME-OF-MODE is the name
292 to display as the major mode in the compilation buffer.
293
294 Fourth arg PARSER is the error parser function (nil means the default). Fifth
295 arg REGEXP-ALIST is the error message regexp alist to use (nil means the
296 default). Sixth arg NAME-FUNCTION is a function called to name the buffer (nil
297 means the default). The defaults for these variables are the global values of
298 \`compilation-parse-errors-function', `compilation-error-regexp-alist', and
299 \`compilation-buffer-name-function', respectively.
300
301 Returns the compilation buffer created."
302 (let (outbuf)
303 (save-excursion
304 (or name-of-mode
305 (setq name-of-mode "Compilation"))
306 (setq outbuf
307 (get-buffer-create
308 (funcall (or name-function compilation-buffer-name-function
309 (function (lambda (mode)
310 (concat "*" (downcase mode) "*"))))
311 name-of-mode)))
312 (set-buffer outbuf)
313 (let ((comp-proc (get-buffer-process (current-buffer))))
314 (if comp-proc
315 (if (or (not (eq (process-status comp-proc) 'run))
316 (yes-or-no-p
317 (format "A %s process is running; kill it? "
318 name-of-mode)))
319 (condition-case ()
320 (progn
321 (interrupt-process comp-proc)
322 (sit-for 1)
323 (delete-process comp-proc))
324 (error nil))
325 (error "Cannot have two processes in `%s' at once"
326 (buffer-name))
327 )))
328 ;; In case the compilation buffer is current, make sure we get the global
329 ;; values of compilation-error-regexp-alist, etc.
330 (kill-all-local-variables))
331 (let ((regexp-alist (or regexp-alist compilation-error-regexp-alist))
332 (parser (or parser compilation-parse-errors-function))
333 (thisdir default-directory)
334 outwin)
335 (save-excursion
336 ;; Clear out the compilation buffer and make it writable.
337 ;; Change its default-directory to the directory where the compilation
338 ;; will happen, and insert a `cd' command to indicate this.
339 (set-buffer outbuf)
340 (setq buffer-read-only nil)
341 (erase-buffer)
342 (setq default-directory thisdir)
343 (insert "cd " thisdir "\n" command "\n")
344 (set-buffer-modified-p nil))
345 ;; If we're already in the compilation buffer, go to the end
346 ;; of the buffer, so point will track the compilation output.
347 (if (eq outbuf (current-buffer))
348 (goto-char (point-max)))
349 ;; Pop up the compilation buffer.
350 (setq outwin (display-buffer outbuf))
351 (save-excursion
352 (set-buffer outbuf)
353 (compilation-mode)
354 (buffer-disable-undo (current-buffer))
355 ;; (setq buffer-read-only t) ;;; Non-ergonomic.
356 (set (make-local-variable 'compilation-parse-errors-function) parser)
357 (set (make-local-variable 'compilation-error-message) error-message)
358 (set (make-local-variable 'compilation-error-regexp-alist) regexp-alist)
359 (setq default-directory thisdir
360 compilation-directory-stack (list default-directory))
361 (set-window-start outwin (point-min))
362 (setq mode-name name-of-mode)
363 (or (eq outwin (selected-window))
364 (set-window-point outwin (point-min)))
365 (and compilation-window-height
366 (= (window-width outwin) (frame-width))
367 (let ((w (selected-window)))
368 (unwind-protect
369 (progn
370 (select-window outwin)
371 (enlarge-window (- compilation-window-height
372 (window-height))))
373 (select-window w))))
374 ;; Start the compilation.
375 (if (fboundp 'start-process)
376 (let ((proc (start-process-shell-command (downcase mode-name)
377 outbuf
378 command)))
379 (set-process-sentinel proc 'compilation-sentinel)
380 (set-process-filter proc 'compilation-filter)
381 (set-marker (process-mark proc) (point) outbuf)
382 (setq compilation-in-progress
383 (cons proc compilation-in-progress)))
384 ;; No asynchronous processes available
385 (message (format "Executing `%s'..." command))
386 (let ((status (call-process shell-file-name nil outbuf nil "-c"
387 command))))
388 (message (format "Executing `%s'...done" command)))))
389 ;; Make it so the next C-x ` will use this buffer.
390 (setq compilation-last-buffer outbuf)))
391
392 (defvar compilation-minor-mode-map
393 (let ((map (make-sparse-keymap)))
394 (define-key map [mouse-2] 'compile-mouse-goto-error)
395 (define-key map "\C-c\C-c" 'compile-goto-error)
396 (define-key map "\C-c\C-k" 'kill-compilation)
397 (define-key map "\M-n" 'compilation-next-error)
398 (define-key map "\M-p" 'compilation-previous-error)
399 (define-key map "\M-{" 'compilation-previous-file)
400 (define-key map "\M-}" 'compilation-next-file)
401 map)
402 "Keymap for `compilation-minor-mode'.")
403
404 (defvar compilation-mode-map
405 (let ((map (cons 'keymap compilation-minor-mode-map)))
406 (define-key map " " 'scroll-up)
407 (define-key map "\^?" 'scroll-down)
408 map)
409 "Keymap for compilation log buffers.
410 `compilation-minor-mode-map' is a cdr of this.")
411
412 (defun compilation-mode ()
413 "Major mode for compilation log buffers.
414 \\<compilation-mode-map>To visit the source for a line-numbered error,
415 move point to the error message line and type \\[compile-goto-error].
416 To kill the compilation, type \\[kill-compilation].
417
418 Runs `compilation-mode-hook' with `run-hooks' (which see)."
419 (interactive)
420 (fundamental-mode)
421 (use-local-map compilation-mode-map)
422 (setq major-mode 'compilation-mode
423 mode-name "Compilation")
424 (compilation-setup)
425 (run-hooks 'compilation-mode-hook))
426
427 ;; Prepare the buffer for the compilation parsing commands to work.
428 (defun compilation-setup ()
429 ;; Make the buffer's mode line show process state.
430 (setq mode-line-process '(":%s"))
431 (set (make-local-variable 'compilation-error-list) nil)
432 (set (make-local-variable 'compilation-old-error-list) nil)
433 (set (make-local-variable 'compilation-parsing-end) 1)
434 (set (make-local-variable 'compilation-directory-stack) nil)
435 (setq compilation-last-buffer (current-buffer)))
436
437 (defvar compilation-minor-mode nil
438 "Non-nil when in compilation-minor-mode.
439 In this minor mode, all the error-parsing commands of the
440 Compilation major mode are available.")
441 (make-variable-buffer-local 'compilation-minor-mode)
442
443 (or (assq 'compilation-minor-mode minor-mode-alist)
444 (setq minor-mode-alist (cons '(compilation-minor-mode " Compilation")
445 minor-mode-alist)))
446 (or (assq 'compilation-minor-mode minor-mode-map-alist)
447 (setq minor-mode-map-alist (cons (cons 'compilation-minor-mode
448 compilation-minor-mode-map)
449 minor-mode-map-alist)))
450
451 ;;;###autoload
452 (defun compilation-minor-mode (&optional arg)
453 "Toggle compilation minor mode.
454 With arg, turn compilation mode on if and only if arg is positive.
455 See `compilation-mode'."
456 (interactive "P")
457 (if (setq compilation-minor-mode (if (null arg)
458 (null compilation-minor-mode)
459 (> (prefix-numeric-value arg) 0)))
460 (compilation-setup)))
461
462 ;; Called when compilation process changes state.
463 (defun compilation-sentinel (proc msg)
464 "Sentinel for compilation buffers."
465 (let ((buffer (process-buffer proc)))
466 (if (memq (process-status proc) '(signal exit))
467 (progn
468 (if (null (buffer-name buffer))
469 ;; buffer killed
470 (set-process-buffer proc nil)
471 (let ((obuf (current-buffer))
472 omax opoint)
473 ;; save-excursion isn't the right thing if
474 ;; process-buffer is current-buffer
475 (unwind-protect
476 (progn
477 ;; Write something in the compilation buffer
478 ;; and hack its mode line.
479 (set-buffer buffer)
480 (let ((buffer-read-only nil))
481 (setq omax (point-max)
482 opoint (point))
483 (goto-char omax)
484 ;; Record where we put the message, so we can ignore it
485 ;; later on.
486 (insert ?\n mode-name " " msg)
487 (forward-char -1)
488 (insert " at " (substring (current-time-string) 0 19))
489 (forward-char 1)
490 (setq mode-line-process
491 (concat ":"
492 (symbol-name (process-status proc))))
493 ;; Since the buffer and mode line will show that the
494 ;; process is dead, we can delete it now. Otherwise it
495 ;; will stay around until M-x list-processes.
496 (delete-process proc)
497 ;; Force mode line redisplay soon.
498 (set-buffer-modified-p (buffer-modified-p)))
499 (if (and opoint (< opoint omax))
500 (goto-char opoint))
501 (if compilation-finish-function
502 (funcall compilation-finish-function buffer msg)))
503 (set-buffer obuf))))
504 (setq compilation-in-progress (delq proc compilation-in-progress))
505 ))))
506
507 (defun compilation-filter (proc string)
508 "Process filter for compilation buffers.
509 Just inserts the text, but uses `insert-before-markers'."
510 (save-excursion
511 (set-buffer (process-buffer proc))
512 (let ((buffer-read-only nil))
513 (save-excursion
514 (goto-char (process-mark proc))
515 (insert-before-markers string)
516 (set-marker (process-mark proc) (point))))))
517
518 ;; Return the cdr of compilation-old-error-list for the error containing point.
519 (defun compile-error-at-point ()
520 (compile-reinitialize-errors nil (point))
521 (let ((errors compilation-old-error-list))
522 (while (and errors
523 (> (point) (car (car errors))))
524 (setq errors (cdr errors)))
525 errors))
526
527 (defsubst compilation-buffer-p (buffer)
528 (assq 'compilation-error-list (buffer-local-variables buffer)))
529
530 (defun compilation-next-error (n)
531 "Move point to the next error in the compilation buffer.
532 Does NOT find the source line like \\[next-error]."
533 (interactive "p")
534 (or (compilation-buffer-p (current-buffer))
535 (error "Not in a compilation buffer."))
536 (setq compilation-last-buffer (current-buffer))
537
538 (let ((errors (compile-error-at-point)))
539
540 ;; Move to the error after the one containing point.
541 (goto-char (car (if (< n 0)
542 (let ((i 0)
543 (e compilation-old-error-list))
544 ;; See how many cdrs away ERRORS is from the start.
545 (while (not (eq e errors))
546 (setq i (1+ i)
547 e (cdr e)))
548 (if (> (- n) i)
549 (error "Moved back past first error")
550 (nth (+ i n) compilation-old-error-list)))
551 (let ((compilation-error-list (cdr errors)))
552 (compile-reinitialize-errors nil nil n)
553 (if compilation-error-list
554 (nth (1- n) compilation-error-list)
555 (error "Moved past last error"))))))))
556
557 (defun compilation-previous-error (n)
558 "Move point to the previous error in the compilation buffer.
559 Does NOT find the source line like \\[next-error]."
560 (interactive "p")
561 (compilation-next-error (- n)))
562
563
564 ;; Given an elt of `compilation-error-list', return an object representing
565 ;; the referenced file which is equal to (but not necessarily eq to) what
566 ;; this function would return for another error in the same file.
567 (defsubst compilation-error-filedata (data)
568 (setq data (cdr data))
569 (if (markerp data)
570 (marker-buffer data)
571 (car data)))
572
573 ;; Return a string describing a value from compilation-error-filedata.
574 ;; This value is not necessarily useful as a file name, but should be
575 ;; indicative to the user of what file's errors are being referred to.
576 (defsubst compilation-error-filedata-file-name (filedata)
577 (if (bufferp filedata)
578 (buffer-file-name filedata)
579 (car filedata)))
580
581 (defun compilation-next-file (n)
582 "Move point to the next error for a different file than the current one."
583 (interactive "p")
584 (or (compilation-buffer-p (current-buffer))
585 (error "Not in a compilation buffer."))
586 (setq compilation-last-buffer (current-buffer))
587
588 (let ((reversed (< n 0))
589 errors filedata)
590
591 (if (not reversed)
592 (setq errors (or (compile-error-at-point)
593 (error "Moved past last error")))
594
595 ;; Get a reversed list of the errors up through the one containing point.
596 (compile-reinitialize-errors nil (point))
597 (setq errors (reverse compilation-old-error-list)
598 n (- n))
599
600 ;; Ignore errors after point. (car ERRORS) will be the error
601 ;; containing point, (cadr ERRORS) the one before it.
602 (while (and errors
603 (< (point) (car (car errors))))
604 (setq errors (cdr errors))))
605
606 (while (> n 0)
607 (setq filedata (compilation-error-filedata (car errors)))
608
609 ;; Skip past the following errors for this file.
610 (while (equal filedata
611 (compilation-error-filedata
612 (car (or errors
613 (if reversed
614 (error "%s the first erring file"
615 (compilation-error-filedata-file-name
616 filedata))
617 (let ((compilation-error-list nil))
618 ;; Parse some more.
619 (compile-reinitialize-errors nil nil 2)
620 (setq errors compilation-error-list)))
621 (error "%s is the last erring file"
622 (compilation-error-filedata-file-name
623 filedata))))))
624 (setq errors (cdr errors)))
625
626 (setq n (1- n)))
627
628 ;; Move to the following error.
629 (goto-char (car (car (or errors
630 (if reversed
631 (error "This is the first erring file")
632 (let ((compilation-error-list nil))
633 ;; Parse the last one.
634 (compile-reinitialize-errors nil nil 1)
635 compilation-error-list))))))))
636
637 (defun compilation-previous-file (n)
638 "Move point to the previous error for a different file than the current one."
639 (interactive "p")
640 (compilation-next-file (- n)))
641
642
643 (defun kill-compilation ()
644 "Kill the process made by the \\[compile] command."
645 (interactive)
646 (let ((buffer (compilation-find-buffer)))
647 (if (get-buffer-process buffer)
648 (interrupt-process (get-buffer-process buffer))
649 (error "The compilation process is not running."))))
650
651
652 ;; Parse any new errors in the compilation buffer,
653 ;; or reparse from the beginning if the user has asked for that.
654 (defun compile-reinitialize-errors (reparse
655 &optional limit-search find-at-least)
656 (save-excursion
657 (set-buffer compilation-last-buffer)
658 ;; If we are out of errors, or if user says "reparse",
659 ;; discard the info we have, to force reparsing.
660 (if (or (eq compilation-error-list t)
661 reparse)
662 (compilation-forget-errors))
663 (if (and compilation-error-list
664 (or (not limit-search)
665 (> compilation-parsing-end limit-search))
666 (or (not find-at-least)
667 (>= (length compilation-error-list) find-at-least)))
668 ;; Since compilation-error-list is non-nil, it points to a specific
669 ;; error the user wanted. So don't move it around.
670 nil
671 ;; This was here for a long time (before my rewrite); why? --roland
672 ;;(switch-to-buffer compilation-last-buffer)
673 (set-buffer-modified-p nil)
674 (if (< compilation-parsing-end (point-max))
675 ;; compilation-error-list might be non-nil if we have a non-nil
676 ;; LIMIT-SEARCH or FIND-AT-LEAST arg. In that case its value
677 ;; records the current position in the error list, and we must
678 ;; preserve that after reparsing.
679 (let ((error-list-pos compilation-error-list))
680 (funcall compilation-parse-errors-function
681 limit-search
682 (and find-at-least
683 ;; We only need enough new parsed errors to reach
684 ;; FIND-AT-LEAST errors past the current
685 ;; position.
686 (- find-at-least (length compilation-error-list))))
687 ;; Remember the entire list for compilation-forget-errors. If
688 ;; this is an incremental parse, append to previous list. If
689 ;; we are parsing anew, compilation-forget-errors cleared
690 ;; compilation-old-error-list above.
691 (setq compilation-old-error-list
692 (nconc compilation-old-error-list compilation-error-list))
693 (if error-list-pos
694 ;; We started in the middle of an existing list of parsed
695 ;; errors before parsing more; restore that position.
696 (setq compilation-error-list error-list-pos))
697 )))))
698
699 (defun compile-mouse-goto-error (event)
700 (interactive "e")
701 (save-excursion
702 (set-buffer (window-buffer (posn-window (event-end event))))
703 (goto-char (posn-point (event-end event)))
704
705 (or (compilation-buffer-p (current-buffer))
706 (error "Not in a compilation buffer."))
707 (setq compilation-last-buffer (current-buffer))
708 (compile-reinitialize-errors nil (point))
709
710 ;; Move to bol; the marker for the error on this line will point there.
711 (beginning-of-line)
712
713 ;; Move compilation-error-list to the elt of compilation-old-error-list
714 ;; we want.
715 (setq compilation-error-list compilation-old-error-list)
716 (while (and compilation-error-list
717 (> (point) (car (car compilation-error-list))))
718 (setq compilation-error-list (cdr compilation-error-list)))
719 (or compilation-error-list
720 (error "No error to go to")))
721 (select-window (posn-window (event-end event)))
722 ;; Move to another window, so that next-error's window changes
723 ;; result in the desired setup.
724 (or (one-window-p)
725 (progn
726 (other-window -1)
727 ;; other-window changed the selected buffer,
728 ;; but we didn't want to do that.
729 (set-buffer compilation-last-buffer)))
730
731 (push-mark)
732 (next-error 1))
733
734 (defun compile-goto-error (&optional argp)
735 "Visit the source for the error message point is on.
736 Use this command in a compilation log buffer. Sets the mark at point there.
737 \\[universal-argument] as a prefix arg means to reparse the buffer's error messages first;
738 other kinds of prefix arguments are ignored."
739 (interactive "P")
740 (or (compilation-buffer-p (current-buffer))
741 (error "Not in a compilation buffer."))
742 (setq compilation-last-buffer (current-buffer))
743 (compile-reinitialize-errors (consp argp) (point))
744
745 ;; Move to bol; the marker for the error on this line will point there.
746 (beginning-of-line)
747
748 ;; Move compilation-error-list to the elt of compilation-old-error-list
749 ;; we want.
750 (setq compilation-error-list compilation-old-error-list)
751 (while (and compilation-error-list
752 (> (point) (car (car compilation-error-list))))
753 (setq compilation-error-list (cdr compilation-error-list)))
754
755 ;; Move to another window, so that next-error's window changes
756 ;; result in the desired setup.
757 (or (one-window-p)
758 (progn
759 (other-window -1)
760 ;; other-window changed the selected buffer,
761 ;; but we didn't want to do that.
762 (set-buffer compilation-last-buffer)))
763
764 (push-mark)
765 (next-error 1))
766
767 ;; Return a compilation buffer.
768 ;; If the current buffer is a compilation buffer, return it.
769 ;; If compilation-last-buffer is set to a live buffer, use that.
770 ;; Otherwise, look for a compilation buffer and signal an error
771 ;; if there are none.
772 (defun compilation-find-buffer (&optional other-buffer)
773 (if (and (not other-buffer)
774 (compilation-buffer-p (current-buffer)))
775 ;; The current buffer is a compilation buffer.
776 (current-buffer)
777 (if (and compilation-last-buffer (buffer-name compilation-last-buffer)
778 (or (not other-buffer) (not (eq compilation-last-buffer
779 (current-buffer)))))
780 compilation-last-buffer
781 (let ((buffers (buffer-list)))
782 (while (and buffers (or (not (compilation-buffer-p (car buffers)))
783 (and other-buffer
784 (eq (car buffers) (current-buffer)))))
785 (setq buffers (cdr buffers)))
786 (if buffers
787 (car buffers)
788 (or (and other-buffer
789 (compilation-buffer-p (current-buffer))
790 ;; The current buffer is a compilation buffer.
791 (progn
792 (if other-buffer
793 (message "This is the only compilation buffer."))
794 (current-buffer)))
795 (error "No compilation started!")))))))
796
797 ;;;###autoload
798 (defun next-error (&optional argp)
799 "Visit next compilation error message and corresponding source code.
800 This operates on the output from the \\[compile] command.
801 If all preparsed error messages have been processed,
802 the error message buffer is checked for new ones.
803
804 A prefix arg specifies how many error messages to move;
805 negative means move back to previous error messages.
806 Just C-u as a prefix means reparse the error message buffer
807 and start at the first error.
808
809 \\[next-error] normally applies to the most recent compilation started,
810 but as long as you are in the middle of parsing errors from one compilation
811 output buffer, you stay with that compilation output buffer.
812
813 Use \\[next-error] in a compilation output buffer to switch to
814 processing errors from that compilation.
815
816 See variables `compilation-parse-errors-function' and
817 \`compilation-error-regexp-alist' for customization ideas."
818 (interactive "P")
819 (setq compilation-last-buffer (compilation-find-buffer))
820 (compilation-goto-locus (compilation-next-error-locus
821 ;; We want to pass a number here only if
822 ;; we got a numeric prefix arg, not just C-u.
823 (and (not (consp argp))
824 (prefix-numeric-value argp))
825 (consp argp))))
826 ;;;###autoload (define-key ctl-x-map "`" 'next-error)
827
828 (defun compilation-next-error-locus (&optional move reparse silent)
829 "Visit next compilation error and return locus in corresponding source code.
830 This operates on the output from the \\[compile] command.
831 If all preparsed error messages have been processed,
832 the error message buffer is checked for new ones.
833
834 Returns a cons (ERROR . SOURCE) of two markers: ERROR is a marker at the
835 location of the error message in the compilation buffer, and SOURCE is a
836 marker at the location in the source code indicated by the error message.
837
838 Optional first arg MOVE says how many error messages to move forwards (or
839 backwards, if negative); default is 1. Optional second arg REPARSE, if
840 non-nil, says to reparse the error message buffer and reset to the first
841 error (plus MOVE - 1). If optional third argument SILENT is non-nil, return
842 nil instead of raising an error if there are no more errors.
843
844 The current buffer should be the desired compilation output buffer."
845 (or move (setq move 1))
846 (compile-reinitialize-errors reparse nil (and (not reparse)
847 (if (< move 1) 0 (1- move))))
848 (let (next-errors next-error)
849 (catch 'no-next-error
850 (save-excursion
851 (set-buffer compilation-last-buffer)
852 ;; compilation-error-list points to the "current" error.
853 (setq next-errors
854 (if (> move 0)
855 (nthcdr (1- move)
856 compilation-error-list)
857 ;; Zero or negative arg; we need to move back in the list.
858 (let ((n (1- move))
859 (i 0)
860 (e compilation-old-error-list))
861 ;; See how many cdrs away the current error is from the start.
862 (while (not (eq e compilation-error-list))
863 (setq i (1+ i)
864 e (cdr e)))
865 (if (> (- n) i)
866 (error "Moved back past first error")
867 (nthcdr (+ i n) compilation-old-error-list))))
868 next-error (car next-errors))
869 (while
870 (if (null next-error)
871 (progn
872 (and move (/= move 1)
873 (error (if (> move 0)
874 "Moved past last error")
875 "Moved back past first error"))
876 ;; Forget existing error messages if compilation has finished.
877 (if (not (and (get-buffer-process (current-buffer))
878 (eq (process-status
879 (get-buffer-process
880 (current-buffer)))
881 'run)))
882 (compilation-forget-errors))
883 (if silent
884 (throw 'no-next-error nil)
885 (error (concat compilation-error-message
886 (and (get-buffer-process (current-buffer))
887 (eq (process-status
888 (get-buffer-process
889 (current-buffer)))
890 'run)
891 " yet")))))
892 (setq compilation-error-list (cdr next-errors))
893 (if (null (cdr next-error))
894 ;; This error is boring. Go to the next.
895 t
896 (or (markerp (cdr next-error))
897 ;; This error has a filename/lineno pair.
898 ;; Find the file and turn it into a marker.
899 (let* ((fileinfo (car (cdr next-error)))
900 (buffer (compilation-find-file (cdr fileinfo)
901 (car fileinfo)
902 (car next-error))))
903 (if (null buffer)
904 ;; We can't find this error's file.
905 ;; Remove all errors in the same file.
906 (progn
907 (setq next-errors compilation-old-error-list)
908 (while next-errors
909 (and (consp (cdr (car next-errors)))
910 (equal (car (cdr (car next-errors)))
911 fileinfo)
912 (progn
913 (set-marker (car (car next-errors)) nil)
914 (setcdr (car next-errors) nil)))
915 (setq next-errors (cdr next-errors)))
916 ;; Look for the next error.
917 t)
918 ;; We found the file. Get a marker for this error.
919 ;; compilation-old-error-list is a buffer-local
920 ;; variable, so we must be careful to extract its value
921 ;; before switching to the source file buffer.
922 (let ((errors compilation-old-error-list)
923 (last-line (nth 1 (cdr next-error)))
924 (column (nth 2 (cdr next-error))))
925 (set-buffer buffer)
926 (save-excursion
927 (save-restriction
928 (widen)
929 (goto-line last-line)
930 (if column
931 (move-to-column column)
932 (beginning-of-line))
933 (setcdr next-error (point-marker))
934 ;; Make all the other error messages referring
935 ;; to the same file have markers into the buffer.
936 (while errors
937 (and (consp (cdr (car errors)))
938 (equal (car (cdr (car errors))) fileinfo)
939 (let* ((this (nth 1 (cdr (car errors))))
940 (column (nth 2 (cdr (car errors))))
941 (lines (- this last-line)))
942 (if (eq selective-display t)
943 ;; When selective-display is t,
944 ;; each C-m is a line boundary,
945 ;; as well as each newline.
946 (if (< lines 0)
947 (re-search-backward "[\n\C-m]"
948 nil 'end
949 (- lines))
950 (re-search-forward "[\n\C-m]"
951 nil 'end
952 lines))
953 (forward-line lines))
954 (if column
955 (move-to-column column))
956 (setq last-line this)
957 (setcdr (car errors) (point-marker))))
958 (setq errors (cdr errors)))))))))
959 ;; If we didn't get a marker for this error, or this
960 ;; marker's buffer was killed, go on to the next one.
961 (or (not (markerp (cdr next-error)))
962 (not (marker-buffer (cdr next-error))))))
963 (setq next-errors compilation-error-list
964 next-error (car next-errors)))))
965
966 ;; Skip over multiple error messages for the same source location,
967 ;; so the next C-x ` won't go to an error in the same place.
968 (while (and compilation-error-list
969 (equal (cdr (car compilation-error-list)) (cdr next-error)))
970 (setq compilation-error-list (cdr compilation-error-list)))
971
972 ;; We now have a marker for the position of the error source code.
973 ;; NEXT-ERROR is a cons (ERROR . SOURCE) of two markers.
974 next-error))
975
976 (defun compilation-goto-locus (next-error)
977 "Jump to an error locus returned by `compilation-next-error-locus'.
978 Takes one argument, a cons (ERROR . SOURCE) of two markers.
979 Selects a window with point at SOURCE, with another window displaying ERROR."
980 (if (and (window-dedicated-p (selected-window))
981 (eq (selected-window) (frame-root-window)))
982 (switch-to-buffer-other-frame (marker-buffer (cdr next-error)))
983 (switch-to-buffer (marker-buffer (cdr next-error))))
984 (goto-char (cdr next-error))
985 ;; If narrowing got in the way of
986 ;; going to the right place, widen.
987 (or (= (point) (marker-position (cdr next-error)))
988 (progn
989 (widen)
990 (goto-char (cdr next-error))))
991
992 ;; Show compilation buffer in other window, scrolled to this error.
993 (let* ((pop-up-windows t)
994 (w (display-buffer (marker-buffer (car next-error)))))
995 (set-window-point w (car next-error))
996 (set-window-start w (car next-error))))
997 \f
998 ;; Find a buffer for file FILENAME.
999 ;; Search the directories in compilation-search-path.
1000 ;; A nil in compilation-search-path means to try the
1001 ;; current directory, which is passed in DIR.
1002 ;; If FILENAME is not found at all, ask the user where to find it.
1003 ;; Pop up the buffer containing MARKER and scroll to MARKER if we ask the user.
1004 (defun compilation-find-file (filename dir marker)
1005 (let ((dirs compilation-search-path)
1006 result name)
1007 (while (and dirs (null result))
1008 (setq name (expand-file-name filename (or (car dirs) dir))
1009 result (and (file-exists-p name)
1010 (find-file-noselect name))
1011 dirs (cdr dirs)))
1012 (or result
1013 ;; The file doesn't exist.
1014 ;; Ask the user where to find it.
1015 ;; If he hits C-g, then the next time he does
1016 ;; next-error, he'll skip past it.
1017 (progn
1018 (let* ((pop-up-windows t)
1019 (w (display-buffer (marker-buffer marker))))
1020 (set-window-point w marker)
1021 (set-window-start w marker))
1022 (setq name
1023 (expand-file-name
1024 (read-file-name
1025 (format "Find this error in: (default %s) "
1026 filename) dir filename t)))
1027 (if (file-directory-p name)
1028 (setq name (concat (file-name-as-directory name) filename)))
1029 (if (file-exists-p name)
1030 (find-file-noselect name))))))
1031
1032 ;; Set compilation-error-list to nil, and unchain the markers that point to the
1033 ;; error messages and their text, so that they no longer slow down gap motion.
1034 ;; This would happen anyway at the next garbage collection, but it is better to
1035 ;; do it right away.
1036 (defun compilation-forget-errors ()
1037 (while compilation-old-error-list
1038 (let ((next-error (car compilation-old-error-list)))
1039 (set-marker (car next-error) nil)
1040 (if (markerp (cdr next-error))
1041 (set-marker (cdr next-error) nil)))
1042 (setq compilation-old-error-list (cdr compilation-old-error-list)))
1043 (setq compilation-error-list nil
1044 compilation-directory-stack nil
1045 compilation-parsing-end 1))
1046
1047
1048 (defun count-regexp-groupings (regexp)
1049 "Return the number of \\( ... \\) groupings in REGEXP (a string)."
1050 (let ((groupings 0)
1051 (len (length regexp))
1052 (i 0)
1053 c)
1054 (while (< i len)
1055 (setq c (aref regexp i)
1056 i (1+ i))
1057 (cond ((= c ?\[)
1058 ;; Find the end of this [...].
1059 (while (and (< i len)
1060 (not (= (aref regexp i) ?\])))
1061 (setq i (1+ i))))
1062 ((= c ?\\)
1063 (if (< i len)
1064 (progn
1065 (setq c (aref regexp i)
1066 i (1+ i))
1067 (if (= c ?\))
1068 ;; We found the end of a grouping,
1069 ;; so bump our counter.
1070 (setq groupings (1+ groupings))))))))
1071 groupings))
1072
1073 (defun compilation-parse-errors (limit-search find-at-least)
1074 "Parse the current buffer as grep, cc or lint error messages.
1075 See variable `compilation-parse-errors-function' for the interface it uses."
1076 (setq compilation-error-list nil)
1077 (message "Parsing error messages...")
1078 (let (text-buffer orig orig-expanded parent-expanded
1079 regexp enter-group leave-group error-group
1080 alist subexpr error-regexp-groups
1081 (found-desired nil)
1082 (compilation-num-errors-found 0))
1083
1084 ;; Don't reparse messages already seen at last parse.
1085 (goto-char compilation-parsing-end)
1086 ;; Don't parse the first two lines as error messages.
1087 ;; This matters for grep.
1088 (if (bobp)
1089 (progn
1090 (forward-line 2)
1091 ;; Move back so point is before the newline.
1092 ;; This matters because some error regexps use \n instead of ^
1093 ;; to be faster.
1094 (forward-char -1)))
1095
1096 ;; Compile all the regexps we want to search for into one.
1097 (setq regexp (concat "\\(" compilation-enter-directory-regexp "\\)\\|"
1098 "\\(" compilation-leave-directory-regexp "\\)\\|"
1099 "\\(" (mapconcat (function
1100 (lambda (elt)
1101 (concat "\\(" (car elt) "\\)")))
1102 compilation-error-regexp-alist
1103 "\\|") "\\)"))
1104
1105 ;; Find out how many \(...\) groupings are in each of the regexps, and set
1106 ;; *-GROUP to the grouping containing each constituent regexp (whose
1107 ;; subgroups will come immediately thereafter) of the big regexp we have
1108 ;; just constructed.
1109 (setq enter-group 1
1110 leave-group (+ enter-group
1111 (count-regexp-groupings
1112 compilation-enter-directory-regexp)
1113 1)
1114 error-group (+ leave-group
1115 (count-regexp-groupings
1116 compilation-leave-directory-regexp)
1117 1))
1118
1119 ;; Compile an alist (IDX FILE LINE [COL]), where IDX is the number of
1120 ;; the subexpression for an entire error-regexp, and FILE and LINE (and
1121 ;; possibly COL) are the numbers for the subexpressions giving the file
1122 ;; name and line number (and possibly column number).
1123 (setq alist (or compilation-error-regexp-alist
1124 (error "compilation-error-regexp-alist is empty!"))
1125 subexpr (1+ error-group))
1126 (while alist
1127 (setq error-regexp-groups
1128 (cons (list subexpr
1129 (+ subexpr (nth 1 (car alist)))
1130 (+ subexpr (nth 2 (car alist)))
1131 (and (nth 3 (car alist))
1132 (+ subexpr (nth 3 (car alist)))))
1133 error-regexp-groups))
1134 (setq subexpr (+ subexpr 1 (count-regexp-groupings (car (car alist)))))
1135 (setq alist (cdr alist)))
1136
1137 (setq orig default-directory)
1138 (setq orig-expanded (file-truename orig))
1139 (setq parent-expanded (expand-file-name "../" orig-expanded))
1140
1141 (while (and (not found-desired)
1142 ;; We don't just pass LIMIT-SEARCH to re-search-forward
1143 ;; because we want to find matches containing LIMIT-SEARCH
1144 ;; but which extend past it.
1145 (re-search-forward regexp nil t))
1146
1147 ;; Figure out which constituent regexp matched.
1148 (cond ((match-beginning enter-group)
1149 ;; The match was the enter-directory regexp.
1150 (let ((dir
1151 (file-name-as-directory
1152 (expand-file-name
1153 (buffer-substring (match-beginning (+ enter-group 1))
1154 (match-end (+ enter-group 1)))))))
1155 ;; The directory name in the "entering" message
1156 ;; is a truename. Try to convert it to a form
1157 ;; like what the user typed in.
1158 (setq dir
1159 (compile-abbreviate-directory dir orig orig-expanded
1160 parent-expanded))
1161 (setq compilation-directory-stack
1162 (cons dir compilation-directory-stack))
1163 (and (file-directory-p dir)
1164 (setq default-directory dir)))
1165
1166 (and limit-search (>= (point) limit-search)
1167 ;; The user wanted a specific error, and we're past it.
1168 ;; We do this check here (and in the leave-group case)
1169 ;; rather than at the end of the loop because if the last
1170 ;; thing seen is an error message, we must carefully
1171 ;; discard the last error when it is the first in a new
1172 ;; file (see below in the error-group case).
1173 (setq found-desired t)))
1174
1175 ((match-beginning leave-group)
1176 ;; The match was the leave-directory regexp.
1177 (let ((beg (match-beginning (+ leave-group 1)))
1178 (stack compilation-directory-stack))
1179 (if beg
1180 (let ((dir
1181 (file-name-as-directory
1182 (expand-file-name
1183 (buffer-substring beg
1184 (match-end (+ leave-group
1185 1)))))))
1186 ;; The directory name in the "entering" message
1187 ;; is a truename. Try to convert it to a form
1188 ;; like what the user typed in.
1189 (setq dir
1190 (compile-abbreviate-directory dir orig orig-expanded
1191 parent-expanded))
1192 (while (and stack
1193 (not (string-equal (car stack) dir)))
1194 (setq stack (cdr stack)))))
1195 (setq compilation-directory-stack (cdr stack))
1196 (setq stack (car compilation-directory-stack))
1197 (if stack
1198 (setq default-directory stack))
1199 )
1200
1201 (and limit-search (>= (point) limit-search)
1202 ;; The user wanted a specific error, and we're past it.
1203 ;; We do this check here (and in the enter-group case)
1204 ;; rather than at the end of the loop because if the last
1205 ;; thing seen is an error message, we must carefully
1206 ;; discard the last error when it is the first in a new
1207 ;; file (see below in the error-group case).
1208 (setq found-desired t)))
1209
1210 ((match-beginning error-group)
1211 ;; The match was the composite error regexp.
1212 ;; Find out which individual regexp matched.
1213 (setq alist error-regexp-groups)
1214 (while (and alist
1215 (null (match-beginning (car (car alist)))))
1216 (setq alist (cdr alist)))
1217 (if alist
1218 (setq alist (car alist))
1219 (error "compilation-parse-errors: impossible regexp match!"))
1220
1221 ;; Extract the file name and line number from the error message.
1222 (let ((beginning-of-match (match-beginning 0)) ;looking-at nukes
1223 (filename (buffer-substring (match-beginning (nth 1 alist))
1224 (match-end (nth 1 alist))))
1225 (linenum (string-to-int
1226 (buffer-substring
1227 (match-beginning (nth 2 alist))
1228 (match-end (nth 2 alist)))))
1229 (column (and (nth 3 alist)
1230 (string-to-int
1231 (buffer-substring
1232 (match-beginning (nth 3 alist))
1233 (match-end (nth 3 alist)))))))
1234
1235 ;; Check for a comint-file-name-prefix and prepend it if
1236 ;; appropriate. (This is very useful for
1237 ;; compilation-minor-mode in an rlogin-mode buffer.)
1238 (and (boundp 'comint-file-name-prefix)
1239 ;; If the file name is relative, default-directory will
1240 ;; already contain the comint-file-name-prefix (done by
1241 ;; compile-abbreviate-directory).
1242 (file-name-absolute-p filename)
1243 (setq filename (concat comint-file-name-prefix filename)))
1244 (setq filename (cons default-directory filename))
1245
1246 ;; Locate the erring file and line.
1247 ;; Cons a new elt onto compilation-error-list,
1248 ;; giving a marker for the current compilation buffer
1249 ;; location, and the file and line number of the error.
1250 (save-excursion
1251 (beginning-of-line 1)
1252 (let ((this (cons (point-marker)
1253 (list filename linenum column))))
1254 ;; Don't add the same source line more than once.
1255 (if (equal (cdr this) (cdr (car compilation-error-list)))
1256 nil
1257 (setq compilation-error-list
1258 (cons this
1259 compilation-error-list))
1260 (setq compilation-num-errors-found
1261 (1+ compilation-num-errors-found)))))
1262 (and (or (and find-at-least (> compilation-num-errors-found
1263 find-at-least))
1264 (and limit-search (>= (point) limit-search)))
1265 ;; We have found as many new errors as the user wants,
1266 ;; or past the buffer position he indicated. We
1267 ;; continue to parse until we have seen all the
1268 ;; consecutive errors in the same file, so the error
1269 ;; positions will be recorded as markers in this buffer
1270 ;; that might change.
1271 (cdr compilation-error-list) ; Must check at least two.
1272 (not (equal (car (cdr (nth 0 compilation-error-list)))
1273 (car (cdr (nth 1 compilation-error-list)))))
1274 (progn
1275 ;; Discard the error just parsed, so that the next
1276 ;; parsing run can get it and the following errors in
1277 ;; the same file all at once. If we didn't do this, we
1278 ;; would have the same problem we are trying to avoid
1279 ;; with the test above, just delayed until the next run!
1280 (setq compilation-error-list
1281 (cdr compilation-error-list))
1282 (goto-char beginning-of-match)
1283 (setq found-desired t)))
1284 )
1285 )
1286 (t
1287 (error "compilation-parse-errors: known groups didn't match!")))
1288
1289 (message "Parsing error messages...%d (%.0f%% of buffer)"
1290 compilation-num-errors-found
1291 ;; Use floating-point because (* 100 (point)) frequently
1292 ;; exceeds the range of Emacs Lisp integers.
1293 (/ (* 100.0 (point)) (point-max)))
1294
1295 (and limit-search (>= (point) limit-search)
1296 ;; The user wanted a specific error, and we're past it.
1297 (setq found-desired t)))
1298 (setq compilation-parsing-end (if found-desired
1299 (point)
1300 ;; We have searched the whole buffer.
1301 (point-max))))
1302 (setq compilation-error-list (nreverse compilation-error-list))
1303 (message "Parsing error messages...done"))
1304
1305 ;; If directory DIR is a subdir of ORIG or of ORIG's parent,
1306 ;; return a relative name for it starting from ORIG or its parent.
1307 ;; ORIG-EXPANDED is an expanded version of ORIG.
1308 ;; PARENT-EXPANDED is an expanded version of ORIG's parent.
1309 ;; Those two args could be computed here, but we run faster by
1310 ;; having the caller compute them just once.
1311 (defun compile-abbreviate-directory (dir orig orig-expanded parent-expanded)
1312 ;; Check for a comint-file-name-prefix and prepend it if appropriate.
1313 ;; (This is very useful for compilation-minor-mode in an rlogin-mode
1314 ;; buffer.)
1315 (if (boundp 'comint-file-name-prefix)
1316 (setq dir (concat comint-file-name-prefix dir)))
1317
1318 (if (and (> (length dir) (length orig-expanded))
1319 (string= orig-expanded
1320 (substring dir 0 (length orig-expanded))))
1321 (setq dir
1322 (concat orig
1323 (substring dir (length orig-expanded)))))
1324 (if (and (> (length dir) (length parent-expanded))
1325 (string= parent-expanded
1326 (substring dir 0 (length parent-expanded))))
1327 (setq dir
1328 (concat (file-name-directory
1329 (directory-file-name orig))
1330 (substring dir (length parent-expanded)))))
1331 dir)
1332
1333 (provide 'compile)
1334
1335 ;;; compile.el ends here