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