PageRenderTime 1362ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/VT100TextStorage.m

http://github.com/kkass/iterm
Objective C | 167 lines | 101 code | 30 blank | 36 comment | 11 complexity | 9c73e359f88ed224b488b61670947236 MD5 | raw file
  1. /*
  2. ** VT100TextStorage.m
  3. **
  4. ** Copyright (c) 2002, 2003
  5. **
  6. ** Author: Ujwal S. Sathyam
  7. **
  8. ** Project: iTerm
  9. **
  10. ** Description: custom text storage object for vt100 terminal.
  11. **
  12. ** This program is free software; you can redistribute it and/or modify
  13. ** it under the terms of the GNU General Public License as published by
  14. ** the Free Software Foundation; either version 2 of the License, or
  15. ** (at your option) any later version.
  16. **
  17. ** This program is distributed in the hope that it will be useful,
  18. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ** GNU General Public License for more details.
  21. **
  22. ** You should have received a copy of the GNU General Public License
  23. ** along with this program; if not, write to the Free Software
  24. ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #import <iTerm/iTerm.h>
  27. #import <iTerm/VT100TextStorage.h>
  28. #define DEBUG_METHOD_TRACE 0
  29. #define DEBUG_ALLOC 0
  30. @implementation VT100TextStorage
  31. - (id)initWithAttributedString:(NSAttributedString *)attrStr
  32. {
  33. #if DEBUG_ALLOC
  34. NSLog(@"VT100TextStorage: initWithAttributedString: %@", attrStr);
  35. #endif
  36. if (self = [super init])
  37. {
  38. contents = attrStr ? [attrStr mutableCopy] : [[NSMutableAttributedString alloc] init];
  39. }
  40. return self;
  41. }
  42. - init
  43. {
  44. return [self initWithAttributedString:nil];
  45. }
  46. - (void)dealloc
  47. {
  48. #if DEBUG_ALLOC
  49. NSLog(@"VT100TextStorage: dealloc");
  50. #endif
  51. [contents release];
  52. [super dealloc];
  53. }
  54. - (NSString *)string
  55. {
  56. return [contents string];
  57. }
  58. - (NSDictionary *)attributesAtIndex:(unsigned)location effectiveRange:(NSRange *)range
  59. {
  60. return [contents attributesAtIndex:location effectiveRange:range];
  61. }
  62. - (void)replaceCharactersInRange:(NSRange)range withString:(NSString *)str
  63. {
  64. #if DEBUG_METHOD_TRACE
  65. NSLog(@"VT100TextStorage: replaceCharactersInRange: (%d,%d) withString:",
  66. range.location, range.length);
  67. NSLog(@"old str = \n'%@'", [[contents attributedSubstringFromRange: range] string]);
  68. NSLog(@"new str = \n'%@'", str);
  69. #endif
  70. // get the original length before edit
  71. int origLen = [self length];
  72. #if 1
  73. // if the length did not change, check what really changed
  74. if(range.length == [str length])
  75. {
  76. NSString *origSubstring, *commonString;
  77. // strip out the common stuff
  78. origSubstring = [[contents attributedSubstringFromRange: range] string];
  79. commonString = [origSubstring commonPrefixWithString: str options: NSLiteralSearch];
  80. if([commonString length] > 0)
  81. {
  82. NSString *origDiffString, *newDiffString;
  83. if([commonString isEqualToString: origSubstring])
  84. origDiffString = @"";
  85. else
  86. origDiffString = [origSubstring substringFromIndex: [commonString length]];
  87. if([commonString isEqualToString: str])
  88. newDiffString = @"";
  89. else
  90. newDiffString = [str substringFromIndex: [commonString length]];
  91. //NSLog(@"Common string = \n'%@'", commonString);
  92. //NSLog(@"origDiff string = \n'%@'", origDiffString);
  93. //NSLog(@"newDiff string = \n'%@'", newDiffString);
  94. // Now replace only the diff
  95. if([origDiffString length] > 0 && [newDiffString length] > 0)
  96. {
  97. NSRange aRange;
  98. aRange = NSMakeRange(range.location + [commonString length], [origDiffString length]);
  99. [contents replaceCharactersInRange: aRange withString: newDiffString];
  100. [self edited:NSTextStorageEditedCharacters range:aRange changeInLength:[self length] - origLen];
  101. return;
  102. }
  103. }
  104. }
  105. #endif
  106. // else do the usual stuff.
  107. [contents replaceCharactersInRange:range withString:str];
  108. [self edited:NSTextStorageEditedCharacters range:range changeInLength:[self length] - origLen];
  109. }
  110. - (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range
  111. {
  112. // do a simple check on whether we are re-applying the same attributes
  113. NSDictionary *currentAttributes;
  114. NSRange longestEffectiveRange;
  115. currentAttributes = [contents attributesAtIndex: range.location longestEffectiveRange: &longestEffectiveRange inRange: range];
  116. if([currentAttributes isEqualToDictionary: attrs] && longestEffectiveRange.length == range.length)
  117. {
  118. return;
  119. }
  120. [contents setAttributes:attrs range:range];
  121. [self edited:NSTextStorageEditedAttributes range:range changeInLength:0];
  122. }
  123. - (void) beginEditing
  124. {
  125. #if DEBUG_METHOD_TRACE
  126. //NSLog(@"VT100TextStorage: beginEditing");
  127. #endif
  128. [super beginEditing];
  129. }
  130. - (void) endEditing
  131. {
  132. #if DEBUG_METHOD_TRACE
  133. //NSLog(@"VT100TextStorage: endEditing");
  134. #endif
  135. [super endEditing];
  136. }
  137. @end