3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Matthias Ettrich
7 * \author Lars Gullik Bjønnes
9 * \author Guillaume Munch
11 * Full author contact details are available in file CREDITS.
17 #include "Paragraph.h"
20 #include "mathed/InsetMath.h"
22 #include "support/debug.h"
23 #include "support/docstring_list.h"
33 bool TexRow::RowEntryList::addEntry(RowEntry const & entry)
36 if (text_entry_ < size())
41 if (size() == 0 || !(operator[](size() - 1) == entry))
42 push_back(RowEntry(entry));
47 TexRow::TextEntry TexRow::RowEntryList::getTextEntry() const
49 if (text_entry_ < size())
50 return operator[](text_entry_).text;
51 return TexRow::text_none;
55 TexRow::RowEntry TexRow::RowEntryList::entry() const
59 return TexRow::row_none;
63 TexRow::TextEntry const TexRow::text_none = { -1, 0 };
64 TexRow::RowEntry const TexRow::row_none = { false, TexRow::text_none };
67 bool TexRow::isNone(TextEntry const & t)
73 bool TexRow::isNone(RowEntry const & r)
75 return !r.is_math && isNone(r.text);
79 void TexRow::reset(bool enable)
82 current_row_ = RowEntryList();
87 TexRow::RowEntry TexRow::textEntry(int id, int pos)
90 entry.is_math = false;
97 TexRow::RowEntry TexRow::mathEntry(uid_type id, idx_type cell)
100 entry.is_math = true;
101 entry.math.cell = cell;
107 bool operator==(TexRow::RowEntry const & entry1,
108 TexRow::RowEntry const & entry2)
110 return entry1.is_math == entry2.is_math
112 ? (entry1.math.id == entry2.math.id
113 && entry1.math.cell == entry2.math.cell)
114 : (entry1.text.id == entry2.text.id
115 && entry1.text.pos == entry2.text.pos));
119 bool TexRow::start(RowEntry entry)
123 return current_row_.addEntry(entry);
127 bool TexRow::start(int id, int pos)
129 return start(textEntry(id,pos));
133 bool TexRow::startMath(uid_type id, idx_type cell)
135 return start(mathEntry(id,cell));
139 void TexRow::newline()
143 rowlist_.push_back(current_row_);
144 current_row_ = RowEntryList();
147 void TexRow::newlines(int num_lines)
151 for (int i = 0; i < num_lines; ++i) {
156 void TexRow::finalize()
164 bool TexRow::getIdFromRow(int row, int & id, int & pos) const
166 TextEntry t = text_none;
168 if (row <= int(rowlist_.size()))
169 while (row > 0 && isNone(t = rowlist_[row - 1].getTextEntry()))
179 TexRow::RowEntry TexRow::rowEntryFromCursorSlice(CursorSlice const & slice)
182 InsetMath * insetMath = slice.asInsetMath();
185 entry.math.id = insetMath->id();
186 entry.math.cell = slice.idx();
187 } else if (slice.text()) {
189 entry.text.id = slice.paragraph().id();
190 entry.text.pos = slice.pos();
199 bool TexRow::sameParOrInsetMath(RowEntry const & entry1,
200 RowEntry const & entry2)
202 return entry1.is_math == entry2.is_math
204 ? (entry1.math.id == entry2.math.id)
205 : (entry1.text.id == entry2.text.id));
209 // assumes it is sameParOrInsetMath
210 int TexRow::comparePos(RowEntry const & entry1,
211 RowEntry const & entry2)
214 return entry2.math.cell - entry1.math.cell;
216 return entry2.text.pos - entry1.text.pos;
220 // An iterator on RowList that goes top-down, left-right
222 // We assume that the end of RowList does not change, which makes things simpler
224 // Records a pair of iterators on the RowEntryList (row_it_, row_end_) and a
225 // pair of iterators on the current row (it_, it_end_).
227 // it_ always points to a valid position unless row_it_ == row_end_.
229 // We could turn this into a proper bidirectional iterator, but we don't need as
232 class TexRow::RowListIterator
235 RowListIterator(RowList::const_iterator r,
236 RowList::const_iterator r_end)
237 : row_it_(r), row_end_(r_end),
238 it_(r == r_end ? RowEntryList::const_iterator() : r->begin()),
239 it_end_(r == r_end ? RowEntryList::const_iterator() : r->end())
246 row_it_(RowList::const_iterator()),
247 row_end_(RowList::const_iterator()),
248 it_(RowEntryList::const_iterator()),
249 it_end_(RowEntryList::const_iterator()) { }
252 RowEntry const & operator*()
258 RowListIterator & operator++()
268 return row_it_ == row_end_;
272 bool operator==(RowListIterator const & a) const
274 return row_it_ == a.row_it_ && ((atEnd() && a.atEnd()) || it_ == a.it_);
278 bool operator!=(RowListIterator const & a) const { return !operator==(a); }
282 RowList::const_iterator const & row() const
287 // ensures that it_ points to a valid value unless row_it_ == row_end_
290 if (row_it_ == row_end_)
292 while (it_ == it_end_) {
294 if (row_it_ != row_end_) {
295 it_ = row_it_->begin();
296 it_end_ = row_it_->end();
302 RowList::const_iterator row_it_;
304 RowList::const_iterator row_end_;
306 RowEntryList::const_iterator it_;
308 RowEntryList::const_iterator it_end_;
312 TexRow::RowListIterator TexRow::begin() const
314 return RowListIterator(rowlist_.begin(), rowlist_.end());
318 TexRow::RowListIterator TexRow::end() const
320 return RowListIterator(rowlist_.end(), rowlist_.end());
324 std::pair<int,int> TexRow::rowFromDocIterator(DocIterator const & dit) const
326 bool beg_found = false;
327 bool end_is_next = true;
329 size_t best_slice = 0;
330 RowEntry best_entry = row_none;
331 size_t const n = dit.depth();
332 // this loop finds a pair (best_beg_row,best_end_row) where best_beg_row is
333 // the first row of the topmost possible CursorSlice, and best_end_row is
334 // the one just before the first row matching the next CursorSlice.
335 RowListIterator const begin = this->begin();//necessary disambiguation
336 RowListIterator const end = this->end();
337 RowListIterator best_beg_entry;
338 //best last entry with same pos as the beg_entry, or first entry with pos
339 //immediately following the beg_entry
340 RowListIterator best_end_entry;
341 RowListIterator it = begin;
342 for (; it != end; ++it) {
343 // Compute the best end row.
345 && (!sameParOrInsetMath(*it, *best_end_entry)
346 || comparePos(*it, *best_end_entry) <= 0)
347 && sameParOrInsetMath(*it, best_entry)) {
348 switch (comparePos(*it, best_entry)) {
350 // Either it is the last one that matches pos...
356 // ...or it is the row preceding the first that matches pos+1
359 if (it.row() != best_end_entry.row())
367 // Compute the best begin row. It is better than the previous one if it
368 // matches either at a deeper level, or at the same level but not
370 for (size_t i = best_slice; i < n; ++i) {
371 TexRow::RowEntry entry_i = rowEntryFromCursorSlice(dit[i]);
372 if (sameParOrInsetMath(*it, entry_i)) {
373 if (comparePos(*it, entry_i) >= 0
376 || !sameParOrInsetMath(*it, *best_beg_entry)
377 || (comparePos(*it, *best_beg_entry) <= 0
378 && comparePos(entry_i, *best_beg_entry) != 0)
385 best_entry = entry_i;
386 best_beg_entry = best_end_entry = it;
394 return std::make_pair(-1,-1);
395 int const best_beg_row = distance(rowlist_.begin(),
396 best_beg_entry.row()) + 1;
397 int const best_end_row = distance(rowlist_.begin(),
398 best_end_entry.row()) + end_offset;
399 return std::make_pair(best_beg_row, best_end_row);
403 std::pair<int,int> TexRow::rowFromCursor(Cursor const & cur) const
405 DocIterator beg = cur.selectionBegin();
406 std::pair<int,int> beg_rows = rowFromDocIterator(beg);
407 if (cur.selection()) {
408 DocIterator end = cur.selectionEnd();
409 if (!cur.selIsMultiCell())
410 end.top().backwardPos();
411 std::pair<int,int> end_rows = rowFromDocIterator(end);
412 return std::make_pair(std::min(beg_rows.first, end_rows.first),
413 std::max(beg_rows.second, end_rows.second));
415 return std::make_pair(beg_rows.first, beg_rows.second);
419 // debugging functions
422 docstring TexRow::asString(RowEntry const & entry)
426 os << "(1," << entry.math.id << "," << entry.math.cell << ")";
428 os << "(0," << entry.text.id << "," << entry.text.pos << ")";
433 ///prepends the texrow to the source given by tex, for debugging purpose
434 void TexRow::prepend(docstring_list & tex) const
436 int const prefix_length = 25;
437 if (tex.size() < rowlist_.size())
438 tex.resize(rowlist_.size());
439 std::vector<RowEntryList>::const_iterator it = rowlist_.begin();
440 std::vector<RowEntryList>::const_iterator const beg = rowlist_.begin();
441 std::vector<RowEntryList>::const_iterator const end = rowlist_.end();
442 for (; it < end; ++it) {
444 std::vector<RowEntry>::const_iterator it2 = it->begin();
445 std::vector<RowEntry>::const_iterator const end2 = it->end();
446 for (; it2 != end2; ++it2)
447 entry += asString(*it2);
448 if (entry.length() < prefix_length)
449 entry = entry + docstring(prefix_length - entry.length(), L' ');
451 tex[i] = entry + " " + tex[i];
457 LyXErr & operator<<(LyXErr & l, TexRow & texrow)
460 for (int i = 0; i < texrow.rows(); i++) {
462 if (texrow.getIdFromRow(i+1,id,pos) && id>0)
463 l << i+1 << ":" << id << ":" << pos << "\n";