elisp @@ macro
[bpt/guile.git] / emacs / ppexpand.el
CommitLineData
752fd3b4
MD
1;;; ppexpand.el --- temporarily expanding macros in a pretty way.
2
6e7d5622 3;; Copyright (C) 2000, 2006 Free Software Foundation, Inc.
752fd3b4 4
53befeb7
NJ
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.
9;;;;
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.
14;;;;
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
18;;;; 02111-1307 USA
752fd3b4
MD
19
20;;; Author: Mikael Djurfeldt <djurfeldt@nada.kth.se>
21
22;;; Commentary:
23
24;; Commands for editing multiline ANSI C compatible string literals.
25
26;;; Code:
27(require 'cmacexp)
28
29(defvar if-mark "#if 0 /* PPEXPAND */")
30(defvar else-mark "#else /* PPEXPAND */")
31(defvar endif-mark "#endif /* PPEXPAND */")
32
33(define-key c-mode-map "\C-ce" 'ppexpand)
34
35(defun ppexpand (start end &optional undo)
36 "Expand C macros in the region, using the C preprocessor.
37The expanded code is run through the `indent' command and inserted
38into the program next to the original code, using an #if/#else/#endif
39construct.
40
41Given a prefix argument, it reverts the change, removing the
42#if/#else/#endif construct and the expanded code.
43
44`c-macro-preprocessor' specifies the preprocessor to use.
45Prompt for arguments to the preprocessor \(e.g. `-DDEBUG -I ./include')
46if the user option `c-macro-prompt-flag' is non-nil.
47
48Noninteractive args are START, END, UNDO.
49For use inside Lisp programs, see also `c-macro-expansion'."
50
51 (interactive (if current-prefix-arg
52 (list nil nil t)
53 (let ((pos1 (point))
54 (pos2 (mark)))
55 (if (< pos1 pos2)
56 (list pos1 pos2 nil)
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: "
63 c-macro-cppflags)))
64 (if undo
65 (let ((pos (point)) if-pos else-pos endif-pos)
66 (save-excursion
67 (end-of-line)
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))
71 (<= if-pos pos)
72 (< pos endif-pos)))
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))
77 (insert orig))))
78 ;; Expand the macro.
79 (let* ((expansion (c-macro-expansion start end
80 (concat c-macro-preprocessor " "
81 c-macro-cppflags) t))
82 (orig (buffer-substring start end)))
83 (setq expansion
84 (with-temp-buffer
85 (insert expansion)
86 (call-process-region (point-min) (point-max) "indent"
87 t ;delete the text
88 t ;output --> current buffer
89 )
90 (buffer-string)))
91 (delete-region start end)
92 (insert if-mark ?\n orig else-mark ?\n expansion endif-mark ?\n)))))