]> git.lyx.org Git - features.git/blob - src/insets/InsetBibtex.cpp
Add support for mixed-encoded biblatex files
[features.git] / src / insets / InsetBibtex.cpp
1 /**
2  * \file InsetBibtex.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author Richard Heck (BibTeX parser improvements)
8  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "InsetBibtex.h"
16
17 #include "BiblioInfo.h"
18 #include "Buffer.h"
19 #include "BufferParams.h"
20 #include "CiteEnginesList.h"
21 #include "Cursor.h"
22 #include "DispatchResult.h"
23 #include "Encoding.h"
24 #include "Exporter.h"
25 #include "Format.h"
26 #include "FuncRequest.h"
27 #include "FuncStatus.h"
28 #include "LaTeXFeatures.h"
29 #include "output_latex.h"
30 #include "output_xhtml.h"
31 #include "OutputParams.h"
32 #include "PDFOptions.h"
33 #include "texstream.h"
34 #include "TextClass.h"
35 #include "TocBackend.h"
36
37 #include "frontends/alert.h"
38
39 #include "support/convert.h"
40 #include "support/debug.h"
41 #include "support/docstream.h"
42 #include "support/docstring_list.h"
43 #include "support/ExceptionMessage.h"
44 #include "support/FileNameList.h"
45 #include "support/filetools.h"
46 #include "support/gettext.h"
47 #include "support/lstrings.h"
48 #include "support/os.h"
49 #include "support/PathChanger.h"
50 #include "support/textutils.h"
51
52 #include <limits>
53
54 using namespace std;
55 using namespace lyx::support;
56
57 namespace lyx {
58
59 namespace Alert = frontend::Alert;
60 namespace os = support::os;
61
62
63 InsetBibtex::InsetBibtex(Buffer * buf, InsetCommandParams const & p)
64         : InsetCommand(buf, p)
65 {}
66
67
68 ParamInfo const & InsetBibtex::findInfo(string const & /* cmdName */)
69 {
70         static ParamInfo param_info_;
71         if (param_info_.empty()) {
72                 param_info_.add("btprint", ParamInfo::LATEX_OPTIONAL);
73                 param_info_.add("bibfiles", ParamInfo::LATEX_REQUIRED);
74                 param_info_.add("options", ParamInfo::LYX_INTERNAL);
75                 param_info_.add("encoding", ParamInfo::LYX_INTERNAL);
76                 param_info_.add("file_encodings", ParamInfo::LYX_INTERNAL);
77                 param_info_.add("biblatexopts", ParamInfo::LATEX_OPTIONAL);
78         }
79         return param_info_;
80 }
81
82
83 void InsetBibtex::doDispatch(Cursor & cur, FuncRequest & cmd)
84 {
85         switch (cmd.action()) {
86
87         case LFUN_INSET_EDIT:
88                 editDatabases();
89                 break;
90
91         case LFUN_INSET_MODIFY: {
92                 InsetCommandParams p(BIBTEX_CODE);
93                 try {
94                         if (!InsetCommand::string2params(to_utf8(cmd.argument()), p)) {
95                                 cur.noScreenUpdate();
96                                 break;
97                         }
98                 } catch (ExceptionMessage const & message) {
99                         if (message.type_ == WarningException) {
100                                 Alert::warning(message.title_, message.details_);
101                                 cur.noScreenUpdate();
102                         } else
103                                 throw;
104                         break;
105                 }
106
107                 cur.recordUndo();
108                 setParams(p);
109                 cur.buffer()->clearBibFileCache();
110                 cur.forceBufferUpdate();
111                 break;
112         }
113
114         default:
115                 InsetCommand::doDispatch(cur, cmd);
116                 break;
117         }
118 }
119
120
121 bool InsetBibtex::getStatus(Cursor & cur, FuncRequest const & cmd,
122                 FuncStatus & flag) const
123 {
124         switch (cmd.action()) {
125         case LFUN_INSET_EDIT:
126                 flag.setEnabled(true);
127                 return true;
128
129         default:
130                 return InsetCommand::getStatus(cur, cmd, flag);
131         }
132 }
133
134
135 void InsetBibtex::editDatabases() const
136 {
137         vector<docstring> bibfilelist = getVectorFromString(getParam("bibfiles"));
138
139         if (bibfilelist.empty())
140                 return;
141
142         int nr_databases = bibfilelist.size();
143         if (nr_databases > 1) {
144                         docstring const engine = usingBiblatex() ? _("Biblatex") : _("BibTeX");
145                         docstring message = bformat(_("The %1$s[[BibTeX/Biblatex]] inset includes %2$s databases.\n"
146                                                        "If you proceed, all of them will be opened."),
147                                                         engine, convert<docstring>(nr_databases));
148                         int const ret = Alert::prompt(_("Open Databases?"),
149                                 message, 0, 1, _("&Cancel"), _("&Proceed"));
150
151                         if (ret == 0)
152                                 return;
153         }
154
155         vector<docstring>::const_iterator it = bibfilelist.begin();
156         vector<docstring>::const_iterator en = bibfilelist.end();
157         for (; it != en; ++it) {
158                 FileName const bibfile = buffer().getBibfilePath(*it);
159                 theFormats().edit(buffer(), bibfile,
160                      theFormats().getFormatFromFile(bibfile));
161         }
162 }
163
164
165 bool InsetBibtex::usingBiblatex() const
166 {
167         return buffer().masterParams().useBiblatex();
168 }
169
170
171 docstring InsetBibtex::screenLabel() const
172 {
173         return usingBiblatex() ? _("Biblatex Generated Bibliography")
174                                : _("BibTeX Generated Bibliography");
175 }
176
177
178 docstring InsetBibtex::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
179 {
180         docstring tip = _("Databases:");
181         vector<docstring> bibfilelist = getVectorFromString(getParam("bibfiles"));
182
183         tip += "<ul>";
184         if (bibfilelist.empty())
185                 tip += "<li>" + _("none") + "</li>";
186         else
187                 for (docstring const & bibfile : bibfilelist)
188                         tip += "<li>" + bibfile + "</li>";
189         tip += "</ul>";
190
191         // Style-Options
192         bool toc = false;
193         docstring style = getParam("options"); // maybe empty! and with bibtotoc
194         docstring bibtotoc = from_ascii("bibtotoc");
195         if (prefixIs(style, bibtotoc)) {
196                 toc = true;
197                 if (contains(style, char_type(',')))
198                         style = split(style, bibtotoc, char_type(','));
199         }
200
201         docstring const btprint = getParam("btprint");
202         if (!usingBiblatex()) {
203                 tip += _("Style File:");
204                 tip += "<ul><li>" + (style.empty() ? _("none") : style) + "</li></ul>";
205
206                 tip += _("Lists:") + " ";
207                 if (btprint == "btPrintAll")
208                         tip += _("all references");
209                 else if (btprint == "btPrintNotCited")
210                         tip += _("all uncited references");
211                 else
212                         tip += _("all cited references");
213                 if (toc) {
214                         tip += ", ";
215                         tip += _("included in TOC");
216                 }
217                 if (!buffer().parent()
218                     && buffer().params().multibib == "child") {
219                         tip += "<br />";
220                         tip += _("Note: This bibliography is not output, since bibliographies in the master file "
221                                  "are not allowed with the setting 'Multiple bibliographies per child document'");
222                 }
223         } else {
224                 tip += _("Lists:") + " ";
225                 if (btprint == "bibbysection")
226                         tip += _("all reference units");
227                 else if (btprint == "btPrintAll")
228                         tip += _("all references");
229                 else
230                         tip += _("all cited references");
231                 if (toc) {
232                         tip += ", ";
233                         tip += _("included in TOC");
234                 }
235                 if (!getParam("biblatexopts").empty()) {
236                         tip += "<br />";
237                         tip += _("Options: ") + getParam("biblatexopts");
238                 }
239         }
240
241         return tip;
242 }
243
244
245 void InsetBibtex::latex(otexstream & os, OutputParams const & runparams) const
246 {
247         // The sequence of the commands:
248         // With normal BibTeX:
249         // 1. \bibliographystyle{style}
250         // 2. \addcontentsline{...} - if option bibtotoc set
251         // 3. \bibliography{database}
252         // With bibtopic:
253         // 1. \bibliographystyle{style}
254         // 2. \begin{btSect}{database}
255         // 3. \btPrint{Cited|NotCited|All}
256         // 4. \end{btSect}
257         // With Biblatex:
258         // \printbibliography[biblatexopts]
259         // or
260         // \bibbysection[biblatexopts] - if btprint is "bibbysection"
261
262         // chapterbib does not allow bibliographies in the master
263         if (!usingBiblatex() && !runparams.is_child
264             && buffer().params().multibib == "child")
265                 return;
266
267         string style = to_utf8(getParam("options")); // maybe empty! and with bibtotoc
268         string bibtotoc;
269         if (prefixIs(style, "bibtotoc")) {
270                 bibtotoc = "bibtotoc";
271                 if (contains(style, ','))
272                         style = split(style, bibtotoc, ',');
273         }
274
275         if (usingBiblatex()) {
276                 // Options
277                 string opts = to_utf8(getParam("biblatexopts"));
278                 // bibtotoc-Option
279                 if (!bibtotoc.empty())
280                         opts = opts.empty() ? "heading=bibintoc" : "heading=bibintoc," + opts;
281                 // The bibliography command
282                 docstring btprint = getParam("btprint");
283                 if (btprint == "btPrintAll")
284                         os << "\\nocite{*}\n";
285                 if (btprint == "bibbysection" && !buffer().masterParams().multibib.empty())
286                         os << "\\bibbysection";
287                 else
288                         os << "\\printbibliography";
289                 if (!opts.empty())
290                         os << "[" << opts << "]";
291                 os << "\n";
292         } else {// using BibTeX
293                 // Database(s)
294                 vector<pair<docstring, string>> const dbs =
295                         buffer().prepareBibFilePaths(runparams, getBibFiles(), false);
296                 vector<docstring> db_out;
297                 for (pair<docstring, string> const & db : dbs)
298                         db_out.push_back(db.first);
299                 // Style options
300                 if (style == "default")
301                         style = buffer().masterParams().defaultBiblioStyle();
302                 if (!style.empty() && !buffer().masterParams().useBibtopic()) {
303                         string base = buffer().masterBuffer()->prepareFileNameForLaTeX(style, ".bst", runparams.nice);
304                         FileName const try_in_file =
305                                 makeAbsPath(base + ".bst", buffer().filePath());
306                         bool const not_from_texmf = try_in_file.isReadableFile();
307                         // If this style does not come from texmf and we are not
308                         // exporting to .tex copy it to the tmp directory.
309                         // This prevents problems with spaces and 8bit characters
310                         // in the file name.
311                         if (!runparams.inComment && !runparams.dryrun && !runparams.nice &&
312                             not_from_texmf) {
313                                 // use new style name
314                                 DocFileName const in_file = DocFileName(try_in_file);
315                                 base = removeExtension(in_file.mangledFileName());
316                                 FileName const out_file = makeAbsPath(base + ".bst",
317                                                 buffer().masterBuffer()->temppath());
318                                 bool const success = in_file.copyTo(out_file);
319                                 if (!success) {
320                                         LYXERR0("Failed to copy '" << in_file
321                                                << "' to '" << out_file << "'");
322                                 }
323                         }
324                         // FIXME UNICODE
325                         os << "\\bibliographystyle{"
326                            << from_utf8(latex_path(buffer().prepareFileNameForLaTeX(base, ".bst", runparams.nice)))
327                            << "}\n";
328                 }
329                 // Warn about spaces in bst path. Warn only once.
330                 static bool warned_about_bst_spaces = false;
331                 if (!warned_about_bst_spaces && runparams.nice && contains(style, ' ')) {
332                         warned_about_bst_spaces = true;
333                         Alert::warning(_("Export Warning!"),
334                                        _("There are spaces in the path to your BibTeX style file.\n"
335                                                       "BibTeX will be unable to find it."));
336                 }
337                 // Encoding
338                 bool encoding_switched = false;
339                 Encoding const * const save_enc = runparams.encoding;
340                 docstring const encoding = getParam("encoding");
341                 if (!encoding.empty() && encoding != from_ascii("default")) {
342                         Encoding const * const enc = encodings.fromLyXName(to_ascii(encoding));
343                         if (enc != runparams.encoding) {
344                                 os << "\\bgroup";
345                                 switchEncoding(os.os(), buffer().params(), runparams, *enc, true);
346                                 runparams.encoding = enc;
347                                 encoding_switched = true;
348                         }
349                 }
350                 // Handle the bibtopic case
351                 if (!db_out.empty() && buffer().masterParams().useBibtopic()) {
352                         os << "\\begin{btSect}";
353                         if (!style.empty())
354                                 os << "[" << style << "]";
355                         os << "{" << getStringFromVector(db_out) << "}\n";
356                         docstring btprint = getParam("btprint");
357                         if (btprint.empty())
358                                 // default
359                                 btprint = from_ascii("btPrintCited");
360                         os << "\\" << btprint << "\n"
361                            << "\\end{btSect}\n";
362                 }
363                 // bibtotoc option
364                 if (!bibtotoc.empty() && !buffer().masterParams().useBibtopic()) {
365                         // set label for hyperref, see http://www.lyx.org/trac/ticket/6470
366                         if (buffer().masterParams().pdfoptions().use_hyperref)
367                                         os << "\\phantomsection";
368                         if (buffer().masterParams().documentClass().hasLaTeXLayout("chapter"))
369                                 os << "\\addcontentsline{toc}{chapter}{\\bibname}";
370                         else if (buffer().masterParams().documentClass().hasLaTeXLayout("section"))
371                                 os << "\\addcontentsline{toc}{section}{\\refname}";
372                 }
373                 // The bibliography command
374                 if (!db_out.empty() && !buffer().masterParams().useBibtopic()) {
375                         docstring btprint = getParam("btprint");
376                         if (btprint == "btPrintAll") {
377                                 os << "\\nocite{*}\n";
378                         }
379                         os << "\\bibliography{" << getStringFromVector(db_out) << "}\n";
380                 }
381                 if (encoding_switched){
382                         // Switch back
383                         switchEncoding(os.os(), buffer().params(),
384                                        runparams, *save_enc, true, true);
385                         os << "\\egroup" << breakln;
386                         runparams.encoding = save_enc;
387                 }
388         }
389 }
390
391
392 docstring_list InsetBibtex::getBibFiles() const
393 {
394         return getVectorFromString(getParam("bibfiles"));
395 }
396
397 namespace {
398
399         // methods for parsing bibtex files
400
401         typedef map<docstring, docstring> VarMap;
402
403         /// remove whitespace characters, optionally a single comma,
404         /// and further whitespace characters from the stream.
405         /// @return true if a comma was found, false otherwise
406         ///
407         bool removeWSAndComma(ifdocstream & ifs) {
408                 char_type ch;
409
410                 if (!ifs)
411                         return false;
412
413                 // skip whitespace
414                 do {
415                         ifs.get(ch);
416                 } while (ifs && isSpace(ch));
417
418                 if (!ifs)
419                         return false;
420
421                 if (ch != ',') {
422                         ifs.putback(ch);
423                         return false;
424                 }
425
426                 // skip whitespace
427                 do {
428                         ifs.get(ch);
429                 } while (ifs && isSpace(ch));
430
431                 if (ifs) {
432                         ifs.putback(ch);
433                 }
434
435                 return true;
436         }
437
438
439         enum charCase {
440                 makeLowerCase,
441                 keepCase
442         };
443
444         /// remove whitespace characters, read characer sequence
445         /// not containing whitespace characters or characters in
446         /// delimChars, and remove further whitespace characters.
447         ///
448         /// @return true if a string of length > 0 could be read.
449         ///
450         bool readTypeOrKey(docstring & val, ifdocstream & ifs,
451                 docstring const & delimChars, docstring const & illegalChars,
452                 charCase chCase) {
453
454                 char_type ch;
455
456                 val.clear();
457
458                 if (!ifs)
459                         return false;
460
461                 // skip whitespace
462                 do {
463                         ifs.get(ch);
464                 } while (ifs && isSpace(ch));
465
466                 if (!ifs)
467                         return false;
468
469                 // read value
470                 while (ifs && !isSpace(ch) &&
471                        delimChars.find(ch) == docstring::npos &&
472                        illegalChars.find(ch) == docstring::npos)
473                 {
474                         if (chCase == makeLowerCase)
475                                 val += lowercase(ch);
476                         else
477                                 val += ch;
478                         ifs.get(ch);
479                 }
480
481                 if (illegalChars.find(ch) != docstring::npos) {
482                         ifs.putback(ch);
483                         return false;
484                 }
485
486                 // skip whitespace
487                 while (ifs && isSpace(ch)) {
488                         ifs.get(ch);
489                 }
490
491                 if (ifs) {
492                         ifs.putback(ch);
493                 }
494
495                 return val.length() > 0;
496         }
497
498         /// read subsequent bibtex values that are delimited with a #-character.
499         /// Concatenate all parts and replace names with the associated string in
500         /// the variable strings.
501         /// @return true if reading was successfull (all single parts were delimited
502         /// correctly)
503         bool readValue(docstring & val, ifdocstream & ifs, const VarMap & strings) {
504
505                 char_type ch;
506
507                 val.clear();
508
509                 if (!ifs)
510                         return false;
511
512                 do {
513                         // skip whitespace
514                         do {
515                                 ifs.get(ch);
516                         } while (ifs && isSpace(ch));
517
518                         if (!ifs)
519                                 return false;
520
521                         // check for field type
522                         if (isDigitASCII(ch)) {
523
524                                 // read integer value
525                                 do {
526                                         val += ch;
527                                         ifs.get(ch);
528                                 } while (ifs && isDigitASCII(ch));
529
530                                 if (!ifs)
531                                         return false;
532
533                         } else if (ch == '"' || ch == '{') {
534                                 // set end delimiter
535                                 char_type delim = ch == '"' ? '"': '}';
536
537                                 // Skip whitespace
538                                 do {
539                                         ifs.get(ch);
540                                 } while (ifs && isSpace(ch));
541
542                                 if (!ifs)
543                                         return false;
544
545                                 // We now have the first non-whitespace character
546                                 // We'll collapse adjacent whitespace.
547                                 bool lastWasWhiteSpace = false;
548
549                                 // inside this delimited text braces must match.
550                                 // Thus we can have a closing delimiter only
551                                 // when nestLevel == 0
552                                 int nestLevel = 0;
553
554                                 while (ifs && (nestLevel > 0 || ch != delim)) {
555                                         if (isSpace(ch)) {
556                                                 lastWasWhiteSpace = true;
557                                                 ifs.get(ch);
558                                                 continue;
559                                         }
560                                         // We output the space only after we stop getting
561                                         // whitespace so as not to output any whitespace
562                                         // at the end of the value.
563                                         if (lastWasWhiteSpace) {
564                                                 lastWasWhiteSpace = false;
565                                                 val += ' ';
566                                         }
567
568                                         val += ch;
569
570                                         // update nesting level
571                                         switch (ch) {
572                                                 case '{':
573                                                         ++nestLevel;
574                                                         break;
575                                                 case '}':
576                                                         --nestLevel;
577                                                         if (nestLevel < 0)
578                                                                 return false;
579                                                         break;
580                                         }
581
582                                         if (ifs)
583                                                 ifs.get(ch);
584                                 }
585
586                                 if (!ifs)
587                                         return false;
588
589                                 // FIXME Why is this here?
590                                 ifs.get(ch);
591
592                                 if (!ifs)
593                                         return false;
594
595                         } else {
596
597                                 // reading a string name
598                                 docstring strName;
599
600                                 while (ifs && !isSpace(ch) && ch != '#' && ch != ',' && ch != '}' && ch != ')') {
601                                         strName += lowercase(ch);
602                                         ifs.get(ch);
603                                 }
604
605                                 if (!ifs)
606                                         return false;
607
608                                 // replace the string with its assigned value or
609                                 // discard it if it's not assigned
610                                 if (strName.length()) {
611                                         VarMap::const_iterator pos = strings.find(strName);
612                                         if (pos != strings.end()) {
613                                                 val += pos->second;
614                                         }
615                                 }
616                         }
617
618                         // skip WS
619                         while (ifs && isSpace(ch)) {
620                                 ifs.get(ch);
621                         }
622
623                         if (!ifs)
624                                 return false;
625
626                         // continue reading next value on concatenate with '#'
627                 } while (ch == '#');
628
629                 ifs.putback(ch);
630
631                 return true;
632         }
633 } // namespace
634
635
636 void InsetBibtex::collectBibKeys(InsetIterator const & /*di*/, FileNameList & checkedFiles) const
637 {
638         parseBibTeXFiles(checkedFiles);
639 }
640
641
642 void InsetBibtex::parseBibTeXFiles(FileNameList & checkedFiles) const
643 {
644         // This bibtex parser is a first step to parse bibtex files
645         // more precisely.
646         //
647         // - it reads the whole bibtex entry and does a syntax check
648         //   (matching delimiters, missing commas,...
649         // - it recovers from errors starting with the next @-character
650         // - it reads @string definitions and replaces them in the
651         //   field values.
652         // - it accepts more characters in keys or value names than
653         //   bibtex does.
654         //
655         // Officially bibtex does only support ASCII, but in practice
656         // you can use any encoding as long as some elements like keys
657         // and names are pure ASCII. We support specifying an encoding,
658         // and we convert the file from that (default is buffer encoding).
659         // We don't restrict keys to ASCII in LyX, since our own
660         // InsetBibitem can generate non-ASCII keys, and nonstandard
661         // 8bit clean bibtex forks exist.
662
663         BiblioInfo keylist;
664
665         docstring_list const files = getBibFiles();
666         for (auto const & bf : files) {
667                 FileName const bibfile = buffer().getBibfilePath(bf);
668                 if (bibfile.empty()) {
669                         LYXERR0("Unable to find path for " << bf << "!");
670                         continue;
671                 }
672                 if (find(checkedFiles.begin(), checkedFiles.end(), bibfile) != checkedFiles.end())
673                         // already checked this one. Skip.
674                         continue;
675                 else
676                         // record that we check this.
677                         checkedFiles.push_back(bibfile);
678                 string encoding = buffer().masterParams().encoding().iconvName();
679                 string const ienc = to_ascii(params()["encoding"]);
680                 if (!ienc.empty() && ienc != "default" && encodings.fromLyXName(ienc))
681                         encoding = encodings.fromLyXName(ienc)->iconvName();
682                 ifdocstream ifs(bibfile.toFilesystemEncoding().c_str(),
683                         ios_base::in, encoding);
684
685                 char_type ch;
686                 VarMap strings;
687
688                 while (ifs) {
689                         ifs.get(ch);
690                         if (!ifs)
691                                 break;
692
693                         if (ch != '@')
694                                 continue;
695
696                         docstring entryType;
697
698                         if (!readTypeOrKey(entryType, ifs, from_ascii("{("), docstring(), makeLowerCase)) {
699                                 lyxerr << "BibTeX Parser: Error reading entry type." << std::endl;
700                                 continue;
701                         }
702
703                         if (!ifs) {
704                                 lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
705                                 continue;
706                         }
707
708                         if (entryType == from_ascii("comment")) {
709                                 ifs.ignore(numeric_limits<int>::max(), '\n');
710                                 continue;
711                         }
712
713                         ifs.get(ch);
714                         if (!ifs) {
715                                 lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
716                                 break;
717                         }
718
719                         if ((ch != '(') && (ch != '{')) {
720                                 lyxerr << "BibTeX Parser: Invalid entry delimiter." << std::endl;
721                                 ifs.putback(ch);
722                                 continue;
723                         }
724
725                         // process the entry
726                         if (entryType == from_ascii("string")) {
727
728                                 // read string and add it to the strings map
729                                 // (or replace it's old value)
730                                 docstring name;
731                                 docstring value;
732
733                                 if (!readTypeOrKey(name, ifs, from_ascii("="), from_ascii("#{}(),"), makeLowerCase)) {
734                                         lyxerr << "BibTeX Parser: Error reading string name." << std::endl;
735                                         continue;
736                                 }
737
738                                 if (!ifs) {
739                                         lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
740                                         continue;
741                                 }
742
743                                 // next char must be an equal sign
744                                 ifs.get(ch);
745                                 if (!ifs || ch != '=') {
746                                         lyxerr << "BibTeX Parser: No `=' after string name: " <<
747                                                         name << "." << std::endl;
748                                         continue;
749                                 }
750
751                                 if (!readValue(value, ifs, strings)) {
752                                         lyxerr << "BibTeX Parser: Unable to read value for string: " <<
753                                                         name << "." << std::endl;
754                                         continue;
755                                 }
756
757                                 strings[name] = value;
758
759                         } else if (entryType == from_ascii("preamble")) {
760
761                                 // preamble definitions are discarded.
762                                 // can they be of any use in lyx?
763                                 docstring value;
764
765                                 if (!readValue(value, ifs, strings)) {
766                                         lyxerr << "BibTeX Parser: Unable to read preamble value." << std::endl;
767                                         continue;
768                                 }
769
770                         } else {
771
772                                 // Citation entry. Try to read the key.
773                                 docstring key;
774
775                                 if (!readTypeOrKey(key, ifs, from_ascii(","), from_ascii("}"), keepCase)) {
776                                         lyxerr << "BibTeX Parser: Unable to read key for entry type:" <<
777                                                         entryType << "." << std::endl;
778                                         continue;
779                                 }
780
781                                 if (!ifs) {
782                                         lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
783                                         continue;
784                                 }
785
786                                 /////////////////////////////////////////////
787                                 // now we have a key, so we will add an entry
788                                 // (even if it's empty, as bibtex does)
789                                 //
790                                 // we now read the field = value pairs.
791                                 // all items must be separated by a comma. If
792                                 // it is missing the scanning of this entry is
793                                 // stopped and the next is searched.
794                                 docstring name;
795                                 docstring value;
796                                 docstring data;
797                                 BibTeXInfo keyvalmap(key, entryType);
798
799                                 bool readNext = removeWSAndComma(ifs);
800
801                                 while (ifs && readNext) {
802
803                                         // read field name
804                                         if (!readTypeOrKey(name, ifs, from_ascii("="),
805                                                            from_ascii("{}(),"), makeLowerCase) || !ifs)
806                                                 break;
807
808                                         // next char must be an equal sign
809                                         // FIXME Whitespace??
810                                         ifs.get(ch);
811                                         if (!ifs) {
812                                                 lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
813                                                 break;
814                                         }
815                                         if (ch != '=') {
816                                                 lyxerr << "BibTeX Parser: Missing `=' after field name: " <<
817                                                                 name << ", for key: " << key << "." << std::endl;
818                                                 ifs.putback(ch);
819                                                 break;
820                                         }
821
822                                         // read field value
823                                         if (!readValue(value, ifs, strings)) {
824                                                 lyxerr << "BibTeX Parser: Unable to read value for field: " <<
825                                                                 name << ", for key: " << key << "." << std::endl;
826                                                 break;
827                                         }
828
829                                         keyvalmap[name] = value;
830                                         data += "\n\n" + value;
831                                         keylist.addFieldName(name);
832                                         readNext = removeWSAndComma(ifs);
833                                 }
834
835                                 // add the new entry
836                                 keylist.addEntryType(entryType);
837                                 keyvalmap.setAllData(data);
838                                 keylist[key] = keyvalmap;
839                         } //< else (citation entry)
840                 } //< searching '@'
841         } //< for loop over files
842
843         buffer().addBiblioInfo(keylist);
844 }
845
846
847 bool InsetBibtex::addDatabase(docstring const & db)
848 {
849         docstring bibfiles = getParam("bibfiles");
850         if (tokenPos(bibfiles, ',', db) != -1)
851                 return false;
852         if (!bibfiles.empty())
853                 bibfiles += ',';
854         setParam("bibfiles", bibfiles + db);
855         return true;
856 }
857
858
859 bool InsetBibtex::delDatabase(docstring const & db)
860 {
861         docstring bibfiles = getParam("bibfiles");
862         if (contains(bibfiles, db)) {
863                 int const n = tokenPos(bibfiles, ',', db);
864                 docstring bd = db;
865                 if (n > 0) {
866                         // this is not the first database
867                         docstring tmp = ',' + bd;
868                         setParam("bibfiles", subst(bibfiles, tmp, docstring()));
869                 } else if (n == 0)
870                         // this is the first (or only) database
871                         setParam("bibfiles", split(bibfiles, bd, ','));
872                 else
873                         return false;
874         }
875         return true;
876 }
877
878
879 void InsetBibtex::validate(LaTeXFeatures & features) const
880 {
881         BufferParams const & mparams = features.buffer().masterParams();
882         if (mparams.useBibtopic())
883                 features.require("bibtopic");
884         else if (!mparams.useBiblatex() && mparams.multibib == "child")
885                 features.require("chapterbib");
886         // FIXME XHTML
887         // It'd be better to be able to get this from an InsetLayout, but at present
888         // InsetLayouts do not seem really to work for things that aren't InsetTexts.
889         if (features.runparams().flavor == OutputParams::HTML)
890                 features.addCSSSnippet("div.bibtexentry { margin-left: 2em; text-indent: -2em; }\n"
891                         "span.bibtexlabel:before{ content: \"[\"; }\n"
892                         "span.bibtexlabel:after{ content: \"] \"; }");
893 }
894
895
896 void InsetBibtex::updateBuffer(ParIterator const &, UpdateType)
897 {
898         buffer().registerBibfiles(getBibFiles());
899         // record encoding of bib files for biblatex
900         string const enc = (params()["encoding"] == from_ascii("default")) ?
901                                 string() : to_ascii(params()["encoding"]);
902         bool invalidate = false;
903         if (buffer().params().bibEncoding() != enc) {
904                 buffer().params().setBibEncoding(enc);
905                 invalidate = true;
906         }
907         map<string, string> encs = getFileEncodings();
908         map<string, string>::const_iterator it = encs.begin();
909         for (; it != encs.end(); ++it) {
910                 if (buffer().params().bibFileEncoding(it->first) != it->second) {
911                         buffer().params().setBibFileEncoding(it->first, it->second);
912                         invalidate = true;
913                 }
914         }
915         if (invalidate)
916                 buffer().invalidateBibinfoCache();
917 }
918
919
920 map<string, string> InsetBibtex::getFileEncodings() const
921 {
922         vector<string> ps =
923                 getVectorFromString(to_utf8(getParam("file_encodings")), "\t");
924         std::map<string, string> res;
925         for (string const & s: ps) {
926                 string key;
927                 string val = split(s, key, ' ');
928                 res[key] = val;
929         }
930         return res;
931 }
932
933
934 docstring InsetBibtex::getRefLabel() const
935 {
936         if (buffer().masterParams().documentClass().hasLaTeXLayout("chapter"))
937                 return buffer().B_("Bibliography");
938         return buffer().B_("References");
939 }
940
941
942 void InsetBibtex::addToToc(DocIterator const & cpit, bool output_active,
943                            UpdateType, TocBackend & backend) const
944 {
945         if (!prefixIs(to_utf8(getParam("options")), "bibtotoc"))
946                 return;
947
948         docstring const str = getRefLabel();
949         TocBuilder & b = backend.builder("tableofcontents");
950         b.pushItem(cpit, str, output_active);
951         b.pop();
952 }
953
954
955 int InsetBibtex::plaintext(odocstringstream & os,
956        OutputParams const & op, size_t max_length) const
957 {
958         docstring const reflabel = getRefLabel();
959
960         // We could output more information here, e.g., what databases are included
961         // and information about options. But I don't necessarily see any reason to
962         // do this right now.
963         if (op.for_tooltip || op.for_toc || op.for_search) {
964                 os << '[' << reflabel << ']' << '\n';
965                 return PLAINTEXT_NEWLINE;
966         }
967
968         BiblioInfo bibinfo = buffer().masterBibInfo();
969         bibinfo.makeCitationLabels(buffer());
970         vector<docstring> const & cites = bibinfo.citedEntries();
971
972         size_t start_size = os.str().size();
973         docstring refoutput;
974         refoutput += reflabel + "\n\n";
975
976         // Tell BiblioInfo our purpose
977         CiteItem ci;
978         ci.context = CiteItem::Export;
979
980         // Now we loop over the entries
981         vector<docstring>::const_iterator vit = cites.begin();
982         vector<docstring>::const_iterator const ven = cites.end();
983         for (; vit != ven; ++vit) {
984                 if (start_size + refoutput.size() >= max_length)
985                         break;
986                 BiblioInfo::const_iterator const biit = bibinfo.find(*vit);
987                 if (biit == bibinfo.end())
988                         continue;
989                 BibTeXInfo const & entry = biit->second;
990                 refoutput += "[" + entry.label() + "] ";
991                 // FIXME Right now, we are calling BibInfo::getInfo on the key,
992                 // which will give us all the cross-referenced info. But for every
993                 // entry, so there's a lot of repitition. This should be fixed.
994                 refoutput += bibinfo.getInfo(entry.key(), buffer(), ci) + "\n\n";
995         }
996         os << refoutput;
997         return refoutput.size();
998 }
999
1000
1001 // FIXME
1002 // docstring InsetBibtex::entriesAsXHTML(vector<docstring> const & entries)
1003 // And then here just: entriesAsXHTML(buffer().masterBibInfo().citedEntries())
1004 docstring InsetBibtex::xhtml(XHTMLStream & xs, OutputParams const &) const
1005 {
1006         BiblioInfo const & bibinfo = buffer().masterBibInfo();
1007         bool const all_entries = getParam("btprint") == "btPrintAll";
1008         vector<docstring> const & cites =
1009             all_entries ? bibinfo.getKeys() : bibinfo.citedEntries();
1010
1011         docstring const reflabel = buffer().B_("References");
1012
1013         // tell BiblioInfo our purpose
1014         CiteItem ci;
1015         ci.context = CiteItem::Export;
1016         ci.richtext = true;
1017         ci.max_key_size = UINT_MAX;
1018
1019         xs << html::StartTag("h2", "class='bibtex'")
1020                 << reflabel
1021                 << html::EndTag("h2")
1022                 << html::StartTag("div", "class='bibtex'");
1023
1024         // Now we loop over the entries
1025         vector<docstring>::const_iterator vit = cites.begin();
1026         vector<docstring>::const_iterator const ven = cites.end();
1027         for (; vit != ven; ++vit) {
1028                 BiblioInfo::const_iterator const biit = bibinfo.find(*vit);
1029                 if (biit == bibinfo.end())
1030                         continue;
1031
1032                 BibTeXInfo const & entry = biit->second;
1033                 string const attr = "class='bibtexentry' id='LyXCite-"
1034                     + to_utf8(html::cleanAttr(entry.key())) + "'";
1035                 xs << html::StartTag("div", attr);
1036
1037                 // don't print labels if we're outputting all entries
1038                 if (!all_entries) {
1039                         xs << html::StartTag("span", "class='bibtexlabel'")
1040                                 << entry.label()
1041                                 << html::EndTag("span");
1042                 }
1043
1044                 // FIXME Right now, we are calling BibInfo::getInfo on the key,
1045                 // which will give us all the cross-referenced info. But for every
1046                 // entry, so there's a lot of repitition. This should be fixed.
1047                 xs << html::StartTag("span", "class='bibtexinfo'")
1048                    << XHTMLStream::ESCAPE_AND
1049                    << bibinfo.getInfo(entry.key(), buffer(), ci)
1050                    << html::EndTag("span")
1051                    << html::EndTag("div")
1052                    << html::CR();
1053         }
1054         xs << html::EndTag("div");
1055         return docstring();
1056 }
1057
1058
1059 void InsetBibtex::write(ostream & os) const
1060 {
1061         params().Write(os, &buffer());
1062 }
1063
1064
1065 string InsetBibtex::contextMenuName() const
1066 {
1067         return "context-bibtex";
1068 }
1069
1070
1071 } // namespace lyx