Skip to content

Commit b3907e6

Browse files
committed
Make Cmd-E work and always share to OS Find Pasteboard
Cmd-E (Edit -> Use Selection for Find") in most macOS apps follow the convention that it shares to the system Find Pasteboard, while Cmd-G uses the Find Pasteboard to find the next item. Make Cmd-E always do that even if MMShareFindPboard is set to NO. This way, normal searches in Vim wouldn't pollute the system find pasteboard, but could still have a way to share to it by using this action.
1 parent 3df190c commit b3907e6

File tree

9 files changed

+70
-11
lines changed

9 files changed

+70
-11
lines changed

runtime/doc/gui_mac.txt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,18 @@ These are the non-standard commands that MacVim supports:
120120
|:macaction| |:macmenu|
121121

122122
*macvim-find*
123-
Whenever you search for something in Vim (e.g. using "/") the search query is
124-
copied to the macOS "Find Pasteboard". The idea is that if you search for
125-
something and switch to another application, then you can hit <D-g> (or <D-G>)
126-
to repeat the search in the new app. The same feature works if you search in
127-
some app, switch to MacVim and hit <D-g>.
123+
Whenever you search for something in Vim (e.g. using "/"), or hit <D-e> when
124+
you have text selected, the search query is copied to the macOS "Find
125+
Pasteboard". The idea is that if you search for something and switch to
126+
another application, then you can hit <D-g> (or <D-G>) to repeat the search in
127+
the new app. The same feature works if you search in some app, switch to
128+
MacVim and hit <D-g>.
128129

129130
If you would like to turn off sharing Vim's search query to the macOS Find
130-
Pasteboard, you can set |MMShareFindPboard| to "NO". <D-g> will still use the
131-
OS Find Pasteboard for searching though (use |n| instead if that's not what
132-
you want).
131+
Pasteboard, you can set |MMShareFindPboard| to "NO". Even when that's set,
132+
<D-g> will still use the OS Find Pasteboard for searching (use |n| instead if
133+
that's not what you want), and <D-e> ("Edit -> Use Selection for Find") will
134+
still share the search pattern to Find Pasteboard.
133135

134136
Note that the command |n| is not the same as <D-g>. The former will repeat
135137
the last search made in Vim, whereas the latter searches for the string on the
@@ -445,6 +447,8 @@ Action Description ~
445447
fileOpen: Show "File Open" dialog
446448
findNext: Search forward using the "Find Pasteboard"
447449
findPrevious: Search backward using the "Find Pasteboard"
450+
useSelectionForFind: Search the selected text and share to "Find
451+
Pasteboard"
448452
fontSizeDown: Decrease font size
449453
fontSizeUp: Increase font size
450454
hide: Hide MacVim

runtime/menu.vim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ elseif has("gui_macvim")
231231
vnoremenu <silent> &Edit.Find.Find\.\.\. y:promptfind <C-R>=<SID>FixFText()<CR><CR>
232232
an 20.410.20 &Edit.Find.Find\ Next <Nop>
233233
an 20.410.30 &Edit.Find.Find\ Previous <Nop>
234-
vnoremenu 20.410.35 &Edit.Find.Use\ Selection\ for\ Find y:let @/=@"<CR>:<BS>
234+
vnoremenu 20.410.35 &Edit.Find.Use\ Selection\ for\ Find <Nop>
235235
else
236236
an 20.410 &Edit.&Find<Tab>/ /
237237
an 20.420 &Edit.Find\ and\ Rep&lace<Tab>:%s :%s/
@@ -1261,7 +1261,7 @@ if has("gui_macvim")
12611261
macm Edit.Find.Find\.\.\. key=<D-f>
12621262
macm Edit.Find.Find\ Next key=<D-g> action=findNext:
12631263
macm Edit.Find.Find\ Previous key=<D-G> action=findPrevious:
1264-
macm Edit.Find.Use\ Selection\ for\ Find key=<D-e>
1264+
macm Edit.Find.Use\ Selection\ for\ Find key=<D-e> action=useSelectionForFind:
12651265
macm Edit.Font.Show\ Fonts action=orderFrontFontPanel:
12661266
macm Edit.Font.Bigger key=<D-=> action=fontSizeUp:
12671267
macm Edit.Font.Smaller key=<D--> action=fontSizeDown:

src/MacVim/Actions.plist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
<string></string>
2323
<key>findPrevious:</key>
2424
<string></string>
25+
<key>useSelectionForFind:</key>
26+
<string></string>
2527
<key>fontSizeDown:</key>
2628
<string></string>
2729
<key>fontSizeUp:</key>

src/MacVim/MMBackend.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ extern NSTimeInterval MMBalloonEvalInternalDelay;
5858
BOOL imState;
5959
int winposX;
6060
int winposY;
61+
BOOL addToFindPboardOverride;
6162
#ifdef FEAT_BEVAL
6263
NSString *lastToolTip;
6364
#endif
@@ -155,6 +156,9 @@ extern NSTimeInterval MMBalloonEvalInternalDelay;
155156
- (void)setWaitForAck:(BOOL)yn;
156157
- (void)waitForConnectionAcknowledgement;
157158

159+
- (BOOL)addToFindPboardOverride;
160+
- (void)clearAddToFindPboardOverride;
161+
158162
- (BOOL)imState;
159163
- (void)setImState:(BOOL)activated;
160164

src/MacVim/MMBackend.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ - (int)checkForModifiedBuffers;
193193
- (void)addInput:(NSString *)input;
194194
- (void)redrawScreen;
195195
- (void)handleFindReplace:(NSDictionary *)args;
196+
- (void)useSelectionForFind;
196197
- (void)handleMarkedText:(NSData *)data;
197198
- (void)handleGesture:(NSData *)data;
198199
#ifdef FEAT_BEVAL
@@ -254,6 +255,8 @@ - (id)init
254255
ASLogNotice(@"Failed to load dictionaries.%@", MMSymlinkWarningString);
255256
}
256257

258+
addToFindPboardOverride = NO;
259+
257260
return self;
258261
}
259262

@@ -1701,6 +1704,16 @@ - (oneway void)acknowledgeConnection
17011704
waitForAck = NO;
17021705
}
17031706

1707+
- (BOOL)addToFindPboardOverride
1708+
{
1709+
return addToFindPboardOverride;
1710+
}
1711+
1712+
- (void)clearAddToFindPboardOverride
1713+
{
1714+
addToFindPboardOverride = NO;
1715+
}
1716+
17041717
- (BOOL)imState
17051718
{
17061719
return imState;
@@ -2072,6 +2085,8 @@ - (void)handleInputEvent:(int)msgid data:(NSData *)data
20722085
[self handleOpenWithArguments:[NSDictionary dictionaryWithData:data]];
20732086
} else if (FindReplaceMsgID == msgid) {
20742087
[self handleFindReplace:[NSDictionary dictionaryWithData:data]];
2088+
} else if (UseSelectionForFindMsgID == msgid) {
2089+
[self useSelectionForFind];
20752090
} else if (ZoomMsgID == msgid) {
20762091
if (!data) return;
20772092
const void *bytes = [data bytes];
@@ -2967,6 +2982,21 @@ - (void)handleFindReplace:(NSDictionary *)args
29672982
vim_free(replace);
29682983
}
29692984

2985+
- (void)useSelectionForFind
2986+
{
2987+
if (VIsual_active && (State & NORMAL)) {
2988+
// This happens when Cmd-E is pressed and is supposed to be consistent
2989+
// with normal macOS apps, so it always writes to the system find
2990+
// pasteboard, unlike normal searches where it could be turned off via
2991+
// MMShareFindPboard. Set an override to make sure it gets shared out.
2992+
// Since gui_macvim_add_to_find_pboard is going to get called after
2993+
// this returns it will be responsible for calling
2994+
// clearAddToFindPboardOverride to clean up.
2995+
addToFindPboardOverride = YES;
2996+
[self addInput:@"y:let @/=@\"<CR>:<BS>"];
2997+
}
2998+
}
2999+
29703000

29713001
- (void)handleMarkedText:(NSData *)data
29723002
{

src/MacVim/MMWindowController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
- (IBAction)performClose:(id)sender;
103103
- (IBAction)findNext:(id)sender;
104104
- (IBAction)findPrevious:(id)sender;
105+
- (IBAction)useSelectionForFind:(id)sender;
105106
- (IBAction)vimMenuItemAction:(id)sender;
106107
- (IBAction)vimToolbarItemAction:(id)sender;
107108
- (IBAction)fontSizeUp:(id)sender;

src/MacVim/MMWindowController.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,11 @@ - (IBAction)findPrevious:(id)sender
877877
[self doFindNext:NO];
878878
}
879879

880+
- (IBAction)useSelectionForFind:(id)sender
881+
{
882+
[vimController sendMessage:UseSelectionForFindMsgID data:nil];
883+
}
884+
880885
- (IBAction)vimMenuItemAction:(id)sender
881886
{
882887
if (![sender isKindOfClass:[NSMenuItem class]]) return;

src/MacVim/MacVim.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ enum {
234234
SetFullScreenColorMsgID,
235235
ShowFindReplaceDialogMsgID,
236236
FindReplaceMsgID,
237+
UseSelectionForFindMsgID,
237238
ActivateKeyScriptMsgID,
238239
DeactivateKeyScriptMsgID,
239240
EnableImControlMsgID,

src/MacVim/gui_macvim.m

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1870,7 +1870,19 @@
18701870
{
18711871
if (!pat) return;
18721872

1873-
if (!MMShareFindPboard) return;
1873+
if ([MMBackend sharedInstance].addToFindPboardOverride) {
1874+
// MMBackend would set addToFindPboardOverride to YES when we are
1875+
// using the useSelectionFodFind: macaction, accessible via Cmd-E,
1876+
// which indicates we always want to share to the find pasteboard
1877+
// unlike normal search. Since this function is called after the action
1878+
// is handled we need to manually clear the override so it doesn't
1879+
// persist in later searches.
1880+
[[MMBackend sharedInstance] clearAddToFindPboardOverride];
1881+
} else {
1882+
if (!MMShareFindPboard) {
1883+
return;
1884+
}
1885+
}
18741886

18751887
#ifdef FEAT_MBYTE
18761888
pat = CONVERT_TO_UTF8(pat);

0 commit comments

Comments
 (0)