+string const Buffer::prepareFileNameForLaTeX(string const & name,
+ string const & ext, bool nice) const
+{
+ string const fname = makeAbsPath(name, filePath()).absFileName();
+ if (FileName::isAbsolute(name) || !FileName(fname + ext).isReadableFile())
+ return name;
+ if (!nice)
+ return fname;
+
+ // FIXME UNICODE
+ return to_utf8(makeRelPath(from_utf8(fname),
+ from_utf8(masterBuffer()->filePath())));
+}
+
+
+vector<pair<docstring, string>> const Buffer::prepareBibFilePaths(OutputParams const & runparams,
+ docstring_list const & bibfilelist,
+ bool const add_extension) const
+{
+ // If we are processing the LaTeX file in a temp directory then
+ // copy the .bib databases to this temp directory, mangling their
+ // names in the process. Store this mangled name in the list of
+ // all databases.
+ // (We need to do all this because BibTeX *really*, *really*
+ // can't handle "files with spaces" and Windows users tend to
+ // use such filenames.)
+ // Otherwise, store the (maybe absolute) path to the original,
+ // unmangled database name.
+
+ vector<pair<docstring, string>> res;
+
+ // determine the export format
+ string const tex_format = flavor2format(runparams.flavor);
+
+ // check for spaces in paths
+ bool found_space = false;
+
+ for (auto const & bit : bibfilelist) {
+ string utf8input = to_utf8(bit);
+ string database =
+ prepareFileNameForLaTeX(utf8input, ".bib", runparams.nice);
+ FileName try_in_file =
+ makeAbsPath(database + ".bib", filePath());
+ bool not_from_texmf = try_in_file.isReadableFile();
+ // If the file has not been found, try with the real file name
+ // (it might come from a child in a sub-directory)
+ if (!not_from_texmf) {
+ try_in_file = getBibfilePath(bit);
+ if (try_in_file.isReadableFile()) {
+ // Check if the file is in texmf
+ FileName kpsefile(findtexfile(changeExtension(utf8input, "bib"), "bib", true));
+ not_from_texmf = kpsefile.empty()
+ || kpsefile.absFileName() != try_in_file.absFileName();
+ if (not_from_texmf)
+ // If this exists, make path relative to the master
+ // FIXME Unicode
+ database =
+ removeExtension(prepareFileNameForLaTeX(
+ to_utf8(makeRelPath(from_utf8(try_in_file.absFileName()),
+ from_utf8(filePath()))),
+ ".bib", runparams.nice));
+ }
+ }
+
+ if (!runparams.inComment && !runparams.dryrun && !runparams.nice &&
+ not_from_texmf) {
+ // mangledFileName() needs the extension
+ DocFileName const in_file = DocFileName(try_in_file);
+ database = removeExtension(in_file.mangledFileName());
+ FileName const out_file = makeAbsPath(database + ".bib",
+ masterBuffer()->temppath());
+ bool const success = in_file.copyTo(out_file);
+ if (!success) {
+ LYXERR0("Failed to copy '" << in_file
+ << "' to '" << out_file << "'");
+ }
+ } else if (!runparams.inComment && runparams.nice && not_from_texmf) {
+ runparams.exportdata->addExternalFile(tex_format, try_in_file, database + ".bib");
+ if (!isValidLaTeXFileName(database)) {
+ frontend::Alert::warning(_("Invalid filename"),
+ _("The following filename will cause troubles "
+ "when running the exported file through LaTeX: ") +
+ from_utf8(database));
+ }
+ if (!isValidDVIFileName(database)) {
+ frontend::Alert::warning(_("Problematic filename for DVI"),
+ _("The following filename can cause troubles "
+ "when running the exported file through LaTeX "
+ "and opening the resulting DVI: ") +
+ from_utf8(database), true);
+ }
+ }
+
+ if (add_extension)
+ database += ".bib";
+
+ // FIXME UNICODE
+ docstring const path = from_utf8(latex_path(database));
+
+ if (contains(path, ' '))
+ found_space = true;
+ string enc;
+ if (params().useBiblatex() && !params().bibFileEncoding(utf8input).empty())
+ enc = params().bibFileEncoding(utf8input);
+
+ bool recorded = false;
+ for (auto const & pe : res) {
+ if (pe.first == path) {
+ recorded = true;
+ break;
+ }
+
+ }
+ if (!recorded)
+ res.push_back(make_pair(path, enc));
+ }
+
+ // Check if there are spaces in the path and warn BibTeX users, if so.
+ // (biber can cope with such paths)
+ if (!prefixIs(runparams.bibtex_command, "biber")) {
+ // Post this warning only once.
+ static bool warned_about_spaces = false;
+ if (!warned_about_spaces &&
+ runparams.nice && found_space) {
+ warned_about_spaces = true;
+ Alert::warning(_("Export Warning!"),
+ _("There are spaces in the paths to your BibTeX databases.\n"
+ "BibTeX will be unable to find them."));
+ }
+ }
+
+ return res;
+}
+
+
+