Monday, February 21, 2011

Re: Including into the current buffer from an external source

Reply to message «Re: Including into the current buffer from an external
source»,
sent 11:29:15 21 February 2011, Monday
by Christian Brabandt:

Your emulation is incorrect: try to get the same output as ``read! echo $'\0'''.
In case of reading file my suggestion works just as well as yours, but does not
require so many code. If you want to emulate `read!' correctly, call `read!'
directly and then use difference between number of lines before and after read
is done, this is much less code and much less possibilities to make an error.

Original message:
> Hi Rostyslaw!
>
> On Sa, 19 Feb 2011, Rostyslaw Lewyckyj wrote:
> > Christian Brabandt wrote:
> >> Are you looking for the :r command? You can input either another file
> >> or even read in the ouput of another command.
> >>
> >> See
> >>
> >> :h :read
> >> :h :read!
> >
> > Well sort of. :read inserts the whole file after the current line.
> > But my memory insists that there is/was a way to select a specified
> > range of records from the source file and insert them at a specific
> > place in the current file. For example: Insert records 25 through 50
> > from file source.txt after record 101 of the current buffer.
>
> Not that I know of. But you can easily script it yourself.
>
> com! -bang -nargs=* -range -complete=file Read :<line1>,<line2>call
> \<sid>Read(<q-bang>, <f-args>)
>
> fun! <sid>Read(bang, cmd, ...) range
> if a:0 == 0
> let args = [0,-1]
> else
> if (a:1 !~ '^\d\+$' && a:1 !~ '^\d\+$') || len(a:000) > 2
> echohl Error
> echom "Usage: Read[!] file|command [0 ...]"
> echohl Normal
> return
> else
> let args = [(a:1 - 1), (a:2 - 1)]
> endif
> endif
> if a:bang == '!'
> let a=split(system(a:cmd), "\n")
> else
> let a=readfile(a:cmd)
> endif
> let _c = getpos('.')
> exe append(a:firstline, a[args[0] : args[1]])
> call setpos('.', _c)
> endfun
>
> Use :[count]Read filename 1 100
> to input the first 100 records below line [count] (default current line)
>
> Use :[count]Read! command 20 50
> to input the records 20 till 50 below line [count]
>
> If you leave out the last 2 parameters, it will input all records found).
>
> regards,
> Christian

No comments:

Post a Comment