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())
46 void TexRow::RowEntryList::forceAddEntry(RowEntry const & entry)
48 if (size() == 0 || !(operator[](size() - 1) == entry))
49 push_back(RowEntry(entry));
53 TexRow::TextEntry TexRow::RowEntryList::getTextEntry() const
55 if (text_entry_ < size())
56 return operator[](text_entry_).text;
57 return TexRow::text_none;
61 TexRow::RowEntry TexRow::RowEntryList::entry() const
65 return TexRow::row_none;
69 void TexRow::RowEntryList::append(RowEntryList const & row)
71 if (text_entry_ >= size())
72 text_entry_ = row.text_entry_ + size();
73 insert(end(), row.begin(), row.end());
77 TexRow::TextEntry const TexRow::text_none = { -1, 0 };
78 TexRow::RowEntry const TexRow::row_none = { false, { TexRow::text_none } };
81 bool TexRow::isNone(TextEntry const & t)
87 bool TexRow::isNone(RowEntry const & r)
89 return !r.is_math && isNone(r.text);
93 void TexRow::reset(bool enable)
96 current_row_ = RowEntryList();
101 TexRow::RowEntry TexRow::textEntry(int id, int pos)
104 entry.is_math = false;
105 entry.text.pos = pos;
111 TexRow::RowEntry TexRow::mathEntry(uid_type id, idx_type cell)
114 entry.is_math = true;
115 entry.math.cell = cell;
121 bool operator==(TexRow::RowEntry const & entry1,
122 TexRow::RowEntry const & entry2)
124 return entry1.is_math == entry2.is_math
126 ? (entry1.math.id == entry2.math.id
127 && entry1.math.cell == entry2.math.cell)
128 : (entry1.text.id == entry2.text.id
129 && entry1.text.pos == entry2.text.pos));
133 bool TexRow::start(RowEntry entry)
137 return current_row_.addEntry(entry);
141 bool TexRow::start(int id, int pos)
143 return start(textEntry(id,pos));
147 void TexRow::forceStart(int id, int pos)
151 return current_row_.forceAddEntry(textEntry(id,pos));
155 void TexRow::startMath(uid_type id, idx_type cell)
157 start(mathEntry(id,cell));
161 void TexRow::newline()
165 rowlist_.push_back(current_row_);
166 current_row_ = RowEntryList();
169 void TexRow::newlines(int num_lines)
173 for (int i = 0; i < num_lines; ++i) {
178 void TexRow::finalize()
186 void TexRow::append(TexRow const & texrow)
188 if (!enabled_ || !texrow.enabled_)
190 RowList::const_iterator it = texrow.rowlist_.begin();
191 RowList::const_iterator const end = texrow.rowlist_.end();
193 current_row_.append(texrow.current_row_);
195 current_row_.append(*it++);
196 rowlist_.push_back(current_row_);
197 rowlist_.insert(rowlist_.end(), it, end);
198 current_row_ = texrow.current_row_;
204 bool TexRow::getIdFromRow(int row, int & id, int & pos) const
206 TextEntry t = text_none;
207 if (row <= int(rowlist_.size()))
208 while (row > 0 && isNone(t = rowlist_[row - 1].getTextEntry()))
216 TexRow::RowEntry TexRow::rowEntryFromCursorSlice(CursorSlice const & slice)
219 InsetMath * insetMath = slice.asInsetMath();
222 entry.math.id = insetMath->id();
223 entry.math.cell = slice.idx();
224 } else if (slice.text()) {
226 entry.text.id = slice.paragraph().id();
227 entry.text.pos = slice.pos();
236 bool TexRow::sameParOrInsetMath(RowEntry const & entry1,
237 RowEntry const & entry2)
239 return entry1.is_math == entry2.is_math
241 ? (entry1.math.id == entry2.math.id)
242 : (entry1.text.id == entry2.text.id));
246 // assumes it is sameParOrInsetMath
247 int TexRow::comparePos(RowEntry const & entry1,
248 RowEntry const & entry2)
251 return entry2.math.cell - entry1.math.cell;
253 return entry2.text.pos - entry1.text.pos;
257 // An iterator on RowList that goes top-down, left-right
259 // We assume that the end of RowList does not change, which makes things simpler
261 // Records a pair of iterators on the RowEntryList (row_it_, row_end_) and a
262 // pair of iterators on the current row (it_, it_end_).
264 // it_ always points to a valid position unless row_it_ == row_end_.
266 // We could turn this into a proper bidirectional iterator, but we don't need as
269 class TexRow::RowListIterator
272 RowListIterator(RowList::const_iterator r,
273 RowList::const_iterator r_end)
274 : row_it_(r), row_end_(r_end),
275 it_(r == r_end ? RowEntryList::const_iterator() : r->begin()),
276 it_end_(r == r_end ? RowEntryList::const_iterator() : r->end())
283 row_it_(RowList::const_iterator()),
284 row_end_(RowList::const_iterator()),
285 it_(RowEntryList::const_iterator()),
286 it_end_(RowEntryList::const_iterator()) { }
289 RowEntry const & operator*()
295 RowListIterator & operator++()
305 return row_it_ == row_end_;
309 bool operator==(RowListIterator const & a) const
311 return row_it_ == a.row_it_ && ((atEnd() && a.atEnd()) || it_ == a.it_);
315 bool operator!=(RowListIterator const & a) const { return !operator==(a); }
319 RowList::const_iterator const & row() const
324 // ensures that it_ points to a valid value unless row_it_ == row_end_
327 if (row_it_ == row_end_)
329 while (it_ == it_end_) {
331 if (row_it_ != row_end_) {
332 it_ = row_it_->begin();
333 it_end_ = row_it_->end();
339 RowList::const_iterator row_it_;
341 RowList::const_iterator row_end_;
343 RowEntryList::const_iterator it_;
345 RowEntryList::const_iterator it_end_;
349 TexRow::RowListIterator TexRow::begin() const
351 return RowListIterator(rowlist_.begin(), rowlist_.end());
355 TexRow::RowListIterator TexRow::end() const
357 return RowListIterator(rowlist_.end(), rowlist_.end());
361 std::pair<int,int> TexRow::rowFromDocIterator(DocIterator const & dit) const
363 bool beg_found = false;
364 bool end_is_next = true;
366 size_t best_slice = 0;
367 RowEntry best_entry = row_none;
368 size_t const n = dit.depth();
369 // this loop finds a pair (best_beg_row,best_end_row) where best_beg_row is
370 // the first row of the topmost possible CursorSlice, and best_end_row is
371 // the one just before the first row matching the next CursorSlice.
372 RowListIterator const begin = this->begin();//necessary disambiguation
373 RowListIterator const end = this->end();
374 RowListIterator best_beg_entry;
375 //best last entry with same pos as the beg_entry, or first entry with pos
376 //immediately following the beg_entry
377 RowListIterator best_end_entry;
378 RowListIterator it = begin;
379 for (; it != end; ++it) {
380 // Compute the best end row.
382 && (!sameParOrInsetMath(*it, *best_end_entry)
383 || comparePos(*it, *best_end_entry) <= 0)
384 && sameParOrInsetMath(*it, best_entry)) {
385 switch (comparePos(*it, best_entry)) {
387 // Either it is the last one that matches pos...
393 // ...or it is the row preceding the first that matches pos+1
396 if (it.row() != best_end_entry.row())
404 // Compute the best begin row. It is better than the previous one if it
405 // matches either at a deeper level, or at the same level but not
407 for (size_t i = best_slice; i < n; ++i) {
408 TexRow::RowEntry entry_i = rowEntryFromCursorSlice(dit[i]);
409 if (sameParOrInsetMath(*it, entry_i)) {
410 if (comparePos(*it, entry_i) >= 0
413 || !sameParOrInsetMath(*it, *best_beg_entry)
414 || (comparePos(*it, *best_beg_entry) <= 0
415 && comparePos(entry_i, *best_beg_entry) != 0)
422 best_entry = entry_i;
423 best_beg_entry = best_end_entry = it;
431 return std::make_pair(-1,-1);
432 int const best_beg_row = distance(rowlist_.begin(),
433 best_beg_entry.row()) + 1;
434 int const best_end_row = distance(rowlist_.begin(),
435 best_end_entry.row()) + end_offset;
436 return std::make_pair(best_beg_row, best_end_row);
440 std::pair<int,int> TexRow::rowFromCursor(Cursor const & cur) const
442 DocIterator beg = cur.selectionBegin();
443 std::pair<int,int> beg_rows = rowFromDocIterator(beg);
444 if (cur.selection()) {
445 DocIterator end = cur.selectionEnd();
446 if (!cur.selIsMultiCell()
447 // backwardPos asserts without the following test, IMO it's not my
448 // duty to check this.
449 && (end.top().pit() != 0
450 || end.top().idx() != 0
451 || end.top().pos() != 0))
452 end.top().backwardPos();
453 std::pair<int,int> end_rows = rowFromDocIterator(end);
454 return std::make_pair(std::min(beg_rows.first, end_rows.first),
455 std::max(beg_rows.second, end_rows.second));
457 return std::make_pair(beg_rows.first, beg_rows.second);
461 // debugging functions
464 docstring TexRow::asString(RowEntry const & entry)
468 os << "(1," << entry.math.id << "," << entry.math.cell << ")";
470 os << "(0," << entry.text.id << "," << entry.text.pos << ")";
475 ///prepends the texrow to the source given by tex, for debugging purpose
476 void TexRow::prepend(docstring_list & tex) const
478 size_type const prefix_length = 25;
479 if (tex.size() < rowlist_.size())
480 tex.resize(rowlist_.size());
481 std::vector<RowEntryList>::const_iterator it = rowlist_.begin();
482 std::vector<RowEntryList>::const_iterator const beg = rowlist_.begin();
483 std::vector<RowEntryList>::const_iterator const end = rowlist_.end();
484 for (; it < end; ++it) {
486 std::vector<RowEntry>::const_iterator it2 = it->begin();
487 std::vector<RowEntry>::const_iterator const end2 = it->end();
488 for (; it2 != end2; ++it2)
489 entry += asString(*it2);
490 if (entry.length() < prefix_length)
491 entry = entry + docstring(prefix_length - entry.length(), L' ');
492 ptrdiff_t i = it - beg;
493 tex[i] = entry + " " + tex[i];
499 LyXErr & operator<<(LyXErr & l, TexRow & texrow)
502 for (int i = 0; i < texrow.rows(); i++) {
504 if (texrow.getIdFromRow(i+1,id,pos) && id>0)
505 l << i+1 << ":" << id << ":" << pos << "\n";