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

WSH (Windows Scripting Host) syntax file

Expand Messages
  • Moore, Paul
    Attached is a syntax file for WSH (Windows Scripting Host) files. The file extension is .wsf (not .wsh!). I wrapped a couple of syn region lines so my mailer
    Message 1 of 7 , Oct 2, 2000
    • 0 Attachment
      Attached is a syntax file for WSH (Windows Scripting Host) files. The file
      extension is .wsf (not .wsh!). I wrapped a couple of syn region lines so my
      mailer doesn't do it. You'll need to rejoin them manually.

      It's a nice example of reusing other syntax files.

      Some points.

      1. I use "runtime syntax/xml.vim" to load the base XML highlighting. Only
      one problem, I'd like this to *fail* if xml.vim isn't available, as
      continuing without the xml stuff is a bit meaningless. I could use :source
      <sfile>:p:h/xml.vim, but that relies on wsh.vim being installed in the same
      directory as xml.vim (which it won't be, unless/until it's accepted into the
      standard distribution). It would be nice to have a version of :runtime which
      failed if the file could not be found.

      2. Similarly, it would be nice to be able to do the "syn include" statements
      without needing to explicitly specify the directory. In general, I suspect
      we need a "find a file on the runtimepath" function, here... It probably
      should be widely used (it won't be at the moment, because the runtimepath
      stuff is new) so could do with being a builtin.

      3. In practice, I *may* want to highlight the XML parts of WSH code
      differently from "general" XML (specifically, to de-emphasize it, as the
      script is more important here...). Unfortunately, if I set highlighting, it
      affects XML and WSH files. What I need is a way to load the XML syntax file,
      but rename the syntax items which get defined, so that I can highlight them
      separately. A bit like "syn include @group", but at the top level.

      Overall, once I worked out the trick, I was pleasantly surprised to see how
      easy it was to reuse the existing syntax files for this. The only thing I
      would say is that it might be nice if the XML syntax file had a few extra
      "hooks" somehow, so that when it is used like this, it could be tailored -
      I'd love to be able to list the valid XML tags for WSH and then have xml.vim
      highlight them differently from "generic" XML. Same for attributes. Also
      note the wshVBScript and wshJavaScript regions - it feels vaguely "tacky" to
      have to match specific tags like this (but hey, it works!!!)

      Hope this is of interest,
      Paul.

      --- Cut here ---
      " Vim syntax file
      " Language: Windows Scripting Host
      " Maintainer: Paul Moore <gustav@...>
      " Last Change: 01 Oct 2000

      " This reuses the XML, VB and JavaScript syntax files. While VB is not
      " VBScript, it's close enough for us. No attempt is made to handle
      " other languages.
      " Send comments, suggestions and requests to the maintainer.

      " source <sfile>:p:h/xml.vim
      runtime syntax/xml.vim
      syn case ignore
      syn include @wshVBScript $VIMRUNTIME/syntax/vb.vim
      syn include @wshJavaScript $VIMRUNTIME/syntax/javascript.vim
      syn region wshVBScript matchgroup=xmlTag
      start="<script[^>]*VBScript[^>]*>" end="</script>"
      contains=@wshVBScript
      syn region wshJavaScript matchgroup=xmlTag
      start="<script[^>]*J\(ava\)\=Script[^>]*>"
      end="</script>"
      contains=@wshJavaScript

      if !exists("did_wsh_syntax_inits")
      let did_wsh_syntax_inits = 1
      " No type-specific highlighting -- it's all reused from other files
      endif

      let b:current_syntax = "wsh"
    • Bram Moolenaar
      ... Looks OK, I ll include it (with a few modifications, see below). ... Ehm, fail in what way? Normally it would then produce an error message, and your
      Message 2 of 7 , Oct 2, 2000
      • 0 Attachment
        Paul Moore wrote:

        > Attached is a syntax file for WSH (Windows Scripting Host) files. The file
        > extension is .wsf (not .wsh!). I wrapped a couple of syn region lines so my
        > mailer doesn't do it. You'll need to rejoin them manually.

        Looks OK, I'll include it (with a few modifications, see below).

        > It's a nice example of reusing other syntax files.
        >
        > Some points.
        >
        > 1. I use "runtime syntax/xml.vim" to load the base XML highlighting. Only
        > one problem, I'd like this to *fail* if xml.vim isn't available, as
        > continuing without the xml stuff is a bit meaningless. I could use :source
        > <sfile>:p:h/xml.vim, but that relies on wsh.vim being installed in the same
        > directory as xml.vim (which it won't be, unless/until it's accepted into the
        > standard distribution). It would be nice to have a version of :runtime which
        > failed if the file could not be found.

        Ehm, fail in what way? Normally it would then produce an error message, and
        your script still continues (with more errors).

        You could test if b:current_syntax has been set to "xml". If it isn't,
        something went wrong.

        > 2. Similarly, it would be nice to be able to do the "syn include" statements
        > without needing to explicitly specify the directory. In general, I suspect
        > we need a "find a file on the runtimepath" function, here... It probably
        > should be widely used (it won't be at the moment, because the runtimepath
        > stuff is new) so could do with being a builtin.

        Hmm, ":syn include" is like searching for a syntax file to include. We could
        search 'runtimepath' for the file. But previously it included the full path,
        which conflicts with using just the file name.

        I suppose adding a new :syntax sub-command will be the simplest solution.
        ":syn includepath"? Or an extra argument: ":syn include path"?


        This is the file as I would include it for Vim 5.7:

        " Vim syntax file
        " Language: Windows Scripting Host
        " Maintainer: Paul Moore <gustav@...>
        " Last Change: 01 Oct 2000

        " This reuses the XML, VB and JavaScript syntax files. While VB is not
        " VBScript, it's close enough for us. No attempt is made to handle
        " other languages.
        " Send comments, suggestions and requests to the maintainer.

        source <sfile>:p:h/xml.vim

        syn case ignore
        syn include @wshVBScript <sfile>:p:h/vb.vim
        syn include @wshJavaScript <sfile>:p:h/javascript.vim
        syn region wshVBScript matchgroup=xmlTag start="<script[^>]*VBScript[^>]*>" end="</script>" contains=@wshVBScript
        syn region wshJavaScript matchgroup=xmlTag start="<script[^>]*J\(ava\)\=Script[^>]*>" end="</script>" contains=@wshJavaScript

        if !exists("did_wsh_syntax_inits")
        let did_wsh_syntax_inits = 1
        " No type-specific highlighting -- it's all reused from other files
        endif

        let b:current_syntax = "wsh"

        --
        hundred-and-one symptoms of being an internet addict:
        134. You consider bandwidth to be more important than carats.

        /// Bram Moolenaar Bram@... http://www.moolenaar.net \\\
        \\\ Vim: http://www.vim.org ICCF Holland: http://iccf-holland.org ///
      • Paul Moore
        From: Bram@moolenaar.net [mailto:Bram@moolenaar.net] ... The mods basically just use the -relative filenames, which is what I would do anyway once the
        Message 3 of 7 , Oct 2, 2000
        • 0 Attachment
          From: Bram@... [mailto:Bram@...]
          > Looks OK, I'll include it (with a few modifications, see below).

          The mods basically just use the <sfile>-relative filenames, which is what I
          would do anyway once the file is being included in the distribution. It's
          only while it's a "personal" file that the <sfile> trick doesn't work.

          > > 1. I use "runtime syntax/xml.vim" to load the base XML
          > > highlighting. Only one problem, I'd like this to *fail*
          [...]
          > Ehm, fail in what way? Normally it would then produce an error
          > message, and your script still continues (with more errors).

          That's what I read from the docs about :runtime. "Read Ex commands from
          {file} in each directory given by 'runtimepath'. There is no error for
          non-existing files." So, specifically, :runtime differs from :source in that
          no error is produced.

          > You could test if b:current_syntax has been set to "xml". If it isn't,
          > something went wrong.

          Yes, that would be reasonable in this case.

          > Hmm, ":syn include" is like searching for a syntax file to
          > include. We could search 'runtimepath' for the file. But
          > previously it included the full path, which conflicts with
          > using just the file name.
          >
          > I suppose adding a new :syntax sub-command will be the simplest solution.
          > ":syn includepath"? Or an extra argument: ":syn include path"?

          Wouldn't it be OK if :syn include loaded the exact path if it was an
          absolute pathname, but otherwise searched 'runtimepath'? That would preserve
          backward compatibility (everything will use absolute paths at the moment)
          while allowing the use of 'runtimepath'.

          If that's not OK, then I'd prefer :syn include path (or maybe :syn include
          runtime, but that's a bit long).

          > This is the file as I would include it for Vim 5.7:

          Looks great to me. Thanks.

          Paul.
        • Bram Moolenaar
          ... It seems that this should be OK. Until now you really needed to use an absolute path. I can t find an example where this isn t used. Should the relative
          Message 4 of 7 , Oct 3, 2000
          • 0 Attachment
            Paul Moore wrote:

            > > Hmm, ":syn include" is like searching for a syntax file to
            > > include. We could search 'runtimepath' for the file. But
            > > previously it included the full path, which conflicts with
            > > using just the file name.
            > >
            > > I suppose adding a new :syntax sub-command will be the simplest solution.
            > > ":syn includepath"? Or an extra argument: ":syn include path"?
            >
            > Wouldn't it be OK if :syn include loaded the exact path if it was an
            > absolute pathname, but otherwise searched 'runtimepath'? That would preserve
            > backward compatibility (everything will use absolute paths at the moment)
            > while allowing the use of 'runtimepath'.

            It seems that this should be OK. Until now you really needed to use an
            absolute path. I can't find an example where this isn't used.

            Should the relative path be searched in 'runtimepath' or in the syntax
            subdirectory? Thus would you use:

            :syn include xml.vim
            or
            :syn include syntax/xml.vim

            I would propose the second, because it gives some room to use another
            subdirectory, e.g.:

            :syn include mysyntax/xml.vim

            --
            hundred-and-one symptoms of being an internet addict:
            141. You'd rather go to http://www.weather.com/ than look out your window.

            /// Bram Moolenaar Bram@... http://www.moolenaar.net \\\
            \\\ Vim: http://www.vim.org ICCF Holland: http://iccf-holland.org ///
          • Moore, Paul
            From: Bram Moolenaar [mailto:Bram@moolenaar.net] ... I agree, that makes sense. Paul.
            Message 5 of 7 , Oct 3, 2000
            • 0 Attachment
              From: Bram Moolenaar [mailto:Bram@...]
              > > Wouldn't it be OK if :syn include loaded the exact path if it
              > > was an absolute pathname, but otherwise searched 'runtimepath'?
              > > That would preserve backward compatibility (everything will
              > > use absolute paths at the moment) while allowing the use of
              > > 'runtimepath'.
              >
              > It seems that this should be OK. Until now you really needed
              > to use an absolute path. I can't find an example where this
              > isn't used.
              >
              > Should the relative path be searched in 'runtimepath' or in the syntax
              > subdirectory? Thus would you use:
              >
              > :syn include xml.vim
              > or
              > :syn include syntax/xml.vim
              >
              > I would propose the second, because it gives some room to use another
              > subdirectory, e.g.:
              >
              > :syn include mysyntax/xml.vim

              I agree, that makes sense.

              Paul.
            • Moore, Paul
              From: Johannes Zellner [mailto:johannes@zellner.org] ... The problem was, that I couldn t really decide what hooks *were* needed - nor could I be sure that
              Message 6 of 7 , Oct 17, 2000
              • 0 Attachment
                From: Johannes Zellner [mailto:johannes@...]
                > > easy it was to reuse the existing syntax files for this.
                > > The only thing I would say is that it might be nice if
                > > the XML syntax file had a few extra "hooks" somehow,
                > > so that when it is used like this, it could be tailored
                >
                > tell me what sort of hooks you need and I'll include them!

                The problem was, that I couldn't really decide what hooks *were* needed -
                nor could I be sure that what I was thinking of was even possible with Vim's
                syntax features as they stand.

                Basically, what I was thinking about was that WSH files have script blocks
                in <script language="xxx"> sections. It's also possible to have no-content
                <script language="xxx" src="yyy"/> tags, which don't include code. I was
                thinking that I'd like to be able to say before loading the XML syntax file
                "I want the content of SCRIPT tags marked as being in group XXX. And
                furthermore, I'd like the group name to be dependent on the value of the
                LANGUAGE attribute. Oh, yes, and I want group XXX-LLL to contain the
                following items."

                OK, reading that, it's not going to be possible.

                The thing that nagged me is that in my syntax file, I've effectively
                reimplemented the parsing for <script> tags. And I've not done a complete
                job (I've ignored CDATA sections in script tags, for example). It seems to
                me that it should be possible to use the work already done in the xml file.

                Annoyingly, XML is precisely a markup format where the contents of tags *is*
                important. So I expect this type of requirement to come up more and more
                often. Maybe we need to work out precisely what additions are needed to
                Vim's syntax features to make this work. A bit like \z was added to cater
                for matching delimiters.

                Maybe, like folding and indenting, there's a need for expression-based
                highlighting...

                This isn't an urgent issue, as I now have the WSH syntax file working. But
                it's something to keep in mind, before too many people reinvent the same
                wheel...

                Paul.
              • Johannes Zellner
                On Tue, Oct 17, 2000 at 10:48:46AM +0100, Moore, Paul wrote: [...] ... [...] you need something like the syntax respecting the dtd of the document, right ?
                Message 7 of 7 , Oct 17, 2000
                • 0 Attachment
                  On Tue, Oct 17, 2000 at 10:48:46AM +0100, Moore, Paul wrote:
                  [...]
                  > Annoyingly, XML is precisely a markup format where the contents of tags *is*
                  > important. So I expect this type of requirement to come up more and more
                  [...]

                  you need something like the syntax respecting the dtd of the
                  document, right ? This could even highlight errors (invalid
                  elemnt content), but I think doing this in a generic way is
                  not an easy task.

                  --
                  Johannes
                Your message has been successfully submitted and would be delivered to recipients shortly.