Skip to content

Commit

Permalink
Added basic mouse support
Browse files Browse the repository at this point in the history
  • Loading branch information
rgburke committed Nov 15, 2016
1 parent 586d8ea commit 5ca469a
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 23 deletions.
8 changes: 5 additions & 3 deletions buffer.c
Expand Up @@ -785,7 +785,7 @@ static void bf_default_movement_selection_handler(Buffer *buffer, int is_select,
}
}

Status bf_set_bp(Buffer *buffer, const BufferPos *pos)
Status bf_set_bp(Buffer *buffer, const BufferPos *pos, int is_select)
{
assert(pos->data == buffer->data);
assert(pos->offset <= gb_length(pos->data));
Expand All @@ -797,7 +797,9 @@ Status bf_set_bp(Buffer *buffer, const BufferPos *pos)

buffer->pos = *pos;

if (bf_selection_started(buffer)) {
if (is_select) {
bf_select_continue(buffer);
} else if (bf_selection_started(buffer)) {
bf_select_reset(buffer);
}

Expand Down Expand Up @@ -2330,7 +2332,7 @@ Status bf_jump_to_matching_bracket(Buffer *buffer)

BufferPos pos = bp_init_from_offset(offset, &buffer->pos);

return bf_set_bp(buffer, &pos);
return bf_set_bp(buffer, &pos, 0);
}

/* Duplicate line or selected lines underneath */
Expand Down
2 changes: 1 addition & 1 deletion buffer.h
Expand Up @@ -136,7 +136,7 @@ int bf_bp_at_screen_line_start(const Buffer *, const BufferPos *);
int bf_bp_at_screen_line_end(const Buffer *, const BufferPos *);
int bf_bp_move_past_buffer_extremes(const BufferPos *, Direction);
int bf_selection_started(const Buffer *);
Status bf_set_bp(Buffer *, const BufferPos *);
Status bf_set_bp(Buffer *, const BufferPos *, int is_select);
Status bf_change_line(Buffer *, BufferPos *, Direction, int is_cursor);
Status bf_change_multi_line(Buffer *, BufferPos *, Direction,
size_t offset, int is_cursor);
Expand Down
28 changes: 28 additions & 0 deletions buffer_view.c
Expand Up @@ -883,3 +883,31 @@ void bv_apply_cell_attributes(BufferView *bv, CellAttribute attr,
}
}
}

int bv_convert_screen_pos_to_buffer_pos(const BufferView *bv,
size_t *row_ptr, size_t *col_ptr)
{
size_t row = *row_ptr;
size_t col = *col_ptr;

if (row >= bv->rows || col >= bv->cols) {
return 0;
}

const BufferPos *screen_start = &bv->screen_start;
size_t line_no = screen_start->line_no;

for (size_t k = 0; k < bv->rows && k <= row; k++) {
Line *line = &bv->lines[k];

if (line->line_no > 0) {
line_no = line->line_no;
}
}

*row_ptr = line_no;
*col_ptr = MAX(1, bv->lines[row].cells[col].col_no);

return 1;
}

2 changes: 2 additions & 0 deletions buffer_view.h
Expand Up @@ -109,5 +109,7 @@ size_t bv_screen_col_no(const struct Buffer *, const BufferPos *);
void bv_apply_cell_attributes(BufferView *, CellAttribute attr,
CellAttribute exclude_cell_attr);
void bv_free_syntax_match_cache(BufferView *);
int bv_convert_screen_pos_to_buffer_pos(const BufferView *,
size_t *row_ptr, size_t *col_ptr);

#endif
33 changes: 28 additions & 5 deletions command.c
Expand Up @@ -91,6 +91,7 @@ static Status cm_buffer_vert_move_lines(const CommandArgs *);
static Status cm_buffer_duplicate_selection(const CommandArgs *);
static Status cm_buffer_join_lines(const CommandArgs *);
static Status cm_buffer_indent(const CommandArgs *);
static Status cm_buffer_mouse_click(const CommandArgs *);
static Status cm_buffer_save_file(const CommandArgs *);
static Status cm_buffer_save_as(const CommandArgs *);
static Status cm_save_file_prompt(Session *, char **file_path_ptr);
Expand Down Expand Up @@ -156,6 +157,7 @@ static const CommandDefinition cm_commands[] = {
[CMD_BP_TO_BUFFER_START] = { NULL , cm_bp_to_buffer_start , CMDSIG(1, VAL_TYPE_INT) , CMDT_BUFFER_MOVE, NULL, NULL },
[CMD_BP_TO_BUFFER_END] = { NULL , cm_bp_to_buffer_end , CMDSIG(1, VAL_TYPE_INT) , CMDT_BUFFER_MOVE, NULL, NULL },
[CMD_BP_GOTO_MATCHING_BRACKET] = { NULL , cm_bp_goto_matching_bracket , CMDSIG_NO_ARGS , CMDT_BUFFER_MOVE, NULL, NULL },
[CMD_BUFFER_MOUSE_CLICK] = { NULL , cm_buffer_mouse_click , CMDSIG_NO_ARGS , CMDT_BUFFER_MOD, NULL, NULL },
[CMD_BUFFER_INSERT_CHAR] = { NULL , cm_buffer_insert_char , CMDSIG(1, VAL_TYPE_STR) , CMDT_BUFFER_MOD, NULL, NULL },
[CMD_BUFFER_INDENT] = { NULL , cm_buffer_indent , CMDSIG(1, VAL_TYPE_INT) , CMDT_BUFFER_MOD, NULL, NULL },
[CMD_BUFFER_DELETE_CHAR] = { NULL , cm_buffer_delete_char , CMDSIG_NO_ARGS , CMDT_BUFFER_MOD, NULL, NULL },
Expand Down Expand Up @@ -240,6 +242,7 @@ static const OperationDefinition cm_operations[] = {
[OP_MOVE_SELECT_BUFFER_START] = { "<wed-move-select-buffer-start>", OM_NORMAL, { INT_VAL_STRUCT(DIRECTION_WITH_SELECT) }, 1, CMD_BP_TO_BUFFER_START, "Select to start of file" },
[OP_MOVE_SELECT_BUFFER_END] = { "<wed-move-select-buffer-end>", OM_NORMAL, { INT_VAL_STRUCT(DIRECTION_WITH_SELECT) }, 1, CMD_BP_TO_BUFFER_END, "Select to end of file" },
[OP_MOVE_MATCHING_BRACKET] = { "<wed-move-matching-bracket>", OM_NORMAL, CMD_NO_ARGS, 0, CMD_BP_GOTO_MATCHING_BRACKET, "Move to matching bracket" },
[OP_MOVE_TO_CLICK_POSITION] = { "<wed-mouse-click>", OM_NORMAL, CMD_NO_ARGS, 0, CMD_BUFFER_MOUSE_CLICK, "Move to mouse click position" },
[OP_INDENT] = { "<wed-indent>", OM_NORMAL, { INT_VAL_STRUCT(DIRECTION_RIGHT) }, 1, CMD_BUFFER_INDENT, "Indent selected text" },
[OP_UNINDENT] = { "<wed-unindent>", OM_NORMAL, { INT_VAL_STRUCT(DIRECTION_LEFT) }, 1, CMD_BUFFER_INDENT, "Unindent selected text" },
[OP_DELETE] = { "<wed-delete>", OM_NORMAL, CMD_NO_ARGS, 0, CMD_BUFFER_DELETE_CHAR, "Delete next character" },
Expand Down Expand Up @@ -1076,6 +1079,26 @@ static Status cm_buffer_indent(const CommandArgs *cmd_args)
return bf_insert_character(sess->active_buffer, "\t", 1);
}

static Status cm_buffer_mouse_click(const CommandArgs *cmd_args)
{
Status status = STATUS_SUCCESS;
Session *sess = cmd_args->sess;
Buffer *buffer = sess->active_buffer;
MouseClickEvent mouse_click = se_get_last_mouse_click_event(sess);
BufferPos new_pos = bp_init_from_line_col(mouse_click.row,
mouse_click.col,
&buffer->pos);

if (mouse_click.type == MCT_PRESS) {
status = bf_set_bp(buffer, &new_pos, 0);
} else if (mouse_click.type == MCT_RELEASE) {
bf_select_continue(buffer);
status = bf_set_bp(buffer, &new_pos, 1);
}

return status;
}

static Status cm_buffer_save_file(const CommandArgs *cmd_args)
{
Session *sess = cmd_args->sess;
Expand Down Expand Up @@ -1351,7 +1374,7 @@ static Status cm_buffer_find_next(const CommandArgs *cmd_args)
se_add_msg(sess, "Search wrapped");
}

status = bf_set_bp(buffer, &buffer->search.last_match_pos);
status = bf_set_bp(buffer, &buffer->search.last_match_pos, 0);

if (STATUS_IS_SUCCESS(status)) {
bf_select_continue(buffer);
Expand Down Expand Up @@ -1528,7 +1551,7 @@ static Status cm_buffer_replace(const CommandArgs *cmd_args)

if (found_match) {
match_num++;
status = bf_set_bp(buffer, &search->last_match_pos);
status = bf_set_bp(buffer, &search->last_match_pos, 0);

if (!STATUS_IS_SUCCESS(status)) {
break;
Expand Down Expand Up @@ -1556,7 +1579,7 @@ static Status cm_buffer_replace(const CommandArgs *cmd_args)

BufferPos buffer_start = buffer->pos;
bp_to_buffer_start(&buffer_start);
status = bf_set_bp(buffer, &buffer_start);
status = bf_set_bp(buffer, &buffer_start, 0);

if (!STATUS_IS_SUCCESS(status)) {
break;
Expand Down Expand Up @@ -2383,13 +2406,13 @@ static Status cm_buffer_filter(const CommandArgs *cmd_args)
GOTO_IF_FAIL(status, cleanup);

BufferPos write_pos = bos.write_pos;
bf_set_bp(buffer, &write_pos);
bf_set_bp(buffer, &write_pos, 0);
bf_to_buffer_end(buffer, 1);
status = bf_delete(buffer, 0);
GOTO_IF_FAIL(status, cleanup);
orig_pos = bp_init_from_line_col(orig_pos.line_no, orig_pos.col_no,
&buffer->pos);
bf_set_bp(buffer, &orig_pos);
bf_set_bp(buffer, &orig_pos, 0);

if (!ec_cmd_successfull(cmd_status)) {
char *error = bf_to_string(err_buffer);
Expand Down
2 changes: 2 additions & 0 deletions command.h
Expand Up @@ -51,6 +51,7 @@ typedef enum {
CMD_BP_TO_BUFFER_START,
CMD_BP_TO_BUFFER_END,
CMD_BP_GOTO_MATCHING_BRACKET,
CMD_BUFFER_MOUSE_CLICK,
CMD_BUFFER_INSERT_CHAR,
CMD_BUFFER_INDENT,
CMD_BUFFER_DELETE_CHAR,
Expand Down Expand Up @@ -136,6 +137,7 @@ typedef enum {
OP_MOVE_SELECT_BUFFER_START,
OP_MOVE_SELECT_BUFFER_END,
OP_MOVE_MATCHING_BRACKET,
OP_MOVE_TO_CLICK_POSITION,
OP_INDENT,
OP_UNINDENT,
OP_DELETE,
Expand Down
22 changes: 21 additions & 1 deletion input.c
Expand Up @@ -131,6 +131,19 @@ static Status ip_add_keystr_input(InputBuffer *input_buffer, size_t pos,
return STATUS_SUCCESS;
}

Status ip_add_mouse_click_event(InputBuffer *input_buffer, const char *keystr,
size_t keystr_len, MouseClickType type,
size_t row, size_t col)
{
input_buffer->last_mouse_click = (MouseClickEvent) {
.type = type,
.row = row,
.col = col
};

return ip_add_keystr_input_to_end(input_buffer, keystr, keystr_len);
}

static int ip_input_available(const InputBuffer *input_buffer)
{
GapBuffer *buffer = input_buffer->buffer;
Expand Down Expand Up @@ -479,7 +492,8 @@ static void ip_handle_error(Session *sess)
static int ip_is_special_key(const TermKeyKey *key)
{
if (key->type == TERMKEY_TYPE_FUNCTION ||
key->type == TERMKEY_TYPE_KEYSYM) {
key->type == TERMKEY_TYPE_KEYSYM ||
key->type == TERMKEY_TYPE_MOUSE) {
return 1;
}

Expand Down Expand Up @@ -531,3 +545,9 @@ static void ip_get_monotonic_time(struct timespec *time)
clock_gettime(CLOCK_MONOTONIC, time);
#endif
}

MouseClickEvent ip_get_last_mouse_click_event(const InputBuffer *input_buffer)
{
return input_buffer->last_mouse_click;
}

22 changes: 16 additions & 6 deletions input.h
Expand Up @@ -25,13 +25,7 @@

struct Session;

typedef enum {
IT_FD,
IT_KEYSTR
} InputType;

typedef struct {
InputType input_type;
TermKey *termkey;
const char *keystr_input;
const char *iter;
Expand All @@ -49,11 +43,23 @@ typedef enum {
IR_EOF
} InputResult;

typedef enum {
MCT_PRESS,
MCT_RELEASE
} MouseClickType;

typedef struct {
MouseClickType type;
size_t row;
size_t col;
} MouseClickEvent;

typedef struct {
GapBuffer *buffer;
InputArgument arg;
InputResult result;
size_t wait_time_nano;
MouseClickEvent last_mouse_click;
} InputBuffer;

int ip_init(InputBuffer *);
Expand All @@ -64,5 +70,9 @@ Status ip_add_keystr_input_to_start(InputBuffer *, const char *keystr,
size_t keystr_len);
Status ip_add_keystr_input_to_end(InputBuffer *, const char *keystr,
size_t keystr_len);
Status ip_add_mouse_click_event(InputBuffer *, const char *keystr,
size_t keystr_len, MouseClickType,
size_t row, size_t col);
MouseClickEvent ip_get_last_mouse_click_event(const InputBuffer *);

#endif
5 changes: 5 additions & 0 deletions session.c
Expand Up @@ -1046,3 +1046,8 @@ void se_determine_filetypes_if_unset(Session *sess, Buffer *buffer)
}
}

MouseClickEvent se_get_last_mouse_click_event(const Session *sess)
{
return ip_get_last_mouse_click_event(&sess->input_buffer);
}

1 change: 1 addition & 0 deletions session.h
Expand Up @@ -129,5 +129,6 @@ int se_session_finished(const Session *);
void se_set_session_finished(Session *);
const char *se_get_file_type_display_name(const Session *, const Buffer *);
void se_determine_filetypes_if_unset(Session *, Buffer *);
MouseClickEvent se_get_last_mouse_click_event(const Session *);

#endif

0 comments on commit 5ca469a

Please sign in to comment.