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