aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lisp/progmodes/cmacexp.el83
1 files changed, 83 insertions, 0 deletions
diff --git a/lisp/progmodes/cmacexp.el b/lisp/progmodes/cmacexp.el
new file mode 100644
index 00000000000..ae77749a020
--- /dev/null
+++ b/lisp/progmodes/cmacexp.el
@@ -0,0 +1,83 @@
1;; C macro expansion
2;; Copyright (C) 1988 Free Software Foundation, Inc.
3
4;; This file is part of GNU Emacs.
5
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.
10
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.
15
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.
19
20
21(defvar c-macro-preprocessor "/lib/cpp"
22 "*Command to be used for C preprocessing.")
23
24(defvar c-macro-options nil
25 "*List of options to use in C preprocessing.
26Each string in the list becomes a separate argument to the preprocessor.
27These arguments precede the filename.
28Use the `-I' option here to specify directories for header files.")
29
30(defun c-macro-expand (beg end)
31 "Display the result of expanding all C macros occurring in the region.
32The expansion is entirely correct because it uses the C preprocessor.
33You can use the variables `c-macro-preprocessor' and `c-macro-options'
34to customize how preprocessing is done, or specify header file directories."
35 (interactive "r")
36 (let ((outbuf (get-buffer-create "*Macroexpansion*"))
37 (tempfile "%%macroexpand%%")
38 process
39 last-needed)
40 (save-excursion
41 (set-buffer outbuf)
42 (erase-buffer))
43 (setq process (apply 'start-process "macros" outbuf c-macro-preprocessor
44 c-macro-options))
45 (set-process-sentinel process '(lambda (&rest x)))
46 (save-restriction
47 (widen)
48 (save-excursion
49 (goto-char beg)
50 (beginning-of-line)
51 (setq last-needed (point))
52 (if (re-search-backward "^[ \t]*#" nil t)
53 (progn
54 ;; Skip continued lines.
55 (while (progn (end-of-line) (= (preceding-char) ?\\))
56 (forward-line 1))
57 ;; Skip the last line of the macro definition we found.
58 (forward-line 1)
59 (setq last-needed (point)))))
60 (write-region (point-min) last-needed tempfile nil 'nomsg)
61 ;; Output comment ender in case last #-directive is inside a comment.
62 ;; Also, terminate any string that we are in.
63 (write-region "*//*\"*/\n" nil tempfile t 'nomsg)
64 (write-region beg end (concat tempfile "x") nil 'nomsg)
65 (process-send-string process (concat "#include \"" tempfile "\"\n"))
66 (process-send-string process "\n")
67 (process-send-string process (concat "#include \"" tempfile "x\"\n"))
68 (process-send-eof process))
69 (while (eq (process-status process) 'run)
70 (accept-process-output))
71 (delete-file tempfile)
72 (delete-file (concat tempfile "x"))
73 (display-buffer outbuf)
74 (save-excursion
75 (set-buffer outbuf)
76 (goto-char (point-max))
77 (forward-line -1)
78 (delete-region (point) (point-max))
79 (re-search-backward "\n# 1 ")
80 (forward-line 2)
81 (while (eolp) (delete-char 1))
82 (delete-region (point-min) (point)))
83 (display-buffer outbuf)))