Skip to content
Browse files

Make it possible to change the default text color

We used to apply the -defaultTextAttributes last, which meant if you used a color, it would overwrite syntax coloring. Now each style incorporates these styles into its style before replacing its attributes.
  • Loading branch information...
1 parent eb3354d commit eb0291d7201116cf9178ffc060dd5a54a42f0687 @uliwitness committed Feb 5, 2012
Showing with 21 additions and 16 deletions.
  1. +21 −16 UKSyntaxColoredTextViewController.m
View
37 UKSyntaxColoredTextViewController.m
@@ -818,9 +818,10 @@ -(void) recolorRange: (NSRange)range
range.length += diff;
// Get the text we'll be working with:
- NSMutableAttributedString* vString = [[NSMutableAttributedString alloc] initWithString: [[[TEXTVIEW textStorage] string] substringWithRange: range]];
+ NSDictionary* vStyles = [self defaultTextAttributes];
+ NSMutableAttributedString* vString = [[NSMutableAttributedString alloc] initWithString: [[[TEXTVIEW textStorage] string] substringWithRange: range] attributes: vStyles];
[vString autorelease];
-
+
// Load colors and fonts to use from preferences:
// Load our dictionary which contains info on coloring this language:
NSDictionary* vSyntaxDefinition = [self syntaxDefinitionDictionary];
@@ -834,7 +835,6 @@ -(void) recolorRange: (NSRange)range
// Loop over all available components:
NSDictionary* vCurrComponent = nil;
- NSDictionary* vStyles = [self defaultTextAttributes];
NSUserDefaults* vPrefs = [NSUserDefaults standardUserDefaults];
while( (vCurrComponent = [vComponentsEnny nextObject]) )
@@ -896,8 +896,8 @@ -(void) recolorRange: (NSRange)range
}
// Replace the range with our recolored part:
- [vString addAttributes: vStyles range: NSMakeRange( 0, [vString length] )];
[[TEXTVIEW textStorage] replaceCharactersInRange: range withAttributedString: vString];
+ [[TEXTVIEW textStorage] fixFontAttributeInRange: range]; // Make sure Japanese etc. fallback fonts get applied.
}
@finally
{
@@ -1032,10 +1032,11 @@ -(void) colorStringsFrom: (NSString*) startCh to: (NSString*) endCh inString: (N
{
NS_DURING
NSScanner* vScanner = [NSScanner scannerWithString: [s string]];
- NSDictionary* vStyles = [NSDictionary dictionaryWithObjectsAndKeys:
+ NSMutableDictionary*vStyles = [[[self defaultTextAttributes] mutableCopy] autorelease];
+ [vStyles addEntriesFromDictionary: [NSDictionary dictionaryWithObjectsAndKeys:
col, NSForegroundColorAttributeName,
attr, TD_SYNTAX_COLORING_MODE_ATTR,
- nil];
+ nil]];
BOOL vIsEndChar = NO;
unichar vEscChar = '\\';
BOOL vDelegateHandlesProgress = [delegate respondsToSelector: @selector(textViewControllerProgressedWhileSyntaxRecoloring:)];
@@ -1098,10 +1099,11 @@ -(void) colorCommentsFrom: (NSString*) startCh to: (NSString*) endCh inString: (
@try
{
NSScanner* vScanner = [NSScanner scannerWithString: [s string]];
- NSDictionary* vStyles = [NSDictionary dictionaryWithObjectsAndKeys:
+ NSMutableDictionary*vStyles = [[[self defaultTextAttributes] mutableCopy] autorelease];
+ [vStyles addEntriesFromDictionary: [NSDictionary dictionaryWithObjectsAndKeys:
col, NSForegroundColorAttributeName,
attr, TD_SYNTAX_COLORING_MODE_ATTR,
- nil];
+ nil]];
BOOL vDelegateHandlesProgress = [delegate respondsToSelector: @selector(textViewControllerProgressedWhileSyntaxRecoloring:)];
while( ![vScanner isAtEnd] )
@@ -1149,10 +1151,11 @@ -(void) colorOneLineComment: (NSString*) startCh inString: (NSMutableAttributedS
@try
{
NSScanner* vScanner = [NSScanner scannerWithString: [s string]];
- NSDictionary* vStyles = [NSDictionary dictionaryWithObjectsAndKeys:
+ NSMutableDictionary*vStyles = [[[self defaultTextAttributes] mutableCopy] autorelease];
+ [vStyles addEntriesFromDictionary: [NSDictionary dictionaryWithObjectsAndKeys:
col, NSForegroundColorAttributeName,
attr, TD_SYNTAX_COLORING_MODE_ATTR,
- nil];
+ nil]];
BOOL vDelegateHandlesProgress = [delegate respondsToSelector: @selector(textViewControllerProgressedWhileSyntaxRecoloring:)];
while( ![vScanner isAtEnd] )
@@ -1200,10 +1203,11 @@ -(void) colorIdentifier: (NSString*) ident inString: (NSMutableAttributedString*
@try
{
NSScanner* vScanner = [NSScanner scannerWithString: [s string]];
- NSDictionary* vStyles = [NSDictionary dictionaryWithObjectsAndKeys:
+ NSMutableDictionary*vStyles = [[[self defaultTextAttributes] mutableCopy] autorelease];
+ [vStyles addEntriesFromDictionary: [NSDictionary dictionaryWithObjectsAndKeys:
col, NSForegroundColorAttributeName,
attr, TD_SYNTAX_COLORING_MODE_ATTR,
- nil];
+ nil]];
int vStartOffs = 0;
BOOL vDelegateHandlesProgress = [delegate respondsToSelector: @selector(textViewControllerProgressedWhileSyntaxRecoloring:)];
@@ -1270,10 +1274,11 @@ -(void) colorTagFrom: (NSString*) startCh to: (NSString*)endCh inString: (NSMuta
@try
{
NSScanner* vScanner = [NSScanner scannerWithString: [s string]];
- NSDictionary* vStyles = [NSDictionary dictionaryWithObjectsAndKeys:
+ NSMutableDictionary*vStyles = [[[self defaultTextAttributes] mutableCopy] autorelease];
+ [vStyles addEntriesFromDictionary: [NSDictionary dictionaryWithObjectsAndKeys:
col, NSForegroundColorAttributeName,
attr, TD_SYNTAX_COLORING_MODE_ATTR,
- nil];
+ nil]];
BOOL vDelegateHandlesProgress = [delegate respondsToSelector: @selector(textViewControllerProgressedWhileSyntaxRecoloring:)];
while( ![vScanner isAtEnd] )
@@ -1298,7 +1303,7 @@ -(void) colorTagFrom: (NSString*) startCh to: (NSString*)endCh inString: (NSMuta
while( ![vScanner isAtEnd] )
{
// Scan up to the next occurence of the terminating sequence:
- (BOOL) [vScanner scanUpToString: endCh intoString:nil];
+ [vScanner scanUpToString: endCh intoString:nil];
// Now, if the mode of the end marker is not the mode we were told to ignore,
// we're finished now and we can exit the inner loop:
@@ -1340,7 +1345,7 @@ -(void) colorTagFrom: (NSString*) startCh to: (NSString*)endCh inString: (NSMuta
-(NSDictionary*) defaultTextAttributes
{
- return [NSDictionary dictionaryWithObject: [NSFont userFixedPitchFontOfSize: 10.0] forKey: NSFontAttributeName];
+ return [NSDictionary dictionaryWithObjectsAndKeys: [NSFont userFixedPitchFontOfSize: 10.0], NSFontAttributeName, nil];
}
@end

0 comments on commit eb0291d

Please sign in to comment.
Something went wrong with that request. Please try again.