home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Utilities / NCCompletionDictionary-2.6 / NCTextViewAdditions.m < prev    next >
Encoding:
Text File  |  1997-08-10  |  2.5 KB  |  85 lines

  1.  
  2. // NCTextViewAdditions.m
  3. //
  4. // Written by Norbert Heger <bertl@hal.kph.tuwien.ac.at>
  5. // Copyright (c)1997
  6. //
  7. // This file is distributed under the terms of the
  8. // GNU General Public License.
  9.  
  10. #import "NCTextViewAdditions.h"
  11. #import "NCCompletionDictionary.h"
  12. #import "NCStringAdditions.h"
  13. #import "NCCompletionWindow.h"
  14. #import "NCRuntimeHacks.h"
  15.  
  16. @implementation NSTextView (NCTextViewAdditions)
  17.  
  18. - (void)performWindowHack
  19. {
  20.     NSWindow *window = [self window];
  21.     Class class = [NCCompletionWindow class];
  22.  
  23.     if ([window class] == [NSWindow class]) {
  24.         [window setClass:class];
  25.     }
  26.     if ([window class] == class) {
  27.         NSEvent *event = [NSApp currentEvent];
  28.         [class setCompletionKeyCode:[event keyCode] modifierFlags:[event modifierFlags]];
  29.     }
  30. }
  31.  
  32. - (BOOL)selectFieldInRange:(NSRange)aRange
  33. {
  34.     NSString *string = [[self textStorage] string];
  35.     NSRange fieldRange = [string rangeFromString:@"«" toString:@"»" range:aRange];
  36.  
  37.     if (fieldRange.length != 0) {
  38.         [self setSelectedRange:fieldRange];
  39.         return YES;
  40.     }
  41.     return NO;
  42. }
  43.  
  44. - (void)selectNextField:(id)sender
  45. {
  46.     NSString *string = [[self textStorage] string];
  47.     NSRange selectedRange = [self selectedRange];
  48.     unsigned int location = selectedRange.location + selectedRange.length;
  49.     NSRange searchRange = NSMakeRange(location, [string length] - location);
  50.  
  51.     if (![self selectFieldInRange:searchRange]) {
  52.         searchRange = NSMakeRange(0, selectedRange.location);
  53.         if (![self selectFieldInRange:searchRange]) { NSBeep(); }
  54.     }
  55. }
  56.  
  57. - (void)completeUsingDictionary:(id)sender
  58. {
  59.     NSRange selectedRange = [self selectedRange];
  60.  
  61.     [self performWindowHack];
  62.  
  63.     if (selectedRange.location > 0 && selectedRange.length == 0) {
  64.         NSTextStorage *storage = [self textStorage];
  65.         NSRange keyRange = [storage doubleClickAtIndex:selectedRange.location - 1];
  66.         NSString *key, *result;
  67.  
  68.         if (keyRange.location + keyRange.length > selectedRange.location) {
  69.             keyRange.length = selectedRange.location - keyRange.location;
  70.         }
  71.         key = [[storage string] substringWithRange:keyRange];
  72.         result = [[NCCompletionDictionary dictionary] objectForKey:key];
  73.         
  74.         if (result != nil) {
  75.             [storage replaceCharactersInRange:keyRange withString:result];
  76.             [self selectFieldInRange:NSMakeRange(keyRange.location, [result length])];
  77.             return; // success
  78.         }
  79.     }
  80.     if ([self respondsToSelector:@selector(complete:)]) { [self complete:nil]; }
  81.     else { NSBeep(); }
  82. }
  83.  
  84. @end
  85.