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