Use the new InsetCommandParams interface (inset part), from Ugras and me

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@15413 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Georg Baum 2006-10-20 16:12:49 +00:00
parent 7450a293de
commit ffe7cb1b78
14 changed files with 112 additions and 99 deletions

View File

@ -772,7 +772,7 @@ bool BufferView::dispatch(FuncRequest const & cmd)
getInsetByCode<InsetRef>(cursor_,
InsetBase::REF_CODE);
if (inset) {
label = lyx::from_utf8(inset->getContents());
label = inset->getParam("reference");
savePosition(0);
}
}

View File

@ -162,10 +162,9 @@ InsetBase * createInset(BufferView * bv, FuncRequest const & cmd)
case LFUN_INDEX_INSERT: {
// Try and generate a valid index entry.
InsetCommandParams icp("index");
string const contents = cmd.argument().empty() ?
bv->getLyXText()->getStringToIndex(bv->cursor()) :
lyx::to_utf8(cmd.argument());
icp.setContents(contents);
icp["name"] = cmd.argument().empty() ?
lyx::from_utf8(bv->getLyXText()->getStringToIndex(bv->cursor())) :
cmd.argument();
return new InsetIndex(icp);
}
@ -380,8 +379,8 @@ InsetBase * readInset(LyXLex & lex, Buffer const & buf)
|| cmdName == "vref"
|| cmdName == "vpageref"
|| cmdName == "prettyref") {
if (!inscmd.getOptions().empty()
|| !inscmd.getContents().empty()) {
if (!inscmd["name"].empty()
|| !inscmd["reference"].empty()) {
inset.reset(new InsetRef(inscmd, buf));
}
} else if (cmdName == "tableofcontents") {

View File

@ -35,13 +35,13 @@ using std::auto_ptr;
using std::ostream;
int InsetBibitem::key_counter = 0;
string const key_prefix = "key-";
docstring const key_prefix = lyx::from_ascii("key-");
InsetBibitem::InsetBibitem(InsetCommandParams const & p)
: InsetCommand(p, "bibitem"), counter(1)
{
if (getContents().empty())
setContents(key_prefix + convert<string>(++key_counter));
if (getParam("key").empty())
setParam("key", key_prefix + convert<docstring>(++key_counter));
}
@ -64,9 +64,10 @@ void InsetBibitem::doDispatch(LCursor & cur, FuncRequest & cmd)
cur.noUpdate();
break;
}
if (p.getContents() != params().getContents())
cur.bv().buffer()->changeRefsIfUnique(params().getContents(),
p.getContents(), InsetBase::CITE_CODE);
if (p["key"] != params()["key"])
// FIXME UNICODE
cur.bv().buffer()->changeRefsIfUnique(lyx::to_utf8(params()["key"]),
lyx::to_utf8(p["key"]), InsetBase::CITE_CODE);
setParams(p);
}
@ -87,8 +88,8 @@ void InsetBibitem::read(Buffer const & buf, LyXLex & lex)
{
InsetCommand::read(buf, lex);
if (prefixIs(getContents(), key_prefix)) {
int const key = convert<int>(getContents().substr(key_prefix.length()));
if (prefixIs(getParam("key"), key_prefix)) {
int const key = convert<int>(getParam("key").substr(key_prefix.length()));
key_counter = max(key_counter, key);
}
}
@ -96,17 +97,14 @@ void InsetBibitem::read(Buffer const & buf, LyXLex & lex)
docstring const InsetBibitem::getBibLabel() const
{
// FIXME UNICODE
return getOptions().empty() ?
convert<docstring>(counter) :
lyx::from_utf8(getOptions());
docstring const & label = getParam("label");
return label.empty() ? convert<docstring>(counter) : label;
}
docstring const InsetBibitem::getScreenLabel(Buffer const &) const
{
// FIXME UNICODE
return lyx::from_utf8(getContents()) + " [" + getBibLabel() + ']';
return getParam("key") + " [" + getBibLabel() + ']';
}

View File

@ -154,7 +154,7 @@ int InsetBibtex::latex(Buffer const & buffer, odocstream & os,
typedef boost::tokenizer<Separator> Tokenizer;
Separator const separator(",");
Tokenizer const tokens(getContents(), separator);
Tokenizer const tokens(lyx::to_utf8(getParam("bibfiles")), separator);
Tokenizer::const_iterator const begin = tokens.begin();
Tokenizer::const_iterator const end = tokens.end();
@ -185,15 +185,15 @@ int InsetBibtex::latex(Buffer const & buffer, odocstream & os,
dbs << ',';
dbs << latex_path(database);
}
string const db_out = dbs.str();
// FIXME UNICODE
docstring const db_out = lyx::from_utf8(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 +201,7 @@ int InsetBibtex::latex(Buffer const & buffer, odocstream & os,
}
// Style-Options
string style = getOptions(); // maybe empty! and with bibtotoc
string style = lyx::to_utf8(getParam("options")); // maybe empty! and with bibtotoc
string bibtotoc;
if (prefixIs(style, "bibtotoc")) {
bibtotoc = "bibtotoc";
@ -252,8 +252,7 @@ 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
@ -289,8 +288,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 +303,8 @@ vector<string> const InsetBibtex::getFiles(Buffer const & buffer) const
vector<string> vec;
string tmp;
string bibfiles = getContents();
// FIXME UNICODE
string bibfiles = lyx::to_utf8(getParam("bibfiles"));
bibfiles = split(bibfiles, tmp, ',');
while (!tmp.empty()) {
string file = findtexfile(changeExtension(tmp, "bib"), "bib");
@ -362,11 +361,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(lyx::to_utf8(getParam("bibfiles")));
if (tokenPos(bibfiles, ',', db) == -1) {
if (!bibfiles.empty())
bibfiles += ',';
setParam("bibfiles", lyx::from_utf8(bibfiles + db));
return true;
}
return false;
@ -375,17 +375,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(lyx::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", lyx::from_utf8(subst(bibfiles, tmp, string())));
} else if (n == 0)
// this is the first (or only) database
setContents(split(contents, bd, ','));
setParam("bibfiles", lyx::from_utf8(split(bibfiles, bd, ',')));
else
return false;
}

View File

@ -12,6 +12,8 @@
#include "insethfill.h"
#include "gettext.h"
#include "support/std_ostream.h"
using lyx::docstring;
@ -42,7 +44,7 @@ void InsetHFill::metrics(MetricsInfo &, Dimension & dim) const
docstring const InsetHFill::getScreenLabel(Buffer const &) const
{
return lyx::from_ascii(getContents());
return _("Horizontal Fill");
}

View File

@ -222,7 +222,7 @@ string const parentFilename(Buffer const & buffer)
string const includedFilename(Buffer const & buffer,
InsetCommandParams const & params)
{
return makeAbsPath(params.getContents(),
return makeAbsPath(lyx::to_utf8(params["filename"]),
onlyPath(parentFilename(buffer)));
}
@ -297,11 +297,11 @@ docstring const InsetInclude::getScreenLabel(Buffer const &) const
temp += ": ";
if (params_.getContents().empty())
if (params_["filename"].empty())
temp += "???";
else
// FIXME: We don't know the encoding of the filename
temp += lyx::from_ascii(onlyFilename(params_.getContents()));
temp += lyx::from_ascii(onlyFilename(lyx::to_utf8(params_["filename"])));
return temp;
}
@ -354,7 +354,7 @@ bool loadIfNeeded(Buffer const & buffer, InsetCommandParams const & params)
int InsetInclude::latex(Buffer const & buffer, odocstream & os,
OutputParams const & runparams) const
{
string incfile(params_.getContents());
string incfile(lyx::to_utf8(params_["filename"]));
// Do nothing if no file name has been specified
if (incfile.empty())
@ -491,7 +491,7 @@ int InsetInclude::plaintext(Buffer const & buffer, odocstream & os,
int InsetInclude::docbook(Buffer const & buffer, odocstream & os,
OutputParams const & runparams) const
{
string incfile(params_.getContents());
string incfile(lyx::to_utf8(params_["filename"]));
// Do nothing if no file name has been specified
if (incfile.empty())
@ -538,7 +538,7 @@ int InsetInclude::docbook(Buffer const & buffer, odocstream & os,
void InsetInclude::validate(LaTeXFeatures & features) const
{
string incfile(params_.getContents());
string incfile(lyx::to_utf8(params_["filename"]));
string writefile;
Buffer const & buffer = features.buffer();

View File

@ -48,7 +48,7 @@ int InsetIndex::docbook(Buffer const &, odocstream & os,
{
// FIXME UNICODE
os << "<indexterm><primary>"
<< lyx::from_ascii(sgml::escapeString(getContents()))
<< lyx::from_ascii(sgml::escapeString(lyx::to_ascii(getParam("name"))))
<< "</primary></indexterm>";
return 0;
}

View File

@ -48,15 +48,13 @@ std::auto_ptr<InsetBase> InsetLabel::doClone() const
void InsetLabel::getLabelList(Buffer const &, std::vector<docstring> & list) const
{
// FIXME UNICODE
list.push_back(lyx::from_utf8(getContents()));
list.push_back(getParam("name"));
}
docstring const InsetLabel::getScreenLabel(Buffer const &) const
{
// FIXME UNICODE
return lyx::from_utf8(getContents());
return getParam("name");
}
@ -71,9 +69,10 @@ void InsetLabel::doDispatch(LCursor & cur, FuncRequest & cmd)
cur.noUpdate();
break;
}
if (p.getContents() != params().getContents())
cur.bv().buffer()->changeRefsIfUnique(params().getContents(),
p.getContents(), InsetBase::REF_CODE);
if (p["name"] != params()["name"])
// FIXME UNICODE
cur.bv().buffer()->changeRefsIfUnique(lyx::to_utf8(params()["name"]),
lyx::to_utf8(p["name"]), InsetBase::REF_CODE);
setParams(p);
break;
}
@ -96,8 +95,7 @@ int InsetLabel::latex(Buffer const &, odocstream & os,
int InsetLabel::plaintext(Buffer const &, odocstream & os,
OutputParams const &) const
{
// FIXME UNICODE
os << '<' << lyx::from_utf8(getContents()) << '>';
os << '<' << getParam("name") << '>';
return 0;
}
@ -107,7 +105,7 @@ int InsetLabel::docbook(Buffer const & buf, odocstream & os,
{
// FIXME UNICODE
os << "<!-- anchor id=\""
<< lyx::from_ascii(sgml::cleanID(buf, runparams, getContents()))
<< lyx::from_ascii(sgml::cleanID(buf, runparams, lyx::to_ascii(getParam("name"))))
<< "\" -->";
return 0;
}

View File

@ -48,7 +48,7 @@ void InsetRef::doDispatch(LCursor & cur, FuncRequest & cmd)
case LFUN_MOUSE_PRESS:
// Eventually trigger dialog with button 3 not 1
if (cmd.button() == mouse_button::button3)
lyx::dispatch(FuncRequest(LFUN_LABEL_GOTO, getContents()));
lyx::dispatch(FuncRequest(LFUN_LABEL_GOTO, getParam("reference")));
else {
InsetCommandMailer("ref", *this).showDialog(&cur.bv());
cur.undispatched();
@ -73,13 +73,11 @@ docstring const InsetRef::getScreenLabel(Buffer const &) const
break;
}
}
// FIXME UNICODE
temp += lyx::from_utf8(getContents());
temp += getParam("reference");
if (!isLatex && !getOptions().empty()) {
if (!isLatex && !getParam("name").empty()) {
temp += "||";
// FIXME UNICODE
temp += lyx::from_utf8(getOptions());
temp += getParam("name");
}
return temp;
}
@ -99,8 +97,7 @@ int InsetRef::latex(Buffer const &, odocstream & os,
int InsetRef::plaintext(Buffer const &, odocstream & os,
OutputParams const &) const
{
// FIXME UNICODE
os << '[' << lyx::from_utf8(getContents()) << ']';
os << '[' << getParam("reference") << ']';
return 0;
}
@ -108,21 +105,23 @@ int InsetRef::plaintext(Buffer const &, odocstream & os,
int InsetRef::docbook(Buffer const & buf, odocstream & os,
OutputParams const & runparams) const
{
// FIXME UNICODE
if (getOptions().empty() && runparams.flavor == OutputParams::XML) {
os << "<xref linkend=\""
<< lyx::from_ascii(sgml::cleanID(buf, runparams, getContents()))
<< "\" />";
} else if (getOptions().empty()) {
os << "<xref linkend=\""
<< lyx::from_ascii(sgml::cleanID(buf, runparams, getContents()))
<< "\">";
docstring const & name = getParam("name");
if (name.empty()) {
if (runparams.flavor == OutputParams::XML) {
os << "<xref linkend=\""
<< lyx::from_ascii(sgml::cleanID(buf, runparams, lyx::to_ascii(getParam("reference"))))
<< "\" />";
} else {
os << "<xref linkend=\""
<< lyx::from_ascii(sgml::cleanID(buf, runparams, lyx::to_ascii(getParam("reference"))))
<< "\">";
}
} else {
os << "<link linkend=\""
<< lyx::from_ascii(sgml::cleanID(buf, runparams, getContents()))
<< "\">"
<< lyx::from_ascii(getOptions())
<< "</link>";
os << "<link linkend=\""
<< lyx::from_ascii(sgml::cleanID(buf, runparams, lyx::to_ascii(getParam("reference"))))
<< "\">"
<< getParam("name")
<< "</link>";
}
return 0;

View File

@ -40,20 +40,19 @@ docstring const InsetUrl::getScreenLabel(Buffer const &) const
docstring const temp =
(getCmdName() == "url") ? _("Url: ") : _("HtmlUrl: ");
string url;
docstring url;
if (!getOptions().empty())
url += getOptions();
if (!getParam("name").empty())
url += getParam("name");
else
url += getContents();
url += getParam("target");
// elide if long
if (url.length() > 30) {
url = url.substr(0, 10) + "..."
+ url.substr(url.length() - 17, url.length());
}
// FIXME UNICODE
return temp + lyx::from_utf8(url);
return temp + url;
}
@ -73,13 +72,11 @@ int InsetUrl::latex(Buffer const &, odocstream & os,
int InsetUrl::plaintext(Buffer const &, odocstream & os,
OutputParams const &) const
{
// FIXME UNICODE
os << '[' << lyx::from_utf8(getContents());
if (getOptions().empty())
os << '[' << getParam("target");
if (getParam("name").empty())
os << ']';
else
// FIXME UNICODE
os << "||" << lyx::from_utf8(getOptions()) << ']';
os << "||" << getParam("name") << ']';
return 0;
}
@ -87,12 +84,11 @@ int InsetUrl::plaintext(Buffer const &, odocstream & os,
int InsetUrl::docbook(Buffer const &, odocstream & os,
OutputParams const &) const
{
// FIXME UNICODE
os << "<ulink url=\""
<< lyx::from_ascii(subst(getContents(), "&", "&amp;"))
<< "\">"
<< lyx::from_ascii(getOptions())
<< "</ulink>";
os << "<ulink url=\""
<< subst(getParam("target"), lyx::from_ascii("&"), lyx::from_ascii("&amp;"))
<< "\">"
<< getParam("name")
<< "</ulink>";
return 0;
}

View File

@ -1118,7 +1118,7 @@ void InsetMathHull::doDispatch(LCursor & cur, FuncRequest & cmd)
if (name == "label") {
InsetCommandParams p("label");
InsetCommandMailer::string2params(name, lyx::to_utf8(cmd.argument()), p);
string str = p.getContents();
string str = lyx::to_utf8(p["name"]);
recordUndoInset(cur);
row_type const r = (type_ == hullMultline) ? nrows() - 1 : cur.row();
str = lyx::support::trim(str);

View File

@ -124,6 +124,13 @@ int convert<int>(string const s)
}
template<>
int convert<int>(docstring const s)
{
return strtol(lyx::to_ascii(s).c_str(), 0, 10);
}
template<>
unsigned int convert<unsigned int>(string const s)
{

View File

@ -329,6 +329,18 @@ bool prefixIs(string const & a, string const & pre)
}
bool prefixIs(docstring const & a, docstring const & pre)
{
docstring::size_type const prelen = pre.length();
docstring::size_type const alen = a.length();
if (prelen > alen || a.empty())
return false;
else
return a.compare(0, prelen, pre) == 0;
}
bool suffixIs(string const & a, char c)
{
if (a.empty()) return false;

View File

@ -91,6 +91,7 @@ std::string const uppercase(std::string const &);
/// Does the std::string start with this prefix?
bool prefixIs(std::string const &, std::string const &);
bool prefixIs(lyx::docstring const &, lyx::docstring const &);
/// Does the string end with this char?
bool suffixIs(std::string const &, char);