New grammar: Tabs settings question


#1

Hi all,

I’m working on a grammar for a language that requires some tabs/spaces setting that I’ve never seen anywhere else. I’m not sure how to set up Atom to follow the language specifications.

This language requires indentation levels of 4 spaces, but as soon as 8 spaces are found (two levels of indentation), a hard tab should be used instead.

Here’s an example:

// First indentation level
....// Second indentation level: 4 spaces
   ->   // Third indentation level: 1 tab with width equal to 8 spaces
   ->   ....// Fourth: 1 hard tab (==8 spaces) + 4 spaces
   ->      ->   // Fifth: 2 hard tabs

I am able to achieve this under KDE’s Kate using this specific modeline:

kate: replace-tabs off; indent-width 4; tab-width 8;

What would be the best way for Atom? Is it possible at all?

I’ve tried the following in my config.cson:

".source.newlanguage":
  editor:
    tabLength: 8
    softTabs: false

But:

  1. The softTab option is ignored completely, spaces are always inserted;
  2. The tabLength affects the indentation created by pressing the tab key, it does not tell Atom to “stretch” tab characters to 8 spaces. This means I cannot reach the above example’s second indentation level as 8 spaces are automatically inserted;
  3. Could these settings go into the language package I’m creating (instead of in the user’s config.cson located in ~/.atom/config.cson)?

Any idea?

Thanks


#2

That is truly weird. May I ask what the language is?


#3

It is indeed really weird. I have never seen such a convention before. Please, if anybody saw this elsewhere, let me know. I’m eager to learn the rationale behind it!

The language is Scientific Vector Language (SVL), created by Chemical Computing Group (my employer) for computational chemistry.

While the language does not require this convention (the code will work regardless of the indentation, like C or Perl) the coding style does.


#4

What I would do is to create a utility to modify the file on save. That way the tab settings can be whatever the user needs or prefers and the file still gets written the way that the compiler or tools require.

You can take a look at my tabs-to-spaces package for possible implementation ideas.


#5

I don’t understand what’s weird about it. It’s very old-school. Must have been common for decades. I really hated the lack of support when I migrated from Emacs to Eclipse for Java programming and I thought Eclipse was just stupid…

Think of it this way: The default tab width on a terminal has been eight characters (columns?) forever. So when you print a line comprising x, then tab, then y; then y ends up in the ninth column. Maybe that number was chosen because it fits nicely into the 80 characters width of punch cards.

Good. So if you want a program to come out right on a terminal, then you’ve got to use a tab width of eight columns, otherwise it won’t look right. If your indentation level is also eight, then no issues, then you can just use a tab for each level of indentation.

But many people prefer an indentation level other than eight. When that happens, you can either use all spaces, or you can use a mix of tabs and spaces based on a tab width of eight columns. Because if you don’t assume a tab width of eight columns, programs such as cat or more won’t show the code correctly.

Today, it seems simpler to use all spaces, but there were times when disk space was scarce so people thought it’s useful to use tab characters instead as a form of compression.

Interestingly, Emacs has a feature to help work with this convention: when you backspace into a tab character, it replaces it with seven spaces, making it look as if you only deleted one space…

Of course, nowadays not that many people use programs like cat or more to look at source code, so they don’t care whether it looks right with these programs. So I think a convention that makes sense is to use only tabs for indentation, and then every programmer can choose their favorite tab width, and thus make the program look their favorite way.

There are Unix programs expand and unexpand (found them on my Mac) that convert between just spaces and a mix of spaces and tabs as described above for the old-school way.

I guess from a practical perspective perhaps the easiest is to replace tabs with spaces on the way from disk to memory, and spaces with tabs on the way from memory to disk. This way, you can set Atom to use just spaces for indentation, with an indentation level of 4.

Or maybe, just maybe, you can convince folks to allow a convention of just spaces… That should be easy to support in many tools.


#6

I would not recommend to start a tabs vs spaces war here. I have slogged through many heated threads over the years and they are never resolved and never will.


#7

Thanks all for your replies!

leedohm: That might be possible, but it would still appear as “spaces only” in the editor…

@kgrossjo: Thanks for the clarification! I now see a (tiny tiny) rationale behind this! :wink: May I ask what was the language(s) you were using that convention? And in what year?
I am doing pressure right now to change the convention to 4 spaces everywhere but that could take some time (if it ever works). Meanwhile I’ll try to adapt the editors I’m using to respect the requires style.

@mark_hahn: Even though 4 spaces are superior ( :wink: ) I don’t want to start such a debate either as I witnessed it too. But, mixing both tabs and spaces was just so… shocking!

I’m not sure with all of this that I’ll be able to set Atom to use that convention. Any other idea?


#8

I think I need to correct myself. I kinda forgot that “weird” and “uncommon” are two different things. I think we can say that the tabs/spaces combo was common once but still weird :blush:


#9

Hm. I’m sure I was using Emacs at that time. So I’m also pretty sure I saw a lot of (Emacs) Lisp. And then I know I was using Perl and Java and a little C.