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/debug.h"
17 #include "support/filetools.h"
18 #include "support/Package.h"
26 using namespace lyx::support
;
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]";
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
)
60 if (tmp
.empty() || tmp
[0] == '#' || tmp
[0] == ' ' || !FileName::isAbsolute(tmp
))
64 FileName
const file(tmp
);
65 if (file
.exists() && !file
.isDirectory()
66 && lastfiles
.size() < num_lastfiles
)
67 lastfiles
.push_back(file
);
69 LYXERR(Debug::INIT
, "LyX: Warning: Ignore last file: " << tmp
);
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())
88 lastfiles
.push_front(file
);
89 if (lastfiles
.size() > num_lastfiles
)
94 void LastFilesSection::setNumberOfLastFiles(unsigned int no
)
96 if (0 < no
&& no
<= absolute_max_last_files
)
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
)
114 if (tmp
.empty() || tmp
[0] == '#' || tmp
[0] == ' ')
119 istringstream
itmp(tmp
);
121 itmp
.ignore(2); // ignore ", "
123 getline(itmp
, fname
);
124 if (!FileName::isAbsolute(fname
))
127 FileName
const file(fname
);
128 if (file
.exists() && !file
.isDirectory()) {
129 lof
.file_name
= file
;
130 lastopened
.push_back(lof
);
133 "LyX: Warning: Ignore last opened file: " << tmp
);
137 "LyX: Warning: unknown state of last opened file: " << tmp
);
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()
164 void LastFilePosSection::read(istream
& is
)
172 if (tmp
== "" || tmp
[0] == '#' || tmp
[0] == ' ')
180 istringstream
itmp(tmp
);
182 itmp
.ignore(2); // ignore ", "
184 itmp
.ignore(2); // ignore ", "
185 getline(itmp
, fname
);
186 if (!FileName::isAbsolute(fname
))
188 FileName
const file(fname
);
189 if (file
.exists() && !file
.isDirectory()
190 && lastfilepos
.size() < num_lastfilepos
)
191 lastfilepos
[file
] = filepos
;
193 LYXERR(Debug::INIT
, "LyX: Warning: Ignore pos of last file: " << fname
);
195 LYXERR(Debug::INIT
, "LyX: Warning: unknown pos of last file: " << tmp
);
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
229 void BookmarksSection::clear()
231 // keep bookmark[0], the temporary one
233 bookmarks
.resize(max_bookmarks
+ 1);
237 void BookmarksSection::read(istream
& is
)
245 if (tmp
== "" || tmp
[0] == '#' || tmp
[0] == ' ')
250 // idx, pit, pos, file\n
255 istringstream
itmp(tmp
);
257 itmp
.ignore(2); // ignore ", "
259 itmp
.ignore(2); // ignore ", "
261 itmp
.ignore(2); // ignore ", "
262 getline(itmp
, fname
);
263 if (!FileName::isAbsolute(fname
))
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);
270 LYXERR(Debug::INIT
, "LyX: Warning: Ignore bookmark of file: " << fname
);
272 LYXERR(Debug::INIT
, "LyX: Warning: unknown Bookmark info: " << tmp
);
278 void BookmarksSection::write(ostream
& os
) const
280 os
<< '\n' << sec_bookmarks
<< '\n';
281 for (size_t i
= 1; i
<= max_bookmarks
; ++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
) {
317 BookmarksSection::Bookmark
const & BookmarksSection::bookmark(unsigned int i
) const
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
)
339 if (tmp
== "" || tmp
[0] == '#' || tmp
[0] == ' ')
342 lastcommands
.push_back(tmp
);
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
;
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"));
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());
397 while (getline(is
, tmp
)) {
398 // Ignore comments, empty line or line stats with ' '
399 if (tmp
== "" || tmp
[0] == '#' || tmp
[0] == ' ')
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
);
415 LYXERR(Debug::INIT
, "LyX: Warning: unknown Session section: " << tmp
);
420 void Session::writeFile() const
422 ofstream
os(session_file
.toFilesystemEncoding().c_str());
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
);
433 LYXERR(Debug::INIT
, "LyX: Warning: unable to save Session: "