static inline void put_paragraph (WEdit * edit, unsigned char *t, off_t p, long indent, off_t size) { off_t cursor; off_t i; int c = '\0'; cursor = edit->buffer.curs1; if (indent != 0) while (strchr ("\t ", edit_buffer_get_byte (&edit->buffer, p)) != NULL) p++; for (i = 0; i < size; i++, p++) { if (i != 0 && indent != 0) { if (t[i - 1] == '\n' && c == '\n') { while (strchr ("\t ", edit_buffer_get_byte (&edit->buffer, p)) != NULL) p++; } else if (t[i - 1] == '\n') { off_t curs; edit_cursor_move (edit, p - edit->buffer.curs1); curs = edit->buffer.curs1; edit_insert_indent (edit, indent); if (cursor >= curs) cursor += edit->buffer.curs1 - p; p = edit->buffer.curs1; } else if (c == '\n') { edit_cursor_move (edit, p - edit->buffer.curs1); while (strchr ("\t ", edit_buffer_get_byte (&edit->buffer, p)) != NULL) { edit_delete (edit, TRUE); if (cursor > edit->buffer.curs1) cursor--; } p = edit->buffer.curs1; } } c = edit_buffer_get_byte (&edit->buffer, p); if (c != t[i]) replace_at (edit, p, t[i]); } edit_cursor_move (edit, cursor - edit->buffer.curs1); /* restore cursor position */ }
static inline void replace_at (WEdit * edit, off_t q, int c) { edit_cursor_move (edit, q - edit->buffer.curs1); edit_delete (edit, TRUE); edit_insert_ahead (edit, c); }
/* replaces a block of text */ static void put_paragraph (WEdit * edit, unsigned char *t, long p, int indent, int size) { long cursor; int i, c = 0; cursor = edit->curs1; if (indent) while (strchr ("\t ", edit_get_byte (edit, p))) p++; for (i = 0; i < size; i++, p++) { if (i && indent) { if (t[i - 1] == '\n' && c == '\n') { while (strchr ("\t ", edit_get_byte (edit, p))) p++; } else if (t[i - 1] == '\n') { long curs; edit_cursor_move (edit, p - edit->curs1); curs = edit->curs1; edit_insert_indent (edit, indent); if (cursor >= curs) cursor += edit->curs1 - p; p = edit->curs1; } else if (c == '\n') { edit_cursor_move (edit, p - edit->curs1); while (strchr ("\t ", edit_get_byte (edit, p))) { edit_delete (edit, 1); if (cursor > edit->curs1) cursor--; } p = edit->curs1; } } c = edit_get_byte (edit, p); if (c != t[i]) replace_at (edit, p, t[i]); } edit_cursor_move (edit, cursor - edit->curs1); /* restore cursor position */ }
static void replace_at (WEdit * edit, long q, int c) { edit_cursor_move (edit, q - edit->curs1); edit_delete (edit, 1); edit_insert_ahead (edit, c); }
void format_paragraph (WEdit * edit, gboolean force) { off_t p, q; long lines; off_t size; GString *t; long indent; unsigned char *t2; gboolean utf8 = FALSE; if (option_word_wrap_line_length < 2) return; if (edit_line_is_blank (edit, edit->buffer.curs_line)) return; p = begin_paragraph (edit, force, &lines); q = end_paragraph (edit, force); indent = test_indent (edit, p, q); t = get_paragraph (&edit->buffer, p, q, indent != 0); size = t->len - 1; if (!force) { off_t i; char *stop_format_chars; if (option_stop_format_chars != NULL && strchr (option_stop_format_chars, t->str[0]) != NULL) { g_string_free (t, TRUE); return; } if (option_stop_format_chars == NULL || *option_stop_format_chars == '\0') stop_format_chars = g_strdup ("\t"); else stop_format_chars = g_strconcat (option_stop_format_chars, "\t", (char *) NULL); for (i = 0; i < size - 1; i++) if (t->str[i] == '\n' && strchr (stop_format_chars, t->str[i + 1]) != NULL) { g_free (stop_format_chars); g_string_free (t, TRUE); return; } g_free (stop_format_chars); } t2 = (unsigned char *) g_string_free (t, FALSE); #ifdef HAVE_CHARSET utf8 = edit->utf8; #endif /* scroll up to show 1st line of paragraph */ edit_move_up (edit, lines, TRUE); /* scroll left as much as possible to show the formatted paragraph */ edit_scroll_left (edit, -edit->start_col); format_this (t2, q - p, indent, utf8); put_paragraph (edit, t2, p, indent, size); g_free ((char *) t2); /* move to the end of paragraph */ q = end_paragraph (edit, force); edit_cursor_move (edit, q - edit->buffer.curs1); /* try move to the start of next paragraph */ if (edit->buffer.curs_line < edit->buffer.lines) { edit_execute_cmd (edit, CK_Home, -1); do { edit_execute_cmd (edit, CK_Down, -1); } while (edit->buffer.curs_line < edit->buffer.lines && edit_line_is_blank (edit, edit->buffer.curs_line)); } }