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