Minor tweaks to the indent program

darcs-hash:20070422185429-ac50b-f054a56d16ba55d2bbbd1d61e8bbfcb6c17a23a9.gz
This commit is contained in:
axel 2007-04-23 04:54:29 +10:00
parent c323fc226f
commit cb179c448b

View File

@ -88,6 +88,8 @@ static int indent( string_buffer_t *out, wchar_t *in, int flags )
int is_command = 1; int is_command = 1;
int indent = 0; int indent = 0;
int do_indent = 1; int do_indent = 1;
int prev_type = 0;
int prev_prev_type = 0;
tok_init( &tok, in, TOK_SHOW_COMMENTS ); tok_init( &tok, in, TOK_SHOW_COMMENTS );
@ -142,6 +144,7 @@ static int indent( string_buffer_t *out, wchar_t *in, int flags )
case TOK_END: case TOK_END:
{ {
if( prev_type != TOK_END || prev_prev_type != TOK_END )
sb_append( out, L"\n" ); sb_append( out, L"\n" );
do_indent = 1; do_indent = 1;
is_command = 1; is_command = 1;
@ -195,6 +198,11 @@ static int indent( string_buffer_t *out, wchar_t *in, int flags )
case TOK_COMMENT: case TOK_COMMENT:
{ {
if( do_indent && flags)
{
insert_tabs( out, indent );
}
sb_printf( out, L"%ls", last ); sb_printf( out, L"%ls", last );
do_indent = 1; do_indent = 1;
break; break;
@ -207,6 +215,9 @@ static int indent( string_buffer_t *out, wchar_t *in, int flags )
} }
} }
prev_prev_type = prev_type;
prev_type = type;
} }
tok_destroy( &tok ); tok_destroy( &tok );
@ -214,6 +225,34 @@ static int indent( string_buffer_t *out, wchar_t *in, int flags )
return res; return res;
} }
wchar_t *trim( wchar_t *in )
{
wchar_t *end;
while( *in == L'\n' )
{
in++;
}
end = in + wcslen(in);
while( 1 )
{
if( end < in+2 )
break;
end--;
if( (*end == L'\n' ) && ( *(end-1) == L'\n' ) )
*end=0;
else
break;
}
return in;
}
int main( int argc, char **argv ) int main( int argc, char **argv )
{ {
@ -308,15 +347,14 @@ int main( int argc, char **argv )
if( !indent( &sb_out, (wchar_t *)sb_in.buff, do_indent ) ) if( !indent( &sb_out, (wchar_t *)sb_in.buff, do_indent ) )
{ {
fwprintf( stdout, L"%ls", trim( (wchar_t *)sb_out.buff) );
fwprintf( stdout, L"%ls", sb_out.buff );
} }
else else
{ {
/* /*
Indenting failed - print original input Indenting failed - print original input
*/ */
fwprintf( stdout, L"%ls", sb_in.buff ); fwprintf( stdout, L"%ls", (wchar_t *)sb_in.buff );
} }