]> code.delx.au - gnu-emacs/blob - lisp/progmodes/compile.el
2136b6b939b558a50ea20880383b65d5aac8ecc6
[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 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 (defconst 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, that cons's car is a cons
45 \(DIRECTORY . FILE\), specifying the file the message is about, and its cdr
46 is the number of the line the message is about. Or its cdr may be nil if
47 that error is not interesting.
48
49 The value may be t instead of a list; this means that the buffer of
50 error messages should be reparsed the next time the list of errors is wanted.")
51
52 (defvar compilation-old-error-list nil
53 "Value of `compilation-error-list' after errors were parsed.")
54
55 (defvar compilation-parse-errors-function 'compilation-parse-errors
56 "Function to call to parse error messages from a compilation.
57 It takes args LIMIT-SEARCH and FIND-AT-LEAST.
58 If LIMIT-SEARCH is non-nil, don't bother parsing past that location.
59 If FIND-AT-LEAST is non-nil, don't bother parsing after finding that
60 many new erros.
61 It should read in the source files which have errors and set
62 `compilation-error-list' to a list with an element for each error message
63 found. See that variable for more info.")
64
65 ;;;###autoload
66 (defvar compilation-buffer-name-function nil
67 "Function to compute the name of a compilation buffer.
68 The function receives one argument, the name of the major mode of the
69 compilation buffer. It should return a string.
70 nil means compute the name with `(concat \"*\" (downcase major-mode) \"*\")'.")
71
72 ;;;###autoload
73 (defvar compilation-finish-function nil
74 "*Function to call when a compilation process finishes.
75 It is called with two arguments: the compilation buffer, and a string
76 describing how the process finished.")
77
78 (defvar compilation-last-buffer nil
79 "The most recent compilation buffer.
80 A buffer becomes most recent when its compilation is started
81 or when it is used with \\[next-error] or \\[compile-goto-error].")
82
83 (defvar compilation-in-progress nil
84 "List of compilation processes now running.")
85 (or (assq 'compilation-in-progress minor-mode-alist)
86 (setq minor-mode-alist (cons '(compilation-in-progress " Compiling")
87 minor-mode-alist)))
88
89 (defvar compilation-parsing-end nil
90 "Position of end of buffer when last error messages were parsed.")
91
92 (defvar compilation-error-message "No more errors"
93 "Message to print when no more matches are found.")
94
95 (defvar compilation-num-errors-found)
96
97 (defvar compilation-error-regexp-alist
98 '(
99 ;; NOTE! This first one is repeated in grep-regexp-alist, below.
100 ;; 4.3BSD grep, cc, lint pass 1:
101 ;; /usr/src/foo/foo.c(8): warning: w may be used before set
102 ;; or GNU utilities
103 ;; foo.c:8: error message
104 ;;
105 ;; We'll insist that the number be followed by a colon or closing
106 ;; paren, because otherwise this matches just about anything
107 ;; containing a number with spaces around it.
108 ("^\\([^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:)]" 1 2)
109 ;; 4.3BSD lint pass 2
110 ;; strcmp: variable # of args. llib-lc(359) :: /usr/src/foo/foo.c(8)
111 ("[ \t:]+\\([^:( \t\n]+\\)[ \t]*[:(]*(+[ \t]*\\([0-9]+\\))[:) \t]*$" 1 2)
112 ;; 4.3BSD lint pass 3
113 ;; bloofle defined( /users/wolfgang/foo.c(4) ), but never used
114 ;; This used to be
115 ;; ("[ \t(]+\\([^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]+" 1 2)
116 ;; which is regexp Impressionism - it matches almost anything!
117 ("([ \t]*\\([^:( \t\n]+\\)[ \t]*[:(][ \t]*\\([0-9]+\\))" 1 2)
118 ;; Line 45 of "foo.c": bloofel undefined (who does this?)
119 ("^[Ll]ine[ \t]+\\([0-9]+\\)[ \t]+of[ \t]+\"\\([^\"\n]+\\)\":" 2 1)
120 ;; Apollo cc, 4.3BSD fc
121 ;; "foo.f", line 3: Error: syntax error near end of statement
122 ("^\"\\([^\"\n]+\\)\", line \\([0-9]+\\):" 1 2)
123 ;; HP-UX 7.0 fc
124 ;; foo.f :16 some horrible error message
125 ("^\\([^ \t\n:]+\\)[ \t]*:\\([0-9]+\\)" 1 2)
126 ;; IBM AIX PS/2 C version 1.1
127 ;; ****** Error number 140 in line 8 of file errors.c ******
128 ("in line \\([0-9]+\\) of file \\([^ \n]+[^. \n]\\)\\.? " 2 1)
129 ;; IBM AIX lint is too painful to do right this way. File name
130 ;; prefixes entire sections rather than being on each line.
131
132 ;; MIPS RISC CC - the one distributed with Ultrix.
133 ("^[^,\n]+: \\([^,\n]+\\), line \\([0-9]+\\):" 1 2)
134 )
135 "Alist that specifies how to match errors in compiler output.
136 Each element has the form (REGEXP FILE-IDX LINE-IDX).
137 If REGEXP matches, the FILE-IDX'th subexpression gives the file
138 name, and the LINE-IDX'th subexpression gives the line number.")
139
140 (defvar grep-regexp-alist
141 '(("^\\([^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2))
142 "Regexp used to match grep hits. See `compilation-error-regexp-alist'.")
143
144 ;;;###autoload
145 (defvar compilation-search-path '(nil)
146 "*List of directories to search for source files named in error messages.
147 Elements should be directory names, not file names of directories.
148 nil as an element means to try the default directory.")
149
150 (defvar compile-command "make -k "
151 "Last shell command used to do a compilation; default for next compilation.
152
153 Sometimes it is useful for files to supply local values for this variable.
154 You might also use mode hooks to specify it in certain modes, like this:
155
156 (setq c-mode-hook
157 '(lambda () (or (file-exists-p \"makefile\") (file-exists-p \"Makefile\")
158 (progn (make-local-variable 'compile-command)
159 (setq compile-command
160 (concat \"make -k \"
161 buffer-file-name))))))")
162
163 (defconst compilation-enter-directory-regexp
164 ": Entering directory `\\(.*\\)'$"
165 "Regular expression matching lines that indicate a new current directory.
166 This must contain one \\(, \\) pair around the directory name.
167
168 The default value matches lines printed by the `-w' option of GNU Make.")
169
170 (defconst compilation-leave-directory-regexp
171 ": Leaving directory `\\(.*\\)'$"
172 "Regular expression matching lines that indicate restoring current directory.
173 This may contain one \\(, \\) pair around the name of the directory
174 being moved from. If it does not, the last directory entered \(by a
175 line matching `compilation-enter-directory-regexp'\) is assumed.
176
177 The default value matches lines printed by the `-w' option of GNU Make.")
178
179 (defvar compilation-directory-stack nil
180 "Stack of previous directories for `compilation-leave-directory-regexp'.
181 The head element is the directory the compilation was started in.")
182
183 ;; History of compile commands.
184 (defvar compile-history nil)
185 ;; History of grep commands.
186 (defvar grep-history nil)
187
188 ;;;###autoload
189 (defun compile (command)
190 "Compile the program including the current buffer. Default: run `make'.
191 Runs COMMAND, a shell command, in a separate process asynchronously
192 with output going to the buffer `*compilation*'.
193
194 You can then use the command \\[next-error] to find the next error message
195 and move to the source code that caused it.
196
197 To run more than one compilation at once, start one and rename the
198 \`*compilation*' buffer to some other name with \\[rename-buffer].
199 Then start the next one.
200
201 The name used for the buffer is actually whatever is returned by
202 the function in `compilation-buffer-name-function', so you can set that
203 to a function that generates a unique name."
204 (interactive (list (read-from-minibuffer "Compile command: "
205 compile-command nil nil
206 '(compile-history . 1))))
207 (setq compile-command command)
208 (save-some-buffers nil nil)
209 (compile-internal compile-command "No more errors"))
210
211 ;;;###autoload
212 (defun grep (command-args)
213 "Run grep, with user-specified args, and collect output in a buffer.
214 While grep runs asynchronously, you can use the \\[next-error] command
215 to find the text that grep hits refer to.
216
217 This command uses a special history list for its arguments, so you can
218 easily repeat a grep command."
219 (interactive
220 (list (read-from-minibuffer "Run grep (like this): "
221 "grep -n " nil nil 'grep-history)))
222 (compile-internal (concat command-args " /dev/null")
223 "No more grep hits" "grep"
224 ;; Give it a simpler regexp to match.
225 nil grep-regexp-alist))
226
227 (defun compile-internal (command error-message
228 &optional name-of-mode parser regexp-alist
229 name-function)
230 "Run compilation command COMMAND (low level interface).
231 ERROR-MESSAGE is a string to print if the user asks to see another error
232 and there are no more errors. Third argument NAME-OF-MODE is the name
233 to display as the major mode in the compilation buffer.
234
235 Fourth arg PARSER is the error parser function (nil means the default). Fifth
236 arg REGEXP-ALIST is the error message regexp alist to use (nil means the
237 default). Sixth arg NAME-FUNCTION is a function called to name the buffer (nil
238 means the default). The defaults for these variables are the global values of
239 \`compilation-parse-errors-function', `compilation-error-regexp-alist', and
240 \`compilation-buffer-name-function', respectively.
241
242 Returns the compilation buffer created."
243 (let (outbuf)
244 (save-excursion
245 (or name-of-mode
246 (setq name-of-mode "Compilation"))
247 (setq outbuf
248 (get-buffer-create
249 (funcall (or name-function compilation-buffer-name-function
250 (function (lambda (mode)
251 (concat "*" (downcase mode) "*"))))
252 name-of-mode)))
253 (set-buffer outbuf)
254 (let ((comp-proc (get-buffer-process (current-buffer))))
255 (if comp-proc
256 (if (or (not (eq (process-status comp-proc) 'run))
257 (yes-or-no-p
258 (format "A %s process is running; kill it? "
259 name-of-mode)))
260 (condition-case ()
261 (progn
262 (interrupt-process comp-proc)
263 (sit-for 1)
264 (delete-process comp-proc))
265 (error nil))
266 (error "Cannot have two processes in `%s' at once"
267 (buffer-name))
268 )))
269 ;; In case the compilation buffer is current, make sure we get the global
270 ;; values of compilation-error-regexp-alist, etc.
271 (kill-all-local-variables))
272 (let ((regexp-alist (or regexp-alist compilation-error-regexp-alist))
273 (parser (or parser compilation-parse-errors-function))
274 (thisdir default-directory)
275 outwin)
276 (save-excursion
277 ;; Clear out the compilation buffer and make it writable.
278 ;; Change its default-directory to the directory where the compilation
279 ;; will happen, and insert a `cd' command to indicate this.
280 (set-buffer outbuf)
281 (setq buffer-read-only nil)
282 (erase-buffer)
283 (setq default-directory thisdir)
284 (insert "cd " thisdir "\n" command "\n")
285 (set-buffer-modified-p nil))
286 ;; If we're already in the compilation buffer, go to the end
287 ;; of the buffer, so point will track the compilation output.
288 (if (eq outbuf (current-buffer))
289 (goto-char (point-max)))
290 ;; Pop up the compilation buffer.
291 (setq outwin (display-buffer outbuf))
292 (set-buffer outbuf)
293 (compilation-mode)
294 (buffer-disable-undo (current-buffer))
295 (setq buffer-read-only t)
296 (set (make-local-variable 'compilation-parse-errors-function) parser)
297 (set (make-local-variable 'compilation-error-message) error-message)
298 (set (make-local-variable 'compilation-error-regexp-alist) regexp-alist)
299 (setq default-directory thisdir
300 compilation-directory-stack (list default-directory))
301 (set-window-start outwin (point-min))
302 (setq mode-name name-of-mode)
303 (or (eq outwin (selected-window))
304 (set-window-point outwin (point-min)))
305 (and compilation-window-height
306 (= (window-width outwin) (frame-width))
307 (let ((w (selected-window)))
308 (unwind-protect
309 (progn
310 (select-window outwin)
311 (enlarge-window (- compilation-window-height
312 (window-height))))
313 (select-window w))))
314 ;; Start the compilation.
315 (let ((proc (start-process-shell-command (downcase mode-name)
316 outbuf
317 command)))
318 (set-process-sentinel proc 'compilation-sentinel)
319 (setq compilation-in-progress (cons proc compilation-in-progress))))
320 ;; Make it so the next C-x ` will use this buffer.
321 (setq compilation-last-buffer outbuf)))
322
323 (defvar compilation-mode-map
324 (let ((map (make-sparse-keymap)))
325 (define-key map "\C-c\C-c" 'compile-goto-error)
326 (define-key map "\C-c\C-k" 'kill-compilation)
327 (define-key map " " 'scroll-up)
328 (define-key map "\^?" 'scroll-down)
329 (define-key map "\M-n" 'compilation-next-error)
330 (define-key map "\M-p" 'compilation-previous-error)
331 (define-key map "\M-{" 'compilation-previous-file)
332 (define-key map "\M-}" 'compilation-next-file)
333 map)
334 "Keymap for compilation log buffers.")
335
336 (defun compilation-mode ()
337 "Major mode for compilation log buffers.
338 \\<compilation-mode-map>To visit the source for a line-numbered error,
339 move point to the error message line and type \\[compile-goto-error].
340 To kill the compilation, type \\[kill-compilation].
341
342 Runs `compilation-mode-hook' with `run-hooks' (which see)."
343 (interactive)
344 (fundamental-mode)
345 (use-local-map compilation-mode-map)
346 (setq major-mode 'compilation-mode)
347 (setq mode-name "Compilation")
348 ;; Make buffer's mode line show process state
349 (setq mode-line-process '(": %s"))
350 (set (make-local-variable 'compilation-error-list) nil)
351 (set (make-local-variable 'compilation-old-error-list) nil)
352 (set (make-local-variable 'compilation-parsing-end) 1)
353 (set (make-local-variable 'compilation-directory-stack) nil)
354 (setq compilation-last-buffer (current-buffer))
355 (run-hooks 'compilation-mode-hook))
356
357 ;; Called when compilation process changes state.
358 (defun compilation-sentinel (proc msg)
359 "Sentinel for compilation buffers."
360 (let ((buffer (process-buffer proc)))
361 (if (memq (process-status proc) '(signal exit))
362 (progn
363 (if (null (buffer-name buffer))
364 ;; buffer killed
365 (set-process-buffer proc nil)
366 (let ((obuf (current-buffer))
367 omax opoint)
368 ;; save-excursion isn't the right thing if
369 ;; process-buffer is current-buffer
370 (unwind-protect
371 (progn
372 ;; Write something in the compilation buffer
373 ;; and hack its mode line.
374 (set-buffer buffer)
375 (let ((buffer-read-only nil))
376 (setq omax (point-max)
377 opoint (point))
378 (goto-char omax)
379 ;; Record where we put the message, so we can ignore it
380 ;; later on.
381 (insert ?\n mode-name " " msg)
382 (forward-char -1)
383 (insert " at " (substring (current-time-string) 0 19))
384 (forward-char 1)
385 (setq mode-line-process
386 (concat ": "
387 (symbol-name (process-status proc))))
388 ;; Since the buffer and mode line will show that the
389 ;; process is dead, we can delete it now. Otherwise it
390 ;; will stay around until M-x list-processes.
391 (delete-process proc)
392 ;; Force mode line redisplay soon.
393 (set-buffer-modified-p (buffer-modified-p)))
394 (if (and opoint (< opoint omax))
395 (goto-char opoint))
396 (if compilation-finish-function
397 (funcall compilation-finish-function buffer msg)))
398 (set-buffer obuf))))
399 (setq compilation-in-progress (delq proc compilation-in-progress))
400 ))))
401
402 ;; Return the cdr of compilation-old-error-list for the error containing point.
403 (defun compile-error-at-point ()
404 (compile-reinitialize-errors nil (point))
405 (let ((errors compilation-old-error-list))
406 (while (and errors
407 (> (point) (car (car errors))))
408 (setq errors (cdr errors)))
409 errors))
410
411 (defun compilation-next-error (n)
412 "Move point to the next error in the compilation buffer.
413 Does NOT find the source line like \\[next-error]."
414 (interactive "p")
415 (or (compilation-buffer-p (current-buffer))
416 (error "Not in a compilation buffer."))
417 (setq compilation-last-buffer (current-buffer))
418
419 (let ((errors (compile-error-at-point)))
420
421 ;; Move to the error after the one containing point.
422 (goto-char (car (if (< n 0)
423 (let ((i 0)
424 (e compilation-old-error-list))
425 ;; See how many cdrs away ERRORS is from the start.
426 (while (not (eq e errors))
427 (setq i (1+ i)
428 e (cdr e)))
429 (if (> (- n) i)
430 (error "Moved back past first error")
431 (nth (+ i n) compilation-old-error-list)))
432 (let ((compilation-error-list (cdr errors)))
433 (compile-reinitialize-errors nil nil n)
434 (if compilation-error-list
435 (nth (1- n) compilation-error-list)
436 (error "Moved past last error"))))))))
437
438 (defun compilation-previous-error (n)
439 "Move point to the previous error in the compilation buffer.
440 Does NOT find the source line like \\[next-error]."
441 (interactive "p")
442 (compilation-next-error (- n)))
443
444
445 (defun compile-file-of-error (data)
446 (setq data (cdr data))
447 (if (markerp data)
448 (buffer-file-name (marker-buffer data))
449 (setq data (car data))
450 (expand-file-name (cdr data) (car data))))
451
452 (defun compilation-next-file (n)
453 "Move point to the next error for a different file than the current one."
454 (interactive "p")
455 (or (compilation-buffer-p (current-buffer))
456 (error "Not in a compilation buffer."))
457 (setq compilation-last-buffer (current-buffer))
458
459 (let ((reversed (< n 0))
460 errors file)
461
462 (if (not reversed)
463 (setq errors (or (compile-error-at-point)
464 (error "Moved past last error")))
465
466 ;; Get a reversed list of the errors up through the one containing point.
467 (compile-reinitialize-errors nil (point))
468 (setq errors (reverse compilation-old-error-list)
469 n (- n))
470
471 ;; Ignore errors after point. (car ERRORS) will be the error
472 ;; containing point, (cadr ERRORS) the one before it.
473 (while (and errors
474 (< (point) (car (car errors))))
475 (setq errors (cdr errors))))
476
477 (while (> n 0)
478 (setq file (compile-file-of-error (car errors)))
479
480 ;; Skip past the other errors for this file.
481 (while (string= file
482 (compile-file-of-error
483 (car (or errors
484 (if reversed
485 (error "%s the first erring file" file)
486 (let ((compilation-error-list nil))
487 ;; Parse some more.
488 (compile-reinitialize-errors nil nil 2)
489 (setq errors compilation-error-list)))
490 (error "%s is the last erring file" file)))))
491 (setq errors (cdr errors)))
492
493 (setq n (1- n)))
494
495 ;; Move to the following error.
496 (goto-char (car (car (or errors
497 (if reversed
498 (error "This is the first erring file")
499 (let ((compilation-error-list nil))
500 ;; Parse the last one.
501 (compile-reinitialize-errors nil nil 1)
502 compilation-error-list))))))))
503
504 (defun compilation-previous-file (n)
505 "Move point to the previous error for a different file than the current one."
506 (interactive "p")
507 (compilation-next-file (- n)))
508
509
510 (defun kill-compilation ()
511 "Kill the process made by the \\[compile] command."
512 (interactive)
513 (let ((buffer (compilation-find-buffer)))
514 (if (get-buffer-process buffer)
515 (interrupt-process (get-buffer-process buffer))
516 (error "The compilation process is not running."))))
517
518
519 ;; Parse any new errors in the compilation buffer,
520 ;; or reparse from the beginning if the user has asked for that.
521 (defun compile-reinitialize-errors (argp &optional limit-search find-at-least)
522 (save-excursion
523 (set-buffer compilation-last-buffer)
524 ;; If we are out of errors, or if user says "reparse",
525 ;; discard the info we have, to force reparsing.
526 (if (or (eq compilation-error-list t)
527 (consp argp))
528 (progn (compilation-forget-errors)
529 (setq compilation-parsing-end 1)))
530 (if (and compilation-error-list
531 (or (not limit-search)
532 (> compilation-parsing-end limit-search))
533 (or (not find-at-least)
534 (> (length compilation-error-list) find-at-least)))
535 ;; Since compilation-error-list is non-nil, it points to a specific
536 ;; error the user wanted. So don't move it around.
537 nil
538 (switch-to-buffer compilation-last-buffer)
539 (set-buffer-modified-p nil)
540 (if (< compilation-parsing-end (point-max))
541 (let ((at-start (= compilation-parsing-end 1)))
542 (funcall compilation-parse-errors-function
543 limit-search find-at-least)
544 ;; Remember the entire list for compilation-forget-errors.
545 ;; If this is an incremental parse, append to previous list.
546 (if at-start
547 (setq compilation-old-error-list compilation-error-list)
548 (setq compilation-old-error-list
549 (nconc compilation-old-error-list compilation-error-list)))
550 )))))
551
552 (defun compile-goto-error (&optional argp)
553 "Visit the source for the error message point is on.
554 Use this command in a compilation log buffer.
555 \\[universal-argument] as a prefix arg means to reparse the buffer's error messages first;
556 other kinds of prefix arguments are ignored."
557 (interactive "P")
558 (or (compilation-buffer-p (current-buffer))
559 (error "Not in a compilation buffer."))
560 (setq compilation-last-buffer (current-buffer))
561 (compile-reinitialize-errors argp (point))
562
563 ;; Move to bol; the marker for the error on this line will point there.
564 (beginning-of-line)
565
566 ;; Move compilation-error-list to the elt of compilation-old-error-list
567 ;; we want.
568 (setq compilation-error-list compilation-old-error-list)
569 (while (and compilation-error-list
570 (> (point) (car (car compilation-error-list))))
571 (setq compilation-error-list (cdr compilation-error-list)))
572
573 ;; Move to another window, so that next-error's window changes
574 ;; result in the desired setup.
575 (or (one-window-p)
576 (progn
577 (other-window -1)
578 ;; other-window changed the selected buffer,
579 ;; but we didn't want to do that.
580 (set-buffer compilation-last-buffer)))
581
582 (next-error 1))
583
584 (defun compilation-buffer-p (buffer)
585 (assq 'compilation-error-list (buffer-local-variables buffer)))
586
587 ;; Return a compilation buffer.
588 ;; If the current buffer is a compilation buffer, return it.
589 ;; If compilation-last-buffer is set to a live buffer, use that.
590 ;; Otherwise, look for a compilation buffer and signal an error
591 ;; if there are none.
592 (defun compilation-find-buffer (&optional other-buffer)
593 (if (and (not other-buffer)
594 (compilation-buffer-p (current-buffer)))
595 ;; The current buffer is a compilation buffer.
596 (current-buffer)
597 (if (and compilation-last-buffer (buffer-name compilation-last-buffer)
598 (or (not other-buffer) (not (eq compilation-last-buffer
599 (current-buffer)))))
600 compilation-last-buffer
601 (let ((buffers (buffer-list)))
602 (while (and buffers (or (not (compilation-buffer-p (car buffers)))
603 (and other-buffer
604 (eq (car buffers) (current-buffer)))))
605 (setq buffers (cdr buffers)))
606 (if buffers
607 (car buffers)
608 (or (and other-buffer
609 (compilation-buffer-p (current-buffer))
610 ;; The current buffer is a compilation buffer.
611 (progn
612 (if other-buffer
613 (message "This is the only compilation buffer."))
614 (current-buffer)))
615 (error "No compilation started!")))))))
616
617 ;;;###autoload
618 (defun next-error (&optional argp)
619 "Visit next compilation error message and corresponding source code.
620 This operates on the output from the \\[compile] command.
621 If all preparsed error messages have been processed,
622 the error message buffer is checked for new ones.
623
624 A prefix arg specifies how many error messages to move;
625 negative means move back to previous error messages.
626 Just C-u as a prefix means reparse the error message buffer
627 and start at the first error.
628
629 \\[next-error] normally applies to the most recent compilation started,
630 but as long as you are in the middle of parsing errors from one compilation
631 output buffer, you stay with that compilation output buffer.
632
633 Use \\[next-error] in a compilation output buffer to switch to
634 processing errors from that compilation.
635
636 See variables `compilation-parse-errors-function' and
637 \`compilation-error-regexp-alist' for customization ideas."
638 (interactive "P")
639 (setq compilation-last-buffer (compilation-find-buffer))
640 (compile-reinitialize-errors argp nil
641 ;; We want to pass a number here only if
642 ;; we got a numeric prefix arg, not just C-u.
643 (and (not (consp argp))
644 (1- (prefix-numeric-value argp))))
645 ;; Make ARGP nil if the prefix arg was just C-u,
646 ;; since that means to reparse the errors, which the
647 ;; compile-reinitialize-errors call just did.
648 ;; Now we are only interested in a numeric prefix arg.
649 (if (consp argp)
650 (setq argp nil))
651 (let (next-errors next-error)
652 (save-excursion
653 (set-buffer compilation-last-buffer)
654 ;; compilation-error-list points to the "current" error.
655 (setq next-errors (nthcdr (1- (prefix-numeric-value argp))
656 compilation-error-list)
657 next-error (car next-errors))
658 (while
659 (progn
660 (if (null next-error)
661 (progn
662 (if argp (if (> (prefix-numeric-value argp) 0)
663 (error "Moved past last error")
664 (error "Moved back past first error")))
665 (compilation-forget-errors)
666 (error (concat compilation-error-message
667 (and (get-buffer-process (current-buffer))
668 (eq (process-status
669 (get-buffer-process
670 (current-buffer)))
671 'run)
672 " yet"))))
673 (setq compilation-error-list (cdr next-errors))
674 (if (null (cdr next-error))
675 ;; This error is boring. Go to the next.
676 t
677 (or (markerp (cdr next-error))
678 ;; This error has a filename/lineno pair.
679 ;; Find the file and turn it into a marker.
680 (let* ((fileinfo (car (cdr next-error)))
681 (buffer (compilation-find-file (cdr fileinfo)
682 (car fileinfo)
683 (car next-error))))
684 (if (null buffer)
685 ;; We can't find this error's file.
686 ;; Remove all errors in the same file.
687 (progn
688 (setq next-errors compilation-old-error-list)
689 (while next-errors
690 (and (consp (cdr (car next-errors)))
691 (equal (car (cdr (car next-errors)))
692 fileinfo)
693 (progn
694 (set-marker (car (car next-errors)) nil)
695 (setcdr (car next-errors) nil)))
696 (setq next-errors (cdr next-errors)))
697 ;; Look for the next error.
698 t)
699 ;; We found the file. Get a marker for this error.
700 ;; compilation-old-error-list is a buffer-local
701 ;; variable, so we must be careful to extract its value
702 ;; before switching to the source file buffer.
703 (let ((errors compilation-old-error-list)
704 (last-line (cdr (cdr next-error))))
705 (set-buffer buffer)
706 (save-excursion
707 (save-restriction
708 (widen)
709 (goto-line last-line)
710 (beginning-of-line)
711 (setcdr next-error (point-marker))
712 ;; Make all the other error messages referring
713 ;; to the same file have markers into the buffer.
714 (while errors
715 (and (consp (cdr (car errors)))
716 (equal (car (cdr (car errors))) fileinfo)
717 (let ((this (cdr (cdr (car errors))))
718 (lines (- (cdr (cdr (car errors)))
719 last-line)))
720 (if (eq selective-display t)
721 (if (< lines 0)
722 (re-search-backward "[\n\C-m]"
723 nil 'end
724 (- lines))
725 (re-search-forward "[\n\C-m]"
726 nil 'end
727 lines))
728 (forward-line lines))
729 (setq last-line this)
730 (setcdr (car errors) (point-marker))))
731 (setq errors (cdr errors)))))))))
732 ;; If we didn't get a marker for this error,
733 ;; go on to the next one.
734 (not (markerp (cdr next-error))))))
735 (setq next-errors compilation-error-list
736 next-error (car next-errors))))
737
738 ;; Skip over multiple error messages for the same source location,
739 ;; so the next C-x ` won't go to an error in the same place.
740 (while (and compilation-error-list
741 (equal (cdr (car compilation-error-list)) (cdr next-error)))
742 (setq compilation-error-list (cdr compilation-error-list)))
743
744 ;; We now have a marker for the position of the error.
745 (switch-to-buffer (marker-buffer (cdr next-error)))
746 (goto-char (cdr next-error))
747 ;; If narrowing got in the way of
748 ;; going to the right place, widen.
749 (or (= (point) (marker-position (cdr next-error)))
750 (progn
751 (widen)
752 (goto-char (cdr next-error))))
753
754 ;; Show compilation buffer in other window, scrolled to this error.
755 (let* ((pop-up-windows t)
756 (w (display-buffer (marker-buffer (car next-error)))))
757 (set-window-point w (car next-error))
758 (set-window-start w (car next-error)))))
759
760 ;;;###autoload
761 (define-key ctl-x-map "`" 'next-error)
762
763 ;; Find a buffer for file FILENAME.
764 ;; Search the directories in compilation-search-path.
765 ;; A nil in compilation-search-path means to try the
766 ;; current directory, which is passed in DIR.
767 ;; If FILENAME is not found at all, ask the user where to find it.
768 ;; Pop up the buffer containing MARKER and scroll to MARKER if we ask the user.
769 (defun compilation-find-file (filename dir marker)
770 (let ((dirs compilation-search-path)
771 result name)
772 (while (and dirs (null result))
773 (setq name (expand-file-name filename (or (car dirs) dir))
774 result (and (file-exists-p name)
775 (find-file-noselect name))
776 dirs (cdr dirs)))
777 (or result
778 ;; The file doesn't exist.
779 ;; Ask the user where to find it.
780 ;; If he hits C-g, then the next time he does
781 ;; next-error, he'll skip past it.
782 (progn
783 (let* ((pop-up-windows t)
784 (w (display-buffer (marker-buffer marker))))
785 (set-window-point w marker)
786 (set-window-start w marker))
787 (setq name
788 (expand-file-name
789 (read-file-name
790 (format "Find this error in: (default %s) "
791 filename) dir filename t)))
792 (if (file-directory-p name)
793 (setq name (concat (file-name-as-directory name) filename)))
794 (if (file-exists-p name)
795 (find-file-noselect name))))))
796
797 ;; Set compilation-error-list to nil, and unchain the markers that point to the
798 ;; error messages and their text, so that they no longer slow down gap motion.
799 ;; This would happen anyway at the next garbage collection, but it is better to
800 ;; do it right away.
801 (defun compilation-forget-errors ()
802 (while compilation-old-error-list
803 (let ((next-error (car compilation-old-error-list)))
804 (set-marker (car next-error) nil)
805 (if (markerp (cdr next-error))
806 (set-marker (cdr next-error) nil)))
807 (setq compilation-old-error-list (cdr compilation-old-error-list)))
808 (setq compilation-error-list nil
809 compilation-directory-stack nil))
810
811
812 (defun count-regexp-groupings (regexp)
813 "Return the number of \\( ... \\) groupings in REGEXP (a string)."
814 (let ((groupings 0)
815 (len (length regexp))
816 (i 0)
817 c)
818 (while (< i len)
819 (setq c (aref regexp i)
820 i (1+ i))
821 (cond ((= c ?\[)
822 ;; Find the end of this [...].
823 (while (and (< i len)
824 (not (= (aref regexp i) ?\])))
825 (setq i (1+ i))))
826 ((= c ?\\)
827 (if (< i len)
828 (progn
829 (setq c (aref regexp i)
830 i (1+ i))
831 (if (= c ?\))
832 ;; We found the end of a grouping,
833 ;; so bump our counter.
834 (setq groupings (1+ groupings))))))))
835 groupings))
836
837 (defun compilation-parse-errors (limit-search find-at-least)
838 "Parse the current buffer as grep, cc or lint error messages.
839 See variable `compilation-parse-errors-function' for the interface it uses."
840 (setq compilation-error-list nil)
841 (message "Parsing error messages...")
842 (let (text-buffer
843 regexp enter-group leave-group error-group
844 alist subexpr error-regexp-groups
845 (found-desired nil)
846 (compilation-num-errors-found 0))
847
848 ;; Don't reparse messages already seen at last parse.
849 (goto-char compilation-parsing-end)
850 ;; Don't parse the first two lines as error messages.
851 ;; This matters for grep.
852 (if (bobp)
853 (forward-line 2))
854
855 ;; Compile all the regexps we want to search for into one.
856 (setq regexp (concat "\\(" compilation-enter-directory-regexp "\\)\\|"
857 "\\(" compilation-leave-directory-regexp "\\)\\|"
858 "\\(" (mapconcat (function
859 (lambda (elt)
860 (concat "\\(" (car elt) "\\)")))
861 compilation-error-regexp-alist
862 "\\|") "\\)"))
863
864 ;; Find out how many \(...\) groupings are in each of the regexps, and set
865 ;; *-GROUP to the grouping containing each constituent regexp (whose
866 ;; subgroups will come immediately thereafter) of the big regexp we have
867 ;; just constructed.
868 (setq enter-group 1
869 leave-group (+ enter-group
870 (count-regexp-groupings
871 compilation-enter-directory-regexp)
872 1)
873 error-group (+ leave-group
874 (count-regexp-groupings
875 compilation-leave-directory-regexp)
876 1))
877
878 ;; Compile an alist (IDX FILE LINE), where IDX is the number of the
879 ;; subexpression for an entire error-regexp, and FILE and LINE are the
880 ;; numbers for the subexpressions giving the file name and line number.
881 (setq alist compilation-error-regexp-alist
882 subexpr (1+ error-group))
883 (while alist
884 (setq error-regexp-groups (cons (list subexpr
885 (+ subexpr (nth 1 (car alist)))
886 (+ subexpr (nth 2 (car alist))))
887 error-regexp-groups))
888 (setq subexpr (+ subexpr 1 (count-regexp-groupings (car (car alist)))))
889 (setq alist (cdr alist)))
890
891 (while (and (not found-desired)
892 ;; We don't just pass LIMIT-SEARCH to re-search-forward
893 ;; because we want to find matches containing LIMIT-SEARCH
894 ;; but which extend past it.
895 (re-search-forward regexp nil t))
896
897 ;; Figure out which constituent regexp matched.
898 (cond ((match-beginning enter-group)
899 ;; The match was the enter-directory regexp.
900 (let ((dir
901 (file-name-as-directory
902 (expand-file-name
903 (buffer-substring (match-beginning (+ enter-group 1))
904 (match-end (+ enter-group 1)))))))
905 (setq compilation-directory-stack
906 (cons dir compilation-directory-stack))
907 (and (file-directory-p dir)
908 (setq default-directory dir))))
909
910 ((match-beginning leave-group)
911 ;; The match was the leave-directory regexp.
912 (let ((beg (match-beginning (+ leave-group 1)))
913 (stack compilation-directory-stack))
914 (if beg
915 (let ((dir
916 (file-name-as-directory
917 (expand-file-name
918 (buffer-substring beg
919 (match-end (+ leave-group
920 1)))))))
921 (while (and stack
922 (not (string-equal (car stack) dir)))
923 (setq stack (cdr stack)))))
924 (setq compilation-directory-stack (cdr stack))
925 (setq stack (car compilation-directory-stack))
926 (if stack
927 (setq default-directory stack))
928 ))
929
930 ((match-beginning error-group)
931 ;; The match was the composite error regexp.
932 ;; Find out which individual regexp matched.
933 (setq alist error-regexp-groups)
934 (while (and alist
935 (null (match-beginning (car (car alist)))))
936 (setq alist (cdr alist)))
937 (if alist
938 (setq alist (car alist))
939 (error "compilation-parse-errors: Impossible regexp match!"))
940
941 ;; Extract the file name and line number from the error message.
942 (let ((beginning-of-match (match-beginning 0)) ;looking-at nukes
943 (filename
944 (cons default-directory
945 (buffer-substring (match-beginning (nth 1 alist))
946 (match-end (nth 1 alist)))))
947 (linenum (save-restriction
948 (narrow-to-region
949 (match-beginning (nth 2 alist))
950 (match-end (nth 2 alist)))
951 (goto-char (point-min))
952 (if (looking-at "[0-9]")
953 (read (current-buffer))))))
954 ;; Locate the erring file and line.
955 ;; Cons a new elt onto compilation-error-list,
956 ;; giving a marker for the current compilation buffer
957 ;; location, and the file and line number of the error.
958 (save-excursion
959 (beginning-of-line 1)
960 (setq compilation-error-list
961 (cons (cons (point-marker)
962 (cons filename linenum))
963 compilation-error-list)))
964 (setq compilation-num-errors-found
965 (1+ compilation-num-errors-found))
966 (and find-at-least (>= compilation-num-errors-found
967 find-at-least)
968 ;; We have found as many new errors as the user wants.
969 ;; We continue to parse until we have seen all
970 ;; the consecutive errors in the same file,
971 ;; so the error positions will be recorded as markers
972 ;; in this buffer that might change.
973 (cdr compilation-error-list) ; Must check at least two.
974 (not (equal (car (cdr (nth 0 compilation-error-list)))
975 (car (cdr (nth 1 compilation-error-list)))))
976 (progn
977 ;; Discard the error just parsed, so that the next
978 ;; parsing run can get it and the following errors in
979 ;; the same file all at once. If we didn't do this, we
980 ;; would have the same problem we are trying to avoid
981 ;; with the test above, just delayed until the next run!
982 (setq compilation-error-list
983 (cdr compilation-error-list))
984 (goto-char beginning-of-match)
985 (setq found-desired t)))
986 )
987 )
988 (t
989 (error "compilation-parse-errors: impossible regexp match!")))
990
991 (message "Parsing error messages...%d (%d%% of buffer)"
992 compilation-num-errors-found
993 (/ (* 100 (point)) (point-max)))
994
995 (and limit-search (>= (point) limit-search)
996 ;; The user wanted a specific error, and we're past it.
997 (setq found-desired t)))
998 (setq compilation-parsing-end (if found-desired
999 (point)
1000 ;; We have searched the whole buffer.
1001 (point-max))))
1002 (setq compilation-error-list (nreverse compilation-error-list))
1003 (message "Parsing error messages...done"))
1004
1005 (define-key ctl-x-map "`" 'next-error)
1006
1007 (provide 'compile)
1008
1009 ;;; compile.el ends here