Ubuntu – How to add a new language definition for syntax highlighting in Gedit

geditjulia-langsyntax highlighting

I've attempted to create a new .lang file for getting Gedit to recognize the Julia programming language. I have read the tutorial, the reference document and the wiki page Gedit/NewLanguage, and I believe the file to be correct. However, upon restarting gedit after copying the new julia.lang file to /usr/share/gtksourceview-3.0/language-specs/, a .jl file is indeed recognized and the syntax highlighting menu is set to julia, but no actual highlighting occurs.

Thinking that I might have done something wrong, I tried instead using an existent language file, for a similar language (Matlab), and only changed the header metadata into:

<language id="julia" _name="Julia" version="2.0" _section="Scientific">
  <metadata>
    <property name="mimetypes">text/x-julia;application/x-julia</property>
    <property name="globs">*.jl</property>
    <property name="line-comment-start">#</property>
  </metadata>

Everything else was kept exactly as-is, and I saved the file as julia.lang. Still, when reopening gedit the same issue occurs. What's more, if I select the Matlab entry from the syntax highlighting menu, the formatting is made correctly (according to Matlab rules), even though both matlab.lang and julia.lang have exactly the same syntax definitions!

What could I be doing wrong? This guy seemed to have the same problem, but it was never revealed in that thread how he solved it (if he ever did). Any ideas?


Update: my mistake was indeed not changing the context id as pointed out by @carandraug in his answer. I did, however, have other issues which I'll note here in case they're of help to anyone:

  1. I reused the "shell-like-comment" definition from def.lang for Julia's single-line comments, but that was getting me a lot of errors. It turns out when a reference context is used, the id attribute cannot be set. Changing from <context id="comment" ref="def:shell-like-comment" /> to <context ref="def:shell-like-comment" /> made the errors go away. I think the reference document (and the tutorial as well) should mention this caveat. I've edited the wiki page to point this out.
  2. Before I figured the problem with the comments context, I commented out its definition, but then I started getting a "style 'def:string' not defined" error. When I uncommented the definition, this error disappeared. I'm not sure what caused it in the first place (considering that indeed a <style id="string"> was defined in def.lang). Any clarifications about this are welcome in the comments 🙂

A final suggestion to anyone developing new language highlight definitions for gedit: don't forget to run gedit from the command line and look at the console output!

Best Answer

I do not know why your original julia.lang file is not working since you're not showing the source, but the one you based on matlab.lang will not work because there's no context inside <definitions> with the same id you mentioned for <language>.

Basically, you're saying the id of this language is julia, but there is no context with that id being defined. So find <context id="matlab" (line 149 of the current HEAD), and change it to julia.

A lang file is usually organized as a bunch of individual contexts for the language features being highlighted, which are then grouped in the end into a main block with the same id as the language, and references to the other contexts.