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