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

Re: script inside XML

Expand Messages
  • Benji Fisher
    ... First, read the docs (following some links) under ... Then, start to look at the java.vim and xml.vim files in $VIMRUNTIME/indent/ . (Or is JavaScript
    Message 1 of 5 , Jan 1, 2004
    • 0 Attachment
      On Thu, Jan 01, 2004 at 11:28:06AM -0500, Brian Burns wrote:
      > Hello,
      >
      > I would like to modify indent/xml.vim so that a script language such as
      > JavaScript could be properly indented inside a CDATA section.
      >
      > For example,
      >
      > <somenamespace:Foo>
      > <![CDATA[
      > function foo()
      > {
      > blah;
      > }
      > ]]>
      > </somenamespace:Foo>
      >
      > The logic can be fairly simple in my case. I would just need to
      > detect an open CDATA tag and switch to javascript mode, then switch
      > back to XML mode at the end of the CDATA section. I'm new to indent
      > and syntax files so I don't know exactly where to begin?
      >
      > Any ideas ?

      First, read the docs (following some links) under

      :help 'indentexpr'

      Then, start to look at the java.vim and xml.vim files in
      $VIMRUNTIME/indent/ . (Or is JavaScript going to be enough different
      from Java that this will not help?) Both of these are pretty well
      commented. Once you get an idea of what is going on, you can modify
      XmlIndentGet() to call GetJavaIndent() when appropriate; if you are
      lucky, the latter will not need any modifications.

      Once you get started, feel free to ask for more specific help.

      HTH --Benji Fisher
    • Brian Burns
      Thanks for the pointer. I modified indent/xml.vim as shown below. I lifted the code from indent/html.vim where it searches for a tag and modified it so
      Message 2 of 5 , Jan 3, 2004
      • 0 Attachment
        Thanks for the pointer.

        I modified indent/xml.vim as shown below. I lifted the code from
        indent/html.vim where it searches for a <pre> tag and modified it so
        that it looks for a <script> tag and calls GetJavaIndent. However, I
        don't think GetJavaIndent is being found because no indentation is
        occuring inside the <script> tag. I'm assuming I need to include
        indent/java.vim. Do I need to temporarily set indentexpr to
        GetJavaIndent function? Am I on the right path here?

        Thanks,
        Brian


        fun! XmlIndentGet(lnum, use_syntax_check)
        " Find a non-empty line above the current line.
        let lnum = prevnonblank(a:lnum - 1)

        " Hit the start of the file, use zero indent.
        if lnum == 0
        return 0
        endif

        if getline(a:lnum) =~ '\c</script>'
        \ || 0 < searchpair('\c<script>', '', '\c</script>', 'nWb')
        \ || 0 < searchpair('\c<script>', '', '\c</script>', 'nW')
        " we're in a line with </pre> or inside <pre> ... </pre>
        "return -1
        include !runtime/indent/java.vim
        return GetJavaIndent()
        endif

        if getline(lnum) =~ '\c</script>'
        " line before the current line a:lnum contains
        " a closing </pre>. --> search for line before
        " starting <pre> to restore the indent.
        let preline = prevnonblank(search('\c<script>', 'bW') - 1)
        if preline > 0
        return indent(preline)
        endif
        endif


        if a:use_syntax_check
        if 0 == <SID>XmlIndentSynCheck(lnum) || 0 ==
        <SID>XmlIndentSynCheck(a:lnum)
        return indent(a:lnum)
        endif
        endif

        let ind = <SID>XmlIndentSum(lnum, -1, indent(lnum))
        let ind = <SID>XmlIndentSum(a:lnum, 0, ind)

        return ind
        endfun




        On Thu, 2004-01-01 at 19:38, Benji Fisher wrote:
        > On Thu, Jan 01, 2004 at 11:28:06AM -0500, Brian Burns wrote:
        > > Hello,
        > >
        > > I would like to modify indent/xml.vim so that a script language such as
        > > JavaScript could be properly indented inside a CDATA section.
        > >
        > > For example,
        > >
        > > <somenamespace:Foo>
        > > <![CDATA[
        > > function foo()
        > > {
        > > blah;
        > > }
        > > ]]>
        > > </somenamespace:Foo>
        > >
        > > The logic can be fairly simple in my case. I would just need to
        > > detect an open CDATA tag and switch to javascript mode, then switch
        > > back to XML mode at the end of the CDATA section. I'm new to indent
        > > and syntax files so I don't know exactly where to begin?
        > >
        > > Any ideas ?
        >
        > First, read the docs (following some links) under
        >
        > :help 'indentexpr'
        >
        > Then, start to look at the java.vim and xml.vim files in
        > $VIMRUNTIME/indent/ . (Or is JavaScript going to be enough different
        > from Java that this will not help?) Both of these are pretty well
        > commented. Once you get an idea of what is going on, you can modify
        > XmlIndentGet() to call GetJavaIndent() when appropriate; if you are
        > lucky, the latter will not need any modifications.
        >
        > Once you get started, feel free to ask for more specific help.
        >
        > HTH --Benji Fisher
      • Benji Fisher
        ... [snip] Without actually scrutinizing the code that I just snipped, I think what you need to do is source indent/java.vim in order to have the
        Message 3 of 5 , Jan 3, 2004
        • 0 Attachment
          On Sat, Jan 03, 2004 at 09:17:13AM -0500, Brian Burns wrote:
          > Thanks for the pointer.
          >
          > I modified indent/xml.vim as shown below. I lifted the code from
          > indent/html.vim where it searches for a <pre> tag and modified it so
          > that it looks for a <script> tag and calls GetJavaIndent. However, I
          > don't think GetJavaIndent is being found because no indentation is
          > occuring inside the <script> tag. I'm assuming I need to include
          > indent/java.vim. Do I need to temporarily set indentexpr to
          > GetJavaIndent function? Am I on the right path here?
          >
          > Thanks,
          > Brian
          [snip]

          Without actually scrutinizing the code that I just snipped, I think
          what you need to do is source indent/java.vim in order to have the
          GetJavaIndent() function defined. You can test

          if exists("*GetJavaIndent")

          to see whether it already exists. I do not think you need to
          temporarily set 'indentexpr', although this is likely to happen when you
          source the Java indent script.

          In short, this is what I suggest trying. Near the top of your
          modified indent/xml.vim (after the part that sets 'indentexpr' each time
          an xml file is loaded, perhaps right before defining the XmlGetIndent()
          function, or whatever it is called)

          if !exists("*GetJavaIndent")
          " only one of the following two lines!
          source $VIMRUNTIME/indent/java.vim " if you want to insist on the
          " version from the ditro
          runtime! indent/java.vim " if you want to allow custom versions
          " further lines to undo the options set in indent/java.vim
          endif

          HTH --Benji Fisher
        • Brian Burns
          Looks like the solution was right under my nose the entire time. If you look at the ant.vim syntax file. They have exactly what I was looking for. I just
          Message 4 of 5 , Jan 5, 2004
          • 0 Attachment
            Looks like the solution was right under my nose the entire time. If you
            look at the ant.vim syntax file. They have exactly what I was looking
            for. I just lifted that code. I never thought to look at the syntax
            files.

            Brian

            On Sat, 2004-01-03 at 13:19, Benji Fisher wrote:
            > On Sat, Jan 03, 2004 at 09:17:13AM -0500, Brian Burns wrote:
            > > Thanks for the pointer.
            > >
            > > I modified indent/xml.vim as shown below. I lifted the code from
            > > indent/html.vim where it searches for a <pre> tag and modified it so
            > > that it looks for a <script> tag and calls GetJavaIndent. However, I
            > > don't think GetJavaIndent is being found because no indentation is
            > > occuring inside the <script> tag. I'm assuming I need to include
            > > indent/java.vim. Do I need to temporarily set indentexpr to
            > > GetJavaIndent function? Am I on the right path here?
            > >
            > > Thanks,
            > > Brian
            > [snip]
            >
            > Without actually scrutinizing the code that I just snipped, I think
            > what you need to do is source indent/java.vim in order to have the
            > GetJavaIndent() function defined. You can test
            >
            > if exists("*GetJavaIndent")
            >
            > to see whether it already exists. I do not think you need to
            > temporarily set 'indentexpr', although this is likely to happen when you
            > source the Java indent script.
            >
            > In short, this is what I suggest trying. Near the top of your
            > modified indent/xml.vim (after the part that sets 'indentexpr' each time
            > an xml file is loaded, perhaps right before defining the XmlGetIndent()
            > function, or whatever it is called)
            >
            > if !exists("*GetJavaIndent")
            > " only one of the following two lines!
            > source $VIMRUNTIME/indent/java.vim " if you want to insist on the
            > " version from the ditro
            > runtime! indent/java.vim " if you want to allow custom versions
            > " further lines to undo the options set in indent/java.vim
            > endif
            >
            > HTH --Benji Fisher
          Your message has been successfully submitted and would be delivered to recipients shortly.