2002-05-30 03:37:24 +00:00
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
* \file TexRow.cpp
|
2003-08-23 00:17:00 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-03-21 17:27:08 +00:00
|
|
|
*
|
2002-05-30 03:37:24 +00:00
|
|
|
* \author Matthias Ettrich
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author Lars Gullik Bjønnes
|
2003-08-23 00:17:00 +00:00
|
|
|
* \author John Levon
|
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-05-30 03:37:24 +00:00
|
|
|
*/
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2015-09-15 04:56:01 +00:00
|
|
|
#include "DocIterator.h"
|
|
|
|
#include "Paragraph.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "TexRow.h"
|
2008-02-18 07:14:42 +00:00
|
|
|
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/debug.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2003-09-05 22:17:02 +00:00
|
|
|
#include <algorithm>
|
2003-05-07 21:27:29 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
2002-12-01 22:59:25 +00:00
|
|
|
|
2015-09-15 04:56:01 +00:00
|
|
|
void TexRow::reset(bool enable)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-11-22 16:19:48 +00:00
|
|
|
rowlist.clear();
|
2003-05-07 21:27:29 +00:00
|
|
|
lastid = -1;
|
1999-09-27 18:44:28 +00:00
|
|
|
lastpos = -1;
|
2015-09-15 04:56:01 +00:00
|
|
|
enabled_ = enable;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-11-22 16:19:48 +00:00
|
|
|
|
2003-05-07 21:27:29 +00:00
|
|
|
void TexRow::start(int id, int pos)
|
1999-11-22 16:19:48 +00:00
|
|
|
{
|
2015-09-15 04:56:01 +00:00
|
|
|
if (!enabled_ || started)
|
2011-03-10 04:05:49 +00:00
|
|
|
return;
|
2003-05-07 21:27:29 +00:00
|
|
|
lastid = id;
|
1999-09-27 18:44:28 +00:00
|
|
|
lastpos = pos;
|
2011-03-10 04:05:49 +00:00
|
|
|
started = true;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-11-22 16:19:48 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
void TexRow::newline()
|
|
|
|
{
|
2015-09-15 04:56:01 +00:00
|
|
|
if (!enabled_)
|
|
|
|
return;
|
|
|
|
RowList::value_type tmp(lastid, lastpos);
|
1999-11-22 16:19:48 +00:00
|
|
|
rowlist.push_back(tmp);
|
2011-03-10 04:05:49 +00:00
|
|
|
started = false;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
2010-02-08 17:39:55 +00:00
|
|
|
void TexRow::newlines(int num_lines)
|
|
|
|
{
|
2015-09-15 04:56:01 +00:00
|
|
|
if (!enabled_)
|
|
|
|
return;
|
2010-02-08 17:39:55 +00:00
|
|
|
for (int i = 0; i < num_lines; ++i) {
|
|
|
|
newline();
|
|
|
|
}
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2015-09-15 04:56:01 +00:00
|
|
|
void TexRow::finalize()
|
|
|
|
{
|
|
|
|
if (!enabled_)
|
|
|
|
return;
|
|
|
|
newline();
|
|
|
|
}
|
|
|
|
|
2000-08-05 05:17:18 +00:00
|
|
|
bool TexRow::getIdFromRow(int row, int & id, int & pos) const
|
|
|
|
{
|
2007-08-14 09:44:38 +00:00
|
|
|
if (row <= 0 || row > int(rowlist.size())) {
|
2007-08-13 14:04:35 +00:00
|
|
|
id = -1;
|
|
|
|
pos = 0;
|
|
|
|
return false;
|
2000-08-05 05:17:18 +00:00
|
|
|
}
|
2007-08-13 14:04:35 +00:00
|
|
|
|
|
|
|
id = rowlist[row - 1].id();
|
|
|
|
pos = rowlist[row - 1].pos();
|
|
|
|
return true;
|
2000-08-05 05:17:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-15 04:56:01 +00:00
|
|
|
std::pair<int,int> TexRow::rowFromDocIterator(DocIterator const & dit) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2015-09-15 04:56:01 +00:00
|
|
|
bool found = false;
|
|
|
|
size_t best_slice = 0;
|
|
|
|
size_t const n = dit.depth();
|
|
|
|
// this loop finds the last row of the topmost possible CursorSlice
|
|
|
|
RowList::const_iterator best_beg_row = rowlist.begin();
|
|
|
|
RowList::const_iterator best_end_row = rowlist.begin();
|
2008-10-18 16:07:04 +00:00
|
|
|
RowList::const_iterator it = rowlist.begin();
|
|
|
|
RowList::const_iterator const end = rowlist.end();
|
|
|
|
for (; it != end; ++it) {
|
2015-09-15 04:56:01 +00:00
|
|
|
if (found) {
|
|
|
|
// Compute the best end row. It is the one that matches pos+1.
|
|
|
|
CursorSlice const & best = dit[best_slice];
|
|
|
|
if (best.text()
|
|
|
|
&& it->id() == best.paragraph().id()
|
|
|
|
&& it->pos() == best.pos() + 1
|
|
|
|
&& (best_end_row->id() != it->id()
|
|
|
|
|| best_end_row->pos() < it->pos()))
|
|
|
|
best_end_row = it;
|
|
|
|
}
|
|
|
|
for (size_t i = best_slice; i < n && dit[i].text(); ++i) {
|
|
|
|
int const id = dit[i].paragraph().id();
|
|
|
|
if (it->id() == id) {
|
|
|
|
if (it->pos() <= dit[i].pos()
|
|
|
|
&& (best_beg_row->id() != id
|
|
|
|
|| it->pos() > best_beg_row->pos())) {
|
|
|
|
found = true;
|
|
|
|
best_slice = i;
|
|
|
|
best_beg_row = best_end_row = it;
|
|
|
|
}
|
|
|
|
//found CursorSlice
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2007-08-13 14:04:35 +00:00
|
|
|
}
|
2015-09-15 04:56:01 +00:00
|
|
|
if (!found)
|
|
|
|
return std::make_pair(-1,-1);
|
|
|
|
int const beg_i = distance(rowlist.begin(), best_beg_row) + 1;
|
|
|
|
// remove one to the end
|
|
|
|
int const end_i = std::max(beg_i,
|
|
|
|
(int)distance(rowlist.begin(), best_end_row));
|
|
|
|
return std::make_pair(beg_i,end_i);
|
2002-03-21 17:27:08 +00:00
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
2015-09-15 04:56:01 +00:00
|
|
|
LyXErr & operator<<(LyXErr & l, TexRow & texrow)
|
|
|
|
{
|
|
|
|
if (l.enabled()) {
|
|
|
|
for (int i = 0; i < texrow.rows(); i++) {
|
|
|
|
int id,pos;
|
|
|
|
if (texrow.getIdFromRow(i+1,id,pos) && id>0)
|
|
|
|
l << i+1 << ":" << id << ":" << pos << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|