2000-07-27 08:55:59 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
#include <config.h>
|
|
|
|
|
1999-10-02 16:21:10 +00:00
|
|
|
#include <cstdlib>
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
#ifdef __GNUG__
|
|
|
|
#pragma implementation
|
|
|
|
#endif
|
|
|
|
|
2001-02-12 14:09:09 +00:00
|
|
|
#include "frontends/Dialogs.h"
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
#include "insetinclude.h"
|
|
|
|
#include "buffer.h"
|
|
|
|
#include "bufferlist.h"
|
1999-10-07 18:44:17 +00:00
|
|
|
#include "debug.h"
|
1999-10-02 16:21:10 +00:00
|
|
|
#include "support/filetools.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
#include "lyxrc.h"
|
|
|
|
#include "LyXView.h"
|
|
|
|
#include "LaTeXFeatures.h"
|
|
|
|
#include "gettext.h"
|
1999-10-02 16:21:10 +00:00
|
|
|
#include "support/FileInfo.h"
|
1999-11-04 01:40:20 +00:00
|
|
|
#include "layout.h"
|
2000-03-12 10:35:05 +00:00
|
|
|
#include "lyxfunc.h"
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2000-04-04 00:19:15 +00:00
|
|
|
using std::ostream;
|
2000-03-28 02:18:55 +00:00
|
|
|
using std::endl;
|
2000-05-19 16:46:01 +00:00
|
|
|
using std::vector;
|
2000-06-07 08:53:40 +00:00
|
|
|
using std::pair;
|
2000-03-28 02:18:55 +00:00
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
extern BufferList bufferlist;
|
|
|
|
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
static inline
|
|
|
|
string unique_id() {
|
2000-09-26 13:54:57 +00:00
|
|
|
static unsigned int seed = 1000;
|
2000-07-02 09:52:44 +00:00
|
|
|
|
|
|
|
std::ostringstream ost;
|
|
|
|
ost << "file" << ++seed;
|
2000-07-25 16:15:35 +00:00
|
|
|
|
|
|
|
// Needed if we use lyxstring.
|
|
|
|
return ost.str().c_str();
|
2000-07-02 09:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-10 11:50:43 +00:00
|
|
|
InsetInclude::InsetInclude(InsetCommandParams const & p, Buffer const & bf)
|
|
|
|
: InsetCommand(p), master(&bf)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
|
|
|
flag = InsetInclude::INCLUDE;
|
|
|
|
noload = false;
|
2000-07-02 09:52:44 +00:00
|
|
|
include_label = unique_id();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
InsetInclude::~InsetInclude()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
|
2000-10-10 11:50:43 +00:00
|
|
|
Inset * InsetInclude::Clone(Buffer const & buffer) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-10-10 11:50:43 +00:00
|
|
|
InsetInclude * ii = new InsetInclude (params(), buffer);
|
1999-09-27 18:44:28 +00:00
|
|
|
ii->setNoLoad(isNoLoad());
|
|
|
|
// By default, the newly created inset is of `include' type,
|
|
|
|
// so we do not test this case.
|
|
|
|
if (isInput())
|
|
|
|
ii->setInput();
|
|
|
|
else if (isVerb()) {
|
|
|
|
ii->setVerb();
|
|
|
|
ii->setVisibleSpace(isVerbVisibleSpace());
|
|
|
|
}
|
1999-11-04 01:40:20 +00:00
|
|
|
return ii;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
1999-12-07 00:44:53 +00:00
|
|
|
|
2000-02-25 12:06:15 +00:00
|
|
|
void InsetInclude::Edit(BufferView * bv, int, int, unsigned int)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2001-02-12 14:09:09 +00:00
|
|
|
bv->owner()->getDialogs()->showInclude(this);
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
void InsetInclude::Write(Buffer const *, ostream & os) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-12-07 00:44:53 +00:00
|
|
|
os << "Include " << getCommand() << "\n";
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-12 11:27:15 +00:00
|
|
|
void InsetInclude::Read(Buffer const * buf, LyXLex & lex)
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-06-12 11:27:15 +00:00
|
|
|
InsetCommand::Read(buf, lex);
|
1999-09-27 18:44:28 +00:00
|
|
|
|
1999-11-15 10:58:38 +00:00
|
|
|
if (getCmdName() == "include")
|
1999-09-27 18:44:28 +00:00
|
|
|
setInclude();
|
|
|
|
else if (getCmdName() == "input")
|
|
|
|
setInput();
|
1999-10-02 16:21:10 +00:00
|
|
|
else if (contains(getCmdName(), "verbatim")) {
|
1999-09-27 18:44:28 +00:00
|
|
|
setVerb();
|
|
|
|
if (getCmdName() == "verbatiminput*")
|
|
|
|
setVisibleSpace(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-04-04 00:19:15 +00:00
|
|
|
bool InsetInclude::display() const
|
|
|
|
{
|
|
|
|
return !isInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
string const InsetInclude::getScreenLabel() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
1999-10-02 16:21:10 +00:00
|
|
|
string temp;
|
1999-09-27 18:44:28 +00:00
|
|
|
if (isInput())
|
|
|
|
temp += _("Input");
|
|
|
|
else if (isVerb()) {
|
|
|
|
temp += _("Verbatim Input");
|
|
|
|
if (isVerbVisibleSpace()) temp += '*';
|
|
|
|
} else temp += _("Include");
|
|
|
|
temp += ": ";
|
|
|
|
|
2000-06-07 08:53:40 +00:00
|
|
|
if (getContents().empty()) {
|
1999-11-15 10:58:38 +00:00
|
|
|
temp+= "???";
|
1999-09-27 18:44:28 +00:00
|
|
|
} else {
|
2000-06-07 08:53:40 +00:00
|
|
|
temp+= getContents();
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
string const InsetInclude::getFileName() const
|
2000-07-27 08:55:59 +00:00
|
|
|
{
|
|
|
|
return MakeAbsPath(getContents(),
|
|
|
|
OnlyPath(getMasterFilename()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
string const InsetInclude::getMasterFilename() const
|
2000-04-04 00:19:15 +00:00
|
|
|
{
|
2000-07-27 08:55:59 +00:00
|
|
|
return master->fileName();
|
2000-04-04 00:19:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
bool InsetInclude::loadIfNeeded() const
|
|
|
|
{
|
|
|
|
if (isNoLoad() || isVerb()) return false;
|
|
|
|
if (!IsLyXFilename(getFileName())) return false;
|
|
|
|
|
|
|
|
if (bufferlist.exists(getFileName())) return true;
|
|
|
|
|
|
|
|
// the readonly flag can/will be wrong, not anymore I think.
|
|
|
|
FileInfo finfo(getFileName());
|
2000-09-26 13:54:57 +00:00
|
|
|
bool const ro = !finfo.writable();
|
|
|
|
return bufferlist.readFile(getFileName(), ro) != 0;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-03 09:13:34 +00:00
|
|
|
int InsetInclude::Latex(Buffer const * buffer, ostream & os,
|
2000-04-19 01:42:55 +00:00
|
|
|
bool /*fragile*/, bool /*fs*/) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-09-26 13:54:57 +00:00
|
|
|
string incfile(getContents());
|
|
|
|
|
2000-03-02 02:19:43 +00:00
|
|
|
// Do nothing if no file name has been specified
|
2000-09-26 13:54:57 +00:00
|
|
|
if (incfile.empty())
|
2000-03-02 02:19:43 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (loadIfNeeded()) {
|
|
|
|
Buffer * tmp = bufferlist.getBuffer(getFileName());
|
|
|
|
|
2000-10-03 09:13:34 +00:00
|
|
|
if (tmp->params.textclass != buffer->params.textclass) {
|
2000-03-02 02:19:43 +00:00
|
|
|
lyxerr << "ERROR: Cannot handle include file `"
|
|
|
|
<< MakeDisplayPath(getFileName())
|
|
|
|
<< "' which has textclass `"
|
|
|
|
<< textclasslist.NameOfClass(tmp->params.textclass)
|
|
|
|
<< "' instead of `"
|
2000-10-03 09:13:34 +00:00
|
|
|
<< textclasslist.NameOfClass(buffer->params.textclass)
|
2000-03-02 02:19:43 +00:00
|
|
|
<< "'." << endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// write it to a file (so far the complete file)
|
2000-05-11 16:12:46 +00:00
|
|
|
string writefile = ChangeExtension(getFileName(), ".tex");
|
2000-10-03 09:13:34 +00:00
|
|
|
if (!buffer->tmppath.empty()
|
|
|
|
&& !buffer->niceFile) {
|
2000-03-02 02:19:43 +00:00
|
|
|
incfile = subst(incfile, '/','@');
|
|
|
|
#ifdef __EMX__
|
|
|
|
incfile = subst(incfile, ':', '$');
|
|
|
|
#endif
|
2000-10-03 09:13:34 +00:00
|
|
|
writefile = AddName(buffer->tmppath, incfile);
|
2000-03-02 02:19:43 +00:00
|
|
|
} else
|
|
|
|
writefile = getFileName();
|
2000-05-11 16:12:46 +00:00
|
|
|
writefile = ChangeExtension(writefile, ".tex");
|
2000-03-02 02:19:43 +00:00
|
|
|
lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
|
|
|
|
lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
|
|
|
|
|
2000-10-03 09:13:34 +00:00
|
|
|
tmp->markDepClean(buffer->tmppath);
|
2000-03-02 02:19:43 +00:00
|
|
|
|
|
|
|
tmp->makeLaTeXFile(writefile,
|
|
|
|
OnlyPath(getMasterFilename()),
|
2000-10-03 09:13:34 +00:00
|
|
|
buffer->niceFile, true);
|
2000-03-02 02:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isVerb()) {
|
2000-06-07 08:53:40 +00:00
|
|
|
os << '\\' << getCmdName() << '{' << incfile << '}';
|
2000-06-12 11:27:15 +00:00
|
|
|
} else if (isInput()) {
|
2000-03-02 02:19:43 +00:00
|
|
|
// \input wants file with extension (default is .tex)
|
|
|
|
if (!IsLyXFilename(getFileName())) {
|
2000-06-07 08:53:40 +00:00
|
|
|
os << '\\' << getCmdName() << '{' << incfile << '}';
|
2000-03-02 02:19:43 +00:00
|
|
|
} else {
|
2000-06-07 08:53:40 +00:00
|
|
|
os << '\\' << getCmdName() << '{'
|
2000-05-11 16:12:46 +00:00
|
|
|
<< ChangeExtension(incfile, ".tex")
|
2000-03-02 02:19:43 +00:00
|
|
|
<< '}';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// \include don't want extension and demands that the
|
|
|
|
// file really have .tex
|
2000-06-07 08:53:40 +00:00
|
|
|
os << '\\' << getCmdName() << '{'
|
2000-05-11 16:12:46 +00:00
|
|
|
<< ChangeExtension(incfile, string())
|
2000-03-02 02:19:43 +00:00
|
|
|
<< '}';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-12-28 16:22:28 +00:00
|
|
|
int InsetInclude::Ascii(Buffer const *, std::ostream & os, int) const
|
|
|
|
{
|
|
|
|
if (isVerb())
|
|
|
|
os << GetFileContents(getFileName());
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-03 09:13:34 +00:00
|
|
|
int InsetInclude::Linuxdoc(Buffer const * buffer, ostream & os) const
|
2000-07-01 12:54:45 +00:00
|
|
|
{
|
2000-09-26 13:54:57 +00:00
|
|
|
string incfile(getContents());
|
|
|
|
|
2000-07-01 12:54:45 +00:00
|
|
|
// Do nothing if no file name has been specified
|
2000-09-26 13:54:57 +00:00
|
|
|
if (incfile.empty())
|
2000-07-01 12:54:45 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (loadIfNeeded()) {
|
|
|
|
Buffer * tmp = bufferlist.getBuffer(getFileName());
|
|
|
|
|
|
|
|
// write it to a file (so far the complete file)
|
|
|
|
string writefile = ChangeExtension(getFileName(), ".sgml");
|
2000-10-03 09:13:34 +00:00
|
|
|
if (!buffer->tmppath.empty() && !buffer->niceFile) {
|
2000-07-01 12:54:45 +00:00
|
|
|
incfile = subst(incfile, '/','@');
|
2000-10-03 09:13:34 +00:00
|
|
|
writefile = AddName(buffer->tmppath, incfile);
|
2000-07-01 12:54:45 +00:00
|
|
|
} else
|
|
|
|
writefile = getFileName();
|
|
|
|
|
2000-11-04 10:00:12 +00:00
|
|
|
if (IsLyXFilename(getFileName()))
|
2000-07-01 12:54:45 +00:00
|
|
|
writefile = ChangeExtension(writefile, ".sgml");
|
|
|
|
|
|
|
|
lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
|
|
|
|
lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
|
|
|
|
|
2000-10-03 09:13:34 +00:00
|
|
|
tmp->makeLinuxDocFile(writefile, buffer->niceFile, true);
|
2000-07-01 12:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isVerb()) {
|
2000-11-13 15:43:36 +00:00
|
|
|
os << "<inlinegraphic fileref=\"" << '&' << include_label << ';'
|
|
|
|
<< "\" format=\"linespecific\">"
|
|
|
|
<< "</inlinegraphic>";
|
2000-07-01 12:54:45 +00:00
|
|
|
} else
|
|
|
|
os << '&' << include_label << ';';
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-03 09:13:34 +00:00
|
|
|
int InsetInclude::DocBook(Buffer const * buffer, ostream & os) const
|
2000-07-01 12:54:45 +00:00
|
|
|
{
|
2000-09-26 13:54:57 +00:00
|
|
|
string incfile(getContents());
|
|
|
|
|
2000-07-01 12:54:45 +00:00
|
|
|
// Do nothing if no file name has been specified
|
2000-09-26 13:54:57 +00:00
|
|
|
if (incfile.empty())
|
2000-07-01 12:54:45 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (loadIfNeeded()) {
|
|
|
|
Buffer * tmp = bufferlist.getBuffer(getFileName());
|
|
|
|
|
|
|
|
// write it to a file (so far the complete file)
|
|
|
|
string writefile = ChangeExtension(getFileName(), ".sgml");
|
2000-10-03 09:13:34 +00:00
|
|
|
if (!buffer->tmppath.empty() && !buffer->niceFile) {
|
2000-07-01 12:54:45 +00:00
|
|
|
incfile = subst(incfile, '/','@');
|
2000-10-03 09:13:34 +00:00
|
|
|
writefile = AddName(buffer->tmppath, incfile);
|
2000-07-01 12:54:45 +00:00
|
|
|
} else
|
|
|
|
writefile = getFileName();
|
2000-11-04 10:00:12 +00:00
|
|
|
if (IsLyXFilename(getFileName()))
|
2000-07-01 12:54:45 +00:00
|
|
|
writefile = ChangeExtension(writefile, ".sgml");
|
|
|
|
|
|
|
|
lyxerr[Debug::LATEX] << "incfile:" << incfile << endl;
|
|
|
|
lyxerr[Debug::LATEX] << "writefile:" << writefile << endl;
|
|
|
|
|
2000-10-03 09:13:34 +00:00
|
|
|
tmp->makeDocBookFile(writefile, buffer->niceFile, true);
|
2000-07-01 12:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isVerb()) {
|
2000-11-13 15:43:36 +00:00
|
|
|
os << "<inlinegraphic fileref=\"" << '&' << include_label << ';'
|
|
|
|
<< "\" format=\"linespecific\">"
|
|
|
|
<< "</inlinegraphic>";
|
2000-07-01 12:54:45 +00:00
|
|
|
} else
|
|
|
|
os << '&' << include_label << ';';
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-11-04 01:40:20 +00:00
|
|
|
void InsetInclude::Validate(LaTeXFeatures & features) const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-07-01 12:54:45 +00:00
|
|
|
|
|
|
|
string incfile(getContents());
|
2000-11-13 15:43:36 +00:00
|
|
|
string writefile; // = ChangeExtension(getFileName(), ".sgml");
|
|
|
|
|
2000-07-01 12:54:45 +00:00
|
|
|
if (!master->tmppath.empty() && !master->niceFile) {
|
|
|
|
incfile = subst(incfile, '/','@');
|
|
|
|
writefile = AddName(master->tmppath, incfile);
|
|
|
|
} else
|
2000-11-13 15:43:36 +00:00
|
|
|
writefile = getFileName();
|
2000-07-01 12:54:45 +00:00
|
|
|
// Use the relative path.
|
2000-11-13 15:43:36 +00:00
|
|
|
//writefile = incfile;
|
2000-07-01 12:54:45 +00:00
|
|
|
|
2000-11-04 10:00:12 +00:00
|
|
|
if (IsLyXFilename(getFileName()))
|
2000-07-01 12:54:45 +00:00
|
|
|
writefile = ChangeExtension(writefile, ".sgml");
|
|
|
|
|
|
|
|
features.IncludedFiles[include_label] = writefile;
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
if (isVerb())
|
|
|
|
features.verbatim = true;
|
|
|
|
|
|
|
|
// Here we must do the fun stuff...
|
|
|
|
// Load the file in the include if it needs
|
|
|
|
// to be loaded:
|
|
|
|
if (loadIfNeeded()) {
|
|
|
|
// a file got loaded
|
1999-11-04 01:40:20 +00:00
|
|
|
Buffer * tmp = bufferlist.getBuffer(getFileName());
|
1999-09-27 18:44:28 +00:00
|
|
|
tmp->validate(features);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
vector<string> const InsetInclude::getLabelList() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-09-26 13:54:57 +00:00
|
|
|
vector<string> l;
|
2000-05-19 16:46:01 +00:00
|
|
|
|
2000-09-26 13:54:57 +00:00
|
|
|
if (loadIfNeeded()) {
|
|
|
|
Buffer * tmp = bufferlist.getBuffer(getFileName());
|
|
|
|
tmp->setParentName("");
|
|
|
|
l = tmp->getLabelList();
|
|
|
|
tmp->setParentName(getMasterFilename());
|
|
|
|
}
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2000-09-26 13:54:57 +00:00
|
|
|
return l;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-14 17:53:12 +00:00
|
|
|
vector<pair<string,string> > const InsetInclude::getKeys() const
|
1999-09-27 18:44:28 +00:00
|
|
|
{
|
2000-06-07 08:53:40 +00:00
|
|
|
vector<pair<string,string> > keys;
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
if (loadIfNeeded()) {
|
2000-09-26 13:54:57 +00:00
|
|
|
Buffer * tmp = bufferlist.getBuffer(getFileName());
|
1999-09-27 18:44:28 +00:00
|
|
|
tmp->setParentName("");
|
2000-09-26 13:54:57 +00:00
|
|
|
keys = tmp->getBibkeyList();
|
1999-09-27 18:44:28 +00:00
|
|
|
tmp->setParentName(getMasterFilename());
|
|
|
|
}
|
|
|
|
|
2000-06-07 08:53:40 +00:00
|
|
|
return keys;
|
1999-09-27 18:44:28 +00:00
|
|
|
}
|