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