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"
|
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) {
|
2008-12-05 15:49:07 +00:00
|
|
|
// sometimes (when setting the master buffer from a child)
|
|
|
|
// we accept a dirty buffer right away (otherwise we'd get
|
|
|
|
// an infinite loop (bug 5514)
|
|
|
|
if (checkBuffer->isClean() || acceptDirty)
|
2007-08-06 04:14:26 +00:00
|
|
|
return checkBuffer;
|
2007-06-19 16:03:47 +00:00
|
|
|
docstring const file = makeDisplayPath(filename.absFilename(), 20);
|
2007-08-06 04:14:26 +00:00
|
|
|
docstring text = bformat(_(
|
|
|
|
"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);
|
|
|
|
if (Alert::prompt(_("Reload saved document?"),
|
|
|
|
text, 0, 1, _("&Reload"), _("&Keep Changes")))
|
|
|
|
return checkBuffer;
|
2007-06-19 16:03:47 +00:00
|
|
|
|
|
|
|
// FIXME: should be LFUN_REVERT
|
2007-12-04 22:21:25 +00:00
|
|
|
theBufferList().release(checkBuffer);
|
|
|
|
// Load it again.
|
|
|
|
return checkAndLoadLyXFile(filename);
|
2007-06-19 16:03:47 +00:00
|
|
|
}
|
|
|
|
|
2008-01-26 20:37:54 +00:00
|
|
|
if (filename.exists()) {
|
|
|
|
if (!filename.isReadableFile()) {
|
|
|
|
docstring text = bformat(_("The file %1$s exists but is not "
|
|
|
|
"readable by the current user."),
|
|
|
|
from_utf8(filename.absFilename()));
|
|
|
|
Alert::error(_("File not readable!"), text);
|
|
|
|
return 0;
|
|
|
|
}
|
2007-06-19 16:03:47 +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
|
|
|
}
|
2007-10-21 10:50:56 +00:00
|
|
|
if (!b->loadLyXFile(filename)) {
|
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?"),
|
|
|
|
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")))
|
|
|
|
return newFile(filename.absFilename(), string(), true);
|
|
|
|
|
|
|
|
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,
|
2005-01-05 20:21:27 +00:00
|
|
|
bool const isNamed)
|
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()) {
|
2003-07-28 14:40:29 +00:00
|
|
|
if (!b->readFile(tname)) {
|
2006-12-17 12:12:17 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isNamed) {
|
|
|
|
b->setUnnamed();
|
|
|
|
b->setFileName(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
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());
|
|
|
|
|
2009-11-08 13:20:08 +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-06-23 09:46:48 +00:00
|
|
|
/*
|
|
|
|
* FIXME : merge with countChars. The structures of the two functions
|
|
|
|
* are similar but, unfortunately, they seem to have a different
|
|
|
|
* notion of what to count. Since nobody ever complained about that,
|
|
|
|
* this proves (again) that any number beats no number ! (JMarc)
|
|
|
|
*/
|
2004-12-27 16:30:27 +00:00
|
|
|
int countWords(DocIterator const & from, DocIterator const & to)
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
bool inword = false;
|
2009-06-23 09:46:48 +00:00
|
|
|
for (DocIterator dit = from ; dit != to ; ) {
|
|
|
|
if (!dit.inTexted()) {
|
|
|
|
dit.forwardPos();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Paragraph const & par = dit.paragraph();
|
|
|
|
pos_type const pos = dit.pos();
|
|
|
|
|
2009-06-21 14:30:57 +00:00
|
|
|
// Copied and adapted from isWordSeparator() in Paragraph
|
2009-06-23 09:46:48 +00:00
|
|
|
if (pos != dit.lastpos() && !par.isDeleted(pos)) {
|
|
|
|
Inset const * ins = par.getInset(pos);
|
|
|
|
if (ins && !ins->producesOutput()) {
|
|
|
|
//skip this inset
|
|
|
|
++dit.top().pos();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (par.isWordSeparator(pos))
|
|
|
|
inword = false;
|
|
|
|
else if (!inword) {
|
2004-12-27 16:30:27 +00:00
|
|
|
++count;
|
|
|
|
inword = true;
|
|
|
|
}
|
2009-06-23 09:46:48 +00:00
|
|
|
}
|
|
|
|
dit.forwardPos();
|
2004-12-27 16:30:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
2005-02-25 11:55:36 +00:00
|
|
|
|
|
|
|
|
2009-06-23 09:46:48 +00:00
|
|
|
int countChars(DocIterator const & from, DocIterator const & to,
|
|
|
|
bool with_blanks)
|
2008-01-08 15:18:00 +00:00
|
|
|
{
|
|
|
|
int chars = 0;
|
|
|
|
int blanks = 0;
|
2009-06-23 09:46:48 +00:00
|
|
|
for (DocIterator dit = from ; dit != to ; ) {
|
|
|
|
if (!dit.inTexted()) {
|
|
|
|
dit.forwardPos();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-01-12 16:34:02 +00:00
|
|
|
Paragraph const & par = dit.paragraph();
|
2008-01-12 13:34:10 +00:00
|
|
|
pos_type const pos = dit.pos();
|
2009-06-23 09:46:48 +00:00
|
|
|
|
2008-01-15 22:30:19 +00:00
|
|
|
if (pos != dit.lastpos() && !par.isDeleted(pos)) {
|
2008-02-09 10:41:49 +00:00
|
|
|
if (Inset const * ins = par.getInset(pos)) {
|
2009-06-23 09:46:48 +00:00
|
|
|
if (!ins->producesOutput()) {
|
|
|
|
//skip this inset
|
|
|
|
++dit.top().pos();
|
|
|
|
continue;
|
|
|
|
}
|
2008-01-12 13:34:10 +00:00
|
|
|
if (ins->isLetter())
|
2008-01-08 15:18:00 +00:00
|
|
|
++chars;
|
2008-01-12 16:34:02 +00:00
|
|
|
else if (with_blanks && ins->isSpace())
|
2008-01-08 15:18:00 +00:00
|
|
|
++blanks;
|
|
|
|
} else {
|
2008-01-12 16:34:02 +00:00
|
|
|
char_type const c = par.getChar(pos);
|
2008-01-08 15:18:00 +00:00
|
|
|
if (isPrintableNonspace(c))
|
|
|
|
++chars;
|
|
|
|
else if (isSpace(c) && with_blanks)
|
|
|
|
++blanks;
|
|
|
|
}
|
|
|
|
}
|
2009-06-23 09:46:48 +00:00
|
|
|
dit.forwardPos();
|
2008-01-08 15:18:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return chars + blanks;
|
|
|
|
}
|
|
|
|
|
2009-10-24 14:57:28 +00:00
|
|
|
|
|
|
|
Buffer * loadIfNeeded(FileName const & fname)
|
|
|
|
{
|
|
|
|
Buffer * buffer = theBufferList().getBuffer(fname);
|
|
|
|
if (!buffer) {
|
|
|
|
if (!fname.exists())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
buffer = theBufferList().newBuffer(fname.absFilename());
|
|
|
|
if (!buffer)
|
|
|
|
// Buffer creation is not possible.
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!buffer->loadLyXFile(fname)) {
|
|
|
|
//close the buffer we just opened
|
|
|
|
theBufferList().release(buffer);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|