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