diff --git a/docs/changelog.rst b/docs/changelog.rst index 55a077b9d..a8d767a79 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -97,6 +97,8 @@ Detailed list of changes - Fix ssh kitten not working on FreeBSD (:iss:`5928`) +- macOS: Export kitty selected text to the system for use with services that accept it (patch by Sertaç Ö. Yıldız) + 0.26.5 [2022-11-07] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/glfw/cocoa_window.m b/glfw/cocoa_window.m index 2ebc7f044..e5b754d2c 100644 --- a/glfw/cocoa_window.m +++ b/glfw/cocoa_window.m @@ -1576,6 +1576,53 @@ void _glfwPlatformUpdateIMEState(_GLFWwindow *w, const GLFWIMEUpdateEvent *ev) { return text; } +// + +// Support services receiving "public.utf8-plain-text" and "NSStringPboardType" +- (id)validRequestorForSendType:(NSString *)sendType returnType:(NSString *)returnType +{ + if ([sendType isEqual:NSPasteboardTypeString] || [sendType isEqual:@"NSStringPboardType"]) { + return self; + } + return nil; +} + +// Selected text as input to be sent to Services +- (BOOL)writeSelectionToPasteboard:(NSPasteboard *)pboard types:(NSArray *)types +{ + NSString *text = [self accessibilitySelectedText]; + if (text && [text length] > 0) { + if ([types containsObject:NSPasteboardTypeString] == YES) { + [pboard declareTypes:@[NSPasteboardTypeString] owner:self]; + return [pboard setString:text forType:NSPasteboardTypeString]; + } else if ([types containsObject:@"NSStringPboardType"] == YES) { + [pboard declareTypes:@[@"NSStringPboardType"] owner:self]; + return [pboard setString:text forType:NSPasteboardTypeString]; + } + } + return NO; +} + +// Service output to be handled +- (BOOL)readSelectionFromPasteboard:(NSPasteboard *)pboard +{ + NSString* text = nil; + NSArray *types = [pboard types]; + if ([types containsObject:NSPasteboardTypeString] == YES) { + text = [pboard stringForType:NSPasteboardTypeString]; // public.utf8-plain-text + } else if ([types containsObject:@"NSStringPboardType"] == YES) { + text = [pboard stringForType:@"NSStringPboardType"]; // for older services (need re-encode?) + } else { + return NO; + } + if (text && [text length] > 0) { + // Terminal.app inserts the output, do the same + [self insertText:text replacementRange:NSMakeRange(0, [markedText length])]; + return YES; + } + return NO; +} + @end // }}}