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