]> git.lyx.org Git - lyx.git/blob - src/insets/InsetBibtex.cpp
6270b1345a035dce3e49ebd4d10845ee106d9623
[lyx.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  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetBibtex.h"
15
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "DispatchResult.h"
19 #include "Encoding.h"
20 #include "Format.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "LaTeXFeatures.h"
24 #include "MetricsInfo.h"
25 #include "OutputParams.h"
26 #include "TextClass.h"
27
28 #include "frontends/alert.h"
29
30 #include "support/convert.h"
31 #include "support/debug.h"
32 #include "support/docstream.h"
33 #include "support/ExceptionMessage.h"
34 #include "support/filetools.h"
35 #include "support/gettext.h"
36 #include "support/lstrings.h"
37 #include "support/os.h"
38 #include "support/Path.h"
39 #include "support/textutils.h"
40
41 #include <boost/regex.hpp>
42
43 #include <limits>
44
45 using namespace std;
46 using namespace lyx::support;
47
48 namespace lyx {
49
50 namespace Alert = frontend::Alert;
51 namespace os = support::os;
52
53
54 InsetBibtex::InsetBibtex(Buffer const & buf, InsetCommandParams const & p)
55         : InsetCommand(p, "bibtex")
56 {
57         Inset::setBuffer(const_cast<Buffer &>(buf));
58         buffer_->invalidateBibinfoCache();
59 }
60
61
62 InsetBibtex::~InsetBibtex()
63 {
64         if (isBufferValid())
65                 buffer_->invalidateBibinfoCache();
66 }
67
68
69 ParamInfo const & InsetBibtex::findInfo(string const & /* cmdName */)
70 {
71         static ParamInfo param_info_;
72         if (param_info_.empty()) {
73                 param_info_.add("btprint", ParamInfo::LATEX_OPTIONAL);
74                 param_info_.add("bibfiles", ParamInfo::LATEX_REQUIRED);
75                 param_info_.add("options", ParamInfo::LYX_INTERNAL);
76         }
77         return param_info_;
78 }
79
80
81 void InsetBibtex::doDispatch(Cursor & cur, FuncRequest & cmd)
82 {
83         switch (cmd.action) {
84
85         case LFUN_INSET_EDIT:
86                 editDatabases();
87                 break;
88
89         case LFUN_INSET_MODIFY: {
90                 InsetCommandParams p(BIBTEX_CODE);
91                 try {
92                         if (!InsetCommand::string2params("bibtex", 
93                                         to_utf8(cmd.argument()), p)) {
94                                 cur.noUpdate();
95                                 break;
96                         }
97                 } catch (ExceptionMessage const & message) {
98                         if (message.type_ == WarningException) {
99                                 Alert::warning(message.title_, message.details_);
100                                 cur.noUpdate();
101                         } else 
102                                 throw message;
103                         break;
104                 }
105                 //
106                 setParams(p);
107                 buffer().updateBibfilesCache();
108                 break;
109         }
110
111         default:
112                 InsetCommand::doDispatch(cur, cmd);
113                 break;
114         }
115 }
116
117
118 bool InsetBibtex::getStatus(Cursor & cur, FuncRequest const & cmd,
119                 FuncStatus & flag) const
120 {
121         switch (cmd.action) {
122         case LFUN_INSET_EDIT:
123                 flag.setEnabled(true);
124                 return true;
125
126         default:
127                 return InsetCommand::getStatus(cur, cmd, flag);
128         }
129 }
130
131
132 void InsetBibtex::editDatabases() const
133 {
134         vector<docstring> bibfilelist = getVectorFromString(getParam("bibfiles"));
135
136         if (bibfilelist.empty())
137                 return;
138
139         int nr_databases = bibfilelist.size();
140         if (nr_databases > 1) {
141                         docstring message = bformat(_("The BibTeX inset includes %1$s databases.\n"
142                                                        "If you proceed, all of them will be opened."),
143                                                         convert<docstring>(nr_databases));
144                         int const ret = Alert::prompt(_("Open Databases?"),
145                                 message, 0, 1, _("&Cancel"), _("&Proceed"));
146
147                         if (ret == 0)
148                                 return;
149         }
150
151         vector<docstring>::const_iterator it = bibfilelist.begin();
152         vector<docstring>::const_iterator en = bibfilelist.end();
153         for (; it != en; ++it) {
154                 FileName bibfile = getBibTeXPath(*it, buffer());
155                 formats.edit(buffer(), bibfile,
156                      formats.getFormatFromFile(bibfile));
157         }
158 }
159
160
161 docstring InsetBibtex::screenLabel() const
162 {
163         return _("BibTeX Generated Bibliography");
164 }
165
166
167 docstring InsetBibtex::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
168 {
169         docstring item = from_ascii("* ");
170         docstring tip = _("Databases:") + "\n";
171         vector<docstring> bibfilelist = getVectorFromString(getParam("bibfiles"));
172
173         if (bibfilelist.empty()) {
174                 tip += item;
175                 tip += _("none");
176         } else {
177                 vector<docstring>::const_iterator it = bibfilelist.begin();
178                 vector<docstring>::const_iterator en = bibfilelist.end();
179                 for (; it != en; ++it) {
180                         tip += item;
181                         tip += *it + "\n";
182                 }
183         }
184
185         // Style-Options
186         bool toc = false;
187         docstring style = getParam("options"); // maybe empty! and with bibtotoc
188         docstring bibtotoc = from_ascii("bibtotoc");
189         if (prefixIs(style, bibtotoc)) {
190                 toc = true;
191                 if (contains(style, char_type(',')))
192                         style = split(style, bibtotoc, char_type(','));
193         }
194
195         tip += _("Style File:") +"\n";
196         tip += item;
197         if (!style.empty())
198                 tip += style;
199         else
200                 tip += _("none");
201
202         tip += "\n" + _("Lists:") + " ";
203         docstring btprint = getParam("btprint");
204                 if (btprint == "btPrintAll")
205                         tip += _("all references");
206                 else if (btprint == "btPrintNotCited")
207                         tip += _("all uncited references");
208                 else
209                         tip += _("all cited references");
210         
211         if (toc) {
212                 tip += ", ";
213                 tip += _("included in TOC");
214         }
215
216         return tip;
217 }
218
219
220 static string normalizeName(Buffer const & buffer,
221         OutputParams const & runparams, string const & name, string const & ext)
222 {
223         string const fname = makeAbsPath(name, buffer.filePath()).absFilename();
224         if (FileName(name).isAbsolute() || !FileName(fname + ext).isReadableFile())
225                 return name;
226         if (!runparams.nice)
227                 return fname;
228
229         // FIXME UNICODE
230         return to_utf8(makeRelPath(from_utf8(fname),
231                                          from_utf8(buffer.masterBuffer()->filePath())));
232 }
233
234
235 int InsetBibtex::latex(odocstream & os, OutputParams const & runparams) const
236 {
237         // the sequence of the commands:
238         // 1. \bibliographystyle{style}
239         // 2. \addcontentsline{...} - if option bibtotoc set
240         // 3. \bibliography{database}
241         // and with bibtopic:
242         // 1. \bibliographystyle{style}
243         // 2. \begin{btSect}{database}
244         // 3. \btPrint{Cited|NotCited|All}
245         // 4. \end{btSect}
246
247         // Database(s)
248         // If we are processing the LaTeX file in a temp directory then
249         // copy the .bib databases to this temp directory, mangling their
250         // names in the process. Store this mangled name in the list of
251         // all databases.
252         // (We need to do all this because BibTeX *really*, *really*
253         // can't handle "files with spaces" and Windows users tend to
254         // use such filenames.)
255         // Otherwise, store the (maybe absolute) path to the original,
256         // unmangled database name.
257         vector<docstring> bibfilelist = getVectorFromString(getParam("bibfiles"));
258         vector<docstring>::const_iterator it = bibfilelist.begin();
259         vector<docstring>::const_iterator en = bibfilelist.end();
260         odocstringstream dbs;
261         bool didone = false;
262
263         for (; it != en; ++it) {
264                 string utf8input = to_utf8(*it);
265                 string database =
266                         normalizeName(buffer(), runparams, utf8input, ".bib");
267                 FileName const try_in_file =
268                         makeAbsPath(database + ".bib", buffer().filePath());
269                 bool const not_from_texmf = try_in_file.isReadableFile();
270
271                 if (!runparams.inComment && !runparams.dryrun && !runparams.nice &&
272                     not_from_texmf) {
273                         // mangledFilename() needs the extension
274                         DocFileName const in_file = DocFileName(try_in_file);
275                         database = removeExtension(in_file.mangledFilename());
276                         FileName const out_file = makeAbsPath(database + ".bib",
277                                         buffer().masterBuffer()->temppath());
278
279                         bool const success = in_file.copyTo(out_file);
280                         if (!success) {
281                                 lyxerr << "Failed to copy '" << in_file
282                                        << "' to '" << out_file << "'"
283                                        << endl;
284                         }
285                 } else if (!runparams.inComment && runparams.nice && not_from_texmf &&
286                            !isValidLaTeXFilename(database)) {
287                                 frontend::Alert::warning(_("Invalid filename"),
288                                                          _("The following filename is likely to cause trouble "
289                                                            "when running the exported file through LaTeX: ") +
290                                                             from_utf8(database));
291                 }
292
293                 if (didone)
294                         dbs << ',';
295                 else 
296                         didone = true;
297                 // FIXME UNICODE
298                 dbs << from_utf8(latex_path(database));
299         }
300         docstring const db_out = dbs.str();
301
302         // Post this warning only once.
303         static bool warned_about_spaces = false;
304         if (!warned_about_spaces &&
305             runparams.nice && db_out.find(' ') != docstring::npos) {
306                 warned_about_spaces = true;
307                 Alert::warning(_("Export Warning!"),
308                                _("There are spaces in the paths to your BibTeX databases.\n"
309                                               "BibTeX will be unable to find them."));
310         }
311         // Style-Options
312         string style = to_utf8(getParam("options")); // maybe empty! and with bibtotoc
313         string bibtotoc;
314         if (prefixIs(style, "bibtotoc")) {
315                 bibtotoc = "bibtotoc";
316                 if (contains(style, ','))
317                         style = split(style, bibtotoc, ',');
318         }
319
320         // line count
321         int nlines = 0;
322
323         if (!style.empty()) {
324                 string base = normalizeName(buffer(), runparams, style, ".bst");
325                 FileName const try_in_file = 
326                         makeAbsPath(base + ".bst", buffer().filePath());
327                 bool const not_from_texmf = try_in_file.isReadableFile();
328                 // If this style does not come from texmf and we are not
329                 // exporting to .tex copy it to the tmp directory.
330                 // This prevents problems with spaces and 8bit charcaters
331                 // in the file name.
332                 if (!runparams.inComment && !runparams.dryrun && !runparams.nice &&
333                     not_from_texmf) {
334                         // use new style name
335                         DocFileName const in_file = DocFileName(try_in_file);
336                         base = removeExtension(in_file.mangledFilename());
337                         FileName const out_file = makeAbsPath(base + ".bst",
338                                         buffer().masterBuffer()->temppath());
339                         bool const success = in_file.copyTo(out_file);
340                         if (!success) {
341                                 lyxerr << "Failed to copy '" << in_file
342                                        << "' to '" << out_file << "'"
343                                        << endl;
344                         }
345                 }
346                 // FIXME UNICODE
347                 os << "\\bibliographystyle{"
348                    << from_utf8(latex_path(normalizeName(buffer(), runparams, base, ".bst")))
349                    << "}\n";
350                 nlines += 1;
351         }
352
353         // Post this warning only once.
354         static bool warned_about_bst_spaces = false;
355         if (!warned_about_bst_spaces && runparams.nice && contains(style, ' ')) {
356                 warned_about_bst_spaces = true;
357                 Alert::warning(_("Export Warning!"),
358                                _("There are spaces in the path to your BibTeX style file.\n"
359                                               "BibTeX will be unable to find it."));
360         }
361
362         if (!db_out.empty() && buffer().params().use_bibtopic) {
363                 os << "\\begin{btSect}{" << db_out << "}\n";
364                 docstring btprint = getParam("btprint");
365                 if (btprint.empty())
366                         // default
367                         btprint = from_ascii("btPrintCited");
368                 os << "\\" << btprint << "\n"
369                    << "\\end{btSect}\n";
370                 nlines += 3;
371         }
372
373         // bibtotoc-Option
374         if (!bibtotoc.empty() && !buffer().params().use_bibtopic) {
375                 if (buffer().params().documentClass().hasLaTeXLayout("chapter")) {
376                         if (buffer().params().sides == OneSide) {
377                                 // oneside
378                                 os << "\\clearpage";
379                         } else {
380                                 // twoside
381                                 os << "\\cleardoublepage";
382                         }
383                         os << "\\addcontentsline{toc}{chapter}{\\bibname}";
384                 } else if (buffer().params().documentClass().hasLaTeXLayout("section"))
385                         os << "\\addcontentsline{toc}{section}{\\refname}";
386         }
387
388         if (!db_out.empty() && !buffer().params().use_bibtopic) {
389                 docstring btprint = getParam("btprint");
390                 if (btprint == "btPrintAll") {
391                         os << "\\nocite{*}\n";
392                         nlines += 1;
393                 }
394                 os << "\\bibliography{" << db_out << "}\n";
395                 nlines += 1;
396         }
397
398         return nlines;
399 }
400
401
402 support::FileNameList InsetBibtex::getBibFiles() const
403 {
404         FileName path(buffer().filePath());
405         support::PathChanger p(path);
406         
407         support::FileNameList vec;
408         
409         vector<docstring> bibfilelist = getVectorFromString(getParam("bibfiles"));
410         vector<docstring>::const_iterator it = bibfilelist.begin();
411         vector<docstring>::const_iterator en = bibfilelist.end();
412         for (; it != en; ++it) {
413                 FileName const file = 
414                         findtexfile(changeExtension(to_utf8(*it), "bib"), "bib");
415
416                 if (!file.empty())
417                         vec.push_back(file);
418                 else
419                         LYXERR0("Couldn't find " + to_utf8(*it) + " in InsetBibtex::getBibFiles()!");
420         }
421         
422         return vec;
423
424 }
425
426 namespace {
427
428         // methods for parsing bibtex files
429
430         typedef map<docstring, docstring> VarMap;
431
432         /// remove whitespace characters, optionally a single comma,
433         /// and further whitespace characters from the stream.
434         /// @return true if a comma was found, false otherwise
435         ///
436         bool removeWSAndComma(ifdocstream & ifs) {
437                 char_type ch;
438
439                 if (!ifs)
440                         return false;
441
442                 // skip whitespace
443                 do {
444                         ifs.get(ch);
445                 } while (ifs && isSpace(ch));
446
447                 if (!ifs)
448                         return false;
449
450                 if (ch != ',') {
451                         ifs.putback(ch);
452                         return false;
453                 }
454
455                 // skip whitespace
456                 do {
457                         ifs.get(ch);
458                 } while (ifs && isSpace(ch));
459
460                 if (ifs) {
461                         ifs.putback(ch);
462                 }
463
464                 return true;
465         }
466
467
468         enum charCase {
469                 makeLowerCase,
470                 keepCase
471         };
472
473         /// remove whitespace characters, read characer sequence
474         /// not containing whitespace characters or characters in
475         /// delimChars, and remove further whitespace characters.
476         ///
477         /// @return true if a string of length > 0 could be read.
478         ///
479         bool readTypeOrKey(docstring & val, ifdocstream & ifs,
480                 docstring const & delimChars, docstring const &illegalChars, 
481                 charCase chCase) {
482
483                 char_type ch;
484
485                 val.clear();
486
487                 if (!ifs)
488                         return false;
489
490                 // skip whitespace
491                 do {
492                         ifs.get(ch);
493                 } while (ifs && isSpace(ch));
494
495                 if (!ifs)
496                         return false;
497
498                 // read value
499                 bool legalChar = true;
500                 while (ifs && !isSpace(ch) && 
501                                                  delimChars.find(ch) == docstring::npos &&
502                                                  (legalChar = (illegalChars.find(ch) == docstring::npos))
503                                         ) 
504                 {
505                         if (chCase == makeLowerCase)
506                                 val += lowercase(ch);
507                         else
508                                 val += ch;
509                         ifs.get(ch);
510                 }
511                 
512                 if (!legalChar) {
513                         ifs.putback(ch);
514                         return false;
515                 }
516
517                 // skip whitespace
518                 while (ifs && isSpace(ch)) {
519                         ifs.get(ch);
520                 }
521
522                 if (ifs) {
523                         ifs.putback(ch);
524                 }
525
526                 return val.length() > 0;
527         }
528
529         /// read subsequent bibtex values that are delimited with a #-character.
530         /// Concatenate all parts and replace names with the associated string in
531         /// the variable strings.
532         /// @return true if reading was successfull (all single parts were delimited
533         /// correctly)
534         bool readValue(docstring & value, ifdocstream & ifs, const VarMap & strings) {
535
536                 char_type ch;
537
538                 value.clear();
539
540                 if (!ifs)
541                         return false;
542
543                 docstring val;
544                 do {
545                         // skip whitespace
546                         do {
547                                 ifs.get(ch);
548                         } while (ifs && isSpace(ch));
549
550                         if (!ifs)
551                                 return false;
552
553                         // check for field type
554                         if (isDigit(ch)) {
555
556                                 // read integer value
557                                 do {
558                                         val += ch;
559                                         ifs.get(ch);
560                                 } while (ifs && isDigit(ch));
561
562                                 if (!ifs)
563                                         return false;
564
565                         } else if (ch == '"' || ch == '{') {
566                                 // set end delimiter
567                                 char_type delim = ch == '"' ? '"': '}';
568
569                                 // Skip whitespace
570                                 do {
571                                         ifs.get(ch);
572                                 } while (ifs && isSpace(ch));
573                                 
574                                 if (!ifs)
575                                         return false;
576                                 
577                                 // We now have the first non-whitespace character
578                                 // We'll collapse adjacent whitespace.
579                                 bool lastWasWhiteSpace = false;
580                                 
581                                 // inside this delimited text braces must match.
582                                 // Thus we can have a closing delimiter only
583                                 // when nestLevel == 0
584                                 int nestLevel = 0;
585  
586                                 while (ifs && (nestLevel > 0 || ch != delim)) {
587                                         if (isSpace(ch)) {
588                                                 lastWasWhiteSpace = true;
589                                                 ifs.get(ch);
590                                                 continue;
591                                         }
592                                         // We output the space only after we stop getting 
593                                         // whitespace so as not to output any whitespace
594                                         // at the end of the value.
595                                         if (lastWasWhiteSpace) {
596                                                 lastWasWhiteSpace = false;
597                                                 val += ' ';
598                                         }
599
600                                         val += ch;
601
602                                         // update nesting level
603                                         switch (ch) {
604                                                 case '{':
605                                                         ++nestLevel;
606                                                         break;
607                                                 case '}':
608                                                         --nestLevel;
609                                                         if (nestLevel < 0) return false;
610                                                         break;
611                                         }
612
613                                         ifs.get(ch);
614                                 }
615
616                                 if (!ifs)
617                                         return false;
618
619                                 ifs.get(ch);
620
621                                 if (!ifs)
622                                         return false;
623
624                         } else {
625
626                                 // reading a string name
627                                 docstring strName;
628
629                                 while (ifs && !isSpace(ch) && ch != '#' && ch != ',' && ch != '}' && ch != ')') {
630                                         strName += lowercase(ch);
631                                         ifs.get(ch);
632                                 }
633
634                                 if (!ifs)
635                                         return false;
636
637                                 // replace the string with its assigned value or
638                                 // discard it if it's not assigned
639                                 if (strName.length()) {
640                                         VarMap::const_iterator pos = strings.find(strName);
641                                         if (pos != strings.end()) {
642                                                 val += pos->second;
643                                         }
644                                 }
645                         }
646
647                         // skip WS
648                         while (ifs && isSpace(ch)) {
649                                 ifs.get(ch);
650                         }
651
652                         if (!ifs)
653                                 return false;
654
655                         // continue reading next value on concatenate with '#'
656                 } while (ch == '#');
657
658                 ifs.putback(ch);
659
660                 // Ok, we now have the value. Now we are going to go
661                 // through it and replace e.g. \"a with its unicode value.
662                 // We'll also strip commands, like \emph, and the like, so 
663                 // it will look nice in the UI.
664                 bool scanning_cmd = false;
665                 bool scanning_math = false;
666                 bool escaped = false; // used to catch \$, etc.
667                 while (val.size()) {
668                         char_type const ch = val[0];
669
670                         // if we're scanning math, we output everything until we
671                         // find an unescaped $, at which point we break out.
672                         if (scanning_math) {
673                                 if (escaped)
674                                         escaped = false;
675                                 else if (ch == '\\')
676                                         escaped = true;
677                                 else if (ch == '$') 
678                                         scanning_math = false;
679                                 value += ch;
680                                 val = val.substr(1);
681                                 continue;
682                         }
683
684                         // if we're scanning a command name, then we just
685                         // discard characters until we hit something that
686                         // isn't alpha.
687                         if (scanning_cmd) {
688                                 if (isAlphaASCII(ch)) {
689                                         val = val.substr(1);
690                                         escaped = false;
691                                         continue;
692                                 }
693                                 // so we're done with this command.
694                                 // now we fall through and check this character.
695                                 scanning_cmd = false;
696                         }
697
698                         // was the last character a \? If so, then this is something like: \\,
699                         // or \$, so we'll just output it. That's probably not always right...
700                         if (escaped) {
701                                 value += ch;
702                                 val = val.substr(1);
703                                 escaped = false;
704                                 continue;
705                         }
706
707                         if (ch == '$') {
708                                 value += ch;
709                                 val = val.substr(1);
710                                 scanning_math = true;
711                                 continue;
712                         }
713
714                         // we just ignore braces
715                         if (ch == '{' || ch == '}') {
716                                 val = val.substr(1);
717                                 continue;
718                         }
719
720                         // we're going to check things that look like commands, so if
721                         // this doesn't, just output it.
722                         if (ch != '\\') {
723                                 value += ch;
724                                 val = val.substr(1);
725                                 continue;
726                         }
727
728                         // ok, could be a command of some sort
729                         // let's see if it corresponds to some unicode
730                         // unicodesymbols has things in the form: \"{u},
731                         // whereas we may see things like: \"u. So we'll
732                         // look for that and change it, if necessary.
733                         static boost::regex const reg("^\\\\\\W\\w");
734                         if (boost::regex_search(to_utf8(val), reg)) {
735                                 val.insert(3, from_ascii("}"));
736                                 val.insert(2, from_ascii("{"));
737                         }
738                         docstring rem;
739                         docstring const cnvtd = Encodings::fromLaTeXCommand(val, rem);
740                         if (!cnvtd.empty()) {
741                                 // it did, so we'll take that bit and proceed with what's left
742                                 value += cnvtd;
743                                 val = rem;
744                                 continue;
745                         }
746                         // it's a command of some sort
747                         scanning_cmd = true;
748                         escaped = true;
749                         val = val.substr(1);
750                 }
751
752                 return true;
753         }
754 }
755
756
757 // This method returns a comma separated list of Bibtex entries
758 void InsetBibtex::fillWithBibKeys(BiblioInfo & keylist,
759         InsetIterator const & /*di*/) const
760 {
761         // This bibtex parser is a first step to parse bibtex files
762         // more precisely.
763         //
764         // - it reads the whole bibtex entry and does a syntax check
765         //   (matching delimiters, missing commas,...
766         // - it recovers from errors starting with the next @-character
767         // - it reads @string definitions and replaces them in the
768         //   field values.
769         // - it accepts more characters in keys or value names than
770         //   bibtex does.
771         //
772         // Officially bibtex does only support ASCII, but in practice
773         // you can use the encoding of the main document as long as
774         // some elements like keys and names are pure ASCII. Therefore
775         // we convert the file from the buffer encoding.
776         // We don't restrict keys to ASCII in LyX, since our own
777         // InsetBibitem can generate non-ASCII keys, and nonstandard
778         // 8bit clean bibtex forks exist.
779         support::FileNameList const files = getBibFiles();
780         support::FileNameList::const_iterator it = files.begin();
781         support::FileNameList::const_iterator en = files.end();
782         for (; it != en; ++ it) {
783                 ifdocstream ifs(it->toFilesystemEncoding().c_str(),
784                         ios_base::in, buffer().params().encoding().iconvName());
785
786                 char_type ch;
787                 VarMap strings;
788
789                 while (ifs) {
790
791                         ifs.get(ch);
792                         if (!ifs)
793                                 break;
794
795                         if (ch != '@')
796                                 continue;
797
798                         docstring entryType;
799
800                         if (!readTypeOrKey(entryType, ifs, from_ascii("{("), docstring(), makeLowerCase)) {
801                                 lyxerr << "InsetBibtex::fillWithBibKeys: Error reading entry type." << std::endl;
802                                 continue;
803                         }
804
805                         if (!ifs) {
806                                 lyxerr << "InsetBibtex::fillWithBibKeys: Unexpected end of file." << std::endl;
807                                 continue;
808                         }
809
810                         if (entryType == from_ascii("comment")) {
811                                 ifs.ignore(numeric_limits<int>::max(), '\n');
812                                 continue;
813                         }
814
815                         ifs.get(ch);
816                         if (!ifs) {
817                                 lyxerr << "InsetBibtex::fillWithBibKeys: Unexpected end of file." << std::endl;
818                                 break;
819                         }
820
821                         if ((ch != '(') && (ch != '{')) {
822                                 lyxerr << "InsetBibtex::fillWithBibKeys: Invalid entry delimiter." << std::endl;
823                                 ifs.putback(ch);
824                                 continue;
825                         }
826
827                         // process the entry
828                         if (entryType == from_ascii("string")) {
829
830                                 // read string and add it to the strings map
831                                 // (or replace it's old value)
832                                 docstring name;
833                                 docstring value;
834
835                                 if (!readTypeOrKey(name, ifs, from_ascii("="), from_ascii("#{}(),"), makeLowerCase)) {
836                                         lyxerr << "InsetBibtex::fillWithBibKeys: Error reading string name." << std::endl;
837                                         continue;
838                                 }
839
840                                 if (!ifs) {
841                                         lyxerr << "InsetBibtex::fillWithBibKeys: Unexpected end of file." << std::endl;
842                                         continue;
843                                 }
844
845                                 // next char must be an equal sign
846                                 ifs.get(ch);
847                                 if (!ifs || ch != '=') {
848                                         lyxerr << "InsetBibtex::fillWithBibKeys: No `=' after string name: " << 
849                                                         name << "." << std::endl;
850                                         continue;
851                                 }
852
853                                 if (!readValue(value, ifs, strings)) {
854                                         lyxerr << "InsetBibtex::fillWithBibKeys: Unable to read value for string: " << 
855                                                         name << "." << std::endl;
856                                         continue;
857                                 }
858
859                                 strings[name] = value;
860
861                         } else if (entryType == from_ascii("preamble")) {
862
863                                 // preamble definitions are discarded.
864                                 // can they be of any use in lyx?
865                                 docstring value;
866
867                                 if (!readValue(value, ifs, strings)) {
868                                         lyxerr << "InsetBibtex::fillWithBibKeys: Unable to read preamble value." << std::endl;
869                                         continue;
870                                 }
871
872                         } else {
873
874                                 // Citation entry. Try to read the key.
875                                 docstring key;
876
877                                 if (!readTypeOrKey(key, ifs, from_ascii(","), from_ascii("}"), keepCase)) {
878                                         lyxerr << "InsetBibtex::fillWithBibKeys: Unable to read key for entry type:" << 
879                                                         entryType << "." << std::endl;
880                                         continue;
881                                 }
882
883                                 if (!ifs) {
884                                         lyxerr << "InsetBibtex::fillWithBibKeys: Unexpected end of file." << std::endl;
885                                         continue;
886                                 }
887
888                                 /////////////////////////////////////////////
889                                 // now we have a key, so we will add an entry 
890                                 // (even if it's empty, as bibtex does)
891                                 //
892                                 // we now read the field = value pairs.
893                                 // all items must be separated by a comma. If
894                                 // it is missing the scanning of this entry is
895                                 // stopped and the next is searched.
896                                 docstring fields;
897                                 docstring name;
898                                 docstring value;
899                                 docstring commaNewline;
900                                 docstring data;
901                                 BibTeXInfo keyvalmap(key, entryType);
902                                 
903                                 bool readNext = removeWSAndComma(ifs);
904  
905                                 while (ifs && readNext) {
906
907                                         // read field name
908                                         if (!readTypeOrKey(name, ifs, from_ascii("="), 
909                                                            from_ascii("{}(),"), makeLowerCase) || !ifs)
910                                                 break;
911
912                                         // next char must be an equal sign
913                                         ifs.get(ch);
914                                         if (!ifs) {
915                                                 lyxerr << "InsetBibtex::fillWithBibKeys: Unexpected end of file." << std::endl;
916                                                 break;
917                                         }
918                                         if (ch != '=') {
919                                                 lyxerr << "InsetBibtex::fillWithBibKeys: Missing `=' after field name: " <<
920                                                                 name << ", for key: " << key << "." << std::endl;
921                                                 ifs.putback(ch);
922                                                 break;
923                                         }
924
925                                         // read field value
926                                         if (!readValue(value, ifs, strings)) {
927                                                 lyxerr << "InsetBibtex::fillWithBibKeys: Unable to read value for field: " <<
928                                                                 name << ", for key: " << key << "." << std::endl;
929                                                 break;
930                                         }
931
932                                         keyvalmap[name] = value;
933                                         data += "\n\n" + value;
934                                         keylist.addFieldName(name);
935                                         readNext = removeWSAndComma(ifs);
936                                 }
937
938                                 // add the new entry
939                                 keylist.addEntryType(entryType);
940                                 keyvalmap.setAllData(data);
941                                 keylist[key] = keyvalmap;
942                         } //< else (citation entry)
943                 } //< searching '@'
944         } //< for loop over files
945 }
946
947
948 FileName InsetBibtex::getBibTeXPath(docstring const & filename, Buffer const & buf)
949 {
950         string texfile = changeExtension(to_utf8(filename), "bib");
951         // note that, if the filename can be found directly from the path, 
952         // findtexfile will just return a FileName object for that path.
953         FileName file(findtexfile(texfile, "bib"));
954         if (file.empty())
955                 file = FileName(makeAbsPath(texfile, buf.filePath()));
956         return file;
957 }
958  
959
960 bool InsetBibtex::addDatabase(docstring const & db)
961 {
962         docstring bibfiles = getParam("bibfiles");
963         if (tokenPos(bibfiles, ',', db) != -1)
964                 return false;
965         if (!bibfiles.empty())
966                 bibfiles += ',';
967         setParam("bibfiles", bibfiles + db);
968         return true;
969 }
970
971
972 bool InsetBibtex::delDatabase(docstring const & db)
973 {
974         docstring bibfiles = getParam("bibfiles");
975         if (contains(bibfiles, db)) {
976                 int const n = tokenPos(bibfiles, ',', db);
977                 docstring bd = db;
978                 if (n > 0) {
979                         // this is not the first database
980                         docstring tmp = ',' + bd;
981                         setParam("bibfiles", subst(bibfiles, tmp, docstring()));
982                 } else if (n == 0)
983                         // this is the first (or only) database
984                         setParam("bibfiles", split(bibfiles, bd, ','));
985                 else
986                         return false;
987         }
988         return true;
989 }
990
991
992 void InsetBibtex::validate(LaTeXFeatures & features) const
993 {
994         if (features.bufferParams().use_bibtopic)
995                 features.require("bibtopic");
996 }
997
998
999 docstring InsetBibtex::contextMenu(BufferView const &, int, int) const
1000 {
1001         return from_ascii("context-bibtex");
1002 }
1003
1004
1005 } // namespace lyx