gedit syntax coloring for AMPL

(Note: the coloring is by no means perfect or even all that logical, so please tweak to whatever extent you desire.)

I use AMPL a lot in my research and I wanted gedit (my linux IDE of choice) to color the syntax to make programming easier.

Below is a quick run down of how to get syntax coloring / syntax highlighting working in Fedora 14 (same procedure works in F12 and F13 as far as I can tell).

Create AMPL Language File

This should be placed in:

~/.local/share/gtksourceview-2.0/language-specs/ampl.lang
<?xml version="1.0" encoding="UTF-8"?>
<!--

 Author: Edwards Research <http://www.edwards-research.com>
 Copyright (C) 2010 Edwards Research <http://www.edwards-research.com>

-->
<language id="ampl" _name="AMPL" version="2.0" _section="Scripts">
  <metadata>
    <!-- <property name="mimetypes">text/x-ampl;text/plain</property> -->
    <property name="mimetypes"></property>
    <property name="globs">*.mod;*.dat;*.run</property>
    <property name="line-comment-start">#</property>
  </metadata>

  <styles>
    <style id="comment" _name="Comment" map-to="def:comment"/>
    <style id="keyword" _name="Keyword" map-to="def:keyword"/>
    <style id="operator" _name="Operator" map-to="def:operator"/>
    <style id="math" _name="math" map-to="def:string"/>
    <style id="logic" _name="logic" map-to="def:special-constant"/>
    <style id="bracket" _name="Bracket" map-to="def:operator"/>
    <style id="floating-point" _name="Floating point number" map-to="def:floating-point"/>
    <style id="decimal" _name="Decimal number" map-to="def:decimal"/>
    <style id="type" _name="Type" map-to="def:type"/>
  </styles>

  <default-regex-options case-sensitive="false"/>

  <definitions>
    <context id="ampl">
      <include>
        <context id="comment" style-ref="comment" end-at-line-end="true">
          <start>#</start>
          <end>\n</end>
        </context>

        <context id="comment-multiline" style-ref="comment" class="comment" class-disabled="no-spell-check">
            <start>/\*</start>
            <end>\*/</end>
            <include>
                <context ref="def:in-comment"/>
            </include>
        </context>

        <context id="keyword" style-ref="keyword">
          <keyword>minimize</keyword>
          <keyword>maximize</keyword>
          <keyword>subject to</keyword>
          <keyword>check</keyword>
          <keyword>within</keyword>
          <keyword>cross</keyword>
          <keyword>in</keyword>
          <keyword>from</keyword>
          <keyword>to</keyword>
          <keyword>obj</keyword>
          <keyword>net_in</keyword>
          <keyword>net_out</keyword>
        </context>

        <context id="type" style-ref="type">
          <keyword>var</keyword>
          <keyword>set</keyword>
          <keyword>param</keyword>
          <keyword>node</keyword>
          <keyword>arc</keyword>
          <keyword>let</keyword>
          <keyword>model</keyword>
          <keyword>data</keyword>
          <keyword>display</keyword>
        </context>

        <context id="math" style-ref="math">
          <keyword>abs</keyword>
          <keyword>acos</keyword>
          <keyword>acosh</keyword>
          <keyword>asin</keyword>
          <keyword>ainsh</keyword>
          <keyword>atan</keyword>
          <keyword>atan2</keyword>
          <keyword>atanh</keyword>
          <keyword>cos</keyword>
          <keyword>cosh</keyword>
          <keyword>exp</keyword>
          <keyword>log</keyword>
          <keyword>log10</keyword>
          <keyword>max</keyword>
          <keyword>min</keyword>
          <keyword>sin</keyword>
          <keyword>sinh</keyword>
          <keyword>sqrt</keyword>
          <keyword>tan</keyword>
          <keyword>tanh</keyword>
          <keyword>sum</keyword>
          <keyword>prod</keyword>
          <keyword>\+</keyword>
          <keyword>-</keyword>
          <keyword>/</keyword>
          <keyword>\*</keyword>
        </context>

        <context id="logic" style-ref="logic">
          <keyword>if</keyword>
          <keyword>then</keyword>
          <keyword>else</keyword>
          <keyword>or</keyword>
          <keyword>exists</keyword>
          <keyword>forall</keyword>
          <keyword>for</keyword>
          <keyword>and</keyword>
          <keyword>not</keyword>
          <keyword>in</keyword>
          <keyword>not in</keyword>
        </context>

        <context id="operators" style-ref="operator" extend-parent="false">
          <match>[:;\=&lt;&gt;]</match>
        </context>

        <context id="brackets" style-ref="bracket" extend-parent="false">
          <match>[\{\}()\[\]]</match>
        </context>

        <context id="float" style-ref="floating-point">
          <match extended="true">
            (?&lt;![\w\.])
            ([0-9]+[Ee][+-]?[0-9]+ |
             ([0-9]*\.[0-9]+ | [0-9]+\.[0-9]*)([Ee][+-]?[0-9]+)?)
            (?![\w\.])
          </match>
        </context>

        <context id="decimal-number" style-ref="decimal">
          <match extended="true">
            (?&lt;![\w\.])
            [+-]?([1-9][0-9]*|0)
            (?![\w\.])
          </match>
        </context>


      </include>
    </context>

  </definitions>
</language>

Since this overloads the text/plain MIME-type, this is all you need to do, however AMPL files may not open in gedit automatically colored correctly. To fix this, you will want to define a MIME-type of text/x-ampl.

Define AMPL MIME-type

This should be placed in:

~/.local/share/mime/text/x-ampl.xml
<?xml version="1.0" encoding="utf-8"?>
<mime-type xmlns="http://www.freedesktop.org/standards/shared-mime-info" type="text/x-ampl">
  <!--Created automatically by update-mime-database. DO NOT EDIT!-->
  <comment>AMPL Source</comment>
  <glob pattern="*.mod"/>
  <glob pattern="*.dat"/>
  <glob pattern="*.run"/>
</mime-type>

Alternatively, you could add lines 2-8 to

/usr/share/mime/packages/freedesktop.org.xml

… and then run

sudo update-mime-database /usr/local/share/mime

… to update the mime database.

Posted Sunday, October 24th, 2010 under linux, programming, tips and tricks.

4 comments

  1. saved my life :) just started with an AMPL seminar and searched for a better editor with syntax highlighting :)

  2. Nice post. I just needed to change gtksourceview-2.0 to gtksourceview-3.0 to make it work with Gedit 3.

  3. Thank you for sharing this! :)

  4. Thanks! This is great!

Leave a Reply