###########################################################################
#                            TextMate Snippets                            #
###########################################################################

snippet imp "#import (imp)" b
#import "${1:`!p snip.rv = re.sub(r'\..*$', '.h', fn)`}"
endsnippet

snippet Imp "#import <> (Imp)"
#import <${1:Cocoa/Cocoa.h}>
endsnippet

snippet cl "020 Class (objc)"
@interface ${1:`!p 
if len(fn):
   snip.rv = re.sub(r'\..*$', '', fn)
else:
   snip.rv = "object"
`} : ${2:NSObject}
{
}
@end

@implementation $1
- (id)init
{
	if((self = [super init]))
	{$0
	}
	return self;
}
@end
endsnippet

snippet array "NSArray (array)"
NSMutableArray *${1:array} = [NSMutableArray array];
endsnippet

snippet dict "NSDictionary (dict)"
NSMutableDictionary *${1:dict} = [NSMutableDictionary dictionary];
endsnippet

snippet forarray "for NSArray loop (forarray)"
unsigned int	${1:object}Count = [${2:array} count];

for(unsigned int index = 0; index < $1Count; index += 1)
{
	${3:id}	$1 = [$2 objectAtIndex:index];
	$0
}
endsnippet

snippet objacc "Object Accessors (objacc)"
- (${1:id})${2:thing}
{
	return $2;
}

- (void)set${2/./\u$0/}:($1)aValue
{
	$0${1/( \*)?$/(?1:$1: )/}old${2/./\u$0/} = $2;
	$2 = [aValue retain];
	[old${2/./\u$0/} release];
}
endsnippet

snippet sel "@selector"
@selector(${1:method}:)
endsnippet

snippet cdacc "CoreData Accessors Implementation"
- (${1:id})${2:attribute}
{
	[self willAccessValueForKey:@"$2"];
	$1 value = [self primitiveValueForKey:@"$2"];
	[self didAccessValueForKey:@"$2"];
	return value;
}

- (void)set${2/./\u$0/}:($1)aValue
{
	[self willChangeValueForKey:@"$2"];
	[self setPrimitiveValue:aValue forKey:@"$2"];
	[self didChangeValueForKey:@"$2"];
}
endsnippet

snippet delegate "Delegate Responds to Selector"
if([${1:[self delegate]} respondsToSelector:@selector(${2:selfDidSomething:})])
	[$1 ${3:${2/((^\s*([A-Za-z0-9_]*:)\s*)|(:\s*$)|(:\s*))/(?2:$2self :\:<>)(?4::)(?5: :)/g}}];

endsnippet

snippet thread "Detach New NSThread"
[NSThread detachNewThreadSelector:@selector(${1:method}:) toTarget:${2:aTarget} withObject:${3:anArgument}]
endsnippet

snippet ibo "IBOutlet (ibo)"
IBOutlet ${1:NSSomeClass} *${2:${1/^[A-Z](?:[A-Z]+|[a-z]+)([A-Z]\w*)/\l$1/}};
endsnippet

snippet I "Initialize Implementation (I)"
+ (void)initialize
{
	[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
		$0@"value", @"key",
		nil]];
}
endsnippet

snippet bind "Key:value binding (bind)"
bind:@"${1:binding}" toObject:${2:observableController} withKeyPath:@"${3:keyPath}" options:${4:nil}
endsnippet

snippet arracc "LoD array (arracc)"
- (void)addObjectTo${1:Things}:(${2:id})anObject
{
	[${3:${1/./\l$0/}} addObject:anObject];
}

- (void)insertObject:($2)anObject in$1AtIndex:(unsigned int)i 
{
	[$3 insertObject:anObject atIndex:i];
}

- ($2)objectIn$1AtIndex:(unsigned int)i
{
	return [$3 objectAtIndex:i];
}

- (unsigned int)indexOfObjectIn$1:($2)anObject
{
	return [$3 indexOfObject:anObject];
}

- (void)removeObjectFrom$1AtIndex:(unsigned int)i
{
	[$3 removeObjectAtIndex:i];
}

- (unsigned int)countOf$1
{
	return [$3 count];
}

- (NSArray *${1/./\l$0/}
{
	return $3;
}

- (void)set$1:(NSArray *)new$1
{
	[$3 setArray:new$1];
}
endsnippet

snippet arracc "LoD array interface (arracc)"
- (void)addObjectTo${1:Things}:(${2:id})anObject;
- (void)insertObject:($2)anObject in$1AtIndex:(unsigned int)i;
- ($2)objectIn$1AtIndex:(unsigned int)i;
- (unsigned int)indexOfObjectIn$1:($2)anObject;
- (void)removeObjectFrom$1AtIndex:(unsigned int)i;
- (unsigned int)countOf$1;
- (NSArray *)${1/./\l$0/};
- (void)set$1:(NSArray *)new$1;
endsnippet

snippet focus "Lock Focus"
[self lockFocus];
$0
[self unlockFocus];
endsnippet

snippet pool "NSAutoreleasePool (pool)"
NSAutoreleasePool *pool = [NSAutoreleasePool new];
$0
[pool drain];
endsnippet

snippet log "NSLog (log) 2"
NSLog(@"$1"${1/[^%]*(%)?.*/(?1:, :\);)/}$2${1/[^%]*(%)?.*/(?1:\);)/}
endsnippet

snippet alert "NSRunAlertPanel (alert)"
int choice = NSRunAlertPanel(@"${1:Something important!}", @"${2:Something important just happend, and now I need to ask you, do you want to continue?}", @"${3:Continue}", @"${4:Cancel}", nil);
if(choice == NSAlertDefaultReturn) // "$3"
{
	$0;
}
else if(choice == NSAlertAlternateReturn) // "$4"
{
	
}
endsnippet

snippet format "NSString stringWithFormat (format)"
[NSString stringWithFormat:@"$1", $2]$0
endsnippet

snippet objacc "Object Accessors Interface (objacc)"
- (${1:id})${2:thing};
- (void)set${2/./\u$0/}:($1)aValue;
endsnippet

snippet prop "Property"
@property (${1/^(e)$|.*/(?1:r)/}${1:r}${1/^(?:(r)|(e)|(c)|(a))$|.*/(?1:etain)(?2:adonly)(?3:opy)(?4:ssign)/}) ${2:NSSomeClass}$ *${3:${2/^[A-Z](?:[A-Z]+|[a-z]+)([A-Z]\w*)/\l$1/}};
endsnippet

snippet getprefs "Read from defaults (getprefs)"
[[NSUserDefaults standardUserDefaults] objectForKey:${1:key}];
endsnippet

snippet obs "Register for Notification"
[[NSNotificationCenter defaultCenter] addObserver:${1:self} selector:@selector(${3:${2/^([A-Z]{2})?(.+?)(Notification)?$/\l$2/}}:) name:${2:NSWindowDidBecomeMainNotification} object:${4:nil}];
endsnippet

snippet responds "Responds to Selector"
if ([${1:self} respondsToSelector:@selector(${2:someSelector:})])
{
    [$1 ${3:${2/((:\s*$)|(:\s*))/:<>(?3: )/g}}];
}
endsnippet

snippet gsave "Save and Restore Graphics Context (gsave)"
[NSGraphicsContext saveGraphicsState];
$0
[NSGraphicsContext restoreGraphicsState];

endsnippet

snippet acc "Scalar Accessors (acc)"
- (${1:unsigned int})${2:thing}
{
	return ${3:$2};
}

- (void)set${2/./\u$0/}:(${1:unsigned int})new${2/./\u$0/}
{
	$3 = new${2/./\u$0/};
}
endsnippet

snippet acc "Scalar Accessors Interface (acc)"
- (${1:unsigned int})${2:thing};
- (void)set${2/./\u$0/}:($1)new${2/./\u$0/};
endsnippet

snippet stracc "String Accessors (stracc)"
- (NSString *)${1:thing}
{
	return ${2:$1};
}

- (void)set${1/.*/\u$0/}:(NSString *)/})${3:a${1/.*/\u$0/}}
{
	$3 = [$3 copy];
	[$2 release];
	$2 = $3;
}
endsnippet

snippet syn "Synthesize"
@synthesize ${1:property};
endsnippet

snippet setprefs "Write to defaults (setprefs)"
[[NSUserDefaults standardUserDefaults] setObject:${1:object} forKey:${2:key}];
endsnippet

# vim:ft=snippets:
