📄 document.cxx.svn-base
字号:
LinesTotal() - prevLinesTotal, text));
}
enteredModification--;
}
return !cb.IsReadOnly();
}
/**
* Insert a string with a length.
*/
bool Document::InsertString(int position, const char *s, int insertLength) {
if (insertLength <= 0) {
return false;
}
CheckReadOnly();
if (enteredModification != 0) {
return false;
} else {
enteredModification++;
if (!cb.IsReadOnly()) {
NotifyModified(
DocModification(
SC_MOD_BEFOREINSERT | SC_PERFORMED_USER,
position, insertLength,
0, s));
int prevLinesTotal = LinesTotal();
bool startSavePoint = cb.IsSavePoint();
bool startSequence = false;
const char *text = cb.InsertString(position, s, insertLength, startSequence);
if (startSavePoint && cb.IsCollectingUndo())
NotifySavePoint(!startSavePoint);
ModifiedAt(position);
NotifyModified(
DocModification(
SC_MOD_INSERTTEXT | SC_PERFORMED_USER | (startSequence?SC_STARTACTION:0),
position, insertLength,
LinesTotal() - prevLinesTotal, text));
}
enteredModification--;
}
return !cb.IsReadOnly();
}
int Document::Undo() {
int newPos = -1;
CheckReadOnly();
if (enteredModification == 0) {
enteredModification++;
if (!cb.IsReadOnly()) {
bool startSavePoint = cb.IsSavePoint();
bool multiLine = false;
int steps = cb.StartUndo();
//Platform::DebugPrintf("Steps=%d\n", steps);
for (int step = 0; step < steps; step++) {
const int prevLinesTotal = LinesTotal();
const Action &action = cb.GetUndoStep();
if (action.at == removeAction) {
NotifyModified(DocModification(
SC_MOD_BEFOREINSERT | SC_PERFORMED_UNDO, action));
} else {
NotifyModified(DocModification(
SC_MOD_BEFOREDELETE | SC_PERFORMED_UNDO, action));
}
cb.PerformUndoStep();
int cellPosition = action.position;
ModifiedAt(cellPosition);
newPos = cellPosition;
int modFlags = SC_PERFORMED_UNDO;
// With undo, an insertion action becomes a deletion notification
if (action.at == removeAction) {
newPos += action.lenData;
modFlags |= SC_MOD_INSERTTEXT;
} else {
modFlags |= SC_MOD_DELETETEXT;
}
if (steps > 1)
modFlags |= SC_MULTISTEPUNDOREDO;
const int linesAdded = LinesTotal() - prevLinesTotal;
if (linesAdded != 0)
multiLine = true;
if (step == steps - 1) {
modFlags |= SC_LASTSTEPINUNDOREDO;
if (multiLine)
modFlags |= SC_MULTILINEUNDOREDO;
}
NotifyModified(DocModification(modFlags, cellPosition, action.lenData,
linesAdded, action.data));
}
bool endSavePoint = cb.IsSavePoint();
if (startSavePoint != endSavePoint)
NotifySavePoint(endSavePoint);
}
enteredModification--;
}
return newPos;
}
int Document::Redo() {
int newPos = -1;
CheckReadOnly();
if (enteredModification == 0) {
enteredModification++;
if (!cb.IsReadOnly()) {
bool startSavePoint = cb.IsSavePoint();
bool multiLine = false;
int steps = cb.StartRedo();
for (int step = 0; step < steps; step++) {
const int prevLinesTotal = LinesTotal();
const Action &action = cb.GetRedoStep();
if (action.at == insertAction) {
NotifyModified(DocModification(
SC_MOD_BEFOREINSERT | SC_PERFORMED_REDO, action));
} else {
NotifyModified(DocModification(
SC_MOD_BEFOREDELETE | SC_PERFORMED_REDO, action));
}
cb.PerformRedoStep();
ModifiedAt(action.position);
newPos = action.position;
int modFlags = SC_PERFORMED_REDO;
if (action.at == insertAction) {
newPos += action.lenData;
modFlags |= SC_MOD_INSERTTEXT;
} else {
modFlags |= SC_MOD_DELETETEXT;
}
if (steps > 1)
modFlags |= SC_MULTISTEPUNDOREDO;
const int linesAdded = LinesTotal() - prevLinesTotal;
if (linesAdded != 0)
multiLine = true;
if (step == steps - 1) {
modFlags |= SC_LASTSTEPINUNDOREDO;
if (multiLine)
modFlags |= SC_MULTILINEUNDOREDO;
}
NotifyModified(
DocModification(modFlags, action.position, action.lenData,
linesAdded, action.data));
}
bool endSavePoint = cb.IsSavePoint();
if (startSavePoint != endSavePoint)
NotifySavePoint(endSavePoint);
}
enteredModification--;
}
return newPos;
}
/**
* Insert a single character.
*/
bool Document::InsertChar(int pos, char ch) {
char chs[1];
chs[0] = ch;
return InsertString(pos, chs, 1);
}
/**
* Insert a null terminated string.
*/
bool Document::InsertCString(int position, const char *s) {
return InsertString(position, s, strlen(s));
}
void Document::ChangeChar(int pos, char ch) {
DeleteChars(pos, 1);
InsertChar(pos, ch);
}
void Document::DelChar(int pos) {
DeleteChars(pos, LenChar(pos));
}
void Document::DelCharBack(int pos) {
if (pos <= 0) {
return;
} else if (IsCrLf(pos - 2)) {
DeleteChars(pos - 2, 2);
} else if (dbcsCodePage) {
int startChar = MovePositionOutsideChar(pos - 1, -1, false);
DeleteChars(startChar, pos - startChar);
} else {
DeleteChars(pos - 1, 1);
}
}
static bool isindentchar(char ch) {
return (ch == ' ') || (ch == '\t');
}
static int NextTab(int pos, int tabSize) {
return ((pos / tabSize) + 1) * tabSize;
}
static void CreateIndentation(char *linebuf, int length, int indent, int tabSize, bool insertSpaces) {
length--; // ensure space for \0
if (!insertSpaces) {
while ((indent >= tabSize) && (length > 0)) {
*linebuf++ = '\t';
indent -= tabSize;
length--;
}
}
while ((indent > 0) && (length > 0)) {
*linebuf++ = ' ';
indent--;
length--;
}
*linebuf = '\0';
}
int Document::GetLineIndentation(int line) {
int indent = 0;
if ((line >= 0) && (line < LinesTotal())) {
int lineStart = LineStart(line);
int length = Length();
for (int i = lineStart;i < length;i++) {
char ch = cb.CharAt(i);
if (ch == ' ')
indent++;
else if (ch == '\t')
indent = NextTab(indent, tabInChars);
else
return indent;
}
}
return indent;
}
void Document::SetLineIndentation(int line, int indent) {
int indentOfLine = GetLineIndentation(line);
if (indent < 0)
indent = 0;
if (indent != indentOfLine) {
char linebuf[1000];
CreateIndentation(linebuf, sizeof(linebuf), indent, tabInChars, !useTabs);
int thisLineStart = LineStart(line);
int indentPos = GetLineIndentPosition(line);
BeginUndoAction();
DeleteChars(thisLineStart, indentPos - thisLineStart);
InsertCString(thisLineStart, linebuf);
EndUndoAction();
}
}
int Document::GetLineIndentPosition(int line) {
if (line < 0)
return 0;
int pos = LineStart(line);
int length = Length();
while ((pos < length) && isindentchar(cb.CharAt(pos))) {
pos++;
}
return pos;
}
int Document::GetColumn(int pos) {
int column = 0;
int line = LineFromPosition(pos);
if ((line >= 0) && (line < LinesTotal())) {
for (int i = LineStart(line);i < pos;) {
char ch = cb.CharAt(i);
if (ch == '\t') {
column = NextTab(column, tabInChars);
i++;
} else if (ch == '\r') {
return column;
} else if (ch == '\n') {
return column;
} else if (i >= Length()) {
return column;
} else {
column++;
i = MovePositionOutsideChar(i + 1, 1, false);
}
}
}
return column;
}
int Document::FindColumn(int line, int column) {
int position = LineStart(line);
int columnCurrent = 0;
if ((line >= 0) && (line < LinesTotal())) {
while ((columnCurrent < column) && (position < Length())) {
char ch = cb.CharAt(position);
if (ch == '\t') {
columnCurrent = NextTab(columnCurrent, tabInChars);
position++;
} else if (ch == '\r') {
return position;
} else if (ch == '\n') {
return position;
} else {
columnCurrent++;
position = MovePositionOutsideChar(position + 1, 1, false);
}
}
}
return position;
}
void Document::Indent(bool forwards, int lineBottom, int lineTop) {
// Dedent - suck white space off the front of the line to dedent by equivalent of a tab
for (int line = lineBottom; line >= lineTop; line--) {
int indentOfLine = GetLineIndentation(line);
if (forwards) {
if (LineStart(line) < LineEnd(line)) {
SetLineIndentation(line, indentOfLine + IndentSize());
}
} else {
SetLineIndentation(line, indentOfLine - IndentSize());
}
}
}
// Convert line endings for a piece of text to a particular mode.
// Stop at len or when a NUL is found.
// Caller must delete the returned pointer.
char *Document::TransformLineEnds(int *pLenOut, const char *s, size_t len, int eolMode) {
char *dest = new char[2 * len + 1];
const char *sptr = s;
char *dptr = dest;
for (size_t i = 0; (i < len) && (*sptr != '\0'); i++) {
if (*sptr == '\n' || *sptr == '\r') {
if (eolMode == SC_EOL_CR) {
*dptr++ = '\r';
} else if (eolMode == SC_EOL_LF) {
*dptr++ = '\n';
} else { // eolMode == SC_EOL_CRLF
*dptr++ = '\r';
*dptr++ = '\n';
}
if ((*sptr == '\r') && (i+1 < len) && (*(sptr+1) == '\n')) {
i++;
sptr++;
}
sptr++;
} else {
*dptr++ = *sptr++;
}
}
*dptr++ = '\0';
*pLenOut = (dptr - dest) - 1;
return dest;
}
void Document::ConvertLineEnds(int eolModeSet) {
BeginUndoAction();
for (int pos = 0; pos < Length(); pos++) {
if (cb.CharAt(pos) == '\r') {
if (cb.CharAt(pos + 1) == '\n') {
// CRLF
if (eolModeSet == SC_EOL_CR) {
DeleteChars(pos + 1, 1); // Delete the LF
} else if (eolModeSet == SC_EOL_LF) {
DeleteChars(pos, 1); // Delete the CR
} else {
pos++;
}
} else {
// CR
if (eolModeSet == SC_EOL_CRLF) {
InsertString(pos + 1, "\n", 1); // Insert LF
pos++;
} else if (eolModeSet == SC_EOL_LF) {
InsertString(pos, "\n", 1); // Insert LF
DeleteChars(pos + 1, 1); // Delete CR
}
}
} else if (cb.CharAt(pos) == '\n') {
// LF
if (eolModeSet == SC_EOL_CRLF) {
InsertString(pos, "\r", 1); // Insert CR
pos++;
} else if (eolModeSet == SC_EOL_CR) {
InsertString(pos, "\r", 1); // Insert CR
DeleteChars(pos + 1, 1); // Delete LF
}
}
}
EndUndoAction();
}
bool Document::IsWhiteLine(int line) {
int currentChar = LineStart(line);
int endLine = LineEnd(line);
while (currentChar < endLine) {
if (cb.CharAt(currentChar) != ' ' && cb.CharAt(currentChar) != '\t') {
return false;
}
++currentChar;
}
return true;
}
int Document::ParaUp(int pos) {
int line = LineFromPosition(pos);
line--;
while (line >= 0 && IsWhiteLine(line)) { // skip empty lines
line--;
}
while (line >= 0 && !IsWhiteLine(line)) { // skip non-empty lines
line--;
}
line++;
return LineStart(line);
}
int Document::ParaDown(int pos) {
int line = LineFromPosition(pos);
while (line < LinesTotal() && !IsWhiteLine(line)) { // skip non-empty lines
line++;
}
while (line < LinesTotal() && IsWhiteLine(line)) { // skip empty lines
line++;
}
if (line < LinesTotal())
return LineStart(line);
else // end of a document
return LineEnd(line-1);
}
CharClassify::cc Document::WordCharClass(unsigned char ch) {
if ((SC_CP_UTF8 == dbcsCodePage) && (ch >= 0x80))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -