4 A Programmer's Text Editor
6 Syntax highlight definitions.
8 Copyright (C) 1991-2009 Angel Ortega <angel@triptico.com>
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2
13 of the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 http://www.triptico.com
28 /** editor actions **/
30 mp.actions['help'] = sub (d) {
33 /* get the special word regex separator for help (if any) */
34 r = d.syntax.help_word_regex;
36 /* if there is a word at the cursor position,
37 try searching for help */
38 if ((w = mp.get_word(d, r)) != NULL)
42 mp.alert(sprintf(L("No help for '%s'."), w));
46 d = mp.new(sprintf(L("<help on '%s'>"), w), h);
51 mp.actions['section_list'] = sub (d) {
52 local l = mp.section_list(d);
55 mp.alert(L("No detection for sections for this mode."));
58 mp.alert(L("No sections were found in this document."));
62 'label' => L("Section list"),
64 'list' => map(sub (e) { e[0]; }, l),
70 mp.search_set_y(d, l[r[0]][1]);
77 /** default key bindings **/
79 mp.keycodes['f1'] = "help";
80 mp.keycodes['ctrl-d'] = "section_list";
82 /** action descriptions **/
84 mp.actdesc['help'] = LL("Help on word over cursor");
85 mp.actdesc['section_list'] = LL("Section list...");
91 /** syntax definitions **/
93 sub mp.syn_token_list(l) { '/\b(' ~ join("|", l) ~ ')\b/'; }
98 'filenames' => [ '/\.c$/i', '/\.h$/i', '/\.l$/i', '/\.y$/i', '/\.d$/i',
99 '/\.cpp$/i', '/\.hpp$/i', '/\.c++$/i', '/\.xpm$/i' ],
100 'help' => [ "man 2 %s", "man 3 %s" ],
104 "for", "while", "if", "switch", "case", "do",
105 "else", "break", "continue", "return",
106 "default", "goto", "main", "fopen", "fclose",
107 "fgets", "fgetc", "fputs", "fputc", "fprintf",
108 "putc", "printf", "sprintf", "strcpy", "strcat",
109 "strcmp", "strncmp", "strtok", "stricmp", "strchr",
110 "strrchr", "strlen", "memcmp", "memcpy", "malloc",
111 "free", "strncpy", "strncat", "snprintf", "strstr",
112 "memset", "memcpy", "va_start", "va_end", "vsprintf",
113 "vsnprintf", "atoi", "qsort", "bsearch", "getenv",
114 "fscanf", "popen", "pclose", "realloc", "fread",
115 "fwrite", "fseek", '\{', '\}', "putchar", "fflush",
116 "wcscmp", "swprintf", "wmemcpy", "swscanf", "sscanf",
117 "wcslen", "wmemset", "wcscpy", "wcsncpy", "wcscoll",
118 "mbstowcs", "wcstombs", "wprintf", "wcschr",
119 "wcsrchr", "wcsstr", "strdup", "wctomb", "mbtowc",
120 "mbrtowc", "wcrtomb", "open", "close", "read",
121 "write", "pipe", "fork", "dup", "dup2", "wait",
122 "execl", "execlp", "execle", "execv", "execvp",
123 "class", "template", "new", "delete", "emit",
124 "using", "namespace", "try", "catch", "throw"
129 "char", "int", "long", "struct", "union", "const",
130 "void", "unsigned", "signed", "auto", "volatile",
131 "enum", "typedef", "float", "double", "extern",
132 "register", "short", "sizeof", "static", "far",
133 "near", "defined", "va_list", "size_t", "wchar_t",
134 "iconv_t", "virtual", "friend", "operator",
135 "public", "protected", "private", "slots", "signals"
137 '/[-=<>:\?\+\*\/\!\%&\|]+/'
140 /* from http://ad.hominem.org/log/2005/05/quoted_strings.php */
141 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/", /* double-quoted strings */
142 "/'([^'\\\\\n]*(\\\\.[^'\\\\\n]*)*)'/", /* single-quoted strings */
143 "/\b-?[0-9]+\b/", /* numbers */
144 "/\b[0-9]+e[0-9]+\b/", /* numbers in exp format */
145 "/\b0x[0-9a-f]+\b/i", /* hex numbers */
146 "/\b[0-9[:upper:]_]+\b/" /* all-caps words */
149 [ '|/\*|', '|\*/|' ], /* C-like */
150 '|//.*$|m', /* C++ */
151 '/^\s*#[a-z]+/m' /* CPP directives */
154 [ "|/\*\*\n|", '|\*/|' ], /* mp_doccer */
155 '/^/\*\*.*\*\*/$/m' /* section mark */
158 'section' => [ '/(^\/\*\*.*\*\*\/$|^#pragma mark|^\w.*\)$)/' ]
163 'name' => 'Resource file',
164 'filenames' => [ '/\.rc$/i' ],
169 /* from http://ad.hominem.org/log/2005/05/quoted_strings.php */
170 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/", /* double-quoted strings */
171 "/'([^'\\\\\n]*(\\\\.[^'\\\\\n]*)*)'/", /* single-quoted strings */
172 "/\b-?[0-9]+\b/", /* numbers */
173 "/\b0x[0-9a-f]+\b/i", /* hex numbers */
174 "/\b[0-9[:upper:]_]+\b/" /* all-caps words */
177 [ '|/\*|', '|\*/|' ], /* C-like */
178 '|//.*$|m', /* C++ */
179 '/^\s*#[a-z]+/m' /* CPP directives */
187 'filenames' => [ '/\.pl$/i', '/\.pm$/i' ],
188 'help' => [ 'perldoc -f %s', 'perldoc %s' ],
189 'help_word_regex' => '/[A-Z_][A-Z0-9_:]*/i',
193 "for", "if", "next", "last", "else", "elsif",
194 "unless", "while", "shift", "unshift", "push",
195 "pop", "delete", "new", "bless", "return",
196 "foreach", "keys", "values", "sort", "grep",
197 "tr", "length", "system", "exec", "fork", "map",
198 "print", "write", "open", "close", "chop",
199 "chomp", "exit", "sleep", "split", "join",
200 "sub", "printf", "sprintf", "s", "glob",
201 "scalar", "my", "local", "undef", "defined",
202 "use", "package", "require", "ref", "can", "isa",
203 "qw", "qq", "eq", "ne", "or", "exists",
204 "and", "not", "import", "our", "caller" ]),
208 '/[:\?\+\*\/\!\$@\%&\|~\.]+/',
212 /* from http://ad.hominem.org/log/2005/05/quoted_strings.php */
213 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/", /* double-quoted strings */
214 "/'([^'\\\\\n]*(\\\\.[^'\\\\\n]*)*)'/", /* single-quoted strings */
215 "/\b-?[0-9]+\b/", /* numbers */
216 "/\b0x[0-9a-f]+\b/i", /* hex numbers */
217 "/\b[0-9[:upper:]_]+\b/", /* all-caps words */
218 [ "/q\(/", "/\)/" ], /* quote */
219 [ "/qw\(/", "/\)/" ], /* quote word */
220 '/\w+\s*=>/', /* barewords as hash keys 1 */
221 '/\{\s*-?\w+\s*\}/', /* barewords as hash keys 2 */
222 [ "/`/", "/`/" ], /* backticks */
223 [ "/<<[\"']?EOF.*$/m", "/^EOF$/m" ] /* 'document here' */
225 /* color all => as word2 */
227 /* color curly brackets as word */
228 'word', [ '/[{}]/' ],
230 "/#.*$/m" /* Comments */
233 "/__END__\n.*$/", /* __END__ */
234 '/^## .*$/m', /* section mark */
235 [ "/^=(head[1-4]|over|item|back|pod|begin|end|for)/m",
236 "/^=cut$/m" ] /* POD */
239 'detect' => sub (d) {
240 /* take the first line */
241 local f = d.txt.lines[0];
243 /* is it a 'she-bang' for Perl? */
244 return regex('/^#!\s*/usr/bin/(env )?perl/', f);
246 'section' => [ '/(^sub \w+|^package|^## )/' ]
253 'filenames' => [ '/\.mpsl?$/i' ],
257 'if', 'else', 'while', 'foreach',
258 'sub', 'break', 'return', 'eq', 'ne' ]),
259 mp.syn_token_list( keys(MPSL.CORE) ),
264 '/[-=<>:\?\+\*\/\!\%&\|]+/'
267 /* from http://ad.hominem.org/log/2005/05/quoted_strings.php */
268 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/", /* double-quoted strings */
269 "/'([^'\\\\\n]*(\\\\.[^'\\\\\n]*)*)'/", /* single-quoted strings */
270 "/\b-?[0-9]+\b/", /* numbers */
271 "/\b0x[0-9a-f]+\b/i", /* hex numbers */
272 "/\b[0-9[:upper:]_]+\b/" /* all-caps words */
275 [ '|/\*|', '|\*/|' ] /* C-like */
278 [ "|/\*\*\n|", '|\*/|' ], /* mp_doccer */
279 '/^/\*\*.*\*\*/$/m' /* section mark */
282 'section' => [ '/(^sub \w+|^\/\*\*.*\*\*\/$)/' ]
287 'name' => 'Shell script',
288 'filenames' => [ '/\.sh$/i', '/makefile/i' ],
292 "if", "then", "else", "elif",
293 "fi", "case", "do", "done", "esac",
294 "for", "until", "while", "break",
295 "in", "source", "alias", "cd",
296 "continue", "echo", "eval", "exec",
297 "exit", "export", "kill", "logout",
298 "printf", "pwd", "read", "return",
299 "shift", "test", "trap", "ulimit",
300 "umask", "unset", "wait", "cp", "rm" ]),
305 '/\b(local|let|set)\b/',
306 '/[-=<>:\?\+\*\!\%&\|]+/',
309 "/\b[0-9[:upper:]_]+\b/" /* all-caps words */
312 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/", /* double-quoted strings */
313 "/'([^'\\\\\n]*(\\\\.[^'\\\\\n]*)*)'/", /* single-quoted strings */
314 "/\b-?[0-9]+\b/", /* numbers */
315 "/\b0x[0-9a-f]+\b/i", /* hex numbers */
316 "/\([A-Za-z0-9_]+\)/", /* parens */
317 [ "/`/", "/`/" ], /* backticks */
318 [ "/<<[\"']?EOF[\"']?;$/m", "/^EOF$/m" ] /* 'document here' */
320 'comments', [ "/#.*$/m" ]
322 'detect' => sub (d) {
323 /* take the first line */
324 local f = d.txt.lines[0];
326 /* is it a 'she-bang' for usual shells? */
327 return regex('/^#!\s*/bin/(sh|bash|csh|dash|ksh)/', f) ||
328 regex('/^#!\s*/usr/bin/make -f/', f);
330 'section' => [ '/(^\w+\(\))/', '/^[A-Za-z0-9_\.-]+:/' ]
337 'filenames' => [ '/\.html$/i', '/\.htm$/i' ],
342 "a", "abbr", "acronym", "address",
343 "area", "b", "base", "bdo", "big",
344 "blockquote", "body", "br", "button",
345 "caption", "center", "cite", "code", "col",
346 "colgroup", "dd", "del", "dfn", "div",
347 "dl", "dt", "em", "fieldset", "form",
348 "h1", "h2", "h3", "h4", "h5", "h6",
349 "head", "hr", "html", "i", "img",
350 "input", "ins", "kbd", "label", "legend",
351 "li", "link", "map", "meta", "noscript",
352 "object", "ol", "optgroup", "option",
353 "p", "param", "pre", "q", "samp",
354 "script", "select", "small", "span",
355 "strong", "style", "sub", "sup", "table",
356 "tbody", "td", "textarea", "tfoot", "th",
357 "thead", "title", "tr", "tt", "ul",
363 "!DOCTYPE", "class", "type",
364 "cellspacing", "cellpadding",
365 "href", "align", "valign", "name", "lang",
366 "value", "action", "width", "height",
367 "content", "http-equiv", "src", "alt",
368 "bgcolor", "text", "link", "vlink", "alink",
372 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/", /* double-quoted strings */
373 "/'([^'\\\\\n]*(\\\\.[^'\\\\\n]*)*)'/" /* single-quoted strings */
376 [ '/<!--/', '/-->/' ]
379 'section' => [ '/<\s*h[0-9]\s*>/' ]
385 'name' => 'Config file',
386 'filenames' => [ '/\.conf$/i', '/\.cfg$/i', '/^.*\/?\.[0-9a-z_-]+rc$/i' ],
388 'word1', [ '/^\[.+\]$/m' ],
389 'word2', [ "/^[^:=\n]+[:=]/m" ],
391 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/", /* double-quoted strings */
392 "/'([^'\\\\\n]*(\\\\.[^'\\\\\n]*)*)'/", /* single-quoted strings */
393 "/\b-?[0-9]+\b/", /* numbers */
394 "/\b0x[0-9a-f]+\b/i" /* hex numbers */
396 'comments', [ "/#.*$/m" ]
404 'filenames' => [ '/\.php[345]?$/i', '/\.inc$/i' ],
408 "and", "array", "as",
409 "bool", "boolean", "break", "case", "class",
410 "const", "continue", "declare", "default",
411 "die", "do", "double", "echo", "else", "elseif",
412 "empty", "enddeclare", "endfor", "endforeach",
413 "endif", "endswitch", "endwhile", "eval",
414 "exit", "extends", "__FILE__", "float", "for",
415 "foreach", "function", "cfunction", "global",
416 "if", "include", "include_once", "int",
417 "integer", "isset", "__LINE__", "list", "new",
418 "object", "old_function", "or", "print", "real",
419 "require", "require_once", "return",
420 "static", "string", "switch", "unset", "use",
421 "var", "while", "xor", 'true', 'false' ]
424 'word2', [ '/\$\w+/', '/[-=<>:\?\+\*\!\%&\|]+/' ],
426 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/", /* double-quoted strings */
427 "/'([^'\\\\\n]*(\\\\.[^'\\\\\n]*)*)'/", /* single-quoted strings */
428 "/\b-?[0-9]+\b/", /* numbers */
429 "/\b0x[0-9a-f]+\b/i", /* hex numbers */
430 "/\b[0-9[:upper:]_]+\b/", /* all-caps words */
431 [ "/`/", "/`/" ] /* backticks */
434 [ '|/\*|', '|\*/|' ], /* C-like */
443 'filenames' => [ '/\.py$/i' ],
447 "and", "assert", "break", "class", "continue",
448 "def", "del", "elif", "else", "except", "exec",
449 "finally", "for", "from", "if", "import", "in",
450 "is", "lambda", "not", "or", "pass", "print",
451 "raise", "return", "try", "while", "yield"
455 'word2', [ '/global/', '/\$\w+/', '/[-=<>:\?\+\*\!\%&\|{}]+/' ],
457 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/", /* double-quoted strings */
458 "/'([^'\\\\\n]*(\\\\.[^'\\\\\n]*)*)'/", /* single-quoted strings */
459 "/\b-?[0-9]+\b/", /* numbers */
460 "/\b0x[0-9a-f]+\b/i", /* hex numbers */
461 "/\b[0-9[:upper:]_]+\b/", /* all-caps words */
462 [ "/`/", "/`/" ] /* backticks */
464 'comments', [ "/#.*$/m" ],
465 'documentation', [ [ '/""".+[^"]$/m', '/"""$/m' ] ]
467 'detect' => sub (d) {
468 /* take the first line */
469 local f = d.txt.lines[0];
471 /* is it a 'she-bang'? */
472 return regex('/^#!\s*/usr/bin/(env )?python/', f);
474 'section' => [ '/^[ \t]*def/' ]
480 'filenames' => [ '/\.rb$/i' ],
484 "BEGIN", "END", "alias", "and", "begin",
485 "break", "case", "class", "def", "defined",
486 "do", "else", "elsif", "end", "ensure",
487 "false", "for", "if", "in", "module", "next",
488 "nil", "not", "or", "redo", "rescue", "retry",
489 "return", "self", "super", "then", "true",
490 "undef", "unless", "until", "when", "while",
491 "yield", "require", "include" ]
494 'word2', [ '/[-=<>:\?\+\*\!\%&\|{}]+/', '/=(begin|end)/' ],
496 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/", /* double-quoted strings */
497 "/'([^'\\\\\n]*(\\\\.[^'\\\\\n]*)*)'/", /* single-quoted strings */
498 "/\b-?[0-9]+\b/", /* numbers */
499 "/\b0x[0-9a-f]+\b/i", /* hex numbers */
500 "/\b[0-9[:upper:]_]+\b/", /* all-caps words */
501 [ "/`/", "/`/" ] /* backticks */
503 'comments', [ "/#.*$/m" ]
505 'detect' => sub (d) {
506 /* take the first line */
507 local f = d.txt.lines[0];
509 /* is it a 'she-bang'? */
510 return regex('/^#!\s*/usr/bin/(env )?ruby/', f);
517 'filenames' => [ '/\.diff$/i', '/\.patch$/i' ],
519 'word1', [ '/^\+.+$/m' ],
520 'word2', [ '/^\-.+$/m' ],
521 'quotes', [ '/^@@.+@@$/m' ]
523 'section' => [ '/^--- ' ]
526 mp.syntax.commit_msg = {
527 'id' => 'commit_msg',
528 'name' => 'VCS commit message',
529 'filenames' => [ '/sv[kn]-commit.*\./' ],
531 'word1', [ '/^AM? .+$/m' ],
532 'word2', [ '/^D .+$/m' ],
533 'quotes', [ '/^M .+$/m' ],
534 'comments', [ '/^=== .+$/m', '/^--.+$/m' ]
540 'name' => 'Gettext file',
541 'filenames' => [ '/\.po$/i' ],
543 'word1', [ '/^msgid/m' ],
544 'word2', [ '/^msgstr/m' ],
545 'comments', [ "/#.*$/m" ],
547 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/" /* double-quoted strings */
554 'name' => 'XML / XGML',
555 'filenames' => [ '/\.xml$/i', '/\.sgml$/i' ],
557 'word1', [ '/<[^>]+>/' ],
558 'word2', [ '/<\?[^\?]+\?>/' ],
560 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/", /* double-quoted strings */
561 "/'([^'\\\\\n]*(\\\\.[^'\\\\\n]*)*)'/" /* single-quoted strings */
564 [ '/<!--/', '/-->/' ]
567 'detect' => sub (d) {
568 /* take the first line */
569 local f = d.txt.lines[0];
571 return regex('/<\?xml/', f);
575 mp.syntax.make_output = {
576 'id' => 'make_output',
577 'name' => 'Make output',
579 'word1', [ "/^.*warning:.*$/m" ],
580 'word2', [ "/^.*error:.*$/m" ]
584 mp.syntax.euphoria = {
586 'name' => 'euphoria',
587 'filenames' => [ '/\.e$/', '/\.eu$/', '/\.ew$/', '/\.ed$/',
588 '/\.ex$/', '/\.exw$/', '/\.exu$/' ],
592 "as", "and", "break", "by", "case", "constant", "continue", "do", "end",
593 "else", "elsif", "elsifdef",
594 "exit", "entry", "enum", "export", "for", "function", "global", "include",
595 "if", "ifdef", "label",
596 "not", "or", "procedure", "return", "retry", "switch", "then", "type",
597 "to", "while", "with", "without", "xor"
602 "atom", "integer", "sequence", "object"
606 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/", /* double-quoted strings */
607 "/'([^'\\\\\n]*(\\\\.[^'\\\\\n]*)*)'/", /* single-quoted strings */
608 "/\b-?#[0-9A-F]+\b/", /* hex numbers */
609 "/\b-?[0-9]+\b/", /* numbers */
610 "/\b[0-9[:upper:]_]+\b/" /* all-caps words */
612 'comments', [ "/--.*$/m" ]
614 'section' => [ '/^[ \t]*(global|export)*[ \t]*(function|procedure)/' ]
618 mp.syntax.mp_templates = {
619 'id' => 'mp_templates',
620 'name' => 'Minimum Profit template file',
621 'filenames' => [ '/\.mp_templates$/' ],
623 'documentation', [ '/^%%.*$/m' ]
625 'section' => [ '/^%%/' ]
629 mp.syntax.hex_view = {
631 'name' => 'Hexadecimal view',
633 'word1', [ "/^\| [0-9A-F]+ \|/m" ],
641 sub mp.detect_syntax(doc)
642 /* tries to detect the syntax of a document */
646 /* loops the syntax highlight definitions */
647 foreach (n, keys(mp.syntax)) {
648 local s = mp.syntax[n];
650 /* test the extensions */
651 foreach (ext, s.filenames) {
652 if (regex(ext, doc.name)) {
659 /* not by extension? try the 'detect' subroutine */
660 foreach (n, keys(mp.syntax)) {
661 local s = mp.syntax[n];
663 if (is_exec(s.detect) && s.detect(doc)) {
671 sub mp.help(doc, word)
675 foreach (c, doc.syntax.help) {
678 /* format the command */
679 c = sprintf(c, word);
682 if ((f = popen(c, "r")) != NULL) {
686 while ((l = read(f)) != NULL)
687 push(h, mp.chomp(l));
694 /* is there already help? don't look for more */
704 * mp.section_list - Returns the list of sections of a document.
707 * Applies the `section' array of regular expressions of the
708 * document's syntax definition and returns it as an array of
709 * line and line number pairs.
711 * If the document has no syntax highlight definition, or it has
712 * one without a `section' definition, NULL is returned. Otherwise,
713 * an array of line, line number pairs is returned (it can be
714 * an empty list if the document has no recognizable sections).
716 sub mp.section_list(doc)
720 if (doc.syntax.section) {
725 while ((l = doc.txt.lines[n]) != NULL) {
726 foreach (ex, doc.syntax.section) {