Skip to content

Commit

Permalink
Refactor the code to take into account the latest comments
Browse files Browse the repository at this point in the history
  • Loading branch information
scresto09 committed May 21, 2024
1 parent 1be4e6a commit 63b7e25
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 51 deletions.
10 changes: 1 addition & 9 deletions vimode/src/cmds/edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,7 @@ void cmd_del_word_left(CmdContext *c, CmdParams *p)

void cmd_undo(CmdContext *c, CmdParams *p)
{
gint i;
undo_start(c);
for (i = 0; i < p->num; i++)
{
if (!SSM(p->sci, SCI_CANUNDO, 0, 0))
break;
SSM(p->sci, SCI_UNDO, 0, 0);
}
undo_end(c);
undo_apply(c, p->num);
}


Expand Down
4 changes: 1 addition & 3 deletions vimode/src/cmds/excmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ void excmd_put(CmdContext *c, ExCmdParams *p)

void excmd_undo(CmdContext *c, ExCmdParams *p)
{
undo_start(c);
SSM(c->sci, SCI_UNDO, 0, 0);
undo_end(c);
undo_apply(c, 1);
}


Expand Down
38 changes: 25 additions & 13 deletions vimode/src/cmds/undo.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,41 @@
#include "undo.h"
#include "utils.h"

void undo_start(CmdContext *c)
void undo_update(CmdContext *c, gint pos)
{
c->undo_pos = -1;
c->undo_is_sol = FALSE;
c->undo_pos = pos;
}


void undo_update(CmdContext *c, gint pos, gboolean is_sol)
static gboolean is_start_of_line(ScintillaObject *sci, gint pos)
{
c->undo_pos = pos;
c->undo_is_sol = is_sol;
gint line = SSM(sci, SCI_LINEFROMPOSITION, pos, 0);
gint line_pos = SSM(sci, SCI_POSITIONFROMLINE, line, 0);

return pos == line_pos;
}


void undo_end(CmdContext *c)
void undo_apply(CmdContext *c, gint num)
{
ScintillaObject *sci = c->sci;
if (c->undo_pos != -1)
gint i;

c->undo_pos = -1;

for (i = 0; i < num; i++)
{
if (c->undo_is_sol) {
goto_nonempty(sci, SSM(sci, SCI_LINEFROMPOSITION, c->undo_pos, 0), FALSE);
} else {
SET_POS(sci, c->undo_pos, FALSE);
}
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);
}
5 changes: 2 additions & 3 deletions vimode/src/cmds/undo.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@

#include "context.h"

void undo_start(CmdContext *c);
void undo_update(CmdContext *c, gint pos, gboolean multiline);
void undo_end(CmdContext *c);
void undo_update(CmdContext *c, gint pos);
void undo_apply(CmdContext *c, gint num);

#endif
2 changes: 0 additions & 2 deletions vimode/src/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ typedef struct

/* cursor position to restore after undo */
gint undo_pos;
/* start of line */
gboolean undo_is_sol;
} CmdContext;

#endif
9 changes: 0 additions & 9 deletions vimode/src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,3 @@ void goto_nonempty(ScintillaObject *sci, gint line, gboolean scroll)
pos = NEXT(sci, pos);
SET_POS(sci, pos, scroll);
}


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 ? TRUE : FALSE;
}
2 changes: 0 additions & 2 deletions vimode/src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,4 @@ void perform_substitute(ScintillaObject *sci, const gchar *cmd, gint from, gint

gint get_line_number_rel(ScintillaObject *sci, gint shift);

gboolean is_start_of_line(ScintillaObject *sci, gint pos);

#endif
13 changes: 3 additions & 10 deletions vimode/src/vi.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ CmdContext ctx =
FALSE, FALSE,
0, 1,
"", 0,
-1, FALSE
-1
};


Expand Down Expand Up @@ -309,15 +309,8 @@ 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))
{
gboolean is_sol = FALSE;

if (nt->length > 1 && is_start_of_line(sci, nt->position))
is_sol = TRUE;

undo_update(&ctx, nt->position, is_sol);
}
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.
Expand Down

0 comments on commit 63b7e25

Please sign in to comment.