1 ;;; ppexpand.el --- temporarily expanding macros in a pretty way.
3 ;; Copyright (C) 2000, 2006 Free Software Foundation, Inc.
5 ;;;; This library is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU Lesser General Public
7 ;;;; License as published by the Free Software Foundation; either
8 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;; This library is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; Lesser General Public License for more details.
15 ;;;; You should have received a copy of the GNU Lesser General Public
16 ;;;; License along with this library; if not, write to the Free
17 ;;;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 ;;; Author: Mikael Djurfeldt <djurfeldt@nada.kth.se>
24 ;; Commands for editing multiline ANSI C compatible string literals.
29 (defvar if-mark
"#if 0 /* PPEXPAND */")
30 (defvar else-mark
"#else /* PPEXPAND */")
31 (defvar endif-mark
"#endif /* PPEXPAND */")
33 (define-key c-mode-map
"\C-ce" 'ppexpand
)
35 (defun ppexpand (start end
&optional undo
)
36 "Expand C macros in the region, using the C preprocessor.
37 The expanded code is run through the `indent' command and inserted
38 into the program next to the original code, using an #if/#else/#endif
41 Given a prefix argument, it reverts the change, removing the
42 #if/#else/#endif construct and the expanded code.
44 `c-macro-preprocessor' specifies the preprocessor to use.
45 Prompt for arguments to the preprocessor \(e.g. `-DDEBUG -I ./include')
46 if the user option `c-macro-prompt-flag' is non-nil.
48 Noninteractive args are START, END, UNDO.
49 For use inside Lisp programs, see also `c-macro-expansion'."
51 (interactive (if current-prefix-arg
57 (list pos2 pos1 nil
)))))
58 (let ((inbuf (current-buffer)))
59 ;; Build the command string.
60 (if c-macro-prompt-flag
61 (setq c-macro-cppflags
62 (read-string "Preprocessor arguments: "
65 (let ((pos (point)) if-pos else-pos endif-pos
)
68 (if (not (and (setq if-pos
(search-backward if-mark nil t
))
69 (setq else-pos
(search-forward else-mark nil t
))
70 (setq endif-pos
(search-forward endif-mark nil t
))
73 (error "Not in ppexpanded region"))
74 (let ((orig (buffer-substring (+ if-pos
(length if-mark
) 1)
75 (- else-pos
(length else-mark
)))))
76 (delete-region if-pos
(+ endif-pos
1))
79 (let* ((expansion (c-macro-expansion start end
80 (concat c-macro-preprocessor
" "
82 (orig (buffer-substring start end
)))
86 (call-process-region (point-min) (point-max) "indent"
88 t
;output --> current buffer
91 (delete-region start end
)
92 (insert if-mark ?
\n orig else-mark ?
\n expansion endif-mark ?
\n)))))