2003-06-20 12:46:28 +00:00
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
* \file buffer_funcs.cpp
|
2003-06-20 12:46:28 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author Lars Gullik Bjønnes
|
2003-06-20 12:46:28 +00:00
|
|
|
* \author Alfredo Braunstein
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2003-06-20 12:46:28 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "buffer_funcs.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Buffer.h"
|
|
|
|
#include "BufferList.h"
|
|
|
|
#include "BufferParams.h"
|
|
|
|
#include "DocIterator.h"
|
|
|
|
#include "Counters.h"
|
|
|
|
#include "ErrorList.h"
|
2005-02-25 11:55:36 +00:00
|
|
|
#include "Floating.h"
|
|
|
|
#include "FloatList.h"
|
2007-10-18 15:29:51 +00:00
|
|
|
#include "InsetList.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Language.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
#include "LaTeX.h"
|
2007-09-29 20:02:32 +00:00
|
|
|
#include "Layout.h"
|
2007-08-13 18:11:43 +00:00
|
|
|
#include "LyX.h"
|
2012-11-15 21:01:19 +00:00
|
|
|
#include "LyXVC.h"
|
2007-04-29 19:53:54 +00:00
|
|
|
#include "TextClass.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Paragraph.h"
|
2006-03-23 20:11:06 +00:00
|
|
|
#include "ParagraphList.h"
|
2005-02-25 11:55:36 +00:00
|
|
|
#include "ParagraphParameters.h"
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "ParIterator.h"
|
|
|
|
#include "TexRow.h"
|
2007-08-15 13:47:32 +00:00
|
|
|
#include "Text.h"
|
2006-11-11 00:35:14 +00:00
|
|
|
#include "TocBackend.h"
|
2003-06-20 12:46:28 +00:00
|
|
|
|
2007-04-28 20:44:46 +00:00
|
|
|
#include "frontends/alert.h"
|
2003-06-20 12:46:28 +00:00
|
|
|
|
2007-04-25 01:24:38 +00:00
|
|
|
#include "insets/InsetBibitem.h"
|
|
|
|
#include "insets/InsetInclude.h"
|
2005-02-25 11:55:36 +00:00
|
|
|
|
2008-04-30 08:26:40 +00:00
|
|
|
#include "support/lassert.h"
|
2007-12-07 11:57:13 +00:00
|
|
|
#include "support/convert.h"
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/debug.h"
|
2003-09-06 18:38:02 +00:00
|
|
|
#include "support/filetools.h"
|
2007-11-29 07:04:28 +00:00
|
|
|
#include "support/gettext.h"
|
2007-11-13 23:50:28 +00:00
|
|
|
#include "support/lstrings.h"
|
2008-01-08 15:18:00 +00:00
|
|
|
#include "support/textutils.h"
|
2007-07-17 17:40:44 +00:00
|
|
|
|
2006-04-16 14:19:25 +00:00
|
|
|
using namespace std;
|
2007-12-12 18:57:56 +00:00
|
|
|
using namespace lyx::support;
|
2006-04-16 14:19:25 +00:00
|
|
|
|
2007-12-12 18:57:56 +00:00
|
|
|
namespace lyx {
|
2003-06-20 12:46:28 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
namespace Alert = frontend::Alert;
|
2003-06-30 23:56:22 +00:00
|
|
|
|
2007-06-19 16:03:47 +00:00
|
|
|
|
2008-12-05 15:49:07 +00:00
|
|
|
Buffer * checkAndLoadLyXFile(FileName const & filename, bool const acceptDirty)
|
2007-06-19 16:03:47 +00:00
|
|
|
{
|
|
|
|
// File already open?
|
2008-07-29 08:16:09 +00:00
|
|
|
Buffer * checkBuffer = theBufferList().getBuffer(filename);
|
2007-08-06 04:14:26 +00:00
|
|
|
if (checkBuffer) {
|
2010-04-10 14:30:59 +00:00
|
|
|
// Sometimes (when setting the master buffer from a child)
|
2008-12-05 15:49:07 +00:00
|
|
|
// we accept a dirty buffer right away (otherwise we'd get
|
2010-04-10 14:30:59 +00:00
|
|
|
// an infinite loop (bug 5514).
|
|
|
|
// We also accept a dirty buffer when the document has not
|
|
|
|
// yet been saved to disk.
|
|
|
|
if (checkBuffer->isClean() || acceptDirty || !filename.exists())
|
2007-08-06 04:14:26 +00:00
|
|
|
return checkBuffer;
|
2010-04-21 01:19:09 +00:00
|
|
|
docstring const file = makeDisplayPath(filename.absFileName(), 20);
|
2010-01-29 21:09:59 +00:00
|
|
|
docstring const text = bformat(_(
|
2007-08-06 04:14:26 +00:00
|
|
|
"The document %1$s is already loaded and has unsaved changes.\n"
|
|
|
|
"Do you want to abandon your changes and reload the version on disk?"), file);
|
2013-11-28 16:06:49 +00:00
|
|
|
int res = Alert::prompt(_("Reload saved document?"),
|
|
|
|
text, 2, 2, _("Yes, &Reload"), _("No, &Keep Changes"), _("&Cancel"));
|
|
|
|
switch (res) {
|
|
|
|
case 0: {
|
|
|
|
// reload the document
|
|
|
|
if (checkBuffer->reload() != Buffer::ReadSuccess)
|
|
|
|
return 0;
|
|
|
|
return checkBuffer;
|
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
// keep changes
|
|
|
|
return checkBuffer;
|
|
|
|
case 2:
|
|
|
|
// cancel
|
2010-01-29 21:09:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2007-06-19 16:03:47 +00:00
|
|
|
}
|
|
|
|
|
2012-11-15 21:01:19 +00:00
|
|
|
bool const exists = filename.exists();
|
|
|
|
bool const tryVC = exists ? false : LyXVC::fileInVC(filename);
|
|
|
|
if (exists || tryVC) {
|
|
|
|
if (exists && !filename.isReadableFile()) {
|
2008-01-26 20:37:54 +00:00
|
|
|
docstring text = bformat(_("The file %1$s exists but is not "
|
|
|
|
"readable by the current user."),
|
2010-04-21 01:19:09 +00:00
|
|
|
from_utf8(filename.absFileName()));
|
2008-01-26 20:37:54 +00:00
|
|
|
Alert::error(_("File not readable!"), text);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-04-21 01:19:09 +00:00
|
|
|
Buffer * b = theBufferList().newBuffer(filename.absFileName());
|
2008-11-16 17:49:00 +00:00
|
|
|
if (!b) {
|
2007-12-17 18:37:13 +00:00
|
|
|
// Buffer creation is not possible.
|
|
|
|
return 0;
|
2008-11-16 17:49:00 +00:00
|
|
|
}
|
2010-11-05 21:21:01 +00:00
|
|
|
if (b->loadLyXFile() != Buffer::ReadSuccess) {
|
2010-01-31 16:17:17 +00:00
|
|
|
// do not save an emergency file when releasing the buffer
|
|
|
|
b->markClean();
|
2007-06-19 16:03:47 +00:00
|
|
|
theBufferList().release(b);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
|
|
|
docstring text = bformat(_("The document %1$s does not yet "
|
|
|
|
"exist.\n\nDo you want to create a new document?"),
|
2010-04-21 01:19:09 +00:00
|
|
|
from_utf8(filename.absFileName()));
|
2007-06-20 21:07:32 +00:00
|
|
|
if (!Alert::prompt(_("Create new document?"),
|
2007-06-19 16:03:47 +00:00
|
|
|
text, 0, 1, _("&Create"), _("Cancel")))
|
2010-04-21 01:19:09 +00:00
|
|
|
return newFile(filename.absFileName(), string(), true);
|
2007-06-19 16:03:47 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-10-21 10:50:56 +00:00
|
|
|
|
2006-09-27 10:24:13 +00:00
|
|
|
// FIXME newFile() should probably be a member method of Application...
|
2003-06-30 23:56:22 +00:00
|
|
|
Buffer * newFile(string const & filename, string const & templatename,
|
2010-11-18 17:35:49 +00:00
|
|
|
bool is_named)
|
2003-06-20 12:46:28 +00:00
|
|
|
{
|
|
|
|
// get a free buffer
|
2006-10-11 17:24:46 +00:00
|
|
|
Buffer * b = theBufferList().newBuffer(filename);
|
2007-12-17 18:37:13 +00:00
|
|
|
if (!b)
|
|
|
|
// Buffer creation is not possible.
|
|
|
|
return 0;
|
2003-06-20 12:46:28 +00:00
|
|
|
|
2006-12-17 12:12:17 +00:00
|
|
|
FileName tname;
|
2003-06-20 12:46:28 +00:00
|
|
|
// use defaults.lyx as a default template if it exists.
|
|
|
|
if (templatename.empty())
|
2006-12-17 12:12:17 +00:00
|
|
|
tname = libFileSearch("templates", "defaults.lyx");
|
2003-06-20 12:46:28 +00:00
|
|
|
else
|
2006-12-27 10:56:11 +00:00
|
|
|
tname = makeAbsPath(templatename);
|
2003-06-20 12:46:28 +00:00
|
|
|
|
|
|
|
if (!tname.empty()) {
|
2010-10-26 13:19:26 +00:00
|
|
|
if (b->loadThisLyXFile(tname) != Buffer::ReadSuccess) {
|
2010-04-21 01:19:09 +00:00
|
|
|
docstring const file = makeDisplayPath(tname.absFileName(), 50);
|
2006-09-11 08:54:10 +00:00
|
|
|
docstring const text = bformat(
|
|
|
|
_("The specified document template\n%1$s\ncould not be read."),
|
|
|
|
file);
|
|
|
|
Alert::error(_("Could not read template"), text);
|
2006-10-11 17:24:46 +00:00
|
|
|
theBufferList().release(b);
|
2006-07-17 15:13:49 +00:00
|
|
|
return 0;
|
2003-06-20 12:46:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-18 17:35:49 +00:00
|
|
|
if (is_named)
|
|
|
|
// in this case, the user chose the filename, so we
|
|
|
|
// assume that she really does want this file.
|
2010-04-12 15:15:35 +00:00
|
|
|
b->markDirty();
|
2010-11-18 17:35:49 +00:00
|
|
|
else
|
|
|
|
b->setUnnamed();
|
2003-06-20 12:46:28 +00:00
|
|
|
|
|
|
|
b->setReadonly(false);
|
2007-10-20 10:03:45 +00:00
|
|
|
b->setFullyLoaded(true);
|
2003-06-20 12:46:28 +00:00
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|
2003-06-24 20:42:15 +00:00
|
|
|
|
|
|
|
|
2009-10-24 14:20:00 +00:00
|
|
|
Buffer * newUnnamedFile(FileName const & path, string const & prefix,
|
|
|
|
string const & templatename)
|
2007-12-07 11:57:13 +00:00
|
|
|
{
|
2013-10-07 22:59:05 +00:00
|
|
|
// FIXME THREAD
|
2009-10-24 14:20:00 +00:00
|
|
|
static map<string, int> file_number;
|
|
|
|
|
|
|
|
FileName filename;
|
|
|
|
|
|
|
|
do {
|
|
|
|
filename.set(path,
|
|
|
|
prefix + convert<string>(++file_number[prefix]) + ".lyx");
|
2007-12-07 11:57:13 +00:00
|
|
|
}
|
2009-10-24 14:20:00 +00:00
|
|
|
while (theBufferList().exists(filename) || filename.isReadableFile());
|
|
|
|
|
2010-04-21 01:19:09 +00:00
|
|
|
return newFile(filename.absFileName(), templatename, false);
|
2007-12-07 11:57:13 +00:00
|
|
|
}
|
|
|
|
|
2009-10-24 14:20:00 +00:00
|
|
|
|
2009-10-24 14:57:28 +00:00
|
|
|
Buffer * loadIfNeeded(FileName const & fname)
|
|
|
|
{
|
|
|
|
Buffer * buffer = theBufferList().getBuffer(fname);
|
|
|
|
if (!buffer) {
|
2012-11-15 21:01:19 +00:00
|
|
|
if (!fname.exists() && !LyXVC::fileInVC(fname))
|
2009-10-24 14:57:28 +00:00
|
|
|
return 0;
|
|
|
|
|
2010-04-21 01:19:09 +00:00
|
|
|
buffer = theBufferList().newBuffer(fname.absFileName());
|
2009-10-24 14:57:28 +00:00
|
|
|
if (!buffer)
|
|
|
|
// Buffer creation is not possible.
|
|
|
|
return 0;
|
|
|
|
|
2010-11-05 21:21:01 +00:00
|
|
|
if (buffer->loadLyXFile() != Buffer::ReadSuccess) {
|
2009-10-24 14:57:28 +00:00
|
|
|
//close the buffer we just opened
|
|
|
|
theBufferList().release(buffer);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|