]> git.lyx.org Git - features.git/commitdiff
Paragraph iterators
authorDekel Tsur <dekelts@tau.ac.il>
Sat, 1 Sep 2001 21:26:34 +0000 (21:26 +0000)
committerDekel Tsur <dekelts@tau.ac.il>
Sat, 1 Sep 2001 21:26:34 +0000 (21:26 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@2649 a592a061-630c-0410-9148-cb99ea01b6c8

15 files changed:
src/BufferView2.C
src/ChangeLog
src/Makefile.am
src/buffer.C
src/buffer.h
src/insets/ChangeLog
src/insets/inset.h
src/insets/insetcollapsable.C
src/insets/insetcollapsable.h
src/insets/insettabular.C
src/insets/insettabular.h
src/insets/insettext.C
src/insets/insettext.h
src/iterators.C [new file with mode: 0644]
src/iterators.h [new file with mode: 0644]

index 3aac225fed9347551cb1e7eb8c430e3798658db7..b58b1c3ca69174f352dac9b8b5d30db93e1f9121 100644 (file)
@@ -32,6 +32,7 @@
 #include "gettext.h"
 #include "undo_funcs.h"
 #include "debug.h"
+#include "iterators.h"
 
 extern BufferList bufferlist;
 
@@ -494,18 +495,20 @@ bool BufferView::ChangeInsets(Inset::Code code,
                              string const & from, string const & to)
 {
        bool flag = false;
-       Paragraph * par = buffer()->paragraph;
        LyXCursor cursor = text->cursor;
        LyXCursor tmpcursor = cursor;
        cursor.par(tmpcursor.par());
        cursor.pos(tmpcursor.pos());
 
-       while (par) {
+       ParIterator end = buffer()->par_iterator_end();
+       for (ParIterator it = buffer()->par_iterator_begin();
+            it != end; ++it) {
+               Paragraph * par = *it;
                bool flag2 = false;
-               for (Paragraph::inset_iterator it = par->inset_iterator_begin();
-                    it != par->inset_iterator_end(); ++it) {
-                       if ((*it)->lyxCode() == code) {
-                               InsetCommand * inset = static_cast<InsetCommand *>(*it);
+               for (Paragraph::inset_iterator it2 = par->inset_iterator_begin();
+                    it2 != par->inset_iterator_end(); ++it2) {
+                       if ((*it2)->lyxCode() == code) {
+                               InsetCommand * inset = static_cast<InsetCommand *>(*it2);
                                if (inset->getContents() == from) {
                                        inset->setContents(to);
                                        flag2 = true;
@@ -514,14 +517,16 @@ bool BufferView::ChangeInsets(Inset::Code code,
                }
                if (flag2) {
                        flag = true;
-                       // this is possible now, since SetCursor takes
-                       // care about footnotes
-                       text->setCursorIntern(this, par, 0);
-                       text->redoParagraphs(this, text->cursor,
-                                            text->cursor.par()->next());
-                       text->fullRebreak(this);
+#warning Fix me
+                       // The test it.size()==1 was needed to prevent crashes.
+                       // How to set the cursor corretly when it.size()>1 ??
+                       if (it.size() == 1) {
+                               text->setCursorIntern(this, par, 0);
+                               text->redoParagraphs(this, text->cursor,
+                                                    text->cursor.par()->next());
+                               text->fullRebreak(this);
+                       }
                }
-               par = par->next();
        }
        text->setCursorIntern(this, cursor.par(), cursor.pos());
        return flag;
index 2ad6f9ee4dd14f749cdafc37b015cc888e0a5914..aa6d9b67f58188fa46c446874f4a768758dcdc20 100644 (file)
@@ -1,3 +1,12 @@
+2001-09-02  Dekel Tsur  <dekelts@tau.ac.il>
+
+       * iterators.[Ch]: New files. Provide paragraph iterators.
+
+       * buffer.C (changeLanguage): Use paragraph iterators.
+       (isMultiLingual): ditto 
+
+       * BufferView2.C (ChangeInsets): Use paragraph iterators.
+
 2001-09-01  Dekel Tsur  <dekelts@tau.ac.il>
 
        * FontLoader.C: Support for cmr font.
index 64b6772746b9f06356eae60f2166343be2f01d9e..1c281449c6fffa2915f3ea01b972037891a3d714 100644 (file)
@@ -124,6 +124,8 @@ lyx_SOURCES = \
        importer.h \
        intl.C \
        intl.h \
+       iterators.C \
+       iterators.h \
        kbmap.C \
        kbmap.h \
        kbsequence.C \
index e99a8a01b8b936f7c0f5b65fd95a5cd4f28d169f..de4866e3b9b9d6aa9ae4fec9dac6902fe679cb8e 100644 (file)
 #include "converter.h"
 #include "BufferView.h"
 #include "ParagraphParameters.h"
+#include "iterators.h"
 
 using std::ostream;
 using std::ofstream;
@@ -3755,22 +3756,19 @@ void Buffer::redraw()
 void Buffer::changeLanguage(Language const * from, Language const * to)
 {
 
-       Paragraph * par = paragraph;
-       while (par) {
-               par->changeLanguage(params, from, to);
-               par = par->next();
-       }
+       ParIterator end = par_iterator_end();
+       for (ParIterator it = par_iterator_begin(); it != end; ++it)
+               (*it)->changeLanguage(params, from, to);
 }
 
 
 bool Buffer::isMultiLingual()
 {
-       Paragraph * par = paragraph;
-       while (par) {
-               if (par->isMultiLingual(params))
+       ParIterator end = par_iterator_end();
+       for (ParIterator it = par_iterator_begin(); it != end; ++it)
+               if ((*it)->isMultiLingual(params))
                        return true;
-               par = par->next();
-       }
+
        return false;
 }
 
@@ -3831,3 +3829,15 @@ Paragraph * Buffer::getParFromID(int id) const
        }
        return 0;
 }
+
+
+ParIterator Buffer::par_iterator_begin()
+{
+        return ParIterator(paragraph);
+}
+
+
+ParIterator Buffer::par_iterator_end()
+{
+        return ParIterator();
+}
index a68acbc26399c9a7a2a019ccae6e6ca67f4640d5..0a34563cf04e0433b2192bb6197368f34e38bf1e 100644 (file)
@@ -30,6 +30,7 @@ class LyXRC;
 class TeXErrors;
 class LaTeXFeatures;
 class Language;
+class ParIterator;
 
 // When lyx 1.3.x starts we should enable this
 // btw. we should also test this with 1.2 so that we
@@ -451,6 +452,12 @@ public:
        inset_iterator inset_const_iterator_end() const {
                return inset_iterator();
        }
+
+       ///
+       ParIterator par_iterator_begin();
+       ///
+       ParIterator par_iterator_end();
+
        ///
        Inset * getInsetFromID(int id_arg) const;
 };
@@ -600,4 +607,5 @@ bool operator!=(Buffer::inset_iterator const & iter1,
                Buffer::inset_iterator const & iter2) {
        return !(iter1 == iter2);
 }
+
 #endif
index 993018c5890b09f52a192b930dab191795f29657..53a723b5d04005dedd3a6f545fa08e3d16c34d7f 100644 (file)
@@ -1,3 +1,7 @@
+2001-09-02  Dekel Tsur  <dekelts@tau.ac.il>
+
+       * inset.h (getFirstParagraph): New virtual method.
+
 2001-08-20  Herbert Voss  <voss@perce.de>
        * insetbib.C: added a option bibtotoc which is from "BIB to TOC" 
        in the the bibtex-database-gui for inserting a line
index 82d84034bc847b5e4c8b53beb69e649535d36426..7a36f616861a5341ae4c1d5c1656c556a7c63f8b 100644 (file)
@@ -264,6 +264,12 @@ public:
        virtual Paragraph * firstParagraph() const {
                return 0;
        }
+
+       ///
+       virtual Paragraph * getFirstParagraph(int /*num*/) const {
+               return 0;
+       }
+
        /// return the cursor if we own one otherwise giv'em just the
        /// BufferView cursor to work with.
        virtual LyXCursor const & cursor(BufferView * bview) const;
index b1905c679cbe7d4361bdc30cd725847e5818eb39..5a9926cff8fa1bd77731f62a4b1c729a2e2024fe 100644 (file)
@@ -577,6 +577,12 @@ Paragraph * InsetCollapsable::firstParagraph() const
 }
 
 
+Paragraph * InsetCollapsable::getFirstParagraph(int i) const
+{
+       return inset.getFirstParagraph(i);
+}
+
+
 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
 {
        return inset.cursor(bv);
index c595acef08561269659e8c2893bbaf02f6fc5a0a..3c9a29aca33096a28b7d36775635cd8123e8f34c 100644 (file)
@@ -165,6 +165,8 @@ public:
        ///
        Paragraph * firstParagraph() const;
        ///
+       Paragraph * getFirstParagraph(int) const;
+       ///
        LyXCursor const & cursor(BufferView *) const;
        ///
        bool isOpen() const { return !collapsed_; }
index f54ef6e196c6ec9ba6610c384219a86db1e87be8..0da1e92eb0c97dc19f884282fe4dbcb41665be14 100644 (file)
@@ -2458,6 +2458,14 @@ Paragraph * InsetTabular::firstParagraph() const
 }
 
 
+Paragraph * InsetTabular::getFirstParagraph(int i) const
+{
+       return (i < tabular->GetNumberOfCells())
+               ? tabular->GetCellInset(i)->getFirstParagraph(0)
+               : 0;
+}
+
+
 LyXCursor const & InsetTabular::cursor(BufferView * bv) const
 {
        if (the_locking_inset)
index 4c4b64a81b2faf15ac70189f25c09b9165b403fc..40c2b0736077615c56a2073c1d762a91a9b61ab8 100644 (file)
@@ -208,6 +208,8 @@ public:
        ///
        Paragraph * firstParagraph() const;
        ///
+       Paragraph * getFirstParagraph(int) const;
+       ///
        LyXCursor const & cursor(BufferView *) const;
        ///
        string const selectNextWord(BufferView *, float & value) const;
index bc431bcada02fd2bdd26845aa7b0cea6c43940c2..ba5070507536e2126870bfa035ccf61656c3900c 100644 (file)
@@ -2122,6 +2122,12 @@ Paragraph * InsetText::firstParagraph() const
 }
 
 
+Paragraph * InsetText::getFirstParagraph(int i) const
+{
+       return (i == 0) ? par : 0;
+}
+
+
 LyXCursor const & InsetText::cursor(BufferView * bv) const
 {
                if (the_locking_inset)
index a654c47b1e507ecf23a52143a27acb5560e43daf..f611bdbec2064cb3817e83a9e673452bcfde796e 100644 (file)
@@ -221,6 +221,8 @@ public:
        ///
        Paragraph * firstParagraph() const;
        ///
+       Paragraph * getFirstParagraph(int) const;
+       ///
        LyXCursor const & cursor(BufferView *) const;
        ///
        Paragraph * paragraph() const;
diff --git a/src/iterators.C b/src/iterators.C
new file mode 100644 (file)
index 0000000..e94479d
--- /dev/null
@@ -0,0 +1,39 @@
+#include <config.h>
+
+#include "iterators.h"
+
+ParIterator  & ParIterator::operator++()
+{
+       while (!positions.empty()) {
+               ParPosition & p = positions.back();
+
+               // Does the current inset contain more "cells" ?
+               if (p.index >= 0) {
+                       ++p.index;
+                       Paragraph * par = (*p.it)->getFirstParagraph(p.index);
+                       if (par) {
+                               positions.push_back(ParPosition(par));
+                               return *this;
+                       }
+                       ++p.it;
+               }
+
+               // Try to find the next inset that contains paragraphs
+               for ( ; p.it != p.par->inset_iterator_end(); ++p.it) {
+                       Paragraph * par = (*p.it)->getFirstParagraph(0);
+                       if (par) {
+                               p.index = 0;
+                               positions.push_back(ParPosition(par));
+                               return *this;
+                       }
+               }
+               // Try to go to the next paragarph
+               if (p.par->next()) {
+                       p = ParPosition(p.par->next());
+                       return *this;
+               }
+
+               positions.pop_back();
+       }
+       return *this;
+}
diff --git a/src/iterators.h b/src/iterators.h
new file mode 100644 (file)
index 0000000..148eda0
--- /dev/null
@@ -0,0 +1,72 @@
+// -*- C++ -*-
+
+#ifndef ITERATORS_H
+#define ITERATORS_H
+
+#include <vector>
+
+#include "paragraph.h"
+
+class ParPosition {
+public:
+       ParPosition(Paragraph * p)
+               : par(p), it(p->inset_iterator_begin()), index(-1) {}
+       ///
+       Paragraph * par;
+       ///
+       Paragraph::inset_iterator it;
+       ///
+       int index;
+};
+
+
+inline
+bool operator==(ParPosition const & pos1, ParPosition const & pos2) {
+       return pos1.par == pos2.par &&
+               pos1.it == pos2.it &&
+               pos1.index == pos2.index;
+}
+
+inline
+bool operator!=(ParPosition const & pos1, ParPosition const & pos2) {
+       return !(pos1 == pos2);
+}
+
+
+class ParIterator {
+public:
+       ///
+       ParIterator() {}
+       //
+       ParIterator(Paragraph * par) 
+               : positions(1, ParPosition(par)) {}
+       ///
+       ParIterator & operator++();
+       ///
+       Paragraph * operator*() { return positions.back().par; }
+       ///
+       vector<ParPosition>::size_type size() const 
+               { return positions.size(); }
+       ///
+       friend
+       bool operator==(ParIterator const & iter1, ParIterator const & iter2);
+private:
+       ///
+       std::vector<ParPosition> positions;
+};
+
+
+///
+inline
+bool operator==(ParIterator const & iter1, ParIterator const & iter2) {
+       return iter1.positions == iter2.positions;
+}
+
+
+///
+inline
+bool operator!=(ParIterator const & iter1, ParIterator const & iter2) {
+       return !(iter1 == iter2);
+}
+
+#endif