Hi all,
This patch adjusts the session file to contain which file was last
opened. It'll look like this:
[last opened files]
0, C:/Users/Vincent/Documents/newfile36.lyx
1, C:/Users/Vincent/Documents/newfile34.lyx
0, C:/Users/Vincent/Documents/newfile33.lyx
0, C:/Users/Vincent/Documents/newfile30.lyx
Ok ?
Vincent
Index: src/frontends/qt4/GuiApplication.cpp
===================================================================
--- src/frontends/qt4/GuiApplication.cpp (revision 29527)
+++ src/frontends/qt4/GuiApplication.cpp (working copy)
@@ -1203,13 +1203,24 @@
return;
Session & session = theSession();
- vector<FileName> const & lastopened = session.lastOpened().getfiles();
+ LastOpenedSection::LastOpened const & lastopened =
+ session.lastOpened().getfiles();
+
+ FileName active_file;
// do not add to the lastfile list since these files are restored from
// last session, and should be already there (regular files), or should
// not be added at all (help files).
- for_each(lastopened.begin(), lastopened.end(),
- bind(&GuiView::loadDocument, current_view_, _1, false));
+ for (size_t i = 0; i < lastopened.size(); ++i) {
+ current_view_->loadDocument(lastopened[i].file_name, false);
+ if (lastopened[i].active)
+ active_file = lastopened[i].file_name;
+ }
+ // Restore last active buffer
+ Buffer * buffer = theBufferList().getBuffer(active_file);
+ if (buffer)
+ current_view_->setBuffer(buffer);
+
// clear this list to save a few bytes of RAM
session.lastOpened().clear();
}
Index: src/frontends/qt4/GuiView.cpp
===================================================================
--- src/frontends/qt4/GuiView.cpp (revision 29532)
+++ src/frontends/qt4/GuiView.cpp (working copy)
@@ -533,7 +533,8 @@
// it can happen that this event arrives without selecting the view,
// e.g. when clicking the close button on a background window.
setFocus();
- setCurrentWorkArea(currentMainWorkArea());
+ GuiWorkArea * active_wa = currentMainWorkArea();
+ setCurrentWorkArea(active_wa);
int splitter_count = d.splitter_->count();
for (; splitter_count; --splitter_count) {
@@ -546,11 +547,12 @@
twa->setCurrentIndex(0);
GuiWorkArea * wa = twa->currentWorkArea();
+ bool const is_active_wa = active_wa == wa;
Buffer * b = &wa->bufferView().buffer();
if (b->parent()) {
// This is a child document, just close the tab
// after saving but keep the file loaded.
- if (!closeBuffer(*b, true)) {
+ if (!closeBuffer(*b, true, is_active_wa)) {
closing_ = false;
close_event->ignore();
return;
@@ -593,7 +595,7 @@
}
// closeBuffer() needs buffer workArea still alive and
// set as currrent one, and destroys it
- if (b && !closeBuffer(*b, true)) {
+ if (b && !closeBuffer(*b, true, is_active_wa)) {
closing_ = false;
close_event->ignore();
return;
@@ -1900,7 +1902,7 @@
}
-bool GuiView::closeBuffer(Buffer & buf, bool tolastopened)
+bool GuiView::closeBuffer(Buffer & buf, bool tolastopened, bool mark_active)
{
// goto bookmark to update bookmark pit.
//FIXME: we should update only the bookmarks related to this buffer!
@@ -1913,7 +1915,7 @@
// do not save childs if their master
// is opened as well
if (tolastopened)
- theSession().lastOpened().add(buf.fileName());
+ theSession().lastOpened().add(buf.fileName(),
mark_active);
if (buf.parent())
// Don't close child documents.
removeWorkArea(currentMainWorkArea());
@@ -1957,7 +1959,7 @@
// save file names to .lyx/session
if (tolastopened)
- theSession().lastOpened().add(buf.fileName());
+ theSession().lastOpened().add(buf.fileName(), mark_active);
if (buf.parent())
// Don't close child documents.
Index: src/frontends/qt4/GuiView.h
===================================================================
--- src/frontends/qt4/GuiView.h (revision 29527)
+++ src/frontends/qt4/GuiView.h (working copy)
@@ -294,7 +294,9 @@
///
bool saveBuffer(Buffer & b);
///
- bool closeBuffer(Buffer & buf, bool tolastopened = false);
+ bool closeBuffer(Buffer & buf, bool tolastopened = false,
+ bool mark_active = false);
+ ///
enum NextOrPrevious {
NEXTBUFFER,
PREVBUFFER
Index: src/Session.cpp
===================================================================
--- src/Session.cpp (revision 29527)
+++ src/Session.cpp (working copy)
@@ -111,14 +111,31 @@
if (c == '[')
break;
getline(is, tmp);
- if (tmp.empty() || tmp[0] == '#' || tmp[0] == ' ' ||
!FileName::isAbsolute(tmp))
+ if (tmp.empty() || tmp[0] == '#' || tmp[0] == ' ')
continue;
- FileName const file(tmp);
- if (file.exists() && !file.isDirectory())
- lastopened.push_back(file);
- else
- LYXERR(Debug::INIT, "LyX: Warning: Ignore last opened
file: " << tmp);
+ try {
+ LastOpenedFile lof;
+ istringstream itmp(tmp);
+ itmp >> lof.active;
+ itmp.ignore(2); // ignore ", "
+ string fname;
+ getline(itmp, fname);
+ if (!FileName::isAbsolute(fname))
+ continue;
+
+ FileName const file(fname);
+ if (file.exists() && !file.isDirectory()) {
+ lof.file_name = file;
+ lastopened.push_back(lof);
+ } else {
+ LYXERR(Debug::INIT,
+ "LyX: Warning: Ignore last opened file:
" << tmp);
+ }
+ } catch (...) {
+ LYXERR(Debug::INIT,
+ "LyX: Warning: unknown state of last opened
file: " << tmp);
+ }
} while (is.good());
}
@@ -126,14 +143,15 @@
void LastOpenedSection::write(ostream & os) const
{
os << '\n' << sec_lastopened << '\n';
- copy(lastopened.begin(), lastopened.end(),
- ostream_iterator<FileName>(os, "\n"));
+ for (size_t i = 0; i < lastopened.size(); ++i)
+ os << lastopened[i].active << ", " << lastopened[i].file_name
<< '\n';
}
-void LastOpenedSection::add(FileName const & file)
+void LastOpenedSection::add(FileName const & file, bool active)
{
- lastopened.push_back(file);
+ LastOpenedFile lof(file, active);
+ lastopened.push_back(lof);
}
Index: src/Session.h
===================================================================
--- src/Session.h (revision 29527)
+++ src/Session.h (working copy)
@@ -106,8 +106,18 @@
{
public:
///
- typedef std::vector<support::FileName> LastOpened;
+ struct LastOpenedFile {
+ LastOpenedFile() : file_name(), active(false) {}
+ LastOpenedFile(support::FileName file_name, bool active)
+ : file_name(file_name), active(active) {}
+
+ support::FileName file_name;
+ bool active;
+ };
+ ///
+ typedef std::vector<LastOpenedFile> LastOpened;
+
public:
///
void read(std::istream & is);
@@ -121,7 +131,7 @@
/** add file to lastopened file list
@param file filename to add
*/
- void add(support::FileName const & file);
+ void add(support::FileName const & file, bool active = false);
/** clear lastopened file list
*/