Branch: refs/heads/master Author: Sylvain Cresto scresto@gmail.com Committer: Sylvain Cresto scresto@gmail.com Date: Wed, 22 May 2024 08:42:16 UTC Commit: cc7d25d426b107ff033c71ea051a90bd4310c7be https://github.com/geany/geany-plugins/commit/cc7d25d426b107ff033c71ea051a90...
Log Message: ----------- vimode: fix cursor position after using undo (u key)
Modified Paths: -------------- vimode/src/Makefile.am vimode/src/cmds/edit.c vimode/src/cmds/excmds.c vimode/src/cmds/undo.c vimode/src/cmds/undo.h vimode/src/context.h vimode/src/utils.c vimode/src/vi.c
Modified: vimode/src/Makefile.am 4 lines changed, 3 insertions(+), 1 deletions(-) =================================================================== @@ -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 \
Modified: vimode/src/cmds/edit.c 9 lines changed, 2 insertions(+), 7 deletions(-) =================================================================== @@ -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); }
Modified: vimode/src/cmds/excmds.c 3 lines changed, 2 insertions(+), 1 deletions(-) =================================================================== @@ -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); }
Modified: vimode/src/cmds/undo.c 59 lines changed, 59 insertions(+), 0 deletions(-) =================================================================== @@ -0,0 +1,59 @@ +/* + * Copyright 2024 Sylvain Cresto scresto@gmail.com + * + * 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); +}
Modified: vimode/src/cmds/undo.h 27 lines changed, 27 insertions(+), 0 deletions(-) =================================================================== @@ -0,0 +1,27 @@ +/* + * Copyright 2024 Sylvain Cresto scresto@gmail.com + * + * 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
Modified: vimode/src/context.h 3 lines changed, 3 insertions(+), 0 deletions(-) =================================================================== @@ -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
Modified: vimode/src/utils.c 1 lines changed, 1 insertions(+), 0 deletions(-) =================================================================== @@ -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);
Modified: vimode/src/vi.c 8 lines changed, 7 insertions(+), 1 deletions(-) =================================================================== @@ -21,6 +21,7 @@ #include "utils.h" #include "keypress.h" #include "excmd-prompt.h" +#include "cmds/undo.h"
#include <gdk/gdkkeysyms.h>
@@ -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. */
-------------- This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).