|
From: Daniel W. <the...@gm...> - 2004-10-02 17:51:25
|
On Saturday 02 October 2004 18:58, Owen Densmore wrote:
> I'd like a macro that would execute a unix command on the current
> selection, replacing it with the result of the command. The command
> would take as input the existing selection.
>
> This is possible with the beanshell, but as far as I know, not the Unix
> shell, right? Possibly I could just build a beanshell command that
> called the shell?
this should be no problem. a year ago i wrote a macro some time ago
to work around some weird encoding problems with the clipboard.
this macro inserts the output of xclip -o at the caret position. maybe
this helps you to get started.
daniel
void PasteX11Selection() {
/** read a stream into a String with the platform default encoding */
String readStream(InputStream in) {
Reader reader = new InputStreamReader(in);
char[] chunk = new char[1024];
StringBuffer out = new StringBuffer();
for (;;) {
int read = reader.read(chunk, 0, chunk.length);
if (read < 0) break;
out.append(chunk, 0, read);
}
reader.close();
return out.toString();
}
// use xclip to access X11-selection content
String[] xclip = { "xclip", "-o" };
Process proc = Runtime.getRuntime().exec(xclip);
String stdout = readStream(proc.InputStream);
String stderr = readStream(proc.ErrorStream);
proc.waitFor();
proc.exitValue();
// insert text at caret position
int caret = textArea.CaretPosition;
buffer.insert(caret, stdout);
textArea.CaretPosition = caret;
}
// check buffer vor changeability
if (buffer.isReadOnly()) {
Macros.error(view, "this file is read only.");
return;
}
PasteX11Selection();
|