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"
|
2001-07-29 15:34:18 +00:00
|
|
|
|
#include "debug.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
|
#include "gettext.h"
|
2001-03-28 14:51:25 +00:00
|
|
|
|
#include "lastfiles.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
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
|
#include "support/FileInfo.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"
|
2003-09-04 01:44:16 +00:00
|
|
|
|
#include "support/lyxlib.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
|
#include "support/os.h"
|
2000-04-12 14:20:08 +00:00
|
|
|
|
#include "support/path.h"
|
2003-07-27 21:04:02 +00:00
|
|
|
|
#include "support/path_defines.h"
|
2002-02-18 19:13:48 +00:00
|
|
|
|
#include "support/systemcall.h"
|
2001-12-28 13:26:54 +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
|
|
|
|
|
2003-09-09 22:13:45 +00:00
|
|
|
|
using lyx::support::AddName;
|
|
|
|
|
using lyx::support::bformat;
|
|
|
|
|
using lyx::support::destroyDir;
|
|
|
|
|
using lyx::support::FileInfo;
|
|
|
|
|
using lyx::support::ForkedProcess;
|
|
|
|
|
using lyx::support::IsLyXFilename;
|
|
|
|
|
using lyx::support::LibFileSearch;
|
|
|
|
|
using lyx::support::MakeAbsPath;
|
|
|
|
|
using lyx::support::MakeDisplayPath;
|
|
|
|
|
using lyx::support::OnlyFilename;
|
|
|
|
|
using lyx::support::OnlyPath;
|
|
|
|
|
using lyx::support::Path;
|
|
|
|
|
using lyx::support::removeAutosaveFile;
|
|
|
|
|
using lyx::support::rename;
|
|
|
|
|
using lyx::support::split;
|
|
|
|
|
using lyx::support::system_lyxdir;
|
|
|
|
|
using lyx::support::Systemcall;
|
|
|
|
|
using lyx::support::tempName;
|
|
|
|
|
using lyx::support::unlink;
|
|
|
|
|
using lyx::support::user_lyxdir;
|
2003-06-30 23:56:22 +00:00
|
|
|
|
|
2003-09-16 11:03:20 +00:00
|
|
|
|
namespace os = lyx::support::os;
|
|
|
|
|
|
2003-11-03 17:47:28 +00:00
|
|
|
|
using std::auto_ptr;
|
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
|
|
|
|
|
//
|
|
|
|
|
|
2003-06-10 14:39:45 +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()) {
|
2003-10-14 21:30:23 +00:00
|
|
|
|
LyX::ref().lastfiles().newFile(buffer->fileName());
|
2003-03-29 07:09:13 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FIXME: we don't tell the user *WHY* the save failed !!
|
|
|
|
|
|
|
|
|
|
string const file = MakeDisplayPath(buffer->fileName(), 30);
|
|
|
|
|
|
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)
|
2003-06-10 14:39:45 +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
|
|
|
|
|
2003-06-10 14:39:45 +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"),
|
2001-03-07 14:25:31 +00:00
|
|
|
|
LFUN_WRITEAS,
|
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
|
|
|
|
|
2001-03-07 14:25:31 +00:00
|
|
|
|
if (!IsLyXFilename(fname))
|
|
|
|
|
fname += ".lyx";
|
2000-11-10 17:29:47 +00:00
|
|
|
|
|
2001-03-12 01:43:12 +00:00
|
|
|
|
FileDialog::Result result =
|
2002-11-17 08:32:09 +00:00
|
|
|
|
fileDlg.save(OnlyPath(fname),
|
2003-01-23 16:23:43 +00:00
|
|
|
|
_("*.lyx| LyX Documents (*.lyx)"),
|
2001-03-12 01:43:12 +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
|
|
|
|
|
fname = MakeAbsPath(fname);
|
|
|
|
|
if (!IsLyXFilename(fname))
|
|
|
|
|
fname += ".lyx";
|
|
|
|
|
} else
|
|
|
|
|
fname = filename;
|
2000-11-10 17:29:47 +00:00
|
|
|
|
|
2003-03-29 07:09:13 +00:00
|
|
|
|
FileInfo const myfile(fname);
|
|
|
|
|
if (myfile.isOK()) {
|
|
|
|
|
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
|
|
|
|
|
2003-06-10 14:39:45 +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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void QuitLyX()
|
|
|
|
|
{
|
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) {
|
2003-03-29 07:09:13 +00:00
|
|
|
|
if (!bufferlist.quitWriteAll())
|
2001-02-01 17:37:09 +00:00
|
|
|
|
return;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-10-14 21:30:23 +00:00
|
|
|
|
LyX::cref().lastfiles().writeFile(lyxrc.lastfiles);
|
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
|
2003-07-26 18:25:29 +00:00
|
|
|
|
lyxerr[Debug::INFO] << "Deleting tmp dir " << os::getTmpDir() << endl;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2003-07-26 18:25:29 +00:00
|
|
|
|
if (destroyDir(os::getTmpDir()) != 0) {
|
2003-05-13 09:48:57 +00:00
|
|
|
|
string msg = bformat(_("Could not remove the temporary directory %1$s"),
|
2003-07-26 18:25:29 +00:00
|
|
|
|
os::getTmpDir());
|
2003-03-29 09:02:08 +00:00
|
|
|
|
Alert::warning(_("Could not remove temporary directory"), msg);
|
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
2002-07-22 18:56:04 +00:00
|
|
|
|
lyx_gui::exit();
|
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) {}
|
|
|
|
|
///
|
2003-11-03 17:47:28 +00:00
|
|
|
|
virtual auto_ptr<ForkedProcess> clone() const {
|
|
|
|
|
return auto_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_);
|
2002-10-31 12:42:26 +00:00
|
|
|
|
return runNonBlocking();
|
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void AutoSave(BufferView * bv)
|
|
|
|
|
// 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 += '#';
|
2002-10-31 12:42:26 +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 !
|
|
|
|
|
//
|
2003-07-07 08:37:02 +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, ':');
|
1999-09-27 18:44:28 +00:00
|
|
|
|
#ifdef __EMX__ // Fix me! lyx_cb.C may not be low level enough to allow this.
|
1999-11-01 04:22:28 +00:00
|
|
|
|
if (name.length() == 1
|
|
|
|
|
&& isalpha(static_cast<unsigned char>(name[0]))
|
|
|
|
|
&& (prefixIs(tmpname, "/") || prefixIs(tmpname, "\\"))) {
|
1999-09-27 18:44:28 +00:00
|
|
|
|
name += ':';
|
1999-11-01 04:22:28 +00:00
|
|
|
|
name += token(tmpname, ':', 0);
|
|
|
|
|
tmpname = split(tmpname, ':');
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
#endif
|
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)
|
2000-03-17 10:14:46 +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-10 09:06:48 +00:00
|
|
|
|
if (bv->text == bv->getLyXText())
|
|
|
|
|
bv->text->clearSelection();
|
|
|
|
|
if (asParagraph)
|
2003-03-17 16:25:00 +00:00
|
|
|
|
bv->getLyXText()->insertStringAsParagraphs(tmpstr);
|
2003-11-10 09:06:48 +00:00
|
|
|
|
else
|
|
|
|
|
bv->getLyXText()->insertStringAsLines(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
|
|
|
|
|
2002-11-17 08:32:09 +00:00
|
|
|
|
FileDialog::Result result = fileDlg.open(bv->owner()->buffer()->filePath());
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileInfo fi(fname);
|
|
|
|
|
|
1999-11-24 22:14:46 +00:00
|
|
|
|
if (!fi.readable()) {
|
2003-03-29 09:02:08 +00:00
|
|
|
|
string const error = strerror(errno);
|
|
|
|
|
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);
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-02-27 16:24:14 +00:00
|
|
|
|
string const getPossibleLabel(BufferView const & bv)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
{
|
2003-10-09 10:52:12 +00:00
|
|
|
|
ParagraphList::iterator pit = bv.getLyXText()->cursorPar();
|
2003-05-22 10:47:31 +00:00
|
|
|
|
ParagraphList & plist = bv.getLyXText()->ownerParagraphs();
|
2000-12-11 09:46:09 +00:00
|
|
|
|
|
2003-05-22 10:47:31 +00:00
|
|
|
|
LyXLayout_ptr layout = pit->layout();
|
|
|
|
|
|
|
|
|
|
if (layout->latextype == LATEX_PARAGRAPH && pit != plist.begin()) {
|
|
|
|
|
ParagraphList::iterator pit2 = boost::prior(pit);
|
|
|
|
|
|
|
|
|
|
LyXLayout_ptr const & layout2 = pit2->layout();
|
2003-02-27 16:24:14 +00:00
|
|
|
|
|
|
|
|
|
if (layout2->latextype != LATEX_PARAGRAPH) {
|
2003-05-22 10:47:31 +00:00
|
|
|
|
pit = pit2;
|
2003-02-27 16:24:14 +00:00
|
|
|
|
layout = layout2;
|
1999-12-30 02:35:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2003-02-27 16:24:14 +00:00
|
|
|
|
|
|
|
|
|
string text = layout->latexname().substr(0, 3);
|
|
|
|
|
if (layout->latexname() == "theorem")
|
|
|
|
|
text = "thm"; // Create a correct prefix for prettyref
|
|
|
|
|
|
|
|
|
|
text += ':';
|
|
|
|
|
if (layout->latextype == LATEX_PARAGRAPH ||
|
|
|
|
|
lyxrc.label_init_length < 0)
|
|
|
|
|
text.erase();
|
|
|
|
|
|
2003-08-28 07:41:31 +00:00
|
|
|
|
string par_text = pit->asString(*bv.buffer(), false);
|
2003-02-27 16:24:14 +00:00
|
|
|
|
for (int i = 0; i < lyxrc.label_init_length; ++i) {
|
|
|
|
|
if (par_text.empty())
|
|
|
|
|
break;
|
|
|
|
|
string head;
|
|
|
|
|
par_text = split(par_text, head, ' ');
|
2003-11-10 09:06:48 +00:00
|
|
|
|
// Is it legal to use spaces in labels ?
|
2003-02-27 16:24:14 +00:00
|
|
|
|
if (i > 0)
|
2003-11-10 09:06:48 +00:00
|
|
|
|
text += '-';
|
2003-02-27 16:24:14 +00:00
|
|
|
|
text += head;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
2003-02-27 16:24:14 +00:00
|
|
|
|
|
|
|
|
|
return text;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// This function runs "configure" and then rereads lyx.defaults to
|
|
|
|
|
// reconfigure the automatic settings.
|
2000-02-04 09:38:32 +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
|
2003-07-27 22:13:29 +00:00
|
|
|
|
Path p(user_lyxdir());
|
2002-02-18 19:13:48 +00:00
|
|
|
|
Systemcall one;
|
2002-03-21 17:27:08 +00:00
|
|
|
|
one.startscript(Systemcall::Wait,
|
2003-07-27 21:04:02 +00:00
|
|
|
|
AddName(system_lyxdir(), "configure"));
|
1999-10-13 17:32:46 +00:00
|
|
|
|
p.pop();
|
2001-04-24 17:33:01 +00:00
|
|
|
|
bv->owner()->message(_("Reloading configuration..."));
|
2000-03-12 10:35:05 +00:00
|
|
|
|
lyxrc.read(LibFileSearch(string(), "lyxrc.defaults"));
|
2003-03-29 09:02:08 +00:00
|
|
|
|
|
|
|
|
|
Alert::information(_("System reconfigured"),
|
|
|
|
|
_("The system has been reconfigured.\n"
|
|
|
|
|
"You need to restart LyX to make use of any \n"
|
|
|
|
|
"updated document class specifications."));
|
1999-09-27 18:44:28 +00:00
|
|
|
|
}
|