]> git.lyx.org Git - lyx.git/blob - src/insets/insetbibtex.C
Convert most of the bibtex machinery to docstring.
[lyx.git] / src / insets / insetbibtex.C
1 /**
2  * \file insetbibtex.C
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  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "insetbibtex.h"
14
15 #include "buffer.h"
16 #include "bufferparams.h"
17 #include "dispatchresult.h"
18 #include "debug.h"
19 #include "encoding.h"
20 #include "funcrequest.h"
21 #include "gettext.h"
22 #include "LaTeXFeatures.h"
23 #include "metricsinfo.h"
24 #include "outputparams.h"
25
26 #include "frontends/Alert.h"
27
28 #include "support/filetools.h"
29 #include "support/lstrings.h"
30 #include "support/lyxlib.h"
31 #include "support/os.h"
32 #include "support/path.h"
33
34 #include <boost/tokenizer.hpp>
35
36
37 namespace lyx {
38
39 using support::absolutePath;
40 using support::ascii_lowercase;
41 using support::changeExtension;
42 using support::contains;
43 using support::copy;
44 using support::DocFileName;
45 using support::FileName;
46 using support::findtexfile;
47 using support::isFileReadable;
48 using support::latex_path;
49 using support::ltrim;
50 using support::makeAbsPath;
51 using support::makeRelPath;
52 using support::Path;
53 using support::prefixIs;
54 using support::removeExtension;
55 using support::rtrim;
56 using support::split;
57 using support::subst;
58 using support::tokenPos;
59 using support::trim;
60
61 namespace Alert = frontend::Alert;
62 namespace os = support::os;
63
64 using std::endl;
65 using std::getline;
66 using std::string;
67 using std::ostream;
68 using std::pair;
69 using std::vector;
70
71
72 InsetBibtex::InsetBibtex(InsetCommandParams const & p)
73         : InsetCommand(p, "bibtex")
74 {}
75
76
77 std::auto_ptr<InsetBase> InsetBibtex::doClone() const
78 {
79         return std::auto_ptr<InsetBase>(new InsetBibtex(*this));
80 }
81
82
83 void InsetBibtex::doDispatch(LCursor & cur, FuncRequest & cmd)
84 {
85         switch (cmd.action) {
86
87         case LFUN_INSET_MODIFY: {
88                 InsetCommandParams p("bibtex");
89                 InsetCommandMailer::string2params("bibtex", to_utf8(cmd.argument()), p);
90                 if (!p.getCmdName().empty()) {
91                         setParams(p);
92                         cur.buffer().updateBibfilesCache();
93                 } else
94                         cur.noUpdate();
95                 break;
96         }
97
98         default:
99                 InsetCommand::doDispatch(cur, cmd);
100                 break;
101         }
102 }
103
104
105 docstring const InsetBibtex::getScreenLabel(Buffer const &) const
106 {
107         return _("BibTeX Generated Bibliography");
108 }
109
110
111 namespace {
112
113 string normalize_name(Buffer const & buffer, OutputParams const & runparams,
114                       string const & name, string const & ext)
115 {
116         string const fname = makeAbsPath(name, buffer.filePath());
117         if (absolutePath(name) || !isFileReadable(FileName(fname + ext)))
118                 return name;
119         else if (!runparams.nice)
120                 return fname;
121         else
122                 return makeRelPath(fname, buffer.getMasterBuffer()->filePath());
123 }
124
125 }
126
127
128 int InsetBibtex::latex(Buffer const & buffer, odocstream & os,
129                        OutputParams const & runparams) const
130 {
131         // the sequence of the commands:
132         // 1. \bibliographystyle{style}
133         // 2. \addcontentsline{...} - if option bibtotoc set
134         // 3. \bibliography{database}
135         // and with bibtopic:
136         // 1. \bibliographystyle{style}
137         // 2. \begin{btSect}{database}
138         // 3. \btPrint{Cited|NotCited|All}
139         // 4. \end{btSect}
140
141         // Database(s)
142         // If we are processing the LaTeX file in a temp directory then
143         // copy the .bib databases to this temp directory, mangling their
144         // names in the process. Store this mangled name in the list of
145         // all databases.
146         // (We need to do all this because BibTeX *really*, *really*
147         // can't handle "files with spaces" and Windows users tend to
148         // use such filenames.)
149         // Otherwise, store the (maybe absolute) path to the original,
150         // unmangled database name.
151         typedef boost::char_separator<char_type> Separator;
152         typedef boost::tokenizer<Separator, docstring::const_iterator, docstring> Tokenizer;
153
154         Separator const separator(from_ascii(",").c_str());
155         // The tokenizer must not be called with temporary strings, since
156         // it does not make a copy and uses iterators of the string further
157         // down. getParam returns a reference, so this is OK.
158         Tokenizer const tokens(getParam("bibfiles"), separator);
159         Tokenizer::const_iterator const begin = tokens.begin();
160         Tokenizer::const_iterator const end = tokens.end();
161
162         odocstringstream dbs;
163         for (Tokenizer::const_iterator it = begin; it != end; ++it) {
164                 docstring const input = trim(*it);
165                 // FIXME UNICODE
166                 string utf8input(to_utf8(input));
167                 string database =
168                         normalize_name(buffer, runparams, utf8input, ".bib");
169                 string const try_in_file = makeAbsPath(database + ".bib", buffer.filePath());
170                 bool const not_from_texmf = isFileReadable(FileName(try_in_file));
171
172                 if (!runparams.inComment && !runparams.dryrun && !runparams.nice &&
173                     not_from_texmf) {
174
175                         // mangledFilename() needs the extension
176                         DocFileName const in_file = DocFileName(try_in_file);
177                         database = removeExtension(in_file.mangledFilename());
178                         FileName const out_file = FileName(makeAbsPath(database + ".bib",
179                                         buffer.getMasterBuffer()->temppath()));
180
181                         bool const success = copy(in_file, out_file);
182                         if (!success) {
183                                 lyxerr << "Failed to copy '" << in_file
184                                        << "' to '" << out_file << "'"
185                                        << endl;
186                         }
187                 }
188
189                 if (it != begin)
190                         dbs << ',';
191                 // FIXME UNICODE
192                 dbs << from_utf8(latex_path(database));
193         }
194         docstring const db_out = dbs.str();
195
196         // Post this warning only once.
197         static bool warned_about_spaces = false;
198         if (!warned_about_spaces &&
199             runparams.nice && db_out.find(' ') != docstring::npos) {
200                 warned_about_spaces = true;
201
202                 Alert::warning(_("Export Warning!"),
203                                _("There are spaces in the paths to your BibTeX databases.\n"
204                                               "BibTeX will be unable to find them."));
205
206         }
207
208         // Style-Options
209         string style = to_utf8(getParam("options")); // maybe empty! and with bibtotoc
210         string bibtotoc;
211         if (prefixIs(style, "bibtotoc")) {
212                 bibtotoc = "bibtotoc";
213                 if (contains(style, ',')) {
214                         style = split(style, bibtotoc, ',');
215                 }
216         }
217
218         // line count
219         int nlines = 0;
220
221         if (!style.empty()) {
222                 string base =
223                         normalize_name(buffer, runparams, style, ".bst");
224                 string const try_in_file = makeAbsPath(base + ".bst", buffer.filePath());
225                 bool const not_from_texmf = isFileReadable(FileName(try_in_file));
226                 // If this style does not come from texmf and we are not
227                 // exporting to .tex copy it to the tmp directory.
228                 // This prevents problems with spaces and 8bit charcaters
229                 // in the file name.
230                 if (!runparams.inComment && !runparams.dryrun && !runparams.nice &&
231                     not_from_texmf) {
232                         // use new style name
233                         DocFileName const in_file = DocFileName(try_in_file);
234                         base = removeExtension(in_file.mangledFilename());
235                         FileName const out_file = FileName(makeAbsPath(base + ".bst",
236                                         buffer.getMasterBuffer()->temppath()));
237                         bool const success = copy(in_file, out_file);
238                         if (!success) {
239                                 lyxerr << "Failed to copy '" << in_file
240                                        << "' to '" << out_file << "'"
241                                        << endl;
242                         }
243                 }
244                 // FIXME UNICODE
245                 os << "\\bibliographystyle{"
246                    << from_utf8(latex_path(normalize_name(buffer, runparams, base, ".bst")))
247                    << "}\n";
248                 nlines += 1;
249         }
250
251         // Post this warning only once.
252         static bool warned_about_bst_spaces = false;
253         if (!warned_about_bst_spaces && runparams.nice && contains(style, ' ')) {
254                 warned_about_bst_spaces = true;
255                 Alert::warning(_("Export Warning!"),
256                                _("There are spaces in the path to your BibTeX style file.\n"
257                                               "BibTeX will be unable to find it."));
258         }
259
260         if (!db_out.empty() && buffer.params().use_bibtopic){
261                 os << "\\begin{btSect}{" << db_out << "}\n";
262                 docstring btprint = getParam("btprint");
263                 if (btprint.empty())
264                         // default
265                         btprint = from_ascii("btPrintCited");
266                 os << "\\" << btprint << "\n"
267                    << "\\end{btSect}\n";
268                 nlines += 3;
269         }
270
271         // bibtotoc-Option
272         if (!bibtotoc.empty() && !buffer.params().use_bibtopic) {
273                 // maybe a problem when a textclass has no "art" as
274                 // part of its name, because it's than book.
275                 // For the "official" lyx-layouts it's no problem to support
276                 // all well
277                 if (!contains(buffer.params().getLyXTextClass().name(),
278                               "art")) {
279                         if (buffer.params().sides == LyXTextClass::OneSide) {
280                                 // oneside
281                                 os << "\\clearpage";
282                         } else {
283                                 // twoside
284                                 os << "\\cleardoublepage";
285                         }
286
287                         // bookclass
288                         os << "\\addcontentsline{toc}{chapter}{\\bibname}";
289
290                 } else {
291                         // article class
292                         os << "\\addcontentsline{toc}{section}{\\refname}";
293                 }
294         }
295
296         if (!db_out.empty() && !buffer.params().use_bibtopic){
297                 os << "\\bibliography{" << db_out << "}\n";
298                 nlines += 1;
299         }
300
301         return nlines;
302 }
303
304
305 vector<FileName> const InsetBibtex::getFiles(Buffer const & buffer) const
306 {
307         Path p(buffer.filePath());
308
309         vector<FileName> vec;
310
311         string tmp;
312         // FIXME UNICODE
313         string bibfiles = to_utf8(getParam("bibfiles"));
314         bibfiles = split(bibfiles, tmp, ',');
315         while (!tmp.empty()) {
316                 FileName const file = findtexfile(changeExtension(tmp, "bib"), "bib");
317                 lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
318
319                 // If we didn't find a matching file name just fail silently
320                 if (!file.empty())
321                         vec.push_back(file);
322
323                 // Get next file name
324                 bibfiles = split(bibfiles, tmp, ',');
325         }
326
327         return vec;
328 }
329
330
331 // This method returns a comma separated list of Bibtex entries
332 void InsetBibtex::fillWithBibKeys(Buffer const & buffer,
333                 std::vector<std::pair<string, docstring> > & keys) const
334 {
335         vector<FileName> const files = getFiles(buffer);
336         for (vector<FileName>::const_iterator it = files.begin();
337              it != files.end(); ++ it) {
338                 // This is a _very_ simple parser for Bibtex database
339                 // files. All it does is to look for lines starting
340                 // in @ and not being @preamble and @string entries.
341                 // It does NOT do any syntax checking!
342
343                 // Officially bibtex does only support ASCII, but in practice
344                 // you can use the encoding of the main document as long as
345                 // some elements like keys and names are pure ASCII. Therefore
346                 // we convert the file from the buffer encoding.
347                 // We don't restrict keys to ASCII in LyX, since our own
348                 // InsetBibitem can generate non-ASCII keys, and nonstandard
349                 // 8bit clean bibtex forks exist.
350                 idocfstream ifs(it->toFilesystemEncoding().c_str(),
351                                 std::ios_base::in,
352                                 buffer.params().encoding().iconvName());
353                 docstring linebuf0;
354                 while (getline(ifs, linebuf0)) {
355                         docstring linebuf = trim(linebuf0);
356                         if (linebuf.empty())
357                                 continue;
358                         if (prefixIs(linebuf, from_ascii("@"))) {
359                                 linebuf = subst(linebuf, '{', '(');
360                                 docstring tmp;
361                                 linebuf = split(linebuf, tmp, '(');
362                                 tmp = ascii_lowercase(tmp);
363                                 if (!prefixIs(tmp, from_ascii("@string")) &&
364                                     !prefixIs(tmp, from_ascii("@preamble"))) {
365                                         linebuf = split(linebuf, tmp, ',');
366                                         tmp = ltrim(tmp, " \t");
367                                         if (!tmp.empty()) {
368                                                 // FIXME UNICODE
369                                                 keys.push_back(pair<string, docstring>(
370                                                         to_utf8(tmp), docstring()));
371                                         }
372                                 }
373                         } else if (!keys.empty())
374                                 keys.back().second += linebuf + '\n';
375                 }
376         }
377 }
378
379
380 bool InsetBibtex::addDatabase(string const & db)
381 {
382         // FIXME UNICODE
383         string bibfiles(to_utf8(getParam("bibfiles")));
384         if (tokenPos(bibfiles, ',', db) == -1) {
385                 if (!bibfiles.empty())
386                         bibfiles += ',';
387                 setParam("bibfiles", from_utf8(bibfiles + db));
388                 return true;
389         }
390         return false;
391 }
392
393
394 bool InsetBibtex::delDatabase(string const & db)
395 {
396         // FIXME UNICODE
397         string bibfiles(to_utf8(getParam("bibfiles")));
398         if (contains(bibfiles, db)) {
399                 int const n = tokenPos(bibfiles, ',', db);
400                 string bd = db;
401                 if (n > 0) {
402                         // this is not the first database
403                         string tmp = ',' + bd;
404                         setParam("bibfiles", from_utf8(subst(bibfiles, tmp, string())));
405                 } else if (n == 0)
406                         // this is the first (or only) database
407                         setParam("bibfiles", from_utf8(split(bibfiles, bd, ',')));
408                 else
409                         return false;
410         }
411         return true;
412 }
413
414
415 void InsetBibtex::validate(LaTeXFeatures & features) const
416 {
417         if (features.bufferParams().use_bibtopic)
418                 features.require("bibtopic");
419 }
420
421
422 } // namespace lyx