]> git.donarmstrong.com Git - lilypond.git/blob - guile18/emacs/ppexpand.el
Import guile-1.8 as multiple upstream tarball component
[lilypond.git] / guile18 / emacs / ppexpand.el
1 ;;; ppexpand.el --- temporarily expanding macros in a pretty way.
2
3 ;; Copyright (C) 2000, 2006 Free Software Foundation, Inc.
4
5 ;; This file is part of GNU Emacs.
6
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)
10 ;; any later version.
11
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.
16
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.
21
22 ;;; Author: Mikael Djurfeldt <djurfeldt@nada.kth.se>
23
24 ;;; Commentary:
25
26 ;; Commands for editing multiline ANSI C compatible string literals.
27
28 ;;; Code:
29 (require 'cmacexp)
30
31 (defvar if-mark "#if 0 /* PPEXPAND */")
32 (defvar else-mark "#else /* PPEXPAND */")
33 (defvar endif-mark "#endif /* PPEXPAND */")
34
35 (define-key c-mode-map "\C-ce" 'ppexpand)
36
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
41 construct.
42
43 Given a prefix argument, it reverts the change, removing the
44 #if/#else/#endif construct and the expanded code.
45
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.
49
50 Noninteractive args are START, END, UNDO.
51 For use inside Lisp programs, see also `c-macro-expansion'."
52
53   (interactive (if current-prefix-arg
54                    (list nil nil t)
55                  (let ((pos1 (point))
56                        (pos2 (mark)))
57                    (if (< pos1 pos2)
58                        (list pos1 pos2 nil)
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: "
65                            c-macro-cppflags)))
66     (if undo
67         (let ((pos (point)) if-pos else-pos endif-pos)
68           (save-excursion
69             (end-of-line)
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))
73                           (<= if-pos pos)
74                           (< pos endif-pos)))
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))
79               (insert orig))))
80       ;; Expand the macro.
81       (let* ((expansion (c-macro-expansion start end
82                                            (concat c-macro-preprocessor " "
83                                                    c-macro-cppflags) t))
84              (orig (buffer-substring start end)))
85         (setq expansion
86               (with-temp-buffer
87                 (insert expansion)
88                 (call-process-region (point-min) (point-max) "indent"
89                                      t          ;delete the text
90                                      t          ;output --> current buffer
91                                      )
92                 (buffer-string)))
93         (delete-region start end)
94         (insert if-mark ?\n orig else-mark ?\n expansion endif-mark ?\n)))))