(initial comments): Copyright 1995; don't speak
[bpt/emacs.git] / lisp / progmodes / compile.el
CommitLineData
daa37602 1;;; compile.el --- run compiler as inferior of Emacs, parse error messages.
fad160d5 2
8fca6c30 3;; Copyright (C) 1985, 86, 87, 93, 94, 1995 Free Software Foundation, Inc.
3a801d0c 4
29add8b9 5;; Author: Roland McGrath <roland@prep.ai.mit.edu>
d1c7011d 6;; Maintainer: FSF
e9571d2a 7;; Keywords: tools, processes
d1c7011d 8
55dfd2c4
RS
9;; This file is part of GNU Emacs.
10
29add8b9
RM
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
55dfd2c4 16;; GNU Emacs is distributed in the hope that it will be useful,
29add8b9
RM
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to
23;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
55dfd2c4 24
5cc57841
ER
25;;; Commentary:
26
27;; This package provides the compile and grep facilities documented in
28;; the Emacs user's manual.
29
d1c7011d
ER
30;;; Code:
31
7c163413
RM
32;;;###autoload
33(defvar compilation-mode-hook nil
4f6e4ad6 34 "*List of hook functions run by `compilation-mode' (see `run-hooks').")
7c163413
RM
35
36;;;###autoload
6e1dbafa 37(defvar compilation-window-height nil
d3cb357b
RM
38 "*Number of lines in a compilation window. If nil, use Emacs default.")
39
55dfd2c4
RS
40(defvar compilation-error-list nil
41 "List of error message descriptors for visiting erring functions.
fc0094d7
RM
42Each error descriptor is a cons (or nil). Its car is a marker pointing to
43an error message. If its cdr is a marker, it points to the text of the
533304f8
RM
44line the message is about. If its cdr is a cons, it is a list
45\(\(DIRECTORY . FILE\) LINE [COLUMN]\). Or its cdr may be nil if that
46error is not interesting.
d3cb357b
RM
47
48The value may be t instead of a list; this means that the buffer of
29478cc5
JB
49error messages should be reparsed the next time the list of errors is wanted.
50
51Some other commands (like `diff') use this list to control the error
52message tracking facilites; if you change its structure, you should make
53sure you also change those packages. Perhaps it is better not to change
54it at all.")
55dfd2c4
RS
55
56(defvar compilation-old-error-list nil
57 "Value of `compilation-error-list' after errors were parsed.")
58
d3cb357b 59(defvar compilation-parse-errors-function 'compilation-parse-errors
b2bb6ec8 60 "Function to call to parse error messages from a compilation.
c540863c
RM
61It takes args LIMIT-SEARCH and FIND-AT-LEAST.
62If LIMIT-SEARCH is non-nil, don't bother parsing past that location.
63If FIND-AT-LEAST is non-nil, don't bother parsing after finding that
e78e10ca 64many new errors.
d3cb357b
RM
65It should read in the source files which have errors and set
66`compilation-error-list' to a list with an element for each error message
67found. See that variable for more info.")
55dfd2c4 68
aa228418 69;;;###autoload
d3cb357b 70(defvar compilation-buffer-name-function nil
6c43f2f9
RS
71 "Function to compute the name of a compilation buffer.
72The function receives one argument, the name of the major mode of the
73compilation buffer. It should return a string.
74nil means compute the name with `(concat \"*\" (downcase major-mode) \"*\")'.")
55dfd2c4 75
aa228418 76;;;###autoload
d3cb357b 77(defvar compilation-finish-function nil
aa228418 78 "*Function to call when a compilation process finishes.
d3cb357b
RM
79It is called with two arguments: the compilation buffer, and a string
80describing how the process finished.")
55dfd2c4 81
d3cb357b 82(defvar compilation-last-buffer nil
6c43f2f9
RS
83 "The most recent compilation buffer.
84A buffer becomes most recent when its compilation is started
85or when it is used with \\[next-error] or \\[compile-goto-error].")
55dfd2c4 86
ebff767c
RM
87(defvar compilation-in-progress nil
88 "List of compilation processes now running.")
89(or (assq 'compilation-in-progress minor-mode-alist)
90 (setq minor-mode-alist (cons '(compilation-in-progress " Compiling")
91 minor-mode-alist)))
92
d3cb357b
RM
93(defvar compilation-parsing-end nil
94 "Position of end of buffer when last error messages were parsed.")
95
96(defvar compilation-error-message "No more errors"
6c43f2f9
RS
97 "Message to print when no more matches are found.")
98
99(defvar compilation-num-errors-found)
d3cb357b
RM
100
101(defvar compilation-error-regexp-alist
102 '(
d86bdede 103 ;; NOTE! See also grep-regexp-alist, below.
7ee790ac 104
d3cb357b 105 ;; 4.3BSD grep, cc, lint pass 1:
7ee790ac
RM
106 ;; /usr/src/foo/foo.c(8): warning: w may be used before set
107 ;; or GNU utilities:
108 ;; foo.c:8: error message
109 ;; or HP-UX 7.0 fc:
110 ;; foo.f :16 some horrible error message
08b1edf4
RM
111 ;; or GNU utilities with column (GNAT 1.82):
112 ;; foo.adb:2:1: Unit name does not match file name
d86bdede 113 ;;
57c8389c
JB
114 ;; We'll insist that the number be followed by a colon or closing
115 ;; paren, because otherwise this matches just about anything
116 ;; containing a number with spaces around it.
08b1edf4
RM
117 ("\n\
118\\([^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
119:\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 2 5)
69d4d27f
RS
120
121 ;; Borland C++:
122 ;; Error ping.c 15: Unable to open include file 'sys/types.h'
123 ;; Warning ping.c 68: Call to function 'func' with no prototype
124 ("\n\\(Error\\|Warning\\) \\([^:( \t\n]+\\)\
125 \\([0-9]+\\)\\([) \t]\\|:[^0-9\n]\\)" 2 3)
7ee790ac 126
d3cb357b 127 ;; 4.3BSD lint pass 2
7ee790ac 128 ;; strcmp: variable # of args. llib-lc(359) :: /usr/src/foo/foo.c(8)
18710add 129 ("[ \t:]\\([^:( \t\n]+\\)[:(](+[ \t]*\\([0-9]+\\))[:) \t]*$" 1 2)
7ee790ac 130
d3cb357b 131 ;; 4.3BSD lint pass 3
7ee790ac 132 ;; bloofle defined( /users/wolfgang/foo.c(4) ), but never used
daa37602
JB
133 ;; This used to be
134 ;; ("[ \t(]+\\([^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]+" 1 2)
135 ;; which is regexp Impressionism - it matches almost anything!
18710add 136 ("([ \t]*\\([^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\))" 1 2)
7ee790ac 137
ff735134 138 ;; Ultrix 3.0 f77:
f89fdaed 139 ;; fort: Severe: addstf.f, line 82: Missing operator or delimiter symbol
984ae4ed
RM
140 ;; Some SGI cc version:
141 ;; cfe: Warning 835: foo.c, line 2: something
142 ("\n\\(cfe\\|fort\\): [^:\n]*: \\([^ \n]*\\), line \\([0-9]+\\):" 2 3)
ff735134
RM
143 ;; Error on line 3 of t.f: Execution error unclassifiable statement
144 ;; Unknown who does this:
145 ;; Line 45 of "foo.c": bloofel undefined
5ff3b093
RM
146 ;; Absoft FORTRAN 77 Compiler 3.1.3
147 ;; error on line 19 of fplot.f: spelling error?
148 ;; warning on line 17 of fplot.f: data type is undefined for variable d
149 ("\\(\n\\|on \\)[Ll]ine[ \t]+\\([0-9]+\\)[ \t]+\
9481474b 150of[ \t]+\"?\\([^\":\n]+\\)\"?:" 3 2)
7ee790ac
RM
151
152 ;; Apollo cc, 4.3BSD fc:
153 ;; "foo.f", line 3: Error: syntax error near end of statement
6d68d793
RS
154 ;; IBM RS6000:
155 ;; "vvouch.c", line 19.5: 1506-046 (S) Syntax error.
cacf1905
RM
156 ;; Unknown compiler:
157 ;; File "foobar.ml", lines 5-8, characters 20-155: blah blah
a0c567b5
RM
158 ;; Microtec mcc68k:
159 ;; "foo.c", line 32 pos 1; (E) syntax error; unexpected symbol: "lossage"
37fd2208
RS
160 ;; GNAT (as of July 94):
161 ;; "foo.adb", line 2(11): warning: file name does not match ...
162 ("\"\\([^,\" \n\t]+\\)\", lines? \\([0-9]+\\)[:., (-]" 1 2)
18710add
JB
163
164 ;; MIPS RISC CC - the one distributed with Ultrix:
7ee790ac 165 ;; ccom: Error: foo.c, line 2: syntax error
dfb89664
RS
166 ;; DEC AXP OSF/1 cc
167 ;; /usr/lib/cmplrs/cc/cfe: Error: foo.c: 1: blah blah
168 ("rror: \\([^,\" \n\t]+\\)[,:] \\(line \\)?\\([0-9]+\\):" 1 3)
7ee790ac
RM
169
170 ;; IBM AIX PS/2 C version 1.1:
171 ;; ****** Error number 140 in line 8 of file errors.c ******
daa37602 172 ("in line \\([0-9]+\\) of file \\([^ \n]+[^. \n]\\)\\.? " 2 1)
d3cb357b
RM
173 ;; IBM AIX lint is too painful to do right this way. File name
174 ;; prefixes entire sections rather than being on each line.
57c8389c 175
72244bea
RM
176 ;; Lucid Compiler, lcc 3.x
177 ;; E, file.cc(35,52) Illegal operation on pointers
c021b7a5 178 ("\n[EW], \\([^(\n]*\\)(\\([0-9]+\\),[ \t]*\\([0-9]+\\)" 1 2 3)
72244bea 179
08b1edf4
RM
180 ;; GNU messages with program name and optional column number.
181 ("\n[^0-9 \n\t:]+:[ \t]*\\([^ \n\t:]+\\):\
182\\([0-9]+\\):\\(\\([0-9]+\\)[: \t]\\)?" 1 2 4)
4dbf8a2c
RM
183
184 ;; Cray C compiler error messages
19ae9cb0 185 ("\n\\(cc\\| cft\\)-[0-9]+ c\\(c\\|f77\\): ERROR \\([^,\n]+, \\)* File = \\([^,\n]+\\), Line = \\([0-9]+\\)" 4 5)
4dbf8a2c
RM
186
187 ;; IBM C/C++ Tools 2.01:
188 ;; foo.c(2:0) : informational EDC0804: Function foo is not referenced.
189 ;; foo.c(3:8) : warning EDC0833: Implicit return statement encountered.
190 ;; foo.c(5:5) : error EDC0350: Syntax error.
54de3248 191 ("\n\\([^( \n\t]+\\)(\\([0-9]+\\):\\([0-9]+\\)) : " 1 2 3)
d3cb357b 192 )
6c43f2f9 193 "Alist that specifies how to match errors in compiler output.
20c1daec 194Each elt has the form (REGEXP FILE-IDX LINE-IDX [COLUMN-IDX FILE-FORMAT...])
72244bea
RM
195If REGEXP matches, the FILE-IDX'th subexpression gives the file name, and
196the LINE-IDX'th subexpression gives the line number. If COLUMN-IDX is
20c1daec
RM
197given, the COLUMN-IDX'th subexpression gives the column number on that line.
198If any FILE-FORMAT is given, each is a format string to produce a file name to
199try; %s in the string is replaced by the text matching the FILE-IDX'th
200subexpression.")
55dfd2c4 201
90016295
RS
202(defvar compilation-read-command t
203 "If not nil, M-x compile reads the compilation command to use.
204Otherwise, M-x compile just uses the value of `compile-command'.")
205
206(defvar compilation-ask-about-save t
207 "If not nil, M-x compile asks which buffers to save before compiling.
208Otherwise, it saves all modified buffers without asking.")
209
15d1a8da
RM
210(defvar grep-regexp-alist
211 '(("^\\([^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2))
212 "Regexp used to match grep hits. See `compilation-error-regexp-alist'.")
213
98b63c41 214(defvar grep-command "grep -n "
203a5711 215 "Last grep command used in \\[grep]; default for next grep.")
98b63c41 216
7c163413 217;;;###autoload
d3cb357b 218(defvar compilation-search-path '(nil)
7c163413 219 "*List of directories to search for source files named in error messages.
d3cb357b
RM
220Elements should be directory names, not file names of directories.
221nil as an element means to try the default directory.")
55dfd2c4
RS
222
223(defvar compile-command "make -k "
224 "Last shell command used to do a compilation; default for next compilation.
225
226Sometimes it is useful for files to supply local values for this variable.
227You might also use mode hooks to specify it in certain modes, like this:
228
229 (setq c-mode-hook
230 '(lambda () (or (file-exists-p \"makefile\") (file-exists-p \"Makefile\")
231 (progn (make-local-variable 'compile-command)
232 (setq compile-command
233 (concat \"make -k \"
234 buffer-file-name))))))")
235
6e1dbafa 236(defvar compilation-enter-directory-regexp
daa37602 237 ": Entering directory `\\(.*\\)'$"
6c43f2f9
RS
238 "Regular expression matching lines that indicate a new current directory.
239This must contain one \\(, \\) pair around the directory name.
d3cb357b
RM
240
241The default value matches lines printed by the `-w' option of GNU Make.")
242
6e1dbafa 243(defvar compilation-leave-directory-regexp
daa37602 244 ": Leaving directory `\\(.*\\)'$"
6c43f2f9
RS
245 "Regular expression matching lines that indicate restoring current directory.
246This may contain one \\(, \\) pair around the name of the directory
247being moved from. If it does not, the last directory entered \(by a
248line matching `compilation-enter-directory-regexp'\) is assumed.
d3cb357b
RM
249
250The default value matches lines printed by the `-w' option of GNU Make.")
251
252(defvar compilation-directory-stack nil
6c43f2f9
RS
253 "Stack of previous directories for `compilation-leave-directory-regexp'.
254The head element is the directory the compilation was started in.")
d3cb357b 255
770970cb
RS
256;; History of compile commands.
257(defvar compile-history nil)
258;; History of grep commands.
259(defvar grep-history nil)
260
9af0d309
RS
261(defvar compilation-mode-font-lock-keywords
262 '(("^\\([^\n:]*:\\([0-9]+:\\)+\\)\\(.*\\)$" 1 font-lock-function-name-face))
263;;; ("^\\([^\n:]*:\\([0-9]+:\\)+\\)\\(.*\\)$" 0 font-lock-keyword-face keep)
264 "Additional expressions to highlight in Compilation mode.")
265
d3cb357b 266;;;###autoload
55dfd2c4
RS
267(defun compile (command)
268 "Compile the program including the current buffer. Default: run `make'.
269Runs COMMAND, a shell command, in a separate process asynchronously
270with output going to the buffer `*compilation*'.
d3cb357b 271
55dfd2c4
RS
272You can then use the command \\[next-error] to find the next error message
273and move to the source code that caused it.
274
08b1edf4
RM
275Interactively, prompts for the command if `compilation-read-command' is
276non-nil; otherwise uses `compile-command'. With prefix arg, always prompts.
277
55dfd2c4 278To run more than one compilation at once, start one and rename the
d3cb357b
RM
279\`*compilation*' buffer to some other name with \\[rename-buffer].
280Then start the next one.
281
282The name used for the buffer is actually whatever is returned by
283the function in `compilation-buffer-name-function', so you can set that
284to a function that generates a unique name."
90016295 285 (interactive
08b1edf4 286 (if (or compilation-read-command current-prefix-arg)
90016295
RS
287 (list (read-from-minibuffer "Compile command: "
288 compile-command nil nil
289 '(compile-history . 1)))
290 (list compile-command)))
55dfd2c4 291 (setq compile-command command)
90016295 292 (save-some-buffers (not compilation-ask-about-save) nil)
d3cb357b 293 (compile-internal compile-command "No more errors"))
55dfd2c4 294
e60476ca
RS
295;;; run compile with the default command line
296(defun recompile ()
297 "Re-compile the program including the current buffer."
298 (interactive)
299 (save-some-buffers (not compilation-ask-about-save) nil)
300 (compile-internal compile-command "No more errors"))
301
d3cb357b 302;;;###autoload
55dfd2c4
RS
303(defun grep (command-args)
304 "Run grep, with user-specified args, and collect output in a buffer.
305While grep runs asynchronously, you can use the \\[next-error] command
d3cb357b
RM
306to find the text that grep hits refer to.
307
770970cb
RS
308This command uses a special history list for its arguments, so you can
309easily repeat a grep command."
55dfd2c4 310 (interactive
770970cb 311 (list (read-from-minibuffer "Run grep (like this): "
98b63c41 312 grep-command nil nil 'grep-history)))
770970cb 313 (compile-internal (concat command-args " /dev/null")
15d1a8da
RM
314 "No more grep hits" "grep"
315 ;; Give it a simpler regexp to match.
316 nil grep-regexp-alist))
55dfd2c4
RS
317
318(defun compile-internal (command error-message
d3cb357b
RM
319 &optional name-of-mode parser regexp-alist
320 name-function)
55dfd2c4
RS
321 "Run compilation command COMMAND (low level interface).
322ERROR-MESSAGE is a string to print if the user asks to see another error
323and there are no more errors. Third argument NAME-OF-MODE is the name
d3cb357b
RM
324to display as the major mode in the compilation buffer.
325
326Fourth arg PARSER is the error parser function (nil means the default). Fifth
327arg REGEXP-ALIST is the error message regexp alist to use (nil means the
328default). Sixth arg NAME-FUNCTION is a function called to name the buffer (nil
329means the default). The defaults for these variables are the global values of
330\`compilation-parse-errors-function', `compilation-error-regexp-alist', and
646bd331
RM
331\`compilation-buffer-name-function', respectively.
332
333Returns the compilation buffer created."
d3cb357b 334 (let (outbuf)
55dfd2c4 335 (save-excursion
d3cb357b
RM
336 (or name-of-mode
337 (setq name-of-mode "Compilation"))
338 (setq outbuf
339 (get-buffer-create
340 (funcall (or name-function compilation-buffer-name-function
341 (function (lambda (mode)
342 (concat "*" (downcase mode) "*"))))
343 name-of-mode)))
344 (set-buffer outbuf)
345 (let ((comp-proc (get-buffer-process (current-buffer))))
346 (if comp-proc
347 (if (or (not (eq (process-status comp-proc) 'run))
348 (yes-or-no-p
bea1d57a
JB
349 (format "A %s process is running; kill it? "
350 name-of-mode)))
d3cb357b
RM
351 (condition-case ()
352 (progn
353 (interrupt-process comp-proc)
354 (sit-for 1)
355 (delete-process comp-proc))
356 (error nil))
357 (error "Cannot have two processes in `%s' at once"
358 (buffer-name))
359 )))
360 ;; In case the compilation buffer is current, make sure we get the global
361 ;; values of compilation-error-regexp-alist, etc.
362 (kill-all-local-variables))
363 (let ((regexp-alist (or regexp-alist compilation-error-regexp-alist))
364 (parser (or parser compilation-parse-errors-function))
365 (thisdir default-directory)
366 outwin)
367 (save-excursion
368 ;; Clear out the compilation buffer and make it writable.
369 ;; Change its default-directory to the directory where the compilation
370 ;; will happen, and insert a `cd' command to indicate this.
371 (set-buffer outbuf)
372 (setq buffer-read-only nil)
5d5ab7ac 373 (buffer-disable-undo (current-buffer))
d3cb357b 374 (erase-buffer)
5d5ab7ac 375 (buffer-enable-undo (current-buffer))
d3cb357b
RM
376 (setq default-directory thisdir)
377 (insert "cd " thisdir "\n" command "\n")
378 (set-buffer-modified-p nil))
379 ;; If we're already in the compilation buffer, go to the end
380 ;; of the buffer, so point will track the compilation output.
381 (if (eq outbuf (current-buffer))
382 (goto-char (point-max)))
383 ;; Pop up the compilation buffer.
384 (setq outwin (display-buffer outbuf))
d4ece679
RS
385 (save-excursion
386 (set-buffer outbuf)
387 (compilation-mode)
a72ba929 388 ;; (setq buffer-read-only t) ;;; Non-ergonomic.
d4ece679
RS
389 (set (make-local-variable 'compilation-parse-errors-function) parser)
390 (set (make-local-variable 'compilation-error-message) error-message)
391 (set (make-local-variable 'compilation-error-regexp-alist) regexp-alist)
392 (setq default-directory thisdir
393 compilation-directory-stack (list default-directory))
394 (set-window-start outwin (point-min))
395 (setq mode-name name-of-mode)
396 (or (eq outwin (selected-window))
397 (set-window-point outwin (point-min)))
c94b02d6 398 (compilation-set-window-height outwin)
d4ece679 399 ;; Start the compilation.
35f7e85b 400 (if (fboundp 'start-process)
2bb240a9
RS
401 (let* ((process-environment (cons "EMACS=t" process-environment))
402 (proc (start-process-shell-command (downcase mode-name)
403 outbuf
404 command)))
35f7e85b
RS
405 (set-process-sentinel proc 'compilation-sentinel)
406 (set-process-filter proc 'compilation-filter)
407 (set-marker (process-mark proc) (point) outbuf)
408 (setq compilation-in-progress
409 (cons proc compilation-in-progress)))
410 ;; No asynchronous processes available
411 (message (format "Executing `%s'..." command))
4dba7b41 412 (sit-for 0) ;; Force redisplay
35f7e85b
RS
413 (let ((status (call-process shell-file-name nil outbuf nil "-c"
414 command))))
415 (message (format "Executing `%s'...done" command)))))
d3cb357b
RM
416 ;; Make it so the next C-x ` will use this buffer.
417 (setq compilation-last-buffer outbuf)))
55dfd2c4 418
c94b02d6
RS
419;; Set the height of WINDOW according to compilation-window-height.
420(defun compilation-set-window-height (window)
421 (and compilation-window-height
422 (= (window-width window) (frame-width (window-frame window)))
423 ;; If window is alone in its frame, aside from a minibuffer,
424 ;; don't change its height.
425 (not (eq window (frame-root-window (window-frame window))))
8205cbfc
RS
426 ;; This save-excursion prevents us from changing the current buffer,
427 ;; which might not be the same as the selected window's buffer.
428 (save-excursion
429 (let ((w (selected-window)))
430 (unwind-protect
431 (progn
432 (select-window window)
433 (enlarge-window (- compilation-window-height
434 (window-height))))
435 (select-window w))))))
c94b02d6 436
0b18a8f6 437(defvar compilation-minor-mode-map
55dfd2c4 438 (let ((map (make-sparse-keymap)))
f98494a4 439 (define-key map [mouse-2] 'compile-mouse-goto-error)
55dfd2c4 440 (define-key map "\C-c\C-c" 'compile-goto-error)
4e7ce12e 441 (define-key map "\C-m" 'compile-goto-error)
d3cb357b 442 (define-key map "\C-c\C-k" 'kill-compilation)
646bd331
RM
443 (define-key map "\M-n" 'compilation-next-error)
444 (define-key map "\M-p" 'compilation-previous-error)
d1ed4475
RM
445 (define-key map "\M-{" 'compilation-previous-file)
446 (define-key map "\M-}" 'compilation-next-file)
55dfd2c4 447 map)
4f6e4ad6 448 "Keymap for `compilation-minor-mode'.")
0b18a8f6
RM
449
450(defvar compilation-mode-map
451 (let ((map (cons 'keymap compilation-minor-mode-map)))
452 (define-key map " " 'scroll-up)
453 (define-key map "\^?" 'scroll-down)
e60476ca
RS
454 ;; Set up the menu-bar
455 (define-key map [menu-bar compilation-menu]
456 (cons "Compile" (make-sparse-keymap "Compile")))
457
458 (define-key map [menu-bar compilation-menu compilation-mode-kill-compilation]
0949ba2b 459 '("Stop Compilation" . kill-compilation))
e60476ca
RS
460 (define-key map [menu-bar compilation-menu compilation-mode-separator2]
461 '("----" . nil))
462 (define-key map [menu-bar compilation-menu compilation-mode-first-error]
0949ba2b 463 '("First Error" . first-error))
e60476ca 464 (define-key map [menu-bar compilation-menu compilation-mode-previous-error]
0949ba2b 465 '("Previous Error" . previous-error))
e60476ca 466 (define-key map [menu-bar compilation-menu compilation-mode-next-error]
0949ba2b 467 '("Next Error" . next-error))
e60476ca
RS
468 (define-key map [menu-bar compilation-menu compilation-separator2]
469 '("----" . nil))
470 (define-key map [menu-bar compilation-menu compilation-mode-grep]
471 '("Grep" . grep))
472 (define-key map [menu-bar compilation-menu compilation-mode-recompile]
473 '("Recompile" . recompile))
474 (define-key map [menu-bar compilation-menu compilation-mode-compile]
475 '("Compile" . compile))
0b18a8f6
RM
476 map)
477 "Keymap for compilation log buffers.
4f6e4ad6 478`compilation-minor-mode-map' is a cdr of this.")
55dfd2c4
RS
479
480(defun compilation-mode ()
481 "Major mode for compilation log buffers.
482\\<compilation-mode-map>To visit the source for a line-numbered error,
d3cb357b 483move point to the error message line and type \\[compile-goto-error].
7c163413
RM
484To kill the compilation, type \\[kill-compilation].
485
486Runs `compilation-mode-hook' with `run-hooks' (which see)."
55dfd2c4 487 (interactive)
9af0d309 488 (kill-all-local-variables)
55dfd2c4 489 (use-local-map compilation-mode-map)
0b18a8f6
RM
490 (setq major-mode 'compilation-mode
491 mode-name "Compilation")
492 (compilation-setup)
63a2daf6
SM
493 (set (make-local-variable 'font-lock-defaults)
494 '(compilation-mode-font-lock-keywords t))
0b18a8f6
RM
495 (run-hooks 'compilation-mode-hook))
496
497;; Prepare the buffer for the compilation parsing commands to work.
498(defun compilation-setup ()
499 ;; Make the buffer's mode line show process state.
ebf0b439 500 (setq mode-line-process '(":%s"))
d3cb357b
RM
501 (set (make-local-variable 'compilation-error-list) nil)
502 (set (make-local-variable 'compilation-old-error-list) nil)
503 (set (make-local-variable 'compilation-parsing-end) 1)
504 (set (make-local-variable 'compilation-directory-stack) nil)
0b18a8f6
RM
505 (setq compilation-last-buffer (current-buffer)))
506
507(defvar compilation-minor-mode nil
508 "Non-nil when in compilation-minor-mode.
509In this minor mode, all the error-parsing commands of the
510Compilation major mode are available.")
d6bd8dca 511(make-variable-buffer-local 'compilation-minor-mode)
0b18a8f6
RM
512
513(or (assq 'compilation-minor-mode minor-mode-alist)
514 (setq minor-mode-alist (cons '(compilation-minor-mode " Compilation")
515 minor-mode-alist)))
516(or (assq 'compilation-minor-mode minor-mode-map-alist)
4f6e4ad6
RS
517 (setq minor-mode-map-alist (cons (cons 'compilation-minor-mode
518 compilation-minor-mode-map)
0b18a8f6
RM
519 minor-mode-map-alist)))
520
d6bd8dca 521;;;###autoload
0b18a8f6
RM
522(defun compilation-minor-mode (&optional arg)
523 "Toggle compilation minor mode.
524With arg, turn compilation mode on if and only if arg is positive.
525See `compilation-mode'."
526 (interactive "P")
527 (if (setq compilation-minor-mode (if (null arg)
528 (null compilation-minor-mode)
529 (> (prefix-numeric-value arg) 0)))
530 (compilation-setup)))
55dfd2c4
RS
531
532;; Called when compilation process changes state.
55dfd2c4 533(defun compilation-sentinel (proc msg)
d3cb357b
RM
534 "Sentinel for compilation buffers."
535 (let ((buffer (process-buffer proc)))
ebff767c
RM
536 (if (memq (process-status proc) '(signal exit))
537 (progn
538 (if (null (buffer-name buffer))
539 ;; buffer killed
540 (set-process-buffer proc nil)
541 (let ((obuf (current-buffer))
542 omax opoint)
543 ;; save-excursion isn't the right thing if
544 ;; process-buffer is current-buffer
545 (unwind-protect
546 (progn
547 ;; Write something in the compilation buffer
548 ;; and hack its mode line.
549 (set-buffer buffer)
fdcba502
RS
550 (let ((buffer-read-only nil))
551 (setq omax (point-max)
552 opoint (point))
553 (goto-char omax)
554 ;; Record where we put the message, so we can ignore it
555 ;; later on.
556 (insert ?\n mode-name " " msg)
557 (forward-char -1)
558 (insert " at " (substring (current-time-string) 0 19))
559 (forward-char 1)
560 (setq mode-line-process
12b4943a
KH
561 (format ":%s [%d]" (process-status proc)
562 (process-exit-status proc)))
fdcba502
RS
563 ;; Since the buffer and mode line will show that the
564 ;; process is dead, we can delete it now. Otherwise it
565 ;; will stay around until M-x list-processes.
566 (delete-process proc)
567 ;; Force mode line redisplay soon.
5d5ab7ac 568 (force-mode-line-update))
ebff767c 569 (if (and opoint (< opoint omax))
6c43f2f9
RS
570 (goto-char opoint))
571 (if compilation-finish-function
572 (funcall compilation-finish-function buffer msg)))
573 (set-buffer obuf))))
ebff767c
RM
574 (setq compilation-in-progress (delq proc compilation-in-progress))
575 ))))
55dfd2c4 576
ad62b7f1
RM
577(defun compilation-filter (proc string)
578 "Process filter for compilation buffers.
4f6e4ad6 579Just inserts the text, but uses `insert-before-markers'."
991c70f8 580 (if (buffer-name (process-buffer proc))
ad62b7f1 581 (save-excursion
991c70f8
RS
582 (set-buffer (process-buffer proc))
583 (let ((buffer-read-only nil))
584 (save-excursion
585 (goto-char (process-mark proc))
586 (insert-before-markers string)
587 (set-marker (process-mark proc) (point)))))))
ad62b7f1 588
b5bb472e
RM
589;; Return the cdr of compilation-old-error-list for the error containing point.
590(defun compile-error-at-point ()
591 (compile-reinitialize-errors nil (point))
592 (let ((errors compilation-old-error-list))
593 (while (and errors
594 (> (point) (car (car errors))))
595 (setq errors (cdr errors)))
596 errors))
597
51ba27e7 598(defsubst compilation-buffer-p (buffer)
20c1daec
RM
599 (save-excursion
600 (set-buffer buffer)
601 (or compilation-minor-mode (eq major-mode 'compilation-mode))))
51ba27e7 602
646bd331
RM
603(defun compilation-next-error (n)
604 "Move point to the next error in the compilation buffer.
605Does NOT find the source line like \\[next-error]."
606 (interactive "p")
607 (or (compilation-buffer-p (current-buffer))
608 (error "Not in a compilation buffer."))
609 (setq compilation-last-buffer (current-buffer))
b5bb472e
RM
610
611 (let ((errors (compile-error-at-point)))
612
613 ;; Move to the error after the one containing point.
614 (goto-char (car (if (< n 0)
615 (let ((i 0)
616 (e compilation-old-error-list))
617 ;; See how many cdrs away ERRORS is from the start.
618 (while (not (eq e errors))
619 (setq i (1+ i)
620 e (cdr e)))
621 (if (> (- n) i)
622 (error "Moved back past first error")
623 (nth (+ i n) compilation-old-error-list)))
624 (let ((compilation-error-list (cdr errors)))
625 (compile-reinitialize-errors nil nil n)
626 (if compilation-error-list
627 (nth (1- n) compilation-error-list)
628 (error "Moved past last error"))))))))
646bd331
RM
629
630(defun compilation-previous-error (n)
631 "Move point to the previous error in the compilation buffer.
632Does NOT find the source line like \\[next-error]."
633 (interactive "p")
634 (compilation-next-error (- n)))
635
636
fc0094d7
RM
637;; Given an elt of `compilation-error-list', return an object representing
638;; the referenced file which is equal to (but not necessarily eq to) what
639;; this function would return for another error in the same file.
640(defsubst compilation-error-filedata (data)
b5bb472e
RM
641 (setq data (cdr data))
642 (if (markerp data)
fc0094d7 643 (marker-buffer data)
aead2f9f 644 (car data)))
b5bb472e 645
fc0094d7
RM
646;; Return a string describing a value from compilation-error-filedata.
647;; This value is not necessarily useful as a file name, but should be
648;; indicative to the user of what file's errors are being referred to.
649(defsubst compilation-error-filedata-file-name (filedata)
650 (if (bufferp filedata)
651 (buffer-file-name filedata)
652 (car filedata)))
653
b5bb472e
RM
654(defun compilation-next-file (n)
655 "Move point to the next error for a different file than the current one."
656 (interactive "p")
657 (or (compilation-buffer-p (current-buffer))
658 (error "Not in a compilation buffer."))
659 (setq compilation-last-buffer (current-buffer))
660
661 (let ((reversed (< n 0))
fc0094d7 662 errors filedata)
b5bb472e
RM
663
664 (if (not reversed)
665 (setq errors (or (compile-error-at-point)
666 (error "Moved past last error")))
667
668 ;; Get a reversed list of the errors up through the one containing point.
669 (compile-reinitialize-errors nil (point))
670 (setq errors (reverse compilation-old-error-list)
671 n (- n))
672
673 ;; Ignore errors after point. (car ERRORS) will be the error
674 ;; containing point, (cadr ERRORS) the one before it.
675 (while (and errors
676 (< (point) (car (car errors))))
677 (setq errors (cdr errors))))
678
679 (while (> n 0)
fc0094d7
RM
680 (setq filedata (compilation-error-filedata (car errors)))
681
682 ;; Skip past the following errors for this file.
683 (while (equal filedata
684 (compilation-error-filedata
685 (car (or errors
686 (if reversed
687 (error "%s the first erring file"
688 (compilation-error-filedata-file-name
689 filedata))
690 (let ((compilation-error-list nil))
691 ;; Parse some more.
692 (compile-reinitialize-errors nil nil 2)
693 (setq errors compilation-error-list)))
694 (error "%s is the last erring file"
695 (compilation-error-filedata-file-name
696 filedata))))))
b5bb472e
RM
697 (setq errors (cdr errors)))
698
699 (setq n (1- n)))
700
701 ;; Move to the following error.
702 (goto-char (car (car (or errors
703 (if reversed
704 (error "This is the first erring file")
705 (let ((compilation-error-list nil))
706 ;; Parse the last one.
707 (compile-reinitialize-errors nil nil 1)
708 compilation-error-list))))))))
709
710(defun compilation-previous-file (n)
711 "Move point to the previous error for a different file than the current one."
712 (interactive "p")
713 (compilation-next-file (- n)))
714
715
55dfd2c4
RS
716(defun kill-compilation ()
717 "Kill the process made by the \\[compile] command."
718 (interactive)
d3cb357b 719 (let ((buffer (compilation-find-buffer)))
55dfd2c4 720 (if (get-buffer-process buffer)
d3cb357b
RM
721 (interrupt-process (get-buffer-process buffer))
722 (error "The compilation process is not running."))))
723
55dfd2c4 724
d3cb357b
RM
725;; Parse any new errors in the compilation buffer,
726;; or reparse from the beginning if the user has asked for that.
eaa3cac5
RM
727(defun compile-reinitialize-errors (reparse
728 &optional limit-search find-at-least)
d3cb357b
RM
729 (save-excursion
730 (set-buffer compilation-last-buffer)
731 ;; If we are out of errors, or if user says "reparse",
732 ;; discard the info we have, to force reparsing.
733 (if (or (eq compilation-error-list t)
eaa3cac5 734 reparse)
51ba27e7 735 (compilation-forget-errors))
c540863c 736 (if (and compilation-error-list
b5bb472e
RM
737 (or (not limit-search)
738 (> compilation-parsing-end limit-search))
c540863c 739 (or (not find-at-least)
a72ba929 740 (>= (length compilation-error-list) find-at-least)))
d3cb357b
RM
741 ;; Since compilation-error-list is non-nil, it points to a specific
742 ;; error the user wanted. So don't move it around.
743 nil
51ba27e7
RM
744 ;; This was here for a long time (before my rewrite); why? --roland
745 ;;(switch-to-buffer compilation-last-buffer)
55dfd2c4 746 (set-buffer-modified-p nil)
b5bb472e 747 (if (< compilation-parsing-end (point-max))
51ba27e7 748 ;; compilation-error-list might be non-nil if we have a non-nil
676a14e1 749 ;; LIMIT-SEARCH or FIND-AT-LEAST arg. In that case its value
51ba27e7
RM
750 ;; records the current position in the error list, and we must
751 ;; preserve that after reparsing.
752 (let ((error-list-pos compilation-error-list))
b5bb472e 753 (funcall compilation-parse-errors-function
51ba27e7
RM
754 limit-search
755 (and find-at-least
756 ;; We only need enough new parsed errors to reach
757 ;; FIND-AT-LEAST errors past the current
758 ;; position.
759 (- find-at-least (length compilation-error-list))))
760 ;; Remember the entire list for compilation-forget-errors. If
761 ;; this is an incremental parse, append to previous list. If
762 ;; we are parsing anew, compilation-forget-errors cleared
763 ;; compilation-old-error-list above.
764 (setq compilation-old-error-list
765 (nconc compilation-old-error-list compilation-error-list))
766 (if error-list-pos
767 ;; We started in the middle of an existing list of parsed
768 ;; errors before parsing more; restore that position.
769 (setq compilation-error-list error-list-pos))
b5bb472e 770 )))))
55dfd2c4 771
f98494a4
RS
772(defun compile-mouse-goto-error (event)
773 (interactive "e")
e78e10ca
KH
774 (save-excursion
775 (set-buffer (window-buffer (posn-window (event-end event))))
776 (goto-char (posn-point (event-end event)))
777
778 (or (compilation-buffer-p (current-buffer))
779 (error "Not in a compilation buffer."))
780 (setq compilation-last-buffer (current-buffer))
781 (compile-reinitialize-errors nil (point))
782
783 ;; Move to bol; the marker for the error on this line will point there.
784 (beginning-of-line)
785
786 ;; Move compilation-error-list to the elt of compilation-old-error-list
787 ;; we want.
788 (setq compilation-error-list compilation-old-error-list)
789 (while (and compilation-error-list
790 (> (point) (car (car compilation-error-list))))
791 (setq compilation-error-list (cdr compilation-error-list)))
792 (or compilation-error-list
793 (error "No error to go to")))
794 (select-window (posn-window (event-end event)))
795 ;; Move to another window, so that next-error's window changes
796 ;; result in the desired setup.
797 (or (one-window-p)
798 (progn
799 (other-window -1)
800 ;; other-window changed the selected buffer,
801 ;; but we didn't want to do that.
802 (set-buffer compilation-last-buffer)))
803
804 (push-mark)
805 (next-error 1))
f98494a4 806
55dfd2c4
RS
807(defun compile-goto-error (&optional argp)
808 "Visit the source for the error message point is on.
31efa7c9 809Use this command in a compilation log buffer. Sets the mark at point there.
b2bb6ec8 810\\[universal-argument] as a prefix arg means to reparse the buffer's error messages first;
55dfd2c4
RS
811other kinds of prefix arguments are ignored."
812 (interactive "P")
d3cb357b
RM
813 (or (compilation-buffer-p (current-buffer))
814 (error "Not in a compilation buffer."))
815 (setq compilation-last-buffer (current-buffer))
eaa3cac5 816 (compile-reinitialize-errors (consp argp) (point))
646bd331 817
15d1a8da
RM
818 ;; Move to bol; the marker for the error on this line will point there.
819 (beginning-of-line)
820
646bd331 821 ;; Move compilation-error-list to the elt of compilation-old-error-list
643f763f 822 ;; we want.
646bd331 823 (setq compilation-error-list compilation-old-error-list)
643f763f
RM
824 (while (and compilation-error-list
825 (> (point) (car (car compilation-error-list))))
646bd331
RM
826 (setq compilation-error-list (cdr compilation-error-list)))
827
55dfd2c4
RS
828 ;; Move to another window, so that next-error's window changes
829 ;; result in the desired setup.
830 (or (one-window-p)
646bd331
RM
831 (progn
832 (other-window -1)
833 ;; other-window changed the selected buffer,
834 ;; but we didn't want to do that.
835 (set-buffer compilation-last-buffer)))
836
31efa7c9 837 (push-mark)
643f763f 838 (next-error 1))
55dfd2c4 839
d3cb357b
RM
840;; Return a compilation buffer.
841;; If the current buffer is a compilation buffer, return it.
842;; If compilation-last-buffer is set to a live buffer, use that.
843;; Otherwise, look for a compilation buffer and signal an error
844;; if there are none.
4746118a
JB
845(defun compilation-find-buffer (&optional other-buffer)
846 (if (and (not other-buffer)
847 (compilation-buffer-p (current-buffer)))
d3cb357b
RM
848 ;; The current buffer is a compilation buffer.
849 (current-buffer)
4746118a
JB
850 (if (and compilation-last-buffer (buffer-name compilation-last-buffer)
851 (or (not other-buffer) (not (eq compilation-last-buffer
852 (current-buffer)))))
d3cb357b
RM
853 compilation-last-buffer
854 (let ((buffers (buffer-list)))
4746118a
JB
855 (while (and buffers (or (not (compilation-buffer-p (car buffers)))
856 (and other-buffer
857 (eq (car buffers) (current-buffer)))))
d3cb357b
RM
858 (setq buffers (cdr buffers)))
859 (if buffers
860 (car buffers)
4746118a
JB
861 (or (and other-buffer
862 (compilation-buffer-p (current-buffer))
863 ;; The current buffer is a compilation buffer.
864 (progn
865 (if other-buffer
866 (message "This is the only compilation buffer."))
867 (current-buffer)))
868 (error "No compilation started!")))))))
d3cb357b
RM
869
870;;;###autoload
55dfd2c4
RS
871(defun next-error (&optional argp)
872 "Visit next compilation error message and corresponding source code.
873This operates on the output from the \\[compile] command.
874If all preparsed error messages have been processed,
875the error message buffer is checked for new ones.
876
877A prefix arg specifies how many error messages to move;
878negative means move back to previous error messages.
879Just C-u as a prefix means reparse the error message buffer
880and start at the first error.
881
882\\[next-error] normally applies to the most recent compilation started,
883but as long as you are in the middle of parsing errors from one compilation
884output buffer, you stay with that compilation output buffer.
885
886Use \\[next-error] in a compilation output buffer to switch to
887processing errors from that compilation.
888
d3cb357b
RM
889See variables `compilation-parse-errors-function' and
890\`compilation-error-regexp-alist' for customization ideas."
55dfd2c4 891 (interactive "P")
d3cb357b 892 (setq compilation-last-buffer (compilation-find-buffer))
eaa3cac5
RM
893 (compilation-goto-locus (compilation-next-error-locus
894 ;; We want to pass a number here only if
895 ;; we got a numeric prefix arg, not just C-u.
896 (and (not (consp argp))
897 (prefix-numeric-value argp))
898 (consp argp))))
899;;;###autoload (define-key ctl-x-map "`" 'next-error)
900
e60476ca
RS
901(defun previous-error ()
902 "Visit previous compilation error message and corresponding source code.
903This operates on the output from the \\[compile] command."
904 (interactive)
eae2c972 905 (next-error -1))
e60476ca
RS
906
907(defun first-error ()
908 "Reparse the error message buffer and start at the first error
909Visit corresponding source code.
910This operates on the output from the \\[compile] command."
911 (interactive)
eae2c972 912 (next-error '(4)))
e60476ca 913
b4300a1a 914(defun compilation-next-error-locus (&optional move reparse silent)
eaa3cac5
RM
915 "Visit next compilation error and return locus in corresponding source code.
916This operates on the output from the \\[compile] command.
917If all preparsed error messages have been processed,
918the error message buffer is checked for new ones.
919
920Returns a cons (ERROR . SOURCE) of two markers: ERROR is a marker at the
921location of the error message in the compilation buffer, and SOURCE is a
922marker at the location in the source code indicated by the error message.
923
924Optional first arg MOVE says how many error messages to move forwards (or
925backwards, if negative); default is 1. Optional second arg REPARSE, if
926non-nil, says to reparse the error message buffer and reset to the first
b4300a1a
RS
927error (plus MOVE - 1). If optional third argument SILENT is non-nil, return
928nil instead of raising an error if there are no more errors.
eaa3cac5
RM
929
930The current buffer should be the desired compilation output buffer."
931 (or move (setq move 1))
932 (compile-reinitialize-errors reparse nil (and (not reparse)
933 (if (< move 1) 0 (1- move))))
d3cb357b 934 (let (next-errors next-error)
7fb63acd
RS
935 (catch 'no-next-error
936 (save-excursion
937 (set-buffer compilation-last-buffer)
938 ;; compilation-error-list points to the "current" error.
939 (setq next-errors
940 (if (> move 0)
941 (nthcdr (1- move)
942 compilation-error-list)
943 ;; Zero or negative arg; we need to move back in the list.
944 (let ((n (1- move))
945 (i 0)
946 (e compilation-old-error-list))
947 ;; See how many cdrs away the current error is from the start.
948 (while (not (eq e compilation-error-list))
949 (setq i (1+ i)
950 e (cdr e)))
951 (if (> (- n) i)
952 (error "Moved back past first error")
953 (nthcdr (+ i n) compilation-old-error-list))))
954 next-error (car next-errors))
955 (while
956 (if (null next-error)
957 (progn
958 (and move (/= move 1)
959 (error (if (> move 0)
960 "Moved past last error")
961 "Moved back past first error"))
962 ;; Forget existing error messages if compilation has finished.
963 (if (not (and (get-buffer-process (current-buffer))
964 (eq (process-status
965 (get-buffer-process
966 (current-buffer)))
967 'run)))
968 (compilation-forget-errors))
969 (if silent
970 (throw 'no-next-error nil)
971 (error (concat compilation-error-message
972 (and (get-buffer-process (current-buffer))
973 (eq (process-status
974 (get-buffer-process
975 (current-buffer)))
976 'run)
b4300a1a 977 " yet")))))
7fb63acd
RS
978 (setq compilation-error-list (cdr next-errors))
979 (if (null (cdr next-error))
980 ;; This error is boring. Go to the next.
981 t
982 (or (markerp (cdr next-error))
983 ;; This error has a filename/lineno pair.
984 ;; Find the file and turn it into a marker.
985 (let* ((fileinfo (car (cdr next-error)))
20c1daec
RM
986 (buffer (apply 'compilation-find-file
987 (car next-error) fileinfo)))
7fb63acd
RS
988 (if (null buffer)
989 ;; We can't find this error's file.
990 ;; Remove all errors in the same file.
991 (progn
992 (setq next-errors compilation-old-error-list)
993 (while next-errors
994 (and (consp (cdr (car next-errors)))
995 (equal (car (cdr (car next-errors)))
996 fileinfo)
997 (progn
998 (set-marker (car (car next-errors)) nil)
999 (setcdr (car next-errors) nil)))
1000 (setq next-errors (cdr next-errors)))
1001 ;; Look for the next error.
1002 t)
1003 ;; We found the file. Get a marker for this error.
1004 ;; compilation-old-error-list is a buffer-local
1005 ;; variable, so we must be careful to extract its value
1006 ;; before switching to the source file buffer.
1007 (let ((errors compilation-old-error-list)
1008 (last-line (nth 1 (cdr next-error)))
1009 (column (nth 2 (cdr next-error))))
1010 (set-buffer buffer)
1011 (save-excursion
1012 (save-restriction
1013 (widen)
1014 (goto-line last-line)
4dbf8a2c 1015 (if (and column (> column 0))
4ad26d84
RM
1016 ;; Columns in error msgs are 1-origin.
1017 (move-to-column (1- column))
7fb63acd
RS
1018 (beginning-of-line))
1019 (setcdr next-error (point-marker))
1020 ;; Make all the other error messages referring
1021 ;; to the same file have markers into the buffer.
1022 (while errors
1023 (and (consp (cdr (car errors)))
1024 (equal (car (cdr (car errors))) fileinfo)
1025 (let* ((this (nth 1 (cdr (car errors))))
1026 (column (nth 2 (cdr (car errors))))
1027 (lines (- this last-line)))
1028 (if (eq selective-display t)
1029 ;; When selective-display is t,
1030 ;; each C-m is a line boundary,
1031 ;; as well as each newline.
1032 (if (< lines 0)
1033 (re-search-backward "[\n\C-m]"
1034 nil 'end
1035 (- lines))
1036 (re-search-forward "[\n\C-m]"
1037 nil 'end
1038 lines))
1039 (forward-line lines))
eae2c972
RM
1040 (if (and column (> column 1))
1041 (move-to-column (1- column))
1042 (beginning-of-line))
7fb63acd
RS
1043 (setq last-line this)
1044 (setcdr (car errors) (point-marker))))
1045 (setq errors (cdr errors)))))))))
1046 ;; If we didn't get a marker for this error, or this
1047 ;; marker's buffer was killed, go on to the next one.
1048 (or (not (markerp (cdr next-error)))
1049 (not (marker-buffer (cdr next-error))))))
1050 (setq next-errors compilation-error-list
b4300a1a 1051 next-error (car next-errors)))))
d3cb357b
RM
1052
1053 ;; Skip over multiple error messages for the same source location,
1054 ;; so the next C-x ` won't go to an error in the same place.
1055 (while (and compilation-error-list
1056 (equal (cdr (car compilation-error-list)) (cdr next-error)))
1057 (setq compilation-error-list (cdr compilation-error-list)))
1058
eaa3cac5
RM
1059 ;; We now have a marker for the position of the error source code.
1060 ;; NEXT-ERROR is a cons (ERROR . SOURCE) of two markers.
1061 next-error))
1062
1063(defun compilation-goto-locus (next-error)
1064 "Jump to an error locus returned by `compilation-next-error-locus'.
1065Takes one argument, a cons (ERROR . SOURCE) of two markers.
1066Selects a window with point at SOURCE, with another window displaying ERROR."
e89a7d77
RS
1067 (if (and (window-dedicated-p (selected-window))
1068 (eq (selected-window) (frame-root-window)))
1069 (switch-to-buffer-other-frame (marker-buffer (cdr next-error)))
1070 (switch-to-buffer (marker-buffer (cdr next-error))))
eaa3cac5
RM
1071 (goto-char (cdr next-error))
1072 ;; If narrowing got in the way of
1073 ;; going to the right place, widen.
1074 (or (= (point) (marker-position (cdr next-error)))
1075 (progn
1076 (widen)
1077 (goto-char (cdr next-error))))
1078
1079 ;; Show compilation buffer in other window, scrolled to this error.
1080 (let* ((pop-up-windows t)
eae2c972 1081 ;; Use an existing window if it is in a visible frame.
c94b02d6 1082 (w (or (get-buffer-window (marker-buffer (car next-error)) 'visible)
eae2c972 1083 ;; Pop up a window.
c94b02d6 1084 (display-buffer (marker-buffer (car next-error))))))
eaa3cac5 1085 (set-window-point w (car next-error))
c94b02d6
RS
1086 (set-window-start w (car next-error))
1087 (compilation-set-window-height w)))
eaa3cac5 1088\f
d3cb357b
RM
1089;; Find a buffer for file FILENAME.
1090;; Search the directories in compilation-search-path.
1091;; A nil in compilation-search-path means to try the
1092;; current directory, which is passed in DIR.
1093;; If FILENAME is not found at all, ask the user where to find it.
1094;; Pop up the buffer containing MARKER and scroll to MARKER if we ask the user.
20c1daec
RM
1095(defun compilation-find-file (marker filename dir &rest formats)
1096 (or formats (setq formats '("%s")))
d3cb357b 1097 (let ((dirs compilation-search-path)
20c1daec 1098 result thisdir fmts name)
d3cb357b 1099 (while (and dirs (null result))
20c1daec
RM
1100 (setq thisdir (or (car dirs) dir)
1101 fmts formats)
1102 (while (and fmts (null result))
1103 (setq name (expand-file-name (format (car fmts) filename) thisdir)
1104 result (and (file-exists-p name)
1105 (find-file-noselect name))
1106 fmts (cdr fmts)))
1107 (setq dirs (cdr dirs)))
d3cb357b
RM
1108 (or result
1109 ;; The file doesn't exist.
1110 ;; Ask the user where to find it.
1111 ;; If he hits C-g, then the next time he does
1112 ;; next-error, he'll skip past it.
1113 (progn
1114 (let* ((pop-up-windows t)
1115 (w (display-buffer (marker-buffer marker))))
1116 (set-window-point w marker)
1117 (set-window-start w marker))
1118 (setq name
1119 (expand-file-name
fe213bf8
RS
1120 (read-file-name (format "Find this error in: (default %s) "
1121 filename)
1122 dir filename t)))
d3cb357b
RM
1123 (if (file-directory-p name)
1124 (setq name (concat (file-name-as-directory name) filename)))
1125 (if (file-exists-p name)
1126 (find-file-noselect name))))))
1127
1128;; Set compilation-error-list to nil, and unchain the markers that point to the
1129;; error messages and their text, so that they no longer slow down gap motion.
1130;; This would happen anyway at the next garbage collection, but it is better to
646bd331 1131;; do it right away.
55dfd2c4
RS
1132(defun compilation-forget-errors ()
1133 (while compilation-old-error-list
1134 (let ((next-error (car compilation-old-error-list)))
1135 (set-marker (car next-error) nil)
d3cb357b
RM
1136 (if (markerp (cdr next-error))
1137 (set-marker (cdr next-error) nil)))
55dfd2c4 1138 (setq compilation-old-error-list (cdr compilation-old-error-list)))
bd3910fe 1139 (setq compilation-error-list nil
51ba27e7
RM
1140 compilation-directory-stack nil
1141 compilation-parsing-end 1))
d3cb357b
RM
1142
1143
1144(defun count-regexp-groupings (regexp)
1145 "Return the number of \\( ... \\) groupings in REGEXP (a string)."
1146 (let ((groupings 0)
1147 (len (length regexp))
1148 (i 0)
1149 c)
1150 (while (< i len)
1151 (setq c (aref regexp i)
1152 i (1+ i))
1153 (cond ((= c ?\[)
1154 ;; Find the end of this [...].
1155 (while (and (< i len)
1156 (not (= (aref regexp i) ?\])))
1157 (setq i (1+ i))))
1158 ((= c ?\\)
1159 (if (< i len)
1160 (progn
1161 (setq c (aref regexp i)
1162 i (1+ i))
1163 (if (= c ?\))
1164 ;; We found the end of a grouping,
1165 ;; so bump our counter.
1166 (setq groupings (1+ groupings))))))))
1167 groupings))
55dfd2c4 1168
c540863c 1169(defun compilation-parse-errors (limit-search find-at-least)
55dfd2c4 1170 "Parse the current buffer as grep, cc or lint error messages.
d3cb357b 1171See variable `compilation-parse-errors-function' for the interface it uses."
55dfd2c4
RS
1172 (setq compilation-error-list nil)
1173 (message "Parsing error messages...")
51501e54 1174 (let (text-buffer orig orig-expanded parent-expanded
d3cb357b 1175 regexp enter-group leave-group error-group
b2bb6ec8 1176 alist subexpr error-regexp-groups
c540863c 1177 (found-desired nil)
6c43f2f9 1178 (compilation-num-errors-found 0))
d3cb357b 1179
55dfd2c4
RS
1180 ;; Don't reparse messages already seen at last parse.
1181 (goto-char compilation-parsing-end)
1182 ;; Don't parse the first two lines as error messages.
1183 ;; This matters for grep.
1184 (if (bobp)
2e4301ef
RM
1185 (progn
1186 (forward-line 2)
1187 ;; Move back so point is before the newline.
1188 ;; This matters because some error regexps use \n instead of ^
1189 ;; to be faster.
1190 (forward-char -1)))
d3cb357b
RM
1191
1192 ;; Compile all the regexps we want to search for into one.
1193 (setq regexp (concat "\\(" compilation-enter-directory-regexp "\\)\\|"
1194 "\\(" compilation-leave-directory-regexp "\\)\\|"
1195 "\\(" (mapconcat (function
1196 (lambda (elt)
1197 (concat "\\(" (car elt) "\\)")))
1198 compilation-error-regexp-alist
1199 "\\|") "\\)"))
1200
1201 ;; Find out how many \(...\) groupings are in each of the regexps, and set
1202 ;; *-GROUP to the grouping containing each constituent regexp (whose
1203 ;; subgroups will come immediately thereafter) of the big regexp we have
1204 ;; just constructed.
1205 (setq enter-group 1
1206 leave-group (+ enter-group
1207 (count-regexp-groupings
1208 compilation-enter-directory-regexp)
1209 1)
1210 error-group (+ leave-group
1211 (count-regexp-groupings
1212 compilation-leave-directory-regexp)
1213 1))
1214
72244bea
RM
1215 ;; Compile an alist (IDX FILE LINE [COL]), where IDX is the number of
1216 ;; the subexpression for an entire error-regexp, and FILE and LINE (and
1217 ;; possibly COL) are the numbers for the subexpressions giving the file
1218 ;; name and line number (and possibly column number).
d4035a26
RM
1219 (setq alist (or compilation-error-regexp-alist
1220 (error "compilation-error-regexp-alist is empty!"))
d3cb357b
RM
1221 subexpr (1+ error-group))
1222 (while alist
72244bea
RM
1223 (setq error-regexp-groups
1224 (cons (list subexpr
1225 (+ subexpr (nth 1 (car alist)))
1226 (+ subexpr (nth 2 (car alist)))
8425a49a
RM
1227 (and (nth 3 (car alist))
1228 (+ subexpr (nth 3 (car alist)))))
72244bea 1229 error-regexp-groups))
d3cb357b
RM
1230 (setq subexpr (+ subexpr 1 (count-regexp-groupings (car (car alist)))))
1231 (setq alist (cdr alist)))
1232
51501e54
RS
1233 (setq orig default-directory)
1234 (setq orig-expanded (file-truename orig))
1235 (setq parent-expanded (expand-file-name "../" orig-expanded))
1236
646bd331 1237 (while (and (not found-desired)
b2bb6ec8
RM
1238 ;; We don't just pass LIMIT-SEARCH to re-search-forward
1239 ;; because we want to find matches containing LIMIT-SEARCH
1240 ;; but which extend past it.
646bd331 1241 (re-search-forward regexp nil t))
dadb11fb 1242
d3cb357b
RM
1243 ;; Figure out which constituent regexp matched.
1244 (cond ((match-beginning enter-group)
1245 ;; The match was the enter-directory regexp.
1246 (let ((dir
1247 (file-name-as-directory
1248 (expand-file-name
1249 (buffer-substring (match-beginning (+ enter-group 1))
1250 (match-end (+ enter-group 1)))))))
51501e54
RS
1251 ;; The directory name in the "entering" message
1252 ;; is a truename. Try to convert it to a form
1253 ;; like what the user typed in.
1254 (setq dir
1255 (compile-abbreviate-directory dir orig orig-expanded
1256 parent-expanded))
d3cb357b
RM
1257 (setq compilation-directory-stack
1258 (cons dir compilation-directory-stack))
1259 (and (file-directory-p dir)
51ba27e7
RM
1260 (setq default-directory dir)))
1261
1262 (and limit-search (>= (point) limit-search)
1263 ;; The user wanted a specific error, and we're past it.
1264 ;; We do this check here (and in the leave-group case)
1265 ;; rather than at the end of the loop because if the last
1266 ;; thing seen is an error message, we must carefully
1267 ;; discard the last error when it is the first in a new
1268 ;; file (see below in the error-group case).
1269 (setq found-desired t)))
1270
d3cb357b
RM
1271 ((match-beginning leave-group)
1272 ;; The match was the leave-directory regexp.
1273 (let ((beg (match-beginning (+ leave-group 1)))
1274 (stack compilation-directory-stack))
1275 (if beg
1276 (let ((dir
1277 (file-name-as-directory
1278 (expand-file-name
1279 (buffer-substring beg
1280 (match-end (+ leave-group
1281 1)))))))
51501e54
RS
1282 ;; The directory name in the "entering" message
1283 ;; is a truename. Try to convert it to a form
1284 ;; like what the user typed in.
1285 (setq dir
1286 (compile-abbreviate-directory dir orig orig-expanded
1287 parent-expanded))
d3cb357b
RM
1288 (while (and stack
1289 (not (string-equal (car stack) dir)))
1290 (setq stack (cdr stack)))))
1291 (setq compilation-directory-stack (cdr stack))
1292 (setq stack (car compilation-directory-stack))
1293 (if stack
1294 (setq default-directory stack))
51ba27e7
RM
1295 )
1296
1297 (and limit-search (>= (point) limit-search)
1298 ;; The user wanted a specific error, and we're past it.
1299 ;; We do this check here (and in the enter-group case)
1300 ;; rather than at the end of the loop because if the last
1301 ;; thing seen is an error message, we must carefully
1302 ;; discard the last error when it is the first in a new
1303 ;; file (see below in the error-group case).
1304 (setq found-desired t)))
1305
d3cb357b
RM
1306 ((match-beginning error-group)
1307 ;; The match was the composite error regexp.
1308 ;; Find out which individual regexp matched.
1309 (setq alist error-regexp-groups)
1310 (while (and alist
1311 (null (match-beginning (car (car alist)))))
1312 (setq alist (cdr alist)))
1313 (if alist
1314 (setq alist (car alist))
d4035a26 1315 (error "compilation-parse-errors: impossible regexp match!"))
d3cb357b
RM
1316
1317 ;; Extract the file name and line number from the error message.
1f82d0e4 1318 (let ((beginning-of-match (match-beginning 0)) ;looking-at nukes
1e0e614d
RM
1319 (filename (buffer-substring (match-beginning (nth 1 alist))
1320 (match-end (nth 1 alist))))
72244bea
RM
1321 (linenum (string-to-int
1322 (buffer-substring
1323 (match-beginning (nth 2 alist))
1324 (match-end (nth 2 alist)))))
1325 (column (and (nth 3 alist)
08b1edf4 1326 (match-beginning (nth 3 alist))
72244bea
RM
1327 (string-to-int
1328 (buffer-substring
1329 (match-beginning (nth 3 alist))
1330 (match-end (nth 3 alist)))))))
1331
1e0e614d
RM
1332 ;; Check for a comint-file-name-prefix and prepend it if
1333 ;; appropriate. (This is very useful for
1334 ;; compilation-minor-mode in an rlogin-mode buffer.)
1335 (and (boundp 'comint-file-name-prefix)
1336 ;; If the file name is relative, default-directory will
1337 ;; already contain the comint-file-name-prefix (done by
1338 ;; compile-abbreviate-directory).
1339 (file-name-absolute-p filename)
1340 (setq filename (concat comint-file-name-prefix filename)))
20c1daec
RM
1341 (setq filename (cons filename (cons default-directory
1342 (nthcdr 4 alist))))
1343
1e0e614d 1344
d3cb357b
RM
1345 ;; Locate the erring file and line.
1346 ;; Cons a new elt onto compilation-error-list,
1347 ;; giving a marker for the current compilation buffer
1348 ;; location, and the file and line number of the error.
1349 (save-excursion
eae2c972
RM
1350 ;; Save as the start of the error the beginning of the
1351 ;; line containing the match unless the match starts at a
1352 ;; newline, in which case the beginning of the next line.
1353 (goto-char beginning-of-match)
1354 (forward-line (if (eolp) 1 0))
ba2700db 1355 (let ((this (cons (point-marker)
533304f8 1356 (list filename linenum column))))
ba2700db
RS
1357 ;; Don't add the same source line more than once.
1358 (if (equal (cdr this) (cdr (car compilation-error-list)))
1359 nil
1360 (setq compilation-error-list
1361 (cons this
1362 compilation-error-list))
1363 (setq compilation-num-errors-found
1364 (1+ compilation-num-errors-found)))))
51ba27e7
RM
1365 (and (or (and find-at-least (> compilation-num-errors-found
1366 find-at-least))
1367 (and limit-search (>= (point) limit-search)))
1368 ;; We have found as many new errors as the user wants,
1369 ;; or past the buffer position he indicated. We
1370 ;; continue to parse until we have seen all the
1371 ;; consecutive errors in the same file, so the error
1372 ;; positions will be recorded as markers in this buffer
1373 ;; that might change.
1f82d0e4
RM
1374 (cdr compilation-error-list) ; Must check at least two.
1375 (not (equal (car (cdr (nth 0 compilation-error-list)))
1376 (car (cdr (nth 1 compilation-error-list)))))
1377 (progn
1378 ;; Discard the error just parsed, so that the next
1379 ;; parsing run can get it and the following errors in
1380 ;; the same file all at once. If we didn't do this, we
1381 ;; would have the same problem we are trying to avoid
1382 ;; with the test above, just delayed until the next run!
1383 (setq compilation-error-list
1384 (cdr compilation-error-list))
1385 (goto-char beginning-of-match)
1386 (setq found-desired t)))
1387 )
41acdfa9 1388 )
d3cb357b 1389 (t
d4035a26 1390 (error "compilation-parse-errors: known groups didn't match!")))
dadb11fb 1391
51ba27e7 1392 (message "Parsing error messages...%d (%.0f%% of buffer)"
6c43f2f9 1393 compilation-num-errors-found
51ba27e7
RM
1394 ;; Use floating-point because (* 100 (point)) frequently
1395 ;; exceeds the range of Emacs Lisp integers.
1396 (/ (* 100.0 (point)) (point-max)))
dadb11fb 1397
b2bb6ec8
RM
1398 (and limit-search (>= (point) limit-search)
1399 ;; The user wanted a specific error, and we're past it.
1400 (setq found-desired t)))
c540863c
RM
1401 (setq compilation-parsing-end (if found-desired
1402 (point)
1403 ;; We have searched the whole buffer.
1404 (point-max))))
1405 (setq compilation-error-list (nreverse compilation-error-list))
1406 (message "Parsing error messages...done"))
55dfd2c4 1407
51501e54
RS
1408;; If directory DIR is a subdir of ORIG or of ORIG's parent,
1409;; return a relative name for it starting from ORIG or its parent.
1410;; ORIG-EXPANDED is an expanded version of ORIG.
1411;; PARENT-EXPANDED is an expanded version of ORIG's parent.
1412;; Those two args could be computed here, but we run faster by
1413;; having the caller compute them just once.
1414(defun compile-abbreviate-directory (dir orig orig-expanded parent-expanded)
1e0e614d
RM
1415 ;; Check for a comint-file-name-prefix and prepend it if appropriate.
1416 ;; (This is very useful for compilation-minor-mode in an rlogin-mode
1417 ;; buffer.)
1418 (if (boundp 'comint-file-name-prefix)
1419 (setq dir (concat comint-file-name-prefix dir)))
1420
51501e54
RS
1421 (if (and (> (length dir) (length orig-expanded))
1422 (string= orig-expanded
1423 (substring dir 0 (length orig-expanded))))
1424 (setq dir
1425 (concat orig
1426 (substring dir (length orig-expanded)))))
1427 (if (and (> (length dir) (length parent-expanded))
1428 (string= parent-expanded
1429 (substring dir 0 (length parent-expanded))))
1430 (setq dir
1431 (concat (file-name-directory
1432 (directory-file-name orig))
1433 (substring dir (length parent-expanded)))))
1434 dir)
1435
4746118a 1436(provide 'compile)
fad160d5
ER
1437
1438;;; compile.el ends here