From b3e4051deda44580d479228b24d19b3a603d397c Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Fri, 31 Jul 2015 11:52:26 +0000 Subject: [PATCH] Adding two new bindable functions which delete all characters from the cursor to the preceding or succeeding word start. git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@5334 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- ChangeLog | 7 +++++++ doc/man/nanorc.5 | 6 ++++++ doc/texinfo/nano.texi | 6 ++++++ src/global.c | 15 ++++++++++++++- src/proto.h | 4 ++++ src/text.c | 41 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 78 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 66898a80..3eb8bc56 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2015-07-31 Benno Schulenberg + * src/text.c (do_cutword, do_cut_prev_word, do_cut_next_word), + src/global.c (shortcut_init, strtosc), doc/texinfo/nano.texi, + doc/man/nanorc.5: Add two new bindable functions, 'cutwordleft' + and 'cutwordright', which delete all characters from the cursor + to the preceding or succeeding word start. Fixes bug #32803. + 2015-07-30 Benno Schulenberg * src/global.c (shortcut_init): Don't show ^R and ^T in the help lines in restricted mode (if possible), to give visual feedback. diff --git a/doc/man/nanorc.5 b/doc/man/nanorc.5 index f405058f..80e566b9 100644 --- a/doc/man/nanorc.5 +++ b/doc/man/nanorc.5 @@ -418,6 +418,12 @@ current cursor position. .B mark Sets the mark at the current position, to start selecting text. .TP +.B cutwordleft +Cuts from the cursor position to the beginning of the preceding word. +.TP +.B cutwordright +Cuts from the cursor position to the beginning of the next word. +.TP .B cutrestoffile Cuts all text from the cursor position till the end of the buffer. .TP diff --git a/doc/texinfo/nano.texi b/doc/texinfo/nano.texi index 3769f2c0..0b250fbb 100644 --- a/doc/texinfo/nano.texi +++ b/doc/texinfo/nano.texi @@ -988,6 +988,12 @@ current cursor position. @item mark Sets the mark at the current position, to start selecting text. +@item cutwordleft +Cuts from the cursor position to the beginning of the preceding word. + +@item cutwordright +Cuts from the cursor position to the beginning of the next word. + @item cutrestoffile Cuts all text from the cursor position till the end of the buffer. diff --git a/src/global.c b/src/global.c index cde81a6c..725ac904 100644 --- a/src/global.c +++ b/src/global.c @@ -587,6 +587,10 @@ void shortcut_init(void) const char *nano_backspace_msg = N_("Delete the character to the left of the cursor"); #ifndef NANO_TINY + const char *nano_cut_word_left_msg = + N_("Cut backward from cursor to word start"); + const char *nano_cut_word_right_msg = + N_("Cut forward from cursor to next word start"); const char *nano_cut_till_eof_msg = N_("Cut from the cursor position to the end of the file"); #endif @@ -893,7 +897,8 @@ void shortcut_init(void) add_to_funcs(do_tab, MMAIN, N_("Tab"), IFSCHELP(nano_tab_msg), TOGETHER, NOVIEW); add_to_funcs(do_enter_void, MMAIN, - N_("Enter"), IFSCHELP(nano_enter_msg), TOGETHER, NOVIEW); + N_("Enter"), IFSCHELP(nano_enter_msg), BLANKAFTER, NOVIEW); + add_to_funcs(do_delete, MMAIN, N_("Delete"), IFSCHELP(nano_delete_msg), TOGETHER, NOVIEW); add_to_funcs(do_backspace, MMAIN, @@ -906,6 +911,10 @@ void shortcut_init(void) NOVIEW); #ifndef NANO_TINY + add_to_funcs(do_cut_prev_word, MMAIN, + N_("Cut Left"), IFSCHELP(nano_cut_word_left_msg), TOGETHER, NOVIEW); + add_to_funcs(do_cut_next_word, MMAIN, + N_("Cut Right"), IFSCHELP(nano_cut_word_right_msg), TOGETHER, NOVIEW); add_to_funcs(do_cut_till_eof, MMAIN, N_("CutTillEnd"), IFSCHELP(nano_cut_till_eof_msg), BLANKAFTER, NOVIEW); #endif @@ -1392,6 +1401,10 @@ sc *strtosc(char *input) s->scfunc = do_prev_word_void; else if (!strcasecmp(input, "nextword")) s->scfunc = do_next_word_void; + else if (!strcasecmp(input, "cutwordleft")) + s->scfunc = do_cut_prev_word; + else if (!strcasecmp(input, "cutwordright")) + s->scfunc = do_cut_next_word; else if (!strcasecmp(input, "findbracket")) s->scfunc = do_find_bracket; else if (!strcasecmp(input, "wordcount")) diff --git a/src/proto.h b/src/proto.h index 777acf60..70e7ee6a 100644 --- a/src/proto.h +++ b/src/proto.h @@ -646,6 +646,10 @@ void do_mark(void); #endif void do_delete(void); void do_backspace(void); +#ifndef NANO_TINY +void do_cut_prev_word(void); +void do_cut_next_word(void); +#endif void do_tab(void); #ifndef NANO_TINY void do_indent(ssize_t cols); diff --git a/src/text.c b/src/text.c index 05a51ca0..ffea145b 100644 --- a/src/text.c +++ b/src/text.c @@ -185,6 +185,47 @@ void do_backspace(void) } } +#ifndef NANO_TINY +/* Delete text from the cursor until the first start of a word to + * the right, or to the left when backward is true. */ +void do_cutword(bool backward) +{ + /* Remember the current cursor position. */ + filestruct *is_current = openfile->current; + size_t is_current_x = openfile->current_x; + + /* Move the cursor to a word start, to the left or to the right. */ + if (backward) + do_prev_word(ISSET(WORD_BOUNDS), FALSE); + else + do_next_word(ISSET(WORD_BOUNDS), FALSE); + + /* Set the mark at the start of that word. */ + openfile->mark_begin = openfile->current; + openfile->mark_begin_x = openfile->current_x; + openfile->mark_set = TRUE; + + /* Put the cursor back where it was, so an undo will put it there too. */ + openfile->current = is_current; + openfile->current_x = is_current_x; + + /* Now kill the marked region and a word is gone. */ + do_cut_text_void(); +} + +/* Delete a word leftward. */ +void do_cut_prev_word(void) +{ + do_cutword(TRUE); +} + +/* Delete a word rightward. */ +void do_cut_next_word(void) +{ + do_cutword(FALSE); +} +#endif /* !NANO_TINY */ + /* Insert a tab. If the TABS_TO_SPACES flag is set, insert the number * of spaces that a tab would normally take up. */ void do_tab(void) -- 2.39.5