Skip to content

Commit

Permalink
Merge pull request #1647 from unxed/fix-tabs
Browse files Browse the repository at this point in the history
fix #1634
  • Loading branch information
elfmz committed Apr 27, 2023
2 parents 29865d7 + 10e0de1 commit 30cdf82
Showing 1 changed file with 48 additions and 22 deletions.
70 changes: 48 additions & 22 deletions far2l/src/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2526,40 +2526,66 @@ int Editor::ProcessKey(int Key)

CurPos = CurLine->GetCurPos();

if (Key < 0x10000 && CurPos > 0 && !Length) {
Edit *PrevLine = CurLine->m_prev;
if (Key < 0x10000 && CurPos > Length) {

while (PrevLine && !PrevLine->GetLength())
// detect space alignment by search for lines starting with space
Edit *PrevLine = CurLine->m_prev;
bool SpaceAligned = false;
while (PrevLine) {
if (PrevLine->GetLength()) {
const wchar_t *PrevStr = L"\x00";
int TmpLength;
PrevLine->GetBinaryString(&PrevStr, nullptr, TmpLength);
if (PrevStr[0] == ' ') {
SpaceAligned = true;
break;
}
if (PrevStr[0] == '\t') {
break;
}
}
PrevLine = PrevLine->m_prev;
}

if (PrevLine) {
int TabPos = CurLine->GetCellCurPos();
CurLine->SetCurPos(0);
const wchar_t *PrevStr = nullptr;
int PrevLength = 0;
PrevLine->GetBinaryString(&PrevStr, nullptr, PrevLength);
// detect if there are any non-space chars in the current line
bool NonSpaceFound = false;
for (int I = 0; I < Length; I++) {
if (!IsSpace(Str[I])) {
NonSpaceFound = true;
break;
}
}

int TabPos = CurLine->GetCellCurPos();
CurLine->SetCurPos(Length);

for (int I = 0; I < PrevLength && IsSpace(PrevStr[I]); I++) {
int NewTabPos = CurLine->GetCellCurPos();
for (int I = Length; I < CurPos; I++) {

if (NewTabPos == TabPos)
break;
int NewTabPos = CurLine->GetCellCurPos();

if (NewTabPos > TabPos) {
CurLine->ProcessKey(KEY_BS);
if (NewTabPos == TabPos)
break;

while (CurLine->GetCellCurPos() < TabPos)
CurLine->ProcessKey(' ');
if (NewTabPos > TabPos) {
CurLine->ProcessKey(KEY_BS);

break;
}
while (CurLine->GetCellCurPos() < TabPos)
CurLine->ProcessKey(' ');

if (NewTabPos < TabPos)
CurLine->ProcessKey(PrevStr[I]);
break;
}

CurLine->SetCellCurPos(TabPos);
if (NewTabPos < TabPos)
CurLine->ProcessKey(
SpaceAligned ||
NonSpaceFound ||
GetConvertTabs() ||
((I + 1 == CurPos) && (TabPos % EdOpt.TabSize))
? ' ' : '\t'
);
}

CurLine->SetCellCurPos(TabPos);
}

if (!SkipCheckUndo) {
Expand Down

0 comments on commit 30cdf82

Please sign in to comment.