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