From 32ffd98e87f2d1379794049e7adf58ef87a3bde9 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Mon, 25 Feb 2013 16:27:16 +0400 Subject: [PATCH] Refactoring: move members from WEdit to edit_buffer_t and rename related functions: WEdit::last_byte -> edit_buffer_t::size WEdit::total_lines -> edit_buffer_t::lines WEdit::curs_line -> edit_buffer_t::curs_line edit_count_lines() -> edit_buffer_count_lines() Signed-off-by: Andrew Borodin --- src/editor/edit-impl.h | 1 - src/editor/edit.c | 192 ++++++++++++++++++---------------------- src/editor/editbuffer.c | 31 ++++++- src/editor/editbuffer.h | 9 +- src/editor/editcmd.c | 40 ++++----- src/editor/editdraw.c | 23 ++--- src/editor/editwidget.h | 3 - src/editor/syntax.c | 2 +- src/editor/wordproc.c | 14 +-- 9 files changed, 163 insertions(+), 152 deletions(-) diff --git a/src/editor/edit-impl.h b/src/editor/edit-impl.h index 8f00d2703..3a1dbd055 100644 --- a/src/editor/edit-impl.h +++ b/src/editor/edit-impl.h @@ -153,7 +153,6 @@ void edit_menu_cmd (WDialog * h); void user_menu (WEdit * edit, const char *menu_file, int selected_entry); void edit_init_menu (struct WMenuBar *menubar); void edit_save_mode_cmd (void); -long edit_count_lines (const WEdit * edit, off_t current, off_t upto); off_t edit_move_forward (const WEdit * edit, off_t current, long lines, off_t upto); off_t edit_move_forward3 (const WEdit * edit, off_t current, long cols, off_t upto); off_t edit_move_backward (const WEdit * edit, off_t current, long lines); diff --git a/src/editor/edit.c b/src/editor/edit.c index c84b05176..faff73f99 100644 --- a/src/editor/edit.c +++ b/src/editor/edit.c @@ -162,9 +162,9 @@ edit_load_file_fast (WEdit * edit, const vfs_path_t * filename_vpath) return FALSE; } - ret = (edit_buffer_read_file (&edit->buffer, file, edit->last_byte) == edit->last_byte); + ret = (edit_buffer_read_file (&edit->buffer, file, edit->buffer.size) == edit->buffer.size); if (ret) - edit->total_lines = edit_count_lines (edit, 0, edit->last_byte); + edit->buffer.lines = edit_buffer_count_lines (&edit->buffer, 0, edit->buffer.size); else { gchar *errmsg; @@ -358,11 +358,10 @@ edit_load_file (WEdit * edit) fast_load = FALSE; } - edit_buffer_init (&edit->buffer); - if (fast_load) { - edit->last_byte = edit->stat1.st_size; + edit_buffer_init (&edit->buffer, edit->stat1.st_size); + if (!edit_load_file_fast (edit, edit->filename_vpath)) { edit_clean (edit); @@ -371,7 +370,8 @@ edit_load_file (WEdit * edit) } else { - edit->last_byte = 0; + edit_buffer_init (&edit->buffer, 0); + if (edit->filename_vpath != NULL && *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) != '\0') { @@ -411,7 +411,7 @@ edit_load_position (WEdit * edit) else if (offset > 0) { edit_cursor_move (edit, offset); - line = edit->curs_line; + line = edit->buffer.curs_line; edit->search_start = edit->buffer.curs1; } @@ -432,7 +432,7 @@ edit_save_position (WEdit * edit) return; book_mark_serialize (edit, BOOK_MARK_COLOR); - save_file_position (edit->filename_vpath, edit->curs_line + 1, edit->curs_col, edit->buffer.curs1, + save_file_position (edit->filename_vpath, edit->buffer.curs_line + 1, edit->curs_col, edit->buffer.curs1, edit->serialized_bookmarks); edit->serialized_bookmarks = NULL; } @@ -614,13 +614,13 @@ edit_find_line (WEdit * edit, long line) memset (edit->line_numbers, 0, sizeof (edit->line_numbers)); memset (edit->line_offsets, 0, sizeof (edit->line_offsets)); /* three offsets that we *know* are line 0 at 0 and these two: */ - edit->line_numbers[1] = edit->curs_line; + edit->line_numbers[1] = edit->buffer.curs_line; edit->line_offsets[1] = edit_bol (edit, edit->buffer.curs1); - edit->line_numbers[2] = edit->total_lines; - edit->line_offsets[2] = edit_bol (edit, edit->last_byte); + edit->line_numbers[2] = edit->buffer.lines; + edit->line_offsets[2] = edit_bol (edit, edit->buffer.size); edit->caches_valid = TRUE; } - if (line >= edit->total_lines) + if (line >= edit->buffer.lines) return edit->line_offsets[2]; if (line <= 0) return 0; @@ -661,17 +661,17 @@ edit_move_up_paragraph (WEdit * edit, gboolean do_scroll) { long i = 0; - if (edit->curs_line > 1) + if (edit->buffer.curs_line > 1) { - if (!edit_line_is_blank (edit, edit->curs_line)) + if (!edit_line_is_blank (edit, edit->buffer.curs_line)) { - for (i = edit->curs_line - 1; i != 0; i--) + for (i = edit->buffer.curs_line - 1; i != 0; i--) if (edit_line_is_blank (edit, i)) break; } - else if (edit_line_is_blank (edit, edit->curs_line - 1)) + else if (edit_line_is_blank (edit, edit->buffer.curs_line - 1)) { - for (i = edit->curs_line - 1; i != 0; i--) + for (i = edit->buffer.curs_line - 1; i != 0; i--) if (!edit_line_is_blank (edit, i)) { i++; @@ -680,13 +680,13 @@ edit_move_up_paragraph (WEdit * edit, gboolean do_scroll) } else { - for (i = edit->curs_line - 1; i != 0; i--) + for (i = edit->buffer.curs_line - 1; i != 0; i--) if (edit_line_is_blank (edit, i)) break; } } - edit_move_up (edit, edit->curs_line - i, do_scroll); + edit_move_up (edit, edit->buffer.curs_line - i, do_scroll); } /* --------------------------------------------------------------------------------------------- */ @@ -698,18 +698,18 @@ edit_move_down_paragraph (WEdit * edit, gboolean do_scroll) { long i; - if (edit->curs_line >= edit->total_lines - 1) - i = edit->total_lines; - else if (!edit_line_is_blank (edit, edit->curs_line)) + if (edit->buffer.curs_line >= edit->buffer.lines - 1) + i = edit->buffer.lines; + else if (!edit_line_is_blank (edit, edit->buffer.curs_line)) { - for (i = edit->curs_line + 1; i != 0; i++) - if (edit_line_is_blank (edit, i) || i >= edit->total_lines) + for (i = edit->buffer.curs_line + 1; i != 0; i++) + if (edit_line_is_blank (edit, i) || i >= edit->buffer.lines) break; } - else if (edit_line_is_blank (edit, edit->curs_line + 1)) + else if (edit_line_is_blank (edit, edit->buffer.curs_line + 1)) { - for (i = edit->curs_line + 1; i != 0; i++) - if (!edit_line_is_blank (edit, i) || i > edit->total_lines) + for (i = edit->buffer.curs_line + 1; i != 0; i++) + if (!edit_line_is_blank (edit, i) || i > edit->buffer.lines) { i--; break; @@ -717,11 +717,11 @@ edit_move_down_paragraph (WEdit * edit, gboolean do_scroll) } else { - for (i = edit->curs_line + 1; i != 0; i++) - if (edit_line_is_blank (edit, i) || i >= edit->total_lines) + for (i = edit->buffer.curs_line + 1; i != 0; i++) + if (edit_line_is_blank (edit, i) || i >= edit->buffer.lines) break; } - edit_move_down (edit, i - edit->curs_line, do_scroll); + edit_move_down (edit, i - edit->buffer.curs_line, do_scroll); } /* --------------------------------------------------------------------------------------------- */ @@ -749,7 +749,7 @@ edit_end_page (WEdit * edit) static void edit_move_to_top (WEdit * edit) { - if (edit->curs_line) + if (edit->buffer.curs_line != 0) { edit_cursor_move (edit, -edit->buffer.curs1); edit_move_to_prev_col (edit, 0); @@ -759,18 +759,17 @@ edit_move_to_top (WEdit * edit) } } - /* --------------------------------------------------------------------------------------------- */ /** goto end of text */ static void edit_move_to_bottom (WEdit * edit) { - if (edit->curs_line < edit->total_lines) + if (edit->buffer.curs_line < edit->buffer.lines) { - edit_move_down (edit, edit->total_lines - edit->curs_row, 0); - edit->start_display = edit->last_byte; - edit->start_line = edit->total_lines; + edit_move_down (edit, edit->buffer.lines - edit->curs_row, 0); + edit->start_display = edit->buffer.size; + edit->start_line = edit->buffer.lines; edit_scroll_upward (edit, WIDGET (edit)->lines - 1); edit->force |= REDRAW_PAGE; } @@ -894,7 +893,7 @@ edit_right_word_move (WEdit * edit, int s) && edit->over_col == 0 && edit->buffer.curs1 == edit_eol (edit, edit->buffer.curs1)) break; edit_cursor_move (edit, 1); - if (edit->buffer.curs1 >= edit->last_byte) + if (edit->buffer.curs1 >= edit->buffer.size) break; c1 = edit_buffer_get_previous_byte (&edit->buffer); c2 = edit_buffer_get_current_byte (&edit->buffer); @@ -983,7 +982,7 @@ static void edit_move_updown (WEdit * edit, long lines, gboolean do_scroll, gboolean direction) { long p; - long l = direction ? edit->curs_line : edit->total_lines - edit->curs_line; + long l = direction ? edit->buffer.curs_line : edit->buffer.lines - edit->buffer.curs_line; if (lines > l) lines = l; @@ -1010,7 +1009,7 @@ edit_move_updown (WEdit * edit, long lines, gboolean do_scroll, gboolean directi #ifdef HAVE_CHARSET /* search start of current multibyte char (like CJK) */ - if (edit->buffer.curs1 > 0 && edit->buffer.curs1 + 1 < edit->last_byte + if (edit->buffer.curs1 > 0 && edit->buffer.curs1 + 1 < edit->buffer.size && edit_buffer_get_current_byte (&edit->buffer) >= 256) { edit_right_char_move_cmd (edit); @@ -1027,7 +1026,7 @@ edit_move_updown (WEdit * edit, long lines, gboolean do_scroll, gboolean directi static void edit_right_delete_word (WEdit * edit) { - while (edit->buffer.curs1 < edit->last_byte) + while (edit->buffer.curs1 < edit->buffer.size) { int c1, c2; @@ -1130,12 +1129,12 @@ edit_do_undo (WEdit * edit) if (edit->start_display > ac - KEY_PRESS) { - edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display); + edit->start_line -= edit_buffer_count_lines (&edit->buffer, ac - KEY_PRESS, edit->start_display); edit->force |= REDRAW_PAGE; } else if (edit->start_display < ac - KEY_PRESS) { - edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS); + edit->start_line += edit_buffer_count_lines (&edit->buffer, edit->start_display, ac - KEY_PRESS); edit->force |= REDRAW_PAGE; } edit->start_display = ac - KEY_PRESS; /* see push and pop above */ @@ -1206,12 +1205,12 @@ edit_do_redo (WEdit * edit) if (edit->start_display > ac - KEY_PRESS) { - edit->start_line -= edit_count_lines (edit, ac - KEY_PRESS, edit->start_display); + edit->start_line -= edit_buffer_count_lines (&edit->buffer, ac - KEY_PRESS, edit->start_display); edit->force |= REDRAW_PAGE; } else if (edit->start_display < ac - KEY_PRESS) { - edit->start_line += edit_count_lines (edit, edit->start_display, ac - KEY_PRESS); + edit->start_line += edit_buffer_count_lines (&edit->buffer, edit->start_display, ac - KEY_PRESS); edit->force |= REDRAW_PAGE; } edit->start_display = ac - KEY_PRESS; /* see push and pop above */ @@ -1449,7 +1448,7 @@ edit_get_bracket (WEdit * edit, gboolean in_screen, unsigned long furthest_brack for (q = edit->buffer.curs1 + inc;; q += inc) { /* out of buffer? */ - if (q >= edit->last_byte || q < 0) + if (q >= edit->buffer.size || q < 0) break; a = edit_buffer_get_byte (&edit->buffer, q); /* don't want to eat CPU */ @@ -1508,7 +1507,7 @@ edit_move_block_to_right (WEdit * edit) do { edit_cursor_move (edit, cur_bol - edit->buffer.curs1); - if (!edit_line_is_blank (edit, edit->curs_line)) + if (!edit_line_is_blank (edit, edit->buffer.curs_line)) { if (option_fill_tabs_with_spaces) insert_spaces_tab (edit, option_fake_half_tabs); @@ -1631,9 +1630,9 @@ edit_insert_column_from_file (WEdit * edit, int file, off_t * start_pos, off_t * for (p = edit->buffer.curs1;; p++) { - if (p == edit->last_byte) + if (p == edit->buffer.size) { - edit_cursor_move (edit, edit->last_byte - edit->buffer.curs1); + edit_cursor_move (edit, edit->buffer.size - edit->buffer.curs1); edit_insert_ahead (edit, '\n'); p++; break; @@ -1751,14 +1750,14 @@ edit_write_stream (WEdit * edit, FILE * f) if (edit->lb == LB_ASIS) { - for (i = 0; i < edit->last_byte; i++) + for (i = 0; i < edit->buffer.size; i++) if (fputc (edit_buffer_get_byte (&edit->buffer, i), f) < 0) break; return i; } /* change line breaks */ - for (i = 0; i < edit->last_byte; i++) + for (i = 0; i < edit->buffer.size; i++) { unsigned char c; @@ -1838,7 +1837,7 @@ edit_write_stream (WEdit * edit, FILE * f) } } - return edit->last_byte; + return edit->buffer.size; } /* --------------------------------------------------------------------------------------------- */ @@ -2466,9 +2465,9 @@ edit_insert (WEdit * edit, int c) /* now we must update some info on the file and check if a redraw is required */ if (c == '\n') { - book_mark_inc (edit, edit->curs_line); - edit->curs_line++; - edit->total_lines++; + book_mark_inc (edit, edit->buffer.curs_line); + edit->buffer.curs_line++; + edit->buffer.lines++; edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR; } @@ -2486,7 +2485,7 @@ edit_insert (WEdit * edit, int c) edit_buffer_insert (&edit->buffer, c); /* update file length */ - edit->last_byte++; + edit->buffer.size++; } /* --------------------------------------------------------------------------------------------- */ @@ -2507,8 +2506,8 @@ edit_insert_ahead (WEdit * edit, int c) edit_modification (edit); if (c == '\n') { - book_mark_inc (edit, edit->curs_line); - edit->total_lines++; + book_mark_inc (edit, edit->buffer.curs_line); + edit->buffer.lines++; edit->force |= REDRAW_AFTER_CURSOR; } /* ordinary char and not space */ @@ -2523,7 +2522,7 @@ edit_insert_ahead (WEdit * edit, int c) edit_buffer_insert_ahead (&edit->buffer, c); - edit->last_byte++; + edit->buffer.size++; } /* --------------------------------------------------------------------------------------------- */ @@ -2580,15 +2579,15 @@ edit_delete (WEdit * edit, gboolean byte_delete) p = edit_buffer_delete (&edit->buffer); - edit->last_byte--; + edit->buffer.size--; edit_push_undo_action (edit, p + 256); } edit_modification (edit); if (p == '\n') { - book_mark_dec (edit, edit->curs_line); - edit->total_lines--; + book_mark_dec (edit, edit->buffer.curs_line); + edit->buffer.lines--; edit->force |= REDRAW_AFTER_CURSOR; } if (edit->buffer.curs1 < edit->start_display) @@ -2641,15 +2640,15 @@ edit_backspace (WEdit * edit, gboolean byte_delete) p = edit_buffer_backspace (&edit->buffer); - edit->last_byte--; + edit->buffer.size--; edit_push_undo_action (edit, p); } edit_modification (edit); if (p == '\n') { - book_mark_dec (edit, edit->curs_line); - edit->curs_line--; - edit->total_lines--; + book_mark_dec (edit, edit->buffer.curs_line); + edit->buffer.curs_line--; + edit->buffer.lines--; edit->force |= REDRAW_AFTER_CURSOR; } @@ -2682,7 +2681,7 @@ edit_cursor_move (WEdit * edit, off_t increment) c = edit_buffer_backspace (&edit->buffer); if (c == '\n') { - edit->curs_line--; + edit->buffer.curs_line--; edit->force |= REDRAW_LINE_BELOW; } } @@ -2700,7 +2699,7 @@ edit_cursor_move (WEdit * edit, off_t increment) c = edit_buffer_delete (&edit->buffer); if (c == '\n') { - edit->curs_line++; + edit->buffer.curs_line++; edit->force |= REDRAW_LINE_ABOVE; } } @@ -2715,8 +2714,8 @@ edit_cursor_move (WEdit * edit, off_t increment) off_t edit_eol (const WEdit * edit, off_t current) { - if (current >= edit->last_byte) - return edit->last_byte; + if (current >= edit->buffer.size) + return edit->buffer.size; for (; edit_buffer_get_byte (&edit->buffer, current) != '\n'; current++) ; @@ -2739,23 +2738,6 @@ edit_bol (const WEdit * edit, off_t current) return current; } -/* --------------------------------------------------------------------------------------------- */ - -long -edit_count_lines (const WEdit * edit, off_t current, off_t upto) -{ - long lines = 0; - - if (upto > edit->last_byte) - upto = edit->last_byte; - if (current < 0) - current = 0; - while (current < upto) - if (edit_buffer_get_byte (&edit->buffer, current++) == '\n') - lines++; - return lines; -} - /* --------------------------------------------------------------------------------------------- */ /* If lines is zero this returns the count of lines from current to upto. */ /* If upto is zero returns index of lines forward current. */ @@ -2764,9 +2746,7 @@ off_t edit_move_forward (const WEdit * edit, off_t current, long lines, off_t upto) { if (upto != 0) - { - return (off_t) edit_count_lines (edit, current, upto); - } + return (off_t) edit_buffer_count_lines (&edit->buffer, current, upto); else { long next; @@ -2775,7 +2755,7 @@ edit_move_forward (const WEdit * edit, off_t current, long lines, off_t upto) while (lines-- != 0) { next = edit_eol (edit, current) + 1; - if (next > edit->last_byte) + if (next > edit->buffer.size) break; else current = next; @@ -2814,7 +2794,7 @@ edit_move_forward3 (const WEdit * edit, off_t current, long cols, off_t upto) cols = -10; } else - q = edit->last_byte + 2; + q = edit->buffer.size + 2; for (col = 0, p = current; p < q; p++) { @@ -2885,7 +2865,7 @@ edit_get_col (const WEdit * edit) void edit_update_curs_row (WEdit * edit) { - edit->curs_row = edit->curs_line - edit->start_line; + edit->curs_row = edit->buffer.curs_line - edit->start_line; } /* --------------------------------------------------------------------------------------------- */ @@ -2932,7 +2912,7 @@ edit_scroll_downward (WEdit * edit, long i) { long lines_below; - lines_below = edit->total_lines - edit->start_line - (WIDGET (edit)->lines - 1); + lines_below = edit->buffer.lines - edit->start_line - (WIDGET (edit)->lines - 1); if (lines_below > 0) { if (i > lines_below) @@ -3048,10 +3028,10 @@ edit_line_is_blank (WEdit * edit, long line) void edit_move_to_line (WEdit * e, long line) { - if (line < e->curs_line) - edit_move_up (e, e->curs_line - line, 0); + if (line < e->buffer.curs_line) + edit_move_up (e, e->buffer.curs_line - line, 0); else - edit_move_down (e, line - e->curs_line, 0); + edit_move_down (e, line - e->buffer.curs_line, 0); edit_scroll_screen_over_cursor (e); } @@ -3138,7 +3118,7 @@ edit_mark_current_word_cmd (WEdit * edit) } edit->mark1 = pos; - for (; pos < edit->last_byte; pos++) + for (; pos < edit->buffer.size; pos++) { int c1, c2; @@ -3149,7 +3129,7 @@ edit_mark_current_word_cmd (WEdit * edit) if ((my_type_of (c1) & my_type_of (c2)) == 0) break; } - edit->mark2 = min (pos + 1, edit->last_byte); + edit->mark2 = min (pos + 1, edit->buffer.size); edit->force |= REDRAW_LINE_ABOVE | REDRAW_AFTER_CURSOR; } @@ -3711,7 +3691,7 @@ edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion) edit_mark_cmd (edit, FALSE); break; case CK_MarkAll: - edit_set_markers (edit, 0, edit->last_byte, 0, 0); + edit_set_markers (edit, 0, edit->buffer.size, 0, 0); edit->force |= REDRAW_PAGE; break; case CK_Unmark: @@ -3734,11 +3714,11 @@ edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion) break; case CK_Bookmark: - book_mark_clear (edit, edit->curs_line, BOOK_MARK_FOUND_COLOR); - if (book_mark_query_color (edit, edit->curs_line, BOOK_MARK_COLOR)) - book_mark_clear (edit, edit->curs_line, BOOK_MARK_COLOR); + book_mark_clear (edit, edit->buffer.curs_line, BOOK_MARK_FOUND_COLOR); + if (book_mark_query_color (edit, edit->buffer.curs_line, BOOK_MARK_COLOR)) + book_mark_clear (edit, edit->buffer.curs_line, BOOK_MARK_COLOR); else - book_mark_insert (edit, edit->curs_line, BOOK_MARK_COLOR); + book_mark_insert (edit, edit->buffer.curs_line, BOOK_MARK_COLOR); break; case CK_BookmarkFlush: book_mark_flush (edit, BOOK_MARK_COLOR); @@ -3750,7 +3730,7 @@ edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion) { edit_book_mark_t *p; - p = book_mark_find (edit, edit->curs_line); + p = book_mark_find (edit, edit->buffer.curs_line); if (p->next != NULL) { p = p->next; @@ -3765,8 +3745,8 @@ edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion) { edit_book_mark_t *p; - p = book_mark_find (edit, edit->curs_line); - while (p->line == edit->curs_line) + p = book_mark_find (edit, edit->buffer.curs_line); + while (p->line == edit->buffer.curs_line) if (p->prev != NULL) p = p->prev; if (p->line >= 0) diff --git a/src/editor/editbuffer.c b/src/editor/editbuffer.c index da9d2ddb0..0b1cd184a 100644 --- a/src/editor/editbuffer.c +++ b/src/editor/editbuffer.c @@ -124,13 +124,16 @@ edit_buffer_get_byte_ptr (const edit_buffer_t * buf, off_t byte_index) */ void -edit_buffer_init (edit_buffer_t * buf) +edit_buffer_init (edit_buffer_t * buf, off_t size) { buf->b1 = g_ptr_array_sized_new (MAXBUFF + 1); buf->b2 = g_ptr_array_sized_new (MAXBUFF + 1); buf->curs1 = 0; buf->curs2 = 0; + + buf->size = size; + buf->lines = 0; } /* --------------------------------------------------------------------------------------------- */ @@ -300,6 +303,32 @@ edit_buffer_get_prev_utf (const edit_buffer_t * buf, off_t byte_index, int *char } #endif /* HAVE_CHARSET */ +/* --------------------------------------------------------------------------------------------- */ +/** + * Count lines in editor buffer. + * + * @param buf editor buffer + * @param first start byte offset + * @param last finish byte offset + * + * @return line numbers between "first" and "last" bytes + */ + +long +edit_buffer_count_lines (const edit_buffer_t * buf, off_t first, off_t last) +{ + long lines = 0; + + first = max (first, 0); + last = min (last, buf->size); + + while (first < last) + if (edit_buffer_get_byte (buf, first++) == '\n') + lines++; + + return lines; +} + /* --------------------------------------------------------------------------------------------- */ /** * Basic low level single character buffer alterations and movements at the cursor: insert character diff --git a/src/editor/editbuffer.h b/src/editor/editbuffer.h index fb685a6b4..3b30f42dc 100644 --- a/src/editor/editbuffer.h +++ b/src/editor/editbuffer.h @@ -38,18 +38,22 @@ /*** structures declarations (and typedefs of structures)*****************************************/ -typedef struct edit_buffer_struct { +typedef struct edit_buffer_struct +{ off_t curs1; /* position of the cursor from the beginning of the file. */ off_t curs2; /* position from the end of the file */ GPtrArray *b1; /* all data up to curs1 */ GPtrArray *b2; /* all data from end of file down to curs2 */ + off_t size; /* file size */ + long lines; /* total lines in the file */ + long curs_line; /* line number of the cursor. */ } edit_buffer_t; /*** global variables defined in .c file *********************************************************/ /*** declarations of public functions ************************************************************/ -void edit_buffer_init (edit_buffer_t * buf); +void edit_buffer_init (edit_buffer_t * buf, off_t size); void edit_buffer_clean (edit_buffer_t * buf); int edit_buffer_get_byte (const edit_buffer_t * buf, off_t byte_index); @@ -57,6 +61,7 @@ int edit_buffer_get_byte (const edit_buffer_t * buf, off_t byte_index); int edit_buffer_get_utf (const edit_buffer_t * buf, off_t byte_index, int *char_width); int edit_buffer_get_prev_utf (const edit_buffer_t * buf, off_t byte_index, int *char_width); #endif +long edit_buffer_count_lines (const edit_buffer_t * buf, off_t first, off_t last); void edit_buffer_insert (edit_buffer_t * buf, int c); void edit_buffer_insert_ahead (edit_buffer_t * buf, int c); diff --git a/src/editor/editcmd.c b/src/editor/editcmd.c index 6bc47edc9..94409033b 100644 --- a/src/editor/editcmd.c +++ b/src/editor/editcmd.c @@ -311,7 +311,7 @@ edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath) { /* do not change line breaks */ filelen = edit_buffer_write_file (&edit->buffer, fd); - if (filelen != edit->last_byte) + if (filelen != edit->buffer.size) { mc_close (fd); goto error_save; @@ -347,7 +347,7 @@ edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath) } } - if (filelen != edit->last_byte) + if (filelen != edit->buffer.size) goto error_save; if (this_save_mode == EDIT_DO_BACKUP) @@ -395,8 +395,8 @@ edit_save_file (WEdit * edit, const vfs_path_t * filename_vpath) static gboolean edit_check_newline (WEdit * edit) { - return !(option_check_nl_at_eof && edit->last_byte > 0 - && edit_buffer_get_byte (&edit->buffer, edit->last_byte - 1) != '\n' + return !(option_check_nl_at_eof && edit->buffer.size > 0 + && edit_buffer_get_byte (&edit->buffer, edit->buffer.size - 1) != '\n' && edit_query_dialog2 (_("Warning"), _("The file you are saving is not finished with a newline"), _("C&ontinue"), _("&Cancel"))); @@ -573,7 +573,7 @@ edit_block_delete (WEdit * edit) edit_push_markers (edit); - curs_line = edit->curs_line; + curs_line = edit->buffer.curs_line; curs_pos = edit->curs_col + edit->over_col; @@ -780,7 +780,7 @@ editcmd_find (WEdit * edit, gsize * len) off_t search_start = edit->search_start; off_t search_end; off_t start_mark = 0; - off_t end_mark = edit->last_byte; + off_t end_mark = edit->buffer.size; int mark_res = 0; char end_string_symbol; @@ -802,11 +802,11 @@ editcmd_find (WEdit * edit, gsize * len) && (start_mark != 0 || edit_buffer_get_byte (&edit->buffer, start_mark - 1) != end_string_symbol)) { start_mark = - edit_calculate_start_of_next_line (edit, start_mark, edit->last_byte, + edit_calculate_start_of_next_line (edit, start_mark, edit->buffer.size, end_string_symbol); } if ((edit->search_line_type & AT_END_LINE) != 0 - && (end_mark - 1 != edit->last_byte + && (end_mark - 1 != edit->buffer.size || edit_buffer_get_byte (&edit->buffer, end_mark) != end_string_symbol)) { end_mark = edit_calculate_end_of_previous_line (edit, end_mark, end_string_symbol); @@ -926,12 +926,12 @@ edit_do_search (WEdit * edit) search_create_bookmark = FALSE; book_mark_flush (edit, -1); - while (mc_search_run (edit->search, (void *) edit, q, edit->last_byte, &len)) + while (mc_search_run (edit->search, (void *) edit, q, edit->buffer.size, &len)) { if (found == 0) edit->search_start = edit->search->normal_offset; found++; - l += edit_count_lines (edit, q, edit->search->normal_offset); + l += edit_buffer_count_lines (&edit->buffer, q, edit->search->normal_offset); if (l != l_last) { book_mark_insert (edit, l, BOOK_MARK_FOUND_COLOR); @@ -1072,7 +1072,7 @@ pipe_mail (WEdit * edit, char *to, char *subject, char *cc) if (p) { off_t i; - for (i = 0; i < edit->last_byte; i++) + for (i = 0; i < edit->buffer.size; i++) fputc (edit_buffer_get_byte (&edit->buffer, i), p); pclose (p); } @@ -1139,7 +1139,7 @@ edit_collect_completions_get_current_word (WEdit * edit, mc_search_t * srch, off off_t i; GString *temp; - if (!mc_search_run (srch, (void *) edit, word_start, edit->last_byte, &len)) + if (!mc_search_run (srch, (void *) edit, word_start, edit->buffer.size, &len)) return NULL; temp = g_string_sized_new (len); @@ -1179,7 +1179,7 @@ edit_collect_completions (WEdit * edit, off_t word_start, gsize word_len, if (mc_config_get_bool (mc_main_config, CONFIG_APP_SECTION, "editor_wordcompletion_collect_entire_file", 0)) { - last_byte = edit->last_byte; + last_byte = edit->buffer.size; } else { @@ -1300,9 +1300,9 @@ edit_insert_column_of_text (WEdit * edit, unsigned char *data, off_t size, long } for (p = edit->buffer.curs1;; p++) { - if (p == edit->last_byte) + if (p == edit->buffer.size) { - edit_cursor_move (edit, edit->last_byte - edit->buffer.curs1); + edit_cursor_move (edit, edit->buffer.size - edit->buffer.curs1); edit_insert_ahead (edit, '\n'); p++; break; @@ -2546,7 +2546,7 @@ edit_replace_cmd (WEdit * edit, int again) edit->search_start = edit->search->normal_offset; /*returns negative on not found or error in pattern */ - if ((edit->search_start >= 0) && (edit->search_start < edit->last_byte)) + if ((edit->search_start >= 0) && (edit->search_start < edit->buffer.size)) { gsize i; GString *repl_str; @@ -2627,7 +2627,7 @@ edit_replace_cmd (WEdit * edit, int again) { edit->search_start += edit->found_len + (len == 0 ? 1 : 0); - if (edit->search_start >= edit->last_byte) + if (edit->search_start >= edit->buffer.size) break; } @@ -2967,7 +2967,7 @@ edit_goto_cmd (WEdit * edit) line = l; if (l < 0) - l = edit->total_lines + l + 2; + l = edit->buffer.lines + l + 2; edit_move_display (edit, l - WIDGET (edit)->lines / 2 - 1); edit_move_to_line (edit, l - 1); edit->force |= REDRAW_COMPLETELY; @@ -3561,7 +3561,7 @@ edit_suggest_current_word (WEdit * edit) void edit_spellcheck_file (WEdit * edit) { - if (edit->curs_line > 0) + if (edit->buffer.curs_line > 0) { edit_cursor_move (edit, -edit->buffer.curs1); edit_move_to_prev_col (edit, 0); @@ -3576,7 +3576,7 @@ edit_spellcheck_file (WEdit * edit) do { - if (edit->buffer.curs1 >= edit->last_byte) + if (edit->buffer.curs1 >= edit->buffer.size) return; c1 = c2; diff --git a/src/editor/editdraw.c b/src/editor/editdraw.c index 434ddd877..789fe4a0a 100644 --- a/src/editor/editdraw.c +++ b/src/editor/editdraw.c @@ -114,7 +114,7 @@ status_string (WEdit * edit, char *s, int w) * otherwise print the current character as is (if printable), * as decimal and as hex. */ - if (edit->buffer.curs1 < edit->last_byte) + if (edit->buffer.curs1 < edit->buffer.size) { #ifdef HAVE_CHARSET if (edit->utf8) @@ -159,8 +159,8 @@ status_string (WEdit * edit, char *s, int w) macro_index < 0 ? '-' : 'R', edit->overwrite == 0 ? '-' : 'O', edit->curs_col + edit->over_col, - edit->curs_line + 1, - edit->total_lines + 1, (long) edit->buffer.curs1, (long) edit->last_byte, byte_str, + edit->buffer.curs_line + 1, + edit->buffer.lines + 1, (long) edit->buffer.curs1, (long) edit->buffer.size, byte_str, #ifdef HAVE_CHARSET mc_global.source_codepage >= 0 ? get_codepage_id (mc_global.source_codepage) : #endif @@ -175,8 +175,8 @@ status_string (WEdit * edit, char *s, int w) edit->curs_col + edit->over_col, edit->start_line + 1, edit->curs_row, - edit->curs_line + 1, - edit->total_lines + 1, (long) edit->buffer.curs1, (long) edit->last_byte, byte_str, + edit->buffer.curs_line + 1, + edit->buffer.lines + 1, (long) edit->buffer.curs1, (long) edit->buffer.size, byte_str, #ifdef HAVE_CHARSET mc_global.source_codepage >= 0 ? get_codepage_id (mc_global.source_codepage) : #endif @@ -233,8 +233,8 @@ edit_status_fullscreen (WEdit * edit, int color) { size_t percent = 100; - if (edit->total_lines + 1 != 0) - percent = (edit->curs_line + 1) * 100 / (edit->total_lines + 1); + if (edit->buffer.lines + 1 != 0) + percent = (edit->buffer.curs_line + 1) * 100 / (edit->buffer.lines + 1); widget_move (h, 0, w - 6 - 6); tty_printf (" %3d%%", percent); } @@ -292,7 +292,8 @@ edit_status_window (WEdit * edit) edit_move (2, w->lines - 1); tty_printf ("%3ld %5ld/%ld %6ld/%ld", edit->curs_col + edit->over_col, - edit->curs_line + 1, edit->total_lines + 1, edit->buffer.curs1, edit->last_byte); + edit->buffer.curs_line + 1, edit->buffer.lines + 1, edit->buffer.curs1, + edit->buffer.size); } /* @@ -303,7 +304,7 @@ edit_status_window (WEdit * edit) if (cols > 46) { edit_move (32, w->lines - 1); - if (edit->buffer.curs1 >= edit->last_byte) + if (edit->buffer.curs1 >= edit->buffer.size) tty_print_string ("[ ]"); #ifdef HAVE_CHARSET else if (edit->utf8) @@ -545,7 +546,7 @@ edit_draw_this_line (WEdit * edit, off_t b, long row, long start_col, long end_c if (option_line_state) { cur_line = edit->start_line + row; - if (cur_line <= (unsigned int) edit->total_lines) + if (cur_line <= (unsigned int) edit->buffer.lines) { g_snprintf (line_stat, LINE_STATE_WIDTH + 1, "%7i ", cur_line + 1); } @@ -564,7 +565,7 @@ edit_draw_this_line (WEdit * edit, off_t b, long row, long start_col, long end_c { eval_marks (edit, &m1, &m2); - if (row <= edit->total_lines - edit->start_line) + if (row <= edit->buffer.lines - edit->start_line) { off_t tws = 0; if (tty_use_colors () && visible_tws) diff --git a/src/editor/editwidget.h b/src/editor/editwidget.h index 1f62827a6..90c73f6ee 100644 --- a/src/editor/editwidget.h +++ b/src/editor/editwidget.h @@ -97,7 +97,6 @@ struct WEdit off_t found_start; /* the found word from a search - start position */ /* display information */ - off_t last_byte; /* Last byte of file */ long start_display; /* First char displayed */ long start_col; /* First displayed column, negative */ long max_column; /* The maximum cursor position ever reached used to calc hori scroll bar */ @@ -115,11 +114,9 @@ struct WEdit unsigned int fullscreen:1; /* Is window fullscreen or not */ long prev_col; /* recent column position of the cursor - used when moving up or down past lines that are shorter than the current line */ - long curs_line; /* line number of the cursor. */ long start_line; /* line number of the top of the page */ /* file info */ - long total_lines; /* total lines in the file */ off_t mark1; /* position of highlight start */ off_t mark2; /* position of highlight end */ off_t end_mark_curs; /* position of cursor after end of highlighting */ diff --git a/src/editor/syntax.c b/src/editor/syntax.c index 4097b1d0b..d94540a20 100644 --- a/src/editor/syntax.c +++ b/src/editor/syntax.c @@ -1399,7 +1399,7 @@ edit_get_syntax_color (WEdit * edit, off_t byte_index) if (!tty_use_colors ()) return 0; - if (edit->rules != NULL && byte_index < edit->last_byte && option_syntax_highlighting) + if (edit->rules != NULL && byte_index < edit->buffer.size && option_syntax_highlighting) { edit_get_rule (edit, byte_index); return translate_rule_to_color (edit, &edit->rule); diff --git a/src/editor/wordproc.c b/src/editor/wordproc.c index 696a1e0d6..a488d7596 100644 --- a/src/editor/wordproc.c +++ b/src/editor/wordproc.c @@ -77,7 +77,7 @@ line_start (WEdit * edit, long line) off_t p; long l; - l = edit->curs_line; + l = edit->buffer.curs_line; p = edit->buffer.curs1; if (line < l) @@ -126,7 +126,7 @@ begin_paragraph (WEdit * edit, gboolean force) { long i; - for (i = edit->curs_line - 1; i >= 0; i--) + for (i = edit->buffer.curs_line - 1; i >= 0; i--) if (edit_line_is_blank (edit, i) || (force && bad_line_start (edit, line_start (edit, i)))) { @@ -134,7 +134,7 @@ begin_paragraph (WEdit * edit, gboolean force) break; } - return edit_move_backward (edit, edit_bol (edit, edit->buffer.curs1), edit->curs_line - i); + return edit_move_backward (edit, edit_bol (edit, edit->buffer.curs1), edit->buffer.curs_line - i); } /* --------------------------------------------------------------------------------------------- */ @@ -148,7 +148,7 @@ end_paragraph (WEdit * edit, gboolean force) { long i; - for (i = edit->curs_line + 1; i <= edit->total_lines; i++) + for (i = edit->buffer.curs_line + 1; i <= edit->buffer.lines; i++) if (edit_line_is_blank (edit, i) || (force && bad_line_start (edit, line_start (edit, i)))) { @@ -158,7 +158,7 @@ end_paragraph (WEdit * edit, gboolean force) return edit_eol (edit, edit_move_forward (edit, edit_bol (edit, edit->buffer.curs1), - i - edit->curs_line, 0)); + i - edit->buffer.curs_line, 0)); } /* --------------------------------------------------------------------------------------------- */ @@ -344,7 +344,7 @@ edit_indent_width (const WEdit * edit, off_t p) /* move to the end of the leading whitespace of the line */ while (strchr ("\t ", edit_buffer_get_byte (&edit->buffer, q)) != NULL - && q < edit->last_byte - 1) + && q < edit->buffer.size - 1) q++; /* count the number of columns of indentation */ return (long) edit_move_forward3 (edit, p, 0, q); @@ -453,7 +453,7 @@ format_paragraph (WEdit * edit, gboolean force) if (option_word_wrap_line_length < 2) return; - if (edit_line_is_blank (edit, edit->curs_line)) + if (edit_line_is_blank (edit, edit->buffer.curs_line)) return; p = begin_paragraph (edit, force);