diff --git a/vimode/src/Makefile.am b/vimode/src/Makefile.am index d6383073f..c6107cb27 100644 --- a/vimode/src/Makefile.am +++ b/vimode/src/Makefile.am @@ -35,7 +35,9 @@ vi_srcs = \ cmds/edit.h \ cmds/edit.c \ cmds/excmds.h \ - cmds/excmds.c + cmds/excmds.c \ + cmds/undo.h \ + cmds/undo.c vimode_la_SOURCES = \ backends/backend-geany.c \ diff --git a/vimode/src/cmds/edit.c b/vimode/src/cmds/edit.c index ecdd0dd19..e2aa216c1 100644 --- a/vimode/src/cmds/edit.c +++ b/vimode/src/cmds/edit.c @@ -17,6 +17,7 @@ */ #include "cmds/edit.h" +#include "cmds/undo.h" #include "utils.h" @@ -161,13 +162,7 @@ void cmd_del_word_left(CmdContext *c, CmdParams *p) void cmd_undo(CmdContext *c, CmdParams *p) { - gint i; - for (i = 0; i < p->num; i++) - { - if (!SSM(p->sci, SCI_CANUNDO, 0, 0)) - break; - SSM(p->sci, SCI_UNDO, 0, 0); - } + undo_apply(c, p->num); } diff --git a/vimode/src/cmds/excmds.c b/vimode/src/cmds/excmds.c index a8f9b02a5..e11047450 100644 --- a/vimode/src/cmds/excmds.c +++ b/vimode/src/cmds/excmds.c @@ -18,6 +18,7 @@ #include "cmds/excmds.h" #include "cmds/edit.h" +#include "cmds/undo.h" #include "utils.h" void excmd_save(CmdContext *c, ExCmdParams *p) @@ -93,7 +94,7 @@ void excmd_put(CmdContext *c, ExCmdParams *p) void excmd_undo(CmdContext *c, ExCmdParams *p) { - SSM(c->sci, SCI_UNDO, 0, 0); + undo_apply(c, 1); } diff --git a/vimode/src/cmds/undo.c b/vimode/src/cmds/undo.c new file mode 100644 index 000000000..0e1c01ec7 --- /dev/null +++ b/vimode/src/cmds/undo.c @@ -0,0 +1,59 @@ +/* + * Copyright 2024 Sylvain Cresto + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#include "undo.h" +#include "utils.h" + +void undo_update(CmdContext *c, gint pos) +{ + c->undo_pos = pos; +} + + +static gboolean is_start_of_line(ScintillaObject *sci, gint pos) +{ + gint line = SSM(sci, SCI_LINEFROMPOSITION, pos, 0); + gint line_pos = SSM(sci, SCI_POSITIONFROMLINE, line, 0); + + return pos == line_pos; +} + + +void undo_apply(CmdContext *c, gint num) +{ + ScintillaObject *sci = c->sci; + gint i; + + c->undo_pos = -1; + + for (i = 0; i < num; i++) + { + if (!SSM(sci, SCI_CANUNDO, 0, 0)) + break; + SSM(sci, SCI_UNDO, 0, 0); + } + + /* exit when no undo has been applied */ + if (c->undo_pos == -1) + return; + + if (is_start_of_line(sci, c->undo_pos)) + goto_nonempty(sci, SSM(sci, SCI_LINEFROMPOSITION, c->undo_pos, 0), FALSE); + else + SET_POS(sci, c->undo_pos, FALSE); +} diff --git a/vimode/src/cmds/undo.h b/vimode/src/cmds/undo.h new file mode 100644 index 000000000..414e7eb7d --- /dev/null +++ b/vimode/src/cmds/undo.h @@ -0,0 +1,27 @@ +/* + * Copyright 2024 Sylvain Cresto + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef __UNDO_H__ +#define __UNDO_H__ + +#include "context.h" + +void undo_update(CmdContext *c, gint pos); +void undo_apply(CmdContext *c, gint num); + +#endif diff --git a/vimode/src/context.h b/vimode/src/context.h index d25573c9b..1d1019b3e 100644 --- a/vimode/src/context.h +++ b/vimode/src/context.h @@ -57,6 +57,9 @@ typedef struct * copied N times when e.g. 'i' is preceded by a number or when using '.' */ gchar insert_buf[INSERT_BUF_LEN]; gint insert_buf_len; + + /* cursor position to restore after undo */ + gint undo_pos; } CmdContext; #endif diff --git a/vimode/src/utils.c b/vimode/src/utils.c index 2a367a80c..ddd5c3f49 100644 --- a/vimode/src/utils.c +++ b/vimode/src/utils.c @@ -210,6 +210,7 @@ gint get_line_number_rel(ScintillaObject *sci, gint shift) return new_line; } + void goto_nonempty(ScintillaObject *sci, gint line, gboolean scroll) { gint line_end_pos = SSM(sci, SCI_GETLINEENDPOSITION, line, 0); diff --git a/vimode/src/vi.c b/vimode/src/vi.c index f5b6c9496..fae264854 100644 --- a/vimode/src/vi.c +++ b/vimode/src/vi.c @@ -21,6 +21,7 @@ #include "utils.h" #include "keypress.h" #include "excmd-prompt.h" +#include "cmds/undo.h" #include @@ -51,7 +52,8 @@ CmdContext ctx = NULL, NULL, NULL, FALSE, FALSE, 0, 1, - "", 0 + "", 0, + -1 }; @@ -306,6 +308,10 @@ gboolean vi_notify_sci(SCNotification *nt) } } + /* Keep position of undo operation */ + if (nt->nmhdr.code == SCN_MODIFIED && (nt->modificationType & SC_MOD_BEFOREINSERT && nt->modificationType & SC_PERFORMED_UNDO) && nt->length > 1) + undo_update(&ctx, nt->position); + /* This makes sure that when we click behind the end of line in command mode, * the cursor is not placed BEHIND the last character but ON the last character. * We want to ignore this when doing selection with mouse as it breaks things. */