Skip to content

Commit 2928f77

Browse files
committed
Support dictionary/data lookups of text
This adds support for looking up data under the mouse cursor. Usually it will bring up a dictionary, but other times it could be a Wikipedia article, Siri knowledge, etc. Apple doesn't really have a good name for it, other than "looking up data", "quick look" (a confusingly similar name with the other Quick Look OS feature), or "show definition". You can activate this by doing Ctrl-Cmd-D when the mouse is over a cursor. If you have a trackpad, you can also either activate this using Force click or three-finger tap (depends on your system preference settings). Note that for Force click, this could potentially make it impossible to use the MacVim `<ForceClick>` mapping in Vim, which allows you to map a force click to a Vim command (#716). This is handled by having a new setting (under a new "Input" preference pane which will have more populated later) that allows you to choose whether to use Force click for data lookup or Vim's `<ForceClick>` mapping. If you have configured to use three-finger taps though this setting wouldn't do anything, and `<ForceClick>` is always send to the Vim mapping. Also, this is lacking a lot of features that a normal macOS application would get, e.g. looking up selected texts (e.g. if you have "ice cream", you may want to select the whole thing to look up the phrase, rather than just "ice" or "cream"), data detector, and much more (e.g. custom API support). They will be done later as part of #1311. Technical details below: The way the OS knows how to look up the data and present it is by talking to the NSTextInput/NSTextInputClient. Previously MacVim implemented NSTextInput partially, and didn't implement the critical firstRectForCharacterRange:actualRange and characterIndexForPoint: functions. First, in this change we change from NSTextInput to NSTextInputClient (which is the newer non-deprecated version), and implement those functions, which allows the OS to query the text storage. By default, the OS sends a quickLookWithEvent: call to us whenever the lookup happens but for some odd reason this isn't automatic for Force clicks, presumably because some apps want to handle Force clicks manually (this is why some apps only work for three-finger taps but not Force clicks for lookups). This isn't documented but I found references in iTerm/Firefox, and basically we just need to manually handle it and send off quickLookWithEvent: when handling Force clicks. For implementing the NSTextInputClient properly, the main issue is making sure that can work properly with input methods / marked texts, which is the other primary purpose for this class (other than inputting keys). For data lookups, I'm representing the grid as a row-major text (with no newline/space in between) and expose that to the OS. This already has some issue because it doesn't handle Vim vertical splits well, as MacVim doesn't really have access to detailed Vim text buffers easily (unless we do a lot of calls back-and-forth). This means wrapped texts won't be looked up properly, which I think is ok. Also, the OS APIs deal with UTF-8 indices, so we can't just convert row/column to raw indices and have to do a lot of character length calculations (especially for wide chars like CJK or emojis) to make sure the returned ranges are consistent and valid. For marked texts though, this presents a challenge because Vim doesn't really have a strong enough API to communicate back-and-forth about the marked positions and whatnot (it only let the GUI know where the current cursor is), and it's hard to implement APIs like `markedRange` properly because some marked texts could be hidden or wrapped (if you implement some of these functions improperly Apple's input methods could start misbehaving especially when you use arrow keys to navigate). In the end I kept the original implementation for treating the marked texts as a range starting from 0, *only* when we have marked text. Kind of a hack but this makes sure we work both in marked text mode (i.e. when inputting texts) and when doing lookups. For simplicity I made it so that you can't do data lookups when in marked text mode now. Data detection: Note that the default implementation is quite bare, and lacks a lot of smart data detection. For example, if you put your mouse over a URL, it won't properly select the whole URL, and addresses and dates for example also won't get grouped together properly. This is because these require additional implementation (e.g. using NSDataDetector) instead of coming "for free", and will be handled later. In fact, Apple's WebKit and NSTextView cheats by calling an internal API framework called "Reveal" (which you can find out by intercepting NSTextView's calls and/or looking at WebKit's source code) which is much more powerful and supports looking up package tracking, airline info, and more, but it's not available to third-party software (that's why Safari's lookup is so much better than Chrome/Firefox's). This isn't tested right now. Future task needs to add XCTest support to properly test this as there are a lot of edge cases involved here. Fix #1191 Part of Epic #1311, which contains other items to be implemented.
1 parent bb4c48a commit 2928f77

12 files changed

+558
-55
lines changed

runtime/doc/gui_mac.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ as general information regarding macOS user defaults.
259259
Here is a list of relevant dictionary entries:
260260

261261
KEY VALUE ~
262+
*MMAllowForceClickLookUp* use Force click for data lookup instead of
263+
<ForceClick> [bool]
262264
*MMCellWidthMultiplier* width of a normal glyph in em units [float]
263265
*MMCmdLineAlignBottom* Pin command-line to bottom of MacVim [bool]
264266
*MMDialogsTrackPwd* open/save dialogs track the Vim pwd [bool]
@@ -783,11 +785,17 @@ Each gesture generates one of the following Vim pseudo keys:
783785

784786
*<SwipeUp>* *<SwipeDown>*
785787
Generated when swiping three fingers across the trackpad in a
786-
vertical direction. (Not supported by the Apple Magic Mouse.)
788+
vertical direction. (Not supported by the Apple Magic Mouse)
787789

788790
*<ForceClick>*
789791
Generated when doing a Force click by pressing hard on a trackpad.
790-
(Only supported on trackpads that support Force Touch.)
792+
(Only supported on trackpads that support Force Touch)
793+
794+
If you have configured to use Force click for "Look up & data
795+
detectors" in the system settings, by default MacVim will do a
796+
dictionary lookup instead of triggering this mapping. You can turn
797+
this off in MacVim's Preference pane, or directly set
798+
|MMAllowForceClickLookUp|.
791799

792800
You can map these keys like with any other key using the |:map| family of
793801
commands. For example, the following commands map left/right swipe to change

runtime/doc/tags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5417,6 +5417,7 @@ LogiPat-flags pi_logipat.txt /*LogiPat-flags*
54175417
Lua if_lua.txt /*Lua*
54185418
M motion.txt /*M*
54195419
MDI starting.txt /*MDI*
5420+
MMAllowForceClickLookUp gui_mac.txt /*MMAllowForceClickLookUp*
54205421
MMAppearanceModeSelection gui_mac.txt /*MMAppearanceModeSelection*
54215422
MMCellWidthMultiplier gui_mac.txt /*MMCellWidthMultiplier*
54225423
MMCmdLineAlignBottom gui_mac.txt /*MMCmdLineAlignBottom*

src/MacVim/Base.lproj/Preferences.xib

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
<customObject id="-2" userLabel="File's Owner" customClass="MMPreferenceController">
1010
<connections>
1111
<outlet property="advancedPreferences" destination="620" id="632"/>
12+
<outlet property="allowForceClickLookUpButton" destination="rlt-zw-mfW" id="xCv-HS-zyJ"/>
1213
<outlet property="appearancePreferences" destination="hr4-G4-3ZG" id="G54-DD-ACh"/>
1314
<outlet property="autoInstallUpdateButton" destination="UYM-W0-Kgl" id="cX5-tk-9WJ"/>
1415
<outlet property="generalPreferences" destination="115" id="143"/>
16+
<outlet property="inputPreferences" destination="Bnq-Nx-GJH" id="FES-rQ-Fpa"/>
1517
<outlet property="layoutPopUpButton" destination="427" id="596"/>
1618
<outlet property="sparkleUpdaterPane" destination="0hT-y8-Hge" id="e0L-sv-OCW"/>
1719
</connections>
@@ -475,6 +477,40 @@
475477
</subviews>
476478
<point key="canvasLocation" x="137.5" y="435"/>
477479
</customView>
480+
<customView id="Bnq-Nx-GJH" userLabel="Input">
481+
<rect key="frame" x="0.0" y="0.0" width="483" height="58"/>
482+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
483+
<subviews>
484+
<customView id="DAP-Yi-QU0" userLabel="Trackpad">
485+
<rect key="frame" x="20" y="20" width="433" height="18"/>
486+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
487+
<subviews>
488+
<textField verticalHuggingPriority="750" horizontalCompressionResistancePriority="250" id="f18-Wr-EgZ">
489+
<rect key="frame" x="-2" y="0.0" width="187" height="17"/>
490+
<autoresizingMask key="autoresizingMask" flexibleMinY="YES"/>
491+
<textFieldCell key="cell" sendsActionOnEndEditing="YES" alignment="right" title="Trackpad:" id="jkp-Ls-ZhN">
492+
<font key="font" metaFont="system"/>
493+
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
494+
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
495+
</textFieldCell>
496+
</textField>
497+
<button id="rlt-zw-mfW" userLabel="Force click option">
498+
<rect key="frame" x="189" y="-1" width="213" height="18"/>
499+
<autoresizingMask key="autoresizingMask" flexibleMinY="YES"/>
500+
<string key="toolTip">Allow Force clicks to look up data when it is configured to do so (under "Trackpad" in System Settings). This will prevent &lt;ForceClick&gt; mappings in Vim from working. If you rely on &lt;ForceClick&gt; mappings, you may want to unset this option. This setting does not matter if you have configured to use three-finger taps to look up instead, as &lt;ForceClick&gt; will always work.</string>
501+
<buttonCell key="cell" type="check" title="Use Force click to look up data" bezelStyle="regularSquare" imagePosition="left" alignment="left" inset="2" id="A5o-Il-XdJ" userLabel="Force click option">
502+
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
503+
<font key="font" metaFont="system"/>
504+
</buttonCell>
505+
<connections>
506+
<binding destination="58" name="value" keyPath="values.MMAllowForceClickLookUp" id="sef-c3-KyZ"/>
507+
</connections>
508+
</button>
509+
</subviews>
510+
</customView>
511+
</subviews>
512+
<point key="canvasLocation" x="137.5" y="679"/>
513+
</customView>
478514
<customView id="620" userLabel="Advanced">
479515
<rect key="frame" x="0.0" y="0.0" width="483" height="264"/>
480516
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>

src/MacVim/MMAppController.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ + (void)initialize
256256
[NSNumber numberWithBool:YES], MMShareFindPboardKey,
257257
[NSNumber numberWithBool:NO], MMSmoothResizeKey,
258258
[NSNumber numberWithBool:NO], MMCmdLineAlignBottomKey,
259+
[NSNumber numberWithBool:YES], MMAllowForceClickLookUpKey,
259260
nil];
260261

261262
[[NSUserDefaults standardUserDefaults] registerDefaults:dict];

src/MacVim/MMCoreTextView.h

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,31 @@
1313
@class MMTextViewHelper;
1414

1515

16+
/// The main text view that manages drawing Vim's content using Core Text, and
17+
/// handles input. We are using this instead of NSTextView because of the
18+
/// custom needs in order to draw Vim's texts, as we don't have access to the
19+
/// full contents of Vim, and works more like a smart terminal to Vim.
20+
///
21+
/// Currently the rendering is done in software via Core Text, but a future
22+
/// extension will add support for Metal rendering which probably will require
23+
/// splitting this class up.
24+
///
25+
/// Since this class implements text rendering/input using a custom view, it
26+
/// implements NSTextInputClient, mostly for the following needs:
27+
/// 1. Text input. This is done via insertText / doCommandBySelector.
28+
/// 2. Input methods (e.g. for CJK). This is done via the marked text and the
29+
/// other APIs like selectedRange/firstRectForCharacterRange/etc.
30+
/// 3. Support native dictionary lookup (quickLookWithEvent:) when the user
31+
/// wants to. This mostly involves implementing the attributeSubstring /
32+
/// firstRectForCharacterRange / characterIndexForPoint APIs.
33+
/// There is an inherent difficulty to implementing NSTextInputClient
34+
/// 'correctly', because it assumes we have an entire text storage with
35+
/// indexable ranges. However, we don't have full access to Vim's internal
36+
/// storage, and we are represening the screen view instead in row-major
37+
/// indexing, but this becomes complicated when we want to implement marked
38+
/// texts. We the relevant parts for comments on how we hack around this.
1639
@interface MMCoreTextView : NSView <
17-
NSTextInput
40+
NSTextInputClient
1841
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_14
1942
, NSFontChanging
2043
, NSMenuItemValidation
@@ -122,8 +145,21 @@
122145
// NSTextView methods
123146
//
124147
- (void)keyDown:(NSEvent *)event;
125-
- (void)insertText:(id)string;
148+
149+
//
150+
// NSTextInputClient methods
151+
//
152+
- (void)insertText:(id)string replacementRange:(NSRange)replacementRange;
126153
- (void)doCommandBySelector:(SEL)selector;
154+
- (void)setMarkedText:(id)string selectedRange:(NSRange)selectedRange replacementRange:(NSRange)replacementRange;
155+
- (void)unmarkText;
156+
- (NSRange)selectedRange;
157+
- (NSRange)markedRange;
158+
- (BOOL)hasMarkedText;
159+
- (nullable NSAttributedString *)attributedSubstringForProposedRange:(NSRange)range actualRange:(nullable NSRangePointer)actualRange;
160+
- (nonnull NSArray<NSAttributedStringKey> *)validAttributesForMarkedText;
161+
- (NSRect)firstRectForCharacterRange:(NSRange)range actualRange:(nullable NSRangePointer)actualRange;
162+
- (NSUInteger)characterIndexForPoint:(NSPoint)point;
127163

128164
//
129165
// NSTextContainer methods

0 commit comments

Comments
 (0)