一月前ぐらいから試験的に使用している萌ディタが、とうとうカラー表示対応になった。時々不安定さを感じることもあるのだけれど、ちょっとしたテキストの編集とかサイトの更新の下書きとかに使う分には十分な機能が付いてきたように思う。(ただし、本格的に何かをしようと思うと、結構メンドクサイ。)
そんなわけで気まぐれにpython用のカラー表示を作ってみた。idleっぽい感じで、最低限のキーワード色分けを行う。文字列の処理あたりは自分でもかなり汚いと思う。オートインデントも頑張れば作れそうだけど、本体がまだα版だから、バージョンアップでざっくり変更があったりすると面倒だしなぁ。


/*
* python.javascript.txt
*
* pythonファイルの拡張子クラス
*
* 2004年9月25日 by nakayato..
* http://cats.ruru.ne.jp/hideworks/
*/

function class_python() {
this.name = 'python';
this.parent = 'srcfile';
this.ext = '\\.py$';
}

var f = class_python.prototype;

f.onInitProp = function (arg, classname, methodname) {
var lex = App.Lexes.Add('python');
App.Prop(this.name, 'lex') = lex.name;

lex.DefaultColor('11..20') = '@string1'; // 10〜19は文字列

/*
lex.Add(
'encoding-descripsion',
'1..9/^.*coding(\\=\\:)?\\s?.*$/',
'color:@altforeground1; background-color:@altbackground1;');
*/
lex.Add(
'encoding-descripsion',
'1..9/^.*coding(\\=\\:)?\\s?.*$/',
'color:#FFFFFF; background-color:@green;');

lex.Add(
'comment',
'1..9/\\#.*$/',
'color:@comment1');

lex.Add(
'l-literal-in',
'1..9/\\\'\\\'\\\'|\\\'|\\"\\"\\"|\\"/',
'state:11');

lex.Add(
'l-literal-escape-in',
'12..15/[\\\\]/',
'no-style; state:+5');
lex.Add(
'l-literal-escape-out',
'16..20/./',
'no-style; state:-5');

lex.Add(
'l-literal-in-sq',
'11/\\\'\\\'\\\'/',
'no-style; state:12');
lex.Add(
's-literal-in-sq',
'11/\\\'/',
'no-style; state:13');
lex.Add(
'l-literal-out-sq',
'12/\\\'\\\'\\\'/',
'no-style; state:1');
lex.Add(
's-literal-out-sq',
'13/\\\'/',
'no-style; state:1');

lex.Add(
'l-literal-in-wq',
'11/\\"\\"\\"/',
'no-style; state:14');
lex.Add(
's-literal-in-wq',
'11/\\"/',
'no-style; state:15');
lex.Add(
'l-literal-out-wq',
'14/\\"\\"\\"/',
'no-style; state:1');
lex.Add(
's-literal-out-wq',
'15/\\"/',
'no-style; state:1');

lex.Add(
'define-in',
'1/\\<(class|def)\\>/',
'color:#FF8000; state:2');
lex.Add(
'define',
'2/\\<[a-zA-Z0-9_]+[^\\(]/',
'color:@blue;');
lex.Add(
'define-out',
'2/\\(|:/',
'no-style; state:1;');
lex.Add(
'define-err',
'2/.*$/',
'color:@red; state:1;');

lex.Add(
'keywords',
'1/\\<(and|del|for|is|raise|assert|elif|from|lambda|return|break|' +
'else|global|not|try|class|except|if|or|while|continue|exec|import|' +
'pass|yield|def|finally|in|print)\\>/',
'color:#FF8000');
lex.Add(
'native-functions',
'1/\\<(__import__|abs|basestring|bool|callable|chr|classmethod|cmp|' +
'compile|complex|delattr|dict|dir|divmod|enumerate|eval|execfile|file|' +
'filter|float|getattr|globals|hasattr|hash|help|hex|id|input|int|isinstance|' +
'issubclass|iter|len|list|locals|long|map|max|min|object|oct|open|ord|' +
'pow|property|range|raw_input|reduce|reload|repr|round|setattr|slice|' +
'staticmethod|str|sum|super|tuple|type|unichr|unicode|vars|xrange|zip)\\>/',
'color:@navy');
lex.Add(
'identifiers',
'1/(\\<|\\.)__?[a-zA-Z][a-zA-Z0-9]+(__)?\\>/',
'color:@identifier1');
lex.Add(
'hexa-literal',
'1/\\<0x[0-9a-f]+\\>/i',
'color:@number1');
lex.Add(
'numeric-literal',
'1/\\<-?((0|[1-9][0-9]*)\\.([0-9]+)?(e[-+]?[0-9]+)?|' +
'\\.[0-9]+(e[-+]?[0-9]+)?|' +
'(0|[1-9][0-9]*)(e[-+]?[0-9]+)?)\\>/i',
'color:@number1');

}
addClass(new class_python());