* de.po: sync with branch.
[lyx.git] / src / Session.cpp
blob7f39bd6be354456b9ef49ef4788302181becce84
1 /**
2 * \file Session.cpp
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
7 * \author Bo Peng
9 * Full author contact details are available in file CREDITS.
12 #include <config.h>
14 #include "Session.h"
16 #include "support/debug.h"
17 #include "support/filetools.h"
18 #include "support/Package.h"
20 #include <fstream>
21 #include <sstream>
22 #include <algorithm>
23 #include <iterator>
25 using namespace std;
26 using namespace lyx::support;
28 namespace {
30 string const sec_lastfiles = "[recent files]";
31 string const sec_lastfilepos = "[cursor positions]";
32 string const sec_lastopened = "[last opened files]";
33 string const sec_bookmarks = "[bookmarks]";
34 string const sec_session = "[session info]";
35 string const sec_toolbars = "[toolbars]";
36 string const sec_lastcommands = "[last commands]";
38 } // anon namespace
41 namespace lyx {
44 LastFilesSection::LastFilesSection(unsigned int num) :
45 default_num_last_files(4),
46 absolute_max_last_files(100)
48 setNumberOfLastFiles(num);
52 void LastFilesSection::read(istream & is)
54 string tmp;
55 do {
56 char c = is.peek();
57 if (c == '[')
58 break;
59 getline(is, tmp);
60 if (tmp.empty() || tmp[0] == '#' || tmp[0] == ' ' || !FileName::isAbsolute(tmp))
61 continue;
63 // read lastfiles
64 FileName const file(tmp);
65 if (file.exists() && !file.isDirectory()
66 && lastfiles.size() < num_lastfiles)
67 lastfiles.push_back(file);
68 else
69 LYXERR(Debug::INIT, "LyX: Warning: Ignore last file: " << tmp);
70 } while (is.good());
74 void LastFilesSection::write(ostream & os) const
76 os << '\n' << sec_lastfiles << '\n';
77 copy(lastfiles.begin(), lastfiles.end(),
78 ostream_iterator<FileName>(os, "\n"));
82 void LastFilesSection::add(FileName const & file)
84 // If file already exist, delete it and reinsert at front.
85 LastFiles::iterator it = find(lastfiles.begin(), lastfiles.end(), file);
86 if (it != lastfiles.end())
87 lastfiles.erase(it);
88 lastfiles.push_front(file);
89 if (lastfiles.size() > num_lastfiles)
90 lastfiles.pop_back();
94 void LastFilesSection::setNumberOfLastFiles(unsigned int no)
96 if (0 < no && no <= absolute_max_last_files)
97 num_lastfiles = no;
98 else {
99 LYXERR(Debug::INIT, "LyX: session: too many last files\n"
100 << "\tdefault (=" << default_num_last_files << ") used.");
101 num_lastfiles = default_num_last_files;
106 void LastOpenedSection::read(istream & is)
108 string tmp;
109 do {
110 char c = is.peek();
111 if (c == '[')
112 break;
113 getline(is, tmp);
114 if (tmp.empty() || tmp[0] == '#' || tmp[0] == ' ')
115 continue;
117 try {
118 LastOpenedFile lof;
119 istringstream itmp(tmp);
120 itmp >> lof.active;
121 itmp.ignore(2); // ignore ", "
122 string fname;
123 getline(itmp, fname);
124 if (!FileName::isAbsolute(fname))
125 continue;
127 FileName const file(fname);
128 if (file.exists() && !file.isDirectory()) {
129 lof.file_name = file;
130 lastopened.push_back(lof);
131 } else {
132 LYXERR(Debug::INIT,
133 "LyX: Warning: Ignore last opened file: " << tmp);
135 } catch (...) {
136 LYXERR(Debug::INIT,
137 "LyX: Warning: unknown state of last opened file: " << tmp);
139 } while (is.good());
143 void LastOpenedSection::write(ostream & os) const
145 os << '\n' << sec_lastopened << '\n';
146 for (size_t i = 0; i < lastopened.size(); ++i)
147 os << lastopened[i].active << ", " << lastopened[i].file_name << '\n';
151 void LastOpenedSection::add(FileName const & file, bool active)
153 LastOpenedFile lof(file, active);
154 lastopened.push_back(lof);
158 void LastOpenedSection::clear()
160 lastopened.clear();
164 void LastFilePosSection::read(istream & is)
166 string tmp;
167 do {
168 char c = is.peek();
169 if (c == '[')
170 break;
171 getline(is, tmp);
172 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
173 continue;
175 try {
176 // read lastfilepos
177 // pos, file\n
178 FilePos filepos;
179 string fname;
180 istringstream itmp(tmp);
181 itmp >> filepos.pit;
182 itmp.ignore(2); // ignore ", "
183 itmp >> filepos.pos;
184 itmp.ignore(2); // ignore ", "
185 getline(itmp, fname);
186 if (!FileName::isAbsolute(fname))
187 continue;
188 FileName const file(fname);
189 if (file.exists() && !file.isDirectory()
190 && lastfilepos.size() < num_lastfilepos)
191 lastfilepos[file] = filepos;
192 else
193 LYXERR(Debug::INIT, "LyX: Warning: Ignore pos of last file: " << fname);
194 } catch (...) {
195 LYXERR(Debug::INIT, "LyX: Warning: unknown pos of last file: " << tmp);
197 } while (is.good());
201 void LastFilePosSection::write(ostream & os) const
203 os << '\n' << sec_lastfilepos << '\n';
204 for (FilePosMap::const_iterator file = lastfilepos.begin();
205 file != lastfilepos.end(); ++file) {
206 os << file->second.pit << ", " << file->second.pos << ", "
207 << file->first << '\n';
212 void LastFilePosSection::save(FileName const & fname, FilePos const & pos)
214 lastfilepos[fname] = pos;
218 LastFilePosSection::FilePos LastFilePosSection::load(FileName const & fname) const
220 FilePosMap::const_iterator entry = lastfilepos.find(fname);
221 // Has position information, return it.
222 if (entry != lastfilepos.end())
223 return entry->second;
224 // Not found, return the first paragraph
225 return FilePos();
229 void BookmarksSection::clear()
231 // keep bookmark[0], the temporary one
232 bookmarks.resize(1);
233 bookmarks.resize(max_bookmarks + 1);
237 void BookmarksSection::read(istream & is)
239 string tmp;
240 do {
241 char c = is.peek();
242 if (c == '[')
243 break;
244 getline(is, tmp);
245 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
246 continue;
248 try {
249 // read bookmarks
250 // idx, pit, pos, file\n
251 unsigned int idx;
252 pit_type pit;
253 pos_type pos;
254 string fname;
255 istringstream itmp(tmp);
256 itmp >> idx;
257 itmp.ignore(2); // ignore ", "
258 itmp >> pit;
259 itmp.ignore(2); // ignore ", "
260 itmp >> pos;
261 itmp.ignore(2); // ignore ", "
262 getline(itmp, fname);
263 if (!FileName::isAbsolute(fname))
264 continue;
265 FileName const file(fname);
266 // only load valid bookmarks
267 if (file.exists() && !file.isDirectory() && idx <= max_bookmarks)
268 bookmarks[idx] = Bookmark(file, pit, pos, 0, 0);
269 else
270 LYXERR(Debug::INIT, "LyX: Warning: Ignore bookmark of file: " << fname);
271 } catch (...) {
272 LYXERR(Debug::INIT, "LyX: Warning: unknown Bookmark info: " << tmp);
274 } while (is.good());
278 void BookmarksSection::write(ostream & os) const
280 os << '\n' << sec_bookmarks << '\n';
281 for (size_t i = 1; i <= max_bookmarks; ++i) {
282 if (isValid(i))
283 os << i << ", "
284 << bookmarks[i].bottom_pit << ", "
285 << bookmarks[i].bottom_pos << ", "
286 << bookmarks[i].filename << '\n';
291 void BookmarksSection::save(FileName const & fname,
292 pit_type bottom_pit, pos_type bottom_pos,
293 int top_id, pos_type top_pos, unsigned int idx)
295 // silently ignore bookmarks when idx is out of range
296 if (idx <= max_bookmarks)
297 bookmarks[idx] = Bookmark(fname, bottom_pit, bottom_pos, top_id, top_pos);
301 bool BookmarksSection::isValid(unsigned int i) const
303 return i <= max_bookmarks && !bookmarks[i].filename.empty();
307 bool BookmarksSection::hasValid() const
309 for (size_t i = 1; i <= size(); ++i) {
310 if (isValid(i))
311 return true;
313 return false;
317 BookmarksSection::Bookmark const & BookmarksSection::bookmark(unsigned int i) const
319 return bookmarks[i];
323 LastCommandsSection::LastCommandsSection(unsigned int num) :
324 default_num_last_commands(30),
325 absolute_max_last_commands(100)
327 setNumberOfLastCommands(num);
331 void LastCommandsSection::read(istream & is)
333 string tmp;
334 do {
335 char c = is.peek();
336 if (c == '[')
337 break;
338 getline(is, tmp);
339 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
340 continue;
342 lastcommands.push_back(tmp);
343 } while (is.good());
347 void LastCommandsSection::write(ostream & os) const
349 os << '\n' << sec_lastcommands << '\n';
350 copy(lastcommands.begin(), lastcommands.end(),
351 ostream_iterator<std::string>(os, "\n"));
355 void LastCommandsSection::setNumberOfLastCommands(unsigned int no)
357 if (0 < no && no <= absolute_max_last_commands)
358 num_lastcommands = no;
359 else {
360 LYXERR(Debug::INIT, "LyX: session: too many last commands\n"
361 << "\tdefault (=" << default_num_last_commands << ") used.");
362 num_lastcommands = default_num_last_commands;
367 void LastCommandsSection::add(std::string const & string)
369 lastcommands.push_back(string);
373 void LastCommandsSection::clear()
375 lastcommands.clear();
379 Session::Session(unsigned int num_last_files, unsigned int num_last_commands) :
380 last_files(num_last_files), last_commands(num_last_commands)
382 // locate the session file
383 // note that the session file name 'session' is hard-coded
384 session_file = FileName(addName(package().user_support().absFilename(), "session"));
386 readFile();
390 void Session::readFile()
392 // we will not complain if we can't find session_file nor will
393 // we issue a warning. (Lgb)
394 ifstream is(session_file.toFilesystemEncoding().c_str());
395 string tmp;
397 while (getline(is, tmp)) {
398 // Ignore comments, empty line or line stats with ' '
399 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
400 continue;
402 // Determine section id
403 if (tmp == sec_lastfiles)
404 lastFiles().read(is);
405 else if (tmp == sec_lastopened)
406 lastOpened().read(is);
407 else if (tmp == sec_lastfilepos)
408 lastFilePos().read(is);
409 else if (tmp == sec_bookmarks)
410 bookmarks().read(is);
411 else if (tmp == sec_lastcommands)
412 lastCommands().read(is);
414 else
415 LYXERR(Debug::INIT, "LyX: Warning: unknown Session section: " << tmp);
420 void Session::writeFile() const
422 ofstream os(session_file.toFilesystemEncoding().c_str());
423 if (os) {
424 os << "## Automatically generated lyx session file \n"
425 << "## Editing this file manually may cause lyx to crash.\n";
427 lastFiles().write(os);
428 lastOpened().write(os);
429 lastFilePos().write(os);
430 lastCommands().write(os);
431 bookmarks().write(os);
432 } else
433 LYXERR(Debug::INIT, "LyX: Warning: unable to save Session: "
434 << session_file);