Xref: utzoo gnu.emacs.help:629 comp.emacs:9773 Path: utzoo!utgpu!watserv1!watmath!uunet!tut.cis.ohio-state.edu!moose.crd.ge.com!montnaro From: montnaro@moose.crd.ge.com (Skip Montanaro) Newsgroups: gnu.emacs.help,comp.emacs Subject: A short holiday present (Quick-n-dirty ELisp protoizer) Message-ID: <9012200501.AA23972@moose.crd.Ge.Com> Date: 20 Dec 90 05:01:49 GMT Sender: daemon@tut.cis.ohio-state.edu Reply-To: montanaro@crdgw1.ge.com (Skip Montanaro) Followup-To: gnu.emacs.help Organization: Gatewayed from the GNU Project mailing list help-gnu-emacs@prep.ai.mit.edu Lines: 72 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"))))