Skip to content

Commit

Permalink
#1 extracted log macros and custom key binding init code
Browse files Browse the repository at this point in the history
  • Loading branch information
Tae Won Ha committed May 25, 2013
1 parent 756a367 commit a078663
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 73 deletions.
34 changes: 1 addition & 33 deletions src/MacVim/MMAppController.m
Expand Up @@ -333,39 +333,7 @@ - (void)applicationWillFinishLaunching:(NSNotification *)notification
forEventClass:kInternetEventClass
andEventID:kAEGetURL];

// Disable the default Cocoa "Key Bindings" since they interfere with the
// way Vim handles keyboard input. Cocoa reads bindings from
// /System/Library/Frameworks/AppKit.framework/Resources/
// StandardKeyBinding.dict
// and
// ~/Library/KeyBindings/DefaultKeyBinding.dict
// To avoid having the user accidentally break keyboard handling (by
// modifying the latter in some unexpected way) in MacVim we load our own
// key binding dictionary from Resource/KeyBinding.plist. We can't disable
// the bindings completely since it would break keyboard handling in
// dialogs so the our custom dictionary contains all the entries from the
// former location.
//
// It is possible to disable key bindings completely by not calling
// interpretKeyEvents: in keyDown: but this also disables key bindings used
// by certain input methods. E.g. Ctrl-Shift-; would no longer work in
// the Kotoeri input manager.
//
// To solve this problem we access a private API and set the key binding
// dictionary to our own custom dictionary here. At this time Cocoa will
// have already read the above mentioned dictionaries so it (hopefully)
// won't try to change the key binding dictionary again after this point.
NSKeyBindingManager *mgr = [NSKeyBindingManager sharedKeyBindingManager];
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *path = [mainBundle pathForResource:@"KeyBinding"
ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
if (mgr && dict) {
[mgr setDictionary:dict];
} else {
ASLogNotice(@"Failed to override the Cocoa key bindings. Keyboard "
"input may behave strangely as a result (path=%@).", path);
}
[MMUtils setVimKeybindings];
}

- (void)applicationDidFinishLaunching:(NSNotification *)notification
Expand Down
66 changes: 66 additions & 0 deletions src/MacVim/MMLog.h
@@ -0,0 +1,66 @@
/* vi:set ts=8 sts=4 sw=4 ft=objc:
*
* VIM - Vi IMproved by Bram Moolenaar
* MacVim GUI port by Bjorn Winckler
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/

#import <Foundation/Foundation.h>
#import <asl.h>

// Logging related functions and macros.
//
// This is a very simplistic logging facility built on top of ASL. Two user
// defaults allow for changing the local log filter level (MMLogLevel) and
// whether logs should be sent to stderr (MMLogToStdErr). (These user defaults
// are only checked during startup.) The default is to block level 6 (info)
// and 7 (debug) logs and _not_ to send logs to stderr. Apart from this
// "syslog" (see "man syslog") can be used to modify the ASL filters (it is
// currently not possible to change the local filter at runtime). For example:
// Enable all logs to reach the ASL database (by default 'debug' and 'info'
// are filtered out, see "man syslogd"):
// $ sudo syslog -c syslogd -d
// Reset the ASL database filter:
// $ sudo syslog -c syslogd off
// Change the master filter to block logs less severe than errors:
// $ sudo syslog -c 0 -e
// Change per-process filter for running MacVim process to block logs less
// severe than warnings:
// $ syslog -c MacVim -w
//
// Note that there are four ASL filters:
// 1) The ASL database filter (syslog -c syslogd ...)
// 2) The master filter (syslog -c 0 ...)
// 3) The per-process filter (syslog -c PID ...)
// 4) The local filter (MMLogLevel)
//
// To view the logs, either use "Console.app" or the "syslog" command:
// $ syslog -w | grep Vim
// To get the logs to show up in Xcode enable the MMLogToStdErr user default.

extern NSString *MMLogLevelKey;
extern NSString *MMLogToStdErrKey;

extern int ASLogLevel;

void ASLInit();

#define ASLog(level, fmt, ...) \
if (level <= ASLogLevel) { \
asl_log(NULL, NULL, level, "%s@%d: %s", \
__PRETTY_FUNCTION__, __LINE__, \
[[NSString stringWithFormat:fmt, ##__VA_ARGS__] UTF8String]); \
}

// Note: These macros are used like ASLogErr(@"text num=%d", 42). Objective-C
// style specifiers (%@) are supported.
#define ASLogCrit(fmt, ...) ASLog(ASL_LEVEL_CRIT, fmt, ##__VA_ARGS__)
#define ASLogErr(fmt, ...) ASLog(ASL_LEVEL_ERR, fmt, ##__VA_ARGS__)
#define ASLogWarn(fmt, ...) ASLog(ASL_LEVEL_WARNING, fmt, ##__VA_ARGS__)
#define ASLogNotice(fmt, ...) ASLog(ASL_LEVEL_NOTICE, fmt, ##__VA_ARGS__)
#define ASLogInfo(fmt, ...) ASLog(ASL_LEVEL_INFO, fmt, ##__VA_ARGS__)
#define ASLogDebug(fmt, ...) ASLog(ASL_LEVEL_DEBUG, fmt, ##__VA_ARGS__)
#define ASLogTmp(fmt, ...) ASLog(ASL_LEVEL_NOTICE, fmt, ##__VA_ARGS__)
42 changes: 42 additions & 0 deletions src/MacVim/MMLog.m
@@ -0,0 +1,42 @@
/* vi:set ts=8 sts=4 sw=4 ft=objc:
*
* VIM - Vi IMproved by Bram Moolenaar
* MacVim GUI port by Bjorn Winckler
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/

#import "MMLog.h"

NSString *MMLogLevelKey = @"MMLogLevel";
NSString *MMLogToStdErrKey = @"MMLogToStdErr";

void
ASLInit()
{
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

// Allow for changing the log level via user defaults. If no key is found
// the default log level will be used (which for ASL is to log everything
// up to ASL_LEVEL_NOTICE). This key is an integer which corresponds to
// the ASL_LEVEL_* macros (0 is most severe, 7 is debug level).
id logLevelObj = [ud objectForKey:MMLogLevelKey];
if (logLevelObj) {
int logLevel = [logLevelObj intValue];
if (logLevel < 0) logLevel = 0;
if (logLevel > ASL_LEVEL_DEBUG) logLevel = ASL_LEVEL_DEBUG;

ASLogLevel = logLevel;
asl_set_filter(NULL, ASL_FILTER_MASK_UPTO(logLevel));
}

// Allow for changing whether a copy of each log should be sent to stderr
// (this defaults to NO if this key is missing in the user defaults
// database). The above filter mask is applied to logs going to stderr,
// contrary to how "vanilla" ASL works.
BOOL logToStdErr = [ud boolForKey:MMLogToStdErrKey];
if (logToStdErr)
asl_add_log_file(NULL, 2); // The file descriptor for stderr is 2
}
33 changes: 29 additions & 4 deletions src/MacVim/MMUtils.h
Expand Up @@ -8,14 +8,12 @@
* See README.txt for an overview of the Vim source code.
*/

#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>

/**
* Utility class which contains convenience class methods.
*/
@interface MMUtils : NSObject {

}
@interface MMUtils : NSObject

/**
* Sets NSQuotedKeystrokeBinding and NSRepeatCountBinding to appropriate values such that VIM works.
Expand All @@ -25,4 +23,31 @@
*/
+ (void)setKeyHandlingUserDefaults;

/**
* Disable the default Cocoa "Key Bindings" since they interfere with the
* way Vim handles keyboard input. Cocoa reads bindings from
* /System/Library/Frameworks/AppKit.framework/Resources/
* StandardKeyBinding.dict
* and
* ~/Library/KeyBindings/DefaultKeyBinding.dict
*
* To avoid having the user accidentally break keyboard handling (by
* modifying the latter in some unexpected way) in MacVim we load our own
* key binding dictionary from Resource/KeyBinding.plist. We can't disable
* the bindings completely since it would break keyboard handling in
* dialogs so the our custom dictionary contains all the entries from the
* former location.
*
* It is possible to disable key bindings completely by not calling
* interpretKeyEvents: in keyDown: but this also disables key bindings used
* by certain input methods. E.g. Ctrl-Shift-; would no longer work in
* the Kotoeri input manager.
*
* To solve this problem we access a private API and set the key binding
* dictionary to our own custom dictionary here. At this time Cocoa will
* have already read the above mentioned dictionaries so it (hopefully)
* won't try to change the key binding dictionary again after this point.
*/
+ (void)setVimKeybindings;

@end
24 changes: 24 additions & 0 deletions src/MacVim/MMUtils.m
Expand Up @@ -9,6 +9,16 @@
*/

#import "MMUtils.h"
#import "MacVim.h"

// This is a private AppKit API gleaned from class-dump.
@interface NSKeyBindingManager : NSObject

+ (id)sharedKeyBindingManager;
- (id)dictionary;
- (void)setDictionary:(id)arg1;

@end

@implementation MMUtils

Expand Down Expand Up @@ -43,4 +53,18 @@ + (void)setKeyHandlingUserDefaults {
prefSet = YES;
}

+ (void)setVimKeybindings {
NSKeyBindingManager *mgr = [NSKeyBindingManager sharedKeyBindingManager];
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *path = [mainBundle pathForResource:@"KeyBinding"
ofType:@"plist"];
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
if (mgr && dict) {
[mgr setDictionary:dict];
} else {
ASLogNotice(@"Failed to override the Cocoa key bindings. Keyboard "
"input may behave strangely as a result (path=%@).", path);
}
}

@end
3 changes: 1 addition & 2 deletions src/MacVim/MacVim.h
Expand Up @@ -11,6 +11,7 @@
#import <Cocoa/Cocoa.h>
#import <asl.h>

#import "MMLog.h"

// Taken from /usr/include/AvailabilityMacros.h
#ifndef MAC_OS_X_VERSION_10_4
Expand Down Expand Up @@ -248,8 +249,6 @@ NSString *debugStringForMessageQueue(NSArray *queue);
// Contrary to the user defaults in Miscellaneous.h these defaults are not
// intitialized to any default values. That is, unless the user sets them
// these keys will not be present in the user default database.
extern NSString *MMLogLevelKey;
extern NSString *MMLogToStdErrKey;

// Argument used to stop MacVim from opening an empty window on startup
// (techincally this is a user default but should not be used as such).
Expand Down
34 changes: 0 additions & 34 deletions src/MacVim/MacVim.m
Expand Up @@ -105,9 +105,6 @@



NSString *MMLogLevelKey = @"MMLogLevel";
NSString *MMLogToStdErrKey = @"MMLogToStdErr";

// Argument used to stop MacVim from opening an empty window on startup
// (techincally this is a user default but should not be used as such).
NSString *MMNoWindowKey = @"MMNoWindow";
Expand Down Expand Up @@ -335,34 +332,3 @@ + (id)dictionaryWithData:(NSData *)data
}

@end




void
ASLInit()
{
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];

// Allow for changing the log level via user defaults. If no key is found
// the default log level will be used (which for ASL is to log everything
// up to ASL_LEVEL_NOTICE). This key is an integer which corresponds to
// the ASL_LEVEL_* macros (0 is most severe, 7 is debug level).
id logLevelObj = [ud objectForKey:MMLogLevelKey];
if (logLevelObj) {
int logLevel = [logLevelObj intValue];
if (logLevel < 0) logLevel = 0;
if (logLevel > ASL_LEVEL_DEBUG) logLevel = ASL_LEVEL_DEBUG;

ASLogLevel = logLevel;
asl_set_filter(NULL, ASL_FILTER_MASK_UPTO(logLevel));
}

// Allow for changing whether a copy of each log should be sent to stderr
// (this defaults to NO if this key is missing in the user defaults
// database). The above filter mask is applied to logs going to stderr,
// contrary to how "vanilla" ASL works.
BOOL logToStdErr = [ud boolForKey:MMLogToStdErrKey];
if (logToStdErr)
asl_add_log_file(NULL, 2); // The file descriptor for stderr is 2
}
6 changes: 6 additions & 0 deletions src/MacVim/MacVim.xcodeproj/project.pbxproj
Expand Up @@ -10,6 +10,7 @@
0395A8330D71ED7800881434 /* DBPrefsWindowController.m in Sources */ = {isa = PBXBuildFile; fileRef = 0395A8320D71ED7800881434 /* DBPrefsWindowController.m */; };
0395A8AA0D72D88B00881434 /* General.png in Resources */ = {isa = PBXBuildFile; fileRef = 0395A8A90D72D88B00881434 /* General.png */; };
1929B3086BD2CD8259242896 /* MMUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 1929B8B5ECD564CAB086FA31 /* MMUtils.m */; };
1929B37E3E8E65A65B0FA9EB /* MMLog.m in Sources */ = {isa = PBXBuildFile; fileRef = 1929B00B0ACC21406965D749 /* MMLog.m */; };
1929BAB9748E48EDB27D1573 /* MMUtils.h in Copy Executables */ = {isa = PBXBuildFile; fileRef = 1929BF8C22F04CBCAC99D88C /* MMUtils.h */; };
1D09AB420C6A4D520045497E /* MMTypesetter.m in Sources */ = {isa = PBXBuildFile; fileRef = 1D09AB400C6A4D520045497E /* MMTypesetter.m */; };
1D0E051C0BA5F83800B6049E /* Colors.plist in Resources */ = {isa = PBXBuildFile; fileRef = 1D0E051B0BA5F83800B6049E /* Colors.plist */; };
Expand Down Expand Up @@ -135,6 +136,8 @@
0395A8A90D72D88B00881434 /* General.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = General.png; sourceTree = "<group>"; };
089C165DFE840E0CC02AAC07 /* English */ = {isa = PBXFileReference; fileEncoding = 10; lastKnownFileType = text.plist.strings; name = English; path = English.lproj/InfoPlist.strings; sourceTree = "<group>"; };
1058C7A1FEA54F0111CA2CBB /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = /System/Library/Frameworks/Cocoa.framework; sourceTree = "<absolute>"; };
1929B00B0ACC21406965D749 /* MMLog.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MMLog.m; sourceTree = "<group>"; };
1929B6AC0540B30EA9119D9B /* MMLog.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MMLog.h; sourceTree = "<group>"; };
1929B8B5ECD564CAB086FA31 /* MMUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MMUtils.m; sourceTree = "<group>"; };
1929BF8C22F04CBCAC99D88C /* MMUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MMUtils.h; sourceTree = "<group>"; };
1D09AB3F0C6A4D520045497E /* MMTypesetter.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; path = MMTypesetter.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -301,6 +304,8 @@
children = (
1929BF8C22F04CBCAC99D88C /* MMUtils.h */,
1929B8B5ECD564CAB086FA31 /* MMUtils.m */,
1929B00B0ACC21406965D749 /* MMLog.m */,
1929B6AC0540B30EA9119D9B /* MMLog.h */,
);
name = VimTextView;
sourceTree = "<group>";
Expand Down Expand Up @@ -576,6 +581,7 @@
1DE63FFB0E71820F00959BDB /* MMCoreTextView.m in Sources */,
1D44972211FCA9B400B0630F /* MMCoreTextView+ToolTip.m in Sources */,
1929B3086BD2CD8259242896 /* MMUtils.m in Sources */,
1929B37E3E8E65A65B0FA9EB /* MMLog.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

0 comments on commit a078663

Please sign in to comment.