     (setq racer-line-number (racer-get-line-number))
     (setq racer-column-number (current-column))
     (setq racer-completion-results `())
-    (write-region nil nil tmp-file-name))
+    (write-region nil nil tmp-file-name nil -1))
 
 (defun racer--candidates ()
   "Run the racer complete command and process the results."
   (setq racer-tmp-file-name (concat (buffer-file-name) ".racertmp"))
   (racer--write-tmp-file racer-tmp-file-name)
   (setenv "RUST_SRC_PATH" (expand-file-name racer-rust-src-path))
-    (let ((lines (process-lines racer-cmd
-				"complete"
-				(number-to-string racer-line-number)
-				(number-to-string racer-column-number)
-				racer-tmp-file-name)))
-      (delete-file racer-tmp-file-name)
-      (dolist (line lines)
-	(when (string-match "^MATCH \\([^,]+\\),\\([^,]+\\),\\([^,]+\\),\\([^,]+\\),\\([^,]+\\),\\(.+\\)$" line)
-	  (let ((completion (match-string 1 line))
-		(linenum (match-string 2 line))
-		(colnum (match-string 3 line))
-		(fname (match-string 4 line))
-		(matchtype (match-string 5 line))
-		(contextstr (match-string 6 line)))
-	    (put-text-property 0 1 'contextstr contextstr completion)
-	    (put-text-property 0 1 'matchtype matchtype completion)
-	    (push completion racer-completion-results))))
-      racer-completion-results))
+  (let ((lines (process-lines racer-cmd
+                              "complete"
+                              (number-to-string racer-line-number)
+                              (number-to-string racer-column-number)
+                              racer-tmp-file-name)))
+    (delete-file racer-tmp-file-name)
+    (dolist (line lines)
+      (when (string-match "^MATCH \\([^,]+\\),\\([^,]+\\),\\([^,]+\\),\\([^,]+\\),\\([^,]+\\),\\(.+\\)$" line)
+        (let ((completion (match-string 1 line))
+              (linenum (match-string 2 line))
+              (colnum (match-string 3 line))
+              (fname (match-string 4 line))
+              (matchtype (match-string 5 line))
+              (contextstr (match-string 6 line)))
+          (put-text-property 0 1 'contextstr contextstr completion)
+          (put-text-property 0 1 'matchtype matchtype completion)
+          (put-text-property 0 1 'fname fname completion)
+          (put-text-property 0 1 'linenum linenum completion)
+          (push completion racer-completion-results))))
+    racer-completion-results))
 
-(defun racer--prefix ()
-  "Run the racer prefix command and process the results."
-  (setq racer-tmp-file-name (concat (buffer-file-name) ".racertmp"))
-  (racer--write-tmp-file racer-tmp-file-name)
-  (setenv "RUST_SRC_PATH" (expand-file-name racer-rust-src-path))
-  (let ((lines (process-lines racer-cmd
-			      "prefix"
-			      (number-to-string racer-line-number)
-			      (number-to-string racer-column-number)
-			      racer-tmp-file-name)))
-    (delete-file racer-tmp-file-name)
-    (when (string-match "^PREFIX \\(.+\\),\\(.+\\),\\(.*\\)$" (nth 0 lines))
-      (match-string 3 (nth 0 lines)))))
+(defun racer--company-prefix ()
+  "Returns the symbol to complete. If racer-begin-after-member-access is t,
+begins completion automatically if the cursor is followed by a dot or ::."
+  (if racer-begin-after-member-access
+    (company-grab-symbol-cons "\\.\\|::" 2)
+    (company-grab-symbol)))
 
-(defun racer--complete-at-point-fn ()
-  "Run the racer complete command and process the results."
-  (setq racer-tmp-file-name (concat (buffer-file-name) ".racertmp"))
-  (racer--write-tmp-file racer-tmp-file-name)
-  (setenv "RUST_SRC_PATH" (expand-file-name racer-rust-src-path))
-  (save-excursion
-    (let ((lines (process-lines racer-cmd
-				"complete"
-				(number-to-string racer-line-number)
-				(number-to-string racer-column-number)
-				racer-tmp-file-name)))
-      (delete-file racer-tmp-file-name)
-      (dolist (line lines)
-	(when (string-match "^MATCH \\([^,]+\\),\\(.+\\)$" line)
-	  (let ((completion (match-string 1 line)))
-	    (push completion racer-completion-results)))
-	
-	(when (string-match "^PREFIX \\(.+\\),\\(.+\\),\\(.*\\)$" line)
-	  (setq racer-start-pos (string-to-number (match-string 1 line)))
-	  (setq racer-end-pos (string-to-number (match-string 2 line))))
-	)
-      )
-    )
-  ;(message "start %s end %s" start end)
-  (list (- (point) (- racer-end-pos racer-start-pos))
-        (point)
-        racer-completion-results)
-  )
+(defun racer--company-annotation (arg)
+  "Gets and formats annotation data from the arg match."
+  (let ((meta (get-text-property 0 'contextstr arg)))
+    (format "%10s : %s"
+            (or (save-match-data
+                  (and (string-match "\\(.+\\) {" meta)
+                       (match-string 1 meta)))
+                meta)
+            (get-text-property 0 'matchtype arg))))
 
-(defun racer-company-complete (command &optional arg &rest ignored)
-  "Run the racer command for `COMMAND' and format using `ARG'.
-`IGNORED' is unused."
-  (interactive)
-  ;(message "PHIL racer-company-complete %s %s %s" command arg ignored)
-  (when (looking-back "[a-zA-z1-9:.]")
-    (cl-case command
-      (prefix (racer--prefix))
-      (candidates (racer--candidates))
-      (duplicates t)
-      (sorted nil)
-      (annotation
-       (progn
-	 (format "%s %10s : %s"
-		 (make-string (max 0 (- 20 (length arg))) ? )
-		 (get-text-property 0 'matchtype arg)
-		 (get-text-property 0 'contextstr arg))))
-      (meta (format "%s" (get-text-property 0 'contextstr arg)))
-;      (doc-buffer (company-doc-buffer "PHil hello Yeah"))
-      )))
+(defun racer--company-location (arg)
+  (let ((fname (get-text-property 0 'fname arg))
+        (linenum (get-text-property 0 'linenum arg)))
+    (cons fname (string-to-number linenum))))
 
-;; (defun racer-complete (command &optional arg &rest ignored)
-;;   (interactive)
-;;   (message "PHIL racer-company-complete %s %s %s" command arg ignored)
-;;   (when (looking-back "[a-zA-z1-9:.]")
-;;     (case command
-;;       (prefix (racer--prefix))
-;;       (candidates (racer--candidates))
-;;       (duplicates t)
-;;       (sorted nil)
-;;       (annotation (format ":%s yeah mother" arg))
-;;       (meta (format "This value is named %s" arg))
-;; ;      (doc-buffer (company-doc-buffer "PHil hello Yeah"))
-;;       )))
-
-
-;; (add-hook 'completion-at-point-functions 'racer-complete nil)
-
-(defun racer--complete-or-indent ()
-  "Complete with company-mode or indent."
-  (interactive)
-  (if (company-manual-begin)
-      (company-complete-common)
-    (indent-according-to-mode)))
 
 (defun string/ends-with (s ending)
   "Return non-nil if string S ends with ENDING."

    "/usr/local/src/rust/src")
  "Path to the rust source tree."
  :type 'file
  :group 'racer)

(defvar racer-file-name)

         (let ((elength (length ending)))
           (string= (substring s (- 0 elength)) ending)))
        (t nil)))


(defun racer-find-definition ()
  "Run the racer find-definition command and process the results."
