summaryrefslogtreecommitdiff
path: root/contrib/genrpbindings
blob: 60573bf07f77f7ad3856ef10b524608066873184 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/perl -w
# genrpbindings -- generate ratpoison bindings for various languages
#
# Ryan Yeske <rcyeske@sfu.ca>
# Tue Dec  4 16:15:53 PST 2001
#
# currently generates bindings for:
# * Perl       (Ratpoison.pm)
# * Emacs Lisp (ratpoison-cmd.el)
# * Ruby       (ratpoison.rb) [Doug Kearns <djkea2@mugc.its.monash.edu.au>]
# 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")

$\="\n";

# set this to your rp binary
$RATPOISON=$ENV{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";
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";

# PERL preamble
print PERL 'package Ratpoison;';
print PERL '$RATPOISON="',$RATPOISON,'";';
print PERL 'sub command { return `$RATPOISON -c "@_"`; }';

# ELISP preamble
print ELISP '(defvar ratpoison-program "',$RATPOISON,'")';
print ELISP <<PREAMBLE;

(defmacro defun-ratpoison (cmd)
  `(progn (defun ,(intern (concat "ratpoison-" (symbol-name cmd))) (&rest args)
          (apply 'ratpoison-cmd ,(symbol-name cmd) args)))

(defun ratpoison-cmd (cmd &rest args)
  (with-temp-buffer
    (call-process ratpoison-program nil (current-buffer) t
		  "-c" (format "%s %s"
			       cmd
			       (mapconcat (lambda (x)
					    (if (stringp x)
						x
					      (prin1-to-string x)))
					  args " ")))
    (buffer-substring (point-min) (if (> (point-max) 1)
				      (- (point-max) 1)
				    (point-max)))))
PREAMBLE

# RUBY preamble
print RUBY  <<PREAMBLE;
module Ratpoison

  RATPOISON="$RATPOISON"

  def command (command, *args)
    return `#{RATPOISON} -c "#{command} #{args.join(' ')}"`
  end
  module_function :command
PREAMBLE

# Scheme preamble

print COMMONLISP <<PREAMBLE;
(defpackage :ratpoison
  (:use :cl))

;; Needs the CLOCC PORT package
(asdf:operate 'asdf:load-op :port)

(in-package :ratpoison)

(defvar ratpoison-program "$RATPOISON")

(defmacro defun-ratpoison (cmd)
  (let ((sym (intern (concatenate 'string "RP-" (symbol-name cmd)))))
    `(progn (defun ,sym (&rest args)
	      (apply 'ratpoison-cmd ,(string-downcase (symbol-name cmd)) args))
	    (export ',sym))))

(defun ratpoison-cmd (cmd &rest args)
  (labels ((mapconcat (fn list sep)
		     (apply 'concatenate 'string
			    (loop for x on list
				  collect (if (cdr x)
					      (concatenate 'string (funcall fn (car x)) sep)
					    (funcall fn (car x))))))
	 (build-cmd (cmd args)
		    (mapconcat (lambda (x)
				 (if (stringp x)
				     x
				   (prin1-to-string x)))
			       (nconc (list cmd) args) " ")))
    (let ((stream (port:pipe-input ratpoison-program
				   "-c" (build-cmd cmd args))))
    (do ((line (read-line stream nil nil)
	       (read-line stream nil nil))
	 (accum nil (cons line accum)))
	((null line) accum)))))
PREAMBLE

# bindings
while (<ACTIONS_C>) {
    if (m!/\*\@begin !) {
	while (<ACTIONS_C>)
	{
	    last if (m!/\*\@end !);
	    if (/{\"(.+)\".+},/) {
	      $nbindings++;
	      print PERL "sub $1 { return command (\"$1\", \@_); }";
	      print ELISP "(defun-ratpoison $1)";
	      print COMMONLISP "(defun-ratpoison $1)";
	      print RUBY "  def $1 (*args)";
	      print RUBY "    return command (\"$1\", args)";
	      print RUBY "  end";
	      print RUBY "  module_function :$1\n";
	    }
	}
    }
}
print "$nbindings bindings.";

# PERL postamble
# nothing

# ELISP postamble
print ELISP '(provide \'ratpoison-cmd)';

# RUBY postamble
print RUBY "end";

close PERL;
print "Created $PERL_FILE";
close ELISP;
print "Created $ELISP_FILE";
close RUBY;
print "Created $RUBY_FILE";
close COMMONLISP;
print "Created $COMMONLISP_FILE";