[prev in list] [next in list] [prev in thread] [next in thread] 

List:       vim
Subject:    Re: [vim] Formatting commnets in C code
From:       "Charles E. Campbell" <cec () gryphon ! gsfc ! nasa ! gov>
Date:       2003-04-30 14:54:44
[Download RAW message or body]

On Tue, Apr 29, 2003 at 05:55:16PM -0700, Roy, Gilles wrote:
> Is it possible to have "gq" only format comments in C code, and not the
> actual C code? I use formatoptions=croq but "gq" still affects C code.
 ---------------------------------------------------------------------

I suggest using Align/AlignMap's \acom  and vis.vim:

 Align: see
    http://www.erols.com/astronaut/vim/textab.html
    or http://vim.sourceforge.net/scripts/script.php?script_id=294

 vis.vim: see
    http://www.erols.com/astronaut/vim/index.html#VimFuncs
    under "Visual Block Commands"


For some examples:

Example 1:

    Original:

    /* this is a
     * multiline comment with a long line in it.
     * We want
     * to apply gq to just the
     * comment.
     */

    (using vis.vim)

    Visual-block select the comment text
    (ctrl-v at the starting "this", move cursor to the ending "comment."

    Apply  :B norm! gq}
    which will apply gq} (handles one paragraph) to just the selected
    text.

    /* this is a multiline comment with a long line in it.  We want to apply gq
     * to just the comment.
     * 
     * 
     * 
     */

    Obviously you may wish to get rid of a few extra now-nearly-blank
    comment lines.

Example 2:

  Original:
    this= some; /* text that */
    we  = want;    /* to modify that isn't */
    previously= lined;  /* up already.  In fact, ideally */
    we= would; /* gq it! */

 (using Align and AlignMaps's \acom then vis.vim as above ) 

 First, apply \acom to the text which will result in:
    this= some;        /* text that                     */
    we  = want;        /* to modify that isn't          */
    previously= lined; /* up already.  In fact, ideally */
    we= would;         /* gq it!                        */

 Second, modify the trailing comments:
 :[range]s/\s\+\*\///
    this= some;        /* text that
    we  = want;        /* to modify that isn't
    previously= lined; /* up already.  In fact, ideally
    we= would;         /* gq it!

 Third, do like Example 1; select text via visual block mode,
 then :B norm! gq} it.
    this= some;        /* text that to modify that isn't up already.  In fact, ideally gq it!*/
    we  = want;        /* */
    previously= lined; /* */
    we= would;         /* */

 Fourth, restore the comment-ending patterns
 :[range]s/$/\*\//

 Finally, re-apply \acom
    this= some;        /* text that to modify that isn't up already.  In fact, ideally gq it! */
    we  = want;        /*                                                                     */
    previously= lined; /*                                                                     */
    we= would;         /*                                                                     */

Seems to me that this latter example requires Too Much Work; it'd
probably be best to put it together in a map.  Let's see...

    :vmap <silent> <Leader>gqcom  '<ma'>mb<Esc>:set lz<CR>:'a,'bAlign /\*<CR>
      \:'a,'bs/\s\+\*\/$//<CR>'a/\/\* /<CR>3lma
      \<c-v>'b$:B norm! gq}<CR>:'a,'bs/$/\*\//<CR>'b\acom:set nolz<CR>

The vmap is best used with the V command; use 

Example 3: using the vmap command above

  Original:
    this= some; /* text that */
    we  = want;    /* to modify that isn't */
    previously= lined;  /* up already.  In fact, ideally */
    we= would; /* gq it! */

  Modified by \gqcom  (my <Leader> is \, obviously you should use yours)
    this= some;        /* text that to modify that isn't up already.  In fact, ideally gq it! */
    we  = want;        /*                                                                     */
    previously= lined; /*                                                                     */
    we= would;         /*                                                                     */

Regards,
Chip Campbell

-- 
        Charles E Campbell, Jr, PhD            _   __   __
        Goddard Space Flight Center           / /_/\_\_/ /
        cec@NgrOyphSon.gPsfAc.nMasa.gov      /_/  \/_//_/
  PGP public key: http://www.erols.com/astronaut/pgp.html
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic