]> git.lyx.org Git - features.git/commitdiff
Bookmarks: use bottom level pit and pos
authorBo Peng <bpeng@lyx.org>
Mon, 16 Apr 2007 19:01:32 +0000 (19:01 +0000)
committerBo Peng <bpeng@lyx.org>
Mon, 16 Apr 2007 19:01:32 +0000 (19:01 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@17829 a592a061-630c-0410-9148-cb99ea01b6c8

src/BufferView.C
src/BufferView.h
src/lyxfunc.C
src/session.C
src/session.h

index cee8bd7c1197a8c31e34afaa53a9d07976e69879..09e0010491faaac0ae168434c3bee6d5bca56d52 100644 (file)
@@ -159,13 +159,14 @@ void BufferView::setBuffer(Buffer * b)
                        BookmarksSection::Bookmark const & bm = LyX::ref().session().bookmarks().bookmark(i);
                        if (buffer()->fileName() != bm.filename.absFilename())
                                continue;
-                       // if par_id or pit has been changed, reset par_pit and par_id
+                       // if top_id or bottom_pit, bottom_pos has been changed, update bookmark
                        // see http://bugzilla.lyx.org/show_bug.cgi?id=3092
                        pit_type new_pit;
+                       pos_type new_pos;
                        int new_id;
-                       boost::tie(new_pit, new_id) = moveToPosition(bm.par_pit, bm.par_id, bm.par_pos);
-                       if (bm.par_pit != new_pit || bm.par_id != new_id)
-                               const_cast<BookmarksSection::Bookmark &>(bm).setPos(new_pit, new_id);
+                       boost::tie(new_pit, new_pos, new_id) = moveToPosition(bm.bottom_pit, bm.bottom_pos, bm.top_id, bm.top_pos);
+                       if (bm.bottom_pit != new_pit || bm.bottom_pos != new_pos || bm.top_id != new_id )
+                               const_cast<BookmarksSection::Bookmark &>(bm).updatePos(new_pit, new_pos, new_id);
                }
                // current buffer is going to be switched-off, save cursor pos
                // Ideally, the whole cursor stack should be saved, but session
@@ -282,7 +283,7 @@ bool BufferView::loadLyXFile(FileName const & filename, bool tolastfiles)
                pos_type pos;
                boost::tie(pit, pos) = LyX::ref().session().lastFilePos().load(filename);
                // if successfully move to pit (returned par_id is not zero), update metrics and reset font
-               if (moveToPosition(pit, 0, pos).get<1>()) {
+               if (moveToPosition(pit, pos, 0, 0).get<1>()) {
                        if (fitCursor())
                                updateMetrics(false);
                        buffer_->text().setCurrentFont(cursor_);
@@ -551,9 +552,14 @@ Change const BufferView::getCurrentChange() const
 
 void BufferView::saveBookmark(unsigned int idx)
 {
+       // tenatively save bookmark, id and pos will be used to
+       // acturately locate a bookmark in a 'live' lyx session.
+       // pit and pos will be updated with bottom level pit/pos 
+       // when lyx exits.
        LyX::ref().session().bookmarks().save(
                FileName(buffer_->fileName()),
-               cursor_.pit(),
+               cursor_.bottom().pit(),
+               cursor_.bottom().pos(),
                cursor_.paragraph().id(),
                cursor_.pos(),
                idx
@@ -564,31 +570,40 @@ void BufferView::saveBookmark(unsigned int idx)
 }
 
 
-boost::tuple<pit_type, int> BufferView::moveToPosition(pit_type par_pit, int par_id, pos_type par_pos)
+boost::tuple<pit_type, pos_type, int> BufferView::moveToPosition(pit_type bottom_pit, pos_type bottom_pos,
+       int top_id, pos_type top_pos)
 {
        cursor_.clearSelection();
 
        // if a valid par_id is given, try it first
-       if (par_id > 0) {
-               ParIterator par = buffer_->getParFromID(par_id);
+       // This is the case for a 'live' bookmark when unique paragraph ID
+       // is used to track bookmarks.
+       if (top_id > 0) {
+               ParIterator par = buffer_->getParFromID(top_id);
                if (par != buffer_->par_iterator_end()) {
-                       setCursor(makeDocIterator(par, min(par->size(), par_pos)));
-                       return boost::make_tuple(cursor_.pit(), par_id);
+                       setCursor(makeDocIterator(par, min(par->size(), top_pos)));
+                       // Note: return bottom (document) level pit.
+                       return boost::make_tuple(cursor_.bottom().pit(), cursor_.bottom().pos(), top_id);
                }
        }
-       // if par_id == 0, or searching through par_id failed
-       if (static_cast<size_t>(par_pit) < buffer_->paragraphs().size()) {
+       // if top_id == 0, or searching through top_id failed
+       // This is the case for a 'restored' bookmark when only bottom 
+       // (document level) pit was saved. Because of this, bookmark
+       // restoration is inaccurate. If a bookmark was within an inset,
+       // it will be restored to the left of the outmost inset that contains
+       // the bookmark.
+       if (static_cast<size_t>(bottom_pit) < buffer_->paragraphs().size()) {
                ParIterator it = buffer_->par_iterator_begin();
                ParIterator const end = buffer_->par_iterator_end();
                for (; it != end; ++it)
-                       if (it.pit() == par_pit) {
+                       if (it.pit() == bottom_pit) {
                                // restored pos may be bigger than it->size
-                               setCursor(makeDocIterator(it, min(par_pos, it->size())));
-                               return boost::make_tuple(par_pit, it->id());
+                               setCursor(makeDocIterator(it, min(bottom_pos, it->size())));
+                               return boost::make_tuple(bottom_pit, bottom_pos, it->id());
                        }
        }
        // both methods fail
-       return boost::make_tuple(pit_type(0), 0);
+       return boost::make_tuple(pit_type(0), pos_type(0), 0);
 }
 
 
index 181b71c14c0d03782d43b5270fb64473a5d81527..a4726346c6934d81054e9bf27ab3d7897a585e3b 100644 (file)
@@ -115,12 +115,13 @@ public:
        /// Save the current position as bookmark.
        /// if idx == 0, save to temp_bookmark
        void saveBookmark(unsigned int idx);
-       /// goto a specified position, try par_id first, and then par_pit
-       /// return the par_pit and par_id of the new paragraph
-       boost::tuple<pit_type, int> moveToPosition(
-               pit_type par_pit, ///< Paragraph pit, used when par_id is zero or invalid.
-               int par_id, ///< Paragraph ID, \sa Paragraph
-               pos_type par_pos ///< Position in the \c Paragraph
+       /// goto a specified position, try top_id first, and then bottom_pit
+       /// return the bottom_pit and top_id of the new paragraph
+       boost::tuple<pit_type, pos_type, int> moveToPosition(
+               pit_type bottom_pit, ///< Paragraph pit, used when par_id is zero or invalid.
+               pos_type bottom_pos, ///< Paragraph pit, used when par_id is zero or invalid.
+               int top_id, ///< Paragraph ID, \sa Paragraph
+               pos_type top_pos ///< Position in the \c Paragraph
                );
        /// return the current change at the cursor.
        Change const getCurrentChange() const;
index e1cf47928231ee2fed01b2585d28c7272450276f..c61681ed4aaba67be61bca1ac284c82652fd8d21 100644 (file)
@@ -264,12 +264,13 @@ void LyXFunc::gotoBookmark(unsigned int idx, bool openFile, bool switchToBuffer)
                }
                // moveToPosition use par_id, and par_pit and return new par_id.
                pit_type new_pit;
+               pos_type new_pos;
                int new_id;
-               boost::tie(new_pit, new_id) = view()->moveToPosition(bm.par_pit, bm.par_id, bm.par_pos);
-               // if par_id or pit has been changed, reset par_pit and par_id
+               boost::tie(new_pit, new_pos, new_id) = view()->moveToPosition(bm.bottom_pit, bm.bottom_pos, bm.top_id, bm.top_pos);
+               // if bottom_pit, bottom_pos or top_id has been changed, update bookmark
                // see http://bugzilla.lyx.org/show_bug.cgi?id=3092
-               if (bm.par_pit != new_pit || bm.par_id != new_id)
-                       const_cast<BookmarksSection::Bookmark &>(bm).setPos(new_pit, new_id);
+               if (bm.bottom_pit != new_pit || bm.bottom_pos != new_pos || bm.top_id != new_id )
+                       const_cast<BookmarksSection::Bookmark &>(bm).updatePos(new_pit, new_pos, new_id);
        } 
 }
 
index 4997d193c668a2dae3508eb2498b6a7bd797d6b0..d8ac631cd31f169701b9b5a6e01a5e2e9c992358 100644 (file)
@@ -272,7 +272,7 @@ void BookmarksSection::read(istream & is)
                        if (fs::exists(file.toFilesystemEncoding()) &&
                            !fs::is_directory(file.toFilesystemEncoding()) &&
                            idx <= max_bookmarks)
-                               bookmarks[idx] = Bookmark(file, pit, 0, pos);
+                               bookmarks[idx] = Bookmark(file, pit, pos, 0, 0);
                        else
                                LYXERR(Debug::INIT) << "LyX: Warning: Ignore bookmark of file: " << fname << endl;
                } catch (...) {
@@ -288,18 +288,19 @@ void BookmarksSection::write(ostream & os) const
        for (size_t i = 1; i <= max_bookmarks; ++i) {
                if (isValid(i))
                        os << i << ", "
-                          << bookmarks[i].par_pit << ", "
-                          << bookmarks[i].par_pos << ", "
+                          << bookmarks[i].bottom_pit << ", "
+                          << bookmarks[i].bottom_pos << ", "
                           << bookmarks[i].filename << '\n';
        }
 }
 
 
-void BookmarksSection::save(FileName const & fname, pit_type par_pit, int par_id, pos_type par_pos, unsigned int idx)
+void BookmarksSection::save(FileName const & fname, pit_type bottom_pit, pos_type bottom_pos,
+       int top_id, pos_type top_pos, unsigned int idx)
 {
        // silently ignore bookmarks when idx is out of range
        if (idx <= max_bookmarks)
-               bookmarks[idx] = Bookmark(fname, par_pit, par_id, par_pos);
+               bookmarks[idx] = Bookmark(fname, bottom_pit, bottom_pos, top_id, top_pos);
 }
 
 
index 3efc9dbdd3548f1678de1e9d50136d777fe19c38..63b9a9a03eef05dbf8f1215f88e9b708270a5285 100644 (file)
@@ -174,27 +174,39 @@ private:
 class BookmarksSection : SessionSection
 {
 public:
-       /// bookmarks
+       /// A bookmark is composed of three parts
+       /// 1. filename
+       /// 2. bottom (whole document) level pit and pos, used to (inaccurately) save/restore a bookmark
+       /// 3. top level id and pos, used to accurately locate bookmark when lyx is running
+       /// 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! :-)
+       /// 
+       /// TODO: bottom level pit and pos will be replaced by StableDocIterator
        class Bookmark {
        public:
                /// Filename
                support::FileName filename;
-               /// Cursor pit, will be saved/restored by .lyx/session
-               pit_type par_pit;
-               /// Cursor paragraph Id, used to lcoate bookmarks for opened files
-               int par_id;
-               /// Cursor position within a paragraph
-               pos_type par_pos;
+               /// Bottom level cursor pit, will be saved/restored by .lyx/session
+               pit_type bottom_pit;
+               /// Bottom level cursor position, will be saved/restore by .lyx/session
+               pos_type bottom_pos;
+               /// Top level cursor id, used to lcoate bookmarks for opened files
+               int top_id;
+               /// Top level cursor position within a paragraph
+               pos_type top_pos;
                ///
-               Bookmark() : par_id(0), par_pos(0) {}
+               Bookmark() : bottom_pit(0), bottom_pos(0), top_id(0), top_pos(0) {}
                ///
-               Bookmark(support::FileName const & f, pit_type pit, int id, pos_type pos)
-                       : filename(f), par_pit(pit), par_id(id), par_pos(pos) {}
-               /// set bookmark par_id, this is because newly loaded bookmark
+               Bookmark(support::FileName const & f, pit_type pit, pos_type pos, int id, pos_type tpos)
+                       : filename(f), bottom_pit(pit), bottom_pos(pos), top_id(id), top_pos(tpos) {}
+               /// set bookmark top_id, this is because newly loaded bookmark
                /// may have zero par_id and par_pit can change during editing, see bug 3092
-               void setPos(pit_type pit, int id) { 
-                       par_pit = pit;
-                       par_id = id;
+               void updatePos(pit_type pit, pos_type pos, int id) { 
+                       bottom_pit = pit;
+                       bottom_pos = pos;
+                       top_id = id;
                }
        };
 
@@ -207,7 +219,8 @@ public:
        BookmarksSection() : bookmarks(10), max_bookmarks(9) {}
 
        /// Save the current position as bookmark
-       void save(support::FileName const & fname, pit_type pit, int par_id, pos_type par_pos, unsigned int idx);
+       void save(support::FileName const & fname, pit_type bottom_pit, pos_type bottom_pos,
+               int top_id, pos_type top_pos, unsigned int idx);
 
        /// return bookmark 0-9, bookmark 0 is the temporary bookmark
        Bookmark const & bookmark(unsigned int i) const;