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