annotate lisp/progmodes/cmacexp.el @ 2478:fb350430116e

(compile-internal): Initialize the process-mark.
author Roland McGrath <roland@gnu.org>
date Wed, 07 Apr 1993 20:58:20 +0000
parents 10e417efb12a
children c381f562cffc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2307
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 1831
diff changeset
1 ;;; cmacexp.el --- C preprocessor macro expansion
662
8a533acedb77 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 612
diff changeset
2
845
213978acbc1e entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 807
diff changeset
3 ;; Copyright (C) 1988 Free Software Foundation, Inc.
213978acbc1e entered into RCS
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 807
diff changeset
4
807
4f28bd14272c *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 662
diff changeset
5 ;; Maintainer: FSF
4f28bd14272c *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 662
diff changeset
6 ;; Keywords: c
4f28bd14272c *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 662
diff changeset
7
315
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
8 ;; This file is part of GNU Emacs.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
9
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
11 ;; it under the terms of the GNU General Public License as published by
807
4f28bd14272c *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 662
diff changeset
12 ;; the Free Software Foundation; either version 2, or (at your option)
315
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
13 ;; any later version.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
14
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
15 ;; GNU Emacs is distributed in the hope that it will be useful,
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
18 ;; GNU General Public License for more details.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
19
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
20 ;; You should have received a copy of the GNU General Public License
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
23
2307
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 1831
diff changeset
24 ;;; Commentary:
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 1831
diff changeset
25
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 1831
diff changeset
26 ;; This package gives you the ability to run the C macro preprocessor
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 1831
diff changeset
27 ;; on the current region, expanding macros within it. This can be useful
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 1831
diff changeset
28 ;; when you're not sure of the value or expansion of such macros and want
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 1831
diff changeset
29 ;; to make sure they're doing what you think they're doing.
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 1831
diff changeset
30 ;;
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 1831
diff changeset
31 ;; This package supports the following option variables:
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 1831
diff changeset
32 ;;
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 1831
diff changeset
33 ;; c-macro-preprocessor --- program to be used for macro expansion.
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 1831
diff changeset
34 ;; c-macro-options --- command-line options to pass it.
10e417efb12a Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 1831
diff changeset
35
807
4f28bd14272c *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 662
diff changeset
36 ;;; Code:
315
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
37
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
38 (defvar c-macro-preprocessor "/lib/cpp"
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
39 "*Command to be used for C preprocessing.")
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
40
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
41 (defvar c-macro-options nil
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
42 "*List of options to use in C preprocessing.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
43 Each string in the list becomes a separate argument to the preprocessor.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
44 These arguments precede the filename.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
45 Use the `-I' option here to specify directories for header files.")
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
46
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
47 (defun c-macro-expand (beg end)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
48 "Display the result of expanding all C macros occurring in the region.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
49 The expansion is entirely correct because it uses the C preprocessor.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
50 You can use the variables `c-macro-preprocessor' and `c-macro-options'
612
68ef62058595 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 315
diff changeset
51 to customize how preprocessing is done, or specify header file directories
68ef62058595 *** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents: 315
diff changeset
52 and macros to predefine."
315
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
53 (interactive "r")
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
54 (let ((outbuf (get-buffer-create "*Macroexpansion*"))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
55 (tempfile "%%macroexpand%%")
1831
1c7d06764d0d (c-macro-expand): Use expanded name to write or delete.
Richard M. Stallman <rms@gnu.org>
parents: 845
diff changeset
56 expanded
315
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
57 process
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
58 last-needed)
1831
1c7d06764d0d (c-macro-expand): Use expanded name to write or delete.
Richard M. Stallman <rms@gnu.org>
parents: 845
diff changeset
59 (setq expanded (expand-file-name tempfile))
315
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
60 (save-excursion
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
61 (set-buffer outbuf)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
62 (erase-buffer))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
63 (setq process (apply 'start-process "macros" outbuf c-macro-preprocessor
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
64 c-macro-options))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
65 (set-process-sentinel process '(lambda (&rest x)))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
66 (save-restriction
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
67 (widen)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
68 (save-excursion
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
69 (goto-char beg)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
70 (beginning-of-line)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
71 (setq last-needed (point))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
72 (if (re-search-backward "^[ \t]*#" nil t)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
73 (progn
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
74 ;; Skip continued lines.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
75 (while (progn (end-of-line) (= (preceding-char) ?\\))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
76 (forward-line 1))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
77 ;; Skip the last line of the macro definition we found.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
78 (forward-line 1)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
79 (setq last-needed (point)))))
1831
1c7d06764d0d (c-macro-expand): Use expanded name to write or delete.
Richard M. Stallman <rms@gnu.org>
parents: 845
diff changeset
80 (write-region (point-min) last-needed expanded nil 'nomsg)
315
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
81 ;; Output comment ender in case last #-directive is inside a comment.
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
82 ;; Also, terminate any string that we are in.
1831
1c7d06764d0d (c-macro-expand): Use expanded name to write or delete.
Richard M. Stallman <rms@gnu.org>
parents: 845
diff changeset
83 (write-region "*//*\"*/\n" nil expanded t 'nomsg)
1c7d06764d0d (c-macro-expand): Use expanded name to write or delete.
Richard M. Stallman <rms@gnu.org>
parents: 845
diff changeset
84 (write-region beg end (concat expanded "x") nil 'nomsg)
315
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
85 (process-send-string process (concat "#include \"" tempfile "\"\n"))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
86 (process-send-string process "\n")
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
87 (process-send-string process (concat "#include \"" tempfile "x\"\n"))
1831
1c7d06764d0d (c-macro-expand): Use expanded name to write or delete.
Richard M. Stallman <rms@gnu.org>
parents: 845
diff changeset
88 (process-send-eof process)
1c7d06764d0d (c-macro-expand): Use expanded name to write or delete.
Richard M. Stallman <rms@gnu.org>
parents: 845
diff changeset
89 ;; HPUX seems to want two eofs.
315
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
90 (process-send-eof process))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
91 (while (eq (process-status process) 'run)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
92 (accept-process-output))
1831
1c7d06764d0d (c-macro-expand): Use expanded name to write or delete.
Richard M. Stallman <rms@gnu.org>
parents: 845
diff changeset
93 (delete-file expanded)
1c7d06764d0d (c-macro-expand): Use expanded name to write or delete.
Richard M. Stallman <rms@gnu.org>
parents: 845
diff changeset
94 (delete-file (concat expanded "x"))
315
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
95 (display-buffer outbuf)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
96 (save-excursion
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
97 (set-buffer outbuf)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
98 (goto-char (point-max))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
99 (forward-line -1)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
100 (delete-region (point) (point-max))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
101 (re-search-backward "\n# 1 ")
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
102 (forward-line 2)
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
103 (while (eolp) (delete-char 1))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
104 (delete-region (point-min) (point)))
faf70884106b Initial revision
Jim Blandy <jimb@redhat.com>
parents:
diff changeset
105 (display-buffer outbuf)))
662
8a533acedb77 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 612
diff changeset
106
8a533acedb77 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 612
diff changeset
107 ;;; cmacexp.el ends here