(require 'cc-mode)
(provide 'cmacexp)
(defvar msdos-shells)
(defgroup c-macro nil
"Expand C macros in a region."
:group 'c)
(defcustom c-macro-shrink-window-flag nil
"*Non-nil means shrink the *Macroexpansion* window to fit its contents."
:type 'boolean
:group 'c-macro)
(defcustom c-macro-prompt-flag nil
"*Non-nil makes `c-macro-expand' prompt for preprocessor arguments."
:type 'boolean
:group 'c-macro)
(defcustom c-macro-preprocessor
(cond ((and (string-match "^[^-]*-[^-]*-\\(solaris\\|sunos5\\)"
system-configuration)
(file-exists-p "/opt/SUNWspro/SC3.0.1/bin/acomp"))
"/opt/SUNWspro/SC3.0.1/bin/acomp -C -E")
((locate-file "/usr/ccs/lib/cpp"
'("/") exec-suffixes 'file-executable-p)
"/usr/ccs/lib/cpp -C")
((locate-file "/lib/cpp"
'("/") exec-suffixes 'file-executable-p)
"/lib/cpp -C")
((locate-file "cpp" exec-path exec-suffixes 'file-executable-p)
"cpp -C")
(t "gcc -E -C -o - -"))
"The preprocessor used by the cmacexp package.
If you change this, be sure to preserve the `-C' (don't strip comments)
option, or to set an equivalent one."
:type 'string
:group 'c-macro)
(defcustom c-macro-cppflags ""
"*Preprocessor flags used by `c-macro-expand'."
:type 'string
:group 'c-macro)
(defconst c-macro-buffer-name "*Macroexpansion*")
(defun c-macro-expand (start end subst)
"Expand C macros in the region, using the C preprocessor.
Normally display output in temp buffer, but
prefix arg means replace the region with it.
`c-macro-preprocessor' specifies the preprocessor to use.
Tf the user option `c-macro-prompt-flag' is non-nil
prompt for arguments to the preprocessor \(e.g. `-DDEBUG -I ./include'),
otherwise use `c-macro-cppflags'.
Noninteractive args are START, END, SUBST.
For use inside Lisp programs, see also `c-macro-expansion'."
(interactive "r\nP")
(let ((inbuf (current-buffer))
(displaybuf (if subst
(get-buffer c-macro-buffer-name)
(get-buffer-create c-macro-buffer-name)))
(expansion ""))
(if c-macro-prompt-flag
(setq c-macro-cppflags
(read-string "Preprocessor arguments: "
c-macro-cppflags)))
(if (and subst
(and buffer-read-only (not inhibit-read-only))
(not (eq inbuf displaybuf)))
(progn
(message
"Buffer is read only: displaying expansion in alternate window")
(sit-for 2)
(setq subst nil)
(or displaybuf
(setq displaybuf (get-buffer-create c-macro-buffer-name)))))
(setq expansion (c-macro-expansion start end
(concat c-macro-preprocessor " "
c-macro-cppflags) t))
(if subst
(let ((exchange (= (point) start)))
(delete-region start end)
(insert expansion)
(if exchange
(exchange-point-and-mark)))
(set-buffer displaybuf)
(setq buffer-read-only nil)
(buffer-disable-undo displaybuf)
(erase-buffer)
(insert expansion)
(set-buffer-modified-p nil)
(if (string= "" expansion)
(message "Null expansion")
(c-macro-display-buffer))
(setq buffer-read-only t)
(setq buffer-auto-save-file-name nil)
(bury-buffer displaybuf))))
(defun c-macro-display-buffer ()
(goto-char (point-min))
(c-mode)
(let ((oldwinheight (window-height))
(alreadythere (get-buffer-window (current-buffer)))
(popped nil)) (or alreadythere
(progn
(display-buffer (current-buffer) t)
(setq popped (/= oldwinheight (window-height)))))
(if (and c-macro-shrink-window-flag (or alreadythere popped))
(let ((oldwin (selected-window))
(minheight 0)
(maxheight 0))
(save-excursion
(select-window (get-buffer-window (current-buffer)))
(setq minheight (if alreadythere
(window-height)
window-min-height))
(setq maxheight (/ (frame-height) 2))
(enlarge-window (- (min maxheight
(max minheight
(+ 2 (vertical-motion (point-max)))))
(window-height)))
(goto-char (point-min))
(select-window oldwin))))))
(defun c-macro-expansion (start end cppcommand &optional display)
"Run a preprocessor on region and return the output as a string.
Expand the region between START and END in the current buffer using
the shell command CPPCOMMAND (e.g. \"/lib/cpp -C -DDEBUG\").
Be sure to use a -C (don't strip comments) or equivalent option.
Optional arg DISPLAY non-nil means show messages in the echo area."
(let ((inbuf (current-buffer))
(outbuf (get-buffer-create " *C Macro Expansion*"))
(filename (if (and buffer-file-name
(string-match (regexp-quote default-directory)
buffer-file-name))
(substring buffer-file-name (match-end 0))
(buffer-name)))
(mymsg (format "Invoking %s%s%s on region..."
c-macro-preprocessor
(if (string= "" c-macro-cppflags) "" " ")
c-macro-cppflags))
(uniquestring "??? !!! ??? start of c-macro expansion ??? !!! ???")
(startlinenum 0)
(linenum 0)
(startstat ())
(startmarker "")
(exit-status 0)
(tempname (make-temp-file
(expand-file-name "cmacexp"
(or small-temporary-file-directory
temporary-file-directory)))))
(unwind-protect
(save-excursion
(save-restriction
(widen)
(let ((in-syntax-table (syntax-table)))
(set-buffer outbuf)
(setq buffer-read-only nil)
(erase-buffer)
(set-syntax-table in-syntax-table))
(insert-buffer-substring inbuf 1 end))
(insert "\n")
(setq startstat (parse-partial-sexp 1 start))
(setq startlinenum (+ (count-lines 1 (point))
(if (bolp) 1 0)))
(goto-char (point-max))
(while (re-search-backward "\n#\\(endif\\|else\\)\\>" start 'move)
(if (equal (nthcdr 3 (parse-partial-sexp start (point)
nil nil startstat))
'(nil nil nil 0 nil)) (progn
(goto-char (match-end 0))
(setq linenum (+ startlinenum
(count-lines start (point))))
(insert (format "\n#line %d \"%s\"\n" linenum filename))
(goto-char (match-beginning 0)))))
(let* ((startinstring (nth 3 startstat))
(startincomment (nth 4 startstat))
(startafterquote (nth 5 startstat))
(startinbcomment (nth 7 startstat)))
(insert (if startafterquote " " "")
(cond (startinstring
(char-to-string startinstring))
(startincomment "*/")
(""))
(setq startmarker
(concat "\n" uniquestring
(cond (startinstring
(char-to-string startinstring))
(startincomment "/*")
(startinbcomment "//"))
(if startafterquote "\\")))
(format "\n#line %d \"%s\"\n" startlinenum filename)))
(if display (message "%s" mymsg))
(setq exit-status
(call-process-region 1 (point-max)
shell-file-name
t (list t tempname) nil "-c"
cppcommand))
(if display (message "%s" (concat mymsg "done")))
(if (= (buffer-size) 0)
(insert "\nPreprocessor produced no output\n")
(goto-char (point-min))
(search-forward startmarker)
(delete-region 1 (point)))
(while (re-search-forward (concat "^# [0-9]+ \""
(regexp-quote filename)
"\"") nil t)
(beginning-of-line)
(let ((beg (point)))
(forward-line 1)
(delete-region beg (point))))
(or (and (or (not (boundp 'msdos-shells))
(not (member (file-name-nondirectory shell-file-name)
msdos-shells)))
(eq exit-status 0))
(zerop (nth 7 (file-attributes (expand-file-name tempname))))
(progn
(goto-char (point-min))
(insert
(format "/* Preprocessor terminated with status %s\n\n Messages from `%s\':\n\n"
exit-status cppcommand))
(goto-char (+ (point)
(nth 1 (insert-file-contents tempname))))
(insert "\n\n*/\n")))
(delete-file tempname)
(buffer-substring 1 (max 1 (- (point-max) 1))))
(kill-buffer outbuf))))