7
votes

I installed emacs C# mode.

The .emacs file is as follows

(require 'csharp-mode)
(setq auto-mode-alist
      (append '(("\\.cs$" . csharp-mode)) auto-mode-alist))
(defun my-csharp-mode-fn ()
  "function that runs when csharp-mode is initialized for a buffer."
  (setq default-tab-width 4)
)
(add-hook  'csharp-mode-hook 'my-csharp-mode-fn t)

It works pretty fine, but I see the block ({..}) is aligned what I intended. I mean, in some cases I have this.

private static int StringCompare(string x, string y)
{
  int result;
  if (x == null)
    {

    }
}

when I expect this

private static int StringCompare(string x, string y)
{
  int result;
  if (x == null)
  {

  }
}

Together with this, I always have 2 indentation for the code, but I want it to be 4.

My questions are

  • How can I control the indentation in C# emacs mode?
  • How can I control the '{' and '}' to have the same indentation as it's previous code.
  • Does C# mode provide compilation to generate exe/dll file within the editor with commands?

I use emacs C# mode on Mac OS X/mono.

ADDED

I found that C# mode can also use C mode, so M-x c-set-style works, and awk style just works for me. The problem is that I have to turn on awk mode whenever I use c mode. Is there a way to run "M-x c-set-style and awk" mode automatically with c mode?

3
to answer your question about compiling - yes. Just use M-x compile. You need to set your compile command appropriately (msbuild, nmake, csc.exe, whatever). csharpmode is also integrated for use with flymake. There is documentation in the csharpmode elisp code describing how this integration works.Cheeso

3 Answers

10
votes

Add these lines to your my-csharp-mode-fn:

; Set indentation level to 4 spaces (instead of 2)
(setq c-basic-offset 4)
; Set the extra indentation before a substatement (e.g. the opening brace in
; the consequent block of an if statement) to 0 (instead of '+)
(c-set-offset 'substatement-open 0)

Alternatively, you can add them to your common C mode hook, which runs for all C mode-related modes: C, C++, Objective-C, Java, C#, and more:

(defun my-c-mode-common-hook ()
  (setq c-basic-offset 4)
  (c-set-offset 'substatement-open 0))
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)

See the CC mode documentation for the nitty-gritty details on customizing indentation.

1
votes

For the indentation, you want (setq c-basic-offset 4) in your hook above.

0
votes

The advice given by @Adam Rosenfeld here for making the changes to your .emacs file can also be implemented via the easy customization interface provided by Emacs.

To set c-basic-offset to 4:

M-x customize-option RET c-basic-offset RET

To set substatement-open to 0:

M-x customize-option RET c-offsets-alist RET

The easy customization interface provided by Emacs will add these settings to your .emacs file for you.

Note that these changes will affect all C mode-related modes. So if this is not what you want, please see Adam's advice for including the appropriate additions to csharp-mode-hook.