Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 410 lines (356 sloc) 10.437 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
/*
* Copyright (c) 2008-2012 Martin Hedenfalk <martin@vicoapp.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#import "ViMark.h"
#import "ViMarkManager.h"
#import "ViDocument.h"
#import "ViDocumentView.h"
#import "ViDocumentController.h"
#import "NSURL-additions.h"

@implementation ViMark

@synthesize name = _name;
@synthesize line = _line;
@synthesize column = _column;
@synthesize location = _location;
@synthesize range = _range;
@synthesize rangeString = _rangeString;
@synthesize url = _url;
@synthesize title = _title;
@synthesize icon = _icon;
@synthesize document = _document;
@synthesize view = _view;
@synthesize representedObject = _representedObject;
@synthesize persistent = _persistent;
@synthesize recentlyRestored = _recentlyRestored;
@synthesize scopes = _scopes;

+ (ViMark *)markWithURL:(NSURL *)aURL
{
return [[[ViMark alloc] initWithURL:aURL
name:nil
title:nil
line:-1
column:-1] autorelease];
}

+ (ViMark *)markWithURL:(NSURL *)aURL
line:(NSInteger)aLine
column:(NSInteger)aColumn
{
return [[[ViMark alloc] initWithURL:aURL
name:nil
title:nil
line:aLine
column:aColumn] autorelease];
}

+ (ViMark *)markWithURL:(NSURL *)aURL
name:(NSString *)aName
title:(id)aTitle
line:(NSInteger)aLine
column:(NSInteger)aColumn
{
return [[[ViMark alloc] initWithURL:aURL
name:aName
title:aTitle
line:aLine
column:aColumn] autorelease];
}

+ (ViMark *)markWithDocument:(ViDocument *)aDocument
name:(NSString *)aName
range:(NSRange)aRange
{
return [[[ViMark alloc] initWithDocument:aDocument
name:aName
range:aRange] autorelease];
}

+ (ViMark *)markWithView:(ViDocumentView *)aDocumentView
name:(NSString *)aName
range:(NSRange)aRange
{
return [[[ViMark alloc] initWithView:aDocumentView
name:aName
range:aRange] autorelease];
}

- (ViMark *)initWithURL:(NSURL *)aURL
name:(NSString *)aName
title:(id)aTitle
line:(NSInteger)aLine
column:(NSInteger)aColumn
{
if (aURL == nil) {
[self release];
return nil;
}

if ((self = [super init]) != nil) {
_url = [[aURL URLByResolvingSymlinksAndAliases:nil] retain];
_name = [aName copy];
_title = [aTitle copy];
_persistent = YES;

_line = aLine;
_column = aColumn;

_location = NSNotFound;
_range = NSMakeRange(NSNotFound, 0);
_rangeStringIsDirty = YES;

ViDocument *doc = [[ViDocumentController sharedDocumentController] documentForURLQuick:_url];
if (doc)
[self setDocument:doc];
else
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(documentAdded:)
name:ViDocumentLoadedNotification
object:nil];
}

DEBUG_INIT();
return self;
}

- (ViMark *)initWithDocument:(ViDocument *)aDocument
name:(NSString *)aName
range:(NSRange)aRange
{
if (aDocument == nil) {
[self release];
return nil;
}

if ((self = [super init]) != nil) {
_persistent = YES;
_document = [aDocument retain];
_name = [aName retain];
[self setRange:aRange];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(documentRemoved:)
name:ViDocumentRemovedNotification
object:_document];

[self.document registerMark:self];
}

DEBUG_INIT();
return self;
}

- (ViMark *)initWithView:(ViDocumentView *)aDocumentView
name:(NSString *)aName
range:(NSRange)aRange
{
if (aDocumentView == nil) {
[self release];
return nil;
}

if ((self = [self initWithDocument:(ViDocument *)aDocumentView.document
name:aName
range:aRange]) != nil) {
_view = aDocumentView; // XXX: not retained!
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(viewClosed:)
name:ViViewClosedNotification
object:_view];
}
return self;
}

- (id)copyWithZone:(NSZone *)zone
{
ViMark *copy = [[self class] allocWithZone:zone];
if (_view) {
copy = [copy initWithView:_view name:_name range:_range];
} else if (_document) {
copy = [copy initWithDocument:_document name:_name range:_range];
} else {
copy = [copy initWithURL:_url name:_name title:_title line:_line column:_column];
}
copy.title = _title;
copy.icon = _icon;
copy.persistent = _persistent;
return copy;
}

- (void)viewClosed:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self
name:ViViewClosedNotification
object:_view];
_view = nil;
DEBUG(@"view %@ closed for mark %@", [notification object], self);
}

- (void)dealloc
{
DEBUG_DEALLOC();

[[NSNotificationCenter defaultCenter] removeObserver:self];

[_name release];
[_title release];
[_icon release];
[_groupName release];
[_url release];
[_document release];
[_lists release];
[_rangeString release];
[_representedObject release];
[_scopes release];
[super dealloc];
}

- (void)documentAdded:(NSNotification *)notification
{
ViDocument *doc = [notification object];
DEBUG(@"added document %@ (expecting url %@)", doc, _url);
if (![doc isKindOfClass:[ViDocument class]])
return;
if (![[doc fileURL] isEqual:_url])
return;

[[NSNotificationCenter defaultCenter] removeObserver:self
name:ViDocumentLoadedNotification
object:nil];

[self setDocument:doc];
}

- (void)setDocument:(ViDocument *)doc
{
[doc retain];
[_document release];
_document = doc;
_view = nil;

if (![_document isKindOfClass:[ViDocument class]]) {
if (doc)
INFO(@"got non-document %@ for mark %@", doc, self);
return;
}

if (_line < 0)
_range = NSMakeRange(NSNotFound, 0);
else {
NSUInteger eol = 0;
NSInteger loc = [[_document textStorage] locationForStartOfLine:_line
length:NULL
contentsEnd:&eol];
DEBUG(@"got line %lu => location %li", _line, loc);
if (loc < 0)
_location = IMAX(0, [[_document textStorage] length] - 1);
else {
_location = loc + IMAX(0, _column - 1);
if (_location > eol)
_location = eol;
}

_range = NSMakeRange(_location, 1);
}

DEBUG(@"got document %@, %lu:%lu => location %lu", _document, _line, _column, _location);

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(documentRemoved:)
name:ViDocumentRemovedNotification
object:_document];

[self.document registerMark:self];
}

- (void)documentRemoved:(NSNotification *)notification
{
DEBUG(@"document %@ was removed (expecting %@)", [notification object], _document);
if ([notification object] != _document)
return;

[[NSNotificationCenter defaultCenter] removeObserver:self
name:ViDocumentRemovedNotification
object:_document];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(documentAdded:)
name:ViDocumentLoadedNotification
object:nil];

[self setURL:[_document fileURL]];
[self setDocument:nil];

// _location = NSNotFound;
// _range = NSMakeRange(NSNotFound, 0);
}

- (void)setLocation:(NSUInteger)aLocation
{
if (_document)
[self setRange:NSMakeRange(aLocation, 1)];
}

- (void)setRange:(NSRange)aRange
{
if (_document) {
[self willChangeValueForKey:@"range"];
[self willChangeValueForKey:@"rangeString"];
[self willChangeValueForKey:@"line"];

_range = aRange;
_rangeStringIsDirty = YES;
_location = _range.location;
_line = [[_document textStorage] lineNumberAtLocation:_location];
_column = [[_document textStorage] columnOffsetAtLocation:_location] + 1;

[self didChangeValueForKey:@"line"];
[self didChangeValueForKey:@"rangeString"];
[self didChangeValueForKey:@"range"];
}
}

- (NSString *)rangeString
{
if (_rangeStringIsDirty) {
[_rangeString release];
// if (_range.location != NSNotFound)
// _rangeString = [NSStringFromRange(_range) retain];
// else
if (_line < 0)
_rangeString = nil;
else
_rangeString = [[NSString alloc] initWithFormat:@"%lu:%lu %@",
_line, _column, NSStringFromRange(_range)];
_rangeStringIsDirty = NO;
}
return _rangeString;
}

- (NSURL *)url
{
/* The URL of the document can change. */
if ([_document fileURL])
return [_document fileURL];
return _url;
}

- (void)setURL:(NSURL *)url
{
[_url release];
_url = [[url URLByResolvingSymlinksAndAliases:nil] retain];
}

- (NSString *)groupName
{
NSURL *u = [self url];
return u ? [u absoluteString] : [_document displayName];
}

- (void)remove
{
for (ViMarkList *list in _lists)
[list removeMark:self];
[_document unregisterMark:self];
}

- (void)registerList:(ViMarkList *)list
{
if (_lists == nil)
_lists = [[NSHashTable alloc] initWithOptions:NSHashTableObjectPointerPersonality capacity:10];
[_lists addObject:list]; // XXX: this ACTUALLY DOES retain the list
}

- (void)unregisterList:(ViMarkList *)list
{
[_lists removeObject:list];
}

- (BOOL)isLeaf
{
return YES;
}

- (id)title
{
return _title ?: _name;
}

- (NSString *)description
{
return [NSString stringWithFormat:@"<ViMark %p (%@): %@, %lu:%lu %@>",
self,
_name ?: _title,
_view ? [_view description] : (_document ? [_document description] : [_url description]),
_line, _column, NSStringFromRange(_range)];
}

@end
Something went wrong with that request. Please try again.