简体   繁体   中英

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 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 Mx 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 "Mx 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.

对于缩进,您需要在上面的钩子中设置(setq c-basic-offset 4)。

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM