3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
16 #include "support/package.h"
17 #include "support/filetools.h"
19 #include <boost/filesystem/operations.hpp>
26 using lyx::support::addName;
27 using lyx::support::package;
29 namespace fs = boost::filesystem;
37 using std::istringstream;
40 using std::ostream_iterator;
46 string const sec_lastfiles = "[recent files]";
47 string const sec_lastfilepos = "[cursor positions]";
48 string const sec_lastopened = "[last opened files]";
49 string const sec_bookmarks = "[bookmarks]";
50 string const sec_session = "[session info]";
51 int const id_lastfiles = 0;
52 int const id_lastfilepos = 1;
53 int const id_lastopened = 2;
54 int const id_bookmarks = 3;
55 int const id_session = 4;
60 Session::Session(unsigned int num) :
61 default_num_last_files(4),
62 absolute_max_last_files(100),
65 setNumberOfLastFiles(num);
66 // locate the session file
67 // note that the session file name 'session' is hard-coded
68 session_file = addName(package().user_support(), "session");
74 void Session::setNumberOfLastFiles(unsigned int no)
76 if (0 < no && no <= absolute_max_last_files)
79 lyxerr << "LyX: session: too many last files\n"
80 << "\tdefault (=" << default_num_last_files
82 num_lastfiles = default_num_last_files;
87 void Session::readFile()
89 // we will not complain if we can't find session_file nor will
90 // we issue a warning. (Lgb)
91 ifstream ifs(session_file.c_str());
95 // the following is currently not implemented very
96 // robustly. (Manually editing of the session file may crash lyx)
98 while (getline(ifs, tmp)) {
99 // Ignore comments, empty line or line stats with ' '
100 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
103 // Determine section id
104 if (tmp == sec_lastfiles) {
105 section = id_lastfiles;
106 } else if (tmp == sec_lastfilepos) {
107 section = id_lastfilepos;
108 } else if (tmp == sec_lastopened) {
109 section = id_lastopened;
110 } else if (tmp == sec_bookmarks) {
111 section = id_bookmarks;
112 } else if (tmp == sec_session) {
113 section = id_session;
114 } else if (section == id_lastfiles) {
116 if (!fs::exists(tmp) || lastfiles.size() >= num_lastfiles)
118 lastfiles.push_back(tmp);
119 } else if (section == id_lastfilepos) {
125 istringstream itmp(tmp);
127 itmp.ignore(2); // ignore ", "
129 itmp.ignore(2); // ignore ", "
131 if (!fs::exists(fname) || lastfilepos.size() >= num_lastfilepos)
133 lastfilepos[fname] = boost::tie(pit, pos);
134 } else if (section == id_lastopened) {
137 if (!fs::exists(tmp))
139 lastopened.push_back(tmp);
140 } else if (section == id_bookmarks) {
142 // bookmarkid, id, pos, file\n
147 istringstream itmp(tmp);
149 itmp.ignore(2); // ignore ", "
151 itmp.ignore(2); // ignore ", "
153 itmp.ignore(2); // ignore ", "
155 // only load valid bookmarks
156 if (fs::exists(fname))
157 bookmarks.push_back(boost::tie(num, fname, id, pos));
158 } else if (section == id_session) {
159 // Read session info, saved as key/value pairs
160 // would better yell if pos returns npos
161 string::size_type pos = tmp.find_first_of(" = ");
162 string key = tmp.substr(0, pos);
163 string value = tmp.substr(pos + 3);
164 sessioninfo[key] = value;
170 void Session::writeFile() const
172 ofstream ofs(session_file.c_str());
174 ofs << "## Automatically generated lyx session file \n"
175 << "## Editing this file manually may cause lyx to crash.\n";
177 ofs << '\n' << sec_lastfiles << '\n';
178 copy(lastfiles.begin(), lastfiles.end(),
179 ostream_iterator<string>(ofs, "\n"));
181 ofs << '\n' << sec_lastfilepos << '\n';
182 for (FilePosMap::const_iterator file = lastfilepos.begin();
183 file != lastfilepos.end(); ++file) {
184 ofs << file->second.get<0>() << ", "
185 << file->second.get<1>() << ", "
186 << file->first << '\n';
189 ofs << '\n' << sec_lastopened << '\n';
190 copy(lastopened.begin(), lastopened.end(),
191 ostream_iterator<string>(ofs, "\n"));
193 ofs << '\n' << sec_bookmarks << '\n';
194 for (BookmarkList::const_iterator bm = bookmarks.begin();
195 bm != bookmarks.end(); ++bm) {
196 // save bookmark number, id, pos, fname
197 ofs << bm->get<0>() << ", "
198 << bm->get<2>() << ", "
199 << bm->get<3>() << ", "
200 << bm->get<1>() << '\n';
203 ofs << '\n' << sec_session << '\n';
204 for (MiscInfo::const_iterator val = sessioninfo.begin();
205 val != sessioninfo.end(); ++val) {
206 ofs << val->first << " = " << val->second << '\n';
209 lyxerr << "LyX: Warning: unable to save Session: "
210 << session_file << endl;
214 void Session::addLastFile(string const & file)
216 // If file already exist, delete it and reinsert at front.
217 LastFiles::iterator it = find(lastfiles.begin(), lastfiles.end(), file);
218 if (it != lastfiles.end())
220 lastfiles.push_front(file);
221 if (lastfiles.size() > num_lastfiles)
222 lastfiles.pop_back();
226 void Session::saveFilePosition(string const & fname, FilePos pos)
228 lastfilepos[fname] = pos;
232 Session::FilePos Session::loadFilePosition(string const & fname) const
234 FilePosMap::const_iterator entry = lastfilepos.find(fname);
235 // Has position information, return it.
236 if (entry != lastfilepos.end())
237 return entry->second;
238 // Not found, return the first paragraph
244 void Session::clearLastOpenedFiles()
250 void Session::addLastOpenedFile(string const & file)
252 lastopened.push_back(file);
256 void Session::saveBookmark(Bookmark const & bookmark)
258 bookmarks.push_back(bookmark);
262 void Session::saveSessionInfo(string const & key, string const & value)
264 sessioninfo[key] = value;
268 string const Session::loadSessionInfo(string const & key, bool release)
270 MiscInfo::const_iterator pos = sessioninfo.find(key);
272 if (pos != sessioninfo.end())
275 sessioninfo.erase(key);