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