*** empty log message ***
[bpt/emacs.git] / lisp / progmodes / cmacexp.el
CommitLineData
c0274f38
ER
1;;; cmacexp.el --- C macro expansion
2
e5167999
ER
3;; Maintainer: FSF
4;; Last-Modified: 17 Apr 1992
5;; Keywords: c
6
c17d15d0
JB
7;; Copyright (C) 1988 Free Software Foundation, Inc.
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
e5167999 13;; the Free Software Foundation; either version 2, or (at your option)
c17d15d0
JB
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to
23;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
e5167999 25;;; Code:
c17d15d0
JB
26
27(defvar c-macro-preprocessor "/lib/cpp"
28 "*Command to be used for C preprocessing.")
29
30(defvar c-macro-options nil
31 "*List of options to use in C preprocessing.
32Each string in the list becomes a separate argument to the preprocessor.
33These arguments precede the filename.
34Use the `-I' option here to specify directories for header files.")
35
36(defun c-macro-expand (beg end)
37 "Display the result of expanding all C macros occurring in the region.
38The expansion is entirely correct because it uses the C preprocessor.
39You can use the variables `c-macro-preprocessor' and `c-macro-options'
46450482
RS
40to customize how preprocessing is done, or specify header file directories
41and macros to predefine."
c17d15d0
JB
42 (interactive "r")
43 (let ((outbuf (get-buffer-create "*Macroexpansion*"))
44 (tempfile "%%macroexpand%%")
45 process
46 last-needed)
47 (save-excursion
48 (set-buffer outbuf)
49 (erase-buffer))
50 (setq process (apply 'start-process "macros" outbuf c-macro-preprocessor
51 c-macro-options))
52 (set-process-sentinel process '(lambda (&rest x)))
53 (save-restriction
54 (widen)
55 (save-excursion
56 (goto-char beg)
57 (beginning-of-line)
58 (setq last-needed (point))
59 (if (re-search-backward "^[ \t]*#" nil t)
60 (progn
61 ;; Skip continued lines.
62 (while (progn (end-of-line) (= (preceding-char) ?\\))
63 (forward-line 1))
64 ;; Skip the last line of the macro definition we found.
65 (forward-line 1)
66 (setq last-needed (point)))))
67 (write-region (point-min) last-needed tempfile nil 'nomsg)
68 ;; Output comment ender in case last #-directive is inside a comment.
69 ;; Also, terminate any string that we are in.
70 (write-region "*//*\"*/\n" nil tempfile t 'nomsg)
71 (write-region beg end (concat tempfile "x") nil 'nomsg)
72 (process-send-string process (concat "#include \"" tempfile "\"\n"))
73 (process-send-string process "\n")
74 (process-send-string process (concat "#include \"" tempfile "x\"\n"))
75 (process-send-eof process))
76 (while (eq (process-status process) 'run)
77 (accept-process-output))
78 (delete-file tempfile)
79 (delete-file (concat tempfile "x"))
80 (display-buffer outbuf)
81 (save-excursion
82 (set-buffer outbuf)
83 (goto-char (point-max))
84 (forward-line -1)
85 (delete-region (point) (point-max))
86 (re-search-backward "\n# 1 ")
87 (forward-line 2)
88 (while (eolp) (delete-char 1))
89 (delete-region (point-min) (point)))
90 (display-buffer outbuf)))
c0274f38
ER
91
92;;; cmacexp.el ends here