2307
|
1 ;;; cmacexp.el --- C preprocessor macro expansion
|
662
|
2
|
845
|
3 ;; Copyright (C) 1988 Free Software Foundation, Inc.
|
|
4
|
807
|
5 ;; Maintainer: FSF
|
|
6 ;; Keywords: c
|
|
7
|
315
|
8 ;; This file is part of GNU Emacs.
|
|
9
|
|
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
|
807
|
12 ;; the Free Software Foundation; either version 2, or (at your option)
|
315
|
13 ;; any later version.
|
|
14
|
|
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.
|
|
19
|
|
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.
|
|
23
|
2307
|
24 ;;; Commentary:
|
|
25
|
|
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.
|
|
30 ;;
|
|
31 ;; This package supports the following option variables:
|
|
32 ;;
|
|
33 ;; c-macro-preprocessor --- program to be used for macro expansion.
|
|
34 ;; c-macro-options --- command-line options to pass it.
|
|
35
|
807
|
36 ;;; Code:
|
315
|
37
|
|
38 (defvar c-macro-preprocessor "/lib/cpp"
|
|
39 "*Command to be used for C preprocessing.")
|
|
40
|
|
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.")
|
|
46
|
|
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'
|
612
|
51 to customize how preprocessing is done, or specify header file directories
|
|
52 and macros to predefine."
|
315
|
53 (interactive "r")
|
|
54 (let ((outbuf (get-buffer-create "*Macroexpansion*"))
|
|
55 (tempfile "%%macroexpand%%")
|
1831
|
56 expanded
|
315
|
57 process
|
|
58 last-needed)
|
1831
|
59 (setq expanded (expand-file-name tempfile))
|
315
|
60 (save-excursion
|
|
61 (set-buffer outbuf)
|
|
62 (erase-buffer))
|
|
63 (setq process (apply 'start-process "macros" outbuf c-macro-preprocessor
|
|
64 c-macro-options))
|
|
65 (set-process-sentinel process '(lambda (&rest x)))
|
|
66 (save-restriction
|
|
67 (widen)
|
|
68 (save-excursion
|
|
69 (goto-char beg)
|
|
70 (beginning-of-line)
|
|
71 (setq last-needed (point))
|
|
72 (if (re-search-backward "^[ \t]*#" nil t)
|
|
73 (progn
|
|
74 ;; Skip continued lines.
|
|
75 (while (progn (end-of-line) (= (preceding-char) ?\\))
|
|
76 (forward-line 1))
|
|
77 ;; Skip the last line of the macro definition we found.
|
|
78 (forward-line 1)
|
|
79 (setq last-needed (point)))))
|
1831
|
80 (write-region (point-min) last-needed expanded nil 'nomsg)
|
315
|
81 ;; Output comment ender in case last #-directive is inside a comment.
|
|
82 ;; Also, terminate any string that we are in.
|
1831
|
83 (write-region "*//*\"*/\n" nil expanded t 'nomsg)
|
|
84 (write-region beg end (concat expanded "x") nil 'nomsg)
|
315
|
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"))
|
1831
|
88 (process-send-eof process)
|
|
89 ;; HPUX seems to want two eofs.
|
315
|
90 (process-send-eof process))
|
|
91 (while (eq (process-status process) 'run)
|
|
92 (accept-process-output))
|
1831
|
93 (delete-file expanded)
|
|
94 (delete-file (concat expanded "x"))
|
315
|
95 (display-buffer outbuf)
|
|
96 (save-excursion
|
|
97 (set-buffer outbuf)
|
|
98 (goto-char (point-max))
|
|
99 (forward-line -1)
|
|
100 (delete-region (point) (point-max))
|
|
101 (re-search-backward "\n# 1 ")
|
|
102 (forward-line 2)
|
|
103 (while (eolp) (delete-char 1))
|
|
104 (delete-region (point-min) (point)))
|
|
105 (display-buffer outbuf)))
|
662
|
106
|
|
107 ;;; cmacexp.el ends here
|