bugGNU nano - Bugs: bug #57796, [Request] smarter...

 
 

You are not allowed to post comments on this tracker with your current authentication level.

bug #57796: [Request] smarter whitespace/indentation handling

Submitter:  Saagar Jha <saagarjha>
Submitted:  Wed 12 Feb 2020 12:49:20 AM UTC
   
 
Severity:  1 - Wish Status:  Wont Fix
Assigned to:  None Open/Closed:  Closed

Jump to the original submission

Mon 03 May 2021 10:26:03 AM UTC, comment #14: 

Nano detecting the current indentation style (tabs or spaces) and unsetting or setting 'tabstospaces' accordingly will not do the right thing when the file used mixed indentation (a tab for a double indent, four spaces for a single indent).  So 'detectindent' is for me a "wontdo".

But... the 'guessindent' from your original comment #0 might be worth looking at.  It looks easy to implement and would always do the right thing as long as the file itself uses either only tabs or only spaces for indentation.  If you want to post a patch for 'guessindent', please open a new issue.

Benno Schulenberg <bens>
Group administrator
Mon 02 Mar 2020 12:21:07 AM UTC, comment #13: 

I do think that the cursor information shown in the status bar isn't the best; maybe I should file a suggestion for that too. I'll work on updating the patch with the new things you brought up.

Saagar Jha <saagarjha>
Tue 25 Feb 2020 02:13:59 PM UTC, comment #12: 

Showing extra information for --constantshow is out of the question: it already shows too much information, so much that it is useless.  (What I might be open to is a new option to show an entirely different status bar or title bar, that gives a very concise position indication, something like in vim or dte, but that is an entirely different issue, not to be discussed here.)

If detectindent cannot do without a manual override, then it is not acceptable.  And adding an explicit fallback is... overboard.  The only fallback would be a plain tab (or whatever 'tabgives' specified).  Don't add complications.  Keep it simple.

Messages about which indentation is detected during startup are not okay.  But warning when a file clearly has mixed indentation ... is not nice, but is necessary when the user specified 'detectindent', because nano cannot handle that.

Benno Schulenberg <bens>
Group administrator
Tue 25 Feb 2020 11:06:40 AM UTC, comment #11: 


> What other applications or editors are there that do automatic indent detection? Vim doesn't do it (not by default, at least), Geany doesn't do it, Emacs... seems to do it, but it's mostly its auto-indent that's being too clever -- plus, by moving around I can get it to insert spaces when the file is all tab-indented.


The newer ones, e.g. Sublime Text, Visual Studio Code. Vim supports plugins so of course there's more than one of these. Geany has this now, too: https://www.geany.org/manual/current#detecting-indent-type

Most of these editors also persistently show indentation information in the status bar; would you be open to tweaking what information is shown with "set constantshow" to include this? Most of them also have a way of changing the indentation dynamically to deal with the cases where the editor doesn't get it quite right.

> In theory, 'detectindent' would have been better than 'tabgives', but the latter works also for new files, the former not.


Ideally we would have detectindent decay to tabgives-like behavior when it can't figure out what to do (e.g. an empty file, or confusing indentation). Would something like 'set detectindent "fallback"' be too complicated?

> There is no need to support mixed indentations, but if we add 'detectindent' to nano, it would be nice when the user gets a warning when such a file is opened.
> If the feature requires a key binding to switch the detection off in case it is wrong, then it is simply a no-no.  The detection must be always correct, and must renounce when it is unsure.


Yes, I agree. Is showing messages in the status bar when a file is opened about indentation-related information OK? Perhaps something like "Detected 4 space indentation" or "Could not determine indent, using fallback".

Saagar Jha <saagarjha>
Mon 24 Feb 2020 06:49:56 PM UTC, comment #10: 

(Another example of mixed tabs and spaces: lib/regcomp.c in gnulib.  The indent step is two spaces, and instead of eight spaces a tab is used. :| )

Benno Schulenberg <bens>
Group administrator
Mon 24 Feb 2020 04:22:55 PM UTC, comment #9: 

What other applications or editors are there that do automatic indent detection?  Vim doesn't do it (not by default, at least), Geany doesn't do it, Emacs... seems to do it, but it's mostly its auto-indent that's being too clever -- plus, by moving around I can get it to insert spaces when the file is all tab-indented.

A syntax-specific 'detectindent' option?  Which then overrides 'tabgives'?  ...  Hrrm.  No.  We already have too many indent-related options.  In theory, 'detectindent' would have been better than 'tabgives', but the latter works also for new files, the former not.

Yes, at the very least 'detectindent' should be fully confident that it got the right indent.  A single-space indent can never be right.  If the file actually uses single-space indents, the user can see that -- they will not mistake a single space for a tab.

There is no need to support mixed indentations, but if we add 'detectindent' to nano, it would be nice when the user gets a warning when such a file is opened.

(These mixed indentations are not so rare, though: recently I've seen another project that uses it.  And... nano itself used it until two years ago.  :)  Until commit 87206c06.  Before that change, reindenting something was a struggle.  After it, I could just select the region and enjoy the ease of <Tab> and <Shift+Tab>.)

If the feature requires a key binding to switch the detection off in case it is wrong, then it is simply a no-no.  The detection must be always correct, and must renounce when it is unsure.

Benno Schulenberg <bens>
Group administrator
Fri 21 Feb 2020 10:04:46 AM UTC, comment #8: 

Appreciate you taking a look :) I revisited how some other applications handle this, and here are some ideas:

> So... it is a nice feature when handling only code files, where the choice is between either tabs or spaces.  But when files can be of many different kinds, it would too often do the wrong thing, so the feature cannot be set in a nanorc file, which makes it much less useful.


Perhaps this should go in the language-specific syntax definitions? So you'd "extendsyntax c detectindent" so this wouldn't activate on plain text files or patches. Another option would be to less confident about the indentation and bail out to the default if the file's indentation seems strange.

> And it breaks down when four-space indents are mixed with single tabs (representing eight spaces), as is the case in some projects (for example: ncurses).


Uh…that's pretty awful :( It might be possible to extend the detection to find this but I'm curious if you think we should try to support them. One that might be worth adding is "tabs to indent, spaces to align" but aside from that IMO it's not really worth trying to support everyone's strange indentation styles. (We'd probably need to have a shortcut to turn off the feature when you have the misfortune of working with strange files like these, of course.)

Saagar Jha <saagarjha>
Thu 20 Feb 2020 06:47:24 PM UTC, comment #7: 

When you first proposed this indent-detection thing, I thought: that is nothing for nano.  But seeing how nano (with your patch) correctly detects a four-space indent in a Python file, and the normal single-tab indent in nano's C files...  Pretty cool.

However, it breaks down when trying to edit a patch file: a Tab becomes a single space.  And it breaks down when four-space indents are mixed with single tabs (representing eight spaces), as is the case in some projects (for example: ncurses).  And when editing nano's NEWS file with this feature, a Tab becomes thirteen spaces.  :|

So... it is a nice feature when handling only code files, where the choice is between either tabs or spaces.  But when files can be of many different kinds, it would too often do the wrong thing, so the feature cannot be set in a nanorc file, which makes it much less useful.

Benno Schulenberg <bens>
Group administrator
Sun 16 Feb 2020 12:01:34 PM UTC, comment #6: 

A rough patch implementing detectindent functionality is attached.

(file #48419)

Saagar Jha <saagarjha>
Fri 14 Feb 2020 11:59:59 PM UTC, comment #5: 

We should probably use a whitespace character for the option, honestly. How about "- " ("dash space") or "-
" ("dash newline"). Just kidding, I'm not really picky about how the option looks, as long as it doesn't end up messing with anything else and you think we can spend an option for it. When I said selected regions, I was talking about showing whitespace characters in marked, highlighted regions: Sublime Text does this for example.

Saagar Jha <saagarjha>
Thu 13 Feb 2020 09:44:27 AM UTC, comment #4: 

Ah!  I thought there only wasn't a command-line option to switch on whitespace display, but apparently there isn't a nanorc option either.  Hmmm...  I've always had the idea of adding a command-line option for this, but we've run out of letters.  (The -S is reserved for changing --softwrap from -$, and -O and -q I want to keep in reserve too.)  The only usable non-letter character seems to be the slash.  How about -/ for turning on whitespace display?

No, there are not multiple selected regions.  You used the words "selected regions" and I thought you meant "certain areas", certain heuristic places where whitespace is especially important.

Benno Schulenberg <bens>
Group administrator
Wed 12 Feb 2020 08:50:26 PM UTC, comment #3: 

comment #2:

> If you have patch for something like 'detectindent' or 'guessindent', I would look at at it.


I'll see if I can write one.

> There is no mechanism to enable certain options only during startup.  You could set 'set whitespacedisplay' in your nanorc, but that would switch it on for all files, and you probably want it only for C files or just a few types of files.


I actually don't mind it being enabled for all files, because I'd get a visual reminder to turn it off if I didn't need it. But in any case, "set whitespacedisplay" in my nanorc doesn't seem to work; perhaps because it's a function and not an option?

> I have no idea for which selected regions nano should show whitespace and for which not, so I will not go there.


Wait, there are multiple selected regions?

Saagar Jha <saagarjha>
Wed 12 Feb 2020 12:33:13 PM UTC, comment #2: 

If you have patch for something like 'detectindent' or 'guessindent', I would look at at it.  But I'm not going to work on this myself.

There is no mechanism to enable certain options only during startup.  You could set 'set whitespacedisplay' in your nanorc, but that would switch it on for all files, and you probably want it only for C files or just a few types of files.

(Personally, I would just get into the habit of quickly toggling M-P twice.  I don't do it every day, but regularly enough -- whenever I have some doubts about the whitespace -- that it has almost become a habit.)

I have no idea for which selected regions nano should show whitespace and for which not, so I will not go there.

As an early-warning system for the problem that trips you up now and then, I would do:

extendsyntax c color ,magenta "^  +"

where there are two spaces between the caret and the plus.  It will make files indented with spaces look ugly, but at least they will not catch you unawares any more.

Benno Schulenberg <bens>
Group administrator
Wed 12 Feb 2020 01:08:35 AM UTC, comment #1: 

Oh, and just to be clear, tabgives (i.e. a language-level setting) is unfortunately not flexible enough for me because I have variation across a single language (e.g. I work with C indented both ways, depending on the project).

Saagar Jha <saagarjha>
Wed 12 Feb 2020 12:49:20 AM UTC, original submission:  

I use nano to write code. While I personally use tabs to indent my code, occasionally I have to work on code written by heathens who use spaces for indentation and unless I realize this in time and enable the correct settings I get a huge mess when I edit these files. So here's a couple of ideas I thought I might spitball:

  • Autodetection of indentation ("detectindent"?) would be nice, but if you think it's too complicated for something like nano something simpler that temporarily switched tabstospaces based on the character to the left of the cursor ("guessindent"?) might work reasonably well.
  • A question that might be a feature request: to see mistakes like this earlier, I'd like to enable whitespacedisplay when opening the file. Is there a way to run functions at startup?
  • Some editors only show whitespace for selected regions, to keep them from being overwhelming by showing up all the time. Perhaps this is something that nano could do too?


They're not completely thought out so I'd be happy to hear any ideas you might have in this area.

Saagar Jha <saagarjha>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attached Files

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by bens (Posted a comment)
  • -email is unavailable- added by saagarjha (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

     

    Follow 5 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2021-05-23 bens Open/ClosedOpen Closed
    2021-05-03 bens StatusNone Wont Fix
    2020-02-16 saagarjha Attached File- Added 0001-Add-an-option-to-detect-indentation.patch, #48419
    2020-02-12 bens Severity3 - Normal 1 - Wish
        Summary[Request] Smarter whitespace/indentation handling [Request] smarter whitespace/indentation handling

    Back to the top

    Powered by Savane 3.13-4448.
    Corresponding source code