# El4r-1.0.4 - EmacsLisp for Ruby

**URL:** <https://www.ruby-forum.com/t/el4r-1-0-4-emacslisp-for-ruby/69487>\
**Category:** Ruby\
**Created:** [September 21, 2006, 10:49am UTC](https://www.ruby-forum.com/t/el4r-1-0-4-emacslisp-for-ruby/69487 "2006-09-21T10:49:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [September 21, 2006, 10:49am UTC](https://www.ruby-forum.com/t/el4r-1-0-4-emacslisp-for-ruby/69487/1 "2006-09-21T10:49:08Z")

</div>

Hi,

El4r enables you to write Emacs programs in Ruby as well as in  
EmacsLisp.  
I call the Ruby language to manipulate Emacs `EmacsRuby’.

El4r and Test::Unit enables you to unit-test EmacsLisp/EmacsRuby  
programs automatically.

El4r is available at  
[http://www.rubyist.net/~rubikitch/computer/el4r/index.en.html](http://www.rubyist.net/~rubikitch/computer/el4r/index.en.html)

== What’s new

=== [2006/09/21] 1.0.4 released

- Smart-defun: defun within ElApp subclass.

=== [2006/03/12] 1.0.3 released

- Interactive specification by Proc objects
- .xemacs/init.el support.
- manpages.

=== [2005/12/11] 1.0.2 released

- Fixed `el4r-rctool’ bug on Windows. No changes except installer.

=== [2005/12/02] 1.0.1 released

- Adjusted to Emacs-snapshot.

=== [2005/12/01] 1.0.0 released

- Installer.
- Packaging policy.
- Fixed fatal error bug.
- Fixed defun-GC problem.
- Renamed: el4r-runtest.rb → el4r-runtest
- New command: el4r
- New EmacsRuby functions.
  - define\_minor\_mode
  - define\_derived\_mode
  - eval\_after\_load

== Download / Install / Setup  
Since version 1.0.0, el4r introduced installer.  
If you got error when downloading, you must update Ruby.  
Here is the shell commands to download, install and setup.  
el4r-rctool setups and updates your dotfiles automatically.

To update older el4r (\<= 0.9.1), you must remove these lines from  
~/.emacs,  
(add-to-list 'load-path “~/src/el4r/elisp/”)  
(require 'el4r)  
(el4r-boot)  
and this line from ~/.el4r/init.rb by hand.  
el4r\_load “el4r-mode.rb”  
In newer el4r, el4r-rctool updates your dotfiles automatically.

ruby -ropen-uri -e  
‘URI(“[http://www.rubyist.net/~rubikitch/archive/el4r-1.0.4.tar.gz](http://www.rubyist.net/~rubikitch/archive/el4r-1.0.4.tar.gz)”).read.display’

> el4r-1.0.4.tar.gz  
> tar xzf el4r-1.0.4.tar.gz  
> cd el4r-1.0.4  
> ruby setup.rb

ruby -S el4r-rctool -p  
ruby -S el4r-rctool -i

The diretory to put EmacsRuby scripts is ~/.el4r by default.  
The environment variable EL4R\_HOME sets the directory to put EmacsRuby  
scripts.

- 

((\<el4r-1.0.4.tar.gz|URL:[http://www.rubyist.net/~rubikitch/archive/el4r-1.0.4.tar.gz](http://www.rubyist.net/~rubikitch/archive/el4r-1.0.4.tar.gz)\>))

Victor B. created ((\<Gentoo  
ebuild|URL:[TheHatcher.com is for sale | HugeDomains](http://darcs.thehatcher.com/vic/erebor-ebuilds/app-emacs/)\>)).  
Thanks.

Boris Daix created ((\<Debian  
package|URL:[http://alysse.dyndns.org/~bdaix/debian/](http://alysse.dyndns.org/~bdaix/debian/)\>)). Thanks.  
Put the following to your /etc/apt/sources.list:  
deb [http://alysse.dyndns.org/~bdaix/debian/packages](http://alysse.dyndns.org/~bdaix/debian/packages) unstable/  
deb-src [http://alysse.dyndns.org/~bdaix/debian/packages](http://alysse.dyndns.org/~bdaix/debian/packages) unstable/

Here is a test program of el4r.

# el4r - EmacsLisp for Ruby

# Copyright (C) 2005 rubikitch [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

# Version: $Id: test-el4r.rb 1328 2006-08-14 21:10:57Z rubikitch $

# This program is free software; you can redistribute it and/or

modify

# it under the terms of the GNU General Public License as published

by

# the Free Software Foundation; either version 2 of the License, or

# (at your option) any later version.

# 

# This program is distributed in the hope that it will be useful,

# but WITHOUT ANY WARRANTY; without even the implied warranty of

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

# GNU General Public License for more details.

# 

# You should have received a copy of the GNU General Public License

# along with this program; if not, write to the Free Software

# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA

02111-1307 USA

require ‘test/unit’

require ‘tempfile’  
require ‘tmpdir’  
require ‘pathname’  
require ‘fileutils’  
class \<\< Tempfile  
def path(content, dir=Dir.tmpdir)  
x = Tempfile.open(“content”, dir)  
x.write content  
x.close  
x.open  
x.path  
end

def pathname(content, dir=Dir.tmpdir)  
Pathname.new(path(content, dir=Dir.tmpdir))  
end  
end

# El4r self test.

## (eevnow "cat `buffer-file-name` | awk '/ def test\_/{print

$2}'|result-long")  
class TestEl4r \< Test::Unit::TestCase

# ElMixin is already included/extended.

# So we can write EmacsRuby in this class.

# Testing ELListCell#to\_ary.

# This method enables us to multiple assignment.

def test\_to\_ary  
list = el4r\_lisp\_eval(%q((list 1 2)))  
one, two = list

```
assert_equal(list.to_a, list.to_ary)
assert_equal(1, one)
assert_equal(2, two)

```

end

# Testing with and match-string.

def test\_match\_string  
lisp = %q((progn  
(switch-to-buffer “a”)

```
          (save-excursion
            (insert "abcdefg\n")
            (goto-char 1)
            (re-search-forward "^\\\\(.+\\\\)$")
            )
          (match-string 1)))

ruby = lambda{

```

##### [with]

```
  with(:save_excursion) do
    goto_char 1
    re_search_forward('^\\(.+\\)$')
  end
  match_string 1

```

##### [/with]

```
}
assert_equal(el4r_lisp_eval(lisp), ruby[])

```

end

# helper method:

# execute a block with temporary buffer.

# and return the contents of buffer.

def with\_temp\_buffer\_string(&block)  
with(:with\_temp\_buffer){  
self.instance\_eval(&block)  
buffer\_string  
}  
end

# this test was in test.el

def test\_test\_el\_\_debug\_ruby\_eval\_report  
actual = with\_temp\_buffer\_string {  
el4r\_lisp\_eval %q((progn  
(el4r-debug-ruby-eval-report “nil”)  
(el4r-debug-ruby-eval-report “true”)  
(el4r-debug-ruby-eval-report “false”)  
(el4r-debug-ruby-eval-report “1 + 6”)  
(el4r-debug-ruby-eval-report “"String"”)  
))  
}  
expected = \<\<EOB  
nil  
=\> nil  
true  
=\> t  
false  
=\> nil  
1 + 6  
=\> 7  
“String”  
=\> “String”  
EOB  
assert\_equal(expected, actual)  
end

def test\_test\_el\_\_condition\_case

```
# (mode-info-describe-function 'signal 'elisp)
# (mode-info-describe-function 'condition-case 'elisp)
el4r_lisp_eval %q((progn

```

(put 'test-error  
'error-conditions  
'(error test-error))  
(put 'test-error ‘error-message “Test Error”)  
))  
#’  
el4r\_lisp\_eval %q((progn  
(setq error-desc nil)  
(condition-case err  
(signal 'test-error ‘(123))  
(test-error (setq error-desc (format “Error is passed: %s” err)))  
)  
))  
#’  
assert\_equal(“Error is passed: (test-error 123)”, elvar.error\_desc)

```
el4r_lisp_eval %q((progn

```

(setq error-desc nil)  
(condition-case err  
(el4r-ruby-eval “el4r\_lisp\_eval("(signal 'test-error '(123))")”)  
(test-error (setq error-desc (format “Error is passed: %s” err)))  
)  
))  
#’  
assert\_equal(“Error is passed: (test-error 123)”, elvar.error\_desc)  
end

# eval test

def test\_el4r\_eval  
result = with\_temp\_buffer\_string{  
el4r\_lisp\_eval(\<\<‘EOF’)  
(insert-string (el4r-ruby-eval “"Hello from ruby from emacs  
from ruby!\n"”))  
EOF  
}  
assert\_equal(“Hello from ruby from emacs from ruby!\n”, result)  
assert\_equal(true, el4r\_lisp\_eval(‘t’))  
end

# list: cons, car/cdr

def test\_list  
list = el(“'(3 2 1)”)  
list = cons(4, list)  
assert\_equal(“(4 3 2 1)”, prin1\_to\_string(list))

```
ary = []
while list
  ary << car(list)
  list = cdr(list)
end
assert_equal("[4, 3, 2, 1]", ary.inspect)

```

end

# pass a Ruby object to Emacs

def test\_object  
obj = Object.new  
assert\_equal(“Is ruby object passed? … true”,  
“Is ruby object passed? … #{car(cons(obj, nil)) ==  
obj}”)  
end

# Using defun ( Proc → Lambda conversion )

def test\_defun\_function  
defun(:my\_ruby\_func) { |a|  
0  
}  
# redefine  
defun(:my\_ruby\_func) { |a|  
“String from my\_ruby\_func: ‘#{a}’”  
}  
assert\_equal(“String from my\_ruby\_func: ‘Hello!’”,  
my\_ruby\_func(“Hello!”))  
end

# defun a command

def test\_defun\_command\_1  
defun(:my\_command, :interactive =\> true) {  
insert\_string(“My Interactive command from Ruby.”); newline  
}

```
assert_equal("My Interactive command from Ruby.\n",
             with_temp_buffer_string{ 

```

call\_interactively(:my\_command) })

end

# defun a command with docstring

def test\_defun\_command\_2

##### [my\_command2]

```
defun(:my_command2,
      :interactive => "d", :docstring => "description...") { |point|
  insert_string("Current point is #{point}."); newline
}

```

##### [/my\_command2]

```
assert_equal("d", nth(1, commandp(:my_command2)))
assert_equal("description...", documentation(:my_command2))
assert_equal("Current point is 1.\n",
             with_temp_buffer_string{ 

```

call\_interactively(:my\_command2) })  
end

# defun a command with lambda

def test\_defun\_command\_3  
sum = nil

##### [my\_command3]

```
interactive_proc = lambda { [1+1, 1] }
defun(:my_command3,
      :interactive => interactive_proc) { |a, b|
  sum = a + b
}

```

##### [/my\_command3]

```
assert_equal nil, sum
call_interactively :my_command3
assert_equal 3, sum

```

end

# defining odd-named function

def test\_defun\_oddname  
# Lisp can define `1+1’ function! LOL  
defun(“1+1”){2}  
assert\_equal(2, funcall(“1+1”))  
end

# Calling lambda

def test\_lambda  
lambda = el4r\_lisp\_eval(“(lambda (i) (+ i 1))”)  
assert\_equal(2, funcall(lambda, 1))  
end

# Calling special form like save-excursion

def test\_with  
x = with\_temp\_buffer\_string {  
insert\_string(“a\n”)  
with(:save\_excursion) {  
beginning\_of\_buffer  
insert\_string(“This is inserted at the beginning of buffer.”);  
newline  
}  
}  
assert\_equal(“This is inserted at the beginning of buffer.\na\n”, x)  
end

# ELListCell

def test\_ELListCell  
assert\_equal([1, 2], cons(1, cons(2, nil)).to\_a )  
assert\_equal([10,20], el4r\_lisp\_eval(%((list 1 2))).map{|x| x\*10})

```
assert_equal({'a'=>1, 'b'=>2}, list(cons("a",1), cons("b", 

```

2)).to\_hash)  
assert\_raises(TypeError){ list(cons(“a”,1), “b”, “c”).to\_hash }

```
assert_equal("ELListCell[1]", list(1).inspect)
assert_equal("ELListCell[1, 2]", list(1,2).inspect)

```

end

# ELConsCell

def test\_ELConsCell  
assert\_equal([1,2], el4r\_cons\_to\_rubyary(cons(1,2)))  
assert\_equal(“ELConsCell[1, 2]”, cons(1,2).inspect)

```
assert_equal("ELListCell[1, 2, ELConsCell[3, 4]]", 

```

list(1,2,cons(3,4)).inspect)  
assert\_equal(“ELListCell[1, 2]”, cons(1, list(2)).inspect)  
end

# ELVector

def test\_ELVector  
v = el4r\_lisp\_eval(“[1 2]”)  
assert( vectorp(v) )  
assert\_equal(“ELVector[1, 2]”, v.inspect)  
assert\_equal(1, v[0])  
assert\_equal(1, v[-2])  
assert\_raises(ArgumentError) { v[2] } # index is too large  
assert\_raises(TypeError) { v[“X”] }

```
assert_equal([1, 2], v[0,2])
assert_equal([1, 2], v.to_a)

# Enumerable
assert_equal(1, v.find{|x| x==1})

# to_ary
one, = v
assert_equal(1, one)

# aset
elvar.v = v
assert_equal(10, v[0]=10)
assert_equal(10, v[0])
assert_equal([10,2], v.to_a)
assert_equal(10, elvar.v[0])
assert_equal([10,2], elvar.v.to_a)
assert_raises(ArgumentError) { v[2]=3 } # index is too large
assert_raises(TypeError) { v["X"]=1 }

v[-1]=20
assert_equal([10,20], elvar.v.to_a)

```

end

# Accessing to lisp variables with elvar

def test\_elvar  
elvar.myvar = 123  
assert\_equal(123, elvar.myvar)

```
elvar["myvar"] = 456
assert_equal(456, elvar["myvar"])

assert( elvar.myvar == elvar["myvar"] )

```

end

# get/set an odd-named variable

def test\_elvar\_\_oddname  
elvar[“_an/odd+variable!_”] = 10  
assert\_equal(10, elvar[“_an/odd+variable!_”])  
end

# Error passing

def test\_error  
assert\_raises(RuntimeError) {  
el4r\_lisp\_eval(\<\<-‘EOF’)  
(el4r-ruby-eval “raise "Is error handled correctly?"”))  
EOF  
}  
end

# let

def test\_let  
elvar.testval = 12  
testval\_in\_letblock = nil  
let(:testval, 24) {  
testval\_in\_letblock = elvar.testval  
}

```
assert_equal(24, testval_in_letblock)
assert_equal(12, elvar.testval)

```

end

# Regexp convert: Convert Ruby regexps to MESSY Emacs regexps.

def test\_regexp

# (find-node “(emacs-ja)Regexps”)

```
conv = lambda{|from,to| assert_equal(to, el4r_conv_regexp(from)) }
conv[//, '']
conv[/a/, 'a']
conv[/a./, 'a.']
conv[/a*/, 'a*']
conv[/a+/, 'a+']
conv[/a?/, 'a?']
conv[/[ab]/, '[ab]' ]
conv[/[^ab]/, '[^ab]' ]
conv[/^ab/, '^ab']
conv[/ab$/, 'ab$']
conv[/a|b/, 'a\|b']
conv[/(ab)/, '\(ab\)']
conv[/\As/, '\`s']
conv[/s\Z/, %q[s\'] ]
# \=
conv[/\bball\B/, '\bball\B']
# \<
# \>
conv[/\w/, '[0-9A-Za-z_]']
conv[/\W/, '[^0-9A-Za-z_]']
# \sC
# \SC
# \D (number)

```

end

# Now you can specify a Ruby regexp to string-match, re-search-forward

and so on  
def test\_string\_match  
s = “a”  
assert\_equal(0, string\_match(“a”, s))  
assert\_equal(0, string\_match(/a/, s))  
assert\_equal(0, string\_match(‘(a|\b)’, s))  
assert\_equal(0, string\_match(/a|b/, s))  
assert\_equal(0, string\_match(/^a/, s))  
assert\_equal(0, string\_match(/a$/, s))  
assert\_equal(0, string\_match(/.\*/, s))  
assert\_equal(nil, string\_match(/not-match/, s))

end

# ElMixin: elisp {}

def test\_elmixin  
eval %{  
class ::Foo  
include ElMixin  
def foo  
elisp {  
[self.class, outer.class]  
}  
end

```
    def one
      1
    end
  end

}

el4r, outer = Foo.new.foo
assert_equal(El4r::ELInstance, el4r)
assert_equal(Foo, outer)

```

end

# EL error

def test\_elerror  
errormsg = nil  
begin  
el4r\_lisp\_eval(%q((defun errorfunc0 ())))  
with(:with\_current\_buffer, “_scratch_”){  
let(:x, 1) {  
with(:save\_excursion){  
errorfunc0 1 # wrong number of argument!!  
}  
}  
}  
flunk  
rescue  
errormsg = $!.to\_s  
end

```
assert_match(/\n\(errorfunc0.+save-excursion.+let.+with-current-buffer.+$/m, 

```

errormsg.to\_s)  
end

# to\_s: Implicitly call prin1\_to\_string

def test\_to\_s  
list = funcall(:list,1)  
assert\_equal(“(1)”, “#{list}”)  
assert\_equal( prin1\_to\_string(list), list.to\_s)  
end

# defadvice 1

def test\_defadvice\_1  
defun(:adtest1){  
elvar.v = 1  
}  
with(:defadvice, el(“adtest1 (after adv activate)”)){  
elvar.v = 2  
}  
adtest1

```
assert_equal(2, elvar.v)

```

end

# defadvice 2

def test\_defadvice\_2  
elvar.w = 0  
elvar.x = 0  
defun(:adtest\_2){  
elvar.w += 1  
3  
}  
defadvice(:adtest\_2, :around, :adv2, :activate) {  
ad\_do\_it  
elvar.x = 10  
ad\_do\_it  
}  
ret = adtest\_2()

```
assert_equal(2, elvar.w)
assert_equal(10, elvar.x)
assert_equal(3, ret)

```

end

# defadvice 3

def test\_defadvice\_3  
begin

##### [adtest3]

```
  # define a function
  defun(:adtest3){ 1 }

```

##### [/adtest3]

```
  assert_equal(1, adtest3())
  assert_equal(nil, commandp(:adtest3))

```

##### [adtest3-advice]

```
  # now define an advice
  defadvice(:adtest3, :around, :adv3, :activate,
            :docstring=>"test advice", :interactive=>true) {
    ad_do_it
    elvar.ad_return_value = 2
  }

```

##### [/ad\_return\_value]

```
  assert(commandp(:adtest3))
  assert_equal(2, adtest3())
  assert_match(/test advice/, documentation(:adtest3))
ensure
  ad_deactivate :adtest3

```

# fmakunbound :adtest3

```
end

```

end

# bufstr

def test\_bufstr  
s = bufstr(newbuf(:name=\>“axx”, :contents=\>“foo!”))  
assert\_equal(“foo!”, s)

```
newbuf(:name=>"axxg", :contents=>"bar!", :current=>true)
s = bufstr
assert_equal("bar!", s)

```

end

def xtest\_ad\_do\_it\_invalid  
assert\_raises(El4r::El4rError){  
ad\_do\_it  
}  
end

# el\_load

def test\_el\_load  
begin  
el = File.expand\_path(“elloadtest.el”)  
open(el, “w”){|w| w.puts(%q((setq elloadtest 100)))}  
el\_load(el)  
assert\_equal(100, elvar.elloadtest)  
ensure  
File.unlink el  
end  
end

# equality

def test\_EQUAL  
b1 = current\_buffer  
b2 = current\_buffer  
assert(b1 == b1)  
assert(b1 == b2)  
assert\_equal(b1,b2)  
end

# test delete-other-windows workaround in xemacs

def test\_delete\_other\_windows  
w = selected\_window  
elvar.window\_min\_height = 1  
split\_window  
split\_window  
delete\_other\_windows  
assert(one\_window\_p)  
assert(eq(w, selected\_window))  
end

# Lisp string → Ruby string special case

def test\_el4r\_lisp2ruby\_\_normal  
cmp = lambda{|str| assert\_equal(str, eval(el4r\_lisp2ruby(str)))}

# (mode-info-describe-function 'prin1-to-string 'elisp)

# (string= “\021” (el4r-ruby-eval (el4r-lisp2ruby “\021”)))

```
cmp[""]
cmp["a"*9999]
cmp['1']
cmp['a']
cmp['\\']
cmp['\\\\']
cmp['\\\\\\']
cmp['""']
cmp['"']
cmp["''"]
cmp['#{1}']
cmp['\#{1}']
cmp['#{\'1\'}']
cmp['#@a']
cmp["\306\374\313\334\270\354"] # NIHONGO in EUC-JP

```

end

def test\_el4r\_lisp2ruby\_\_treat\_ctrl\_codes  
cmp = lambda{|str| assert\_equal(str, eval(el4r\_lisp2ruby(str)))}  
elvar.coding\_system\_for\_write :binary  
elvar.coding\_system\_for\_write :binary  
set\_buffer\_file\_coding\_system :binary  
el4r\_treat\_ctrl\_codes {  
cmp[“”]  
cmp[“a”\*9999]  
cmp[‘1’]  
cmp[‘a’]  
cmp[‘\’]  
cmp[‘\\’]  
cmp[‘\\\’]  
cmp[‘“”’]  
cmp[‘"’]  
cmp[“‘’”]  
cmp[‘#{1}’]  
cmp[‘#{1}’]  
cmp[‘#{'1'}’]  
cmp[‘#@a’]

```
  cmp["\ca"]
  cmp["\cb"]
  cmp["\cc"]
  cmp["\cd"]
  cmp["\ce"]
  cmp["\cf"]
  cmp["\cg"]
  cmp["\ch"]
  cmp["\ci"]
  cmp["\cj"]
  cmp["\ck"]
  cmp["\cl"]
  # C-m
  # cmp["\cn"] failed on xemacs
  # cmp["\co"] failed on xemacs
  cmp["\cp"]
  cmp["\cq"]
  # C-r
  cmp["\cs"]
  cmp["\ct"]
  cmp["\cu"]
  cmp["\cv"]
  cmp["\cw"]
  cmp["\cx"]
  # cmp["\cy"]
  cmp["\cz"]
  cmp["\306\374\313\334\270\354"] # NIHONGO in EUC-JP
}

```

end

def el4r\_load\_test\_helper(dir)  
begin  
$loaded = nil  
tmpscript = “#{dir}/ **testtmp**.rb”  
“$loaded = true”.writef(tmpscript)  
el4r\_load “ **testtmp**.rb”  
assert\_equal(true, $loaded)  
ensure  
FileUtils.rm\_f tmpscript  
end  
end

def test\_el4r\_load\_\_load\_path  
begin  
load\_path\_orig = el4r.conf.el4r\_load\_path  
tmp = Dir.tmpdir  
load\_path = [tmp, “#{tmp}/a”]  
el4r.conf.el4r\_load\_path = load\_path  
load\_path.each do |dir|  
FileUtils.mkdir\_p dir  
el4r\_load\_test\_helper dir  
end  
ensure  
el4r.conf.el4r\_load\_path = load\_path\_orig  
FileUtils.rm\_rf “#{tmp}/a”  
end

end

def test\_el4r\_load\_\_not\_exist  
assert\_raises(LoadError) { el4r\_load “\_\_not\_exist.rb” }  
assert\_equal(false, el4r\_load(“\_\_not\_exist.rb”, true))  
end

def test\_el4r\_load\_\_order  
begin  
$loaded = nil  
load\_path = el4r.conf.el4r\_load\_path = [ el4r\_homedir,  
el4r.site\_dir ]  
FileUtils.mkdir\_p load\_path  
rb = “ **testtmp**.rb”  
home\_rb = File.expand\_path(rb, el4r\_homedir)  
site\_rb = File.expand\_path(rb, el4r.site\_dir)  
“$loaded = :OK”.writef(home\_rb)  
“$loaded = :NG”.writef(site\_rb)  
el4r\_load rb  
assert\_equal(:OK, $loaded)  
ensure  
FileUtils.rm\_f [home\_rb, site\_rb]  
end  
end

def test\_stdlib\_loaded  
assert\_equal(true, fboundp(:winconf\_push))  
end

def test\_winconf  
# make a winconf  
switch\_to\_buffer “a buffer”  
insert “string”  
pt = point  
# current\_window\_configuration does not works with xemacs -batch. I  
do not know why.  
assert( one\_window\_p )  
buf = current\_buffer

```
winconf_push

# alter the winconf
goto_char 1
split_window

winconf_pop

# revive the winconf
assert( one_window_p )
assert_equal(buf, current_buffer)
assert_equal(pt, point)

```

end

def test\_el4r\_output  
printf(“\t\n\ca!%s!”,“a”)  
print(1)  
assert\_equal(“\t\n\ca!a!1”, bufstr(“_el4r:output_”))  
end

def test\_process\_autoloads  
begin  
tmp = Dir.tmpdir  
autoload\_dir = “#{tmp}/autoload”  
FileUtils.mkdir\_p autoload\_dir  
$ary = []  
%w[01first.rb 02second.rb 03third.rb].each\_with\_index do |fn, i|  
open(File.join(autoload\_dir, fn), “w”){|f| f.write “$ary \<\<  
#{i}” }  
end  
el4r\_process\_autoloads autoload\_dir

```
  assert_equal [0,1,2], $ary
ensure
  FileUtils.rm_rf autoload_dir
end

```

end

def test\_eval\_after\_load  
begin  
tmp = Dir.tmpdir  
add\_to\_list :load\_path, tmp  
el = “#{tmp}/hoge.el”  
open(el, “w”){|f| f.write “(setq hoge 100)” }

```
  elvar.hoge = 1
  eval_after_load("hoge") do
    elvar.hoge = 200
  end
  assert_equal 1, elvar.hoge

  el_load "hoge"
  assert_equal 200, elvar.hoge
ensure
  FileUtils.rm_f el
end

```

end

def test\_define\_derived\_mode  
@passed = false

##### [derived]

```
define_derived_mode(:foo_mode, :fundamental_mode, "FOO", "doc") do
  @passed = true
end

```

##### [/derived]

```
assert_equal false, @passed
foo_mode
assert_equal true, @passed
assert_equal "foo-mode", elvar.major_mode.to_s

@passed = false
define_derived_mode("bar-mode", el(:foo_mode), "Bar") do
  @passed = true
end
assert_equal false, @passed
bar_mode
assert_equal true, @passed

define_derived_mode("baz-mode", el(:bar_mode), "Baz")
baz_mode
assert_equal "baz-mode", elvar.major_mode.to_s

```

end

def test\_define\_minor\_mode  
@passed = false

##### [minor-mode]

```
define_minor_mode(:a_minor_mode, "test minor mode") do
  @passed = true
end

```

##### [/minor-mode]

```
assert_equal false, @passed
a_minor_mode
assert_equal true, @passed
assert_equal true, elvar.a_minor_mode

```

end

# end of TestEl4r

end

# newbuf examples

class TestNewbuf \< Test::Unit::TestCase  
include ElMixin

def setup  
@bufname = “buffer-does-not-exist!!!”  
end

def teardown  
kill\_buffer(@bufname) if get\_buffer(@bufname)  
end

def setbuf  
set\_buffer @x  
end

def test\_create  
@x = newbuf(:name=\>@bufname)  
setbuf  
assert\_equal(true, bufferp(@x))  
assert\_equal(@bufname, buffer\_name(@x))  
assert\_equal(“”, buffer\_string)

```
y = newbuf(:name=>@bufname)
assert(eq(@x,y))

```

end

def test\_contents  
@x = newbuf(:name=\>@bufname, :contents=\>“foo”)  
setbuf  
assert\_equal(“foo”, buffer\_string)  
assert\_equal(4, “foo”.length+1)  
assert\_equal(4, point)

```
# buffer is erased
@x = newbuf(:name=>@bufname, :contents=>"bar")
setbuf
assert_equal("bar", buffer_string)

```

end

def test\_file  
begin  
file = Tempfile.path(“abcd”)  
@x = newbuf(:file=\>file)  
setbuf  
assert\_equal(file, buffer\_file\_name)  
assert\_equal(“abcd”, buffer\_string)  
ensure  
kill\_buffer nil  
File.unlink file  
end  
end

def test\_name\_and\_file  
begin  
file1 = Tempfile.path(“abcd”)  
@x = newbuf(:name=\>@bufname, :file=\>file1)  
setbuf  
assert\_equal(nil, buffer\_file\_name)  
assert\_equal(“abcd”, buffer\_string)

```
  # buffer is erased
  file2 = Tempfile.path("abcde")
  @x = newbuf(:name=>@bufname, :file=>file2)
  setbuf
  assert_equal("abcde", buffer_string)

ensure
  kill_buffer nil
  File.unlink file1
  File.unlink file2
end

```

end

def test\_argerror  
assert\_raises(ArgumentError){ newbuf }  
assert\_raises(ArgumentError){ newbuf(:name=\>nil) }  
assert\_raises(ArgumentError){ newbuf(1) }  
assert\_raises(ArgumentError){ newbuf(“1”) } # hmm.  
assert\_raises(ArgumentError){ newbuf(:name=\>@bufname, :line=\>“a”) }  
assert\_raises(ArgumentError){ newbuf(:name=\>@bufname, :point=\>“a”) }  
end

def test\_current\_line  
@x = newbuf(:name=\>@bufname, :contents=\>“a\nb\nc\nd”, :line=\>2)  
setbuf  
assert\_equal(“b”, char\_to\_string(char\_after))  
end

def test\_point  
@x = newbuf(:name=\>@bufname, :contents=\>“abcde”, :point=\>2)  
setbuf  
assert\_equal(“b”, char\_to\_string(char\_after))  
end

def test\_display  
elvar.pop\_up\_windows = true  
@x = newbuf(:name=\>@bufname, :display=\>true)  
assert(get\_buffer\_window(@x))  
assert\_nil(one\_window\_p)  
assert\_nil(eq(selected\_window, get\_buffer\_window(@x)))  
end

def test\_display\_pop  
elvar.pop\_up\_windows = true  
@x = newbuf(:name=\>@bufname, :display=\>:pop)  
assert(get\_buffer\_window(@x))  
assert\_nil(one\_window\_p)  
assert(eq(selected\_window, get\_buffer\_window(@x)))  
end

def test\_display\_only  
elvar.pop\_up\_windows = true  
@x = newbuf(:name=\>@bufname, :display=\>:only)  
assert(get\_buffer\_window(@x))  
assert(one\_window\_p)  
assert(eq(selected\_window, get\_buffer\_window(@x)))  
end

def test\_current  
@x = newbuf(:name=\>@bufname, :current=\>true)  
assert\_nil(get\_buffer\_window(@x))  
assert(eq(current\_buffer, @x))  
end

def test\_read\_only  
b1 = newbuf(:name=\>@bufname, :current=\>true, :read\_only=\>true,  
:contents=\>“a”)  
assert(eq(elvar.buffer\_read\_only, true))  
assert\_equal(“a”, buffer\_string)

```
b2 = newbuf(:name=>@bufname, :current=>true, :read_only=>true, 

```

:contents=\>“c”)  
assert(eq(b1,b2))  
assert(eq(elvar.buffer\_read\_only, true))  
assert\_equal(“c”, buffer\_string)  
end

def test\_bury  
buf = newbuf(:name=\>@bufname, :display=\>:pop, :bury=\>true)  
assert(eq(buf, (buffer\_list nil)[-1]))  
end

def test\_block  
buf = newbuf(:name=\>@bufname, :current=\>true) {  
text\_mode  
}  
mode = with(:with\_current\_buffer,buf){elvar.major\_mode}.to\_s  
assert\_equal(“text-mode”, mode)  
end  
end

class TestDefunWithinClass \< Test::Unit::TestCase

class Foo  
include ElMixin

```
def initialize(x)
  elvar.v = x[:value]
  defun(:twice_v) do
    elvar.v *= 2
  end

  defun(:str0) do
    do_str0 x[:str]
  end
end

def do_str0(str)
  (str*2).upcase
end

```

end

def test0  
Foo.new(:value=\>10, :str=\>“ab”)  
twice\_v  
assert\_equal(20, elvar.v)  
assert\_equal(“ABAB”, str0)  
end  
end

class TestElApp \< Test::Unit::TestCase

##### [ElApp]

class Foo \< ElApp  
def initialize(x)  
elvar.v = x[:value]  
defun(:twice\_v) do  
elvar.v \*= 2  
end

```
  defun(:str0) do
    do_str0 x[:str]
  end
end

def do_str0(str)
  (str*2).capitalize
end

```

end

##### [/ElApp]

def test0  
Foo.run(:value=\>10, :str=\>“ab”)  
twice\_v  
assert\_equal(20, elvar.v)  
assert\_equal(“Abab”, str0)  
end  
end

class TestSmartDefun \< Test::Unit::TestCase

##### [smart\_defun]

class SmartDefunSample \< ElApp  
def my\_square(x)  
x\*x  
end

```
defun(:testdefun, :interactive=>true) do |x|
  # This block is evaluated within a context of the SmartDefunSample 

```

INSTANCE.  
# Not a context of the SmartDefunSample class!!  
x ||= 16  
elvar.val = my\_square(x) # call an instance method.  
end  
end

##### [/smart\_defun]

def test\_defun  
SmartDefunSample.run  
elvar.val = 0  
testdefun(10)  
assert\_equal(100, elvar.val)  
call\_interactively(:testdefun)  
assert\_equal(256, elvar.val)  
end

class DefvarSample \< ElApp  
defvar(:blah\_blah, 2)  
defun(:setblah) {|x| elvar.blah\_blah = x }  
end

def test\_blah  
DefvarSample.run  
assert\_equal 2, el4r\_lisp\_eval(“blah-blah”)  
setblah(20)  
assert\_equal 20, el4r\_lisp\_eval(“blah-blah”)  
end

class LispEvalSample \< ElApp  
el4r\_lisp\_eval %((setq lisp-eval-sample 9999))  
end

def test\_lisp\_eval\_sample  
LispEvalSample.run  
assert\_equal 9999, elvar.lisp\_eval\_sample  
end

end
