far2l icon indicating copy to clipboard operation
far2l copied to clipboard

[feature request] expand tabs option based on path

Open unxed opened this issue 3 years ago • 2 comments

putty's sources use spaces. far2l uses tabs. Continuously switching between modes produces mixed files, which is mess. It would be great to have an option to configure "expand tabs" editor option differently for different paths.

unxed avatar Jan 30 '22 09:01 unxed

or may be even autodetect..

elfmz avatar Jan 30 '22 18:01 elfmz

default editor's charset by path is also appreciated

unxed avatar Feb 19 '22 12:02 unxed

Please implement if possible. Still hurts.

unxed avatar Mar 03 '23 14:03 unxed

See branch edit-live-expandtab There was imlemented changing tabs/spaces by F5 in editor and saving tabs expansion per-file in history. Planned improvement is Shift+F5 to allow per-file spaces count modification on the fly and may be increasing history elements count from 512 to 4096 or so.. Later will cause old histories to be lost BTW.

elfmz avatar Mar 05 '23 10:03 elfmz

Great! Will look within a half of an hour. Maybe it is possible to detect indentation style for the first time opened files also?

unxed avatar Mar 05 '23 10:03 unxed

Looks working!

unxed avatar Mar 05 '23 11:03 unxed

With this simple addition I've got just that I wanted :)

diff --git a/far2l/src/fileedit.cpp b/far2l/src/fileedit.cpp
index 83373f59..c7d21392 100644
--- a/far2l/src/fileedit.cpp
+++ b/far2l/src/fileedit.cpp
@@ -1530,6 +1530,8 @@ int FileEditor::LoadFile(const wchar_t *Name,int &UserBreak)
 	EditFile.GetSize(FileSize);
 	DWORD StartTime=WINPORT(GetTickCount)();
 
+	int ConvertTabsMode = m_editor->GetConvertTabs();
+
 	while ((GetCode=GetStr.GetString(&Str, m_codepage, StrLength)))
 	{
 		if (GetCode == -1)
@@ -1589,6 +1591,14 @@ int FileEditor::LoadFile(const wchar_t *Name,int &UserBreak)
 			LastLineCR=1;
 		}
 
+		// detect indentation mode by the last string of file
+		// what starts with tab or two spaces
+		if (Str[0] == '\t') {
+			ConvertTabsMode = EXPAND_NOTABS;
+		} else if ((Str[0] == ' ') && (Str[1] == ' ')) {
+			ConvertTabsMode = EXPAND_NEWTABS;
+		}
+
 		if (!m_editor->InsertString(Str, StrLength))
 		{
 			EditFile.Close();
@@ -1596,6 +1606,33 @@ int FileEditor::LoadFile(const wchar_t *Name,int &UserBreak)
 		}
 	}
 
+	// tab mode auto detection
+	if (m_editor->GetConvertTabs() != EXPAND_ALLTABS) {
+		m_editor->SetConvertTabs(ConvertTabsMode);
+	}
+
+	// trying to apply .editorconfig setting
+	// sudo apt-get install editorconfig
+	wchar_t command[4137];
+	std::wstring NameStr = Name;
+	QuoteCmdArg(NameStr);
+	swprintf(command, 4136, L"editorconfig %ls | grep indent_style=space", NameStr.c_str());
+	if (!system(Wide2MB(command).c_str())) {
+		m_editor->SetConvertTabs(EXPAND_NEWTABS);
+	}
+	swprintf(command, 4136, L"editorconfig %ls | grep indent_style=tab", NameStr.c_str());
+	if (!system(Wide2MB(command).c_str())) {
+		m_editor->SetConvertTabs(EXPAND_NOTABS);
+	}
+	swprintf(command, 4136, L"editorconfig %ls | grep tab_width=2", NameStr.c_str());
+	if (!system(Wide2MB(command).c_str())) {
+	    m_editor->SetTabSize(2);
+	}
+	swprintf(command, 4136, L"editorconfig %ls | grep tab_width=4", NameStr.c_str());
+	if (!system(Wide2MB(command).c_str())) {
+	    m_editor->SetTabSize(4);
+	}
+
 	BadConversion = !GetStr.IsConversionValid();
 	if (BadConversion)
 	{

unxed avatar Mar 05 '23 12:03 unxed

See branch edit-live-expandtab There was imlemented changing tabs/spaces by F5 in editor and saving tabs expansion per-file in history. Planned improvement is Shift+F5 to allow per-file spaces count modification on the fly and may be increasing history elements count from 512 to 4096 or so.. Later will cause old histories to be lost BTW.

Extra fine! Very useful features! But it required clear simple info about current mode. In top status line in Editor some info will be useful:

  • number of current tab spaces
  • TAB as \t or expand to spaces
  • sign about on/off tab autodetection

For example may be it write as: AT8 - autodetect tab as \t 8 symbols S4 - manually tab as 4 spaces etc

akruphi avatar Mar 05 '23 12:03 akruphi

May be useful also add in Editor switch to show spaces, tabs and new lines as special symbols (may be more gray than standard text color). For example: each space as · U+00B7, tab as U+21E5 or U+21B9 , new lines as U+21B5 , carriage return as ??? (not now idea).

akruphi avatar Mar 05 '23 13:03 akruphi

There is already option Show white space in F9/Options/Editor settings

elfmz avatar Mar 05 '23 15:03 elfmz

There is already option Show white space in F9/Options/Editor settings

Ooops! Fine! Please add keyboard shortcut to on/off show white space in Editor on-fly. May be by F3 or F9, which now free.

akruphi avatar Mar 05 '23 18:03 akruphi

Whitespaces toggling added on Ctrl+F5. This however not saved per file, should it be? IMHO not, but can reconsider..

elfmz avatar Mar 05 '23 20:03 elfmz

Merged to master, regarding autodetection - may be will do it as an extra option, i just afraid that it will be too much unpredictable

elfmz avatar Mar 05 '23 20:03 elfmz

Merged to master, regarding autodetection - may be will do it as an extra option, i just afraid that it will be too much unpredictable

Let user decide: it may be enabled as fourth "expand tabs" option: "expand new tabs if no tabs present in file".

unxed avatar Mar 05 '23 20:03 unxed

There are several examples of tab detection, may be it worth looking at them to select best logic: https://unix.stackexchange.com/a/478448

unxed avatar Mar 05 '23 20:03 unxed

Its still heuristics, no matter how its done. Also, i think if implementing this then need also check 'sibling' files in same in case new file being created or in case algorithm assumed unreliable decision.

elfmz avatar Mar 05 '23 20:03 elfmz

check 'sibling' files

Sounds good

unxed avatar Mar 05 '23 20:03 unxed

When you press F5 in tab mode, all tabs in the file are replaced with spaces. I do not think it's a good idea. It would be better to switch to tab mode 2, rather than tab mode 1, so that the switch only affects newly entered characters, not existing ones.

unxed avatar Mar 05 '23 21:03 unxed

may be its better to save tabs state only if it was modified explicitly (by F5)? and keep default if it wasnt?

elfmz avatar Mar 05 '23 23:03 elfmz

may be its better to save tabs state only if it was modified explicitly (by F5)? and keep default if it wasnt?

agree

unxed avatar Mar 05 '23 23:03 unxed

Whitespaces toggling added on Ctrl+F5. This however not saved per file, should it be? IMHO not, but can reconsider..

Thank you. Very useful features, which usually need in moment to verify real formatting (now in far2l show contrast bright white-spaces with colors equal to text not comfort during regular editing). I think it not need to save individually per file.

For me toggle white-space more often action then change tab spaces and may be if it possible switch toggling to 1-key (may be from Clrt+F5 to F3), but here I not sure.

akruphi avatar Mar 06 '23 06:03 akruphi

first i suspect that other still free keys can be used by plugins or by some future useful functionality so i don't like reserving all F-s right now, second all that whitespaces/tabs-to-space convertions are similar and so looks better to be on same F with different mods...

elfmz avatar Mar 06 '23 08:03 elfmz

first i suspect that other still free keys can be used by plugins or by some future useful functionality so i don't like reserving all F-s right now, second all that whitespaces/tabs-to-space convertions are similar and so looks better to be on same F with different mods...

I agree. Thank you for efforts & fine far2l. May be change F5 to toggle white-space and Ctrl+F5 to toggle space/tab mode? What opinion @unxed about what action more often change tabs or show white-space?

akruphi avatar Mar 06 '23 10:03 akruphi

No opinion. Should use new features for a while before making conclusions. Also, my UX is specific: I use tabs auto detection patch, so do not need to switch tab mode manually often. With all it's simplicity, for all source code files I've tested it with it gave me no failed detections. It uses the last line of the file what starts with a tab character or two spaces to determine the type of indentation. There is sometimes mixed formatting at the beginning of source code files due to different headers and comments, using end of file for auto detection works better because the end of any source code file usually looks something like this:

    }
}

unxed avatar Mar 06 '23 12:03 unxed

As for determining the tab mode for new files, I could not come up with a sufficiently reliable algorithm. What if it's the first file in the folder? What to do with files with different extensions, like .c and .h? Probably the only reliable solution would be to use per-folder configuration files .far2l with contents like

[Editor]
ExpandTabs=2

If no such file is found in the current folder, far2l should look in parent folders through directory tree up to the root folder. If still no such file found, auto detection should be used, and for the new files global editor setting should be used. Both features, per-folder configuration and tab mode detection should be implemented as optional, being enabled in global configuration like:

[x] Enable per-folder .far2l config files support // enabled by default
[ ] Enable tab mode auto detection // disabled by default

With both whose checkboxes disabled far2l should behave just like it behaves now.

Also auto detection may be improved by asking user explicitly during opening file: "Tab indentation detected in file, but expanding tabs to spaces is enabled. Do you want to switch expanding tabs to spaces off for this file to avoid mixed indentation?"

unxed avatar Mar 06 '23 13:03 unxed

maybe this? https://editorconfig.org/

elfmz avatar Mar 06 '23 15:03 elfmz

https://editorconfig.org/

Looks good!

unxed avatar Mar 06 '23 15:03 unxed

Wow, libeditorconfig is even available in Ubuntu/Fedora/brew repos!

~$ sudo apt-get install editorconfig
Чтение списков пакетов… Готово
Построение дерева зависимостей… Готово
Чтение информации о состоянии… Готово
Будут установлены следующие дополнительные пакеты:
  libeditorconfig0
Следующие НОВЫЕ пакеты будут установлены:
  editorconfig libeditorconfig0

unxed avatar Mar 06 '23 21:03 unxed

format doesnt look as something much more than usual ini file with few simple extensions so better to avoid extra dependency

elfmz avatar Mar 06 '23 21:03 elfmz

It turns out that several projects I work with already use this format. It's great if far2l starts supporting it out of the box!

unxed avatar Mar 06 '23 21:03 unxed