Search code examples
vimschemelispvim-syntax-highlighting

Vim 8.0 extended syntax highlighting for scheme


I use different versions of scheme (Gambit-C and Guile) and I have written a syntax file for (most of) Gambit-C's keywords. However I only want to use it when I am using files for Gambit-C.

Sourcing it from my vimrc does not work as the scheme syntax is sourced after my vimrc and If I source it using -s < scriptin > or -c < cmd >, it only works for the first file opened and I have to source it for each extra file/argument.

I saw this solution which solved my problem partially, but I do not really want vim to use the Gambit-C syntax for every scheme file. I also tried adding

if exists("b:is_gambit") || exists("is_gambit")
       "syntax extensions
         ....."
endif 

and using it like was shown in the chicken scheme vim help but that did not work and then I realized that it did not seem to work for chicken scheme either (i.e. Setting "let b:is_chicken" in the ".vim/ftplugin/scheme.vim" file does not add chickens syntax to scheme)!!

Am I doing something wrong with the variables "(let b:is_chicken)" and (let b:is_gambit) or is there another way to make it work for selected scheme files eg making .vimrc source a file after all the files have loaded?

UPDATE: It turns out that for some reason, ".vim/ftplugin/scheme.vim" was not getting sourced and the variable "b:is_chicken was not set (this can be checked using ":scriptnames" or "echo b:is_chicken"). I then put "let b:is_chicken" in ".vim/syntax/scheme.vim" and the chicken scheme syntax worked. This does not distinguish between different scheme files though.


Solution

  • There seem to be three ways to do what I wanted, I needed to (try to) understand variables, autocmd and modelines

    1. Adding to the to the vimrc that will be used for Gambit-C files (i.e. vi -u {vimrc} [Gambit-C files] ) any one of the following

               let g:is_gambit=1  "" This works on all the buffers 
      

    or

                :autocmd BufRead *.scm let b:is_gambit "" This works for all the buffers but is redundant given the above.
    

    and placing the syntax file of the form

       if exists("b:is_gambit") || exists("is_gambit")
             "syntax extensions
                ....."
       endif 
    

    as set up above to ".vim/after/syntax/scheme.vim".

    Note: Setting b:is_gambit in the -u {vimrc} only sets it for the current buffer and g:is_gambit means all open (scheme .i.e. ".scm" ) buffers (if b:is_gambit is set in the ".vim/syntax/scheme.vim" it works in all buffers though). For variables, b = current buffer and g = global.

    or......

    1. Adding to the to the vimrc that will be used for Gambit-C files (i.e. vi -u {vimrc} [Gambit-C files] )

           :autocmd BufEnter *.scm source /path/to/file/gambitc.vim ""Note BufEnter not BufRead
      

    Where gambitc.vim is the syntax file for Gambit-C.

    Note: The gambitc.vim here only needs the extra syntax to be added to the scheme syntax

    and......

    1. As explained properly here setting modelines in your general vimrc and placing the Gambit-C syntax file in ".vim/syntax/" under a different name (e.g ".vim/syntax/gambitc.vim") and sourcing the original scheme syntax by placing this in the file

             :source /usr/share/vim/vim80/syntax/scheme.vim
      

    or whatever the path to the scheme syntax file is.

    Then in every file you want to use the Gambit-C syntax type

                ;; vim: filetype=gambitc
    

    i.e comment this line from the current language and then write the statement.

    If there is anything wrong with any of my solutions please let me know, I personally prefer the second one as it involves less work when porting it to different machines, though I suspect using the "g:is_gambit" method is more efficient