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