开发者

C# Emacs mode questions - indentation and build

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 f开发者_运维知识库ile 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?


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜