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