Tuesday, December 1, 2009

Re: Custom Paragraph Formatting with gq

Robert Chan schrieb:
> I have logbook entries that have the following structure:
>
> [entry title 1]
> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse blandit
> ipsum vel elit ultrices pharetra rhoncus tellus aliquam. Donec quis dolor ac
> elit vestibulum rhoncus. Quisque adipiscing dolor vitae urna molestie in
> vulputate arcu mattis. Donec felis augue, aliquam dictum mattis vitae,
> pretium in massa.
>
> What I would like to do is apply 'gq' command to format all the paragraphs
> in the file, but the [entry title 1] header also gets formatted (which I
> don't want.) So it turns out something like this:
>
> [entry title 1] Lorem ipsum dolor sit amet, consectetur adipiscing elit.
> Suspendisse blandit ipsum vel elit ultrices pharetra rhoncus tellus aliquam.
> Donec quis dolor ac elit vestibulum rhoncus. Quisque adipiscing dolor vitae
> urna molestie in vulputate arcu mattis. Donec felis augue, aliquam dictum
> mattis vitae, pretium in massa.
>
> I want it to result in this way:
>
> [entry title 1]
> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse
> blandit
> ipsum vel elit ultrices pharetra rhoncus tellus aliquam. Donec quis dolor
> ac
> elit vestibulum rhoncus. Quisque adipiscing dolor vitae urna molestie in
> vulputate arcu mattis. Donec felis augue, aliquam dictum mattis vitae,
> pretium
> in massa.
>
> Any ideas?
>
> (I've been reading 'formatoptions' and fo-table but could not find anything
> of related to custom paragraph settings.)
>
> Thanks.

If it wasn't so damn unusable, I would recommend 'formatexpr':
:h 'fex

But you can also go with a user command:


com! -bar -range=% GqLogBook <line1>,<line2>call s:GqLogBook()

func! s:GqLogBook() range
exec a:firstline. "mark ["
exec a:lastline. "mark ]"
'[,']s/^\[.*]$/&\r/
normal! '[gq']
'[,']s/^\[.*]\zs\n$//
endfunc


This is supposed to work by temporarily adding empty lines below entry titles
before invoking the normal formatting command.

--
Andy

--
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php

No comments: