compile.el 50 KB
Newer Older
Jim Blandy's avatar
Jim Blandy committed
1
;;; compile.el --- run compiler as inferior of Emacs, parse error messages.
Eric S. Raymond's avatar
Eric S. Raymond committed
2

3
;; Copyright (C) 1985, 86, 87, 93, 94 Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
4

Roland McGrath's avatar
Roland McGrath committed
5
;; Author: Roland McGrath <roland@prep.ai.mit.edu>
Eric S. Raymond's avatar
Eric S. Raymond committed
6
;; Maintainer: FSF
Eric S. Raymond's avatar
Eric S. Raymond committed
7
;; Keywords: tools, processes
Eric S. Raymond's avatar
Eric S. Raymond committed
8

Richard M. Stallman's avatar
Richard M. Stallman committed
9 10
;; This file is part of GNU Emacs.

Roland McGrath's avatar
Roland McGrath committed
11 12 13 14 15
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

Richard M. Stallman's avatar
Richard M. Stallman committed
16
;; GNU Emacs is distributed in the hope that it will be useful,
Roland McGrath's avatar
Roland McGrath committed
17 18 19 20 21 22 23
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
Richard M. Stallman's avatar
Richard M. Stallman committed
24

Eric S. Raymond's avatar
Eric S. Raymond committed
25 26 27 28 29
;;; Commentary:

;; This package provides the compile and grep facilities documented in
;; the Emacs user's manual.

Eric S. Raymond's avatar
Eric S. Raymond committed
30 31
;;; Code:

Roland McGrath's avatar
Roland McGrath committed
32 33
;;;###autoload
(defvar compilation-mode-hook nil
34
  "*List of hook functions run by `compilation-mode' (see `run-hooks').")
Roland McGrath's avatar
Roland McGrath committed
35 36

;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
37 38 39
(defconst compilation-window-height nil
  "*Number of lines in a compilation window.  If nil, use Emacs default.")

Richard M. Stallman's avatar
Richard M. Stallman committed
40 41
(defvar compilation-error-list nil
  "List of error message descriptors for visiting erring functions.
42 43
Each error descriptor is a cons (or nil).  Its car is a marker pointing to
an error message.  If its cdr is a marker, it points to the text of the
44 45 46
line the message is about.  If its cdr is a cons, it is a list
\(\(DIRECTORY . FILE\) LINE [COLUMN]\).  Or its cdr may be nil if that
error is not interesting.
Roland McGrath's avatar
Roland McGrath committed
47 48

The value may be t instead of a list; this means that the buffer of
Jim Blandy's avatar
Jim Blandy committed
49 50 51 52 53 54
error messages should be reparsed the next time the list of errors is wanted.

Some other commands (like `diff') use this list to control the error
message tracking facilites; if you change its structure, you should make
sure you also change those packages.  Perhaps it is better not to change
it at all.")
Richard M. Stallman's avatar
Richard M. Stallman committed
55 56 57 58

(defvar compilation-old-error-list nil
  "Value of `compilation-error-list' after errors were parsed.")

Roland McGrath's avatar
Roland McGrath committed
59
(defvar compilation-parse-errors-function 'compilation-parse-errors 
Roland McGrath's avatar
Roland McGrath committed
60
  "Function to call to parse error messages from a compilation.
Roland McGrath's avatar
Roland McGrath committed
61 62 63
It takes args LIMIT-SEARCH and FIND-AT-LEAST.
If LIMIT-SEARCH is non-nil, don't bother parsing past that location.
If FIND-AT-LEAST is non-nil, don't bother parsing after finding that 
64
many new errors.
Roland McGrath's avatar
Roland McGrath committed
65 66 67
It should read in the source files which have errors and set
`compilation-error-list' to a list with an element for each error message
found.  See that variable for more info.")
Richard M. Stallman's avatar
Richard M. Stallman committed
68

Jim Blandy's avatar
Jim Blandy committed
69
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
70
(defvar compilation-buffer-name-function nil
Richard M. Stallman's avatar
Richard M. Stallman committed
71 72 73 74
  "Function to compute the name of a compilation buffer.
The function receives one argument, the name of the major mode of the
compilation buffer.  It should return a string.
nil means compute the name with `(concat \"*\" (downcase major-mode) \"*\")'.")
Richard M. Stallman's avatar
Richard M. Stallman committed
75

Jim Blandy's avatar
Jim Blandy committed
76
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
77
(defvar compilation-finish-function nil
Jim Blandy's avatar
Jim Blandy committed
78
  "*Function to call when a compilation process finishes.
Roland McGrath's avatar
Roland McGrath committed
79 80
It is called with two arguments: the compilation buffer, and a string
describing how the process finished.")
Richard M. Stallman's avatar
Richard M. Stallman committed
81

Roland McGrath's avatar
Roland McGrath committed
82
(defvar compilation-last-buffer nil
Richard M. Stallman's avatar
Richard M. Stallman committed
83 84 85
  "The most recent compilation buffer.
A buffer becomes most recent when its compilation is started
or when it is used with \\[next-error] or \\[compile-goto-error].")
Richard M. Stallman's avatar
Richard M. Stallman committed
86

Roland McGrath's avatar
Roland McGrath committed
87 88 89 90 91 92
(defvar compilation-in-progress nil
  "List of compilation processes now running.")
(or (assq 'compilation-in-progress minor-mode-alist)
    (setq minor-mode-alist (cons '(compilation-in-progress " Compiling")
				 minor-mode-alist)))

Roland McGrath's avatar
Roland McGrath committed
93 94 95 96
(defvar compilation-parsing-end nil
  "Position of end of buffer when last error messages were parsed.")

(defvar compilation-error-message "No more errors"
Richard M. Stallman's avatar
Richard M. Stallman committed
97 98 99
  "Message to print when no more matches are found.")

(defvar compilation-num-errors-found)
Roland McGrath's avatar
Roland McGrath committed
100 101 102

(defvar compilation-error-regexp-alist
  '(
Roland McGrath's avatar
Roland McGrath committed
103
    ;; NOTE!  This first one is repeated in grep-regexp-alist, below.
104

Roland McGrath's avatar
Roland McGrath committed
105
    ;; 4.3BSD grep, cc, lint pass 1:
106 107 108 109 110
    ;; 	/usr/src/foo/foo.c(8): warning: w may be used before set
    ;; or GNU utilities:
    ;; 	foo.c:8: error message
    ;; or HP-UX 7.0 fc:
    ;; 	foo.f          :16    some horrible error message
111 112 113
    ;; or Borland C++:
    ;;  Error ping.c 15: Unable to open include file 'sys/types.h'
    ;;  Warning ping.c 68: Call to function 'func' with no prototype
114 115 116 117
    ;;
    ;; We'll insist that the number be followed by a colon or closing
    ;; paren, because otherwise this matches just about anything
    ;; containing a number with spaces around it.
118 119
    ("\n\\(Error\\|Warning\\)[ \t]*\\([^:( \t\n]+\\)\
[:(][ \t]*\\([0-9]+\\)[:) \t]" 2 3)
120

Roland McGrath's avatar
Roland McGrath committed
121
    ;; 4.3BSD lint pass 2
122
    ;; 	strcmp: variable # of args. llib-lc(359)  ::  /usr/src/foo/foo.c(8)
123
    ("[ \t:]\\([^:( \t\n]+\\)[:(](+[ \t]*\\([0-9]+\\))[:) \t]*$" 1 2)
124

Roland McGrath's avatar
Roland McGrath committed
125
    ;; 4.3BSD lint pass 3
126
    ;; 	bloofle defined( /users/wolfgang/foo.c(4) ), but never used
Jim Blandy's avatar
Jim Blandy committed
127 128 129
    ;; This used to be
    ;; ("[ \t(]+\\([^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]+" 1 2)
    ;; which is regexp Impressionism - it matches almost anything!
130
    ("([ \t]*\\([^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\))" 1 2)
131

132
    ;; Ultrix 3.0 f77:
133 134
    ;;  fort: Severe: addstf.f, line 82: Missing operator or delimiter symbol
    ("\nfort: [^:\n]*: \\([^ \n]*\\), line \\([0-9]+\\):" 1 2)
135 136 137
    ;;  Error on line 3 of t.f: Execution error unclassifiable statement    
    ;; Unknown who does this:
    ;;  Line 45 of "foo.c": bloofel undefined
138 139 140 141
    ;; Absoft FORTRAN 77 Compiler 3.1.3
    ;;  error on line 19 of fplot.f: spelling error?
    ;;  warning on line 17 of fplot.f: data type is undefined for variable d
    ("\\(\n\\|on \\)[Ll]ine[ \t]+\\([0-9]+\\)[ \t]+\
142
of[ \t]+\"?\\([^\":\n]+\\)\"?:" 3 2)
143 144 145

    ;; Apollo cc, 4.3BSD fc:
    ;;	"foo.f", line 3: Error: syntax error near end of statement
146 147
    ;; IBM RS6000:
    ;;  "vvouch.c", line 19.5: 1506-046 (S) Syntax error.
148 149
    ;; Unknown compiler:
    ;;  File "foobar.ml", lines 5-8, characters 20-155: blah blah
150 151 152
    ;; Microtec mcc68k:
    ;;  "foo.c", line 32 pos 1; (E) syntax error; unexpected symbol: "lossage"
    ("\"\\([^,\" \n\t]+\\)\", lines? \\([0-9]+\\)[:., -]" 1 2)
153 154

    ;; MIPS RISC CC - the one distributed with Ultrix:
155
    ;;	ccom: Error: foo.c, line 2: syntax error
156 157 158
    ;; DEC AXP OSF/1 cc
    ;;  /usr/lib/cmplrs/cc/cfe: Error: foo.c: 1: blah blah 
    ("rror: \\([^,\" \n\t]+\\)[,:] \\(line \\)?\\([0-9]+\\):" 1 3)
159 160 161

    ;; IBM AIX PS/2 C version 1.1:
    ;;	****** Error number 140 in line 8 of file errors.c ******
Jim Blandy's avatar
Jim Blandy committed
162
    ("in line \\([0-9]+\\) of file \\([^ \n]+[^. \n]\\)\\.? " 2 1)
Roland McGrath's avatar
Roland McGrath committed
163 164
    ;; IBM AIX lint is too painful to do right this way.  File name
    ;; prefixes entire sections rather than being on each line.
165

166 167
    ;; Lucid Compiler, lcc 3.x
    ;; E, file.cc(35,52) Illegal operation on pointers
168
    ("\n[EW], \\([^(\n]*\\)(\\([0-9]+\\),[ \t]*\\([0-9]+\\)" 1 2 3)
169

Roland McGrath's avatar
Roland McGrath committed
170
    )
Richard M. Stallman's avatar
Richard M. Stallman committed
171
  "Alist that specifies how to match errors in compiler output.
172 173 174
Each element has the form (REGEXP FILE-IDX LINE-IDX [COLUMN-IDX]).
If REGEXP matches, the FILE-IDX'th subexpression gives the file name, and
the LINE-IDX'th subexpression gives the line number.  If COLUMN-IDX is
175
given, the COLUMN-IDX'th subexpression gives the column number on that line.")
Richard M. Stallman's avatar
Richard M. Stallman committed
176

177 178 179 180 181 182 183 184
(defvar compilation-read-command t
  "If not nil, M-x compile reads the compilation command to use.
Otherwise, M-x compile just uses the value of `compile-command'.")

(defvar compilation-ask-about-save t
  "If not nil, M-x compile asks which buffers to save before compiling.
Otherwise, it saves all modified buffers without asking.")

Roland McGrath's avatar
Roland McGrath committed
185 186 187 188
(defvar grep-regexp-alist
  '(("^\\([^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2))
  "Regexp used to match grep hits.  See `compilation-error-regexp-alist'.")

189 190 191
(defvar grep-command "grep -n "
  "Last grep command used in \\{grep}; default for next grep.")

Roland McGrath's avatar
Roland McGrath committed
192
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
193
(defvar compilation-search-path '(nil)
Roland McGrath's avatar
Roland McGrath committed
194
  "*List of directories to search for source files named in error messages.
Roland McGrath's avatar
Roland McGrath committed
195 196
Elements should be directory names, not file names of directories.
nil as an element means to try the default directory.")
Richard M. Stallman's avatar
Richard M. Stallman committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210

(defvar compile-command "make -k "
  "Last shell command used to do a compilation; default for next compilation.

Sometimes it is useful for files to supply local values for this variable.
You might also use mode hooks to specify it in certain modes, like this:

    (setq c-mode-hook
      '(lambda () (or (file-exists-p \"makefile\") (file-exists-p \"Makefile\")
		      (progn (make-local-variable 'compile-command)
			     (setq compile-command
				    (concat \"make -k \"
					    buffer-file-name))))))")

Roland McGrath's avatar
Roland McGrath committed
211
(defconst compilation-enter-directory-regexp
Jim Blandy's avatar
Jim Blandy committed
212
  ": Entering directory `\\(.*\\)'$"
Richard M. Stallman's avatar
Richard M. Stallman committed
213 214
  "Regular expression matching lines that indicate a new current directory.
This must contain one \\(, \\) pair around the directory name.
Roland McGrath's avatar
Roland McGrath committed
215 216 217 218

The default value matches lines printed by the `-w' option of GNU Make.")

(defconst compilation-leave-directory-regexp
Jim Blandy's avatar
Jim Blandy committed
219
  ": Leaving directory `\\(.*\\)'$"
Richard M. Stallman's avatar
Richard M. Stallman committed
220 221 222 223
  "Regular expression matching lines that indicate restoring current directory.
This may contain one \\(, \\) pair around the name of the directory
being moved from.  If it does not, the last directory entered \(by a
line matching `compilation-enter-directory-regexp'\) is assumed.
Roland McGrath's avatar
Roland McGrath committed
224 225 226 227

The default value matches lines printed by the `-w' option of GNU Make.")

(defvar compilation-directory-stack nil
Richard M. Stallman's avatar
Richard M. Stallman committed
228 229
  "Stack of previous directories for `compilation-leave-directory-regexp'.
The head element is the directory the compilation was started in.")
Roland McGrath's avatar
Roland McGrath committed
230

231 232 233 234 235
;; History of compile commands.
(defvar compile-history nil)
;; History of grep commands.
(defvar grep-history nil)

Roland McGrath's avatar
Roland McGrath committed
236
;;;###autoload
Richard M. Stallman's avatar
Richard M. Stallman committed
237 238 239 240
(defun compile (command)
  "Compile the program including the current buffer.  Default: run `make'.
Runs COMMAND, a shell command, in a separate process asynchronously
with output going to the buffer `*compilation*'.
Roland McGrath's avatar
Roland McGrath committed
241

Richard M. Stallman's avatar
Richard M. Stallman committed
242 243 244 245
You can then use the command \\[next-error] to find the next error message
and move to the source code that caused it.

To run more than one compilation at once, start one and rename the
Roland McGrath's avatar
Roland McGrath committed
246 247 248 249 250 251
\`*compilation*' buffer to some other name with \\[rename-buffer].
Then start the next one.

The name used for the buffer is actually whatever is returned by
the function in `compilation-buffer-name-function', so you can set that
to a function that generates a unique name."
252 253 254 255 256 257
  (interactive
   (if compilation-read-command
       (list (read-from-minibuffer "Compile command: "
                                 compile-command nil nil
                                 '(compile-history . 1)))
     (list compile-command)))
Richard M. Stallman's avatar
Richard M. Stallman committed
258
  (setq compile-command command)
259
  (save-some-buffers (not compilation-ask-about-save) nil)
Roland McGrath's avatar
Roland McGrath committed
260
  (compile-internal compile-command "No more errors"))
Richard M. Stallman's avatar
Richard M. Stallman committed
261

Roland McGrath's avatar
Roland McGrath committed
262
;;;###autoload
Richard M. Stallman's avatar
Richard M. Stallman committed
263 264 265
(defun grep (command-args)
  "Run grep, with user-specified args, and collect output in a buffer.
While grep runs asynchronously, you can use the \\[next-error] command
Roland McGrath's avatar
Roland McGrath committed
266 267
to find the text that grep hits refer to.

268 269
This command uses a special history list for its arguments, so you can
easily repeat a grep command."
Richard M. Stallman's avatar
Richard M. Stallman committed
270
  (interactive
271
   (list (read-from-minibuffer "Run grep (like this): "
272
			       grep-command nil nil 'grep-history)))
273
  (compile-internal (concat command-args " /dev/null")
Roland McGrath's avatar
Roland McGrath committed
274 275 276
		    "No more grep hits" "grep"
		    ;; Give it a simpler regexp to match.
		    nil grep-regexp-alist))
Richard M. Stallman's avatar
Richard M. Stallman committed
277 278

(defun compile-internal (command error-message
Roland McGrath's avatar
Roland McGrath committed
279 280
				 &optional name-of-mode parser regexp-alist
				 name-function)
Richard M. Stallman's avatar
Richard M. Stallman committed
281 282 283
  "Run compilation command COMMAND (low level interface).
ERROR-MESSAGE is a string to print if the user asks to see another error
and there are no more errors.  Third argument NAME-OF-MODE is the name
Roland McGrath's avatar
Roland McGrath committed
284 285 286 287 288 289 290
to display as the major mode in the compilation buffer.

Fourth arg PARSER is the error parser function (nil means the default).  Fifth
arg REGEXP-ALIST is the error message regexp alist to use (nil means the
default).  Sixth arg NAME-FUNCTION is a function called to name the buffer (nil
means the default).  The defaults for these variables are the global values of
\`compilation-parse-errors-function', `compilation-error-regexp-alist', and
Roland McGrath's avatar
Roland McGrath committed
291 292 293
\`compilation-buffer-name-function', respectively.

Returns the compilation buffer created."
Roland McGrath's avatar
Roland McGrath committed
294
  (let (outbuf)
Richard M. Stallman's avatar
Richard M. Stallman committed
295
    (save-excursion
Roland McGrath's avatar
Roland McGrath committed
296 297 298 299 300 301 302 303 304 305 306 307 308
      (or name-of-mode
	  (setq name-of-mode "Compilation"))
      (setq outbuf
	    (get-buffer-create
	     (funcall (or name-function compilation-buffer-name-function
			  (function (lambda (mode)
				      (concat "*" (downcase mode) "*"))))
		      name-of-mode)))
      (set-buffer outbuf)
      (let ((comp-proc (get-buffer-process (current-buffer))))
	(if comp-proc
	    (if (or (not (eq (process-status comp-proc) 'run))
		    (yes-or-no-p
309 310
		     (format "A %s process is running; kill it? "
			     name-of-mode)))
Roland McGrath's avatar
Roland McGrath committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
		(condition-case ()
		    (progn
		      (interrupt-process comp-proc)
		      (sit-for 1)
		      (delete-process comp-proc))
		  (error nil))
	      (error "Cannot have two processes in `%s' at once"
		     (buffer-name))
	      )))
      ;; In case the compilation buffer is current, make sure we get the global
      ;; values of compilation-error-regexp-alist, etc.
      (kill-all-local-variables))
    (let ((regexp-alist (or regexp-alist compilation-error-regexp-alist))
	  (parser (or parser compilation-parse-errors-function))
	  (thisdir default-directory)
	  outwin) 
      (save-excursion
	;; Clear out the compilation buffer and make it writable.
	;; Change its default-directory to the directory where the compilation
	;; will happen, and insert a `cd' command to indicate this.
	(set-buffer outbuf)
	(setq buffer-read-only nil)
	(erase-buffer)
	(setq default-directory thisdir)
	(insert "cd " thisdir "\n" command "\n")
	(set-buffer-modified-p nil))
      ;; If we're already in the compilation buffer, go to the end
      ;; of the buffer, so point will track the compilation output.
      (if (eq outbuf (current-buffer))
	  (goto-char (point-max)))
      ;; Pop up the compilation buffer.
      (setq outwin (display-buffer outbuf))
343 344 345 346
      (save-excursion
	(set-buffer outbuf)
	(compilation-mode)
	(buffer-disable-undo (current-buffer))
347
	;; (setq buffer-read-only t)  ;;; Non-ergonomic.
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
	(set (make-local-variable 'compilation-parse-errors-function) parser)
	(set (make-local-variable 'compilation-error-message) error-message)
	(set (make-local-variable 'compilation-error-regexp-alist) regexp-alist)
	(setq default-directory thisdir
	      compilation-directory-stack (list default-directory))
	(set-window-start outwin (point-min))
	(setq mode-name name-of-mode)
	(or (eq outwin (selected-window))
	    (set-window-point outwin (point-min)))
	(and compilation-window-height
	     (= (window-width outwin) (frame-width))
	     (let ((w (selected-window)))
	       (unwind-protect
		   (progn
		     (select-window outwin)
		     (enlarge-window (- compilation-window-height
					(window-height))))
		 (select-window w))))
	;; Start the compilation.
367 368 369 370 371 372 373 374 375 376 377 378 379 380
	(if (fboundp 'start-process)
	    (let ((proc (start-process-shell-command (downcase mode-name)
						     outbuf
						     command)))
	      (set-process-sentinel proc 'compilation-sentinel)
	      (set-process-filter proc 'compilation-filter)
	      (set-marker (process-mark proc) (point) outbuf)
	      (setq compilation-in-progress 
		    (cons proc compilation-in-progress)))
	  ;; No asynchronous processes available
	  (message (format "Executing `%s'..." command))
	  (let ((status (call-process shell-file-name nil outbuf nil "-c"
				      command))))
	  (message (format "Executing `%s'...done" command)))))
Roland McGrath's avatar
Roland McGrath committed
381 382
    ;; Make it so the next C-x ` will use this buffer.
    (setq compilation-last-buffer outbuf)))
Richard M. Stallman's avatar
Richard M. Stallman committed
383

384
(defvar compilation-minor-mode-map
Richard M. Stallman's avatar
Richard M. Stallman committed
385
  (let ((map (make-sparse-keymap)))
386
    (define-key map [mouse-2] 'compile-mouse-goto-error)
Richard M. Stallman's avatar
Richard M. Stallman committed
387
    (define-key map "\C-c\C-c" 'compile-goto-error)
Roland McGrath's avatar
Roland McGrath committed
388
    (define-key map "\C-c\C-k" 'kill-compilation)
Roland McGrath's avatar
Roland McGrath committed
389 390
    (define-key map "\M-n" 'compilation-next-error)
    (define-key map "\M-p" 'compilation-previous-error)
Roland McGrath's avatar
Roland McGrath committed
391 392
    (define-key map "\M-{" 'compilation-previous-file)
    (define-key map "\M-}" 'compilation-next-file)
Richard M. Stallman's avatar
Richard M. Stallman committed
393
    map)
394
  "Keymap for `compilation-minor-mode'.")
395 396 397 398 399 400 401

(defvar compilation-mode-map
  (let ((map (cons 'keymap compilation-minor-mode-map)))
    (define-key map " " 'scroll-up)
    (define-key map "\^?" 'scroll-down)
    map)
  "Keymap for compilation log buffers.
402
`compilation-minor-mode-map' is a cdr of this.")
Richard M. Stallman's avatar
Richard M. Stallman committed
403 404 405 406

(defun compilation-mode ()
  "Major mode for compilation log buffers.
\\<compilation-mode-map>To visit the source for a line-numbered error,
Roland McGrath's avatar
Roland McGrath committed
407
move point to the error message line and type \\[compile-goto-error].
Roland McGrath's avatar
Roland McGrath committed
408 409 410
To kill the compilation, type \\[kill-compilation].

Runs `compilation-mode-hook' with `run-hooks' (which see)."
Richard M. Stallman's avatar
Richard M. Stallman committed
411 412 413
  (interactive)
  (fundamental-mode)
  (use-local-map compilation-mode-map)
414 415 416 417 418 419 420 421
  (setq major-mode 'compilation-mode
	mode-name "Compilation")
  (compilation-setup)
  (run-hooks 'compilation-mode-hook))

;; Prepare the buffer for the compilation parsing commands to work.
(defun compilation-setup ()
  ;; Make the buffer's mode line show process state.
422
  (setq mode-line-process '(":%s"))
Roland McGrath's avatar
Roland McGrath committed
423 424 425 426
  (set (make-local-variable 'compilation-error-list) nil)
  (set (make-local-variable 'compilation-old-error-list) nil)
  (set (make-local-variable 'compilation-parsing-end) 1)
  (set (make-local-variable 'compilation-directory-stack) nil)
427 428 429 430 431 432
  (setq compilation-last-buffer (current-buffer)))

(defvar compilation-minor-mode nil
  "Non-nil when in compilation-minor-mode.
In this minor mode, all the error-parsing commands of the
Compilation major mode are available.")
433
(make-variable-buffer-local 'compilation-minor-mode)
434 435 436 437 438

(or (assq 'compilation-minor-mode minor-mode-alist)
    (setq minor-mode-alist (cons '(compilation-minor-mode " Compilation")
				 minor-mode-alist)))
(or (assq 'compilation-minor-mode minor-mode-map-alist)
439 440
    (setq minor-mode-map-alist (cons (cons 'compilation-minor-mode
					   compilation-minor-mode-map)
441 442
				     minor-mode-map-alist)))

443
;;;###autoload
444 445 446 447 448 449 450 451 452
(defun compilation-minor-mode (&optional arg)
  "Toggle compilation minor mode.
With arg, turn compilation mode on if and only if arg is positive.
See `compilation-mode'."
  (interactive "P")
  (if (setq compilation-minor-mode (if (null arg)
				       (null compilation-minor-mode)
				     (> (prefix-numeric-value arg) 0)))
      (compilation-setup)))
Richard M. Stallman's avatar
Richard M. Stallman committed
453 454 455

;; Called when compilation process changes state.
(defun compilation-sentinel (proc msg)
Roland McGrath's avatar
Roland McGrath committed
456 457
  "Sentinel for compilation buffers."
  (let ((buffer (process-buffer proc)))
Roland McGrath's avatar
Roland McGrath committed
458 459 460 461 462 463 464 465 466 467 468 469 470 471
    (if (memq (process-status proc) '(signal exit))
	(progn
	  (if (null (buffer-name buffer))
	      ;; buffer killed
	      (set-process-buffer proc nil)
	    (let ((obuf (current-buffer))
		  omax opoint)
	      ;; save-excursion isn't the right thing if
	      ;; process-buffer is current-buffer
	      (unwind-protect
		  (progn
		    ;; Write something in the compilation buffer
		    ;; and hack its mode line.
		    (set-buffer buffer)
472 473 474 475 476 477 478 479 480 481 482
		    (let ((buffer-read-only nil))
		      (setq omax (point-max)
			    opoint (point))
		      (goto-char omax)
		      ;; Record where we put the message, so we can ignore it
		      ;; later on.
		      (insert ?\n mode-name " " msg)
		      (forward-char -1)
		      (insert " at " (substring (current-time-string) 0 19))
		      (forward-char 1)
		      (setq mode-line-process
483
			    (concat ":"
484 485 486 487 488 489 490
				    (symbol-name (process-status proc))))
		      ;; Since the buffer and mode line will show that the
		      ;; process is dead, we can delete it now.  Otherwise it
		      ;; will stay around until M-x list-processes.
		      (delete-process proc)
		      ;; Force mode line redisplay soon.
		      (set-buffer-modified-p (buffer-modified-p)))
Roland McGrath's avatar
Roland McGrath committed
491
		    (if (and opoint (< opoint omax))
Richard M. Stallman's avatar
Richard M. Stallman committed
492 493 494 495
			(goto-char opoint))
		    (if compilation-finish-function
			(funcall compilation-finish-function buffer msg)))
		(set-buffer obuf))))
Roland McGrath's avatar
Roland McGrath committed
496 497
	  (setq compilation-in-progress (delq proc compilation-in-progress))
	  ))))
Richard M. Stallman's avatar
Richard M. Stallman committed
498

499 500
(defun compilation-filter (proc string)
  "Process filter for compilation buffers.
501
Just inserts the text, but uses `insert-before-markers'."
502 503 504 505 506 507 508 509
  (save-excursion
    (set-buffer (process-buffer proc))
    (let ((buffer-read-only nil))
      (save-excursion
	(goto-char (process-mark proc))
	(insert-before-markers string)
	(set-marker (process-mark proc) (point))))))

Roland McGrath's avatar
Roland McGrath committed
510 511 512 513 514 515 516 517 518
;; Return the cdr of compilation-old-error-list for the error containing point.
(defun compile-error-at-point ()
  (compile-reinitialize-errors nil (point))
  (let ((errors compilation-old-error-list))
    (while (and errors
		(> (point) (car (car errors))))
      (setq errors (cdr errors)))
    errors))

519 520 521
(defsubst compilation-buffer-p (buffer)
  (assq 'compilation-error-list (buffer-local-variables buffer)))

Roland McGrath's avatar
Roland McGrath committed
522 523 524 525 526 527 528
(defun compilation-next-error (n)
  "Move point to the next error in the compilation buffer.
Does NOT find the source line like \\[next-error]."
  (interactive "p")
  (or (compilation-buffer-p (current-buffer))
      (error "Not in a compilation buffer."))
  (setq compilation-last-buffer (current-buffer))
Roland McGrath's avatar
Roland McGrath committed
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547

  (let ((errors (compile-error-at-point)))

    ;; Move to the error after the one containing point.
    (goto-char (car (if (< n 0)
			(let ((i 0)
			      (e compilation-old-error-list))
			  ;; See how many cdrs away ERRORS is from the start.
			  (while (not (eq e errors))
			    (setq i (1+ i)
				  e (cdr e)))
			  (if (> (- n) i)
			      (error "Moved back past first error")
			    (nth (+ i n) compilation-old-error-list)))
		      (let ((compilation-error-list (cdr errors)))
			(compile-reinitialize-errors nil nil n)
			(if compilation-error-list
			    (nth (1- n) compilation-error-list)
			  (error "Moved past last error"))))))))
Roland McGrath's avatar
Roland McGrath committed
548 549 550 551 552 553 554 555

(defun compilation-previous-error (n)
  "Move point to the previous error in the compilation buffer.
Does NOT find the source line like \\[next-error]."
  (interactive "p")
  (compilation-next-error (- n)))


556 557 558 559
;; Given an elt of `compilation-error-list', return an object representing
;; the referenced file which is equal to (but not necessarily eq to) what
;; this function would return for another error in the same file.
(defsubst compilation-error-filedata (data)
Roland McGrath's avatar
Roland McGrath committed
560 561
  (setq data (cdr data))
  (if (markerp data)
562
      (marker-buffer data)
563
    (car data)))
Roland McGrath's avatar
Roland McGrath committed
564

565 566 567 568 569 570 571 572
;; Return a string describing a value from compilation-error-filedata.
;; This value is not necessarily useful as a file name, but should be
;; indicative to the user of what file's errors are being referred to.
(defsubst compilation-error-filedata-file-name (filedata)
  (if (bufferp filedata)
      (buffer-file-name filedata)
    (car filedata)))

Roland McGrath's avatar
Roland McGrath committed
573 574 575 576 577 578 579 580
(defun compilation-next-file (n)
  "Move point to the next error for a different file than the current one."
  (interactive "p")
  (or (compilation-buffer-p (current-buffer))
      (error "Not in a compilation buffer."))
  (setq compilation-last-buffer (current-buffer))

  (let ((reversed (< n 0))
581
	errors filedata)
Roland McGrath's avatar
Roland McGrath committed
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598

    (if (not reversed)
	(setq errors (or (compile-error-at-point)
			 (error "Moved past last error")))

      ;; Get a reversed list of the errors up through the one containing point.
      (compile-reinitialize-errors nil (point))
      (setq errors (reverse compilation-old-error-list)
	    n (- n))

      ;; Ignore errors after point.  (car ERRORS) will be the error
      ;; containing point, (cadr ERRORS) the one before it.
      (while (and errors
		  (< (point) (car (car errors))))
	(setq errors (cdr errors))))

    (while (> n 0)
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
      (setq filedata (compilation-error-filedata (car errors)))

      ;; Skip past the following errors for this file.
      (while (equal filedata
		    (compilation-error-filedata
		     (car (or errors
			      (if reversed
				  (error "%s the first erring file"
					 (compilation-error-filedata-file-name
					  filedata))
				(let ((compilation-error-list nil))
				  ;; Parse some more.
				  (compile-reinitialize-errors nil nil 2)
				  (setq errors compilation-error-list)))
			      (error "%s is the last erring file" 
				     (compilation-error-filedata-file-name
				      filedata))))))
Roland McGrath's avatar
Roland McGrath committed
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
	(setq errors (cdr errors)))

      (setq n (1- n)))

    ;; Move to the following error.
    (goto-char (car (car (or errors
			     (if reversed
				 (error "This is the first erring file")
			       (let ((compilation-error-list nil))
				 ;; Parse the last one.
				 (compile-reinitialize-errors nil nil 1)
				 compilation-error-list))))))))

(defun compilation-previous-file (n)
  "Move point to the previous error for a different file than the current one."
  (interactive "p")
  (compilation-next-file (- n)))


Richard M. Stallman's avatar
Richard M. Stallman committed
635 636 637
(defun kill-compilation ()
  "Kill the process made by the \\[compile] command."
  (interactive)
Roland McGrath's avatar
Roland McGrath committed
638
  (let ((buffer (compilation-find-buffer)))
Richard M. Stallman's avatar
Richard M. Stallman committed
639
    (if (get-buffer-process buffer)
Roland McGrath's avatar
Roland McGrath committed
640 641 642
	(interrupt-process (get-buffer-process buffer))
      (error "The compilation process is not running."))))

Richard M. Stallman's avatar
Richard M. Stallman committed
643

Roland McGrath's avatar
Roland McGrath committed
644 645
;; Parse any new errors in the compilation buffer,
;; or reparse from the beginning if the user has asked for that.
646 647
(defun compile-reinitialize-errors (reparse
				    &optional limit-search find-at-least)
Roland McGrath's avatar
Roland McGrath committed
648 649 650 651 652
  (save-excursion
    (set-buffer compilation-last-buffer)
    ;; If we are out of errors, or if user says "reparse",
    ;; discard the info we have, to force reparsing.
    (if (or (eq compilation-error-list t)
653
	    reparse)
654
	(compilation-forget-errors))
Roland McGrath's avatar
Roland McGrath committed
655
    (if (and compilation-error-list
Roland McGrath's avatar
Roland McGrath committed
656 657
	     (or (not limit-search)
		 (> compilation-parsing-end limit-search))
Roland McGrath's avatar
Roland McGrath committed
658
	     (or (not find-at-least)
659
		 (>= (length compilation-error-list) find-at-least)))
Roland McGrath's avatar
Roland McGrath committed
660 661 662
	;; Since compilation-error-list is non-nil, it points to a specific
	;; error the user wanted.  So don't move it around.
	nil
663 664
      ;; This was here for a long time (before my rewrite); why? --roland
      ;;(switch-to-buffer compilation-last-buffer)
Richard M. Stallman's avatar
Richard M. Stallman committed
665
      (set-buffer-modified-p nil)
Roland McGrath's avatar
Roland McGrath committed
666
      (if (< compilation-parsing-end (point-max))
667
	  ;; compilation-error-list might be non-nil if we have a non-nil
668
	  ;; LIMIT-SEARCH or FIND-AT-LEAST arg.  In that case its value
669 670 671
	  ;; records the current position in the error list, and we must
	  ;; preserve that after reparsing.
	  (let ((error-list-pos compilation-error-list))
Roland McGrath's avatar
Roland McGrath committed
672
	    (funcall compilation-parse-errors-function
673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
		     limit-search
		     (and find-at-least
			  ;; We only need enough new parsed errors to reach
			  ;; FIND-AT-LEAST errors past the current
			  ;; position.
			  (- find-at-least (length compilation-error-list))))
	    ;; Remember the entire list for compilation-forget-errors.  If
	    ;; this is an incremental parse, append to previous list.  If
	    ;; we are parsing anew, compilation-forget-errors cleared
	    ;; compilation-old-error-list above.
	    (setq compilation-old-error-list
		  (nconc compilation-old-error-list compilation-error-list))
	    (if error-list-pos
		;; We started in the middle of an existing list of parsed
		;; errors before parsing more; restore that position.
		(setq compilation-error-list error-list-pos))
Roland McGrath's avatar
Roland McGrath committed
689
	    )))))
Richard M. Stallman's avatar
Richard M. Stallman committed
690

691 692
(defun compile-mouse-goto-error (event)
  (interactive "e")
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
  (save-excursion
    (set-buffer (window-buffer (posn-window (event-end event))))
    (goto-char (posn-point (event-end event)))

    (or (compilation-buffer-p (current-buffer))
	(error "Not in a compilation buffer."))
    (setq compilation-last-buffer (current-buffer))
    (compile-reinitialize-errors nil (point))

    ;; Move to bol; the marker for the error on this line will point there.
    (beginning-of-line)

    ;; Move compilation-error-list to the elt of compilation-old-error-list
    ;; we want.
    (setq compilation-error-list compilation-old-error-list)
    (while (and compilation-error-list
		(> (point) (car (car compilation-error-list))))
      (setq compilation-error-list (cdr compilation-error-list)))
    (or compilation-error-list
	(error "No error to go to")))
  (select-window (posn-window (event-end event)))
  ;; Move to another window, so that next-error's window changes
  ;; result in the desired setup.
  (or (one-window-p)
      (progn
	(other-window -1)
	;; other-window changed the selected buffer,
	;; but we didn't want to do that.
	(set-buffer compilation-last-buffer)))

  (push-mark)
  (next-error 1))
725

Richard M. Stallman's avatar
Richard M. Stallman committed
726 727
(defun compile-goto-error (&optional argp)
  "Visit the source for the error message point is on.
728
Use this command in a compilation log buffer.  Sets the mark at point there.
Roland McGrath's avatar
Roland McGrath committed
729
\\[universal-argument] as a prefix arg means to reparse the buffer's error messages first;
Richard M. Stallman's avatar
Richard M. Stallman committed
730 731
other kinds of prefix arguments are ignored."
  (interactive "P")
Roland McGrath's avatar
Roland McGrath committed
732 733 734
  (or (compilation-buffer-p (current-buffer))
      (error "Not in a compilation buffer."))
  (setq compilation-last-buffer (current-buffer))
735
  (compile-reinitialize-errors (consp argp) (point))
Roland McGrath's avatar
Roland McGrath committed
736

Roland McGrath's avatar
Roland McGrath committed
737 738 739
  ;; Move to bol; the marker for the error on this line will point there.
  (beginning-of-line)

Roland McGrath's avatar
Roland McGrath committed
740
  ;; Move compilation-error-list to the elt of compilation-old-error-list
Roland McGrath's avatar
Roland McGrath committed
741
  ;; we want.
Roland McGrath's avatar
Roland McGrath committed
742
  (setq compilation-error-list compilation-old-error-list)
Roland McGrath's avatar
Roland McGrath committed
743 744
  (while (and compilation-error-list
	      (> (point) (car (car compilation-error-list))))
Roland McGrath's avatar
Roland McGrath committed
745 746
    (setq compilation-error-list (cdr compilation-error-list)))

Richard M. Stallman's avatar
Richard M. Stallman committed
747 748 749
  ;; Move to another window, so that next-error's window changes
  ;; result in the desired setup.
  (or (one-window-p)
Roland McGrath's avatar
Roland McGrath committed
750 751 752 753 754 755
      (progn
	(other-window -1)
	;; other-window changed the selected buffer,
	;; but we didn't want to do that.
	(set-buffer compilation-last-buffer)))

756
  (push-mark)
Roland McGrath's avatar
Roland McGrath committed
757
  (next-error 1))
Richard M. Stallman's avatar
Richard M. Stallman committed
758

Roland McGrath's avatar
Roland McGrath committed
759 760 761 762 763
;; Return a compilation buffer.
;; If the current buffer is a compilation buffer, return it.
;; If compilation-last-buffer is set to a live buffer, use that.
;; Otherwise, look for a compilation buffer and signal an error
;; if there are none.
Jim Blandy's avatar
Jim Blandy committed
764 765 766
(defun compilation-find-buffer (&optional other-buffer)
  (if (and (not other-buffer)
	   (compilation-buffer-p (current-buffer)))
Roland McGrath's avatar
Roland McGrath committed
767 768
      ;; The current buffer is a compilation buffer.
      (current-buffer)
Jim Blandy's avatar
Jim Blandy committed
769 770 771
    (if (and compilation-last-buffer (buffer-name compilation-last-buffer)
	     (or (not other-buffer) (not (eq compilation-last-buffer
					     (current-buffer)))))
Roland McGrath's avatar
Roland McGrath committed
772 773
	compilation-last-buffer
      (let ((buffers (buffer-list)))
Jim Blandy's avatar
Jim Blandy committed
774 775 776
	(while (and buffers (or (not (compilation-buffer-p (car buffers)))
				(and other-buffer
				     (eq (car buffers) (current-buffer)))))
Roland McGrath's avatar
Roland McGrath committed
777 778 779
	  (setq buffers (cdr buffers)))
	(if buffers
	    (car buffers)
Jim Blandy's avatar
Jim Blandy committed
780 781 782 783 784 785 786 787
	  (or (and other-buffer
		   (compilation-buffer-p (current-buffer))
		   ;; The current buffer is a compilation buffer.
		   (progn
		     (if other-buffer
			 (message "This is the only compilation buffer."))
		     (current-buffer)))
	      (error "No compilation started!")))))))
Roland McGrath's avatar
Roland McGrath committed
788 789

;;;###autoload
Richard M. Stallman's avatar
Richard M. Stallman committed
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
(defun next-error (&optional argp)
  "Visit next compilation error message and corresponding source code.
This operates on the output from the \\[compile] command.
If all preparsed error messages have been processed,
the error message buffer is checked for new ones.

A prefix arg specifies how many error messages to move;
negative means move back to previous error messages.
Just C-u as a prefix means reparse the error message buffer
and start at the first error.

\\[next-error] normally applies to the most recent compilation started,
but as long as you are in the middle of parsing errors from one compilation
output buffer, you stay with that compilation output buffer.

Use \\[next-error] in a compilation output buffer to switch to
processing errors from that compilation.

Roland McGrath's avatar
Roland McGrath committed
808 809
See variables `compilation-parse-errors-function' and
\`compilation-error-regexp-alist' for customization ideas."
Richard M. Stallman's avatar
Richard M. Stallman committed
810
  (interactive "P")
Roland McGrath's avatar
Roland McGrath committed
811
  (setq compilation-last-buffer (compilation-find-buffer))
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
  (compilation-goto-locus (compilation-next-error-locus
			   ;; We want to pass a number here only if
			   ;; we got a numeric prefix arg, not just C-u.
			   (and (not (consp argp))
				(prefix-numeric-value argp))
			   (consp argp))))
;;;###autoload (define-key ctl-x-map "`" 'next-error)

(defun compilation-next-error-locus (&optional move reparse)
  "Visit next compilation error and return locus in corresponding source code.
This operates on the output from the \\[compile] command.
If all preparsed error messages have been processed,
the error message buffer is checked for new ones.

Returns a cons (ERROR . SOURCE) of two markers: ERROR is a marker at the
location of the error message in the compilation buffer, and SOURCE is a
marker at the location in the source code indicated by the error message.

Optional first arg MOVE says how many error messages to move forwards (or
backwards, if negative); default is 1.  Optional second arg REPARSE, if
non-nil, says to reparse the error message buffer and reset to the first
error (plus MOVE - 1).

The current buffer should be the desired compilation output buffer."
  (or move (setq move 1))
  (compile-reinitialize-errors reparse nil (and (not reparse)
						(if (< move 1) 0 (1- move))))
Roland McGrath's avatar
Roland McGrath committed
839 840 841
  (let (next-errors next-error)
    (save-excursion
      (set-buffer compilation-last-buffer)
Roland McGrath's avatar
Roland McGrath committed
842
      ;; compilation-error-list points to the "current" error.
843
      (setq next-errors 
844 845
	    (if (> move 0)
		(nthcdr (1- move)
846 847
			compilation-error-list)
	      ;; Zero or negative arg; we need to move back in the list.
848
	      (let ((n (1- move))
849 850 851 852 853 854 855 856 857
		    (i 0)
		    (e compilation-old-error-list))
		;; See how many cdrs away the current error is from the start.
		(while (not (eq e compilation-error-list))
		  (setq i (1+ i)
			e (cdr e)))
		(if (> (- n) i)
		    (error "Moved back past first error")
		  (nthcdr (+ i n) compilation-old-error-list))))
Roland McGrath's avatar
Roland McGrath committed
858 859
	    next-error (car next-errors))
      (while
860 861
	  (if (null next-error)
	      (progn
862 863 864 865
		(and move (/= move 1)
		     (error (if (> move 0)
				"Moved past last error")
			    "Moved back past first error"))
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
		(compilation-forget-errors)
		(error (concat compilation-error-message
			       (and (get-buffer-process (current-buffer))
				    (eq (process-status
					 (get-buffer-process
					  (current-buffer)))
					'run)
				    " yet"))))
	    (setq compilation-error-list (cdr next-errors))
	    (if (null (cdr next-error))
		;; This error is boring.  Go to the next.
		t
	      (or (markerp (cdr next-error))
		  ;; This error has a filename/lineno pair.
		  ;; Find the file and turn it into a marker.
		  (let* ((fileinfo (car (cdr next-error)))
			 (buffer (compilation-find-file (cdr fileinfo)
							(car fileinfo)
							(car next-error))))
		    (if (null buffer)
			;; We can't find this error's file.
			;; Remove all errors in the same file.
			(progn
			  (setq next-errors compilation-old-error-list)
			  (while next-errors
			    (and (consp (cdr (car next-errors)))
				 (equal (car (cdr (car next-errors)))
					fileinfo)
				 (progn
				   (set-marker (car (car next-errors)) nil)
				   (setcdr (car next-errors) nil)))
			    (setq next-errors (cdr next-errors)))
			  ;; Look for the next error.
			  t)
		      ;; We found the file.  Get a marker for this error.
		      ;; compilation-old-error-list is a buffer-local
		      ;; variable, so we must be careful to extract its value
		      ;; before switching to the source file buffer.
		      (let ((errors compilation-old-error-list)
			    (last-line (nth 1 (cdr next-error)))
			    (column (nth 2 (cdr next-error))))
			(set-buffer buffer)
			(save-excursion
			  (save-restriction
			    (widen)
			    (goto-line last-line)
			    (if column
				(move-to-column column)
			      (beginning-of-line))
			    (setcdr next-error (point-marker))
			    ;; Make all the other error messages referring
			    ;; to the same file have markers into the buffer.
			    (while errors
			      (and (consp (cdr (car errors)))
				   (equal (car (cdr (car errors))) fileinfo)
				   (let* ((this (nth 1 (cdr (car errors))))
					  (column (nth 2 (cdr (car errors))))
					  (lines (- this last-line)))
				     (if (eq selective-display t)
					 ;; When selective-display is t,
					 ;; each C-m is a line boundary,
					 ;; as well as each newline.
					 (if (< lines 0)
					     (re-search-backward "[\n\C-m]"
								 nil 'end
								 (- lines))
					   (re-search-forward "[\n\C-m]"
							      nil 'end
							      lines))
				       (forward-line lines))
				     (if column
					 (move-to-column column))
				     (setq last-line this)
				     (setcdr (car errors) (point-marker))))
			      (setq errors (cdr errors)))))))))
	      ;; If we didn't get a marker for this error, or this
	      ;; marker's buffer was killed, go on to the next one.
	      (or (not (markerp (cdr next-error)))
		  (not (marker-buffer (cdr next-error))))))
Roland McGrath's avatar
Roland McGrath committed
945 946 947 948 949 950 951 952 953
	(setq next-errors compilation-error-list
	      next-error (car next-errors))))

    ;; Skip over multiple error messages for the same source location,
    ;; so the next C-x ` won't go to an error in the same place.
    (while (and compilation-error-list
		(equal (cdr (car compilation-error-list)) (cdr next-error)))
      (setq compilation-error-list (cdr compilation-error-list)))

954 955 956 957 958 959 960 961
    ;; We now have a marker for the position of the error source code.
    ;; NEXT-ERROR is a cons (ERROR . SOURCE) of two markers.
    next-error))

(defun compilation-goto-locus (next-error)
  "Jump to an error locus returned by `compilation-next-error-locus'.
Takes one argument, a cons (ERROR . SOURCE) of two markers.
Selects a window with point at SOURCE, with another window displaying ERROR."
962 963 964 965
  (if (and (window-dedicated-p (selected-window))
	   (eq (selected-window) (frame-root-window)))
      (switch-to-buffer-other-frame (marker-buffer (cdr next-error)))
    (switch-to-buffer (marker-buffer (cdr next-error))))
966 967 968 969 970 971 972 973 974 975 976 977 978 979
  (goto-char (cdr next-error))
  ;; If narrowing got in the way of
  ;; going to the right place, widen.
  (or (= (point) (marker-position (cdr next-error)))
      (progn
	(widen)
	(goto-char (cdr next-error))))

  ;; Show compilation buffer in other window, scrolled to this error.
  (let* ((pop-up-windows t)
	 (w (display-buffer (marker-buffer (car next-error)))))
    (set-window-point w (car next-error))
    (set-window-start w (car next-error))))

Roland McGrath's avatar
Roland McGrath committed
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
;; Find a buffer for file FILENAME.
;; Search the directories in compilation-search-path.
;; A nil in compilation-search-path means to try the
;; current directory, which is passed in DIR.
;; If FILENAME is not found at all, ask the user where to find it.
;; Pop up the buffer containing MARKER and scroll to MARKER if we ask the user.
(defun compilation-find-file (filename dir marker)
  (let ((dirs compilation-search-path)
	result name)
    (while (and dirs (null result))
      (setq name (expand-file-name filename (or (car dirs) dir))
	    result (and (file-exists-p name)
			(find-file-noselect name))
	    dirs (cdr dirs)))
    (or result
	;; The file doesn't exist.
	;; Ask the user where to find it.
	;; If he hits C-g, then the next time he does
	;; next-error, he'll skip past it.
	(progn
	  (let* ((pop-up-windows t)
		 (w (display-buffer (marker-buffer marker))))
	    (set-window-point w marker)
	    (set-window-start w marker))
	  (setq name
		(expand-file-name
		 (read-file-name
		  (format "Find this error in: (default %s) "
			  filename) dir filename t)))
	  (if (file-directory-p name)
	      (setq name (concat (file-name-as-directory name) filename)))
	  (if (file-exists-p name)
	      (find-file-noselect name))))))

;; Set compilation-error-list to nil, and unchain the markers that point to the
;; error messages and their text, so that they no longer slow down gap motion.
;; This would happen anyway at the next garbage collection, but it is better to
Roland McGrath's avatar
Roland McGrath committed
1017
;; do it right away.
Richard M. Stallman's avatar
Richard M. Stallman committed
1018 1019 1020 1021
(defun compilation-forget-errors ()
  (while compilation-old-error-list
    (let ((next-error (car compilation-old-error-list)))
      (set-marker (car next-error) nil)
Roland McGrath's avatar
Roland McGrath committed
1022 1023
      (if (markerp (cdr next-error))
	  (set-marker (cdr next-error) nil)))
Richard M. Stallman's avatar
Richard M. Stallman committed
1024
    (setq compilation-old-error-list (cdr compilation-old-error-list)))
1025
  (setq compilation-error-list nil
1026 1027
	compilation-directory-stack nil
	compilation-parsing-end 1))
Roland McGrath's avatar
Roland McGrath committed
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053


(defun count-regexp-groupings (regexp)
  "Return the number of \\( ... \\) groupings in REGEXP (a string)."
  (let ((groupings 0)
	(len (length regexp))
	(i 0)
	c)
    (while (< i len)
      (setq c (aref regexp i)
	    i (1+ i))
      (cond ((= c ?\[)
	     ;; Find the end of this [...].
	     (while (and (< i len)
			 (not (= (aref regexp i) ?\])))
	       (setq i (1+ i))))
	    ((= c ?\\)
	     (if (< i len)
		 (progn
		   (setq c (aref regexp i)
			 i (1+ i))
		   (if (= c ?\))
		       ;; We found the end of a grouping,
		       ;; so bump our counter.
		       (setq groupings (1+ groupings))))))))
    groupings))
Richard M. Stallman's avatar
Richard M. Stallman committed
1054

Roland McGrath's avatar
Roland McGrath committed
1055
(defun compilation-parse-errors (limit-search find-at-least)
Richard M. Stallman's avatar
Richard M. Stallman committed
1056
  "Parse the current buffer as grep, cc or lint error messages.
Roland McGrath's avatar
Roland McGrath committed
1057
See variable `compilation-parse-errors-function' for the interface it uses."
Richard M. Stallman's avatar
Richard M. Stallman committed
1058 1059
  (setq compilation-error-list nil)
  (message "Parsing error messages...")
1060
  (let (text-buffer orig orig-expanded parent-expanded
Roland McGrath's avatar
Roland McGrath committed
1061
	regexp enter-group leave-group error-group
Roland McGrath's avatar
Roland McGrath committed
1062
	alist subexpr error-regexp-groups
Roland McGrath's avatar
Roland McGrath committed
1063
	(found-desired nil)
Richard M. Stallman's avatar
Richard M. Stallman committed
1064
	(compilation-num-errors-found 0))
Roland McGrath's avatar
Roland McGrath committed
1065

Richard M. Stallman's avatar
Richard M. Stallman committed
1066 1067 1068 1069 1070
    ;; Don't reparse messages already seen at last parse.
    (goto-char compilation-parsing-end)
    ;; Don't parse the first two lines as error messages.
    ;; This matters for grep.
    (if (bobp)
1071 1072 1073 1074 1075 1076
	(progn
	  (forward-line 2)
	  ;; Move back so point is before the newline.
	  ;; This matters because some error regexps use \n instead of ^
	  ;; to be faster.
	  (forward-char -1)))
Roland McGrath's avatar
Roland McGrath committed
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100

    ;; Compile all the regexps we want to search for into one.
    (setq regexp (concat "\\(" compilation-enter-directory-regexp "\\)\\|"
			 "\\(" compilation-leave-directory-regexp "\\)\\|"
			 "\\(" (mapconcat (function
					   (lambda (elt)
					     (concat "\\(" (car elt) "\\)")))
					  compilation-error-regexp-alist
					  "\\|") "\\)"))

    ;; Find out how many \(...\) groupings are in each of the regexps, and set
    ;; *-GROUP to the grouping containing each constituent regexp (whose
    ;; subgroups will come immediately thereafter) of the big regexp we have
    ;; just constructed.
    (setq enter-group 1
	  leave-group (+ enter-group
			 (count-regexp-groupings
			  compilation-enter-directory-regexp)
			 1)
	  error-group (+ leave-group
			 (count-regexp-groupings
			  compilation-leave-directory-regexp)
			 1))

1101 1102 1103 1104
    ;; Compile an alist (IDX FILE LINE [COL]), where IDX is the number of
    ;; the subexpression for an entire error-regexp, and FILE and LINE (and
    ;; possibly COL) are the numbers for the subexpressions giving the file
    ;; name and line number (and possibly column number).
1105 1106
    (setq alist (or compilation-error-regexp-alist
		    (error "compilation-error-regexp-alist is empty!"))
Roland McGrath's avatar
Roland McGrath committed
1107 1108
	  subexpr (1+ error-group))
    (while alist
1109 1110 1111 1112
      (setq error-regexp-groups
	    (cons (list subexpr
			(+ subexpr (nth 1 (car alist)))
			(+ subexpr (nth 2 (car alist)))
1113 1114
			(and (nth 3 (car alist))
			     (+ subexpr (nth 3 (car alist)))))
1115
		  error-regexp-groups))
Roland McGrath's avatar
Roland McGrath committed
1116 1117 1118
      (setq subexpr (+ subexpr 1 (count-regexp-groupings (car (car alist)))))
      (setq alist (cdr alist)))

1119 1120 1121 1122
    (setq orig default-directory)
    (setq orig-expanded (file-truename orig))
    (setq parent-expanded (expand-file-name "../" orig-expanded))

Roland McGrath's avatar
Roland McGrath committed
1123
    (while (and (not found-desired)
Roland McGrath's avatar
Roland McGrath committed
1124 1125 1126
		;; We don't just pass LIMIT-SEARCH to re-search-forward
		;; because we want to find matches containing LIMIT-SEARCH
		;; but which extend past it.
Roland McGrath's avatar
Roland McGrath committed
1127
		(re-search-forward regexp nil t))
Roland McGrath's avatar
Roland McGrath committed
1128

Roland McGrath's avatar
Roland McGrath committed
1129 1130 1131 1132 1133 1134 1135 1136
      ;; Figure out which constituent regexp matched.
      (cond ((match-beginning enter-group)
	     ;; The match was the enter-directory regexp.
	     (let ((dir
		    (file-name-as-directory
		     (expand-file-name
		      (buffer-substring (match-beginning (+ enter-group 1))
					(match-end (+ enter-group 1)))))))
1137 1138 1139 1140 1141 1142
	       ;; The directory name in the "entering" message
	       ;; is a truename.  Try to convert it to a form
	       ;; like what the user typed in.
	       (setq dir
		     (compile-abbreviate-directory dir orig orig-expanded
						   parent-expanded))
Roland McGrath's avatar
Roland McGrath committed
1143 1144 1145
	       (setq compilation-directory-stack
		     (cons dir compilation-directory-stack))
	       (and (file-directory-p dir)
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155