[Bug 82662] new feature: (global) cursor position history

andreas braendle abraendle at gmx.de
Fri Jun 4 00:58:17 UTC 2004


------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.
      
http://bugs.kde.org/show_bug.cgi?id=82662      




------- Additional Comments From abraendle gmx de  2004-06-01 22:20 -------

// CURSHIST.CPP

#include "curshist.h"
#include <string.h>


CGlobalCursorPosHistory::CGlobalCursorPosHistory()
{
   m_hist = new CursHistEntry[GCPH_HISTSZ+2];  // +2 savety (+1 needed)
   num=0; curr=0;
   
   m_stack = new CursHistEntry[GCPH_HSTACK];
   num_s=0;               
}


CGlobalCursorPosHistory::~CGlobalCursorPosHistory()
{
   delete[] m_hist;
}

void CGlobalCursorPosHistory::CursorChanged(QEditorWidget* FileId, int line, int column)
{
 int newidx;
 int currline=m_hist[curr].LineCol/256;
 if (line>currline-GCPH_BLOCK && line<currline+GCPH_BLOCK)  // replace Entry -- if we are withing same BLOCK
    newidx = curr;
 else {
   // make room for new entry
   newidx=curr+1;
   memcpy((void*)&m_hist[newidx+1],(void*)&m_hist[newidx], (num-newidx-1)* sizeof(CursHistEntry));
   // remark: might lead to GCPH_HISTSZ+1 entries, but this is ok!
   num++;

   // if too much entries, delete either the first or the last
   if (GCPH_HISTSZ>num) {

     if (newidx>(3*GCPH_HISTSZ)/5)  // 60 % for history, 40 % for future
     {
       memcpy((void*)&m_hist[0],(void*)&m_hist[1], GCPH_HISTSZ*sizeof(CursHistEntry)); // delete first entry
       newidx--;
     } //else: do nothing           // = delete last entry

     num=GCPH_HISTSZ;
   }
 }

 if (line>0xFFFFFF) line=0xFFFFFF;
 if (line<0) line=0;
 if (column>0xFF) column=0xFF;
 if (column<0) column=0;

 m_hist[newidx].FileId=FileId;
 m_hist[newidx].LineCol=256*line+column; 
}

void CGlobalCursorPosHistory::FileClosed(QEditorWidget* FileId)
{
 for (int i=0; i<num; i++)
 {
   if (m_hist[i].FileId==FileId) {
     memcpy((void*)&m_hist[i],(void*)&m_hist[i+1], (num-i-1)*sizeof(CursHistEntry));
     num--;
     if (curr>i) curr--;
     if (curr>num-1) curr--;
   }
 }
}


/* 
The following functions return:
 the editor view to go to
 line+column (returned by reference)

editor view=NULL always means don't change cursor position!
*/
QEditorWidget* CGlobalCursorPosHistory::NavigateBack(int &line, int &column)
{
if (num<=0) return NULL;  // NULL: No History Entries exist yet, Navigation not possible

if (curr>=1) curr--;

line  =m_hist[curr].LineCol / 256;
column=m_hist[curr].LineCol % 256;
return m_hist[curr].FileId;
}


QEditorWidget* CGlobalCursorPosHistory::NavigateForward(int &line, int &column)
{
if (num<=0) return NULL;  // NULL: No History Entries exist yet

if (curr<num-1) curr++;

line  =m_hist[curr].LineCol / 256;
column=m_hist[curr].LineCol % 256;
return m_hist[curr].FileId;
}


QEditorWidget* CGlobalCursorPosHistory::NavigateLeave(int &line, int &column) //PUSH
{
if (num<=0) return NULL;  // NULL: No History Entries exist yet, Navigation not possible

// PUSH curr ON stack
// make space for one entry at 0
memcpy((void*)&m_stack[1],(void*)&m_stack[0], (GCPH_HSTACK-1)*sizeof(CursHistEntry)); 

m_stack[0].LineCol=m_hist[curr].LineCol;
m_stack[0].FileId =m_hist[curr].FileId;
num_s++; if (num_s>GCPH_HSTACK) num_s=GCPH_HSTACK;
num--;

return NavigateBack(line,column); // new position is curr-1
}


QEditorWidget* CGlobalCursorPosHistory::NavigatePop(int &line, int &column) //POP
{
if (num_s<=0) return NULL;  // NULL: stack empty, Navigation not possible

// get topmost stack entry (=m_stack[0])
line            =m_stack[0].LineCol / 256;
column          =m_stack[0].LineCol % 256;
QEditorWidget* p=m_stack[0].FileId;

// insert the entry in history list
CursorChanged(p,line,column); 

// delete topmost stack entry (=m_stack[0])
memcpy((void*)&m_stack[0],(void*)&m_stack[1], (GCPH_HSTACK-1)*sizeof(CursHistEntry)); 
num_s--;

return p;
}




More information about the KDevelop-devel mailing list