(get_paragraph): refactoring.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2013-02-24 18:14:29 +04:00
parent c5d35ac93b
commit 253d27b1a3

View File

@ -163,28 +163,24 @@ end_paragraph (WEdit * edit, gboolean force)
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
static unsigned char * static GString *
get_paragraph (WEdit * edit, off_t p, off_t q, gboolean indent, off_t * size) get_paragraph (const edit_buffer_t * buf, off_t p, off_t q, gboolean indent)
{ {
unsigned char *s, *t; GString *t;
#if 0 t = g_string_sized_new (128);
t = g_try_malloc ((q - p) + 2 * (q - p) / option_word_wrap_line_length + 10);
#else for (; p < q; p++)
t = g_try_malloc (2 * (q - p) + 100);
#endif
if (t == NULL)
return NULL;
for (s = t; p < q; p++, s++)
{ {
if (indent && edit_buffer_get_byte (&edit->buffer, p - 1) == '\n') if (indent && edit_buffer_get_byte (buf, p - 1) == '\n')
while (strchr ("\t ", edit_buffer_get_byte (&edit->buffer, p)) != NULL) while (strchr ("\t ", edit_buffer_get_byte (buf, p)) != NULL)
p++; p++;
*s = edit_buffer_get_byte (&edit->buffer, p);
g_string_append_c (t, edit_buffer_get_byte (buf, p));
} }
*size = (off_t) (s - t);
/* FIXME: all variables related to 'size' should be fixed */ g_string_append_c (t, '\n');
t[*size] = '\n';
return t; return t;
} }
@ -451,8 +447,9 @@ format_paragraph (WEdit * edit, gboolean force)
{ {
off_t p, q; off_t p, q;
off_t size; off_t size;
unsigned char *t; GString *t;
long indent; long indent;
unsigned char *t2;
if (option_word_wrap_line_length < 2) if (option_word_wrap_line_length < 2)
return; return;
@ -463,31 +460,26 @@ format_paragraph (WEdit * edit, gboolean force)
q = end_paragraph (edit, force); q = end_paragraph (edit, force);
indent = test_indent (edit, p, q); indent = test_indent (edit, p, q);
t = get_paragraph (edit, p, q, indent != 0, &size); t = get_paragraph (&edit->buffer, p, q, indent != 0);
if (t == NULL) size = t->len - 1;
return;
if (!force) if (!force)
{ {
off_t i; off_t i;
if (strchr (NO_FORMAT_CHARS_START, *t) != NULL) if (strchr (NO_FORMAT_CHARS_START, t->str[0]) == NULL)
{ for (i = 0; i < size - 1; i++)
g_free (t); if (t->str[i] == '\n' && strchr (NO_FORMAT_CHARS_START "\t ", t->str[i + 1]) != NULL)
return; break;
}
for (i = 0; i < size - 1; i++) g_string_free (t, TRUE);
if (t[i] == '\n' && strchr (NO_FORMAT_CHARS_START "\t ", t[i + 1]) != NULL) return;
{
g_free (t);
return;
}
} }
format_this (t, q - p, indent); t2 = (unsigned char *) g_string_free (t, FALSE);
put_paragraph (edit, t, p, indent, size); format_this (t2, q - p, indent);
g_free (t); put_paragraph (edit, t2, p, indent, size);
g_free (t2);
/* Scroll left as much as possible to show the formatted paragraph */ /* Scroll left as much as possible to show the formatted paragraph */
edit_scroll_left (edit, -edit->start_col); edit_scroll_left (edit, -edit->start_col);