 ;; (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."
-      (cond ((>= (length s) (length ending))
-             (let ((elength (length ending)))
-               (string= (substring s (- 0 elength)) ending)))
-            (t nil)))
+  "Return non-nil if string S ends with ENDING."
+  (cond ((>= (length s) (length ending))
+         (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."
   (interactive)
-  (setq tmpfilename (concat (buffer-file-name) ".racertmp"))
-  (racer--write-tmp-file tmpfilename)
-  (setenv "RUST_SRC_PATH" rust-srcpath)
+  (setq racer-tmp-file-name (concat (buffer-file-name) ".racertmp"))
+  (racer--write-tmp-file racer-tmp-file-name)
+  (setenv "RUST_SRC_PATH" racer-rust-src-path)
   (push-mark)
-  (let ((lines (process-lines racer-cmd 
+  (let ((lines (process-lines racer-cmd
 			      "find-definition"
-			      (number-to-string linenum)
-			      (number-to-string col) 
-			      tmpfilename)))
-    (delete-file tmpfilename)
+			      (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 ((linenum (match-string 2 line))

(defvar racer-cmd "/home/pld/src/rust/racer/bin/racer")
(defvar rust-srcpath "/usr/local/src/rust/src")

; rust-mode no longer requires cl, so am putting it here for now (this file uses 'case')
(require 'cl)

(defun racer--write-tmp-file (tmpfilename) 
    (push-mark)
    (setq filename (buffer-file-name))
    (setq linenum (count-lines 1 (point)))
    (setq col (current-column))
    (setq completion-results `())
    (write-region nil nil tmpfilename))

(defun racer--candidates () 
  (setq tmpfilename (concat (buffer-file-name) ".racertmp"))
  (racer--write-tmp-file tmpfilename)
  (setenv "RUST_SRC_PATH" rust-srcpath)
    (let ((lines (process-lines racer-cmd 
				"complete"
				(number-to-string linenum)
				(number-to-string col) 
				tmpfilename)))
      (delete-file tmpfilename)
      (dolist (line lines)
	(when (string-match "^MATCH \\([^,]+\\),\\([^,]+\\),\\([^,]+\\),\\([^,]+\\),\\([^,]+\\),\\(.+\\)$" line)
	  (let ((completion (match-string 1 line))

	  (if (string/ends-with fname ".racertmp")
	      (find-file (substring fname 0 -9))
	    (find-file fname))
	  (forward-line (string-to-number linenum))
	  (forward-char (string-to-number charnum))
	  )))))

