regexvimctagstagbar

vim tagbar: custom language configuration


I am trying to configure a "custom language" for vim tagbar that would allow the tagbar to be used for viewing Abaqus Finite Element Analysis input files.

I duplicated examples from the following links:

http://github.com/majutsushi/tagbar/wiki

http://andrew.stwrt.ca/posts/vim-ctags/

ctags, vimwiki, vim and tagbar-plugin

http://ctags.sourceforge.net/EXTENDING.html

This is the software I'm using:

Example of an Abaqus input file example.inp

** Analyst:
** Comments
** More comments
**
****************************************
** Section Name One
****************************************
*Keyword1
*KEYWORD2
**
****************************************
** Section Name Two
****************************************
*Keyword3

This is what I added to my vim configuration file ~/.vimrc

let g:tagbar_type_AbaqusINP = {
    \ 'ctagstype' : 'AbaqusINP',
    \ 'kinds' : [
        \ 's:Section',
        \ 'k:Keyword'
    \ ],
    \ 'sort' : 0
\ }

I created a ctags configuration file ~/.ctags

--langdef=AbaqusINP
--langmap=AbaqusINP:.inp
--regex-AbaqusINP=/^\*\*\*[\*]+\n\*\*[ \tA-Za-z]+/\3/s,Section/
--regex-AbaqusINP=/^\*[A-Za-z]+/\1/k,Keyword/

When I open an input file and then open the tagbar nothing is there. AbaqusINP *.inp is in the list when I call the following from the command line.

$ ctags --list-maps
Ant       *.build.xml
...
AbaqusINP *.inp

When I try to manually create a tags file I get the following warnings for every line where a keyword is found.

$ ctags example.inp
ctags: Warning: example.inp:8: null expansion of name pattern "\1"
ctags: Warning: example.inp:9: null expansion of name pattern "\1"
ctags: Warning: example.inp:14: null expansion of name pattern "\1"

The tags file is created, but is only populated with the default header.

!_TAG_FILE_FORMAT   2   /extended format; --format=1 will not append ;" to lines/
!_TAG_FILE_SORTED   1   /0=unsorted, 1=sorted, 2=foldcase/
!_TAG_PROGRAM_AUTHOR    Darren Hiebert  /dhiebert@users.sourceforge.net/
!_TAG_PROGRAM_NAME  Exuberant Ctags // 
!_TAG_PROGRAM_URL   http://ctags.sourceforge.net    /official site/
!_TAG_PROGRAM_VERSION   5.8 //

Any thoughts on why ctags is not creating the tags for tagbar to use? Thanks for your help.



Update: Clarify the desired regular expressions.

I would like the section names and the keywords to be assigned tags. I tried these patterns in vim and they seemed to work right.

For the example.inp file above I would like the regex for sections to return:


For the example.inp file above I would like the regex for keywords to return:

keyword_regex = ^\*[A-Za-z]+


Solution

  • According to this ctags does not support multiple line regular expressions.

    The closer solution I could get to was:

    --langdef=AbaqusINP
    --langmap=AbaqusINP:.inp
    --regex-AbaqusINP=/^\*\*([ \tA-Za-z0-9]+)/\1/s,Section/
    --regex-AbaqusINP=/^\*([A-Za-z0-9]+)/\1/k,Keyword/
    

    Take into account that the \1 in the replace expression refers to groups in the match expression, so the parentheses are necessary around the keywords of interest. This was the reason why you received those warnings. I've also fixed the keyword regular expression to allow the inclusion of numbers.

    The configuration above will return the following tags for the example you posted:

    Analyst test.inp    /^** Analyst:$/;"   s
    Comments    test.inp    /^** Comments$/;"   s
    KEYWORD2    test.inp    /^*KEYWORD2$/;" k
    Keyword1    test.inp    /^*Keyword1$/;" k
    More comments   test.inp    /^** More comments$/;"  s
    Section Name One    test.inp    /^** Section Name One$/;"   s
    Section Name Two    test.inp    /^** Section Name Two$/;"   s
    

    This still returns the comments, which is not desirable. But from what I can gather you are using normal comments to define sections, so I guess that the format is not fixed. If this is true, one possible solution would be:

    --langdef=AbaqusINP
    --langmap=AbaqusINP:.inp
    --regex-AbaqusINP=/^\*\* *(Section +[ \tA-Za-z0-9]+)/\1/s,Section/
    --regex-AbaqusINP=/^\*([A-Za-z0-9]+)/\1/k,Keyword/
    

    Although it requires that all Sections start with the keyword "Section", it will return what you are looking for:

    KEYWORD2    test.inp    /^*KEYWORD2$/;" k
    Keyword1    test.inp    /^*Keyword1$/;" k
    Section Name One    test.inp    /^** Section Name One$/;"   s
    Section Name Two    test.inp    /^** Section Name Two$/;"   s