2 ;; Copyright (C) 1988 Free Software Foundation, Inc.
4 ;; This file is part of GNU Emacs.
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)
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.
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.
21 (defvar c-macro-preprocessor
"/lib/cpp"
22 "*Command to be used for C preprocessing.")
24 (defvar c-macro-options nil
25 "*List of options to use in C preprocessing.
26 Each string in the list becomes a separate argument to the preprocessor.
27 These arguments precede the filename.
28 Use the `-I' option here to specify directories for header files.")
30 (defun c-macro-expand (beg end
)
31 "Display the result of expanding all C macros occurring in the region.
32 The expansion is entirely correct because it uses the C preprocessor.
33 You can use the variables `c-macro-preprocessor' and `c-macro-options'
34 to customize how preprocessing is done, or specify header file directories
35 and macros to predefine."
37 (let ((outbuf (get-buffer-create "*Macroexpansion*"))
38 (tempfile "%%macroexpand%%")
44 (setq process
(apply 'start-process
"macros" outbuf c-macro-preprocessor
46 (set-process-sentinel process
'(lambda (&rest x
)))
52 (setq last-needed
(point))
53 (if (re-search-backward "^[ \t]*#" nil t
)
55 ;; Skip continued lines.
56 (while (progn (end-of-line) (= (preceding-char) ?
\\))
58 ;; Skip the last line of the macro definition we found.
60 (setq last-needed
(point)))))
61 (write-region (point-min) last-needed tempfile nil
'nomsg
)
62 ;; Output comment ender in case last #-directive is inside a comment.
63 ;; Also, terminate any string that we are in.
64 (write-region "*//*\"*/\n" nil tempfile t
'nomsg
)
65 (write-region beg end
(concat tempfile
"x") nil
'nomsg
)
66 (process-send-string process
(concat "#include \"" tempfile
"\"\n"))
67 (process-send-string process
"\n")
68 (process-send-string process
(concat "#include \"" tempfile
"x\"\n"))
69 (process-send-eof process
))
70 (while (eq (process-status process
) 'run
)
71 (accept-process-output))
72 (delete-file tempfile
)
73 (delete-file (concat tempfile
"x"))
74 (display-buffer outbuf
)
77 (goto-char (point-max))
79 (delete-region (point) (point-max))
80 (re-search-backward "\n# 1 ")
82 (while (eolp) (delete-char 1))
83 (delete-region (point-min) (point)))
84 (display-buffer outbuf
)))