3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
13 #include "InsetLabel.h"
18 #include "BufferView.h"
19 #include "DispatchResult.h"
20 #include "FuncRequest.h"
21 #include "InsetIterator.h"
22 #include "ParIterator.h"
25 #include "TocBackend.h"
27 #include "frontends/alert.h"
29 #include "support/lyxalgo.h"
30 #include "support/gettext.h"
31 #include "support/lstrings.h"
34 using namespace lyx::support;
39 InsetLabel::InsetLabel(InsetCommandParams const & p)
40 : InsetCommand(p, "label")
44 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
46 static ParamInfo param_info_;
47 if (param_info_.empty()) {
48 param_info_.add("name", ParamInfo::LATEX_REQUIRED);
54 void InsetLabel::getLabelList(vector<docstring> & list) const
56 list.push_back(getParam("name"));
60 docstring InsetLabel::screenLabel() const
62 return getParam("name");
66 void InsetLabel::updateLabels(ParIterator const & it)
68 docstring const & label = getParam("name");
69 if (buffer().insetLabel(label))
70 // Problem: We already have an InsetLabel with the same name!
72 buffer().setInsetLabel(label, this);
76 void InsetLabel::addToToc(ParConstIterator const & cpit) const
78 docstring const & label = getParam("name");
79 Toc & toc = buffer().tocBackend().toc("label");
80 if (buffer().insetLabel(label) != this) {
81 toc.push_back(TocItem(cpit, 0, _("DUPLICATE: ") + label));
84 toc.push_back(TocItem(cpit, 0, label));
85 Buffer::References const & refs = buffer().references(label);
86 Buffer::References::const_iterator it = refs.begin();
87 Buffer::References::const_iterator end = refs.end();
88 for (; it != end; ++it) {
89 ParConstIterator const ref_pit(it->second);
90 toc.push_back(TocItem(ref_pit, 1, it->first->screenLabel()));
95 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
99 case LFUN_INSET_MODIFY: {
100 InsetCommandParams p(LABEL_CODE);
102 InsetCommandMailer::string2params("label", to_utf8(cmd.argument()), p);
103 if (p.getCmdName().empty()) {
107 if (p["name"] != params()["name"])
108 cur.bv().buffer().changeRefsIfUnique(params()["name"],
109 p["name"], REF_CODE);
115 InsetCommand::doDispatch(cur, cmd);
121 int InsetLabel::latex(odocstream & os, OutputParams const &) const
123 os << escape(getCommand());
128 int InsetLabel::plaintext(odocstream & os, OutputParams const &) const
130 docstring const str = getParam("name");
131 os << '<' << str << '>';
132 return 2 + str.size();
136 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
138 os << "<!-- anchor id=\""
139 << sgml::cleanID(buffer(), runparams, getParam("name"))