From 4cad5d6f679f2dc5c23a30d3133e9f049bc01f64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Diego=20Aur=C3=A9lio=20Mesquita?= Date: Thu, 12 Oct 2017 02:17:34 -0300 Subject: [PATCH] Implement overtype mode. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marco Diego Aurélio Mesquita --- src/browser.c | 2 +- src/global.c | 11 +++++++++++ src/nano.c | 6 +++++- src/proto.h | 5 +++++ src/winio.c | 33 +++++++++++++++++++++++++++++---- 5 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/browser.c b/src/browser.c index 3c978bce..5d09644f 100644 --- a/src/browser.c +++ b/src/browser.c @@ -628,7 +628,7 @@ void browser_refresh(void) /* If requested, put the cursor on the selected item and switch it on. */ if (ISSET(SHOW_CURSOR)) { wmove(edit, the_row, the_column); - curs_set(1); + unhide_cursor(); } wnoutrefresh(edit); diff --git a/src/global.c b/src/global.c index 13a138dd..9cf87066 100644 --- a/src/global.c +++ b/src/global.c @@ -70,6 +70,9 @@ bool inhelp = FALSE; char *title = NULL; /* When not NULL: the title of the current help text. */ +bool overtype_mode = FALSE; + /* Whether we're in overtype mode */ + bool more_than_one = FALSE; /* Whether more than one buffer is or has been open. */ @@ -637,6 +640,8 @@ void shortcut_init(void) const char *nano_recordmacro_msg = N_("Start/stop recording a macro"); const char *nano_runmacro_msg = N_("Run the last recorded macro"); #endif + const char *nano_overtype_msg = + N_("Toggle overtype mode"); const char *nano_case_msg = N_("Toggle the case sensitivity of the search"); const char *nano_reverse_msg = @@ -923,6 +928,10 @@ void shortcut_init(void) add_to_funcs(do_enter, MMAIN, N_("Enter"), IFSCHELP(nano_enter_msg), BLANKAFTER, NOVIEW); + add_to_funcs(do_overtype, MMAIN, + N_("Overtype"), IFSCHELP(nano_overtype_msg), BLANKAFTER, + NOVIEW); + add_to_funcs(do_delete, MMAIN, N_("Delete"), IFSCHELP(nano_delete_msg), TOGETHER, NOVIEW); add_to_funcs(do_backspace, MMAIN, @@ -1561,6 +1570,8 @@ sc *strtosc(const char *input) else if (!strcasecmp(input, "redo")) s->scfunc = do_redo; #endif + else if (!strcasecmp(input, "overtype")) + s->scfunc = do_overtype; else if (!strcasecmp(input, "left") || !strcasecmp(input, "back")) s->scfunc = do_left; diff --git a/src/nano.c b/src/nano.c index b3888d9d..f9fa2daa 100644 --- a/src/nano.c +++ b/src/nano.c @@ -1629,6 +1629,10 @@ int do_input(bool allow_funcs) /* Store the byte, and leave room for a terminating zero. */ puddle = charealloc(puddle, depth + 2); puddle[depth++] = (char)input; + if (overtype_mode && + openfile->current->data[openfile->current_x] != '\0' && + input != '\n') + do_delete(); } #ifndef NANO_TINY if (openfile->mark_set && openfile->kind_of_mark == SOFTMARK) { @@ -2479,7 +2483,7 @@ int main(int argc, char **argv) /* Create the three subwindows, based on the current screen dimensions. */ window_init(); - curs_set(0); + hide_cursor(); editwincols = COLS; diff --git a/src/proto.h b/src/proto.h index 2bc7fe7a..ea2d6319 100644 --- a/src/proto.h +++ b/src/proto.h @@ -49,6 +49,8 @@ extern bool have_palette; extern bool suppress_cursorpos; +extern bool overtype_mode; + extern message_type lastmessage; extern filestruct *pletion_line; @@ -447,6 +449,9 @@ void disable_flow_control(void); void enable_flow_control(void); void terminal_init(void); void unbound_key(int code); +void hide_cursor(void); +void unhide_cursor(void); +void do_overtype(void); int do_input(bool allow_funcs); #ifdef ENABLE_MOUSE int do_mouse(void); diff --git a/src/winio.c b/src/winio.c index dd717b4c..8bc5416a 100644 --- a/src/winio.c +++ b/src/winio.c @@ -113,6 +113,31 @@ void run_macro(void) } #endif /* !NANO_TINY */ +/* Hides cursor */ +void hide_cursor(void) +{ + curs_set(0); +} + +/* Unhides cursor considering overtype mode */ +void unhide_cursor(void) +{ + curs_set(overtype_mode ? 2 : 1); +} + +/* Toggles overtype mode */ +void do_overtype(void) +{ + overtype_mode = !overtype_mode; + + if (overtype_mode) + statusbar(_("Overtype mode enabled")); + else + statusbar(_("Overtype mode disabled")); + + unhide_cursor(); +} + /* Control character compatibility: * * - Ctrl-H is Backspace under ASCII, ANSI, VT100, and VT220. @@ -184,7 +209,7 @@ void get_key_buffer(WINDOW *win) doupdate(); if (reveal_cursor) - curs_set(1); + unhide_cursor(); /* Read in the first keystroke using whatever mode we're in. */ while (input == ERR) { @@ -197,7 +222,7 @@ void get_key_buffer(WINDOW *win) } #endif if (input == ERR && !waiting_mode) { - curs_set(0); + hide_cursor(); return; } @@ -209,7 +234,7 @@ void get_key_buffer(WINDOW *win) die(_("Too many errors from stdin")); } - curs_set(0); + hide_cursor(); /* Increment the length of the keystroke buffer, and save the value * of the keystroke at the end of it. */ @@ -1335,7 +1360,7 @@ int parse_escape_sequence(WINDOW *win, int kbinput) lastmessage = HUSH; if (currmenu == MMAIN) { place_the_cursor(); - curs_set(1); + unhide_cursor(); } } } -- 2.11.0