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.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$/', '/\.h$/', '/\.l$/', '/\.y$/', '/\.d$/',
99 '/\.cpp$/', '/\.hpp$/', '/\.c++$/', '/\.xpm$/' ],
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",
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 "/\b0x[0-9a-f]+\b/i", /* hex numbers */
145 "/\b[0-9[:upper:]_]+\b/" /* all-caps words */
148 [ '|/\*|', '|\*/|' ], /* C-like */
149 '|//.*$|m', /* C++ */
150 '/^\s*#[a-z]+/m' /* CPP directives */
153 [ "|/\*\*\n|", '|\*/|' ], /* mp_doccer */
154 '/^/\*\*.*\*\*/$/m' /* section mark */
157 'section' => [ '/(^\/\*\*.*\*\*\/$|^#pragma mark|^\w.*\)$)/' ]
162 'name' => 'Resource file',
163 'filenames' => [ '/\.rc$/' ],
168 /* from http://ad.hominem.org/log/2005/05/quoted_strings.php */
169 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/", /* double-quoted strings */
170 "/'([^'\\\\\n]*(\\\\.[^'\\\\\n]*)*)'/", /* single-quoted strings */
171 "/\b-?[0-9]+\b/", /* numbers */
172 "/\b0x[0-9a-f]+\b/i", /* hex numbers */
173 "/\b[0-9[:upper:]_]+\b/" /* all-caps words */
176 [ '|/\*|', '|\*/|' ], /* C-like */
177 '|//.*$|m', /* C++ */
178 '/^\s*#[a-z]+/m' /* CPP directives */
186 'filenames' => [ '/\.pl$/i', '/\.pm$/' ],
187 'help' => [ 'perldoc -f %s', 'perldoc %s' ],
188 'help_word_regex' => '/[A-Z_][A-Z0-9_:]*/i',
192 "for", "if", "next", "last", "else", "elsif",
193 "unless", "while", "shift", "unshift", "push",
194 "pop", "delete", "new", "bless", "return",
195 "foreach", "keys", "values", "sort", "grep",
196 "tr", "length", "system", "exec", "fork", "map",
197 "print", "write", "open", "close", "chop",
198 "chomp", "exit", "sleep", "split", "join",
199 "sub", "printf", "sprintf", "s", "glob",
200 "scalar", "my", "local", "undef", "defined",
201 "use", "package", "require", "ref", "can", "isa",
202 "qw", "qq", "eq", "ne", "or", "exists",
203 "and", "not", "import", "our", "caller" ]),
207 '/[:\?\+\*\/\!\$@\%&\|~\.]+/',
211 /* from http://ad.hominem.org/log/2005/05/quoted_strings.php */
212 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/", /* double-quoted strings */
213 "/'([^'\\\\\n]*(\\\\.[^'\\\\\n]*)*)'/", /* single-quoted strings */
214 "/\b-?[0-9]+\b/", /* numbers */
215 "/\b0x[0-9a-f]+\b/i", /* hex numbers */
216 "/\b[0-9[:upper:]_]+\b/", /* all-caps words */
217 [ "/q\(/", "/\)/" ], /* quote */
218 [ "/qw\(/", "/\)/" ], /* quote word */
219 '/\w+\s*=>/', /* barewords as hash keys 1 */
220 '/\{\s*-?\w+\s*\}/', /* barewords as hash keys 2 */
221 [ "/`/", "/`/" ], /* backticks */
222 [ "/<<[\"']?EOF.*$/m", "/^EOF$/m" ] /* 'document here' */
224 /* color all => as word2 */
226 /* color curly brackets as word */
227 'word', [ '/[{}]/' ],
229 "/#.*$/m" /* Comments */
232 "/__END__\n.*$/", /* __END__ */
233 '/^## .*$/m', /* section mark */
234 [ "/^=(head[1-4]|over|item|back|pod|begin|end|for)/m",
235 "/^=cut$/m" ] /* POD */
238 'detect' => sub (d) {
239 /* take the first line */
240 local f = d.txt.lines[0];
242 /* is it a 'she-bang' for Perl? */
243 return regex('/^#!\s*/usr/bin/(env )?perl/', f);
245 'section' => [ '/(^sub \w+|^package|^## )/' ]
252 'filenames' => [ '/\.mpsl$/' ],
256 'if', 'else', 'while', 'foreach',
257 'sub', 'break', 'return', 'eq', 'ne' ]),
258 mp.syn_token_list( keys(MPSL.CORE) ),
263 '/[-=<>:\?\+\*\/\!\%&\|]+/'
266 /* from http://ad.hominem.org/log/2005/05/quoted_strings.php */
267 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/", /* double-quoted strings */
268 "/'([^'\\\\\n]*(\\\\.[^'\\\\\n]*)*)'/", /* single-quoted strings */
269 "/\b-?[0-9]+\b/", /* numbers */
270 "/\b0x[0-9a-f]+\b/i", /* hex numbers */
271 "/\b[0-9[:upper:]_]+\b/" /* all-caps words */
274 [ '|/\*|', '|\*/|' ] /* C-like */
277 [ "|/\*\*\n|", '|\*/|' ], /* mp_doccer */
278 '/^/\*\*.*\*\*/$/m' /* section mark */
281 'section' => [ '/(^sub \w+|^\/\*\*.*\*\*\/$)/' ]
286 'name' => 'Shell script',
287 'filenames' => [ '/\.sh$/', '/makefile/i' ],
291 "if", "then", "else", "elif",
292 "fi", "case", "do", "done", "esac",
293 "for", "until", "while", "break",
294 "in", "source", "alias", "cd",
295 "continue", "echo", "eval", "exec",
296 "exit", "export", "kill", "logout",
297 "printf", "pwd", "read", "return",
298 "shift", "test", "trap", "ulimit",
299 "umask", "unset", "wait", "cp", "rm" ]),
304 '/\b(local|let|set)\b/',
305 '/[-=<>:\?\+\*\!\%&\|]+/',
308 "/\b[0-9[:upper:]_]+\b/" /* all-caps words */
311 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/", /* double-quoted strings */
312 "/'([^'\\\\\n]*(\\\\.[^'\\\\\n]*)*)'/", /* single-quoted strings */
313 "/\b-?[0-9]+\b/", /* numbers */
314 "/\b0x[0-9a-f]+\b/i", /* hex numbers */
315 "/\([A-Za-z0-9_]+\)/", /* parens */
316 [ "/`/", "/`/" ], /* backticks */
317 [ "/<<[\"']?EOF[\"']?;$/m", "/^EOF$/m" ] /* 'document here' */
319 'comments', [ "/#.*$/m" ]
321 'detect' => sub (d) {
322 /* take the first line */
323 local f = d.txt.lines[0];
325 /* is it a 'she-bang' for usual shells? */
326 return regex('/^#!\s*/bin/(sh|bash|csh|dash|ksh)/', f) ||
327 regex('/^#!\s*/usr/bin/make -f/', f);
329 'section' => [ '/(^\w+\(\))/', '/^[A-Za-z0-9_\.-]+:/' ]
336 'filenames' => [ '/\.html$/', '/\.htm$/' ],
341 "a", "abbr", "acronym", "address",
342 "area", "b", "base", "bdo", "big",
343 "blockquote", "body", "br", "button",
344 "caption", "center", "cite", "code", "col",
345 "colgroup", "dd", "del", "dfn", "div",
346 "dl", "dt", "em", "fieldset", "form",
347 "h1", "h2", "h3", "h4", "h5", "h6",
348 "head", "hr", "html", "i", "img",
349 "input", "ins", "kbd", "label", "legend",
350 "li", "link", "map", "meta", "noscript",
351 "object", "ol", "optgroup", "option",
352 "p", "param", "pre", "q", "samp",
353 "script", "select", "small", "span",
354 "strong", "style", "sub", "sup", "table",
355 "tbody", "td", "textarea", "tfoot", "th",
356 "thead", "title", "tr", "tt", "ul",
362 "!DOCTYPE", "class", "type",
363 "cellspacing", "cellpadding",
364 "href", "align", "valign", "name", "lang",
365 "value", "action", "width", "height",
366 "content", "http-equiv", "src", "alt",
367 "bgcolor", "text", "link", "vlink", "alink",
371 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/", /* double-quoted strings */
372 "/'([^'\\\\\n]*(\\\\.[^'\\\\\n]*)*)'/" /* single-quoted strings */
375 [ '/<!--/', '/-->/' ]
378 'section' => [ '/<\s*h[0-9]\s*>/' ]
384 'name' => 'Config file',
385 'filenames' => [ '/\.conf$/', '/\.cfg$/', '/^.*\/?\.[0-9a-z_-]+rc$/' ],
387 'word1', [ '/^\[.+\]$/m' ],
388 'word2', [ "/^[^:=\n]+[:=]/m" ],
390 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/", /* double-quoted strings */
391 "/'([^'\\\\\n]*(\\\\.[^'\\\\\n]*)*)'/", /* single-quoted strings */
392 "/\b-?[0-9]+\b/", /* numbers */
393 "/\b0x[0-9a-f]+\b/i" /* hex numbers */
395 'comments', [ "/#.*$/m" ]
403 'filenames' => [ '/\.php[345]?$/', '/\.inc$/' ],
407 "and", "array", "as",
408 "bool", "boolean", "break", "case", "class",
409 "const", "continue", "declare", "default",
410 "die", "do", "double", "echo", "else", "elseif",
411 "empty", "enddeclare", "endfor", "endforeach",
412 "endif", "endswitch", "endwhile", "eval",
413 "exit", "extends", "__FILE__", "float", "for",
414 "foreach", "function", "cfunction", "global",
415 "if", "include", "include_once", "int",
416 "integer", "isset", "__LINE__", "list", "new",
417 "object", "old_function", "or", "print", "real",
418 "require", "require_once", "return",
419 "static", "string", "switch", "unset", "use",
420 "var", "while", "xor", 'true', 'false' ]
423 'word2', [ '/\$\w+/', '/[-=<>:\?\+\*\!\%&\|]+/' ],
425 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/", /* double-quoted strings */
426 "/'([^'\\\\\n]*(\\\\.[^'\\\\\n]*)*)'/", /* single-quoted strings */
427 "/\b-?[0-9]+\b/", /* numbers */
428 "/\b0x[0-9a-f]+\b/i", /* hex numbers */
429 "/\b[0-9[:upper:]_]+\b/", /* all-caps words */
430 [ "/`/", "/`/" ] /* backticks */
433 [ '|/\*|', '|\*/|' ], /* C-like */
442 'filenames' => [ '/\.py$/' ],
446 "and", "assert", "break", "class", "continue",
447 "def", "del", "elif", "else", "except", "exec",
448 "finally", "for", "from", "if", "import", "in",
449 "is", "lambda", "not", "or", "pass", "print",
450 "raise", "return", "try", "while", "yield"
454 'word2', [ '/global/', '/\$\w+/', '/[-=<>:\?\+\*\!\%&\|{}]+/' ],
456 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/", /* double-quoted strings */
457 "/'([^'\\\\\n]*(\\\\.[^'\\\\\n]*)*)'/", /* single-quoted strings */
458 "/\b-?[0-9]+\b/", /* numbers */
459 "/\b0x[0-9a-f]+\b/i", /* hex numbers */
460 "/\b[0-9[:upper:]_]+\b/", /* all-caps words */
461 [ "/`/", "/`/" ] /* backticks */
463 'comments', [ "/#.*$/m" ],
464 'documentation', [ [ '/""".+[^"]$/m', '/"""$/m' ] ]
466 'detect' => sub (d) {
467 /* take the first line */
468 local f = d.txt.lines[0];
470 /* is it a 'she-bang'? */
471 return regex('/^#!\s*/usr/bin/(env )?python/', f);
473 'section' => [ '/^[ \t]*def/' ]
479 'filenames' => [ '/\.rb$/' ],
483 "BEGIN", "END", "alias", "and", "begin",
484 "break", "case", "class", "def", "defined",
485 "do", "else", "elsif", "end", "ensure",
486 "false", "for", "if", "in", "module", "next",
487 "nil", "not", "or", "redo", "rescue", "retry",
488 "return", "self", "super", "then", "true",
489 "undef", "unless", "until", "when", "while",
490 "yield", "require", "include" ]
493 'word2', [ '/[-=<>:\?\+\*\!\%&\|{}]+/', '/=(begin|end)/' ],
495 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/", /* double-quoted strings */
496 "/'([^'\\\\\n]*(\\\\.[^'\\\\\n]*)*)'/", /* single-quoted strings */
497 "/\b-?[0-9]+\b/", /* numbers */
498 "/\b0x[0-9a-f]+\b/i", /* hex numbers */
499 "/\b[0-9[:upper:]_]+\b/", /* all-caps words */
500 [ "/`/", "/`/" ] /* backticks */
502 'comments', [ "/#.*$/m" ]
504 'detect' => sub (d) {
505 /* take the first line */
506 local f = d.txt.lines[0];
508 /* is it a 'she-bang'? */
509 return regex('/^#!\s*/usr/bin/(env )?ruby/', f);
516 'filenames' => [ '/\.diff$/', '/\.patch$/' ],
518 'word1', [ '/^\+.+$/m' ],
519 'word2', [ '/^\-.+$/m' ],
520 'quotes', [ '/^@@.+@@$/m' ]
522 'section' => [ '/^--- ' ]
525 mp.syntax.commit_msg = {
526 'id' => 'commit_msg',
527 'name' => 'VCS commit message',
528 'filenames' => [ '/sv[kn]-commit.*\./' ],
530 'word1', [ '/^AM? .+$/m' ],
531 'word2', [ '/^D .+$/m' ],
532 'quotes', [ '/^M .+$/m' ],
533 'comments', [ '/^=== .+$/m', '/^--.+$/m' ]
539 'name' => 'Gettext file',
540 'filenames' => [ '/\.po$/' ],
542 'word1', [ '/^msgid/m' ],
543 'word2', [ '/^msgstr/m' ],
544 'comments', [ "/#.*$/m" ],
546 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/" /* double-quoted strings */
553 'name' => 'XML / XGML',
554 'filenames' => [ '/\.xml$/', '/\.sgml$/' ],
556 'word1', [ '/<[^>]+>/' ],
557 'word2', [ '/<\?[^\?]+\?>/' ],
559 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/", /* double-quoted strings */
560 "/'([^'\\\\\n]*(\\\\.[^'\\\\\n]*)*)'/" /* single-quoted strings */
563 [ '/<!--/', '/-->/' ]
566 'detect' => sub (d) {
567 /* take the first line */
568 local f = d.txt.lines[0];
570 return regex('/<\?xml/', f);
574 mp.syntax.make_output = {
575 'id' => 'make_output',
576 'name' => 'Make output',
578 'word1', [ "/^.*warning:.*$/m" ],
579 'word2', [ "/^.*error:.*$/m" ]
583 mp.syntax.euphoria = {
585 'name' => 'euphoria',
586 'filenames' => [ '/\.e$/', '/\.eu$/', '/\.ew$/', '/\.ed$/',
587 '/\.ex$/', '/\.exw$/', '/\.exu$/' ],
591 "as", "and", "break", "by", "case", "constant", "continue", "do", "end",
592 "else", "elsif", "elsifdef",
593 "exit", "entry", "enum", "export", "for", "function", "global", "include",
594 "if", "ifdef", "label",
595 "not", "or", "procedure", "return", "retry", "switch", "then", "type",
596 "to", "while", "with", "without", "xor"
601 "atom", "integer", "sequence", "object"
605 "/\"([^\"\\\\\n]*(\\\\.[^\"\\\\\n]*)*)\"/", /* double-quoted strings */
606 "/'([^'\\\\\n]*(\\\\.[^'\\\\\n]*)*)'/", /* single-quoted strings */
607 "/\b-?#[0-9A-F]+\b/", /* hex numbers */
608 "/\b-?[0-9]+\b/", /* numbers */
609 "/\b[0-9[:upper:]_]+\b/" /* all-caps words */
611 'comments', [ "/--.*$/m" ]
613 'section' => [ '/^[ \t]*(global|export)*[ \t]*(function|procedure)/' ]
617 mp.syntax.mp_templates = {
618 'id' => 'mp_templates',
619 'name' => 'Minimum Profit template file',
620 'filenames' => [ '/\.mp_templates$/' ],
622 'documentation', [ '/^%%.*$/m' ]
624 'section' => [ '/^%%/' ]
630 sub mp.detect_syntax(doc)
631 /* tries to detect the syntax of a document */
635 /* loops the syntax highlight definitions */
636 foreach (n, keys(mp.syntax)) {
637 local s = mp.syntax[n];
639 /* test the extensions */
640 foreach (ext, s.filenames) {
641 if (regex(ext, doc.name)) {
648 /* not by extension? try the 'detect' subroutine */
649 foreach (n, keys(mp.syntax)) {
650 local s = mp.syntax[n];
652 if (is_exec(s.detect) && s.detect(doc)) {
660 sub mp.help(doc, word)
664 foreach (c, doc.syntax.help) {
667 /* format the command */
668 c = sprintf(c, word);
671 if ((f = popen(c, "r")) != NULL) {
675 while ((l = read(f)) != NULL)
676 push(h, mp.chomp(l));
683 /* is there already help? don't look for more */
693 * mp.section_list - Returns the list of sections of a document.
696 * Applies the `section' array of regular expressions of the
697 * document's syntax definition and returns it as an array of
698 * line and line number pairs.
700 * If the document has no syntax highlight definition, or it has
701 * one without a `section' definition, NULL is returned. Otherwise,
702 * an array of line, line number pairs is returned (it can be
703 * an empty list if the document has no recognizable sections).
705 sub mp.section_list(doc)
709 if (doc.syntax.section) {
714 while ((l = doc.txt.lines[n]) != NULL) {
715 foreach (ex, doc.syntax.section) {