Index: trunk/ICeCoffEE/ICeCoffEE/ICeCoffEE.m
===================================================================
--- trunk/ICeCoffEE/ICeCoffEE/ICeCoffEE.m	(revision 381)
+++ trunk/ICeCoffEE/ICeCoffEE/ICeCoffEE.m	(revision 388)
@@ -7,5 +7,5 @@
 #import "ICeCoffEESuper.h"
 #import "ICeCoffEEServices.h"
-#import "ICeCoffEETrigger.h"
+#import "ICeCoffEETextViewTrigger.h"
 #import "ICeCoffEEParser.h"
 
@@ -190,5 +190,5 @@
 Boolean ICCF_enabled = true;
 
-BOOL ICCF_HandleException(NSException *e) {
+BOOL ICCF_HandleException(NSException *e, NSEvent *event) {
     if ([e reason] == nil || [[e reason] length] == 0)
         return NO;
@@ -196,4 +196,7 @@
     if (ICCF_prefs.errorSoundEnabled) NSBeep();
     if (!ICCF_prefs.errorDialogEnabled) return YES;
+    
+    [[NSApplication sharedApplication] activateIgnoringOtherApps: YES];
+    [[event window] makeKeyAndOrderFront: nil];
     
     int result = NSRunAlertPanel(ICCF_LocalizedString(@"AlertTitle"), ICCF_LocalizedString(@"AlertMessage%@"), nil, nil, ICCF_LocalizedString(@"AlertDisableButton"), e);
@@ -208,34 +211,27 @@
 
 void ICCF_LaunchURLFromTextView(NSTextView *self, NSEvent *triggeringEvent) {
-    NSRange range = [self selectedRange];
     NSColor *insertionPointColor = [self insertionPointColor];
-    NSString *s = [[self textStorage] string]; // according to the class documentation, sending 'string' is guaranteed to be O(1)
-    int i;
 
     NS_DURING
 
+        NSString *s = [[self textStorage] string]; // according to the class documentation, sending 'string' is guaranteed to be O(1)
+        unsigned length = [s length];
+        NSCAssert(s != nil, ICCF_LocalizedString(@"Sorry, ICeCoffEE is unable to locate the insertion point or selection"));
+        NSCAssert(length != 0, ICCF_LocalizedString(@"No text was found"));
+
+        ICCF_StartIC();
+
+        NSRange range = [self selectedRange];
         NSCAssert(range.location != NSNotFound, ICCF_LocalizedString(@"There is no insertion point or selection in the text field where you clicked"));
-        NSCAssert(s != nil, ICCF_LocalizedString(@"Sorry, ICeCoffEE is unable to locate the insertion point or selection"));
-
-        ICCF_StartIC();
-
-        NSCAssert([s length] != 0, ICCF_LocalizedString(@"No text was found"));
-
-        if (range.location == [s length]) range.location--; // work around bug in selectionRangeForProposedRange (r. 2845418)
-
-        // XXX is this even worth it to get a starting range?  Can just grab back and forth ICCF_MAX_URL_LEN (will need to remove some ICCF_CheckRange calls though)
-        range = [self selectionRangeForProposedRange: range granularity: NSSelectByWord];
-
-        // However, NSSelectByWord does not capture even the approximate boundaries of a URL
-        // (text to a space/line ending character); it'll stop at a period in the middle of a hostname.
-        // So, we expand it as follows:
-        
-        range = ICCF_URLEnclosingRange(s, range);
-
-        [self setSelectedRange: range affinity: NSSelectionAffinityDownstream stillSelecting: NO];
-        [self display];
+    
+        if (range.length == 0) {
+            if (range.location == length) range.location--;
+            range.length = 1;
+            range = ICCF_URLEnclosingRange(s, range);
+            [self setSelectedRange: range affinity: NSSelectionAffinityDownstream stillSelecting: NO];
+        }
 
         if (ICCF_LaunchURL([s substringWithRange: range], ICCF_KeyboardAction(triggeringEvent)) && ICCF_prefs.textBlinkEnabled) {
-            for (i = 0 ; i < ICCF_prefs.textBlinkCount ; i++) {
+            for (unsigned i = 0 ; i < ICCF_prefs.textBlinkCount ; i++) {
                 NSRange emptyRange = {range.location, 0};
                 [self setSelectedRange: emptyRange affinity: NSSelectionAffinityDownstream stillSelecting: YES];
@@ -250,5 +246,5 @@
 
     NS_HANDLER
-        ICCF_HandleException(localException);
+        ICCF_HandleException(localException, triggeringEvent);
     NS_ENDHANDLER
 
@@ -418,5 +414,5 @@
 }
 
-- (void)mouseDown:(NSEvent *)e;
+- (void)mouseDown:(NSEvent *)downEvent;
 {
 #if ICCF_DEBUG
@@ -427,39 +423,22 @@
     }
     down = YES;
-    ICLog(@"ICeCoffEE down: %@", e);
+    ICLog(@"ICeCoffEE down: %@", downEvent);
 #endif
-    if (ICCF_sharedTrigger != nil) {
-	ICLog(@"%@ cancelling", ICCF_sharedTrigger);
-	[ICCF_sharedTrigger cancel];
-    }
-    if (ICCF_enabled && ICCF_prefs.commandClickEnabled && ICCF_EventIsCommandMouseDown(e)) {
-	BOOL inheritModifierFlags;
-	if ([self respondsToSelector: @selector(selectedRanges)]) {
-	    // Command-multiple-click or -drag for discontiguous selection, Mac OS X 10.4 or later
-	    inheritModifierFlags = YES;
-	} else {
-	    // don't want to trigger selection extension or anything else; pass through as a plain click
-	    // (on Mac OS X 10.3, command does not modify behavior)
-	    inheritModifierFlags = NO;
-	}
-	[super mouseDown: ICCF_MouseDownEventWithModifierFlags(e, inheritModifierFlags)];
+    [ICeCoffEETrigger cancel];
+
+    if (ICCF_enabled && ICCF_prefs.commandClickEnabled && ICCF_EventIsCommandMouseDown(downEvent)) {
+	[super mouseDown: ICCF_MouseDownEventWithModifierFlags(downEvent, YES)];
         // we don't actually get a mouseUp event, just wait for mouseDown to return
         NSEvent *upEvent = [[self window] currentEvent];
-        NSPoint downPt = [e locationInWindow];
+        NSPoint downPt = [downEvent locationInWindow];
         NSPoint upPt = [upEvent locationInWindow];
         ICLog(@"next: %@", upEvent);
         NSAssert([upEvent type] == NSLeftMouseUp, @"NSTextView mouseDown: did not return with current event as mouse up!");
         if (abs(downPt.x - upPt.x) <= kICHysteresisPixels && abs(downPt.y - upPt.y) <= kICHysteresisPixels) {
-	    if (inheritModifierFlags) {
-		// Mac OS X 10.4 and later: make sure we don't have a command-double-click
-		[ICeCoffEETrigger setTriggerForEvent: e onTarget: self]; // gets stored in ICCF_sharedTrigger; the reason for this weird calling pattern is that we don't want to add methods to NSTextView, and we don't want to add a method call on every mouseDown
-		ICLog(@"%@ set", ICCF_sharedTrigger);
-	    } else {
-		// Mac OS X 10.3
-		ICCF_LaunchURLFromTextView(self, e);
-	    }
+            // make sure we don't have a Command-double-click
+            [ICeCoffEETextViewTrigger setTriggerForEvent: downEvent onTarget: self]; // gets stored in ICCF_sharedTrigger; the reason for this weird calling pattern is that we don't want to add methods to NSTextView, and we don't want to add a method call on every mouseDown
         }
     } else {
-        [super mouseDown: e];
+        [super mouseDown: downEvent];
     }
 #if ICCF_DEBUG
