* Licence details can be found in the file COPYING.
*
* \author Alejandro Aguilar Sierra
- * \author Angus Leeming
*
* Full author contact details are available in file CREDITS.
*/
#include <config.h>
#include "insetbibtex.h"
-#include "metricsinfo.h"
+
#include "buffer.h"
-#include "BufferView.h"
+#include "bufferparams.h"
+#include "dispatchresult.h"
#include "debug.h"
#include "funcrequest.h"
#include "gettext.h"
-#include "latexrunparams.h"
-#include "lyxlex.h"
-#include "Lsstream.h"
+#include "LaTeXFeatures.h"
#include "metricsinfo.h"
+#include "outputparams.h"
+#include "frontends/Alert.h"
+
+#include "support/filename.h"
#include "support/filetools.h"
-#include "support/path.h"
-#include "support/os.h"
#include "support/lstrings.h"
-#include "support/lyxalgo.h"
-#include "support/LAssert.h"
-#include "support/tostr.h"
+#include "support/lyxlib.h"
+#include "support/os.h"
+#include "support/path.h"
-#include <fstream>
+#include <boost/tokenizer.hpp>
-using namespace lyx;
-using support::FileName;
+#include <fstream>
+#include <sstream>
+
+using lyx::support::AbsolutePath;
+using lyx::support::ascii_lowercase;
+using lyx::support::ChangeExtension;
+using lyx::support::contains;
+using lyx::support::copy;
+using lyx::support::FileName;
+using lyx::support::findtexfile;
+using lyx::support::IsFileReadable;
+using lyx::support::latex_path;
+using lyx::support::ltrim;
+using lyx::support::MakeAbsPath;
+using lyx::support::MakeRelPath;
+using lyx::support::Path;
+using lyx::support::prefixIs;
+using lyx::support::rtrim;
+using lyx::support::split;
+using lyx::support::subst;
+using lyx::support::tokenPos;
+using lyx::support::trim;
+
+namespace os = lyx::support::os;
-using std::ostream;
-using std::ifstream;
using std::endl;
-using std::vector;
+using std::getline;
+using std::string;
+using std::ifstream;
+using std::ostream;
using std::pair;
+using std::vector;
-InsetBibtexParams::InsetBibtexParams()
- : bibtotoc(false)
-{}
-
-
-bool InsetBibtexParams::empty() const
-{
- return databases.empty();
-}
-
-
-void InsetBibtexParams::erase()
-{
- databases.clear();
- style.erase();
- bibtotoc = false;
-}
-
-
-void InsetBibtexParams::write(Buffer const & buffer, std::ostream & os) const
-{
- os << "Bibtex\n";
-
- vector<FileName>::const_iterator it = databases.begin();
- vector<FileName>::const_iterator end = databases.end();
- for (; it != end; ++it) {
- os << "\tfilename "
- << it->outputFilename(buffer.filePath())
- << '\n';
- }
- if (!style.empty())
- os << "\tstyle " << style << '\n';
- if (bibtotoc)
- os << "\tbibtotoc " << tostr(bibtotoc) << '\n';
-}
-
-
-void InsetBibtexParams::read(Buffer const & buffer, LyXLex & lex)
-{
- enum BibtexTags {
- BIB_FILENAME = 1,
- BIB_STYLE,
- BIB_BIBTOTOC,
- BIB_END
- };
-
- keyword_item bibtex_tags[] = {
- { "\\end_inset", BIB_END },
- { "bibtotoc", BIB_BIBTOTOC },
- { "filename", BIB_FILENAME},
- { "style", BIB_STYLE}
- };
-
- pushpophelper pph(lex, bibtex_tags, BIB_END);
-
- bool found_end = false;
- bool read_error = false;
-
- while (lex.isOK()) {
- switch (lex.lex()) {
- case BIB_FILENAME: {
- lex.next();
- FileName filename;
- filename.set(lex.getString(), buffer.filePath());
- databases.push_back(filename);
- break;
- }
-
- case BIB_STYLE: {
- lex.next();
- style = lex.getString();
- break;
- }
-
- case BIB_BIBTOTOC: {
- lex.next();
- bibtotoc = lex.getBool();
- break;
- }
-
- case BIB_END:
- found_end = true;
- break;
-
- default:
- lex.printError("BibtexInset::read: "
- "Wrong tag: $$Token");
- read_error = true;
- break;
- }
-
- if (found_end || read_error)
- break;
- }
-
- if (!found_end) {
- lex.printError("BibtexInset::read: "
- "Missing \\end_inset.");
- }
-}
-
-
-InsetBibtex::InsetBibtex()
- : set_label_(false), center_indent_(0)
-{}
-
-
-
-InsetBibtex::InsetBibtex(InsetBibtexParams const & p)
- : params_(p), set_label_(false), center_indent_(0)
+InsetBibtex::InsetBibtex(InsetCommandParams const & p)
+ : InsetCommand(p, "bibtex")
{}
-InsetBibtex::~InsetBibtex()
-{
- InsetBibtexMailer(*this).hideDialog();
-}
-
-
-std::auto_ptr<InsetBase> InsetBibtex::clone() const
+std::auto_ptr<InsetBase> InsetBibtex::doClone() const
{
return std::auto_ptr<InsetBase>(new InsetBibtex(*this));
}
-dispatch_result InsetBibtex::localDispatch(FuncRequest const & cmd)
+void InsetBibtex::doDispatch(LCursor & cur, FuncRequest & cmd)
{
switch (cmd.action) {
- case LFUN_INSET_EDIT:
- InsetBibtexMailer(*this).showDialog(cmd.view());
- return DISPATCHED;
-
case LFUN_INSET_MODIFY: {
- Buffer const * buffer = cmd.view()->buffer();
- InsetBibtexParams p;
- InsetBibtexMailer::string2params(cmd.argument, *buffer, p);
- setParams(p);
- return DISPATCHED;
+ InsetCommandParams p;
+ InsetCommandMailer::string2params("bibtex", cmd.argument, p);
+ if (!p.getCmdName().empty())
+ setParams(p);
+ else
+ cur.noUpdate();
+ break;
}
- case LFUN_INSET_DIALOG_UPDATE:
- InsetBibtexMailer(*this).updateDialog(cmd.view());
- return DISPATCHED;
-
- case LFUN_MOUSE_RELEASE:
- return localDispatch(FuncRequest(cmd.view(), LFUN_INSET_EDIT));
-
default:
- return InsetOld::localDispatch(cmd);
+ InsetCommand::doDispatch(cur, cmd);
+ break;
}
-
}
+
string const InsetBibtex::getScreenLabel(Buffer const &) const
{
- return _("BibTeX Generated References");
+ return _("BibTeX Generated Bibliography");
}
-void InsetBibtex::metrics(MetricsInfo & mi, Dimension & dim) const
+namespace {
+
+string normalize_name(Buffer const & buffer, OutputParams const & runparams,
+ string const & name, string const & ext)
{
- if (!set_label_) {
- set_label_ = true;
- button_.update(getScreenLabel(*mi.base.bv->buffer()),
- editable() != NOT_EDITABLE);
- }
- button_.metrics(mi, dim);
- center_indent_ = (mi.base.textwidth - dim.wid) / 2;
- dim.wid = mi.base.textwidth;
- dim_ = dim;
+ string const fname = MakeAbsPath(name, buffer.filePath());
+ if (AbsolutePath(name) || !IsFileReadable(fname + ext))
+ return name;
+ else if (!runparams.nice)
+ return fname;
+ else
+ return MakeRelPath(fname, buffer.getMasterBuffer()->filePath());
}
-
-void InsetBibtex::draw(PainterInfo & pi, int x, int y) const
-{
- button_.draw(pi, x + center_indent_, y);
}
-void InsetBibtex::write(Buffer const & buffer, std::ostream & os) const
+int InsetBibtex::latex(Buffer const & buffer, ostream & os,
+ OutputParams const & runparams) const
{
- params().write(buffer, os);
-}
+ // the sequence of the commands:
+ // 1. \bibliographystyle{style}
+ // 2. \addcontentsline{...} - if option bibtotoc set
+ // 3. \bibliography{database}
+ // and with bibtopic:
+ // 1. \bibliographystyle{style}
+ // 2. \begin{btSect}{database}
+ // 3. \btPrint{Cited|NotCited|All}
+ // 4. \end{btSect}
+
+ // Database(s)
+ // If we are processing the LaTeX file in a temp directory then
+ // copy the .bib databases to this temp directory, mangling their
+ // names in the process. Store this mangled name in the list of
+ // all databases.
+ // (We need to do all this because BibTeX *really*, *really*
+ // can't handle "files with spaces" and Windows users tend to
+ // use such filenames.)
+ // Otherwise, store the (maybe absolute) path to the original,
+ // unmangled database name.
+ typedef boost::char_separator<char> Separator;
+ typedef boost::tokenizer<Separator> Tokenizer;
+
+ Separator const separator(",");
+ Tokenizer const tokens(getContents(), separator);
+ Tokenizer::const_iterator const begin = tokens.begin();
+ Tokenizer::const_iterator const end = tokens.end();
+
+ std::ostringstream dbs;
+ for (Tokenizer::const_iterator it = begin; it != end; ++it) {
+ string const input = trim(*it);
+ string database =
+ normalize_name(buffer, runparams, input, ".bib");
+ string const in_file = database + ".bib";
+
+ if (!runparams.nice && IsFileReadable(in_file)) {
+
+ database = FileName(database).mangledFilename();
+ string const out_file = MakeAbsPath(database + ".bib",
+ buffer.getMasterBuffer()->temppath());
+
+ bool const success = copy(in_file, out_file);
+ if (!success) {
+ lyxerr << "Failed to copy '" << in_file
+ << "' to '" << out_file << "'"
+ << endl;
+ }
+ }
+ if (it != begin)
+ dbs << ',';
+ dbs << latex_path(database);
+ }
+ string const db_out = dbs.str();
-void InsetBibtex::read(Buffer const & buffer, LyXLex & lex)
-{
- InsetBibtexParams p;
- p.read(buffer, lex);
+ // Post this warning only once.
+ static bool warned_about_spaces = false;
+ if (!warned_about_spaces &&
+ runparams.nice && db_out.find(' ') != string::npos) {
+ warned_about_spaces = true;
- // Replace the inset's store
- setParams(p);
-}
+ Alert::warning(_("Export Warning!"),
+ _("There are spaces in the paths to your BibTeX databases.\n"
+ "BibTeX will be unable to find them."));
+ }
-int InsetBibtex::latex(Buffer const & buffer, ostream & os,
- LatexRunParams const & runparams) const
-{
- ostringstream ss;
- // 1. \bibliographystyle{style}
- if (!params().style.empty()) { // we want no \biblio...{}
- string style = params().style;
+ // Style-Options
+ string style = getOptions(); // maybe empty! and with bibtotoc
+ string bibtotoc;
+ if (prefixIs(style, "bibtotoc")) {
+ bibtotoc = "bibtotoc";
+ if (contains(style, ',')) {
+ style = split(style, bibtotoc, ',');
+ }
+ }
- string const abs_style =
- support::MakeAbsPath(style, buffer.filePath());
- if (!runparams.nice && support::IsFileReadable(abs_style + ".bst"))
- style = abs_style;
+ // line count
+ int nlines = 0;
- ss << "\\bibliographystyle{" << style << "}\n";
+ if (!style.empty()) {
+ os << "\\bibliographystyle{"
+ << latex_path(normalize_name(buffer, runparams, style, ".bst"))
+ << "}\n";
+ nlines += 1;
}
- // 2. \addcontentsline{...} - if option bibtotoc set
- if (params().bibtotoc) {
- // Assumption: if the textclass name does not contain "art",
- // then it's a book.
- BufferParams const & bp = buffer.params;
- if (!support::contains(bp.getLyXTextClass().name(), "art")) {
- if (bp.sides == LyXTextClass::OneSide) {
- ss << "\\clearpage";
+ if (!db_out.empty() && buffer.params().use_bibtopic){
+ os << "\\begin{btSect}{" << db_out << "}\n";
+ string btprint = getSecOptions();
+ if (btprint.empty())
+ // default
+ btprint = "btPrintCited";
+ os << "\\" << btprint << "\n"
+ << "\\end{btSect}\n";
+ nlines += 3;
+ }
+
+ // bibtotoc-Option
+ if (!bibtotoc.empty() && !buffer.params().use_bibtopic) {
+ // maybe a problem when a textclass has no "art" as
+ // part of its name, because it's than book.
+ // For the "official" lyx-layouts it's no problem to support
+ // all well
+ if (!contains(buffer.params().getLyXTextClass().name(),
+ "art")) {
+ if (buffer.params().sides == LyXTextClass::OneSide) {
+ // oneside
+ os << "\\clearpage";
} else {
- ss << "\\cleardoublepage";
+ // twoside
+ os << "\\cleardoublepage";
}
- // book class
- ss << "\\addcontentsline{toc}{chapter}{\\bibname}\n";
+ // bookclass
+ os << "\\addcontentsline{toc}{chapter}{\\bibname}";
+
} else {
// article class
- ss << "\\addcontentsline{toc}{section}{\\refname}\n";
+ os << "\\addcontentsline{toc}{section}{\\refname}";
}
}
- // 3. \bibliography{database}
- // If we generate in a temp dir, we _need_ to use the absolute path,
- // else rely on the user.
- ss << "\\bibliography{";
- vector<FileName>::const_iterator begin = params().databases.begin();
- vector<FileName>::const_iterator end = params().databases.end();
- vector<FileName>::const_iterator it = begin;
- for (; it != end; ++it) {
- if (it != begin)
- ss << ',';
- string db = it->outputFilename(buffer.filePath());
- if (!runparams.nice &&
- support::IsFileReadable(it->absFilename())+".bib")
- db = support::os::external_path(it->absFilename());
-
- ss << db;
+ if (!db_out.empty() && !buffer.params().use_bibtopic){
+ os << "\\bibliography{" << db_out << "}\n";
+ nlines += 1;
}
- ss << '}';
- string const output = STRCONV(ss.str());
- os << output;
- return int(lyx::count(output.begin(), output.end(),'\n') + 1);
+ return nlines;
}
-int InsetBibtex::ascii(Buffer const &, std::ostream &, int) const
+vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
{
- return 0;
-}
+ Path p(buffer.filePath());
+ vector<string> vec;
-int InsetBibtex::linuxdoc(Buffer const &, std::ostream &) const
-{
- return 0;
-}
+ string tmp;
+ string bibfiles = getContents();
+ bibfiles = split(bibfiles, tmp, ',');
+ while (!tmp.empty()) {
+ string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
+ lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
+ // If we didn't find a matching file name just fail silently
+ if (!file.empty())
+ vec.push_back(file);
-int InsetBibtex::docbook(Buffer const &, std::ostream &, bool) const
-{
- return 0;
-}
-
-
-vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
-{
- support::Path p(buffer.filePath());
-
- vector<string> files;
- vector<FileName>::const_iterator it = params().databases.begin();
- vector<FileName>::const_iterator end = params().databases.end();
- for (; it != end; ++it) {
- // I really do need to pass the buffer path here...
- // FileName needs extending it would seem.
- string file_in = it->relFilename(buffer.filePath());
- string file_out = support::findtexfile(
- support::ChangeExtension(file_in, "bib"), "bib");
- lyxerr[Debug::LATEX] << "Bibfile: " << file_in
- << ' ' << file_out << endl;
-
- // If we don't find a matching file name just fail silently
- if (!file_out.empty())
- files.push_back(file_out);
+ // Get next file name
+ bibfiles = split(bibfiles, tmp, ',');
}
- return files;
+ return vec;
}
ifstream ifs(it->c_str());
string linebuf0;
while (getline(ifs, linebuf0)) {
- string linebuf = support::trim(linebuf0);
+ string linebuf = trim(linebuf0);
if (linebuf.empty()) continue;
- if (support::prefixIs(linebuf, "@")) {
- linebuf = support::subst(linebuf, '{', '(');
+ if (prefixIs(linebuf, "@")) {
+ linebuf = subst(linebuf, '{', '(');
string tmp;
- linebuf = support::split(linebuf, tmp, '(');
- tmp = support::ascii_lowercase(tmp);
- if (!support::prefixIs(tmp, "@string")
- && !support::prefixIs(tmp, "@preamble")) {
- linebuf = support::split(linebuf, tmp, ',');
- tmp = support::ltrim(tmp, " \t");
+ linebuf = split(linebuf, tmp, '(');
+ tmp = ascii_lowercase(tmp);
+ if (!prefixIs(tmp, "@string")
+ && !prefixIs(tmp, "@preamble")) {
+ linebuf = split(linebuf, tmp, ',');
+ tmp = ltrim(tmp, " \t");
if (!tmp.empty()) {
keys.push_back(pair<string,string>(tmp,string()));
}
}
-bool InsetBibtex::addDatabase(string const & /* db */)
+bool InsetBibtex::addDatabase(string const & db)
{
-#ifdef WITH_WARNINGS
-#warning addDatabase is currently disabled (no LFUN).
-#endif
-#if 0
- vector<string>
string contents(getContents());
- if (!support::contains(contents, db)) {
+ if (tokenPos(contents, ',', db) == -1) {
if (!contents.empty())
contents += ',';
setContents(contents + db);
return true;
}
-#endif
return false;
}
-bool InsetBibtex::delDatabase(string const & /* db */)
+bool InsetBibtex::delDatabase(string const & db)
{
-#ifdef WITH_WARNINGS
-#warning delDatabase is currently disabled (no LFUN).
-#endif
-#if 0
- if (support::contains(getContents(), db)) {
+ string contents(getContents());
+ if (contains(contents, db)) {
+ int const n = tokenPos(contents, ',', db);
string bd = db;
- int const n = tokenPos(getContents(), ',', bd);
if (n > 0) {
- // Weird code, would someone care to explain this?(Lgb)
- string tmp(", ");
- tmp += bd;
- setContents(support::subst(getContents(), tmp, ", "));
+ // this is not the first database
+ string tmp = ',' + bd;
+ setContents(subst(contents, tmp, ""));
} else if (n == 0)
- setContents(support::split(getContents(), bd, ','));
+ // this is the first (or only) database
+ setContents(split(contents, bd, ','));
else
return false;
}
-#endif
return true;
}
-void InsetBibtex::setParams(InsetBibtexParams const & params)
-{
- params_ = params;
-}
-
-
-string const InsetBibtexMailer::name_ = "bibtex";
-
-
-InsetBibtexMailer::InsetBibtexMailer(InsetBibtex & inset)
- : inset_(inset)
-{}
-
-
-string const InsetBibtexMailer::inset2string(Buffer const & buffer) const
-{
- return params2string(inset_.params(), buffer);
-}
-
-
-void InsetBibtexMailer::string2params(string const & in,
- Buffer const & buffer,
- InsetBibtexParams & params)
-{
- params = InsetBibtexParams();
-
- if (in.empty())
- return;
-
- istringstream data(STRCONV(in));
- LyXLex lex(0,0);
- lex.setStream(data);
-
- if (lex.isOK()) {
- lex.next();
- string const token = lex.getString();
- if (token != name_)
- return;
- }
-
- // This is part of the inset proper that is usually swallowed
- // by Buffer::readInset
- if (lex.isOK()) {
- lex.next();
- string const token = lex.getString();
- if (token != "Bibtex")
- return;
- }
-
- if (lex.isOK()) {
- params.read(buffer, lex);
- }
-}
-
-
-string const InsetBibtexMailer::params2string(InsetBibtexParams const & params,
- Buffer const & buffer)
+void InsetBibtex::validate(LaTeXFeatures & features) const
{
- ostringstream data;
- data << name_ << ' ';
- params.write(buffer, data);
- data << "\\end_inset\n";
- return STRCONV(data.str());
+ if (features.bufferParams().use_bibtopic)
+ features.require("bibtopic");
}