Loading ...
Sorry, an error occurred while loading the content.

Custom Paragraph Formatting with gq

Expand Messages
  • Robert Chan
    I have logbook entries that have the following structure: [entry title 1] Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse blandit ipsum
    Message 1 of 9 , Nov 30, 2009
    • 0 Attachment
      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.

      --
      You received this message from the "vim_use" maillist.
      For more information, visit http://www.vim.org/maillist.php
    • Maxim Kim
      ... Last time I tried to do almost the same I didn t find the way to do it. But ... could give you some information. -- You received this message from the
      Message 2 of 9 , Dec 1, 2009
      • 0 Attachment
        On 1 дек, 09:05, Robert Chan <vibrant.softw...@...> wrote:
        > I have logbook entries that have the following structure:
        >
        > Any ideas?
        >
        > (I've been reading 'formatoptions' and fo-table but could not find anything
        > of related to custom paragraph settings.)
        >
        > Thanks.

        Last time I tried to do almost the same I didn't find the way to do
        it.
        But
        :h paragraph
        could give you some information.

        --
        You received this message from the "vim_use" maillist.
        For more information, visit http://www.vim.org/maillist.php
      • Andy Wokula
        ... But you can also go with a user command: com! -bar -range=% GqLogBook , call s:GqLogBook() func! s:GqLogBook() range exec a:firstline. mark
        Message 3 of 9 , Dec 1, 2009
        • 0 Attachment
          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
        • Andy Wokula
          ... Having sent that, I immediately regretted it. In this case, it isn t that hard: setlocal formatexpr=FexLogBook() func! FexLogBook() if mode() =~# [iR]
          Message 4 of 9 , Dec 1, 2009
          • 0 Attachment
            Andy Wokula schrieb:
            > Robert Chan schrieb:
            >> I have logbook entries that have the following structure:

            > If it wasn't so d*** unusable, I would recommend 'formatexpr':
            > :h 'fex

            Having sent that, I immediately regretted it.
            In this case, it isn't that hard:

            setlocal formatexpr=FexLogBook()

            func! FexLogBook()
            if mode() =~# '[iR]'
            return 1
            endif
            exec v:lnum. "mark ["
            exec (v:lnum + v:count - 1). "mark ]"
            '[,']s/^\[.*]$/&\r/
            setl fex=
            normal! '[gq']
            setl fex=FexLogBook()
            '[,']s/^\[.*]\zs\n$//
            return
            endfunc

            --
            Andy

            --
            You received this message from the "vim_use" maillist.
            For more information, visit http://www.vim.org/maillist.php
          • Maxim Kim
            ... Wow! Thanks, Andy! -- You received this message from the vim_use maillist. For more information, visit http://www.vim.org/maillist.php
            Message 5 of 9 , Dec 1, 2009
            • 0 Attachment
              On 1 дек, 17:54, Andy Wokula <anw...@...> wrote:
              > Andy Wokula schrieb:
              >
              > > Robert Chan schrieb:
              > >> I have logbook entries that have the following structure:
              > > If it wasn't so d*** unusable, I would recommend 'formatexpr':
              > >     :h 'fex
              >
              > Having sent that, I immediately regretted it.
              > In this case, it isn't that hard:
              >
              > setlocal formatexpr=FexLogBook()
              >
              > func! FexLogBook()
              >     if mode() =~# '[iR]'
              >         return 1
              >     endif
              >     exec v:lnum. "mark ["
              >     exec (v:lnum + v:count - 1). "mark ]"
              >     '[,']s/^\[.*]$/&\r/
              >     setl fex=
              >     normal! '[gq']
              >     setl fex=FexLogBook()
              >     '[,']s/^\[.*]\zs\n$//
              >     return
              > endfunc

              Wow!

              Thanks, Andy!

              --
              You received this message from the "vim_use" maillist.
              For more information, visit http://www.vim.org/maillist.php
            • Christian Brabandt
              Hi Andy! ... Nice. But there seems to be a bug somewhere. With this file [headline1] Some Text here. Some more text Even more text [headline2] Some Text here.
              Message 6 of 9 , Dec 1, 2009
              • 0 Attachment
                Hi Andy!

                On Di, 01 Dez 2009, Andy Wokula wrote:

                > Andy Wokula schrieb:
                > setlocal formatexpr=FexLogBook()
                >
                > func! FexLogBook()
                > if mode() =~# '[iR]'
                > return 1
                > endif
                > exec v:lnum. "mark ["
                > exec (v:lnum + v:count - 1). "mark ]"
                > '[,']s/^\[.*]$/&\r/
                > setl fex=
                > normal! '[gq']
                > setl fex=FexLogBook()
                > '[,']s/^\[.*]\zs\n$//
                > return
                > endfunc
                >
                Nice. But there seems to be a bug somewhere.

                With this file
                [headline1]
                Some Text here.
                Some more text
                Even more text


                [headline2]
                Some Text here.
                Some more text
                Even more text

                Now executing vim on Windows (this does not happen on Linux):

                vim -u NONE file.txt
                :so fex.vim
                :norm! gqG
                :norm! u

                Now the file looks like this:

                [headline1]
                Some Text here.
                Some more (ext
                Even more text


                [headline2]
                Some Text here.
                Some more text
                Even more text

                Notice the ( in the first paragraph. The position of the parenthesis
                changes, but the buffer stays modified and only :e! reloaded it
                correctly. This happened with vim 7.2.284 on Windows, same Version under
                Linux is not affected. Can any Windows user confirm this bahaviour?


                Mit freundlichen Grüßen
                Christian
                --
                :wq!

                --
                You received this message from the "vim_use" maillist.
                For more information, visit http://www.vim.org/maillist.php
              • Andy Wokula
                ... Here on Win32, I checked with gVim 7.0 and gVim7.2.218, and I get almost the same: [headline1] Some Text (ere. Some more text Even more text [headline2]
                Message 7 of 9 , Dec 1, 2009
                • 0 Attachment
                  Christian Brabandt schrieb:
                  > Hi Andy!
                  >
                  > On Di, 01 Dez 2009, Andy Wokula wrote:
                  >
                  >> Andy Wokula schrieb:
                  >> setlocal formatexpr=FexLogBook()
                  >>
                  >> func! FexLogBook()
                  >> if mode() =~# '[iR]'
                  >> return 1
                  >> endif
                  >> exec v:lnum. "mark ["
                  >> exec (v:lnum + v:count - 1). "mark ]"
                  >> '[,']s/^\[.*]$/&\r/
                  >> setl fex=
                  >> normal! '[gq']
                  >> setl fex=FexLogBook()
                  >> '[,']s/^\[.*]\zs\n$//
                  >> return
                  >> endfunc
                  >>
                  > Nice. But there seems to be a bug somewhere.
                  >
                  > With this file

                  > [headline1]
                  > Some Text here.
                  > Some more text
                  > Even more text
                  >
                  >
                  > [headline2]
                  > Some Text here.
                  > Some more text
                  > Even more text

                  > Now executing vim on Windows (this does not happen on Linux):
                  >
                  > vim -u NONE file.txt
                  > :so fex.vim
                  > :norm! gqG
                  > :norm! u
                  >
                  > Now the file looks like this:

                  > [headline1]
                  > Some Text here.
                  > Some more (ext
                  > Even more text
                  >
                  >
                  > [headline2]
                  > Some Text here.
                  > Some more text
                  > Even more text

                  > Notice the ( in the first paragraph. The position of the parenthesis
                  > changes, but the buffer stays modified and only :e! reloaded it
                  > correctly. This happened with vim 7.2.284 on Windows, same Version under
                  > Linux is not affected. Can any Windows user confirm this bahaviour?

                  Here on Win32, I checked with gVim 7.0 and gVim7.2.218, and I get almost
                  the same:


                  [headline1]
                  Some Text (ere.
                  Some more text
                  Even more text


                  [headline2]
                  Some Text here.
                  Some more text
                  Even more text


                  After undoing, the buffer is not left modified.
                  And I typed the commands in (not using :normal).

                  Maybe the recursive gq is a problem?

                  --
                  Andy

                  --
                  You received this message from the "vim_use" maillist.
                  For more information, visit http://www.vim.org/maillist.php
                • Christian Brabandt
                  Fullquote, cause copying this message to vim_dev Hi Andy! ... Sorry, I meant my buffer was not left modified either. Here is an minimal example: func!
                  Message 8 of 9 , Dec 1, 2009
                  • 0 Attachment
                    Fullquote, 'cause copying this message to vim_dev

                    Hi Andy!

                    On Di, 01 Dez 2009, Andy Wokula wrote:

                    > Christian Brabandt schrieb:
                    > > Hi Andy!
                    > >
                    > > On Di, 01 Dez 2009, Andy Wokula wrote:
                    > >
                    > >> Andy Wokula schrieb:
                    > >> setlocal formatexpr=FexLogBook()
                    > >>
                    > >> func! FexLogBook()
                    > >> if mode() =~# '[iR]'
                    > >> return 1
                    > >> endif
                    > >> exec v:lnum. "mark ["
                    > >> exec (v:lnum + v:count - 1). "mark ]"
                    > >> '[,']s/^\[.*]$/&\r/
                    > >> setl fex=
                    > >> normal! '[gq']
                    > >> setl fex=FexLogBook()
                    > >> '[,']s/^\[.*]\zs\n$//
                    > >> return
                    > >> endfunc
                    > >>
                    > > Nice. But there seems to be a bug somewhere.
                    > >
                    > > With this file
                    >
                    > > [headline1]
                    > > Some Text here.
                    > > Some more text
                    > > Even more text
                    > >
                    > >
                    > > [headline2]
                    > > Some Text here.
                    > > Some more text
                    > > Even more text
                    >
                    > > Now executing vim on Windows (this does not happen on Linux):
                    > >
                    > > vim -u NONE file.txt
                    > > :so fex.vim
                    > > :norm! gqG
                    > > :norm! u
                    > >
                    > > Now the file looks like this:
                    >
                    > > [headline1]
                    > > Some Text here.
                    > > Some more (ext
                    > > Even more text
                    > >
                    > >
                    > > [headline2]
                    > > Some Text here.
                    > > Some more text
                    > > Even more text
                    >
                    > > Notice the ( in the first paragraph. The position of the parenthesis
                    > > changes, but the buffer stays modified and only :e! reloaded it
                    > > correctly. This happened with vim 7.2.284 on Windows, same Version under
                    > > Linux is not affected. Can any Windows user confirm this bahaviour?
                    >
                    > Here on Win32, I checked with gVim 7.0 and gVim7.2.218, and I get almost
                    > the same:
                    >
                    >
                    > [headline1]
                    > Some Text (ere.
                    > Some more text
                    > Even more text
                    >
                    >
                    > [headline2]
                    > Some Text here.
                    > Some more text
                    > Even more text
                    >
                    >
                    > After undoing, the buffer is not left modified.
                    > And I typed the commands in (not using :normal).
                    >
                    > Maybe the recursive gq is a problem?
                    >

                    Sorry, I meant my buffer was not left modified either.

                    Here is an minimal example:
                    func! FexLogBook()
                    set fex=
                    exe 'normal! gqap'
                    endfunc
                    setlocal formatexpr=FexLogBook()
                    call feedkeys('gqapu', 't')

                    Open vim with the sample text file from above:
                    ~$ vim -u NONE -N fex.txt
                    and source the file:
                    :so fex.vim

                    Note the change. BTW: this does not happen, when you call vim like this:
                    ~$ vim -u NONE -N -S fex.vim fex.txt

                    Try sourcing it several times, the change does not always occur and
                    looks differently sometimes. e.g. I get this result:
                    [headline1]()

                    However changing the function to this, does not result in the bug:
                    func! FexLogBook()
                    let &l:fex=''
                    exe 'normal! gqap'
                    endfunc
                    setlocal formatexpr=FexLogBook()
                    call feedkeys('gqapu', 't')


                    regards,
                    Christian
                    --
                    :wq

                    --
                    You received this message from the "vim_use" maillist.
                    For more information, visit http://www.vim.org/maillist.php
                  • Chris Jones
                    ... [..] ... If you re in a rush, or just lazy, another option is to record a macro: qr record macro name r
                    Message 9 of 9 , Dec 1, 2009
                    • 0 Attachment
                      On Tue, Dec 01, 2009 at 01:05:32AM EST, Robert Chan wrote:
                      > 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:

                      [..]

                      > Any ideas?
                      >
                      > (I've been reading 'formatoptions' and fo-table but could not find anything
                      > of related to custom paragraph settings.)

                      If you're in a rush, or just lazy, another option is to record a macro:

                      qr record macro name 'r'
                      o^[/[^MO^[?]^Mjjgqipjdd?]^Mjdd/[^M format one entry
                      q end recording
                      ugg undo and move to line 1

                      999@r replay 999 times

                      The macro actually works to some extent, and is a variation on adding blank lines
                      temporarily.

                      CJ

                      --
                      You received this message from the "vim_use" maillist.
                      For more information, visit http://www.vim.org/maillist.php
                    Your message has been successfully submitted and would be delivered to recipients shortly.