From 30a4fa669e58ccdf55eed924761b7cdcd2f69cb8 Mon Sep 17 00:00:00 2001 From: Kurt Hindenburg Date: Sat, 24 Dec 2011 11:13:03 -0500 Subject: [PATCH] Apply astyle-kdelibs Over the years, the coding style is all over the place. Use 'git diff -w --ignore-all-space' to see non-whitespace changes. --- src/Screen.cpp | 704 ++++++++++++++++++++++--------------------------- src/Screen.h | 52 ++-- 2 files changed, 336 insertions(+), 420 deletions(-) diff --git a/src/Screen.cpp b/src/Screen.cpp index b68437293..3d8944c5b 100644 --- a/src/Screen.cpp +++ b/src/Screen.cpp @@ -48,10 +48,10 @@ const bool BS_CLEARS = false ; //Macro to convert x,y position on screen to position within an image. // -//Originally the image was stored as one large contiguous block of +//Originally the image was stored as one large contiguous block of //memory, so a position within the image could be represented as an //offset from the beginning of the block. For efficiency reasons this -//is no longer the case. +//is no longer the case. //Many internal parts of this class still use this representation for parameters and so on, //notably moveImage() and clearImage(). //This macro converts from an X,Y position into an image offset. @@ -60,15 +60,15 @@ const bool BS_CLEARS = false ; #endif const Character Screen::defaultChar = Character(' ', - CharacterColor(COLOR_SPACE_DEFAULT,DEFAULT_FORE_COLOR), - CharacterColor(COLOR_SPACE_DEFAULT,DEFAULT_BACK_COLOR), - DEFAULT_RENDITION, - false); + CharacterColor(COLOR_SPACE_DEFAULT, DEFAULT_FORE_COLOR), + CharacterColor(COLOR_SPACE_DEFAULT, DEFAULT_BACK_COLOR), + DEFAULT_RENDITION, + false); Screen::Screen(int l, int c): lines(l), columns(c), - screenLines(new ImageLine[lines+1] ), + screenLines(new ImageLine[lines + 1]), _scrolledLines(0), _droppedLines(0), history(new HistoryScrollNone()), @@ -80,9 +80,9 @@ Screen::Screen(int l, int c): effectiveForeground(CharacterColor()), effectiveBackground(CharacterColor()), effectiveRendition(0), lastPos(-1) { - lineProperties.resize(lines+1); - for (int i=0;i _bottomMargin ? lines-1 : _bottomMargin; - cuX = qMin(columns-1,cuX); // nowrap! - cuY = qMin(stop,cuY+n); + int stop = cuY > _bottomMargin ? lines - 1 : _bottomMargin; + cuX = qMin(columns - 1, cuX); // nowrap! + cuY = qMin(stop, cuY + n); } void Screen::cursorLeft(int n) - //=CUB +//=CUB { if (n == 0) n = 1; // Default - cuX = qMin(columns-1,cuX); // nowrap! - cuX = qMax(0,cuX-n); + cuX = qMin(columns - 1, cuX); // nowrap! + cuX = qMax(0, cuX - n); } void Screen::cursorRight(int n) - //=CUF +//=CUF { if (n == 0) n = 1; // Default - cuX = qMin(columns-1,cuX+n); + cuX = qMin(columns - 1, cuX + n); } void Screen::setMargins(int top, int bot) - //=STBM +//=STBM { if (top == 0) top = 1; // Default if (bot == 0) bot = lines; // Default top = top - 1; // Adjust to internal lineno bot = bot - 1; // Adjust to internal lineno - if ( !( 0 <= top && top < bot && bot < lines ) ) - { //Debug()<<" setRegion("< 0) cuY -= 1; } void Screen::nextLine() - //=NEL +//=NEL { toStartOfLine(); index(); } @@ -182,71 +182,69 @@ void Screen::nextLine() void Screen::eraseChars(int n) { if (n == 0) n = 1; // Default - int p = qMax(0,qMin(cuX+n-1,columns-1)); - clearImage(loc(cuX,cuY),loc(p,cuY),' '); + int p = qMax(0, qMin(cuX + n - 1, columns - 1)); + clearImage(loc(cuX, cuY), loc(p, cuY), ' '); } void Screen::deleteChars(int n) { - Q_ASSERT( n >= 0 ); + Q_ASSERT(n >= 0); // always delete at least one char - if (n == 0) - n = 1; + if (n == 0) + n = 1; // if cursor is beyond the end of the line there is nothing to do - if ( cuX >= screenLines[cuY].count() ) + if (cuX >= screenLines[cuY].count()) return; - if ( cuX+n > screenLines[cuY].count() ) + if (cuX + n > screenLines[cuY].count()) n = screenLines[cuY].count() - cuX; - Q_ASSERT( n >= 0 ); - Q_ASSERT( cuX+n <= screenLines[cuY].count() ); + Q_ASSERT(n >= 0); + Q_ASSERT(cuX + n <= screenLines[cuY].count()); - screenLines[cuY].remove(cuX,n); + screenLines[cuY].remove(cuX, n); } void Screen::insertChars(int n) { if (n == 0) n = 1; // Default - if ( screenLines[cuY].size() < cuX ) + if (screenLines[cuY].size() < cuX) screenLines[cuY].resize(cuX); - screenLines[cuY].insert(cuX,n,Character(' ')); + screenLines[cuY].insert(cuX, n, Character(' ')); - if ( screenLines[cuY].count() > columns ) + if (screenLines[cuY].count() > columns) screenLines[cuY].resize(columns); } void Screen::deleteLines(int n) { if (n == 0) n = 1; // Default - scrollUp(cuY,n); + scrollUp(cuY, n); } void Screen::insertLines(int n) { if (n == 0) n = 1; // Default - scrollDown(cuY,n); + scrollDown(cuY, n); } void Screen::setMode(int m) { currentModes[m] = true; - switch(m) - { - case MODE_Origin : cuX = 0; cuY = _topMargin; break; //FIXME: home + switch (m) { + case MODE_Origin : cuX = 0; cuY = _topMargin; break; //FIXME: home } } void Screen::resetMode(int m) { currentModes[m] = false; - switch(m) - { - case MODE_Origin : cuX = 0; cuY = 0; break; //FIXME: home + switch (m) { + case MODE_Origin : cuX = 0; cuY = 0; break; //FIXME: home } } @@ -276,9 +274,9 @@ void Screen::saveCursor() void Screen::restoreCursor() { - cuX = qMin(savedState.cursorColumn,columns-1); - cuY = qMin(savedState.cursorLine,lines-1); - currentRendition = savedState.rendition; + cuX = qMin(savedState.cursorColumn, columns - 1); + cuY = qMin(savedState.cursorLine, lines - 1); + currentRendition = savedState.rendition; currentForeground = savedState.foreground; currentBackground = savedState.background; updateEffectiveRendition(); @@ -286,42 +284,41 @@ void Screen::restoreCursor() void Screen::resizeImage(int new_lines, int new_columns) { - if ((new_lines==lines) && (new_columns==columns)) return; + if ((new_lines == lines) && (new_columns == columns)) return; - if (cuY > new_lines-1) - { // attempt to preserve focus and lines - _bottomMargin = lines-1; //FIXME: margin lost - for (int i = 0; i < cuY-(new_lines-1); i++) - { - addHistLine(); scrollUp(0,1); + if (cuY > new_lines - 1) { + // attempt to preserve focus and lines + _bottomMargin = lines - 1; //FIXME: margin lost + for (int i = 0; i < cuY - (new_lines - 1); i++) { + addHistLine(); scrollUp(0, 1); } } // create new screen lines and copy from old to new - ImageLine* newScreenLines = new ImageLine[new_lines+1]; - for (int i=0; i < qMin(lines,new_lines+1) ;i++) - newScreenLines[i]=screenLines[i]; - for (int i=lines;(i > 0) && (i 0) && (i < new_lines + 1); i++) + newScreenLines[i].resize(new_columns); - lineProperties.resize(new_lines+1); - for (int i=lines;(i > 0) && (i 0) && (i < new_lines + 1); i++) lineProperties[i] = LINE_DEFAULT; clearSelection(); - delete[] screenLines; + delete[] screenLines; screenLines = newScreenLines; lines = new_lines; columns = new_columns; - cuX = qMin(cuX,columns-1); - cuY = qMin(cuY,lines-1); + cuX = qMin(cuX, columns - 1); + cuY = qMin(cuY, lines - 1); // FIXME: try to keep values, evtl. - _topMargin=0; - _bottomMargin=lines-1; + _topMargin = 0; + _bottomMargin = lines - 1; initTabStops(); clearSelection(); } @@ -329,7 +326,7 @@ void Screen::resizeImage(int new_lines, int new_columns) void Screen::setDefaultMargins() { _topMargin = 0; - _bottomMargin = lines-1; + _bottomMargin = lines - 1; } @@ -368,54 +365,47 @@ void Screen::setDefaultMargins() */ void Screen::reverseRendition(Character& p) const -{ - CharacterColor f = p.foregroundColor; +{ + CharacterColor f = p.foregroundColor; CharacterColor b = p.backgroundColor; - p.foregroundColor = b; + p.foregroundColor = b; p.backgroundColor = f; //p->r &= ~RE_TRANSPARENT; } void Screen::updateEffectiveRendition() { effectiveRendition = currentRendition; - if (currentRendition & RE_REVERSE) - { + if (currentRendition & RE_REVERSE) { effectiveForeground = currentBackground; effectiveBackground = currentForeground; - } - else - { + } else { effectiveForeground = currentForeground; effectiveBackground = currentBackground; } - if ( currentRendition & RE_BOLD ) + if (currentRendition & RE_BOLD) effectiveForeground.setIntensive(); } void Screen::copyFromHistory(Character* dest, int startLine, int count) const { - Q_ASSERT( startLine >= 0 && count > 0 && startLine + count <= history->getLines() ); + Q_ASSERT(startLine >= 0 && count > 0 && startLine + count <= history->getLines()); - for (int line = startLine; line < startLine + count; line++) - { - const int length = qMin(columns,history->getLineLen(line)); - const int destLineOffset = (line-startLine)*columns; + for (int line = startLine; line < startLine + count; line++) { + const int length = qMin(columns, history->getLineLen(line)); + const int destLineOffset = (line - startLine) * columns; - history->getCells(line,0,length,dest + destLineOffset); + history->getCells(line, 0, length, dest + destLineOffset); - for (int column = length; column < columns; column++) - dest[destLineOffset+column] = defaultChar; + for (int column = length; column < columns; column++) + dest[destLineOffset + column] = defaultChar; // invert selected text - if (selBegin !=-1) - { - for (int column = 0; column < columns; column++) - { - if (isSelected(column,line)) - { - reverseRendition(dest[destLineOffset + column]); + if (selBegin != -1) { + for (int column = 0; column < columns; column++) { + if (isSelected(column, line)) { + reverseRendition(dest[destLineOffset + column]); } } } @@ -424,82 +414,77 @@ void Screen::copyFromHistory(Character* dest, int startLine, int count) const void Screen::copyFromScreen(Character* dest , int startLine , int count) const { - Q_ASSERT( startLine >= 0 && count > 0 && startLine + count <= lines ); + Q_ASSERT(startLine >= 0 && count > 0 && startLine + count <= lines); - for (int line = startLine; line < (startLine+count) ; line++) - { - int srcLineStartIndex = line*columns; - int destLineStartIndex = (line-startLine)*columns; + for (int line = startLine; line < (startLine + count) ; line++) { + int srcLineStartIndex = line * columns; + int destLineStartIndex = (line - startLine) * columns; - for (int column = 0; column < columns; column++) - { - int srcIndex = srcLineStartIndex + column; + for (int column = 0; column < columns; column++) { + int srcIndex = srcLineStartIndex + column; int destIndex = destLineStartIndex + column; - dest[destIndex] = screenLines[srcIndex/columns].value(srcIndex%columns,defaultChar); + dest[destIndex] = screenLines[srcIndex / columns].value(srcIndex % columns, defaultChar); // invert selected text - if (selBegin != -1 && isSelected(column,line + history->getLines())) - reverseRendition(dest[destIndex]); + if (selBegin != -1 && isSelected(column, line + history->getLines())) + reverseRendition(dest[destIndex]); } } } -void Screen::getImage( Character* dest, int size, int startLine, int endLine ) const +void Screen::getImage(Character* dest, int size, int startLine, int endLine) const { - Q_ASSERT( startLine >= 0 ); - Q_ASSERT( endLine >= startLine && endLine < history->getLines() + lines ); + Q_ASSERT(startLine >= 0); + Q_ASSERT(endLine >= startLine && endLine < history->getLines() + lines); const int mergedLines = endLine - startLine + 1; - Q_ASSERT( size >= mergedLines * columns ); - Q_UNUSED( size ); + Q_ASSERT(size >= mergedLines * columns); + Q_UNUSED(size); - const int linesInHistoryBuffer = qBound(0,history->getLines()-startLine,mergedLines); + const int linesInHistoryBuffer = qBound(0, history->getLines() - startLine, mergedLines); const int linesInScreenBuffer = mergedLines - linesInHistoryBuffer; // copy lines from history buffer if (linesInHistoryBuffer > 0) - copyFromHistory(dest,startLine,linesInHistoryBuffer); + copyFromHistory(dest, startLine, linesInHistoryBuffer); // copy lines from screen buffer if (linesInScreenBuffer > 0) - copyFromScreen(dest + linesInHistoryBuffer*columns, - startLine + linesInHistoryBuffer - history->getLines(), - linesInScreenBuffer); + copyFromScreen(dest + linesInHistoryBuffer * columns, + startLine + linesInHistoryBuffer - history->getLines(), + linesInScreenBuffer); // invert display when in screen mode - if (getMode(MODE_Screen)) - { - for (int i = 0; i < mergedLines*columns; i++) + if (getMode(MODE_Screen)) { + for (int i = 0; i < mergedLines * columns; i++) reverseRendition(dest[i]); // for reverse display } // mark the character at the current cursor position int cursorIndex = loc(cuX, cuY + linesInHistoryBuffer); - if(getMode(MODE_Cursor) && cursorIndex < columns*mergedLines) + if (getMode(MODE_Cursor) && cursorIndex < columns * mergedLines) dest[cursorIndex].rendition |= RE_CURSOR; } -QVector Screen::getLineProperties( int startLine , int endLine ) const +QVector Screen::getLineProperties(int startLine , int endLine) const { - Q_ASSERT( startLine >= 0 ); - Q_ASSERT( endLine >= startLine && endLine < history->getLines() + lines ); + Q_ASSERT(startLine >= 0); + Q_ASSERT(endLine >= startLine && endLine < history->getLines() + lines); - const int mergedLines = endLine-startLine+1; - const int linesInHistory = qBound(0,history->getLines()-startLine,mergedLines); + const int mergedLines = endLine - startLine + 1; + const int linesInHistory = qBound(0, history->getLines() - startLine, mergedLines); const int linesInScreen = mergedLines - linesInHistory; QVector result(mergedLines); int index = 0; // copy properties for lines in history - for (int line = startLine; line < startLine + linesInHistory; line++) - { + for (int line = startLine; line < startLine + linesInHistory; line++) { //TODO Support for line properties other than wrapped lines - if (history->isWrappedLine(line)) - { + if (history->isWrappedLine(line)) { result[index] = (LineProperty)(result[index] | LINE_WRAPPED); } index++; @@ -507,9 +492,8 @@ QVector Screen::getLineProperties( int startLine , int endLine ) c // copy properties for lines in screen buffer const int firstScreenLine = startLine + linesInHistory - history->getLines(); - for (int line = firstScreenLine; line < firstScreenLine+linesInScreen; line++) - { - result[index]=lineProperties[line]; + for (int line = firstScreenLine; line < firstScreenLine + linesInScreen; line++) { + result[index] = lineProperties[line]; index++; } @@ -518,20 +502,20 @@ QVector Screen::getLineProperties( int startLine , int endLine ) c void Screen::reset(bool clearScreen) { - setMode(MODE_Wrap ); saveMode(MODE_Wrap ); // wrap at end of margin + setMode(MODE_Wrap); saveMode(MODE_Wrap); // wrap at end of margin resetMode(MODE_Origin); saveMode(MODE_Origin); // position refere to [1,1] resetMode(MODE_Insert); saveMode(MODE_Insert); // overstroke setMode(MODE_Cursor); // cursor visible resetMode(MODE_Screen); // screen not inverse resetMode(MODE_NewLine); - _topMargin=0; - _bottomMargin=lines-1; + _topMargin = 0; + _bottomMargin = lines - 1; setDefaultRendition(); saveCursor(); - if ( clearScreen ) + if (clearScreen) clear(); } @@ -543,14 +527,13 @@ void Screen::clear() void Screen::backspace() { - cuX = qMin(columns-1,cuX); // nowrap! - cuX = qMax(0,cuX-1); + cuX = qMin(columns - 1, cuX); // nowrap! + cuX = qMax(0, cuX - 1); - if (screenLines[cuY].size() < cuX+1) - screenLines[cuY].resize(cuX+1); + if (screenLines[cuY].size() < cuX + 1) + screenLines[cuY].resize(cuX + 1); - if (BS_CLEARS) - { + if (BS_CLEARS) { screenLines[cuY][cuX].character = ' '; screenLines[cuY][cuX].rendition = screenLines[cuY][cuX].rendition & ~RE_EXTENDED_CHAR; } @@ -560,10 +543,9 @@ void Screen::tab(int n) { // note that TAB is a format effector (does not write ' '); if (n == 0) n = 1; - while((n > 0) && (cuX < columns-1)) - { - cursorRight(1); - while((cuX < columns-1) && !tabStops[cuX]) + while ((n > 0) && (cuX < columns - 1)) { + cursorRight(1); + while ((cuX < columns - 1) && !tabStops[cuX]) cursorRight(1); n--; } @@ -573,9 +555,8 @@ void Screen::backtab(int n) { // note that TAB is a format effector (does not write ' '); if (n == 0) n = 1; - while((n > 0) && (cuX > 0)) - { - cursorLeft(1); while((cuX > 0) && !tabStops[cuX]) cursorLeft(1); + while ((n > 0) && (cuX > 0)) { + cursorLeft(1); while ((cuX > 0) && !tabStops[cuX]) cursorLeft(1); n--; } } @@ -598,24 +579,24 @@ void Screen::initTabStops() // Arrg! The 1st tabstop has to be one longer than the other. // i.e. the kids start counting from 0 instead of 1. // Other programs might behave correctly. Be aware. - for (int i = 0; i < columns; i++) - tabStops[i] = (i%8 == 0 && i != 0); + for (int i = 0; i < columns; i++) + tabStops[i] = (i % 8 == 0 && i != 0); } void Screen::newLine() { - if (getMode(MODE_NewLine)) + if (getMode(MODE_NewLine)) toStartOfLine(); index(); } void Screen::checkSelection(int from, int to) { - if (selBegin == -1) + if (selBegin == -1) return; int scr_TL = loc(0, history->getLines()); //Clear entire selection if it overlaps region [from, to] - if ( (selBottomRight >= (from+scr_TL)) && (selTopLeft <= (to+scr_TL)) ) + if ((selBottomRight >= (from + scr_TL)) && (selTopLeft <= (to + scr_TL))) clearSelection(); } @@ -629,59 +610,47 @@ void Screen::displayCharacter(unsigned short c) int w = konsole_wcwidth(c); if (w < 0) return; - else if (w == 0) - { + else if (w == 0) { if (QChar(c).category() != QChar::Mark_NonSpacing) return; int charToCombineWithX = -1; int charToCombineWithY = -1; - if (cuX == 0) - { + if (cuX == 0) { // We are at the beginning of a line, check // if previous line has a character at the end we can combine with - if (cuY > 0 && columns == screenLines[cuY - 1].size()) - { + if (cuY > 0 && columns == screenLines[cuY - 1].size()) { charToCombineWithX = columns - 1; charToCombineWithY = cuY - 1; - } - else - { + } else { // There is nothing to combine with // TODO Seems gnome-terminal shows the characters alone // might be worth investigating how to do that return; } - } - else - { + } else { charToCombineWithX = cuX - 1; charToCombineWithY = cuY; } // Prevent "cat"ing binary files from causing crashes. - if (charToCombineWithX >= screenLines[charToCombineWithY].size()) - { + if (charToCombineWithX >= screenLines[charToCombineWithY].size()) { return; } Character& currentChar = screenLines[charToCombineWithY][charToCombineWithX]; - if ((currentChar.rendition & RE_EXTENDED_CHAR) == 0) - { + if ((currentChar.rendition & RE_EXTENDED_CHAR) == 0) { const ushort chars[2] = { currentChar.character, c }; currentChar.rendition |= RE_EXTENDED_CHAR; currentChar.character = ExtendedCharTable::instance.createExtendedChar(chars, 2); - } - else - { + } else { ushort extendedCharLength; const ushort* oldChars = ExtendedCharTable::instance.lookupExtendedChar(currentChar.character, extendedCharLength); Q_ASSERT(oldChars); - if (oldChars) - { + if (oldChars) { Q_ASSERT(extendedCharLength > 1); Q_ASSERT(extendedCharLength < 65535); ushort* chars = new ushort[extendedCharLength + 1]; - memcpy (chars, oldChars, sizeof(ushort) * extendedCharLength); + memcpy(chars, oldChars, sizeof(ushort) * extendedCharLength); chars[extendedCharLength] = c; currentChar.character = ExtendedCharTable::instance.createExtendedChar(chars, extendedCharLength + 1); delete[] chars; @@ -690,24 +659,22 @@ void Screen::displayCharacter(unsigned short c) return; } - if (cuX+w > columns) { + if (cuX + w > columns) { if (getMode(MODE_Wrap)) { lineProperties[cuY] = (LineProperty)(lineProperties[cuY] | LINE_WRAPPED); nextLine(); - } - else - cuX = columns-w; + } else + cuX = columns - w; } // ensure current line vector has enough elements - if (screenLines[cuY].size() < cuX+w) - { - screenLines[cuY].resize(cuX+w); + if (screenLines[cuY].size() < cuX + w) { + screenLines[cuY].resize(cuX + w); } if (getMode(MODE_Insert)) insertChars(w); - lastPos = loc(cuX,cuY); + lastPos = loc(cuX, cuY); // check if selection is still valid. checkSelection(lastPos, lastPos); @@ -722,12 +689,11 @@ void Screen::displayCharacter(unsigned short c) int i = 0; int newCursorX = cuX + w--; - while(w) - { + while (w) { i++; - if ( screenLines[cuY].size() < cuX + i + 1 ) - screenLines[cuY].resize(cuX+i+1); + if (screenLines[cuY].size() < cuX + i + 1) + screenLines[cuY].resize(cuX + i + 1); Character& ch = screenLines[cuY][cuX + i]; ch.character = 0; @@ -775,11 +741,11 @@ void Screen::scrollUp(int from, int n) if (n <= 0 || from + n > _bottomMargin) return; _scrolledLines -= n; - _lastScrolledRegion = QRect(0,_topMargin,columns-1,(_bottomMargin-_topMargin)); + _lastScrolledRegion = QRect(0, _topMargin, columns - 1, (_bottomMargin - _topMargin)); //FIXME: make sure `topMargin', `bottomMargin', `from', `n' is in bounds. - moveImage(loc(0,from),loc(0,from+n),loc(columns-1,_bottomMargin)); - clearImage(loc(0,_bottomMargin-n+1),loc(columns-1,_bottomMargin),' '); + moveImage(loc(0, from), loc(0, from + n), loc(columns - 1, _bottomMargin)); + clearImage(loc(0, _bottomMargin - n + 1), loc(columns - 1, _bottomMargin), ' '); } void Screen::scrollDown(int n) @@ -793,14 +759,14 @@ void Screen::scrollDown(int from, int n) _scrolledLines += n; //FIXME: make sure `topMargin', `bottomMargin', `from', `n' is in bounds. - if (n <= 0) + if (n <= 0) return; - if (from > _bottomMargin) + if (from > _bottomMargin) return; - if (from + n > _bottomMargin) + if (from + n > _bottomMargin) n = _bottomMargin - from; - moveImage(loc(0,from+n),loc(0,from),loc(columns-1,_bottomMargin-n)); - clearImage(loc(0,from),loc(columns-1,from+n-1),' '); + moveImage(loc(0, from + n), loc(0, from), loc(columns - 1, _bottomMargin - n)); + clearImage(loc(0, from), loc(columns - 1, from + n - 1), ' '); } void Screen::setCursorYX(int y, int x) @@ -812,14 +778,14 @@ void Screen::setCursorX(int x) { if (x == 0) x = 1; // Default x -= 1; // Adjust - cuX = qMax(0,qMin(columns-1, x)); + cuX = qMax(0, qMin(columns - 1, x)); } void Screen::setCursorY(int y) { if (y == 0) y = 1; // Default y -= 1; // Adjust - cuY = qMax(0,qMin(lines -1, y + (getMode(MODE_Origin) ? _topMargin : 0) )); + cuY = qMax(0, qMin(lines - 1, y + (getMode(MODE_Origin) ? _topMargin : 0))); } void Screen::home() @@ -844,96 +810,84 @@ int Screen::getCursorY() const } void Screen::clearImage(int loca, int loce, char c) -{ - int scr_TL=loc(0,history->getLines()); +{ + int scr_TL = loc(0, history->getLines()); //FIXME: check positions //Clear entire selection if it overlaps region to be moved... - if ( (selBottomRight > (loca+scr_TL) )&&(selTopLeft < (loce+scr_TL)) ) - { + if ((selBottomRight > (loca + scr_TL)) && (selTopLeft < (loce + scr_TL))) { clearSelection(); } - int topLine = loca/columns; - int bottomLine = loce/columns; + int topLine = loca / columns; + int bottomLine = loce / columns; - Character clearCh(c,currentForeground,currentBackground,DEFAULT_RENDITION,false); + Character clearCh(c, currentForeground, currentBackground, DEFAULT_RENDITION, false); //if the character being used to clear the area is the same as the //default character, the affected lines can simply be shrunk. - bool isDefaultCh = ( clearCh == Screen::defaultChar ); + bool isDefaultCh = (clearCh == Screen::defaultChar); - for (int y=topLine;y<=bottomLine;y++) - { + for (int y = topLine; y <= bottomLine; y++) { lineProperties[y] = 0; - int endCol = ( y == bottomLine) ? loce%columns : columns-1; - int startCol = ( y == topLine ) ? loca%columns : 0; + int endCol = (y == bottomLine) ? loce % columns : columns - 1; + int startCol = (y == topLine) ? loca % columns : 0; QVector& line = screenLines[y]; - if ( isDefaultCh && endCol == columns-1 ) - { + if (isDefaultCh && endCol == columns - 1) { line.resize(startCol); - } - else - { + } else { if (line.size() < endCol + 1) - line.resize(endCol+1); + line.resize(endCol + 1); Character* data = line.data(); - for (int i=startCol;i<=endCol;i++) - data[i]=clearCh; + for (int i = startCol; i <= endCol; i++) + data[i] = clearCh; } } } void Screen::moveImage(int dest, int sourceBegin, int sourceEnd) { - Q_ASSERT( sourceBegin <= sourceEnd ); + Q_ASSERT(sourceBegin <= sourceEnd); - int lines=(sourceEnd-sourceBegin)/columns; + int lines = (sourceEnd - sourceBegin) / columns; //move screen image and line properties: - //the source and destination areas of the image may overlap, - //so it matters that we do the copy in the right order - + //the source and destination areas of the image may overlap, + //so it matters that we do the copy in the right order - //forwards if dest < sourceBegin or backwards otherwise. //(search the web for 'memmove implementation' for details) - if (dest < sourceBegin) - { - for (int i=0;i<=lines;i++) - { - screenLines[ (dest/columns)+i ] = screenLines[ (sourceBegin/columns)+i ]; - lineProperties[(dest/columns)+i]=lineProperties[(sourceBegin/columns)+i]; + if (dest < sourceBegin) { + for (int i = 0; i <= lines; i++) { + screenLines[(dest / columns) + i ] = screenLines[(sourceBegin / columns) + i ]; + lineProperties[(dest / columns) + i] = lineProperties[(sourceBegin / columns) + i]; } - } - else - { - for (int i=lines;i>=0;i--) - { - screenLines[ (dest/columns)+i ] = screenLines[ (sourceBegin/columns)+i ]; - lineProperties[(dest/columns)+i]=lineProperties[(sourceBegin/columns)+i]; + } else { + for (int i = lines; i >= 0; i--) { + screenLines[(dest / columns) + i ] = screenLines[(sourceBegin / columns) + i ]; + lineProperties[(dest / columns) + i] = lineProperties[(sourceBegin / columns) + i]; } } - if (lastPos != -1) - { + if (lastPos != -1) { int diff = dest - sourceBegin; // Scroll by this amount lastPos += diff; - if ((lastPos < 0) || (lastPos >= (lines*columns))) + if ((lastPos < 0) || (lastPos >= (lines * columns))) lastPos = -1; } // Adjust selection to follow scroll. - if (selBegin != -1) - { + if (selBegin != -1) { bool beginIsTL = (selBegin == selTopLeft); int diff = dest - sourceBegin; // Scroll by this amount - int scr_TL=loc(0,history->getLines()); - int srca = sourceBegin+scr_TL; // Translate index from screen to global - int srce = sourceEnd+scr_TL; // Translate index from screen to global - int desta = srca+diff; - int deste = srce+diff; + int scr_TL = loc(0, history->getLines()); + int srca = sourceBegin + scr_TL; // Translate index from screen to global + int srce = sourceEnd + scr_TL; // Translate index from screen to global + int desta = srca + diff; + int deste = srce + diff; if ((selTopLeft >= srca) && (selTopLeft <= srce)) selTopLeft += diff; @@ -945,12 +899,9 @@ void Screen::moveImage(int dest, int sourceBegin, int sourceEnd) else if ((selBottomRight >= desta) && (selBottomRight <= deste)) selBottomRight = -1; // Clear selection (see below) - if (selBottomRight < 0) - { + if (selBottomRight < 0) { clearSelection(); - } - else - { + } else { if (selTopLeft < 0) selTopLeft = 0; } @@ -964,23 +915,22 @@ void Screen::moveImage(int dest, int sourceBegin, int sourceEnd) void Screen::clearToEndOfScreen() { - clearImage(loc(cuX,cuY),loc(columns-1,lines-1),' '); + clearImage(loc(cuX, cuY), loc(columns - 1, lines - 1), ' '); } void Screen::clearToBeginOfScreen() { - clearImage(loc(0,0),loc(cuX,cuY),' '); + clearImage(loc(0, 0), loc(cuX, cuY), ' '); } void Screen::clearEntireScreen() { // Add entire screen to history - for (int i = 0; i < (lines-1); i++) - { - addHistLine(); scrollUp(0,1); + for (int i = 0; i < (lines - 1); i++) { + addHistLine(); scrollUp(0, 1); } - clearImage(loc(0,0),loc(columns-1,lines-1),' '); + clearImage(loc(0, 0), loc(columns - 1, lines - 1), ' '); } /*! fill screen with 'E' @@ -989,22 +939,22 @@ void Screen::clearEntireScreen() void Screen::helpAlign() { - clearImage(loc(0,0),loc(columns-1,lines-1),'E'); + clearImage(loc(0, 0), loc(columns - 1, lines - 1), 'E'); } void Screen::clearToEndOfLine() { - clearImage(loc(cuX,cuY),loc(columns-1,cuY),' '); + clearImage(loc(cuX, cuY), loc(columns - 1, cuY), ' '); } void Screen::clearToBeginOfLine() { - clearImage(loc(0,cuY),loc(cuX,cuY),' '); + clearImage(loc(0, cuY), loc(cuX, cuY), ' '); } void Screen::clearEntireLine() { - clearImage(loc(0,cuY),loc(columns-1,cuY),' '); + clearImage(loc(0, cuY), loc(columns - 1, cuY), ' '); } void Screen::setRendition(int re) @@ -1021,8 +971,8 @@ void Screen::resetRendition(int re) void Screen::setDefaultRendition() { - setForeColor(COLOR_SPACE_DEFAULT,DEFAULT_FORE_COLOR); - setBackColor(COLOR_SPACE_DEFAULT,DEFAULT_BACK_COLOR); + setForeColor(COLOR_SPACE_DEFAULT, DEFAULT_FORE_COLOR); + setBackColor(COLOR_SPACE_DEFAULT, DEFAULT_BACK_COLOR); currentRendition = DEFAULT_RENDITION; updateEffectiveRendition(); } @@ -1031,23 +981,23 @@ void Screen::setForeColor(int space, int color) { currentForeground = CharacterColor(space, color); - if ( currentForeground.isValid() ) + if (currentForeground.isValid()) updateEffectiveRendition(); - else - setForeColor(COLOR_SPACE_DEFAULT,DEFAULT_FORE_COLOR); + else + setForeColor(COLOR_SPACE_DEFAULT, DEFAULT_FORE_COLOR); } void Screen::setBackColor(int space, int color) { currentBackground = CharacterColor(space, color); - if ( currentBackground.isValid() ) + if (currentBackground.isValid()) updateEffectiveRendition(); else - setBackColor(COLOR_SPACE_DEFAULT,DEFAULT_BACK_COLOR); + setBackColor(COLOR_SPACE_DEFAULT, DEFAULT_BACK_COLOR); } -void Screen::clearSelection() +void Screen::clearSelection() { selBottomRight = -1; selTopLeft = -1; @@ -1056,33 +1006,27 @@ void Screen::clearSelection() void Screen::getSelectionStart(int& column , int& line) const { - if ( selTopLeft != -1 ) - { + if (selTopLeft != -1) { column = selTopLeft % columns; - line = selTopLeft / columns; - } - else - { + line = selTopLeft / columns; + } else { column = cuX + getHistLines(); line = cuY + getHistLines(); } } void Screen::getSelectionEnd(int& column , int& line) const { - if ( selBottomRight != -1 ) - { + if (selBottomRight != -1) { column = selBottomRight % columns; line = selBottomRight / columns; - } - else - { + } else { column = cuX + getHistLines(); line = cuY + getHistLines(); - } + } } void Screen::setSelectionStart(const int x, const int y, const bool mode) { - selBegin = loc(x,y); + selBegin = loc(x, y); /* FIXME, HACK to correct for x too far to the right... */ if (x == columns) selBegin--; @@ -1091,22 +1035,19 @@ void Screen::setSelectionStart(const int x, const int y, const bool mode) blockSelectionMode = mode; } -void Screen::setSelectionEnd( const int x, const int y) +void Screen::setSelectionEnd(const int x, const int y) { - if (selBegin == -1) + if (selBegin == -1) return; - int endPos = loc(x,y); + int endPos = loc(x, y); - if (endPos < selBegin) - { + if (endPos < selBegin) { selTopLeft = endPos; selBottomRight = selBegin; - } - else - { + } else { /* FIXME, HACK to correct for x too far to the right... */ - if (x == columns) + if (x == columns) endPos--; selTopLeft = selBegin; @@ -1114,28 +1055,26 @@ void Screen::setSelectionEnd( const int x, const int y) } // Normalize the selection in column mode - if (blockSelectionMode) - { + if (blockSelectionMode) { int topRow = selTopLeft / columns; int topColumn = selTopLeft % columns; int bottomRow = selBottomRight / columns; int bottomColumn = selBottomRight % columns; - selTopLeft = loc(qMin(topColumn,bottomColumn),topRow); - selBottomRight = loc(qMax(topColumn,bottomColumn),bottomRow); + selTopLeft = loc(qMin(topColumn, bottomColumn), topRow); + selBottomRight = loc(qMax(topColumn, bottomColumn), bottomRow); } } -bool Screen::isSelected( const int x,const int y) const +bool Screen::isSelected(const int x, const int y) const { bool columnInSelection = true; - if (blockSelectionMode) - { + if (blockSelectionMode) { columnInSelection = x >= (selTopLeft % columns) && - x <= (selBottomRight % columns); + x <= (selBottomRight % columns); } - int pos = loc(x,y); + int pos = loc(x, y); return pos >= selTopLeft && pos <= selBottomRight && columnInSelection; } @@ -1157,137 +1096,124 @@ bool Screen::isSelectionValid() const return selTopLeft >= 0 && selBottomRight >= 0; } -void Screen::writeSelectionToStream(TerminalCharacterDecoder* decoder , - bool preserveLineBreaks) const +void Screen::writeSelectionToStream(TerminalCharacterDecoder* decoder , + bool preserveLineBreaks) const { if (!isSelectionValid()) return; - writeToStream(decoder,selTopLeft,selBottomRight,preserveLineBreaks); + writeToStream(decoder, selTopLeft, selBottomRight, preserveLineBreaks); } -void Screen::writeToStream(TerminalCharacterDecoder* decoder, - int startIndex, int endIndex, - bool preserveLineBreaks) const +void Screen::writeToStream(TerminalCharacterDecoder* decoder, + int startIndex, int endIndex, + bool preserveLineBreaks) const { - int top = startIndex / columns; + int top = startIndex / columns; int left = startIndex % columns; int bottom = endIndex / columns; int right = endIndex % columns; - Q_ASSERT( top >= 0 && left >= 0 && bottom >= 0 && right >= 0 ); + Q_ASSERT(top >= 0 && left >= 0 && bottom >= 0 && right >= 0); - for (int y=top;y<=bottom;y++) - { + for (int y = top; y <= bottom; y++) { int start = 0; - if ( y == top || blockSelectionMode ) start = left; + if (y == top || blockSelectionMode) start = left; int count = -1; - if ( y == bottom || blockSelectionMode ) count = right - start + 1; + if (y == bottom || blockSelectionMode) count = right - start + 1; - const bool appendNewLine = ( y != bottom ); - int copied = copyLineToStream( y, - start, - count, - decoder, - appendNewLine, - preserveLineBreaks ); + const bool appendNewLine = (y != bottom); + int copied = copyLineToStream(y, + start, + count, + decoder, + appendNewLine, + preserveLineBreaks); // if the selection goes beyond the end of the last line then // append a new line character. // // this makes it possible to 'select' a trailing new line character after - // the text on a line. - if ( y == bottom && - copied < count ) - { + // the text on a line. + if (y == bottom && + copied < count) { Character newLineChar('\n'); - decoder->decodeLine(&newLineChar,1,0); + decoder->decodeLine(&newLineChar, 1, 0); } - } + } } -int Screen::copyLineToStream(int line , - int start, - int count, - TerminalCharacterDecoder* decoder, - bool appendNewLine, - bool preserveLineBreaks) const +int Screen::copyLineToStream(int line , + int start, + int count, + TerminalCharacterDecoder* decoder, + bool appendNewLine, + bool preserveLineBreaks) const { //buffer to hold characters for decoding - //the buffer is static to avoid initialising every + //the buffer is static to avoid initialising every //element on each call to copyLineToStream //(which is unnecessary since all elements will be overwritten anyway) static const int MAX_CHARS = 1024; static Character characterBuffer[MAX_CHARS]; - assert( count < MAX_CHARS ); + assert(count < MAX_CHARS); LineProperty currentLineProperties = 0; //determine if the line is in the history buffer or the screen image - if (line < history->getLines()) - { + if (line < history->getLines()) { const int lineLength = history->getLineLen(line); // ensure that start position is before end of line - start = qMin(start,qMax(0,lineLength-1)); + start = qMin(start, qMax(0, lineLength - 1)); // retrieve line from history buffer. It is assumed // that the history buffer does not store trailing white space - // at the end of the line, so it does not need to be trimmed here - if (count == -1) - { - count = lineLength-start; - } - else - { - count = qMin(start+count,lineLength)-start; + // at the end of the line, so it does not need to be trimmed here + if (count == -1) { + count = lineLength - start; + } else { + count = qMin(start + count, lineLength) - start; } // safety checks - assert( start >= 0 ); - assert( count >= 0 ); - assert( (start+count) <= history->getLineLen(line) ); + assert(start >= 0); + assert(count >= 0); + assert((start + count) <= history->getLineLen(line)); - history->getCells(line,start,count,characterBuffer); + history->getCells(line, start, count, characterBuffer); - if ( history->isWrappedLine(line) ) + if (history->isWrappedLine(line)) currentLineProperties |= LINE_WRAPPED; - } - else - { - if ( count == -1 ) + } else { + if (count == -1) count = columns - start; - assert( count >= 0 ); + assert(count >= 0); - const int screenLine = line-history->getLines(); + const int screenLine = line - history->getLines(); Character* data = screenLines[screenLine].data(); int length = screenLines[screenLine].count(); //retrieve line from screen image - for (int i=start;i < qMin(start+count,length);i++) - { - characterBuffer[i-start] = data[i]; + for (int i = start; i < qMin(start + count, length); i++) { + characterBuffer[i - start] = data[i]; } // count cannot be any greater than length - count = qBound(0,count,length-start); + count = qBound(0, count, length - start); - Q_ASSERT( screenLine < lineProperties.count() ); - currentLineProperties |= lineProperties[screenLine]; + Q_ASSERT(screenLine < lineProperties.count()); + currentLineProperties |= lineProperties[screenLine]; } - if ( appendNewLine && (count+1 < MAX_CHARS) ) - { - if ( currentLineProperties & LINE_WRAPPED ) - { + if (appendNewLine && (count + 1 < MAX_CHARS)) { + if (currentLineProperties & LINE_WRAPPED) { // do nothing extra when this line is wrapped. - } - else - { + } else { // When users ask not to preserve the linebreaks, they usually mean: // `treat LINEBREAK as SPACE, thus joining multiple lines into // single line in the same way as 'J' does in VIM.` @@ -1296,16 +1222,16 @@ int Screen::copyLineToStream(int line , } } - //decode line and write to text stream - decoder->decodeLine( (Character*) characterBuffer , - count, currentLineProperties ); + //decode line and write to text stream + decoder->decodeLine((Character*) characterBuffer , + count, currentLineProperties); return count; } void Screen::writeLinesToStream(TerminalCharacterDecoder* decoder, int fromLine, int toLine) const { - writeToStream(decoder,loc(0,fromLine),loc(columns-1,toLine)); + writeToStream(decoder, loc(0, fromLine), loc(columns - 1, toLine)); } void Screen::addHistLine() @@ -1313,12 +1239,11 @@ void Screen::addHistLine() // add line to history buffer // we have to take care about scrolling, too... - if (hasScroll()) - { + if (hasScroll()) { int oldHistLines = history->getLines(); history->addCellsVector(screenLines[0]); - history->addLine( lineProperties[0] & LINE_WRAPPED ); + history->addLine(lineProperties[0] & LINE_WRAPPED); int newHistLines = history->getLines(); @@ -1326,23 +1251,20 @@ void Screen::addHistLine() // If the history is full, increment the count // of dropped lines - if ( newHistLines == oldHistLines ) + if (newHistLines == oldHistLines) _droppedLines++; // Adjust selection for the new point of reference - if (newHistLines > oldHistLines) - { - if (selBegin != -1) - { + if (newHistLines > oldHistLines) { + if (selBegin != -1) { selTopLeft += columns; selBottomRight += columns; } } - if (selBegin != -1) - { + if (selBegin != -1) { // Scroll selection in history up - int top_BR = loc(0, 1+newHistLines); + int top_BR = loc(0, 1 + newHistLines); if (selTopLeft < top_BR) selTopLeft -= columns; @@ -1352,8 +1274,7 @@ void Screen::addHistLine() if (selBottomRight < 0) clearSelection(); - else - { + else { if (selTopLeft < 0) selTopLeft = 0; } @@ -1376,10 +1297,9 @@ void Screen::setScroll(const HistoryType& t , bool copyPreviousScroll) { clearSelection(); - if ( copyPreviousScroll ) + if (copyPreviousScroll) history = t.scroll(history); - else - { + else { HistoryScroll* oldScroll = history; history = t.scroll(0); delete oldScroll; @@ -1398,13 +1318,13 @@ const HistoryType& Screen::getScroll() const void Screen::setLineProperty(LineProperty property , bool enable) { - if ( enable ) + if (enable) lineProperties[cuY] = (LineProperty)(lineProperties[cuY] | property); else lineProperties[cuY] = (LineProperty)(lineProperties[cuY] & ~property); } void Screen::fillWithDefaultChar(Character* dest, int count) { - for (int i=0;i getLineProperties( int startLine , int endLine ) const; + QVector getLineProperties(int startLine , int endLine) const; /** Return the number of lines. */ - int getLines() const - { return lines; } + int getLines() const { + return lines; + } /** Return the number of columns. */ - int getColumns() const - { return columns; } + int getColumns() const { + return columns; + } /** Return the number of lines in the history buffer. */ int getHistLines() const; /** @@ -439,7 +441,7 @@ public: * Returns true if the character at (@p column, @p line) is part of the * current selection. */ - bool isSelected(const int column,const int line) const; + bool isSelected(const int column, const int line) const; /** * Convenience method. Returns the currently selected text. @@ -547,26 +549,20 @@ public: */ static void fillWithDefaultChar(Character* dest, int count); - void setCurrentTerminalDisplay(TerminalDisplay* display) - { - _currentTerminalDisplay = display; + void setCurrentTerminalDisplay(TerminalDisplay* display) { + _currentTerminalDisplay = display; } - TerminalDisplay* currentTerminalDisplay() - { + TerminalDisplay* currentTerminalDisplay() { return _currentTerminalDisplay; } - QSet usedExtendedChars() const - { + QSet usedExtendedChars() const { QSet result; - for (int i = 0; i < lines; ++i) - { + for (int i = 0; i < lines; ++i) { const ImageLine& il = screenLines[i]; - for (int j = 0; j < columns; ++j) - { - if (il[j].rendition & RE_EXTENDED_CHAR) - { + for (int j = 0; j < columns; ++j) { + if (il[j].rendition & RE_EXTENDED_CHAR) { result << il[j].character; } } @@ -589,9 +585,9 @@ private: //count - the number of characters on the line to copy //decoder - a decoder which converts terminal characters (an Character array) into text //appendNewLine - if true a new line character (\n) is appended to the end of the line - int copyLineToStream(int line, - int start, - int count, + int copyLineToStream(int line, + int start, + int count, TerminalCharacterDecoder* decoder, bool appendNewLine, bool preserveLineBreaks) const; @@ -625,7 +621,7 @@ private: bool isSelectionValid() const; // copies text from 'startIndex' to 'endIndex' to a stream // startIndex and endIndex are positions generated using the loc(x,y) macro - void writeToStream(TerminalCharacterDecoder* decoder, int startIndex, + void writeToStream(TerminalCharacterDecoder* decoder, int startIndex, int endIndex, bool preserveLineBreaks = true) const; // copies 'count' lines from the screen buffer into 'dest', // starting from 'startLine', where 0 is the first line in the screen buffer @@ -647,7 +643,7 @@ private: int _droppedLines; - QVarLengthArray lineProperties; + QVarLengthArray lineProperties; // history buffer --------------- HistoryScroll* history; @@ -659,7 +655,7 @@ private: // cursor color and rendition info CharacterColor currentForeground; CharacterColor currentBackground; - quint8 currentRendition; + quint8 currentRendition; // margins ---------------- int _topMargin; @@ -684,11 +680,11 @@ private: CharacterColor effectiveBackground; // the cu_* variables above quint8 effectiveRendition; // to speed up operation - class SavedState + class SavedState { public: SavedState() - : cursorColumn(0),cursorLine(0),rendition(0) {} + : cursorColumn(0), cursorLine(0), rendition(0) {} int cursorColumn; int cursorLine;