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