1999-09-27 18:44:28 +00:00
|
|
|
// -*- C++ -*-
|
2002-05-30 03:37:24 +00:00
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
* \file TexRow.h
|
2003-08-23 00:17:00 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-03-21 17:27:08 +00:00
|
|
|
*
|
2002-05-30 03:37:24 +00:00
|
|
|
* \author Matthias Ettrich
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author Lars Gullik Bjønnes
|
2003-08-23 00:17:00 +00:00
|
|
|
* \author John Levon
|
2015-10-13 23:17:05 +00:00
|
|
|
* \author Guillaume Munch
|
2003-08-23 00:17:00 +00:00
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
2002-05-30 03:37:24 +00:00
|
|
|
*/
|
|
|
|
|
2015-10-13 22:51:50 +00:00
|
|
|
/* Note about debugging options:
|
|
|
|
*
|
|
|
|
* When compiled in devel mode and run with the option -dbg latex, two ways
|
|
|
|
* of debugging TexRow are available:
|
|
|
|
*
|
|
|
|
* 1. The source view panel prepends the full TexRow information to the LaTeX
|
|
|
|
* output.
|
|
|
|
*
|
|
|
|
* 2. Clicking on any line in the source view moves the buffer to the location
|
|
|
|
* recognised by TexRow.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
1999-11-04 01:40:20 +00:00
|
|
|
#ifndef TEXROW_H
|
|
|
|
#define TEXROW_H
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2015-09-15 04:56:01 +00:00
|
|
|
#include "support/debug.h"
|
2016-10-22 22:25:05 +00:00
|
|
|
#include "support/docstring.h"
|
2016-06-20 02:47:40 +00:00
|
|
|
#include "support/types.h"
|
2000-03-28 02:18:55 +00:00
|
|
|
|
2015-09-15 04:56:01 +00:00
|
|
|
#include <vector>
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
namespace lyx {
|
|
|
|
|
2016-09-03 22:54:05 +00:00
|
|
|
class Buffer;
|
2015-10-13 19:26:21 +00:00
|
|
|
class Cursor;
|
2015-10-13 23:17:05 +00:00
|
|
|
class CursorSlice;
|
2015-09-15 04:56:01 +00:00
|
|
|
class DocIterator;
|
2015-10-13 23:17:05 +00:00
|
|
|
class docstring_list;
|
2016-09-05 02:23:24 +00:00
|
|
|
class FuncRequest;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
2015-10-13 23:17:05 +00:00
|
|
|
/// types for cells and math insets
|
2015-10-13 23:23:12 +00:00
|
|
|
typedef void const * uid_type;
|
2015-10-13 23:17:05 +00:00
|
|
|
typedef size_t idx_type;
|
|
|
|
|
2015-10-13 23:23:12 +00:00
|
|
|
|
2007-08-12 14:54:54 +00:00
|
|
|
/// Represents the correspondence between paragraphs and the generated
|
2008-10-31 14:13:44 +00:00
|
|
|
/// LaTeX file
|
2007-08-12 14:54:54 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
class TexRow {
|
2016-10-11 10:09:38 +00:00
|
|
|
public:
|
2016-10-11 12:14:48 +00:00
|
|
|
/// We begin with defining the types of row information we are tracking
|
|
|
|
///
|
|
|
|
|
|
|
|
/// type of row entries
|
|
|
|
enum RowType {
|
|
|
|
text_entry,
|
|
|
|
math_entry,
|
|
|
|
begin_document
|
|
|
|
};
|
|
|
|
|
2016-10-11 10:09:38 +00:00
|
|
|
/// an individual par id/pos <=> row mapping
|
|
|
|
struct TextEntry { int id; pos_type pos; };
|
|
|
|
|
|
|
|
/// an individual math id/cell <=> row mapping
|
|
|
|
struct MathEntry { uid_type id; idx_type cell; };
|
|
|
|
|
|
|
|
/// a container for passing entries around
|
|
|
|
struct RowEntry {
|
2016-10-11 12:14:48 +00:00
|
|
|
RowType type;
|
2016-10-11 10:09:38 +00:00
|
|
|
union {
|
2016-10-11 12:14:48 +00:00
|
|
|
struct TextEntry text;// iff the type is text_entry
|
|
|
|
struct MathEntry math;// iff the type is row_entry
|
|
|
|
struct {} begindocument;// iff the type is begin_document
|
2016-10-11 10:09:38 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-10-11 12:14:48 +00:00
|
|
|
/// Encapsulates the paragraph and position for later use
|
|
|
|
static RowEntry textEntry(int id, pos_type pos);
|
|
|
|
/// Encapsulates a cell and position for later use
|
|
|
|
static RowEntry mathEntry(uid_type id, idx_type cell);
|
|
|
|
/// Denotes the beginning of the document
|
|
|
|
static RowEntry beginDocument();
|
|
|
|
|
|
|
|
/// Converts a CursorSlice into a RowEntry
|
|
|
|
static RowEntry rowEntryFromCursorSlice(CursorSlice const & slice);
|
|
|
|
|
|
|
|
static const TextEntry text_none;
|
|
|
|
static const RowEntry row_none;
|
|
|
|
/// Returns true if RowEntry is devoid of information
|
|
|
|
static bool isNone(RowEntry entry);
|
|
|
|
/// Returns true if TextEntry is devoid of information
|
|
|
|
static bool isNone(TextEntry entry);
|
|
|
|
|
2016-10-11 10:09:38 +00:00
|
|
|
private:
|
2016-06-20 02:47:40 +00:00
|
|
|
/// id/pos correspondence for a single row
|
|
|
|
class RowEntryList;
|
2015-10-13 22:51:50 +00:00
|
|
|
|
2016-06-20 02:47:40 +00:00
|
|
|
/// container of id/pos <=> row mapping
|
|
|
|
/// invariant: in any enabled_ TexRow, rowlist_ will contain at least one
|
|
|
|
/// Row (the current row)
|
|
|
|
typedef std::vector<RowEntryList> RowList;
|
|
|
|
///
|
|
|
|
RowList rowlist_;
|
|
|
|
///
|
|
|
|
RowEntryList & currentRow();
|
2015-10-13 23:17:05 +00:00
|
|
|
|
2016-06-20 02:47:40 +00:00
|
|
|
///
|
|
|
|
class RowListIterator;
|
|
|
|
///
|
|
|
|
RowListIterator begin() const;
|
|
|
|
///
|
|
|
|
RowListIterator end() const;
|
|
|
|
public:
|
|
|
|
///
|
2016-09-04 02:02:47 +00:00
|
|
|
TexRow();
|
2015-10-13 22:51:50 +00:00
|
|
|
|
2016-09-22 23:42:57 +00:00
|
|
|
#if !(defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ == 6))
|
2016-09-04 02:21:19 +00:00
|
|
|
/// Copy can be expensive and is not usually useful for TexRow.
|
|
|
|
/// Force explicit copy, prefer move instead. This also prevents
|
|
|
|
/// move()s from being converted into copy silently.
|
|
|
|
explicit TexRow(TexRow const & other) = default;
|
|
|
|
TexRow(TexRow && other) = default;
|
|
|
|
TexRow & operator=(TexRow const & other) = default;
|
|
|
|
TexRow & operator=(TexRow && other) = default;
|
2016-09-22 23:42:57 +00:00
|
|
|
# else
|
|
|
|
//for gcc 4.6, nothing to do: it's enough to disable implicit copy during
|
|
|
|
// dev with more recent versions of gcc.
|
|
|
|
#endif
|
2016-09-04 02:21:19 +00:00
|
|
|
|
2016-09-04 02:02:47 +00:00
|
|
|
/// Clears structure.
|
|
|
|
void reset();
|
2015-10-13 23:17:05 +00:00
|
|
|
|
|
|
|
/// for debugging purposes
|
2016-06-20 02:47:40 +00:00
|
|
|
static docstring asString(RowEntry entry);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2015-10-13 23:17:05 +00:00
|
|
|
/// Defines the row information for the current line
|
|
|
|
/// returns true if this entry will appear on the current row
|
|
|
|
bool start(RowEntry entry);
|
|
|
|
/// Defines the paragraph and position for the current line
|
|
|
|
/// returns true if this entry will appear on the current row
|
2016-09-03 22:52:55 +00:00
|
|
|
bool start(int id, pos_type pos);
|
2015-10-13 22:51:50 +00:00
|
|
|
/// Defines a cell and position for the current line. Always appear in the
|
|
|
|
/// current row.
|
|
|
|
void startMath(uid_type id, idx_type cell);
|
|
|
|
/// Defines the paragraph for the current cell-like inset. Always appears
|
|
|
|
/// in the current row like a math cell, but is detached from the normal
|
|
|
|
/// text flow. Note: since the cell idx is not recorded it does not work as
|
|
|
|
/// well as for math grids; if we were to do that properly we would need to
|
|
|
|
/// access the id of the parent Tabular inset from the CursorSlice.
|
2016-09-03 22:52:55 +00:00
|
|
|
void forceStart(int id, pos_type pos);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
/// Insert node when line is completed
|
|
|
|
void newline();
|
2010-02-08 17:39:55 +00:00
|
|
|
/// Insert multiple nodes when zero or more lines are completed
|
2016-06-20 02:47:40 +00:00
|
|
|
void newlines(size_t num_lines);
|
2015-09-15 04:56:01 +00:00
|
|
|
|
2002-05-30 03:37:24 +00:00
|
|
|
/**
|
2016-09-03 22:54:05 +00:00
|
|
|
* getEntriesFromRow - find pids and position for a given row
|
2016-10-11 12:14:48 +00:00
|
|
|
* This is the main algorithm behind reverse-search.
|
2016-09-05 02:23:24 +00:00
|
|
|
* @param row number to find
|
2016-09-03 22:54:05 +00:00
|
|
|
* @return a pair of TextEntry denoting the start and end of the position.
|
|
|
|
* The TextEntry values can be isNone(). If no row is found then the first
|
|
|
|
* value isNone().
|
2002-05-30 03:37:24 +00:00
|
|
|
*/
|
2016-09-03 22:54:05 +00:00
|
|
|
std::pair<TextEntry,TextEntry> getEntriesFromRow(int row) const;
|
|
|
|
|
2016-09-05 02:23:24 +00:00
|
|
|
/**
|
|
|
|
* getDocIteratorFromEntries - find pids and positions for a given row
|
|
|
|
* @param buffer where to look
|
|
|
|
* @return a pair of DocIterators denoting the start and end of the
|
|
|
|
* position. The DocIterators can be invalid. The starting DocIterator
|
|
|
|
* being invalid means that no location was found. Note: there is no
|
|
|
|
* guarantee that the DocIterators are in the same inset or even at the
|
|
|
|
* same depth.
|
|
|
|
*/
|
|
|
|
static std::pair<DocIterator, DocIterator> getDocIteratorsFromEntries(
|
|
|
|
TextEntry start,
|
|
|
|
TextEntry end,
|
|
|
|
Buffer const & buf);
|
|
|
|
|
|
|
|
// A FuncRequest to select from start to end
|
|
|
|
static FuncRequest goToFunc(TextEntry start, TextEntry end);
|
2016-10-11 09:22:20 +00:00
|
|
|
// A FuncRequest to select a row
|
|
|
|
FuncRequest goToFuncFromRow(int const row) const;
|
2016-09-05 02:23:24 +00:00
|
|
|
|
2016-09-03 22:54:05 +00:00
|
|
|
/**
|
|
|
|
* getDocIteratorFromRow - find pids and positions for a given row
|
|
|
|
* @param row number to find
|
2016-09-05 02:23:24 +00:00
|
|
|
* @param buffer where to look
|
|
|
|
* @return a pair of DocIterators as above.
|
2016-09-03 22:54:05 +00:00
|
|
|
*/
|
2016-09-05 02:23:24 +00:00
|
|
|
std::pair<DocIterator, DocIterator> getDocIteratorsFromRow(
|
2016-09-03 22:54:05 +00:00
|
|
|
int row,
|
|
|
|
Buffer const & buf) const;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2015-09-15 04:56:01 +00:00
|
|
|
/// Finds the best pair of rows for dit
|
|
|
|
/// returns (-1,-1) if not found.
|
2016-10-11 12:14:48 +00:00
|
|
|
/// This is the main algorithm behind forward-search.
|
2015-09-15 04:56:01 +00:00
|
|
|
std::pair<int,int> rowFromDocIterator(DocIterator const & dit) const;
|
2015-10-13 19:26:21 +00:00
|
|
|
|
|
|
|
/// Finds the best pair of rows for cursor, taking the selection into
|
|
|
|
/// account
|
|
|
|
/// returns (-1,-1) if not found.
|
|
|
|
std::pair<int,int> rowFromCursor(Cursor const & dit) const;
|
2016-06-20 02:47:40 +00:00
|
|
|
|
2002-05-30 03:37:24 +00:00
|
|
|
/// Returns the number of rows contained
|
2016-09-04 02:21:19 +00:00
|
|
|
size_t rows() const;
|
|
|
|
/// Fill or trim to reach the row count \param r
|
|
|
|
void setRows(size_t r);
|
2015-10-13 23:17:05 +00:00
|
|
|
|
2015-10-13 22:51:50 +00:00
|
|
|
/// appends texrow. the final line of this is merged with the first line of
|
|
|
|
/// texrow.
|
2016-06-20 02:47:40 +00:00
|
|
|
void append(TexRow texrow);
|
2015-10-13 22:51:50 +00:00
|
|
|
|
2015-10-13 23:17:05 +00:00
|
|
|
/// for debugging purpose
|
|
|
|
void prepend(docstring_list &) const;
|
2000-05-04 08:14:34 +00:00
|
|
|
|
2000-08-05 05:17:18 +00:00
|
|
|
private:
|
2016-10-11 12:14:48 +00:00
|
|
|
/// true iff same paragraph or math inset or begin_document
|
2016-06-20 02:47:40 +00:00
|
|
|
static bool sameParOrInsetMath(RowEntry entry1, RowEntry entry2);
|
|
|
|
/// computes the distance in pos or cell index
|
|
|
|
/// assumes it is the sameParOrInsetMath
|
|
|
|
static int comparePos(RowEntry entry1, RowEntry entry2);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-09-04 02:21:19 +00:00
|
|
|
/// TexString : dumb struct to pass around docstrings with TexRow information.
|
2016-09-25 10:38:53 +00:00
|
|
|
/// They are best created using otexstringstream.
|
2016-09-04 02:21:19 +00:00
|
|
|
/// They can be output to otexrowstreams and otexstreams.
|
|
|
|
/// A valid TexString has as many newlines in str as in texrow. Be careful not
|
|
|
|
/// to introduce a mismatch between the line and the row counts, as this will
|
|
|
|
/// assert in devel mode when outputting to a otexstream.
|
|
|
|
struct TexString {
|
|
|
|
///
|
|
|
|
docstring str;
|
|
|
|
///
|
|
|
|
TexRow texrow;
|
2016-09-22 23:42:57 +00:00
|
|
|
#if !(defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ == 6))
|
2016-09-04 02:21:19 +00:00
|
|
|
/// Copy can be expensive and is not usually useful for TexString.
|
|
|
|
/// Force explicit copy, prefer move instead. This also prevents
|
|
|
|
/// move()s from being converted into copy silently.
|
|
|
|
explicit TexString(TexString const &) = default;
|
|
|
|
TexString(TexString && other) = default;
|
|
|
|
TexString & operator=(TexString const & other) = default;
|
|
|
|
TexString & operator=(TexString && other) = default;
|
2016-09-22 23:42:57 +00:00
|
|
|
# else
|
|
|
|
//for gcc 4.6, nothing to do: it's enough to disable implicit copy during
|
|
|
|
// dev with more recent versions of gcc.
|
|
|
|
#endif
|
2016-09-25 10:38:53 +00:00
|
|
|
/// Empty TexString
|
2016-09-04 02:21:19 +00:00
|
|
|
TexString() = default;
|
2016-09-25 10:38:53 +00:00
|
|
|
/// Texstring containing str and TexRow with enough lines which are empty
|
|
|
|
explicit TexString(docstring str);
|
|
|
|
/// Texstring containing str and texrow. Must be valid.
|
|
|
|
TexString(docstring str, TexRow texrow);
|
|
|
|
/// Ensure that the string and the TexRow have as many newlines.
|
2016-09-04 02:21:19 +00:00
|
|
|
void validate();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-06-20 02:47:40 +00:00
|
|
|
// Standard container needs a complete type
|
|
|
|
class TexRow::RowEntryList {
|
|
|
|
// For each row we store a list of one special TextEntry and several
|
|
|
|
// RowEntries. (The order is important.) We only want one text entry
|
|
|
|
// because we do not want to store every position in the lyx file. On the
|
|
|
|
// other hand we want to record all math and table cells positions for
|
|
|
|
// enough precision. Usually the count of cells is easier to handle.
|
|
|
|
// The RowEntries are used for forward-search and the code preview pane.
|
|
|
|
std::vector<RowEntry> v_;
|
|
|
|
// The TextEntry is currently used for reverse-search and the error
|
|
|
|
// reporting dialog. Once the latter are adapted to rely on the more precise
|
|
|
|
// RowEntries above, it can be removed.
|
|
|
|
TextEntry text_entry_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef std::vector<RowEntry>::iterator iterator;
|
|
|
|
iterator begin() { return v_.begin(); }
|
|
|
|
iterator end() { return v_.end(); }
|
2015-10-13 23:17:05 +00:00
|
|
|
///
|
2016-06-20 02:47:40 +00:00
|
|
|
typedef std::vector<RowEntry>::const_iterator const_iterator;
|
|
|
|
const_iterator begin() const { return v_.cbegin(); }
|
|
|
|
const_iterator end() const { return v_.cend(); }
|
2015-10-13 23:17:05 +00:00
|
|
|
///
|
2016-06-20 02:47:40 +00:00
|
|
|
RowEntryList() : text_entry_(TexRow::text_none) {}
|
|
|
|
|
|
|
|
// returns true if the row entry will appear in the row entry list
|
|
|
|
bool addEntry(RowEntry entry);
|
|
|
|
|
|
|
|
// the row entry will appear in the row entry list, but it never counts
|
|
|
|
// as a proper text entry.
|
|
|
|
void forceAddEntry(RowEntry entry);
|
|
|
|
|
|
|
|
// returns the TextEntry or TexRow::text_none if none
|
|
|
|
TextEntry getTextEntry() const;
|
|
|
|
|
|
|
|
// appends a row
|
|
|
|
void append(RowEntryList row);
|
1999-09-27 18:44:28 +00:00
|
|
|
};
|
2002-12-01 22:59:25 +00:00
|
|
|
|
2015-10-13 23:17:05 +00:00
|
|
|
|
2016-10-11 10:09:38 +00:00
|
|
|
bool operator==(TexRow::RowEntry entry1, TexRow::RowEntry entry2);
|
2016-06-20 02:47:40 +00:00
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|
|
|
|
|
2002-05-30 03:37:24 +00:00
|
|
|
#endif // TEXROW_H
|