Compare commits
4 Commits
bbd23187bc
...
e8eaa2366a
Author | SHA1 | Date | |
---|---|---|---|
e8eaa2366a | |||
12d8f6a931 | |||
5d0c105b10 | |||
33a8e26efc |
62
sdn.cpp
62
sdn.cpp
@ -415,7 +415,8 @@ enum { ALT = 1 << 24, SYM = 1 << 25 }; // Outside the range of Unicode
|
||||
XX(CHDIR) XX(PARENT) XX(GO_START) XX(GO_HOME) \
|
||||
XX(SEARCH) XX(RENAME) XX(RENAME_PREFILL) \
|
||||
XX(TOGGLE_FULL) XX(REVERSE_SORT) XX(SHOW_HIDDEN) XX(REDRAW) XX(RELOAD) \
|
||||
XX(INPUT_ABORT) XX(INPUT_CONFIRM) XX(INPUT_B_DELETE)
|
||||
XX(INPUT_ABORT) XX(INPUT_CONFIRM) XX(INPUT_B_DELETE) XX(INPUT_DELETE) \
|
||||
XX(INPUT_BACKWARD) XX(INPUT_FORWARD) XX(INPUT_BEGINNING) XX(INPUT_END)
|
||||
|
||||
#define XX(name) ACTION_ ## name,
|
||||
enum action { ACTIONS(XX) ACTION_COUNT };
|
||||
@ -452,7 +453,11 @@ static map<wint_t, action> g_input_actions {
|
||||
{L'\r', ACTION_INPUT_CONFIRM}, {KEY (ENTER), ACTION_INPUT_CONFIRM},
|
||||
// Sometimes terminfo is wrong, we need to accept both of these
|
||||
{L'\b', ACTION_INPUT_B_DELETE}, {CTRL ('?'), ACTION_INPUT_B_DELETE},
|
||||
{KEY (BACKSPACE), ACTION_INPUT_B_DELETE},
|
||||
{KEY (BACKSPACE), ACTION_INPUT_B_DELETE}, {KEY (DC), ACTION_INPUT_DELETE},
|
||||
{CTRL ('B'), ACTION_INPUT_BACKWARD}, {KEY (LEFT), ACTION_INPUT_BACKWARD},
|
||||
{CTRL ('F'), ACTION_INPUT_FORWARD}, {KEY (RIGHT), ACTION_INPUT_FORWARD},
|
||||
{CTRL ('A'), ACTION_INPUT_BEGINNING}, {KEY (HOME), ACTION_INPUT_BEGINNING},
|
||||
{CTRL ('E'), ACTION_INPUT_END}, {KEY (END), ACTION_INPUT_END},
|
||||
};
|
||||
static const map<string, map<wint_t, action>*> g_binding_contexts {
|
||||
{"normal", &g_normal_actions}, {"input", &g_input_actions},
|
||||
@ -523,6 +528,7 @@ static struct {
|
||||
|
||||
const wchar_t *editor; ///< Prompt string for editing
|
||||
wstring editor_line; ///< Current user input
|
||||
int editor_cursor = 0; ///< Cursor position
|
||||
void (*editor_on_change) (); ///< Callback on editor change
|
||||
void (*editor_on_confirm) (); ///< Callback on editor confirmation
|
||||
|
||||
@ -742,8 +748,11 @@ fun update () {
|
||||
curs_set (0);
|
||||
if (g.editor) {
|
||||
move (LINES - 1, 0);
|
||||
auto p = apply_attrs (wstring (g.editor) + L": ", 0);
|
||||
move (LINES - 1, print (p + apply_attrs (g.editor_line, 0), COLS - 1));
|
||||
auto prompt = apply_attrs (wstring (g.editor) + L": ", 0);
|
||||
auto line = apply_attrs (g.editor_line, 0);
|
||||
print (prompt + line, COLS - 1);
|
||||
auto start = sanitize (prompt + line.substr (0, g.editor_cursor));
|
||||
move (LINES - 1, compute_width (start));
|
||||
curs_set (1);
|
||||
} else if (!g.message.empty ()) {
|
||||
move (LINES - 1, 0);
|
||||
@ -1175,20 +1184,56 @@ fun handle_editor (wint_t c) {
|
||||
g.editor_on_confirm ();
|
||||
// Fall-through
|
||||
case ACTION_INPUT_ABORT:
|
||||
g.editor_line.clear ();
|
||||
g.editor = 0;
|
||||
g.editor_line.clear ();
|
||||
g.editor_cursor = 0;
|
||||
g.editor_on_change = nullptr;
|
||||
g.editor_on_confirm = nullptr;
|
||||
break;
|
||||
case ACTION_INPUT_BEGINNING:
|
||||
g.editor_cursor = 0;
|
||||
break;
|
||||
case ACTION_INPUT_END:
|
||||
g.editor_cursor = g.editor_line.length ();
|
||||
break;
|
||||
case ACTION_INPUT_BACKWARD:
|
||||
while (g.editor_cursor > 0) {
|
||||
if (--g.editor_cursor <= 0
|
||||
|| wcwidth (g.editor_line.at (g.editor_cursor)))
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case ACTION_INPUT_FORWARD:
|
||||
while (g.editor_cursor < int (g.editor_line.length ())) {
|
||||
if (++g.editor_cursor >= int (g.editor_line.length ())
|
||||
|| wcwidth (g.editor_line.at (g.editor_cursor)))
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case ACTION_INPUT_B_DELETE:
|
||||
if (!g.editor_line.empty ())
|
||||
g.editor_line.erase (g.editor_line.length () - 1);
|
||||
// Remove the last character including its postfix combining characters
|
||||
while (g.editor_cursor > 0) {
|
||||
auto erased = g.editor_line.at (--g.editor_cursor);
|
||||
g.editor_line.erase (g.editor_cursor, 1);
|
||||
if (wcwidth (erased))
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case ACTION_INPUT_DELETE:
|
||||
// Remove the next character including its postfix combining characters
|
||||
while (g.editor_cursor < int (g.editor_line.length ())) {
|
||||
g.editor_line.erase (g.editor_cursor, 1);
|
||||
if (g.editor_cursor >= int (g.editor_line.length ())
|
||||
|| wcwidth (g.editor_line.at (g.editor_cursor)))
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (c & (ALT | SYM)) {
|
||||
beep ();
|
||||
} else {
|
||||
g.editor_line += c;
|
||||
g.editor_line.insert (g.editor_cursor, 1, c);
|
||||
g.editor_cursor++;
|
||||
if (g.editor_on_change)
|
||||
g.editor_on_change ();
|
||||
}
|
||||
@ -1312,6 +1357,7 @@ fun handle (wint_t c) -> bool {
|
||||
break;
|
||||
case ACTION_RENAME_PREFILL:
|
||||
g.editor_line = to_wide (current.filename);
|
||||
g.editor_cursor = g.editor_line.length ();
|
||||
// Fall-through
|
||||
case ACTION_RENAME:
|
||||
g.editor = L"rename";
|
||||
|
Loading…
x
Reference in New Issue
Block a user