commit 1fbcf3720098ca508ce90c84b0f002055beba73b
Author: Jean-Marc Lasgouttes <[email protected]>
Date:   Wed Apr 9 15:45:46 2025 +0200

    Move fixBiblio to updateBuffer (2/4) : rewrite partly fixBiblio
    
    Now that function takes care of cursor update using the newly
    introduced updateAfterInsert. This will be changed later to handle all
    views of a given document.
    
    As a consequence, do not return an int anymore.
    
    When two bibinsets are present, only the first one is kept. When an
    inset needs to be moved to position 0, always use a copy by simplicity
    (the old code was wrong when tracking changes but still deleting
    elements because they are of same author).
---
 src/Paragraph.cpp   | 98 ++++++++++++++++++++++++++---------------------------
 src/Paragraph.h     |  8 ++---
 src/TextMetrics.cpp | 10 +-----
 3 files changed, 51 insertions(+), 65 deletions(-)

diff --git a/src/Paragraph.cpp b/src/Paragraph.cpp
index 1be292f16f..f5db5abca7 100644
--- a/src/Paragraph.cpp
+++ b/src/Paragraph.cpp
@@ -4634,71 +4634,69 @@ bool Paragraph::brokenBiblio() const
 }
 
 
-int Paragraph::fixBiblio(Buffer const & buffer)
+void Paragraph::fixBiblio(Cursor & cur)
 {
-       // FIXME: when there was already an inset at 0, the return value is 1,
-       // which does not tell whether another inset has been removed; the
-       // cursor cannot be correctly updated.
-
-       bool const track_changes = buffer.params().track_changes;
+       bool const track_changes = cur.buffer()->params().track_changes;
        int bibitem_pos = getInsetPos(BIBITEM_CODE, 0, true);
+       Cursor changecur = cur;
 
        // The case where paragraph is not BIBLIO
        if (d->layout_->labeltype != LABEL_BIBLIO) {
                if (bibitem_pos == -1)
                        // No InsetBibitem => OK
-                       return 0;
+                       return;
                // There is an InsetBibitem: remove it!
-               eraseChar(bibitem_pos, track_changes);
-               return (bibitem_pos == 0) ? -1 : -bibitem_pos;
+               if (eraseChar(bibitem_pos, track_changes)) {
+                       changecur.pos() = bibitem_pos;
+                       cur.updateAfterInsertion(changecur, -1);
+                       cur.resetAnchor();
+               }
        }
-
-       bool const hasbibitem0 = bibitem_pos == 0;
-       if (hasbibitem0) {
+       else if (bibitem_pos == 0) {
                bibitem_pos = getInsetPos(BIBITEM_CODE, 1, true);
                // There was an InsetBibitem at pos 0,
                // and no other one => OK
                if (bibitem_pos == -1)
-                       return 0;
-               // there is a bibitem at the 0 position, but since
-               // there is a second one, we copy the second on the
-               // first. We're assuming there are at most two of
-               // these, which there should be.
-               // FIXME: why does it make sense to do that rather
-               // than keep the first? (JMarc)
-               Inset * inset = releaseInset(bibitem_pos);
-               d->insetlist_.begin()->inset = inset;
-               // This needs to be done to update the counter (#8499)
-               buffer.updateBuffer();
-               return -bibitem_pos;
-       }
-
-       // We need to create an inset at the beginning
-       Inset * inset = nullptr;
-       if (bibitem_pos > 0) {
-               // There was one somewhere in the paragraph, let's move it
-               // * With change tracking, we use a clone
-               //   and leave the old inset at its position
-               //   (marked deleted)
-               // * Without change tracking, we release the inset
-               //   from its previous InsetList position
-               inset = track_changes
-                               ? new InsetBibitem(const_cast<Buffer 
*>(&buffer),
-                                                  
getInset(bibitem_pos)->asInsetCommand()->params())
-                               : d->insetlist_.release(bibitem_pos);
-               eraseChar(bibitem_pos, track_changes);
-       } else
-               // No inset found -- make a fresh one
-               inset = new InsetBibitem(const_cast<Buffer *>(&buffer),
-                                        InsetCommandParams(BIBITEM_CODE));
+                       return;
+               // there is a bibitem at the 0 position, so remove the second
+               // 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();
+               }
+       } else {
 
-       Font font(inherit_font, buffer.params().language);
-       insertInset(0, inset, font, Change(track_changes ? Change::INSERTED
-                                                  : Change::UNCHANGED));
+               // We need to create an inset at the beginning
+               Inset * inset = nullptr;
+               if (bibitem_pos > 0) {
+                       // There was one somewhere in the paragraph, let's move 
it
+                       // * With change tracking, we use a clone
+                       //   and leave the old inset at its position
+                       //   (marked deleted)
+                       // * Without change tracking, we release the inset
+                       //   from its previous InsetList position
+                       inset = new InsetBibitem(cur.buffer(),
+                                                
getInset(bibitem_pos)->asInsetCommand()->params());
+                       if (eraseChar(bibitem_pos, track_changes)) {
+                               changecur.pos() = bibitem_pos;
+                               cur.updateAfterInsertion(changecur, -1);
+                               cur.resetAnchor();
+                       }
+               } else
+                       // No inset found -- make a fresh one
+                       inset = new InsetBibitem(cur.buffer(), 
InsetCommandParams(BIBITEM_CODE));
+
+               Font font(inherit_font, cur.buffer()->params().language);
+               insertInset(0, inset, font, Change(track_changes ? 
Change::INSERTED : Change::UNCHANGED));
+               changecur.pos() = 0;
+               cur.updateAfterInsertion(changecur);
+               cur.resetAnchor();
+       }
 
-       // This is needed to get the counters right
-       buffer.updateBuffer();
-       return 1;
+       // This is needed to get the counters right (#8499)
+       cur.buffer()->updateBuffer();
 }
 
 
diff --git a/src/Paragraph.h b/src/Paragraph.h
index 8bd9a6ee5f..3182864b0f 100644
--- a/src/Paragraph.h
+++ b/src/Paragraph.h
@@ -463,12 +463,8 @@ public:
        bool brokenBiblio() const;
        /// Check if we are in a Biblio environment and insert or
        /// delete InsetBibitems as necessary.
-       /// \retval int 1, if we had to add an inset, in which case
-       /// the cursor will need to move cursor forward; -pos, if we deleted
-       /// an inset, in which case pos is the position from which the inset
-       /// was deleted, and the cursor will need to be moved back one if it
-       /// was previously past that position. Return 0 otherwise.
-       int fixBiblio(Buffer const & buffer);
+       /// \param cur : the cursor that needs to be updated accordingly
+       void fixBiblio(Cursor & cur);
 
        /// For each author, set 'used' to true if there is a change
        /// by this author in the paragraph.
diff --git a/src/TextMetrics.cpp b/src/TextMetrics.cpp
index acf4140788..6ef1a565a4 100644
--- a/src/TextMetrics.cpp
+++ b/src/TextMetrics.cpp
@@ -513,15 +513,7 @@ bool TextMetrics::redoParagraph(pit_type const pit, bool 
const align_rows)
                if (&cur.inset() == &text_->inset())
                        cur.recordUndo(pit, pit);
 
-               int const moveCursor = par.fixBiblio(buffer);
-
-               // Is it necessary to update the cursor?
-               if (&cur.inset() == &text_->inset() && cur.pit() == pit) {
-                       if (moveCursor > 0)
-                               cur.posForward();
-                       else if (moveCursor < 0 && cur.pos() >= -moveCursor)
-                               cur.posBackward();
-               }
+               par.fixBiblio(cur);
        }
 
        // Optimisation: this is used in the next two loops
-- 
lyx-cvs mailing list
[email protected]
https://lists.lyx.org/mailman/listinfo/lyx-cvs

Reply via email to