   )
 
 
+; Consider using 'text' for filtering
+(defun fsharp-completions (line col text)
+  (message (format "fsharp-completions called for (%d,%d) and '%s'" line col text))
+  (progn
+    (setq ac-fsharp-status 'fetch-in-progress)
+    (setq ac-fsharp-data nil)
+    (ac-fsharp-parse-file)
+    (ac-fsharp-send-completion-request line col)
+    (while (eq ac-fsharp-status 'fetch-in-progress)
+      (accept-process-output ac-fsharp-completion-process))
+    ac-fsharp-data))
 
 (defun fsharp-completion-at-point ()
-  "Return the data to complete the GDB command before point."
+  "Return a function ready to interrogate the F# compiler service for completions at point."
   (let ((end (point))
         (start
          (save-excursion
            (skip-chars-backward "^ ." (line-beginning-position))
            (point))))
+    (message (format "completion-at-point called for (%d,%d)" start end))
     (list start end
-          (progn
-            (setq ac-fsharp-status 'fetch-in-progress)
-            (setq ac-fsharp-data nil)
-            (ac-fsharp-parse-file)
-            (ac-fsharp-send-completion-request)
-            (while (eq ac-fsharp-status 'fetch-in-progress)
-              (accept-process-output ac-fsharp-completion-process))
-            ac-fsharp-data
-            ))))
+          (completion-table-dynamic
+           (apply-partially #'fsharp-completions
+                            (- (line-number-at-pos) 1)
+                            (current-column))))
+            ))
 
 (add-hook 'completion-at-point-functions #'fsharp-completion-at-point)
 ;(set (make-local-variable 'gud-gdb-completion-function) 'gud-gdb-completions)

(defvar ac-fsharp-data "")

(defun log-to-proc-buf (proc str)
  (let ((buf (process-buffer proc)))
    (when (buffer-live-p buf)
      (with-current-buffer buf
        (goto-char (process-mark proc))
        (insert str)
        (set-marker (process-mark proc) (point)))
      (if (get-buffer-window buf)
          (save-selected-window
            (select-window (get-buffer-window buf))
            (goto-char (process-mark proc)))))))

(defun log-psendstr (proc str)
  (log-to-proc-buf proc str)

  (log-psendstr ac-fsharp-completion-process
                (format "project \"%s\"\n" buffer-file-name)))

(defun ac-fsharp-send-completion-request (line col)
  (let ((request (format "completion \"%s\" %d %d\n"
                               (buffer-file-name) line col)))
    (message (format "Sending completion request for: '%s' of '%s'" ac-prefix request))
      (log-psendstr ac-fsharp-completion-process request)))

