annotate lisp/progmodes/cmacexp.el @ 394:5bd386d1a1cc

Initial revision
author Jim Blandy <jimb@redhat.com>
date Thu, 15 Aug 1991 21:29:06 +0000
parents faf70884106b
children 68ef62058595
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
315
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
1 ;; C macro expansion
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
2 ;; Copyright (C) 1988 Free Software Foundation, Inc.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
3
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
4 ;; This file is part of GNU Emacs.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
5
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
6 ;; GNU Emacs is free software; you can redistribute it and/or modify
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
7 ;; it under the terms of the GNU General Public License as published by
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
8 ;; the Free Software Foundation; either version 1, or (at your option)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
9 ;; any later version.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
10
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
11 ;; GNU Emacs is distributed in the hope that it will be useful,
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
12 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
14 ;; GNU General Public License for more details.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
15
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
16 ;; You should have received a copy of the GNU General Public License
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
17 ;; along with GNU Emacs; see the file COPYING. If not, write to
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
18 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
19
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
20
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
21 (defvar c-macro-preprocessor "/lib/cpp"
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
22 "*Command to be used for C preprocessing.")
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
23
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
24 (defvar c-macro-options nil
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
25 "*List of options to use in C preprocessing.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
26 Each string in the list becomes a separate argument to the preprocessor.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
27 These arguments precede the filename.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
28 Use the `-I' option here to specify directories for header files.")
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
29
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
30 (defun c-macro-expand (beg end)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
31 "Display the result of expanding all C macros occurring in the region.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
32 The expansion is entirely correct because it uses the C preprocessor.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
33 You can use the variables `c-macro-preprocessor' and `c-macro-options'
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
34 to customize how preprocessing is done, or specify header file directories."
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
35 (interactive "r")
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
36 (let ((outbuf (get-buffer-create "*Macroexpansion*"))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
37 (tempfile "%%macroexpand%%")
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
38 process
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
39 last-needed)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
40 (save-excursion
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
41 (set-buffer outbuf)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
42 (erase-buffer))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
43 (setq process (apply 'start-process "macros" outbuf c-macro-preprocessor
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
44 c-macro-options))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
45 (set-process-sentinel process '(lambda (&rest x)))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
46 (save-restriction
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
47 (widen)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
48 (save-excursion
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
49 (goto-char beg)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
50 (beginning-of-line)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
51 (setq last-needed (point))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
52 (if (re-search-backward "^[ \t]*#" nil t)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
53 (progn
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
54 ;; Skip continued lines.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
55 (while (progn (end-of-line) (= (preceding-char) ?\\))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
56 (forward-line 1))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
57 ;; Skip the last line of the macro definition we found.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
58 (forward-line 1)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
59 (setq last-needed (point)))))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
60 (write-region (point-min) last-needed tempfile nil 'nomsg)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
61 ;; Output comment ender in case last #-directive is inside a comment.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
62 ;; Also, terminate any string that we are in.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
63 (write-region "*//*\"*/\n" nil tempfile t 'nomsg)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
64 (write-region beg end (concat tempfile "x") nil 'nomsg)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
65 (process-send-string process (concat "#include \"" tempfile "\"\n"))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
66 (process-send-string process "\n")
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
67 (process-send-string process (concat "#include \"" tempfile "x\"\n"))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
68 (process-send-eof process))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
69 (while (eq (process-status process) 'run)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
70 (accept-process-output))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
71 (delete-file tempfile)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
72 (delete-file (concat tempfile "x"))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
73 (display-buffer outbuf)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
74 (save-excursion
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
75 (set-buffer outbuf)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
76 (goto-char (point-max))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
77 (forward-line -1)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
78 (delete-region (point) (point-max))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
79 (re-search-backward "\n# 1 ")
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
80 (forward-line 2)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
81 (while (eolp) (delete-char 1))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
82 (delete-region (point-min) (point)))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
83 (display-buffer outbuf)))