Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Trying to use only tabs for indenting c# files - csharp-mode error?

32 views
Skip to first unread message

Guido Van Hoecke

unread,
Jan 8, 2012, 8:01:38 AM1/8/12
to help-gn...@gnu.org, Óscar Fuentes
Hi,

Objective: use only tabs for indenting c# files

I start with an 'emacs -Q' session.

C-x C-f MyClass.cs

(This file has been created with MonoDevelop, and uses only tabs for
indentation, and - in MonoDevelop - the tab width is set to 4.)

As expected, MyClass.cs is shown in fundamental mode.

We want to see the whitespace, so

M-x whitespace-mode

This shows clearly that only tabs are used for indentation.

So lets load csharp-mode and select that mode:

M-x load-file csharp-mode.el
M-X csharp-mode

That removes the whitespace visualisation, so reactivate it:

M-x whitespace-mode

Tell emacs that it should use tabs for indenting:

M-x set-variable [enter] indent-tabs-mode [enter] t [enter]

Ultimately, I want the tab-width set to 4, but for now I just want to
leave or set it to 8:

M-x set-variable [enter] tab-width [enter] 8 [enter]

I will use following legend when showing file content:
# represents a leading tab
+ represents a leading empty space (following a preceding tab)
= represents a leading space character

Here's the source file:

namespace Application
{
#+++++++public class MyClass
#+++++++{
#+++++++#+++++++public MyClass ()
#+++++++#+++++++{
#+++++++#+++++++}
#+++++++}
}

Deleting the first tab changes it to:

namespace Application
{
=======public class MyClass
#+++++++{
#+++++++#+++++++public MyClass ()
#+++++++#+++++++{
#+++++++#+++++++}
#+++++++}
}

Hitting tab key while point is the 'public class' line changes it to:

namespace Application
{
==public class MyClass
#+++++++{
#+++++++#+++++++public MyClass ()
#+++++++#+++++++{
#+++++++#+++++++}
#+++++++}
}

I can't help but feeling that this is in error!

The tab-width is 8,
indent-tabs-mode is t
and still, it uses spaces to indent, and only two of them.

For the record, the emacs and csharp-mode versions:

GNU Emacs 23.3.1 (x86_64-apple-darwin, NS apple-appkit-1038.36)
of 2011-12-13 on bob.porkrind.org

;;; csharp-mode.el --- C# mode derived mode

;; Author : Dylan R. E. Moonfire (original)
;; Maintainer : Dino Chiesa <dpch...@hotmail.com>
;; Created : Feburary 2005
;; Modified : May 2011
;; Version : 0.8.6
;; Keywords : c# languages oop mode
;; X-URL : http://code.google.com/p/csharpmode/
;; Last-saved : <2011-May-21 20:28:30>

So one more test: insert a 'public string MyString' line
before the 'public Myclass()' line by positioning point after the second
{ and just typing:

namespace Application
{
==public class MyClass
#+++++++{
#+++++++==public string MyString;
#+++++++#+++++++public MyClass ()
#+++++++#+++++++{
#+++++++#+++++++}
#+++++++}
}

The leading tab is respected and replicated, but the new line is then
indented by two spaces rather than by an extra tab!?

And finally"

M-<
C-SPC
M->
<tab>

produces following result:


namespace Application
{
==public class MyClass
=={
====public string MyString;
====public MyClass ()
===={
====}
==}
}

So obviously neither tab-width nor indent-tabs-mode are obeyed nor
respected!

Please advise,

TIA,


Guido

--
If I had only known, I would have been a locksmith.
-- Albert Einstein

http://vanhoecke.org ... and go2 places!

Óscar Fuentes

unread,
Jan 8, 2012, 9:58:43 AM1/8/12
to Guido Van Hoecke, help-gn...@gnu.org
Hello Guido.

I have no time right now to delve into this problem, but this may be
interesting:

C-h f csharp-mode

<snipped documentation...>
(setq indent-tabs-mode nil) ;; tabs are evil
<...snipped documentation>

I don't know if this has something to do with a limitation or known bug
on csharp-mode or it simply is a personal preference of the author (like
some other he hard-coded into csharp-mode.el), but it would good to
contact him.

OTOH, a quick look at the begining of your message points to a
difference on how we tested the issue: in my case I activated
indent-tabs-mode after activating csharp-mode, while on yours it was the
reverse. Sorry if you tried both ways.

Guido Van Hoecke

unread,
Jan 8, 2012, 10:49:45 AM1/8/12
to Óscar Fuentes, help-gn...@gnu.org
Hi Óscar,

On Sun, Jan 8, 2012 at 15:58, Óscar Fuentes <o...@wanadoo.es> wrote:
> <snipped documentation...>
>    (setq indent-tabs-mode nil) ;; tabs are evil
> <...snipped documentation>
>
> I don't know if this has something to do with a limitation or known bug
> on csharp-mode or it simply is a personal preference of the author (like
> some other he hard-coded into csharp-mode.el), but it would good to
> contact him.

I normally do not use tabs in my coding. But I am collaborating here in
C# projects where the technical lead dictates tab for indenting (and
sets their width to 4).

> OTOH, a quick look at the begining of your message points to a
> difference on how we tested the issue: in my case I activated
> indent-tabs-mode after activating csharp-mode, while on yours it was the
> reverse. Sorry if you tried both ways.

Well I tried it now and I'm sorry that it does not make any difference.


Guido

--
Piping down the valleys wild,
Piping songs of pleasant glee,
On a cloud I saw a child,
And he laughing said to me:
"Pipe a song about a Lamb!"
So I piped with merry cheer.
"Piper, pipe that song again;"
So I piped: he wept to hear.
-- William Blake, "Songs of Innocence"

PJ Weisberg

unread,
Jan 8, 2012, 12:29:52 PM1/8/12
to Guido Van Hoecke, Óscar Fuentes, help-gn...@gnu.org
On Sunday, January 8, 2012, Guido Van Hoecke <gui...@gmail.com> wrote:
> I can't help but feeling that this is in error!
>
> The tab-width is 8,
> indent-tabs-mode is t
> and still, it uses spaces to indent, and only two of them.

I know nothing about csharp-mode, but you have a misperception here.  If Emacs feels that the proper place for that line to start is column 2, and the tab width is 8, then spaces *must* be used, because no combination of tabs can get you to column 2.  I know some modes have knobs you can adjust to get different indentation styles, but I don't know what's available in csharp-mode.

Given what Oscar pointed out, I wouldn't be surprised if the indentation function contains some implicit assumption that will result in spaces always being used, anyway.

--

-PJ

Óscar Fuentes

unread,
Jan 8, 2012, 2:06:42 PM1/8/12
to Guido Van Hoecke, help-gn...@gnu.org
Guido Van Hoecke <gui...@gmail.com> writes:

[snip]

> namespace Application
> {
> ==public class MyClass
> =={
> ====public string MyString;
> ====public MyClass ()
> ===={
> ====}
> ==}
> }
>
> So obviously neither tab-width nor indent-tabs-mode are obeyed nor
> respected!

All your examples here makes me think that csharp-mode is indenting by
two spaces per level. As PJ says, tabs will not be used unless the line
is preceded by `tab-width' spaces (8 in your case). If you set tab-width
to 4, I expect to see tabs used for the most deeply indented lines on
the example quoted above.

Put this in your .emacs and see if it makes a difference:

(defun my-csharp-mode-fn ()
(c-set-style "C#")
(setq tab-width 4)
(setq indent-tabs-mode t))

(add-hook 'csharp-mode-hook 'my-csharp-mode-fn)

Guido Van Hoecke

unread,
Jan 8, 2012, 2:19:42 PM1/8/12
to Óscar Fuentes, help-gn...@gnu.org, Dino Chiesa
It makes no difference.


Guido

--
I prefer the most unjust peace to the most righteous war.
-- Cicero

Even peace may be purchased at too high a price.
-- Poor Richard

Alan Mackenzie

unread,
Jan 9, 2012, 4:46:45 AM1/9/12
to
Guido Van Hoecke <gui...@gmail.com> wrote:
> Hi,

> Objective: use only tabs for indenting c# files

[ .... ]
You have told it to use a tab instead of 8 spaces. You haven't told
it how far to indent, though.

[ .... ]

> And finally"

> M-<
> C-SPC
> M->
> <tab>

> produces following result:


> namespace Application
> {
> ==public class MyClass
> =={
> ====public string MyString;
> ====public MyClass ()
> ===={
> ====}
> ==}
> }

> So obviously neither tab-width nor indent-tabs-mode are obeyed nor
> respected!

> Please advise,

C# mode is derived from CC Mode, so read the CC Mode manual. :-)
However, for your specific problem, the variable c-basic-offset needs
to be set to 8. Currently, it looks like it is 2.

> ;;; csharp-mode.el --- C# mode derived mode
>
> ;; Author : Dylan R. E. Moonfire (original)
> ;; Maintainer : Dino Chiesa <dpch...@hotmail.com>
> ;; Created : Feburary 2005
> ;; Modified : May 2011
> ;; Version : 0.8.6
> ;; Keywords : c# languages oop mode
> ;; X-URL : http://code.google.com/p/csharpmode/
> ;; Last-saved : <2011-May-21 20:28:30>

Perhaps you could contact Dino Chiesa and ask him to include "CC Mode"
(with that capitalisation, please) somewhere in that information.

> TIA,


> Guido

--
Alan Mackenzie (Nuremberg, Germany).

Uday Reddy

unread,
Jan 14, 2012, 1:32:30 PM1/14/12
to Guido Van Hoecke, help-gn...@gnu.org
On 1/8/2012 1:01 PM, Guido Van Hoecke wrote:

>
> I can't help but feeling that this is in error!
>
> The tab-width is 8,
> indent-tabs-mode is t
> and still, it uses spaces to indent, and only two of them.

Here is a nice article that clarifies the mystery of tabs.

http://www.jwz.org/doc/tabs-vs-spaces.html

Cheers,
Uday

0 new messages