Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using csharp-mode and Mono

Tags:

c#

emacs

macos

I'm using csharp-mode in Emacs 23 on OS X. I'd like to get the flymake syntax checking working but I'm not familiar enough with emacs lisp to know where to change things in the csharp-mode.el file to change the compiler. Any assistance would be appreciated.

like image 522
Jeremiah Peschka Avatar asked Jul 28 '11 18:07

Jeremiah Peschka


1 Answers

If you add the following to your Emacs init file, that should do the job:

(add-hook  'csharp-mode-hook 'flymake-mode)

From EmacsWiki:

History The original flymake-for-csharp came from a Blog Post on MSDN. It has since been generalized, updated, and made more reliable and flexible. In May 2011, it was integrated into csharp-mode itself.

In order to change the compiler, you can add a comment at the top of your C# code:

// flymake: csc.exe /t:module /R:MyAssembly.dll @@FILE@@

For more detail on options for changing the compiler, see the comments in the csharp-mode.el source file (search for "csharp-flymake-get-cmdline").

EDIT: Ok, based on your comment below about not wanting to put the flymake comment line in your C# code, I've come up with an alternative solution. Put the following code in your Emacs init file. Change the (setq my-csharp-default-compiler "mono @@FILE@@") line to whatever compile line you want. Now, whenever you open a C# file, you should be able to use flymake without adding the comment line into your C# source. If, at some later stage, you want to use the standard csharp-mode mechanism (of looking for a flymake comment in the C# source file), you just have to change the statement to (setq my-csharp-default-compiler nil).

;; Basic code required for C# mode
(require 'flymake)
(autoload 'csharp-mode "csharp-mode" "Major mode for editing C# code." t)
(setq auto-mode-alist  (append '(("\\.cs$" . csharp-mode)) auto-mode-alist))

;; Custom code to use a default compiler string for all C# files
(defvar my-csharp-default-compiler nil)
(setq my-csharp-default-compiler "mono @@FILE@@")

(defun my-csharp-get-value-from-comments (marker-string line-limit)
  my-csharp-default-compiler)

(add-hook 'csharp-mode-hook (lambda ()
                              (if my-csharp-default-compiler
                                  (progn
                                    (fset 'orig-csharp-get-value-from-comments
                                          (symbol-function 'csharp-get-value-from-comments))
                                    (fset 'csharp-get-value-from-comments
                                          (symbol-function 'my-csharp-get-value-from-comments))))
                              (flymake-mode)))
like image 161
zev Avatar answered Nov 14 '22 15:11

zev