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