Monday, June 10, 2013

Re: Measuring lines

meino.cramer@gmx.de a écrit:
> Paul Isambert <zappathustra@free.fr> [13-06-10 04:00]:
> > Tim Chase<vim@tim.thechases.com> a écrit:
> > > On 2013-06-09 20:37, meino.cramer@gmx.de wrote:
> > > > Paul Isambert <zappathustra@free.fr> [13-06-09 20:28]:
> > > > > I'm not sure I understand you correctly, but perhaps the following
> > > >> will help:
> > > >>
> > > >> function! s:bars ()
> > > [snip]
> > > >> endfunction
> > > >> com! -buffer Bars call s:bars()
> > > >>
> > > >> | | | | abcde | |
> > > >> produces:
> > > >> | | | | abcde | |
> > > >> 0 12 3 10 7 1
> > > >
> > > > yes,yes,yes,!!! Exactly what I want! THANKS A LOT! You helped me
> > > > more as you may think :) ! Great help! 8)
> > > ...
> > > >> (I'm sure there's a Vim wizard out there who can do all that with
> > > >> only ":substitute".)
> > >
> > > Though just a first pass, since Paul threw down the challenge:
> > >
> > > :s/.*/\=submatch(0)."\n".(join(map(split(submatch(0), '|', 1),
> > > 'printf("%".strlen(v:val)."s", strlen(v:val))')))/g
> > >
> > > gives a pretty close approximation to his solution as a
> > > one-line :substitute command. Differences I notice:
> > >
> > > 1) it has one extra 0-width field listed at the end which shows the
> > > width of the last column (in Paul's sample input, it was zero-width,
> > > but if there were additional characters, it would list how wide the
> > > field was)
> > >
> > > 2) it right-justifies the number with the pipe rather than
> > > left-justifying it.
> > >
> > > So on Paul's example, I get
> > >
> > > | | | | abcde | |
> > > 0 12 3 10 7 1 0
> >
> > The following is, I hope, an improvement: it removes the trailing zero
> > and fixes cases when the line doesn't begin with a pipe. The best part
> > of it, though, is the the "nohl" at the end :)
> >
> > :s/.*/\=submatch(0)."\n".substitute(join(map(split(submatch(0), '|', 1), 'printf("%".(strlen(v:val)+1)."s", strlen(v:val))'), ''), '\s*0$', '', '')/g | nohl
> >
> > Best,
> > Paul
> >
> > --
> > --
> > You received this message from the "vim_use" maillist.
> > Do not top-post! Type your reply below the text you are replying to.
> > For more information, visit http://www.vim.org/maillist.php
> >
> > ---
> > You received this message because you are subscribed to the Google Groups "vim_use" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
> > For more options, visit https://groups.google.com/groups/opt_out.
> >
> >
>
> Hi Paul,
>
> this is really a very use- and helpful peace of code!
> The only thing I would like to add is a second line below the first
> line of numbers, which would add the absolute offset of each pipe
> symbol from the beginning of the line...
> What need I to change?

If you're still using my first solution (which could be made much more
compact following Tim's code, but I didn't do that here), then switch
to this new version:

function! s:bars ()
let newline = ""
let newline2 = ""
let lineno = line(".")
let str = getline(lineno)
let cnt = stridx(str, "|")
let offset = 0
let offset2 = 0
let total = 0
while cnt >= 0
let newline .= repeat(" ", cnt-offset) . cnt
let total += total > 0 ? cnt+1 : cnt
let newline2 .= repeat(" ", cnt-offset2) . total
let offset = strlen(cnt)-1
let offset2 = strlen(total)-1
let str = strpart(str, cnt+1)
let cnt = stridx(str, "|")
endwhile
call append(lineno, [newline, newline2])
endfunction
com! -buffer Bars call s:bars()

This assumes that by "absolute offset from the beginning of the line"
you mean "number of characters before the pipe", i.e. "column-1". A
slight modification is in order if you simply want the column number
(simply replace the line "let total += total > 0 ? cnt+1 : cnt" with
"let total += cnt+1").

Best,
Paul

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: