home *** CD-ROM | disk | FTP | other *** search
/ Dream 49 / Amiga_Dream_49.iso / atari / texte / 1857bin-d2.zoo / lisp / compile.el < prev    next >
Lisp/Scheme  |  1991-12-02  |  12KB  |  309 lines

  1. ;; Run compiler as inferior of Emacs, and parse its error messages.
  2. ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
  3.  
  4. ;; This file is part of GNU Emacs.
  5.  
  6. ;; GNU Emacs is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation; either version 1, or (at your option)
  9. ;; any later version.
  10.  
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ;; GNU General Public License for more details.
  15.  
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  18. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20. (provide 'compile)
  21.  
  22. (defvar compilation-process nil
  23.   "Process created by compile command, or nil if none exists now.
  24. Note that the process may have been \"deleted\" and still
  25. be the value of this variable.")
  26.  
  27. (defvar compilation-error-list nil
  28.   "List of error message descriptors for visiting erring functions.
  29. Each error descriptor is a list of length two.
  30. Its car is a marker pointing to an error message.
  31. Its cadr is a marker pointing to the text of the line the message is about,
  32.   or nil if that is not interesting.
  33. The value may be t instead of a list;
  34. this means that the buffer of error messages should be reparsed
  35. the next time the list of errors is wanted.")
  36.  
  37. (defvar compilation-parsing-end nil
  38.   "Position of end of buffer when last error messages parsed.")
  39.  
  40. (defvar compilation-error-message nil
  41.   "Message to print when no more matches for compilation-error-regexp are found")
  42.  
  43. ;; The filename excludes colons to avoid confusion when error message
  44. ;; starts with digits.
  45. (defvar compilation-error-regexp
  46.   "\\([^ :\n]+\\(: *\\|, line \\|(\\)[0-9]+\\)\\|\\([0-9]+ *of *[^ \n]+\\)"
  47.   "Regular expression for filename/linenumber in error in compilation log.")
  48.  
  49. (defun compile (command)
  50.   "Compile the program including the current buffer.  Default: run `make'.
  51. Runs COMMAND, a shell command, in a separate process asynchronously
  52. with output going to the buffer *compilation*.
  53. You can then use the command \\[next-error] to find the next error message
  54. and move to the source code that caused it."
  55.   (interactive (list (read-string "Compile command: " compile-command)))
  56.   (setq compile-command command)
  57.   (compile1 compile-command "No more errors"))
  58.  
  59. (defun grep (command)
  60.   "Run grep, with user-specified args, and collect output in a buffer.
  61. While grep runs asynchronously, you can use the \\[next-error] command
  62. to find the text that grep hits refer to."
  63.   (interactive "sRun grep (with args): ")
  64.   (compile1 (concat "grep -n " command " /dev/null")
  65.         "No more grep hits" "grep"))
  66.  
  67. (defun compile1 (command error-message &optional name-of-mode)
  68.   (save-some-buffers)
  69.   (if compilation-process
  70.       (if (or (not (eq (process-status compilation-process) 'run))
  71.           (yes-or-no-p "A compilation process is running; kill it? "))
  72.       (condition-case ()
  73.           (let ((comp-proc compilation-process))
  74.         (interrupt-process comp-proc)
  75.         (sit-for 1)
  76.         (delete-process comp-proc))
  77.         (error nil))
  78.     (error "Cannot have two compilation processes")))
  79.   (setq compilation-process nil)
  80.   (compilation-forget-errors)
  81.   (setq compilation-error-list t)
  82.   (setq compilation-error-message error-message)
  83.   (setq compilation-process
  84.     (start-process "compilation" "*compilation*"
  85.                shell-file-name
  86.                "-c" (concat "exec " command)))
  87.   (with-output-to-temp-buffer "*compilation*"
  88.     (princ "cd ")
  89.     (princ default-directory)
  90.     (terpri)
  91.     (princ command)
  92.     (terpri))
  93.   (set-process-sentinel compilation-process 'compilation-sentinel)
  94.   (let* ((thisdir default-directory)
  95.      (outbuf (process-buffer compilation-process))
  96.      (outwin (get-buffer-window outbuf))
  97.      (regexp compilation-error-regexp))
  98.     (if (eq outbuf (current-buffer))
  99.     (goto-char (point-max)))
  100.     (save-excursion
  101.       (set-buffer outbuf)
  102.       (buffer-flush-undo outbuf)
  103.       (let ((start (save-excursion (set-buffer outbuf) (point-min))))
  104.     (set-window-start outwin start)
  105.     (or (eq outwin (selected-window))
  106.         (set-window-point outwin start)))
  107.       (setq default-directory thisdir)
  108.       (fundamental-mode)
  109.       (make-local-variable 'compilation-error-regexp)
  110.       (setq compilation-error-regexp regexp)
  111.       (setq mode-name (or name-of-mode "Compilation"))
  112.       ;; Make log buffer's mode line show process state
  113.       (setq mode-line-process '(": %s")))))
  114.  
  115. ;; Called when compilation process changes state.
  116.  
  117. (defun compilation-sentinel (proc msg)
  118.   (cond ((null (buffer-name (process-buffer proc)))
  119.      ;; buffer killed
  120.      (set-process-buffer proc nil))
  121.     ((memq (process-status proc) '(signal exit))
  122.      (let* ((obuf (current-buffer))
  123.         omax opoint)
  124.        ;; save-excursion isn't the right thing if
  125.        ;;  process-buffer is current-buffer
  126.        (unwind-protect
  127.            (progn
  128.          ;; Write something in *compilation* and hack its mode line,
  129.          (set-buffer (process-buffer proc))
  130.          (setq omax (point-max) opoint (point))
  131.          (goto-char (point-max))
  132.          (insert ?\n mode-name " " msg)
  133.          (forward-char -1)
  134.          (insert " at "
  135.              (substring (current-time-string) 0 -5))
  136.          (forward-char 1)
  137.          (setq mode-line-process
  138.                (concat ": "
  139.                    (symbol-name (process-status proc))))
  140.          ;; If buffer and mode line will show that the process
  141.          ;; is dead, we can delete it now.  Otherwise it
  142.          ;; will stay around until M-x list-processes.
  143.          (delete-process proc))
  144.          (setq compilation-process nil)
  145.          ;; Force mode line redisplay soon
  146.          (set-buffer-modified-p (buffer-modified-p)))
  147.        (if (and opoint (< opoint omax))
  148.            (goto-char opoint))
  149.        (set-buffer obuf)))))
  150.  
  151. (defun kill-compilation ()
  152.   "Kill the process made by the \\[compile] command."
  153.   (interactive)
  154.   (if compilation-process
  155.       (interrupt-process compilation-process)))
  156.  
  157. (defun kill-grep ()
  158.   "Kill the process made by the \\[grep] command."
  159.   (interactive)
  160.   (if compilation-process
  161.       (interrupt-process compilation-process)))
  162.  
  163. (defun next-error (&optional argp)
  164.   "Visit next compilation error message and corresponding source code.
  165. This operates on the output from the \\[compile] command.
  166. If all preparsed error messages have been processed,
  167. the error message buffer is checked for new ones.
  168. A non-nil argument (prefix arg, if interactive)
  169. means reparse the error message buffer and start at the first error."
  170.   (interactive "P")
  171.   (if (or (eq compilation-error-list t)
  172.       argp)
  173.       (progn (compilation-forget-errors)
  174.          (setq compilation-parsing-end 1)))
  175.   (if compilation-error-list
  176.       nil
  177.     (save-excursion
  178.       (set-buffer "*compilation*")
  179.       (set-buffer-modified-p nil)
  180.       (compilation-parse-errors)))
  181.   (let ((next-error (car compilation-error-list)))
  182.     (if (null next-error)
  183.     (error (concat compilation-error-message
  184.                (if (and compilation-process
  185.                 (eq (process-status compilation-process)
  186.                     'run))
  187.                " yet" ""))))
  188.     (setq compilation-error-list (cdr compilation-error-list))
  189.     (if (null (car (cdr next-error)))
  190.     nil
  191.       (switch-to-buffer (marker-buffer (car (cdr next-error))))
  192.       (goto-char (car (cdr next-error)))
  193.       (set-marker (car (cdr next-error)) nil))
  194.     (let* ((pop-up-windows t)
  195.        (w (display-buffer (marker-buffer (car next-error)))))
  196.       (set-window-point w (car next-error))
  197.       (set-window-start w (car next-error)))
  198.     (set-marker (car next-error) nil)))
  199.  
  200. ;; Set compilation-error-list to nil, and
  201. ;; unchain the markers that point to the error messages and their text,
  202. ;; so that they no longer slow down gap motion.
  203. ;; This would happen anyway at the next garbage collection,
  204. ;; but it is better to do it right away.
  205. (defun compilation-forget-errors ()
  206.   (if (eq compilation-error-list t)
  207.       (setq compilation-error-list nil))
  208.   (while compilation-error-list
  209.     (let ((next-error (car compilation-error-list)))
  210.       (set-marker (car next-error) nil)
  211.       (if (car (cdr next-error))
  212.       (set-marker (car (cdr next-error)) nil)))
  213.     (setq compilation-error-list (cdr compilation-error-list))))
  214.  
  215. (defun compilation-parse-errors ()
  216.   "Parse the current buffer as error messages.
  217. This makes a list of error descriptors, compilation-error-list.
  218. For each source-file, line-number pair in the buffer,
  219. the source file is read in, and the text location is saved in compilation-error-list.
  220. The function next-error, assigned to \\[next-error], takes the next error off the list
  221. and visits its location."
  222.   (setq compilation-error-list nil)
  223.   (message "Parsing error messages...")
  224.   (let (text-buffer
  225.     last-filename last-linenum)
  226.     ;; Don't reparse messages already seen at last parse.
  227.     (goto-char compilation-parsing-end)
  228.     ;; Don't parse the first two lines as error messages.
  229.     ;; This matters for grep.
  230.     (if (bobp)
  231.     (forward-line 2))
  232.     (while (re-search-forward compilation-error-regexp nil t)
  233.       (let (linenum filename
  234.         error-marker text-marker)
  235.     ;; Extract file name and line number from error message.
  236.     (save-restriction
  237.       (narrow-to-region (match-beginning 0) (match-end 0))
  238.       (goto-char (point-max))
  239.       (skip-chars-backward "[0-9]")
  240.       ;; If it's a lint message, use the last file(linenum) on the line.
  241.       ;; Normally we use the first on the line.
  242.       (if (= (preceding-char) ?\()
  243.           (progn
  244.         (narrow-to-region (point-min) (1+ (buffer-size)))
  245.         (end-of-line)
  246.         (re-search-backward compilation-error-regexp)
  247.         (skip-chars-backward "^ \t\n")
  248.         (narrow-to-region (point) (match-end 0))
  249.         (goto-char (point-max))
  250.         (skip-chars-backward "[0-9]")))
  251.       ;; Are we looking at a "filename-first" or "line-number-first" form?
  252.       (if (looking-at "[0-9]")
  253.           (progn
  254.         (setq linenum (read (current-buffer)))
  255.         (goto-char (point-min)))
  256.         ;; Line number at start, file name at end.
  257.         (progn
  258.           (goto-char (point-min))
  259.           (setq linenum (read (current-buffer)))
  260.           (goto-char (point-max))
  261.           (skip-chars-backward "^ \t\n")))
  262.       (setq filename (compilation-grab-filename)))
  263.     ;; Locate the erring file and line.
  264.     (if (and (equal filename last-filename)
  265.          (= linenum last-linenum))
  266.         nil
  267.       (beginning-of-line 1)
  268.       (setq error-marker (point-marker))
  269.       ;; text-buffer gets the buffer containing this error's file.
  270.       (if (not (equal filename last-filename))
  271.           (setq text-buffer
  272.             (and (file-exists-p (setq last-filename filename))
  273.              (find-file-noselect filename))
  274.             last-linenum 0))
  275.       (if text-buffer
  276.           ;; Go to that buffer and find the erring line.
  277.           (save-excursion
  278.         (set-buffer text-buffer)
  279.         (if (zerop last-linenum)
  280.             (progn
  281.               (goto-char 1)
  282.               (setq last-linenum 1)))
  283.         (forward-line (- linenum last-linenum))
  284.         (setq last-linenum linenum)
  285.         (setq text-marker (point-marker))
  286.         (setq compilation-error-list
  287.               (cons (list error-marker text-marker)
  288.                 compilation-error-list)))))
  289.     (forward-line 1)))
  290.     (setq compilation-parsing-end (point-max)))
  291.   (message "Parsing error messages...done")
  292.   (setq compilation-error-list (nreverse compilation-error-list)))
  293.  
  294. (defun compilation-grab-filename ()
  295.   "Return a string which is a filename, starting at point.
  296. Ignore quotes and parentheses around it, as well as trailing colons."
  297.   (if (eq (following-char) ?\")
  298.       (save-restriction
  299.     (narrow-to-region (point)
  300.               (progn (forward-sexp 1) (point)))
  301.     (goto-char (point-min))
  302.     (read (current-buffer)))
  303.     (buffer-substring (point)
  304.               (progn
  305.             (skip-chars-forward "^ :,\n\t(")
  306.             (point)))))
  307.  
  308. (define-key ctl-x-map "`" 'next-error)
  309.