[66] | 1 | // ICeCoffEE - Internet Config Cocoa Editor Extension
|
---|
| 2 | // Nicholas Riley <mailto:icecoffee@sabi.net>
|
---|
| 3 |
|
---|
| 4 | /* To do/think about:
|
---|
| 5 |
|
---|
| 6 | - Carbon contextual menu plugin which presents Services (yah!)
|
---|
| 7 | for both files and text
|
---|
| 8 | - if it's not a URL, try using TextExtras' open list
|
---|
[74] | 9 | - TXNClick - MLTE has its own support in Jaguar and later, but it's lousy
|
---|
[66] | 10 |
|
---|
| 11 | Done:
|
---|
| 12 |
|
---|
[74] | 13 | - TEClick - TextEdit
|
---|
[66] | 14 | - flash on success (like BBEdit)
|
---|
| 15 | - display dialog on failure (decode OSStatus)
|
---|
[74] | 16 | - adjust URL blinking
|
---|
| 17 | - app exclusion list - make a pref pane (see AquaShade config)
|
---|
[75] | 18 | - _LSCopyApplicationURLsForItemURL - list apps
|
---|
[79] | 19 | - Menu on command-option-click: add bookmark, open with other helper, pass to configurable service, ...?
|
---|
[66] | 20 |
|
---|
| 21 | */
|
---|
| 22 |
|
---|
| 23 | #import "ICeCoffEE.h"
|
---|
| 24 | #import <Carbon/Carbon.h>
|
---|
| 25 | #include <unistd.h>
|
---|
| 26 | #import "ICeCoffEESuper.h"
|
---|
[75] | 27 | #import "ICeCoffEEActionMenu.h"
|
---|
[66] | 28 |
|
---|
[74] | 29 | iccfPrefRec ICCF_prefs;
|
---|
[66] | 30 |
|
---|
[74] | 31 | NSString *ICCF_ErrString(OSStatus err, NSString *context) {
|
---|
| 32 | if (err == noErr || err == userCanceledErr) return nil;
|
---|
[66] | 33 |
|
---|
[74] | 34 | NSString *errNum = [NSString stringWithFormat: @"%ld", err];
|
---|
| 35 | NSString *errDesc = ICCF_LocalizedString(errNum);
|
---|
[66] | 36 |
|
---|
[74] | 37 | if (errDesc == NULL || errDesc == errNum)
|
---|
| 38 | errDesc = [NSString stringWithFormat: ICCF_LocalizedString(@"An unknown error occurred in %@"), context];
|
---|
| 39 |
|
---|
[66] | 40 | return [NSString stringWithFormat: @"%@ (%d)", errDesc, (int)err];
|
---|
| 41 | }
|
---|
| 42 |
|
---|
[74] | 43 | CFStringRef ICCF_CopyErrString(OSStatus err, CFStringRef context) {
|
---|
| 44 | if (err == noErr || err == userCanceledErr) return NULL;
|
---|
| 45 |
|
---|
| 46 | CFStringRef errNum = CFStringCreateWithFormat(NULL, NULL, CFSTR("%ld"), err);
|
---|
| 47 | CFStringRef errDesc = ICCF_CopyLocalizedString(errNum);
|
---|
| 48 |
|
---|
| 49 | if (errDesc == NULL || errDesc == errNum) {
|
---|
| 50 | CFStringRef errDescFormat = ICCF_CopyLocalizedString(CFSTR("An unknown error occurred in %@"));
|
---|
| 51 | if (errDesc != NULL) CFRelease(errDesc);
|
---|
| 52 | errDesc = CFStringCreateWithFormat(NULL, NULL, errDescFormat, context);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | CFStringRef errStr = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@ (%d)"), errDesc, (int)err);
|
---|
| 56 |
|
---|
| 57 | if (errNum != NULL) CFRelease(errNum);
|
---|
| 58 | if (errDesc != NULL) CFRelease(errDesc);
|
---|
| 59 | return errStr;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[66] | 62 | BOOL ICCF_EventIsCommandMouseDown(NSEvent *e) {
|
---|
[74] | 63 | unsigned int modifierFlags = [e modifierFlags];
|
---|
| 64 | return ([e type] == NSLeftMouseDown &&
|
---|
| 65 | (modifierFlags == NSCommandKeyMask || modifierFlags == (NSCommandKeyMask | NSAlternateKeyMask))
|
---|
| 66 | && [e clickCount] == 1);
|
---|
[66] | 67 | }
|
---|
| 68 |
|
---|
[74] | 69 | BOOL ICCF_OptionKeyIsDown() {
|
---|
| 70 | unsigned int modifierFlags = [[NSApp currentEvent] modifierFlags];
|
---|
| 71 | return (modifierFlags & NSAlternateKeyMask) != 0;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[66] | 74 | void ICCF_CheckRange(NSRange range) {
|
---|
[74] | 75 | NSCAssert(range.length > 0, ICCF_LocalizedString(@"No URL is selected"));
|
---|
| 76 | NSCAssert1(range.length <= ICCF_MAX_URL_LEN, ICCF_LocalizedString(@"The potential URL is longer than %lu characters"), ICCF_MAX_URL_LEN);
|
---|
[66] | 77 | }
|
---|
| 78 |
|
---|
| 79 | void ICCF_Delimiters(NSCharacterSet **leftPtr, NSCharacterSet **rightPtr) {
|
---|
| 80 | static NSCharacterSet *urlLeftDelimiters = nil, *urlRightDelimiters = nil;
|
---|
| 81 |
|
---|
| 82 | if (urlLeftDelimiters == nil || urlRightDelimiters == nil) {
|
---|
| 83 | NSMutableCharacterSet *set = [[NSCharacterSet whitespaceAndNewlineCharacterSet] mutableCopy];
|
---|
| 84 | NSMutableCharacterSet *tmpSet;
|
---|
| 85 | [urlLeftDelimiters release];
|
---|
| 86 | [urlRightDelimiters release];
|
---|
| 87 |
|
---|
| 88 | [set autorelease];
|
---|
| 89 | [set formUnionWithCharacterSet: [NSCharacterSet punctuationCharacterSet]];
|
---|
| 90 | [set removeCharactersInString: @";/?:@&=+$,-_.!~*'()%#"]; // RFC 2396 ¤2.2, 2.3, 2.4, plus #
|
---|
| 91 |
|
---|
| 92 | tmpSet = [[set mutableCopy] autorelease];
|
---|
| 93 | [tmpSet formUnionWithCharacterSet: [NSCharacterSet characterSetWithCharactersInString: @"><("]];
|
---|
| 94 | urlLeftDelimiters = [tmpSet copy]; // make immutable again - for efficiency
|
---|
| 95 |
|
---|
| 96 | tmpSet = [[set mutableCopy] autorelease];
|
---|
| 97 | [tmpSet formUnionWithCharacterSet: [NSCharacterSet characterSetWithCharactersInString: @"><)"]];
|
---|
| 98 | urlRightDelimiters = [tmpSet copy]; // make immutable again - for efficiency
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | *leftPtr = urlLeftDelimiters; *rightPtr = urlRightDelimiters;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | static ICInstance ICCF_icInst = NULL;
|
---|
| 105 |
|
---|
| 106 | void ICCF_StartIC() {
|
---|
| 107 | OSStatus err;
|
---|
| 108 |
|
---|
| 109 | if (ICCF_icInst != NULL) {
|
---|
| 110 | ICLog(@"ICCF_StartIC: Internet Config is already running!");
|
---|
| 111 | ICCF_StopIC();
|
---|
| 112 | }
|
---|
[74] | 113 | err = ICStart(&ICCF_icInst, kICCFCreator);
|
---|
| 114 | NSCAssert1(err == noErr, ICCF_LocalizedString(@"Unable to start Internet Config (error %d)"), err);
|
---|
[66] | 115 | }
|
---|
| 116 |
|
---|
| 117 | void ICCF_StopIC() {
|
---|
| 118 | if (ICCF_icInst == NULL) {
|
---|
| 119 | ICLog(@"ICCF_StopIC: Internet Config is not running!");
|
---|
| 120 | } else {
|
---|
| 121 | ICStop(ICCF_icInst);
|
---|
| 122 | ICCF_icInst = NULL;
|
---|
| 123 | }
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | ICInstance ICCF_GetInst() {
|
---|
| 127 | NSCAssert(ICCF_icInst != NULL, @"Internal error: Called ICCF_GetInst without ICCF_StartIC");
|
---|
| 128 | return ICCF_icInst;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[79] | 131 | ConstStringPtr ICCF_GetHint(ICInstance inst, const char *urlData, Size length, long *selStart, long *selEnd, Boolean *needsSlashes) {
|
---|
[74] | 132 | Handle h = NewHandle(0);
|
---|
| 133 | OSStatus err;
|
---|
[79] | 134 |
|
---|
[74] | 135 | if (h == NULL) return NULL;
|
---|
| 136 |
|
---|
| 137 | // parse the URL providing a bogus protocol, to get rid of escaped forms
|
---|
[79] | 138 | err = ICParseURL(inst, "\p*", urlData, length, selStart, selEnd, h);
|
---|
[74] | 139 | if (err != noErr) return NULL;
|
---|
| 140 |
|
---|
| 141 | // scan through the parsed URL looking for characters not found in email addresses
|
---|
| 142 | Size hSize = GetHandleSize(h);
|
---|
| 143 | if (hSize == 0) return NULL;
|
---|
| 144 |
|
---|
| 145 | const char *urlParsed = *h;
|
---|
| 146 | long i = 0;
|
---|
| 147 | Boolean sawAt = false;
|
---|
[79] | 148 | if (hSize >= 2 && urlParsed[0] == '*' && urlParsed[1] == ':') {
|
---|
[74] | 149 | // this is an IC-inserted protocol; skip over it
|
---|
| 150 | i = 2;
|
---|
[79] | 151 | *needsSlashes = (hSize < i + 2 || urlParsed[i] != '/' || urlParsed[i + 1] != '/');
|
---|
| 152 | } else *needsSlashes = false;
|
---|
[74] | 153 | for ( ; i < hSize ; i++) {
|
---|
| 154 | char c = urlParsed[i];
|
---|
| 155 | if (c == '@') {
|
---|
| 156 | sawAt = true;
|
---|
| 157 | } else if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
|
---|
| 158 | (c == '+' || c == '-' || c == '_' || c == '!' || c == '.'))) {
|
---|
| 159 | DisposeHandle(h);
|
---|
| 160 | return "\phttp";
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 | DisposeHandle(h);
|
---|
[79] | 164 | if (sawAt) {
|
---|
| 165 | *needsSlashes = false;
|
---|
| 166 | return "\pmailto";
|
---|
| 167 | }
|
---|
| 168 | return "\phttp";
|
---|
[74] | 169 | }
|
---|
| 170 |
|
---|
[79] | 171 | static const char *kICSlashes = "//";
|
---|
| 172 |
|
---|
| 173 | void ICCF_AddSlashes(Handle h, ConstStringPtr hint) {
|
---|
| 174 | Size sizeBefore = GetHandleSize(h);
|
---|
| 175 | unsigned char hintLength = StrLength(hint);
|
---|
| 176 | char *copy = (char *)malloc(sizeBefore);
|
---|
| 177 | memcpy(copy, *h, sizeBefore);
|
---|
| 178 | ICLog(@"ICCF_AddSlashes before: |%s|\n", *h);
|
---|
| 179 | ReallocateHandle(h, sizeBefore + 2);
|
---|
| 180 |
|
---|
| 181 | // if *h begins with '<hint>:', then copy the slashes after it
|
---|
| 182 | if (sizeBefore > hintLength + 1 && strncmp(&hint[1], copy, hintLength) == 0 && copy[hintLength] == ':') {
|
---|
| 183 | memcpy(*h, copy, hintLength + 1);
|
---|
| 184 | memcpy(*h + hintLength + 1, kICSlashes, 2);
|
---|
| 185 | memcpy(*h + hintLength + 3, ©[hintLength + 1], sizeBefore - hintLength - 1);
|
---|
| 186 | } else {
|
---|
| 187 | memcpy(*h, kICSlashes, 2);
|
---|
| 188 | memcpy(*h + 2, copy, sizeBefore);
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | free(copy);
|
---|
| 192 | ICLog(@"ICCF_AddSlashes after: |%s|\n", *h);
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[66] | 195 | void ICCF_ParseURL(NSString *string, NSRange *range) {
|
---|
| 196 | OSStatus err;
|
---|
| 197 | Handle h;
|
---|
| 198 | long selStart, selEnd;
|
---|
| 199 | char *urlData = NULL;
|
---|
| 200 |
|
---|
| 201 | NSCAssert(range->length == [string length], @"Internal error: URL string is wrong length");
|
---|
| 202 |
|
---|
| 203 | NS_DURING
|
---|
| 204 | if ([[NSCharacterSet characterSetWithCharactersInString: @";,."] characterIsMember:
|
---|
| 205 | [string characterAtIndex: range->length - 1]]) {
|
---|
| 206 | range->length--;
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | string = [string substringToIndex: range->length];
|
---|
| 210 |
|
---|
| 211 | ICLog(@"Parsing URL |%@|", string);
|
---|
| 212 |
|
---|
| 213 | urlData = (char *)malloc( (range->length + 1) * sizeof(char));
|
---|
[74] | 214 | NSCAssert(urlData != NULL, @"Internal error: canÕt allocate memory for URL string");
|
---|
[66] | 215 |
|
---|
| 216 | selStart = 0; selEnd = range->length;
|
---|
| 217 |
|
---|
| 218 | [string getCString: urlData];
|
---|
| 219 |
|
---|
| 220 | h = NewHandle(0);
|
---|
[74] | 221 | NSCAssert(h != NULL, @"Internal error: canÕt allocate URL handle");
|
---|
| 222 |
|
---|
[66] | 223 | err = ICParseURL(ICCF_GetInst(), "\pmailto", urlData, range->length, &selStart, &selEnd, h);
|
---|
[74] | 224 | DisposeHandle(h);
|
---|
| 225 |
|
---|
[66] | 226 | ICCF_OSErrCAssert(err, @"ICParseURL");
|
---|
| 227 |
|
---|
[74] | 228 | range->length = selEnd - selStart;
|
---|
[66] | 229 | range->location += selStart;
|
---|
| 230 | NS_HANDLER
|
---|
| 231 | free(urlData);
|
---|
| 232 | [localException raise];
|
---|
| 233 | NS_ENDHANDLER
|
---|
| 234 |
|
---|
| 235 | free(urlData);
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[74] | 238 | void ICCF_LaunchURL(NSString *string, BOOL chooseApp) {
|
---|
[66] | 239 | OSStatus err;
|
---|
| 240 | long selStart, selEnd;
|
---|
| 241 | unsigned len = [string length];
|
---|
| 242 |
|
---|
[79] | 243 | Handle h = NULL;
|
---|
[66] | 244 |
|
---|
| 245 | NS_DURING
|
---|
[79] | 246 | h = NewHandle(len);
|
---|
| 247 | if (h == NULL)
|
---|
| 248 | ICCF_OSErrCAssert(MemError(), @"NewHandle");
|
---|
[66] | 249 |
|
---|
[79] | 250 | if (CFStringGetBytes((CFStringRef)string, CFRangeMake(0, len), kCFStringEncodingASCII, '\0', false, *h, len, NULL) != len)
|
---|
| 251 | ICCF_OSErrCAssert(kTECNoConversionPathErr, @"CFStringGetBytes");
|
---|
[66] | 252 |
|
---|
| 253 | selStart = 0; selEnd = len;
|
---|
| 254 |
|
---|
[79] | 255 | Boolean needsSlashes;
|
---|
| 256 | ConstStringPtr hint = ICCF_GetHint(ICCF_GetInst(), *h, len, &selStart, &selEnd, &needsSlashes);
|
---|
[74] | 257 | NSCAssert(hint != NULL, @"Internal error: canÕt get protocol hint for URL");
|
---|
| 258 |
|
---|
[79] | 259 | if (needsSlashes) {
|
---|
| 260 | ICCF_AddSlashes(h, hint);
|
---|
| 261 | len = selEnd = GetHandleSize(h);
|
---|
| 262 | }
|
---|
| 263 |
|
---|
[74] | 264 | if (chooseApp) {
|
---|
[79] | 265 | err = ICCF_DoURLActionMenu(ICCF_GetInst(), hint, *h, selStart, selEnd);
|
---|
[75] | 266 | ICCF_OSErrCAssert(err, @"ICCF_DoURLActionMenu");
|
---|
[74] | 267 | } else {
|
---|
[79] | 268 | err = ICLaunchURL(ICCF_GetInst(), hint, *h, len, &selStart, &selEnd);
|
---|
[74] | 269 | ICCF_OSErrCAssert(err, @"ICLaunchURL");
|
---|
| 270 | }
|
---|
[66] | 271 |
|
---|
| 272 | NS_HANDLER
|
---|
[79] | 273 | DisposeHandle(h);
|
---|
[66] | 274 | [localException raise];
|
---|
| 275 | NS_ENDHANDLER
|
---|
| 276 |
|
---|
[79] | 277 | DisposeHandle(h);
|
---|
[66] | 278 | }
|
---|
| 279 |
|
---|
[74] | 280 | // XXX not sure what to do if there's already a selection; BBEdit and MLTE extend it, Tex-Edit Plus doesn't.
|
---|
[66] | 281 | // RFC-ordained max URL length, just to avoid passing IC multi-megabyte documents
|
---|
| 282 | #if ICCF_DEBUG
|
---|
| 283 | const long ICCF_MAX_URL_LEN = 1024; // XXX change later
|
---|
| 284 | #else
|
---|
| 285 | const long ICCF_MAX_URL_LEN = 1024;
|
---|
| 286 | #endif
|
---|
| 287 |
|
---|
[74] | 288 | Boolean ICCF_enabled = true;
|
---|
[66] | 289 |
|
---|
[74] | 290 | BOOL ICCF_HandleException(NSException *e) {
|
---|
| 291 | if ([e reason] == nil || [[e reason] length] == 0)
|
---|
| 292 | return NO;
|
---|
| 293 |
|
---|
| 294 | if (ICCF_prefs.errorSoundEnabled) NSBeep();
|
---|
| 295 | if (!ICCF_prefs.errorDialogEnabled) return YES;
|
---|
| 296 |
|
---|
| 297 | int result = NSRunAlertPanel(ICCF_LocalizedString(@"AlertTitle"), ICCF_LocalizedString(@"AlertMessage%@"), nil, nil, ICCF_LocalizedString(@"AlertDisableButton"), e);
|
---|
[66] | 298 | if (result != NSAlertDefaultReturn) {
|
---|
[74] | 299 | result = NSRunAlertPanel(ICCF_LocalizedString(@"DisableAlertTitle"), ICCF_LocalizedString(@"DisableAlertMessage"), ICCF_LocalizedString(@"DisableAlertDisableButton"), ICCF_LocalizedString(@"DisableAlertDontDisableButton"), nil);
|
---|
[66] | 300 | if (result == NSAlertDefaultReturn)
|
---|
| 301 | ICCF_enabled = NO;
|
---|
| 302 | }
|
---|
[74] | 303 | return YES;
|
---|
[66] | 304 | }
|
---|
| 305 |
|
---|
| 306 | void ICCF_LaunchURLFromTextView(NSTextView *self) {
|
---|
| 307 | NSCharacterSet *urlLeftDelimiters = nil, *urlRightDelimiters = nil;
|
---|
| 308 | NSRange range = [self selectedRange], delimiterRange;
|
---|
| 309 | NSColor *insertionPointColor = [self insertionPointColor];
|
---|
| 310 | NSString *s = [[self textStorage] string]; // according to the class documentation, sending 'string' is guaranteed to be O(1)
|
---|
| 311 | unsigned extraLen;
|
---|
| 312 | int i;
|
---|
| 313 |
|
---|
| 314 | NS_DURING
|
---|
| 315 |
|
---|
[74] | 316 | NSCAssert(range.location != NSNotFound, ICCF_LocalizedString(@"There is no insertion point or selection in the text field where you clicked"));
|
---|
| 317 | NSCAssert(s != nil, ICCF_LocalizedString(@"Sorry, ICeCoffEE is unable to locate the insertion point or selection"));
|
---|
[66] | 318 |
|
---|
| 319 | ICCF_StartIC();
|
---|
| 320 |
|
---|
[74] | 321 | NSCAssert([s length] != 0, ICCF_LocalizedString(@"No text was found"));
|
---|
[66] | 322 |
|
---|
| 323 | if (range.location == [s length]) range.location--; // work around bug in selectionRangeForProposedRange (r. 2845418)
|
---|
| 324 |
|
---|
| 325 | range = [self selectionRangeForProposedRange: range granularity: NSSelectByWord];
|
---|
| 326 |
|
---|
| 327 | // However, NSSelectByWord does not capture even the approximate boundaries of a URL
|
---|
| 328 | // (text to a space/line ending character); it'll stop at a period in the middle of a hostname.
|
---|
| 329 | // So, we expand it as follows:
|
---|
| 330 |
|
---|
| 331 | ICCF_CheckRange(range);
|
---|
| 332 |
|
---|
| 333 | ICCF_Delimiters(&urlLeftDelimiters, &urlRightDelimiters);
|
---|
| 334 |
|
---|
| 335 | // XXX instead of 0, make this stop at the max URL length to prevent protracted searches
|
---|
| 336 | // add 1 to range to trap delimiters that are on the edge of the selection (i.e., <...)
|
---|
| 337 | delimiterRange = [s rangeOfCharacterFromSet: urlLeftDelimiters
|
---|
| 338 | options: NSLiteralSearch | NSBackwardsSearch
|
---|
| 339 | range: NSMakeRange(0, range.location + (range.location != [s length]))];
|
---|
| 340 | if (delimiterRange.location == NSNotFound) {
|
---|
| 341 | // extend to beginning of string
|
---|
| 342 | range.length += range.location;
|
---|
| 343 | range.location = 0;
|
---|
| 344 | } else {
|
---|
| 345 | NSCAssert(delimiterRange.length == 1, @"Internal error: delimiter matched range is not of length 1");
|
---|
| 346 | range.length += range.location - delimiterRange.location - 1;
|
---|
| 347 | range.location = delimiterRange.location + 1;
|
---|
| 348 | }
|
---|
| 349 |
|
---|
| 350 | ICCF_CheckRange(range);
|
---|
| 351 |
|
---|
| 352 | // XXX instead of length of string, make this stop at the max URL length to prevent protracted searches
|
---|
| 353 | // add 1 to range to trap delimiters that are on the edge of the selection (i.e., ...>)
|
---|
| 354 | extraLen = [s length] - range.location - range.length;
|
---|
| 355 | delimiterRange = [s rangeOfCharacterFromSet: urlRightDelimiters
|
---|
| 356 | options: NSLiteralSearch
|
---|
| 357 | range: NSMakeRange(range.location + range.length - (range.length != 0),
|
---|
| 358 | extraLen + (range.length != 0))];
|
---|
| 359 | if (delimiterRange.location == NSNotFound) {
|
---|
| 360 | // extend to end of string
|
---|
| 361 | range.length += extraLen;
|
---|
| 362 | } else {
|
---|
| 363 | NSCAssert(delimiterRange.length == 1, @"Internal error: delimiter matched range is not of length 1");
|
---|
| 364 | range.length += delimiterRange.location - range.location - range.length;
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | ICCF_CheckRange(range);
|
---|
| 368 |
|
---|
| 369 | ICCF_ParseURL([s substringWithRange: range], &range);
|
---|
| 370 |
|
---|
| 371 | [self setSelectedRange: range affinity: NSSelectionAffinityDownstream stillSelecting: NO];
|
---|
| 372 | [self display];
|
---|
| 373 |
|
---|
[74] | 374 | ICCF_LaunchURL([s substringWithRange: range], ICCF_OptionKeyIsDown());
|
---|
[66] | 375 |
|
---|
[74] | 376 | if (ICCF_prefs.textBlinkEnabled) {
|
---|
| 377 | for (i = 0 ; i < ICCF_prefs.textBlinkCount ; i++) {
|
---|
| 378 | NSRange emptyRange = {range.location, 0};
|
---|
| 379 | [self setSelectedRange: emptyRange affinity: NSSelectionAffinityDownstream stillSelecting: YES];
|
---|
| 380 | [self display];
|
---|
| 381 | usleep(kICBlinkDelayUsecs);
|
---|
| 382 | [self setInsertionPointColor: [self backgroundColor]];
|
---|
| 383 | [self setSelectedRange: range affinity: NSSelectionAffinityDownstream stillSelecting: YES];
|
---|
| 384 | [self display];
|
---|
| 385 | usleep(kICBlinkDelayUsecs);
|
---|
| 386 | }
|
---|
| 387 | }
|
---|
| 388 |
|
---|
[66] | 389 | NS_HANDLER
|
---|
| 390 | ICCF_HandleException(localException);
|
---|
| 391 | NS_ENDHANDLER
|
---|
| 392 |
|
---|
| 393 | ICCF_StopIC();
|
---|
| 394 | [self setInsertionPointColor: insertionPointColor];
|
---|
| 395 | }
|
---|
| 396 |
|
---|
| 397 | NSString * const ICCF_SERVICES_ITEM = @"ICeCoffEE Services Item";
|
---|
| 398 |
|
---|
| 399 | NSMenuItem *ICCF_ServicesMenuItem() {
|
---|
| 400 | NSMenuItem *servicesItem;
|
---|
| 401 | NSMenu *servicesMenu;
|
---|
| 402 | // XXX better to just use [[NSApp servicesMenu] title]? That grabs the title from the existing Services submenu.
|
---|
| 403 | NSString *servicesTitle = [[NSBundle bundleWithIdentifier: @"com.apple.AppKit"] localizedStringForKey: @"Services" value: nil table: @"ServicesMenu"];
|
---|
| 404 | if (servicesTitle == nil) {
|
---|
| 405 | ICLog(@"CanÕt get localized text for ÒServicesÓ in AppKit.framework");
|
---|
| 406 | servicesTitle = @"Services";
|
---|
| 407 | }
|
---|
| 408 | servicesMenu = [[NSMenu alloc] initWithTitle: servicesTitle];
|
---|
| 409 | servicesItem = [[NSMenuItem alloc] initWithTitle: servicesTitle action:nil keyEquivalent:@""];
|
---|
| 410 | [[NSApplication sharedApplication] setServicesMenu: servicesMenu];
|
---|
| 411 | [servicesItem setSubmenu: servicesMenu];
|
---|
| 412 | [servicesItem setRepresentedObject: ICCF_SERVICES_ITEM];
|
---|
| 413 | [servicesMenu release];
|
---|
| 414 | return servicesItem;
|
---|
| 415 | }
|
---|
| 416 |
|
---|
[74] | 417 | void ICCF_AddRemoveServicesMenu() {
|
---|
| 418 | // needed because:
|
---|
| 419 | // (a) we get called before the runloop has properly started and will crash if we don't delay on app startup
|
---|
| 420 | // (b) the APE message handler calls us from another thread and nothing happens if we try to add a menu on it
|
---|
| 421 | [ICeCoffEE performSelectorOnMainThread: @selector(IC_addRemoveServicesMenu) withObject: nil waitUntilDone: NO];
|
---|
[66] | 422 | }
|
---|
| 423 |
|
---|
| 424 | NSMenu *ICCF_MenuForEvent(NSTextView *self, NSMenu *contextMenu, NSEvent *e) {
|
---|
| 425 | if (contextMenu != nil && [e type] == NSRightMouseDown || ([e type] == NSLeftMouseDown && [e modifierFlags] & NSControlKeyMask)) {
|
---|
| 426 | int servicesItemIndex = [contextMenu indexOfItemWithRepresentedObject: ICCF_SERVICES_ITEM];
|
---|
[74] | 427 | if (servicesItemIndex == -1 && ICCF_prefs.servicesInContextualMenu) {
|
---|
[66] | 428 | [contextMenu addItem: [NSMenuItem separatorItem]];
|
---|
| 429 | [contextMenu addItem: ICCF_ServicesMenuItem()];
|
---|
[74] | 430 | } else if (servicesItemIndex != -1 && !ICCF_prefs.servicesInContextualMenu) {
|
---|
| 431 | [contextMenu removeItemAtIndex: servicesItemIndex];
|
---|
| 432 | [contextMenu removeItemAtIndex: servicesItemIndex - 1];
|
---|
[66] | 433 | }
|
---|
| 434 | }
|
---|
| 435 | return contextMenu;
|
---|
| 436 | }
|
---|
| 437 |
|
---|
[74] | 438 | @implementation ICeCoffEE
|
---|
| 439 |
|
---|
| 440 | + (void)IC_addRemoveServicesMenu;
|
---|
[66] | 441 | {
|
---|
| 442 | NSMenu *mainMenu = [[NSApplication sharedApplication] mainMenu];
|
---|
[74] | 443 | static NSMenuItem *servicesItem = nil;
|
---|
| 444 |
|
---|
| 445 | if (servicesItem == nil && ICCF_prefs.servicesInMenuBar) {
|
---|
| 446 | servicesItem = [ICCF_ServicesMenuItem() retain];
|
---|
[66] | 447 |
|
---|
[74] | 448 | int insertLoc = [mainMenu indexOfItemWithSubmenu: [NSApp windowsMenu]];
|
---|
| 449 | if (insertLoc == -1)
|
---|
| 450 | insertLoc = [mainMenu numberOfItems];
|
---|
[66] | 451 |
|
---|
[74] | 452 | [mainMenu insertItem: servicesItem atIndex: insertLoc];
|
---|
| 453 | } else if (servicesItem != nil && !ICCF_prefs.servicesInMenuBar) {
|
---|
| 454 | [mainMenu removeItem: servicesItem];
|
---|
| 455 | [servicesItem release];
|
---|
| 456 | servicesItem = nil;
|
---|
| 457 | }
|
---|
[66] | 458 | }
|
---|
| 459 |
|
---|
| 460 | // XXX localization?
|
---|
| 461 | - (NSMenu *)menuForEvent:(NSEvent *)e;
|
---|
| 462 | {
|
---|
| 463 | NSMenu *myMenu = [super menuForEvent: e];
|
---|
| 464 | return ICCF_MenuForEvent(self, myMenu, e);
|
---|
| 465 | }
|
---|
| 466 |
|
---|
| 467 | - (void)mouseDown:(NSEvent *)e;
|
---|
| 468 | {
|
---|
| 469 | #if ICCF_DEBUG
|
---|
| 470 | static BOOL down = NO;
|
---|
| 471 | if (down) {
|
---|
| 472 | ICLog(@"recursive invocation!");
|
---|
| 473 | return;
|
---|
| 474 | }
|
---|
| 475 | down = YES;
|
---|
| 476 | ICLog(@"ICeCoffEE down: %@", e);
|
---|
| 477 | #endif
|
---|
| 478 | // we don't actually get a mouseUp event, just wait for mouseDown to return
|
---|
| 479 | [super mouseDown: e];
|
---|
[74] | 480 | if (!ICCF_enabled || !ICCF_prefs.commandClickEnabled) {
|
---|
| 481 | #if ICCF_DEBUG
|
---|
| 482 | down = NO;
|
---|
| 483 | #endif
|
---|
| 484 | return;
|
---|
| 485 | }
|
---|
| 486 | // don't want command-shift-click, etc. to trigger
|
---|
[66] | 487 | if (ICCF_EventIsCommandMouseDown(e)) {
|
---|
| 488 | NSEvent *upEvent = [[self window] currentEvent];
|
---|
| 489 | NSPoint downPt = [e locationInWindow];
|
---|
| 490 | NSPoint upPt = [upEvent locationInWindow];
|
---|
| 491 | ICLog(@"next: %@", upEvent);
|
---|
| 492 | NSAssert([upEvent type] == NSLeftMouseUp, @"NSTextView mouseDown: did not return with current event as mouse up!");
|
---|
[74] | 493 | if (abs(downPt.x - upPt.x) <= kICHysteresisPixels && abs(downPt.y - upPt.y) <= kICHysteresisPixels) {
|
---|
[66] | 494 | ICCF_LaunchURLFromTextView(self);
|
---|
| 495 | }
|
---|
| 496 | }
|
---|
| 497 | #if ICCF_DEBUG
|
---|
| 498 | down = NO;
|
---|
| 499 | #endif
|
---|
| 500 | }
|
---|
| 501 |
|
---|
| 502 | @end
|
---|