Posts

Adding MATLAB Block Comment Syntax to Notepad++ / Scintilla

in Technical Articles

June 2013 Edit: Block comment support was officially added to Scintilla in April 2013. Newer versions should include MATLAB/Octave style block comments automatically.

A few days ago on Super User, it was brought to my attention that the current version of Notepad++ does not work with MATLAB/Octave’s multiline/block comment syntax. In MATLAB, comments are denoted by %, with block comments started by %{ and ended with %} (you can use the same syntax with Octave, except you may also use # instead of % characters).

Notepad++ is based on Scintilla, a cross platform code editing component. I had a quick look through the lexer for MATLAB documents (the lexers/LexMatlab.cxx file), and indeed, there was no mention of any kind of block or multiline comments.

As it turns out, adding support for block comments was fairly easy. I’ve already submitted a bug report to the Scintilla Bug Tracker, which includes a patch you can apply to the LexMatlab.cxx file mentioned above (note that the bug report has been addressed, and block comment support was included officially in Scintilla starting in April 2013). Technical information regarding the patch is detailed below.

Once the fix has been made, you can simply rebuild Scintilla, and replace the binaries on your system with the new ones (I tested the fix in SciTE). For people on Windows using Notepad++, you will need to recompile the Scintilla SciLexer.dll incluced with the Notepad++ source distribution (the LexMatlab.cxx file is the same, so you can use the same patch), and replace the existing .DLL in the Notepad++ folder with the new one. As this is a fairly cosmetic issue, a developer has indicated that the MATLAB lexer will be fixed early next year.

To implement the patch, I simply extended the SCE_MATLAB_COMMENT to cover both single-line and block comments. You can see all of the changes outlined in the following patch:

diff -r 85ed8066ea9c lexers/LexMatlab.cxx
--- a/lexers/LexMatlab.cxx	Mon Oct 22 08:59:54 2012 +1100
+++ b/lexers/LexMatlab.cxx	Tue Oct 23 21:55:11 2012 -0400
@@ -66,6 +66,7 @@
 	styler.StartAt(startPos);
 
 	bool transpose = false;
+	bool inBlockComment = false;
 
 	StyleContext sc(startPos, length, initStyle, styler);
 
@@ -121,15 +122,24 @@
 			} else if (sc.ch == '\"') {
 				sc.ForwardSetState(SCE_MATLAB_DEFAULT);
 			}
-		} else if (sc.state == SCE_MATLAB_COMMENT || sc.state == SCE_MATLAB_COMMAND) {
+		} else if (sc.state == SCE_MATLAB_COMMAND) {
 			if (sc.atLineEnd) {
 				sc.SetState(SCE_MATLAB_DEFAULT);
 				transpose = false;
 			}
+		} else if (sc.state == SCE_MATLAB_COMMENT) {
+			if (inBlockComment && IsCommentChar(sc.chPrev) && sc.ch == '}') {
+				sc.SetState(SCE_MATLAB_DEFAULT);
+				inBlockComment = false;
+			} else if (sc.atLineEnd && !inBlockComment) {
+				sc.SetState(SCE_MATLAB_DEFAULT);
+				transpose = false;
+			}
 		}
 
 		if (sc.state == SCE_MATLAB_DEFAULT) {
 			if (IsCommentChar(sc.ch)) {
+				if (sc.chNext == '{') inBlockComment = true;
 				sc.SetState(SCE_MATLAB_COMMENT);
 			} else if (sc.ch == '!' && sc.chNext != '=' ) {
 				sc.SetState(SCE_MATLAB_COMMAND);

It should be noted that the above patch doesn’t apply block comments as per the MATLAB/Octave specification, but rather, uses the %{ and %} tokens like C-style comments. Technically, the only way to start and end a MATLAB/Octave block comment is to have the %} on a line by itself (excluding whitespace), but this is again just a cosmetic issue (since the comments are correctly colourized between the block-comment tokens).



Post a Comment

Your email address will not be published. Required fields are marked *

*