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