The R Project SVN R-packages

Rev

Rev 1423 | Rev 4648 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
898 urbaneks 1
/*
2
 *  R.app : a Cocoa front end to: "R A Computer Language for Statistical Data Analysis"
3
 *  
4
 *  R.app Copyright notes:
1254 iacus 5
 *                     Copyright (C) 2004-5  The R Foundation
898 urbaneks 6
 *                     written by Stefano M. Iacus and Simon Urbanek
7
 *
8
 *                  
9
 *  R Copyright notes:
10
 *                     Copyright (C) 1995-1996   Robert Gentleman and Ross Ihaka
11
 *                     Copyright (C) 1998-2001   The R Development Core Team
12
 *                     Copyright (C) 2002-2004   The R Foundation
13
 *
14
 *  This program is free software; you can redistribute it and/or modify
15
 *  it under the terms of the GNU General Public License as published by
16
 *  the Free Software Foundation; either version 2 of the License, or
17
 *  (at your option) any later version.
18
 *
19
 *  This program is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  A copy of the GNU General Public License is available via WWW at
25
 *  http://www.gnu.org/copyleft/gpl.html.  You can also obtain it by
26
 *  writing to the Free Software Foundation, Inc., 59 Temple Place,
27
 *  Suite 330, Boston, MA  02111-1307  USA.
28
 *
29
 *  Created by Simon Urbanek on 1/11/05.
30
 */
31
 
32
#import "REditorTextStorage.h"
1397 urbaneks 33
#import "RGUI.h"
34
#import "PreferenceKeys.h"
898 urbaneks 35
 
36
@implementation REditorTextStorage
37
 
38
- (id) init
39
{
40
	self = [super init];
41
	if (self) {
1397 urbaneks 42
		cont = [[NSTextStorage alloc] init];
43
		highlightColorAttr = nil;
44
		currentHighlight = -1;
45
		pendingHilite = -1;
46
		insideReplaceEdited = NO;
1459 urbaneks 47
		[self updatePreferences];
1397 urbaneks 48
		[[Preferences sharedPreferences] addDependent:self];
898 urbaneks 49
	}
50
	return self;
51
}
52
 
53
- (void) dealloc
54
{
1397 urbaneks 55
	[[Preferences sharedPreferences] removeDependent:self];
56
	[[NSNotificationCenter defaultCenter] removeObserver:self];
898 urbaneks 57
	[cont release];
1253 urbaneks 58
	[super dealloc];
898 urbaneks 59
}
60
 
61
// mandatory primitive methods
62
 
63
- (NSString*) string
64
{
65
	return [cont string];
66
}
67
 
68
- (NSDictionary *) attributesAtIndex:(unsigned)index effectiveRange:(NSRangePointer)aRange
69
{
1397 urbaneks 70
	[super ensureAttributesAreFixedInRange:NSMakeRange(index,1)];
898 urbaneks 71
	return [cont attributesAtIndex:index effectiveRange:aRange];
72
}
73
 
74
- (void) replaceCharactersInRange:(NSRange)aRange withString:(NSString *)aString
75
{
1397 urbaneks 76
	int origLen = [self length];
1423 urbaneks 77
	if (currentHighlight>-1) [self resetHighlights];
898 urbaneks 78
	[cont replaceCharactersInRange:aRange withString:aString];
1397 urbaneks 79
	insideReplaceEdited = YES;
80
	[super ensureAttributesAreFixedInRange:NSMakeRange(aRange.location,[aString length])];
81
	[self edited:NSTextStorageEditedCharacters range:aRange changeInLength:[self length] - origLen];
82
	insideReplaceEdited = NO;
83
	if (pendingHilite>-1) {
84
		[self highlightCharacter:pendingHilite];
85
		pendingHilite = -1;
86
	}
898 urbaneks 87
}
88
 
89
- (void)setAttributes:(NSDictionary *)attributes range:(NSRange)aRange
90
{
91
	[cont setAttributes:attributes range:aRange];
1397 urbaneks 92
	[super ensureAttributesAreFixedInRange:aRange];
898 urbaneks 93
	[self edited:NSTextStorageEditedAttributes range:aRange changeInLength:0];
94
}
95
 
1397 urbaneks 96
- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)aRange
97
{
98
	[cont addAttribute:name value:value range:aRange];
99
	[self ensureAttributesAreFixedInRange:aRange];
100
	[self edited:NSTextStorageEditedAttributes range:aRange changeInLength:0];
101
}
102
 
898 urbaneks 103
// end of primitive methods
104
 
1397 urbaneks 105
- (BOOL)fixesAttributesLazily
106
{
107
	return YES;
108
}
898 urbaneks 109
 
1397 urbaneks 110
- (NSLayoutManager*) layoutManager
111
{
112
	NSArray *lms = [self layoutManagers];
113
	if (lms) {
114
		int lmsc = [lms count];
115
		if (lmsc>0) {
116
			if (lmsc>1) SLog(@"REditorTextStorage(%@).layoutManager: more than one LM exists, returning the first one",self);
117
			return [lms objectAtIndex:0];
118
		}
119
	}
120
	return nil;
121
}
898 urbaneks 122
 
1397 urbaneks 123
-(void)resetHighlights
124
{
125
	if (currentHighlight>-1) {
126
		if (currentHighlight<[cont length]) {
127
			NSLayoutManager *lm = [self layoutManager];
128
			if (lm) {
129
				NSRange fr = NSMakeRange(currentHighlight,1);
130
				NSDictionary *d = [lm temporaryAttributesAtCharacterIndex:currentHighlight effectiveRange:&fr];
131
				if (!d || [d objectForKey:NSBackgroundColorAttributeName]==nil) {
132
					fr = NSMakeRange(0,[self length]);
133
					SLog(@"resetHighlights: attribute at %d not found, clearing all %d characters - better safe than sorry", currentHighlight, fr.length);
134
				}
135
				[lm removeTemporaryAttribute:NSBackgroundColorAttributeName forCharacterRange:fr];
136
			}
137
		}
138
		currentHighlight=-1;
139
	}
140
}
141
 
142
-(void)highlightCharacter: (int) pos
143
{
144
	if (insideReplaceEdited) {
145
		pendingHilite = pos;
146
		return;
147
	}
148
	[self resetHighlights];
149
	if (pos>=0 && pos<[self length]) {
150
		NSLayoutManager *lm = [self layoutManager];
151
		if (lm) {
152
			currentHighlight=pos;
153
			[lm setTemporaryAttributes:highlightColorAttr forCharacterRange:NSMakeRange(pos, 1)];
154
			[self performSelector:@selector(resetBackgroundColor:) withObject:nil afterDelay:braceHighlightInterval];
155
		} else SLog(@"highlightCharacter: attempt to set highlight %d beyond the text range 0:%d - I refuse!", pos, [self length]-1);
156
	}
157
}
158
 
159
-(void)resetBackgroundColor:(id)sender
160
{
161
	[self resetHighlights];
162
}
163
 
164
- (void) updatePreferences {
165
	NSColor *c = [Preferences unarchivedObjectForKey: @"braceHighlightColor" withDefault: nil];
166
	if (!c) c=[NSColor redColor];
167
	if (highlightColorAttr) [highlightColorAttr release];
168
	highlightColorAttr = [[NSDictionary alloc] initWithObjectsAndKeys:c, NSBackgroundColorAttributeName, nil];
169
	showMatchingBraces = [Preferences flagForKey:showBraceHighlightingKey withDefault: YES];
170
	braceHighlightInterval = [[Preferences stringForKey:highlightIntervalKey withDefault: @"0.2"] doubleValue];
171
}
172
 
898 urbaneks 173
@end