123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- (require 'cmacexp)
- (defvar if-mark "#if 0 /* PPEXPAND */")
- (defvar else-mark "#else /* PPEXPAND */")
- (defvar endif-mark "#endif /* PPEXPAND */")
- (define-key c-mode-map "\C-ce" 'ppexpand)
- (defun ppexpand (start end &optional undo)
- "Expand C macros in the region, using the C preprocessor.
- The expanded code is run through the `indent' command and inserted
- into the program next to the original code, using an #if/#else/#endif
- construct.
- Given a prefix argument, it reverts the change, removing the
- #if/#else/#endif construct and the expanded code.
- `c-macro-preprocessor' specifies the preprocessor to use.
- Prompt for arguments to the preprocessor \(e.g. `-DDEBUG -I ./include')
- if the user option `c-macro-prompt-flag' is non-nil.
- Noninteractive args are START, END, UNDO.
- For use inside Lisp programs, see also `c-macro-expansion'."
- (interactive (if current-prefix-arg
- (list nil nil t)
- (let ((pos1 (point))
- (pos2 (mark)))
- (if (< pos1 pos2)
- (list pos1 pos2 nil)
- (list pos2 pos1 nil)))))
- (let ((inbuf (current-buffer)))
-
- (if c-macro-prompt-flag
- (setq c-macro-cppflags
- (read-string "Preprocessor arguments: "
- c-macro-cppflags)))
- (if undo
- (let ((pos (point)) if-pos else-pos endif-pos)
- (save-excursion
- (end-of-line)
- (if (not (and (setq if-pos (search-backward if-mark nil t))
- (setq else-pos (search-forward else-mark nil t))
- (setq endif-pos (search-forward endif-mark nil t))
- (<= if-pos pos)
- (< pos endif-pos)))
- (error "Not in ppexpanded region"))
- (let ((orig (buffer-substring (+ if-pos (length if-mark) 1)
- (- else-pos (length else-mark)))))
- (delete-region if-pos (+ endif-pos 1))
- (insert orig))))
-
- (let* ((expansion (c-macro-expansion start end
- (concat c-macro-preprocessor " "
- c-macro-cppflags) t))
- (orig (buffer-substring start end)))
- (setq expansion
- (with-temp-buffer
- (insert expansion)
- (call-process-region (point-min) (point-max) "indent"
- t
- t
- )
- (buffer-string)))
- (delete-region start end)
- (insert if-mark ?\n orig else-mark ?\n expansion endif-mark ?\n)))))
|