auto-indent



moin,

once upon a time, i wrote:
> proski wrote:
> > ossi wrote:
> > > the type of white-space used by auto-indent should not be based on the
> > > "fill tabs with spaces" setting. instead, the leading whitespace from
> > > the above line should be copied. otherwise it's impossible to use a
> > > tabbing style like
> > > <tab>if (...)
> > > <tab><tab>if (cond1 && function(par1,
> > > <tab><tab>                      par2))
> > > <tab><tab><tab>statement;
> [to say it in words: tab for indentation, space for padding]
> > > (which is _the_ tabbing style).
> > 
> > I have no idea what you mean.
> >
> preserving the already present indentation style as much as possible.
> take the above example and imagine i want to add par3 to the function
> call. when i press enter on the first closing paren, the new line should
> be identical to the par2 line up to the point where the cursor is placed
> (below the p of par2, obviouly).

and this time i have a patch.
i implemented two variants, therefore the ifdef.
the first variant takes the nearest line with any non-whitespace as a
template, like current mcedit does. but unlike the current code, it does
not truncate the indentation to the current column (this can happen if
the line we are coming from is whitespace only and shorter than the
template line). this is also the way the old borland editors behaved,
but it's a true nightmare if you have no trailing whitespace removal.
the second variant just copies the leading whitespace of the previous
line, even if it is whitespace-only or even empty. i think this is
perfectly reasonable.


the second patch is just an optimization - the cheaper condition should
be evaluated first.

fwiw, the editor (and prolly all of mc) is full of such constructs. it
also contains things like "if (char[p] && p_in_certain_range)" where p
could actually go out of bounds unless constrained from outside.  this
should be fixed, if only for the sake of clarity.

-- 
Hi! I'm a .signature virus! Copy me into your ~/.signature, please!
--
Chaos, panic, and disorder - my work here is done.
Index: edit.c
===================================================================
RCS file: /cvsroot/mc/mc/edit/edit.c,v
retrieving revision 1.97
diff -U2 -r1.97 edit.c
--- edit.c	7 Feb 2005 07:31:19 -0000	1.97
+++ edit.c	12 Feb 2005 14:11:10 -0000
@@ -1928,15 +1937,26 @@
 
 static void
-edit_auto_indent (WEdit * edit, int extra, int no_advance)
+edit_auto_indent (WEdit * edit)
 {
     long p;
-    int indent;
+    char c;
     p = edit->curs1;
-    while (isspace (edit_get_byte (edit, p - 1)) && p > 0)	/* move back/up to a line with text */
+#if 0
+    /* move back/up to a line with text */
+    while (p > 0 && isspace (edit_get_byte (edit, p - 1)))
 	p--;
-    indent = edit_indent_width (edit, edit_bol (edit, p));
-    if (edit->curs_col < indent && no_advance)
-	indent = edit->curs_col;
-    edit_insert_indent (edit, indent + (option_fake_half_tabs ? HALF_TAB_SIZE : TAB_SIZE) * space_width * extra);
+#else
+    /* use the previous line as a template */
+    if (!--p)
+	return;
+#endif
+    p = edit_bol (edit, p);
+    /* copy the leading whitespace of the line */
+    for (;;) {	/* no range check - the line _is_ \n-terminated */
+	c = edit_get_byte (edit, p++);
+	if (c != ' ' && c != '\t')
+	    break;
+	edit_insert (edit, c);
+    }
 }
 
@@ -2249,10 +2269,10 @@
 	    edit_double_newline (edit);
 	    if (option_return_does_auto_indent)
-		edit_auto_indent (edit, 0, 1);
+		edit_auto_indent (edit);
 	    format_paragraph (edit, 0);
 	} else {
 	    edit_insert (edit, '\n');
 	    if (option_return_does_auto_indent) {
-		edit_auto_indent (edit, 0, 1);
+		edit_auto_indent (edit);
 	    }
 	}
Index: edit.c
===================================================================
RCS file: /cvsroot/mc/mc/edit/edit.c,v
retrieving revision 1.97
diff -U2 -r1.97 edit.c
--- edit.c	7 Feb 2005 07:31:19 -0000	1.97
+++ edit.c	12 Feb 2005 14:11:10 -0000
@@ -1351,5 +1351,5 @@
     edit_cursor_move (edit, edit_move_forward3 (edit, p, edit->prev_col, 0) - edit->curs1);
 
-    if (is_in_indent (edit) && option_fake_half_tabs) {
+    if (option_fake_half_tabs && is_in_indent (edit)) {
 	edit_update_curs_col (edit);
 	if (space_width)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]