Skip to content

Commit

Permalink
Fix code to use getreg() instead
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ychin committed Oct 2, 2022
1 parent 8fda035 commit 61c2175
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 36 deletions.
27 changes: 11 additions & 16 deletions src/MacVim/MMBackend.m
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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.
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.
Expand All @@ -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
Expand All @@ -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];
Expand Down
12 changes: 5 additions & 7 deletions src/MacVim/MMWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down
14 changes: 1 addition & 13 deletions src/clipboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 61c2175

Please sign in to comment.