       ;; fsharp-indent-offset.  Never turn it on, because the user must
       ;; have explicitly turned it off.
       (if (/= tab-width fsharp-indent-offset)
-          (setq indent-tabs-mode nil))
-      )))
-
-(defun fsharp-set-compile-command ()
-  "Hook to set compile-command locally, unless there is a Makefile in the
-   current directory."
-  (interactive)
-  (unless (or (null buffer-file-name)
-              (file-exists-p "makefile")
-              (file-exists-p "Makefile"))
-    (let* ((filename (file-name-nondirectory buffer-file-name))
-           (basename (file-name-sans-extension filename))
-           (command nil))
-      (cond
-       ((string-match ".*\\.fs\$" filename)
-        (setq command fsharp-compiler) ; (concat "fsc -o " basename)
-        )
-       ((string-match ".*\\.fsl\$" filename) ;FIXME
-        (setq command "fslex"))
-       ((string-match ".*\\.fsy\$" filename) ;FIXME
-        (setq command "fsyacc"))
-       )
-      (if command
-          (progn
-            (make-local-variable 'compile-command)
-            (setq compile-command (concat command " " filename))))
-      )))
-
-(add-hook 'fsharp-mode-hook 'fsharp-set-compile-command)
+          (setq indent-tabs-mode nil)))))
+
+(defun fsharp-choose-compile-command (file)
+  "Format an appropriate compilation command, depending on several factors:
+1. The presence of a makefile
+2. The file type
+3. The presence of an fsproj. "
+  (let* ((fname    (file-name-nondirectory file))
+         (ext      (file-name-extension file))
+         (fsproj   (fsharp-mode/find-sln-or-fsproj file))
+         (makefile (or (file-exists-p "Makefile") (file-exists-p "makefile"))))
+    (cond
+     (makefile          compile-command)
+     ((equal ext "fsl") (concat "fslex "  file))
+     ((equal ext "fsy") (concat "fsyacc " file))
+     ;; For .fs files, try to find a project first.
+     (fsproj            (concat fsharp-build-command " /nologo " fsproj))
+     ((equal ext "fs")  (concat fsharp-compile-command " --nologo " file))
+     (t                 compile-command))))
 
 (defun fsharp-find-alternate-file ()
   (interactive)

  (make-local-variable 'indent-line-function)
  (make-local-variable 'add-log-current-defun-function)
  (make-local-variable 'underline-minimum-offset)

  (add-hook 'completion-at-point-functions #'ac-fsharp-completion-at-point)



;;user customizable variables

(namespace fsharp-mode
  :export
  [find-sln-or-fsproj])

(defvar fsharp-mode-version 0.7
  "Version of this fsharp-mode")

;;; Compilation

(defvar fsharp-compile-command
  (or (executable-find "fsharpc")
      (executable-find "fsc"))
  "The program used to compile F# source files.")

(defvar fsharp-build-command
  (or (executable-find "xbuild")
      (executable-find "msbuild"))
  "The command used to build F# projects and solutions.")

(defvar fsharp-compiler nil
  "The command used to compile an individual F# buffer.
This will be set to a sane default, depending the type of file
and whether it is in a project directory.")
(make-variable-buffer-local 'fsharp-compiler)

;;; ----------------------------------------------------------------------------

(defvar fsharp-shell-active nil
  "Non nil when a subshell is running.")
