]> git.lyx.org Git - lyx.git/blobdiff - src/TexRow.h
Provide proper fallback if a bibliography processor is not found
[lyx.git] / src / TexRow.h
index 226866fd927af7fc7fd4b136f8ed8cb7ec95c660..e85b141aeff70bc546b902190dc340da12e4a360 100644 (file)
 #define TEXROW_H
 
 #include "support/debug.h"
+#include "support/docstring.h"
 #include "support/types.h"
 
 #include <vector>
 
 namespace lyx {
 
-class LyXErr;
+class Buffer;
 class Cursor;
 class CursorSlice;
 class DocIterator;
 class docstring_list;
+class FuncRequest;
 
 /// types for cells and math insets
 typedef void const * uid_type;
 typedef size_t idx_type;
 
 
-/// an individual par id/pos <=> row mapping
-struct TextEntry { int id; int pos; };
+/// Represents the correspondence between paragraphs and the generated
+/// LaTeX file
 
-/// an individual math id/cell <=> row mapping
-struct MathEntry { uid_type id; idx_type cell; };
+class TexRow {
+public:
+       /// We begin with defining the types of row information we are tracking
+       ///
 
-/// a container for passing entries around
-struct RowEntry {
-       bool is_math;// true iff the union is a math
-       union {
-               struct TextEntry text;
-               struct MathEntry math;
+       /// type of row entries
+       enum RowType {
+               text_entry,
+               math_entry,
+               begin_document
        };
-};
 
+       /// an individual par id/pos <=> row mapping
+       struct TextEntry { int id; pos_type pos; };
 
-/// Represents the correspondence between paragraphs and the generated
-/// LaTeX file
+       /// an individual math id/cell <=> row mapping
+       struct MathEntry { uid_type id; idx_type cell; };
 
-class TexRow {
+       /// a container for passing entries around
+       struct RowEntry {
+               RowType type;
+               union {
+                       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
+               };
+       };
+
+       /// 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);
+
+private:
        /// id/pos correspondence for a single row
        class RowEntryList;
 
@@ -76,8 +107,6 @@ class TexRow {
        ///
        RowList rowlist_;
        ///
-       bool enabled_;
-       ///
        RowEntryList & currentRow();
 
        ///
@@ -88,25 +117,23 @@ class TexRow {
        RowListIterator end() const;
 public:
        ///
-       TexRow(bool enable = true);
-
-       /// Clears structure.  Set enable to false if texrow is not needed, to avoid
-       /// computing TexRow when it is going to be immediately discarded.
-       void reset(bool enable = true);
-
-       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);
-
-       /// Converts a CursorSlice into a RowEntry
-       static RowEntry rowEntryFromCursorSlice(CursorSlice const & slice);
-       /// Encapsulates the paragraph and position for later use
-       static RowEntry textEntry(int id, int pos);
-       /// Encapsulates a cell and position for later use
-       static RowEntry mathEntry(uid_type id, idx_type cell);
+       TexRow();
+
+#if !(defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ == 6))
+       /// 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;
+# else
+       //for gcc 4.6, nothing to do: it's enough to disable implicit copy during
+       // dev with more recent versions of gcc.
+#endif
+
+       /// Clears structure.
+       void reset();
 
        /// for debugging purposes
        static docstring asString(RowEntry entry);
@@ -116,7 +143,7 @@ public:
        bool start(RowEntry entry);
        /// Defines the paragraph and position for the current line
        /// returns true if this entry will appear on the current row
-       bool start(int id, int pos);
+       bool start(int id, pos_type pos);
        /// Defines a cell and position for the current line.  Always appear in the
        /// current row.
        void startMath(uid_type id, idx_type cell);
@@ -125,7 +152,7 @@ public:
        /// 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.
-       void forceStart(int id, int pos);
+       void forceStart(int id, pos_type pos);
 
        /// Insert node when line is completed
        void newline();
@@ -133,19 +160,47 @@ public:
        void newlines(size_t num_lines);
 
        /**
-        * getIdFromRow - find pid and position for a given row
-        * @param row row number to find
-        * @param id set to id if found
-        * @param pos set to paragraph position if found
-        * @return true if found, false otherwise
-        *
-        * If the row could not be found, pos is set to zero and
-        * id is set to -1
+        * getEntriesFromRow - find pids and position for a given row
+        * This is the main algorithm behind reverse-search.
+        * @param row number to find
+        * @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().
         */
-       bool getIdFromRow(int row, int & id, int & pos) const;
+       std::pair<TextEntry,TextEntry> getEntriesFromRow(int row) const;
+
+       /**
+        * 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);
+       // A FuncRequest to select a row
+       FuncRequest goToFuncFromRow(int const row) const;
+
+       /**
+        * getDocIteratorFromRow - find pids and positions for a given row
+        * @param row number to find
+        * @param buffer where to look
+        * @return a pair of DocIterators as above.
+        */
+       std::pair<DocIterator, DocIterator> getDocIteratorsFromRow(
+           int row,
+           Buffer const & buf) const;
 
        /// Finds the best pair of rows for dit
        /// returns (-1,-1) if not found.
+       /// This is the main algorithm behind forward-search.
        std::pair<int,int> rowFromDocIterator(DocIterator const & dit) const;
 
        /// Finds the best pair of rows for cursor, taking the selection into
@@ -154,7 +209,9 @@ public:
        std::pair<int,int> rowFromCursor(Cursor const & dit) const;
 
        /// Returns the number of rows contained
-       int rows() const;
+       size_t rows() const;
+       /// Fill or trim to reach the row count \param r
+       void setRows(size_t r);
 
        /// appends texrow. the final line of this is merged with the first line of
        /// texrow.
@@ -164,7 +221,7 @@ public:
        void prepend(docstring_list &) const;
 
 private:
-       /// true iff same paragraph or math inset
+       /// true iff same paragraph or math inset or begin_document
        static bool sameParOrInsetMath(RowEntry entry1, RowEntry entry2);
        /// computes the distance in pos or cell index
        /// assumes it is the sameParOrInsetMath
@@ -173,6 +230,40 @@ private:
 };
 
 
+/// TexString : dumb struct to pass around docstrings with TexRow information.
+/// They are best created using otexstringstream.
+/// 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;
+#if !(defined(__GNUC__) && (__GNUC__ == 4) && (__GNUC_MINOR__ == 6))
+       /// 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;
+# else
+       //for gcc 4.6, nothing to do: it's enough to disable implicit copy during
+       // dev with more recent versions of gcc.
+#endif
+       /// Empty TexString
+       TexString() = default;
+       /// 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.
+       void validate();
+};
+
+
 // Standard container needs a complete type
 class TexRow::RowEntryList {
        // For each row we store a list of one special TextEntry and several
@@ -213,10 +304,7 @@ public:
 };
 
 
-bool operator==(RowEntry entry1, RowEntry entry2);
-
-
-LyXErr & operator<<(LyXErr &, TexRow const &);
+bool operator==(TexRow::RowEntry entry1, TexRow::RowEntry entry2);
 
 
 } // namespace lyx