*** empty log message ***
[emacs.git] / lisp / progmodes / compile.el
bloba1596617eea77996081c1401a02217a3be901edf
1 ;;; compile.el --- run compiler as inferior of Emacs, parse error messages.
3 ;; Maintainer: FSF
4 ;; Last-Modified: 05 Jul 1992
6 ;;;!!! dup removal is broken.
8 ;; Copyright (C) 1985-1991 Free Software Foundation, Inc.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY. No author or distributor
14 ;; accepts responsibility to anyone for the consequences of using it
15 ;; or for whether it serves any particular purpose or works at all,
16 ;; unless he says so in writing. Refer to the GNU Emacs General Public
17 ;; License for full details.
19 ;; Everyone is granted permission to copy, modify and redistribute
20 ;; GNU Emacs, but only under the conditions described in the
21 ;; GNU Emacs General Public License. A copy of this license is
22 ;; supposed to have been given to you along with GNU Emacs so you
23 ;; can know your rights and responsibilities. It should be in a
24 ;; file named COPYING. Among other things, the copyright notice
25 ;; and this notice must be preserved on all copies.
27 ;;; Code:
29 ;;;###autoload
30 (defvar compilation-mode-hook nil
31 "*List of hook functions run by compilation-mode (see `run-hooks').")
33 ;;;###autoload
34 (defconst compilation-window-height nil
35 "*Number of lines in a compilation window. If nil, use Emacs default.")
37 (defvar compilation-error-list nil
38 "List of error message descriptors for visiting erring functions.
39 Each error descriptor is a cons (or nil).
40 Its car is a marker pointing to an error message.
41 If its cdr is a marker, it points to the text of the line the message is about.
42 If its cdr is a cons, that cons's car is a cons (DIRECTORY . FILE), specifying
43 file the message is about, and its cdr is the number of the line the message
44 is about. Or its cdr may be nil if that error is not interesting.
46 The value may be t instead of a list; this means that the buffer of
47 error messages should be reparsed the next time the list of errors is wanted.")
49 (defvar compilation-old-error-list nil
50 "Value of `compilation-error-list' after errors were parsed.")
52 (defvar compilation-parse-errors-function 'compilation-parse-errors
53 "Function to call (with no args) to parse error messages from a compilation.
54 It should read in the source files which have errors and set
55 `compilation-error-list' to a list with an element for each error message
56 found. See that variable for more info.")
58 ;;;###autoload
59 (defvar compilation-buffer-name-function nil
60 "*Function to call with one argument, the name of the major mode of the
61 compilation buffer, to give the buffer a name. It should return a string.
62 If nil, the name \"*compilation*\" is used for compilation buffers,
63 and the name \"*grep*\" is used for grep buffers.
64 \(Actually, the name (concat \"*\" (downcase major-mode) \"*\") is used.)")
66 ;;;###autoload
67 (defvar compilation-finish-function nil
68 "*Function to call when a compilation process finishes.
69 It is called with two arguments: the compilation buffer, and a string
70 describing how the process finished.")
72 (defvar compilation-last-buffer nil
73 "The buffer in which the last compilation was started,
74 or which was used by the last \\[next-error] or \\[compile-goto-error].")
76 (defvar compilation-in-progress nil
77 "List of compilation processes now running.")
78 (or (assq 'compilation-in-progress minor-mode-alist)
79 (setq minor-mode-alist (cons '(compilation-in-progress " Compiling")
80 minor-mode-alist)))
82 (defvar compilation-parsing-end nil
83 "Position of end of buffer when last error messages were parsed.")
85 (defvar compilation-error-message "No more errors"
86 "Message to print when no more matches for `compilation-error-regexp-alist'
87 are found.")
89 (defvar compilation-error-regexp-alist
91 ;; 4.3BSD grep, cc, lint pass 1:
92 ;; /usr/src/foo/foo.c(8): warning: w may be used before set
93 ;; or GNU utilities
94 ;; foo.c:8: error message
95 ("^\\([^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2)
96 ;; 4.3BSD lint pass 2
97 ;; strcmp: variable # of args. llib-lc(359) :: /usr/src/foo/foo.c(8)
98 ("[ \t:]+\\([^:( \t\n]+\\)[ \t]*[:(]*(+[ \t]*\\([0-9]+\\))[:) \t]*$" 1 2)
99 ;; 4.3BSD lint pass 3
100 ;; bloofle defined( /users/wolfgang/foo.c(4) ), but never used
101 ;; This used to be
102 ;; ("[ \t(]+\\([^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]+" 1 2)
103 ;; which is regexp Impressionism - it matches almost anything!
104 ("([ \t]*\\([^:( \t\n]+\\)[ \t]*[:(][ \t]*\\([0-9]+\\))" 1 2)
105 ;; Line 45 of "foo.c": bloofel undefined (who does this?)
106 ("^[Ll]ine[ \t]+\\([0-9]+\\)[ \t]+of[ \t]+\"\\([^\"\n]+\\)\":" 2 1)
107 ;; Apollo cc, 4.3BSD fc
108 ;; "foo.f", line 3: Error: syntax error near end of statement
109 ("^\"\\([^\"\n]+\\)\", line \\([0-9]+\\):" 1 2)
110 ;; HP-UX 7.0 fc
111 ;; foo.f :16 some horrible error message
112 ("^\\([^ \t\n:]+\\)[ \t]*:\\([0-9]+\\)" 1 2)
113 ;; IBM AIX PS/2 C version 1.1
114 ;; ****** Error number 140 in line 8 of file errors.c ******
115 ("in line \\([0-9]+\\) of file \\([^ \n]+[^. \n]\\)\\.? " 2 1)
116 ;; IBM AIX lint is too painful to do right this way. File name
117 ;; prefixes entire sections rather than being on each line.
119 "Alist (REGEXP FILE-IDX LINE-IDX) of regular expressions to match errors in
120 compilation. If REGEXP matches, the FILE-IDX'th subexpression gives the file
121 name, and the LINE-IDX'th subexpression gives the line number.")
123 ;;;###autoload
124 (defvar compilation-search-path '(nil)
125 "*List of directories to search for source files named in error messages.
126 Elements should be directory names, not file names of directories.
127 nil as an element means to try the default directory.")
129 (defvar compile-command "make -k "
130 "Last shell command used to do a compilation; default for next compilation.
132 Sometimes it is useful for files to supply local values for this variable.
133 You might also use mode hooks to specify it in certain modes, like this:
135 (setq c-mode-hook
136 '(lambda () (or (file-exists-p \"makefile\") (file-exists-p \"Makefile\")
137 (progn (make-local-variable 'compile-command)
138 (setq compile-command
139 (concat \"make -k \"
140 buffer-file-name))))))")
142 ;;;###autoload
143 (defvar grep-command "grep -n "
144 "Last shell command used to do a grep search; default for next search.
145 Typically \"grep -n\" or \"egrep -n\".
146 \(The \"-n\" option tells grep to output line numbers.)")
148 (defconst compilation-enter-directory-regexp
149 ": Entering directory `\\(.*\\)'$"
150 "Regular expression for a line in the compilation log that
151 changes the current directory. This must contain one \\(, \\) pair
152 around the directory name.
154 The default value matches lines printed by the `-w' option of GNU Make.")
156 (defconst compilation-leave-directory-regexp
157 ": Leaving directory `\\(.*\\)'$"
158 "Regular expression for a line in the compilation log that
159 changes the current directory to a previous value. This may
160 contain one \\(, \\) pair around the name of the directory
161 being moved from. If it does not, the last directory entered
162 \(by a line matching `compilation-enter-directory-regexp'\) is assumed.
164 The default value matches lines printed by the `-w' option of GNU Make.")
166 (defvar compilation-directory-stack nil
167 "Stack of directories entered by lines matching
168 \`compilation-enter-directory-regexp' and not yet left by lines matching
169 \`compilation-leave-directory-regexp'. The head element is the directory
170 the compilation was started in.")
172 ;;;###autoload
173 (defun compile (command)
174 "Compile the program including the current buffer. Default: run `make'.
175 Runs COMMAND, a shell command, in a separate process asynchronously
176 with output going to the buffer `*compilation*'.
178 You can then use the command \\[next-error] to find the next error message
179 and move to the source code that caused it.
181 To run more than one compilation at once, start one and rename the
182 \`*compilation*' buffer to some other name with \\[rename-buffer].
183 Then start the next one.
185 The name used for the buffer is actually whatever is returned by
186 the function in `compilation-buffer-name-function', so you can set that
187 to a function that generates a unique name."
188 (interactive (list (read-string "Compile command: " compile-command)))
189 (setq compile-command command)
190 (save-some-buffers nil nil)
191 (compile-internal compile-command "No more errors"))
193 ;;;###autoload
194 (defun grep (command-args)
195 "Run grep, with user-specified args, and collect output in a buffer.
196 While grep runs asynchronously, you can use the \\[next-error] command
197 to find the text that grep hits refer to.
199 The variable `grep-command' holds the last grep command run,
200 and is the default for future runs. The command should use the `-n'
201 flag, so that line numbers are displayed for each match.
202 What the user enters in response to the prompt for grep args is
203 appended to everything up to and including the `-n' in `grep-command'."
204 (interactive
205 (list (read-string (concat "Run "
206 (substring grep-command 0
207 (string-match "[\t ]+" grep-command))
208 " (with args): ")
209 (progn
210 (string-match "-n[\t ]+" grep-command)
211 (substring grep-command (match-end 0))))))
212 ;; why a redundant string-match? It might not be interactive ...
213 (setq grep-command (concat (substring grep-command 0
214 (progn
215 (string-match "-n" grep-command)
216 (match-end 0)))
217 " " command-args))
218 (compile-internal (concat grep-command " /dev/null")
219 "No more grep hits" "grep"))
221 (defun compile-internal (command error-message
222 &optional name-of-mode parser regexp-alist
223 name-function)
224 "Run compilation command COMMAND (low level interface).
225 ERROR-MESSAGE is a string to print if the user asks to see another error
226 and there are no more errors. Third argument NAME-OF-MODE is the name
227 to display as the major mode in the compilation buffer.
229 Fourth arg PARSER is the error parser function (nil means the default). Fifth
230 arg REGEXP-ALIST is the error message regexp alist to use (nil means the
231 default). Sixth arg NAME-FUNCTION is a function called to name the buffer (nil
232 means the default). The defaults for these variables are the global values of
233 \`compilation-parse-errors-function', `compilation-error-regexp-alist', and
234 \`compilation-buffer-name-function', respectively."
235 (let (outbuf)
236 (save-excursion
237 (or name-of-mode
238 (setq name-of-mode "Compilation"))
239 (setq outbuf
240 (get-buffer-create
241 (funcall (or name-function compilation-buffer-name-function
242 (function (lambda (mode)
243 (concat "*" (downcase mode) "*"))))
244 name-of-mode)))
245 (set-buffer outbuf)
246 (let ((comp-proc (get-buffer-process (current-buffer))))
247 (if comp-proc
248 (if (or (not (eq (process-status comp-proc) 'run))
249 (yes-or-no-p
250 "A compilation process is running; kill it? "))
251 (condition-case ()
252 (progn
253 (interrupt-process comp-proc)
254 (sit-for 1)
255 (delete-process comp-proc))
256 (error nil))
257 (error "Cannot have two processes in `%s' at once"
258 (buffer-name))
260 ;; In case the compilation buffer is current, make sure we get the global
261 ;; values of compilation-error-regexp-alist, etc.
262 (kill-all-local-variables))
263 (let ((regexp-alist (or regexp-alist compilation-error-regexp-alist))
264 (parser (or parser compilation-parse-errors-function))
265 (thisdir default-directory)
266 outwin)
267 (save-excursion
268 ;; Clear out the compilation buffer and make it writable.
269 ;; Change its default-directory to the directory where the compilation
270 ;; will happen, and insert a `cd' command to indicate this.
271 (set-buffer outbuf)
272 (setq buffer-read-only nil)
273 (erase-buffer)
274 (setq default-directory thisdir)
275 (insert "cd " thisdir "\n" command "\n")
276 (set-buffer-modified-p nil))
277 ;; If we're already in the compilation buffer, go to the end
278 ;; of the buffer, so point will track the compilation output.
279 (if (eq outbuf (current-buffer))
280 (goto-char (point-max)))
281 ;; Pop up the compilation buffer.
282 (setq outwin (display-buffer outbuf))
283 (set-buffer outbuf)
284 (compilation-mode)
285 (buffer-disable-undo (current-buffer))
286 (setq buffer-read-only t)
287 (set (make-local-variable 'compilation-parse-errors-function) parser)
288 (set (make-local-variable 'compilation-error-message) error-message)
289 (set (make-local-variable 'compilation-error-regexp-alist) regexp-alist)
290 (setq default-directory thisdir
291 compilation-directory-stack (list default-directory))
292 (set-window-start outwin (point-min))
293 (setq mode-name name-of-mode)
294 (or (eq outwin (selected-window))
295 (set-window-point outwin (point-min)))
296 (and compilation-window-height
297 (= (window-width outwin) (frame-width))
298 (let ((w (selected-window)))
299 (unwind-protect
300 (progn
301 (select-window outwin)
302 (enlarge-window (- compilation-window-height
303 (window-height))))
304 (select-window w))))
305 ;; Start the compilation.
306 (let ((proc (start-process-shell-command (downcase mode-name)
307 outbuf
308 command)))
309 (set-process-sentinel proc 'compilation-sentinel)
310 (setq compilation-in-progress (cons proc compilation-in-progress))))
311 ;; Make it so the next C-x ` will use this buffer.
312 (setq compilation-last-buffer outbuf)))
314 (defvar compilation-mode-map
315 (let ((map (make-sparse-keymap)))
316 (define-key map "\C-c\C-c" 'compile-goto-error)
317 (define-key map "\C-c\C-k" 'kill-compilation)
318 map)
319 "Keymap for compilation log buffers.")
321 (defun compilation-mode ()
322 "Major mode for compilation log buffers.
323 \\<compilation-mode-map>To visit the source for a line-numbered error,
324 move point to the error message line and type \\[compile-goto-error].
325 To kill the compilation, type \\[kill-compilation].
327 Runs `compilation-mode-hook' with `run-hooks' (which see)."
328 (interactive)
329 (fundamental-mode)
330 (use-local-map compilation-mode-map)
331 (setq major-mode 'compilation-mode)
332 (setq mode-name "Compilation")
333 ;; Make buffer's mode line show process state
334 (setq mode-line-process '(": %s"))
335 (set (make-local-variable 'compilation-error-list) nil)
336 (set (make-local-variable 'compilation-old-error-list) nil)
337 (set (make-local-variable 'compilation-parsing-end) 1)
338 (set (make-local-variable 'compilation-directory-stack) nil)
339 (setq compilation-last-buffer (current-buffer))
340 (run-hooks 'compilation-mode-hook))
342 ;; Called when compilation process changes state.
343 (defun compilation-sentinel (proc msg)
344 "Sentinel for compilation buffers."
345 (let ((buffer (process-buffer proc)))
346 (if (memq (process-status proc) '(signal exit))
347 (progn
348 (if (null (buffer-name buffer))
349 ;; buffer killed
350 (set-process-buffer proc nil)
351 (let ((obuf (current-buffer))
352 omax opoint)
353 ;; save-excursion isn't the right thing if
354 ;; process-buffer is current-buffer
355 (unwind-protect
356 (progn
357 ;; Write something in the compilation buffer
358 ;; and hack its mode line.
359 (set-buffer buffer)
360 (setq buffer-read-only nil)
361 (setq omax (point-max)
362 opoint (point))
363 (goto-char omax)
364 ;; Record where we put the message, so we can ignore it
365 ;; later on.
366 (insert ?\n mode-name " " msg)
367 (forward-char -1)
368 (insert " at " (substring (current-time-string) 0 19))
369 (forward-char 1)
370 (setq mode-line-process
371 (concat ": "
372 (symbol-name (process-status proc))))
373 ;; Since the buffer and mode line will show that the
374 ;; process is dead, we can delete it now. Otherwise it
375 ;; will stay around until M-x list-processes.
376 (delete-process proc)
377 ;; Force mode line redisplay soon.
378 (set-buffer-modified-p (buffer-modified-p))
379 (setq buffer-read-only t) ;I think is this wrong --roland
380 (if (and opoint (< opoint omax))
381 (goto-char opoint)))
382 (set-buffer obuf))
383 (if compilation-finish-function
384 (funcall compilation-finish-function buffer msg))
386 (setq compilation-in-progress (delq proc compilation-in-progress))
387 ))))
389 (defun kill-compilation ()
390 "Kill the process made by the \\[compile] command."
391 (interactive)
392 (let ((buffer (compilation-find-buffer)))
393 (if (get-buffer-process buffer)
394 (interrupt-process (get-buffer-process buffer))
395 (error "The compilation process is not running."))))
398 ;; Parse any new errors in the compilation buffer,
399 ;; or reparse from the beginning if the user has asked for that.
400 (defun compile-reinitialize-errors (argp)
401 (save-excursion
402 (set-buffer compilation-last-buffer)
403 ;; If we are out of errors, or if user says "reparse",
404 ;; discard the info we have, to force reparsing.
405 (if (or (eq compilation-error-list t)
406 (consp argp))
407 (progn (compilation-forget-errors)
408 (setq compilation-parsing-end 1)))
409 (if compilation-error-list
410 ;; Since compilation-error-list is non-nil, it points to a specific
411 ;; error the user wanted. So don't move it around.
413 (switch-to-buffer compilation-last-buffer)
414 (set-buffer-modified-p nil)
415 (let ((at-start (= compilation-parsing-end 1)))
416 (funcall compilation-parse-errors-function)
417 ;; Remember the entire list for compilation-forget-errors.
418 ;; If this is an incremental parse, append to previous list.
419 (if at-start
420 (setq compilation-old-error-list compilation-error-list)
421 (setq compilation-old-error-list
422 (nconc compilation-old-error-list compilation-error-list)))))))
424 (defun compile-goto-error (&optional argp)
425 "Visit the source for the error message point is on.
426 Use this command in a compilation log buffer.
427 C-u as a prefix arg means to reparse the buffer's error messages first;
428 other kinds of prefix arguments are ignored."
429 (interactive "P")
430 (or (compilation-buffer-p (current-buffer))
431 (error "Not in a compilation buffer."))
432 (setq compilation-last-buffer (current-buffer))
433 (compile-reinitialize-errors argp)
434 (save-excursion
435 (beginning-of-line)
436 ;; Move compilation-error-list to the elt of
437 ;; compilation-old-error-list whose car is the error we want.
438 (setq compilation-error-list
439 (memq (let (elt)
440 (while (not (or (setq elt (assoc (point-marker)
441 compilation-old-error-list))
442 (eobp)))
443 ;; This line doesn't contain an error.
444 ;; Move forward a line and look again.
445 (forward-line 1))
446 elt)
447 compilation-old-error-list)))
448 ;; Move to another window, so that next-error's window changes
449 ;; result in the desired setup.
450 (or (one-window-p)
451 (other-window -1))
452 (next-error 1))
454 (defun compilation-buffer-p (buffer)
455 (assq 'compilation-error-list (buffer-local-variables buffer)))
457 ;; Return a compilation buffer.
458 ;; If the current buffer is a compilation buffer, return it.
459 ;; If compilation-last-buffer is set to a live buffer, use that.
460 ;; Otherwise, look for a compilation buffer and signal an error
461 ;; if there are none.
462 (defun compilation-find-buffer (&optional other-buffer)
463 (if (and (not other-buffer)
464 (compilation-buffer-p (current-buffer)))
465 ;; The current buffer is a compilation buffer.
466 (current-buffer)
467 (if (and compilation-last-buffer (buffer-name compilation-last-buffer)
468 (or (not other-buffer) (not (eq compilation-last-buffer
469 (current-buffer)))))
470 compilation-last-buffer
471 (let ((buffers (buffer-list)))
472 (while (and buffers (or (not (compilation-buffer-p (car buffers)))
473 (and other-buffer
474 (eq (car buffers) (current-buffer)))))
475 (setq buffers (cdr buffers)))
476 (if buffers
477 (car buffers)
478 (or (and other-buffer
479 (compilation-buffer-p (current-buffer))
480 ;; The current buffer is a compilation buffer.
481 (progn
482 (if other-buffer
483 (message "This is the only compilation buffer."))
484 (current-buffer)))
485 (error "No compilation started!")))))))
487 ;;;###autoload
488 (defun next-error (&optional argp)
489 "Visit next compilation error message and corresponding source code.
490 This operates on the output from the \\[compile] command.
491 If all preparsed error messages have been processed,
492 the error message buffer is checked for new ones.
494 A prefix arg specifies how many error messages to move;
495 negative means move back to previous error messages.
496 Just C-u as a prefix means reparse the error message buffer
497 and start at the first error.
499 \\[next-error] normally applies to the most recent compilation started,
500 but as long as you are in the middle of parsing errors from one compilation
501 output buffer, you stay with that compilation output buffer.
503 Use \\[next-error] in a compilation output buffer to switch to
504 processing errors from that compilation.
506 See variables `compilation-parse-errors-function' and
507 \`compilation-error-regexp-alist' for customization ideas."
508 (interactive "P")
509 (setq compilation-last-buffer (compilation-find-buffer))
510 (compile-reinitialize-errors argp)
511 ;; Make ARGP nil if the prefix arg was just C-u,
512 ;; since that means to reparse the errors, which the
513 ;; compile-reinitialize-errors call just did.
514 ;; Now we are only interested in a numeric prefix arg.
515 (if (consp argp)
516 (setq argp nil))
517 (let (next-errors next-error)
518 (save-excursion
519 (set-buffer compilation-last-buffer)
520 (setq next-errors (nthcdr (+ (- (length compilation-old-error-list)
521 (length compilation-error-list)
523 (prefix-numeric-value argp))
524 compilation-old-error-list)
525 next-error (car next-errors))
526 (while
527 (progn
528 (if (null next-error)
529 (progn
530 (if argp (if (> (prefix-numeric-value argp) 0)
531 (error "Moved past last error")
532 (error "Moved back past first error")))
533 (compilation-forget-errors)
534 (error (concat compilation-error-message
535 (and (get-buffer-process (current-buffer))
536 (eq (process-status
537 (get-buffer-process
538 (current-buffer)))
539 'run)
540 " yet"))))
541 (setq compilation-error-list (cdr next-errors))
542 (if (null (cdr next-error))
543 ;; This error is boring. Go to the next.
545 (or (markerp (cdr next-error))
546 ;; This error has a filename/lineno pair.
547 ;; Find the file and turn it into a marker.
548 (let* ((fileinfo (car (cdr next-error)))
549 (buffer (compilation-find-file (cdr fileinfo)
550 (car fileinfo)
551 (car next-error))))
552 (if (null buffer)
553 ;; We can't find this error's file.
554 ;; Remove all errors in the same file.
555 (progn
556 (setq next-errors compilation-old-error-list)
557 (while next-errors
558 (and (consp (cdr (car next-errors)))
559 (equal (car (cdr (car next-errors)))
560 fileinfo)
561 (progn
562 (set-marker (car (car next-errors)) nil)
563 (setcdr (car next-errors) nil)))
564 (setq next-errors (cdr next-errors)))
565 ;; Look for the next error.
567 ;; We found the file. Get a marker for this error.
568 (set-buffer buffer)
569 (save-excursion
570 (save-restriction
571 (widen)
572 (let ((errors compilation-old-error-list)
573 (last-line (cdr (cdr next-error))))
574 (goto-line last-line)
575 (beginning-of-line)
576 (setcdr next-error (point-marker))
577 ;; Make all the other error messages referring
578 ;; to the same file have markers into the buffer.
579 (while errors
580 (and (consp (cdr (car errors)))
581 (equal (car (cdr (car errors))) fileinfo)
582 (let ((this (cdr (cdr (car errors))))
583 (lines (- (cdr (cdr (car errors)))
584 last-line)))
585 (if (eq selective-display t)
586 (if (< lines 0)
587 (re-search-backward "[\n\C-m]"
588 nil 'end
589 (- lines))
590 (re-search-forward "[\n\C-m]"
591 nil 'end
592 lines))
593 (forward-line lines))
594 (setq last-line this)
595 (setcdr (car errors) (point-marker))))
596 (setq errors (cdr errors)))))))))
597 ;; If we didn't get a marker for this error,
598 ;; go on to the next one.
599 (not (markerp (cdr next-error))))))
600 (setq next-errors compilation-error-list
601 next-error (car next-errors))))
603 ;; Skip over multiple error messages for the same source location,
604 ;; so the next C-x ` won't go to an error in the same place.
605 (while (and compilation-error-list
606 (equal (cdr (car compilation-error-list)) (cdr next-error)))
607 (setq compilation-error-list (cdr compilation-error-list)))
609 ;; We now have a marker for the position of the error.
610 (switch-to-buffer (marker-buffer (cdr next-error)))
611 (goto-char (cdr next-error))
612 ;; If narrowing got in the way of
613 ;; going to the right place, widen.
614 (or (= (point) (marker-position (cdr next-error)))
615 (progn
616 (widen)
617 (goto-char (cdr next-error))))
619 ;; Show compilation buffer in other window, scrolled to this error.
620 (let* ((pop-up-windows t)
621 (w (display-buffer (marker-buffer (car next-error)))))
622 (set-window-point w (car next-error))
623 (set-window-start w (car next-error)))))
625 ;;;###autoload
626 (define-key ctl-x-map "`" 'next-error)
628 ;; Find a buffer for file FILENAME.
629 ;; Search the directories in compilation-search-path.
630 ;; A nil in compilation-search-path means to try the
631 ;; current directory, which is passed in DIR.
632 ;; If FILENAME is not found at all, ask the user where to find it.
633 ;; Pop up the buffer containing MARKER and scroll to MARKER if we ask the user.
634 (defun compilation-find-file (filename dir marker)
635 (let ((dirs compilation-search-path)
636 result name)
637 (while (and dirs (null result))
638 (setq name (expand-file-name filename (or (car dirs) dir))
639 result (and (file-exists-p name)
640 (find-file-noselect name))
641 dirs (cdr dirs)))
642 (or result
643 ;; The file doesn't exist.
644 ;; Ask the user where to find it.
645 ;; If he hits C-g, then the next time he does
646 ;; next-error, he'll skip past it.
647 (progn
648 (let* ((pop-up-windows t)
649 (w (display-buffer (marker-buffer marker))))
650 (set-window-point w marker)
651 (set-window-start w marker))
652 (setq name
653 (expand-file-name
654 (read-file-name
655 (format "Find this error in: (default %s) "
656 filename) dir filename t)))
657 (if (file-directory-p name)
658 (setq name (concat (file-name-as-directory name) filename)))
659 (if (file-exists-p name)
660 (find-file-noselect name))))))
662 ;; Set compilation-error-list to nil, and unchain the markers that point to the
663 ;; error messages and their text, so that they no longer slow down gap motion.
664 ;; This would happen anyway at the next garbage collection, but it is better to
665 ;; do it the right away.
666 (defun compilation-forget-errors ()
667 (while compilation-old-error-list
668 (let ((next-error (car compilation-old-error-list)))
669 (set-marker (car next-error) nil)
670 (if (markerp (cdr next-error))
671 (set-marker (cdr next-error) nil)))
672 (setq compilation-old-error-list (cdr compilation-old-error-list)))
673 (setq compilation-error-list nil)
674 (while (cdr compilation-directory-stack)
675 (setq compilation-directory-stack (cdr compilation-directory-stack))))
678 (defun count-regexp-groupings (regexp)
679 "Return the number of \\( ... \\) groupings in REGEXP (a string)."
680 (let ((groupings 0)
681 (len (length regexp))
682 (i 0)
684 (while (< i len)
685 (setq c (aref regexp i)
686 i (1+ i))
687 (cond ((= c ?\[)
688 ;; Find the end of this [...].
689 (while (and (< i len)
690 (not (= (aref regexp i) ?\])))
691 (setq i (1+ i))))
692 ((= c ?\\)
693 (if (< i len)
694 (progn
695 (setq c (aref regexp i)
696 i (1+ i))
697 (if (= c ?\))
698 ;; We found the end of a grouping,
699 ;; so bump our counter.
700 (setq groupings (1+ groupings))))))))
701 groupings))
703 (defun compilation-parse-errors ()
704 "Parse the current buffer as grep, cc or lint error messages.
705 See variable `compilation-parse-errors-function' for the interface it uses."
706 (setq compilation-error-list nil)
707 (message "Parsing error messages...")
708 (let (text-buffer
709 regexp enter-group leave-group error-group
710 alist subexpr error-regexp-groups)
712 ;; Don't reparse messages already seen at last parse.
713 (goto-char compilation-parsing-end)
714 ;; Don't parse the first two lines as error messages.
715 ;; This matters for grep.
716 (if (bobp)
717 (forward-line 2))
719 ;; Compile all the regexps we want to search for into one.
720 (setq regexp (concat "\\(" compilation-enter-directory-regexp "\\)\\|"
721 "\\(" compilation-leave-directory-regexp "\\)\\|"
722 "\\(" (mapconcat (function
723 (lambda (elt)
724 (concat "\\(" (car elt) "\\)")))
725 compilation-error-regexp-alist
726 "\\|") "\\)"))
728 ;; Find out how many \(...\) groupings are in each of the regexps, and set
729 ;; *-GROUP to the grouping containing each constituent regexp (whose
730 ;; subgroups will come immediately thereafter) of the big regexp we have
731 ;; just constructed.
732 (setq enter-group 1
733 leave-group (+ enter-group
734 (count-regexp-groupings
735 compilation-enter-directory-regexp)
737 error-group (+ leave-group
738 (count-regexp-groupings
739 compilation-leave-directory-regexp)
742 ;; Compile an alist (IDX FILE LINE), where IDX is the number of the
743 ;; subexpression for an entire error-regexp, and FILE and LINE are the
744 ;; numbers for the subexpressions giving the file name and line number.
745 (setq alist compilation-error-regexp-alist
746 subexpr (1+ error-group))
747 (while alist
748 (setq error-regexp-groups (cons (list subexpr
749 (+ subexpr (nth 1 (car alist)))
750 (+ subexpr (nth 2 (car alist))))
751 error-regexp-groups))
752 (setq subexpr (+ subexpr 1 (count-regexp-groupings (car (car alist)))))
753 (setq alist (cdr alist)))
755 (while (re-search-forward regexp nil t)
756 ;; Figure out which constituent regexp matched.
757 (cond ((match-beginning enter-group)
758 ;; The match was the enter-directory regexp.
759 (let ((dir
760 (file-name-as-directory
761 (expand-file-name
762 (buffer-substring (match-beginning (+ enter-group 1))
763 (match-end (+ enter-group 1)))))))
764 (setq compilation-directory-stack
765 (cons dir compilation-directory-stack))
766 (and (file-directory-p dir)
767 (setq default-directory dir))))
769 ((match-beginning leave-group)
770 ;; The match was the leave-directory regexp.
771 (let ((beg (match-beginning (+ leave-group 1)))
772 (stack compilation-directory-stack))
773 (if beg
774 (let ((dir
775 (file-name-as-directory
776 (expand-file-name
777 (buffer-substring beg
778 (match-end (+ leave-group
779 1)))))))
780 (while (and stack
781 (not (string-equal (car stack) dir)))
782 (setq stack (cdr stack)))))
783 (setq compilation-directory-stack (cdr stack))
784 (setq stack (car compilation-directory-stack))
785 (if stack
786 (setq default-directory stack))
789 ((match-beginning error-group)
790 ;; The match was the composite error regexp.
791 ;; Find out which individual regexp matched.
792 (setq alist error-regexp-groups)
793 (while (and alist
794 (null (match-beginning (car (car alist)))))
795 (setq alist (cdr alist)))
796 (if alist
797 (setq alist (car alist))
798 (error "Impossible regexp match!"))
800 ;; Extract the file name and line number from the error message.
801 (let ((filename
802 (cons default-directory
803 (buffer-substring (match-beginning (nth 1 alist))
804 (match-end (nth 1 alist)))))
805 (linenum (save-restriction
806 (narrow-to-region
807 (match-beginning (nth 2 alist))
808 (match-end (nth 2 alist)))
809 (goto-char (point-min))
810 (if (looking-at "[0-9]")
811 (read (current-buffer))))))
812 ;; Locate the erring file and line.
813 ;; Cons a new elt onto compilation-error-list,
814 ;; giving a marker for the current compilation buffer
815 ;; location, and the file and line number of the error.
816 (save-excursion
817 (beginning-of-line 1)
818 (setq compilation-error-list
819 (cons (cons (point-marker)
820 (cons filename linenum))
821 compilation-error-list)))))
823 (error "Impossible regexp match!"))))
824 (setq compilation-parsing-end (point-max)))
825 (message "Parsing error messages...done")
826 (setq compilation-error-list (nreverse compilation-error-list)))
828 (define-key ctl-x-map "`" 'next-error)
830 (provide 'compile)
832 ;;; compile.el ends here