Rewrite the way moving around works

It seemed like a good thing at first to combine all the movement stuff
in a single function, however as soon as you want to move multiple lines
at once it becomes very difficult to do that, especially if you aren't
familiar with C.

This commit splits it into 4 separate functions, where the argument
(arg->i) is how many times we move. This means it is now possible to
choose how many lines to move without even editing the function itself,
just through keybinds. This also makes the "fastmove" keybindings
redundant.

Note that calcoffsets() and drawmenu() after that must be called AFTER the for loop added with this commit, otherwise
you can see the selection moving from each line to the next which is
rather ugly.
This commit is contained in:
speedie 2023-03-20 18:05:53 +01:00
parent 13d6ba9065
commit b59f851fdc
3 changed files with 108 additions and 101 deletions

View file

@ -62,12 +62,12 @@ static Key keys[] = {
{ 0, 0, XK_t, toggleimg, {0} },
{ 0, 0, XK_h, flipimg, {.i = 1 } },
{ 0, 0, XK_v, flipimg, {.i = 0 } },
{ 0, 0, XK_k, move, {.i = 1 } },
{ 0, 0, XK_j, move, {.i = 2 } },
{ 0, 0, XK_h, move, {.i = 3 } },
{ 0, 0, XK_l, move, {.i = 4 } },
{ 0, CONTROL, XK_u, fastmoveup, {.i = 5 } },
{ 0, CONTROL, XK_d, fastmovedown, {.i = 5 } },
{ 0, 0, XK_k, moveup, {0} },
{ 0, 0, XK_j, movedown, {0} },
{ 0, 0, XK_h, moveleft, {0} },
{ 0, 0, XK_l, moveright, {0} },
{ 0, CONTROL, XK_u, moveup, {.i = 5 } },
{ 0, CONTROL, XK_d, movedown, {.i = 5 } },
{ 0, CONTROL, XK_k, setlines, {.i = +1 } },
{ 0, CONTROL, XK_j, setlines, {.i = -1 } },
{ 0, CONTROL, XK_h, setcolumns, {.i = +1 } },

View file

@ -1,106 +1,112 @@
void
move(const Arg *arg)
moveleft(const Arg *arg)
{
struct item *tmpsel;
int i, offscreen = 0;
int argu = arg->i ? arg->i : 1;
if (columns > 1) {
if (!sel)
return;
tmpsel = sel;
for (i = 0; i < lines; i++) {
if (!tmpsel->left || tmpsel->left->right != tmpsel) {
if (offscreen)
drawmenu();
return;
}
if (tmpsel == curr)
offscreen = 1;
tmpsel = tmpsel->left;
}
sel = tmpsel;
if (offscreen) {
for (int j = 0; j < argu; j++) {
curr = prev;
}
}
drawmenu();
calcoffsets();
}
if (cursor > 0 && (!sel || !sel->left || lines > 0)) {
cursor = nextrune(-1);
drawmenu();
}
}
void
moveright(const Arg *arg)
{
struct item *tmpsel;
int i, offscreen = 0;
int argu = arg->i ? arg->i : 1;
if (columns > 1) {
if (!sel)
return;
tmpsel = sel;
for (i = 0; i < lines; i++) {
if (!tmpsel->right || tmpsel->right->left != tmpsel) {
if (offscreen)
drawmenu();
return;
}
tmpsel = tmpsel->right;
if (tmpsel == next)
offscreen = 1;
}
sel = tmpsel;
if (offscreen) {
for (int j = 0; j < argu; j++)
curr = next;
}
calcoffsets();
}
drawmenu();
if (text[cursor] != '\0') {
cursor = nextrune(+1);
drawmenu();
}
}
void
movedown(const Arg *arg)
{
struct item *tmpsel;
int i, offscreen = 0;
int argu = arg->i ? arg->i : 1;
for (int j = 0; j < argu; j++) {
if (sel && sel->right && (sel = sel->right) == next) {
curr = next;
}
}
calcoffsets();
drawmenu();
}
void
moveup(const Arg *arg)
{
struct item *tmpsel;
int i, offscreen = 0;
if (arg->i == 3) { // left
if (columns > 1) {
if (!sel)
return;
tmpsel = sel;
for (i = 0; i < lines; i++) {
if (!tmpsel->left || tmpsel->left->right != tmpsel) {
if (offscreen)
drawmenu();
return;
}
if (tmpsel == curr)
offscreen = 1;
tmpsel = tmpsel->left;
}
sel = tmpsel;
if (offscreen) {
curr = prev;
calcoffsets();
}
drawmenu();
}
int argu = arg->i ? arg->i : 1;
if (cursor > 0 && (!sel || !sel->left || lines > 0)) {
cursor = nextrune(-1);
drawmenu();
}
if (lines > 0)
return;
} else if (arg->i == 4) { // right
if (columns > 1) {
if (!sel)
return;
tmpsel = sel;
for (i = 0; i < lines; i++) {
if (!tmpsel->right || tmpsel->right->left != tmpsel) {
if (offscreen)
drawmenu();
return;
}
tmpsel = tmpsel->right;
if (tmpsel == next)
offscreen = 1;
}
sel = tmpsel;
if (offscreen) {
curr = next;
calcoffsets();
}
drawmenu();
}
if (text[cursor] != '\0') {
cursor = nextrune(+1);
drawmenu();
}
if (lines > 0)
return;
} else if (arg->i == 2) { // down
if (sel && sel->right && (sel = sel->right) == next) {
curr = next;
calcoffsets();
}
drawmenu();
} else if (arg->i == 1) { // up
for (int j = 0; j < argu; j++) {
if (sel && sel->left && (sel = sel->left)->right == curr) {
curr = prev;
calcoffsets();
}
drawmenu();
}
}
void
fastmoveup(const Arg *arg)
{
for (int i = 0; i < arg->i; i++) {
if (sel && sel->left && (sel = sel->left)->right == curr) {
curr = prev;
}
}
calcoffsets();
drawmenu();
}
void
fastmovedown(const Arg *arg)
{
for (int i = 0; i < arg->i; i++) {
if (sel && sel->right && (sel = sel->right) == next) {
curr = next;
}
}
calcoffsets();
calcoffsets();
drawmenu();
}

View file

@ -6,7 +6,10 @@ typedef union {
} Arg;
// declare keybind functions
static void move(const Arg *arg);
static void moveup(const Arg *arg);
static void movedown(const Arg *arg);
static void moveleft(const Arg *arg);
static void moveright(const Arg *arg);
static void moveend(const Arg *arg);
static void movestart(const Arg *arg);
static void movenext(const Arg *arg);
@ -33,5 +36,3 @@ static void setimgpos(const Arg *arg);
static void setimggaps(const Arg *arg);
static void setlines(const Arg *arg);
static void setcolumns(const Arg *arg);
static void fastmoveup(const Arg *arg);
static void fastmovedown(const Arg *arg);