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