]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetbibtex.C
hopefully fix tex2lyx linking.
[lyx.git] / src / insets / insetbibtex.C
index 2a5ee6f4ce1b5eb1575948f38c746dbe8978603a..d0f8ff7d6690a88aff86e0ae4110a380223ffe65 100644 (file)
 #include <fstream>
 #include <sstream>
 
-using lyx::support::AbsolutePath;
-using lyx::support::ascii_lowercase;
-using lyx::support::ChangeExtension;
-using lyx::support::contains;
-using lyx::support::copy;
-using lyx::support::FileName;
-using lyx::support::findtexfile;
-using lyx::support::isFileReadable;
-using lyx::support::latex_path;
-using lyx::support::ltrim;
-using lyx::support::MakeAbsPath;
-using lyx::support::MakeRelPath;
-using lyx::support::Path;
-using lyx::support::prefixIs;
-using lyx::support::removeExtension;
-using lyx::support::rtrim;
-using lyx::support::split;
-using lyx::support::subst;
-using lyx::support::tokenPos;
-using lyx::support::trim;
-
-namespace os = lyx::support::os;
+
+namespace lyx {
+
+using support::absolutePath;
+using support::ascii_lowercase;
+using support::changeExtension;
+using support::contains;
+using support::copy;
+using support::DocFileName;
+using support::findtexfile;
+using support::isFileReadable;
+using support::latex_path;
+using support::ltrim;
+using support::makeAbsPath;
+using support::makeRelPath;
+using support::Path;
+using support::prefixIs;
+using support::removeExtension;
+using support::rtrim;
+using support::split;
+using support::subst;
+using support::tokenPos;
+using support::trim;
+
+namespace Alert = frontend::Alert;
+namespace os = support::os;
 
 using std::endl;
 using std::getline;
@@ -84,11 +88,12 @@ void InsetBibtex::doDispatch(LCursor & cur, FuncRequest & cmd)
        switch (cmd.action) {
 
        case LFUN_INSET_MODIFY: {
-               InsetCommandParams p;
-               InsetCommandMailer::string2params("bibtex", cmd.argument, p);
-               if (!p.getCmdName().empty())
+               InsetCommandParams p("bibtex");
+               InsetCommandMailer::string2params("bibtex", to_utf8(cmd.argument()), p);
+               if (!p.getCmdName().empty()) {
                        setParams(p);
-               else
+                       cur.buffer().updateBibfilesCache();
+               } else
                        cur.noUpdate();
                break;
        }
@@ -100,7 +105,7 @@ void InsetBibtex::doDispatch(LCursor & cur, FuncRequest & cmd)
 }
 
 
-string const InsetBibtex::getScreenLabel(Buffer const &) const
+docstring const InsetBibtex::getScreenLabel(Buffer const &) const
 {
        return _("BibTeX Generated Bibliography");
 }
@@ -111,19 +116,19 @@ namespace {
 string normalize_name(Buffer const & buffer, OutputParams const & runparams,
                      string const & name, string const & ext)
 {
-       string const fname = MakeAbsPath(name, buffer.filePath());
-       if (AbsolutePath(name) || !isFileReadable(fname + ext))
+       string const fname = makeAbsPath(name, buffer.filePath());
+       if (absolutePath(name) || !isFileReadable(fname + ext))
                return name;
        else if (!runparams.nice)
                return fname;
        else
-               return MakeRelPath(fname, buffer.getMasterBuffer()->filePath());
+               return makeRelPath(fname, buffer.getMasterBuffer()->filePath());
 }
 
 }
 
 
-int InsetBibtex::latex(Buffer const & buffer, ostream & os,
+int InsetBibtex::latex(Buffer const & buffer, odocstream & os,
                       OutputParams const & runparams) const
 {
        // the sequence of the commands:
@@ -146,27 +151,32 @@ int InsetBibtex::latex(Buffer const & buffer, ostream & os,
        // use such filenames.)
        // Otherwise, store the (maybe absolute) path to the original,
        // unmangled database name.
-       typedef boost::char_separator<char> Separator;
-       typedef boost::tokenizer<Separator> Tokenizer;
-
-       Separator const separator(",");
-       Tokenizer const tokens(getContents(), separator);
+       typedef boost::char_separator<char_type> Separator;
+       typedef boost::tokenizer<Separator, docstring::const_iterator, docstring> Tokenizer;
+
+       Separator const separator(from_ascii(",").c_str());
+       // The tokenizer must not be called with temporary strings, since
+       // it does not make a copy and uses iterators of the string further
+       // down. getParam returns a reference, so this is OK.
+       Tokenizer const tokens(getParam("bibfiles"), separator);
        Tokenizer::const_iterator const begin = tokens.begin();
        Tokenizer::const_iterator const end = tokens.end();
 
-       std::ostringstream dbs;
+       odocstringstream dbs;
        for (Tokenizer::const_iterator it = begin; it != end; ++it) {
-               string const input = trim(*it);
+               docstring const input = trim(*it);
+               // FIXME UNICODE
+               string utf8input(to_utf8(input));
                string database =
-                       normalize_name(buffer, runparams, input, ".bib");
+                       normalize_name(buffer, runparams, utf8input, ".bib");
                string const in_file = database + ".bib";
 
-               if (!runparams.inComment && !runparams.nice &&
+               if (!runparams.inComment && !runparams.dryrun && !runparams.nice &&
                    isFileReadable(in_file)) {
 
                        // mangledFilename() needs the extension
-                       database = removeExtension(FileName(in_file).mangledFilename());
-                       string const out_file = MakeAbsPath(database + ".bib",
+                       database = removeExtension(DocFileName(in_file).mangledFilename());
+                       string const out_file = makeAbsPath(database + ".bib",
                                        buffer.getMasterBuffer()->temppath());
 
                        bool const success = copy(in_file, out_file);
@@ -179,24 +189,25 @@ int InsetBibtex::latex(Buffer const & buffer, ostream & os,
 
                if (it != begin)
                        dbs << ',';
-               dbs << latex_path(database);
+               // FIXME UNICODE
+               dbs << from_utf8(latex_path(database));
        }
-       string const db_out = dbs.str();
+       docstring const db_out = dbs.str();
 
        // Post this warning only once.
        static bool warned_about_spaces = false;
        if (!warned_about_spaces &&
-           runparams.nice && db_out.find(' ') != string::npos) {
+           runparams.nice && db_out.find(' ') != docstring::npos) {
                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."));
+                                             "BibTeX will be unable to find them."));
 
        }
 
        // Style-Options
-       string style = getOptions(); // maybe empty! and with bibtotoc
+       string style = to_utf8(getParam("options")); // maybe empty! and with bibtotoc
        string bibtotoc;
        if (prefixIs(style, "bibtotoc")) {
                bibtotoc = "bibtotoc";
@@ -216,12 +227,12 @@ int InsetBibtex::latex(Buffer const & buffer, ostream & os,
                // exporting to .tex copy it to the tmp directory.
                // This prevents problems with spaces and 8bit charcaters
                // in the file name.
-               if (!runparams.inComment && !runparams.nice &&
+               if (!runparams.inComment && !runparams.dryrun && !runparams.nice &&
                    isFileReadable(in_file)) {
                        // use new style name
                        base = removeExtension(
-                                       FileName(in_file).mangledFilename());
-                       string const out_file = MakeAbsPath(base + ".bst",
+                                       DocFileName(in_file).mangledFilename());
+                       string const out_file = makeAbsPath(base + ".bst",
                                        buffer.getMasterBuffer()->temppath());
                        bool const success = copy(in_file, out_file);
                        if (!success) {
@@ -230,8 +241,9 @@ int InsetBibtex::latex(Buffer const & buffer, ostream & os,
                                       << endl;
                        }
                }
+               // FIXME UNICODE
                os << "\\bibliographystyle{"
-                  << latex_path(normalize_name(buffer, runparams, base, ".bst"))
+                  << from_utf8(latex_path(normalize_name(buffer, runparams, base, ".bst")))
                   << "}\n";
                nlines += 1;
        }
@@ -242,15 +254,15 @@ int InsetBibtex::latex(Buffer const & buffer, ostream & os,
                warned_about_bst_spaces = true;
                Alert::warning(_("Export Warning!"),
                               _("There are spaces in the path to your BibTeX style file.\n"
-                                "BibTeX will be unable to find it."));
+                                             "BibTeX will be unable to find it."));
        }
 
        if (!db_out.empty() && buffer.params().use_bibtopic){
                os << "\\begin{btSect}{" << db_out << "}\n";
-               string btprint = getSecOptions();
+               docstring btprint = getParam("btprint");
                if (btprint.empty())
                        // default
-                       btprint = "btPrintCited";
+                       btprint = from_ascii("btPrintCited");
                os << "\\" << btprint << "\n"
                   << "\\end{btSect}\n";
                nlines += 3;
@@ -297,10 +309,11 @@ vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
        vector<string> vec;
 
        string tmp;
-       string bibfiles = getContents();
+       // FIXME UNICODE
+       string bibfiles = to_utf8(getParam("bibfiles"));
        bibfiles = split(bibfiles, tmp, ',');
        while (!tmp.empty()) {
-               string file = findtexfile(ChangeExtension(tmp, "bib"), "bib");
+               string file = findtexfile(changeExtension(tmp, "bib"), "bib");
                lyxerr[Debug::LATEX] << "Bibfile: " << file << endl;
 
                // If we didn't find a matching file name just fail silently
@@ -354,11 +367,12 @@ void InsetBibtex::fillWithBibKeys(Buffer const & buffer,
 
 bool InsetBibtex::addDatabase(string const & db)
 {
-       string contents(getContents());
-       if (tokenPos(contents, ',', db) == -1) {
-               if (!contents.empty())
-                       contents += ',';
-               setContents(contents + db);
+       // FIXME UNICODE
+       string bibfiles(to_utf8(getParam("bibfiles")));
+       if (tokenPos(bibfiles, ',', db) == -1) {
+               if (!bibfiles.empty())
+                       bibfiles += ',';
+               setParam("bibfiles", from_utf8(bibfiles + db));
                return true;
        }
        return false;
@@ -367,17 +381,18 @@ bool InsetBibtex::addDatabase(string const & db)
 
 bool InsetBibtex::delDatabase(string const & db)
 {
-       string contents(getContents());
-       if (contains(contents, db)) {
-               int const n = tokenPos(contents, ',', db);
+       // FIXME UNICODE
+       string bibfiles(to_utf8(getParam("bibfiles")));
+       if (contains(bibfiles, db)) {
+               int const n = tokenPos(bibfiles, ',', db);
                string bd = db;
                if (n > 0) {
                        // this is not the first database
                        string tmp = ',' + bd;
-                       setContents(subst(contents, tmp, ""));
+                       setParam("bibfiles", from_utf8(subst(bibfiles, tmp, string())));
                } else if (n == 0)
                        // this is the first (or only) database
-                       setContents(split(contents, bd, ','));
+                       setParam("bibfiles", from_utf8(split(bibfiles, bd, ',')));
                else
                        return false;
        }
@@ -390,3 +405,6 @@ void InsetBibtex::validate(LaTeXFeatures & features) const
        if (features.bufferParams().use_bibtopic)
                features.require("bibtopic");
 }
+
+
+} // namespace lyx