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

How to preview the members of a struct in C project?

Expand Messages
  • lazhang
    Dear Vimmers, Lots of IDEs support a highly desired function which lists all members of struct after you type in the full name of the struct. Is there any
    Message 1 of 5 , Aug 15, 2002
      Dear Vimmers,

      Lots of IDEs support a highly desired function which lists all members of struct after you type in the full name of the struct. Is there any function(or script) of Vim doing the same thing. I am now using SourceInsight to write C code and doing all other text editing using Vim. If Vim(or any plug-in) can provide this, then I don't have to use any other editors.

      Thanks
      Larry
    • Gary Holloway
      / FROM: lazhang , Aug 15 18:17 2002 ... END: lazhang Here s what I ve been using (it had some custom stuff in it, I *think* I got it
      Message 2 of 5 , Aug 15, 2002
        / FROM: "lazhang" <lazhang@...>, Aug 15 18:17 2002
        | ABOUT: How to preview the members of a struct in C project?
        |
        | Dear Vimmers,
        |
        | Lots of IDEs support a highly desired function which lists all members of struct after you type in the full name of the struct. Is there any function(or script) of Vim doing the same thing. I am now using SourceInsight to write C code and doing all other text editing using Vim. If Vim(or any plug-in) can provide this, then I don't have to use any other editors.
        |
        | Thanks
        | Larry
        |
        \ END: "lazhang"

        Here's what I've been using (it had some custom stuff in it, I *think* I got it
        all out).

        Basically, does a g} command to bring up the tag (assuming you're using
        exuberan ctags and having it tag structures) in the preview window. Next, it
        looks at the located tag, and based on what it looks like adjusts the preview
        window height and posistions the file in the preview window so that the
        "entire" tag is shown -- in the case of a structure, the "entire" tag includes
        the opening brace through the closing brace. That's where the style used can
        have an effect. We use the style:

        typedef struct foo {
        int member1;
        long member2;
        } FooType;

        So, including the line having the opening brace through the line having the
        closing brace shows the entire type.

        If it doesn't work with your style, it should only take some minor tweaks to
        the patterns used.

        -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-

        " Open a preview window for a tag, and change the preview window's height
        " to show the "entire" tag; i.e., an entire structure, a function's comment
        " (if any) and all it's arguments, etc.
        "
        " NOTE:
        " The preview window is a feature only available in version 6 (and later)
        "
        " EXAMPLE USAGE:
        "
        " command! -nargs=? -complete=tag_listfiles PreviewTag call PreviewTag(<f-args>)
        "
        " " Tag on current word into preview window.
        " map <C-\> :PreviewTag<CR>

        function! PreviewTag(...)
        let current_file = expand('%:p')

        " Open the tag in the preview window...
        if a:0 == 0
        " Use the word under the cursor as the tag
        wincmd g}
        else
        " Use the argument(s) as the tag
        if a:0 > 1
        " Tag contains spaces (yes, that's legal); make a single tag
        " variable.
        let tag = a:1
        let i = 2
        while i <= a:0
        exe "let tag = tag . ' ' ." . a:{i}
        let i = i + 1
        endwhile
        else
        let tag = a:1
        endif
        exe 'ptjump ' . tag
        endif

        " Jump to the preview window; if unsuccessful, presumably there were
        " multiple tags, and the user did not select one (or there was simply no
        " tag match), so there is no preview window.
        "
        let v:errmsg = ''
        silent! wincmd P
        if v:errmsg != ''
        return
        endif

        " Turn off wrapping in the preview window.
        setlocal nowrap

        let tag_line = line('.')
        let context_start = tag_line
        let context_end = tag_line
        let text = getline(context_start)

        " Of course this depends upon coding style...it works for us.
        if text =~ '\<\(\i\+\)($'
        " Looks like a function; locate the last parameter (i.e., the closing
        " parentheses).
        "
        let fn = matchstr(text, '\<\(\i\+\)($')
        normal! $%
        let context_end = line('.')

        if context_end == context_start + 3
        \ && getline(context_end + 1) =~ '^{'
        \ && getline(context_start + 1) =~ '^const.*\*\<a,'
        \ && getline(context_start + 2) =~ '^const.*\*\<b'

        " Looks like a comparison function, show the body (usually just
        " several CMP_* macros).
        "
        normal! j0%
        let context_end = line('.')
        exe context_start
        else
        if fn =~ '\(^\|_\)\(compare\|cmp\)\(_\|$\)' && getline(context_end + 1) =~ '^{'
        " Looks like a bag compare routine; show the entire function.
        normal! j%
        let context_end = line('.')
        normal! %k
        endif

        " See if there is a standard function-header " comment.
        let id = strpart(fn, 0, strlen(fn) - 1)
        if search('FUNCTION:\s*' . id . '$', 'bW')
        " Found a standard function header, include it in the preview.
        normal! k
        let context_start = line('.')
        else
        " Nope; get back to the original line.
        normal! %
        endif
        endif

        " Put our first line at the top of the preview window.
        let normal = 'zt'

        elseif text =~ '^\s*}\s*\i\+;'
        " Looks like a typedef, e.g. (Foobar):
        "
        " typedef {
        " ...
        " } Foobar;
        "
        let context_end = context_start

        " Go to the start of the typedef.
        normal! 0%
        let context_start = line('.')

        " Get back to the original line.
        normal! %

        " Put this line (e.g., Foobar) at the bottom.
        let normal = 'zb'

        elseif text =~ '^\s*\i\+\s*\(=\s*.*\)\=,\=\s*\(\/\*.*\*\/\|\/\/.*\)\?$'
        " Looks like an enumeration (i.e., "tag_match", not the enumerated
        " type, "EnumType"), e.g:
        "
        " typedef enum {
        " ...
        " tag_match = 1, // This is an enumeration.
        " ...
        " } EnumType;
        "
        if search('^}', 'W')
        let context_end = line('.')
        normal! %
        if getline('.') =~ '\<enum\>'
        let context_start = line('.')
        else
        let context_start = tag_line
        let context_end = tag_line
        exe tag_line
        endif
        endif

        let normal = 'zt' . tag_line . 'G'

        elseif text =~ '^\s*\(}\s*\|\(\(static\|extern\)\s\+\)\=\(const\s\+\)\=\(\*\s\)*\)\=\i\+\s*\[\s*\d*\s*\]\s*=\(\s*{\s*\)\=$'
        " Looks like an array initialization, e.g. (foobar):
        "
        " foobar[] = { or typedef struct { or foobar[] =
        " { ... }, int f1; { ... }
        " { ... }, ...
        " ... } foobar[] = {
        " }; { ... },
        " ...
        " };
        if text !~ '{\s*$'
        normal! j
        if getline(".") =~ '^\s*{'
        if getline(".") =~ '^{'
        normal! %
        else
        normal! 0f{%
        endif
        let context_end = line('.')
        endif
        else
        normal! 0f{%
        let context_end = line('.')
        endif

        if text =~ '^\s*}'
        " Looks like second form (with typedef).
        normal! %$F}%
        let context_start = line('.')
        else
        exe tag_line
        endif
        let normal = 'zt' . tag_line . 'G'

        elseif text =~ '^#define.*\\$'
        " Looks like a multi-line macro definition; make sure the entire
        " macro is shown.
        "
        if search('[^\\]$', 'W')
        let context_end = line('.')
        exe tag_line
        endif
        let normal = 'zt'
        endif

        if context_start == context_end
        " Not recognized as anything we want to handle specially (no
        " context-block); set the preview height to the default, and put this
        " line at the second-from-the-top.
        "
        let context_start = 1
        let context_end = &previewheight

        " Set the preview height to the default. Do this explicitly; because
        " it's not *always* done below.
        "
        exe &previewheight . 'wincmd _'

        " Put the tag-matching line second-from-the-top.
        let normal = 'kztj'
        endif

        " Make the preview window big enough to show the function comment and all
        " the function arguments.
        "
        exe ((context_end - context_start) + 1) . 'wincmd _'

        " Don't let a scrolloff setting mess up our positioning.
        let save_scrolloff = &scrolloff
        se scrolloff=0

        exe 'normal! ' . normal

        let &scrolloff = save_scrolloff

        " Return to the original window.
        wincmd p
        endfunction
      • Guido Van Hoecke
        Gary, How about posting this script at http://vim.sourceforge.net/scripts.php ? Guido.
        Message 3 of 5 , Aug 15, 2002
          Gary,

          How about posting this script at
          http://vim.sourceforge.net/scripts.php ?

          Guido.
        • Gary Holloway
          Ok, done. I thought it was too heavily tied to the style used to be usefull generally, but what the heck. :) PreviewTag.vim g / FROM: Guido Van Hoecke
          Message 4 of 5 , Aug 15, 2002
            Ok, done.

            I thought it was too heavily tied to the style used to be usefull generally,
            but what the heck. :)

            PreviewTag.vim

            g

            / FROM: Guido Van Hoecke <Guido@...>, Aug 15 20:39 2002
            | ABOUT: Re: How to preview the members of a struct in C project?
            |
            | Gary,
            |
            | How about posting this script at
            | http://vim.sourceforge.net/scripts.php ?
            |
            | Guido.
            |
            \ END: Guido Van Hoecke
          • Benji Fisher
            ... Have you looked at http://users.erols.com/astronaut/vim/#Cfunc HTH --Benji Fisher
            Message 5 of 5 , Aug 16, 2002
              lazhang wrote:
              >
              > Dear Vimmers,
              >
              > Lots of IDEs support a highly desired function which lists all members of struct after you type in the full name of the struct. Is there any function(or script) of Vim doing the same thing. I am now using SourceInsight to write C code and doing all other text editing using Vim. If Vim(or any plug-in) can provide this, then I don't have to use any other editors.

              Have you looked at

              http://users.erols.com/astronaut/vim/#Cfunc

              HTH --Benji Fisher
            Your message has been successfully submitted and would be delivered to recipients shortly.