Wednesday, June 2, 2010

Re: Different center alignment

Hi,

Pablo Giménez wrote:
>
> when writing dov files is very common to use this format:
>
> 2.1 User
> Manual *user-manual*
>
> In this example the begining of the line is in the first column and the
> end of it it at the latest column, usually 80 cos the textwidth by
> defualt is this.
> In between I use spaces to fill the line.
> I have tried with the :center command and other options from several
> plugins, all of them do the same with center alignment, they truncate
> the speaces in between to give you something like:
> 2.1 User Manual *user-manual*
> So basically I just need something that expand all the spaces to fill
> the whole line, somethign that expands rather than truncate.
> Any ideas?

:s/^\s*\(.\{-\}\)\s\+\(\*.*\*\)\s*$/\=submatch(1) . repeat(' ', &tw ? &tw : &wm ? winwidth(0) - &wm : winwidth(0) - len(submatch(1)) - len(submatch(2))) . submatch(2)/

(this is on line). I assumed the line always ends with a tag between two
stars, so the first part

^\s*\(.\{-\}\)\s\+\(\*.*\*\)\s*$

matches the left text with "\(.\{-\}\)", some whitespace with "\s\+" and
the tag with "\(\*.*\*\)". Leading and trailing whitespace are ignored
and the text and the tag are captured.

The replacement text

\=submatch(1) . repeat(' ', &tw ? &tw : &wm ? winwidth(0) - &wm : winwidth(0) - len(submatch(1)) - len(submatch(2))) . submatch(2)

concatenates three parts. The first and third part are straightforward:
these are the captured submatches. The second part is a bit more complex.
We want to insert some spaces so we use the repeat() function. The first
parameter says what to repeat and the second one how often. The count
is calculated by first determining the maximum length of the whole line
with

&tw ? &tw : &wm ? winwidth(0) - &wm : winwidth(0)

This says: If 'textwidth' is set, use it; else if 'wrapmargin' is set,
subtract its value from the width of the current window; else use the
whole window width.

From this width the lengths of both submatches are subtracted, resulting
in the number of spaces to insert between the text and the tag.

Regards,
Jürgen

--
Sometimes I think the surest sign that intelligent life exists elsewhere
in the universe is that none of it has tried to contact us. (Calvin)

--
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

No comments:

Post a Comment