home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / packages / compile.el < prev    next >
Encoding:
Text File  |  1995-08-04  |  59.7 KB  |  1,569 lines

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