Custom gedit Syntax Highlighting for Dummies?

I want to make (have, actually) a custom syntax highlighting for gedit.

There would be just a few different items:

A line that begins with 2 tabs. ----- One color. A line that begins with 3 tabs. ----- Another color. A line that begins with 4 tabs. ----- Another color. and a line that begins with INT. or EXT. ----- Another Color.

This is for writing a screenplay.

I looked at the language definition wiki page for gedit but it is way over my head.

Is there a simple way I could do this?


The following was derived from the GtkSourceView reference manual, Language Definition v2.0 Tutorial and Reference.

Create the file /usr/share/gtksourceview-2.0/language-specs/screenplay.lang (as root), with the following contents:

<?xml version="1.0" encoding="UTF-8"?>
<language id="screenplay" _name="Screenplay" version="2.0" _section="Markup">
  <metadata>
    <property name="mimetypes">text/plain</property>
    <property name="globs">*.script</property>
  </metadata>
  <styles>
    <style id="indent2" _name="2x indented"  map-to="def:comment" />
    <style id="indent3" _name="3x indented"  map-to="def:constant" />
    <style id="indent4" _name="4x indented"  map-to="def:identifier" />
    <style id="intext"  _name="INT. or EXT." map-to="def:statement" />
  </styles>
  <definitions>
    <context id="screenplay">
      <include>
        <context id="indent4" style-ref="indent4">
          <start>^\t\t\t\t</start>
          <end>$</end>
        </context>
        <context id="indent3" style-ref="indent3">
          <start>^\t\t\t</start>
          <end>$</end>
        </context>
        <context id="indent2" style-ref="indent2">
          <start>^\t\t</start>
          <end>$</end>
        </context>
        <context id="intext" style-ref="intext">
          <start>^(INT|EXT)\.</start>
          <end>$</end>
        </context>
      </include>
    </context>
  </definitions>
</language>

Make sure the file is world-readable by running

sudo chmod 0644 /usr/share/gtksourceview-2.0/language-specs/screenplay.lang

Restart gedit. Now any file named *.script that you open in gedit should show the type "Screenplay" in the status bar at bottom. Lines indented with 2, 3, or 4 tabs should be colored blue, magenta, and cyan, respectively, and lines beginning with INT. or EXT. will be red and bold-face.

You can adjust this in a few ways:

  • The .script file name suffix is set on line 5, in the "globs" property. You can change this in the obvious way, and include multiple values if you want, separated by semicolons (;).

  • The colors that you get for the indented and INT. and EXT. lines depend on the particular color scheme that you've selected for gedit. They're the colors of the comment, constant, identifier, and statement styles (that's what map-to="def:comment" etc. do). To get different results, you can change the gedit color scheme (Edit > Preferences > Fonts and Colors), or rearrange the map-to values in the <style> tags. If you want more control, you can create your own color scheme as follows:

    cd /usr/share/gtksourceview-2.0/styles
    sudo cp classic.xml screenplay.xml
    

    Edit screenplay.xml (as root). Replace the first three non-comment lines by:

    <style-scheme id="screenplay" _name="Screenplay" version="1.0">
      <author>YAS</author>
      <_description>Screenplay color scheme</_description>
    

    Then adjust the colors (and maybe other styles too, e.g. bold="true") of the def:comment, def:constant, def:identifier, and def:statement styles. Restart gedit again, and change gedit's color scheme to Screenplay to see your new colors. Note that that's a global change though; you may want to change back to your regular color scheme to edit other files.

Note that any time you change the screenplay.lang or screenplay.xml files, you'll have to restart gedit to see the results.