1 ;;; compile.el --- run compiler as inferior of Emacs, parse error messages.
3 ;; Copyright (C) 1985, 86, 87, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
5 ;; Author: Roland McGrath <roland@prep.ai.mit.edu>
7 ;; Keywords: tools, processes
9 ;; This file is part of GNU Emacs.
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)
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.
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.
28 ;; This package provides the compile and grep facilities documented in
29 ;; the Emacs user's manual.
34 (defvar compilation-mode-hook nil
35 "*List of hook functions run by `compilation-mode' (see `run-hooks').")
38 (defvar compilation-window-height nil
39 "*Number of lines in a compilation window. If nil, use Emacs default.")
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.
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.")
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.
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.
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
69 (defvar compilation-old-error-list nil
70 "Value of `compilation-error-list' after errors were parsed.")
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
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.")
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.")
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) \"*\")'.")
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.")
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.")
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].")
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")
119 (defvar compilation-parsing-end nil
120 "Position of end of buffer when last error messages were parsed.")
122 (defvar compilation-error-message "No more errors"
123 "Message to print when no more matches are found.")
125 (defvar compilation-num-errors-found)
127 (defvar compilation-error-regexp-alist
129 ;; NOTE! See also grep-regexp-alist, below.
131 ;; 4.3BSD grep, cc, lint pass 1:
132 ;; /usr/src/foo/foo.c(8): warning: w may be used before set
134 ;; foo.c:8: error message
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
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.
144 \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
145 :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 2 5)
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)
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)
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]*$"
163 ;; 4.3BSD lint pass 3
164 ;; bloofle defined( /users/wolfgang/foo.c(4) ), but never used
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)
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)
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)
190 ;; Apollo cc, 4.3BSD fc:
191 ;; "foo.f", line 3: Error: syntax error near end of statement
193 ;; "vvouch.c", line 19.5: 1506-046 (S) Syntax error.
195 ;; File "foobar.ml", lines 5-8, characters 20-155: blah blah
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)
205 ;; MIPS RISC CC - the one distributed with Ultrix:
206 ;; ccom: Error: foo.c, line 2: syntax error
208 ;; /usr/lib/cmplrs/cc/cfe: Error: foo.c: 1: blah blah
209 (".*rror: \\([^,\" \n\t]+\\)[,:] \\(line \\)?\\([0-9]+\\):" 1 3)
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.
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)
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)
225 ;; Cray C compiler error messages
226 ("\\(cc\\| cft\\)-[0-9]+ c\\(c\\|f77\\): ERROR \\([^,\n]+, \\)* File = \\([^,\n]+\\), Line = \\([0-9]+\\)" 4 5)
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)
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)
239 ;; syntax error at automake line 922, near "':'"
240 ("\n.* at \\([^ ]+\\) line \\([0-9]+\\)," 1 2)
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
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'.")
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.")
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'.")
263 ;; The system null device. (Should reference NULL_DEVICE from C.)
264 (defvar grep-null-device "/dev/null" "The system null device.")
266 ;; Use zgrep if available, to work nicely with compressed files.
267 ;; Otherwise, use ordinary grep.
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)
276 "The default grep program for `grep-command' and `grep-find-command'.")
278 ;; Use -e if grep supports it,
279 ;; because that avoids lossage if the pattern starts with `-'.
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)
286 (format "%s -n -e " grep-program)
287 (format "%s -n " grep-program))
288 "The default grep command for \\[grep].")
290 (defvar grep-find-use-xargs
291 (if (equal (call-process "find" nil nil nil
292 grep-null-device "-print0")
295 "Whether \\[grep-find] uses the `xargs' utility by default.
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'.
300 This variable's value takes effect when `compile.el' is loaded
301 by influencing the default value for the variable `grep-find-command'.")
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))
307 (format "find . -type f -print | xargs %s" grep-command))
308 (t (cons (format "find . -type f -exec %s {} /dev/null \\;"
310 (+ 22 (length grep-command)))))
311 "The default find command for \\[grep-find].")
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.")
319 (defvar compile-command "make -k "
320 "Last shell command used to do a compilation; default for next compilation.
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:
326 '(lambda () (or (file-exists-p \"makefile\") (file-exists-p \"Makefile\")
327 (progn (make-local-variable 'compile-command)
328 (setq compile-command
330 buffer-file-name))))))")
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.
337 The default value matches lines printed by the `-w' option of GNU Make.")
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.
346 The default value matches lines printed by the `-w' option of GNU Make.")
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.")
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.")
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)
364 (defun compilation-mode-font-lock-keywords ()
365 "Return expressions to highlight in Compilation mode."
368 ;; Compiler warning/error lines.
369 (mapcar #'(lambda (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)
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)))
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*'.
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.
390 Interactively, prompts for the command if `compilation-read-command' is
391 non-nil; otherwise uses `compile-command'. With prefix arg, always prompts.
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.
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."
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"))
410 ;;; run compile with the default command line
412 "Re-compile the program including the current buffer."
414 (save-some-buffers (not compilation-ask-about-save) nil)
415 (compile-internal compile-command "No more errors"))
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)
423 '("finished (matches found)\n" . "matched"))
425 '("finished with no matches found\n" . "no match"))
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.
436 This command uses a special history list for its arguments, so you can
437 easily repeat a grep command."
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)
447 "No more grep hits" "grep"
448 ;; Give it a simpler regexp to match.
449 nil grep-regexp-alist)))))
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.
458 This command uses a special history list for its arguments, so you can
459 easily repeat a find command."
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)))
466 (defun compile-internal (command error-message
467 &optional name-of-mode parser regexp-alist
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.
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.
481 Returns the compilation buffer created."
485 (setq name-of-mode "Compilation"))
488 (funcall (or name-function compilation-buffer-name-function
489 (function (lambda (mode)
490 (concat "*" (downcase mode) "*"))))
493 (let ((comp-proc (get-buffer-process (current-buffer))))
495 (if (or (not (eq (process-status comp-proc) 'run))
497 (format "A %s process is running; kill it? "
501 (interrupt-process comp-proc)
503 (delete-process comp-proc))
505 (error "Cannot have two processes in `%s' at once"
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)
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.
520 (setq buffer-read-only nil)
521 (buffer-disable-undo (current-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))
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)
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"
568 (cond ((numberp status)
569 (compilation-handle-exit 'exit status
573 exited abnormally with code %d\n"
576 (compilation-handle-exit 'signal status
577 (concat status "\n")))
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)))
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.
594 (let ((w (selected-window)))
597 (select-window window)
598 (enlarge-window (- compilation-window-height
600 (select-window w))))))
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)
613 "Keymap for `compilation-minor-mode'.")
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")))
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]
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]
635 (define-key map [menu-bar compilation-menu compilation-mode-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))
642 "Keymap for compilation log buffers.
643 `compilation-minor-mode-map' is a cdr of this.")
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].
652 Runs `compilation-mode-hook' with `run-hooks' (which see)."
654 (kill-all-local-variables)
655 (use-local-map compilation-mode-map)
656 (setq major-mode 'compilation-mode
657 mode-name "Compilation")
659 (set (make-local-variable 'font-lock-defaults)
660 '(compilation-mode-font-lock-keywords t))
661 (run-hooks 'compilation-mode-hook))
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)))
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)
679 (or (assq 'compilation-minor-mode minor-mode-alist)
680 (setq minor-mode-alist (cons '(compilation-minor-mode " Compilation")
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)))
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'."
694 (if (setq compilation-minor-mode (if (null arg)
695 (null compilation-minor-mode)
696 (> (prefix-numeric-value arg) 0)))
699 (run-hooks 'compilation-minor-mode-hook))))
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)))
710 ;; Record where we put the message, so we can ignore it
713 (insert ?\n mode-name " " (car status))
715 (insert " at " (substring (current-time-string) 0 19))
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))
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
728 (goto-line compile-auto-highlight)
730 (if compilation-finish-function
731 (funcall compilation-finish-function (current-buffer) msg))
732 (let ((functions compilation-finish-functions))
734 (funcall (car functions) (current-buffer) msg)
735 (setq functions (cdr functions))))))
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))
743 (if (null (buffer-name buffer))
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
751 ;; Write something in the compilation buffer
752 ;; and hack its mode line.
754 (compilation-handle-exit (process-status proc)
755 (process-exit-status proc)
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))
762 (setq compilation-in-progress (delq proc compilation-in-progress))
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))
770 (set-buffer (process-buffer proc))
771 (let ((buffer-read-only nil))
773 (goto-char (process-mark proc))
774 (insert-before-markers string)
775 (run-hooks 'compilation-filter-hook)
776 (set-marker (process-mark proc) (point)))))))
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))
783 (> (point) (car (car errors))))
784 (setq errors (cdr errors)))
787 (defsubst compilation-buffer-p (buffer)
790 (or compilation-minor-mode (eq major-mode 'compilation-mode))))
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]."
796 (or (compilation-buffer-p (current-buffer))
797 (error "Not in a compilation buffer."))
798 (setq compilation-last-buffer (current-buffer))
800 (let ((errors (compile-error-at-point)))
802 ;; Move to the error after the one containing point.
803 (goto-char (car (if (< n 0)
805 (e compilation-old-error-list))
806 ;; See how many cdrs away ERRORS is from the start.
807 (while (not (eq e errors))
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"))))))))
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]."
823 (compilation-next-error (- n)))
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))
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)
843 (defun compilation-next-file (n)
844 "Move point to the next error for a different file than the current one."
846 (or (compilation-buffer-p (current-buffer))
847 (error "Not in a compilation buffer."))
848 (setq compilation-last-buffer (current-buffer))
850 (let ((reversed (< n 0))
854 (setq errors (or (compile-error-at-point)
855 (error "Moved past last error")))
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)
862 ;; Ignore errors after point. (car ERRORS) will be the error
863 ;; containing point, (cadr ERRORS) the one before it.
865 (< (point) (car (car errors))))
866 (setq errors (cdr errors))))
869 (setq filedata (compilation-error-filedata (car errors)))
871 ;; Skip past the following errors for this file.
872 (while (equal filedata
873 (compilation-error-filedata
876 (error "%s the first erring file"
877 (compilation-error-filedata-file-name
879 (let ((compilation-error-list nil))
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
886 (setq errors (cdr errors)))
890 ;; Move to the following error.
891 (goto-char (car (car (or errors
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))))))))
899 (defun compilation-previous-file (n)
900 "Move point to the previous error for a different file than the current one."
902 (compilation-next-file (- n)))
905 (defun kill-compilation ()
906 "Kill the process made by the \\[compile] command."
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."))))
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)
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)
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.
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
945 ;; We only need enough new parsed errors to reach
946 ;; FIND-AT-LEAST errors past the current
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))
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))
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))))
971 (defun compile-mouse-goto-error (event)
974 (set-buffer (window-buffer (posn-window (event-end event))))
975 (goto-char (posn-point (event-end event)))
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'.
985 (compile-reinitialize-errors nil (point))
987 ;; Move to bol; the marker for the error on this line will point there.
990 ;; Move compilation-error-list to the elt of compilation-old-error-list
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.
1004 ;; other-window changed the selected buffer,
1005 ;; but we didn't want to do that.
1006 (set-buffer compilation-last-buffer)))
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."
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))
1022 ;; Move to bol; the marker for the error on this line will point there.
1025 ;; Move compilation-error-list to the elt of compilation-old-error-list
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)))
1032 ;; Move to another window, so that next-error's window changes
1033 ;; result in the desired setup.
1037 ;; other-window changed the selected buffer,
1038 ;; but we didn't want to do that.
1039 (set-buffer compilation-last-buffer)))
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.
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)))
1062 (eq (car buffers) (current-buffer)))))
1063 (setq buffers (cdr buffers)))
1066 (or (and other-buffer
1067 (compilation-buffer-p (current-buffer))
1068 ;; The current buffer is a compilation buffer.
1071 (message "This is the only compilation buffer."))
1073 (error "No compilation started!")))))))
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.
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.
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.
1091 Use \\[next-error] in a compilation output buffer to switch to
1092 processing errors from that compilation.
1094 See variables `compilation-parse-errors-function' and
1095 \`compilation-error-regexp-alist' for customization ideas."
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))
1104 ;;;###autoload (define-key ctl-x-map "`" 'next-error)
1106 (defun previous-error ()
1107 "Visit previous compilation error message and corresponding source code.
1108 This operates on the output from the \\[compile] command."
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."
1119 (defvar compilation-skip-to-next-location nil
1120 "*If non-nil, skip multiple error messages for the same source location.")
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.
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.
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.
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
1145 (set-buffer compilation-last-buffer)
1146 ;; compilation-error-list points to the "current" error.
1150 compilation-error-list)
1151 ;; Zero or negative arg; we need to move back in the list.
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))
1160 (error "Moved back past first error")
1161 (nthcdr (+ i n) compilation-old-error-list))))
1162 next-error (car next-errors))
1164 (if (null next-error)
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))
1176 (compilation-forget-errors))
1178 (throw 'no-next-error nil)
1179 (error (concat compilation-error-message
1180 (and (get-buffer-process (current-buffer))
1186 (setq compilation-error-list (cdr next-errors))
1187 (if (null (cdr next-error))
1188 ;; This error is boring. Go to the next.
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)))
1197 ;; We can't find this error's file.
1198 ;; Remove all errors in the same file.
1200 (setq next-errors compilation-old-error-list)
1202 (and (consp (cdr (car next-errors)))
1203 (equal (car (cdr (car next-errors)))
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.
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))))
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.
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.
1241 (re-search-backward "[\n\C-m]"
1244 (re-search-forward "[\n\C-m]"
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)))))
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))))
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.
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)))
1286 (goto-char (cdr next-error))))
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)
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)))
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)
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))
1324 (setq dirs (cdr dirs)))
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
1336 (format "Find this error in: (default %s) "
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)))))))
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)))
1364 (defun count-regexp-groupings (regexp)
1365 "Return the number of \\( ... \\) groupings in REGEXP (a string)."
1367 (len (length regexp))
1371 (setq c (aref regexp i)
1374 ;; Find the end of this [...].
1375 (while (and (< i len)
1376 (not (= (aref regexp i) ?\])))
1381 (setq c (aref regexp i)
1384 ;; We found the end of a grouping,
1385 ;; so bump our counter.
1386 (setq groupings (1+ groupings))))))))
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
1398 (compilation-num-errors-found 0))
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.
1407 ;; Move back so point is before the newline.
1408 ;; This matters because some error regexps use \n instead of ^
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
1417 (concat "\\(" (car elt) "\\)")))
1418 compilation-error-regexp-alist
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.
1426 leave-group (+ enter-group
1427 (count-regexp-groupings
1428 compilation-enter-directory-regexp)
1430 error-group (+ leave-group
1431 (count-regexp-groupings
1432 compilation-leave-directory-regexp)
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))
1443 (setq error-regexp-groups
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)))
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)))
1462 (while (and (not found-desired)
1463 ;; Instead of using re-search-forward,
1464 ;; we use this loop which tries only at each line.
1466 (while (and (not (eobp))
1467 (not (looking-at regexp)))
1471 ;; Move to the end of the match we just found.
1472 (goto-char (match-end 0))
1474 ;; Figure out which constituent regexp matched.
1475 (cond ((match-beginning enter-group)
1476 ;; The match was the enter-directory regexp.
1478 (file-name-as-directory
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.
1486 (compile-abbreviate-directory dir orig orig-expanded
1488 (setq compilation-directory-stack
1489 (cons dir compilation-directory-stack))
1490 (and (file-directory-p dir)
1491 (setq default-directory dir)))
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)))
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))
1508 (file-name-as-directory
1510 (buffer-substring beg
1511 (match-end (+ leave-group
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.
1517 (compile-abbreviate-directory dir orig orig-expanded
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))
1525 (setq default-directory stack))
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)))
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)
1542 (null (match-beginning (car (car alist)))))
1543 (setq alist (cdr alist)))
1545 (setq alist (car alist))
1546 (error "compilation-parse-errors: impossible regexp match!"))
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
1554 (match-beginning (nth 2 alist))
1555 (match-end (nth 2 alist)))))
1556 (column (and (nth 3 alist)
1557 (match-beginning (nth 3 alist))
1560 (match-beginning (nth 3 alist))
1561 (match-end (nth 3 alist)))))))
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)))
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
1579 (setq filename (command-line-normalize-file-name filename))
1580 (setq filename (cons filename (cons default-directory
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.
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
1599 (cdr (car compilation-error-list))))
1601 (setq compilation-error-list
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
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)))))
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)))
1631 (error "compilation-parse-errors: known groups didn't match!")))
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)))
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
1644 ;; We have searched the whole buffer.
1646 (setq compilation-error-list (nreverse compilation-error-list))
1647 (message "Parsing error messages...done"))
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))
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
1663 (if (boundp 'comint-file-name-prefix)
1664 (setq dir (concat comint-file-name-prefix dir)))
1666 (if (and (> (length dir) (length orig-expanded))
1667 (string= orig-expanded
1668 (substring dir 0 (length orig-expanded))))
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))))
1676 (concat (file-name-directory
1677 (directory-file-name orig))
1678 (substring dir (length parent-expanded)))))
1683 ;;; compile.el ends here