compile.el 62.8 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

Simon Marshall's avatar
Simon Marshall committed
3
;; Copyright (C) 1985, 86, 87, 93, 94, 95, 96, 1997 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
;; 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
Erik Naggum's avatar
Erik Naggum committed
22 23 24
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
Richard M. Stallman's avatar
Richard M. Stallman committed
25

Eric S. Raymond's avatar
Eric S. Raymond committed
26 27 28 29 30
;;; 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
31 32
;;; Code:

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

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

41
(defvar compile-auto-highlight nil
42
  "*Specify how many compiler errors to highlight (and parse) initially.
43
\(Highlighting applies to ean error message when the mouse is over it.)
44 45
If this is a number N, all compiler error messages in the first N lines
are highlighted and parsed as soon as they arrive in Emacs.
46
If t, highlight and parse the whole compilation output as soon as it arrives.
47 48 49
If nil, don't highlight or parse any of the buffer until you try to
move to the error messages.

50
Those messages which are not parsed and highlighted initially
51 52
will be parsed and highlighted as soon as you try to move to them.")

Richard M. Stallman's avatar
Richard M. Stallman committed
53 54
(defvar compilation-error-list nil
  "List of error message descriptors for visiting erring functions.
55 56
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
57 58 59
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
60 61

The value may be t instead of a list; this means that the buffer of
Jim Blandy's avatar
Jim Blandy committed
62 63 64
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
65
message tracking facilities; if you change its structure, you should make
Jim Blandy's avatar
Jim Blandy committed
66 67
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
68 69 70 71

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

72
(defvar compilation-parse-errors-function 'compilation-parse-errors
Roland McGrath's avatar
Roland McGrath committed
73
  "Function to call to parse error messages from a compilation.
Roland McGrath's avatar
Roland McGrath committed
74 75
It takes args LIMIT-SEARCH and FIND-AT-LEAST.
If LIMIT-SEARCH is non-nil, don't bother parsing past that location.
76
If FIND-AT-LEAST is non-nil, don't bother parsing after finding that
77
many new errors.
Roland McGrath's avatar
Roland McGrath committed
78 79 80
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
81

82 83 84 85 86 87 88
;;;###autoload
(defvar compilation-process-setup-function nil
  "*Function to call to customize the compilation process.
This functions is called immediately before the compilation process is
started.  It can be used to set any variables or functions that are used
while processing the output of the compilation process.")

Jim Blandy's avatar
Jim Blandy committed
89
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
90
(defvar compilation-buffer-name-function nil
Richard M. Stallman's avatar
Richard M. Stallman committed
91 92 93 94
  "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
95

Jim Blandy's avatar
Jim Blandy committed
96
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
97
(defvar compilation-finish-function nil
Jim Blandy's avatar
Jim Blandy committed
98
  "*Function to call when a compilation process finishes.
Roland McGrath's avatar
Roland McGrath committed
99 100
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
101

102 103 104 105 106 107
;;;###autoload
(defvar compilation-finish-functions nil
  "*Functions to call when a compilation process finishes.
Each function is called with two arguments: the compilation buffer,
and a string describing how the process finished.")

Roland McGrath's avatar
Roland McGrath committed
108
(defvar compilation-last-buffer nil
Richard M. Stallman's avatar
Richard M. Stallman committed
109 110 111
  "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
112

Roland McGrath's avatar
Roland McGrath committed
113 114 115 116 117 118
(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
119 120 121 122
(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
123 124 125
  "Message to print when no more matches are found.")

(defvar compilation-num-errors-found)
Roland McGrath's avatar
Roland McGrath committed
126 127 128

(defvar compilation-error-regexp-alist
  '(
129
    ;; NOTE!  See also grep-regexp-alist, below.
130

Roland McGrath's avatar
Roland McGrath committed
131
    ;; 4.3BSD grep, cc, lint pass 1:
132 133 134 135 136
    ;; 	/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
137 138
    ;; or GNU utilities with column (GNAT 1.82):
    ;;   foo.adb:2:1: Unit name does not match file name
139
    ;;
140 141 142
    ;; 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.
143
    ("\
144
\\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
145
:\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 2 5)
146

147 148 149
    ;; Microsoft C/C++:
    ;;  keyboard.c(537) : warning C4005: 'min' : macro redefinition
    ;;  d:\tmp\test.c(23) : error C2143: syntax error : missing ';' before 'if'
150
    ("\\(\\([a-zA-Z]:\\)?[^:( \t\n-]+\\)[:(][ \t]*\\([0-9]+\\)[:) \t]" 1 3)
151

152 153 154
    ;; 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
155
    ("\\(Error\\|Warning\\) \\([a-zA-Z]?:?[^:( \t\n]+\\)\
156
 \\([0-9]+\\)\\([) \t]\\|:[^0-9\n]\\)" 2 3)
157

Roland McGrath's avatar
Roland McGrath committed
158
    ;; 4.3BSD lint pass 2
159
    ;; 	strcmp: variable # of args. llib-lc(359)  ::  /usr/src/foo/foo.c(8)
160
    (".*[ \t:]\\([a-zA-Z]?:?[^:( \t\n]+\\)[:(](+[ \t]*\\([0-9]+\\))[:) \t]*$"
161
     1 2)
162

Roland McGrath's avatar
Roland McGrath committed
163
    ;; 4.3BSD lint pass 3
164
    ;; 	bloofle defined( /users/wolfgang/foo.c(4) ), but never used
Jim Blandy's avatar
Jim Blandy committed
165
    ;; This used to be
166
    ;; ("[ \t(]+\\([a-zA-Z]?:?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]+" 1 2)
Jim Blandy's avatar
Jim Blandy committed
167
    ;; which is regexp Impressionism - it matches almost anything!
168
    (".*([ \t]*\\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\))" 1 2)
169

170 171
    ;; MIPS lint pass<n>; looks good for SunPro lint also
    ;;  TrimMask (255) in solomon.c may be indistinguishable from TrimMasks (93) in solomon.c due to truncation
172
    ("[^\n ]+ (\\([0-9]+\\)) in \\([^ \n]+\\)" 2 1)
173
    ;;  name defined but never used: LinInt in cmap_calc.c(199)
174
    (".*in \\([^(\n]+\\)(\\([0-9]+\\))$" 1 2)
175

176
    ;; Ultrix 3.0 f77:
177
    ;;  fort: Severe: addstf.f, line 82: Missing operator or delimiter symbol
178 179
    ;; Some SGI cc version:
    ;;  cfe: Warning 835: foo.c, line 2: something
180
    ("\\(cfe\\|fort\\): [^:\n]*: \\([^ \n]*\\), line \\([0-9]+\\):" 2 3)
181
    ;;  Error on line 3 of t.f: Execution error unclassifiable statement
182
    ;; Unknown who does this:
183
    ;;  Line 45 of "foo.c": bloofle undefined
184 185 186
    ;; 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
187
    ("\\(\\|.* on \\)[Ll]ine[ \t]+\\([0-9]+\\)[ \t]+\
188
of[ \t]+\"?\\([a-zA-Z]?:?[^\":\n]+\\)\"?:" 3 2)
189 190 191

    ;; Apollo cc, 4.3BSD fc:
    ;;	"foo.f", line 3: Error: syntax error near end of statement
192 193
    ;; IBM RS6000:
    ;;  "vvouch.c", line 19.5: 1506-046 (S) Syntax error.
194 195
    ;; Unknown compiler:
    ;;  File "foobar.ml", lines 5-8, characters 20-155: blah blah
196 197
    ;; Microtec mcc68k:
    ;;  "foo.c", line 32 pos 1; (E) syntax error; unexpected symbol: "lossage"
198
    ;; GNAT (as of July 94):
199
    ;;  "foo.adb", line 2(11): warning: file name does not match ...
200 201
    ;; IBM AIX xlc compiler:
    ;;  "src/swapping.c", line 30.34: 1506-342 (W) "/*" detected in comment.
202
    (".*\"\\([^,\" \n\t]+\\)\", lines? \
203
\\([0-9]+\\)\\([\(.]\\([0-9]+\\)\)?\\)?[:., (-]" 1 2 4)
204 205

    ;; MIPS RISC CC - the one distributed with Ultrix:
206
    ;;	ccom: Error: foo.c, line 2: syntax error
207
    ;; DEC AXP OSF/1 cc
208
    ;;  /usr/lib/cmplrs/cc/cfe: Error: foo.c: 1: blah blah
209
    (".*rror: \\([^,\" \n\t]+\\)[,:] \\(line \\)?\\([0-9]+\\):" 1 3)
210 211 212

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

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

221
    ;; GNU messages with program name and optional column number.
222
    ("[a-zA-Z]?:?[^0-9 \n\t:]+[^ \n\t:]*:[ \t]*\\([^ \n\t:]+\\):\
223
\\([0-9]+\\):\\(\\([0-9]+\\)[: \t]\\)?" 1 2 4)
224 225

    ;; Cray C compiler error messages
226
    ("\\(cc\\| cft\\)-[0-9]+ c\\(c\\|f77\\): ERROR \\([^,\n]+, \\)* File = \\([^,\n]+\\), Line = \\([0-9]+\\)" 4 5)
227 228 229 230 231

    ;; IBM C/C++ Tools 2.01:
    ;;  foo.c(2:0) : informational EDC0804: Function foo is not referenced.
    ;;  foo.c(3:8) : warning EDC0833: Implicit return statement encountered.
    ;;  foo.c(5:5) : error EDC0350: Syntax error.
232
    ("\\([^( \n\t]+\\)(\\([0-9]+\\):\\([0-9]+\\)) : " 1 2 3)
233 234 235

    ;; Sun ada (VADS, Solaris):
    ;;  /home3/xdhar/rcds_rc/main.a, line 361, char 6:syntax error: "," inserted
236
    ("\\([^, \n\t]+\\), line \\([0-9]+\\), char \\([0-9]+\\)[:., \(-]" 1 2 3)
Roland McGrath's avatar
Roland McGrath committed
237
    )
Richard M. Stallman's avatar
Richard M. Stallman committed
238
  "Alist that specifies how to match errors in compiler output.
239
Each elt has the form (REGEXP FILE-IDX LINE-IDX [COLUMN-IDX FILE-FORMAT...])
240 241
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
242 243 244 245
given, the COLUMN-IDX'th subexpression gives the column number on that line.
If any FILE-FORMAT is given, each is a format string to produce a file name to
try; %s in the string is replaced by the text matching the FILE-IDX'th
subexpression.")
Richard M. Stallman's avatar
Richard M. Stallman committed
246

247 248 249 250 251 252 253 254
(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
255
(defvar grep-regexp-alist
256
  '(("^\\([a-zA-Z]?:?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2))
Roland McGrath's avatar
Roland McGrath committed
257 258
  "Regexp used to match grep hits.  See `compilation-error-regexp-alist'.")

259
(defvar grep-command "grep -n "
Richard M. Stallman's avatar
Richard M. Stallman committed
260
  "Last grep command used in \\[grep]; default for next grep.")
261

Roland McGrath's avatar
Roland McGrath committed
262
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
263
(defvar compilation-search-path '(nil)
Roland McGrath's avatar
Roland McGrath committed
264
  "*List of directories to search for source files named in error messages.
Roland McGrath's avatar
Roland McGrath committed
265 266
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
267 268 269 270 271 272 273 274 275 276 277 278 279 280

(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))))))")

281
(defvar compilation-enter-directory-regexp
282
  ".*: Entering directory `\\(.*\\)'$"
Richard M. Stallman's avatar
Richard M. Stallman committed
283 284
  "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
285 286 287

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

288
(defvar compilation-leave-directory-regexp
289
  ".*: Leaving directory `\\(.*\\)'$"
Richard M. Stallman's avatar
Richard M. Stallman committed
290 291 292 293
  "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
294 295 296 297

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
298 299
  "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
300

301 302
(defvar compilation-exit-message-function nil "\
If non-nil, called when a compilation process dies to return a status message.
303 304 305
This should be a function of three arguments: process status, exit status,
and exit message; it returns a cons (MESSAGE . MODELINE) of the strings to
write into the compilation buffer, and to put in its mode line.")
306

307 308 309 310 311
;; History of compile commands.
(defvar compile-history nil)
;; History of grep commands.
(defvar grep-history nil)

Simon Marshall's avatar
Simon Marshall committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
(defun compilation-mode-font-lock-keywords ()
  "Return expressions to highlight in Compilation mode."
  (nconc
   ;;
   ;; Compiler warning/error lines.
   (mapcar #'(lambda (item)
	       (list (nth 0 item)
		     (list (nth 1 item) 'font-lock-warning-face nil t)
		     (list (nth 2 item) 'font-lock-variable-name-face nil t)))
	   compilation-error-regexp-alist)
   (list
    ;;
    ;; Compiler output lines.  Recognise `make[n]:' lines too.
    '("^\\([A-Za-z_0-9/\.+-]+\\)\\(\\[\\([0-9]+\\)\\]\\)?[ \t]*:"
      (1 font-lock-function-name-face) (3 font-lock-comment-face nil t)))
   ))
328

Roland McGrath's avatar
Roland McGrath committed
329
;;;###autoload
Richard M. Stallman's avatar
Richard M. Stallman committed
330 331 332 333
(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
334

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

338 339 340
Interactively, prompts for the command if `compilation-read-command' is
non-nil; otherwise uses `compile-command'.  With prefix arg, always prompts.

Richard M. Stallman's avatar
Richard M. Stallman committed
341
To run more than one compilation at once, start one and rename the
Roland McGrath's avatar
Roland McGrath committed
342 343 344 345 346 347
\`*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."
348
  (interactive
349
   (if (or compilation-read-command current-prefix-arg)
350 351 352 353
       (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
354
  (setq compile-command command)
355
  (save-some-buffers (not compilation-ask-about-save) nil)
Roland McGrath's avatar
Roland McGrath committed
356
  (compile-internal compile-command "No more errors"))
Richard M. Stallman's avatar
Richard M. Stallman committed
357

358 359 360 361 362 363
;;; run compile with the default command line
(defun recompile ()
  "Re-compile the program including the current buffer."
  (interactive)
  (save-some-buffers (not compilation-ask-about-save) nil)
  (compile-internal compile-command "No more errors"))
364 365 366 367

;; The system null device. (Should reference NULL_DEVICE from C.)
(defvar grep-null-device "/dev/null" "The system null device.")

368 369 370 371 372 373 374 375 376 377 378 379 380
(defun grep-process-setup ()
  "Set up `compilation-exit-message-function' for `grep'."
  (set (make-local-variable 'compilation-exit-message-function)
       (lambda (status code msg)
	 (if (eq status 'exit)
	     (cond ((zerop code)
		    '("finished (matches found)\n" . "matched"))
		   ((= code 1)
		    '("finished with no matches found\n" . "no match"))
		   (t
		    (cons msg code)))
	   (cons msg code)))))

Roland McGrath's avatar
Roland McGrath committed
381
;;;###autoload
Richard M. Stallman's avatar
Richard M. Stallman committed
382 383 384
(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
385 386
to find the text that grep hits refer to.

387 388
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
389
  (interactive
390
   (list (read-from-minibuffer "Run grep (like this): "
391
			       grep-command nil nil 'grep-history)))
392 393 394 395 396 397 398
  ;; Setting process-setup-function makes exit-message-function work
  ;; even when async processes aren't supported.
  (let* ((compilation-process-setup-function 'grep-process-setup)
	 (buf (compile-internal (concat command-args " " grep-null-device)
				"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
399 400

(defun compile-internal (command error-message
Roland McGrath's avatar
Roland McGrath committed
401 402
				 &optional name-of-mode parser regexp-alist
				 name-function)
Richard M. Stallman's avatar
Richard M. Stallman committed
403 404 405
  "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
406 407 408 409 410 411 412
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
413 414 415
\`compilation-buffer-name-function', respectively.

Returns the compilation buffer created."
Roland McGrath's avatar
Roland McGrath committed
416
  (let (outbuf)
Richard M. Stallman's avatar
Richard M. Stallman committed
417
    (save-excursion
Roland McGrath's avatar
Roland McGrath committed
418 419 420 421 422 423 424 425 426 427 428 429 430
      (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
431 432
		     (format "A %s process is running; kill it? "
			     name-of-mode)))
Roland McGrath's avatar
Roland McGrath committed
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
		(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)
448
	  outwin)
Roland McGrath's avatar
Roland McGrath committed
449 450 451 452 453 454
      (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)
455
	(buffer-disable-undo (current-buffer))
Roland McGrath's avatar
Roland McGrath committed
456
	(erase-buffer)
457
	(buffer-enable-undo (current-buffer))
Roland McGrath's avatar
Roland McGrath committed
458 459 460 461 462 463 464 465 466
	(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))
467 468 469
      (save-excursion
	(set-buffer outbuf)
	(compilation-mode)
470
	;; (setq buffer-read-only t)  ;;; Non-ergonomic.
471 472 473 474 475 476 477 478 479
	(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)))
480
	(compilation-set-window-height outwin)
481 482
	(if compilation-process-setup-function
	    (funcall compilation-process-setup-function))
483
	;; Start the compilation.
484
	(if (fboundp 'start-process)
485 486 487 488
	    (let* ((process-environment (cons "EMACS=t" process-environment))
		   (proc (start-process-shell-command (downcase mode-name)
						      outbuf
						      command)))
489 490 491
	      (set-process-sentinel proc 'compilation-sentinel)
	      (set-process-filter proc 'compilation-filter)
	      (set-marker (process-mark proc) (point) outbuf)
492
	      (setq compilation-in-progress
493
		    (cons proc compilation-in-progress)))
494 495
	  ;; No asynchronous processes available.
	  (message "Executing `%s'..." command)
496 497
	  ;; Fake modeline display as if `start-process' were run.
	  (setq mode-line-process ":run")
498 499
	  (force-mode-line-update)
	  (sit-for 0)			; Force redisplay
500
	  (let ((status (call-process shell-file-name nil outbuf nil "-c"
501 502 503 504 505 506 507 508 509 510 511 512 513 514
				      command)))
	    (cond ((numberp status)
		   (compilation-handle-exit 'exit status
					    (if (zerop status)
						"finished\n"
					      (format "\
exited abnormally with code %d\n"
						      status))))
		  ((stringp status)
		   (compilation-handle-exit 'signal status
					    (concat status "\n")))
		  (t
		   (compilation-handle-exit 'bizarre status status))))
	  (message "Executing `%s'...done" command))))
Roland McGrath's avatar
Roland McGrath committed
515 516
    ;; 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
517

518 519 520 521 522 523 524
;; Set the height of WINDOW according to compilation-window-height.
(defun compilation-set-window-height (window)
  (and compilation-window-height
       (= (window-width window) (frame-width (window-frame window)))
       ;; If window is alone in its frame, aside from a minibuffer,
       ;; don't change its height.
       (not (eq window (frame-root-window (window-frame window))))
525 526 527 528 529 530 531 532 533 534
       ;; This save-excursion prevents us from changing the current buffer,
       ;; which might not be the same as the selected window's buffer.
       (save-excursion
	 (let ((w (selected-window)))
	   (unwind-protect
	       (progn
		 (select-window window)
		 (enlarge-window (- compilation-window-height
				    (window-height))))
	     (select-window w))))))
535

536
(defvar compilation-minor-mode-map
Richard M. Stallman's avatar
Richard M. Stallman committed
537
  (let ((map (make-sparse-keymap)))
538
    (define-key map [mouse-2] 'compile-mouse-goto-error)
Richard M. Stallman's avatar
Richard M. Stallman committed
539
    (define-key map "\C-c\C-c" 'compile-goto-error)
540
    (define-key map "\C-m" 'compile-goto-error)
Roland McGrath's avatar
Roland McGrath committed
541
    (define-key map "\C-c\C-k" 'kill-compilation)
Roland McGrath's avatar
Roland McGrath committed
542 543
    (define-key map "\M-n" 'compilation-next-error)
    (define-key map "\M-p" 'compilation-previous-error)
Roland McGrath's avatar
Roland McGrath committed
544 545
    (define-key map "\M-{" 'compilation-previous-file)
    (define-key map "\M-}" 'compilation-next-file)
Richard M. Stallman's avatar
Richard M. Stallman committed
546
    map)
547
  "Keymap for `compilation-minor-mode'.")
548 549 550 551 552

(defvar compilation-mode-map
  (let ((map (cons 'keymap compilation-minor-mode-map)))
    (define-key map " " 'scroll-up)
    (define-key map "\^?" 'scroll-down)
553 554 555 556 557
    ;; Set up the menu-bar
    (define-key map [menu-bar compilation-menu]
      (cons "Compile" (make-sparse-keymap "Compile")))

    (define-key map [menu-bar compilation-menu compilation-mode-kill-compilation]
558
      '("Stop Compilation" . kill-compilation))
559 560 561
    (define-key map [menu-bar compilation-menu compilation-mode-separator2]
      '("----" . nil))
    (define-key map [menu-bar compilation-menu compilation-mode-first-error]
562
      '("First Error" . first-error))
563
    (define-key map [menu-bar compilation-menu compilation-mode-previous-error]
564
      '("Previous Error" . previous-error))
565
    (define-key map [menu-bar compilation-menu compilation-mode-next-error]
566
      '("Next Error" . next-error))
567 568 569 570 571 572 573 574
    (define-key map [menu-bar compilation-menu compilation-separator2]
      '("----" . nil))
    (define-key map [menu-bar compilation-menu compilation-mode-grep]
      '("Grep" . grep))
    (define-key map [menu-bar compilation-menu compilation-mode-recompile]
      '("Recompile" . recompile))
    (define-key map [menu-bar compilation-menu compilation-mode-compile]
      '("Compile" . compile))
575 576
    map)
  "Keymap for compilation log buffers.
577
`compilation-minor-mode-map' is a cdr of this.")
Richard M. Stallman's avatar
Richard M. Stallman committed
578

579
;;;###autoload
Richard M. Stallman's avatar
Richard M. Stallman committed
580 581 582
(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
583
move point to the error message line and type \\[compile-goto-error].
Roland McGrath's avatar
Roland McGrath committed
584 585 586
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
587
  (interactive)
588
  (kill-all-local-variables)
Richard M. Stallman's avatar
Richard M. Stallman committed
589
  (use-local-map compilation-mode-map)
590 591 592
  (setq major-mode 'compilation-mode
	mode-name "Compilation")
  (compilation-setup)
593 594
  (set (make-local-variable 'font-lock-defaults)
       '(compilation-mode-font-lock-keywords t))
595 596 597 598 599
  (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.
600
  (setq mode-line-process '(":%s"))
Roland McGrath's avatar
Roland McGrath committed
601 602 603 604
  (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)
605 606 607 608 609 610
  (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.")
611
(make-variable-buffer-local 'compilation-minor-mode)
612 613 614 615 616

(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)
617 618
    (setq minor-mode-map-alist (cons (cons 'compilation-minor-mode
					   compilation-minor-mode-map)
619 620
				     minor-mode-map-alist)))

621
;;;###autoload
622 623 624
(defun compilation-minor-mode (&optional arg)
  "Toggle compilation minor mode.
With arg, turn compilation mode on if and only if arg is positive.
625 626
See `compilation-mode'.
Turning the mode on runs the normal hook `compilation-minor-mode-hook'."
627 628 629 630
  (interactive "P")
  (if (setq compilation-minor-mode (if (null arg)
				       (null compilation-minor-mode)
				     (> (prefix-numeric-value arg) 0)))
631 632
      (progn
	(compilation-setup)
633
	(run-hooks 'compilation-minor-mode-hook))))
Richard M. Stallman's avatar
Richard M. Stallman committed
634

635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
;; Write msg in the current buffer and hack its mode-line-process.
(defun compilation-handle-exit (process-status exit-status msg)
  (let ((buffer-read-only nil)
	(status (if compilation-exit-message-function
		    (funcall compilation-exit-message-function
			     process-status exit-status msg)
		  (cons msg exit-status)))
	(omax (point-max))
	(opoint (point)))
    ;; Record where we put the message, so we can ignore it
    ;; later on.
    (goto-char omax)
    (insert ?\n mode-name " " (car status))
    (forward-char -1)
    (insert " at " (substring (current-time-string) 0 19))
    (forward-char 1)
651
    (setq mode-line-process (format ":%s [%s]" process-status (cdr status)))
652 653 654 655
    ;; Force mode line redisplay soon.
    (force-mode-line-update)
    (if (and opoint (< opoint omax))
	(goto-char opoint))
656 657
    ;; Automatically parse (and mouse-highlight) error messages:
    (cond ((eq compile-auto-highlight t)
658
	   (compile-reinitialize-errors nil (point-max)))
659 660 661 662 663
	  ((numberp compile-auto-highlight)
	   (compile-reinitialize-errors nil
					(save-excursion
					  (goto-line compile-auto-highlight)
					  (point)))))
664
    (if compilation-finish-function
665 666 667 668 669
	(funcall compilation-finish-function (current-buffer) msg))
    (let ((functions compilation-finish-functions))
      (while functions
	(funcall (car functions) (current-buffer) msg)
	(setq functions (cdr functions))))))
670

Richard M. Stallman's avatar
Richard M. Stallman committed
671 672
;; Called when compilation process changes state.
(defun compilation-sentinel (proc msg)
Roland McGrath's avatar
Roland McGrath committed
673 674
  "Sentinel for compilation buffers."
  (let ((buffer (process-buffer proc)))
Roland McGrath's avatar
Roland McGrath committed
675 676 677 678 679
    (if (memq (process-status proc) '(signal exit))
	(progn
	  (if (null (buffer-name buffer))
	      ;; buffer killed
	      (set-process-buffer proc nil)
680
	    (let ((obuf (current-buffer)))
Roland McGrath's avatar
Roland McGrath committed
681 682 683 684 685 686 687
	      ;; 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)
688 689 690 691 692 693 694
		    (compilation-handle-exit (process-status proc)
					     (process-exit-status proc)
					     msg)
		    ;; 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))
Richard M. Stallman's avatar
Richard M. Stallman committed
695
		(set-buffer obuf))))
Roland McGrath's avatar
Roland McGrath committed
696 697
	  (setq compilation-in-progress (delq proc compilation-in-progress))
	  ))))
Richard M. Stallman's avatar
Richard M. Stallman committed
698

699 700
(defun compilation-filter (proc string)
  "Process filter for compilation buffers.
701
Just inserts the text, but uses `insert-before-markers'."
702
  (if (buffer-name (process-buffer proc))
703
      (save-excursion
704 705 706 707 708
	(set-buffer (process-buffer proc))
	(let ((buffer-read-only nil))
	  (save-excursion
	    (goto-char (process-mark proc))
	    (insert-before-markers string)
709
	    (run-hooks 'compilation-filter-hook)
710
	    (set-marker (process-mark proc) (point)))))))
711

Roland McGrath's avatar
Roland McGrath committed
712 713 714 715 716 717 718 719 720
;; 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))

721
(defsubst compilation-buffer-p (buffer)
722 723 724
  (save-excursion
    (set-buffer buffer)
    (or compilation-minor-mode (eq major-mode 'compilation-mode))))
725

Roland McGrath's avatar
Roland McGrath committed
726 727 728 729 730 731 732
(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
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751

  (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
752 753 754 755 756 757 758 759

(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)))


760 761 762 763
;; 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
764 765
  (setq data (cdr data))
  (if (markerp data)
766
      (marker-buffer data)
767
    (car data)))
Roland McGrath's avatar
Roland McGrath committed
768

769 770 771 772 773 774 775 776
;; 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
777 778 779 780 781 782 783 784
(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))
785
	errors filedata)
Roland McGrath's avatar
Roland McGrath committed
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802

    (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)
803 804 805 806 807 808 809 810 811 812 813 814 815 816
      (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)))
817
			      (error "%s is the last erring file"
818 819
				     (compilation-error-filedata-file-name
				      filedata))))))
Roland McGrath's avatar
Roland McGrath committed
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
	(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
839 840 841
(defun kill-compilation ()
  "Kill the process made by the \\[compile] command."
  (interactive)
Roland McGrath's avatar
Roland McGrath committed
842
  (let ((buffer (compilation-find-buffer)))
Richard M. Stallman's avatar
Richard M. Stallman committed
843
    (if (get-buffer-process buffer)
Roland McGrath's avatar
Roland McGrath committed
844 845 846
	(interrupt-process (get-buffer-process buffer))
      (error "The compilation process is not running."))))

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

Roland McGrath's avatar
Roland McGrath committed
848 849
;; Parse any new errors in the compilation buffer,
;; or reparse from the beginning if the user has asked for that.
850 851
(defun compile-reinitialize-errors (reparse
				    &optional limit-search find-at-least)
Roland McGrath's avatar
Roland McGrath committed
852 853 854 855 856
  (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)
857
	    reparse)
858
	(compilation-forget-errors))
Roland McGrath's avatar
Roland McGrath committed
859
    (if (and compilation-error-list
Roland McGrath's avatar
Roland McGrath committed
860 861
	     (or (not limit-search)
		 (> compilation-parsing-end limit-search))
Roland McGrath's avatar
Roland McGrath committed
862
	     (or (not find-at-least)
863
		 (>= (length compilation-error-list) find-at-least)))
Roland McGrath's avatar
Roland McGrath committed
864 865 866
	;; Since compilation-error-list is non-nil, it points to a specific
	;; error the user wanted.  So don't move it around.
	nil
867 868
      ;; 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
869
      (set-buffer-modified-p nil)
Roland McGrath's avatar
Roland McGrath committed
870
      (if (< compilation-parsing-end (point-max))
871
	  ;; compilation-error-list might be non-nil if we have a non-nil
872
	  ;; LIMIT-SEARCH or FIND-AT-LEAST arg.  In that case its value
873 874 875
	  ;; 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
876
	    (funcall compilation-parse-errors-function
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
		     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))
893
	    ;; Mouse-Highlight (the first line of) each error message when the
894 895 896 897 898 899 900 901 902
	    ;; mouse pointer moves over it:
	    (let ((inhibit-read-only t)
		  (error-list compilation-error-list))
	      (while error-list
		(save-excursion
		  (put-text-property (goto-char (car (car error-list)))
				     (progn (end-of-line) (point))
				     'mouse-face 'highlight))
		(setq error-list (cdr error-list))))
Roland McGrath's avatar
Roland McGrath committed
903
	    )))))
Richard M. Stallman's avatar
Richard M. Stallman committed
904

905 906
(defun compile-mouse-goto-error (event)
  (interactive "e")
907 908 909 910 911 912 913
  (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))
914 915 916 917 918
    ;; `compile-reinitialize-errors' needs to see the complete filename
    ;; on the line where they clicked the mouse.  Since it only looks
    ;; upto point, moving point to eol makes sure the filename is
    ;; visible to `compile-reinitialize-errors'.
    (end-of-line)
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
    (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))
944

Richard M. Stallman's avatar
Richard M. Stallman committed
945 946
(defun compile-goto-error (&optional argp)
  "Visit the source for the error message point is on.
947
Use this command in a compilation log buffer.  Sets the mark at point there.
Roland McGrath's avatar
Roland McGrath committed
948
\\[universal-argument] as a prefix arg means to reparse the buffer's error messages first;
Richard M. Stallman's avatar
Richard M. Stallman committed
949 950
other kinds of prefix arguments are ignored."
  (interactive "P")
Roland McGrath's avatar
Roland McGrath committed
951 952 953
  (or (compilation-buffer-p (current-buffer))
      (error "Not in a compilation buffer."))
  (setq compilation-last-buffer (current-buffer))
954
  (compile-reinitialize-errors (consp argp) (point))
Roland McGrath's avatar
Roland McGrath committed
955

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

Roland McGrath's avatar
Roland McGrath committed
959
  ;; Move compilation-error-list to the elt of compilation-old-error-list
Roland McGrath's avatar
Roland McGrath committed
960
  ;; we want.
Roland McGrath's avatar
Roland McGrath committed
961
  (setq compilation-error-list compilation-old-error-list)
Roland McGrath's avatar
Roland McGrath committed
962 963
  (while (and compilation-error-list
	      (> (point) (car (car compilation-error-list))))
Roland McGrath's avatar
Roland McGrath committed
964 965
    (setq compilation-error-list (cdr compilation-error-list)))

Richard M. Stallman's avatar
Richard M. Stallman committed
966 967 968
  ;; 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
969 970 971 972