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