From:	CRDGW2::CRDGW2::MRGATE::"SMTP::AI.MIT.EDU::GNULISTS" 21-DEC-1990 19:08:20.09
To:	MRGATE::"ARISIA::EVERHART"
CC:	
Subj:	A short holiday present (Quick-n-dirty ELisp protoizer)

Received:  by crdgw1.ge.com (5.57/GE 1.80)
	 id AA24690; Fri, 21 Dec 90 17:22:04 EST
Received: by life.ai.mit.edu (4.1/AI-4.10) id AA19746; Thu, 20 Dec 90 19:04:21 EST
Return-Path: <gnulists@ai.mit.edu>
Received: from wheat-chex (wheat-chex.ai.mit.edu) by life.ai.mit.edu (4.1/AI-4.10) id AA19389; Thu, 20 Dec 90 18:46:17 EST
Received: by wheat-chex (4.1/AI-4.10) id AA23535; Thu, 20 Dec 90 18:46:17 EST
Resent-Date: Thu, 20 Dec 90 00:01:49 EST
Resent-From: gnulists@ai.mit.edu
Resent-Message-Id: <9012202346.AA23535@wheat-chex>
Received: from crdgw1.ge.com by life.ai.mit.edu (4.1/AI-4.10) id AA02752; Thu, 20 Dec 90 00:01:33 EST
Received:  by crdgw1.ge.com (5.57/GE 1.80)
	 id AA08844; Thu, 20 Dec 90 00:01:22 EST
Received: from spyder.crd.Ge.Com by moose.crd.Ge.Com (4.0/SMI-4.0/GE-CRD @(#)sun4.ease	1.13 4/13/89)
	id AA23972; Thu, 20 Dec 90 00:01:49 EST
Date: Thu, 20 Dec 90 00:01:49 EST
From: montnaro@moose (Skip Montanaro)
Sender: gnulists@ai.mit.edu
Message-Id: <9012200501.AA23972@moose.crd.Ge.Com>
Received: by spyder.crd.Ge.Com (4.1/SMI-4.0/GE-CRD @(#)sun4.ease	1.13 4/13/89)
	id AA08307; Thu, 20 Dec 90 00:01:44 EST
Original-To: info-gcc@prep.ai.mit.edu, info-gnu-emacs@prep.ai.mit.edu
To: help-gcc@prep.ai.mit.edu, help-gnu-emacs@prep.ai.mit.edu
Subject: A short holiday present (Quick-n-dirty ELisp protoizer)
Reply-To: montanaro@crdgw1.ge.com (Skip Montanaro)

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"))))




