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
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_);
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
}
-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);
}
/// 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;
}
// 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);
}
}
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 (...) {
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);
}
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;
}
};
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;