[LyX/master] Adjust bookmark position when inserting/deleting from paragraph

Jean-Marc Lasgouttes lasgouttes at lyx.org
Thu Mar 11 14:56:24 UTC 2021


commit 86bfa10abb57a0af7bbddc3af2c7cdb891c77203
Author: Jean-Marc Lasgouttes <lasgouttes at lyx.org>
Date:   Thu Mar 11 15:57:06 2021 +0100

    Adjust bookmark position when inserting/deleting from paragraph
    
    Without this, bookmarks positions drift when editing.
---
 src/Paragraph.cpp |    9 +++++++++
 src/Session.cpp   |    9 +++++++++
 src/Session.h     |    9 ++++++++-
 3 files changed, 26 insertions(+), 1 deletions(-)

diff --git a/src/Paragraph.cpp b/src/Paragraph.cpp
index a327f15..8ac87d3 100644
--- a/src/Paragraph.cpp
+++ b/src/Paragraph.cpp
@@ -37,6 +37,7 @@
 #include "output_xhtml.h"
 #include "output_docbook.h"
 #include "ParagraphParameters.h"
+#include "Session.h"
 #include "SpellChecker.h"
 #include "texstream.h"
 #include "TexRow.h"
@@ -832,6 +833,10 @@ void Paragraph::Private::insertChar(pos_type pos, char_type c,
 
 	// Update list of misspelled positions
 	speller_state_.increasePosAfterPos(pos);
+
+	// Update bookmarks
+	theSession().bookmarks().adjustPosAfterPos(inset_owner_->buffer().fileName(),
+	                                           id_, pos, 1);
 }
 
 
@@ -916,6 +921,10 @@ bool Paragraph::eraseChar(pos_type pos, bool trackChanges)
 	d->speller_state_.decreasePosAfterPos(pos);
 	d->speller_state_.refreshLast(size());
 
+	// Update bookmarks
+	theSession().bookmarks().adjustPosAfterPos(d->inset_owner_->buffer().fileName(),
+	                                           d->id_, pos, -1);
+
 	return true;
 }
 
diff --git a/src/Session.cpp b/src/Session.cpp
index 16b6df7..a814991 100644
--- a/src/Session.cpp
+++ b/src/Session.cpp
@@ -353,6 +353,15 @@ BookmarksSection::bookmarksInPar(FileName const & fn, int const par_id) const
 }
 
 
+void BookmarksSection::adjustPosAfterPos(FileName const & fn,
+	int const par_id, pos_type pos, int offset)
+{
+	for (Bookmark & bkm : bookmarks)
+		if (bkm.filename == fn && bkm.top_id == par_id && bkm.top_pos > pos)
+			bkm.top_pos += offset;
+}
+
+
 LastCommandsSection::LastCommandsSection(unsigned int num) :
 	default_num_last_commands(30),
 	absolute_max_last_commands(100)
diff --git a/src/Session.h b/src/Session.h
index 857e056..b7443b9 100644
--- a/src/Session.h
+++ b/src/Session.h
@@ -196,7 +196,7 @@ public:
 	/// top and bottom level information sometimes needs to be sync'ed. In particular,
 	/// top_id is determined when a bookmark is restored from session; and
 	/// bottom_pit and bottom_pos are determined from top_id when a bookmark
-	/// is save to session. (What a mess! :-)
+	/// is saved to session. (What a mess! :-)
 	///
 	/// TODO: bottom level pit and pos will be replaced by StableDocIterator
 	class Bookmark {
@@ -268,6 +268,13 @@ public:
 	/// return a list of bookmarks and position for this paragraph
 	BookmarkPosList bookmarksInPar(support::FileName const & fn, int par_id) const;
 
+	/* An insertion/deletion in paragraph \c par_id of buffer \c fn
+	 * lead to an offset \c offset after position \c pos. Update
+	 * bookmarks accordingly.
+	 */
+	void adjustPosAfterPos(support::FileName const & fn,
+	                       int const par_id, pos_type pos, int offset);
+
 private:
 
 	/// allow 9 regular bookmarks, bookmark 0 is temporary


More information about the lyx-cvs mailing list