Skip to content

Commit

Permalink
• further improvements for Format R Code
Browse files Browse the repository at this point in the history
- keep users empty lines since (but clean them)
- sped it up a bit
- fixed mixing indentation of 4 and 2 spaces coming from deparse()
• avoid re-entering Rd tools or R code formatting methods
• made deparse's width.cutoff customizable via Pref key RScriptEditorFormatWidthCutoff; if set to 0 the window's width will be taken otherwise the user's setting


git-svn-id: https://svn.r-project.org/R-packages/trunk/Mac-GUI@6059 694ef91d-65df-0310-b7bb-92e67a308ead
  • Loading branch information
Hans-Jörg Bibiko committed Feb 2, 2012
1 parent 76b0824 commit f8eafed
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 140 deletions.
68 changes: 42 additions & 26 deletions NSString_RAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ - (NSString *)runBashCommandWithEnvironment:(NSDictionary*)shellEnvironment atCu
BOOL userTerminated = NO;
BOOL redirectForScript = NO;
BOOL isDir = NO;
BOOL nonWaitingMode = ([self hasSuffix:@"&"]) ? YES : NO;

NSMutableArray *scriptHeaderArguments = [NSMutableArray array];
NSString *scriptPath = @"";
Expand Down Expand Up @@ -112,7 +113,7 @@ - (NSString *)runBashCommandWithEnvironment:(NSDictionary*)shellEnvironment atCu
}
}
} else {
[scriptHeaderArguments addObject:@"/bin/sh"];
[scriptHeaderArguments addObject:@"/bin/bash"];
NSError *writeError = nil;
[self writeToFile:scriptFilePath atomically:YES encoding:NSUTF8StringEncoding error:&writeError];
if(writeError == nil) {
Expand All @@ -126,9 +127,12 @@ - (NSString *)runBashCommandWithEnvironment:(NSDictionary*)shellEnvironment atCu

NSTask *bashTask = [[NSTask alloc] init];
[bashTask setLaunchPath:@"/bin/bash"];

NSMutableDictionary *theEnv = [NSMutableDictionary dictionary];
[theEnv setDictionary:shellEnvironment];
if(shellEnvironment)
[theEnv setDictionary:shellEnvironment];
else
[theEnv setDictionary:[[NSProcessInfo processInfo] environment]];

[theEnv setObject:[NSNumber numberWithInteger:kBASHTaskRedirectActionNone] forKey:kBASHTaskShellVariableExitNone];
[theEnv setObject:[NSNumber numberWithInteger:kBASHTaskRedirectActionReplaceSection] forKey:kBASHTaskShellVariableExitReplaceSelection];
Expand All @@ -151,37 +155,49 @@ - (NSString *)runBashCommandWithEnvironment:(NSDictionary*)shellEnvironment atCu
else
[bashTask setArguments:[NSArray arrayWithObjects:@"-c", [NSString stringWithFormat:@"%@ > %@", [scriptHeaderArguments componentsJoinedByString:@" "], stdoutFilePath], nil]];

NSPipe *stderr_pipe = [NSPipe pipe];
[bashTask setStandardError:stderr_pipe];
NSFileHandle *stderr_file = [stderr_pipe fileHandleForReading];

NSFileHandle *stderr_file = nil;
NSPipe *stderr_pipe = nil;
if(!nonWaitingMode) {
stderr_pipe = [NSPipe pipe];
[bashTask setStandardError:stderr_pipe];
stderr_file = [stderr_pipe fileHandleForReading];
}
[bashTask launch];

NSInteger pid = -1;
pid = [bashTask processIdentifier];

// Listen to ⌘. to terminate
while(1) {
if(![bashTask isRunning] || [bashTask processIdentifier] == 0) break;
NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantPast]
inMode:NSDefaultRunLoopMode
dequeue:YES];
usleep(1000);
if(!event) continue;
if ([event type] == NSKeyDown) {
unichar key = [[event characters] length] == 1 ? [[event characters] characterAtIndex:0] : 0;
if (([event modifierFlags] & NSCommandKeyMask) && key == '.') {
[bashTask terminate];
userTerminated = YES;
break;
if(!nonWaitingMode) {
// Listen to ⌘. to terminate
while(1) {
if(![bashTask isRunning] || [bashTask processIdentifier] == 0) break;
NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantPast]
inMode:NSDefaultRunLoopMode
dequeue:YES];
usleep(1000);
if(!event) continue;
if ([event type] == NSKeyDown) {
unichar key = [[event characters] length] == 1 ? [[event characters] characterAtIndex:0] : 0;
if (([event modifierFlags] & NSCommandKeyMask) && key == '.') {
[bashTask terminate];
userTerminated = YES;
break;
}
[NSApp sendEvent:event];
} else {
[NSApp sendEvent:event];
}
[NSApp sendEvent:event];
} else {
[NSApp sendEvent:event];
}
}

[bashTask waitUntilExit];
[bashTask waitUntilExit];
} else {
if (bashTask) [bashTask release];
[fm removeItemAtPath:scriptFilePath error:nil];
[fm removeItemAtPath:stdoutFilePath error:nil];
return @"";
}

// Remove files
[fm removeItemAtPath:scriptFilePath error:nil];
Expand Down
2 changes: 2 additions & 0 deletions PreferenceKeys.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
#define lastUsedFileEncoding @"LastUsedFileEncoding"
#define usedFileEncodings @"UsedFileEncodings"

#define RScriptEditorFormatWidthCutoff @"RScriptEditorFormatWidthCutoff"

#define FontSizeKey @"Console Font Size"
#define RScriptEditorTabWidth @"R Script Editor tab width"
#define RScriptEditorDefaultFont @"R Script Editor default font"
Expand Down
1 change: 1 addition & 0 deletions RDocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
BOOL isEditable; // determines whether this document can be edited
BOOL isREdit; // set to YES by R_Edit to exit modal state on close
BOOL fileTypeWasChangedWhileSaving;
BOOL rdToolsAreWorking;

NSPopUpButton *encodingPopUp;

Expand Down
50 changes: 43 additions & 7 deletions RDocument.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ - (id)init
isEditable=YES;
isREdit=NO;
myWinCtrl=nil;
rdToolsAreWorking=NO;
fileTypeWasChangedWhileSaving = NO;
}
return self;
Expand Down Expand Up @@ -565,20 +566,27 @@ - (BOOL) checkRdDocumentWithFilePath:(NSString*)inputFile reportSuccess:(BOOL)re
- (BOOL) checkRdDocument
{

if(rdToolsAreWorking) return NO;
rdToolsAreWorking = YES;

NSString *tempName = [NSTemporaryDirectory() stringByAppendingPathComponent: [NSString stringWithFormat: @"%.0f.", [NSDate timeIntervalSinceReferenceDate] * 1000.0]];
NSString *inputFile = [NSString stringWithFormat: @"%@%@", tempName, @"rd"];

BOOL success = [self checkRdDocumentWithFilePath:inputFile reportSuccess:YES];

[[NSFileManager defaultManager] removeItemAtPath:inputFile error:NULL];


rdToolsAreWorking = NO;
return success;

}

- (void) insertRdDataTemplate
{

if(rdToolsAreWorking) return;
rdToolsAreWorking = YES;

[myWinCtrl setStatusLineText:[NSString stringWithFormat:@"%@", NLS(@"press ⌘. to cancel")]];

NSError *bashError = nil;
Expand All @@ -589,12 +597,14 @@ - (void) insertRdDataTemplate
if(bashError != nil) {
NSBeep();
NSLog(@"RDocumentWinCtrl.insertRdDataTemplate bailed due to BASH error:\n%@", bashError);
rdToolsAreWorking = NO;
return;
}

if(!templateStr) {
NSBeep();
NSLog(@"RDocumentWinCtrl.insertRdDataTemplate bailed; no response from called R session");
rdToolsAreWorking = NO;
return;
}

Expand All @@ -607,11 +617,17 @@ - (void) insertRdDataTemplate
NSRange newFunRange = [templateStr rangeOfString:NLS(@"DATA_NAME")];
[[myWinCtrl textView] setSelectedRange:newFunRange];
[[myWinCtrl textView] scrollRangeToVisible:newFunRange];

rdToolsAreWorking = NO;

}

- (void) insertRdFunctionTemplate
{

if(rdToolsAreWorking) return;
rdToolsAreWorking = YES;

[myWinCtrl setStatusLineText:[NSString stringWithFormat:@"%@", NLS(@"press ⌘. to cancel")]];

NSError *bashError = nil;
Expand All @@ -622,12 +638,14 @@ - (void) insertRdFunctionTemplate
if(bashError != nil) {
NSBeep();
NSLog(@"RDocumentWinCtrl.insertRdFunctionTemplate bailed due to BASH error:\n%@", bashError);
rdToolsAreWorking = NO;
return;
}

if(!templateStr) {
NSBeep();
NSLog(@"RDocumentWinCtrl.insertRdFunctionTemplate bailed; no response from called R session");
rdToolsAreWorking = NO;
return;
}

Expand All @@ -640,11 +658,17 @@ - (void) insertRdFunctionTemplate
NSRange newFunRange = [templateStr rangeOfString:NLS(@"FUNCTION_NAME")];
[[myWinCtrl textView] setSelectedRange:newFunRange];
[[myWinCtrl textView] scrollRangeToVisible:newFunRange];

rdToolsAreWorking = NO;

}

- (BOOL) convertRd2PDF
{

if(rdToolsAreWorking) return NO;
rdToolsAreWorking = YES;

BOOL success = YES;
NSError *bashError = nil;

Expand Down Expand Up @@ -682,6 +706,7 @@ - (BOOL) convertRd2PDF
[[NSFileManager defaultManager] removeItemAtPath:inputFile error:NULL];
[[NSFileManager defaultManager] removeItemAtPath:pdfOutputFile error:NULL];
[myWinCtrl setStatusLineText:@""];
rdToolsAreWorking = NO;
return NO;
}

Expand Down Expand Up @@ -723,9 +748,12 @@ - (BOOL) convertRd2PDF
[[NSFileManager defaultManager] removeItemAtPath:inputFile error:NULL];
[[NSFileManager defaultManager] removeItemAtPath:pdfOutputFile error:NULL];
[[NSFileManager defaultManager] removeItemAtPath:errOutputFile error:NULL];
rdToolsAreWorking = NO;
return NO;
}

rdToolsAreWorking = NO;

// After 100 secs all temporary files will be deleted even if R was quitted meanwhile
[self performSelector:@selector(removeFiles:) withObject:[NSArray arrayWithObjects:inputFile, pdfOutputFile, errOutputFile, nil] afterDelay:100];

Expand All @@ -735,19 +763,25 @@ - (BOOL) convertRd2PDF
- (BOOL) convertRd2HTML
{

if(rdToolsAreWorking) return NO;
rdToolsAreWorking = YES;

BOOL success = YES;

NSString *tempName = [NSTemporaryDirectory() stringByAppendingPathComponent: [NSString stringWithFormat: @"%.0f.", [NSDate timeIntervalSinceReferenceDate] * 1000.0]];
NSString *RhomeCSS = @"R.css";

[myWinCtrl setStatusLineText:[NSString stringWithFormat:@"%@", NLS(@"press ⌘. to cancel")]];
NSString *Rhome = [@"R --slave --vanilla -e 'cat(R.home())'" runBashCommandWithEnvironment:nil atCurrentDirectoryPath:nil error:nil];
[myWinCtrl setStatusLineText:@""];
NSString *Rhome = [[RController sharedController] home];

if(!Rhome || ![Rhome length]) {
[myWinCtrl setStatusLineText:[NSString stringWithFormat:@"%@", NLS(@"press ⌘. to cancel")]];
Rhome = [@"R --slave --vanilla -e 'cat(R.home())'" runBashCommandWithEnvironment:nil atCurrentDirectoryPath:nil error:nil];
[myWinCtrl setStatusLineText:@""];
}

if(Rhome && [Rhome length]) {
RhomeCSS = [NSString stringWithFormat:@"file://%@/doc/html/R.css", Rhome];
}

NSError *error;
NSString *inputFile = [NSString stringWithFormat: @"%@%@", tempName, @"rd"];
NSString *htmlOutputFile = [NSString stringWithFormat: @"%@%@", tempName, @"html"];
Expand All @@ -758,6 +792,7 @@ - (BOOL) convertRd2HTML

if(![self checkRdDocumentWithFilePath:inputFile reportSuccess:NO]) {
[[NSFileManager defaultManager] removeItemAtPath:inputFile error:NULL];
rdToolsAreWorking = NO;
return NO;
}

Expand Down Expand Up @@ -788,9 +823,10 @@ - (BOOL) convertRd2HTML
NSBeep();
success = NO;
}

}

rdToolsAreWorking = NO;

// After 100 secs all temporary files will be deleted even if R was quitted meanwhile
[self performSelector:@selector(removeFiles:) withObject:[NSArray arrayWithObjects:inputFile, htmlOutputFile, nil] afterDelay:100];

Expand Down
1 change: 1 addition & 0 deletions RDocumentWinCtrl.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ extern NSColor *shColorIdentifier;
BOOL plainFile; // overriders preferences - if YES syntax HL is disabled
BOOL argsHints; // fn args hinting
BOOL lastLineWasCodeIndented;
BOOL isFormattingRcode;

int hsType; // help search type

Expand Down
Loading

0 comments on commit f8eafed

Please sign in to comment.