[comp.emacs] A short holiday present

montnaro@moose.crd.ge.com (Skip Montanaro) (12/20/90)

I know more elaborate systems have been developed, but here's a short and
fast protoizer I just wrote in ELisp to convert some C code to use function
prototypes. It works about as well as the mark-c-function command does. 

I tend to define (old-style) C functions in a very consistent manner,
something like:

    static void next_state(s, last, next)
	 fpstate s;
	 fpstate *last;
	 fpstate *next;
    {
	...
    }

so it's no big deal to pick out the header (declarator) of a function
definition after executing mark-c-function.  The ELisp at the end of this
note converts the above function definition into:

    static void next_state(fpstate s, fpstate *last, fpstate *next)
    {
	...
    }

and places

    static void next_state(fpstate s, fpstate *last, fpstate *next);

at the end of a buffer named *protos*. A couple flush-line commands cleans
out the junk (like leading comments and static functions). The rest can
usually be inserted into a .h file.

A simple keyboard macro serves to walk you through a buffer, one function at
a time:

    (fset 'proto-step "\C-[xold-to-proto\C-M\C-[\C-[(sit-for 0)\C-M\C-X\C-X")

 

Skip (montanaro@crdgw1.ge.com)

- ----------
(defun old-to-proto ()
  (interactive)
  (mark-c-function)
  (let ((s (progn
	     (next-line 1)
	     (beginning-of-line)
	     (point)))
	(e (save-excursion
	     (search-forward "{")
	     (backward-char 1)
	     (point))))
    (let ((hdr (buffer-substring s e)))
      (delete-region s e)
      (save-excursion
	(get-buffer-create "*protos*")
	(set-buffer "*protos*")
	(end-of-buffer)
	(save-excursion (insert hdr))
	(save-excursion (replace-regexp "(.*)
 +" "("))
	(save-excursion (replace-regexp ";" ","))
	(save-excursion (replace-regexp ",
 +" ": "))
	(save-excursion (replace-string "," ");"))
	(save-excursion (replace-string ":" ","))
	(save-excursion (replace-string ");" ")"))
	(save-excursion (replace-string ")" ");"))
	(save-excursion (replace-regexp "( *)" "(void)"))
	(setq hdr (buffer-substring (point) (- (point-max) 2))))
      (insert hdr "\n"))))