Source-highlight for Matlab/octave

- - posted in homebrew, matlab, tips

I recently worked with Matlab a lot. When in console, sometimes I want to use less to quickly examine the file content, and I have already set it up such that it uses source-highlight to output colorful escape sequence to the console. However, source-highlight does not come with a syntax support for Matlab by default. Luckily, this post and this (in Chinese) provides a solution.

First of all, install source-highlight using homebrew.

1
brew install source-highlight

Note that source-highlight depends on boost, and as of the date of this post, brew provides a precompiled library (bottle) for boost. However, the python support is compiled against the system python, so if you installed a custom one (say installed via homebrew) and use it by default, brew will compile the boost from source instead, which takes an extremely long time. To prevent this, we need to unlink it first, and link it back afterwards. That is,

1
2
brew unlink python
brew link python    # after installation

Go to the following folder and create two files,

1
cd `brew --prefix source-highlight`/share/source-highlight
matlab.style
1
2
3
keyword blue;
string #a020f0;
comment darkgreen;
matlab.lang
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
keyword = "break|case|catch|classdef|continue|else|elseif|end|for|function|global|if|otherwise|parfor|persistent|return|spmd|switch|try|while"

keyword = '^\s*(break|case|catch|classdef|continue|else|elseif|end|for|function|global|if|otherwise|parfor|persistent|return|spmd|switch|try|while)(?=\s)'

include "function.lang"

state command start '^\s*[a-zA-Z][\w]*(?=\s)(?!\s+\.?[*/+-:]\s+\w)(?!\s+=)' begin
        comment start "%"
        string delim '[[:blank:]]' '[^%;]*'
end

comment delim "%{" "%}" multiline nested
comment start "%"

string delim '(?<![\w.])\'' "'" escape "''"

Finally, edit lang.map to create a mapping for matlab file.

lang.map
1
echo 'm = matlab.lang' >> lang.map

Comments