ppexpand.el 3.2 KB

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