*** empty log message ***
[emacs.git] / lisp / progmodes / cmacexp.el
blobf60c2254f5a4ab4e7dce4e5893685a8ec820eda4
1 ;; C macro expansion
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)
9 ;; any later version.
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."
36 (interactive "r")
37 (let ((outbuf (get-buffer-create "*Macroexpansion*"))
38 (tempfile "%%macroexpand%%")
39 process
40 last-needed)
41 (save-excursion
42 (set-buffer outbuf)
43 (erase-buffer))
44 (setq process (apply 'start-process "macros" outbuf c-macro-preprocessor
45 c-macro-options))
46 (set-process-sentinel process '(lambda (&rest x)))
47 (save-restriction
48 (widen)
49 (save-excursion
50 (goto-char beg)
51 (beginning-of-line)
52 (setq last-needed (point))
53 (if (re-search-backward "^[ \t]*#" nil t)
54 (progn
55 ;; Skip continued lines.
56 (while (progn (end-of-line) (= (preceding-char) ?\\))
57 (forward-line 1))
58 ;; Skip the last line of the macro definition we found.
59 (forward-line 1)
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)
75 (save-excursion
76 (set-buffer outbuf)
77 (goto-char (point-max))
78 (forward-line -1)
79 (delete-region (point) (point-max))
80 (re-search-backward "\n# 1 ")
81 (forward-line 2)
82 (while (eolp) (delete-char 1))
83 (delete-region (point-min) (point)))
84 (display-buffer outbuf)))