1 ;;; ppexpand.el --- temporarily expanding macros in a pretty way.
3 ;; Copyright (C) 2000, 2006 Free Software Foundation, Inc.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; GNU Emacs is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to the
19 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 ;; Boston, MA 02110-1301, USA.
22 ;;; Author: Mikael Djurfeldt <djurfeldt@nada.kth.se>
26 ;; Commands for editing multiline ANSI C compatible string literals.
31 (defvar if-mark "#if 0 /* PPEXPAND */")
32 (defvar else-mark "#else /* PPEXPAND */")
33 (defvar endif-mark "#endif /* PPEXPAND */")
35 (define-key c-mode-map "\C-ce" 'ppexpand)
37 (defun ppexpand (start end &optional undo)
38 "Expand C macros in the region, using the C preprocessor.
39 The expanded code is run through the `indent' command and inserted
40 into the program next to the original code, using an #if/#else/#endif
43 Given a prefix argument, it reverts the change, removing the
44 #if/#else/#endif construct and the expanded code.
46 `c-macro-preprocessor' specifies the preprocessor to use.
47 Prompt for arguments to the preprocessor \(e.g. `-DDEBUG -I ./include')
48 if the user option `c-macro-prompt-flag' is non-nil.
50 Noninteractive args are START, END, UNDO.
51 For use inside Lisp programs, see also `c-macro-expansion'."
53 (interactive (if current-prefix-arg
59 (list pos2 pos1 nil)))))
60 (let ((inbuf (current-buffer)))
61 ;; Build the command string.
62 (if c-macro-prompt-flag
63 (setq c-macro-cppflags
64 (read-string "Preprocessor arguments: "
67 (let ((pos (point)) if-pos else-pos endif-pos)
70 (if (not (and (setq if-pos (search-backward if-mark nil t))
71 (setq else-pos (search-forward else-mark nil t))
72 (setq endif-pos (search-forward endif-mark nil t))
75 (error "Not in ppexpanded region"))
76 (let ((orig (buffer-substring (+ if-pos (length if-mark) 1)
77 (- else-pos (length else-mark)))))
78 (delete-region if-pos (+ endif-pos 1))
81 (let* ((expansion (c-macro-expansion start end
82 (concat c-macro-preprocessor " "
84 (orig (buffer-substring start end)))
88 (call-process-region (point-min) (point-max) "indent"
90 t ;output --> current buffer
93 (delete-region start end)
94 (insert if-mark ?\n orig else-mark ?\n expansion endif-mark ?\n)))))