Syntax highlighting of Oberon code (Imperative Programming)

From Spivey's Corner
Jump to: navigation, search

... using the Gnome text editor

There are many text editors that you might use in the lab sessions and in your own programming, but the Gnome text editor gedit is simple and convenient. However, if you open a file of Oberon code that ends in .m, the editor by default chooses a seemingly random set of rules for syntax highlighting.

The problem is that the rules built into Gnome identify files with names ending in .m as containing code written in Objective C, a completely different language. To fix this, we need to add a new rule that identifies these files as containing Oberon code, and add to the text editor a language description that tells it how to highlight the syntax of Oberon.

Of course, you can solve the problem another way by turning off syntax highlighting and work in monochrome; to do this, just choose the menu item View/Highlight mode/None.

Alternatively, follow the instructions below, which assume that the editor is an up-to-date version based on gtksourceview-2.0[1]

  1. Create the directory ~/.local/share/gtksourceview-2.0/language-specs, and in it place this file oberon.lang.
  2. Create the directory ~/.local/share/mime/packages, and in it place this file Overrides.xml. If the file already exists, then you may need to edit it and incorporate the contents of the Overrides.xml file that is provided here.
  3. Run the command update-mime-database ~/.local/share/mime. This will compile the Overrides.xml file into a form that Gnome and the editor can use.
  4. To my eyes, the default highlighting colours look rather garish. For a more subtle effect, create the directory ~/.local/share/gtksourceview-2.0/styles, and in it place this file sober.xml.

In these instructions, ~ refers to your home directory; if this is /home/mike, then the first directory mentioned is actually /home/mike/.local/share/gtksourceview-2.0/language-specs. Note also that by a common unix convention, files and directories whose names start with a dot do not show up in directory listings. Use ls -a to see them, or choose View/Show hidden files in a browser window.

When you have followed these instructions, you may find that double-clicking on a .m file shows a dialog box where you must choose to open these files with the Text Editor application.[2] In the editor, you should see the file with appropriate syntax highlighting. If you installed sober.xml you should see that a new colour scheme named 'Sober' has appeared in the list under Edit/Preferences/Fonts & Colors.

... using Notepad++

[This section was contributed by Merlyn, a former student].

Syntax highlighting can be obtained using the Oberon-2 Lexer plugin, and limited autocompletion[3] using the Obide plugin, both part of the Oberon Revival Project hosted on SourceForge.[4]

You need to use Notepad++ v5.7 or earlier, rather than the latest (v5.9 as of time of writing).

Once both plugins have been installed as dictated by the PluginName.txt file in each archive[5], simply restart Notepad++.

Spivey's preferred style is that Oberon files end with the extension .m. To configure Notepad++ to automatically recognise .m files as Oberon files (rather than Matlab ones):

  1. Launch Notepad++.
  2. Go to Settings > Style Configurator...
  3. In the dialog that appears, scroll down the Languages list until you find Oberon-2. (Because it's a plugin, it'll be after all the built-in languages - so unless you really like plugins, it'll be at the very bottom.)
  4. Select Oberon-2, and note the appearance of the Default ext: and User ext: boxes underneath the Language: and Style: selectors.
  5. Change User ext: from OB2 Ob2 oB2 to OB2 Ob2 oB2 m

  1. To check this, run the command ldd /usr/bin/gedit | grep gtksourceview and check that it refers to a library with a name like and not one like
  2. This creates an entry in ~/.local/share/applications/mimeapps.list
  3. To fix the plugin's Ctrl-Space function, go to Settings > Shortcut Mapper... and change the other non-plugin function that normally uses Ctrl-Space to something else.
  5. Short version: (substituting C:\Program Files (x86)\ for C:\Program Files\ on 64-bit Windows installs, and whatever your system drive letter actually is for C)
    1. Quit Notepad++
    2. Extract Oberon2LexerU.dll and ObideU.dll from their respective archives to C:\Program Files\Notepad++\Plugins\.
    3. Extract Config/Oberon2LexerU.xml and Config/Obide.ini to C:\Program Files\Notepad++\Plugins\Config\.
    4. Done!