2 * \file InsetBibtex.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
7 * \author Richard Heck (BibTeX parser improvements)
9 * Full author contact details are available in file CREDITS.
14 #include "InsetBibtex.h"
16 #include "BiblioInfo.h"
18 #include "BufferParams.h"
20 #include "DispatchResult.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
26 #include "LaTeXFeatures.h"
27 #include "output_xhtml.h"
28 #include "OutputParams.h"
29 #include "PDFOptions.h"
30 #include "TextClass.h"
32 #include "frontends/alert.h"
34 #include "support/convert.h"
35 #include "support/debug.h"
36 #include "support/docstream.h"
37 #include "support/ExceptionMessage.h"
38 #include "support/FileNameList.h"
39 #include "support/filetools.h"
40 #include "support/gettext.h"
41 #include "support/lstrings.h"
42 #include "support/os.h"
43 #include "support/Path.h"
44 #include "support/textutils.h"
49 using namespace lyx::support;
53 namespace Alert = frontend::Alert;
54 namespace os = support::os;
57 InsetBibtex::InsetBibtex(Buffer * buf, InsetCommandParams const & p)
58 : InsetCommand(buf, p)
60 buffer().invalidateBibinfoCache();
64 InsetBibtex::~InsetBibtex()
67 buffer().invalidateBibfileCache();
71 ParamInfo const & InsetBibtex::findInfo(string const & /* cmdName */)
73 static ParamInfo param_info_;
74 if (param_info_.empty()) {
75 param_info_.add("btprint", ParamInfo::LATEX_OPTIONAL);
76 param_info_.add("bibfiles", ParamInfo::LATEX_REQUIRED);
77 param_info_.add("options", ParamInfo::LYX_INTERNAL);
83 void InsetBibtex::doDispatch(Cursor & cur, FuncRequest & cmd)
85 switch (cmd.action()) {
91 case LFUN_INSET_MODIFY: {
92 InsetCommandParams p(BIBTEX_CODE);
94 if (!InsetCommand::string2params(to_utf8(cmd.argument()), p)) {
98 } catch (ExceptionMessage const & message) {
99 if (message.type_ == WarningException) {
100 Alert::warning(message.title_, message.details_);
101 cur.noScreenUpdate();
108 buffer().invalidateBibfileCache();
109 cur.forceBufferUpdate();
114 InsetCommand::doDispatch(cur, cmd);
120 bool InsetBibtex::getStatus(Cursor & cur, FuncRequest const & cmd,
121 FuncStatus & flag) const
123 switch (cmd.action()) {
124 case LFUN_INSET_EDIT:
125 flag.setEnabled(true);
129 return InsetCommand::getStatus(cur, cmd, flag);
134 void InsetBibtex::editDatabases() const
136 vector<docstring> bibfilelist = getVectorFromString(getParam("bibfiles"));
138 if (bibfilelist.empty())
141 int nr_databases = bibfilelist.size();
142 if (nr_databases > 1) {
143 docstring message = bformat(_("The BibTeX inset includes %1$s databases.\n"
144 "If you proceed, all of them will be opened."),
145 convert<docstring>(nr_databases));
146 int const ret = Alert::prompt(_("Open Databases?"),
147 message, 0, 1, _("&Cancel"), _("&Proceed"));
153 vector<docstring>::const_iterator it = bibfilelist.begin();
154 vector<docstring>::const_iterator en = bibfilelist.end();
155 for (; it != en; ++it) {
156 FileName const bibfile = getBibTeXPath(*it, buffer());
157 formats.edit(buffer(), bibfile,
158 formats.getFormatFromFile(bibfile));
163 docstring InsetBibtex::screenLabel() const
165 return _("BibTeX Generated Bibliography");
169 docstring InsetBibtex::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
171 docstring item = from_ascii("* ");
172 docstring tip = _("Databases:") + "\n";
173 vector<docstring> bibfilelist = getVectorFromString(getParam("bibfiles"));
175 if (bibfilelist.empty()) {
179 vector<docstring>::const_iterator it = bibfilelist.begin();
180 vector<docstring>::const_iterator en = bibfilelist.end();
181 for (; it != en; ++it) {
189 docstring style = getParam("options"); // maybe empty! and with bibtotoc
190 docstring bibtotoc = from_ascii("bibtotoc");
191 if (prefixIs(style, bibtotoc)) {
193 if (contains(style, char_type(',')))
194 style = split(style, bibtotoc, char_type(','));
197 tip += _("Style File:") +"\n";
204 tip += "\n" + _("Lists:") + " ";
205 docstring btprint = getParam("btprint");
206 if (btprint == "btPrintAll")
207 tip += _("all references");
208 else if (btprint == "btPrintNotCited")
209 tip += _("all uncited references");
211 tip += _("all cited references");
215 tip += _("included in TOC");
222 static string normalizeName(Buffer const & buffer,
223 OutputParams const & runparams, string const & name, string const & ext)
225 string const fname = makeAbsPath(name, buffer.filePath()).absFileName();
226 if (FileName::isAbsolute(name) || !FileName(fname + ext).isReadableFile())
232 return to_utf8(makeRelPath(from_utf8(fname),
233 from_utf8(buffer.masterBuffer()->filePath())));
237 void InsetBibtex::updateBuffer(ParIterator const &, UpdateType)
239 if (buffer().isBibInfoCacheValid())
241 BiblioInfo & bi = buffer().masterBibInfo();
246 int InsetBibtex::latex(odocstream & os, OutputParams const & runparams) const
248 // the sequence of the commands:
249 // 1. \bibliographystyle{style}
250 // 2. \addcontentsline{...} - if option bibtotoc set
251 // 3. \bibliography{database}
252 // and with bibtopic:
253 // 1. \bibliographystyle{style}
254 // 2. \begin{btSect}{database}
255 // 3. \btPrint{Cited|NotCited|All}
259 // If we are processing the LaTeX file in a temp directory then
260 // copy the .bib databases to this temp directory, mangling their
261 // names in the process. Store this mangled name in the list of
263 // (We need to do all this because BibTeX *really*, *really*
264 // can't handle "files with spaces" and Windows users tend to
265 // use such filenames.)
266 // Otherwise, store the (maybe absolute) path to the original,
267 // unmangled database name.
268 vector<docstring> bibfilelist = getVectorFromString(getParam("bibfiles"));
269 vector<docstring>::const_iterator it = bibfilelist.begin();
270 vector<docstring>::const_iterator en = bibfilelist.end();
271 odocstringstream dbs;
274 for (; it != en; ++it) {
275 string utf8input = to_utf8(*it);
277 normalizeName(buffer(), runparams, utf8input, ".bib");
278 FileName const try_in_file =
279 makeAbsPath(database + ".bib", buffer().filePath());
280 bool const not_from_texmf = try_in_file.isReadableFile();
282 if (!runparams.inComment && !runparams.dryrun && !runparams.nice &&
284 // mangledFileName() needs the extension
285 DocFileName const in_file = DocFileName(try_in_file);
286 database = removeExtension(in_file.mangledFileName());
287 FileName const out_file = makeAbsPath(database + ".bib",
288 buffer().masterBuffer()->temppath());
290 bool const success = in_file.copyTo(out_file);
292 lyxerr << "Failed to copy '" << in_file
293 << "' to '" << out_file << "'"
296 } else if (!runparams.inComment && runparams.nice && not_from_texmf) {
297 if (!isValidLaTeXFileName(database)) {
298 frontend::Alert::warning(_("Invalid filename"),
299 _("The following filename will cause troubles "
300 "when running the exported file through LaTeX: ") +
301 from_utf8(database));
303 if (!isValidDVIFileName(database)) {
304 frontend::Alert::warning(_("Problematic filename for DVI"),
305 _("The following filename can cause troubles "
306 "when running the exported file through LaTeX "
307 "and opening the resulting DVI: ") +
308 from_utf8(database), true);
317 dbs << from_utf8(latex_path(database));
319 docstring const db_out = dbs.str();
321 // Post this warning only once.
322 static bool warned_about_spaces = false;
323 if (!warned_about_spaces &&
324 runparams.nice && db_out.find(' ') != docstring::npos) {
325 warned_about_spaces = true;
326 Alert::warning(_("Export Warning!"),
327 _("There are spaces in the paths to your BibTeX databases.\n"
328 "BibTeX will be unable to find them."));
331 string style = to_utf8(getParam("options")); // maybe empty! and with bibtotoc
333 if (prefixIs(style, "bibtotoc")) {
334 bibtotoc = "bibtotoc";
335 if (contains(style, ','))
336 style = split(style, bibtotoc, ',');
342 if (!style.empty()) {
343 string base = normalizeName(buffer(), runparams, style, ".bst");
344 FileName const try_in_file =
345 makeAbsPath(base + ".bst", buffer().filePath());
346 bool const not_from_texmf = try_in_file.isReadableFile();
347 // If this style does not come from texmf and we are not
348 // exporting to .tex copy it to the tmp directory.
349 // This prevents problems with spaces and 8bit charcaters
351 if (!runparams.inComment && !runparams.dryrun && !runparams.nice &&
353 // use new style name
354 DocFileName const in_file = DocFileName(try_in_file);
355 base = removeExtension(in_file.mangledFileName());
356 FileName const out_file = makeAbsPath(base + ".bst",
357 buffer().masterBuffer()->temppath());
358 bool const success = in_file.copyTo(out_file);
360 lyxerr << "Failed to copy '" << in_file
361 << "' to '" << out_file << "'"
366 os << "\\bibliographystyle{"
367 << from_utf8(latex_path(normalizeName(buffer(), runparams, base, ".bst")))
372 // Post this warning only once.
373 static bool warned_about_bst_spaces = false;
374 if (!warned_about_bst_spaces && runparams.nice && contains(style, ' ')) {
375 warned_about_bst_spaces = true;
376 Alert::warning(_("Export Warning!"),
377 _("There are spaces in the path to your BibTeX style file.\n"
378 "BibTeX will be unable to find it."));
381 if (!db_out.empty() && buffer().params().use_bibtopic) {
382 os << "\\begin{btSect}{" << db_out << "}\n";
383 docstring btprint = getParam("btprint");
386 btprint = from_ascii("btPrintCited");
387 os << "\\" << btprint << "\n"
388 << "\\end{btSect}\n";
393 if (!bibtotoc.empty() && !buffer().params().use_bibtopic) {
394 // set label for hyperref, see http://www.lyx.org/trac/ticket/6470
395 if (buffer().params().pdfoptions().use_hyperref)
396 os << "\\phantomsection";
397 if (buffer().params().documentClass().hasLaTeXLayout("chapter"))
398 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
399 else if (buffer().params().documentClass().hasLaTeXLayout("section"))
400 os << "\\addcontentsline{toc}{section}{\\refname}";
403 if (!db_out.empty() && !buffer().params().use_bibtopic) {
404 docstring btprint = getParam("btprint");
405 if (btprint == "btPrintAll") {
406 os << "\\nocite{*}\n";
409 os << "\\bibliography{" << db_out << "}\n";
417 support::FileNameList InsetBibtex::getBibFiles() const
419 FileName path(buffer().filePath());
420 support::PathChanger p(path);
422 support::FileNameList vec;
424 vector<docstring> bibfilelist = getVectorFromString(getParam("bibfiles"));
425 vector<docstring>::const_iterator it = bibfilelist.begin();
426 vector<docstring>::const_iterator en = bibfilelist.end();
427 for (; it != en; ++it) {
428 FileName const file = getBibTeXPath(*it, buffer());
433 LYXERR0("Couldn't find " + to_utf8(*it) + " in InsetBibtex::getBibFiles()!");
442 // methods for parsing bibtex files
444 typedef map<docstring, docstring> VarMap;
446 /// remove whitespace characters, optionally a single comma,
447 /// and further whitespace characters from the stream.
448 /// @return true if a comma was found, false otherwise
450 bool removeWSAndComma(ifdocstream & ifs) {
459 } while (ifs && isSpace(ch));
472 } while (ifs && isSpace(ch));
487 /// remove whitespace characters, read characer sequence
488 /// not containing whitespace characters or characters in
489 /// delimChars, and remove further whitespace characters.
491 /// @return true if a string of length > 0 could be read.
493 bool readTypeOrKey(docstring & val, ifdocstream & ifs,
494 docstring const & delimChars, docstring const & illegalChars,
507 } while (ifs && isSpace(ch));
513 bool legalChar = true;
514 while (ifs && !isSpace(ch) &&
515 delimChars.find(ch) == docstring::npos &&
516 (legalChar = (illegalChars.find(ch) == docstring::npos))
519 if (chCase == makeLowerCase)
520 val += lowercase(ch);
532 while (ifs && isSpace(ch)) {
540 return val.length() > 0;
543 /// read subsequent bibtex values that are delimited with a #-character.
544 /// Concatenate all parts and replace names with the associated string in
545 /// the variable strings.
546 /// @return true if reading was successfull (all single parts were delimited
548 bool readValue(docstring & val, ifdocstream & ifs, const VarMap & strings) {
561 } while (ifs && isSpace(ch));
566 // check for field type
569 // read integer value
573 } while (ifs && isDigit(ch));
578 } else if (ch == '"' || ch == '{') {
580 char_type delim = ch == '"' ? '"': '}';
585 } while (ifs && isSpace(ch));
590 // We now have the first non-whitespace character
591 // We'll collapse adjacent whitespace.
592 bool lastWasWhiteSpace = false;
594 // inside this delimited text braces must match.
595 // Thus we can have a closing delimiter only
596 // when nestLevel == 0
599 while (ifs && (nestLevel > 0 || ch != delim)) {
601 lastWasWhiteSpace = true;
605 // We output the space only after we stop getting
606 // whitespace so as not to output any whitespace
607 // at the end of the value.
608 if (lastWasWhiteSpace) {
609 lastWasWhiteSpace = false;
615 // update nesting level
634 // FIXME Why is this here?
642 // reading a string name
645 while (ifs && !isSpace(ch) && ch != '#' && ch != ',' && ch != '}' && ch != ')') {
646 strName += lowercase(ch);
653 // replace the string with its assigned value or
654 // discard it if it's not assigned
655 if (strName.length()) {
656 VarMap::const_iterator pos = strings.find(strName);
657 if (pos != strings.end()) {
664 while (ifs && isSpace(ch)) {
671 // continue reading next value on concatenate with '#'
681 void InsetBibtex::fillWithBibKeys(BiblioInfo & keylist,
682 InsetIterator const & /*di*/) const
684 fillWithBibKeys(keylist);
688 void InsetBibtex::fillWithBibKeys(BiblioInfo & keylist) const
690 // This bibtex parser is a first step to parse bibtex files
693 // - it reads the whole bibtex entry and does a syntax check
694 // (matching delimiters, missing commas,...
695 // - it recovers from errors starting with the next @-character
696 // - it reads @string definitions and replaces them in the
698 // - it accepts more characters in keys or value names than
701 // Officially bibtex does only support ASCII, but in practice
702 // you can use the encoding of the main document as long as
703 // some elements like keys and names are pure ASCII. Therefore
704 // we convert the file from the buffer encoding.
705 // We don't restrict keys to ASCII in LyX, since our own
706 // InsetBibitem can generate non-ASCII keys, and nonstandard
707 // 8bit clean bibtex forks exist.
708 support::FileNameList const files = getBibFiles();
709 support::FileNameList::const_iterator it = files.begin();
710 support::FileNameList::const_iterator en = files.end();
711 for (; it != en; ++ it) {
712 ifdocstream ifs(it->toFilesystemEncoding().c_str(),
713 ios_base::in, buffer().params().encoding().iconvName());
729 if (!readTypeOrKey(entryType, ifs, from_ascii("{("), docstring(), makeLowerCase)) {
730 lyxerr << "BibTeX Parser: Error reading entry type." << std::endl;
735 lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
739 if (entryType == from_ascii("comment")) {
740 ifs.ignore(numeric_limits<int>::max(), '\n');
746 lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
750 if ((ch != '(') && (ch != '{')) {
751 lyxerr << "BibTeX Parser: Invalid entry delimiter." << std::endl;
757 if (entryType == from_ascii("string")) {
759 // read string and add it to the strings map
760 // (or replace it's old value)
764 if (!readTypeOrKey(name, ifs, from_ascii("="), from_ascii("#{}(),"), makeLowerCase)) {
765 lyxerr << "BibTeX Parser: Error reading string name." << std::endl;
770 lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
774 // next char must be an equal sign
776 if (!ifs || ch != '=') {
777 lyxerr << "BibTeX Parser: No `=' after string name: " <<
778 name << "." << std::endl;
782 if (!readValue(value, ifs, strings)) {
783 lyxerr << "BibTeX Parser: Unable to read value for string: " <<
784 name << "." << std::endl;
788 strings[name] = value;
790 } else if (entryType == from_ascii("preamble")) {
792 // preamble definitions are discarded.
793 // can they be of any use in lyx?
796 if (!readValue(value, ifs, strings)) {
797 lyxerr << "BibTeX Parser: Unable to read preamble value." << std::endl;
803 // Citation entry. Try to read the key.
806 if (!readTypeOrKey(key, ifs, from_ascii(","), from_ascii("}"), keepCase)) {
807 lyxerr << "BibTeX Parser: Unable to read key for entry type:" <<
808 entryType << "." << std::endl;
813 lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
817 /////////////////////////////////////////////
818 // now we have a key, so we will add an entry
819 // (even if it's empty, as bibtex does)
821 // we now read the field = value pairs.
822 // all items must be separated by a comma. If
823 // it is missing the scanning of this entry is
824 // stopped and the next is searched.
828 BibTeXInfo keyvalmap(key, entryType);
830 bool readNext = removeWSAndComma(ifs);
832 while (ifs && readNext) {
835 if (!readTypeOrKey(name, ifs, from_ascii("="),
836 from_ascii("{}(),"), makeLowerCase) || !ifs)
839 // next char must be an equal sign
840 // FIXME Whitespace??
843 lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
847 lyxerr << "BibTeX Parser: Missing `=' after field name: " <<
848 name << ", for key: " << key << "." << std::endl;
854 if (!readValue(value, ifs, strings)) {
855 lyxerr << "BibTeX Parser: Unable to read value for field: " <<
856 name << ", for key: " << key << "." << std::endl;
860 keyvalmap[name] = value;
861 data += "\n\n" + value;
862 keylist.addFieldName(name);
863 readNext = removeWSAndComma(ifs);
867 keylist.addEntryType(entryType);
868 keyvalmap.setAllData(data);
869 keylist[key] = keyvalmap;
870 } //< else (citation entry)
872 } //< for loop over files
876 FileName InsetBibtex::getBibTeXPath(docstring const & filename, Buffer const & buf)
878 string texfile = changeExtension(to_utf8(filename), "bib");
879 // note that, if the filename can be found directly from the path,
880 // findtexfile will just return a FileName object for that path.
881 FileName file(findtexfile(texfile, "bib"));
883 file = FileName(makeAbsPath(texfile, buf.filePath()));
888 bool InsetBibtex::addDatabase(docstring const & db)
890 docstring bibfiles = getParam("bibfiles");
891 if (tokenPos(bibfiles, ',', db) != -1)
893 if (!bibfiles.empty())
895 setParam("bibfiles", bibfiles + db);
900 bool InsetBibtex::delDatabase(docstring const & db)
902 docstring bibfiles = getParam("bibfiles");
903 if (contains(bibfiles, db)) {
904 int const n = tokenPos(bibfiles, ',', db);
907 // this is not the first database
908 docstring tmp = ',' + bd;
909 setParam("bibfiles", subst(bibfiles, tmp, docstring()));
911 // this is the first (or only) database
912 setParam("bibfiles", split(bibfiles, bd, ','));
920 void InsetBibtex::validate(LaTeXFeatures & features) const
922 if (features.bufferParams().use_bibtopic)
923 features.require("bibtopic");
925 // It'd be better to be able to get this from an InsetLayout, but at present
926 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
927 if (features.runparams().flavor == OutputParams::HTML)
928 features.addPreambleSnippet("<style type=\"text/css\">\n"
929 "div.bibtexentry { margin-left: 2em; text-indent: -2em; }\n"
930 "span.bibtexlabel:before{ content: \"[\"; }\n"
931 "span.bibtexlabel:after{ content: \"] \"; }\n"
937 // docstring InsetBibtex::entriesAsXHTML(vector<docstring> const & entries)
938 // And then here just: entriesAsXHTML(buffer().masterBibInfo().citedEntries())
939 docstring InsetBibtex::xhtml(XHTMLStream & xs, OutputParams const &) const
941 BiblioInfo const & bibinfo = buffer().masterBibInfo();
942 vector<docstring> const & cites = bibinfo.citedEntries();
943 CiteEngine const engine = buffer().params().citeEngine();
945 (engine == ENGINE_BASIC || engine == ENGINE_NATBIB_NUMERICAL);
947 docstring reflabel = from_ascii("References");
948 Language const * l = buffer().params().language;
950 reflabel = translateIfPossible(reflabel, l->code());
952 xs << html::StartTag("h2", "class='bibtex'")
954 << html::EndTag("h2")
955 << html::StartTag("div", "class='bibtex'");
957 // Now we loop over the entries
958 vector<docstring>::const_iterator vit = cites.begin();
959 vector<docstring>::const_iterator const ven = cites.end();
960 for (; vit != ven; ++vit) {
961 BiblioInfo::const_iterator const biit = bibinfo.find(*vit);
962 if (biit == bibinfo.end())
964 BibTeXInfo const & entry = biit->second;
965 xs << html::StartTag("div", "class='bibtexentry'");
967 // The same name/id problem we have elsewhere.
968 string const attr = "id='" + to_utf8(entry.key()) + "'";
969 xs << html::CompTag("a", attr);
972 citekey = entry.citeNumber();
974 docstring const auth = entry.getAbbreviatedAuthor();
975 // we do it this way so as to access the xref, if necessary
976 // note that this also gives us the modifier
977 docstring const year = bibinfo.getYear(*vit, true);
978 if (!auth.empty() && !year.empty())
979 citekey = auth + ' ' + year;
981 if (citekey.empty()) {
982 citekey = entry.label();
984 citekey = entry.key();
986 xs << html::StartTag("span", "class='bibtexlabel'")
988 << html::EndTag("span");
989 // FIXME Right now, we are calling BibInfo::getInfo on the key,
990 // which will give us all the cross-referenced info. But for every
991 // entry, so there's a lot of repitition. This should be fixed.
992 xs << html::StartTag("span", "class='bibtexinfo'")
993 << XHTMLStream::ESCAPE_AND
994 << bibinfo.getInfo(entry.key(), buffer(), true)
995 << html::EndTag("span")
996 << html::EndTag("div");
999 xs << html::EndTag("div");
1004 docstring InsetBibtex::contextMenuName() const
1006 return from_ascii("context-bibtex");