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.
18 #include "Paragraph.h"
21 #include "mathed/InsetMath.h"
23 #include "support/debug.h"
24 #include "support/docstring_list.h"
25 #include "support/lassert.h"
37 bool TexRow::RowEntryList::addEntry(RowEntry entry)
40 if (!isNone(text_entry_))
43 text_entry_ = entry.text;
50 void TexRow::RowEntryList::forceAddEntry(RowEntry entry)
52 if (v_.empty() || !(v_.back() == entry))
57 TextEntry TexRow::RowEntryList::getTextEntry() const
59 if (!isNone(text_entry_))
61 return TexRow::text_none;
65 void TexRow::RowEntryList::append(RowEntryList row)
67 if (isNone(text_entry_))
68 text_entry_ = row.text_entry_;
69 move(row.begin(), row.end(), back_inserter(v_));
79 TextEntry const TexRow::text_none = { -1, 0 };
80 RowEntry const TexRow::row_none = { false, { TexRow::text_none } };
84 bool TexRow::isNone(TextEntry t)
91 bool TexRow::isNone(RowEntry r)
93 return !r.is_math && isNone(r.text);
104 TexRow::RowEntryList & TexRow::currentRow()
106 return rowlist_.back();
111 RowEntry TexRow::textEntry(int id, pos_type pos)
114 entry.is_math = false;
115 entry.text.pos = pos;
122 RowEntry TexRow::mathEntry(uid_type id, idx_type cell)
125 entry.is_math = true;
126 entry.math.cell = cell;
132 bool operator==(RowEntry entry1, RowEntry entry2)
134 return entry1.is_math == entry2.is_math
136 ? (entry1.math.id == entry2.math.id
137 && entry1.math.cell == entry2.math.cell)
138 : (entry1.text.id == entry2.text.id
139 && entry1.text.pos == entry2.text.pos));
143 bool TexRow::start(RowEntry entry)
145 return currentRow().addEntry(entry);
149 bool TexRow::start(int id, pos_type pos)
151 return start(textEntry(id,pos));
155 void TexRow::forceStart(int id, pos_type pos)
157 return currentRow().forceAddEntry(textEntry(id,pos));
161 void TexRow::startMath(uid_type id, idx_type cell)
163 start(mathEntry(id,cell));
167 void TexRow::newline()
169 rowlist_.push_back(RowEntryList());
173 void TexRow::newlines(size_t num_lines)
180 void TexRow::append(TexRow other)
182 RowList::iterator it = other.rowlist_.begin();
183 RowList::iterator const end = other.rowlist_.end();
184 LASSERT(it != end, return);
185 currentRow().append(move(*it++));
186 move(it, end, back_inserter(rowlist_));
190 bool TexRow::getIdFromRow(int row, int & id, int & pos) const
192 LYXERR(Debug::LATEX, "getIdFromRow: row " << row << " requested");
193 TextEntry t = text_none;
194 if (row <= int(rowlist_.size()))
195 while (row > 0 && isNone(t = rowlist_[row - 1].getTextEntry()))
203 pair<TextEntry, TextEntry> TexRow::getEntriesFromRow(int const row) const
205 LYXERR(Debug::LATEX, "getEntriesFromRow: row " << row << " requested");
206 // check bounds for row - 1, our target index
208 return {text_none, text_none};
209 size_t const i = static_cast<size_t>(row - 1);
210 if (i >= rowlist_.size())
211 return {text_none, text_none};
212 // find the start entry
214 while (j > 0 && isNone(rowlist_[j].getTextEntry()))
216 TextEntry start = rowlist_[j].getTextEntry();
217 // find the end entry
219 while (j < rowlist_.size() && isNone(rowlist_[j].getTextEntry()))
222 (j < rowlist_.size()) ? rowlist_[j].getTextEntry() : text_none;
223 // The following occurs for a displayed math inset for instance (for good
224 // reasons involving subtleties of the algorithm in getRowFromDocIterator).
225 // We want this inset selected.
226 if (start.id == end.id && start.pos == end.pos)
232 pair<DocIterator, DocIterator> TexRow::getDocIteratorFromRow(
234 Buffer const & buf) const
236 TextEntry start, end;
237 tie(start,end) = getEntriesFromRow(row);
239 "getDocIteratorFromRow: for row " << row << ", TexRow has found "
240 "start (id=" << start.id << ",pos=" << start.pos << "), "
241 "end (id=" << end.id << ",pos=" << end.pos << ")");
243 DocIterator dit_start = buf.getParFromID(start.id);
245 dit_start.pos() = min(start.pos, dit_start.lastpos());
247 DocIterator dit_end = buf.getParFromID(end.id);
249 dit_end.pos() = min(end.pos, dit_end.lastpos());
250 // So far dit_end belongs to the next row. Step backwards.
251 if (!dit_end.top().at_cell_begin()) {
252 CursorSlice end_top = dit_end.top();
253 end_top.backwardPos();
254 if (dit_start && end_top != dit_start.top())
255 dit_end.top() = end_top;
257 dit_end.boundary(true);
259 return {dit_start, dit_end};
264 RowEntry TexRow::rowEntryFromCursorSlice(CursorSlice const & slice)
267 InsetMath * insetMath = slice.asInsetMath();
270 entry.math.id = insetMath->id();
271 entry.math.cell = slice.idx();
272 } else if (slice.text()) {
274 entry.text.id = slice.paragraph().id();
275 entry.text.pos = slice.pos();
277 LASSERT(false, return row_none);
283 bool TexRow::sameParOrInsetMath(RowEntry entry1, RowEntry entry2)
285 return entry1.is_math == entry2.is_math
287 ? (entry1.math.id == entry2.math.id)
288 : (entry1.text.id == entry2.text.id));
293 int TexRow::comparePos(RowEntry entry1, RowEntry entry2)
295 // assume it is sameParOrInsetMath
297 return entry2.math.cell - entry1.math.cell;
299 return entry2.text.pos - entry1.text.pos;
303 // An iterator on RowList that goes top-down, left-right
305 // We assume that the end of RowList does not change, which makes things simpler
307 // Records a pair of iterators on the RowEntryList (row_it_, row_end_) and a
308 // pair of iterators on the current row (it_, it_end_).
310 // it_ always points to a valid position unless row_it_ == row_end_.
312 // We could turn this into a proper bidirectional iterator, but we don't need as
315 class TexRow::RowListIterator
318 RowListIterator(RowList::const_iterator r,
319 RowList::const_iterator r_end)
320 : row_it_(r), row_end_(r_end),
321 it_(r == r_end ? RowEntryList::const_iterator() : r->begin()),
322 it_end_(r == r_end ? RowEntryList::const_iterator() : r->end())
329 row_it_(RowList::const_iterator()),
330 row_end_(RowList::const_iterator()),
331 it_(RowEntryList::const_iterator()),
332 it_end_(RowEntryList::const_iterator()) { }
335 RowEntry const & operator*()
341 RowListIterator & operator++()
351 return row_it_ == row_end_;
355 bool operator==(RowListIterator const & a) const
357 return row_it_ == a.row_it_ && ((atEnd() && a.atEnd()) || it_ == a.it_);
361 bool operator!=(RowListIterator const & a) const { return !operator==(a); }
365 RowList::const_iterator const & row() const
370 // ensures that it_ points to a valid value unless row_it_ == row_end_
373 if (row_it_ == row_end_)
375 while (it_ == it_end_) {
377 if (row_it_ != row_end_) {
378 it_ = row_it_->begin();
379 it_end_ = row_it_->end();
385 RowList::const_iterator row_it_;
387 RowList::const_iterator row_end_;
389 RowEntryList::const_iterator it_;
391 RowEntryList::const_iterator it_end_;
395 TexRow::RowListIterator TexRow::begin() const
397 return RowListIterator(rowlist_.begin(), rowlist_.end());
401 TexRow::RowListIterator TexRow::end() const
403 return RowListIterator(rowlist_.end(), rowlist_.end());
407 pair<int,int> TexRow::rowFromDocIterator(DocIterator const & dit) const
409 bool beg_found = false;
410 bool end_is_next = true;
412 size_t best_slice = 0;
413 RowEntry best_entry = row_none;
414 size_t const n = dit.depth();
415 // this loop finds a pair (best_beg_row,best_end_row) where best_beg_row is
416 // the first row of the topmost possible CursorSlice, and best_end_row is
417 // the one just before the first row matching the next CursorSlice.
418 RowListIterator const begin = this->begin();//necessary disambiguation
419 RowListIterator const end = this->end();
420 RowListIterator best_beg_entry;
421 //best last entry with same pos as the beg_entry, or first entry with pos
422 //immediately following the beg_entry
423 RowListIterator best_end_entry;
424 RowListIterator it = begin;
425 for (; it != end; ++it) {
426 // Compute the best end row.
428 && (!sameParOrInsetMath(*it, *best_end_entry)
429 || comparePos(*it, *best_end_entry) <= 0)
430 && sameParOrInsetMath(*it, best_entry)) {
431 switch (comparePos(*it, best_entry)) {
433 // Either it is the last one that matches pos...
439 // ...or it is the row preceding the first that matches pos+1
442 if (it.row() != best_end_entry.row())
450 // Compute the best begin row. It is better than the previous one if it
451 // matches either at a deeper level, or at the same level but not
453 for (size_t i = best_slice; i < n; ++i) {
454 RowEntry entry_i = rowEntryFromCursorSlice(dit[i]);
455 if (sameParOrInsetMath(*it, entry_i)) {
456 if (comparePos(*it, entry_i) >= 0
459 || !sameParOrInsetMath(*it, *best_beg_entry)
460 || (comparePos(*it, *best_beg_entry) <= 0
461 && comparePos(entry_i, *best_beg_entry) != 0)
468 best_entry = entry_i;
469 best_beg_entry = best_end_entry = it;
477 return make_pair(-1,-1);
478 int const best_beg_row = distance(rowlist_.begin(),
479 best_beg_entry.row()) + 1;
480 int const best_end_row = distance(rowlist_.begin(),
481 best_end_entry.row()) + end_offset;
482 return make_pair(best_beg_row, best_end_row);
486 pair<int,int> TexRow::rowFromCursor(Cursor const & cur) const
488 DocIterator beg = cur.selectionBegin();
489 pair<int,int> beg_rows = rowFromDocIterator(beg);
490 if (cur.selection()) {
491 DocIterator end = cur.selectionEnd();
492 if (!cur.selIsMultiCell() && !end.top().at_cell_begin())
493 end.top().backwardPos();
494 pair<int,int> end_rows = rowFromDocIterator(end);
495 return make_pair(min(beg_rows.first, end_rows.first),
496 max(beg_rows.second, end_rows.second));
498 return make_pair(beg_rows.first, beg_rows.second);
502 int TexRow::rows() const
504 return rowlist_.size();
508 // debugging functions
511 docstring TexRow::asString(RowEntry entry)
515 os << "(1," << entry.math.id << "," << entry.math.cell << ")";
517 os << "(0," << entry.text.id << "," << entry.text.pos << ")";
522 ///prepends the texrow to the source given by tex, for debugging purpose
523 void TexRow::prepend(docstring_list & tex) const
525 size_type const prefix_length = 25;
526 if (tex.size() < rowlist_.size())
527 tex.resize(rowlist_.size());
528 auto it = rowlist_.cbegin();
529 auto const beg = rowlist_.cbegin();
530 auto const end = rowlist_.cend();
531 for (; it < end; ++it) {
533 for (RowEntry const & e : *it)
534 entry += asString(e);
535 if (entry.length() < prefix_length)
536 entry = entry + docstring(prefix_length - entry.length(), ' ');
537 ptrdiff_t i = it - beg;
538 tex[i] = entry + " " + tex[i];
544 LyXErr & operator<<(LyXErr & l, TexRow const & texrow)
547 for (int i = 0; i < texrow.rows(); i++) {
549 if (texrow.getIdFromRow(i+1,id,pos) && id>0)
550 l << i+1 << ":" << id << ":" << pos << "\n";