From ec21536b9f01c412c8a9d7adb97b9651e89dc881 Mon Sep 17 00:00:00 2001
From: Robert Knight <robertknight@gmail.com>
Date: Mon, 17 Sep 2001 00:00:00 +0400
Subject: konsole: Fix number of lines of scrollback kept

Fix number of lines of scrollback kept to match that set by the user.
Previously only 'number of lines set'-1 were visible.

This reapplies old KDE patch. The changes were previously reverted in
10e4114459 for unknown reasons (presumably by accident).

Patch-by: tfelker2@uiuc.edu
Origin: https://github.com/KDE/konsole/commit/5d7bb3771e93007b3b9ddc18aad77b8bbf17786c/
Bug: https://bugs.kde.org/show_bug.cgi?id=95990
Bug: https://mirror.git.trinitydesktop.org/gitea/tde/arts/issues/15
Co-authored-by: Alexander Golubev <fatzer2@gmail.com>
Signed-off-by: Alexander Golubev <fatzer2@gmail.com>
---
 konsole/konsole/TEHistory.cpp | 110 +++++++++++++++++++-----------------------
 konsole/konsole/TEHistory.h   |  13 ++---
 2 files changed, 54 insertions(+), 69 deletions(-)

diff --git a/konsole/konsole/TEHistory.cpp b/konsole/konsole/TEHistory.cpp
index cbe8b199d..f4df945c8 100644
--- a/konsole/konsole/TEHistory.cpp
+++ b/konsole/konsole/TEHistory.cpp
@@ -78,7 +78,7 @@ HistoryFile::HistoryFile()
     length(0)
 {
   if (tmpFile.status() == 0)
-  { 
+  {
     tmpFile.unlink();
     ion = tmpFile.handle();
   }
@@ -133,7 +133,7 @@ bool HistoryScroll::hasScroll()
 
 // History Scroll File //////////////////////////////////////
 
-/* 
+/*
    The history scroll makes a Row(Row(Cell)) from
    two history buffers. The index buffer contains
    start of line positions which refere to the cells
@@ -153,7 +153,7 @@ HistoryScrollFile::HistoryScrollFile(const TQString &logFileName)
 HistoryScrollFile::~HistoryScrollFile()
 {
 }
- 
+
 int HistoryScrollFile::getLines()
 {
   return index.len() / sizeof(int);
@@ -207,70 +207,39 @@ void HistoryScrollFile::addLine(bool previousWrapped)
 // History Scroll Buffer //////////////////////////////////////
 HistoryScrollBuffer::HistoryScrollBuffer(unsigned int maxNbLines)
   : HistoryScroll(new HistoryTypeBuffer(maxNbLines)),
+    m_histBuffer(maxNbLines),
+    m_wrappedLine(maxNbLines),
     m_maxNbLines(maxNbLines),
     m_nbLines(0),
-    m_arrayIndex(0),
-    m_buffFilled(false)
+    m_arrayIndex(maxNbLines - 1)
 {
-  m_histBuffer.setAutoDelete(true);
-  m_histBuffer.resize(maxNbLines);
-  m_wrappedLine.resize(maxNbLines);
 }
 
 HistoryScrollBuffer::~HistoryScrollBuffer()
 {
+  for(size_t line = 0; line < m_nbLines; ++line) {
+     delete m_histBuffer[adjustLineNb(line)];
+  }
 }
 
 void HistoryScrollBuffer::addCells(ca a[], int count)
 {
-  //unsigned int nbLines = countLines(bytes, len);
-
   histline* newLine = new histline;
 
   newLine->duplicate(a, count);
-  
+
   ++m_arrayIndex;
   if (m_arrayIndex >= m_maxNbLines) {
      m_arrayIndex = 0;
-     m_buffFilled = true;
-    }
+  }
 
-  // FIXME: See BR96605
-  if (m_nbLines < m_maxNbLines - 1) ++m_nbLines;
+  if (m_nbLines < m_maxNbLines) ++m_nbLines;
 
-  // m_histBuffer.remove(m_arrayIndex); // not necessary
+  delete m_histBuffer[m_arrayIndex];
   m_histBuffer.insert(m_arrayIndex, newLine);
   m_wrappedLine.clearBit(m_arrayIndex);
 }
 
-void HistoryScrollBuffer::normalize()
-{
-  if (!m_buffFilled || !m_arrayIndex) return;
-  TQPtrVector<histline> newHistBuffer;
-  newHistBuffer.resize(m_maxNbLines);
-  TQBitArray newWrappedLine;
-  newWrappedLine.resize(m_maxNbLines);
-  for(int i = 0; i < (int) m_maxNbLines-2; i++)
-  {
-     int lineno = adjustLineNb(i);
-     newHistBuffer.insert(i+1, m_histBuffer[lineno]);
-     newWrappedLine.setBit(i+1, m_wrappedLine[lineno]);
-  }
-  m_histBuffer.setAutoDelete(false);
-  // Qt 2.3: QVector copy assignment is buggy :-(
-  //  m_histBuffer = newHistBuffer;
-  for(int i = 0; i < (int) m_maxNbLines; i++)
-  {
-     m_histBuffer.insert(i, newHistBuffer[i]);
-     m_wrappedLine.setBit(i, newWrappedLine[i]);
-  }
-  m_histBuffer.setAutoDelete(true);
-
-  m_arrayIndex = m_maxNbLines;
-  m_buffFilled = false;
-  m_nbLines = m_maxNbLines-2;
-}
-
 void HistoryScrollBuffer::addLine(bool previousWrapped)
 {
   m_wrappedLine.setBit(m_arrayIndex,previousWrapped);
@@ -307,7 +276,7 @@ void HistoryScrollBuffer::getCells(int lineno, int colno, int count, ca res[])
   assert (lineno < (int) m_maxNbLines);
 
   lineno = adjustLineNb(lineno);
-  
+
   histline *l = m_histBuffer[lineno];
 
   if (!l) {
@@ -315,19 +284,40 @@ void HistoryScrollBuffer::getCells(int lineno, int colno, int count, ca res[])
     return;
   }
 
-  assert((colno < (int) l->size()) || (count == 0));
-    
+  assert(colno <= (int) l->size() - count);
+
   memcpy(res, l->data() + colno, count * sizeof(ca));
 }
 
 void HistoryScrollBuffer::setMaxNbLines(unsigned int nbLines)
 {
-  normalize();
+  TQPtrVector<histline> newHistBuffer(nbLines);
+  TQBitArray newWrappedLine(nbLines);
+
+  size_t preservedLines = (nbLines > m_nbLines ? m_nbLines : nbLines); //min
+
+  // delete any lines that will be lost
+  size_t lineOld;
+  for(lineOld = 0; lineOld < m_nbLines - preservedLines; ++lineOld) {
+     delete m_histBuffer[adjustLineNb(lineOld)];
+  }
+
+  // copy the lines to new arrays
+  size_t indexNew = 0;
+  while(indexNew < preservedLines) {
+     newHistBuffer.insert(indexNew, m_histBuffer[adjustLineNb(lineOld)]);
+     newWrappedLine.setBit(indexNew, m_wrappedLine[adjustLineNb(lineOld)]);
+     ++lineOld;
+     ++indexNew;
+  }
+  m_arrayIndex = preservedLines - 1;
+
+  m_histBuffer = newHistBuffer;
+  m_wrappedLine = newWrappedLine;
+
   m_maxNbLines = nbLines;
-  m_histBuffer.resize(m_maxNbLines);
-  m_wrappedLine.resize(m_maxNbLines);
-  if (m_nbLines > m_maxNbLines - 2)
-     m_nbLines = m_maxNbLines -2;
+  if (m_nbLines > m_maxNbLines)
+     m_nbLines = m_maxNbLines;
 
   delete m_histType;
   m_histType = new HistoryTypeBuffer(nbLines);
@@ -335,10 +325,10 @@ void HistoryScrollBuffer::setMaxNbLines(unsigned int nbLines)
 
 int HistoryScrollBuffer::adjustLineNb(int lineno)
 {
-  if (m_buffFilled)
-      return (lineno + m_arrayIndex + 2) % m_maxNbLines;
-  else
-      return lineno ? lineno + 1 : 0;
+  // lineno = 0:               oldest line
+  // lineno = getLines() - 1:  newest line
+
+  return (m_arrayIndex + lineno - (m_nbLines - 1) + m_maxNbLines) % m_maxNbLines;
 }
 
 
@@ -438,7 +428,7 @@ void HistoryScrollBlockArray::getCells(int lineno, int colno,
 void HistoryScrollBlockArray::addCells(ca a[], int count)
 {
   Block *b = m_blockArray.lastBlock();
-  
+
   if (!b) return;
 
   // put cells in block's data
@@ -456,7 +446,7 @@ void HistoryScrollBlockArray::addCells(ca a[], int count)
   // store line length
   size_t *pLen = new size_t;
   *pLen = count;
-  
+
   m_lineLengths.replace(m_blockArray.getCurrent(), pLen);
 }
 
@@ -600,7 +590,7 @@ const TQString& HistoryTypeFile::getFileName() const
 
 HistoryScroll* HistoryTypeFile::getScroll(HistoryScroll *old) const
 {
-  if (dynamic_cast<HistoryFile *>(old)) 
+  if (dynamic_cast<HistoryFile *>(old))
      return old; // Unchanged.
 
   HistoryScroll *newScroll = new HistoryScrollFile(m_fileName);
@@ -627,7 +617,7 @@ HistoryScroll* HistoryTypeFile::getScroll(HistoryScroll *old) const
   }
 
   delete old;
-  return newScroll; 
+  return newScroll;
 }
 
 unsigned int HistoryTypeFile::getSize() const
diff --git a/konsole/konsole/TEHistory.h b/konsole/konsole/TEHistory.h
index ff16433b7..812c9eaed 100644
--- a/konsole/konsole/TEHistory.h
+++ b/konsole/konsole/TEHistory.h
@@ -137,21 +137,16 @@ public:
 
   void setMaxNbLines(unsigned int nbLines);
   unsigned int maxNbLines() { return m_maxNbLines; }
-  
+
 
 private:
   int adjustLineNb(int lineno);
 
-  // Normalize buffer so that the size can be changed.
-  void normalize();
-
-  bool m_hasScroll;
   TQPtrVector<histline> m_histBuffer;
   TQBitArray m_wrappedLine;
   unsigned int m_maxNbLines;
   unsigned int m_nbLines;
   unsigned int m_arrayIndex;
-  bool         m_buffFilled;
 
 };
 
@@ -232,7 +227,7 @@ class HistoryTypeBlockArray : public HistoryType
 {
 public:
   HistoryTypeBlockArray(size_t size);
-  
+
   virtual bool isOn() const;
   virtual unsigned int getSize() const;
 
@@ -242,7 +237,7 @@ protected:
   size_t m_size;
 };
 
-#if 1 // Disabled for now 
+#if 1 // Disabled for now
 class HistoryTypeFile : public HistoryType
 {
 public:
@@ -263,7 +258,7 @@ class HistoryTypeBuffer : public HistoryType
 {
 public:
   HistoryTypeBuffer(unsigned int nbLines);
-  
+
   virtual bool isOn() const;
   virtual unsigned int getSize() const;
 
-- 
cgit v1.2.3

