X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2Finsets%2Finsetbibtex.C;h=fa604fddd381159f9b1e90203dcfaab0bbae44ed;hb=e28331ed63062dea10d0a21b9ec12034b4b17b9a;hp=503e66846307c6cdbd1a79bc4672ff806d5652b0;hpb=9498f9758fcfd317c74969b12397b753846eb2f6;p=lyx.git diff --git a/src/insets/insetbibtex.C b/src/insets/insetbibtex.C index 503e668463..fa604fddd3 100644 --- a/src/insets/insetbibtex.C +++ b/src/insets/insetbibtex.C @@ -4,7 +4,6 @@ * 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. */ @@ -12,478 +11,412 @@ #include #include "insetbibtex.h" -#include "metricsinfo.h" + #include "buffer.h" -#include "BufferView.h" +#include "bufferparams.h" +#include "dispatchresult.h" #include "debug.h" +#include "encoding.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/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 -#include -using namespace lyx; +namespace lyx { + +using support::absolutePath; +using support::ascii_lowercase; +using support::changeExtension; +using support::contains; +using support::copy; +using support::DocFileName; using support::FileName; +using support::findtexfile; +using support::isFileReadable; +using support::latex_path; +using support::ltrim; +using support::makeAbsPath; +using support::makeRelPath; +using support::Path; +using support::prefixIs; +using support::removeExtension; +using support::rtrim; +using support::split; +using support::subst; +using support::tokenPos; +using support::trim; + +namespace Alert = frontend::Alert; +namespace os = support::os; -using std::ostream; -using std::ifstream; using std::endl; -using std::vector; +using std::getline; +using std::string; +using std::ostream; using std::pair; +using std::vector; -InsetBibtexParams::InsetBibtexParams() - : bibtotoc(false) +InsetBibtex::InsetBibtex(InsetCommandParams const & p) + : InsetCommand(p, "bibtex") {} -bool InsetBibtexParams::empty() const +std::auto_ptr InsetBibtex::doClone() const { - return databases.empty(); + return std::auto_ptr(new InsetBibtex(*this)); } -void InsetBibtexParams::erase() +void InsetBibtex::doDispatch(LCursor & cur, FuncRequest & cmd) { - databases.clear(); - style.erase(); - bibtotoc = false; -} + switch (cmd.action) { + case LFUN_INSET_MODIFY: { + InsetCommandParams p("bibtex"); + InsetCommandMailer::string2params("bibtex", to_utf8(cmd.argument()), p); + if (!p.getCmdName().empty()) { + setParams(p); + cur.buffer().updateBibfilesCache(); + } else + cur.noUpdate(); + break; + } -void InsetBibtexParams::write(Buffer const & buffer, std::ostream & os) const -{ - os << "Bibtex\n"; - - vector::const_iterator it = databases.begin(); - vector::const_iterator end = databases.end(); - for (; it != end; ++it) { - os << "\tfilename " - << it->outputFilename(buffer.filePath()) - << '\n'; + default: + InsetCommand::doDispatch(cur, cmd); + break; } - if (!style.empty()) - os << "\tstyle " << style << '\n'; - if (bibtotoc) - os << "\tbibtotoc " << tostr(bibtotoc) << '\n'; } -void InsetBibtexParams::read(Buffer const & buffer, LyXLex & lex) +docstring const InsetBibtex::getScreenLabel(Buffer const &) const { - 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."); - } + return _("BibTeX Generated Bibliography"); } -InsetBibtex::InsetBibtex() - : set_label_(false), center_indent_(0) -{} - - - -InsetBibtex::InsetBibtex(InsetBibtexParams const & p) - : params_(p), set_label_(false), center_indent_(0) -{} - +namespace { -InsetBibtex::~InsetBibtex() +string normalize_name(Buffer const & buffer, OutputParams const & runparams, + string const & name, string const & ext) { - InsetBibtexMailer(*this).hideDialog(); + string const fname = makeAbsPath(name, buffer.filePath()).absFilename(); + if (absolutePath(name) || !isFileReadable(FileName(fname + ext))) + return name; + else if (!runparams.nice) + return fname; + else + return makeRelPath(fname, buffer.getMasterBuffer()->filePath()); } - -std::auto_ptr InsetBibtex::clone() const -{ - return std::auto_ptr(new InsetBibtex(*this)); } -dispatch_result InsetBibtex::localDispatch(FuncRequest const & cmd) +int InsetBibtex::latex(Buffer const & buffer, odocstream & os, + OutputParams const & runparams) const { - switch (cmd.action) { - - case LFUN_INSET_EDIT: - InsetBibtexMailer(*this).showDialog(cmd.view()); - return DISPATCHED; + // 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 Separator; + typedef boost::tokenizer Tokenizer; + + Separator const separator(from_ascii(",").c_str()); + // The tokenizer must not be called with temporary strings, since + // it does not make a copy and uses iterators of the string further + // down. getParam returns a reference, so this is OK. + Tokenizer const tokens(getParam("bibfiles"), separator); + Tokenizer::const_iterator const begin = tokens.begin(); + Tokenizer::const_iterator const end = tokens.end(); + + odocstringstream dbs; + for (Tokenizer::const_iterator it = begin; it != end; ++it) { + docstring const input = trim(*it); + // FIXME UNICODE + string utf8input(to_utf8(input)); + string database = + normalize_name(buffer, runparams, utf8input, ".bib"); + FileName const try_in_file(makeAbsPath(database + ".bib", buffer.filePath())); + bool const not_from_texmf = isFileReadable(try_in_file); + + if (!runparams.inComment && !runparams.dryrun && !runparams.nice && + not_from_texmf) { + + // mangledFilename() needs the extension + DocFileName const in_file = DocFileName(try_in_file); + database = removeExtension(in_file.mangledFilename()); + FileName 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; + } + } - case LFUN_INSET_MODIFY: { - Buffer const * buffer = cmd.view()->buffer(); - InsetBibtexParams p; - InsetBibtexMailer::string2params(cmd.argument, *buffer, p); - setParams(p); - return DISPATCHED; + if (it != begin) + dbs << ','; + // FIXME UNICODE + dbs << from_utf8(latex_path(database)); } + docstring const db_out = dbs.str(); - case LFUN_INSET_DIALOG_UPDATE: - InsetBibtexMailer(*this).updateDialog(cmd.view()); - return DISPATCHED; + // Post this warning only once. + static bool warned_about_spaces = false; + if (!warned_about_spaces && + runparams.nice && db_out.find(' ') != docstring::npos) { + warned_about_spaces = true; - case LFUN_MOUSE_RELEASE: - return localDispatch(FuncRequest(cmd.view(), LFUN_INSET_EDIT)); + Alert::warning(_("Export Warning!"), + _("There are spaces in the paths to your BibTeX databases.\n" + "BibTeX will be unable to find them.")); - default: - return InsetOld::localDispatch(cmd); } -} - -string const InsetBibtex::getScreenLabel(Buffer const &) const -{ - return _("BibTeX Generated References"); -} - - -void InsetBibtex::metrics(MetricsInfo & mi, Dimension & dim) const -{ - if (!set_label_) { - set_label_ = true; - button_.update(getScreenLabel(*mi.base.bv->buffer()), - editable() != NOT_EDITABLE); + // Style-Options + string style = to_utf8(getParam("options")); // maybe empty! and with bibtotoc + string bibtotoc; + if (prefixIs(style, "bibtotoc")) { + bibtotoc = "bibtotoc"; + if (contains(style, ',')) { + style = split(style, bibtotoc, ','); + } } - button_.metrics(mi, dim); - center_indent_ = (mi.base.textwidth - dim.wid) / 2; - dim.wid = mi.base.textwidth; - dim_ = dim; -} - - -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 -{ - params().write(buffer, os); -} - - -void InsetBibtex::read(Buffer const & buffer, LyXLex & lex) -{ - InsetBibtexParams p; - p.read(buffer, lex); - - // Replace the inset's store - setParams(p); -} - -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; + // line count + int nlines = 0; + + if (!style.empty()) { + string base = + normalize_name(buffer, runparams, style, ".bst"); + FileName const try_in_file(makeAbsPath(base + ".bst", buffer.filePath())); + bool const not_from_texmf = isFileReadable(try_in_file); + // If this style does not come from texmf and we are not + // exporting to .tex copy it to the tmp directory. + // This prevents problems with spaces and 8bit charcaters + // in the file name. + if (!runparams.inComment && !runparams.dryrun && !runparams.nice && + not_from_texmf) { + // use new style name + DocFileName const in_file = DocFileName(try_in_file); + base = removeExtension(in_file.mangledFilename()); + FileName const out_file(makeAbsPath(base + ".bst", + buffer.getMasterBuffer()->temppath())); + bool const success = copy(in_file, out_file); + if (!success) { + lyxerr << "Failed to copy '" << in_file + << "' to '" << out_file << "'" + << endl; + } + } + // FIXME UNICODE + os << "\\bibliographystyle{" + << from_utf8(latex_path(normalize_name(buffer, runparams, base, ".bst"))) + << "}\n"; + nlines += 1; + } - string const abs_style = - support::MakeAbsPath(style, buffer.filePath()); - if (!runparams.nice && support::IsFileReadable(abs_style + ".bst")) - style = abs_style; + // Post this warning only once. + static bool warned_about_bst_spaces = false; + if (!warned_about_bst_spaces && runparams.nice && contains(style, ' ')) { + warned_about_bst_spaces = true; + Alert::warning(_("Export Warning!"), + _("There are spaces in the path to your BibTeX style file.\n" + "BibTeX will be unable to find it.")); + } - ss << "\\bibliographystyle{" << style << "}\n"; + if (!db_out.empty() && buffer.params().use_bibtopic){ + os << "\\begin{btSect}{" << db_out << "}\n"; + docstring btprint = getParam("btprint"); + if (btprint.empty()) + // default + btprint = from_ascii("btPrintCited"); + os << "\\" << btprint << "\n" + << "\\end{btSect}\n"; + nlines += 3; } - // 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"; + // 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::const_iterator begin = params().databases.begin(); - vector::const_iterator end = params().databases.end(); - vector::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 const InsetBibtex::getFiles(Buffer const & buffer) const { - return 0; -} + Path p(buffer.filePath()); + vector vec; -int InsetBibtex::linuxdoc(Buffer const &, std::ostream &) const -{ - return 0; -} + string tmp; + // FIXME UNICODE + string bibfiles = to_utf8(getParam("bibfiles")); + bibfiles = split(bibfiles, tmp, ','); + while (!tmp.empty()) { + FileName const 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 const InsetBibtex::getFiles(Buffer const & buffer) const -{ - support::Path p(buffer.filePath()); - - vector files; - vector::const_iterator it = params().databases.begin(); - vector::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; } // This method returns a comma separated list of Bibtex entries void InsetBibtex::fillWithBibKeys(Buffer const & buffer, - std::vector > & keys) const + std::vector > & keys) const { - vector const files = getFiles(buffer); - for (vector::const_iterator it = files.begin(); + vector const files = getFiles(buffer); + for (vector::const_iterator it = files.begin(); it != files.end(); ++ it) { // This is a _very_ simple parser for Bibtex database // files. All it does is to look for lines starting // in @ and not being @preamble and @string entries. // It does NOT do any syntax checking! - ifstream ifs(it->c_str()); - string linebuf0; + + // Officially bibtex does only support ASCII, but in practice + // you can use the encoding of the main document as long as + // some elements like keys and names are pure ASCII. Therefore + // we convert the file from the buffer encoding. + // We don't restrict keys to ASCII in LyX, since our own + // InsetBibitem can generate non-ASCII keys, and nonstandard + // 8bit clean bibtex forks exist. + idocfstream ifs(it->toFilesystemEncoding().c_str(), + std::ios_base::in, + buffer.params().encoding().iconvName()); + docstring linebuf0; while (getline(ifs, linebuf0)) { - string linebuf = support::trim(linebuf0); - if (linebuf.empty()) continue; - if (support::prefixIs(linebuf, "@")) { - linebuf = support::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"); + docstring linebuf = trim(linebuf0); + if (linebuf.empty()) + continue; + if (prefixIs(linebuf, '@')) { + linebuf = subst(linebuf, '{', '('); + docstring tmp; + linebuf = split(linebuf, tmp, '('); + tmp = ascii_lowercase(tmp); + if (!prefixIs(tmp, from_ascii("@string")) && + !prefixIs(tmp, from_ascii("@preamble"))) { + linebuf = split(linebuf, tmp, ','); + tmp = ltrim(tmp, " \t"); if (!tmp.empty()) { - keys.push_back(pair(tmp,string())); + // FIXME UNICODE + keys.push_back(pair( + to_utf8(tmp), docstring())); } } - } else if (!keys.empty()) { - keys.back().second += linebuf + "\n"; - } + } else if (!keys.empty()) + keys.back().second += linebuf + '\n'; } } } -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 contents(getContents()); - if (!support::contains(contents, db)) { - if (!contents.empty()) - contents += ','; - setContents(contents + db); + // FIXME UNICODE + string bibfiles(to_utf8(getParam("bibfiles"))); + if (tokenPos(bibfiles, ',', db) == -1) { + if (!bibfiles.empty()) + bibfiles += ','; + setParam("bibfiles", from_utf8(bibfiles + 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)) { + // FIXME UNICODE + string bibfiles(to_utf8(getParam("bibfiles"))); + if (contains(bibfiles, db)) { + int const n = tokenPos(bibfiles, ',', 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; + setParam("bibfiles", from_utf8(subst(bibfiles, tmp, string()))); } else if (n == 0) - setContents(support::split(getContents(), bd, ',')); + // this is the first (or only) database + setParam("bibfiles", from_utf8(split(bibfiles, 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 +void InsetBibtex::validate(LaTeXFeatures & features) const { - return params2string(inset_.params(), buffer); + if (features.bufferParams().use_bibtopic) + features.require("bibtopic"); } -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) -{ - ostringstream data; - data << name_ << ' '; - params.write(buffer, data); - data << "\\end_inset\n"; - return STRCONV(data.str()); -} +} // namespace lyx