Skip to content

Commit

Permalink
Merge pull request #250 from bambu/macmeta
Browse files Browse the repository at this point in the history
Added macmeta support
  • Loading branch information
rogual committed Apr 30, 2016
2 parents 82b8384 + 28bd831 commit d6ef684
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 22 deletions.
5 changes: 5 additions & 0 deletions doc/nvim_dot_app.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ MacShowFontSelector()
Fonts chosen from the GUI font selector are stored to User Defaults, and
take effect on startup until overridden by a MacSetFont call.

MacSetOptionAsMeta(value)
If value is 1, pressing the option (alt) key will be interpreted as a
meta command. With this set any option hotkeys are bindable as <M-..>.
If the value is 0, all option keys are interpreted as normal (the default).

MacSetFontShouldAntialias(value)
If value is 1, enable font antialiasing (the default). If value is 0,
disable it. Calling with any other value is undefined behaviour.
Expand Down
4 changes: 4 additions & 0 deletions res/nvimrc
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ function! MacOpenFileInBufferOrNewTab(filename)
endif
endfunction

function! MacSetOptionAsMeta(value)
call rpcnotify(1, "neovim.app.setOptAsMeta", a:value)
endfunction

function! MacSetFontShouldAntialias(value)
call rpcnotify(1, "neovim.app.shouldAntialias", a:value)
endfunction
Expand Down
15 changes: 9 additions & 6 deletions src/input.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ - (void)keyDown:(NSEvent *)event
NSTextInputContext *con = [NSTextInputContext currentInputContext];
[NSCursor setHiddenUntilMouseMoves:YES];

/* When a deadkey is received on keydown the length is 0. Allow
NSTextInputContext to handle the key press */
if ([[event characters] length] == 0 || [self hasMarkedText])
/* When a deadkey is received the character length is 0. Allow
NSTextInputContext to handle the key press only if Macmeta is
not turned on */
if ([self hasMarkedText]) {
[con handleEvent:event];
else {
} else if (!mOptAsMeta && ([[event characters] length] == 0)) {
[con handleEvent:event];
} else {
std::stringstream raw;
translateKeyEvent(raw, event);
translateKeyEvent(raw, event, mOptAsMeta);
std::string raws = raw.str();

if (raws.size())
Expand All @@ -40,7 +43,7 @@ - (BOOL)performKeyEquivalent:(NSEvent *)event
[self keyDown:event];
return YES;
}

return NO;
}

Expand Down
6 changes: 3 additions & 3 deletions src/keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

@class NSEvent;

void addModifiedName(std::ostream &, NSEvent *, const char *name);
void translateKeyEvent(std::ostream &, unsigned short keyCode, unsigned flags);
void translateKeyEvent(std::ostream &, NSEvent *);
void addModifiedName(std::ostream &, NSEvent *, const char *);
void translateKeyEvent(std::ostream &, unsigned short, unsigned, BOOL);
void translateKeyEvent(std::ostream &, NSEvent *, BOOL);
18 changes: 9 additions & 9 deletions src/keys.mm
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ static unsigned cocoaToCarbonModifiers(unsigned mods)
const UCKeyboardLayout *layout =
(const UCKeyboardLayout *)CFDataGetBytePtr(layoutData);

unsigned deadKeyState;
unsigned deadKeyState = 0;
unichar chars[5];
UniCharCount numChars;

Expand Down Expand Up @@ -146,7 +146,7 @@ void addModifiedName(std::ostream &os, NSEvent *event, const char *name)
addModifiedName(os, [event modifierFlags], clickCount, name);
}

void translateKeyEvent(std::ostream &os, unsigned short keyCode, unsigned flags)
void translateKeyEvent(std::ostream &os, unsigned short keyCode, unsigned flags, BOOL optAsMeta)
{
const char *name = keyName(keyCode);

Expand Down Expand Up @@ -174,11 +174,11 @@ send them as modifiers (no "S-" or "M-") */
than sending the alternate character for that key; nobody wants to
map <C-∆> */
if (flags & NSAlternateKeyMask)
if (flags & (NSCommandKeyMask | NSControlKeyMask)) {
chars = stringFromModifiedKey(keyCode, flags & NSShiftKeyMask);
sendflags |= NSAlternateKeyMask;
flags &= ~NSAlternateKeyMask;
}
if (flags & (NSCommandKeyMask | NSControlKeyMask) || optAsMeta) {
chars = stringFromModifiedKey(keyCode, flags & NSShiftKeyMask);
sendflags |= NSAlternateKeyMask;
flags &= ~NSAlternateKeyMask;
}

/* Vim doesn't distinguish between <C-j> and <C-J>, so let's send the
S- modifier if:
Expand Down Expand Up @@ -214,7 +214,7 @@ send them as modifiers (no "S-" or "M-") */
}
}

void translateKeyEvent(std::ostream &os, NSEvent *event)
void translateKeyEvent(std::ostream &os, NSEvent *event, BOOL optAsMeta)
{
unsigned short keyCode = [event keyCode];

Expand All @@ -227,5 +227,5 @@ void translateKeyEvent(std::ostream &os, NSEvent *event)
NSCommandKeyMask
);

translateKeyEvent(os, keyCode, flags);
translateKeyEvent(os, keyCode, flags, optAsMeta);
}
2 changes: 1 addition & 1 deletion src/tests/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
static void test(unsigned short keyCode, unsigned flags, std::string expected)
{
std::stringstream ss;
translateKeyEvent(ss, keyCode, flags);
translateKeyEvent(ss, keyCode, flags, NO);
std::string got = ss.str();
if (got != expected) {
pass = false;
Expand Down
3 changes: 3 additions & 0 deletions src/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class Vim;
bool mMenuNeedsUpdate;

NSString *mMarkedText;
BOOL mOptAsMeta;
}

- (void)cutText;
Expand All @@ -45,6 +46,8 @@ class Vim;
- (void)setFontProgramatically:(NSFont *)font;
- (void)setShouldAntialias:(BOOL)shouldAntialias;

- (void)setOptionAsMeta:(BOOL)isEnabled;

- (void)openFile:(NSString *)filename;

- (void)requestResize:(CGSize)cellSize;
Expand Down
7 changes: 7 additions & 0 deletions src/view.mm
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ - (id)initWithFrame:(NSRect)frame vim:(Vim *)vim
mCursorPos = mCursorDisplayPos = CGPointZero;
mCursorOn = true;

mOptAsMeta = NO;

[self registerForDraggedTypes:[NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
}

Expand Down Expand Up @@ -202,6 +204,11 @@ - (void)changeFont:(id)sender
[defaults setFloat:mFont.pointSize forKey:@"fontSize"];
}

- (void)setOptionAsMeta:(BOOL)isEnabled
{
mOptAsMeta = isEnabled;
}

- (void)setShouldAntialias:(BOOL)shouldAntialias
{
CGContextSetShouldAntialias(mCanvasContext, shouldAntialias);
Expand Down
10 changes: 7 additions & 3 deletions src/window.mm
Original file line number Diff line number Diff line change
Expand Up @@ -401,14 +401,18 @@ - (void)notified:(const std::string &)note withData:(const msgpack::object &)upd
mVim->vim_report_error(msg);
}
}
else if (note == "neovim.app.bufchanged") {
else if (note == "neovim.app.bufchanged") {
int isModified = update_o.via.array.ptr[0].convert();
[self setDocumentEdited:isModified?YES:NO];
}
else if (note == "neovim.app.closeTabOrWindow") {
else if (note == "neovim.app.closeTabOrWindow") {
[self closeTabOrWindow];
}
else if (note == "neovim.app.shouldAntialias") {
else if (note == "neovim.app.setOptAsMeta") {
BOOL optAsMeta = update_o.via.array.ptr[0].convert();
[mMainView setOptionAsMeta:optAsMeta];
}
else if (note == "neovim.app.shouldAntialias") {
BOOL shouldAntialias = update_o.via.array.ptr[0].convert();
[mMainView setShouldAntialias:shouldAntialias];
}
Expand Down

0 comments on commit d6ef684

Please sign in to comment.