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 "FuncRequest.h"
19 #include "Paragraph.h"
22 #include "mathed/InsetMath.h"
24 #include "support/convert.h"
25 #include "support/debug.h"
26 #include "support/docstring_list.h"
27 #include "support/lassert.h"
39 TexString::TexString(docstring s)
40 : str(move(s)), texrow(TexRow())
42 texrow.setRows(1 + count(str.begin(), str.end(), '\n'));
46 TexString::TexString(docstring s, TexRow t)
47 : str(move(s)), texrow(move(t))
53 void TexString::validate()
55 size_t lines = 1 + count(str.begin(), str.end(), '\n');
56 size_t rows = texrow.rows();
57 bool valid = lines == rows;
59 LYXERR0("TexString has " << lines << " lines but " << rows << " rows." );
60 // Assert in devel mode. This is important to catch bugs early, otherwise
61 // they might be hard to notice and find. Recover gracefully in release
63 LASSERT(valid, texrow.setRows(lines));
67 bool TexRow::RowEntryList::addEntry(RowEntry entry)
70 if (isNone(text_entry_))
71 text_entry_ = entry.text;
72 else if (!v_.empty() && TexRow::sameParOrInsetMath(v_.back(), entry))
80 void TexRow::RowEntryList::forceAddEntry(RowEntry entry)
82 if (v_.empty() || !(v_.back() == entry))
87 TextEntry TexRow::RowEntryList::getTextEntry() const
89 if (!isNone(text_entry_))
91 return TexRow::text_none;
95 void TexRow::RowEntryList::append(RowEntryList row)
97 if (isNone(text_entry_))
98 text_entry_ = row.text_entry_;
99 move(row.begin(), row.end(), back_inserter(v_));
109 TextEntry const TexRow::text_none = { -1, 0 };
110 RowEntry const TexRow::row_none = { false, { TexRow::text_none } };
114 bool TexRow::isNone(TextEntry t)
121 bool TexRow::isNone(RowEntry r)
123 return !r.is_math && isNone(r.text);
134 TexRow::RowEntryList & TexRow::currentRow()
136 return rowlist_.back();
141 RowEntry TexRow::textEntry(int id, pos_type pos)
144 entry.is_math = false;
145 entry.text.pos = pos;
152 RowEntry TexRow::mathEntry(uid_type id, idx_type cell)
155 entry.is_math = true;
156 entry.math.cell = cell;
162 bool operator==(RowEntry entry1, RowEntry entry2)
164 return entry1.is_math == entry2.is_math
166 ? (entry1.math.id == entry2.math.id
167 && entry1.math.cell == entry2.math.cell)
168 : (entry1.text.id == entry2.text.id
169 && entry1.text.pos == entry2.text.pos));
173 bool TexRow::start(RowEntry entry)
175 return currentRow().addEntry(entry);
179 bool TexRow::start(int id, pos_type pos)
181 return start(textEntry(id,pos));
185 void TexRow::forceStart(int id, pos_type pos)
187 return currentRow().forceAddEntry(textEntry(id,pos));
191 void TexRow::startMath(uid_type id, idx_type cell)
193 start(mathEntry(id,cell));
197 void TexRow::newline()
199 rowlist_.push_back(RowEntryList());
203 void TexRow::newlines(size_t num_lines)
210 void TexRow::append(TexRow other)
212 RowList::iterator it = other.rowlist_.begin();
213 RowList::iterator const end = other.rowlist_.end();
214 LASSERT(it != end, return);
215 currentRow().append(move(*it++));
216 move(it, end, back_inserter(rowlist_));
220 bool TexRow::getIdFromRow(int row, int & id, int & pos) const
222 LYXERR(Debug::LATEX, "getIdFromRow: row " << row << " requested");
223 TextEntry t = text_none;
224 if (row <= int(rowlist_.size()))
225 while (row > 0 && isNone(t = rowlist_[row - 1].getTextEntry()))
233 pair<TextEntry, TextEntry> TexRow::getEntriesFromRow(int const row) const
235 LYXERR(Debug::LATEX, "getEntriesFromRow: row " << row << " requested");
236 // check bounds for row - 1, our target index
238 return {text_none, text_none};
239 size_t const i = static_cast<size_t>(row - 1);
240 if (i >= rowlist_.size())
241 return {text_none, text_none};
242 // find the start entry
244 while (j > 0 && isNone(rowlist_[j].getTextEntry()))
246 TextEntry start = rowlist_[j].getTextEntry();
247 // find the end entry
249 while (j < rowlist_.size() && isNone(rowlist_[j].getTextEntry()))
252 (j < rowlist_.size()) ? rowlist_[j].getTextEntry() : text_none;
253 // The following occurs for a displayed math inset for instance (for good
254 // reasons involving subtleties of the algorithm in getRowFromDocIterator).
255 // We want this inset selected.
256 if (start.id == end.id && start.pos == end.pos)
262 pair<DocIterator, DocIterator> TexRow::getDocIteratorsFromRow(
264 Buffer const & buf) const
266 TextEntry start, end;
267 tie(start,end) = getEntriesFromRow(row);
268 return getDocIteratorsFromEntries(start, end, buf);
273 pair<DocIterator, DocIterator> TexRow::getDocIteratorsFromEntries(
279 DocIterator dit_start = buf.getParFromID(start.id);
281 dit_start.pos() = min(start.pos, dit_start.lastpos());
283 DocIterator dit_end = buf.getParFromID(end.id);
285 dit_end.pos() = min(end.pos, dit_end.lastpos());
286 // So far dit_end belongs to the next row. Step backwards.
287 if (!dit_end.top().at_cell_begin()) {
288 CursorSlice end_top = dit_end.top();
289 end_top.backwardPos();
290 if (dit_start && end_top != dit_start.top())
291 dit_end.top() = end_top;
293 dit_end.boundary(true);
295 return {dit_start, dit_end};
300 FuncRequest TexRow::goToFunc(TextEntry start, TextEntry end)
302 return {LFUN_PARAGRAPH_GOTO,
303 convert<string>(start.id) + " " + convert<string>(start.pos) + " " +
304 convert<string>(end.id) + " " + convert<string>(end.pos)};
310 RowEntry TexRow::rowEntryFromCursorSlice(CursorSlice const & slice)
313 InsetMath * insetMath = slice.asInsetMath();
316 entry.math.id = insetMath->id();
317 entry.math.cell = slice.idx();
318 } else if (slice.text()) {
320 entry.text.id = slice.paragraph().id();
321 entry.text.pos = slice.pos();
323 LASSERT(false, return row_none);
329 bool TexRow::sameParOrInsetMath(RowEntry entry1, RowEntry entry2)
331 return entry1.is_math == entry2.is_math
333 ? (entry1.math.id == entry2.math.id)
334 : (entry1.text.id == entry2.text.id));
339 int TexRow::comparePos(RowEntry entry1, RowEntry entry2)
341 // assume it is sameParOrInsetMath
343 return entry2.math.cell - entry1.math.cell;
345 return entry2.text.pos - entry1.text.pos;
349 // An iterator on RowList that goes top-down, left-right
351 // We assume that the end of RowList does not change, which makes things simpler
353 // Records a pair of iterators on the RowEntryList (row_it_, row_end_) and a
354 // pair of iterators on the current row (it_, it_end_).
356 // it_ always points to a valid position unless row_it_ == row_end_.
358 // We could turn this into a proper bidirectional iterator, but we don't need as
361 class TexRow::RowListIterator
364 RowListIterator(RowList::const_iterator r,
365 RowList::const_iterator r_end)
366 : row_it_(r), row_end_(r_end),
367 it_(r == r_end ? RowEntryList::const_iterator() : r->begin()),
368 it_end_(r == r_end ? RowEntryList::const_iterator() : r->end())
375 row_it_(RowList::const_iterator()),
376 row_end_(RowList::const_iterator()),
377 it_(RowEntryList::const_iterator()),
378 it_end_(RowEntryList::const_iterator()) { }
381 RowEntry const & operator*()
387 RowListIterator & operator++()
397 return row_it_ == row_end_;
401 bool operator==(RowListIterator const & a) const
403 return row_it_ == a.row_it_ && ((atEnd() && a.atEnd()) || it_ == a.it_);
407 bool operator!=(RowListIterator const & a) const { return !operator==(a); }
411 RowList::const_iterator const & row() const
416 // ensures that it_ points to a valid value unless row_it_ == row_end_
419 if (row_it_ == row_end_)
421 while (it_ == it_end_) {
423 if (row_it_ != row_end_) {
424 it_ = row_it_->begin();
425 it_end_ = row_it_->end();
431 RowList::const_iterator row_it_;
433 RowList::const_iterator row_end_;
435 RowEntryList::const_iterator it_;
437 RowEntryList::const_iterator it_end_;
441 TexRow::RowListIterator TexRow::begin() const
443 return RowListIterator(rowlist_.begin(), rowlist_.end());
447 TexRow::RowListIterator TexRow::end() const
449 return RowListIterator(rowlist_.end(), rowlist_.end());
453 pair<int,int> TexRow::rowFromDocIterator(DocIterator const & dit) const
455 bool beg_found = false;
456 bool end_is_next = true;
458 size_t best_slice = 0;
459 RowEntry best_entry = row_none;
460 size_t const n = dit.depth();
461 // this loop finds a pair (best_beg_row,best_end_row) where best_beg_row is
462 // the first row of the topmost possible CursorSlice, and best_end_row is
463 // the one just before the first row matching the next CursorSlice.
464 RowListIterator const begin = this->begin();//necessary disambiguation
465 RowListIterator const end = this->end();
466 RowListIterator best_beg_entry;
467 //best last entry with same pos as the beg_entry, or first entry with pos
468 //immediately following the beg_entry
469 RowListIterator best_end_entry;
470 RowListIterator it = begin;
471 for (; it != end; ++it) {
472 // Compute the best end row.
474 && (!sameParOrInsetMath(*it, *best_end_entry)
475 || comparePos(*it, *best_end_entry) <= 0)
476 && sameParOrInsetMath(*it, best_entry)) {
477 switch (comparePos(*it, best_entry)) {
479 // Either it is the last one that matches pos...
485 // ...or it is the row preceding the first that matches pos+1
488 if (it.row() != best_end_entry.row())
496 // Compute the best begin row. It is better than the previous one if it
497 // matches either at a deeper level, or at the same level but not
499 for (size_t i = best_slice; i < n; ++i) {
500 RowEntry entry_i = rowEntryFromCursorSlice(dit[i]);
501 if (sameParOrInsetMath(*it, entry_i)) {
502 if (comparePos(*it, entry_i) >= 0
505 || !sameParOrInsetMath(*it, *best_beg_entry)
506 || (comparePos(*it, *best_beg_entry) <= 0
507 && comparePos(entry_i, *best_beg_entry) != 0)
514 best_entry = entry_i;
515 best_beg_entry = best_end_entry = it;
523 return make_pair(-1,-1);
524 int const best_beg_row = distance(rowlist_.begin(),
525 best_beg_entry.row()) + 1;
526 int const best_end_row = distance(rowlist_.begin(),
527 best_end_entry.row()) + end_offset;
528 return make_pair(best_beg_row, best_end_row);
532 pair<int,int> TexRow::rowFromCursor(Cursor const & cur) const
534 DocIterator beg = cur.selectionBegin();
535 pair<int,int> beg_rows = rowFromDocIterator(beg);
536 if (cur.selection()) {
537 DocIterator end = cur.selectionEnd();
538 if (!cur.selIsMultiCell() && !end.top().at_cell_begin())
539 end.top().backwardPos();
540 pair<int,int> end_rows = rowFromDocIterator(end);
541 return make_pair(min(beg_rows.first, end_rows.first),
542 max(beg_rows.second, end_rows.second));
544 return make_pair(beg_rows.first, beg_rows.second);
548 size_t TexRow::rows() const
550 return rowlist_.size();
554 void TexRow::setRows(size_t r)
556 rowlist_.resize(r, RowEntryList());
560 // debugging functions
563 docstring TexRow::asString(RowEntry entry)
567 os << "(1," << entry.math.id << "," << entry.math.cell << ")";
569 os << "(0," << entry.text.id << "," << entry.text.pos << ")";
574 ///prepends the texrow to the source given by tex, for debugging purpose
575 void TexRow::prepend(docstring_list & tex) const
577 size_type const prefix_length = 25;
578 if (tex.size() < rowlist_.size())
579 tex.resize(rowlist_.size());
580 auto it = rowlist_.cbegin();
581 auto const beg = rowlist_.cbegin();
582 auto const end = rowlist_.cend();
583 for (; it < end; ++it) {
585 for (RowEntry const & e : *it)
586 entry += asString(e);
587 if (entry.length() < prefix_length)
588 entry = entry + docstring(prefix_length - entry.length(), ' ');
589 ptrdiff_t i = it - beg;
590 tex[i] = entry + " " + tex[i];