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 Alan Walker

Different World flac

Alan Walker. 2018. Writer: Shy Nodi;Alan Walker;Fredrik Borch Olsen;James Njie;Marcus Arnbekk;Gunnar Greve Pettersen;K-391;Corsak;Shy Martin;Magnus Bertelsen.
2

Tell Me It's Over

3 Ariana Grande

​Thank U, Next flac

Ariana Grande. 2018. Writer: Crazy Mike;Scootie;Victoria Monét;Tayla Parx;TBHits;Ariana Grande.
4 Mesto

Wait Another Day flac

Mesto. 2018.
5 Anne-Marie

Rewrite The Stars flac

Anne-Marie. 2018. Writer: Benj Pasek;Justin Paul.
6 Rita Ora

Let You Love Me flac

Rita Ora. 2018. Writer: Rita Ora;Easyfun;Fred Gibson;Noonie Bao;LotusIV;Ilsey Juber.
7 Conor Maynard

How You Love Me flac

Conor Maynard. 2018. Writer: Yoshi Breen;Thom Bridges;Hardwell;Rik Annema;Conor Maynard;Cimo Fränkel;Snoop Dogg.
8 Fitz And The Tantrums

HandClap flac

Fitz And The Tantrums. 2017. Writer: Fitz And The Tantrums;Eric Frederic;Sam Hollander.
9 ZAYN

There You Are flac

ZAYN. 2018. Writer: Joe Garrett;Levi Lennox;Michael Hannides;Anthony Hannides;ZAYN.
10 (G)I-DLE

POP/STARS flac

(G)I-DLE. 2018. Writer: Riot Music Team;Harloe.
11 Clean Bandit

Baby flac

Clean Bandit. 2018. Writer: Jack Patterson;Kamille;Jason Evigan;Matthew Knott;Marina;Luis Fonsi.
12 Bastille

Grip flac

Bastille. 2018. Writer: Dan Smith;Espen Berg;Joakim Haukaas;Mark Crew;Simen Eriksrud;Thomas Eriksen.
13 Ariana Grande

Imagine flac

Ariana Grande. 2018. Writer: Jameel Roberts;Priscilla Renea;Happy Perez;Andrew "Pop" Wansel;Ariana Grande.
14 Mark Ronson

Nothing Breaks Like A Heart flac

Mark Ronson. 2018. Writer: Thomas Brenneck;Maxime Picard;Ilsey Juber;Conor Szymanski;Clement Picard;Mark Ronson;Miley Cyrus.
15 ZAYN

Good Years flac

ZAYN. 2018. Writer: Anthony Hannides;Khaled Rohaim;Michael Hannides;ZAYN;Herbie Crichlow.
16 The Chainsmokers

Beach House flac

The Chainsmokers. 2018. Writer: Andrew Taggart.
17 Clean Bandit

Mama flac

Clean Bandit. 2018. Writer: Jason Evigan;Jack Patterson;Grace Chatto;Ellie Goulding;Caroline Ailin.
18 Halsey

Without Me (Illenium Remix) flac

Halsey. 2018. Writer: Halsey;Delacey;Louis Bell;Amy Allen;Justin Timberlake;Timbaland;Scott Storch.
19 Martin Garrix

Dreamer (Nicky Romero Remix) flac

Martin Garrix. 2018. Writer: Yury Parkhomenko;Aleksandr Parkhomenko;Ilsey Juber;Brian Lee;Mike Yung;Martin Garrix.
20 Julian Jordan

Glitch flac

Julian Jordan. 2018.

Related questions

Hot questions

Language

Popular Tags