Add selection support to TAB and Space conversion commands

Fix #12720, fix #12745, close #12739
This commit is contained in:
ArkadiuszMichalski 2022-12-31 11:57:40 +01:00 committed by Don Ho
parent 6c3659fe73
commit 1652236f32

View File

@ -1280,220 +1280,288 @@ bool Notepad_plus::replaceInOpenedFiles()
return true; return true;
} }
void Notepad_plus::wsTabConvert(spaceTab whichWay) void Notepad_plus::wsTabConvert(spaceTab whichWay)
{ {
// block selection is not supported
if ((_pEditView->execute(SCI_GETSELECTIONMODE) == SC_SEL_RECTANGLE) || (_pEditView->execute(SCI_GETSELECTIONMODE) == SC_SEL_THIN))
return;
intptr_t tabWidth = _pEditView->execute(SCI_GETTABWIDTH); intptr_t tabWidth = _pEditView->execute(SCI_GETTABWIDTH);
intptr_t currentPos = _pEditView->execute(SCI_GETCURRENTPOS); intptr_t currentPos = _pEditView->execute(SCI_GETCURRENTPOS);
intptr_t lastLine = _pEditView->lastZeroBasedLineNumber(); intptr_t currentLine = _pEditView->execute(SCI_LINEFROMPOSITION, currentPos);
intptr_t docLength = _pEditView->execute(SCI_GETLENGTH) + 1; intptr_t currentPosInLine = currentPos - _pEditView->execute(SCI_POSITIONFROMLINE, currentLine);
if (docLength < 2)
return;
intptr_t count = 0; intptr_t startLine = 0;
intptr_t column = 0; intptr_t endLine = _pEditView->lastZeroBasedLineNumber();
intptr_t newCurrentPos = 0; intptr_t endLineCorrect = endLine;
intptr_t tabStop = tabWidth - 1; // remember, counting from zero ! intptr_t dataLength = _pEditView->execute(SCI_GETLENGTH) + 1;
bool onlyLeading = false; intptr_t mainSelAnchor = _pEditView->execute(SCI_GETANCHOR);
vector<int> bookmarks; bool isEntireDoc = (mainSelAnchor == currentPos);
vector<int> folding;
for (int i=0; i<lastLine; ++i) // restore original selection if nothing has changed
{ auto restoreSelection = [this, mainSelAnchor, currentPos, isEntireDoc]()
if (bookmarkPresent(i)) {
bookmarks.push_back(i); if (!isEntireDoc)
{
_pEditView->execute(SCI_SETANCHOR, mainSelAnchor);
_pEditView->execute(SCI_SETCURRENTPOS, currentPos);
}
};
if ((_pEditView->execute(SCI_GETFOLDLEVEL, i) & SC_FOLDLEVELHEADERFLAG)) // auto-expand of partially selected lines
if (_pEditView->execute(SCI_GETFOLDEXPANDED, i) == 0) if (!isEntireDoc)
folding.push_back(i); {
} intptr_t startPos = _pEditView->execute(SCI_GETSELECTIONSTART);
startLine = _pEditView->execute(SCI_LINEFROMPOSITION, startPos);
intptr_t endPos = _pEditView->execute(SCI_GETSELECTIONEND);
endLine = endLineCorrect = _pEditView->execute(SCI_LINEFROMPOSITION, endPos);
char * source = new char[docLength]; if (startPos != _pEditView->execute(SCI_POSITIONFROMLINE, startLine))
if (source == NULL) startPos = _pEditView->execute(SCI_POSITIONFROMLINE, startLine);
return;
_pEditView->execute(SCI_GETTEXT, docLength, reinterpret_cast<LPARAM>(source));
if (whichWay == tab2Space) if (endPos == _pEditView->execute(SCI_POSITIONFROMLINE, endLine))
{ endLineCorrect = endLine - 1;
// count how many tabs are there else if (endPos < _pEditView->execute(SCI_GETLINEENDPOSITION, endLine))
for (const char * ch=source; *ch; ++ch) endPos = _pEditView->execute(SCI_GETLINEENDPOSITION, endLine);
{
if (*ch == '\t')
++count;
}
if (count == 0)
{
delete [] source;
return;
}
}
// allocate tabwidth-1 chars extra per tab, just to be safe
size_t newlen = docLength + count * (tabWidth - 1) + 1;
char * destination = new char[newlen];
if (destination == NULL)
{
delete [] source;
return;
}
char * dest = destination;
switch (whichWay) dataLength = endPos - startPos + 1;
{ _pEditView->execute(SCI_SETSEL, startPos, endPos);
case tab2Space: }
{
// rip through each line of the file
for (int i = 0; source[i] != '\0'; ++i)
{
if (source[i] == '\t')
{
intptr_t insertTabs = tabWidth - (column % tabWidth);
for (int j = 0; j < insertTabs; ++j)
{
*dest++ = ' ';
if (i <= currentPos)
++newCurrentPos;
}
column += insertTabs;
}
else
{
*dest++ = source[i];
if (i <= currentPos)
++newCurrentPos;
if ((source[i] == '\n') || (source[i] == '\r'))
column = 0;
else if ((source[i] & 0xC0) != 0x80) // UTF_8 support: count only bytes that don't start with 10......
++column;
}
}
*dest = '\0';
break;
}
case space2TabLeading:
{
onlyLeading = true;
}
case space2TabAll:
{
bool nextChar = false;
int counter = 0;
bool nonSpaceFound = false;
for (int i=0; source[i] != '\0'; ++i)
{
if (nonSpaceFound == false)
{
while (source[i + counter] == ' ')
{
if ((column + counter) == tabStop)
{
tabStop += tabWidth;
if (counter >= 1) // counter is counted from 0, so counter >= max-1
{
*dest++ = '\t';
i += counter;
column += counter + 1;
counter = 0;
nextChar = true;
if (i <= currentPos)
++newCurrentPos;
break;
}
else if (source[i+1] == ' ' || source[i+1] == '\t') // if followed by space or TAB, convert even a single space to TAB
{
*dest++ = '\t';
i++;
column += 1;
counter = 0;
if (i <= currentPos)
++newCurrentPos;
}
else // single space, don't convert it to TAB
{
*dest++ = source[i];
column += 1;
counter = 0;
nextChar = true;
if (i <= currentPos)
++newCurrentPos;
break;
}
}
else
++counter;
}
if (nextChar == true) if (dataLength < 2)
{ {
nextChar = false; restoreSelection();
continue; return;
} }
if (source[i] == ' ' && source[i + counter] == '\t') // spaces "absorbed" by a TAB on the right intptr_t changeDataCount = 0;
{ intptr_t newCurrentPos = 0;
*dest++ = '\t'; vector<intptr_t> folding;
i += counter;
column = tabStop + 1;
tabStop += tabWidth;
counter = 0;
if (i <= currentPos)
++newCurrentPos;
continue;
}
}
if (onlyLeading == true && nonSpaceFound == false) _pEditView->execute(SCI_BEGINUNDOACTION);
nonSpaceFound = true;
if (source[i] == '\n' || source[i] == '\r') for (intptr_t idx = startLine; idx < endLineCorrect + 1; ++idx)
{ {
*dest++ = source[i]; intptr_t startPos = _pEditView->execute(SCI_POSITIONFROMLINE, idx);
column = 0; intptr_t endPos = _pEditView->execute(SCI_GETLINEENDPOSITION, idx);
tabStop = tabWidth - 1; dataLength = endPos - startPos + 1;
nonSpaceFound = false;
}
else if (source[i] == '\t')
{
*dest++ = source[i];
column = tabStop + 1;
tabStop += tabWidth;
counter = 0;
}
else
{
*dest++ = source[i];
counter = 0;
if ((source[i] & 0xC0) != 0x80) // UTF_8 support: count only bytes that don't start with 10......
{
++column;
if (column > 0 && column % tabWidth == 0) char * source = new char[dataLength];
tabStop += tabWidth; if (source == NULL)
} continue;
}
if (i <= currentPos) source[dataLength - 1] = '\0'; // make sure to have correct data termination
++newCurrentPos; _pEditView->execute(SCI_SETTARGETRANGE, startPos, endPos);
} _pEditView->execute(SCI_GETTARGETTEXT, 0, reinterpret_cast<LPARAM>(source));
*dest = '\0';
break;
}
}
_pEditView->execute(SCI_BEGINUNDOACTION); intptr_t count = 0;
_pEditView->execute(SCI_SETTEXT, 0, reinterpret_cast<LPARAM>(destination)); intptr_t column = 0;
_pEditView->execute(SCI_GOTOPOS, newCurrentPos); intptr_t tabStop = tabWidth - 1; // remember, counting from zero !
bool onlyLeading = false;
for (size_t i=0; i<bookmarks.size(); ++i) if (whichWay == tab2Space)
_pEditView->execute(SCI_MARKERADD, bookmarks[i], MARK_BOOKMARK); {
// count how many tabs are there
for (const char * ch = source; *ch; ++ch)
{
if (*ch == '\t')
++count;
}
if (count == 0)
{
delete [] source;
continue;
}
}
// allocate tabwidth-1 chars extra per tab, just to be safe
size_t newLen = dataLength + count * (tabWidth - 1) + 1;
char * destination = new char[newLen];
if (destination == NULL)
{
delete [] source;
continue;
}
char * dest = destination;
intptr_t changeDataLineCount = 0;
for (size_t i=0; i<folding.size(); ++i) switch (whichWay)
_pEditView->fold(folding[i], false); {
case tab2Space:
{
// rip through each line of the file
for (int i = 0; source[i] != '\0'; ++i)
{
if (source[i] == '\t')
{
intptr_t insertTabs = tabWidth - (column % tabWidth);
for (int j = 0; j < insertTabs; ++j)
{
*dest++ = ' ';
changeDataCount++;
changeDataLineCount++;
if (idx == currentLine && i < currentPosInLine)
++newCurrentPos;
}
column += insertTabs;
}
else
{
*dest++ = source[i];
if (idx == currentLine && i < currentPosInLine)
++newCurrentPos;
if ((source[i] == '\n') || (source[i] == '\r'))
column = 0;
else if ((source[i] & 0xC0) != 0x80) // UTF_8 support: count only bytes that don't start with 10......
++column;
}
}
*dest = '\0';
break;
}
case space2TabLeading:
{
onlyLeading = true;
}
case space2TabAll:
{
bool nextChar = false;
int counter = 0;
bool nonSpaceFound = false;
for (int i = 0; source[i] != '\0'; ++i)
{
if (nonSpaceFound == false)
{
while (source[i + counter] == ' ')
{
if ((column + counter) == tabStop)
{
tabStop += tabWidth;
if (counter >= 1) // counter is counted from 0, so counter >= max-1
{
*dest++ = '\t';
changeDataCount++;
changeDataLineCount++;
i += counter;
column += counter + 1;
counter = 0;
nextChar = true;
if (idx == currentLine && i <= currentPosInLine)
++newCurrentPos;
break;
}
else if (source[i + 1] == ' ' || source[i + 1] == '\t') // if followed by space or TAB, convert even a single space to TAB
{
*dest++ = '\t';
changeDataCount++;
changeDataLineCount++;
i++;
column += 1;
counter = 0;
if (idx == currentLine && i <= currentPosInLine)
++newCurrentPos;
}
else // single space, don't convert it to TAB
{
*dest++ = source[i];
column += 1;
counter = 0;
nextChar = true;
if (idx == currentLine && i <= currentPosInLine)
++newCurrentPos;
break;
}
}
else
++counter;
}
_pEditView->execute(SCI_ENDUNDOACTION); if (nextChar == true)
{
nextChar = false;
continue;
}
if (source[i] == ' ' && source[i + counter] == '\t') // spaces "absorbed" by a TAB on the right
{
*dest++ = '\t';
changeDataCount++;
changeDataLineCount++;
i += counter;
column = tabStop + 1;
tabStop += tabWidth;
counter = 0;
if (idx == currentLine && i <= currentPosInLine)
++newCurrentPos;
continue;
}
}
if (onlyLeading == true && nonSpaceFound == false)
nonSpaceFound = true;
if (source[i] == '\n' || source[i] == '\r')
{
*dest++ = source[i];
column = 0;
tabStop = tabWidth - 1;
nonSpaceFound = false;
}
else if (source[i] == '\t')
{
*dest++ = source[i];
column = tabStop + 1;
tabStop += tabWidth;
counter = 0;
}
else
{
*dest++ = source[i];
counter = 0;
if ((source[i] & 0xC0) != 0x80) // UTF_8 support: count only bytes that don't start with 10......
{
++column;
if (column > 0 && column % tabWidth == 0)
tabStop += tabWidth;
}
}
if (idx == currentLine && i < currentPosInLine)
++newCurrentPos;
}
*dest = '\0';
break;
}
}
if ((_pEditView->execute(SCI_GETFOLDLEVEL, idx) & SC_FOLDLEVELHEADERFLAG))
if (_pEditView->execute(SCI_GETFOLDEXPANDED, idx) == 0)
folding.push_back(idx);
if (changeDataLineCount)
_pEditView->execute(SCI_REPLACETARGET, static_cast<WPARAM>(-1), reinterpret_cast<LPARAM>(destination));
// clean up
delete [] source;
delete [] destination;
}
_pEditView->execute(SCI_ENDUNDOACTION);
if (changeDataCount)
{
if (!isEntireDoc)
_pEditView->execute(SCI_SETSEL, _pEditView->execute(SCI_POSITIONFROMLINE, startLine), endLineCorrect != endLine ? _pEditView->execute(SCI_POSITIONFROMLINE, endLine) : _pEditView->execute(SCI_GETLINEENDPOSITION, endLine));
else
_pEditView->execute(SCI_GOTOPOS, _pEditView->execute(SCI_POSITIONFROMLINE, currentLine) + newCurrentPos);
for (size_t i = 0; i < folding.size(); ++i)
_pEditView->fold(folding[i], false);
}
else
restoreSelection();
// clean up
delete [] source;
delete [] destination;
} }
void Notepad_plus::doTrim(trimOp whichPart) void Notepad_plus::doTrim(trimOp whichPart)