#!/usr/bin/env perl # genrpbindings -- generate ratpoison bindings for various languages # # Copyright (C) 2003, 2004 Ryan Yeske, Doug Kearns, Shawn Betts # # currently generates bindings for: # * Perl (Ratpoison.pm) Ryan Yeske # * Emacs Lisp (ratpoison-cmd.el) Ryan Yeske # * Ruby (ratpoison.rb) Doug Kearns # * Common Lisp (ratpoison.lisp) Shawn Betts # * Python (ratpoison.py) Mike O'Connor # add more languages! # # Bindings are just very thin wrappers, no argument checking is done. # All of the functions return a string. # # Example: ratpoison --command='echo hello world' # # #!perl # use Ratpoison; # Ratpoison::echo ("hello world") # # ;;; elisp # (require 'ratpoison-cmd) # (ratpoison-echo "hello world") # # #!ruby # require "ratpoison" # Ratpoison.echo ("hello world") # # ;;; Common Lisp # (load "ratpoison.lisp") # (ratpoison:rp-echo "hello world") # # #!python # import ratpoison # ratpoison.echo( "hello world" ) use warnings; $\="\n"; # Feel free to hardcode this if you installed ratpoison in # a non-standard path, but note that you can also use the RATPOISON # environment variable $RATPOISON = 'ratpoison'; # open source file $ACTIONS_C="../src/actions.c"; open ACTIONS_C or die "Can't open $ACTIONS_C"; # open target files $PERL_FILE="./Ratpoison.pm"; $ELISP_FILE="./ratpoison-cmd.el"; $RUBY_FILE="./ratpoison.rb"; $COMMONLISP_FILE="./ratpoison.lisp"; $PYTHON_FILE="./ratpoison.py"; open PERL, ">$PERL_FILE" or die "Can't create $PERL_FILE"; open ELISP, ">$ELISP_FILE" or die "Can't create $ELISP_FILE"; open RUBY, ">$RUBY_FILE" or die "Can't create $RUBY_FILE"; open COMMONLISP, ">$COMMONLISP_FILE" or die "Can't create $COMMONLISP_FILE"; open PYTHON, ">$PYTHON_FILE" or die "Can't create $PYTHON_FILE"; # PERL preamble print PERL 'package Ratpoison;'; print PERL '$RATPOISON=$ENV{RATPOISON} ? $ENV{RATPOISON} : "',$RATPOISON,'";'; print PERL 'sub command { my $a = "@_"; $a =~ s/(\')/\'\\\\\'\'/g; return `$RATPOISON -c \'$a\'`; }'; # ELISP preamble print ELISP < (point-max) 1) (- (point-max) 1) (point-max))))) PREAMBLE # RUBY preamble print RUBY <) { if (m!/\*\@begin !) { while () { last if (m!/\*\@end !); if (/\s*add_command\s*\(\"([^\"]+)\",\s*[^\"]+,\s*([0-9]+),\s*[0-9]+,\s*([0-9]+)/) { my $name = $1; my $numargs = $2; my $optargs = $3; # Skip the arguments for (my $i=0; $i<$numargs; $i++) { ; } $nbindings++; print PERL "sub $name { return command (\"$name\", \@_); }"; print ELISP "(defun-ratpoison $name)"; print COMMONLISP "(defun-ratpoison $name)"; print RUBY " def $name(*args)"; print RUBY " return command(\"$name\", args)"; print RUBY " end"; print RUBY " module_function :$name\n"; print PYTHON "def rp_$name( *args ): return rp_command ( '$name ' + ' '.join( args ) )"; } } } } print "$nbindings bindings."; # PERL postamble # nothing # ELISP postamble print ELISP '(provide \'ratpoison-cmd)'; # RUBY postamble print RUBY "end"; # COMMON-LISP postamble # nothing # PYTHON postamble # nothing close PERL; print "Created $PERL_FILE"; close ELISP; print "Created $ELISP_FILE"; close RUBY; print "Created $RUBY_FILE"; close COMMONLISP; print "Created $COMMONLISP_FILE"; close PYTHON; print "Created $PYTHON_FILE";