Skip to content

Instantly share code, notes, and snippets.

@mnzk
Created November 23, 2011 09:30
Show Gist options
  • Save mnzk/1388274 to your computer and use it in GitHub Desktop.
Save mnzk/1388274 to your computer and use it in GitHub Desktop.
A Helper of fsharp-mode (v0.3) for Mono
;;; fs-mono.el --- A Helper of fsharp-mode (v0.3) for Mono
(require 'cl)
(add-to-list 'load-path "~/.emacs.d/site-lisp/fsharp-mode")
(add-to-list 'auto-mode-alist '("\\.fs[iylx]?$" . fsharp-mode))
(require 'fsharp)
(autoload 'run-fsharp "inf-fsharp" "Run an inferior F# process." t)
(defvar fs-mono:mono-version 4)
(defvar fs-mono:base-path nil)
(defvar fs-mono:mono-path-list nil)
(defvar fs-mono:mono-path nil)
(defvar fs-mono:execute-options nil)
(defvar fs-mono:execute-command nil)
(defvar fs-mono:compile-options nil)
(defvar fs-mono:compile-command nil)
(defvar fs-mono:repl-options "--readline-")
(defvar fs-mono:repl-command nil)
(defvar fs-mono:script-options "--exec")
(defvar fs-mono:script-command nil)
(defun fs-mono:add-assembly-path (path)
(add-to-list 'fs-mono:mono-path-list (expand-file-name path)))
(defun fs-mono:set-assembly-paths (paths)
(setq fs-mono:mono-path-list
(mapcar 'expand-file-name paths)))
(defun fs-mono:get-dlls (dir)
(let ((files (directory-files dir))
(pred (lambda (s)
(and (string-match "\\.dll$" s)
(not (string-match "^FSharp\\.Core\\.dll$" s))))))
(remove-if-not pred files)))
(defun fs-mono:get-fsi-cmd ()
(concat fs-mono:base-path (if (= 2 fs-mono:mono-version)
"/bin/fsharpi2" "/bin/fsharpi")))
(defun fs-mono:get-fsc-cmd ()
(concat fs-mono:base-path (if (= 2 fs-mono:mono-version)
"/bin/fsharpc2" "/bin/fsharpc")))
(defun fs-mono:init-env ()
(interactive)
(setq fs-mono:mono-path
(mapconcat 'identity fs-mono:mono-path-list ":"))
(setenv "MONO_PATH" fs-mono:mono-path)
(setq fs-mono:execute-command
(format "MONO_PATH=%s mono" fs-mono:mono-path))
(let* ((dlls (apply 'append
(mapcar 'fs-mono:get-dlls fs-mono:mono-path-list)))
(refs (mapconcat '(lambda (s) (concat "-r:" s))
dlls " "))
(includes (mapconcat '(lambda (s) (concat "-I:" s))
fs-mono:mono-path-list " ")))
(setq fs-mono:compile-options (concat includes " " refs))
(setq fs-mono:compile-command (concat (fs-mono:get-fsc-cmd)
" " fs-mono:compile-options)))
(setq fs-mono:repl-command
(concat (fs-mono:get-fsi-cmd) " " fs-mono:repl-options))
(setq fs-mono:script-command
(concat (fs-mono:get-fsi-cmd) " " fs-mono:script-options))
(setq inferior-fsharp-program fs-mono:repl-command)
(setq fsharp-compiler fs-mono:compile-command))
(defun fs-mono:run-file ()
(interactive)
(let ((name (buffer-file-name)))
(cond
((string-match "^\\(.*\\)\\.\\(fs\\|fsi\\)$" name)
(shell-command (format "%s %s.exe"
fs-mono:execute-command
(match-string 1 name))))
((string-match "^\\(.*\\)\\.\\(fsx\\)$" name)
(shell-command (format "%s %s"
fs-mono:script-command
name)))
(t
(message "Not Executable File : %s" name)))))
;; Settings --------------------------------------------------
(setq fs-mono:base-path "~/local/mono") ;; fsharp install path
;;(setq fs-mono:mono-version 2) ;; if need
;; Assembly Path Settings
(cond
;; mono v2.0
((= 2 fs-mono:mono-version)
(fs-mono:set-assembly-paths
(list (concat fs-mono:base-path "/lib/mono/2.0"))))
;; mono v4.0
(t
(fs-mono:set-assembly-paths
(list (concat fs-mono:base-path "/lib/mono/4.0")
(concat fs-mono:base-path "/FSharpPowerPack-2.0.0.0/bin")
(concat fs-mono:base-path "/FParsec")))))
(fs-mono:init-env)
;; hook
(define-key fsharp-mode-map "\C-cx" 'fs-mono:run-file)
(provide 'fs-mono)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment