Index: TWDocument.h
===================================================================
--- TWDocument.h	(revision 9579)
+++ TWDocument.h	(working copy)
@@ -6,7 +6,7 @@
 @class OgreTextFinder;
 @class SCKSourceFile;
 
-@interface TWDocument : NSDocument
+@interface TWDocument : NSDocument <NSTetxViewDelegate>
 {
   NSAttributedString *aString;
   NSScrollView *scrollView;
Index: TWTextView.h
===================================================================
--- TWTextView.h	(revision 9579)
+++ TWTextView.h	(working copy)
@@ -1,8 +1,15 @@
 /* All Rights reserved */
 
 #include <AppKit/AppKit.h>
+@class SCKCodeCompletionResutls;
 
 @interface TWTextView: NSTextView
 {
+	// Cache the latest result
+	NSInteger location;
+	SCKCodeCompletionResutls *codeCompletionResults;
 }
+
+@property NSInteger location;
+@property SCKCodeCompletionResutls *codeCompletionResults;
 @end
Index: TWDocument.m
===================================================================
--- TWDocument.m	(revision 9579)
+++ TWDocument.m	(working copy)
@@ -223,4 +223,29 @@
   [[TWCharacterPanel sharedCharacterPanel] orderFront: self];
 }
 
+// Completion for the word currently being typed
+- (NSArray *) textView: (NSTextView *)textView
+		   completions: (NSArray *)words
+   forPartialWordRange: (NSRange)range
+   indexOfSelectedItem: (NSInteger *)index
+{
+	// Finding the insert point
+	NSInteger insertPoint = range.location;
+	// Getting the start of the token
+	NSInteger startOfToken = 0;
+	
+	// Doing completion
+	SCKCodeCompletionResults *codeCompletionResults = [sourceFile completeAtLocation: insertPoint];
+	
+	// Cache the latest result
+	[textView setcodeCompletionResults: codeCompletionResults];
+	[textView setLocation: insertPoint];
+	
+	// FIXME: Filter the result. Might NSTextView does it.
+	
+	// Sort the completion array
+	return [[codeCompletionResults completions]
+			sortArrayUsingSelector: @selector(caseInsensitiveCompare:)];
+}
+
 @end
Index: TWTextView.m
===================================================================
--- TWTextView.m	(revision 9579)
+++ TWTextView.m	(working copy)
@@ -3,6 +3,39 @@
 #include <AppKit/AppKit.h>
 #include "TWTextView.h"
 
+NSTimer *timer = nil;
+
 @implementation TWTextView
+@synthesize location, codeCompletionResults;
 
+- (id)init
+{
+	if (self = [super init])
+	{
+		codeCompletionResults = nil;
+	}
+	
+	return self;
+}
+
+- (void)textDidEndEditing: (NSNotification *)aNotification
+{
+	[timer invalidate];
+}
+
+- (void)keyDown: (NSEvent *)theEvent
+{
+	[timer invalidate];
+	
+	// esc key code is 55
+	if (55 != [theEvent keyCode])
+	{
+		timer = [NSTimer scheduledTimerWithTimeInterval: 0.5
+												 target: self
+											   selector: @selector(complete:)
+											   userInfo: nil
+												 repeat: NO];
+	}
+}
+
 @end
