Mercurial > emacs
annotate lisp/progmodes/compile.el @ 795:c693d56ef36d
*** empty log message ***
author | Eric S. Raymond <esr@snark.thyrsus.com> |
---|---|
date | Thu, 16 Jul 1992 18:28:42 +0000 |
parents | cd00bdacc17b |
children | 33aaf4b71a1d |
rev | line source |
---|---|
727 | 1 ;;; compile.el --- run compiler as inferior of Emacs, parse error messages. |
712
16823e891d56
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
621
diff
changeset
|
2 |
795
c693d56ef36d
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
778
diff
changeset
|
3 ;; Maintainer: FSF |
c693d56ef36d
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
778
diff
changeset
|
4 ;; Last-Modified: 05 Jul 1992 |
c693d56ef36d
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
778
diff
changeset
|
5 |
418 | 6 ;;;!!! dup removal is broken. |
712
16823e891d56
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
621
diff
changeset
|
7 |
418 | 8 ;; Copyright (C) 1985-1991 Free Software Foundation, Inc. |
71 | 9 |
10 ;; This file is part of GNU Emacs. | |
11 | |
418 | 12 ;; GNU Emacs is distributed in the hope that it will be useful, |
13 ;; but WITHOUT ANY WARRANTY. No author or distributor | |
14 ;; accepts responsibility to anyone for the consequences of using it | |
15 ;; or for whether it serves any particular purpose or works at all, | |
16 ;; unless he says so in writing. Refer to the GNU Emacs General Public | |
17 ;; License for full details. | |
71 | 18 |
418 | 19 ;; Everyone is granted permission to copy, modify and redistribute |
20 ;; GNU Emacs, but only under the conditions described in the | |
21 ;; GNU Emacs General Public License. A copy of this license is | |
22 ;; supposed to have been given to you along with GNU Emacs so you | |
23 ;; can know your rights and responsibilities. It should be in a | |
24 ;; file named COPYING. Among other things, the copyright notice | |
25 ;; and this notice must be preserved on all copies. | |
71 | 26 |
795
c693d56ef36d
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
778
diff
changeset
|
27 ;;; Code: |
c693d56ef36d
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
778
diff
changeset
|
28 |
569 | 29 ;;;###autoload |
30 (defvar compilation-mode-hook nil | |
31 "*List of hook functions run by compilation-mode (see `run-hooks').") | |
32 | |
33 ;;;###autoload | |
418 | 34 (defconst compilation-window-height nil |
35 "*Number of lines in a compilation window. If nil, use Emacs default.") | |
36 | |
71 | 37 (defvar compilation-error-list nil |
38 "List of error message descriptors for visiting erring functions. | |
418 | 39 Each error descriptor is a cons (or nil). |
71 | 40 Its car is a marker pointing to an error message. |
418 | 41 If its cdr is a marker, it points to the text of the line the message is about. |
42 If its cdr is a cons, that cons's car is a cons (DIRECTORY . FILE), specifying | |
43 file the message is about, and its cdr is the number of the line the message | |
44 is about. Or its cdr may be nil if that error is not interesting. | |
45 | |
46 The value may be t instead of a list; this means that the buffer of | |
47 error messages should be reparsed the next time the list of errors is wanted.") | |
71 | 48 |
49 (defvar compilation-old-error-list nil | |
50 "Value of `compilation-error-list' after errors were parsed.") | |
51 | |
418 | 52 (defvar compilation-parse-errors-function 'compilation-parse-errors |
53 "Function to call (with no args) to parse error messages from a compilation. | |
54 It should read in the source files which have errors and set | |
55 `compilation-error-list' to a list with an element for each error message | |
56 found. See that variable for more info.") | |
71 | 57 |
474 | 58 ;;;###autoload |
418 | 59 (defvar compilation-buffer-name-function nil |
474 | 60 "*Function to call with one argument, the name of the major mode of the |
418 | 61 compilation buffer, to give the buffer a name. It should return a string. |
62 If nil, the name \"*compilation*\" is used for compilation buffers, | |
63 and the name \"*grep*\" is used for grep buffers. | |
474 | 64 \(Actually, the name (concat \"*\" (downcase major-mode) \"*\") is used.)") |
71 | 65 |
474 | 66 ;;;###autoload |
418 | 67 (defvar compilation-finish-function nil |
474 | 68 "*Function to call when a compilation process finishes. |
418 | 69 It is called with two arguments: the compilation buffer, and a string |
70 describing how the process finished.") | |
71 | |
72 (defvar compilation-last-buffer nil | |
73 "The buffer in which the last compilation was started, | |
74 or which was used by the last \\[next-error] or \\[compile-goto-error].") | |
71 | 75 |
740 | 76 (defvar compilation-in-progress nil |
77 "List of compilation processes now running.") | |
78 (or (assq 'compilation-in-progress minor-mode-alist) | |
79 (setq minor-mode-alist (cons '(compilation-in-progress " Compiling") | |
80 minor-mode-alist))) | |
81 | |
71 | 82 (defvar compilation-parsing-end nil |
418 | 83 "Position of end of buffer when last error messages were parsed.") |
71 | 84 |
418 | 85 (defvar compilation-error-message "No more errors" |
86 "Message to print when no more matches for `compilation-error-regexp-alist' | |
87 are found.") | |
71 | 88 |
418 | 89 (defvar compilation-error-regexp-alist |
90 '( | |
91 ;; 4.3BSD grep, cc, lint pass 1: | |
92 ;; /usr/src/foo/foo.c(8): warning: w may be used before set | |
93 ;; or GNU utilities | |
94 ;; foo.c:8: error message | |
95 ("^\\([^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2) | |
96 ;; 4.3BSD lint pass 2 | |
97 ;; strcmp: variable # of args. llib-lc(359) :: /usr/src/foo/foo.c(8) | |
727 | 98 ("[ \t:]+\\([^:( \t\n]+\\)[ \t]*[:(]*(+[ \t]*\\([0-9]+\\))[:) \t]*$" 1 2) |
418 | 99 ;; 4.3BSD lint pass 3 |
100 ;; bloofle defined( /users/wolfgang/foo.c(4) ), but never used | |
727 | 101 ;; This used to be |
102 ;; ("[ \t(]+\\([^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]+" 1 2) | |
103 ;; which is regexp Impressionism - it matches almost anything! | |
104 ("([ \t]*\\([^:( \t\n]+\\)[ \t]*[:(][ \t]*\\([0-9]+\\))" 1 2) | |
418 | 105 ;; Line 45 of "foo.c": bloofel undefined (who does this?) |
727 | 106 ("^[Ll]ine[ \t]+\\([0-9]+\\)[ \t]+of[ \t]+\"\\([^\"\n]+\\)\":" 2 1) |
418 | 107 ;; Apollo cc, 4.3BSD fc |
108 ;; "foo.f", line 3: Error: syntax error near end of statement | |
727 | 109 ("^\"\\([^\"\n]+\\)\", line \\([0-9]+\\):" 1 2) |
418 | 110 ;; HP-UX 7.0 fc |
111 ;; foo.f :16 some horrible error message | |
727 | 112 ("^\\([^ \t\n:]+\\)[ \t]*:\\([0-9]+\\)" 1 2) |
418 | 113 ;; IBM AIX PS/2 C version 1.1 |
114 ;; ****** Error number 140 in line 8 of file errors.c ****** | |
727 | 115 ("in line \\([0-9]+\\) of file \\([^ \n]+[^. \n]\\)\\.? " 2 1) |
418 | 116 ;; IBM AIX lint is too painful to do right this way. File name |
117 ;; prefixes entire sections rather than being on each line. | |
118 ) | |
119 "Alist (REGEXP FILE-IDX LINE-IDX) of regular expressions to match errors in | |
120 compilation. If REGEXP matches, the FILE-IDX'th subexpression gives the file | |
121 name, and the LINE-IDX'th subexpression gives the line number.") | |
71 | 122 |
569 | 123 ;;;###autoload |
418 | 124 (defvar compilation-search-path '(nil) |
569 | 125 "*List of directories to search for source files named in error messages. |
418 | 126 Elements should be directory names, not file names of directories. |
127 nil as an element means to try the default directory.") | |
71 | 128 |
129 (defvar compile-command "make -k " | |
130 "Last shell command used to do a compilation; default for next compilation. | |
131 | |
132 Sometimes it is useful for files to supply local values for this variable. | |
133 You might also use mode hooks to specify it in certain modes, like this: | |
134 | |
135 (setq c-mode-hook | |
136 '(lambda () (or (file-exists-p \"makefile\") (file-exists-p \"Makefile\") | |
137 (progn (make-local-variable 'compile-command) | |
138 (setq compile-command | |
139 (concat \"make -k \" | |
140 buffer-file-name))))))") | |
141 | |
418 | 142 ;;;###autoload |
143 (defvar grep-command "grep -n " | |
144 "Last shell command used to do a grep search; default for next search. | |
145 Typically \"grep -n\" or \"egrep -n\". | |
146 \(The \"-n\" option tells grep to output line numbers.)") | |
147 | |
148 (defconst compilation-enter-directory-regexp | |
727 | 149 ": Entering directory `\\(.*\\)'$" |
418 | 150 "Regular expression for a line in the compilation log that |
727 | 151 changes the current directory. This must contain one \\(, \\) pair |
418 | 152 around the directory name. |
153 | |
154 The default value matches lines printed by the `-w' option of GNU Make.") | |
71 | 155 |
418 | 156 (defconst compilation-leave-directory-regexp |
727 | 157 ": Leaving directory `\\(.*\\)'$" |
418 | 158 "Regular expression for a line in the compilation log that |
159 changes the current directory to a previous value. This may | |
727 | 160 contain one \\(, \\) pair around the name of the directory |
418 | 161 being moved from. If it does not, the last directory entered |
162 \(by a line matching `compilation-enter-directory-regexp'\) is assumed. | |
163 | |
164 The default value matches lines printed by the `-w' option of GNU Make.") | |
165 | |
166 (defvar compilation-directory-stack nil | |
167 "Stack of directories entered by lines matching | |
168 \`compilation-enter-directory-regexp' and not yet left by lines matching | |
169 \`compilation-leave-directory-regexp'. The head element is the directory | |
170 the compilation was started in.") | |
171 | |
172 ;;;###autoload | |
71 | 173 (defun compile (command) |
174 "Compile the program including the current buffer. Default: run `make'. | |
175 Runs COMMAND, a shell command, in a separate process asynchronously | |
176 with output going to the buffer `*compilation*'. | |
418 | 177 |
71 | 178 You can then use the command \\[next-error] to find the next error message |
179 and move to the source code that caused it. | |
180 | |
181 To run more than one compilation at once, start one and rename the | |
418 | 182 \`*compilation*' buffer to some other name with \\[rename-buffer]. |
183 Then start the next one. | |
184 | |
185 The name used for the buffer is actually whatever is returned by | |
186 the function in `compilation-buffer-name-function', so you can set that | |
187 to a function that generates a unique name." | |
71 | 188 (interactive (list (read-string "Compile command: " compile-command))) |
189 (setq compile-command command) | |
190 (save-some-buffers nil nil) | |
418 | 191 (compile-internal compile-command "No more errors")) |
71 | 192 |
418 | 193 ;;;###autoload |
71 | 194 (defun grep (command-args) |
195 "Run grep, with user-specified args, and collect output in a buffer. | |
196 While grep runs asynchronously, you can use the \\[next-error] command | |
418 | 197 to find the text that grep hits refer to. |
198 | |
199 The variable `grep-command' holds the last grep command run, | |
200 and is the default for future runs. The command should use the `-n' | |
201 flag, so that line numbers are displayed for each match. | |
202 What the user enters in response to the prompt for grep args is | |
203 appended to everything up to and including the `-n' in `grep-command'." | |
71 | 204 (interactive |
205 (list (read-string (concat "Run " | |
206 (substring grep-command 0 | |
207 (string-match "[\t ]+" grep-command)) | |
208 " (with args): ") | |
209 (progn | |
210 (string-match "-n[\t ]+" grep-command) | |
211 (substring grep-command (match-end 0)))))) | |
212 ;; why a redundant string-match? It might not be interactive ... | |
213 (setq grep-command (concat (substring grep-command 0 | |
214 (progn | |
215 (string-match "-n" grep-command) | |
216 (match-end 0))) | |
217 " " command-args)) | |
218 (compile-internal (concat grep-command " /dev/null") | |
219 "No more grep hits" "grep")) | |
220 | |
221 (defun compile-internal (command error-message | |
418 | 222 &optional name-of-mode parser regexp-alist |
223 name-function) | |
71 | 224 "Run compilation command COMMAND (low level interface). |
225 ERROR-MESSAGE is a string to print if the user asks to see another error | |
226 and there are no more errors. Third argument NAME-OF-MODE is the name | |
418 | 227 to display as the major mode in the compilation buffer. |
71 | 228 |
418 | 229 Fourth arg PARSER is the error parser function (nil means the default). Fifth |
230 arg REGEXP-ALIST is the error message regexp alist to use (nil means the | |
231 default). Sixth arg NAME-FUNCTION is a function called to name the buffer (nil | |
232 means the default). The defaults for these variables are the global values of | |
233 \`compilation-parse-errors-function', `compilation-error-regexp-alist', and | |
234 \`compilation-buffer-name-function', respectively." | |
235 (let (outbuf) | |
71 | 236 (save-excursion |
418 | 237 (or name-of-mode |
238 (setq name-of-mode "Compilation")) | |
239 (setq outbuf | |
240 (get-buffer-create | |
241 (funcall (or name-function compilation-buffer-name-function | |
242 (function (lambda (mode) | |
243 (concat "*" (downcase mode) "*")))) | |
244 name-of-mode))) | |
71 | 245 (set-buffer outbuf) |
418 | 246 (let ((comp-proc (get-buffer-process (current-buffer)))) |
247 (if comp-proc | |
248 (if (or (not (eq (process-status comp-proc) 'run)) | |
249 (yes-or-no-p | |
250 "A compilation process is running; kill it? ")) | |
251 (condition-case () | |
252 (progn | |
253 (interrupt-process comp-proc) | |
254 (sit-for 1) | |
255 (delete-process comp-proc)) | |
256 (error nil)) | |
257 (error "Cannot have two processes in `%s' at once" | |
258 (buffer-name)) | |
259 ))) | |
260 ;; In case the compilation buffer is current, make sure we get the global | |
261 ;; values of compilation-error-regexp-alist, etc. | |
262 (kill-all-local-variables)) | |
263 (let ((regexp-alist (or regexp-alist compilation-error-regexp-alist)) | |
264 (parser (or parser compilation-parse-errors-function)) | |
265 (thisdir default-directory) | |
266 outwin) | |
267 (save-excursion | |
268 ;; Clear out the compilation buffer and make it writable. | |
269 ;; Change its default-directory to the directory where the compilation | |
270 ;; will happen, and insert a `cd' command to indicate this. | |
271 (set-buffer outbuf) | |
272 (setq buffer-read-only nil) | |
273 (erase-buffer) | |
274 (setq default-directory thisdir) | |
275 (insert "cd " thisdir "\n" command "\n") | |
276 (set-buffer-modified-p nil)) | |
277 ;; If we're already in the compilation buffer, go to the end | |
278 ;; of the buffer, so point will track the compilation output. | |
279 (if (eq outbuf (current-buffer)) | |
280 (goto-char (point-max))) | |
281 ;; Pop up the compilation buffer. | |
282 (setq outwin (display-buffer outbuf)) | |
283 (set-buffer outbuf) | |
71 | 284 (compilation-mode) |
712
16823e891d56
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
621
diff
changeset
|
285 (buffer-disable-undo (current-buffer)) |
732 | 286 (setq buffer-read-only t) |
418 | 287 (set (make-local-variable 'compilation-parse-errors-function) parser) |
288 (set (make-local-variable 'compilation-error-message) error-message) | |
289 (set (make-local-variable 'compilation-error-regexp-alist) regexp-alist) | |
290 (setq default-directory thisdir | |
291 compilation-directory-stack (list default-directory)) | |
71 | 292 (set-window-start outwin (point-min)) |
418 | 293 (setq mode-name name-of-mode) |
71 | 294 (or (eq outwin (selected-window)) |
418 | 295 (set-window-point outwin (point-min))) |
296 (and compilation-window-height | |
778 | 297 (= (window-width outwin) (frame-width)) |
418 | 298 (let ((w (selected-window))) |
299 (unwind-protect | |
300 (progn | |
301 (select-window outwin) | |
302 (enlarge-window (- compilation-window-height | |
303 (window-height)))) | |
304 (select-window w)))) | |
305 ;; Start the compilation. | |
740 | 306 (let ((proc (start-process-shell-command (downcase mode-name) |
307 outbuf | |
308 command))) | |
309 (set-process-sentinel proc 'compilation-sentinel) | |
310 (setq compilation-in-progress (cons proc compilation-in-progress)))) | |
418 | 311 ;; Make it so the next C-x ` will use this buffer. |
312 (setq compilation-last-buffer outbuf))) | |
71 | 313 |
314 (defvar compilation-mode-map | |
315 (let ((map (make-sparse-keymap))) | |
316 (define-key map "\C-c\C-c" 'compile-goto-error) | |
418 | 317 (define-key map "\C-c\C-k" 'kill-compilation) |
71 | 318 map) |
319 "Keymap for compilation log buffers.") | |
320 | |
321 (defun compilation-mode () | |
322 "Major mode for compilation log buffers. | |
323 \\<compilation-mode-map>To visit the source for a line-numbered error, | |
418 | 324 move point to the error message line and type \\[compile-goto-error]. |
569 | 325 To kill the compilation, type \\[kill-compilation]. |
326 | |
327 Runs `compilation-mode-hook' with `run-hooks' (which see)." | |
71 | 328 (interactive) |
329 (fundamental-mode) | |
330 (use-local-map compilation-mode-map) | |
331 (setq major-mode 'compilation-mode) | |
332 (setq mode-name "Compilation") | |
418 | 333 ;; Make buffer's mode line show process state |
334 (setq mode-line-process '(": %s")) | |
335 (set (make-local-variable 'compilation-error-list) nil) | |
336 (set (make-local-variable 'compilation-old-error-list) nil) | |
337 (set (make-local-variable 'compilation-parsing-end) 1) | |
338 (set (make-local-variable 'compilation-directory-stack) nil) | |
569 | 339 (setq compilation-last-buffer (current-buffer)) |
340 (run-hooks 'compilation-mode-hook)) | |
71 | 341 |
342 ;; Called when compilation process changes state. | |
343 (defun compilation-sentinel (proc msg) | |
418 | 344 "Sentinel for compilation buffers." |
345 (let ((buffer (process-buffer proc))) | |
740 | 346 (if (memq (process-status proc) '(signal exit)) |
347 (progn | |
348 (if (null (buffer-name buffer)) | |
349 ;; buffer killed | |
350 (set-process-buffer proc nil) | |
351 (let ((obuf (current-buffer)) | |
352 omax opoint) | |
353 ;; save-excursion isn't the right thing if | |
354 ;; process-buffer is current-buffer | |
355 (unwind-protect | |
356 (progn | |
357 ;; Write something in the compilation buffer | |
358 ;; and hack its mode line. | |
359 (set-buffer buffer) | |
360 (setq buffer-read-only nil) | |
361 (setq omax (point-max) | |
362 opoint (point)) | |
363 (goto-char omax) | |
364 ;; Record where we put the message, so we can ignore it | |
365 ;; later on. | |
366 (insert ?\n mode-name " " msg) | |
367 (forward-char -1) | |
368 (insert " at " (substring (current-time-string) 0 19)) | |
369 (forward-char 1) | |
370 (setq mode-line-process | |
371 (concat ": " | |
372 (symbol-name (process-status proc)))) | |
373 ;; Since the buffer and mode line will show that the | |
374 ;; process is dead, we can delete it now. Otherwise it | |
375 ;; will stay around until M-x list-processes. | |
376 (delete-process proc) | |
377 ;; Force mode line redisplay soon. | |
378 (set-buffer-modified-p (buffer-modified-p)) | |
379 (setq buffer-read-only t) ;I think is this wrong --roland | |
380 (if (and opoint (< opoint omax)) | |
381 (goto-char opoint))) | |
382 (set-buffer obuf)) | |
383 (if compilation-finish-function | |
384 (funcall compilation-finish-function buffer msg)) | |
385 )) | |
386 (setq compilation-in-progress (delq proc compilation-in-progress)) | |
387 )))) | |
71 | 388 |
389 (defun kill-compilation () | |
390 "Kill the process made by the \\[compile] command." | |
391 (interactive) | |
418 | 392 (let ((buffer (compilation-find-buffer))) |
71 | 393 (if (get-buffer-process buffer) |
418 | 394 (interrupt-process (get-buffer-process buffer)) |
395 (error "The compilation process is not running.")))) | |
71 | 396 |
418 | 397 |
398 ;; Parse any new errors in the compilation buffer, | |
399 ;; or reparse from the beginning if the user has asked for that. | |
71 | 400 (defun compile-reinitialize-errors (argp) |
418 | 401 (save-excursion |
402 (set-buffer compilation-last-buffer) | |
403 ;; If we are out of errors, or if user says "reparse", | |
404 ;; discard the info we have, to force reparsing. | |
405 (if (or (eq compilation-error-list t) | |
406 (consp argp)) | |
407 (progn (compilation-forget-errors) | |
408 (setq compilation-parsing-end 1))) | |
409 (if compilation-error-list | |
410 ;; Since compilation-error-list is non-nil, it points to a specific | |
411 ;; error the user wanted. So don't move it around. | |
412 nil | |
413 (switch-to-buffer compilation-last-buffer) | |
71 | 414 (set-buffer-modified-p nil) |
415 (let ((at-start (= compilation-parsing-end 1))) | |
418 | 416 (funcall compilation-parse-errors-function) |
71 | 417 ;; Remember the entire list for compilation-forget-errors. |
418 ;; If this is an incremental parse, append to previous list. | |
419 (if at-start | |
420 (setq compilation-old-error-list compilation-error-list) | |
421 (setq compilation-old-error-list | |
422 (nconc compilation-old-error-list compilation-error-list))))))) | |
423 | |
424 (defun compile-goto-error (&optional argp) | |
425 "Visit the source for the error message point is on. | |
426 Use this command in a compilation log buffer. | |
427 C-u as a prefix arg means to reparse the buffer's error messages first; | |
428 other kinds of prefix arguments are ignored." | |
429 (interactive "P") | |
418 | 430 (or (compilation-buffer-p (current-buffer)) |
431 (error "Not in a compilation buffer.")) | |
432 (setq compilation-last-buffer (current-buffer)) | |
71 | 433 (compile-reinitialize-errors argp) |
434 (save-excursion | |
435 (beginning-of-line) | |
418 | 436 ;; Move compilation-error-list to the elt of |
437 ;; compilation-old-error-list whose car is the error we want. | |
71 | 438 (setq compilation-error-list |
418 | 439 (memq (let (elt) |
440 (while (not (or (setq elt (assoc (point-marker) | |
441 compilation-old-error-list)) | |
442 (eobp))) | |
443 ;; This line doesn't contain an error. | |
444 ;; Move forward a line and look again. | |
445 (forward-line 1)) | |
446 elt) | |
71 | 447 compilation-old-error-list))) |
448 ;; Move to another window, so that next-error's window changes | |
449 ;; result in the desired setup. | |
450 (or (one-window-p) | |
451 (other-window -1)) | |
452 (next-error 1)) | |
453 | |
418 | 454 (defun compilation-buffer-p (buffer) |
455 (assq 'compilation-error-list (buffer-local-variables buffer))) | |
456 | |
457 ;; Return a compilation buffer. | |
458 ;; If the current buffer is a compilation buffer, return it. | |
459 ;; If compilation-last-buffer is set to a live buffer, use that. | |
460 ;; Otherwise, look for a compilation buffer and signal an error | |
461 ;; if there are none. | |
621 | 462 (defun compilation-find-buffer (&optional other-buffer) |
463 (if (and (not other-buffer) | |
464 (compilation-buffer-p (current-buffer))) | |
418 | 465 ;; The current buffer is a compilation buffer. |
466 (current-buffer) | |
621 | 467 (if (and compilation-last-buffer (buffer-name compilation-last-buffer) |
468 (or (not other-buffer) (not (eq compilation-last-buffer | |
469 (current-buffer))))) | |
418 | 470 compilation-last-buffer |
471 (let ((buffers (buffer-list))) | |
621 | 472 (while (and buffers (or (not (compilation-buffer-p (car buffers))) |
473 (and other-buffer | |
474 (eq (car buffers) (current-buffer))))) | |
418 | 475 (setq buffers (cdr buffers))) |
476 (if buffers | |
477 (car buffers) | |
621 | 478 (or (and other-buffer |
479 (compilation-buffer-p (current-buffer)) | |
480 ;; The current buffer is a compilation buffer. | |
481 (progn | |
482 (if other-buffer | |
483 (message "This is the only compilation buffer.")) | |
484 (current-buffer))) | |
485 (error "No compilation started!"))))))) | |
418 | 486 |
487 ;;;###autoload | |
71 | 488 (defun next-error (&optional argp) |
489 "Visit next compilation error message and corresponding source code. | |
490 This operates on the output from the \\[compile] command. | |
491 If all preparsed error messages have been processed, | |
492 the error message buffer is checked for new ones. | |
493 | |
494 A prefix arg specifies how many error messages to move; | |
495 negative means move back to previous error messages. | |
496 Just C-u as a prefix means reparse the error message buffer | |
497 and start at the first error. | |
498 | |
499 \\[next-error] normally applies to the most recent compilation started, | |
500 but as long as you are in the middle of parsing errors from one compilation | |
501 output buffer, you stay with that compilation output buffer. | |
502 | |
503 Use \\[next-error] in a compilation output buffer to switch to | |
504 processing errors from that compilation. | |
505 | |
418 | 506 See variables `compilation-parse-errors-function' and |
507 \`compilation-error-regexp-alist' for customization ideas." | |
71 | 508 (interactive "P") |
418 | 509 (setq compilation-last-buffer (compilation-find-buffer)) |
71 | 510 (compile-reinitialize-errors argp) |
418 | 511 ;; Make ARGP nil if the prefix arg was just C-u, |
512 ;; since that means to reparse the errors, which the | |
513 ;; compile-reinitialize-errors call just did. | |
514 ;; Now we are only interested in a numeric prefix arg. | |
71 | 515 (if (consp argp) |
516 (setq argp nil)) | |
418 | 517 (let (next-errors next-error) |
518 (save-excursion | |
519 (set-buffer compilation-last-buffer) | |
520 (setq next-errors (nthcdr (+ (- (length compilation-old-error-list) | |
521 (length compilation-error-list) | |
522 1) | |
523 (prefix-numeric-value argp)) | |
524 compilation-old-error-list) | |
525 next-error (car next-errors)) | |
526 (while | |
71 | 527 (progn |
418 | 528 (if (null next-error) |
529 (progn | |
530 (if argp (if (> (prefix-numeric-value argp) 0) | |
531 (error "Moved past last error") | |
532 (error "Moved back past first error"))) | |
533 (compilation-forget-errors) | |
534 (error (concat compilation-error-message | |
535 (and (get-buffer-process (current-buffer)) | |
536 (eq (process-status | |
537 (get-buffer-process | |
538 (current-buffer))) | |
539 'run) | |
540 " yet")))) | |
541 (setq compilation-error-list (cdr next-errors)) | |
542 (if (null (cdr next-error)) | |
543 ;; This error is boring. Go to the next. | |
544 t | |
545 (or (markerp (cdr next-error)) | |
546 ;; This error has a filename/lineno pair. | |
547 ;; Find the file and turn it into a marker. | |
548 (let* ((fileinfo (car (cdr next-error))) | |
549 (buffer (compilation-find-file (cdr fileinfo) | |
550 (car fileinfo) | |
551 (car next-error)))) | |
552 (if (null buffer) | |
553 ;; We can't find this error's file. | |
554 ;; Remove all errors in the same file. | |
555 (progn | |
556 (setq next-errors compilation-old-error-list) | |
557 (while next-errors | |
558 (and (consp (cdr (car next-errors))) | |
559 (equal (car (cdr (car next-errors))) | |
560 fileinfo) | |
561 (progn | |
562 (set-marker (car (car next-errors)) nil) | |
563 (setcdr (car next-errors) nil))) | |
564 (setq next-errors (cdr next-errors))) | |
565 ;; Look for the next error. | |
566 t) | |
567 ;; We found the file. Get a marker for this error. | |
568 (set-buffer buffer) | |
569 (save-excursion | |
570 (save-restriction | |
571 (widen) | |
572 (let ((errors compilation-old-error-list) | |
573 (last-line (cdr (cdr next-error)))) | |
574 (goto-line last-line) | |
575 (beginning-of-line) | |
576 (setcdr next-error (point-marker)) | |
577 ;; Make all the other error messages referring | |
578 ;; to the same file have markers into the buffer. | |
579 (while errors | |
580 (and (consp (cdr (car errors))) | |
581 (equal (car (cdr (car errors))) fileinfo) | |
582 (let ((this (cdr (cdr (car errors)))) | |
583 (lines (- (cdr (cdr (car errors))) | |
584 last-line))) | |
585 (if (eq selective-display t) | |
586 (if (< lines 0) | |
587 (re-search-backward "[\n\C-m]" | |
588 nil 'end | |
589 (- lines)) | |
590 (re-search-forward "[\n\C-m]" | |
591 nil 'end | |
592 lines)) | |
593 (forward-line lines)) | |
594 (setq last-line this) | |
595 (setcdr (car errors) (point-marker)))) | |
596 (setq errors (cdr errors))))))))) | |
597 ;; If we didn't get a marker for this error, | |
598 ;; go on to the next one. | |
599 (not (markerp (cdr next-error)))))) | |
600 (setq next-errors compilation-error-list | |
601 next-error (car next-errors)))) | |
602 | |
603 ;; Skip over multiple error messages for the same source location, | |
604 ;; so the next C-x ` won't go to an error in the same place. | |
605 (while (and compilation-error-list | |
606 (equal (cdr (car compilation-error-list)) (cdr next-error))) | |
607 (setq compilation-error-list (cdr compilation-error-list))) | |
608 | |
609 ;; We now have a marker for the position of the error. | |
610 (switch-to-buffer (marker-buffer (cdr next-error))) | |
611 (goto-char (cdr next-error)) | |
612 ;; If narrowing got in the way of | |
613 ;; going to the right place, widen. | |
614 (or (= (point) (marker-position (cdr next-error))) | |
615 (progn | |
616 (widen) | |
617 (goto-char (cdr next-error)))) | |
618 | |
71 | 619 ;; Show compilation buffer in other window, scrolled to this error. |
620 (let* ((pop-up-windows t) | |
621 (w (display-buffer (marker-buffer (car next-error))))) | |
622 (set-window-point w (car next-error)) | |
418 | 623 (set-window-start w (car next-error))))) |
624 | |
625 ;;;###autoload | |
626 (define-key ctl-x-map "`" 'next-error) | |
71 | 627 |
418 | 628 ;; Find a buffer for file FILENAME. |
629 ;; Search the directories in compilation-search-path. | |
630 ;; A nil in compilation-search-path means to try the | |
631 ;; current directory, which is passed in DIR. | |
632 ;; If FILENAME is not found at all, ask the user where to find it. | |
633 ;; Pop up the buffer containing MARKER and scroll to MARKER if we ask the user. | |
634 (defun compilation-find-file (filename dir marker) | |
635 (let ((dirs compilation-search-path) | |
636 result name) | |
637 (while (and dirs (null result)) | |
638 (setq name (expand-file-name filename (or (car dirs) dir)) | |
639 result (and (file-exists-p name) | |
640 (find-file-noselect name)) | |
641 dirs (cdr dirs))) | |
642 (or result | |
643 ;; The file doesn't exist. | |
644 ;; Ask the user where to find it. | |
645 ;; If he hits C-g, then the next time he does | |
646 ;; next-error, he'll skip past it. | |
647 (progn | |
648 (let* ((pop-up-windows t) | |
649 (w (display-buffer (marker-buffer marker)))) | |
650 (set-window-point w marker) | |
651 (set-window-start w marker)) | |
652 (setq name | |
653 (expand-file-name | |
654 (read-file-name | |
655 (format "Find this error in: (default %s) " | |
656 filename) dir filename t))) | |
657 (if (file-directory-p name) | |
658 (setq name (concat (file-name-as-directory name) filename))) | |
659 (if (file-exists-p name) | |
660 (find-file-noselect name)))))) | |
661 | |
662 ;; Set compilation-error-list to nil, and unchain the markers that point to the | |
663 ;; error messages and their text, so that they no longer slow down gap motion. | |
664 ;; This would happen anyway at the next garbage collection, but it is better to | |
665 ;; do it the right away. | |
71 | 666 (defun compilation-forget-errors () |
667 (while compilation-old-error-list | |
668 (let ((next-error (car compilation-old-error-list))) | |
669 (set-marker (car next-error) nil) | |
418 | 670 (if (markerp (cdr next-error)) |
671 (set-marker (cdr next-error) nil))) | |
71 | 672 (setq compilation-old-error-list (cdr compilation-old-error-list))) |
418 | 673 (setq compilation-error-list nil) |
674 (while (cdr compilation-directory-stack) | |
675 (setq compilation-directory-stack (cdr compilation-directory-stack)))) | |
676 | |
677 | |
678 (defun count-regexp-groupings (regexp) | |
679 "Return the number of \\( ... \\) groupings in REGEXP (a string)." | |
680 (let ((groupings 0) | |
681 (len (length regexp)) | |
682 (i 0) | |
683 c) | |
684 (while (< i len) | |
685 (setq c (aref regexp i) | |
686 i (1+ i)) | |
687 (cond ((= c ?\[) | |
688 ;; Find the end of this [...]. | |
689 (while (and (< i len) | |
690 (not (= (aref regexp i) ?\]))) | |
691 (setq i (1+ i)))) | |
692 ((= c ?\\) | |
693 (if (< i len) | |
694 (progn | |
695 (setq c (aref regexp i) | |
696 i (1+ i)) | |
697 (if (= c ?\)) | |
698 ;; We found the end of a grouping, | |
699 ;; so bump our counter. | |
700 (setq groupings (1+ groupings)))))))) | |
701 groupings)) | |
71 | 702 |
703 (defun compilation-parse-errors () | |
704 "Parse the current buffer as grep, cc or lint error messages. | |
418 | 705 See variable `compilation-parse-errors-function' for the interface it uses." |
71 | 706 (setq compilation-error-list nil) |
707 (message "Parsing error messages...") | |
708 (let (text-buffer | |
418 | 709 regexp enter-group leave-group error-group |
710 alist subexpr error-regexp-groups) | |
711 | |
71 | 712 ;; Don't reparse messages already seen at last parse. |
713 (goto-char compilation-parsing-end) | |
714 ;; Don't parse the first two lines as error messages. | |
715 ;; This matters for grep. | |
716 (if (bobp) | |
717 (forward-line 2)) | |
418 | 718 |
719 ;; Compile all the regexps we want to search for into one. | |
720 (setq regexp (concat "\\(" compilation-enter-directory-regexp "\\)\\|" | |
721 "\\(" compilation-leave-directory-regexp "\\)\\|" | |
722 "\\(" (mapconcat (function | |
723 (lambda (elt) | |
724 (concat "\\(" (car elt) "\\)"))) | |
725 compilation-error-regexp-alist | |
726 "\\|") "\\)")) | |
727 | |
728 ;; Find out how many \(...\) groupings are in each of the regexps, and set | |
729 ;; *-GROUP to the grouping containing each constituent regexp (whose | |
730 ;; subgroups will come immediately thereafter) of the big regexp we have | |
731 ;; just constructed. | |
732 (setq enter-group 1 | |
733 leave-group (+ enter-group | |
734 (count-regexp-groupings | |
735 compilation-enter-directory-regexp) | |
736 1) | |
737 error-group (+ leave-group | |
738 (count-regexp-groupings | |
739 compilation-leave-directory-regexp) | |
740 1)) | |
741 | |
742 ;; Compile an alist (IDX FILE LINE), where IDX is the number of the | |
743 ;; subexpression for an entire error-regexp, and FILE and LINE are the | |
744 ;; numbers for the subexpressions giving the file name and line number. | |
745 (setq alist compilation-error-regexp-alist | |
746 subexpr (1+ error-group)) | |
747 (while alist | |
748 (setq error-regexp-groups (cons (list subexpr | |
749 (+ subexpr (nth 1 (car alist))) | |
750 (+ subexpr (nth 2 (car alist)))) | |
751 error-regexp-groups)) | |
752 (setq subexpr (+ subexpr 1 (count-regexp-groupings (car (car alist))))) | |
753 (setq alist (cdr alist))) | |
754 | |
755 (while (re-search-forward regexp nil t) | |
756 ;; Figure out which constituent regexp matched. | |
757 (cond ((match-beginning enter-group) | |
758 ;; The match was the enter-directory regexp. | |
759 (let ((dir | |
760 (file-name-as-directory | |
761 (expand-file-name | |
762 (buffer-substring (match-beginning (+ enter-group 1)) | |
763 (match-end (+ enter-group 1))))))) | |
764 (setq compilation-directory-stack | |
765 (cons dir compilation-directory-stack)) | |
766 (and (file-directory-p dir) | |
767 (setq default-directory dir)))) | |
768 | |
769 ((match-beginning leave-group) | |
770 ;; The match was the leave-directory regexp. | |
771 (let ((beg (match-beginning (+ leave-group 1))) | |
772 (stack compilation-directory-stack)) | |
773 (if beg | |
774 (let ((dir | |
775 (file-name-as-directory | |
776 (expand-file-name | |
777 (buffer-substring beg | |
778 (match-end (+ leave-group | |
779 1))))))) | |
780 (while (and stack | |
781 (not (string-equal (car stack) dir))) | |
782 (setq stack (cdr stack))))) | |
783 (setq compilation-directory-stack (cdr stack)) | |
784 (setq stack (car compilation-directory-stack)) | |
785 (if stack | |
786 (setq default-directory stack)) | |
787 )) | |
788 | |
789 ((match-beginning error-group) | |
790 ;; The match was the composite error regexp. | |
791 ;; Find out which individual regexp matched. | |
792 (setq alist error-regexp-groups) | |
793 (while (and alist | |
794 (null (match-beginning (car (car alist))))) | |
795 (setq alist (cdr alist))) | |
796 (if alist | |
797 (setq alist (car alist)) | |
798 (error "Impossible regexp match!")) | |
799 | |
800 ;; Extract the file name and line number from the error message. | |
801 (let ((filename | |
802 (cons default-directory | |
803 (buffer-substring (match-beginning (nth 1 alist)) | |
804 (match-end (nth 1 alist))))) | |
805 (linenum (save-restriction | |
806 (narrow-to-region | |
807 (match-beginning (nth 2 alist)) | |
808 (match-end (nth 2 alist))) | |
809 (goto-char (point-min)) | |
810 (if (looking-at "[0-9]") | |
811 (read (current-buffer)))))) | |
812 ;; Locate the erring file and line. | |
813 ;; Cons a new elt onto compilation-error-list, | |
814 ;; giving a marker for the current compilation buffer | |
815 ;; location, and the file and line number of the error. | |
816 (save-excursion | |
817 (beginning-of-line 1) | |
818 (setq compilation-error-list | |
819 (cons (cons (point-marker) | |
820 (cons filename linenum)) | |
821 compilation-error-list))))) | |
822 (t | |
823 (error "Impossible regexp match!")))) | |
71 | 824 (setq compilation-parsing-end (point-max))) |
825 (message "Parsing error messages...done") | |
826 (setq compilation-error-list (nreverse compilation-error-list))) | |
827 | |
828 (define-key ctl-x-map "`" 'next-error) | |
621 | 829 |
830 (provide 'compile) | |
712
16823e891d56
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
621
diff
changeset
|
831 |
16823e891d56
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
621
diff
changeset
|
832 ;;; compile.el ends here |