Replace non-breaking spaces with normal spaces in BSSourceView.
[macgdbp.git] / Source / BSSourceView.mm
1 /*
2 * MacGDBp
3 * Copyright (c) 2007 - 2011, Blue Static <http://www.bluestatic.org>
4 *
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17 #import "BSSourceView.h"
18
19 #import "BSLineNumberRulerView.h"
20 #import "BSSourceViewTextView.h"
21
22 @interface BSSourceView (Private)
23 - (void)setupViews;
24 - (void)errorHighlightingFile:(NSNotification*)notif;
25 - (void)setPlainTextStringFromFile:(NSString*)filePath;
26 @end
27
28 @implementation BSSourceView
29
30 @synthesize textView = textView_;
31 @synthesize scrollView = scrollView_;
32 @synthesize markers = markers_;
33 @synthesize markedLine = markedLine_;
34 @synthesize delegate = delegate_;
35 @synthesize file = file_;
36
37 /**
38 * Initializes the source view with the path of a file
39 */
40 - (id)initWithFrame:(NSRect)frame
41 {
42 if (self = [super initWithFrame:frame]) {
43 [self setupViews];
44 [[NSNotificationCenter defaultCenter]
45 addObserver:self
46 selector:@selector(errorHighlightingFile:)
47 name:NSFileHandleReadToEndOfFileCompletionNotification
48 object:nil
49 ];
50 }
51 return self;
52 }
53
54 /**
55 * Dealloc
56 */
57 - (void)dealloc
58 {
59 [file_ release];
60 [markers_ release];
61
62 [scrollView_ removeFromSuperview];
63 [textView_ removeFromSuperview];
64
65 [super dealloc];
66 }
67
68 - (void)setMarkers:(NSSet*)markers {
69 [markers_ release];
70 markers_ = [markers copy];
71
72 [ruler_ setNeedsDisplay:YES];
73 }
74
75 - (void)setMarkedLine:(NSUInteger)markedLine {
76 markedLine_ = markedLine;
77 [ruler_ setNeedsDisplay:YES];
78 }
79
80 /**
81 * Sets the file name as well as the text of the source view
82 */
83 - (void)setFile:(NSString*)f
84 {
85 if (file_ != f) {
86 [file_ release];
87 file_ = [f retain];
88 }
89
90 if (![[NSFileManager defaultManager] fileExistsAtPath:f]) {
91 [textView_ setString:@""];
92 return;
93 }
94
95 @try {
96 // Attempt to use the PHP CLI to highlight the source file as HTML
97 NSPipe* outPipe = [NSPipe pipe];
98 NSPipe* errPipe = [NSPipe pipe];
99 NSTask* task = [[NSTask new] autorelease];
100
101 [task setLaunchPath:@"/usr/bin/php"]; // This is the path to the default Leopard PHP executable
102 [task setArguments:[NSArray arrayWithObjects:@"-s", f, nil]];
103 [task setStandardOutput:outPipe];
104 [task setStandardError:errPipe];
105 [task launch];
106
107 [[errPipe fileHandleForReading] readToEndOfFileInBackgroundAndNotify];
108
109 NSData* data = [[outPipe fileHandleForReading] readDataToEndOfFile];
110 NSMutableAttributedString* source =
111 [[NSMutableAttributedString alloc] initWithHTML:data
112 options:@{ NSCharacterEncodingDocumentAttribute : @(NSUTF8StringEncoding) }
113 documentAttributes:nil];
114 NSMutableString* stringData = [source mutableString];
115 // PHP uses &nbsp; in the highlighted output, which should be converted
116 // back to normal spaces.
117 [stringData replaceOccurrencesOfString:@"\u00A0" withString:@" " options:0 range:NSMakeRange(0, stringData.length)];
118 [[textView_ textStorage] setAttributedString:source];
119 [source release];
120 } @catch (NSException* exception) {
121 // If the PHP executable is not available then the NSTask will throw an exception
122 [self setPlainTextStringFromFile:f];
123 }
124
125 [ruler_ performLayout];
126 }
127
128 /**
129 * Sets the contents of the SourceView via a string rather than loading from a path
130 */
131 - (void)setString:(NSString*)source asFile:(NSString*)path
132 {
133 // create the temp file
134 NSError* error = nil;
135 NSString* tmpPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"MacGDBpHighlighter"];
136 [source writeToFile:tmpPath atomically:NO encoding:NSUTF8StringEncoding error:&error];
137 if (error) {
138 [textView_ setString:source];
139 return;
140 }
141
142 // highlight the temporary file
143 [self setFile:tmpPath];
144
145 // delete the temp file
146 [[NSFileManager defaultManager] removeItemAtPath:tmpPath error:NULL];
147
148 // plop in our fake path so nobody knows the difference
149 if (path != file_) {
150 [file_ release];
151 file_ = [path copy];
152 }
153
154 [ruler_ performLayout];
155 }
156
157 /**
158 * If an error occurs in reading the highlighted PHP source, this will merely set the string
159 */
160 - (void)errorHighlightingFile:(NSNotification*)notif
161 {
162 NSData* data = [[notif userInfo] objectForKey:NSFileHandleNotificationDataItem];
163 if ([data length] > 0 && file_) // there's something on stderr, so the PHP CLI failed
164 [self setPlainTextStringFromFile:file_];
165 }
166
167 /**
168 * Flip the coordinates
169 */
170 - (BOOL)isFlipped
171 {
172 return YES;
173 }
174
175 /**
176 * Tells the text view to scroll to a certain line
177 */
178 - (void)scrollToLine:(NSUInteger)line
179 {
180 if ([[textView_ textStorage] length] == 0)
181 return;
182
183 // go through the document until we find the NSRange for the line we want
184 NSUInteger rangeIndex = 0;
185 for (NSUInteger i = 0; i < line; i++) {
186 rangeIndex = NSMaxRange([[textView_ string] lineRangeForRange:NSMakeRange(rangeIndex, 0)]);
187 }
188
189 // now get the true start/end markers for it
190 NSUInteger lineStart, lineEnd;
191 [[textView_ string] getLineStart:&lineStart
192 end:NULL
193 contentsEnd:&lineEnd
194 forRange:NSMakeRange(rangeIndex - 1, 0)];
195 [textView_ scrollRangeToVisible:[[textView_ string]
196 lineRangeForRange:NSMakeRange(lineStart, lineEnd - lineStart)]];
197 [scrollView_ setNeedsDisplay:YES];
198 }
199
200 /**
201 * Setup all the subviews for the source metaview
202 */
203 - (void)setupViews
204 {
205 // Create the scroll view.
206 scrollView_ = [[[NSScrollView alloc] initWithFrame:[self bounds]] autorelease];
207 [scrollView_ setHasHorizontalScroller:YES];
208 [scrollView_ setHasVerticalScroller:YES];
209 [scrollView_ setAutohidesScrollers:YES];
210 [scrollView_ setBorderType:NSBezelBorder];
211 [scrollView_ setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
212 [[scrollView_ contentView] setAutoresizesSubviews:YES];
213 [self addSubview:scrollView_];
214
215 // add the text view to the scroll view
216 NSRect textFrame;
217 textFrame.origin = NSMakePoint(0.0, 0.0);
218 textFrame.size = [scrollView_ contentSize];
219 textView_ = [[[BSSourceViewTextView alloc] initWithFrame:textFrame] autorelease];
220 [textView_ setSourceView:self];
221 [textView_ setEditable:NO];
222 [textView_ setFont:[NSFont fontWithName:@"Monaco" size:10.0]];
223 [textView_ setHorizontallyResizable:YES];
224 [textView_ setVerticallyResizable:YES];
225 [textView_ setMinSize:textFrame.size];
226 [textView_ setMaxSize:NSMakeSize(FLT_MAX, FLT_MAX)];
227 [[textView_ textContainer] setContainerSize:NSMakeSize(FLT_MAX, FLT_MAX)];
228 [[textView_ textContainer] setWidthTracksTextView:NO];
229 [[textView_ textContainer] setHeightTracksTextView:NO];
230 [textView_ setAutoresizingMask:NSViewNotSizable];
231 [scrollView_ setDocumentView:textView_];
232
233 // Set up the ruler.
234 ruler_ = [[[BSLineNumberRulerView alloc] initWithSourceView:self] autorelease];
235 [scrollView_ setVerticalRulerView:ruler_];
236 [scrollView_ setHasHorizontalRuler:NO];
237 [scrollView_ setHasVerticalRuler:YES];
238 [scrollView_ setRulersVisible:YES];
239
240 NSArray* types = [NSArray arrayWithObject:NSFilenamesPboardType];
241 [self registerForDraggedTypes:types];
242 }
243
244 /**
245 * Gets the plain-text representation of the file at |filePath| and sets the
246 * contents in the source view.
247 */
248 - (void)setPlainTextStringFromFile:(NSString*)filePath
249 {
250 NSError* error = nil;
251 NSString* contents = [NSString stringWithContentsOfFile:filePath
252 encoding:NSUTF8StringEncoding
253 error:&error];
254 if (error) {
255 NSLog(@"Error reading file at %@: %@", filePath, error);
256 return;
257 }
258 [textView_ setString:contents];
259 }
260
261 // Drag Handlers ///////////////////////////////////////////////////////////////
262
263 /**
264 * Validates an initiated drag operation.
265 */
266 - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender
267 {
268 return NSDragOperationCopy;
269 }
270
271 /**
272 * Performs a dragging operation of files to set the contents of the file.
273 */
274 - (BOOL)performDragOperation:(id<NSDraggingInfo>)sender
275 {
276 NSPasteboard* pboard = [sender draggingPasteboard];
277 if ([[pboard types] containsObject:NSFilenamesPboardType]) {
278 NSArray* files = [pboard propertyListForType:NSFilenamesPboardType];
279 if ([files count]) {
280 NSString* filename = [files objectAtIndex:0];
281 [self setFile:filename];
282 return YES;
283 }
284 }
285 return NO;
286 }
287
288 @end