--- AppController.m.ORG	Sun Apr  2 11:02:35 2006
+++ AppController.m	Sun Apr  2 15:16:51 2006
@@ -40,12 +40,116 @@
 -(void) writeString: (NSString*) aString
 	 attributes: (NSDictionary*) attributes
 {
-  NSAttributedString* attrStr =
-    [[NSAttributedString alloc]
-      initWithString: aString
-      attributes: attributes];
-  [[searchResultView textStorage] appendAttributedString: attrStr];
-  [attrStr release];
+  NSString
+    *buffer = aString;
+  NSMutableAttributedString
+    *as = [[NSMutableAttributedString alloc] initWithString: buffer];
+  NSRange
+    range = NSMakeRange(0., [buffer length]);
+  
+  [[searchResultView textStorage] beginEditing];
+  {
+    [as addAttributes: attributes  range: range];
+    
+    {
+      NSString
+        *string = [as string];
+      NSRange
+        begin;
+      
+      do
+      {
+        NSRange
+          searchRange = NSMakeRange(0., [string length]);
+        
+        begin = [string rangeOfString: @"{"
+                              options: 0
+                                range: searchRange];
+        
+        if( 0 < begin.length )
+        {
+          NSCharacterSet
+            *cSet = [NSCharacterSet characterSetWithCharactersInString: @"}"];
+          NSRange
+            end;
+          NSString
+            *word;
+          BOOL
+            replace = YES;
+          
+          //
+          // Check whether '{' is immediately followed by '}'
+          //
+          {
+            searchRange.location = begin.location + begin.length;
+            searchRange.length = [string length] - searchRange.location;
+            
+            end = [string rangeOfCharacterFromSet: cSet
+                                          options: 0
+                                            range: searchRange];
+            
+            if( end.location == (begin.location + 1) )
+            {
+              replace = NO;
+            }
+          }
+          
+          //
+          // Remove the '{'
+          //
+          if( replace )
+          {
+            [as replaceCharactersInRange: NSMakeRange(begin.location, 1.)
+                              withString: @""];
+            searchRange.location = begin.location + begin.length;
+            searchRange.length = [string length] - searchRange.location;
+            
+            end = [string rangeOfCharacterFromSet: cSet
+                                          options: 0
+                                            range: searchRange];
+            
+            if( 0 == end.length )
+            {
+              end.location = [string length]-1;
+            }
+            begin.length = end.location - begin.location;
+            word = [string substringWithRange: begin];
+            
+            //
+            // Due to a bug (?) in -back, underlines won't be displayed so
+            // we also use color. We also 'fake' link to be an URL, as I
+            // can't figure out any other (easy) way to handle a click on
+            // that particular word.
+            //
+            
+            attributes = [NSDictionary dictionaryWithObjectsAndKeys:
+                           word, NSLinkAttributeName,
+                           [NSNumber numberWithInt: NSSingleUnderlineStyle], NSUnderlineStyleAttributeName,
+                           [NSColor redColor], NSForegroundColorAttributeName,
+                           nil];
+            [as addAttributes: attributes range: begin];
+            
+            //
+            // Now remove the '}' and fix the length
+            //
+            [as replaceCharactersInRange: NSMakeRange(end.location, 1.)
+                              withString: @""];
+            searchRange.length--;
+          } // replace
+        }
+      }
+      while( 0 != begin.length );
+    }
+    [[searchResultView textStorage] appendAttributedString: as];
+  }
+  [[searchResultView textStorage] endEditing];
+  [as release];
+  
+  [searchResultView scrollRangeToVisible: NSMakeRange(0., 0.)];
+  [searchResultView setSelectedRange: NSMakeRange(0., 0.)];
+  [searchResultView setNeedsDisplay: YES];
+
+  return;
 }
 
 -(void) writeBigHeadline: (NSString*) aString {
@@ -66,6 +170,12 @@
 -(void) writeString: (NSString*) aString
 	       link: (id) aClickable
 {
+//
+// The following is ifdef'd out because I haven't been able to trigger
+// this method so I have no idea whether it will work with the added
+// attribute code above.
+//
+#if 0
   // fall back to 'no link'
   NSDictionary* attributes = normalAttributes;
   
@@ -88,7 +198,10 @@
   
   // write
   [self writeString: aString
-	attributes: attributes];
+         attributes: attributes];
+#else
+  [self writeLine: aString];
+#endif
 }
 
 @end // AppController (DefinitionWriter)
@@ -149,26 +262,10 @@
    clickedOnLink: (id) link
 	 atIndex: (unsigned) charIndex
 {
-  if ([link respondsToSelector: @selector(click)]) {
-    
-    NS_DURING
-      {
-	[link click];
-      }
-    NS_HANDLER
-      {
-	NSRunAlertPanel(@"Link click failed!",
-			[localException reason],
-			@"Oh no!", nil, nil);
-	return NO;
-      }
-    NS_ENDHANDLER;
-    
-    return YES;
-  } else {
-    NSLog(@"Link %@ clicked, but it doesn't respond to 'click'", link);
-    return NO;
-  }
+  // Set searchStringControl to the word that has been clicked on
+  [searchStringControl setStringValue: link];
+  // ...and search for a definition
+  [self defineWord: [searchStringControl stringValue]];
 }
 
 
