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]";
43 LastFilesSection::LastFilesSection(unsigned int num
) :
44 default_num_last_files(4),
45 absolute_max_last_files(100)
47 setNumberOfLastFiles(num
);
51 void LastFilesSection::read(istream
& is
)
59 FileName
const file(tmp
);
60 if (tmp
== "" || tmp
[0] == '#' || tmp
[0] == ' ' || !file
.isAbsolute())
64 if (file
.exists() && !file
.isDirectory()
65 && lastfiles
.size() < num_lastfiles
)
66 lastfiles
.push_back(file
);
68 LYXERR(Debug::INIT
, "LyX: Warning: Ignore last file: " << tmp
);
73 void LastFilesSection::write(ostream
& os
) const
75 os
<< '\n' << sec_lastfiles
<< '\n';
76 copy(lastfiles
.begin(), lastfiles
.end(),
77 ostream_iterator
<FileName
>(os
, "\n"));
81 void LastFilesSection::add(FileName
const & file
)
83 // If file already exist, delete it and reinsert at front.
84 LastFiles::iterator it
= find(lastfiles
.begin(), lastfiles
.end(), file
);
85 if (it
!= lastfiles
.end())
87 lastfiles
.push_front(file
);
88 if (lastfiles
.size() > num_lastfiles
)
93 void LastFilesSection::setNumberOfLastFiles(unsigned int no
)
95 if (0 < no
&& no
<= absolute_max_last_files
)
98 LYXERR(Debug::INIT
, "LyX: session: too many last files\n"
99 << "\tdefault (=" << default_num_last_files
<< ") used.");
100 num_lastfiles
= default_num_last_files
;
105 void LastOpenedSection::read(istream
& is
)
113 FileName
const file(tmp
);
114 if (tmp
== "" || tmp
[0] == '#' || tmp
[0] == ' ' || !file
.isAbsolute())
117 if (file
.exists() && !file
.isDirectory())
118 lastopened
.push_back(file
);
120 LYXERR(Debug::INIT
, "LyX: Warning: Ignore last opened file: " << tmp
);
125 void LastOpenedSection::write(ostream
& os
) const
127 os
<< '\n' << sec_lastopened
<< '\n';
128 copy(lastopened
.begin(), lastopened
.end(),
129 ostream_iterator
<FileName
>(os
, "\n"));
133 void LastOpenedSection::add(FileName
const & file
)
135 lastopened
.push_back(file
);
139 void LastOpenedSection::clear()
145 void LastFilePosSection::read(istream
& is
)
153 if (tmp
== "" || tmp
[0] == '#' || tmp
[0] == ' ')
161 istringstream
itmp(tmp
);
163 itmp
.ignore(2); // ignore ", "
165 itmp
.ignore(2); // ignore ", "
166 getline(itmp
, fname
);
167 FileName
const file(fname
);
168 if (!file
.isAbsolute())
170 if (file
.exists() && !file
.isDirectory()
171 && lastfilepos
.size() < num_lastfilepos
)
172 lastfilepos
[file
] = filepos
;
174 LYXERR(Debug::INIT
, "LyX: Warning: Ignore pos of last file: " << fname
);
176 LYXERR(Debug::INIT
, "LyX: Warning: unknown pos of last file: " << tmp
);
182 void LastFilePosSection::write(ostream
& os
) const
184 os
<< '\n' << sec_lastfilepos
<< '\n';
185 for (FilePosMap::const_iterator file
= lastfilepos
.begin();
186 file
!= lastfilepos
.end(); ++file
) {
187 os
<< file
->second
.pit
<< ", " << file
->second
.pos
<< ", "
188 << file
->first
<< '\n';
193 void LastFilePosSection::save(FileName
const & fname
, FilePos
const & pos
)
195 lastfilepos
[fname
] = pos
;
199 LastFilePosSection::FilePos
LastFilePosSection::load(FileName
const & fname
) const
201 FilePosMap::const_iterator entry
= lastfilepos
.find(fname
);
202 // Has position information, return it.
203 if (entry
!= lastfilepos
.end())
204 return entry
->second
;
205 // Not found, return the first paragraph
210 void BookmarksSection::clear()
212 // keep bookmark[0], the temporary one
214 bookmarks
.resize(max_bookmarks
+ 1);
218 void BookmarksSection::read(istream
& is
)
226 if (tmp
== "" || tmp
[0] == '#' || tmp
[0] == ' ')
231 // idx, pit, pos, file\n
236 istringstream
itmp(tmp
);
238 itmp
.ignore(2); // ignore ", "
240 itmp
.ignore(2); // ignore ", "
242 itmp
.ignore(2); // ignore ", "
243 getline(itmp
, fname
);
244 FileName
const file(fname
);
245 if (!file
.isAbsolute())
247 // only load valid bookmarks
248 if (file
.exists() && !file
.isDirectory() && idx
<= max_bookmarks
)
249 bookmarks
[idx
] = Bookmark(file
, pit
, pos
, 0, 0);
251 LYXERR(Debug::INIT
, "LyX: Warning: Ignore bookmark of file: " << fname
);
253 LYXERR(Debug::INIT
, "LyX: Warning: unknown Bookmark info: " << tmp
);
259 void BookmarksSection::write(ostream
& os
) const
261 os
<< '\n' << sec_bookmarks
<< '\n';
262 for (size_t i
= 1; i
<= max_bookmarks
; ++i
) {
265 << bookmarks
[i
].bottom_pit
<< ", "
266 << bookmarks
[i
].bottom_pos
<< ", "
267 << bookmarks
[i
].filename
<< '\n';
272 void BookmarksSection::save(FileName
const & fname
,
273 pit_type bottom_pit
, pos_type bottom_pos
,
274 int top_id
, pos_type top_pos
, unsigned int idx
)
276 // silently ignore bookmarks when idx is out of range
277 if (idx
<= max_bookmarks
)
278 bookmarks
[idx
] = Bookmark(fname
, bottom_pit
, bottom_pos
, top_id
, top_pos
);
282 bool BookmarksSection::isValid(unsigned int i
) const
284 return i
<= max_bookmarks
&& !bookmarks
[i
].filename
.empty();
288 BookmarksSection::Bookmark
const & BookmarksSection::bookmark(unsigned int i
) const
294 void ToolbarSection::read(istream
& is
)
302 if (tmp
== "" || tmp
[0] == '#' || tmp
[0] == ' ')
306 // Read session info, saved as key/value pairs
307 // would better yell if pos returns npos
308 size_t pos
= tmp
.find_first_of(" = ");
309 // silently ignore lines without " = "
310 if (pos
!= string::npos
) {
312 item
.key
= tmp
.substr(0, pos
);
315 istringstream
value(tmp
.substr(pos
+ 3));
318 value
>> item
.info
.posx
;
319 value
>> item
.info
.posy
;
320 item
.info
.state
= ToolbarInfo::State(state
);
321 item
.info
.location
= ToolbarInfo::Location(location
);
322 toolbars
.push_back(item
);
324 LYXERR(Debug::INIT
, "LyX: Warning: Ignore toolbar info: " << tmp
);
326 LYXERR(Debug::INIT
, "LyX: Warning: unknown Toolbar info: " << tmp
);
329 // sort the toolbars by location, line and position
330 sort(toolbars
.begin(), toolbars
.end());
334 void ToolbarSection::write(ostream
& os
) const
336 os
<< '\n' << sec_toolbars
<< '\n';
337 for (ToolbarList::const_iterator tb
= toolbars
.begin();
338 tb
!= toolbars
.end(); ++tb
) {
339 os
<< tb
->key
<< " = "
340 << static_cast<int>(tb
->info
.state
) << " "
341 << static_cast<int>(tb
->info
.location
) << " "
342 << tb
->info
.posx
<< " "
343 << tb
->info
.posy
<< '\n';
348 ToolbarSection::ToolbarInfo
& ToolbarSection::load(string
const & name
)
350 for (ToolbarList::iterator tb
= toolbars
.begin();
351 tb
!= toolbars
.end(); ++tb
)
358 toolbars
.push_back(item
);
359 return toolbars
.back().info
;
363 bool operator<(ToolbarSection::ToolbarItem
const & a
, ToolbarSection::ToolbarItem
const & b
)
365 ToolbarSection::ToolbarInfo lhs
= a
.info
;
366 ToolbarSection::ToolbarInfo rhs
= b
.info
;
368 if (lhs
.state
!= rhs
.state
)
369 return static_cast<int>(lhs
.state
) < static_cast<int>(rhs
.state
);
370 // order of dock does not really matter
371 if (lhs
.location
!= rhs
.location
)
372 return static_cast<int>(lhs
.location
) < static_cast<int>(rhs
.location
);
373 // if the same dock, the order depends on position
374 if (lhs
.location
== ToolbarSection::ToolbarInfo::TOP
||
375 lhs
.location
== ToolbarSection::ToolbarInfo::BOTTOM
)
376 return lhs
.posy
< rhs
.posy
|| (lhs
.posy
== rhs
.posy
&& lhs
.posx
< rhs
.posx
);
377 else if (lhs
.location
== ToolbarSection::ToolbarInfo::LEFT
||
378 lhs
.location
== ToolbarSection::ToolbarInfo::RIGHT
)
379 return lhs
.posx
< rhs
.posx
|| (lhs
.posx
== rhs
.posx
&& lhs
.posy
< rhs
.posy
);
384 void SessionInfoSection::read(istream
& is
)
392 if (tmp
== "" || tmp
[0] == '#' || tmp
[0] == ' ')
396 // Read session info, saved as key/value pairs
397 // would better yell if pos returns npos
398 string::size_type pos
= tmp
.find_first_of(" = ");
399 // silently ignore lines without " = "
400 if (pos
!= string::npos
) {
401 string key
= tmp
.substr(0, pos
);
402 string value
= tmp
.substr(pos
+ 3);
403 sessioninfo
[key
] = value
;
405 LYXERR(Debug::INIT
, "LyX: Warning: Ignore session info: " << tmp
);
407 LYXERR(Debug::INIT
, "LyX: Warning: unknown Session info: " << tmp
);
413 void SessionInfoSection::write(ostream
& os
) const
415 os
<< '\n' << sec_session
<< '\n';
416 for (MiscInfo::const_iterator val
= sessioninfo
.begin();
417 val
!= sessioninfo
.end(); ++val
) {
418 os
<< val
->first
<< " = " << val
->second
<< '\n';
423 void SessionInfoSection::save(string
const & key
, string
const & value
)
425 sessioninfo
[key
] = value
;
429 string
const SessionInfoSection::load(string
const & key
, bool release
)
431 MiscInfo::const_iterator pos
= sessioninfo
.find(key
);
433 if (pos
!= sessioninfo
.end())
436 sessioninfo
.erase(key
);
441 Session::Session(unsigned int num
) :
444 // locate the session file
445 // note that the session file name 'session' is hard-coded
446 session_file
= FileName(addName(package().user_support().absFilename(), "session"));
452 void Session::readFile()
454 // we will not complain if we can't find session_file nor will
455 // we issue a warning. (Lgb)
456 ifstream
is(session_file
.toFilesystemEncoding().c_str());
459 while (getline(is
, tmp
)) {
460 // Ignore comments, empty line or line stats with ' '
461 if (tmp
== "" || tmp
[0] == '#' || tmp
[0] == ' ')
464 // Determine section id
465 if (tmp
== sec_lastfiles
)
466 lastFiles().read(is
);
467 else if (tmp
== sec_lastopened
)
468 lastOpened().read(is
);
469 else if (tmp
== sec_lastfilepos
)
470 lastFilePos().read(is
);
471 else if (tmp
== sec_bookmarks
)
472 bookmarks().read(is
);
473 else if (tmp
== sec_toolbars
)
475 else if (tmp
== sec_session
)
476 sessionInfo().read(is
);
478 LYXERR(Debug::INIT
, "LyX: Warning: unknown Session section: " << tmp
);
483 void Session::writeFile() const
485 ofstream
os(session_file
.toFilesystemEncoding().c_str());
487 os
<< "## Automatically generated lyx session file \n"
488 << "## Editing this file manually may cause lyx to crash.\n";
490 lastFiles().write(os
);
491 lastOpened().write(os
);
492 lastFilePos().write(os
);
493 bookmarks().write(os
);
494 toolbars().write(os
);
495 sessionInfo().write(os
);
497 LYXERR(Debug::INIT
, "LyX: Warning: unable to save Session: "