Ticket #404 (closed enhancement: fixed)

Opened 6 months ago

Last modified 7 weeks ago

Automatically determine tab size if it's not given by a modeline variable

Reported by: rob Owned by: rob
Priority: minor Milestone: 0.7.5
Component: major mode Keywords:
Cc:

Description

If a file doesn't have the tab size set through a modeline variable, it would be nice to automatically determine the tab size. SciTE has an algorithm to do it in C++:

void SciTEBase::DiscoverIndentSetting() {
        int lengthDoc = LengthDocument();
        WindowAccessor acc(wEditor.GetID(), props);
        bool newline = true;
        int indent = 0; // current line indentation
        int tabSizes[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // number of lines with corresponding indentation (index 0 - tab)
        int prevIndent = 0; // previous line indentation
        int prevTabSize = -1; // previous line tab size
        for (int i = 0; i < lengthDoc; i++) {
                char ch = acc[i];
                if (ch == '\r' || ch == '\n') {
                        indent = 0;
                        newline = true;
                } else if (newline && ch == ' ') {
                        indent++;
                } else if (newline) {
                        if (indent) {
                                if (indent == prevIndent && prevTabSize != -1) {
                                        tabSizes[prevTabSize]++;
                                } else if (indent > prevIndent && prevIndent != -1) {
                                        if (indent - prevIndent <= 8) {
                                                prevTabSize = indent - prevIndent;
                                                tabSizes[prevTabSize]++;
                                        } else {
                                                prevTabSize = -1;
                                        }
                                }
                                prevIndent = indent;
                        } else if (ch == '\t') {
                                tabSizes[0]++;
                                prevIndent = -1;
                        } else {
                                prevIndent = 0;
                        }
                        newline = false;
                }
        }
        // maximum non-zero indent
        int topTabSize = -1;
        for (int j = 0; j <= 8; j++) {
                if (tabSizes[j] && (topTabSize == -1 || tabSizes[j] > tabSizes[topTabSize])) {
                        topTabSize = j;
                }
        }
        // set indentation
        if (topTabSize == 0) {
                SendEditor(SCI_SETUSETABS, 1);
        } else if (topTabSize != -1) {
                SendEditor(SCI_SETUSETABS, 0);
                SendEditor(SCI_SETINDENT, topTabSize);
        }
}

but maybe someone already has coded something similar in python?

Attachments

Change History

Changed 6 months ago by rob

Here's an elisp version of the same idea

Changed 2 months ago by rob

  • milestone changed from 0.7.4 to 0.7.5

Changed 7 weeks ago by rob

  • status changed from new to closed
  • resolution set to fixed

(In [1497]) Fixed #404: added GuessIndentSize? action that uses the SciTE algorithm to determine tab size

Add/Change #404 (Automatically determine tab size if it's not given by a modeline variable)

Author



Action
as closed
Next status will be 'reopened'
 
Note: See TracTickets for help on using tickets.