Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for prepending to the cutbuffer in addition to replacing it. #353

Merged
merged 9 commits into from Jan 26, 2016
51 changes: 36 additions & 15 deletions src/fe-text/gui-entry.c
Expand Up @@ -534,7 +534,7 @@ char *gui_entry_get_cutbuffer(GUI_ENTRY_REC *entry)
return buf;
}

void gui_entry_erase_to(GUI_ENTRY_REC *entry, int pos, int update_cutbuffer)
void gui_entry_erase_to(GUI_ENTRY_REC *entry, int pos, CUTBUFFER_UPDATE_OP update_cutbuffer)
{
int newpos, size = 0;

Expand All @@ -545,7 +545,7 @@ void gui_entry_erase_to(GUI_ENTRY_REC *entry, int pos, int update_cutbuffer)
gui_entry_erase(entry, size, update_cutbuffer);
}

void gui_entry_erase(GUI_ENTRY_REC *entry, int size, int update_cutbuffer)
void gui_entry_erase(GUI_ENTRY_REC *entry, int size, CUTBUFFER_UPDATE_OP update_cutbuffer)
{
size_t w = 0;

Expand All @@ -554,17 +554,39 @@ void gui_entry_erase(GUI_ENTRY_REC *entry, int size, int update_cutbuffer)
if (size == 0 || entry->pos < size)
return;

if (update_cutbuffer) {
/* put erased text to cutbuffer */
if (entry->cutbuffer_len < size) {
g_free(entry->cutbuffer);
entry->cutbuffer = g_new(unichar, size+1);
}
switch (update_cutbuffer) {
case CUTBUFFER_UPDATE_PREPEND:
if (entry->cutbuffer_len) {
int cutbuffer_new_size = entry->cutbuffer_len + size;
unichar *tmpcutbuffer = entry->cutbuffer;
entry->cutbuffer = g_new(unichar, cutbuffer_new_size+1);

memcpy(entry->cutbuffer, entry->text + entry->pos - size,
size * sizeof(unichar));
memcpy(entry->cutbuffer + size, tmpcutbuffer,

entry->cutbuffer_len * sizeof(unichar));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation got really weird in this line.

entry->cutbuffer_len = cutbuffer_new_size;
entry->cutbuffer[cutbuffer_new_size] = '\0';

g_free(tmpcutbuffer);
break;
}
/* fall through to REPLACE if cutbuffer_len was 0 */
case CUTBUFFER_UPDATE_REPLACE:
/* put erased text to cutbuffer */
if (entry->cutbuffer_len < size) {
g_free(entry->cutbuffer);
entry->cutbuffer = g_new(unichar, size+1);
}

entry->cutbuffer_len = size;
entry->cutbuffer[size] = '\0';
memcpy(entry->cutbuffer, entry->text + entry->pos - size,
size * sizeof(unichar));
entry->cutbuffer_len = size;
entry->cutbuffer[size] = '\0';
memcpy(entry->cutbuffer, entry->text + entry->pos - size,
size * sizeof(unichar));
break;
case CUTBUFFER_UPDATE_NOOP:
break;
}

if (entry->utf8)
Expand Down Expand Up @@ -601,7 +623,7 @@ void gui_entry_erase_cell(GUI_ENTRY_REC *entry)
gui_entry_draw(entry);
}

void gui_entry_erase_word(GUI_ENTRY_REC *entry, int to_space)
void gui_entry_erase_word(GUI_ENTRY_REC *entry, int to_space, CUTBUFFER_UPDATE_OP cutbuffer_op)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's up with this line being deleted? Mistake or somehow intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was not intentional. Fixed.

int to;

Expand All @@ -624,7 +646,6 @@ void gui_entry_erase_word(GUI_ENTRY_REC *entry, int to_space)
}
if (to > 0) to++;

gui_entry_erase(entry, entry->pos-to, TRUE);
}

void gui_entry_erase_next_word(GUI_ENTRY_REC *entry, int to_space)
Expand All @@ -650,7 +671,7 @@ void gui_entry_erase_next_word(GUI_ENTRY_REC *entry, int to_space)

size = to-entry->pos;
entry->pos = to;
gui_entry_erase(entry, size, TRUE);
gui_entry_erase(entry, size, CUTBUFFER_UPDATE_REPLACE);
}

void gui_entry_transpose_chars(GUI_ENTRY_REC *entry)
Expand Down
15 changes: 12 additions & 3 deletions src/fe-text/gui-entry.h
@@ -1,6 +1,8 @@
#ifndef __GUI_ENTRY_H
#define __GUI_ENTRY_H

#define CUTBUFFER_PREPEND 42
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this line is redundant now


typedef struct {
int text_len, text_alloc; /* as shorts, not chars */
unichar *text;
Expand All @@ -20,6 +22,12 @@ typedef struct {
unsigned int utf8:1;
} GUI_ENTRY_REC;

typedef enum {
CUTBUFFER_UPDATE_NOOP,
CUTBUFFER_UPDATE_REPLACE,
CUTBUFFER_UPDATE_PREPEND
} CUTBUFFER_UPDATE_OP;

extern GUI_ENTRY_REC *active_entry;

GUI_ENTRY_REC *gui_entry_create(int xpos, int ypos, int width, int utf8);
Expand All @@ -40,10 +48,10 @@ void gui_entry_insert_text(GUI_ENTRY_REC *entry, const char *str);
void gui_entry_insert_char(GUI_ENTRY_REC *entry, unichar chr);

char *gui_entry_get_cutbuffer(GUI_ENTRY_REC *entry);
void gui_entry_erase_to(GUI_ENTRY_REC *entry, int pos, int update_cutbuffer);
void gui_entry_erase(GUI_ENTRY_REC *entry, int size, int update_cutbuffer);
void gui_entry_erase_to(GUI_ENTRY_REC *entry, int pos, CUTBUFFER_UPDATE_OP update_cutbuffer);
void gui_entry_erase(GUI_ENTRY_REC *entry, int size, CUTBUFFER_UPDATE_OP update_cutbuffer);
void gui_entry_erase_cell(GUI_ENTRY_REC *entry);
void gui_entry_erase_word(GUI_ENTRY_REC *entry, int to_space);
void gui_entry_erase_word(GUI_ENTRY_REC *entry, int to_space, CUTBUFFER_UPDATE_OP cutbuffer_op);
void gui_entry_erase_next_word(GUI_ENTRY_REC *entry, int to_space);

void gui_entry_transpose_chars(GUI_ENTRY_REC *entry);
Expand All @@ -60,4 +68,5 @@ void gui_entry_move_words(GUI_ENTRY_REC *entry, int count, int to_space);

void gui_entry_redraw(GUI_ENTRY_REC *entry);


#endif
15 changes: 9 additions & 6 deletions src/fe-text/gui-readline.c
Expand Up @@ -68,6 +68,8 @@ static int paste_timeout_id;

static void sig_input(void);

static int key_repeated = FALSE;

void input_listen_init(int handle)
{
readtag = g_input_add_poll(handle,
Expand Down Expand Up @@ -361,6 +363,7 @@ static void sig_gui_key_pressed(gpointer keyp)
int ret;

key = GPOINTER_TO_INT(keyp);
key_repeated = key == prev_key;

if (redir != NULL && redir->flags & ENTRY_REDIRECT_FLAG_HOTKEY) {
handle_key_redirect(key);
Expand Down Expand Up @@ -527,15 +530,15 @@ static void key_forward_to_space(void)
static void key_erase_line(void)
{
gui_entry_set_pos(active_entry, active_entry->text_len);
gui_entry_erase(active_entry, active_entry->text_len, TRUE);
gui_entry_erase(active_entry, active_entry->text_len, CUTBUFFER_UPDATE_REPLACE);
}

static void key_erase_to_beg_of_line(void)
{
int pos;

pos = gui_entry_get_pos(active_entry);
gui_entry_erase(active_entry, pos, TRUE);
gui_entry_erase(active_entry, pos, CUTBUFFER_UPDATE_REPLACE);
}

static void key_erase_to_end_of_line(void)
Expand All @@ -544,7 +547,7 @@ static void key_erase_to_end_of_line(void)

pos = gui_entry_get_pos(active_entry);
gui_entry_set_pos(active_entry, active_entry->text_len);
gui_entry_erase(active_entry, active_entry->text_len - pos, TRUE);
gui_entry_erase(active_entry, active_entry->text_len - pos, CUTBUFFER_UPDATE_REPLACE);
}

static void key_yank_from_cutbuffer(void)
Expand Down Expand Up @@ -591,12 +594,12 @@ static void key_delete_character(void)

static void key_backspace(void)
{
gui_entry_erase(active_entry, 1, FALSE);
gui_entry_erase(active_entry, 1, CUTBUFFER_UPDATE_NOOP);
}

static void key_delete_previous_word(void)
{
gui_entry_erase_word(active_entry, FALSE);
gui_entry_erase_word(active_entry, FALSE, CUTBUFFER_UPDATE_REPLACE);
}

static void key_delete_next_word(void)
Expand All @@ -606,7 +609,7 @@ static void key_delete_next_word(void)

static void key_delete_to_previous_space(void)
{
gui_entry_erase_word(active_entry, TRUE);
gui_entry_erase_word(active_entry, TRUE, CUTBUFFER_UPDATE_REPLACE);
}

static void key_delete_to_next_space(void)
Expand Down