home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / emacs-19.28-src.tgz / tar.out / fsf / emacs / lisp / compile.el < prev    next >
Lisp/Scheme  |  1996-09-28  |  56KB  |  1,417 lines

  1. ;;; compile.el --- run compiler as inferior of Emacs, parse error messages.
  2.  
  3. ;; Copyright (C) 1985, 86, 87, 93, 94 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Roland McGrath <roland@prep.ai.mit.edu>
  6. ;; Maintainer: FSF
  7. ;; Keywords: tools, processes
  8.  
  9. ;; This file is part of GNU Emacs.
  10.  
  11. ;; GNU Emacs is free software; you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. ;; GNU General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  23. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25. ;;; Commentary:
  26.  
  27. ;; This package provides the compile and grep facilities documented in
  28. ;; the Emacs user's manual.
  29.  
  30. ;;; Code:
  31.  
  32. ;;;###autoload
  33. (defvar compilation-mode-hook nil
  34.   "*List of hook functions run by `compilation-mode' (see `run-hooks').")
  35.  
  36. ;;;###autoload
  37. (defvar compilation-window-height nil
  38.   "*Number of lines in a compilation window.  If nil, use Emacs default.")
  39.  
  40. (defvar compilation-error-list nil
  41.   "List of error message descriptors for visiting erring functions.
  42. Each error descriptor is a cons (or nil).  Its car is a marker pointing to
  43. an error message.  If its cdr is a marker, it points to the text of the
  44. line the message is about.  If its cdr is a cons, it is a list
  45. \(\(DIRECTORY . FILE\) LINE [COLUMN]\).  Or its cdr may be nil if that
  46. error is not interesting.
  47.  
  48. The value may be t instead of a list; this means that the buffer of
  49. error messages should be reparsed the next time the list of errors is wanted.
  50.  
  51. Some other commands (like `diff') use this list to control the error
  52. message tracking facilites; if you change its structure, you should make
  53. sure you also change those packages.  Perhaps it is better not to change
  54. it at all.")
  55.  
  56. (defvar compilation-old-error-list nil
  57.   "Value of `compilation-error-list' after errors were parsed.")
  58.  
  59. (defvar compilation-parse-errors-function 'compilation-parse-errors 
  60.   "Function to call to parse error messages from a compilation.
  61. It takes args LIMIT-SEARCH and FIND-AT-LEAST.
  62. If LIMIT-SEARCH is non-nil, don't bother parsing past that location.
  63. If FIND-AT-LEAST is non-nil, don't bother parsing after finding that 
  64. many new errors.
  65. It should read in the source files which have errors and set
  66. `compilation-error-list' to a list with an element for each error message
  67. found.  See that variable for more info.")
  68.  
  69. ;;;###autoload
  70. (defvar compilation-buffer-name-function nil
  71.   "Function to compute the name of a compilation buffer.
  72. The function receives one argument, the name of the major mode of the
  73. compilation buffer.  It should return a string.
  74. nil means compute the name with `(concat \"*\" (downcase major-mode) \"*\")'.")
  75.  
  76. ;;;###autoload
  77. (defvar compilation-finish-function nil
  78.   "*Function to call when a compilation process finishes.
  79. It is called with two arguments: the compilation buffer, and a string
  80. describing how the process finished.")
  81.  
  82. (defvar compilation-last-buffer nil
  83.   "The most recent compilation buffer.
  84. A buffer becomes most recent when its compilation is started
  85. or when it is used with \\[next-error] or \\[compile-goto-error].")
  86.  
  87. (defvar compilation-in-progress nil
  88.   "List of compilation processes now running.")
  89. (or (assq 'compilation-in-progress minor-mode-alist)
  90.     (setq minor-mode-alist (cons '(compilation-in-progress " Compiling")
  91.                  minor-mode-alist)))
  92.  
  93. (defvar compilation-parsing-end nil
  94.   "Position of end of buffer when last error messages were parsed.")
  95.  
  96. (defvar compilation-error-message "No more errors"
  97.   "Message to print when no more matches are found.")
  98.  
  99. (defvar compilation-num-errors-found)
  100.  
  101. (defvar compilation-error-regexp-alist
  102.   '(
  103.     ;; NOTE!  See also grep-regexp-alist, below.
  104.  
  105.     ;; 4.3BSD grep, cc, lint pass 1:
  106.     ;;     /usr/src/foo/foo.c(8): warning: w may be used before set
  107.     ;; or GNU utilities:
  108.     ;;     foo.c:8: error message
  109.     ;; or HP-UX 7.0 fc:
  110.     ;;     foo.f          :16    some horrible error message
  111.     ;;
  112.     ;; We refuse to match  file:number:number
  113.     ;; because we want to leave that for another case (see below, GNAT).
  114.     ;; 
  115.     ;; We'll insist that the number be followed by a colon or closing
  116.     ;; paren, because otherwise this matches just about anything
  117.     ;; containing a number with spaces around it.
  118.     ("\n\\([^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|:[^0-9\n]\\)" 1 2)
  119.  
  120.     ;; GNU error message with a program name in it.
  121.     ;;  compilername:file:linenum: error message
  122.     ("\n\\([^:( \t\n]+\\):\\([^:( \t\n]+\\):[ \t]*\\([0-9]+\\)\\(:[^0-9\n]\\)"
  123.      2 3)
  124.  
  125.     ;; Borland C++:
  126.     ;;  Error ping.c 15: Unable to open include file 'sys/types.h'
  127.     ;;  Warning ping.c 68: Call to function 'func' with no prototype
  128.     ("\n\\(Error\\|Warning\\) \\([^:( \t\n]+\\)\
  129.  \\([0-9]+\\)\\([) \t]\\|:[^0-9\n]\\)" 2 3)
  130.  
  131.     ;; 4.3BSD lint pass 2
  132.     ;;     strcmp: variable # of args. llib-lc(359)  ::  /usr/src/foo/foo.c(8)
  133.     ("[ \t:]\\([^:( \t\n]+\\)[:(](+[ \t]*\\([0-9]+\\))[:) \t]*$" 1 2)
  134.  
  135.     ;; 4.3BSD lint pass 3
  136.     ;;     bloofle defined( /users/wolfgang/foo.c(4) ), but never used
  137.     ;; This used to be
  138.     ;; ("[ \t(]+\\([^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]+" 1 2)
  139.     ;; which is regexp Impressionism - it matches almost anything!
  140.     ("([ \t]*\\([^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\))" 1 2)
  141.  
  142.     ;; Ultrix 3.0 f77:
  143.     ;;  fort: Severe: addstf.f, line 82: Missing operator or delimiter symbol
  144.     ("\nfort: [^:\n]*: \\([^ \n]*\\), line \\([0-9]+\\):" 1 2)
  145.     ;;  Error on line 3 of t.f: Execution error unclassifiable statement    
  146.     ;; Unknown who does this:
  147.     ;;  Line 45 of "foo.c": bloofel undefined
  148.     ;; Absoft FORTRAN 77 Compiler 3.1.3
  149.     ;;  error on line 19 of fplot.f: spelling error?
  150.     ;;  warning on line 17 of fplot.f: data type is undefined for variable d
  151.     ("\\(\n\\|on \\)[Ll]ine[ \t]+\\([0-9]+\\)[ \t]+\
  152. of[ \t]+\"?\\([^\":\n]+\\)\"?:" 3 2)
  153.  
  154.     ;; Apollo cc, 4.3BSD fc:
  155.     ;;    "foo.f", line 3: Error: syntax error near end of statement
  156.     ;; IBM RS6000:
  157.     ;;  "vvouch.c", line 19.5: 1506-046 (S) Syntax error.
  158.     ;; Unknown compiler:
  159.     ;;  File "foobar.ml", lines 5-8, characters 20-155: blah blah
  160.     ;; Microtec mcc68k:
  161.     ;;  "foo.c", line 32 pos 1; (E) syntax error; unexpected symbol: "lossage"
  162.     ;; GNAT (as of July 94): 
  163.     ;;  "foo.adb", line 2(11): warning: file name does not match ...
  164.     ("\"\\([^,\" \n\t]+\\)\", lines? \\([0-9]+\\)[:., (-]" 1 2)
  165.  
  166.     ;; MIPS RISC CC - the one distributed with Ultrix:
  167.     ;;    ccom: Error: foo.c, line 2: syntax error
  168.     ;; DEC AXP OSF/1 cc
  169.     ;;  /usr/lib/cmplrs/cc/cfe: Error: foo.c: 1: blah blah 
  170.     ("rror: \\([^,\" \n\t]+\\)[,:] \\(line \\)?\\([0-9]+\\):" 1 3)
  171.  
  172.     ;; IBM AIX PS/2 C version 1.1:
  173.     ;;    ****** Error number 140 in line 8 of file errors.c ******
  174.     ("in line \\([0-9]+\\) of file \\([^ \n]+[^. \n]\\)\\.? " 2 1)
  175.     ;; IBM AIX lint is too painful to do right this way.  File name
  176.     ;; prefixes entire sections rather than being on each line.
  177.  
  178.     ;; Lucid Compiler, lcc 3.x
  179.     ;; E, file.cc(35,52) Illegal operation on pointers
  180.     ("\n[EW], \\([^(\n]*\\)(\\([0-9]+\\),[ \t]*\\([0-9]+\\)" 1 2 3)
  181.  
  182.     ;; GNAT compiler v1.82
  183.     ;; foo.adb:2:1: Unit name does not match file name
  184.     ("\n\\([^ \n\t:]+\\):\\([0-9]+\\):\\([0-9]+\\)[: \t]" 1 2 3)
  185.  
  186.     ;; GNU message with program name and column number.
  187.     ("\n\\([^ \n\t:]+\\):\\([^ \n\t:]+\\):\
  188. \\([0-9]+\\):\\([0-9]+\\)[: \t]" 2 3 4)
  189.  
  190.     ;; Compiler Toolkit cocktail
  191.     ;; "ctla.pars", 151, 38: Error       illegal character: ;
  192.     ;; "ctla.pars", 167,  1: Warning     node type not used: ModuleName
  193.     ;; CHFIXME: find out how to "or" functions
  194.     ("\n\\\"\\([^ \n\\\"]+\\)\\\",[ ]*\\([0-9]+\\),[ ]*\\([0-9]+\\): Error" 1 2 3)
  195.     ("\n\\\"\\([^ \n\\\"]+\\)\\\",[ ]*\\([0-9]+\\),[ ]*\\([0-9]+\\): Warning" 1 2 3)
  196.  
  197.     ;; AmigaOS SAS/C Compiler 6.3
  198.     ;; amiga_dump.c 53 Error 72: conflict with previous declaration
  199.     ;; amiga_dump.c 164 Warning 72: conflict with previous declaration
  200.     ;; CHFIXME: find out how to "or" functions
  201.     ("\n\\([^ \n]+\\) \\([0-9]+\\) Warning" 1 2)
  202.     ("\n\\([^ \n]+\\) \\([0-9]+\\) Error" 1 2)
  203.     )
  204.   "Alist that specifies how to match errors in compiler output.
  205. Each element has the form (REGEXP FILE-IDX LINE-IDX [COLUMN-IDX]).
  206. If REGEXP matches, the FILE-IDX'th subexpression gives the file name, and
  207. the LINE-IDX'th subexpression gives the line number.  If COLUMN-IDX is
  208. given, the COLUMN-IDX'th subexpression gives the column number on that line.")
  209.  
  210. (defvar compilation-read-command t
  211.   "If not nil, M-x compile reads the compilation command to use.
  212. Otherwise, M-x compile just uses the value of `compile-command'.")
  213.  
  214. (defvar compilation-ask-about-save t
  215.   "If not nil, M-x compile asks which buffers to save before compiling.
  216. Otherwise, it saves all modified buffers without asking.")
  217.  
  218. (defvar grep-regexp-alist
  219.   '(("^\\([^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2))
  220.   "Regexp used to match grep hits.  See `compilation-error-regexp-alist'.")
  221.  
  222. (defvar grep-command "grep -n "
  223.   "Last grep command used in \\[grep]; default for next grep.")
  224.  
  225. ;;;###autoload
  226. (defvar compilation-search-path '(nil)
  227.   "*List of directories to search for source files named in error messages.
  228. Elements should be directory names, not file names of directories.
  229. nil as an element means to try the default directory.")
  230.  
  231. (defvar compile-command "make -k "
  232.   "Last shell command used to do a compilation; default for next compilation.
  233.  
  234. Sometimes it is useful for files to supply local values for this variable.
  235. You might also use mode hooks to specify it in certain modes, like this:
  236.  
  237.     (setq c-mode-hook
  238.       '(lambda () (or (file-exists-p \"makefile\") (file-exists-p \"Makefile\")
  239.               (progn (make-local-variable 'compile-command)
  240.                  (setq compile-command
  241.                     (concat \"make -k \"
  242.                         buffer-file-name))))))")
  243.  
  244. (defvar compilation-enter-directory-regexp
  245.   ": Entering directory `\\(.*\\)'$"
  246.   "Regular expression matching lines that indicate a new current directory.
  247. This must contain one \\(, \\) pair around the directory name.
  248.  
  249. The default value matches lines printed by the `-w' option of GNU Make.")
  250.  
  251. (defvar compilation-leave-directory-regexp
  252.   ": Leaving directory `\\(.*\\)'$"
  253.   "Regular expression matching lines that indicate restoring current directory.
  254. This may contain one \\(, \\) pair around the name of the directory
  255. being moved from.  If it does not, the last directory entered \(by a
  256. line matching `compilation-enter-directory-regexp'\) is assumed.
  257.  
  258. The default value matches lines printed by the `-w' option of GNU Make.")
  259.  
  260. (defvar compilation-directory-stack nil
  261.   "Stack of previous directories for `compilation-leave-directory-regexp'.
  262. The head element is the directory the compilation was started in.")
  263.  
  264. ;; History of compile commands.
  265. (defvar compile-history nil)
  266. ;; History of grep commands.
  267. (defvar grep-history nil)
  268.  
  269. ;;;###autoload
  270. (defun compile (command)
  271.   "Compile the program including the current buffer.  Default: run `make'.
  272. Runs COMMAND, a shell command, in a separate process asynchronously
  273. with output going to the buffer `*compilation*'.
  274.  
  275. You can then use the command \\[next-error] to find the next error message
  276. and move to the source code that caused it.
  277.  
  278. To run more than one compilation at once, start one and rename the
  279. \`*compilation*' buffer to some other name with \\[rename-buffer].
  280. Then start the next one.
  281.  
  282. The name used for the buffer is actually whatever is returned by
  283. the function in `compilation-buffer-name-function', so you can set that
  284. to a function that generates a unique name."
  285.   (interactive
  286.    (if compilation-read-command
  287.        (list (read-from-minibuffer "Compile command: "
  288.                                  compile-command nil nil
  289.                                  '(compile-history . 1)))
  290.      (list compile-command)))
  291.   (setq compile-command command)
  292.   (save-some-buffers (not compilation-ask-about-save) nil)
  293.   (compile-internal compile-command "No more errors"))
  294.  
  295. ;;; run compile with the default command line
  296. (defun recompile ()
  297.   "Re-compile the program including the current buffer."
  298.   (interactive)
  299.   (save-some-buffers (not compilation-ask-about-save) nil)
  300.   (compile-internal compile-command "No more errors"))
  301.   
  302. ;;;###autoload
  303. (defun grep (command-args)
  304.   "Run grep, with user-specified args, and collect output in a buffer.
  305. While grep runs asynchronously, you can use the \\[next-error] command
  306. to find the text that grep hits refer to.
  307.  
  308. This command uses a special history list for its arguments, so you can
  309. easily repeat a grep command."
  310.   (interactive
  311.    (list (read-from-minibuffer "Run grep (like this): "
  312.                    grep-command nil nil 'grep-history)))
  313.   (compile-internal (concat command-args 
  314.                 (if (eq system-type 'amigaos) " nil:"
  315.                   " /dev/null"))
  316.             "No more grep hits" "grep"
  317.             ;; Give it a simpler regexp to match.
  318.             nil grep-regexp-alist))
  319.  
  320. (defun compile-internal (command error-message
  321.                  &optional name-of-mode parser regexp-alist
  322.                  name-function)
  323.   "Run compilation command COMMAND (low level interface).
  324. ERROR-MESSAGE is a string to print if the user asks to see another error
  325. and there are no more errors.  Third argument NAME-OF-MODE is the name
  326. to display as the major mode in the compilation buffer.
  327.  
  328. Fourth arg PARSER is the error parser function (nil means the default).  Fifth
  329. arg REGEXP-ALIST is the error message regexp alist to use (nil means the
  330. default).  Sixth arg NAME-FUNCTION is a function called to name the buffer (nil
  331. means the default).  The defaults for these variables are the global values of
  332. \`compilation-parse-errors-function', `compilation-error-regexp-alist', and
  333. \`compilation-buffer-name-function', respectively.
  334.  
  335. Returns the compilation buffer created."
  336.   (let (outbuf)
  337.     (save-excursion
  338.       (or name-of-mode
  339.       (setq name-of-mode "Compilation"))
  340.       (setq outbuf
  341.         (get-buffer-create
  342.          (funcall (or name-function compilation-buffer-name-function
  343.               (function (lambda (mode)
  344.                       (concat "*" (downcase mode) "*"))))
  345.               name-of-mode)))
  346.       (set-buffer outbuf)
  347.       (let ((comp-proc (get-buffer-process (current-buffer))))
  348.     (if comp-proc
  349.         (if (or (not (eq (process-status comp-proc) 'run))
  350.             (yes-or-no-p
  351.              (format "A %s process is running; kill it? "
  352.                  name-of-mode)))
  353.         (condition-case ()
  354.             (progn
  355.               (interrupt-process comp-proc)
  356.               (sit-for 1)
  357.               (delete-process comp-proc))
  358.           (error nil))
  359.           (error "Cannot have two processes in `%s' at once"
  360.              (buffer-name))
  361.           )))
  362.       ;; In case the compilation buffer is current, make sure we get the global
  363.       ;; values of compilation-error-regexp-alist, etc.
  364.       (kill-all-local-variables))
  365.     (let ((regexp-alist (or regexp-alist compilation-error-regexp-alist))
  366.       (parser (or parser compilation-parse-errors-function))
  367.       (thisdir default-directory)
  368.       outwin) 
  369.       (save-excursion
  370.     ;; Clear out the compilation buffer and make it writable.
  371.     ;; Change its default-directory to the directory where the compilation
  372.     ;; will happen, and insert a `cd' command to indicate this.
  373.     (set-buffer outbuf)
  374.     (setq buffer-read-only nil)
  375.     (erase-buffer)
  376.     (setq default-directory thisdir)
  377.     (insert "cd " thisdir "\n" command "\n")
  378.     (set-buffer-modified-p nil))
  379.       ;; If we're already in the compilation buffer, go to the end
  380.       ;; of the buffer, so point will track the compilation output.
  381.       (if (eq outbuf (current-buffer))
  382.       (goto-char (point-max)))
  383.       ;; Pop up the compilation buffer.
  384.       (setq outwin (display-buffer outbuf))
  385.       (save-excursion
  386.     (set-buffer outbuf)
  387.     (compilation-mode)
  388.     (buffer-disable-undo (current-buffer))
  389.     ;; (setq buffer-read-only t)  ;;; Non-ergonomic.
  390.     (set (make-local-variable 'compilation-parse-errors-function) parser)
  391.     (set (make-local-variable 'compilation-error-message) error-message)
  392.     (set (make-local-variable 'compilation-error-regexp-alist) regexp-alist)
  393.     (setq default-directory thisdir
  394.           compilation-directory-stack (list default-directory))
  395.     (set-window-start outwin (point-min))
  396.     (setq mode-name name-of-mode)
  397.     (or (eq outwin (selected-window))
  398.         (set-window-point outwin (point-min)))
  399.     (compilation-set-window-height outwin)
  400.     ;; Start the compilation.
  401.     (if (fboundp 'start-process)
  402.         (let ((proc (start-process-shell-command (downcase mode-name)
  403.                              outbuf
  404.                              command)))
  405.           (set-process-sentinel proc 'compilation-sentinel)
  406.           (set-process-filter proc 'compilation-filter)
  407.           (set-marker (process-mark proc) (point) outbuf)
  408.           (setq compilation-in-progress 
  409.             (cons proc compilation-in-progress)))
  410.       ;; No asynchronous processes available
  411.       (message (format "Executing `%s'..." command))
  412.       (let ((status (call-process shell-file-name nil outbuf nil "-c"
  413.                       command))))
  414.       (message (format "Executing `%s'...done" command)))))
  415.     ;; Make it so the next C-x ` will use this buffer.
  416.     (setq compilation-last-buffer outbuf)))
  417.  
  418. ;; Set the height of WINDOW according to compilation-window-height.
  419. (defun compilation-set-window-height (window)
  420.   (and compilation-window-height
  421.        (= (window-width window) (frame-width (window-frame window)))
  422.        ;; If window is alone in its frame, aside from a minibuffer,
  423.        ;; don't change its height.
  424.        (not (eq window (frame-root-window (window-frame window))))
  425.        ;; This save-excursion prevents us from changing the current buffer,
  426.        ;; which might not be the same as the selected window's buffer.
  427.        (save-excursion
  428.      (let ((w (selected-window)))
  429.        (unwind-protect
  430.            (progn
  431.          (select-window window)
  432.          (enlarge-window (- compilation-window-height
  433.                     (window-height))))
  434.          (select-window w))))))
  435.  
  436. (defvar compilation-minor-mode-map
  437.   (let ((map (make-sparse-keymap)))
  438.     (define-key map [mouse-2] 'compile-mouse-goto-error)
  439.     (define-key map "\C-c\C-c" 'compile-goto-error)
  440.     (define-key map "\C-c\C-k" 'kill-compilation)
  441.     (define-key map "\M-n" 'compilation-next-error)
  442.     (define-key map "\M-p" 'compilation-previous-error)
  443.     (define-key map "\M-{" 'compilation-previous-file)
  444.     (define-key map "\M-}" 'compilation-next-file)
  445.     map)
  446.   "Keymap for `compilation-minor-mode'.")
  447.  
  448. (defvar compilation-mode-map
  449.   (let ((map (cons 'keymap compilation-minor-mode-map)))
  450.     (define-key map " " 'scroll-up)
  451.     (define-key map "\^?" 'scroll-down)
  452.     ;; Set up the menu-bar
  453.     (define-key map [menu-bar compilation-menu]
  454.       (cons "Compile" (make-sparse-keymap "Compile")))
  455.  
  456.     (define-key map [menu-bar compilation-menu compilation-mode-kill-compilation]
  457.       '("Stop compilation" . kill-compilation))
  458.     (define-key map [menu-bar compilation-menu compilation-mode-separator2]
  459.       '("----" . nil))
  460.     (define-key map [menu-bar compilation-menu compilation-mode-first-error]
  461.       '("First error" . first-error))
  462.     (define-key map [menu-bar compilation-menu compilation-mode-previous-error]
  463.       '("Previous error" . previous-error))
  464.     (define-key map [menu-bar compilation-menu compilation-mode-next-error]
  465.       '("Next error" . next-error))
  466.     (define-key map [menu-bar compilation-menu compilation-separator2]
  467.       '("----" . nil))
  468.     (define-key map [menu-bar compilation-menu compilation-mode-grep]
  469.       '("Grep" . grep))
  470.     (define-key map [menu-bar compilation-menu compilation-mode-recompile]
  471.       '("Recompile" . recompile))
  472.     (define-key map [menu-bar compilation-menu compilation-mode-compile]
  473.       '("Compile" . compile))
  474.     map)
  475.   "Keymap for compilation log buffers.
  476. `compilation-minor-mode-map' is a cdr of this.")
  477.  
  478. (defun compilation-mode ()
  479.   "Major mode for compilation log buffers.
  480. \\<compilation-mode-map>To visit the source for a line-numbered error,
  481. move point to the error message line and type \\[compile-goto-error].
  482. To kill the compilation, type \\[kill-compilation].
  483.  
  484. Runs `compilation-mode-hook' with `run-hooks' (which see)."
  485.   (interactive)
  486.   (fundamental-mode)
  487.   (use-local-map compilation-mode-map)
  488.   (setq major-mode 'compilation-mode
  489.     mode-name "Compilation")
  490.   (compilation-setup)
  491.   (run-hooks 'compilation-mode-hook))
  492.  
  493. ;; Prepare the buffer for the compilation parsing commands to work.
  494. (defun compilation-setup ()
  495.   ;; Make the buffer's mode line show process state.
  496.   (setq mode-line-process '(":%s"))
  497.   (set (make-local-variable 'compilation-error-list) nil)
  498.   (set (make-local-variable 'compilation-old-error-list) nil)
  499.   (set (make-local-variable 'compilation-parsing-end) 1)
  500.   (set (make-local-variable 'compilation-directory-stack) nil)
  501.   (setq compilation-last-buffer (current-buffer)))
  502.  
  503. (defvar compilation-minor-mode nil
  504.   "Non-nil when in compilation-minor-mode.
  505. In this minor mode, all the error-parsing commands of the
  506. Compilation major mode are available.")
  507. (make-variable-buffer-local 'compilation-minor-mode)
  508.  
  509. (or (assq 'compilation-minor-mode minor-mode-alist)
  510.     (setq minor-mode-alist (cons '(compilation-minor-mode " Compilation")
  511.                  minor-mode-alist)))
  512. (or (assq 'compilation-minor-mode minor-mode-map-alist)
  513.     (setq minor-mode-map-alist (cons (cons 'compilation-minor-mode
  514.                        compilation-minor-mode-map)
  515.                      minor-mode-map-alist)))
  516.  
  517. ;;;###autoload
  518. (defun compilation-minor-mode (&optional arg)
  519.   "Toggle compilation minor mode.
  520. With arg, turn compilation mode on if and only if arg is positive.
  521. See `compilation-mode'."
  522.   (interactive "P")
  523.   (if (setq compilation-minor-mode (if (null arg)
  524.                        (null compilation-minor-mode)
  525.                      (> (prefix-numeric-value arg) 0)))
  526.       (compilation-setup)))
  527.  
  528. ;; Called when compilation process changes state.
  529. (defun compilation-sentinel (proc msg)
  530.   "Sentinel for compilation buffers."
  531.   (let ((buffer (process-buffer proc)))
  532.     (if (memq (process-status proc) '(signal exit))
  533.     (progn
  534.       (if (null (buffer-name buffer))
  535.           ;; buffer killed
  536.           (set-process-buffer proc nil)
  537.         (let ((obuf (current-buffer))
  538.           omax opoint)
  539.           ;; save-excursion isn't the right thing if
  540.           ;; process-buffer is current-buffer
  541.           (unwind-protect
  542.           (progn
  543.             ;; Write something in the compilation buffer
  544.             ;; and hack its mode line.
  545.             (set-buffer buffer)
  546.             (let ((buffer-read-only nil))
  547.               (setq omax (point-max)
  548.                 opoint (point))
  549.               (goto-char omax)
  550.               ;; Record where we put the message, so we can ignore it
  551.               ;; later on.
  552.               (insert ?\n mode-name " " msg)
  553.               (forward-char -1)
  554.               (insert " at " (substring (current-time-string) 0 19))
  555.               (forward-char 1)
  556.               (setq mode-line-process
  557.                 (concat ":"
  558.                     (symbol-name (process-status proc))))
  559.               ;; Since the buffer and mode line will show that the
  560.               ;; process is dead, we can delete it now.  Otherwise it
  561.               ;; will stay around until M-x list-processes.
  562.               (delete-process proc)
  563.               ;; Force mode line redisplay soon.
  564.               (set-buffer-modified-p (buffer-modified-p)))
  565.             (if (and opoint (< opoint omax))
  566.             (goto-char opoint))
  567.             (if compilation-finish-function
  568.             (funcall compilation-finish-function buffer msg)))
  569.         (set-buffer obuf))))
  570.       (setq compilation-in-progress (delq proc compilation-in-progress))
  571.       ))))
  572.  
  573. (defun compilation-filter (proc string)
  574.   "Process filter for compilation buffers.
  575. Just inserts the text, but uses `insert-before-markers'."
  576.   (save-excursion
  577.     (set-buffer (process-buffer proc))
  578.     (let ((buffer-read-only nil))
  579.       (save-excursion
  580.     (goto-char (process-mark proc))
  581.     (insert-before-markers string)
  582.     (set-marker (process-mark proc) (point))))))
  583.  
  584. ;; Return the cdr of compilation-old-error-list for the error containing point.
  585. (defun compile-error-at-point ()
  586.   (compile-reinitialize-errors nil (point))
  587.   (let ((errors compilation-old-error-list))
  588.     (while (and errors
  589.         (> (point) (car (car errors))))
  590.       (setq errors (cdr errors)))
  591.     errors))
  592.  
  593. (defsubst compilation-buffer-p (buffer)
  594.   (assq 'compilation-error-list (buffer-local-variables buffer)))
  595.  
  596. (defun compilation-next-error (n)
  597.   "Move point to the next error in the compilation buffer.
  598. Does NOT find the source line like \\[next-error]."
  599.   (interactive "p")
  600.   (or (compilation-buffer-p (current-buffer))
  601.       (error "Not in a compilation buffer."))
  602.   (setq compilation-last-buffer (current-buffer))
  603.  
  604.   (let ((errors (compile-error-at-point)))
  605.  
  606.     ;; Move to the error after the one containing point.
  607.     (goto-char (car (if (< n 0)
  608.             (let ((i 0)
  609.                   (e compilation-old-error-list))
  610.               ;; See how many cdrs away ERRORS is from the start.
  611.               (while (not (eq e errors))
  612.                 (setq i (1+ i)
  613.                   e (cdr e)))
  614.               (if (> (- n) i)
  615.                   (error "Moved back past first error")
  616.                 (nth (+ i n) compilation-old-error-list)))
  617.               (let ((compilation-error-list (cdr errors)))
  618.             (compile-reinitialize-errors nil nil n)
  619.             (if compilation-error-list
  620.                 (nth (1- n) compilation-error-list)
  621.               (error "Moved past last error"))))))))
  622.  
  623. (defun compilation-previous-error (n)
  624.   "Move point to the previous error in the compilation buffer.
  625. Does NOT find the source line like \\[next-error]."
  626.   (interactive "p")
  627.   (compilation-next-error (- n)))
  628.  
  629.  
  630. ;; Given an elt of `compilation-error-list', return an object representing
  631. ;; the referenced file which is equal to (but not necessarily eq to) what
  632. ;; this function would return for another error in the same file.
  633. (defsubst compilation-error-filedata (data)
  634.   (setq data (cdr data))
  635.   (if (markerp data)
  636.       (marker-buffer data)
  637.     (car data)))
  638.  
  639. ;; Return a string describing a value from compilation-error-filedata.
  640. ;; This value is not necessarily useful as a file name, but should be
  641. ;; indicative to the user of what file's errors are being referred to.
  642. (defsubst compilation-error-filedata-file-name (filedata)
  643.   (if (bufferp filedata)
  644.       (buffer-file-name filedata)
  645.     (car filedata)))
  646.  
  647. (defun compilation-next-file (n)
  648.   "Move point to the next error for a different file than the current one."
  649.   (interactive "p")
  650.   (or (compilation-buffer-p (current-buffer))
  651.       (error "Not in a compilation buffer."))
  652.   (setq compilation-last-buffer (current-buffer))
  653.  
  654.   (let ((reversed (< n 0))
  655.     errors filedata)
  656.  
  657.     (if (not reversed)
  658.     (setq errors (or (compile-error-at-point)
  659.              (error "Moved past last error")))
  660.  
  661.       ;; Get a reversed list of the errors up through the one containing point.
  662.       (compile-reinitialize-errors nil (point))
  663.       (setq errors (reverse compilation-old-error-list)
  664.         n (- n))
  665.  
  666.       ;; Ignore errors after point.  (car ERRORS) will be the error
  667.       ;; containing point, (cadr ERRORS) the one before it.
  668.       (while (and errors
  669.           (< (point) (car (car errors))))
  670.     (setq errors (cdr errors))))
  671.  
  672.     (while (> n 0)
  673.       (setq filedata (compilation-error-filedata (car errors)))
  674.  
  675.       ;; Skip past the following errors for this file.
  676.       (while (equal filedata
  677.             (compilation-error-filedata
  678.              (car (or errors
  679.                   (if reversed
  680.                   (error "%s the first erring file"
  681.                      (compilation-error-filedata-file-name
  682.                       filedata))
  683.                 (let ((compilation-error-list nil))
  684.                   ;; Parse some more.
  685.                   (compile-reinitialize-errors nil nil 2)
  686.                   (setq errors compilation-error-list)))
  687.                   (error "%s is the last erring file" 
  688.                      (compilation-error-filedata-file-name
  689.                       filedata))))))
  690.     (setq errors (cdr errors)))
  691.  
  692.       (setq n (1- n)))
  693.  
  694.     ;; Move to the following error.
  695.     (goto-char (car (car (or errors
  696.                  (if reversed
  697.                  (error "This is the first erring file")
  698.                    (let ((compilation-error-list nil))
  699.                  ;; Parse the last one.
  700.                  (compile-reinitialize-errors nil nil 1)
  701.                  compilation-error-list))))))))
  702.  
  703. (defun compilation-previous-file (n)
  704.   "Move point to the previous error for a different file than the current one."
  705.   (interactive "p")
  706.   (compilation-next-file (- n)))
  707.  
  708.  
  709. (defun kill-compilation ()
  710.   "Kill the process made by the \\[compile] command."
  711.   (interactive)
  712.   (let ((buffer (compilation-find-buffer)))
  713.     (if (get-buffer-process buffer)
  714.     (interrupt-process (get-buffer-process buffer))
  715.       (error "The compilation process is not running."))))
  716.  
  717.  
  718. ;; Parse any new errors in the compilation buffer,
  719. ;; or reparse from the beginning if the user has asked for that.
  720. (defun compile-reinitialize-errors (reparse
  721.                     &optional limit-search find-at-least)
  722.   (save-excursion
  723.     (set-buffer compilation-last-buffer)
  724.     ;; If we are out of errors, or if user says "reparse",
  725.     ;; discard the info we have, to force reparsing.
  726.     (if (or (eq compilation-error-list t)
  727.         reparse)
  728.     (compilation-forget-errors))
  729.     (if (and compilation-error-list
  730.          (or (not limit-search)
  731.          (> compilation-parsing-end limit-search))
  732.          (or (not find-at-least)
  733.          (>= (length compilation-error-list) find-at-least)))
  734.     ;; Since compilation-error-list is non-nil, it points to a specific
  735.     ;; error the user wanted.  So don't move it around.
  736.     nil
  737.       ;; This was here for a long time (before my rewrite); why? --roland
  738.       ;;(switch-to-buffer compilation-last-buffer)
  739.       (set-buffer-modified-p nil)
  740.       (if (< compilation-parsing-end (point-max))
  741.       ;; compilation-error-list might be non-nil if we have a non-nil
  742.       ;; LIMIT-SEARCH or FIND-AT-LEAST arg.  In that case its value
  743.       ;; records the current position in the error list, and we must
  744.       ;; preserve that after reparsing.
  745.       (let ((error-list-pos compilation-error-list))
  746.         (funcall compilation-parse-errors-function
  747.              limit-search
  748.              (and find-at-least
  749.               ;; We only need enough new parsed errors to reach
  750.               ;; FIND-AT-LEAST errors past the current
  751.               ;; position.
  752.               (- find-at-least (length compilation-error-list))))
  753.         ;; Remember the entire list for compilation-forget-errors.  If
  754.         ;; this is an incremental parse, append to previous list.  If
  755.         ;; we are parsing anew, compilation-forget-errors cleared
  756.         ;; compilation-old-error-list above.
  757.         (setq compilation-old-error-list
  758.           (nconc compilation-old-error-list compilation-error-list))
  759.         (if error-list-pos
  760.         ;; We started in the middle of an existing list of parsed
  761.         ;; errors before parsing more; restore that position.
  762.         (setq compilation-error-list error-list-pos))
  763.         )))))
  764.  
  765. (defun compile-mouse-goto-error (event)
  766.   (interactive "e")
  767.   (save-excursion
  768.     (set-buffer (window-buffer (posn-window (event-end event))))
  769.     (goto-char (posn-point (event-end event)))
  770.  
  771.     (or (compilation-buffer-p (current-buffer))
  772.     (error "Not in a compilation buffer."))
  773.     (setq compilation-last-buffer (current-buffer))
  774.     (compile-reinitialize-errors nil (point))
  775.  
  776.     ;; Move to bol; the marker for the error on this line will point there.
  777.     (beginning-of-line)
  778.  
  779.     ;; Move compilation-error-list to the elt of compilation-old-error-list
  780.     ;; we want.
  781.     (setq compilation-error-list compilation-old-error-list)
  782.     (while (and compilation-error-list
  783.         (> (point) (car (car compilation-error-list))))
  784.       (setq compilation-error-list (cdr compilation-error-list)))
  785.     (or compilation-error-list
  786.     (error "No error to go to")))
  787.   (select-window (posn-window (event-end event)))
  788.   ;; Move to another window, so that next-error's window changes
  789.   ;; result in the desired setup.
  790.   (or (one-window-p)
  791.       (progn
  792.     (other-window -1)
  793.     ;; other-window changed the selected buffer,
  794.     ;; but we didn't want to do that.
  795.     (set-buffer compilation-last-buffer)))
  796.  
  797.   (push-mark)
  798.   (next-error 1))
  799.  
  800. (defun compile-goto-error (&optional argp)
  801.   "Visit the source for the error message point is on.
  802. Use this command in a compilation log buffer.  Sets the mark at point there.
  803. \\[universal-argument] as a prefix arg means to reparse the buffer's error messages first;
  804. other kinds of prefix arguments are ignored."
  805.   (interactive "P")
  806.   (or (compilation-buffer-p (current-buffer))
  807.       (error "Not in a compilation buffer."))
  808.   (setq compilation-last-buffer (current-buffer))
  809.   (compile-reinitialize-errors (consp argp) (point))
  810.  
  811.   ;; Move to bol; the marker for the error on this line will point there.
  812.   (beginning-of-line)
  813.  
  814.   ;; Move compilation-error-list to the elt of compilation-old-error-list
  815.   ;; we want.
  816.   (setq compilation-error-list compilation-old-error-list)
  817.   (while (and compilation-error-list
  818.           (> (point) (car (car compilation-error-list))))
  819.     (setq compilation-error-list (cdr compilation-error-list)))
  820.  
  821.   ;; Move to another window, so that next-error's window changes
  822.   ;; result in the desired setup.
  823.   (or (one-window-p)
  824.       (progn
  825.     (other-window -1)
  826.     ;; other-window changed the selected buffer,
  827.     ;; but we didn't want to do that.
  828.     (set-buffer compilation-last-buffer)))
  829.  
  830.   (push-mark)
  831.   (next-error 1))
  832.  
  833. ;; Return a compilation buffer.
  834. ;; If the current buffer is a compilation buffer, return it.
  835. ;; If compilation-last-buffer is set to a live buffer, use that.
  836. ;; Otherwise, look for a compilation buffer and signal an error
  837. ;; if there are none.
  838. (defun compilation-find-buffer (&optional other-buffer)
  839.   (if (and (not other-buffer)
  840.        (compilation-buffer-p (current-buffer)))
  841.       ;; The current buffer is a compilation buffer.
  842.       (current-buffer)
  843.     (if (and compilation-last-buffer (buffer-name compilation-last-buffer)
  844.          (or (not other-buffer) (not (eq compilation-last-buffer
  845.                          (current-buffer)))))
  846.     compilation-last-buffer
  847.       (let ((buffers (buffer-list)))
  848.     (while (and buffers (or (not (compilation-buffer-p (car buffers)))
  849.                 (and other-buffer
  850.                      (eq (car buffers) (current-buffer)))))
  851.       (setq buffers (cdr buffers)))
  852.     (if buffers
  853.         (car buffers)
  854.       (or (and other-buffer
  855.            (compilation-buffer-p (current-buffer))
  856.            ;; The current buffer is a compilation buffer.
  857.            (progn
  858.              (if other-buffer
  859.              (message "This is the only compilation buffer."))
  860.              (current-buffer)))
  861.           (error "No compilation started!")))))))
  862.  
  863. ;;;###autoload
  864. (defun next-error (&optional argp)
  865.   "Visit next compilation error message and corresponding source code.
  866. This operates on the output from the \\[compile] command.
  867. If all preparsed error messages have been processed,
  868. the error message buffer is checked for new ones.
  869.  
  870. A prefix arg specifies how many error messages to move;
  871. negative means move back to previous error messages.
  872. Just C-u as a prefix means reparse the error message buffer
  873. and start at the first error.
  874.  
  875. \\[next-error] normally applies to the most recent compilation started,
  876. but as long as you are in the middle of parsing errors from one compilation
  877. output buffer, you stay with that compilation output buffer.
  878.  
  879. Use \\[next-error] in a compilation output buffer to switch to
  880. processing errors from that compilation.
  881.  
  882. See variables `compilation-parse-errors-function' and
  883. \`compilation-error-regexp-alist' for customization ideas."
  884.   (interactive "P")
  885.   (setq compilation-last-buffer (compilation-find-buffer))
  886.   (compilation-goto-locus (compilation-next-error-locus
  887.                ;; We want to pass a number here only if
  888.                ;; we got a numeric prefix arg, not just C-u.
  889.                (and (not (consp argp))
  890.                 (prefix-numeric-value argp))
  891.                (consp argp))))
  892. ;;;###autoload (define-key ctl-x-map "`" 'next-error)
  893.  
  894. (defun previous-error ()
  895.   "Visit previous compilation error message and corresponding source code.
  896. This operates on the output from the \\[compile] command."
  897.   (interactive)
  898.   (next-error '-1))
  899.  
  900. (defun first-error ()
  901.   "Reparse the error message buffer and start at the first error
  902. Visit corresponding source code.
  903. This operates on the output from the \\[compile] command."
  904.   (interactive)
  905.   (next-error '(1.1)))
  906.  
  907. (defun compilation-next-error-locus (&optional move reparse silent)
  908.   "Visit next compilation error and return locus in corresponding source code.
  909. This operates on the output from the \\[compile] command.
  910. If all preparsed error messages have been processed,
  911. the error message buffer is checked for new ones.
  912.  
  913. Returns a cons (ERROR . SOURCE) of two markers: ERROR is a marker at the
  914. location of the error message in the compilation buffer, and SOURCE is a
  915. marker at the location in the source code indicated by the error message.
  916.  
  917. Optional first arg MOVE says how many error messages to move forwards (or
  918. backwards, if negative); default is 1.  Optional second arg REPARSE, if
  919. non-nil, says to reparse the error message buffer and reset to the first
  920. error (plus MOVE - 1).  If optional third argument SILENT is non-nil, return 
  921. nil instead of raising an error if there are no more errors.
  922.  
  923. The current buffer should be the desired compilation output buffer."
  924.   (or move (setq move 1))
  925.   (compile-reinitialize-errors reparse nil (and (not reparse)
  926.                         (if (< move 1) 0 (1- move))))
  927.   (let (next-errors next-error)
  928.     (catch 'no-next-error
  929.       (save-excursion
  930.     (set-buffer compilation-last-buffer)
  931.     ;; compilation-error-list points to the "current" error.
  932.     (setq next-errors 
  933.           (if (> move 0)
  934.           (nthcdr (1- move)
  935.               compilation-error-list)
  936.         ;; Zero or negative arg; we need to move back in the list.
  937.         (let ((n (1- move))
  938.               (i 0)
  939.               (e compilation-old-error-list))
  940.           ;; See how many cdrs away the current error is from the start.
  941.           (while (not (eq e compilation-error-list))
  942.             (setq i (1+ i)
  943.               e (cdr e)))
  944.           (if (> (- n) i)
  945.               (error "Moved back past first error")
  946.             (nthcdr (+ i n) compilation-old-error-list))))
  947.           next-error (car next-errors))
  948.     (while
  949.         (if (null next-error)
  950.         (progn
  951.           (and move (/= move 1)
  952.                (error (if (> move 0)
  953.                   "Moved past last error")
  954.                   "Moved back past first error"))
  955.           ;; Forget existing error messages if compilation has finished.
  956.           (if (not (and (get-buffer-process (current-buffer))
  957.                 (eq (process-status
  958.                      (get-buffer-process
  959.                       (current-buffer)))
  960.                     'run)))
  961.               (compilation-forget-errors))
  962.           (if silent
  963.               (throw 'no-next-error nil)
  964.             (error (concat compilation-error-message
  965.                    (and (get-buffer-process (current-buffer))
  966.                     (eq (process-status
  967.                          (get-buffer-process
  968.                           (current-buffer)))
  969.                         'run)
  970.                     " yet")))))
  971.           (setq compilation-error-list (cdr next-errors))
  972.           (if (null (cdr next-error))
  973.           ;; This error is boring.  Go to the next.
  974.           t
  975.         (or (markerp (cdr next-error))
  976.             ;; This error has a filename/lineno pair.
  977.             ;; Find the file and turn it into a marker.
  978.             (let* ((fileinfo (car (cdr next-error)))
  979.                (buffer (compilation-find-file (cdr fileinfo)
  980.                               (car fileinfo)
  981.                               (car next-error))))
  982.               (if (null buffer)
  983.               ;; We can't find this error's file.
  984.               ;; Remove all errors in the same file.
  985.               (progn
  986.                 (setq next-errors compilation-old-error-list)
  987.                 (while next-errors
  988.                   (and (consp (cdr (car next-errors)))
  989.                    (equal (car (cdr (car next-errors)))
  990.                       fileinfo)
  991.                    (progn
  992.                      (set-marker (car (car next-errors)) nil)
  993.                      (setcdr (car next-errors) nil)))
  994.                   (setq next-errors (cdr next-errors)))
  995.                 ;; Look for the next error.
  996.                 t)
  997.             ;; We found the file.  Get a marker for this error.
  998.             ;; compilation-old-error-list is a buffer-local
  999.             ;; variable, so we must be careful to extract its value
  1000.             ;; before switching to the source file buffer.
  1001.             (let ((errors compilation-old-error-list)
  1002.                   (last-line (nth 1 (cdr next-error)))
  1003.                   (column (nth 2 (cdr next-error))))
  1004.               (set-buffer buffer)
  1005.               (save-excursion
  1006.                 (save-restriction
  1007.                   (widen)
  1008.                   (goto-line last-line)
  1009.                   (if column
  1010.                   (move-to-column column)
  1011.                 (beginning-of-line))
  1012.                   (setcdr next-error (point-marker))
  1013.                   ;; Make all the other error messages referring
  1014.                   ;; to the same file have markers into the buffer.
  1015.                   (while errors
  1016.                 (and (consp (cdr (car errors)))
  1017.                      (equal (car (cdr (car errors))) fileinfo)
  1018.                      (let* ((this (nth 1 (cdr (car errors))))
  1019.                         (column (nth 2 (cdr (car errors))))
  1020.                         (lines (- this last-line)))
  1021.                        (if (eq selective-display t)
  1022.                        ;; When selective-display is t,
  1023.                        ;; each C-m is a line boundary,
  1024.                        ;; as well as each newline.
  1025.                        (if (< lines 0)
  1026.                            (re-search-backward "[\n\C-m]"
  1027.                                    nil 'end
  1028.                                    (- lines))
  1029.                          (re-search-forward "[\n\C-m]"
  1030.                                 nil 'end
  1031.                                 lines))
  1032.                      (forward-line lines))
  1033.                        (if column
  1034.                        (move-to-column column))
  1035.                        (setq last-line this)
  1036.                        (setcdr (car errors) (point-marker))))
  1037.                 (setq errors (cdr errors)))))))))
  1038.         ;; If we didn't get a marker for this error, or this
  1039.         ;; marker's buffer was killed, go on to the next one.
  1040.         (or (not (markerp (cdr next-error)))
  1041.             (not (marker-buffer (cdr next-error))))))
  1042.       (setq next-errors compilation-error-list
  1043.         next-error (car next-errors)))))
  1044.  
  1045.     ;; Skip over multiple error messages for the same source location,
  1046.     ;; so the next C-x ` won't go to an error in the same place.
  1047.     (while (and compilation-error-list
  1048.         (equal (cdr (car compilation-error-list)) (cdr next-error)))
  1049.       (setq compilation-error-list (cdr compilation-error-list)))
  1050.  
  1051.     ;; We now have a marker for the position of the error source code.
  1052.     ;; NEXT-ERROR is a cons (ERROR . SOURCE) of two markers.
  1053.     next-error))
  1054.  
  1055. (defun compilation-goto-locus (next-error)
  1056.   "Jump to an error locus returned by `compilation-next-error-locus'.
  1057. Takes one argument, a cons (ERROR . SOURCE) of two markers.
  1058. Selects a window with point at SOURCE, with another window displaying ERROR."
  1059.   (if (and (window-dedicated-p (selected-window))
  1060.        (eq (selected-window) (frame-root-window)))
  1061.       (switch-to-buffer-other-frame (marker-buffer (cdr next-error)))
  1062.     (switch-to-buffer (marker-buffer (cdr next-error))))
  1063.   (goto-char (cdr next-error))
  1064.   ;; If narrowing got in the way of
  1065.   ;; going to the right place, widen.
  1066.   (or (= (point) (marker-position (cdr next-error)))
  1067.       (progn
  1068.     (widen)
  1069.     (goto-char (cdr next-error))))
  1070.  
  1071.   ;; Show compilation buffer in other window, scrolled to this error.
  1072.   (let* ((pop-up-windows t)
  1073.      (w (or (get-buffer-window (marker-buffer (car next-error)) 'visible)
  1074.         (display-buffer (marker-buffer (car next-error))))))
  1075.     (set-window-point w (car next-error))
  1076.     (set-window-start w (car next-error))
  1077.     (compilation-set-window-height w)))
  1078.  
  1079. ;; Find a buffer for file FILENAME.
  1080. ;; Search the directories in compilation-search-path.
  1081. ;; A nil in compilation-search-path means to try the
  1082. ;; current directory, which is passed in DIR.
  1083. ;; If FILENAME is not found at all, ask the user where to find it.
  1084. ;; Pop up the buffer containing MARKER and scroll to MARKER if we ask the user.
  1085. (defun compilation-find-file (filename dir marker)
  1086.   (let ((dirs compilation-search-path)
  1087.     result name)
  1088.     (while (and dirs (null result))
  1089.       (setq name (expand-file-name filename (or (car dirs) dir))
  1090.         result (and (file-exists-p name)
  1091.             (find-file-noselect name))
  1092.         dirs (cdr dirs)))
  1093.     (or result
  1094.     ;; The file doesn't exist.
  1095.     ;; Ask the user where to find it.
  1096.     ;; If he hits C-g, then the next time he does
  1097.     ;; next-error, he'll skip past it.
  1098.     (progn
  1099.       (let* ((pop-up-windows t)
  1100.          (w (display-buffer (marker-buffer marker))))
  1101.         (set-window-point w marker)
  1102.         (set-window-start w marker))
  1103.       (setq name
  1104.         (expand-file-name
  1105.          (read-file-name
  1106.           (format "Find this error in: (default %s) "
  1107.               filename) dir filename t)))
  1108.       (if (file-directory-p name)
  1109.           (setq name (concat (file-name-as-directory name) filename)))
  1110.       (if (file-exists-p name)
  1111.           (find-file-noselect name))))))
  1112.  
  1113. ;; Set compilation-error-list to nil, and unchain the markers that point to the
  1114. ;; error messages and their text, so that they no longer slow down gap motion.
  1115. ;; This would happen anyway at the next garbage collection, but it is better to
  1116. ;; do it right away.
  1117. (defun compilation-forget-errors ()
  1118.   (while compilation-old-error-list
  1119.     (let ((next-error (car compilation-old-error-list)))
  1120.       (set-marker (car next-error) nil)
  1121.       (if (markerp (cdr next-error))
  1122.       (set-marker (cdr next-error) nil)))
  1123.     (setq compilation-old-error-list (cdr compilation-old-error-list)))
  1124.   (setq compilation-error-list nil
  1125.     compilation-directory-stack nil
  1126.     compilation-parsing-end 1))
  1127.  
  1128.  
  1129. (defun count-regexp-groupings (regexp)
  1130.   "Return the number of \\( ... \\) groupings in REGEXP (a string)."
  1131.   (let ((groupings 0)
  1132.     (len (length regexp))
  1133.     (i 0)
  1134.     c)
  1135.     (while (< i len)
  1136.       (setq c (aref regexp i)
  1137.         i (1+ i))
  1138.       (cond ((= c ?\[)
  1139.          ;; Find the end of this [...].
  1140.          (while (and (< i len)
  1141.              (not (= (aref regexp i) ?\])))
  1142.            (setq i (1+ i))))
  1143.         ((= c ?\\)
  1144.          (if (< i len)
  1145.          (progn
  1146.            (setq c (aref regexp i)
  1147.              i (1+ i))
  1148.            (if (= c ?\))
  1149.                ;; We found the end of a grouping,
  1150.                ;; so bump our counter.
  1151.                (setq groupings (1+ groupings))))))))
  1152.     groupings))
  1153.  
  1154. (defun compilation-parse-errors (limit-search find-at-least)
  1155.   "Parse the current buffer as grep, cc or lint error messages.
  1156. See variable `compilation-parse-errors-function' for the interface it uses."
  1157.   (setq compilation-error-list nil)
  1158.   (message "Parsing error messages...")
  1159.   (let (text-buffer orig orig-expanded parent-expanded
  1160.     regexp enter-group leave-group error-group
  1161.     alist subexpr error-regexp-groups
  1162.     (found-desired nil)
  1163.     (compilation-num-errors-found 0))
  1164.  
  1165.     ;; Don't reparse messages already seen at last parse.
  1166.     (goto-char compilation-parsing-end)
  1167.     ;; Don't parse the first two lines as error messages.
  1168.     ;; This matters for grep.
  1169.     (if (bobp)
  1170.     (progn
  1171.       (forward-line 2)
  1172.       ;; Move back so point is before the newline.
  1173.       ;; This matters because some error regexps use \n instead of ^
  1174.       ;; to be faster.
  1175.       (forward-char -1)))
  1176.  
  1177.     ;; Compile all the regexps we want to search for into one.
  1178.     (setq regexp (concat "\\(" compilation-enter-directory-regexp "\\)\\|"
  1179.              "\\(" compilation-leave-directory-regexp "\\)\\|"
  1180.              "\\(" (mapconcat (function
  1181.                        (lambda (elt)
  1182.                          (concat "\\(" (car elt) "\\)")))
  1183.                       compilation-error-regexp-alist
  1184.                       "\\|") "\\)"))
  1185.  
  1186.     ;; Find out how many \(...\) groupings are in each of the regexps, and set
  1187.     ;; *-GROUP to the grouping containing each constituent regexp (whose
  1188.     ;; subgroups will come immediately thereafter) of the big regexp we have
  1189.     ;; just constructed.
  1190.     (setq enter-group 1
  1191.       leave-group (+ enter-group
  1192.              (count-regexp-groupings
  1193.               compilation-enter-directory-regexp)
  1194.              1)
  1195.       error-group (+ leave-group
  1196.              (count-regexp-groupings
  1197.               compilation-leave-directory-regexp)
  1198.              1))
  1199.  
  1200.     ;; Compile an alist (IDX FILE LINE [COL]), where IDX is the number of
  1201.     ;; the subexpression for an entire error-regexp, and FILE and LINE (and
  1202.     ;; possibly COL) are the numbers for the subexpressions giving the file
  1203.     ;; name and line number (and possibly column number).
  1204.     (setq alist (or compilation-error-regexp-alist
  1205.             (error "compilation-error-regexp-alist is empty!"))
  1206.       subexpr (1+ error-group))
  1207.     (while alist
  1208.       (setq error-regexp-groups
  1209.         (cons (list subexpr
  1210.             (+ subexpr (nth 1 (car alist)))
  1211.             (+ subexpr (nth 2 (car alist)))
  1212.             (and (nth 3 (car alist))
  1213.                  (+ subexpr (nth 3 (car alist)))))
  1214.           error-regexp-groups))
  1215.       (setq subexpr (+ subexpr 1 (count-regexp-groupings (car (car alist)))))
  1216.       (setq alist (cdr alist)))
  1217.  
  1218.     (setq orig default-directory)
  1219.     (setq orig-expanded (file-truename orig))
  1220.     (setq parent-expanded (expand-file-name "../" orig-expanded))
  1221.  
  1222.     (while (and (not found-desired)
  1223.         ;; We don't just pass LIMIT-SEARCH to re-search-forward
  1224.         ;; because we want to find matches containing LIMIT-SEARCH
  1225.         ;; but which extend past it.
  1226.         (re-search-forward regexp nil t))
  1227.  
  1228.       ;; Figure out which constituent regexp matched.
  1229.       (cond ((match-beginning enter-group)
  1230.          ;; The match was the enter-directory regexp.
  1231.          (let ((dir
  1232.             (file-name-as-directory
  1233.              (expand-file-name
  1234.               (buffer-substring (match-beginning (+ enter-group 1))
  1235.                     (match-end (+ enter-group 1)))))))
  1236.            ;; The directory name in the "entering" message
  1237.            ;; is a truename.  Try to convert it to a form
  1238.            ;; like what the user typed in.
  1239.            (setq dir
  1240.              (compile-abbreviate-directory dir orig orig-expanded
  1241.                            parent-expanded))
  1242.            (setq compilation-directory-stack
  1243.              (cons dir compilation-directory-stack))
  1244.            (and (file-directory-p dir)
  1245.             (setq default-directory dir)))
  1246.  
  1247.          (and limit-search (>= (point) limit-search)
  1248.           ;; The user wanted a specific error, and we're past it.
  1249.           ;; We do this check here (and in the leave-group case)
  1250.           ;; rather than at the end of the loop because if the last
  1251.           ;; thing seen is an error message, we must carefully
  1252.           ;; discard the last error when it is the first in a new
  1253.           ;; file (see below in the error-group case).
  1254.           (setq found-desired t)))
  1255.  
  1256.         ((match-beginning leave-group)
  1257.          ;; The match was the leave-directory regexp.
  1258.          (let ((beg (match-beginning (+ leave-group 1)))
  1259.            (stack compilation-directory-stack))
  1260.            (if beg
  1261.            (let ((dir
  1262.               (file-name-as-directory
  1263.                (expand-file-name
  1264.                 (buffer-substring beg
  1265.                           (match-end (+ leave-group
  1266.                                 1)))))))
  1267.              ;; The directory name in the "entering" message
  1268.              ;; is a truename.  Try to convert it to a form
  1269.              ;; like what the user typed in.
  1270.              (setq dir
  1271.                (compile-abbreviate-directory dir orig orig-expanded
  1272.                              parent-expanded))
  1273.              (while (and stack
  1274.                  (not (string-equal (car stack) dir)))
  1275.                (setq stack (cdr stack)))))
  1276.            (setq compilation-directory-stack (cdr stack))
  1277.            (setq stack (car compilation-directory-stack))
  1278.            (if stack
  1279.            (setq default-directory stack))
  1280.            )
  1281.  
  1282.          (and limit-search (>= (point) limit-search)
  1283.           ;; The user wanted a specific error, and we're past it.
  1284.           ;; We do this check here (and in the enter-group case)
  1285.           ;; rather than at the end of the loop because if the last
  1286.           ;; thing seen is an error message, we must carefully
  1287.           ;; discard the last error when it is the first in a new
  1288.           ;; file (see below in the error-group case).
  1289.           (setq found-desired t)))
  1290.  
  1291.         ((match-beginning error-group)
  1292.          ;; The match was the composite error regexp.
  1293.          ;; Find out which individual regexp matched.
  1294.          (setq alist error-regexp-groups)
  1295.          (while (and alist
  1296.              (null (match-beginning (car (car alist)))))
  1297.            (setq alist (cdr alist)))
  1298.          (if alist
  1299.          (setq alist (car alist))
  1300.            (error "compilation-parse-errors: impossible regexp match!"))
  1301.          
  1302.          ;; Extract the file name and line number from the error message.
  1303.          (let ((beginning-of-match (match-beginning 0)) ;looking-at nukes
  1304.            (filename (buffer-substring (match-beginning (nth 1 alist))
  1305.                            (match-end (nth 1 alist))))
  1306.            (linenum (string-to-int
  1307.                  (buffer-substring
  1308.                   (match-beginning (nth 2 alist))
  1309.                   (match-end (nth 2 alist)))))
  1310.            (column (and (nth 3 alist)
  1311.                 (string-to-int
  1312.                  (buffer-substring
  1313.                   (match-beginning (nth 3 alist))
  1314.                   (match-end (nth 3 alist)))))))
  1315.  
  1316.            ;; Check for a comint-file-name-prefix and prepend it if
  1317.            ;; appropriate.  (This is very useful for
  1318.            ;; compilation-minor-mode in an rlogin-mode buffer.)
  1319.            (and (boundp 'comint-file-name-prefix)
  1320.             ;; If the file name is relative, default-directory will
  1321.             ;; already contain the comint-file-name-prefix (done by
  1322.             ;; compile-abbreviate-directory).
  1323.             (file-name-absolute-p filename)
  1324.             (setq filename (concat comint-file-name-prefix filename)))
  1325.            (setq filename (cons default-directory filename))
  1326.  
  1327.            ;; Locate the erring file and line.
  1328.            ;; Cons a new elt onto compilation-error-list,
  1329.            ;; giving a marker for the current compilation buffer
  1330.            ;; location, and the file and line number of the error.
  1331.            (save-excursion
  1332.          (beginning-of-line 1)
  1333.          (let ((this (cons (point-marker)
  1334.                    (list filename linenum column))))
  1335.            ;; Don't add the same source line more than once.
  1336.            (if (equal (cdr this) (cdr (car compilation-error-list)))
  1337.                nil
  1338.              (setq compilation-error-list
  1339.                (cons this
  1340.                  compilation-error-list))
  1341.              (setq compilation-num-errors-found
  1342.                (1+ compilation-num-errors-found)))))
  1343.            (and (or (and find-at-least (> compilation-num-errors-found
  1344.                           find-at-least))
  1345.             (and limit-search (>= (point) limit-search)))
  1346.             ;; We have found as many new errors as the user wants,
  1347.             ;; or past the buffer position he indicated.  We
  1348.             ;; continue to parse until we have seen all the
  1349.             ;; consecutive errors in the same file, so the error
  1350.             ;; positions will be recorded as markers in this buffer
  1351.             ;; that might change.
  1352.             (cdr compilation-error-list) ; Must check at least two.
  1353.             (not (equal (car (cdr (nth 0 compilation-error-list)))
  1354.                 (car (cdr (nth 1 compilation-error-list)))))
  1355.             (progn
  1356.               ;; Discard the error just parsed, so that the next
  1357.               ;; parsing run can get it and the following errors in
  1358.               ;; the same file all at once.  If we didn't do this, we
  1359.               ;; would have the same problem we are trying to avoid
  1360.               ;; with the test above, just delayed until the next run!
  1361.               (setq compilation-error-list
  1362.                 (cdr compilation-error-list))
  1363.               (goto-char beginning-of-match)
  1364.               (setq found-desired t)))
  1365.            )
  1366.          )
  1367.         (t
  1368.          (error "compilation-parse-errors: known groups didn't match!")))
  1369.  
  1370.       (message "Parsing error messages...%d (%.0f%% of buffer)"
  1371.            compilation-num-errors-found
  1372.            ;; Use floating-point because (* 100 (point)) frequently
  1373.            ;; exceeds the range of Emacs Lisp integers.
  1374.            (/ (* 100.0 (point)) (point-max)))
  1375.  
  1376.       (and limit-search (>= (point) limit-search)
  1377.        ;; The user wanted a specific error, and we're past it.
  1378.        (setq found-desired t)))
  1379.     (setq compilation-parsing-end (if found-desired
  1380.                       (point)
  1381.                     ;; We have searched the whole buffer.
  1382.                     (point-max))))
  1383.   (setq compilation-error-list (nreverse compilation-error-list))
  1384.   (message "Parsing error messages...done"))
  1385.  
  1386. ;; If directory DIR is a subdir of ORIG or of ORIG's parent,
  1387. ;; return a relative name for it starting from ORIG or its parent.
  1388. ;; ORIG-EXPANDED is an expanded version of ORIG.
  1389. ;; PARENT-EXPANDED is an expanded version of ORIG's parent.
  1390. ;; Those two args could be computed here, but we run faster by
  1391. ;; having the caller compute them just once.
  1392. (defun compile-abbreviate-directory (dir orig orig-expanded parent-expanded)
  1393.   ;; Check for a comint-file-name-prefix and prepend it if appropriate.
  1394.   ;; (This is very useful for compilation-minor-mode in an rlogin-mode
  1395.   ;; buffer.)
  1396.   (if (boundp 'comint-file-name-prefix)
  1397.       (setq dir (concat comint-file-name-prefix dir)))
  1398.  
  1399.   (if (and (> (length dir) (length orig-expanded))
  1400.        (string= orig-expanded
  1401.             (substring dir 0 (length orig-expanded))))
  1402.       (setq dir
  1403.         (concat orig
  1404.             (substring dir (length orig-expanded)))))
  1405.   (if (and (> (length dir) (length parent-expanded))
  1406.        (string= parent-expanded
  1407.             (substring dir 0 (length parent-expanded))))
  1408.     (setq dir
  1409.       (concat (file-name-directory
  1410.            (directory-file-name orig))
  1411.           (substring dir (length parent-expanded)))))
  1412.   dir)
  1413.  
  1414. (provide 'compile)
  1415.  
  1416. ;;; compile.el ends here
  1417.