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