Return to Snippet

Revision: 51360
at October 18, 2013 21:10 by guai


Updated Code
buffer.setMode("ruby");
buffers = jEdit.getBuffers();
for(i = 0; i < buffers.length; i++) {
    jrc = buffers[i].getProperty("JRubyContainer");
    if(jrc != null) { break; }
}
if(jrc == null) {
	jarfilenamefilter = new java.io.FilenameFilter() {
		boolean accept(File dir, String name) {
			return name.endsWith(".jar");
		}
	};
    libfiles = new File(System.getenv("JRUBY_HOME") + "/lib/").listFiles(jarfilenamefilter);
    cl = this.getClass().getClassLoader();
    for(i = 0; i < libfiles.length; i++)
        cl.addURL(new URL("jar:" + libfiles[i].toURL().toString() + "!/"));
    jrc = new org.jruby.embed.ScriptingContainer();
	jrc.setClassLoader(cl);
}
for(i = 0; i < buffers.length; i++) {
    buffers[i].setProperty("JRubyContainer", jrc);
}
outbuffer = buffer.getProperty("outbuffer");
if((outbuffer == null) || outbuffer.isClosed()) {
    outbuffer = jEdit.newFile(view);
    buffer.setProperty("outbuffer", outbuffer);
}
out = new java.io.StringWriter();
jrc.setWriter(out);
String text() {
    view.showBuffer(buffer);
    res = textArea.getSelectedText();
    if(res == null || res.length() == 0) { res = buffer.getText(); }
    return res;
}
text = text();
jrc.remove("DATA");
ma = java.util.regex.Pattern.compile("(?sm)\\A(.+)^__END__$(.*)\\z").matcher(text);
if(ma.matches()) {
    text = ma.group(1);
    jrc.put("DATA", ma.group(2));
}
result = jrc.runScriptlet(new java.io.ByteArrayInputStream(("# -*- encoding : utf-8 -*-\n\n" + text).getBytes("UTF-8")), "(eval)");
outbuffer.remove(0, outbuffer.getLength());
outbuffer.insert(0, out.toString());
if(result != null) {
	outbuffer.insert(outbuffer.getLength(),"\n\tResult: " + result);
    view.showBuffer(outbuffer);
}

Revision: 51359
at July 30, 2013 22:26 by guai


Updated Code
buffer.setMode("ruby");
buffers = jEdit.getBuffers();
for(i = 0; i < buffers.length; i++) {
    jrc = buffers[i].getProperty("JRubyContainer");
    if(jrc != null) { break; }
}
if(jrc == null) {
	jarfilenamefilter = new java.io.FilenameFilter() {
		boolean accept(File dir, String name) {
			return name.endsWith(".jar");
		}
	};
    libfiles = new File(System.getenv("JRUBY_HOME") + "/lib/").listFiles(jarfilenamefilter);
    cl = this.getClass().getClassLoader();
    for(i = 0; i < libfiles.length; i++)
        cl.addURL(new URL("jar:" + libfiles[i].toURL().toString() + "!/"));
    jrc = cl.loadClass("org.jruby.embed.ScriptingContainer").newInstance();
    //jrc.setCompatVersion(cl.loadClass("org.jruby.CompatVersion").getEnumConstants()[2]); //JRUBY1_9
    //jrc.setKCode(cl.loadClass("org.jruby.util.KCode").getEnumConstants()[2]); //UTF8
    //Macros.message(view, jrc.getClassLoader().getClass().getName());
	jrc.setClassLoader(cl);
}
for(i = 0; i < buffers.length; i++) {
    buffers[i].setProperty("JRubyContainer", jrc);
}
outbuffer = buffer.getProperty("outbuffer");
if((outbuffer == null) || outbuffer.isClosed()) {
    outbuffer = jEdit.newFile(view);
    buffer.setProperty("outbuffer", outbuffer);
}
out = new java.io.StringWriter();
jrc.setWriter(out);
String text() {
    view.showBuffer(buffer);
    res = textArea.getSelectedText();
    if(res == null || res.length() == 0) { res = buffer.getText(); }
    return res;
}
text = text();
jrc.remove("DATA");
ma = java.util.regex.Pattern.compile("(?sm)\\A(.+)^__END__$(.*)\\z").matcher(text);
if(ma.matches()) {
    text = ma.group(1);
    jrc.put("DATA", ma.group(2));
}
result = jrc.runScriptlet(new java.io.ByteArrayInputStream(("# -*- encoding : utf-8 -*-\n\n" + text).getBytes("UTF-8")), "(eval)");
outbuffer.remove(0, outbuffer.getLength());
outbuffer.insert(0, out.toString());
if(result != null) {
	outbuffer.insert(outbuffer.getLength(),"\n\tResult: " + result);
    view.showBuffer(outbuffer);
}

Revision: 51358
at April 10, 2013 21:38 by guai


Updated Code
buffer.setMode("ruby");
buffers = jEdit.getBuffers();
for(i = 0; i < buffers.length; i++) {
    jrc = buffers[i].getProperty("JRubyContainer");
    if(jrc != null) { break; }
}
if(jrc == null) {
	jarfilenamefilter = new java.io.FilenameFilter() {
		boolean accept(File dir, String name) {
			return name.endsWith(".jar");
		}
	};
    libfiles = new File(System.getenv("JRUBY_HOME") + "/lib/").listFiles(jarfilenamefilter);
    urls = new URL[libfiles.length];
    for(i = 0; i < libfiles.length; i++) {
        urls[i] = new URL("jar:" + libfiles[i].toURL().toString() + "!/");
    }
    jrubycl = new java.net.URLClassLoader(urls, this.getClass().getClassLoader());
    jrc = jrubycl.loadClass("org.jruby.embed.ScriptingContainer").newInstance();
    //jrc.setCompatVersion(jrubycl.loadClass("org.jruby.CompatVersion").getEnumConstants()[2]); //JRUBY1_9
    //jrc.setKCode(jrubycl.loadClass("org.jruby.util.KCode").getEnumConstants()[2]); //UTF8
    jrc.setClassLoader(jrubycl);
}
for(i = 0; i < buffers.length; i++) {
    buffers[i].setProperty("JRubyContainer", jrc);
}
outbuffer = buffer.getProperty("outbuffer");
if((outbuffer == null) || outbuffer.isClosed()) {
    outbuffer = jEdit.newFile(view);
    buffer.setProperty("outbuffer", outbuffer);
}
out = new java.io.StringWriter();
jrc.setWriter(out);
String text() {
    view.showBuffer(buffer);
    res = textArea.getSelectedText();
    if(res == null || res.length() == 0) { res = buffer.getText(); }
    return res;
}
text = text();
jrc.remove("DATA");
ma = java.util.regex.Pattern.compile("(?sm)\\A(.+)^__END__$(.*)\\z").matcher(text);
if(ma.matches()) {
    text = ma.group(1);
    jrc.put("DATA", ma.group(2));
}
result = jrc.runScriptlet(new java.io.ByteArrayInputStream(("# -*- encoding : utf-8 -*-\n" + text).getBytes("UTF-8")), "script.rb");
outbuffer.remove(0, outbuffer.getLength());
outbuffer.insert(0, out.toString());
outbuffer.insert(outbuffer.getLength(),"\n\tResult: " + result);
if(result != null) {
    view.showBuffer(outbuffer);
}

Revision: 51357
at February 27, 2013 18:17 by guai


Updated Code
buffer.setMode("ruby");
buffers = jEdit.getBuffers();
for(i = 0; i < buffers.length; i++) {
    jrc = buffers[i].getProperty("JRubyContainer");
    if(jrc != null) { break; }
}
if(jrc == null) {
	jarfilenamefilter = new java.io.FilenameFilter() {
		boolean accept(File dir, String name) {
			return name.endsWith(".jar");
		}
	};
    libfiles = new File(System.getenv("JRUBY_HOME") + "/lib/").listFiles(jarfilenamefilter);
    urls = new URL[libfiles.length];
    for(i = 0; i < libfiles.length; i++) {
        urls[i] = new URL("jar:" + libfiles[i].toURL().toString() + "!/");
    }
    jrubycl = new java.net.URLClassLoader(urls, this.getClass().getClassLoader());
    jrc = jrubycl.loadClass("org.jruby.embed.ScriptingContainer").newInstance();
    jrc.setClassLoader(jrubycl);
}
for(i = 0; i < buffers.length; i++) {
    buffers[i].setProperty("JRubyContainer", jrc);
}
outbuffer = buffer.getProperty("outbuffer");
if((outbuffer == null) || outbuffer.isClosed()) {
    outbuffer = jEdit.newFile(view);
    buffer.setProperty("outbuffer", outbuffer);
}
out = new java.io.StringWriter();
jrc.setWriter(out);
String text() {
    view.showBuffer(buffer);
    res = textArea.getSelectedText();
    if(res == null || res.length() == 0) { res = buffer.getText(); }
    return res;
}
text = text();
jrc.remove("DATA");
ma = java.util.regex.Pattern.compile("(?sm)\\A(.+)^__END__$(.*)\\z").matcher(text);
if(ma.matches()) {
    text = ma.group(1);
    jrc.put("DATA", ma.group(2));
}
result = jrc.runScriptlet(text);
outbuffer.remove(0, outbuffer.getLength());
outbuffer.insert(0, out.toString());
outbuffer.insert(outbuffer.getLength(),"\n\tResult: " + result);
if(result != null) {
    view.showBuffer(outbuffer);
}

Revision: 51356
at February 27, 2013 18:15 by guai


Updated Code
buffer.setMode("ruby");
buffers = jEdit.getBuffers();
for(i = 0; i < buffers.length; i++) {
    jrc = buffers[i].getProperty("JRubyContainer");
    if(jrc != null) { break; }
}
jarfilenamefilter = new java.io.FilenameFilter() {
	boolean accept(File dir, String name) {
		return name.endsWith(".jar");
	}
};
if(jrc == null) {
    libfiles = new File(System.getenv("JRUBY_HOME") + "/lib/").listFiles(jarfilenamefilter);
    urls = new URL[libfiles.length];
    for(i = 0; i < libfiles.length; i++) {
        urls[i] = new URL("jar:" + libfiles[i].toURL().toString() + "!/");
    }
    jrubycl = new java.net.URLClassLoader(urls, this.getClass().getClassLoader());
    jrc = jrubycl.loadClass("org.jruby.embed.ScriptingContainer").newInstance();
    jrc.setClassLoader(jrubycl);
}
for(i = 0; i < buffers.length; i++) {
    buffers[i].setProperty("JRubyContainer", jrc);
}
outbuffer = buffer.getProperty("outbuffer");
if((outbuffer == null) || outbuffer.isClosed()) {
    outbuffer = jEdit.newFile(view);
    buffer.setProperty("outbuffer", outbuffer);
}
out = new java.io.StringWriter();
jrc.setWriter(out);
String text() {
    view.showBuffer(buffer);
    res = textArea.getSelectedText();
    if(res == null || res.length() == 0) { res = buffer.getText(); }
    return res;
}
text = text();
jrc.remove("DATA");
ma = java.util.regex.Pattern.compile("(?sm)\\A(.+)^__END__$(.*)\\z").matcher(text);
if(ma.matches()) {
    text = ma.group(1);
    jrc.put("DATA", ma.group(2));
}
result = jrc.runScriptlet(text);
outbuffer.remove(0, outbuffer.getLength());
outbuffer.insert(0, out.toString());
outbuffer.insert(outbuffer.getLength(),"\n\tResult: " + result);
if(result != null) {
    view.showBuffer(outbuffer);
}

Revision: 51355
at December 30, 2011 18:17 by guai


Updated Code
buffer.setMode("ruby");
buffers = jEdit.getBuffers();
for(i = 0; i < buffers.length; i++) {
    jrc = buffers[i].getProperty("JRubyContainer");
    if(jrc != null) { break; }
}
if(jrc == null) {
    libfiles = new File(System.getenv("JRUBY_HOME") + "/lib/").listFiles();
    urls = new URL[libfiles.length];
    for(i = 0; i < libfiles.length; i++) {
        urls[i] = new URL("jar:" + libfiles[i].toURL().toString() + "!/");
    }
    jrubycl = new java.net.URLClassLoader(urls, this.getClass().getClassLoader());
    jrc = jrubycl.loadClass("org.jruby.embed.ScriptingContainer").newInstance();
    jrc.setClassLoader(jrubycl);
}
for(i = 0; i < buffers.length; i++) {
    buffers[i].setProperty("JRubyContainer", jrc);
}
outbuffer = buffer.getProperty("outbuffer");
if((outbuffer == null) || outbuffer.isClosed()) {
    outbuffer = jEdit.newFile(view);
    buffer.setProperty("outbuffer", outbuffer);
}
out = new java.io.StringWriter();
jrc.setWriter(out);
String text() {
    view.showBuffer(buffer);
    res = textArea.getSelectedText();
    if(res == null || res.length() == 0) { res = buffer.getText(); }
    return res;
}
text = text();
jrc.remove("DATA");
ma = java.util.regex.Pattern.compile("(?sm)\\A(.+)^__END__$(.*)\\z").matcher(text);
if(ma.matches()) {
    text = ma.group(1);
    jrc.put("DATA", ma.group(2));
}
result = jrc.runScriptlet(text);
outbuffer.remove(0, outbuffer.getLength());
outbuffer.insert(0, out.toString());
outbuffer.insert(outbuffer.getLength(),"\n\tResult: " + result);
if(result != null) {
    view.showBuffer(outbuffer);
}

Revision: 51354
at September 23, 2011 01:55 by guai


Updated Code
buffer.setMode("ruby");
buffers = jEdit.getBuffers();
for(i = 0; i < buffers.length; i++) {
    jrc = buffers[i].getProperty("JRubyContainer");
    if(jrc != null) { break; }
}
if(jrc == null) {
    libfiles = new File(System.getenv("JRUBY_HOME") + "/lib/").listFiles();
    urls = new URL[libfiles.length];
    for(i = 0; i < libfiles.length; i++) {
        urls[i] = new URL("jar:" + libfiles[i].toURL().toString() + "!/");
    }
    jrubycl = new java.net.URLClassLoader(urls, this.getClass().getClassLoader());
    jrc = jrubycl.loadClass("org.jruby.embed.ScriptingContainer").newInstance();
    jrc.setClassLoader(jrubycl);
}
for(i = 0; i < buffers.length; i++) {
    buffers[i].setProperty("JRubyContainer", jrc);
}
outbuffer = buffer.getProperty("outbuffer");
if((outbuffer == null) || outbuffer.isClosed()) {
    outbuffer = jEdit.newFile(view);
    buffer.setProperty("outbuffer", outbuffer);
}
out = new java.io.StringWriter();
jrc.setWriter(out);
String text() {
    res = textArea.getSelectedText();
    if(res == null || res.length() == 0) { res = buffer.getText(); }
    return res;
}
text = text();
jrc.remove("DATA");
ma = java.util.regex.Pattern.compile("(?sm)\\A(.+)^__END__$(.*)\\z").matcher(text);
if(ma.matches()) {
    text = ma.group(1);
    jrc.put("DATA", ma.group(2));
}
result = jrc.runScriptlet(text);
outbuffer.remove(0, outbuffer.getLength());
outbuffer.insert(0, out.toString());
outbuffer.insert(outbuffer.getLength(),"\n\tResult: " + result);
view.showBuffer(outbuffer);

Revision: 51353
at September 22, 2011 01:10 by guai


Updated Code
buffer.setMode("ruby");
buffers = jEdit.getBuffers();
for(i = 0; i < buffers.length; i++) {
    jrc = buffers[i].getProperty("JRubyContainer");
    if(jrc != null) { break; }
}
if(jrc == null) {
    libfiles = new File(System.getenv("JRUBY_HOME") + "/lib/").listFiles();
    urls = new URL[libfiles.length];
    for(i = 0; i < libfiles.length; i++) {
        urls[i] = new URL("jar:" + libfiles[i].toURL().toString() + "!/");
    }
    jrubycl = new java.net.URLClassLoader(urls, this.getClass().getClassLoader());
    jrc = jrubycl.loadClass("org.jruby.embed.ScriptingContainer").newInstance();
    jrc.setClassLoader(jrubycl);
}
for(i = 0; i < buffers.length; i++) {
    buffers[i].setProperty("JRubyContainer", jrc);
}
outbuffer = buffer.getProperty("outbuffer");
if((outbuffer == null) || outbuffer.isClosed()) {
    outbuffer = jEdit.newFile(view);
    buffer.setProperty("outbuffer", outbuffer);
}
out = new java.io.StringWriter();
jrc.setWriter(out);
String text() {
    res = textArea.getSelectedText();
    if(res == null || res.length() == 0) { res = buffer.getText(); }
    return res;
}
result = jrc.runScriptlet(text());
outbuffer.remove(0, outbuffer.getLength());
outbuffer.insert(0, out.toString());
outbuffer.insert(outbuffer.getLength(),"\n\tResult: " + result);
view.showBuffer(outbuffer);

Revision: 51352
at September 22, 2011 00:47 by guai


Initial Code
buffer.setMode("ruby");
buffers = jEdit.getBuffers();
for(i = 0; i < buffers.length; i++) {
    jrc = buffers[i].getProperty("JRubyContainer");
    if(jrc != null) { break; }
}
if(jrc == null) {
    libfiles = new File(System.getenv("JRUBY_HOME") + "/lib/").listFiles();
    urls = new URL[libfiles.length];
    for(i = 0; i < libfiles.length; i++) {
        urls[i] = new URL("jar:" + libfiles[i].toURL().toString() + "!/");
    }
    jrubycl = new java.net.URLClassLoader(urls, this.getClass().getClassLoader());
    jrc = jrubycl.loadClass("org.jruby.embed.ScriptingContainer").newInstance();
    jrc.setClassLoader(jrubycl);
}
for(i = 0; i < buffers.length; i++) {
    buffers[i].setProperty("JRubyContainer", jrc);
}
outbuffer = buffer.getProperty("outbuffer");
if((outbuffer == null) || outbuffer.isClosed()) {
    outbuffer = jEdit.newFile(view);
    buffer.setProperty("outbuffer", outbuffer);
}
out = new ByteArrayOutputStream();
jrc.setOutput(new PrintStream(out));
String text() {
    res = textArea.getSelectedText();
    if(res == null || res.length() == 0) { res = buffer.getText(); }
    return res;
}
result = jrc.runScriptlet(text());
outbuffer.remove(0, outbuffer.getLength());
outbuffer.insert(0, out.toString());
outbuffer.insert(outbuffer.getLength(),"\n\tResult: " + result);
view.showBuffer(outbuffer);

Initial URL

                                

Initial Description
Almost the same as my snippet for groovy, but for jruby

Initial Title
jEdit macro integrating jRuby

Initial Tags

                                

Initial Language
Java