aboutsummaryrefslogtreecommitdiffhomepage
path: root/fish_indent.c
diff options
context:
space:
mode:
authorGravatar axel <axel@liljencrantz.se>2007-04-23 04:54:29 +1000
committerGravatar axel <axel@liljencrantz.se>2007-04-23 04:54:29 +1000
commitcb179c448bc9f2b984b1740307b916ec8f3eb9e1 (patch)
treeab6a914182c6005d9bf71ec3adff3cc19aea17f5 /fish_indent.c
parentc323fc226f4d562b70b79c74f788b70648fdae41 (diff)
Minor tweaks to the indent program
darcs-hash:20070422185429-ac50b-f054a56d16ba55d2bbbd1d61e8bbfcb6c17a23a9.gz
Diffstat (limited to 'fish_indent.c')
-rw-r--r--fish_indent.c52
1 files changed, 45 insertions, 7 deletions
diff --git a/fish_indent.c b/fish_indent.c
index edf8cd42..eb9c2ca5 100644
--- a/fish_indent.c
+++ b/fish_indent.c
@@ -88,6 +88,8 @@ static int indent( string_buffer_t *out, wchar_t *in, int flags )
int is_command = 1;
int indent = 0;
int do_indent = 1;
+ int prev_type = 0;
+ int prev_prev_type = 0;
tok_init( &tok, in, TOK_SHOW_COMMENTS );
@@ -127,14 +129,14 @@ static int indent( string_buffer_t *out, wchar_t *in, int flags )
insert_tabs( out, indent );
}
- sb_printf( out, L"%ls ", last );
+ sb_printf( out, L"%ls", last );
indent = next_indent;
}
else
{
- sb_printf( out, L"%ls ", last );
+ sb_printf( out, L" %ls", last );
}
break;
@@ -142,7 +144,8 @@ static int indent( string_buffer_t *out, wchar_t *in, int flags )
case TOK_END:
{
- sb_append( out, L"\n" );
+ if( prev_type != TOK_END || prev_prev_type != TOK_END )
+ sb_append( out, L"\n" );
do_indent = 1;
is_command = 1;
break;
@@ -195,6 +198,11 @@ static int indent( string_buffer_t *out, wchar_t *in, int flags )
case TOK_COMMENT:
{
+ if( do_indent && flags)
+ {
+ insert_tabs( out, indent );
+ }
+
sb_printf( out, L"%ls", last );
do_indent = 1;
break;
@@ -206,7 +214,10 @@ static int indent( string_buffer_t *out, wchar_t *in, int flags )
exit(1);
}
}
-
+
+ prev_prev_type = prev_type;
+ prev_type = type;
+
}
tok_destroy( &tok );
@@ -214,6 +225,34 @@ static int indent( string_buffer_t *out, wchar_t *in, int flags )
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 )
{
@@ -308,15 +347,14 @@ int main( int argc, char **argv )
if( !indent( &sb_out, (wchar_t *)sb_in.buff, do_indent ) )
{
-
- fwprintf( stdout, L"%ls", sb_out.buff );
+ fwprintf( stdout, L"%ls", trim( (wchar_t *)sb_out.buff) );
}
else
{
/*
Indenting failed - print original input
*/
- fwprintf( stdout, L"%ls", sb_in.buff );
+ fwprintf( stdout, L"%ls", (wchar_t *)sb_in.buff );
}