]> git.lyx.org Git - lyx.git/blobdiff - src/insets/insetbibtex.C
hopefully fix tex2lyx linking.
[lyx.git] / src / insets / insetbibtex.C
index ae89a8696a654050be337474c6328503cfd59fb2..d0f8ff7d6690a88aff86e0ae4110a380223ffe65 100644 (file)
 #include <fstream>
 #include <sstream>
 
-using lyx::docstring;
-using lyx::odocstream;
-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 Alert = lyx::frontend::Alert;
-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;
@@ -88,7 +89,7 @@ void InsetBibtex::doDispatch(LCursor & cur, FuncRequest & cmd)
 
        case LFUN_INSET_MODIFY: {
                InsetCommandParams p("bibtex");
-               InsetCommandMailer::string2params("bibtex", lyx::to_utf8(cmd.argument()), p);
+               InsetCommandMailer::string2params("bibtex", to_utf8(cmd.argument()), p);
                if (!p.getCmdName().empty()) {
                        setParams(p);
                        cur.buffer().updateBibfilesCache();
@@ -150,26 +151,31 @@ int InsetBibtex::latex(Buffer const & buffer, odocstream & 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.dryrun && !runparams.nice &&
                    isFileReadable(in_file)) {
 
                        // mangledFilename() needs the extension
-                       database = removeExtension(FileName(in_file).mangledFilename());
+                       database = removeExtension(DocFileName(in_file).mangledFilename());
                        string const out_file = makeAbsPath(database + ".bib",
                                        buffer.getMasterBuffer()->temppath());
 
@@ -183,17 +189,17 @@ int InsetBibtex::latex(Buffer const & buffer, odocstream & 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;
 
-               // FIXME UNICODE
                Alert::warning(_("Export Warning!"),
                               _("There are spaces in the paths to your BibTeX databases.\n"
                                              "BibTeX will be unable to find them."));
@@ -201,7 +207,7 @@ int InsetBibtex::latex(Buffer const & buffer, odocstream & os,
        }
 
        // 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";
@@ -225,7 +231,7 @@ int InsetBibtex::latex(Buffer const & buffer, odocstream & os,
                    isFileReadable(in_file)) {
                        // use new style name
                        base = removeExtension(
-                                       FileName(in_file).mangledFilename());
+                                       DocFileName(in_file).mangledFilename());
                        string const out_file = makeAbsPath(base + ".bst",
                                        buffer.getMasterBuffer()->temppath());
                        bool const success = copy(in_file, out_file);
@@ -237,7 +243,7 @@ int InsetBibtex::latex(Buffer const & buffer, odocstream & os,
                }
                // FIXME UNICODE
                os << "\\bibliographystyle{"
-                  << lyx::from_utf8(latex_path(normalize_name(buffer, runparams, base, ".bst")))
+                  << from_utf8(latex_path(normalize_name(buffer, runparams, base, ".bst")))
                   << "}\n";
                nlines += 1;
        }
@@ -252,12 +258,11 @@ int InsetBibtex::latex(Buffer const & buffer, odocstream & os,
        }
 
        if (!db_out.empty() && buffer.params().use_bibtopic){
-               // FIXME UNICODE
-               os << "\\begin{btSect}{" << lyx::from_utf8(db_out) << "}\n";
+               os << "\\begin{btSect}{" << db_out << "}\n";
                docstring btprint = getParam("btprint");
                if (btprint.empty())
                        // default
-                       btprint = lyx::from_ascii("btPrintCited");
+                       btprint = from_ascii("btPrintCited");
                os << "\\" << btprint << "\n"
                   << "\\end{btSect}\n";
                nlines += 3;
@@ -289,8 +294,7 @@ int InsetBibtex::latex(Buffer const & buffer, odocstream & os,
        }
 
        if (!db_out.empty() && !buffer.params().use_bibtopic){
-               // FIXME UNICODE
-               os << "\\bibliography{" << lyx::from_utf8(db_out) << "}\n";
+               os << "\\bibliography{" << db_out << "}\n";
                nlines += 1;
        }
 
@@ -305,7 +309,8 @@ 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");
@@ -362,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;
@@ -375,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;
        }
@@ -398,3 +405,6 @@ void InsetBibtex::validate(LaTeXFeatures & features) const
        if (features.bufferParams().use_bibtopic)
                features.require("bibtopic");
 }
+
+
+} // namespace lyx