Syntax folder under plugin/foo/syntax/foo.vim not automatically applied when filetype change?

wengwengweng 08/26/2018. 1 answers, 64 views
syntax-highlighting plugin-system

I have a plugin under nvim/plugin/foo, but the syntax file nvim/plugin/foo/syntax/foo.vim is not automatically applied when the filetype change, but if I put it under nvim/syntax/foo.vim it works

I'm now manually doing this in nvim/plugin/foo/autoload/foo.vim

let s:srcdir = expand('<sfile>:h:h:p')

func! foo#open()

    " plugin stuff
    exec 'source ' . fnameescape(s:srcdir . '/syntax/foo.vim')

endfunc

which I don't know if it's the right way to deal with plugin syntax

1 Answers


D. Ben Knoble 09/25/2018.

Difference between vim plugins & packages

:help plugin tells us

Vim's functionality can be extended by adding plugins.  A plugin is nothing
more than a Vim script file that is loaded automatically when Vim starts.  You
can add a plugin very easily by dropping it in your plugin directory.

:help packages gives

A Vim package is a directory that contains one or more plugins.  The
advantages over normal plugins:
- A package can be downloaded as an archive and unpacked in its own directory.
  Thus the files are not mixed with files of other plugins.  That makes it
  easy to update and remove.
- A package can be a git, mercurial, etc. repository.  That makes it really
  easy to update.
...

So vim calls 'plugins' single script files that execute commands. 'Packages', on the other hand, contain one or more plugins. Unfortunately, vim doesn't distinguish here where perhaps it should.

The plugins you and I are familiar with are the GitHub repos and vim.org compressed directories where the structure matches that of the runtime path:

This is a list of directories which will be searched for runtime
    files:
      filetype.vim  filetypes by file name |new-filetype|
      scripts.vim   filetypes by file contents |new-filetype-scripts|
      autoload/ automatically loaded scripts |autoload-functions|
      colors/   color scheme files |:colorscheme|
      compiler/ compiler files |:compiler|
      doc/      documentation |write-local-help|
      ftplugin/ filetype plugins |write-filetype-plugin|
      indent/   indent scripts |indent-expression|
      keymap/   key mapping files |mbyte-keymap|
      lang/     menu translations |:menutrans|
      menu.vim  GUI menus |menu.vim|
      pack/     packages |:packadd|
      plugin/   plugin scripts |write-plugin|
      print/    files for printing |postscript-print-encoding|
      spell/    spell checking files |spell|
      syntax/   syntax files |mysyntaxfile|
      tutor/    files for vimtutor |tutor|

That is, what you and I conceive of as plugins are really mini-runtime directories. But, we just said a plugin is a single script?!

Well, no. A plugin script lives in &rtp/plugin and is just executed. A plugin in the more usual sense is a collection of scripts organized into this mini-runtime directory.

And each of those directories can live in a package. See my dotfiles for some examples of package organization.

What do I do?

Your question deals specifically with a (new) filetype and its accompanying syntax files. But, to play nicely in the vim plugin ecosystem, you still want to mimic the runtimepath structure.

  • ftdetect can hold vimscript to detect the filetype, as below; and
  • syntax can hold vimscript to define the syntax for the filetype

So you might have the following files

" myplugin/ftdetect/foo.vim
au BufNewFile,BufRead *.foo set filetype=foo

" myplugin/syntax/foo.vim
if exists("b:current_syntax")
     finish
endif

echom "Our syntax highlighting code will go here."

let b:current_syntax = "foo"

" myplugin/autoload/myplugin.vim
function! myplugin#utility()
     " do something useful
endfunction

myplugin can then be a git repo (or similar) that is "installed" to ~/.vim/pack/some_fancy_name/start/myplugin.


Notes

  1. Learn Vimscript the Hardway from Steve Losh contains a much deeper introduction to this material than I can present here.

HighResolutionMusic.com - Download Hi-Res Songs

1 AJR

Birthday Party flac

AJR. 2019. Writer: Adam Met;Jack Met;Ryan Met;Peter Ivers;David Lynch.
2 Loote

Your Side Of The Bed flac

Loote. 2018. Writer: ​Jesse Saint John;Jackson Foote;Emma Lov Block.
3 AJR

100 Bad Days flac

AJR. 2019. Writer: Jack Met;Adam Met;Ryan Met.
4 Joe Jonas

Longer Than I Thought flac

Joe Jonas. 2018. Writer: Patrick Nissley;Jackson Foote;Dave Katz.
5 Loote

Out Of My Head flac

Loote. 2018. Writer: Emma Lov Block;Michael Pollack;Jeremy Dussolliet;Jackson Foote.
6 Iselin Solheim

Anyone Out There flac

Iselin Solheim. 2019. Writer: Iselin Solheim;Max Grahn.
7 Loote

Wish I Never Met You flac

Loote. 2018. Writer: Jackson Foote;Alex Peter Koste;Jeremy Dussolliet;Emma Lov Block.
8 Kim Petras

Heart To Break flac

Kim Petras. 2018. Writer: Cirkut;Aaron Joseph;Dr. Luke;Jacob Kasher;Kim Petras.
9 A L E X

Out On The Trampoline At Night flac

A L E X. 2018. Writer: A L E X.
10 A L E X

I Want To Hold Your Hand flac

A L E X. 2018. Writer: A L E X.
11 A L E X

Field flac

A L E X. 2018. Writer: A L E X.
12 A L E X

Save Me flac

A L E X. 2018. Writer: A L E X.
13 Devin

Summer Lover flac

Devin. 2019. Writer: Tommy Lee James;Stuart Crichton;Oliver Heldens;Nile Rodgers;Devin Guisande.
14 A L E X

9 To 5 flac

A L E X. 2018. Writer: A L E X.
15 A L E X

Skirt flac

A L E X. 2018. Writer: A L E X.
16 Florian Picasso

Midnight Sun (Extended Version) flac

Florian Picasso. 2019.
17 Florian Picasso

Midnight Sun flac

Florian Picasso. 2019.
18 21 Savage

Enzo flac

21 Savage. 2019. Writer: YungLunchBox;Sheck Wes;Offset;Gucci Mane;21 Savage;DJ Snake.
19 Tales Of Ratatösk

Battle Of The Doomed Gods 320kbps

Tales Of Ratatösk. 2019.
20 Tales Of Ratatösk

Andro 320kbps

Tales Of Ratatösk. 2019.

Related questions

Hot questions

Language

Popular Tags