Implement readline-style Ctrl-U and Ctrl-Y

develop
lethosor 2014-08-09 20:10:34 -04:00
parent e71fc0455c
commit 71236afa42
1 changed files with 19 additions and 6 deletions

@ -85,7 +85,7 @@ static int isUnsupportedTerm(void)
if (term == NULL) return 0; if (term == NULL) return 0;
for (j = 0; unsupported_term[j]; j++) for (j = 0; unsupported_term[j]; j++)
if (!strcasecmp(term,unsupported_term[j])) return 1; if (!strcasecmp(term,unsupported_term[j])) return 1;
return 0; return 0;
} }
const char * ANSI_CLS = "\033[2J"; const char * ANSI_CLS = "\033[2J";
@ -648,15 +648,27 @@ namespace DFHack
prompt_refresh(); prompt_refresh();
} }
break; break;
case 21: // Ctrl+u, delete the whole line. case 21: // Ctrl+u, delete from current to beginning of line.
raw_buffer.clear(); if (raw_cursor > 0)
yank_buffer = raw_buffer.substr(0, raw_cursor);
raw_buffer.erase(0, raw_cursor);
raw_cursor = 0; raw_cursor = 0;
prompt_refresh(); prompt_refresh();
break; break;
case 11: // Ctrl+k, delete from current to end of line. case 11: // Ctrl+k, delete from current to end of line.
if (raw_cursor < raw_buffer.size())
yank_buffer = raw_buffer.substr(raw_cursor, raw_buffer.size() - raw_cursor);
raw_buffer.erase(raw_cursor); raw_buffer.erase(raw_cursor);
prompt_refresh(); prompt_refresh();
break; break;
case 25: // Ctrl+y, paste last text deleted with Ctrl+u/k
if (yank_buffer.size())
{
raw_buffer.insert(raw_cursor, yank_buffer);
raw_cursor += yank_buffer.size();
prompt_refresh();
}
break;
case 1: // Ctrl+a, go to the start of the line case 1: // Ctrl+a, go to the start of the line
raw_cursor = 0; raw_cursor = 0;
prompt_refresh(); prompt_refresh();
@ -684,9 +696,10 @@ namespace DFHack
con_lineedit con_lineedit
} state; } state;
bool in_batch; bool in_batch;
std::string prompt; // current prompt string std::string prompt; // current prompt string
std::string raw_buffer; // current raw mode buffer std::string raw_buffer; // current raw mode buffer
int raw_cursor; // cursor position in the buffer std::string yank_buffer; // last text deleted with Ctrl-K/Ctrl-U
int raw_cursor; // cursor position in the buffer
// thread exit mechanism // thread exit mechanism
int exit_pipe[2]; int exit_pipe[2];
fd_set descriptor_set; fd_set descriptor_set;