coldcmode.sl.gz
snippets · 4.6 KB · 2010-03-30
ColdC editing mode for the jed editor (coldcmode.sl).
Download « Back to the snippets list
% coldcmode for jed (a smaller emacs-like editor)
%
% Author: Neale Pickett <zephyr@roguetrader.com>
% Comments: Nothing special, just ripped it from cmode
%
%% This program is free software; you can redistribute it and/or
%% modify it under the terms of the GNU General Public License as
%% published by the Free Software Foundation; either version 2 of the
%% License, or (at your option) any later version.
%%
%% This program is distributed in the hope that it will be useful,
%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
%% General Public License for more details.
%%
%% You should have received a copy of the GNU General Public License
%% along with this program; see the file COPYING. If not, write to
%% the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
%% Boston, MA 02111-1307, USA.
define is_slang_mode ()
{
variable is_slang;
(, is_slang) = what_mode ();
is_slang & 8;
}
define coldc_find_effective_eol ()
{
bol ();
while (ffind_char ('%'))
{
go_right_1 ();
if (parse_to_point () == -2)
{
return;
}
}
eol ();
}
define slmode_bskip_comment ()
{
forever
{
bskip_chars (" \t%");
push_mark ();
bol ();
pop_mark (not(looking_at_char ('#')));
!if (bolp ()) return;
!if (left (1)) return;
coldc_find_effective_eol ();
}
}
% This function also skips preprocessor lines
define coldc_bskip_over_comment ()
{
variable num_up;
if (is_slang_mode ()) return slmode_bskip_comment ();
forever
{
bskip_chars (" \t\n");
push_mark ();
num_up = 0;
while (up(1))
{
eol ();
!if (blooking_at ("\\"))
{
go_down (1);
break;
}
num_up++;
}
if (num_up)
{
bol_skip_white ();
if (looking_at_char ('#'))
{
pop_mark_0 ();
continue;
}
}
pop_mark_1 ();
!if (blooking_at ("*/"))
{
push_mark ();
#iftrue
if (bfind ("//"))
{
if (0 == parse_to_point ())
{
% Not in a comment or string
pop_mark_0 ();
continue;
}
}
#endif
bol ();
!if (bobp ())
#iftrue
if (looking_at_char ('#'))
#else
if (orelse
{looking_at_char ('#')}
% This is a hack for C++ code. I am not sure how good it
% is...
{skip_white (); looking_at ("//")})
#endif
{
pop_mark_0 ();
continue;
}
pop_mark_1 ();
break;
}
!if (bsearch ("/*")) break;
}
}
define coldc_looking_at (token)
{
variable cse = CASE_SEARCH, ret = 0;
CASE_SEARCH = 0;
if (looking_at(token))
{
push_spot ();
go_right(strlen(token));
POINT;
skip_chars ("\t :({");
ret = (POINT - ()) or eolp();
pop_spot ();
}
CASE_SEARCH = cse;
ret;
}
define coldc_indent_to (n)
{
bol_skip_white ();
if (what_column != n)
{
bol_trim ();
n--;
whitespace (n);
}
}
define coldc_indent_preprocess_line ()
{
variable col;
push_spot_bol ();
trim ();
!if (up_1 ())
{
pop_spot ();
return;
}
!if (bol_bsearch_char ('#'))
{
pop_spot ();
return;
}
go_right_1 ();
skip_white ();
col = what_column ();
if (looking_at ("if"))
col += C_Preprocess_Indent;
else if (looking_at ("el"))
col += C_Preprocess_Indent;
pop_spot ();
go_right_1 ();
skip_white ();
if (looking_at_char ('e'))
col -= C_Preprocess_Indent;
if (what_column () == col)
return;
bskip_white ();
trim ();
whitespace (col - 2);
}
define coldc_indent_line ()
{
variable val, col, extra_indent = 0;
variable match_char, match_indent, this_char;
variable match_mark;
push_spot ();
bol_skip_white ();
this_char = what_char ();
EXIT_BLOCK
{
bol_trim ();
pop_spot ();
}
if (looking_at_char ('#'))
{
coldc_indent_preprocess_line ();
return;
}
push_spot ();
if (up_1 ())
{
eol ();
if (blooking_at ("\\") and not (blooking_at ("\\\\")))
{
pop_spot ();
return;
}
}
pop_spot ();
EXIT_BLOCK
{
push_mark ();
bskip_white ();
1;
if (bolp ())
{
pop ();
skip_white ();
0;
}
pop_mark (()); % take argument from stack
}
if (orelse
{ coldc_looking_at("case"); }
{ coldc_looking_at("default"); }
{ coldc_looking_at("protected");}
{ coldc_looking_at("private");}
{ coldc_looking_at("public");}
)
{
if (ffind_char (':'))
{
extra_indent -= ColdC_INDENT;
extra_indent += ColdC_Colon_Offset;
}
bol ();
}
else
{
forever
{
coldc_bskip_over_comment ();
!if (orelse
{ blooking_at (";") }
{ blooking_at (",") }
{ blooking_at ("{") }
{ blooking_at ("}") }
{ blooking_at (":") })
{
extra_indent += ColdC_CONTINUED_OFFSET;
}
!if (blooking_at (")")) break;
push_mark ();
go_left_1 ();
if (1 != find_matching_delimiter (')'))
{
pop_mark_1 ();
break;
}
coldc_bskip_over_comment ();
push_spot ();
if ((1 == find_matching_delimiter (')')), pop_spot ())
{
pop_mark_1 ();
break;
}
pop_mark_0 ();
bol ();
}
}
val = find_matching_delimiter (')');
col = what_column ();
if ((val < 0) and looking_at ("/*")) val = -2;
match_mark = create_user_mark ();
match_char = what_char;
bol_skip_white ();
match_indent = what_column ();
pop_spot ();
switch (val)
{
case 0: % mismatch
if (match_char == '{')
{
push_spot ();
goto_user_mark (match_mark);
bskip_white ();
if (blooking_at (")"))
{
go_left_1 ();
if (1 == find_matching_delimiter (')'))
{
bol_skip_white ();
match_indent = what_column ();
}
}
pop_spot ();
col = match_indent;
col += ColdC_INDENT;
}
else
{
push_spot_bol ();
trim ();
if (looking_at_char ('*')) insert_single_space ();
pop_spot ();
return;
}
}
{
case 1:
extra_indent = 1; % match found
}
{
case -2: % inside comment
if (is_slang_mode ()) return;
push_spot ();
if (this_char != '\\') col++;
coldc_indent_to (col); % col++ because we are looking at /*
if ((looking_at_char ('*'))
or not (eolp ())) pop_spot ();
else
{
insert ("* ");
pop_spot ();
if (what_column () <= col)
{
goto_column (col + 2);
}
}
return;
}
{
case 2:
push_spot_bol ();
trim ();
pop_spot ();
return;
}
switch (this_char)
{
case '}':
col -= ColdC_INDENT;
}
{
case '{':
col += ColdC_BRACE;
}
{
pop (); col += extra_indent;
}
push_spot ();
coldc_indent_to (col);
pop_spot ();
}
% This function returns zero if the line does not begin with "* @ "
% or returns the indent column if it does. This combination of characters
% will most certainly appear in a comment. See the c file jed/src/intrin.c
% for examples of how it is exploited.
define coldc_is_comment_example ()
{
push_spot ();
bol_skip_white ();
0;
if (looking_at ("* @ "))
{
pop ();
what_column ();
}
pop_spot ();
}
define coldc_newline_and_indent ()
{
variable slcom = "%!% ";
if (is_slang_mode ())
{
push_spot_bol ();
if (looking_at (slcom), pop_spot ())
{
newline ();
insert (slcom);
return;
}
}
variable col = coldc_is_comment_example ();
newline ();
if (col)
{
coldc_indent_to (col);
insert ("* @ ");
}
else coldc_indent_line ();
}
define coldc_parse_to_point ()
{
parse_to_point () or coldc_is_comment_example ();
}
define coldc_insert_bra ()
{
if (coldc_parse_to_point ())
insert_char ('{');
else
{
push_spot ();
coldc_bskip_over_comment ();
if (blooking_at (","), pop_spot ())
{
insert_char ('{');
}
else
{
push_spot ();
skip_white ();
if (eolp ())
{
bskip_white ();
if (not (bolp ()) and ColdC_BRA_NEWLINE, pop_spot ()) newline ();
push_spot ();
bskip_white ();
bolp (); % on stack
pop_spot ();
insert_char ('{');
if ( () ) coldc_indent_line (); % off stack
eol ();
if (ColdC_BRA_NEWLINE) coldc_newline_and_indent ();
}
else
{
pop_spot ();
insert_char ('{');
}
}
}
}
define coldc_insert_ket ()
{
variable status = coldc_parse_to_point ();
push_spot ();
skip_white ();
push_spot ();
if (status
or not (eolp ())
or (bol_skip_white (), looking_at_char ('{')), pop_spot ())
{
pop_spot ();
insert_char ('}');
blink_match ();
return;
}
bskip_white ();
if (bolp (), pop_spot ())
{
insert_char ('}');
trim ();
}
else
{
eol ();
insert ("\n}");
}
coldc_indent_line ();
eol ();
blink_match ();
if (ColdC_BRA_NEWLINE) coldc_newline_and_indent ();
}
define coldc_insert_colon ()
{
insert_char (':');
!if (coldc_parse_to_point ())
coldc_indent_line ();
}
$1 = "ColdC";
!if (keymap_p ($1)) make_keymap ($1);
%definekey ("indent_line", "\t", $1);
%definekey ("coldc_newline_and_indent", "\r", $1);
definekey ("coldc_insert_bra", "{", $1);
definekey ("coldc_insert_ket", "}", $1);
definekey ("coldc_insert_colon", ":", $1);
definekey ("coldc_make_comment", "\e;", $1);
definekey ("coldc_comment_region", "^X;", $1);
definekey ("coldc_format_paragraph", "\eq", $1);
definekey ("coldc_top_of_function", "\e^A", $1);
definekey ("coldc_end_of_function", "\e^E", $1);
definekey ("coldc_mark_function", "\e^H", $1);
% Now create and initialize the syntax tables.
create_syntax_table ("ColdC");
define_syntax ("/*", "*/", '%', "ColdC");
define_syntax ("([{", ")]}", '(', "ColdC");
define_syntax ('"', '"', "ColdC");
%define_syntax ('\'', '\'', "ColdC");
%define_syntax ('\\', '\\', "ColdC");
define_syntax ("0-9a-zA-Z_", 'w', "ColdC"); % words
define_syntax ("-+0-9a-fA-F.xXL", '0', "ColdC"); % Numbers
define_syntax (",;.?:", ',', "ColdC");
define_syntax ('#', '#', "ColdC");
define_syntax ("%-+/&*=<>|!~^", '+', "ColdC");
set_syntax_flags ("ColdC", 4);
#ifdef HAS_DFA_SYNTAX
enable_highlight_cache("coldcmode.dfa", "ColdC");
define_highlight_rule("^[ \t]*#", "PQpreprocess", "ColdC");
define_highlight_rule("//.*", "comment", "ColdC");
define_highlight_rule("/\\*.*\\*/", "Qcomment", "ColdC");
define_highlight_rule("^([^/]|/[^\\*])*\\*/", "Qcomment", "ColdC");
define_highlight_rule("/\\*.*", "comment", "ColdC");
define_highlight_rule("^[ \t]*\\*+([ \t].*)?$", "comment", "ColdC");
define_highlight_rule("[A-Za-z_\\$][A-Za-z_0-9\\$]*", "Knormal", "ColdC");
define_highlight_rule("[0-9]+(\\.[0-9]*)?([Ee][\\+\\-]?[0-9]*)?",
"number", "ColdC");
define_highlight_rule("0[xX][0-9A-Fa-f]*[LlUu]*", "number", "ColdC");
define_highlight_rule("[0-9]+[LlUu]*", "number", "ColdC");
define_highlight_rule("\"([^\"\\\\]|\\\\.)*\"", "string", "ColdC");
define_highlight_rule("\"([^\"\\\\]|\\\\.)*\\\\?$", "string", "ColdC");
%define_highlight_rule("'([^'\\\\]|\\\\.)*'", "string", "ColdC");
%define_highlight_rule("'([^'\\\\]|\\\\.)*\\\\?$", "string", "ColdC");
define_highlight_rule("[ \t]+", "normal", "ColdC");
define_highlight_rule("[\\(\\[{}\\]\\),;\\.\\?:]", "delimiter", "ColdC");
define_highlight_rule("[%\\-\\+/&\\*=<>\\|!~\\^]", "operator", "ColdC");
build_highlight_table("ColdC");
#endif
% Type 0 keywords
() = define_keywords_n ("ColdC", "doif", 2, 0);
() = define_keywords_n ("ColdC", "argforvar", 3, 0);
() = define_keywords_n ("ColdC", "elsegoto", 4, 0);
() = define_keywords_n ("ColdC", "breakcatchwhile", 5, 0);
() = define_keywords_n ("ColdC", "returnswitch", 6, 0);
() = define_keywords_n ("ColdC", "default", 7, 0);
() = define_keywords_n ("ColdC", "continue", 8, 0);
% Type 1 keywords (built-in methods)
() = define_keywords_n("ColdC",
"absmaxminpad",
3, 1);
() = define_keywords_n("ColdC",
"datafeoffilejoinmathsizethisticktimetype",
4, 1);
() = define_keywords_n("ColdC",
strncat ("classcryptctimedblogerrorfilesfopenfread",
"fseekfstatmtimepausestacktasksthrowtoerr",
"tointtostrtosymunionvalid",
3),
5, 1);
() = define_keywords_n("ColdC",
strncat ("atomicbackupbuflencallercancelcreate",
"cwritedeletefchmodfclosefflushfmkdir",
"frmdirfwriteinsertlookupmethodobjnum",
"randomregexpresumesendersetaddstrcmp",
"strfmtstrlenstrsedstrsubsubbufsubstr",
5),
6, 1);
() = define_keywords_n("ColdC",
strncat ("add_varcoldmudcompilecwritefdefiner",
"del_vardestroyexecuteexplodefremove",
"frenameget_varlistlenmethodsobjname",
"parentsrefreshreplacerethrowset_var",
"sublistsuspendtask_idtofloat",
5),
7, 1);
() = define_keywords_n("ColdC",
strncat ("bufgraftchildrendict_adddict_delstrgraft",
"toobjnumshutdown",
2),
8, 1);
() = define_keywords_n("ColdC",
strncat ("ancestorsbind_portchparentsclear_var",
"coldmud_5dict_keyslistgraftlocaltime",
"lowercasesetremovetoliteraltraceback",
"uppercasevariables",
4),
9, 1);
() = define_keywords_n("ColdC",
strncat ("add_methodbuf_to_strconnectiondel_method",
"str_to_bufticks_left",
2),
10, 1);
() = define_keywords_n("ColdC",
strncat ("buf_replacelist_methoddel_objname",
"descendantsfind_methodmatch_begin",
"method_infoset_objnameunbind_port",
3),
11, 1);
() = define_keywords_n("ColdC",
"has_ancestormatch_regexpmethod_flags",
12, 1);
() = define_keywords_n("ColdC",
strncat ("bind_functiondict_containsmatch_pattern",
"method_accessrename_methodset_heartbeat",
2),
13, 1);
() = define_keywords_n("ColdC",
"buf_to_stringsmatch_templatestrings_to_buf",
14, 1);
() = define_keywords_n("ColdC",
"method_bytecodeopen_connectionunbind_function",
15, 1);
() = define_keywords_n("ColdC",
"close_connectionfind_next_methodset_method_flags",
16, 1);
() = define_keywords_n("ColdC",
"set_method_access",
17, 1);
() = define_keywords_n("ColdC",
"reassign_connection",
19, 1);
%!% Protoytype: Void cmode ();
%!% This is a mode that is dedicated to facilitate the editing of C language files.
%!% Functions that affect this mode include:
%!% @ function: default binding:
%!% @ coldc_insert_bra {
%!% @ coldc_insert_ket }
%!% @ newline_and_indent RETURN
%!% @ indent_line TAB
%!% @ goto_match Ctrl-\
%!% @ coldc_make_comment ESC ;
%!% @ coldc_format_paragraph ESC q
%!% @ coldc_top_of_function ESC Ctrl-A
%!% @ coldc_end_of_function ESC Ctrl-E
%!% @ coldc_mark_function ESC Ctrl-H
%!% Variables affecting indentation include:
%!% @ ColdC_INDENT
%!% @ ColdC_BRACE
%!% @ ColdC_BRA_NEWLINE
%!% @ ColdC_CONTINUED_OFFSET
%!% @ ColdC_Comment_Column (used by coldc_make_comment)
define coldc_mode ()
{
variable kmap = "ColdC";
set_mode(kmap, 2);
use_keymap(kmap);
use_syntax_table (kmap);
set_buffer_hook ("par_sep", "coldc_paragraph_sep");
set_buffer_hook ("indent_hook", "coldc_indent_line");
set_buffer_hook ("newline_indent_hook", "coldc_newline_and_indent");
runhooks("coldc_mode_hook");
}