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