annotate lisp/modes/cmacexp.el @ 48:56c54cf7c5b6 r19-16b90

Import from CVS: tag r19-16b90
author cvs
date Mon, 13 Aug 2007 08:56:04 +0200
parents ac2d302a0011
children 131b0175ea99
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; cmacexp.el --- expand C macros in a region
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
3 ;; Copyright (C) 1992, 1994, 1996 Free Software Foundation, Inc.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;; Author: Francesco Potorti` <pot@cnuce.cnr.it>
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
6 ;; Version: $Id: cmacexp.el,v 1.1.1.2 1996/12/18 03:44:32 steve Exp $
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;; Adapted-By: ESR
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 ;; Keywords: c
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 ;; This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;; XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;; under the terms of the GNU General Public License as published by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;; the Free Software Foundation; either version 2, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 ;; any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ;; XEmacs is distributed in the hope that it will be useful, but
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 ;; General Public License for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 ;; You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 ;; along with XEmacs; see the file COPYING. If not, write to the Free
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
24 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
25 ;; 02111-1307, USA.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
27 ;;; Synched up with: FSF 19.34.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ;; USAGE =============================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 ;; In C mode C-C C-e is bound to c-macro-expand. The result of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 ;; expansion is put in a separate buffer. A user option allows the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 ;; window displaying the buffer to be optimally sized.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 ;; When called with a C-u prefix, c-macro-expand replaces the selected
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 ;; region with the expansion. Both the preprocessor name and the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ;; initial flag can be set by the user. If c-macro-prompt-flag is set
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 ;; to a non-nil value the user is offered to change the options to the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 ;; preprocessor each time c-macro-expand is invoked. Preprocessor
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 ;; arguments default to the last ones entered. If c-macro-prompt-flag
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 ;; is nil, one must use M-x set-variable to set a different value for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 ;; c-macro-cppflags.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 ;; A c-macro-expansion function is provided for non-interactive use.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 ;; INSTALLATION ======================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 ;; Put the following in your ~/.emacs file.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 ;; If you want the *Macroexpansion* window to be not higher than
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 ;; necessary:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 ;;(setq c-macro-shrink-window-flag t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 ;; If you use a preprocessor other than /lib/cpp (be careful to set a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 ;; -C option or equivalent in order to make the preprocessor not to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 ;; strip the comments):
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 ;;(setq c-macro-preprocessor "gpp -C")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 ;; If you often use a particular set of flags:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 ;;(setq c-macro-cppflags "-I /usr/include/local -DDEBUG"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 ;; If you want the "Preprocessor arguments: " prompt:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 ;;(setq c-macro-prompt-flag t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 ;; BUG REPORTS =======================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 ;; Please report bugs, suggestions, complaints and so on to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 ;; pot@cnuce.cnr.it (Francesco Potorti`).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 ;; IMPROVEMENTS OVER emacs 18.xx cmacexp.el ==========================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 ;; - A lot of user and programmer visible changes. See above.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 ;; - #line directives are inserted, so __LINE__ and __FILE__ are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 ;; correctly expanded. Works even with START inside a string, a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 ;; comment or a region #ifdef'd away by cpp. cpp is invoked with -C,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 ;; making comments visible in the expansion.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 ;; - All work is done in core memory, no need for temporary files.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 ;; ACKNOWLEDGEMENTS ==================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 ;; A lot of thanks to Don Maszle who did a great work of testing, bug
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 ;; reporting and suggestion of new features. This work has been
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 ;; partially inspired by Don Maszle and Jonathan Segal's.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 ;; BUGS ==============================================================
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 ;; If the start point of the region is inside a macro definition the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 ;; macro expansion is often inaccurate.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 (require 'cc-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 (provide 'cmacexp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 (defvar c-macro-shrink-window-flag nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 "*Non-nil means shrink the *Macroexpansion* window to fit its contents.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 (defvar c-macro-prompt-flag nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 "*Non-nil makes `c-macro-expand' prompt for preprocessor arguments.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
101 (defvar c-macro-preprocessor
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
102 ;; Cannot rely on standard directory on MS-DOS to find CPP.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
103 (cond ((eq system-type 'ms-dos) "cpp -C")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
104 ;; Solaris has it in an unusual place.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
105 ((and (string-match "^[^-]*-[^-]*-\\(solaris\\|sunos5\\)"
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
106 system-configuration)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
107 (file-exists-p "/opt/SUNWspro/SC3.0.1/bin/acomp"))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
108 "/opt/SUNWspro/SC3.0.1/bin/acomp -C -E")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
109 (t "/lib/cpp -C"))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 "The preprocessor used by the cmacexp package.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 If you change this, be sure to preserve the `-C' (don't strip comments)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 option, or to set an equivalent one.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (defvar c-macro-cppflags ""
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 "*Preprocessor flags used by `c-macro-expand'.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 (defconst c-macro-buffer-name "*Macroexpansion*")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
120 ;; Autoload for XEmacs
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 (defun c-macro-expand (start end subst)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 "Expand C macros in the region, using the C preprocessor.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 Normally display output in temp buffer, but
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 prefix arg means replace the region with it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 `c-macro-preprocessor' specifies the preprocessor to use.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 Prompt for arguments to the preprocessor \(e.g. `-DDEBUG -I ./include')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 if the user option `c-macro-prompt-flag' is non-nil.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 Noninteractive args are START, END, SUBST.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 For use inside Lisp programs, see also `c-macro-expansion'."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 (interactive "r\nP")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 (let ((inbuf (current-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 (displaybuf (if subst
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 (get-buffer c-macro-buffer-name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 (get-buffer-create c-macro-buffer-name)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 (expansion ""))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 ;; Build the command string.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 (if c-macro-prompt-flag
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 (setq c-macro-cppflags
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 (read-string "Preprocessor arguments: "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 c-macro-cppflags)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 ;; Decide where to display output.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 (if (and subst
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 (and buffer-read-only (not inhibit-read-only))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 (not (eq inbuf displaybuf)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 (message
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 "Buffer is read only: displaying expansion in alternate window")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 (sit-for 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 (setq subst nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 (or displaybuf
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 (setq displaybuf (get-buffer-create c-macro-buffer-name)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 ;; Expand the macro and output it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 (setq expansion (c-macro-expansion start end
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 (concat c-macro-preprocessor " "
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 c-macro-cppflags) t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 (if subst
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 (let ((exchange (= (point) start)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 (delete-region start end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 (insert expansion)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 (if exchange
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 (exchange-point-and-mark)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 (set-buffer displaybuf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 (setq buffer-read-only nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 (buffer-disable-undo displaybuf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 (erase-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 (insert expansion)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 (set-buffer-modified-p nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 (if (string= "" expansion)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 (message "Null expansion")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 (c-macro-display-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 (setq buffer-read-only t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 (setq buffer-auto-save-file-name nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 (bury-buffer displaybuf))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 ;; Display the current buffer in a window which is either just large
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 ;; enough to contain the entire buffer, or half the size of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 ;; screen, whichever is smaller. Do not select the new
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 ;; window.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 ;; Several factors influence window resizing so that the window is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 ;; sized optimally if it is created anew, and so that it is messed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 ;; with minimally if it has been created by the user. If the window
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 ;; chosen for display exists already but contains something else, the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 ;; window is not re-sized. If the window already contains the current
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 ;; buffer, it is never shrunk, but possibly expanded. Finally, if the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 ;; variable c-macro-shrink-window-flag is nil the window size is *never*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 ;; changed.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 (defun c-macro-display-buffer ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 (c-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 (let ((oldwinheight (window-height))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 (alreadythere ;the window was already there
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 (get-buffer-window (current-buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 (popped nil)) ;the window popped changing the layout
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 (or alreadythere
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 (display-buffer (current-buffer) t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 (setq popped (/= oldwinheight (window-height)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 (if (and c-macro-shrink-window-flag ;user wants fancy shrinking :\)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 (or alreadythere popped))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 ;; Enlarge up to half screen, or shrink properly.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 (let ((oldwin (selected-window))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 (minheight 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 (maxheight 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 (select-window (get-buffer-window (current-buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 (setq minheight (if alreadythere
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 (window-height)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 window-min-height))
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
215 ;; XEmacs change
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 (setq maxheight (/ (screen-height) 2))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 (enlarge-window (- (min maxheight
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 (max minheight
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 (+ 2 (vertical-motion (point-max)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 (window-height)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 (select-window oldwin))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 (defun c-macro-expansion (start end cppcommand &optional display)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 "Run a preprocessor on region and return the output as a string.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 Expand the region between START and END in the current buffer using
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 the shell command CPPCOMMAND (e.g. \"/lib/cpp -C -DDEBUG\").
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 Be sure to use a -C (don't strip comments) or equivalent option.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 Optional arg DISPLAY non-nil means show messages in the echo area."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 ;; Copy the current buffer's contents to a temporary hidden buffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 ;; Delete from END to end of buffer. Insert a preprocessor #line
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 ;; directive at START and after each #endif following START that are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 ;; not inside a comment or a string. Put all the strings thus
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 ;; inserted (without the "line" substring) in a list named linelist.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 ;; If START is inside a comment, prepend "*/" and append "/*" to the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 ;; #line directive. If inside a string, prepend and append "\"".
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 ;; Preprocess the buffer contents, then look for all the lines stored
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 ;; in linelist starting from end of buffer. The last line so found is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 ;; where START was, so return the substring from point to end of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 ;; buffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 (let ((inbuf (current-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 (outbuf (get-buffer-create " *C Macro Expansion*"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 (filename (if (and buffer-file-name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 (string-match (regexp-quote default-directory)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 buffer-file-name))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 (substring buffer-file-name (match-end 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 (buffer-name)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 (mymsg (format "Invoking %s%s%s on region..."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 c-macro-preprocessor
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 (if (string= "" c-macro-cppflags) "" " ")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 c-macro-cppflags))
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
254 (uniquestring "??? !!! ??? start of c-macro expansion ??? !!! ???")
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 (startlinenum 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 (linenum 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 (startstat ())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 (startmarker "")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 (exit-status 0)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
260 (tempname (make-temp-name (concat
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
261 (or (getenv "TMPDIR") (getenv "TEMP")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
262 (getenv "TMP") "/tmp")
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
263 "/"))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 (unwind-protect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 (save-restriction
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 (widen)
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
268 (let ((in-syntax-table (syntax-table)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
269 (set-buffer outbuf)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
270 (setq buffer-read-only nil)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
271 (erase-buffer)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
272 (set-syntax-table in-syntax-table))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 (insert-buffer-substring inbuf 1 end))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 ;; We have copied inbuf to outbuf. Point is at end of
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
276 ;; outbuf. Inset a newline at the end, so cpp can correctly
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
277 ;; parse a token ending at END.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
278 (insert "\n")
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 ;; Save sexp status and line number at START.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 (setq startstat (parse-partial-sexp 1 start))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 (setq startlinenum (+ (count-lines 1 (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 (if (bolp) 1 0)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 ;; Now we insert the #line directives after all #endif or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 ;; #else following START going backward, so the lines we
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 ;; insert don't change the line numbers.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 ;(switch-to-buffer outbuf) (debug) ;debugging instructions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 (goto-char (point-max))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 (while (re-search-backward "\n#\\(endif\\|else\\)\\>" start 'move)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 (if (equal (nthcdr 3 (parse-partial-sexp start (point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 nil nil startstat))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 '(nil nil nil 0 nil)) ;neither in string nor in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 ;comment nor after quote
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 (goto-char (match-end 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 (setq linenum (+ startlinenum
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 (count-lines start (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 (insert (format "\n#line %d \"%s\"\n" linenum filename))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 (goto-char (match-beginning 0)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 ;; Now we are at START. Insert the first #line directive.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 ;; This must work even inside a string or comment, or after a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 ;; quote.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 (let* ((startinstring (nth 3 startstat))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 (startincomment (nth 4 startstat))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 (startafterquote (nth 5 startstat))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 (startinbcomment (nth 7 startstat)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 (insert (if startafterquote " " "")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 (cond (startinstring
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 (char-to-string startinstring))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 (startincomment "*/")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 (""))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 (setq startmarker
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 (concat "\n" uniquestring
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 (cond (startinstring
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 (char-to-string startinstring))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 (startincomment "/*")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 (startinbcomment "//"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 (if startafterquote "\\")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 (format "\n#line %d \"%s\"\n" startlinenum filename)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 ;; Call the preprocessor.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 (if display (message mymsg))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 (setq exit-status
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
326 (call-process-region 1 (point-max)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
327 shell-file-name
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
328 t (list t tempname) nil "-c"
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
329 cppcommand))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 (if display (message (concat mymsg "done")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 (if (= (buffer-size) 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 ;; Empty output is normal after a fatal error.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 (insert "\nPreprocessor produced no output\n")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 ;; Find and delete the mark of the start of the expansion.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 ;; Look for `# nn "file.c"' lines and delete them.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 (search-forward startmarker)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 (delete-region 1 (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 (while (re-search-forward (concat "^# [0-9]+ \""
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 (regexp-quote filename)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 "\"") nil t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 (beginning-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 (let ((beg (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 (forward-line 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 (delete-region beg (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 ;; If CPP got errors, show them at the beginning.
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
348 ;; MS-DOS shells don't return the exit code of their children.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
349 ;; Look at the size of the error message file instead, but
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
350 ;; don't punish those MS-DOS users who have a shell that does
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
351 ;; return an error code.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
352 (or (and (or (not (boundp 'msdos-shells))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
353 (not (member (file-name-nondirectory shell-file-name)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
354 msdos-shells)))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
355 (eq exit-status 0))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
356 (zerop (nth 7 (file-attributes (expand-file-name tempname))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 (goto-char (point-min))
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
359 ;; Put the messages inside a comment, so they won't get in
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
360 ;; the way of font-lock, highlighting etc.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
361 (insert
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
362 (format "/* Preprocessor terminated with status %s\n\n Messages from `%s\':\n\n"
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
363 exit-status cppcommand))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
364 (goto-char (+ (point)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
365 (nth 1 (insert-file-contents tempname))))
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
366 (insert "\n\n*/\n")))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 (delete-file tempname)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 ;; Compute the return value, keeping in account the space
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 ;; inserted at the end of the buffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 (buffer-substring 1 (max 1 (- (point-max) 1))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 ;; Cleanup.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 (kill-buffer outbuf))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 ;;; cmacexp.el ends here