* text.C: remove big comment on invalid Paragraph pointers as it is

not valid anymore

	* text_funcs.[Ch]: merge with ...

	* text.C: ... this

	* lyxtext.h:
	* text2.C:
	* text3.C: adjust

	* Makefile.am: remove text_funcs.[Ch]


git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@8075 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
André Pönitz 2003-11-11 10:08:35 +00:00
parent 29eaa76094
commit 49cbe77634
8 changed files with 139 additions and 259 deletions

View File

@ -1,3 +1,21 @@
2003-11-11 André Pönitz <poenitz@gmx.net>
* text.C: remove big comment on invalid Paragraph pointers as it is
not valid anymore
2003-11-11 André Pönitz <poenitz@gmx.net>
* text_funcs.[Ch]: merge with ...
* text.C: ... this
* lyxtext.h:
* text2.C:
* text3.C: adjust
* Makefile.am: remove text_funcs.[Ch]
2003-11-11 Alfredo Braunstein <abraunst@libero.it>
* cursor.C (getPos): return absolute cached y coord

View File

@ -262,8 +262,6 @@ lyx_SOURCES = \
text3.C \
textcursor.C \
textcursor.h \
text_funcs.C \
text_funcs.h \
toc.C \
toc.h \
trans.C \

View File

@ -439,6 +439,11 @@ public:
std::string selectionAsString(Buffer const & buffer, bool label) const;
///
double spacing(Paragraph const &) const;
///
void cursorLeftOneWord(LyXCursor &);
///
void cursorRightOneWord(LyXCursor &);
private:
/** Cursor related data.
Later this variable has to be removed. There should be now internal

View File

@ -37,7 +37,6 @@
#include "paragraph_funcs.h"
#include "ParagraphParameters.h"
#include "rowpainter.h"
#include "text_funcs.h"
#include "undo.h"
#include "vspace.h"
#include "WordLangTuple.h"
@ -128,31 +127,6 @@ int LyXText::workWidth() const
}
// This is the comments that some of the warnings below refers to.
// There are some issues in this file and I don't think they are
// really related to the FIX_DOUBLE_SPACE patch. I'd rather think that
// this is a problem that has been here almost from day one and that a
// larger userbase with different access patters triggers the bad
// behaviour. (segfaults.) What I think happen is: In several places
// we store the paragraph in the current cursor and then moves the
// cursor. This movement of the cursor will delete paragraph at the
// old position if it is now empty. This will make the temporary
// pointer to the old cursor paragraph invalid and dangerous to use.
// And is some cases this will trigger a segfault. I have marked some
// of the cases where this happens with a warning, but I am sure there
// are others in this file and in text2.C. There is also a note in
// Delete() that you should read. In Delete I store the paragraph->id
// instead of a pointer to the paragraph. I am pretty sure this faulty
// use of temporary pointers to paragraphs that might have gotten
// invalidated (through a cursor movement) before they are used, are
// the cause of the strange crashes we get reported often.
//
// It is very tiresom to change this code, especially when it is as
// hard to read as it is. Help to fix all the cases where this is done
// would be greately appreciated.
//
// Lgb
int LyXText::singleWidth(ParagraphList::iterator pit, pos_type pos) const
{
if (pos >= pit->size())
@ -1142,16 +1116,12 @@ void LyXText::prepareToPrint(ParagraphList::iterator pit, Row & row) const
}
// important for the screen
// the cursor set functions have a special mechanism. When they
// realize, that you left an empty paragraph, they will delete it.
// They also delete the corresponding row
void LyXText::cursorRightOneWord()
{
::cursorRightOneWord(*this, cursor, ownerParagraphs());
cursorRightOneWord(cursor);
setCursor(cursorPar(), cursor.pos());
}
@ -1161,7 +1131,7 @@ void LyXText::cursorRightOneWord()
void LyXText::cursorLeftOneWord()
{
LyXCursor tmpcursor = cursor;
::cursorLeftOneWord(*this, tmpcursor, ownerParagraphs());
cursorLeftOneWord(tmpcursor);
setCursor(getPar(tmpcursor), tmpcursor.pos());
}
@ -1170,7 +1140,7 @@ void LyXText::selectWord(word_location loc)
{
LyXCursor from = cursor;
LyXCursor to = cursor;
::getWord(*this, from, to, loc, ownerParagraphs());
getWord(from, to, loc);
if (cursor != from)
setCursor(from.par(), from.pos());
if (to == from)
@ -1302,7 +1272,7 @@ void LyXText::changeCase(LyXText::TextCase action)
to = selection.end;
} else {
from = cursor;
::getWord(*this, from, to, lyx::PARTIAL_WORD, ownerParagraphs());
getWord(from, to, lyx::PARTIAL_WORD);
setCursor(to.par(), to.pos() + 1);
}
@ -1703,3 +1673,112 @@ bool LyXText::isFirstRow(ParagraphList::iterator pit, Row const & row) const
{
return row.pos() == 0 && pit == ownerParagraphs().begin();
}
void LyXText::cursorLeftOneWord(LyXCursor & cursor)
{
// treat HFills, floats and Insets as words
ParagraphList::iterator pit = cursorPar();
size_t pos = cursor.pos();
while (pos &&
(pit->isSeparator(pos - 1) ||
pit->isKomma(pos - 1) ||
pit->isNewline(pos - 1)) &&
!(pit->isHfill(pos - 1) ||
pit->isInset(pos - 1)))
--pos;
if (pos &&
(pit->isInset(pos - 1) ||
pit->isHfill(pos - 1))) {
--pos;
} else if (!pos) {
if (pit != ownerParagraphs().begin()) {
--pit;
pos = pit->size();
}
} else { // Here, cur != 0
while (pos > 0 && pit->isWord(pos - 1))
--pos;
}
cursor.par(parOffset(pit));
cursor.pos(pos);
}
void LyXText::cursorRightOneWord(LyXCursor & cursor)
{
// treat floats, HFills and Insets as words
ParagraphList::iterator pit = cursorPar();
pos_type pos = cursor.pos();
if (pos == pit->size() &&
boost::next(pit) != ownerParagraphs().end()) {
++pit;
pos = 0;
} else {
// Skip through initial nonword stuff.
while (pos < pit->size() && !pit->isWord(pos)) {
++pos;
}
// Advance through word.
while (pos < pit->size() && pit->isWord(pos)) {
++pos;
}
}
cursor.par(parOffset(pit));
cursor.pos(pos);
}
void LyXText::getWord(LyXCursor & from, LyXCursor & to, word_location const loc)
{
ParagraphList::iterator from_par = getPar(from);
ParagraphList::iterator to_par = getPar(to);
switch (loc) {
case lyx::WHOLE_WORD_STRICT:
if (from.pos() == 0 || from.pos() == from_par->size()
|| from_par->isSeparator(from.pos())
|| from_par->isKomma(from.pos())
|| from_par->isNewline(from.pos())
|| from_par->isSeparator(from.pos() - 1)
|| from_par->isKomma(from.pos() - 1)
|| from_par->isNewline(from.pos() - 1)) {
to = from;
return;
}
// no break here, we go to the next
case lyx::WHOLE_WORD:
// Move cursor to the beginning, when not already there.
if (from.pos() && !from_par->isSeparator(from.pos() - 1)
&& !(from_par->isKomma(from.pos() - 1)
|| from_par->isNewline(from.pos() - 1)))
cursorLeftOneWord(from);
break;
case lyx::PREVIOUS_WORD:
// always move the cursor to the beginning of previous word
cursorLeftOneWord(from);
break;
case lyx::NEXT_WORD:
lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet"
<< endl;
break;
case lyx::PARTIAL_WORD:
break;
}
to = from;
while (to.pos() < to_par->size()
&& !to_par->isSeparator(to.pos())
&& !to_par->isKomma(to.pos())
&& !to_par->isNewline(to.pos())
&& !to_par->isHfill(to.pos())
&& !to_par->isInset(to.pos()))
{
to.pos(to.pos() + 1);
}
}

View File

@ -399,7 +399,7 @@ void LyXText::setLayout(string const & layout)
bool LyXText::changeDepth(bv_funcs::DEPTH_CHANGE type, bool test_only)
{
ParagraphList::iterator pit = cursorPar();
ParagraphList::iterator end = cursorPar();
ParagraphList::iterator end = pit;
ParagraphList::iterator start = pit;
if (selection.set()) {

View File

@ -33,7 +33,6 @@
#include "paragraph.h"
#include "paragraph_funcs.h"
#include "ParagraphParameters.h"
#include "text_funcs.h"
#include "undo.h"
#include "vspace.h"
@ -703,7 +702,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
case LFUN_WORDSEL: {
LyXCursor cur1 = cursor;
LyXCursor cur2;
::getWord(*this, cur1, cur2, lyx::WHOLE_WORD, ownerParagraphs());
getWord(cur1, cur2, lyx::WHOLE_WORD);
setCursor(cur1.par(), cur1.pos());
clearSelection();
setCursor(cur2.par(), cur2.pos());
@ -1231,8 +1230,7 @@ DispatchResult LyXText::dispatch(FuncRequest const & cmd)
gotoInset(InsetOld::NOTE_CODE, false);
break;
case LFUN_REFERENCE_GOTO:
{
case LFUN_REFERENCE_GOTO: {
vector<InsetOld::Code> tmp;
tmp.push_back(InsetOld::LABEL_CODE);
tmp.push_back(InsetOld::REF_CODE);

View File

@ -1,177 +0,0 @@
/**
* \file text_funcs.C
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Lars Gullik Bjønnes
* \author John Levon
*
* Full author contact details are available in file CREDITS.
*
* This file contains some utility functions for actually mutating
* the text contents of a document
*/
#include <config.h>
#include "text_funcs.h"
#include "debug.h"
#include "lyxcursor.h"
#include "lyxtext.h"
#include "paragraph.h"
#include <boost/next_prior.hpp>
using lyx::pos_type;
using lyx::word_location;
using std::endl;
bool transposeChars(LyXText & text, LyXCursor const & cursor)
{
ParagraphList::iterator tmppit = text.cursorPar();
pos_type tmppos = cursor.pos();
// First decide if it is possible to transpose at all
if (tmppos == 0 || tmppos == tmppit->size())
return false;
if (isDeletedText(*tmppit, tmppos - 1)
|| isDeletedText(*tmppit, tmppos))
return false;
unsigned char c1 = tmppit->getChar(tmppos);
unsigned char c2 = tmppit->getChar(tmppos - 1);
// We should have an implementation that handles insets
// as well, but that will have to come later. (Lgb)
if (c1 == Paragraph::META_INSET || c2 == Paragraph::META_INSET)
return false;
bool const erased = tmppit->erase(tmppos - 1, tmppos + 1);
size_t const ipos = erased ? tmppos - 1 : tmppos + 1;
tmppit->insertChar(ipos, c1);
tmppit->insertChar(ipos + 1, c2);
return true;
}
void cursorLeftOneWord(LyXText & text,
LyXCursor & cursor, ParagraphList const & pars)
{
// treat HFills, floats and Insets as words
ParagraphList::iterator pit = text.cursorPar();
size_t pos = cursor.pos();
while (pos &&
(pit->isSeparator(pos - 1) ||
pit->isKomma(pos - 1) ||
pit->isNewline(pos - 1)) &&
!(pit->isHfill(pos - 1) ||
pit->isInset(pos - 1)))
--pos;
if (pos &&
(pit->isInset(pos - 1) ||
pit->isHfill(pos - 1))) {
--pos;
} else if (!pos) {
// cast only for BSD's g++ 2.95
if (pit != const_cast<ParagraphList &>(pars).begin()) {
--pit;
pos = pit->size();
}
} else { // Here, cur != 0
while (pos > 0 && pit->isWord(pos - 1))
--pos;
}
cursor.par(text.parOffset(pit));
cursor.pos(pos);
}
void cursorRightOneWord(LyXText & text,
LyXCursor & cursor, ParagraphList const & pars)
{
// treat floats, HFills and Insets as words
ParagraphList::iterator pit = text.cursorPar();
pos_type pos = cursor.pos();
// CHECK See comment on top of text.C
// cast only for BSD's g++ 2.95
if (pos == pit->size() &&
boost::next(pit) != const_cast<ParagraphList &>(pars).end()) {
++pit;
pos = 0;
} else {
// Skip through initial nonword stuff.
while (pos < pit->size() && !pit->isWord(pos)) {
++pos;
}
// Advance through word.
while (pos < pit->size() && pit->isWord(pos)) {
++pos;
}
}
cursor.par(text.parOffset(pit));
cursor.pos(pos);
}
// Select current word. This depends on behaviour of
// CursorLeftOneWord(), so it is patched as well.
void getWord(LyXText & text, LyXCursor & from, LyXCursor & to,
word_location const loc, ParagraphList const & pars)
{
ParagraphList::iterator from_par = text.getPar(from);
ParagraphList::iterator to_par = text.getPar(to);
switch (loc) {
case lyx::WHOLE_WORD_STRICT:
if (from.pos() == 0 || from.pos() == from_par->size()
|| from_par->isSeparator(from.pos())
|| from_par->isKomma(from.pos())
|| from_par->isNewline(from.pos())
|| from_par->isSeparator(from.pos() - 1)
|| from_par->isKomma(from.pos() - 1)
|| from_par->isNewline(from.pos() - 1)) {
to = from;
return;
}
// no break here, we go to the next
case lyx::WHOLE_WORD:
// Move cursor to the beginning, when not already there.
if (from.pos() && !from_par->isSeparator(from.pos() - 1)
&& !(from_par->isKomma(from.pos() - 1)
|| from_par->isNewline(from.pos() - 1)))
cursorLeftOneWord(text, from, pars);
break;
case lyx::PREVIOUS_WORD:
// always move the cursor to the beginning of previous word
cursorLeftOneWord(text, from, pars);
break;
case lyx::NEXT_WORD:
lyxerr << "LyXText::getWord: NEXT_WORD not implemented yet"
<< endl;
break;
case lyx::PARTIAL_WORD:
break;
}
to = from;
while (to.pos() < to_par->size()
&& !to_par->isSeparator(to.pos())
&& !to_par->isKomma(to.pos())
&& !to_par->isNewline(to.pos())
&& !to_par->isHfill(to.pos())
&& !to_par->isInset(to.pos()))
{
to.pos(to.pos() + 1);
}
}

View File

@ -1,41 +0,0 @@
// -*- C++ -*-
/**
* \file text_funcs.h
* This file is part of LyX, the document processor.
* Licence details can be found in the file COPYING.
*
* \author Lars Gullik Bjønnes
* \author John Levon
*
* Full author contact details are available in file CREDITS.
*
* This file contains some utility functions for actually mutating
* the text contents of a document
*/
#ifndef TEXT_FUNCS_H
#define TEXT_FUNCS_H
#include "ParagraphList_fwd.h"
#include "support/types.h"
class LyXCursor;
class LyXText;
// do no use LyXText or BufferView here
///
bool transposeChars(LyXText &, LyXCursor const & cursor);
///
void cursorLeftOneWord(LyXText &, LyXCursor &, ParagraphList const &);
///
void cursorRightOneWord(LyXText &, LyXCursor &, ParagraphList const &);
// Select current word. This depends on behaviour of
// CursorLeftOneWord(), so it is patched as well.
void getWord(LyXText &, LyXCursor & from, LyXCursor & to, lyx::word_location const loc,
ParagraphList const & pars);
#endif // TEXT_FUNCS_H