Skip to content

Commit

Permalink
Add proprietary escape code to copy text to the clipboard. To use, se…
Browse files Browse the repository at this point in the history
…nd esc]50;CopyToClipboard=X^G followed by text followed by esc]50;EndCopy^G, where X is a clipboard name of ruler, find, font, or general. If not specified, general is used.
  • Loading branch information
gnachman committed Sep 5, 2011
1 parent a16a47f commit df94632
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 14 deletions.
7 changes: 7 additions & 0 deletions Headers/iTerm/PTYSession.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@ typedef enum {
// After receiving new output, we keep running the updateDisplay timer for a few seconds to catch
// changes in job name.
NSDate* updateDisplayUntil_;

// If not nil, we're aggregating text to append to a pasteboard. The pasteboard will be
// updated when this is set to nil.
NSString *pasteboard_;
NSMutableData *pbtext_;
}

// Return the current pasteboard value as a string.
Expand Down Expand Up @@ -478,6 +483,8 @@ typedef enum {
// Image for dragging one session.
- (NSImage *)dragImage;

- (void)setPasteboard:(NSString *)pbName;

@end

@interface PTYSession (ScriptingSupport)
Expand Down
1 change: 1 addition & 0 deletions Headers/iTerm/iTerm.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#define OSX_LEOPARDORLATER (floor(NSAppKitVersionNumber) > 824)

BOOL IsLionOrLater(void);
BOOL IsSnowLeopardOrLater(void);
BOOL IsLeopard(void);

#if MAC_OS_X_VERSION_MAX_ALLOWED < 1070
Expand Down
3 changes: 0 additions & 3 deletions Headers/iTerm/iTermController.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
@class PasteboardHistory;
@class GTMCarbonHotKey;

BOOL IsLionOrLater(void);
BOOL IsSnowLeopardOrLater();

@interface iTermController : NSObject
{
// PseudoTerminal objects
Expand Down
55 changes: 45 additions & 10 deletions PTYSession.m
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ - (id)init

- (void)dealloc
{
[pasteboard_ release];
[pbtext_ release];
[slowPasteBuffer release];
if (slowPasteTimer) {
[slowPasteTimer invalidate];
Expand Down Expand Up @@ -710,11 +712,6 @@ - (void)readTask:(NSData*)data
DebugLog([NSString stringWithFormat:@"readTask called with %d bytes. The last byte is %d", (int)length, (int)bytes[length-1]]);
}

#if DEBUG_METHOD_TRACE
NSLog(@"%s(%d):-[PTYSession readTask:%@]", __FILE__, __LINE__,
[[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:nil]);
#endif

[TERMINAL putStreamData:data];

VT100TCC token;
Expand All @@ -726,14 +723,29 @@ - (void)readTask:(NSData*)data
token.type != VT100_WAIT &&
token.type != VT100CC_NULL)) {
// process token
if (token.type != VT100_SKIP) {
if (token.type == VT100_NOTSUPPORT) {
//NSLog(@"%s(%d):not support token", __FILE__ , __LINE__);
} else {
if (token.type != VT100_SKIP) { // VT100_SKIP = there was no data to read
if (pasteboard_) {
// We are probably copying text to the clipboard until esc]50;EndCopy^G is received.
if (token.type != XTERMCC_SET_KVP || ![token.u.string hasPrefix:@"CopyToClipboard"]) {
// Append text to clipboard except for initial command that turns on copying to
// the clipboard.
[pbtext_ appendData:[NSData dataWithBytes:token.position
length:token.length]];
}

// Don't allow more than 100MB to be added to the pasteboard queue in case someone
// forgets to send the EndCopy command.
const int kMaxPasteboardBytes = 100 * 1024 * 1024;
if ([pbtext_ length] > kMaxPasteboardBytes) {
[self setPasteboard:nil];
}
}

if (token.type != VT100_NOTSUPPORT) {
[SCREEN putToken:token];
}
}
} // end token processing loop
}

gettimeofday(&lastOutput, NULL);
newOutput = YES;
Expand Down Expand Up @@ -3030,6 +3042,29 @@ - (NSImage *)imageOfSession:(BOOL)flip
return textviewImage;
}

- (void)setPasteboard:(NSString *)pbName
{
if (pbName) {
[pasteboard_ autorelease];
pasteboard_ = [pbName copy];
[pbtext_ release];
pbtext_ = [[NSMutableData alloc] init];
} else {
NSPasteboard *pboard = [NSPasteboard pasteboardWithName:pasteboard_];
[pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:self];
[pboard setData:pbtext_ forType:NSStringPboardType];

[pasteboard_ release];
pasteboard_ = nil;
[pbtext_ release];
pbtext_ = nil;

// In case it was the find pasteboard that chagned
[[NSNotificationCenter defaultCenter] postNotificationName:@"iTermLoadFindStringFromSharedPasteboard"
object:nil
userInfo:nil];
}
}

@end

Expand Down
12 changes: 12 additions & 0 deletions VT100Terminal.m
Original file line number Diff line number Diff line change
Expand Up @@ -2707,6 +2707,18 @@ - (void)_setRGB:(VT100TCC)token
[[SCREEN session] setPreferencesFromAddressBookEntry:dict];
[[SCREEN session] remarry];
}
} else if ([key isEqualToString:@"CopyToClipboard"]) {
if ([value isEqualToString:@"ruler"]) {
[[SCREEN session] setPasteboard:NSGeneralPboard];
} else if ([value isEqualToString:@"find"]) {
[[SCREEN session] setPasteboard:NSFindPboard];
} else if ([value isEqualToString:@"font"]) {
[[SCREEN session] setPasteboard:NSFontPboard];
} else {
[[SCREEN session] setPasteboard:NSGeneralPboard];
}
} else if ([key isEqualToString:@"EndCopy"]) {
[[SCREEN session] setPasteboard:nil];
}
} else if (token.type == XTERMCC_SET_PALETTE) {
NSString* argument = token.u.string;
Expand Down
2 changes: 1 addition & 1 deletion iTermController.m
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ BOOL IsLionOrLater(void) {
}
}

BOOL IsSnowLeopardOrLater() {
BOOL IsSnowLeopardOrLater(void) {
unsigned major;
unsigned minor;
if ([iTermController getSystemVersionMajor:&major minor:&minor bugFix:nil]) {
Expand Down

0 comments on commit df94632

Please sign in to comment.