Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Emacs mode questions - indentation and build

Tags:

c#

emacs

mono

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?

like image 241
prosseek Avatar asked Oct 17 '10 18:10

prosseek


2 Answers

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.

like image 121
Adam Rosenfield Avatar answered Sep 25 '22 17:09

Adam Rosenfield


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

like image 44
cristobalito Avatar answered Sep 25 '22 17:09

cristobalito