Skip to content

Commit f45f939

Browse files
committed
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.
1 parent 8fda035 commit f45f939

File tree

3 files changed

+17
-36
lines changed

3 files changed

+17
-36
lines changed

src/MacVim/MMBackend.m

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,10 +1357,10 @@ - (NSString *)evaluateExpression:(in bycopy NSString *)expr
13571357
return eval;
13581358
}
13591359

1360-
/// Extracts the text currently selected in visual mode, and return them in str/len.
1360+
/// Extracts the text currently selected in visual mode, and returns it.
13611361
///
1362-
/// @return the motion type (e.g. blockwise), or -1 for failure.
1363-
static int extractSelectedText(char_u **str, long_u *len)
1362+
/// @return the string representing the selected text, or NULL if failure.
1363+
static char_u *extractSelectedText()
13641364
{
13651365
// Note: Most of the functionality in Vim that allows for extracting useful
13661366
// text from a selection are in the register & clipboard utility functions.
@@ -1372,7 +1372,7 @@ static int extractSelectedText(char_u **str, long_u *len)
13721372

13731373
if (!(VIsual_active && (State & MODE_NORMAL))) {
13741374
// This only works when we are in visual mode and have stuff to select.
1375-
return -1;
1375+
return NULL;
13761376
}
13771377

13781378
// 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)
14231423
ca.retval = CA_NO_ADJ_OP_END;
14241424
do_pending_operator(&ca, 0, TRUE);
14251425

1426-
// Step 3: Convert the yank register to a single piece of useful text. This
1427-
// will handle all the edge cases of different modes (e.g. blockwise, etc).
1428-
const int convert_result = clip_convert_selection(str, len, NULL);
1426+
// Step 3: Extract the text from the yank ('0') register.
1427+
char_u *str = get_reg_contents(0, 0);
14291428

14301429
// Step 4: Clean up the yank register, and restore it back.
14311430
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)
14471446

14481447
unblock_autocmds();
14491448

1450-
return convert_result;
1449+
return str;
14511450
}
14521451

14531452
/// Extract the currently selected text (in visual mode) and send that to the
@@ -1460,23 +1459,19 @@ - (BOOL)selectedTextToPasteboard:(byref NSPasteboard *)pboard
14601459
if (!pboard)
14611460
return YES;
14621461

1463-
long_u llen = 0; char_u *str = 0;
1464-
int type = extractSelectedText(&str, &llen);
1465-
if (type < 0)
1462+
char_u *str = extractSelectedText();
1463+
if (!str)
14661464
return NO;
14671465

1468-
// TODO: Avoid overflow.
1469-
int len = (int)llen;
14701466
if (output_conv.vc_type != CONV_NONE) {
1471-
char_u *conv_str = string_convert(&output_conv, str, &len);
1467+
char_u *conv_str = string_convert(&output_conv, str, NULL);
14721468
if (conv_str) {
14731469
vim_free(str);
14741470
str = conv_str;
14751471
}
14761472
}
14771473

1478-
NSString *string = [[NSString alloc]
1479-
initWithBytes:str length:len encoding:NSUTF8StringEncoding];
1474+
NSString *string = [[NSString alloc] initWithUTF8String:(char*)str];
14801475

14811476
NSArray *types = [NSArray arrayWithObject:NSStringPboardType];
14821477
[pboard declareTypes:types owner:nil];

src/MacVim/MMWindowController.m

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,13 +1685,11 @@ - (NSTabViewItem *)addNewTabViewItem
16851685
/// Ask Vim to fill in the pasteboard with the currently selected text in visual mode.
16861686
- (BOOL)askBackendForSelectedText:(NSPasteboard *)pb
16871687
{
1688-
// We use a dedicated API for this instead of just using something like
1689-
// evaluateExpression because there's a fair bit of logic in Vim that
1690-
// figures out how to convert from a visual selection to an externally
1691-
// presentable text in the clipboard code. Part of the complexity has to do
1692-
// with the three modes (character/blockwise/linewise) that visual mode can
1693-
// be in. We would like to reuse that code, instead of hacking it via some
1694-
// expression evaluation results.
1688+
// This could potentially be done via evaluateExpression by yanking the
1689+
// selection, then returning the results via getreg('@') and restoring the
1690+
// register. Using a dedicated API is probably a little safer (e.g. it
1691+
// prevents TextYankPost autocmd's from triggering) and efficient
1692+
// and hence this is what we use for now.
16951693
BOOL reply = NO;
16961694
id backendProxy = [vimController backendProxy];
16971695

src/clipboard.c

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2114,19 +2114,7 @@ clip_convert_selection(char_u **str, long_u *len, Clipboard_T *cbd)
21142114
int_u eolsize;
21152115
yankreg_T *y_ptr;
21162116

2117-
if (!cbd)
2118-
{
2119-
// MacVim extension: This makes this function much more useful as we
2120-
// can now extract usable texts from any registers for use instead of
2121-
// being forced to go through the system clipboard. This is useful for
2122-
// features that expose selected texts (e.g. system services) without
2123-
// polluting the system clipboard.
2124-
int unname_register = get_unname_register();
2125-
if (unname_register < 0)
2126-
return -1;
2127-
y_ptr = get_y_register(unname_register);
2128-
}
2129-
else if (cbd == &clip_plus)
2117+
if (cbd == &clip_plus)
21302118
y_ptr = get_y_register(PLUS_REGISTER);
21312119
else
21322120
y_ptr = get_y_register(STAR_REGISTER);

0 commit comments

Comments
 (0)