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

Multiple Types of Syntax Highlighting in One File

Expand Messages
  • A. S. Budden
    Evenin All (or not, depending on your timezone), I was just reading http://www.vim.org/tips/tip.php?tip_id=857 about having blocks of different language code
    Message 1 of 5 , Sep 28, 2005
    View Source
    • 0 Attachment
      Evenin' All (or not, depending on your timezone),

      I was just reading http://www.vim.org/tips/tip.php?tip_id=857 about
      having blocks of different language code in one file and having vim
      syntax highlight them all correctly -- I was impressed.

      Now, I realise that this is _ridiculously_ optimistic and that the
      answer is probably going to be "no chance", but I thought I'd ask
      anyway. What I would like to be able to do is syntax highlight part
      of a line in a different language, specifically perl within matlab
      comments. For example,

      --SNIP--
      % matlab_function.m
      disp('Hello World');

      % PERL: #!/usr/bin/perl
      % PERL: print "Hello World\n";
      --SNIP--

      With the first two lines highlighted as matlab code, the "% PERL: "
      bits in green (or whatever comment colour you use) and the rest of the
      last two lines highlighted as a normal perl program.

      So, who's going to be the first to say "no chance"?

      Or... is there a chance?

      Many thanks for the help (and all the countless other bits of help
      over the last few years),

      Al
    • A. J. Mechelynck
      ... I don t know how, but I believe it is possible, since Vim can highlight CSS or vbscript or jscript within HTML, or (I think) HTML within PHP, and
      Message 2 of 5 , Sep 28, 2005
      View Source
      • 0 Attachment
        A. S. Budden wrote:
        > Evenin' All (or not, depending on your timezone),
        >
        > I was just reading http://www.vim.org/tips/tip.php?tip_id=857 about
        > having blocks of different language code in one file and having vim
        > syntax highlight them all correctly -- I was impressed.
        >
        > Now, I realise that this is _ridiculously_ optimistic and that the
        > answer is probably going to be "no chance", but I thought I'd ask
        > anyway. What I would like to be able to do is syntax highlight part
        > of a line in a different language, specifically perl within matlab
        > comments. For example,
        >
        > --SNIP--
        > % matlab_function.m
        > disp('Hello World');
        >
        > % PERL: #!/usr/bin/perl
        > % PERL: print "Hello World\n";
        > --SNIP--
        >
        > With the first two lines highlighted as matlab code, the "% PERL: "
        > bits in green (or whatever comment colour you use) and the rest of the
        > last two lines highlighted as a normal perl program.
        >
        > So, who's going to be the first to say "no chance"?
        >
        > Or... is there a chance?
        >
        > Many thanks for the help (and all the countless other bits of help
        > over the last few years),
        >
        > Al
        >
        >
        >
        >
        I don't know how, but I believe it is possible, since Vim can highlight
        CSS or vbscript or jscript within HTML, or (I think) HTML within PHP,
        and ends-of-lines are essentially irrelevant in HTML. If I were you I
        would look how it does it.

        Best regards,
        Tony.
      • A. S. Budden
        ... [snip] ... OK! Well, if it s possible, then it s probably doable! (well, maybe). I ll have a delve over the weekend and see if I can see how the html
        Message 3 of 5 , Sep 29, 2005
        View Source
        • 0 Attachment
          On 28/09/05, A. J. Mechelynck <antoine.mechelynck@...> wrote:
          > A. S. Budden wrote:
          > > Now, I realise that this is _ridiculously_ optimistic and that the
          > > answer is probably going to be "no chance", but I thought I'd ask
          > > anyway. What I would like to be able to do is syntax highlight part
          > > of a line in a different language, specifically perl within matlab
          [snip]

          > I don't know how, but I believe it is possible, since Vim can highlight
          > CSS or vbscript or jscript within HTML, or (I think) HTML within PHP,
          > and ends-of-lines are essentially irrelevant in HTML. If I were you I
          > would look how it does it.

          OK! Well, if it's possible, then it's probably doable! (well, maybe).
          I'll have a delve over the weekend and see if I can see how the html
          syntax highlighter works. Thanks for the help!

          Al
        • Charles E. Campbell, Jr.
          ... Hello! ... [-- perl --] {{{3 if (has( perl ) || g:vimembedscript) && filereadable(expand( :p:h ). /perl.vim ) unlet! b:current_syntax syn include
          Message 4 of 5 , Sep 29, 2005
          View Source
          • 0 Attachment
            A. S. Budden wrote:

            >I was just reading http://www.vim.org/tips/tip.php?tip_id=857 about
            >having blocks of different language code in one file and having vim
            >syntax highlight them all correctly -- I was impressed.
            >
            >Now, I realise that this is _ridiculously_ optimistic and that the
            >answer is probably going to be "no chance", but I thought I'd ask
            >anyway. What I would like to be able to do is syntax highlight part
            >of a line in a different language, specifically perl within matlab
            >comments...snip
            >
            >
            Hello!

            Here's a snippet from syntax/vim.vim, including perl highlighting:

            ---------------------------------------------------------------------
            " [-- perl --] {{{3
            if (has("perl") || g:vimembedscript) &&
            filereadable(expand("<sfile>:p:h")."/perl.vim")
            unlet! b:current_syntax
            syn include @vimPerlScript <sfile>:p:h/perl.vim
            syn region vimPerlRegion matchgroup=vimScriptDelim
            start=+pe\%[rl]\s*<<\s*\z(.*\)$+ end=+^\z1$+ contains=@vimPerlScript
            syn region vimPerlRegion matchgroup=vimScriptDelim
            start=+pe\%[rl]\s*<<\s*$+ end=+\.$+ contains=@vimPerlScript
            endif
            ---------------------------------------------------------------------

            So, the perl syntax highlighting is "included" into a cluster.
            All syntax items in it will have the "contained" flag added. (see :he
            syn-include).

            There are two regions specified above that include perl syntax.
            So, if you can specify a syntax region (see :he syn-region),
            it can contain whatever syntax highlighting you specify.

            Regards,
            Chip Campbell
          • A. S. Budden
            ... Fantastic! Thank you for your help (yet again). I ve got it implemented with the following in ~/.vim/after/syntax/matlab.vim: unlet! b:current_syntax syn
            Message 5 of 5 , Oct 1, 2005
            View Source
            • 0 Attachment
              On 29/09/05, Charles E. Campbell, Jr. <drchip@...> wrote:
              > A. S. Budden wrote:
              >
              > >I was just reading http://www.vim.org/tips/tip.php?tip_id=857 about
              > >having blocks of different language code in one file and having vim
              > >syntax highlight them all correctly -- I was impressed.
              > >
              > >Now, I realise that this is _ridiculously_ optimistic and that the
              > >answer is probably going to be "no chance", but I thought I'd ask
              > >anyway. What I would like to be able to do is syntax highlight part
              > >of a line in a different language, specifically perl within matlab
              > >comments...snip
              > >
              > >
              > Hello!
              >
              > Here's a snippet from syntax/vim.vim, including perl highlighting:
              >
              > ---------------------------------------------------------------------
              > " [-- perl --] {{{3
              > if (has("perl") || g:vimembedscript) &&
              > filereadable(expand("<sfile>:p:h")."/perl.vim")
              > unlet! b:current_syntax
              > syn include @vimPerlScript <sfile>:p:h/perl.vim
              > syn region vimPerlRegion matchgroup=vimScriptDelim
              > start=+pe\%[rl]\s*<<\s*\z(.*\)$+ end=+^\z1$+ contains=@vimPerlScript
              > syn region vimPerlRegion matchgroup=vimScriptDelim
              > start=+pe\%[rl]\s*<<\s*$+ end=+\.$+ contains=@vimPerlScript
              > endif
              > ---------------------------------------------------------------------
              >
              > So, the perl syntax highlighting is "included" into a cluster.
              > All syntax items in it will have the "contained" flag added. (see :he
              > syn-include).
              >
              > There are two regions specified above that include perl syntax.
              > So, if you can specify a syntax region (see :he syn-region),
              > it can contain whatever syntax highlighting you specify.

              Fantastic!

              Thank you for your help (yet again). I've got it implemented with the
              following in ~/.vim/after/syntax/matlab.vim:

              unlet! b:current_syntax
              syn include @matlabPerlScript $VIMRUNTIME/syntax/perl.vim
              syn region matlabPerlRegion matchgroup=matlabScriptDelim start=+^%
              PERL: +hs=s+8 end=+$+ contains=@matlabPerlScript

              (in case gmail fluffs it up, that should be three lines)

              This is perfectly good for what I need, but just out of curiosity, can
              you think of a way of making it keep the "% PERL: " bit still
              highlighted as a comment -- the above changes it back to plain text.
              I've tried adding

              syn match Comment +^% PERL: +

              after the above code, but it just makes a mess of everything! Not that
              important really, I can live without the green line start...

              Many thanks again,

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