Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x-macdown URL scheme implementation #985

Merged
merged 4 commits into from
Jun 3, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Added some line breaks to try to keep lines under 80 characters long
Jean Jourdain committed Jun 2, 2018

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
commit dc64911687a051867c886074e8ed9d2f87619534
31 changes: 20 additions & 11 deletions MacDown/Code/Application/MPMainController.m
Original file line number Diff line number Diff line change
@@ -117,11 +117,16 @@ - (void)applicationDidFinishLaunching:(NSNotification *)notification
[invocation invoke];
#pragma clang diagnostic pop
}
[[NSAppleEventManager sharedAppleEventManager] setEventHandler:self andSelector:@selector(openUrlSchemeAppleEvent:withReplyEvent:) forEventClass:kInternetEventClass andEventID:kAEGetURL];
[[NSAppleEventManager sharedAppleEventManager]
setEventHandler:self
andSelector:@selector(openUrlSchemeAppleEvent:withReplyEvent:)
forEventClass:kInternetEventClass andEventID:kAEGetURL];
}

// Open a file from a browser with url of the form "x-macdown://open?url=file:///path/to/a/file&line=123&column=45"
- (void)openUrlSchemeAppleEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)reply
// Open a file from a browser with url of the form :
// "x-macdown://open?url=file:///path/to/a/file&line=123&column=45"
- (void)openUrlSchemeAppleEvent:(NSAppleEventDescriptor *)event
withReplyEvent:(NSAppleEventDescriptor *)reply
{
NSString *urlString = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
if (!urlString) {
@@ -131,7 +136,8 @@ - (void)openUrlSchemeAppleEvent:(NSAppleEventDescriptor *)event withReplyEvent:(
if (!url) {
return;
}
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:url resolvingAgainstBaseURL:NO];
NSURLComponents *urlComponents = [NSURLComponents componentsWithURL:url
resolvingAgainstBaseURL:NO];
if (!urlComponents) {
return;
}
@@ -147,9 +153,12 @@ - (void)openUrlSchemeAppleEvent:(NSAppleEventDescriptor *)event withReplyEvent:(
if (!fileParam) {
return;
}
// FIXME: Could not figure out how to place the insertion point at a given line and column.
/* Unused */ NSString *lineParam = [self valueForKey:@"line" fromQueryItems:queryItems];
/* Unused */ NSString *columnParam = [self valueForKey:@"column" fromQueryItems:queryItems];
// FIXME: Could not figure out how to place the insertion point at a given
// line and column.
/* Unused */ NSString *lineParam = [self valueForKey:@"line"
fromQueryItems:queryItems];
/* Unused */ NSString *columnParam = [self valueForKey:@"column"
fromQueryItems:queryItems];
NSLog(@"%@:%@:%@", fileParam, lineParam, columnParam);

NSURL *target = [NSURL URLWithString:fileParam];
@@ -324,19 +333,19 @@ - (void)openPendingFiles

- (void)openPendingPipedContent {
NSDocumentController *c = [NSDocumentController sharedDocumentController];

if (self.preferences.pipedContentFileToOpen) {
NSURL *pipedContentFileToOpenURL = [NSURL fileURLWithPath:self.preferences.pipedContentFileToOpen];
NSError *readPipedContentError;
NSString *pipedContentString = [NSString stringWithContentsOfURL:pipedContentFileToOpenURL encoding:NSUTF8StringEncoding error:&readPipedContentError];

NSError *openDocumentError;
MPDocument *document = (MPDocument *)[c openUntitledDocumentAndDisplay:YES error:&openDocumentError];

if (document && openDocumentError == nil && readPipedContentError == nil) {
document.markdown = pipedContentString;
}

self.preferences.pipedContentFileToOpen = nil;
[self.preferences synchronize];
}