[TxMt] Re: Cocoa Autocomplete

Hans-Jörg Bibiko bibiko at eva.mpg.de
Thu Jan 11 23:07:31 UTC 2007


>> If, however, there was a way to interface with the Cocoa  
>> "complete:", then it would solve these problems. Perhaps there may  
>> be a way of doing this using an AppleScript.
> I think you could write a Foundation command-line tool that calls  
> [[NSSpellChecker sharedSpellChecker]  
> completionsForPartialWordRange:inString:language:inSpellDocumentWithTa 
> g:] and provides its results via tm_dialog.  I did a similar thing  
> in writing a tool that asks BibDesk for citation completions, and  
> it works pretty well.

Well, that's a good idea, but I have a small problem with that.

The function

[[NSSpellChecker sharedSpellChecker]  

needs a inSpellDocumentWithTag and a language.

But, there is a problem. I invoke this command by writing a tmCommand  
which calls BASH. The used NSSpellChecker has no info about TM's  
spelling language and there's also no unique inSpellDocumentWithTag.
In other words you loose the nice feature of having a ranked output  
and you have to set the language by yourself.

I don't know whether I'm right, but I believe that NSSpellChecker can  
only invoke inside of TM or, maybe, by putting it into a tmPlugin to  
get this nice feature.

Here is my humble test script 'a.m':

#import <AppKit/AppKit.h>

int main(int argc, char* argv[])
	NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

	[NSApplication sharedApplication];

	NSString *wd = [NSString stringWithUTF8String:argv[1]];
	NSRange rg = {0, [wd length]};

	NSSpellChecker* checker = [NSSpellChecker sharedSpellChecker];
	NSArray* list;

	NSFileHandle *fh = [NSFileHandle fileHandleWithStandardOutput];
	NSString *lg = @"en_GB";
	if (argc > 2) {
		lg = [NSString stringWithUTF8String:argv[2]];

	list = [checker completionsForPartialWordRange:rg inString:wd  
language:lg inSpellDocumentWithTag:0];
	int cind;
	for(cind=0; cind<[list count]; cind++){
		[fh writeData:[(NSData*)[list objectAtIndex:cind]  
		[fh writeData:[@"\n" dataUsingEncoding:NSUTF8StringEncoding]];
	[pool release];


to compile it write: gcc a.m -framework AppKit

to run it:
 >a.out "beef"
 >a.out "app" "en_US"
 >a.out "Wanderu" "de"

You can replace this command within the Ruby script.
Hint: This code (Ruby) is not utf-8 save!

Any comments? Did I something wrong?



More information about the textmate mailing list