ppexpand.el 3.2 KB

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