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 Angus Leeming
10 * \author Jürgen Vigna
12 * Full author contact details are available in file CREDITS.
20 #include "bufferlist.h"
21 #include "BufferView.h"
22 #include "buffer_funcs.h"
27 #include "LaTeXFeatures.h"
29 #include "lyxlayout.h"
32 #include "paragraph.h"
34 #include "frontends/Alert.h"
35 #include "frontends/Application.h"
36 #include "frontends/FileDialog.h"
37 #include "frontends/LyXView.h"
39 #include "support/filefilterlist.h"
40 #include "support/filetools.h"
41 #include "support/fontutils.h"
42 #include "support/forkedcall.h"
43 #include "support/fs_extras.h"
44 #include "support/lyxlib.h"
45 #include "support/package.h"
46 #include "support/path.h"
47 #include "support/systemcall.h"
49 #if !defined (HAVE_FORK)
53 #include <boost/shared_ptr.hpp>
54 #include <boost/filesystem/operations.hpp>
62 using support::addName;
63 using support::bformat;
64 using support::FileFilterList;
65 using support::FileName;
66 using support::ForkedProcess;
67 using support::isLyXFilename;
68 using support::libFileSearch;
69 using support::makeAbsPath;
70 using support::makeDisplayPath;
71 using support::onlyFilename;
72 using support::onlyPath;
73 using support::package;
74 using support::removeAutosaveFile;
75 using support::rename;
77 using support::Systemcall;
78 using support::tempName;
79 using support::unlink;
81 using boost::shared_ptr;
83 namespace Alert = frontend::Alert;
84 namespace fs = boost::filesystem;
86 using std::back_inserter;
93 using std::istream_iterator;
96 // this should be static, but I need it in buffer.C
97 bool quitting; // flag, that we are quitting the program
103 bool menuWrite(Buffer * buffer)
105 if (buffer->save()) {
106 LyX::ref().session().lastFiles().add(FileName(buffer->fileName()));
110 // FIXME: we don't tell the user *WHY* the save failed !!
112 docstring const file = makeDisplayPath(buffer->fileName(), 30);
114 docstring text = bformat(_("The document %1$s could not be saved.\n\n"
115 "Do you want to rename the document and try again?"), file);
116 int const ret = Alert::prompt(_("Rename and save?"),
117 text, 0, 1, _("&Rename"), _("&Cancel"));
120 return writeAs(buffer);
126 bool writeAs(Buffer * buffer, string const & newname)
128 string fname = buffer->fileName();
129 string const oldname = fname;
131 if (newname.empty()) {
134 FileDialog fileDlg(_("Choose a filename to save document as"),
135 LFUN_BUFFER_WRITE_AS,
136 make_pair(_("Documents|#o#O"), from_utf8(lyxrc.document_path)),
137 make_pair(_("Templates|#T#t"), from_utf8(lyxrc.template_path)));
139 if (!isLyXFilename(fname))
142 FileFilterList const filter (_("LyX Documents (*.lyx)"));
144 FileDialog::Result result =
145 fileDlg.save(from_utf8(onlyPath(fname)),
147 from_utf8(onlyFilename(fname)));
149 if (result.first == FileDialog::Later)
152 fname = to_utf8(result.second);
157 // Make sure the absolute filename ends with appropriate suffix
158 fname = makeAbsPath(fname);
159 if (!isLyXFilename(fname))
164 FileName const filename(fname);
165 if (fs::exists(filename.toFilesystemEncoding())) {
166 docstring const file = makeDisplayPath(fname, 30);
167 docstring text = bformat(_("The document %1$s already exists.\n\n"
168 "Do you want to over-write that document?"), file);
169 int const ret = Alert::prompt(_("Over-write document?"),
170 text, 0, 1, _("&Over-write"), _("&Cancel"));
176 // Ok, change the name of the buffer
177 buffer->setFileName(fname);
179 bool unnamed = buffer->isUnnamed();
180 buffer->setUnnamed(false);
182 if (!menuWrite(buffer)) {
183 buffer->setFileName(oldname);
184 buffer->setUnnamed(unnamed);
188 removeAutosaveFile(oldname);
195 class AutoSaveBuffer : public ForkedProcess {
198 AutoSaveBuffer(BufferView & bv, FileName const & fname)
199 : bv_(bv), fname_(fname) {}
201 virtual shared_ptr<ForkedProcess> clone() const
203 return shared_ptr<ForkedProcess>(new AutoSaveBuffer(*this));
209 virtual int generateChild();
216 int AutoSaveBuffer::start()
218 command_ = to_utf8(bformat(_("Auto-saving %1$s"), from_utf8(fname_.absFilename())));
219 return run(DontWait);
223 int AutoSaveBuffer::generateChild()
225 // tmp_ret will be located (usually) in /tmp
226 // will that be a problem?
227 pid_t const pid = fork(); // If you want to debug the autosave
228 // you should set pid to -1, and comment out the
230 if (pid == 0 || pid == -1) {
231 // pid = -1 signifies that lyx was unable
232 // to fork. But we will do the save
236 FileName const tmp_ret(tempName(string(), "lyxauto"));
237 if (!tmp_ret.empty()) {
238 bv_.buffer()->writeFile(tmp_ret);
239 // assume successful write of tmp_ret
240 if (!rename(tmp_ret, fname_)) {
242 // most likely couldn't move between filesystems
243 // unless write of tmp_ret failed
244 // so remove tmp file (if it exists)
252 // failed to write/rename tmp_ret so try writing direct
253 if (!bv_.buffer()->writeFile(fname_)) {
254 // It is dangerous to do this in the child,
255 // but safe in the parent, so...
257 // emit message signal.
258 bv_.buffer()->message(_("Autosave failed!"));
261 if (pid == 0) { // we are the child so...
271 void autoSave(BufferView * bv)
272 // should probably be moved into BufferList (Lgb)
273 // Perfect target for a thread...
278 if (bv->buffer()->isBakClean() || bv->buffer()->isReadonly()) {
279 // We don't save now, but we'll try again later
280 bv->buffer()->resetAutosaveTimers();
284 // emit message signal.
285 bv->buffer()->message(_("Autosaving current document..."));
287 // create autosave filename
288 string fname = bv->buffer()->filePath();
290 fname += onlyFilename(bv->buffer()->fileName());
293 AutoSaveBuffer autosave(*bv, FileName(fname));
296 bv->buffer()->markBakClean();
297 bv->buffer()->resetAutosaveTimers();
302 // Copyright CHT Software Service GmbH
305 // create new file with template
308 void newFile(BufferView * bv, string const & filename)
310 // Split argument by :
312 string tmpname = split(filename, name, ':');
313 lyxerr[Debug::INFO] << "Arg is " << filename
314 << "\nName is " << name
315 << "\nTemplate is " << tmpname << endl;
317 Buffer * const b = newFile(name, tmpname);
323 // Insert ascii file (if filename is empty, prompt for one)
324 void insertAsciiFile(BufferView * bv, string const & f, bool asParagraph)
329 // FIXME: We don't know the encoding of the file
330 docstring const tmpstr = from_utf8(getContentsOfAsciiFile(bv, f, asParagraph));
334 // clear the selection
335 LyXText const & text = bv->buffer()->text();
336 if (&text == bv->getLyXText())
337 bv->cursor().clearSelection();
339 bv->getLyXText()->insertStringAsParagraphs(bv->cursor(), tmpstr);
341 bv->getLyXText()->insertStringAsLines(bv->cursor(), tmpstr);
346 // Insert ascii file (if filename is empty, prompt for one)
347 string getContentsOfAsciiFile(BufferView * bv, string const & f, bool asParagraph)
352 FileDialog fileDlg(_("Select file to insert"),
353 (asParagraph) ? LFUN_FILE_INSERT_ASCII_PARA : LFUN_FILE_INSERT_ASCII);
355 FileDialog::Result result =
356 fileDlg.open(from_utf8(bv->buffer()->filePath()),
357 FileFilterList(), docstring());
359 if (result.first == FileDialog::Later)
362 fname = FileName(makeAbsPath(to_utf8(result.second)));
368 if (!fs::is_readable(fname.toFilesystemEncoding())) {
369 docstring const error = from_ascii(strerror(errno));
370 docstring const file = makeDisplayPath(fname.absFilename(), 50);
371 docstring const text = bformat(_("Could not read the specified document\n"
372 "%1$s\ndue to the error: %2$s"), file, error);
373 Alert::error(_("Could not read file"), text);
377 ifstream ifs(fname.toFilesystemEncoding().c_str());
379 docstring const error = from_ascii(strerror(errno));
380 docstring const file = makeDisplayPath(fname.absFilename(), 50);
381 docstring const text = bformat(_("Could not open the specified document\n"
382 "%1$s\ndue to the error: %2$s"), file, error);
383 Alert::error(_("Could not open file"), text);
387 ifs.unsetf(ios::skipws);
388 istream_iterator<char> ii(ifs);
389 istream_iterator<char> end;
390 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
391 // We use this until the compilers get better...
392 std::vector<char> tmp;
393 copy(ii, end, back_inserter(tmp));
394 string const tmpstr(tmp.begin(), tmp.end());
396 // This is what we want to use and what we will use once the
397 // compilers get good enough.
398 //string tmpstr(ii, end); // yet a reason for using std::string
399 // alternate approach to get the file into a string:
401 copy(ii, end, back_inserter(tmpstr));
408 // This function runs "configure" and then rereads lyx.defaults to
409 // reconfigure the automatic settings.
410 void reconfigure(LyXView & lv)
412 // emit message signal.
413 lv.message(_("Running configure..."));
415 // Run configure in user lyx directory
416 support::Path p(package().user_support());
417 string const configure_command = package().configure_command();
419 one.startscript(Systemcall::Wait, configure_command);
421 // emit message signal.
422 lv.message(_("Reloading configuration..."));
423 lyxrc.read(libFileSearch(string(), "lyxrc.defaults"));
424 // Re-read packages.lst
425 LaTeXFeatures::getAvailable();
427 Alert::information(_("System reconfigured"),
428 _("The system has been reconfigured.\n"
429 "You need to restart LyX to make use of any\n"
430 "updated document class specifications."));