From b6980eda52b7e74879362b470ce9f013179f2aa7 Mon Sep 17 00:00:00 2001 From: Yee Cheng Chin Date: Sun, 2 Oct 2022 01:17:31 -0700 Subject: [PATCH] Fix code to use getreg() instead I just missed the fact that getreg() also works and I don't need to hack to use clip_convert_selection() and the mod on it. This is kind of... odd, because clip_convert_selection() is essentially doing the same thing as getreg() with *very* minor differences. You would hope that would be consolidated and refactored into a single function, but of course not. Also cleaned up some comments to make it clear that evaluateExpression probably would have worked except for the autocmd blocking part. --- src/MacVim/MMBackend.m | 27 +++++++++++---------------- src/MacVim/MMWindowController.m | 12 +++++------- src/clipboard.c | 14 +------------- 3 files changed, 17 insertions(+), 36 deletions(-) diff --git a/src/MacVim/MMBackend.m b/src/MacVim/MMBackend.m index 8e503caac2..0dcfe43bd5 100644 --- a/src/MacVim/MMBackend.m +++ b/src/MacVim/MMBackend.m @@ -1357,10 +1357,10 @@ - (NSString *)evaluateExpression:(in bycopy NSString *)expr return eval; } -/// Extracts the text currently selected in visual mode, and return them in str/len. +/// Extracts the text currently selected in visual mode, and returns it. /// -/// @return the motion type (e.g. blockwise), or -1 for failure. -static int extractSelectedText(char_u **str, long_u *len) +/// @return the string representing the selected text, or NULL if failure. +static char_u *extractSelectedText() { // Note: Most of the functionality in Vim that allows for extracting useful // text from a selection are in the register & clipboard utility functions. @@ -1372,7 +1372,7 @@ static int extractSelectedText(char_u **str, long_u *len) if (!(VIsual_active && (State & MODE_NORMAL))) { // This only works when we are in visual mode and have stuff to select. - return -1; + return NULL; } // Step 1: Find a register to yank the selection to. If we don't do this we @@ -1423,9 +1423,8 @@ static int extractSelectedText(char_u **str, long_u *len) ca.retval = CA_NO_ADJ_OP_END; do_pending_operator(&ca, 0, TRUE); - // Step 3: Convert the yank register to a single piece of useful text. This - // will handle all the edge cases of different modes (e.g. blockwise, etc). - const int convert_result = clip_convert_selection(str, len, NULL); + // Step 3: Extract the text from the yank ('0') register. + const char_u *str = get_reg_contents(0, 0); // Step 4: Clean up the yank register, and restore it back. set_y_current(target_reg); // should not be necessary as it's done in do_pending_operator above (since regname was set to 0), but just to be safe and verbose in intention. @@ -1447,7 +1446,7 @@ static int extractSelectedText(char_u **str, long_u *len) unblock_autocmds(); - return convert_result; + return str; } /// Extract the currently selected text (in visual mode) and send that to the @@ -1460,23 +1459,19 @@ - (BOOL)selectedTextToPasteboard:(byref NSPasteboard *)pboard if (!pboard) return YES; - long_u llen = 0; char_u *str = 0; - int type = extractSelectedText(&str, &llen); - if (type < 0) + char_u *str = extractSelectedText(); + if (!str) return NO; - // TODO: Avoid overflow. - int len = (int)llen; if (output_conv.vc_type != CONV_NONE) { - char_u *conv_str = string_convert(&output_conv, str, &len); + char_u *conv_str = string_convert(&output_conv, str, NULL); if (conv_str) { vim_free(str); str = conv_str; } } - NSString *string = [[NSString alloc] - initWithBytes:str length:len encoding:NSUTF8StringEncoding]; + NSString *string = [[NSString alloc] initWithUTF8String:str]; NSArray *types = [NSArray arrayWithObject:NSStringPboardType]; [pboard declareTypes:types owner:nil]; diff --git a/src/MacVim/MMWindowController.m b/src/MacVim/MMWindowController.m index 298fe8dd9e..571b2770eb 100644 --- a/src/MacVim/MMWindowController.m +++ b/src/MacVim/MMWindowController.m @@ -1685,13 +1685,11 @@ - (NSTabViewItem *)addNewTabViewItem /// Ask Vim to fill in the pasteboard with the currently selected text in visual mode. - (BOOL)askBackendForSelectedText:(NSPasteboard *)pb { - // We use a dedicated API for this instead of just using something like - // evaluateExpression because there's a fair bit of logic in Vim that - // figures out how to convert from a visual selection to an externally - // presentable text in the clipboard code. Part of the complexity has to do - // with the three modes (character/blockwise/linewise) that visual mode can - // be in. We would like to reuse that code, instead of hacking it via some - // expression evaluation results. + // This could potentially be done via evaluateExpression by yanking the + // selection, then returning the results via getreg('@') and restoring the + // register. Using a dedicated API is probably a little safer (e.g. it + // prevents TextYankPost autocmd's from triggering) and efficient + // and hence this is what we use for now. BOOL reply = NO; id backendProxy = [vimController backendProxy]; diff --git a/src/clipboard.c b/src/clipboard.c index 78abdeddda..5e97a5fc64 100644 --- a/src/clipboard.c +++ b/src/clipboard.c @@ -2114,19 +2114,7 @@ clip_convert_selection(char_u **str, long_u *len, Clipboard_T *cbd) int_u eolsize; yankreg_T *y_ptr; - if (!cbd) - { - // MacVim extension: This makes this function much more useful as we - // can now extract usable texts from any registers for use instead of - // being forced to go through the system clipboard. This is useful for - // features that expose selected texts (e.g. system services) without - // polluting the system clipboard. - int unname_register = get_unname_register(); - if (unname_register < 0) - return -1; - y_ptr = get_y_register(unname_register); - } - else if (cbd == &clip_plus) + if (cbd == &clip_plus) y_ptr = get_y_register(PLUS_REGISTER); else y_ptr = get_y_register(STAR_REGISTER);