Skip to content

Commit

Permalink
Add a new key - w
Browse files Browse the repository at this point in the history
  • Loading branch information
coditva authored Nov 23, 2017
1 parent e14dd4f commit a8ea6eb
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 35 deletions.
8 changes: 0 additions & 8 deletions include/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,4 @@ void map_set_cursor(point_t);
void map_set_real_cursor();


/**
* Search the map for the given tile in the given direction from the cursor
* @param The tile to search for
* @param The step for increment
* @return The pointer to the tile
*/
point_t * map_search_tile(map_tile_t, int);

#endif /* end of include guard: MAP_H_K8P4JCPW */
52 changes: 49 additions & 3 deletions src/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,26 @@

#include <stdlib.h> /* for malloc(), NULL */
#include <string.h> /* for strcmp() */
#include <ctype.h> /* for isalnum() */

#include "datatypes.h"
#include "command.h"
#include "interface.h"
#include "map.h"


void increment_cursor(const map_t *map, point_t *point)
{
point -> x++;
if (point -> x >= map -> size.x) {
point -> x = 1;
point -> y++;
}
if (point -> y >= map -> size.y) {
point = NULL;
}
}

int get_count()
{
int count = 0;
Expand Down Expand Up @@ -77,6 +90,9 @@ int get_motion()
point_t * exec_motion(const map_t *map, const command_t *command)
{
point_t temp = { .y = map -> cursor.y, .x = map -> real_x };
map_tile_t tile;
int flag1 = 0;
int flag2 = 1;

switch (command -> motion.value) {
case 'j':
Expand All @@ -91,6 +107,30 @@ point_t * exec_motion(const map_t *map, const command_t *command)
case 'h':
temp.x = map -> cursor.x - 1;
break;

case 'w':
tile = map_get_tile(temp);
if (tile.type != TILE_TEXT) break;

/* how: at the start, flag is the 1 if the current tile is
* alphanumeric. we keep incrementing cursor till an opposite
* value is found. but if we encounter space between incrementing
* any non-space value is the stop point */
flag1 = isalnum(tile.value);
flag2 = 0; /* when any non-space works, it's 1 */
while (1) {
if ((flag1 != isalnum(tile.value) || flag2)
&& tile.value != ' ')
break;

increment_cursor(map, &temp);
/* &temp becomes NULL if end of map */
if (!&temp) break;
tile = map_get_tile(temp);

if (tile.value == ' ') flag2 = 1;
}
break;
}

if (temp.y == map -> cursor.y && !map_is_free(temp)) {
Expand Down Expand Up @@ -158,7 +198,12 @@ command_t * command_get()
command -> type = COMMAND_OTHER;
command -> count = get_count();
command -> oper = get_oper();
command -> motion.count = get_count();
if (command -> oper == 0) {
command -> motion.count = command -> count;
command -> count = 1;
} else {
command -> motion.count = get_count();
}
command -> motion.value = get_motion();

return command;
Expand All @@ -179,8 +224,9 @@ void command_exec(const map_t *map, const command_t *command)

point_t *end = exec_motion(map, command);
if (end) {
if (end -> y == map -> cursor.y
&& end -> x != map -> cursor.x) {
/* the real x value is not updated on using the j & k keys */
if (command -> motion.value != 'j'
&& command -> motion.value != 'k') {
map_set_cursor(*end);
map_set_real_cursor();
}
Expand Down
26 changes: 2 additions & 24 deletions src/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ map_tile_t map_get_tile(point_t point)
{
if (point.x <= map -> size.x && point.y <= map -> size.y)
return map -> data[convert_point_to_linear(point)];
return map -> data[0];
return map -> data[0]; /* TODO: RETURN ERROR */
}

void map_set_tile(point_t point, map_tile_t tile)
Expand All @@ -97,7 +97,7 @@ void map_set_tile(point_t point, map_tile_t tile)

int convert_point_to_linear(point_t point)
{
if (point.x <= map -> size.x && point.y <= map -> size.y)
if (point.x < map -> size.x && point.y < map -> size.y)
return map -> size.x * point.y + point.x;
return -1;
}
Expand Down Expand Up @@ -196,25 +196,3 @@ void map_set_real_cursor()
{
map -> real_x = map -> cursor.x;
}

point_t * map_search_tile(map_tile_t searchtile, int step)
{
map_tile_t tile;
point_t *point = (point_t *) malloc(sizeof(point_t));

point -> x = map -> cursor.x;
point -> y = map -> cursor.y;

while (point -> x < map -> size.x || point -> y < map -> size.y) {
tile = map_get_tile(*point);
if (tile.type == searchtile.type && tile.value == searchtile.value) {
return point;
}
point -> x += step;
if (point -> x > map -> size.x) {
point -> x = 0;
point -> y += (step > 0) ? 1 : -1;
}
}
return NULL;
}

0 comments on commit a8ea6eb

Please sign in to comment.