home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mega Top 1
/
os2_top1.zip
/
os2_top1
/
DEMO
/
RIM22
/
MACROS
/
C_INDENT.RM
< prev
next >
Wrap
Text File
|
1994-02-04
|
12KB
|
516 lines
/*
** indent.rm - indenting routines
*/
#include "macro.h"
/* IndentStyle is an internal variable within the editor
** set it with RSE_FLAGS or on the command line using
** the -Is flag 0 = none 1 = regular 2 = smart 3 = template
** Default: 1
*/
extern long IndentStyle;
/* IndentSize is an internal variable within the editor
** set it with RSE_FLAGS or on the command line using
** the -Iwnnn where 'nnn' is size of indent. Default: 3
*/
extern long IndentSize;
HKEYBOARD _c_smart, _c_template, _c_alt_template, _r_keymap;
#define DEBUGGING 1
#if DEBUGGING
char szDebug[256];
#endif
typedef struct pstate {
long beginning_of_defun;
long containing_cexp;
int incomment;
int instring;
int quoted;
int level;
} PSTATE;
typedef struct template_t TEMPLATE, *PTEMPLATE;
struct template_t {
char *abbrev; /* what is tested to see if a match exists */
char *expansion; /* what to expand to */
short min; /* need at least this many chars for this template */
unsigned flags; /* special handling flags */
};
extern int DistanceToNextIndent(long col); /* defined in slide.rm */
extern int DistanceToNextIndent(long col);
extern void slide_in(void);
extern void slide_out(void);
extern int calculate_c_indent(PSTATE *); /* defined in c_smart.rm */
extern void _c_newline();
extern void _c_indent();
extern void _c_open(void);
extern void _c_close(void);
void _expand(PTEMPLATE template);
void _c_expand(PTEMPLATE template);
void _c_matching_brace(void);
void just_brace(void);
void just_cbrace(void);
void just_space(void);
void
_c_next_word(void) {
long len;
len = SrchFwd("([a-zA-Z0-9_]+)|([^ \t\f\na-zA-Z0-9_]+)|([ \t\f\n]+)[ \t\f\n]*", -1, 1, 0);
if ( len > 0 )
PosNextChar(len);
} /* end _c_next_word() */
void
_h_next_word(void) {
_c_next_word();
}
void
_cpp_next_word(void) {
_c_next_word();
}
void
_cxx_next_word(void) {
_c_next_word();
}
void
_hpp_next_word(void) {
_c_next_word();
}
void
_hxx_next_word(void) {
_c_next_word();
}
void
_inl_next_word(void) {
_c_next_word();
}
void
_rm_next_word(void) {
_c_next_word();
}
void
_c_prev_word(void) {
__prev_word();
} /* end _c_next_word() */
void
_h_prev_word(void) {
_c_prev_word();
}
void
_cpp_prev_word(void) {
_c_prev_word();
}
void
_cxx_prev_word(void) {
_c_prev_word();
}
void
_hpp_prev_word(void) {
_c_prev_word();
}
void
_hxx_prev_word(void) {
_c_prev_word();
}
void
_inl_prev_word(void) {
_c_prev_word();
}
void
_rm_prev_word(void) {
_c_prev_word();
}
void
BufQueryPos(long *l, long *c) {
if ( l )
*l = BufQueryLine();
if ( c )
*c = BufQueryColumn();
} /* end BufQueryPos() */
int
AtEOF(void) {
return BufQueryOffset() == BufQueryFilesize();
} /* end AtEOF() */
/*
** Template editing
**
** For a keyword to match it must only have whitespace ahead of it and
** you must be at the end of the line.
** If a match is found, the remainder of the statement
** is filled in and indented automatically.
**
*/
#if 0
typedef struct template_t TEMPLATE, *PTEMPLATE;
struct template_t {
char *abbrev; /* what is tested to see if a match exists */
char *expansion; /* what to expand to */
short min; /* need at least this many chars for this template */
unsigned flags; /* special handling flags */
}
#endif
/* if you want opening braces on same line as statement set to 1 else set to 0 */
#define BRACES_ON_SAME_LINE 1
#if BRACES_ON_SAME_LINE
TEMPLATE c_templates[] = {
{ "if" , "if ( % )" , 2, 0 },
{ "if{" , "if ( % ) {\n}" , 3, 0 },
{ "els" , "else" , 2, 0 },
{ "el{" , "else {\n% \n}" , 3, 0 },
{ "ei" , "else if ( % )" , 2, 0 },
{ "ei{" , "else if ( % ) {\n}" , 3, 0 },
{ "else if" , "else if ( % )" , 6, 0 },
{ "else if{" , "else if ( % ) {\n}" , 8, 0 },
{ "while" , "while ( % )" , 2, 0 },
{ "wh{" , "while ( % ) {\n}" , 3, 0 },
{ "for" , "for ( %; ; )" , 2, 0 },
{ "for{" , "for ( %; ; ) {\n}" , 4, 0 },
{ "switch" , "switch ( % ) {\n}" , 2, 0 },
{ "break" , "break;" , 2, 0 },
{ "case" , "case :~\b" , 2, 0 },
{ "default" , "default:" , 2, 0 },
{ "do" , "do {\n%;\n} while ( );" , 2, 0 },
{ "return" , "return %;" , 2, 0 },
{ "continue" , "continue;" , 2, 0 },
{ NULL , NULL , 0, 0 },
};
#else /* place opening brace on following line */
TEMPLATE c_templates[] = {
{ "if" , "if ( % )" , 2, 0 },
{ "if{" , "if ( % )\n{\n}" , 3, 0 },
{ "els" , "else" , 2, 0 },
{ "el{" , "else\n{\n% \n}" , 3, 0 },
{ "ei" , "else if ( % )" , 2, 0 },
{ "ei{" , "else if ( % )\n{\n}" , 3, 0 },
{ "else if" , "else if ( % )" , 6, 0 },
{ "else if{" , "else if ( % )\n{\n}" , 8, 0 },
{ "while" , "while ( % )" , 2, 0 },
{ "wh{" , "while ( % )\n{\n}" , 3, 0 },
{ "for" , "for ( %; ; )" , 2, 0 },
{ "for{" , "for ( %; ; )\n{\n}" , 4, 0 },
{ "switch" , "switch ( % )\n{\n}" , 2, 0 },
{ "break" , "break;" , 2, 0 },
{ "case" , "case :~\b" , 2, 0 },
{ "default" , "default:" , 2, 0 },
{ "do" , "do\n{\n%;\n} while ( );", 2, 0 },
{ "return" , "return %;" , 2, 0 },
{ "continue" , "continue;" , 2, 0 },
{ NULL , NULL , 0, 0 },
};
#endif
void
_expand(PTEMPLATE template) {
int expanded = 0;
long len;
int index;
int c;
char *psz, *pszTrim;
PTEMPLATE item = NULL;
HMARK hmCursor = 0L;
if ( !AtEOF() && BufQueryChar() != '\n' ) { /* Are we at the end of the line? */
if ( KbdQueryBufferKeyboard() == _c_alt_template ) /* expand bound to Tab key */
slide_in();
else
SelfInsert();
return;
}
MarkPushPos();
PosSOT();
psz = BufRead(); /* get remainder of line */
pszTrim = rtrim(psz);
len = strlen(pszTrim);
if ( !len ) {
free(psz);
MarkPopPos();
SelfInsert();
return;
}
/* try to match one of the templates */
for ( index = 0; template[index].abbrev; ++index ) {
if ( len >= template[index].min &&
!strncmp(template[index].abbrev, pszTrim, len) ) {
item = &template[index];
break;
}
}
free(psz);
if ( !item ) { /* no match */
MarkPopPos();
SelfInsert();
return;
}
/* dump saved position */
MarkPopPos(0);
/* have a template to expand */
BufDeleteToEOL();
psz = item->expansion;
while ( c = *psz++ ) {
switch ( c ) {
default:
BufInsertChar(c);
break;
case '\n':
_c_newline();
break;
case '{':
_c_open();
break;
case '}':
_c_close();
break;
case '\b': /* back-up a character */
PosPrevChar();
break;
case '~': /* do indentation now */
c_indenter();
break;
case '%': /* remember cursor position */
hmCursor = MarkAllocLocal();
break;
} /* end switch */
} /* end while there is more to the expansion */
c_indenter();
if ( hmCursor ) {
MarkGoto(hmCursor);
MarkFree(hmCursor);
}
} /* end _expand() */
void
_c_expand(void) {
_expand(c_templates);
}
void
_c_matching_brace(void) {
char *pszLine;
char *pszTmp;
MarkPushPos();
PosSOL();
pszLine = BufRead();
MarkPopPos();
_c_open();
/*
** Search current line for:
** Line start
** or else or do
** or typedef or struct followed by optional whitespace followed by optional non-whitespace
** or ')' or ':'
** followed by optional whitespace and end of line
*/
if ( SrchString("((else)|(do))|((typedef)|(struct)[ \t]*[^ \t\n]*)|[):]|^[ \t]*&", pszLine, NULL, -1) >= 0 ) {
pszTmp = BufRead();
if ( !*rtrim(ltrim(pszTmp)) ) { /* if no text following */
_c_newline();
_c_close();
}
free(pszTmp);
}
free(pszLine);
} /* end _c_matching_brace() */
void
just_brace (void) {
BufInsertChar('{');
}
void
just_cbrace (void) {
BufInsertChar('}');
}
void
just_space (void) {
BufInsertChar(' ');
}
/*
** _c_init
**
** Sets up the language specific features for 'C' and related language
** files.
*/
_c_init(void) {
/*
** Make the C keyboard bindings override the regular keyboard
** for this buffer.
*/
switch ( IndentStyle ) {
case 0: return;
case 1: KbdSetBufferKeyboard(_r_keymap); break;
case 2: KbdSetBufferKeyboard(_c_smart); break;
case 3: KbdSetBufferKeyboard(_c_template); break;
case 4: KbdSetBufferKeyboard(_c_alt_template); break;
}
} /* end _c_init() */
_h_init(void) {
_c_init();
}
_cpp_init(void) { /* C++ source files */
_c_init();
}
_cxx_init(void) { /* C++ source files */
_c_init();
}
_hpp_init(void) { /* C++ header files */
_c_init();
}
_hxx_init(void) { /* C++ header files */
_c_init();
}
_inl_init(void) { /* C++ inline files */
_c_init();
}
_rm_init(void) { /* RimStar macro files */
_c_init();
}
_asm_init(void) { /* assembler files */
BufSetTabs("9");
KbdSetBufferKeyboard(_r_keymap); /* enable regular indenting */
}
void
_init(void) {
LibAutoload("slide", "slide_in", "slide_out");
LibAutoload("c_smart", "_c_newline", "_c_newline_break", "_c_indent", "_c_close", "_c_open",
"_c_indent_function", "_c_start_of_func",
"_c_end_of_func", "_c_forward_cexp",
"_c_backward_cexp", "c_indenter");
/*
** Create keyboards for the keys we need to remap
** These keyboards are used as Buffer keyboards which
** over-ride and/or supplement the current keyboard
** when that buffer is current.
*/
/* Create regular indenting keyboard ( -Is1 ) */
KbdPush();
KbdBind("Enter", "BufNewlineIndent");
KbdBind("Tab", "slide_in");
KbdBind("Shift+Tab", "slide_out");
_r_keymap = KbdPop();
/* Create smart indenting keyboard ( -Is2 ) */
KbdPush();
KbdBind("Enter", "_c_newline_break");
KbdBind("Kp_Enter", "_c_newline");
KbdBind("Shift+Enter", "BufNewlineIndent");
KbdBind("Tab", "_c_indent");
KbdBind("Shift+Tab", "slide_out");
KbdBind("Shift + {", "_c_open");
KbdBind("Shift + }", "_c_close");
KbdBind("Ctrl+Alt+A", "_c_start_of_func");
KbdBind("Ctrl+Alt+E", "_c_end_of_func");
KbdBind("Ctrl+Alt+Q", "_c_indent_function");
KbdBind("Ctrl+Alt+F", "_c_forward_cexp");
KbdBind("Ctrl+Alt+B", "_c_backward_cexp");
KbdBind("Ctrl+{", "just_brace");
KbdBind("Ctrl+}", "just_cbrace");
_c_smart = KbdPop();
/* Create template editing keyboard ( -Is3 ) */
KbdPush();
KbdBind("Enter", "_c_newline_break");
KbdBind("Kp_Enter", "_c_newline");
KbdBind("Shift+Enter", "BufNewlineIndent");
KbdBind("Tab", "_c_indent");
KbdBind("Shift+Tab", "slide_out");
KbdBind("Space", "_c_expand");
KbdBind("Shift + {", "_c_matching_brace");
KbdBind("Shift + }", "_c_close");
KbdBind("Ctrl+Alt+A", "_c_start_of_func");
KbdBind("Ctrl+Alt+E", "_c_end_of_func");
KbdBind("Ctrl+Alt+Q", "_c_indent_function");
KbdBind("Ctrl+Alt+F", "_c_forward_cexp");
KbdBind("Ctrl+Alt+B", "_c_backward_cexp");
KbdBind("Ctrl+{", "just_brace");
KbdBind("Ctrl+}", "just_cbrace");
KbdBind("Ctrl+Space", "just_space");
_c_template = KbdPop();
/* Create alternate template editing keyboard ( -Is4 ) */
KbdPush();
KbdBind("Enter", "_c_newline_break");
KbdBind("Kp_Enter", "_c_newline");
KbdBind("Shift+Enter", "BufNewlineIndent");
KbdBind("Tab", "_c_expand");
KbdBind("Shift+Tab", "slide_out");
KbdBind("Shift + {", "_c_open");
KbdBind("Shift + }", "_c_close");
KbdBind("Ctrl+Alt+A", "_c_start_of_func");
KbdBind("Ctrl+Alt+E", "_c_end_of_func");
KbdBind("Ctrl+Alt+Q", "_c_indent_function");
KbdBind("Ctrl+Alt+F", "_c_forward_cexp");
KbdBind("Ctrl+Alt+B", "_c_backward_cexp");
KbdBind("Ctrl+{", "just_brace");
KbdBind("Ctrl+}", "just_cbrace");
KbdBind("Ctrl+Space", "just_space");
_c_alt_template = KbdPop();
} /* end _init() */
/*
** End module: c_indent.rm
*/