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/forkedcall.h"
42 #include "support/fs_extras.h"
43 #include "support/lyxlib.h"
44 #include "support/package.h"
45 #include "support/path.h"
46 #include "support/systemcall.h"
48 #if !defined (HAVE_FORK)
52 #include <boost/shared_ptr.hpp>
53 #include <boost/filesystem/operations.hpp>
61 using support::bformat;
62 using support::FileFilterList;
63 using support::FileName;
64 using support::ForkedProcess;
65 using support::isLyXFilename;
66 using support::libFileSearch;
67 using support::makeAbsPath;
68 using support::makeDisplayPath;
69 using support::onlyFilename;
70 using support::onlyPath;
71 using support::package;
72 using support::removeAutosaveFile;
73 using support::rename;
75 using support::Systemcall;
76 using support::tempName;
77 using support::unlink;
79 using boost::shared_ptr;
81 namespace Alert = frontend::Alert;
82 namespace fs = boost::filesystem;
84 using std::back_inserter;
91 using std::istream_iterator;
94 // this should be static, but I need it in buffer.C
95 bool quitting; // flag, that we are quitting the program
101 bool menuWrite(Buffer * buffer)
103 if (buffer->save()) {
104 LyX::ref().session().lastFiles().add(FileName(buffer->fileName()));
108 // FIXME: we don't tell the user *WHY* the save failed !!
110 docstring const file = makeDisplayPath(buffer->fileName(), 30);
112 docstring text = bformat(_("The document %1$s could not be saved.\n\n"
113 "Do you want to rename the document and try again?"), file);
114 int const ret = Alert::prompt(_("Rename and save?"),
115 text, 0, 1, _("&Rename"), _("&Cancel"));
118 return writeAs(buffer);
124 bool writeAs(Buffer * buffer, string const & newname)
126 string fname = buffer->fileName();
127 string const oldname = fname;
129 if (newname.empty()) {
132 FileDialog fileDlg(_("Choose a filename to save document as"),
133 LFUN_BUFFER_WRITE_AS,
134 make_pair(_("Documents|#o#O"), from_utf8(lyxrc.document_path)),
135 make_pair(_("Templates|#T#t"), from_utf8(lyxrc.template_path)));
137 if (!isLyXFilename(fname))
140 FileFilterList const filter (_("LyX Documents (*.lyx)"));
142 FileDialog::Result result =
143 fileDlg.save(from_utf8(onlyPath(fname)),
145 from_utf8(onlyFilename(fname)));
147 if (result.first == FileDialog::Later)
150 fname = to_utf8(result.second);
155 // Make sure the absolute filename ends with appropriate suffix
156 fname = makeAbsPath(fname).absFilename();
157 if (!isLyXFilename(fname))
162 FileName const filename(fname);
163 if (fs::exists(filename.toFilesystemEncoding())) {
164 docstring const file = makeDisplayPath(fname, 30);
165 docstring text = bformat(_("The document %1$s already exists.\n\n"
166 "Do you want to over-write that document?"), file);
167 int const ret = Alert::prompt(_("Over-write document?"),
168 text, 0, 1, _("&Over-write"), _("&Cancel"));
174 // Ok, change the name of the buffer
175 buffer->setFileName(fname);
177 bool unnamed = buffer->isUnnamed();
178 buffer->setUnnamed(false);
180 if (!menuWrite(buffer)) {
181 buffer->setFileName(oldname);
182 buffer->setUnnamed(unnamed);
186 removeAutosaveFile(oldname);
193 class AutoSaveBuffer : public ForkedProcess {
196 AutoSaveBuffer(BufferView & bv, FileName const & fname)
197 : bv_(bv), fname_(fname) {}
199 virtual shared_ptr<ForkedProcess> clone() const
201 return shared_ptr<ForkedProcess>(new AutoSaveBuffer(*this));
207 virtual int generateChild();
214 int AutoSaveBuffer::start()
216 command_ = to_utf8(bformat(_("Auto-saving %1$s"), from_utf8(fname_.absFilename())));
217 return run(DontWait);
221 int AutoSaveBuffer::generateChild()
223 // tmp_ret will be located (usually) in /tmp
224 // will that be a problem?
225 pid_t const pid = fork(); // If you want to debug the autosave
226 // you should set pid to -1, and comment out the
228 if (pid == 0 || pid == -1) {
229 // pid = -1 signifies that lyx was unable
230 // to fork. But we will do the save
234 FileName const tmp_ret(tempName(FileName(), "lyxauto"));
235 if (!tmp_ret.empty()) {
236 bv_.buffer()->writeFile(tmp_ret);
237 // assume successful write of tmp_ret
238 if (!rename(tmp_ret, fname_)) {
240 // most likely couldn't move between filesystems
241 // unless write of tmp_ret failed
242 // so remove tmp file (if it exists)
250 // failed to write/rename tmp_ret so try writing direct
251 if (!bv_.buffer()->writeFile(fname_)) {
252 // It is dangerous to do this in the child,
253 // but safe in the parent, so...
255 // emit message signal.
256 bv_.buffer()->message(_("Autosave failed!"));
259 if (pid == 0) { // we are the child so...
269 void autoSave(BufferView * bv)
270 // should probably be moved into BufferList (Lgb)
271 // Perfect target for a thread...
276 if (bv->buffer()->isBakClean() || bv->buffer()->isReadonly()) {
277 // We don't save now, but we'll try again later
278 bv->buffer()->resetAutosaveTimers();
282 // emit message signal.
283 bv->buffer()->message(_("Autosaving current document..."));
285 // create autosave filename
286 string fname = bv->buffer()->filePath();
288 fname += onlyFilename(bv->buffer()->fileName());
291 AutoSaveBuffer autosave(*bv, FileName(fname));
294 bv->buffer()->markBakClean();
295 bv->buffer()->resetAutosaveTimers();
300 // Copyright CHT Software Service GmbH
303 // create new file with template
306 void newFile(BufferView * bv, string const & filename)
308 // Split argument by :
310 string tmpname = split(filename, name, ':');
311 lyxerr[Debug::INFO] << "Arg is " << filename
312 << "\nName is " << name
313 << "\nTemplate is " << tmpname << endl;
315 Buffer * const b = newFile(name, tmpname);
321 // Insert plain text file (if filename is empty, prompt for one)
322 void insertPlaintextFile(BufferView * bv, string const & f, bool asParagraph)
327 // FIXME: We don't know the encoding of the file
328 docstring const tmpstr = from_utf8(getContentsOfPlaintextFile(bv, f, asParagraph));
332 // clear the selection
333 LyXText const & text = bv->buffer()->text();
334 if (&text == bv->cursor().innerText())
335 bv->cursor().clearSelection();
337 bv->cursor().innerText()->insertStringAsParagraphs(bv->cursor(), tmpstr);
339 bv->cursor().innerText()->insertStringAsLines(bv->cursor(), tmpstr);
343 // Insert plain text file (if filename is empty, prompt for one)
344 string getContentsOfPlaintextFile(BufferView * bv, string const & f, bool asParagraph)
349 FileDialog fileDlg(_("Select file to insert"),
350 (asParagraph) ? LFUN_FILE_INSERT_PLAINTEXT_PARA : LFUN_FILE_INSERT_PLAINTEXT);
352 FileDialog::Result result =
353 fileDlg.open(from_utf8(bv->buffer()->filePath()),
354 FileFilterList(), docstring());
356 if (result.first == FileDialog::Later)
359 fname = makeAbsPath(to_utf8(result.second));
365 if (!fs::is_readable(fname.toFilesystemEncoding())) {
366 docstring const error = from_ascii(strerror(errno));
367 docstring const file = makeDisplayPath(fname.absFilename(), 50);
368 docstring const text = bformat(_("Could not read the specified document\n"
369 "%1$s\ndue to the error: %2$s"), file, error);
370 Alert::error(_("Could not read file"), text);
374 ifstream ifs(fname.toFilesystemEncoding().c_str());
376 docstring const error = from_ascii(strerror(errno));
377 docstring const file = makeDisplayPath(fname.absFilename(), 50);
378 docstring const text = bformat(_("Could not open the specified document\n"
379 "%1$s\ndue to the error: %2$s"), file, error);
380 Alert::error(_("Could not open file"), text);
384 ifs.unsetf(ios::skipws);
385 istream_iterator<char> ii(ifs);
386 istream_iterator<char> end;
387 #if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
388 // We use this until the compilers get better...
389 std::vector<char> tmp;
390 copy(ii, end, back_inserter(tmp));
391 string const tmpstr(tmp.begin(), tmp.end());
393 // This is what we want to use and what we will use once the
394 // compilers get good enough.
395 //string tmpstr(ii, end); // yet a reason for using std::string
396 // alternate approach to get the file into a string:
398 copy(ii, end, back_inserter(tmpstr));
405 // This function runs "configure" and then rereads lyx.defaults to
406 // reconfigure the automatic settings.
407 void reconfigure(LyXView & lv)
409 // emit message signal.
410 lv.message(_("Running configure..."));
412 // Run configure in user lyx directory
413 support::Path p(package().user_support());
414 string const configure_command = package().configure_command();
416 one.startscript(Systemcall::Wait, configure_command);
418 // emit message signal.
419 lv.message(_("Reloading configuration..."));
420 lyxrc.read(libFileSearch(string(), "lyxrc.defaults"));
421 // Re-read packages.lst
422 LaTeXFeatures::getAvailable();
424 Alert::information(_("System reconfigured"),
425 _("The system has been reconfigured.\n"
426 "You need to restart LyX to make use of any\n"
427 "updated document class specifications."));