Friday, August 26, 2011

Re: display faster output of external app

Reply to message «Re: display faster output of external app»,
sent 23:01:52 26 August 2011, Friday
by Gary Johnson:

> current line with g:output. Therefore you could replace
>
> call s:OPC.AddValueAtEndOfFile(g:output)
>
> with
>
> exe "s/ .*/ ".g:output."/"
>
> or even
>
> exe "s/ .*/ ".system(s:cmd)."/"
>
> That assumes that the output of s:cmd doesn't contain any slashes.
> I doubt that that is the bottleneck, though.
You must not write this. See :h s/\=, it is much better solution. I personally
will prefer

let line=getline('.')
call setline('.', line[:stridx(line, ' ')].g:output)
Advantages:
1. It does not spoil current substitute pattern.
2. It does not use regular expressions where they are not needed.
3. It can't be broken by user mappings (as original solution).
4. It can't be broken by &magic setting.

or (full original answer emulation):

let [d, before, after; d2]=matchlist(getline('.'),
\'\v^(\s*\S{,'.s:maxlinelength.'})(\S*)')
call setline('.', before.' '.g:output.after)
Advantages: 1., 3., 4. from above.

By the way, you forgot that `^' does not go to the start of the line. It goes to
first non-blank (or to the end of line if it does not exist).

Original message:
> On 2011-08-26, niva wrote:
> > Hi,
> >
> > I am calling an external app from vim like that :
> >
> > 1/ calling the application
> > let g:output=system(s:cmd)
> >
> > 2/ deleting old returned value and updating with new one
> > call s:OPC.AddValueAtEndOfFile(g:output)
> >
> > " Write value at end of line {{{1
> > fun! s:OPC.AddValueAtEndOfFile(value)
> >
> > exe "norm ^f d$"
> > exe "norm ^".s:maxlinelength."l"
> > exe "norm a ".a:value
> >
> > endfunction
> > ==============================================
> >
> > In fact this algo is called at each line to process in my entry file.
> > I have done another method that do this task on several lines 10.
> >
> > Is there another way to make the call of external app or the update
> > method faster ?
>
> Again generally speaking, it's faster to process text using Vim's
> commands and functions than to write a bunch of instructions that
> Vim has to parse as well as execute. Your AddValueAtEndOfFile()
> function simply replaces everything after the first space in the
> current line with g:output. Therefore you could replace
>
> call s:OPC.AddValueAtEndOfFile(g:output)
>
> with
>
> exe "s/ .*/ ".g:output."/"
>
> or even
>
> exe "s/ .*/ ".system(s:cmd)."/"
>
> That assumes that the output of s:cmd doesn't contain any slashes.
> I doubt that that is the bottleneck, though.
>
> Regards,
> Gary

No comments:

Post a Comment