home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / modes / cmacexp.el < prev    next >
Encoding:
Text File  |  1992-06-29  |  2.7 KB  |  72 lines

  1. ;; C macro expansion
  2. ;; Copyright (C) 1988 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.  
  21. (defun c-macro-expand (beg end)
  22.   "Display the result of expanding all C macros occurring in the region.
  23. The expansion is entirely correct because it uses the C preprocessor."
  24.   (interactive "r")
  25.   (let ((outbuf (get-buffer-create "*Macroexpansion*"))
  26.     (tempfile "%%macroexpand%%")
  27.     process
  28.     last-needed)
  29.     (save-excursion
  30.       (set-buffer outbuf)
  31.       (erase-buffer))
  32.     (setq process (start-process "macros" outbuf "/lib/cpp"))
  33.     (set-process-sentinel process '(lambda (&rest x)))
  34.     (save-restriction
  35.       (widen)
  36.       (save-excursion
  37.     (goto-char beg)
  38.     (beginning-of-line)
  39.     (setq last-needed (point))
  40.     (if (re-search-backward "^[ \t]*#" nil t)
  41.         (progn
  42.           ;; Skip continued lines.
  43.           (while (progn (end-of-line) (= (preceding-char) ?\\))
  44.         (forward-line 1))
  45.           ;; Skip the last line of the macro definition we found.
  46.           (forward-line 1)
  47.           (setq last-needed (point)))))
  48.       (write-region (point-min) last-needed tempfile nil 'nomsg)
  49.       ;; Output comment ender in case last #-directive is inside a comment.
  50.       ;; Also, terminate any string that we are in.
  51.       (write-region "*//*\"*/\n" nil tempfile t 'nomsg)
  52.       (write-region beg end (concat tempfile "x") nil 'nomsg)
  53.       (process-send-string process (concat "#include \"" tempfile "\"\n"))
  54.       (process-send-string process "\n")
  55.       (process-send-string process (concat "#include \"" tempfile "x\"\n"))
  56.       (process-send-eof process))
  57.     (while (eq (process-status process) 'run)
  58.       (accept-process-output))
  59.     (delete-file tempfile)
  60.     (delete-file (concat tempfile "x"))
  61.     (display-buffer outbuf)
  62.     (save-excursion
  63.       (set-buffer outbuf)
  64.       (goto-char (point-max))
  65.       (forward-line -1)
  66.       (delete-region (point) (point-max))
  67.       (re-search-backward "\n# 1 ")
  68.       (forward-line 2)
  69.       (while (eolp) (delete-char 1))
  70.       (delete-region (point-min) (point)))
  71.     (display-buffer outbuf)))
  72.