-
Notifications
You must be signed in to change notification settings - Fork 7
/
HelpManager.m
388 lines (328 loc) · 11.5 KB
/
HelpManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
* R.app : a Cocoa front end to: "R A Computer Language for Statistical Data Analysis"
*
* R.app Copyright notes:
* Copyright (C) 2004-5 The R Foundation
* written by Stefano M. Iacus and Simon Urbanek
*
*
* R Copyright notes:
* Copyright (C) 1995-1996 Robert Gentleman and Ross Ihaka
* Copyright (C) 1998-2001 The R Development Core Team
* Copyright (C) 2002-2004 The R Foundation
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* A copy of the GNU General Public License is available via WWW at
* http://www.gnu.org/copyleft/gpl.html. You can also obtain it by
* writing to the Free Software Foundation, Inc., 59 Temple Place,
* Suite 330, Boston, MA 02111-1307 USA.
*/
#import "RGUI.h"
#import "HelpManager.h"
#import "RController.h"
#import "REngine/REngine.h"
#import "RegexKitLite.h"
static id sharedHMController;
@implementation HelpManager
- (id)init
{
self = [super init];
if (self) {
sharedHMController = self;
home = nil;
searchType = kExactMatch;
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver: self];
[super dealloc];
}
- (IBAction)runHelpSearch:(id)sender
{
if([[sender stringValue] length]==0)
return;
NSString *searchString;
NSCharacterSet *charSet;
charSet = [NSCharacterSet characterSetWithCharactersInString:@"'\""];
searchString = [[sender stringValue] stringByTrimmingCharactersInSet:charSet];
SLog(@"runHelpSearch: <%@>", searchString);
// [self sendInput:[NSString stringWithFormat:@"help(\"%@\")", searchString]];
if(searchType == kFuzzyMatch){
[[REngine mainEngine] executeString:[NSString stringWithFormat:@"print(help.search(\"%@\"))", searchString]];
[sender setStringValue:@""];
} else {
[self showHelpFor: searchString];
}
}
- (void)showHelpUsingFile: (NSString *)file topic: (NSString*) topic
{
if (!file) return;
if (!topic) topic=@"<unknown>";
NSString *url = nil;
if ([file hasPrefix:@"http://"])
url = file;
else {
int port = [[RController sharedController] helpServerPort];
if (port == 0) {
NSRunInformationalAlertPanel(NLS(@"Cannot start HTML help server."), NLS(@"Help"), NLS(@"Ok"), nil, nil);
return;
}
if (!home) home = [[RController sharedController] home];
if ([file hasPrefix:home])
file = [file substringFromIndex:[home length]];
url = [NSString stringWithFormat:@"http://localhost:%d%@", port, file];
}
SLog(@"HelpManager.showHelpUsingFile:\"%@\", topic=%@, URL=%@", file, topic, url);
if(url != nil) {
if ([Preferences flagForKey:kExternalHelp withDefault:NO])
[[REngine mainEngine] executeString:[NSString stringWithFormat:@"browseURL(\"%@\")", url]];
else {
[[HelpView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
[helpWindow makeKeyAndOrderFront:self];
}
}
}
- (IBAction)executeSelection:(id)sender
{
DOMRange *dr = [HelpView selectedDOMRange];
if (dr) { /* we don't do line-exec since we don't get the text outside the selection */
NSString *stx = [dr markupString];
// Ok, some simple processing here - it may not work in all cases
stx = [stx stringByReplacingOccurrencesOfRegex:@"(?i)<br[^>]*?>" withString:@"\n"];
stx = [stx stringByReplacingOccurrencesOfRegex:@"<[^>]*?>" withString:@""];
stx = [stx stringByReplacingOccurrencesOfString:@"<" withString:@"<"];
stx = [stx stringByReplacingOccurrencesOfString:@">" withString:@">"];
stx = [stx stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
[[RController sharedController] sendInput:stx];
}
}
- (void)showHelpFor:(NSString *)topic
{
NSString *searchString;
NSCharacterSet *charSet;
if (!topic) return; /* should we issue an error? This happens only if the encoding is wrong */
charSet = [NSCharacterSet characterSetWithCharactersInString:@"'\""];
searchString = [topic stringByTrimmingCharactersInSet:charSet];
SLog(@"showHelpFor: <%@>", searchString);
if(searchType == kFuzzyMatch) {
[[REngine mainEngine] executeString:[NSString stringWithFormat:@"print(help.search(\"%@\"))", searchString]];
return;
}
REngine *re = [REngine mainEngine];
RSEXP *x= [re evaluateString:[NSString stringWithFormat:@"as.character(help(\"%@\", help_type='html'))",searchString]];
if ((x==nil) || ([x string]==NULL)) {
NSString *topicString = [NSString stringWithFormat:@"Topic: %@", searchString];
int res = NSRunInformationalAlertPanel(NLS(@"Can't find help for topic, would you like to expand the search?"), topicString, NLS(@"No"), NLS(@"Yes"), nil);
if (!res)
[[REngine mainEngine] executeString:[NSString stringWithFormat:@"print(help.search(\"%@\"))", searchString]];
else {
// if user dismiss alert panel set focus back to caller window
NSArray *orderedWindows = [NSApp orderedWindows];
int i;
for(i=0; i<[orderedWindows count]; i++) {
if([[orderedWindows objectAtIndex:i] isVisible]) {
[[orderedWindows objectAtIndex:i] makeKeyAndOrderFront:nil];
return;
}
}
}
return;
}
[x release];
[re executeString:[NSString stringWithFormat:@"print(help(\"%@\", help_type='html'))",searchString]];
}
- (NSWindow*) window
{
return helpWindow;
}
- (WebView*)webView
{
return HelpView;
}
- (IBAction)showMainHelp:(id)sender
{
REngine *re = [REngine mainEngine];
RSEXP *x = [re evaluateString:@"try(getOption(\"main.help.url\"))"];
if ((x==nil) | ([x string]==nil)){
[re executeString:@"try(main.help.url())"];
[x release];
x = [re evaluateString:@"try(getOption(\"main.help.url\"))"];
if((x == nil) | ([x string]==nil)){
[x release];
return;
}
}
NSString *url = [x string];
if(url != nil)
[[HelpView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
[helpWindow makeKeyAndOrderFront:self];
[x release];
}
- (IBAction)showRFAQ:(id)sender
{
NSString *url = [[NSBundle mainBundle] resourcePath];
if (!url) {
REngine *re = [REngine mainEngine];
RSEXP *x= [re evaluateString:@"file.path(R.home(),\"RMacOSX-FAQ.html\")"];
if(x==nil)
return;
url = [x string];
[x release];
if (url) url = [NSString stringWithFormat:@"file://%@", url];
} else
url = [NSString stringWithFormat:@"file://%@/RMacOSX-FAQ.html", url];
if(url != nil) {
[[HelpView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
[helpWindow makeKeyAndOrderFront:self];
}
}
- (IBAction)whatsNew:(id)sender
{
SLog(@"What's New");
REngine *re = [REngine mainEngine];
/* syntax-highlighting kills us, so we use TextEdit for now */
// [re executeString:@"system(paste('open -a /Applications/TextEdit.app',file.path(R.home(),'NEWS')))"];
// {
// NSBundle* myBundle = [NSBundle mainBundle];
// if (myBundle)
// system([[NSString stringWithFormat:@"open -a /Applications/TextEdit.app \"%@/NEWS\"", [myBundle resourcePath]] UTF8String]);
// }
NSBundle *myBundle = [NSBundle mainBundle];
if(myBundle) {
SLog(@" - resource path: %@", [myBundle resourcePath]);
RSEXP *xx = [re evaluateString:[NSString stringWithFormat:@"file.show('%@/NEWS')", [[myBundle resourcePath] stringByReplacingOccurrencesOfString:@"'" withString:@"\\'"]]];
if(xx) [xx release];
}
RSEXP *x = [re evaluateString:@"file.show(file.path(R.home(),\"NEWS\"))"];
if(!x) return;
[x release];
}
+ (id) sharedController{
return sharedHMController;
}
- (void)showHelpFileForURL:(NSURL*)url
{
if(url != nil) {
SLog(@"HelpManager:showHelpFileForURL %@", [url absoluteString]);
[[HelpView mainFrame] loadRequest:[NSURLRequest requestWithURL:url]];
[helpWindow makeKeyAndOrderFront:self];
return;
}
NSBeep();
}
- (IBAction)printDocument:(id)sender
{
//TODO: for now, if displayed doc is a PDF open it via "Preview" for printing
NSString *currentURL = [[HelpView mainFrameURL] lowercaseString];
if([currentURL hasSuffix:@".pdf"]) {
REngine *re = [REngine mainEngine];
if (![re beginProtected]) {
SLog(@"HelpManager.printPDF bailed because protected REngine entry failed [***]");
return;
}
[re executeString:[NSString stringWithFormat:@"system('open \"%@\"', intern=TRUE, wait=FALSE)", currentURL]];
[re endProtected];
return;
}
NSPrintInfo *printInfo;
NSPrintOperation *printOp;
printInfo = [NSPrintInfo sharedPrintInfo];
[printInfo setHorizontalPagination: NSFitPagination];
[printInfo setVerticalPagination: NSAutoPagination];
[printInfo setVerticallyCentered:NO];
printOp = [NSPrintOperation printOperationWithView:[[[HelpView mainFrame] frameView] documentView]
printInfo:printInfo];
[printOp setShowPanels:YES];
[printOp runOperationModalForWindow:[self window]
delegate:self
didRunSelector:@selector(sheetDidEnd:returnCode:contextInfo:)
contextInfo:@""];
}
- (void) setSearchTypeViaSender:(id)sender
{
if(sender == nil) {
[self setSearchType:kExactMatch];
return;
}
int type = [sender tag];
if (type==kFuzzyMatch || type==kExactMatch) {
[self setSearchType:type];
NSMenu *m = [(NSSearchFieldCell*)sender menu];
if(!m) return;
[[m itemWithTag:kFuzzyMatch] setState:(searchType==kFuzzyMatch)?NSOnState:NSOffState];
[[m itemWithTag:kExactMatch] setState:(searchType==kExactMatch)?NSOnState:NSOffState];
}
}
- (void) awakeFromNib
{
[self setSearchTypeViaSender:nil];
}
- (IBAction)changeSearchType:(id)sender
{
[self setSearchTypeViaSender:sender];
}
- (void)setSearchType:(int)type
{
if(type == kExactMatch || type == kFuzzyMatch) {
if(type != searchType) {
SLog(@"HelpManger - searchType was changed from %d to %d", searchType, type);
searchType = type;
// Update searchField's searchMenuTemplate
NSMenu *m = [[searchField cell] searchMenuTemplate];
[[m itemWithTag:kExactMatch] setState:(type == kExactMatch) ? NSOnState : NSOffState];
[[m itemWithTag:kFuzzyMatch] setState:(type == kExactMatch) ? NSOffState : NSOnState];
[[searchField cell] setSearchMenuTemplate:m];
// If searchType was changed notify all other help search fields
[[NSNotificationCenter defaultCenter] postNotificationName:@"HelpSearchTypeChanged" object:nil];
}
}
}
- (int)searchType
{
return searchType;
}
- (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
}
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
[back setEnabled: [sender canGoBack]];
[forward setEnabled: [sender canGoForward]];
}
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
if ([menuItem action] == @selector(executeSelection:)) {
return ([HelpView selectedDOMRange] == nil) ? NO : YES;
}
return YES;
}
- (void)supportsWebViewSwipingInHistory
{
return;
}
- (void)supportsWebViewMagnifying
{
return;
}
- (void)sheetDidEnd:(id)sheet returnCode:(int)returnCode contextInfo:(NSString *)contextInfo
{
SLog(@"HelpManger: sheetDidEnd: returnCode: %d contextInfo: %@", returnCode, contextInfo);
// Order out the sheet - could be a NSPanel or NSWindow
if ([sheet respondsToSelector:@selector(orderOut:)]) {
[sheet orderOut:nil];
}
else if ([sheet respondsToSelector:@selector(window)]) {
[[sheet window] orderOut:nil];
}
[helpWindow makeKeyAndOrderFront:nil];
}
@end