[txmt-dev] DIALOG x-insert - bug - a fix suggestion

Hans-Jörg Bibiko bibiko at eva.mpg.de
Tue Sep 18 09:19:41 UTC 2012


Hi,

I know this is not the preferred way to send a change request but it's only one entire file: 

PlugIns > dialog > Commands > Utilities > TextMate.mm


Here the content:
-------------
#import "TextMate.h"

@interface NSObject (OakTextViewPrivate)
- (id)insertSnippetWithOptions:(NSDictionary*)options;
- (void)makeTextViewFirstResponder:(id)sender;
- (void)newDocumentAndActivate:(id)sender;
@end

id frontMostTextViewForSelector(SEL selector)
{

	// unique method for identifying a OakTextView
	SEL checkSelector = @selector(scopeContext);

	// Find the front most document's OakTextView
	for(NSWindow* win in [NSApp orderedWindows])
	{

		if([[win firstResponder] respondsToSelector:checkSelector] 
				&& [[win firstResponder] respondsToSelector:selector])
			return [win firstResponder];

		NSMutableArray* allViews = [[[[win contentView] subviews] mutableCopy] autorelease];
		for(NSView* view in allViews)
			if([view respondsToSelector:checkSelector] && [view respondsToSelector:selector])
				return view;

		NSMutableArray* allSubViews = [NSMutableArray array];
		for(NSUInteger i = 0; i < [allViews count]; ++i)
			[allSubViews addObjectsFromArray:[(id)CFArrayGetValueAtIndex((CFArrayRef)allViews, i) subviews]];
		for(NSView* view in allSubViews) {
			if([view respondsToSelector:checkSelector] && [view respondsToSelector:selector])
				return view;
		}

	}

	// If no textView was found create a new document
	if(id tmApp = [NSApp targetForAction:@selector(newDocument:)])
	{

		[tmApp newDocumentAndActivate:nil];

		if([[NSApp orderedWindows] count]
			&& [[[[NSApp orderedWindows] objectAtIndex:0] windowController] tryToPerform:
								@selector(makeTextViewFirstResponder:) with:nil])
		{
			id textView = [NSApp targetForAction:checkSelector];
			if(textView && [textView respondsToSelector:selector]) {
				// TODO How to know whether TM has finished the loading of a new doc?
				// Otherwise it could happen that a x-insert command will inserted something
				// _before_ TM will refresh an empty new doc with the content of @"";
				usleep(10000);
				return textView;
			}
		}
	}

	return nil;

}

void insert_text (NSString* someText)
{
	if(id textView = frontMostTextViewForSelector(@selector(insertText:)))
		[textView insertText:someText];
}

void insert_snippet (NSString* aSnippet)
{
	if(id textView = frontMostTextViewForSelector(@selector(insertSnippetWithOptions:)))
		[textView insertSnippetWithOptions:
			[NSDictionary dictionaryWithObject:aSnippet forKey:@"content"]];
}

-------------

The idea is to find the front most OakTextView first and then to process insert_text or insert_snippet. 
If no OakTextView was found it creates a new untitled document before inserting. Here I have a problem: How to know after invoking newDocumentAndActivate: whether TM2 did load the new window entirely? I solved it tentatively by using usleep(10000) to give TM2 a bit time but this is _not_ elegant. The issue here is that it happened to me in 10% of all trials that "insert_text" was faster then the initialization of the new doc, i.e. I saw the inserted text for a few msecs before the initialization routine replaced its content by @"".



Kind regards,
--Hans





More information about the textmate-dev mailing list