On Tue, 27 Jun 2006 at 4:51pm, Benji Fisher wrote:
> On Tue, Jun 27, 2006 at 03:56:22PM -0400, Dimitriy V. Masterov wrote:
> > Thank you very much for your suggestion. I'm still having trouble
> > yanking the highlighted text instead of the line the cursor was on.
> >
> > I was able to do something like this in jEdit using
> > textArea.getSelectedText(). Is there something analogous for Vim?
> >
> > My jEdit code is here:
> >
> > rundolines() {
> > // Copy selected text
> > String text = textArea.getSelectedText();
> >
> > // Create a temp file
> > File temp = File.createTempFile("dofilelines", ".do");
> >
> > // Delete temp file when program exits.
> > temp.deleteOnExit();
> >
> > // Write to temp file
> > BufferedWriter out = new BufferedWriter(new FileWriter(temp));
> > out.write(text);
> > out.close();
> >
> > // Run the temporary file in Stata
> > String command="C:\\Program Files\\Scripts\\rundo.exe " + "\"" +
> > temp + "\"";
> > exec(command);
> > }
> >
> > rundolines();
>
> Are you having trouble yanking the text "manually" or from a
> script? If you are in Visual mode, then change to Command-Line mode to
> execute a :command or a Function(), then you have to get back into
> Visual mode in order to yank the text you want. Based on your jEdit
> code above, I am guessing you want something like this:
>
> fun! Rundolines()
> " Copy Visual text into register a
> normal! gv"ay
> let temp = tempname() . ".do"
> execute "split" temp
> " or :edit or :tabedit instead of :split
> put a
> let command = ...
> execute command
> " I assume we are still editing the temp file now.
> q!
> endfun
>
> Most users seem to forget that :execute can take more than one argument.
> Thus my first :execute line should do the same as
>
> execute "split " . temp
>
> Disclaimer: the entire function is untested. (Just in case it is not
> clear that the last :let line is incomplete.)
>
> HTH --Benji Fisher
>
You can do this more elegantly using the new Vim7 features (again
untested):
function! Rundolines()
let selectedLines = getbufline('%', line("'<"), line("'>"))
if col("'>") < strlen(getline(line("'>")))
let selectedLines[-1] = strpart(selectedLines[-1], 0, col("'>"))
endif
if col("'<") != 1
let selectedLines[0] = strpart(selectedLines[0], col("'<")-1)
endif
let temp = tempname() . ".do"
call writefile(selectedLines, temp)
" Do something with temp file.
call delete(temp)
endfunction
Of course, you can also mix the normal command and still use the
writefile() like this (note, unlike the getbuflist() you have to be
in the buffer before you use this approach):
function! Rundolines()
normal! gv"ay
let selectedLines = split(@a, "\n")
let temp = tempname() . ".do"
call writefile(selectedLines, temp)
" Do something with temp file.
call delete(temp)
endfunction
It is better to save and restore the @a value though. Considering your
jEdit code, you would probably prefer the first approach.
--
HTH,
Hari
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com