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