]> git.lyx.org Git - features.git/blob - src/insets/InsetBibtex.cpp
Consider individual bib file encoding when parsing
[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         size_t 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 ienc = buffer().masterParams().bibFileEncoding(to_utf8(bf));
680                 if (ienc.empty() || ienc == "general")
681                         ienc = to_ascii(params()["encoding"]);
682
683                 if (!ienc.empty() && ienc != "default" && ienc != "auto" && encodings.fromLyXName(ienc))
684                         encoding = encodings.fromLyXName(ienc)->iconvName();
685                 ifdocstream ifs(bibfile.toFilesystemEncoding().c_str(),
686                         ios_base::in, encoding);
687
688                 char_type ch;
689                 VarMap strings;
690
691                 while (ifs) {
692                         ifs.get(ch);
693                         if (!ifs)
694                                 break;
695
696                         if (ch != '@')
697                                 continue;
698
699                         docstring entryType;
700
701                         if (!readTypeOrKey(entryType, ifs, from_ascii("{("), docstring(), makeLowerCase)) {
702                                 lyxerr << "BibTeX Parser: Error reading entry type." << std::endl;
703                                 continue;
704                         }
705
706                         if (!ifs) {
707                                 lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
708                                 continue;
709                         }
710
711                         if (entryType == from_ascii("comment")) {
712                                 ifs.ignore(numeric_limits<int>::max(), '\n');
713                                 continue;
714                         }
715
716                         ifs.get(ch);
717                         if (!ifs) {
718                                 lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
719                                 break;
720                         }
721
722                         if ((ch != '(') && (ch != '{')) {
723                                 lyxerr << "BibTeX Parser: Invalid entry delimiter." << std::endl;
724                                 ifs.putback(ch);
725                                 continue;
726                         }
727
728                         // process the entry
729                         if (entryType == from_ascii("string")) {
730
731                                 // read string and add it to the strings map
732                                 // (or replace it's old value)
733                                 docstring name;
734                                 docstring value;
735
736                                 if (!readTypeOrKey(name, ifs, from_ascii("="), from_ascii("#{}(),"), makeLowerCase)) {
737                                         lyxerr << "BibTeX Parser: Error reading string name." << std::endl;
738                                         continue;
739                                 }
740
741                                 if (!ifs) {
742                                         lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
743                                         continue;
744                                 }
745
746                                 // next char must be an equal sign
747                                 ifs.get(ch);
748                                 if (!ifs || ch != '=') {
749                                         lyxerr << "BibTeX Parser: No `=' after string name: " <<
750                                                         name << "." << std::endl;
751                                         continue;
752                                 }
753
754                                 if (!readValue(value, ifs, strings)) {
755                                         lyxerr << "BibTeX Parser: Unable to read value for string: " <<
756                                                         name << "." << std::endl;
757                                         continue;
758                                 }
759
760                                 strings[name] = value;
761
762                         } else if (entryType == from_ascii("preamble")) {
763
764                                 // preamble definitions are discarded.
765                                 // can they be of any use in lyx?
766                                 docstring value;
767
768                                 if (!readValue(value, ifs, strings)) {
769                                         lyxerr << "BibTeX Parser: Unable to read preamble value." << std::endl;
770                                         continue;
771                                 }
772
773                         } else {
774
775                                 // Citation entry. Try to read the key.
776                                 docstring key;
777
778                                 if (!readTypeOrKey(key, ifs, from_ascii(","), from_ascii("}"), keepCase)) {
779                                         lyxerr << "BibTeX Parser: Unable to read key for entry type:" <<
780                                                         entryType << "." << std::endl;
781                                         continue;
782                                 }
783
784                                 if (!ifs) {
785                                         lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
786                                         continue;
787                                 }
788
789                                 /////////////////////////////////////////////
790                                 // now we have a key, so we will add an entry
791                                 // (even if it's empty, as bibtex does)
792                                 //
793                                 // we now read the field = value pairs.
794                                 // all items must be separated by a comma. If
795                                 // it is missing the scanning of this entry is
796                                 // stopped and the next is searched.
797                                 docstring name;
798                                 docstring value;
799                                 docstring data;
800                                 BibTeXInfo keyvalmap(key, entryType);
801
802                                 bool readNext = removeWSAndComma(ifs);
803
804                                 while (ifs && readNext) {
805
806                                         // read field name
807                                         if (!readTypeOrKey(name, ifs, from_ascii("="),
808                                                            from_ascii("{}(),"), makeLowerCase) || !ifs)
809                                                 break;
810
811                                         // next char must be an equal sign
812                                         // FIXME Whitespace??
813                                         ifs.get(ch);
814                                         if (!ifs) {
815                                                 lyxerr << "BibTeX Parser: Unexpected end of file." << std::endl;
816                                                 break;
817                                         }
818                                         if (ch != '=') {
819                                                 lyxerr << "BibTeX Parser: Missing `=' after field name: " <<
820                                                                 name << ", for key: " << key << "." << std::endl;
821                                                 ifs.putback(ch);
822                                                 break;
823                                         }
824
825                                         // read field value
826                                         if (!readValue(value, ifs, strings)) {
827                                                 lyxerr << "BibTeX Parser: Unable to read value for field: " <<
828                                                                 name << ", for key: " << key << "." << std::endl;
829                                                 break;
830                                         }
831
832                                         keyvalmap[name] = value;
833                                         data += "\n\n" + value;
834                                         keylist.addFieldName(name);
835                                         readNext = removeWSAndComma(ifs);
836                                 }
837
838                                 // add the new entry
839                                 keylist.addEntryType(entryType);
840                                 keyvalmap.setAllData(data);
841                                 keylist[key] = keyvalmap;
842                         } //< else (citation entry)
843                 } //< searching '@'
844         } //< for loop over files
845
846         buffer().addBiblioInfo(keylist);
847 }
848
849
850 bool InsetBibtex::addDatabase(docstring const & db)
851 {
852         docstring bibfiles = getParam("bibfiles");
853         if (tokenPos(bibfiles, ',', db) != -1)
854                 return false;
855         if (!bibfiles.empty())
856                 bibfiles += ',';
857         setParam("bibfiles", bibfiles + db);
858         return true;
859 }
860
861
862 bool InsetBibtex::delDatabase(docstring const & db)
863 {
864         docstring bibfiles = getParam("bibfiles");
865         if (contains(bibfiles, db)) {
866                 int const n = tokenPos(bibfiles, ',', db);
867                 docstring bd = db;
868                 if (n > 0) {
869                         // this is not the first database
870                         docstring tmp = ',' + bd;
871                         setParam("bibfiles", subst(bibfiles, tmp, docstring()));
872                 } else if (n == 0)
873                         // this is the first (or only) database
874                         setParam("bibfiles", split(bibfiles, bd, ','));
875                 else
876                         return false;
877         }
878         return true;
879 }
880
881
882 void InsetBibtex::validate(LaTeXFeatures & features) const
883 {
884         BufferParams const & mparams = features.buffer().masterParams();
885         if (mparams.useBibtopic())
886                 features.require("bibtopic");
887         else if (!mparams.useBiblatex() && mparams.multibib == "child")
888                 features.require("chapterbib");
889         // FIXME XHTML
890         // It'd be better to be able to get this from an InsetLayout, but at present
891         // InsetLayouts do not seem really to work for things that aren't InsetTexts.
892         if (features.runparams().flavor == OutputParams::HTML)
893                 features.addCSSSnippet("div.bibtexentry { margin-left: 2em; text-indent: -2em; }\n"
894                         "span.bibtexlabel:before{ content: \"[\"; }\n"
895                         "span.bibtexlabel:after{ content: \"] \"; }");
896 }
897
898
899 void InsetBibtex::updateBuffer(ParIterator const &, UpdateType)
900 {
901         buffer().registerBibfiles(getBibFiles());
902         // record encoding of bib files for biblatex
903         string const enc = (params()["encoding"] == from_ascii("default")) ?
904                                 string() : to_ascii(params()["encoding"]);
905         bool invalidate = false;
906         if (buffer().params().bibEncoding() != enc) {
907                 buffer().params().setBibEncoding(enc);
908                 invalidate = true;
909         }
910         map<string, string> encs = getFileEncodings();
911         map<string, string>::const_iterator it = encs.begin();
912         for (; it != encs.end(); ++it) {
913                 if (buffer().params().bibFileEncoding(it->first) != it->second) {
914                         buffer().params().setBibFileEncoding(it->first, it->second);
915                         invalidate = true;
916                 }
917         }
918         if (invalidate)
919                 buffer().invalidateBibinfoCache();
920 }
921
922
923 map<string, string> InsetBibtex::getFileEncodings() const
924 {
925         vector<string> ps =
926                 getVectorFromString(to_utf8(getParam("file_encodings")), "\t");
927         std::map<string, string> res;
928         for (string const & s: ps) {
929                 string key;
930                 string val = split(s, key, ' ');
931                 res[key] = val;
932         }
933         return res;
934 }
935
936
937 docstring InsetBibtex::getRefLabel() const
938 {
939         if (buffer().masterParams().documentClass().hasLaTeXLayout("chapter"))
940                 return buffer().B_("Bibliography");
941         return buffer().B_("References");
942 }
943
944
945 void InsetBibtex::addToToc(DocIterator const & cpit, bool output_active,
946                            UpdateType, TocBackend & backend) const
947 {
948         if (!prefixIs(to_utf8(getParam("options")), "bibtotoc"))
949                 return;
950
951         docstring const str = getRefLabel();
952         TocBuilder & b = backend.builder("tableofcontents");
953         b.pushItem(cpit, str, output_active);
954         b.pop();
955 }
956
957
958 int InsetBibtex::plaintext(odocstringstream & os,
959        OutputParams const & op, size_t max_length) const
960 {
961         docstring const reflabel = getRefLabel();
962
963         // We could output more information here, e.g., what databases are included
964         // and information about options. But I don't necessarily see any reason to
965         // do this right now.
966         if (op.for_tooltip || op.for_toc || op.for_search) {
967                 os << '[' << reflabel << ']' << '\n';
968                 return PLAINTEXT_NEWLINE;
969         }
970
971         BiblioInfo bibinfo = buffer().masterBibInfo();
972         bibinfo.makeCitationLabels(buffer());
973         vector<docstring> const & cites = bibinfo.citedEntries();
974
975         size_t start_size = os.str().size();
976         docstring refoutput;
977         refoutput += reflabel + "\n\n";
978
979         // Tell BiblioInfo our purpose
980         CiteItem ci;
981         ci.context = CiteItem::Export;
982
983         // Now we loop over the entries
984         vector<docstring>::const_iterator vit = cites.begin();
985         vector<docstring>::const_iterator const ven = cites.end();
986         for (; vit != ven; ++vit) {
987                 if (start_size + refoutput.size() >= max_length)
988                         break;
989                 BiblioInfo::const_iterator const biit = bibinfo.find(*vit);
990                 if (biit == bibinfo.end())
991                         continue;
992                 BibTeXInfo const & entry = biit->second;
993                 refoutput += "[" + entry.label() + "] ";
994                 // FIXME Right now, we are calling BibInfo::getInfo on the key,
995                 // which will give us all the cross-referenced info. But for every
996                 // entry, so there's a lot of repitition. This should be fixed.
997                 refoutput += bibinfo.getInfo(entry.key(), buffer(), ci) + "\n\n";
998         }
999         os << refoutput;
1000         return int(refoutput.size());
1001 }
1002
1003
1004 // FIXME
1005 // docstring InsetBibtex::entriesAsXHTML(vector<docstring> const & entries)
1006 // And then here just: entriesAsXHTML(buffer().masterBibInfo().citedEntries())
1007 docstring InsetBibtex::xhtml(XHTMLStream & xs, OutputParams const &) const
1008 {
1009         BiblioInfo const & bibinfo = buffer().masterBibInfo();
1010         bool const all_entries = getParam("btprint") == "btPrintAll";
1011         vector<docstring> const & cites =
1012             all_entries ? bibinfo.getKeys() : bibinfo.citedEntries();
1013
1014         docstring const reflabel = buffer().B_("References");
1015
1016         // tell BiblioInfo our purpose
1017         CiteItem ci;
1018         ci.context = CiteItem::Export;
1019         ci.richtext = true;
1020         ci.max_key_size = UINT_MAX;
1021
1022         xs << html::StartTag("h2", "class='bibtex'")
1023                 << reflabel
1024                 << html::EndTag("h2")
1025                 << html::StartTag("div", "class='bibtex'");
1026
1027         // Now we loop over the entries
1028         vector<docstring>::const_iterator vit = cites.begin();
1029         vector<docstring>::const_iterator const ven = cites.end();
1030         for (; vit != ven; ++vit) {
1031                 BiblioInfo::const_iterator const biit = bibinfo.find(*vit);
1032                 if (biit == bibinfo.end())
1033                         continue;
1034
1035                 BibTeXInfo const & entry = biit->second;
1036                 string const attr = "class='bibtexentry' id='LyXCite-"
1037                     + to_utf8(html::cleanAttr(entry.key())) + "'";
1038                 xs << html::StartTag("div", attr);
1039
1040                 // don't print labels if we're outputting all entries
1041                 if (!all_entries) {
1042                         xs << html::StartTag("span", "class='bibtexlabel'")
1043                                 << entry.label()
1044                                 << html::EndTag("span");
1045                 }
1046
1047                 // FIXME Right now, we are calling BibInfo::getInfo on the key,
1048                 // which will give us all the cross-referenced info. But for every
1049                 // entry, so there's a lot of repitition. This should be fixed.
1050                 xs << html::StartTag("span", "class='bibtexinfo'")
1051                    << XHTMLStream::ESCAPE_AND
1052                    << bibinfo.getInfo(entry.key(), buffer(), ci)
1053                    << html::EndTag("span")
1054                    << html::EndTag("div")
1055                    << html::CR();
1056         }
1057         xs << html::EndTag("div");
1058         return docstring();
1059 }
1060
1061
1062 void InsetBibtex::write(ostream & os) const
1063 {
1064         params().Write(os, &buffer());
1065 }
1066
1067
1068 string InsetBibtex::contextMenuName() const
1069 {
1070         return "context-bibtex";
1071 }
1072
1073
1074 } // namespace lyx