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