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