From 9b47c2b172600b1e23eb9958981082bdb874ffca Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Mon, 14 Jul 2008 15:13:34 -0400 Subject: [PATCH] Check and see if stderr has any data on it when syntax highlighting. If so, only show plain text. * Source/BSSourceView.m: (initWithFrame): Register to be an observer of the file handle reader notification (setFile:): Add another pipe for stderr, and read in background from it (errorHighlightingFile:): New method to handle the notification --- Source/BSSourceView.m | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/Source/BSSourceView.m b/Source/BSSourceView.m index 6054a94..43cd3f4 100644 --- a/Source/BSSourceView.m +++ b/Source/BSSourceView.m @@ -18,6 +18,7 @@ @interface BSSourceView (Private) - (void)setupViews; +- (void)errorHighlightingFile:(NSNotification *)notif; @end @implementation BSSourceView @@ -32,6 +33,10 @@ if (self = [super initWithFrame:frame]) { [self setupViews]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(errorHighlightingFile:) + name:NSFileHandleReadToEndOfFileCompletionNotification + object:nil]; } return self; } @@ -64,13 +69,19 @@ @try { // Attempt to use the PHP CLI to highlight the source file as HTML - NSPipe *pipe = [NSPipe pipe]; + NSPipe *outPipe = [NSPipe pipe]; + NSPipe *errPipe = [NSPipe pipe]; NSTask *task = [[NSTask new] autorelease]; + [task setLaunchPath:@"/usr/bin/php"]; // This is the path to the default Leopard PHP executable [task setArguments:[NSArray arrayWithObjects:@"-s", f, nil]]; - [task setStandardOutput:pipe]; + [task setStandardOutput:outPipe]; + [task setStandardError:errPipe]; [task launch]; - NSData *data = [[pipe fileHandleForReading] readDataToEndOfFile]; + + [[errPipe fileHandleForReading] readToEndOfFileInBackgroundAndNotify]; + + NSData *data = [[outPipe fileHandleForReading] readDataToEndOfFile]; NSAttributedString *source = [[NSAttributedString alloc] initWithHTML:data documentAttributes:NULL]; [[textView textStorage] setAttributedString:source]; [source release]; @@ -82,6 +93,16 @@ } } +/** + * If an error occurs in reading the highlighted PHP source, this will merely set the string + */ +- (void)errorHighlightingFile:(NSNotification *)notif +{ + NSData *data = [[notif userInfo] objectForKey:NSFileHandleNotificationDataItem]; + if ([data length] > 0) // there's something on stderr, so the PHP CLI failed + [textView setString:[NSString stringWithContentsOfFile:file]]; +} + /** * Flip the coordinates */ -- 2.43.5