1 ;;; cmacexp.el --- C preprocessor macro expansion
3 ;; Copyright (C) 1988 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;; This package gives you the ability to run the C macro preprocessor
27 ;; on the current region, expanding macros within it. This can be useful
28 ;; when you're not sure of the value or expansion of such macros and want
29 ;; to make sure they're doing what you think they're doing.
31 ;; This package supports the following option variables:
33 ;; c-macro-preprocessor --- program to be used for macro expansion.
34 ;; c-macro-options --- command-line options to pass it.
38 (defvar c-macro-preprocessor
"/lib/cpp"
39 "*Command to be used for C preprocessing.")
41 (defvar c-macro-options nil
42 "*List of options to use in C preprocessing.
43 Each string in the list becomes a separate argument to the preprocessor.
44 These arguments precede the filename.
45 Use the `-I' option here to specify directories for header files.")
47 (defun c-macro-expand (beg end
)
48 "Display the result of expanding all C macros occurring in the region.
49 The expansion is entirely correct because it uses the C preprocessor.
50 You can use the variables `c-macro-preprocessor' and `c-macro-options'
51 to customize how preprocessing is done, or specify header file directories
52 and macros to predefine."
54 (let ((outbuf (get-buffer-create "*Macroexpansion*"))
55 (tempfile "%%macroexpand%%")
59 (setq expanded
(expand-file-name tempfile
))
63 (setq process
(apply 'start-process
"macros" outbuf c-macro-preprocessor
65 (set-process-sentinel process
'(lambda (&rest x
)))
71 (setq last-needed
(point))
72 (if (re-search-backward "^[ \t]*#" nil t
)
74 ;; Skip continued lines.
75 (while (progn (end-of-line) (= (preceding-char) ?
\\))
77 ;; Skip the last line of the macro definition we found.
79 (setq last-needed
(point)))))
80 (write-region (point-min) last-needed expanded nil
'nomsg
)
81 ;; Output comment ender in case last #-directive is inside a comment.
82 ;; Also, terminate any string that we are in.
83 (write-region "*//*\"*/\n" nil expanded t
'nomsg
)
84 (write-region beg end
(concat expanded
"x") nil
'nomsg
)
85 (process-send-string process
(concat "#include \"" tempfile
"\"\n"))
86 (process-send-string process
"\n")
87 (process-send-string process
(concat "#include \"" tempfile
"x\"\n"))
88 (process-send-eof process
)
89 ;; HPUX seems to want two eofs.
90 (process-send-eof process
))
91 (while (eq (process-status process
) 'run
)
92 (accept-process-output))
93 (delete-file expanded
)
94 (delete-file (concat expanded
"x"))
95 (display-buffer outbuf
)
98 (goto-char (point-max))
100 (delete-region (point) (point-max))
101 (re-search-backward "\n# 1 ")
103 (while (eolp) (delete-char 1))
104 (delete-region (point-min) (point)))
105 (display-buffer outbuf
)))
107 ;;; cmacexp.el ends here