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