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