2003-08-23 00:17:00 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file lyx_cb.C
|
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
2002-03-21 17:27:08 +00:00
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* \author Lars Gullik Bj<EFBFBD>nnes
|
|
|
|
|
* \author Angus Leeming
|
|
|
|
|
* \author John Levon
|
|
|
|
|
* \author Andr<EFBFBD> P<EFBFBD>nitz
|
|
|
|
|
* \author J<EFBFBD>rgen Vigna
|
2002-03-21 17:27:08 +00:00
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
|
*/
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
|
|
#include "lyx_cb.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
|
|
2001-07-29 15:34:18 +00:00
|
|
|
|
#include "buffer.h"
|
2001-03-28 14:51:25 +00:00
|
|
|
|
#include "bufferlist.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
|
#include "BufferView.h"
|
2004-01-20 14:25:24 +00:00
|
|
|
|
#include "cursor.h"
|
2001-07-29 15:34:18 +00:00
|
|
|
|
#include "debug.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
|
#include "gettext.h"
|
2006-04-05 22:56:18 +00:00
|
|
|
|
#include "session.h"
|
2005-01-28 14:50:23 +00:00
|
|
|
|
#include "LaTeXFeatures.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
|
#include "lyx_main.h"
|
2003-09-09 11:24:33 +00:00
|
|
|
|
#include "lyxlayout.h"
|
2001-03-28 14:51:25 +00:00
|
|
|
|
#include "lyxrc.h"
|
|
|
|
|
#include "lyxtext.h"
|
2003-09-06 17:23:08 +00:00
|
|
|
|
#include "paragraph.h"
|
2001-12-28 13:26:54 +00:00
|
|
|
|
|
2001-11-26 10:19:58 +00:00
|
|
|
|
#include "frontends/Alert.h"
|
2001-03-28 14:51:25 +00:00
|
|
|
|
#include "frontends/FileDialog.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
|
#include "frontends/lyx_gui.h"
|
|
|
|
|
#include "frontends/LyXView.h"
|
2001-12-28 13:26:54 +00:00
|
|
|
|
|
2004-11-25 11:08:32 +00:00
|
|
|
|
#include "support/filefilterlist.h"
|
2000-04-12 14:20:08 +00:00
|
|
|
|
#include "support/filetools.h"
|
2002-10-31 12:42:26 +00:00
|
|
|
|
#include "support/forkedcall.h"
|
2005-01-31 10:42:26 +00:00
|
|
|
|
#include "support/fs_extras.h"
|
2003-09-04 01:44:16 +00:00
|
|
|
|
#include "support/lyxlib.h"
|
2005-01-10 19:17:43 +00:00
|
|
|
|
#include "support/package.h"
|
2000-04-12 14:20:08 +00:00
|
|
|
|
#include "support/path.h"
|
2002-02-18 19:13:48 +00:00
|
|
|
|
#include "support/systemcall.h"
|
2001-12-28 13:26:54 +00:00
|
|
|
|
|
2005-05-05 21:08:18 +00:00
|
|
|
|
#if !defined (HAVE_FORK)
|
2005-05-02 13:35:30 +00:00
|
|
|
|
# define fork() -1
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-03-23 14:39:41 +00:00
|
|
|
|
#include <boost/shared_ptr.hpp>
|
2005-01-31 10:42:26 +00:00
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
2004-03-23 14:39:41 +00:00
|
|
|
|
|
2003-09-05 22:17:02 +00:00
|
|
|
|
#include <cerrno>
|
2001-12-28 13:26:54 +00:00
|
|
|
|
#include <fstream>
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
|
using lyx::support::addName;
|
2003-09-09 22:13:45 +00:00
|
|
|
|
using lyx::support::bformat;
|
|
|
|
|
using lyx::support::destroyDir;
|
2004-01-08 10:59:51 +00:00
|
|
|
|
using lyx::support::FileFilterList;
|
2003-09-09 22:13:45 +00:00
|
|
|
|
using lyx::support::ForkedProcess;
|
2006-04-08 09:09:57 +00:00
|
|
|
|
using lyx::support::isLyXFilename;
|
2006-04-08 22:31:11 +00:00
|
|
|
|
using lyx::support::libFileSearch;
|
|
|
|
|
using lyx::support::makeAbsPath;
|
|
|
|
|
using lyx::support::makeDisplayPath;
|
|
|
|
|
using lyx::support::onlyFilename;
|
|
|
|
|
using lyx::support::onlyPath;
|
2003-09-09 22:13:45 +00:00
|
|
|
|
using lyx::support::Path;
|
2005-01-10 19:17:43 +00:00
|
|
|
|
using lyx::support::package;
|
2006-04-08 22:31:11 +00:00
|
|
|
|
using lyx::support::quoteName;
|
2003-09-09 22:13:45 +00:00
|
|
|
|
using lyx::support::removeAutosaveFile;
|
|
|
|
|
using lyx::support::rename;
|
|
|
|
|
using lyx::support::split;
|
|
|
|
|
using lyx::support::Systemcall;
|
|
|
|
|
using lyx::support::tempName;
|
|
|
|
|
using lyx::support::unlink;
|
2003-09-16 11:03:20 +00:00
|
|
|
|
|
2004-03-23 14:39:41 +00:00
|
|
|
|
using boost::shared_ptr;
|
|
|
|
|
|
2005-01-31 10:42:26 +00:00
|
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
|
|
2003-09-08 00:33:41 +00:00
|
|
|
|
using std::back_inserter;
|
2000-03-28 02:18:55 +00:00
|
|
|
|
using std::copy;
|
|
|
|
|
using std::endl;
|
2003-09-08 00:33:41 +00:00
|
|
|
|
using std::make_pair;
|
2003-10-06 15:43:21 +00:00
|
|
|
|
using std::string;
|
2003-09-08 00:33:41 +00:00
|
|
|
|
using std::ifstream;
|
2001-03-12 08:52:37 +00:00
|
|
|
|
using std::ios;
|
2000-03-28 02:18:55 +00:00
|
|
|
|
using std::istream_iterator;
|
|
|
|
|
|
2003-09-09 22:13:45 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
extern BufferList bufferlist;
|
|
|
|
|
// this should be static, but I need it in buffer.C
|
|
|
|
|
bool quitting; // flag, that we are quitting the program
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Menu callbacks
|
|
|
|
|
//
|
|
|
|
|
|
2006-04-09 00:26:19 +00:00
|
|
|
|
bool menuWrite(Buffer * buffer)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2003-03-29 07:09:13 +00:00
|
|
|
|
if (buffer->save()) {
|
2006-04-05 22:56:18 +00:00
|
|
|
|
LyX::ref().session().addLastFile(buffer->fileName());
|
2003-03-29 07:09:13 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME: we don't tell the user *WHY* the save failed !!
|
|
|
|
|
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const file = makeDisplayPath(buffer->fileName(), 30);
|
2003-03-29 07:09:13 +00:00
|
|
|
|
|
2003-05-13 09:48:57 +00:00
|
|
|
|
string text = bformat(_("The document %1$s could not be saved.\n\n"
|
|
|
|
|
"Do you want to rename the document and try again?"), file);
|
2003-03-29 07:09:13 +00:00
|
|
|
|
int const ret = Alert::prompt(_("Rename and save?"),
|
2003-04-27 16:40:50 +00:00
|
|
|
|
text, 0, 1, _("&Rename"), _("&Cancel"));
|
2003-03-29 07:09:13 +00:00
|
|
|
|
|
|
|
|
|
if (ret == 0)
|
2006-04-09 00:26:19 +00:00
|
|
|
|
return writeAs(buffer);
|
2003-03-29 07:09:13 +00:00
|
|
|
|
return false;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-03-07 14:25:31 +00:00
|
|
|
|
|
2006-04-09 00:26:19 +00:00
|
|
|
|
bool writeAs(Buffer * buffer, string const & filename)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
1999-12-10 00:07:59 +00:00
|
|
|
|
string fname = buffer->fileName();
|
2001-12-28 13:26:54 +00:00
|
|
|
|
string const oldname = fname;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2001-03-07 14:25:31 +00:00
|
|
|
|
if (filename.empty()) {
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-02-21 12:22:25 +00:00
|
|
|
|
FileDialog fileDlg(_("Choose a filename to save document as"),
|
2006-05-05 20:23:12 +00:00
|
|
|
|
LFUN_BUFFER_WRITE_AS,
|
2002-01-13 15:01:25 +00:00
|
|
|
|
make_pair(string(_("Documents|#o#O")),
|
2001-03-12 01:43:12 +00:00
|
|
|
|
string(lyxrc.document_path)),
|
2002-01-13 15:01:25 +00:00
|
|
|
|
make_pair(string(_("Templates|#T#t")),
|
2001-03-12 01:43:12 +00:00
|
|
|
|
string(lyxrc.template_path)));
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2006-04-08 09:09:57 +00:00
|
|
|
|
if (!isLyXFilename(fname))
|
2001-03-07 14:25:31 +00:00
|
|
|
|
fname += ".lyx";
|
2000-11-10 17:29:47 +00:00
|
|
|
|
|
2004-01-08 10:59:51 +00:00
|
|
|
|
FileFilterList const filter (_("LyX Documents (*.lyx)"));
|
|
|
|
|
|
2001-03-12 01:43:12 +00:00
|
|
|
|
FileDialog::Result result =
|
2006-04-08 22:31:11 +00:00
|
|
|
|
fileDlg.save(onlyPath(fname),
|
2004-01-08 10:59:51 +00:00
|
|
|
|
filter,
|
2006-04-08 22:31:11 +00:00
|
|
|
|
onlyFilename(fname));
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2001-03-07 14:25:31 +00:00
|
|
|
|
if (result.first == FileDialog::Later)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
fname = result.second;
|
|
|
|
|
|
|
|
|
|
if (fname.empty())
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// Make sure the absolute filename ends with appropriate suffix
|
2006-04-08 22:31:11 +00:00
|
|
|
|
fname = makeAbsPath(fname);
|
2006-04-08 09:09:57 +00:00
|
|
|
|
if (!isLyXFilename(fname))
|
2001-03-07 14:25:31 +00:00
|
|
|
|
fname += ".lyx";
|
|
|
|
|
} else
|
|
|
|
|
fname = filename;
|
2000-11-10 17:29:47 +00:00
|
|
|
|
|
2005-01-31 10:42:26 +00:00
|
|
|
|
if (fs::exists(fname)) {
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const file = makeDisplayPath(fname, 30);
|
2003-05-13 09:48:57 +00:00
|
|
|
|
string text = bformat(_("The document %1$s already exists.\n\n"
|
|
|
|
|
"Do you want to over-write that document?"), file);
|
2003-03-29 07:09:13 +00:00
|
|
|
|
int const ret = Alert::prompt(_("Over-write document?"),
|
2003-04-27 16:40:50 +00:00
|
|
|
|
text, 0, 1, _("&Over-write"), _("&Cancel"));
|
2003-03-29 07:09:13 +00:00
|
|
|
|
|
|
|
|
|
if (ret == 1)
|
2000-07-26 13:43:16 +00:00
|
|
|
|
return false;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ok, change the name of the buffer
|
2001-03-07 14:25:31 +00:00
|
|
|
|
buffer->setFileName(fname);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
buffer->markDirty();
|
2000-08-08 15:36:25 +00:00
|
|
|
|
bool unnamed = buffer->isUnnamed();
|
2000-07-26 13:43:16 +00:00
|
|
|
|
buffer->setUnnamed(false);
|
2001-03-07 14:25:31 +00:00
|
|
|
|
|
2006-04-09 00:26:19 +00:00
|
|
|
|
if (!menuWrite(buffer)) {
|
2003-03-29 07:09:13 +00:00
|
|
|
|
buffer->setFileName(oldname);
|
|
|
|
|
buffer->setUnnamed(unnamed);
|
|
|
|
|
return false;
|
2000-08-08 15:36:25 +00:00
|
|
|
|
}
|
2003-03-29 07:09:13 +00:00
|
|
|
|
|
2000-08-08 15:36:25 +00:00
|
|
|
|
removeAutosaveFile(oldname);
|
2000-07-26 13:43:16 +00:00
|
|
|
|
return true;
|
2000-08-08 15:36:25 +00:00
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
|
2006-04-09 00:26:19 +00:00
|
|
|
|
void quitLyX(bool noask)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2001-06-27 14:10:35 +00:00
|
|
|
|
lyxerr[Debug::INFO] << "Running QuitLyX." << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-03-31 02:59:34 +00:00
|
|
|
|
if (lyx_gui::use_gui) {
|
2005-07-16 10:35:43 +00:00
|
|
|
|
if (!noask && !bufferlist.quitWriteAll())
|
2001-02-01 17:37:09 +00:00
|
|
|
|
return;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2006-04-05 22:56:18 +00:00
|
|
|
|
LyX::cref().session().writeFile();
|
2001-02-01 17:37:09 +00:00
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
// Set a flag that we do quitting from the program,
|
|
|
|
|
// so no refreshes are necessary.
|
|
|
|
|
quitting = true;
|
|
|
|
|
|
|
|
|
|
// close buffers first
|
|
|
|
|
bufferlist.closeAll();
|
|
|
|
|
|
|
|
|
|
// do any other cleanup procedures now
|
2005-01-10 19:17:43 +00:00
|
|
|
|
lyxerr[Debug::INFO] << "Deleting tmp dir " << package().temp_dir() << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2005-01-31 10:42:26 +00:00
|
|
|
|
if (!destroyDir(package().temp_dir())) {
|
|
|
|
|
string const msg =
|
|
|
|
|
bformat(_("Unable to remove the temporary directory %1$s"),
|
2005-01-10 19:17:43 +00:00
|
|
|
|
package().temp_dir());
|
2005-01-31 10:42:26 +00:00
|
|
|
|
Alert::warning(_("Unable to remove temporary directory"), msg);
|
2003-03-29 09:02:08 +00:00
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2006-06-07 20:08:37 +00:00
|
|
|
|
lyx_gui::exit(0);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-10-31 12:42:26 +00:00
|
|
|
|
namespace {
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2002-10-31 12:42:26 +00:00
|
|
|
|
class AutoSaveBuffer : public ForkedProcess {
|
|
|
|
|
public:
|
|
|
|
|
///
|
|
|
|
|
AutoSaveBuffer(BufferView & bv, string const & fname)
|
|
|
|
|
: bv_(bv), fname_(fname) {}
|
|
|
|
|
///
|
2004-03-23 14:39:41 +00:00
|
|
|
|
virtual shared_ptr<ForkedProcess> clone() const
|
|
|
|
|
{
|
|
|
|
|
return shared_ptr<ForkedProcess>(new AutoSaveBuffer(*this));
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
2002-10-31 12:42:26 +00:00
|
|
|
|
///
|
|
|
|
|
int start();
|
|
|
|
|
private:
|
|
|
|
|
///
|
|
|
|
|
virtual int generateChild();
|
|
|
|
|
///
|
|
|
|
|
BufferView & bv_;
|
|
|
|
|
string fname_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int AutoSaveBuffer::start()
|
|
|
|
|
{
|
2003-05-13 09:48:57 +00:00
|
|
|
|
command_ = bformat(_("Auto-saving %1$s"), fname_);
|
2004-03-26 23:55:33 +00:00
|
|
|
|
return run(DontWait);
|
2002-10-31 12:42:26 +00:00
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2002-10-31 12:42:26 +00:00
|
|
|
|
int AutoSaveBuffer::generateChild()
|
|
|
|
|
{
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// tmp_ret will be located (usually) in /tmp
|
|
|
|
|
// will that be a problem?
|
2000-11-14 02:01:57 +00:00
|
|
|
|
pid_t const pid = fork(); // If you want to debug the autosave
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// you should set pid to -1, and comment out the
|
|
|
|
|
// fork.
|
|
|
|
|
if (pid == 0 || pid == -1) {
|
|
|
|
|
// pid = -1 signifies that lyx was unable
|
|
|
|
|
// to fork. But we will do the save
|
|
|
|
|
// anyway.
|
|
|
|
|
bool failed = false;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
|
string const tmp_ret = tempName(string(), "lyxauto");
|
1999-09-27 18:44:28 +00:00
|
|
|
|
if (!tmp_ret.empty()) {
|
2002-10-31 12:42:26 +00:00
|
|
|
|
bv_.buffer()->writeFile(tmp_ret);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// assume successful write of tmp_ret
|
2003-06-30 23:56:22 +00:00
|
|
|
|
if (!rename(tmp_ret, fname_)) {
|
1999-09-27 18:44:28 +00:00
|
|
|
|
failed = true;
|
|
|
|
|
// most likely couldn't move between filesystems
|
|
|
|
|
// unless write of tmp_ret failed
|
|
|
|
|
// so remove tmp file (if it exists)
|
2003-06-30 23:56:22 +00:00
|
|
|
|
unlink(tmp_ret);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
failed = true;
|
|
|
|
|
}
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
if (failed) {
|
|
|
|
|
// failed to write/rename tmp_ret so try writing direct
|
2002-10-31 12:42:26 +00:00
|
|
|
|
if (!bv_.buffer()->writeFile(fname_)) {
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// It is dangerous to do this in the child,
|
|
|
|
|
// but safe in the parent, so...
|
|
|
|
|
if (pid == -1)
|
2002-10-31 12:42:26 +00:00
|
|
|
|
bv_.owner()->message(_("Autosave failed!"));
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (pid == 0) { // we are the child so...
|
|
|
|
|
_exit(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-10-31 12:42:26 +00:00
|
|
|
|
return pid;
|
|
|
|
|
}
|
2002-11-25 01:15:10 +00:00
|
|
|
|
|
2002-10-31 12:42:26 +00:00
|
|
|
|
} // namespace anon
|
|
|
|
|
|
|
|
|
|
|
2006-04-09 00:26:19 +00:00
|
|
|
|
void autoSave(BufferView * bv)
|
2002-10-31 12:42:26 +00:00
|
|
|
|
// should probably be moved into BufferList (Lgb)
|
|
|
|
|
// Perfect target for a thread...
|
|
|
|
|
{
|
|
|
|
|
if (!bv->available())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (bv->buffer()->isBakClean() || bv->buffer()->isReadonly()) {
|
|
|
|
|
// We don't save now, but we'll try again later
|
|
|
|
|
bv->owner()->resetAutosaveTimer();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bv->owner()->message(_("Autosaving current document..."));
|
|
|
|
|
|
|
|
|
|
// create autosave filename
|
|
|
|
|
string fname = bv->buffer()->filePath();
|
2002-11-27 10:30:28 +00:00
|
|
|
|
fname += '#';
|
2006-04-08 22:31:11 +00:00
|
|
|
|
fname += onlyFilename(bv->buffer()->fileName());
|
2002-11-27 10:30:28 +00:00
|
|
|
|
fname += '#';
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2002-10-31 12:42:26 +00:00
|
|
|
|
AutoSaveBuffer autosave(*bv, fname);
|
|
|
|
|
autosave.start();
|
2002-11-25 01:15:10 +00:00
|
|
|
|
|
2000-04-11 22:55:29 +00:00
|
|
|
|
bv->buffer()->markBakClean();
|
|
|
|
|
bv->owner()->resetAutosaveTimer();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2000-03-09 03:36:48 +00:00
|
|
|
|
// Copyright CHT Software Service GmbH
|
1999-09-27 18:44:28 +00:00
|
|
|
|
// Uwe C. Schroeder
|
|
|
|
|
//
|
|
|
|
|
// create new file with template
|
|
|
|
|
// SERVERCMD !
|
|
|
|
|
//
|
2006-04-09 00:26:19 +00:00
|
|
|
|
void newFile(BufferView * bv, string const & filename)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
|
|
|
|
// Split argument by :
|
1999-10-02 16:21:10 +00:00
|
|
|
|
string name;
|
1999-11-01 04:22:28 +00:00
|
|
|
|
string tmpname = split(filename, name, ':');
|
2001-06-27 14:10:35 +00:00
|
|
|
|
lyxerr[Debug::INFO] << "Arg is " << filename
|
|
|
|
|
<< "\nName is " << name
|
|
|
|
|
<< "\nTemplate is " << tmpname << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-07-07 08:37:02 +00:00
|
|
|
|
bv->newFile(name, tmpname);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Insert ascii file (if filename is empty, prompt for one)
|
2006-04-09 00:26:19 +00:00
|
|
|
|
void insertAsciiFile(BufferView * bv, string const & f, bool asParagraph)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2002-03-21 17:27:08 +00:00
|
|
|
|
if (!bv->available())
|
2001-03-07 14:25:31 +00:00
|
|
|
|
return;
|
2002-03-04 15:42:54 +00:00
|
|
|
|
|
|
|
|
|
string const tmpstr = getContentsOfAsciiFile(bv, f, asParagraph);
|
|
|
|
|
if (tmpstr.empty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// clear the selection
|
2003-11-28 08:55:12 +00:00
|
|
|
|
if (bv->text() == bv->getLyXText())
|
2004-01-20 14:25:24 +00:00
|
|
|
|
bv->cursor().clearSelection();
|
2003-11-10 09:06:48 +00:00
|
|
|
|
if (asParagraph)
|
2004-02-13 07:30:59 +00:00
|
|
|
|
bv->getLyXText()->insertStringAsParagraphs(bv->cursor(), tmpstr);
|
2003-11-10 09:06:48 +00:00
|
|
|
|
else
|
2004-02-13 07:30:59 +00:00
|
|
|
|
bv->getLyXText()->insertStringAsLines(bv->cursor(), tmpstr);
|
2003-08-04 09:06:35 +00:00
|
|
|
|
bv->update();
|
2002-03-04 15:42:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Insert ascii file (if filename is empty, prompt for one)
|
|
|
|
|
string getContentsOfAsciiFile(BufferView * bv, string const & f, bool asParagraph)
|
|
|
|
|
{
|
|
|
|
|
string fname = f;
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
if (fname.empty()) {
|
2003-02-21 12:22:25 +00:00
|
|
|
|
FileDialog fileDlg(_("Select file to insert"),
|
2001-03-07 14:25:31 +00:00
|
|
|
|
(asParagraph) ? LFUN_FILE_INSERT_ASCII_PARA : LFUN_FILE_INSERT_ASCII);
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2004-01-08 10:59:51 +00:00
|
|
|
|
FileDialog::Result result =
|
|
|
|
|
fileDlg.open(bv->owner()->buffer()->filePath(),
|
|
|
|
|
FileFilterList(), string());
|
2001-03-07 14:25:31 +00:00
|
|
|
|
|
|
|
|
|
if (result.first == FileDialog::Later)
|
2002-03-04 15:42:54 +00:00
|
|
|
|
return string();
|
2001-03-07 14:25:31 +00:00
|
|
|
|
|
|
|
|
|
fname = result.second;
|
|
|
|
|
|
2002-03-21 17:27:08 +00:00
|
|
|
|
if (fname.empty())
|
2002-03-04 15:42:54 +00:00
|
|
|
|
return string();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2005-01-31 10:42:26 +00:00
|
|
|
|
if (!fs::is_readable(fname)) {
|
2003-03-29 09:02:08 +00:00
|
|
|
|
string const error = strerror(errno);
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const file = makeDisplayPath(fname, 50);
|
2003-05-13 09:48:57 +00:00
|
|
|
|
string const text = bformat(_("Could not read the specified document\n"
|
|
|
|
|
"%1$s\ndue to the error: %2$s"), file, error);
|
2003-03-29 09:02:08 +00:00
|
|
|
|
Alert::error(_("Could not read file"), text);
|
2002-03-04 15:42:54 +00:00
|
|
|
|
return string();
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
1999-11-24 22:14:46 +00:00
|
|
|
|
|
2000-01-06 02:44:26 +00:00
|
|
|
|
ifstream ifs(fname.c_str());
|
|
|
|
|
if (!ifs) {
|
2003-03-29 09:02:08 +00:00
|
|
|
|
string const error = strerror(errno);
|
2006-04-08 22:31:11 +00:00
|
|
|
|
string const file = makeDisplayPath(fname, 50);
|
2003-05-13 09:48:57 +00:00
|
|
|
|
string const text = bformat(_("Could not open the specified document\n"
|
|
|
|
|
"%1$s\ndue to the error: %2$s"), file, error);
|
2003-03-29 09:02:08 +00:00
|
|
|
|
Alert::error(_("Could not open file"), text);
|
2002-03-04 15:42:54 +00:00
|
|
|
|
return string();
|
1999-11-24 22:14:46 +00:00
|
|
|
|
}
|
1999-11-15 12:01:38 +00:00
|
|
|
|
|
2000-03-28 02:18:55 +00:00
|
|
|
|
ifs.unsetf(ios::skipws);
|
|
|
|
|
istream_iterator<char> ii(ifs);
|
|
|
|
|
istream_iterator<char> end;
|
2000-07-24 21:49:58 +00:00
|
|
|
|
#if !defined(USE_INCLUDED_STRING) && !defined(STD_STRING_IS_GOOD)
|
|
|
|
|
// We use this until the compilers get better...
|
2003-09-08 00:33:41 +00:00
|
|
|
|
std::vector<char> tmp;
|
2000-07-24 21:49:58 +00:00
|
|
|
|
copy(ii, end, back_inserter(tmp));
|
2000-12-06 22:24:17 +00:00
|
|
|
|
string const tmpstr(tmp.begin(), tmp.end());
|
2000-07-24 21:49:58 +00:00
|
|
|
|
#else
|
|
|
|
|
// This is what we want to use and what we will use once the
|
2002-03-21 17:27:08 +00:00
|
|
|
|
// compilers get good enough.
|
2000-03-17 10:14:46 +00:00
|
|
|
|
//string tmpstr(ii, end); // yet a reason for using std::string
|
2000-03-16 04:29:22 +00:00
|
|
|
|
// alternate approach to get the file into a string:
|
2000-03-17 10:14:46 +00:00
|
|
|
|
string tmpstr;
|
|
|
|
|
copy(ii, end, back_inserter(tmpstr));
|
2000-07-24 21:49:58 +00:00
|
|
|
|
#endif
|
2002-03-21 17:27:08 +00:00
|
|
|
|
|
2002-03-04 15:42:54 +00:00
|
|
|
|
return tmpstr;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// This function runs "configure" and then rereads lyx.defaults to
|
|
|
|
|
// reconfigure the automatic settings.
|
2006-04-09 00:26:19 +00:00
|
|
|
|
void reconfigure(BufferView * bv)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2001-04-24 17:33:01 +00:00
|
|
|
|
bv->owner()->message(_("Running configure..."));
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
// Run configure in user lyx directory
|
2005-01-10 19:17:43 +00:00
|
|
|
|
Path p(package().user_support());
|
2006-04-22 11:58:02 +00:00
|
|
|
|
string const configure_command = package().configure_command();
|
2002-02-18 19:13:48 +00:00
|
|
|
|
Systemcall one;
|
2005-01-05 10:08:34 +00:00
|
|
|
|
one.startscript(Systemcall::Wait, configure_command);
|
1999-10-13 17:32:46 +00:00
|
|
|
|
p.pop();
|
2001-04-24 17:33:01 +00:00
|
|
|
|
bv->owner()->message(_("Reloading configuration..."));
|
2006-04-08 22:31:11 +00:00
|
|
|
|
lyxrc.read(libFileSearch(string(), "lyxrc.defaults"));
|
2005-01-28 14:50:23 +00:00
|
|
|
|
// Re-read packages.lst
|
|
|
|
|
LaTeXFeatures::getAvailable();
|
2003-03-29 09:02:08 +00:00
|
|
|
|
|
|
|
|
|
Alert::information(_("System reconfigured"),
|
|
|
|
|
_("The system has been reconfigured.\n"
|
2004-08-16 11:27:51 +00:00
|
|
|
|
"You need to restart LyX to make use of any\n"
|
2003-03-29 09:02:08 +00:00
|
|
|
|
"updated document class specifications."));
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|