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