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