...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 | 650 | history[#history + 1] = text |
| 651 | 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 | 664 | local function handle_edit() |
| 654 | 665 | if selectable_items then |
| 655 | 666 | matches = {} |
| @@ -664,14 +675,7 @@ local function handle_edit() · 8 unmodified lines | ||
| 664 | 675 | return |
| 665 | 676 | end |
| 666 | 677 | |
| 667 | − -- Don't show completions after a command is entered because they move its | |
| 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 | |
| 678 | + handle_cursor_move() | |
| 675 | 679 | |
| 676 | 680 | if input_caller then |
| 677 | 681 | mp.commandv('script-message-to', input_caller, 'input-event', 'edited', |
| @@ -714,15 +718,13 @@ end · 36 unmodified lines | ||
| 714 | 718 | -- Move the cursor to the next character (Right) |
| 715 | 719 | local function next_char() |
| 716 | 720 | cursor = next_utf8(line, cursor) |
| 717 | − suggestion_buffer = {} | |
| 718 | − update() | |
| 721 | + handle_cursor_move() | |
| 719 | 722 | end |
| 720 | 723 | |
| 721 | 724 | -- Move the cursor to the previous character (Left) |
| 722 | 725 | local function prev_char() |
| 723 | 726 | cursor = prev_utf8(line, cursor) |
| 724 | − suggestion_buffer = {} | |
| 725 | − update() | |
| 727 | + handle_cursor_move() | |
| 726 | 728 | end |
| 727 | 729 | |
| 728 | 730 | -- Clear the current line (Ctrl+C) |
| @@ -801,7 +803,7 @@ local function handle_enter() · 72 unmodified lines | ||
| 801 | 803 | line = #matches > 0 and matches[selected_match].text or '' |
| 802 | 804 | cursor = #line + 1 |
| 803 | 805 | log_buffers[id] = {} |
| 804 | − update() | |
| 806 | + handle_edit() | |
| 805 | 807 | unbind_mouse() |
| 806 | 808 | return |
| 807 | 809 | end |
| @@ -918,8 +920,7 @@ local function go_history(new_pos) · 110 unmodified lines | ||
| 918 | 920 | end |
| 919 | 921 | cursor = line:len() + 1 |
| 920 | 922 | insert_mode = false |
| 921 | − suggestion_buffer = {} | |
| 922 | − update() | |
| 923 | + handle_edit() | |
| 923 | 924 | end |
| 924 | 925 | |
| 925 | 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 | 1036 | -- string in order to do a "backwards" find. This wouldn't be as annoying |
| 1036 | 1037 | -- to do if Lua didn't insist on 1-based indexing. |
| 1037 | 1038 | cursor = line:len() - select(2, line:reverse():find('%s*[^%s]*', line:len() - cursor + 2)) + 1 |
| 1038 | − suggestion_buffer = {} | |
| 1039 | − update() | |
| 1039 | + handle_cursor_move() | |
| 1040 | 1040 | end |
| 1041 | 1041 | |
| 1042 | 1042 | -- Move to the end of the current word, or if already at the end, the end of |
| 1043 | 1043 | -- the next word. (Ctrl+Right) |
| 1044 | 1044 | local function next_word() |
| 1045 | 1045 | cursor = select(2, line:find('%s*[^%s]*', cursor)) + 1 |
| 1046 | − suggestion_buffer = {} | |
| 1047 | − update() | |
| 1046 | + handle_cursor_move() | |
| 1048 | 1047 | end |
| 1049 | 1048 | |
| 1050 | 1049 | -- Move the cursor to the beginning of the line (HOME) |
| 1051 | 1050 | local function go_home() |
| 1052 | 1051 | cursor = 1 |
| 1053 | − suggestion_buffer = {} | |
| 1054 | − update() | |
| 1052 | + handle_cursor_move() | |
| 1055 | 1053 | end |
| 1056 | 1054 | |
| 1057 | 1055 | -- Move the cursor to the end of the line (END) |
| 1058 | 1056 | local function go_end() |
| 1059 | 1057 | cursor = line:len() + 1 |
| 1060 | − suggestion_buffer = {} | |
| 1061 | − update() | |
| 1058 | + handle_cursor_move() | |
| 1062 | 1059 | end |
| 1063 | 1060 | |
| 1064 | 1061 | -- Delete from the cursor to the beginning of the word (Ctrl+Backspace) |