...and after navigating the history. I thought of making the new function accept the new cursor position and set it, but it would not be clear that you're only supposed to call it when handle_edit() is not already called.
Unified · Split
| @@ -650,6 +650,17 @@ local function history_add(text) · 649 unmodified lines | |||
| 650 | history[#history + 1] = text | 650 | history[#history + 1] = text |
| 651 | end | 651 | end |
| 652 | 652 | ||
| 653 | local function handle_cursor_move() | ||
| 654 | -- Don't show completions after a command is entered because they move its | ||
| 655 | -- output up, and allow clearing completions by emptying the line. | ||
| 656 | if line == '' then | ||
| 657 | suggestion_buffer = {} | ||
| 658 | update() | ||
| 659 | else | ||
| 660 | complete() | ||
| 661 | end | ||
| 662 | end | ||
| 663 | |||
| 653 | local function handle_edit() | 664 | local function handle_edit() |
| 654 | if selectable_items then | 665 | if selectable_items then |
| 655 | matches = {} | 666 | matches = {} |
| @@ -664,14 +675,7 @@ local function handle_edit() · 8 unmodified lines | |||
| 664 | return | 675 | return |
| 665 | end | 676 | end |
| 666 | 677 | ||
| 667 | -- Don't show completions after a command is entered because they move its | 678 | handle_cursor_move() |
| 668 | -- output up, and allow clearing completions by emptying the line. | ||
| 669 | if line == '' then | ||
| 670 | suggestion_buffer = {} | ||
| 671 | update() | ||
| 672 | else | ||
| 673 | complete() | ||
| 674 | end | ||
| 675 | 679 | ||
| 676 | if input_caller then | 680 | if input_caller then |
| 677 | mp.commandv('script-message-to', input_caller, 'input-event', 'edited', | 681 | mp.commandv('script-message-to', input_caller, 'input-event', 'edited', |
| @@ -714,15 +718,13 @@ end · 36 unmodified lines | |||
| 714 | -- Move the cursor to the next character (Right) | 718 | -- Move the cursor to the next character (Right) |
| 715 | local function next_char() | 719 | local function next_char() |
| 716 | cursor = next_utf8(line, cursor) | 720 | cursor = next_utf8(line, cursor) |
| 717 | suggestion_buffer = {} | 721 | handle_cursor_move() |
| 718 | update() | ||
| 719 | end | 722 | end |
| 720 | 723 | ||
| 721 | -- Move the cursor to the previous character (Left) | 724 | -- Move the cursor to the previous character (Left) |
| 722 | local function prev_char() | 725 | local function prev_char() |
| 723 | cursor = prev_utf8(line, cursor) | 726 | cursor = prev_utf8(line, cursor) |
| 724 | suggestion_buffer = {} | 727 | handle_cursor_move() |
| 725 | update() | ||
| 726 | end | 728 | end |
| 727 | 729 | ||
| 728 | -- Clear the current line (Ctrl+C) | 730 | -- Clear the current line (Ctrl+C) |
| @@ -801,7 +803,7 @@ local function handle_enter() · 72 unmodified lines | |||
| 801 | line = #matches > 0 and matches[selected_match].text or '' | 803 | line = #matches > 0 and matches[selected_match].text or '' |
| 802 | cursor = #line + 1 | 804 | cursor = #line + 1 |
| 803 | log_buffers[id] = {} | 805 | log_buffers[id] = {} |
| 804 | update() | 806 | handle_edit() |
| 805 | unbind_mouse() | 807 | unbind_mouse() |
| 806 | return | 808 | return |
| 807 | end | 809 | end |
| @@ -918,8 +920,7 @@ local function go_history(new_pos) · 110 unmodified lines | |||
| 918 | end | 920 | end |
| 919 | cursor = line:len() + 1 | 921 | cursor = line:len() + 1 |
| 920 | insert_mode = false | 922 | insert_mode = false |
| 921 | suggestion_buffer = {} | 923 | handle_edit() |
| 922 | update() | ||
| 923 | end | 924 | end |
| 924 | 925 | ||
| 925 | -- Go to the specified relative position in the command history (Up, Down) | 926 | -- Go to the specified relative position in the command history (Up, Down) |
| @@ -1035,30 +1036,26 @@ local function prev_word() · 109 unmodified lines | |||
| 1035 | -- string in order to do a "backwards" find. This wouldn't be as annoying | 1036 | -- string in order to do a "backwards" find. This wouldn't be as annoying |
| 1036 | -- to do if Lua didn't insist on 1-based indexing. | 1037 | -- to do if Lua didn't insist on 1-based indexing. |
| 1037 | cursor = line:len() - select(2, line:reverse():find('%s*[^%s]*', line:len() - cursor + 2)) + 1 | 1038 | cursor = line:len() - select(2, line:reverse():find('%s*[^%s]*', line:len() - cursor + 2)) + 1 |
| 1038 | suggestion_buffer = {} | 1039 | handle_cursor_move() |
| 1039 | update() | ||
| 1040 | end | 1040 | end |
| 1041 | 1041 | ||
| 1042 | -- Move to the end of the current word, or if already at the end, the end of | 1042 | -- Move to the end of the current word, or if already at the end, the end of |
| 1043 | -- the next word. (Ctrl+Right) | 1043 | -- the next word. (Ctrl+Right) |
| 1044 | local function next_word() | 1044 | local function next_word() |
| 1045 | cursor = select(2, line:find('%s*[^%s]*', cursor)) + 1 | 1045 | cursor = select(2, line:find('%s*[^%s]*', cursor)) + 1 |
| 1046 | suggestion_buffer = {} | 1046 | handle_cursor_move() |
| 1047 | update() | ||
| 1048 | end | 1047 | end |
| 1049 | 1048 | ||
| 1050 | -- Move the cursor to the beginning of the line (HOME) | 1049 | -- Move the cursor to the beginning of the line (HOME) |
| 1051 | local function go_home() | 1050 | local function go_home() |
| 1052 | cursor = 1 | 1051 | cursor = 1 |
| 1053 | suggestion_buffer = {} | 1052 | handle_cursor_move() |
| 1054 | update() | ||
| 1055 | end | 1053 | end |
| 1056 | 1054 | ||
| 1057 | -- Move the cursor to the end of the line (END) | 1055 | -- Move the cursor to the end of the line (END) |
| 1058 | local function go_end() | 1056 | local function go_end() |
| 1059 | cursor = line:len() + 1 | 1057 | cursor = line:len() + 1 |
| 1060 | suggestion_buffer = {} | 1058 | handle_cursor_move() |
| 1061 | update() | ||
| 1062 | end | 1059 | end |
| 1063 | 1060 | ||
| 1064 | -- Delete from the cursor to the beginning of the word (Ctrl+Backspace) | 1061 | -- Delete from the cursor to the beginning of the word (Ctrl+Backspace) |