From ed8c80f48d3fe2d8772b566dca3d997ecb750147 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Mon, 18 Feb 2013 12:13:05 +0400 Subject: [PATCH] New editor buffer API to delete character at cursor position. Signed-off-by: Andrew Borodin --- src/editor/edit.c | 20 +++------------- src/editor/editbuffer.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ src/editor/editbuffer.h | 2 ++ 3 files changed, 69 insertions(+), 17 deletions(-) diff --git a/src/editor/edit.c b/src/editor/edit.c index a661e78d4..c4d231d33 100644 --- a/src/editor/edit.c +++ b/src/editor/edit.c @@ -2577,17 +2577,9 @@ edit_delete (WEdit * edit, gboolean byte_delete) if (edit->last_get_rule > edit->buffer.curs1) edit->last_get_rule--; - p = edit->buffer.buffers2[(edit->buffer.curs2 - 1) >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - - ((edit->buffer.curs2 - - 1) & M_EDIT_BUF_SIZE) - 1]; + p = edit_buffer_delete (&edit->buffer); - if (!(edit->buffer.curs2 & M_EDIT_BUF_SIZE)) - { - g_free (edit->buffer.buffers2[edit->buffer.curs2 >> S_EDIT_BUF_SIZE]); - edit->buffer.buffers2[edit->buffer.curs2 >> S_EDIT_BUF_SIZE] = NULL; - } edit->last_byte--; - edit->buffer.curs2--; edit_push_undo_action (edit, p + 256); } @@ -2646,15 +2638,9 @@ edit_backspace (WEdit * edit, gboolean byte_delete) if (edit->last_get_rule >= edit->buffer.curs1) edit->last_get_rule--; - p = *(edit->buffer.buffers1[(edit->buffer.curs1 - 1) >> S_EDIT_BUF_SIZE] + - ((edit->buffer.curs1 - 1) & M_EDIT_BUF_SIZE)); - if (((edit->buffer.curs1 - 1) & M_EDIT_BUF_SIZE) == 0) - { - g_free (edit->buffer.buffers1[edit->buffer.curs1 >> S_EDIT_BUF_SIZE]); - edit->buffer.buffers1[edit->buffer.curs1 >> S_EDIT_BUF_SIZE] = NULL; - } + p = edit_buffer_backspace (&edit->buffer); + edit->last_byte--; - edit->buffer.curs1--; edit_push_undo_action (edit, p); } edit_modification (edit); diff --git a/src/editor/editbuffer.c b/src/editor/editbuffer.c index f061ebc3a..f76204ba2 100644 --- a/src/editor/editbuffer.c +++ b/src/editor/editbuffer.c @@ -355,6 +355,70 @@ edit_buffer_insert_ahead (edit_buffer_t * buf, int c) /* --------------------------------------------------------------------------------------------- */ /** + * Basic low level single character buffer alterations and movements at the cursor: delete character + * at the cursor position. + * + * @param buf pointer to editor buffer + * @param c character to insert + */ + +int +edit_buffer_delete (edit_buffer_t * buf) +{ + unsigned char c; + off_t prev; + off_t i; + + prev = buf->curs2 - 1; + + i = prev & M_EDIT_BUF_SIZE; + c = buf->buffers2[prev >> S_EDIT_BUF_SIZE][EDIT_BUF_SIZE - 1 - i]; + + if ((buf->curs2 & M_EDIT_BUF_SIZE) == 0) + { + g_free (buf->buffers2[buf->curs2 >> S_EDIT_BUF_SIZE]); + buf->buffers2[buf->curs2 >> S_EDIT_BUF_SIZE] = NULL; + } + + buf->curs2 = prev; + + return c; +} + +/* --------------------------------------------------------------------------------------------- */ +/** + * Basic low level single character buffer alterations and movements at the cursor: delete character + * before the cursor position and move left. + * + * @param buf pointer to editor buffer + * @param c character to insert + */ + +int +edit_buffer_backspace (edit_buffer_t * buf) +{ + unsigned char c; + off_t prev; + off_t i; + + prev = buf->curs1 - 1; + + i = prev & M_EDIT_BUF_SIZE; + c = buf->buffers1[prev >> S_EDIT_BUF_SIZE][i]; + + if (i == 0) + { + g_free (buf->buffers1[buf->curs1 >> S_EDIT_BUF_SIZE]); + buf->buffers1[buf->curs1 >> S_EDIT_BUF_SIZE] = NULL; + } + + buf->curs1 = prev; + + return c; +} + +/* --------------------------------------------------------------------------------------------- */ +/** * Load file into editor buffer * * @param buf pointer to editor buffer diff --git a/src/editor/editbuffer.h b/src/editor/editbuffer.h index 4f83672c9..b62e9a380 100644 --- a/src/editor/editbuffer.h +++ b/src/editor/editbuffer.h @@ -60,6 +60,8 @@ int edit_buffer_get_prev_utf (const edit_buffer_t * buf, off_t byte_index, int * void edit_buffer_insert (edit_buffer_t * buf, int c); void edit_buffer_insert_ahead (edit_buffer_t * buf, int c); +int edit_buffer_delete (edit_buffer_t * buf); +int edit_buffer_backspace (edit_buffer_t * buf); off_t edit_buffer_read_file (edit_buffer_t * buf, int fd, off_t size); off_t edit_buffer_write_file (edit_buffer_t * buf, int fd); -- 2.11.4.GIT