This is a major-mode for editing C# files.

The most current version of this package can always be found on github:

* https://github.com/josteink/csharp-mode

It provides an editing mode for C# code and includes code indentation, fontification, codedoc generation, and integration with imenu.el.

Note:

[NxhtmlMode nXhtml] includes a slightly modified version of latest csharp-mode.el.

== Usage Notes and Hints ==

=== Imenu ===

Imenu support is new for v0.8.3.  It indexes the entire .cs buffer and produces a menu of the classes, methods, properties, etc.  For large buffers this can cause a delay in opening a file, of several seconds. If you don't want this, see '''`csharp-want-imenu'''' .

=== Flymake ===

To use flymake, just open a C# file.  If you have flymake loaded (you can do (require 'flymake) in your csharp-mode-hook fn) then it will automatically start working. The flymake stuff in csharp-mode assumes csc.exe is on your path; if it is not, you can
explicitly specify the flymake command to use in code comments, like this:

http://i.imgur.com/28V7e.png

You can run [http://msdn.microsoft.com/en-us/library/bb429476(VS.80).aspx FxCop] for your flymake. This is really handy.  To do so, you need at least v0.8.3, and you need a comment like this near the top of your file:

<pre>
// flymake: c:\fxcop\FxCopcmd.exe /c /rid:-Microsoft.Naming#CA1704  /f:Base64stream.dll
</pre>

All of the issues found by flymake will be highlighted in the buffer.

If you use Flymake - with any language - you may want to check out some complementary stuff.

* FlymakeCursor automatically displays the flymake error for the current line in the minibuffer. The default flymake behavior is to display the message on mouse hover, so the FlymakeCursor thing is nice for keyboard jockeys.

* [http://www.emacswiki.org/emacs/RFringe RFringe] displays buffer-relative locations in the fringe. It gives you a visual summary of the flymake issues, and their approximate locations.

http://i.imgur.com/SgoLa.png

Neither FlymakeCursor nor RFringe is part of csharp-mode *or* flymake; they are complementary, add-on things.

=== Compile ===

csharp-mode sets the '''`compilation-error-regexp-alist-alist'''', so that error messages emitted by csc.exe will be properly handled by next-error and prev-error. The default build command is 'nmake' but you can specify it explicitly in each buffer with in-code comments:

<pre>
// compile: c:\.NET3.5\csc.exe /t:module /r:MyAssembly.dll file.cs
</pre>

Example:

http://i.imgur.com/28V7e.png

You can replace that compile command with whatever you like. For example:

<pre>
// compile: msbuild.exe /p:Configuration:Release
</pre>



=== YASnippet ===

yasnippet is a nice snippet expansion package, available elsewhere. csharp-mode defines some snippets automatically for you, and makes them available if you have yasnippet.  This means you can type for<TAB> and get a template of a for loop. There are snips for switch statements, properties, while loops, and so on. Pretty handy. You can also define your own snips.

=== CSharpRepl ===

This starts the repl on C-c C-z:
<pre>
(defun my-csharp-repl ()
    "Switch to the CSharpRepl buffer, creating it if necessary."
    (interactive)
    (if-let ((buf (get-buffer "*CSharpRepl*")))
        (pop-to-buffer buf)
      (when-let ((b (make-comint "CSharpRepl" "csharp")))
        (switch-to-buffer-other-window b))))
(define-key csharp-mode-map (kbd "C-c C-z") 'my-csharp-repl)
</pre>


=== Other tidbits ===

There's codedoc support starting with version v0.7.2 (but you should really upgrade to v0.8.?).  For the code doc: position point above a method/class, and type three slashes; you will get a codedoc template inserted into the buffer, similar to the effect you may have seen in VisualStudio.  Fill-paragraph works as expected in the documentation string; there should be no need to setq paragraph-separate in your csharp-mode hook function.  -- DinoChiesa

Example:

http://imgur.com/vFvf3.gif

It tries to be smart about which template to insert.  If you type 3 slashes after a closing </para>, then you'll get another set of <para></para> .

http://i44.tinypic.com/zl99bc.jpg

[new]

csharp-mode Works with Linum.el :

http://i42.tinypic.com/29pdmjd.jpg



----
== Complementary Stuff / Minor Modes ==

||[http://code.google.com/p/yasnippet/ yasnippet.el]||||dynamically expand snippets, like dabbrev on steroids. [http://blogs.msdn.com/dotnetinterop/archive/2008/04/15/c-snippets-for-yasnippet-el-emacs-snippets.aspx How to use yasnippet with C#.]||
||ImenuMode||||produce an index of the contents of a C# buffer.||
||HideShow||||hide and show regions of code.[http://blogs.msdn.com/dotnetinterop/archive/2008/04/14/making-hideshow-el-work-with-csharp-mode-el-and-region-endregion.aspx How to use hideshow.el with C#]||
||FlymakeCsharp||||now included with csharp-mode. check syntax more-or-less continually, and use overlays to highlight syntax problems in the code.||
||FlymakeCursor||||display flymake messages in the minibuffer, automatically, after a delay.||
||[http://code.google.com/p/csharpmode/source/browse/trunk/aspx-mode.el aspx-mode.el]||||multi-mode for editing ASPX files within emacs. Handles HTML, JS, CSS, and C#.||
||[http://web-mode.org web-mode.el]||||multi-mode for editing ASPX/JSP/PHP that also embed  JS and CSS.||
||[[MSTFS]]||||Emacs integration with Team foundation server, via the tf.exe tool||
||[http://stud4.tuwien.ac.at/~e0225855/linum/linum.html Linum.el]||||line numbers (See local elisp: Lisp:linum-ex.el)||
||[http://www.gnu.org/software/emacs/manual/html_node/emacs/Reverting.html Auto-Revert]||||refresh .cs buffers, after you've edited them externally, for example, in Visual Studio.||
||[http://code.google.com/p/csharpmode/source/browse/trunk/csharp-completion.el code completion] ||||Code completion elisp logic, that is integrated with csharp-mode.  It runs powershell as an inferior shell within emacs, to dynamically compile code snippets, and then introspect the result to provide realtime (subsecond) code completion in csharp buffers. To get it to work, you need the [http://code.google.com/p/csharpmode/source/browse/trunk/CscompUtilities.cs C# code], and be sure to read the [http://code.google.com/p/csharpmode/source/browse/trunk/Readme.txt readme]. Works only on Windows. Requires Powershell (free). Does not require Visual Studio, or SharpDevelop.||
||[http://ozymandias.dk/emacs/emacs.html#vj-complete vj-complete] ||||Is C# Code Completion in emacs really possible? This is an alternative code completion effort that pre-dates the above.||
||[http://code.google.com/p/idebridge/ ide-bridge]||||C# Code Completion/refactoring/navigation/compilation. Integrates Emacs with SharpDevelop.||
||[http://github.com/sp3ctum/omnisharp-emacs Omnisharp-Emacs]||||Provides smart completion, refactoring, finding implementations/usages, eldoc integration and more.||






== Comments / Remarks / Gripes ==

=== With Omnisharp, it's amazing. ===
I have been using csharp-mode everyday in combination with [http://github.com/sp3ctum/omnisharp-emacs Omnisharp-Emacs] and it has been a joy. I am able to manage large solutions, and do my building from emacs. The code completion, and IDE like features from omnisharp provide little reason to use Visual Studio any longer. The base of csharp-mode is old and not updated, but it handles indenting and fontification and that's really all I need from it.
    
=== Folding tip ===

I use the following hide-show setup for folding brace-blocks (just like c/c++ mode)

<pre>
(unless (assoc 'csharp-mode hs-special-modes-alist)
          (push '(csharp-mode
                  "{" "}"
                  "/[*/]"
                  nil
                  hs-c-like-adjust-block-beginning)
                hs-special-modes-alist))
</pre>

I also wrote a lineup function for multiline strings:
<pre>
string foo = @"My very long string " +
              "that spans multiple lines.";
</pre>
Here it is:
<pre>
(defun csharp-lineup-string-cont (langelem)
  "Like `c-lineup-string-cont' but works with csharp string continuations."
  (save-excursion
    (back-to-indentation)
    (and (looking-at "@?\\s\"")
         (let ((quote (if (equal (char-after) ?@)
                          (char-after (1+ (point)))
                        (char-after)))
               pos)
           (while (and (progn (c-backward-syntactic-ws)
                              (when (eq (char-before) ?+)
                                (backward-char)
                                (c-backward-syntactic-ws))
                              (eq (char-before) quote))
                       (c-safe (c-backward-sexp) t)
                       ;; uncomment this to lineup under the @
                       ;;(progn (if (eq (char-before) ?@) (backward-char)) t)
                       (/= (setq pos (point)) (c-point 'boi))))
           (when pos
             (goto-char pos)
             (vector (current-column)))))))
</pre>
Use for the statement-cont option in c-offsets-alist.  I use it like so:

  (statement-cont . (my-lineup-csharp-string-cont +))

-- DaveS

== Some History: previous attempts at C# mode ==

; http://mfgames.com/csharp-mode
:Work by DylanMoonfire on a derived cc-mode. See these threads on sourceforge [http://sourceforge.net/mailarchive/forum.php?thread_id=6676996&forum_id=6827], [http://sourceforge.net/mailarchive/forum.php?thread_id=6596410&forum_id=6827] for more info. [http://cinki.mfgames.com/buglist.cgi?bug_status=__all__&product=csharp-mode Bug Tracking] (Broken link as of December 2009)
:: The above link was changed to point people to the Google Code page since I'm not working on it anymore. I left the link to the historical releases on the site. There were no reported issues when the bug tracker was shut down at the end of 2009. -- DylanMoonfire (November 2010)


; http://www.cybercom.net/~zbrad/DotNet/Emacs/
:This is the original.  It involves changes to a set of standard elisp files.  It apparently doesn't handle @"\" correctly.

* please send me a more detailed description of the problem reported here? Thanks mailto:zbrad@cybercom.net

; http://davh.dk/script/
:Is a mod of zbrad's original. Collects all the changes into a single file.  It also doesn't solve the @"\" problem.
[http://lists.ximian.com/pipermail/mono-list/2002-May/006182.html]

; http://csde.sourceforge.net/
:Based on JavaDevelopmentEnvironment.  Appears to be abandonware.  Not sure if this has a C# mode or not.

; http://www.devhood.com/tools/tool_details.aspx?tool_id=643 : this is just a link to zbrad's code.

; http://www.loveshack.ukfsn.org/emacs/index.html#cc-php-js
:A modified CC mode from Emacs 22, with .elcs for Emacs 21.  Done by someone who doesn't use C#, but reported deficiencies will probably get fixed.


----
CategoryModes ProgrammingModes
