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"
|
2006-09-29 22:06:28 +00:00
|
|
|
|
#include "bufferlist.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
|
#include "BufferView.h"
|
2006-07-13 16:37:55 +00:00
|
|
|
|
#include "buffer_funcs.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"
|
2006-09-27 10:24:13 +00:00
|
|
|
|
#include "frontends/Application.h"
|
2001-03-28 14:51:25 +00:00
|
|
|
|
#include "frontends/FileDialog.h"
|
2006-11-01 13:14:21 +00:00
|
|
|
|
#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"
|
2006-10-02 10:48:03 +00:00
|
|
|
|
#include "support/fontutils.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-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
|
|
using support::addName;
|
|
|
|
|
using support::bformat;
|
|
|
|
|
using support::FileFilterList;
|
2006-11-26 21:30:39 +00:00
|
|
|
|
using support::FileName;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
using support::ForkedProcess;
|
|
|
|
|
using support::isLyXFilename;
|
|
|
|
|
using support::libFileSearch;
|
|
|
|
|
using support::makeAbsPath;
|
|
|
|
|
using support::makeDisplayPath;
|
|
|
|
|
using support::onlyFilename;
|
|
|
|
|
using support::onlyPath;
|
|
|
|
|
using support::package;
|
|
|
|
|
using support::removeAutosaveFile;
|
|
|
|
|
using support::rename;
|
|
|
|
|
using support::split;
|
|
|
|
|
using support::Systemcall;
|
|
|
|
|
using support::tempName;
|
|
|
|
|
using support::unlink;
|
2003-09-16 11:03:20 +00:00
|
|
|
|
|
2004-03-23 14:39:41 +00:00
|
|
|
|
using boost::shared_ptr;
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
namespace Alert = frontend::Alert;
|
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
|
|
|
|
// 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-12-02 16:07:15 +00:00
|
|
|
|
LyX::ref().session().lastFiles().add(FileName(buffer->fileName()));
|
2003-03-29 07:09:13 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME: we don't tell the user *WHY* the save failed !!
|
|
|
|
|
|
2006-09-11 08:54:10 +00:00
|
|
|
|
docstring const file = makeDisplayPath(buffer->fileName(), 30);
|
2003-03-29 07:09:13 +00:00
|
|
|
|
|
2006-09-11 08:54:10 +00:00
|
|
|
|
docstring text = bformat(_("The document %1$s could not be saved.\n\n"
|
|
|
|
|
"Do you want to rename the document and try again?"), file);
|
|
|
|
|
int const ret = Alert::prompt(_("Rename and save?"),
|
|
|
|
|
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-12-05 12:57:01 +00:00
|
|
|
|
bool writeAs(Buffer * buffer, string const & newname)
|
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
|
|
|
|
|
2006-12-05 12:57:01 +00:00
|
|
|
|
if (newname.empty()) {
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2006-10-09 16:33:20 +00:00
|
|
|
|
// FIXME UNICODE
|
|
|
|
|
FileDialog fileDlg(_("Choose a filename to save document as"),
|
2006-05-05 20:23:12 +00:00
|
|
|
|
LFUN_BUFFER_WRITE_AS,
|
2006-10-21 00:16:43 +00:00
|
|
|
|
make_pair(_("Documents|#o#O"), from_utf8(lyxrc.document_path)),
|
|
|
|
|
make_pair(_("Templates|#T#t"), from_utf8(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
|
|
|
|
|
2006-10-09 16:33:20 +00:00
|
|
|
|
FileFilterList const filter (_("LyX Documents (*.lyx)"));
|
2004-01-08 10:59:51 +00:00
|
|
|
|
|
2001-03-12 01:43:12 +00:00
|
|
|
|
FileDialog::Result result =
|
2006-10-21 00:16:43 +00:00
|
|
|
|
fileDlg.save(from_utf8(onlyPath(fname)),
|
2004-01-08 10:59:51 +00:00
|
|
|
|
filter,
|
2006-10-21 00:16:43 +00:00
|
|
|
|
from_utf8(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;
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
fname = to_utf8(result.second);
|
2001-03-07 14:25:31 +00:00
|
|
|
|
|
|
|
|
|
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
|
2006-12-05 12:57:01 +00:00
|
|
|
|
fname = newname;
|
2000-11-10 17:29:47 +00:00
|
|
|
|
|
2006-12-05 12:57:01 +00:00
|
|
|
|
FileName const filename(fname);
|
|
|
|
|
if (fs::exists(filename.toFilesystemEncoding())) {
|
2006-09-11 08:54:10 +00:00
|
|
|
|
docstring const file = makeDisplayPath(fname, 30);
|
|
|
|
|
docstring text = bformat(_("The document %1$s already exists.\n\n"
|
|
|
|
|
"Do you want to over-write that document?"), file);
|
|
|
|
|
int const ret = Alert::prompt(_("Over-write document?"),
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
///
|
2006-11-26 21:30:39 +00:00
|
|
|
|
AutoSaveBuffer(BufferView & bv, FileName const & fname)
|
2002-10-31 12:42:26 +00:00
|
|
|
|
: 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_;
|
2006-11-26 21:30:39 +00:00
|
|
|
|
FileName fname_;
|
2002-10-31 12:42:26 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int AutoSaveBuffer::start()
|
|
|
|
|
{
|
2006-11-26 21:30:39 +00:00
|
|
|
|
command_ = to_utf8(bformat(_("Auto-saving %1$s"), from_utf8(fname_.absFilename())));
|
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
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
|
FileName const tmp_ret(tempName(string(), "lyxauto"));
|
1999-09-27 18:44:28 +00:00
|
|
|
|
if (!tmp_ret.empty()) {
|
2006-11-30 16:59:50 +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
|
2006-11-30 16:59:50 +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)
|
2006-08-23 21:14:43 +00:00
|
|
|
|
// emit message signal.
|
2006-09-11 08:54:10 +00:00
|
|
|
|
bv_.buffer()->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...
|
|
|
|
|
{
|
2006-09-16 14:00:13 +00:00
|
|
|
|
if (!bv->buffer())
|
2002-10-31 12:42:26 +00:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (bv->buffer()->isBakClean() || bv->buffer()->isReadonly()) {
|
|
|
|
|
// We don't save now, but we'll try again later
|
2006-09-26 09:59:03 +00:00
|
|
|
|
bv->buffer()->resetAutosaveTimers();
|
2002-10-31 12:42:26 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-23 21:14:43 +00:00
|
|
|
|
// emit message signal.
|
2006-09-11 08:54:10 +00:00
|
|
|
|
bv->buffer()->message(_("Autosaving current document..."));
|
2002-10-31 12:42:26 +00:00
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2006-11-26 21:30:39 +00:00
|
|
|
|
AutoSaveBuffer autosave(*bv, FileName(fname));
|
2002-10-31 12:42:26 +00:00
|
|
|
|
autosave.start();
|
2002-11-25 01:15:10 +00:00
|
|
|
|
|
2000-04-11 22:55:29 +00:00
|
|
|
|
bv->buffer()->markBakClean();
|
2006-09-26 09:59:03 +00:00
|
|
|
|
bv->buffer()->resetAutosaveTimers();
|
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
|
|
|
|
|
2006-07-17 15:13:49 +00:00
|
|
|
|
Buffer * const b = newFile(name, tmpname);
|
|
|
|
|
if (b)
|
|
|
|
|
bv->setBuffer(b);
|
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
|
|
|
|
{
|
2006-09-16 14:00:13 +00:00
|
|
|
|
if (!bv->buffer())
|
2001-03-07 14:25:31 +00:00
|
|
|
|
return;
|
2002-03-04 15:42:54 +00:00
|
|
|
|
|
2006-09-03 07:02:38 +00:00
|
|
|
|
// FIXME: We don't know the encoding of the file
|
2006-10-21 00:16:43 +00:00
|
|
|
|
docstring const tmpstr = from_utf8(getContentsOfAsciiFile(bv, f, asParagraph));
|
2002-03-04 15:42:54 +00:00
|
|
|
|
if (tmpstr.empty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// clear the selection
|
2006-09-16 18:11:38 +00:00
|
|
|
|
LyXText const & text = bv->buffer()->text();
|
2006-09-16 13:34:09 +00:00
|
|
|
|
if (&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)
|
|
|
|
|
{
|
2006-12-04 14:45:17 +00:00
|
|
|
|
FileName fname(f);
|
2002-03-04 15:42:54 +00:00
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
|
if (fname.empty()) {
|
2006-10-09 16:33:20 +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 =
|
2006-10-21 00:16:43 +00:00
|
|
|
|
fileDlg.open(from_utf8(bv->buffer()->filePath()),
|
2006-10-09 16:33:20 +00:00
|
|
|
|
FileFilterList(), docstring());
|
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
|
|
|
|
|
2006-12-04 14:45:17 +00:00
|
|
|
|
fname = FileName(makeAbsPath(to_utf8(result.second)));
|
2001-03-07 14:25:31 +00:00
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2006-12-04 14:45:17 +00:00
|
|
|
|
if (!fs::is_readable(fname.toFilesystemEncoding())) {
|
2006-10-21 00:16:43 +00:00
|
|
|
|
docstring const error = from_ascii(strerror(errno));
|
2006-12-04 14:45:17 +00:00
|
|
|
|
docstring const file = makeDisplayPath(fname.absFilename(), 50);
|
2006-09-11 08:54:10 +00:00
|
|
|
|
docstring const text = bformat(_("Could not read the specified document\n"
|
|
|
|
|
"%1$s\ndue to the error: %2$s"), file, error);
|
|
|
|
|
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
|
|
|
|
|
2006-12-04 14:45:17 +00:00
|
|
|
|
ifstream ifs(fname.toFilesystemEncoding().c_str());
|
2000-01-06 02:44:26 +00:00
|
|
|
|
if (!ifs) {
|
2006-10-21 00:16:43 +00:00
|
|
|
|
docstring const error = from_ascii(strerror(errno));
|
2006-12-04 14:45:17 +00:00
|
|
|
|
docstring const file = makeDisplayPath(fname.absFilename(), 50);
|
2006-09-11 08:54:10 +00:00
|
|
|
|
docstring const text = bformat(_("Could not open the specified document\n"
|
|
|
|
|
"%1$s\ndue to the error: %2$s"), file, error);
|
|
|
|
|
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-11-01 13:14:21 +00:00
|
|
|
|
void reconfigure(LyXView & lv)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2006-08-23 21:14:43 +00:00
|
|
|
|
// emit message signal.
|
2006-11-01 13:14:21 +00:00
|
|
|
|
lv.message(_("Running configure..."));
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
|
// Run configure in user lyx directory
|
2006-10-21 00:16:43 +00:00
|
|
|
|
support::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();
|
2006-08-23 21:14:43 +00:00
|
|
|
|
// emit message signal.
|
2006-11-01 13:14:21 +00:00
|
|
|
|
lv.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
|
|
|
|
|
2006-09-11 08:54:10 +00:00
|
|
|
|
Alert::information(_("System reconfigured"),
|
|
|
|
|
_("The system has been reconfigured.\n"
|
2006-09-09 15:27:44 +00:00
|
|
|
|
"You need to restart LyX to make use of any\n"
|
2006-09-11 08:54:10 +00:00
|
|
|
|
"updated document class specifications."));
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|