commit 0285dacadbe684b4f2661bdac87e926f1daa5708
Author: Jean-Marc Lasgouttes <[email protected]>
Date:   Thu Apr 10 13:42:41 2025 +0200

    Move fixBiblio to updateBuffer (3/4): fix cursors of all views
    
    Introduce Buffer::updateCursorsAfterInsertion/Deletion.
    
    This can be called after a change in a paragraph, so that the cursors
    of all open views can be updated . Currently, only fixBibio uses it.
---
 src/Buffer.cpp                    |  7 +++++++
 src/Buffer.h                      | 12 ++++++++++++
 src/Paragraph.cpp                 | 34 +++++++++++++++-------------------
 src/Paragraph.h                   |  2 +-
 src/frontends/WorkAreaManager.cpp |  8 ++++++++
 src/frontends/WorkAreaManager.h   |  4 ++++
 6 files changed, 47 insertions(+), 20 deletions(-)

diff --git a/src/Buffer.cpp b/src/Buffer.cpp
index b2ad6cf071..3db3637364 100644
--- a/src/Buffer.cpp
+++ b/src/Buffer.cpp
@@ -691,6 +691,13 @@ void Buffer::changed(bool update_metrics) const
 }
 
 
+void Buffer::updateCursorsAfterInsertion(DocIterator const & dit, int const 
count)
+{
+       if (d->wa_)
+               d->wa_->updateCursorsAfterInsertion(dit, count);
+}
+
+
 frontend::WorkAreaManager & Buffer::workAreaManager() const
 {
        LBUFERR(d->wa_);
diff --git a/src/Buffer.h b/src/Buffer.h
index 2bb488661f..f081932ba0 100644
--- a/src/Buffer.h
+++ b/src/Buffer.h
@@ -670,6 +670,18 @@ public:
 
        /// This function is called when the buffer is changed.
        void changed(bool update_metrics) const;
+       /**  Update the cursors held by bufferviews iterator after an  insertion
+        * \param dit : the place where the insertion happened.
+        * \param count : the number of characters added (or deleted if 
negative).
+        */
+       void updateCursorsAfterInsertion(DocIterator const & dit, int count = 
1);
+       /**  Update the cursors held by bufferviews iterator after a deletion
+        * \param dit : the place where the insertion happened.
+        * \param count : the number of characters deleted (or added if 
negative).
+        */
+       void updateCursorsAfterDeletion(DocIterator const & dit, int count = 1)
+               { updateCursorsAfterInsertion(dit, -count); }
+
        ///
        void setChild(DocIterator const & dit, Buffer * child);
        ///
diff --git a/src/Paragraph.cpp b/src/Paragraph.cpp
index f5db5abca7..9a028291d4 100644
--- a/src/Paragraph.cpp
+++ b/src/Paragraph.cpp
@@ -4634,11 +4634,11 @@ bool Paragraph::brokenBiblio() const
 }
 
 
-void Paragraph::fixBiblio(Cursor & cur)
+void Paragraph::fixBiblio(DocIterator & dit)
 {
-       bool const track_changes = cur.buffer()->params().track_changes;
+       bool const track_changes = dit.buffer()->params().track_changes;
        int bibitem_pos = getInsetPos(BIBITEM_CODE, 0, true);
-       Cursor changecur = cur;
+       DocIterator changedit = dit;
 
        // The case where paragraph is not BIBLIO
        if (d->layout_->labeltype != LABEL_BIBLIO) {
@@ -4647,9 +4647,8 @@ void Paragraph::fixBiblio(Cursor & cur)
                        return;
                // There is an InsetBibitem: remove it!
                if (eraseChar(bibitem_pos, track_changes)) {
-                       changecur.pos() = bibitem_pos;
-                       cur.updateAfterInsertion(changecur, -1);
-                       cur.resetAnchor();
+                       changedit.pos() = bibitem_pos;
+                       dit.buffer()->updateCursorsAfterDeletion(changedit);
                }
        }
        else if (bibitem_pos == 0) {
@@ -4662,9 +4661,8 @@ void Paragraph::fixBiblio(Cursor & cur)
                // one. We're assuming there are at most two of these, which
                // there should be.
                if (eraseChar(bibitem_pos, track_changes)) {
-                       changecur.pos() = bibitem_pos;
-                       cur.updateAfterInsertion(changecur, -1);
-                       cur.resetAnchor();
+                       changedit.pos() = bibitem_pos;
+                       dit.buffer()->updateCursorsAfterDeletion(changedit);
                }
        } else {
 
@@ -4677,26 +4675,24 @@ void Paragraph::fixBiblio(Cursor & cur)
                        //   (marked deleted)
                        // * Without change tracking, we release the inset
                        //   from its previous InsetList position
-                       inset = new InsetBibitem(cur.buffer(),
+                       inset = new InsetBibitem(dit.buffer(),
                                                 
getInset(bibitem_pos)->asInsetCommand()->params());
                        if (eraseChar(bibitem_pos, track_changes)) {
-                               changecur.pos() = bibitem_pos;
-                               cur.updateAfterInsertion(changecur, -1);
-                               cur.resetAnchor();
+                               changedit.pos() = bibitem_pos;
+                               
dit.buffer()->updateCursorsAfterDeletion(changedit);
                        }
                } else
                        // No inset found -- make a fresh one
-                       inset = new InsetBibitem(cur.buffer(), 
InsetCommandParams(BIBITEM_CODE));
+                       inset = new InsetBibitem(dit.buffer(), 
InsetCommandParams(BIBITEM_CODE));
 
-               Font font(inherit_font, cur.buffer()->params().language);
+               Font font(inherit_font, dit.buffer()->params().language);
                insertInset(0, inset, font, Change(track_changes ? 
Change::INSERTED : Change::UNCHANGED));
-               changecur.pos() = 0;
-               cur.updateAfterInsertion(changecur);
-               cur.resetAnchor();
+               changedit.pos() = 0;
+               dit.buffer()->updateCursorsAfterInsertion(changedit);
        }
 
        // This is needed to get the counters right (#8499)
-       cur.buffer()->updateBuffer();
+       dit.buffer()->updateBuffer();
 }
 
 
diff --git a/src/Paragraph.h b/src/Paragraph.h
index 3182864b0f..5b85739471 100644
--- a/src/Paragraph.h
+++ b/src/Paragraph.h
@@ -464,7 +464,7 @@ public:
        /// Check if we are in a Biblio environment and insert or
        /// delete InsetBibitems as necessary.
        /// \param cur : the cursor that needs to be updated accordingly
-       void fixBiblio(Cursor & cur);
+       void fixBiblio(DocIterator & dit);
 
        /// For each author, set 'used' to true if there is a change
        /// by this author in the paragraph.
diff --git a/src/frontends/WorkAreaManager.cpp 
b/src/frontends/WorkAreaManager.cpp
index 324d5571af..0eb4cd34df 100644
--- a/src/frontends/WorkAreaManager.cpp
+++ b/src/frontends/WorkAreaManager.cpp
@@ -80,6 +80,14 @@ void WorkAreaManager::sanitizeCursors()
        }
 }
 
+void WorkAreaManager::updateCursorsAfterInsertion(DocIterator const & dit, int 
count)
+{
+       for (WorkArea * wa : work_areas_) {
+               wa->bufferView().cursor().updateAfterInsertion(dit, count);
+               
wa->bufferView().cursor().realAnchor().updateAfterInsertion(dit, count);
+       }
+}
+
 
 } // namespace frontend
 } // namespace lyx
diff --git a/src/frontends/WorkAreaManager.h b/src/frontends/WorkAreaManager.h
index 73548592fa..95a8832240 100644
--- a/src/frontends/WorkAreaManager.h
+++ b/src/frontends/WorkAreaManager.h
@@ -17,6 +17,7 @@
 namespace lyx {
 
 class Buffer;
+class DocIterator;
 
 namespace frontend {
 
@@ -51,6 +52,9 @@ public:
        bool unhide(Buffer * buf) const;
        /// Fix cursors in all buffer views held by work areas.
        void sanitizeCursors();
+       /// Update all cursors after insertion (or deletion is count < 0)
+       void updateCursorsAfterInsertion(DocIterator const & dit, int count);
+
 
 private:
        typedef std::list<WorkArea *>::iterator iterator;
-- 
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to