]> git.lyx.org Git - lyx.git/blob - src/session.C
Fix breakage caused by bad commits.
[lyx.git] / src / session.C
1 /**
2  * \file session.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author Bo Peng
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "session.h"
15 #include "debug.h"
16 #include "support/package.h"
17 #include "support/filetools.h"
18
19 #include <boost/filesystem/operations.hpp>
20
21 #include <fstream>
22 #include <sstream>
23 #include <algorithm>
24 #include <iterator>
25
26 using lyx::support::addName;
27 using lyx::support::package;
28
29 namespace fs = boost::filesystem;
30
31 using std::vector;
32 using std::getline;
33 using std::string;
34 using std::ifstream;
35 using std::ofstream;
36 using std::endl;
37 using std::istringstream;
38 using std::copy;
39 using std::find;
40 using std::ostream_iterator;
41
42 namespace lyx{
43
44 namespace {
45
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;
56
57 } // anon namespace
58
59
60 Session::Session(unsigned int num) :
61         default_num_last_files(4),
62         absolute_max_last_files(100),
63         num_lastfilepos(100)
64 {
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");
69         //
70         readFile();
71 }
72
73
74 void Session::setNumberOfLastFiles(unsigned int no)
75 {
76         if (0 < no && no <= absolute_max_last_files)
77                 num_lastfiles = no;
78         else {
79                 lyxerr << "LyX: session: too many last files\n"
80                        << "\tdefault (=" << default_num_last_files
81                        << ") used." << endl;
82                 num_lastfiles = default_num_last_files;
83         }
84 }
85
86
87 void Session::readFile()
88 {
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());
92         string tmp;
93         int section = -1;
94
95         // the following is currently not implemented very
96         // robustly. (Manually editing of the session file may crash lyx)
97         //
98         while (getline(ifs, tmp)) {
99                 // Ignore comments, empty line or line stats with ' '
100                 if (tmp == "" || tmp[0] == '#' || tmp[0] == ' ')
101                         continue;
102
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) {
115                         // read lastfiles
116                         if (!fs::exists(tmp) || lastfiles.size() >= num_lastfiles)
117                                 continue;
118                         lastfiles.push_back(tmp);
119                 } else if (section == id_lastfilepos) {
120                         // read lastfilepos
121                         // pos, file\n
122                         lyx::pit_type pit;
123                         lyx::pos_type pos;
124                         string fname;
125                         istringstream itmp(tmp);
126                         itmp >> pit;
127                         itmp.ignore(2);  // ignore ", "
128                         itmp >> pos;
129                         itmp.ignore(2);  // ignore ", "
130                         itmp >> fname;
131                         if (!fs::exists(fname) || lastfilepos.size() >= num_lastfilepos)
132                                 continue;
133                         lastfilepos[fname] = boost::tie(pit, pos);
134                 } else if (section == id_lastopened) {
135                         // read lastopened
136                         // files
137                         if (!fs::exists(tmp))
138                                 continue;
139                         lastopened.push_back(tmp);
140                 } else if (section == id_bookmarks) {
141                         // read bookmarks
142                         // bookmarkid, id, pos, file\n
143                         unsigned int num;
144                         unsigned int id;
145                         lyx::pos_type pos;
146                         string fname;
147                         istringstream itmp(tmp);
148                         itmp >> num;
149                         itmp.ignore(2);  // ignore ", "
150                         itmp >> id;
151                         itmp.ignore(2);  // ignore ", "
152                         itmp >> pos;
153                         itmp.ignore(2);  // ignore ", "
154                         itmp >> fname;
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;
165                 }
166         }
167 }
168
169
170 void Session::writeFile() const
171 {
172         ofstream ofs(session_file.c_str());
173         if (ofs) {
174                 ofs << "## Automatically generated lyx session file \n"
175                     << "## Editing this file manually may cause lyx to crash.\n";
176                 // first section
177                 ofs << '\n' << sec_lastfiles << '\n';
178                 copy(lastfiles.begin(), lastfiles.end(),
179                      ostream_iterator<string>(ofs, "\n"));
180                 // second section
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';
187                 }
188                 // third section
189                 ofs << '\n' << sec_lastopened << '\n';
190                 copy(lastopened.begin(), lastopened.end(),
191                      ostream_iterator<string>(ofs, "\n"));
192                 // fourth section
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';
201                 }
202                 // fifth section
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';
207                 }
208         } else
209                 lyxerr << "LyX: Warning: unable to save Session: "
210                        << session_file << endl;
211 }
212
213
214 void Session::addLastFile(string const & file)
215 {
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())
219                 lastfiles.erase(it);
220         lastfiles.push_front(file);
221         if (lastfiles.size() > num_lastfiles)
222                 lastfiles.pop_back();
223 }
224
225
226 void Session::saveFilePosition(string const & fname, FilePos pos)
227 {
228         lastfilepos[fname] = pos;
229 }
230
231
232 Session::FilePos Session::loadFilePosition(string const & fname) const
233 {
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
239         else
240                 return 0;
241 }
242
243
244 void Session::clearLastOpenedFiles()
245 {
246         lastopened.clear();
247 }
248
249
250 void Session::addLastOpenedFile(string const & file)
251 {
252         lastopened.push_back(file);
253 }
254
255
256 void Session::saveBookmark(Bookmark const & bookmark)
257 {
258         bookmarks.push_back(bookmark);
259 }
260
261
262 void Session::saveSessionInfo(string const & key, string const & value)
263 {
264         sessioninfo[key] = value;
265 }
266
267
268 string const Session::loadSessionInfo(string const & key, bool release)
269 {
270         MiscInfo::const_iterator pos = sessioninfo.find(key);
271         string value;
272         if (pos != sessioninfo.end())
273                 value = pos->second;
274         if (release)
275                 sessioninfo.erase(key);
276         return value;
277 }
278
279 }