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()
253 : TextEntry{start.id, -1}; // last position
254 // The following occurs for a displayed math inset for instance (for good
255 // reasons involving subtleties of the algorithm in getRowFromDocIterator).
256 // We want this inset selected.
257 if (start.id == end.id && start.pos == end.pos)
263 pair<DocIterator, DocIterator> TexRow::getDocIteratorsFromRow(
265 Buffer const & buf) const
267 TextEntry start, end;
268 tie(start,end) = getEntriesFromRow(row);
269 return getDocIteratorsFromEntries(start, end, buf);
274 pair<DocIterator, DocIterator> TexRow::getDocIteratorsFromEntries(
279 auto set_pos = [](DocIterator & dit, pos_type pos) {
280 dit.pos() = (pos >= 0) ? min(pos, dit.lastpos())
281 // negative pos values are counted from the end
282 : max(dit.lastpos() + pos + 1, pos_type(0));
285 DocIterator dit_start = buf.getParFromID(start.id);
287 set_pos(dit_start, start.pos);
289 DocIterator dit_end = buf.getParFromID(end.id);
291 set_pos(dit_end, end.pos);
292 // Step backwards to prevent selecting the beginning of another
294 if (dit_end.pos() == 0 && !dit_end.top().at_cell_begin()) {
295 CursorSlice end_top = dit_end.top();
296 end_top.backwardPos();
297 if (dit_start && end_top != dit_start.top())
298 dit_end.top() = end_top;
300 dit_end.boundary(true);
302 return {dit_start, dit_end};
307 FuncRequest TexRow::goToFunc(TextEntry start, TextEntry end)
309 return {LFUN_PARAGRAPH_GOTO,
310 convert<string>(start.id) + " " + convert<string>(start.pos) + " " +
311 convert<string>(end.id) + " " + convert<string>(end.pos)};
316 FuncRequest TexRow::goToFunc(std::pair<TextEntry,TextEntry> entries)
318 return goToFunc(entries.first, entries.second);
323 RowEntry TexRow::rowEntryFromCursorSlice(CursorSlice const & slice)
326 InsetMath * insetMath = slice.asInsetMath();
329 entry.math.id = insetMath->id();
330 entry.math.cell = slice.idx();
331 } else if (slice.text()) {
333 entry.text.id = slice.paragraph().id();
334 entry.text.pos = slice.pos();
336 LASSERT(false, return row_none);
342 bool TexRow::sameParOrInsetMath(RowEntry entry1, RowEntry entry2)
344 return entry1.is_math == entry2.is_math
346 ? (entry1.math.id == entry2.math.id)
347 : (entry1.text.id == entry2.text.id));
352 int TexRow::comparePos(RowEntry entry1, RowEntry entry2)
354 // assume it is sameParOrInsetMath
356 return entry2.math.cell - entry1.math.cell;
358 return entry2.text.pos - entry1.text.pos;
362 // An iterator on RowList that goes top-down, left-right
364 // We assume that the end of RowList does not change, which makes things simpler
366 // Records a pair of iterators on the RowEntryList (row_it_, row_end_) and a
367 // pair of iterators on the current row (it_, it_end_).
369 // it_ always points to a valid position unless row_it_ == row_end_.
371 // We could turn this into a proper bidirectional iterator, but we don't need as
374 class TexRow::RowListIterator
377 RowListIterator(RowList::const_iterator r,
378 RowList::const_iterator r_end)
379 : row_it_(r), row_end_(r_end),
380 it_(r == r_end ? RowEntryList::const_iterator() : r->begin()),
381 it_end_(r == r_end ? RowEntryList::const_iterator() : r->end())
388 row_it_(RowList::const_iterator()),
389 row_end_(RowList::const_iterator()),
390 it_(RowEntryList::const_iterator()),
391 it_end_(RowEntryList::const_iterator()) { }
394 RowEntry const & operator*()
400 RowListIterator & operator++()
410 return row_it_ == row_end_;
414 bool operator==(RowListIterator const & a) const
416 return row_it_ == a.row_it_ && ((atEnd() && a.atEnd()) || it_ == a.it_);
420 bool operator!=(RowListIterator const & a) const { return !operator==(a); }
424 RowList::const_iterator const & row() const
429 // ensures that it_ points to a valid value unless row_it_ == row_end_
432 if (row_it_ == row_end_)
434 while (it_ == it_end_) {
436 if (row_it_ != row_end_) {
437 it_ = row_it_->begin();
438 it_end_ = row_it_->end();
444 RowList::const_iterator row_it_;
446 RowList::const_iterator row_end_;
448 RowEntryList::const_iterator it_;
450 RowEntryList::const_iterator it_end_;
454 TexRow::RowListIterator TexRow::begin() const
456 return RowListIterator(rowlist_.begin(), rowlist_.end());
460 TexRow::RowListIterator TexRow::end() const
462 return RowListIterator(rowlist_.end(), rowlist_.end());
466 pair<int,int> TexRow::rowFromDocIterator(DocIterator const & dit) const
468 bool beg_found = false;
469 bool end_is_next = true;
471 size_t best_slice = 0;
472 RowEntry best_entry = row_none;
473 size_t const n = dit.depth();
474 // this loop finds a pair (best_beg_row,best_end_row) where best_beg_row is
475 // the first row of the topmost possible CursorSlice, and best_end_row is
476 // the one just before the first row matching the next CursorSlice.
477 RowListIterator const begin = this->begin();//necessary disambiguation
478 RowListIterator const end = this->end();
479 RowListIterator best_beg_entry;
480 //best last entry with same pos as the beg_entry, or first entry with pos
481 //immediately following the beg_entry
482 RowListIterator best_end_entry;
483 RowListIterator it = begin;
484 for (; it != end; ++it) {
485 // Compute the best end row.
487 && (!sameParOrInsetMath(*it, *best_end_entry)
488 || comparePos(*it, *best_end_entry) <= 0)
489 && sameParOrInsetMath(*it, best_entry)) {
490 switch (comparePos(*it, best_entry)) {
492 // Either it is the last one that matches pos...
498 // ...or it is the row preceding the first that matches pos+1
501 if (it.row() != best_end_entry.row())
509 // Compute the best begin row. It is better than the previous one if it
510 // matches either at a deeper level, or at the same level but not
512 for (size_t i = best_slice; i < n; ++i) {
513 RowEntry entry_i = rowEntryFromCursorSlice(dit[i]);
514 if (sameParOrInsetMath(*it, entry_i)) {
515 if (comparePos(*it, entry_i) >= 0
518 || !sameParOrInsetMath(*it, *best_beg_entry)
519 || (comparePos(*it, *best_beg_entry) <= 0
520 && comparePos(entry_i, *best_beg_entry) != 0)
527 best_entry = entry_i;
528 best_beg_entry = best_end_entry = it;
536 return make_pair(-1,-1);
537 int const best_beg_row = distance(rowlist_.begin(),
538 best_beg_entry.row()) + 1;
539 int const best_end_row = distance(rowlist_.begin(),
540 best_end_entry.row()) + end_offset;
541 return make_pair(best_beg_row, best_end_row);
545 pair<int,int> TexRow::rowFromCursor(Cursor const & cur) const
547 DocIterator beg = cur.selectionBegin();
548 pair<int,int> beg_rows = rowFromDocIterator(beg);
549 if (cur.selection()) {
550 DocIterator end = cur.selectionEnd();
551 if (!cur.selIsMultiCell() && !end.top().at_cell_begin())
552 end.top().backwardPos();
553 pair<int,int> end_rows = rowFromDocIterator(end);
554 return make_pair(min(beg_rows.first, end_rows.first),
555 max(beg_rows.second, end_rows.second));
557 return make_pair(beg_rows.first, beg_rows.second);
561 size_t TexRow::rows() const
563 return rowlist_.size();
567 void TexRow::setRows(size_t r)
569 rowlist_.resize(r, RowEntryList());
573 // debugging functions
576 docstring TexRow::asString(RowEntry entry)
580 os << "(1," << entry.math.id << "," << entry.math.cell << ")";
582 os << "(0," << entry.text.id << "," << entry.text.pos << ")";
587 ///prepends the texrow to the source given by tex, for debugging purpose
588 void TexRow::prepend(docstring_list & tex) const
590 size_type const prefix_length = 25;
591 if (tex.size() < rowlist_.size())
592 tex.resize(rowlist_.size());
593 auto it = rowlist_.cbegin();
594 auto const beg = rowlist_.cbegin();
595 auto const end = rowlist_.cend();
596 for (; it < end; ++it) {
598 for (RowEntry const & e : *it)
599 entry += asString(e);
600 if (entry.length() < prefix_length)
601 entry = entry + docstring(prefix_length - entry.length(), ' ');
602 ptrdiff_t i = it - beg;
603 tex[i] = entry + " " + tex[i];