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