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"
17 #include "buffer_funcs.h"
19 #include "BufferView.h"
20 #include "CutAndPaste.h"
21 #include "DispatchResult.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
24 #include "InsetIterator.h"
26 #include "ParIterator.h"
29 #include "TocBackend.h"
31 #include "frontends/alert.h"
33 #include "support/convert.h"
34 #include "support/gettext.h"
35 #include "support/lstrings.h"
36 #include "support/lyxalgo.h"
39 using namespace lyx::support;
44 InsetLabel::InsetLabel(InsetCommandParams const & p)
45 : InsetCommand(p, "label")
49 void InsetLabel::initView()
51 updateCommand(getParam("name"));
55 void InsetLabel::updateCommand(docstring const & new_label, bool updaterefs)
57 docstring const old_label = getParam("name");
58 docstring label = new_label;
60 while (buffer().insetLabel(label)) {
61 label = new_label + '-' + convert<docstring>(i);
65 if (label != new_label) {
66 // Warn the user that the label has been changed to something else.
67 frontend::Alert::warning(_("Label names must be unique!"),
68 bformat(_("The label %1$s already exists,\n"
69 "it will be changed to %2$s."), new_label, label));
72 buffer().undo().beginUndoGroup();
73 setParam("name", label);
76 Buffer::References & refs = buffer().references(old_label);
77 Buffer::References::iterator it = refs.begin();
78 Buffer::References::iterator end = refs.end();
79 for (; it != end; ++it) {
80 buffer().undo().recordUndo(it->second);
81 it->first->setParam("reference", label);
84 buffer().undo().endUndoGroup();
86 // We need an update of the Buffer reference cache. This is achieved by
88 buffer().updateLabels();
92 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
94 static ParamInfo param_info_;
95 if (param_info_.empty())
96 param_info_.add("name", ParamInfo::LATEX_REQUIRED);
101 docstring InsetLabel::screenLabel() const
103 return screen_label_;
107 void InsetLabel::updateLabels(ParIterator const &)
109 docstring const & label = getParam("name");
110 if (buffer().insetLabel(label)) {
111 // Problem: We already have an InsetLabel with the same name!
112 screen_label_ = _("DUPLICATE: ") + label;
115 buffer().setInsetLabel(label, this);
116 screen_label_ = label;
120 void InsetLabel::addToToc(DocIterator const & cpit)
122 docstring const & label = getParam("name");
123 Toc & toc = buffer().tocBackend().toc("label");
124 if (buffer().insetLabel(label) != this) {
125 toc.push_back(TocItem(cpit, 0, screen_label_));
128 toc.push_back(TocItem(cpit, 0, screen_label_));
129 Buffer::References const & refs = buffer().references(label);
130 Buffer::References::const_iterator it = refs.begin();
131 Buffer::References::const_iterator end = refs.end();
132 for (; it != end; ++it) {
133 DocIterator const ref_pit(it->second);
134 toc.push_back(TocItem(ref_pit, 1, it->first->screenLabel()));
139 bool InsetLabel::getStatus(Cursor & cur, FuncRequest const & cmd,
140 FuncStatus & status) const
143 switch (cmd.action) {
144 case LFUN_INSERT_LABEL_AS_REF:
145 case LFUN_COPY_LABEL_AS_REF:
149 return InsetCommand::getStatus(cur, cmd, status);
152 status.setEnabled(enabled);
157 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
159 switch (cmd.action) {
161 case LFUN_INSET_MODIFY: {
162 InsetCommandParams p(LABEL_CODE);
164 InsetCommand::string2params("label", to_utf8(cmd.argument()), p);
165 if (p.getCmdName().empty()) {
169 if (p["name"] != params()["name"])
170 updateCommand(p["name"]);
174 case LFUN_COPY_LABEL_AS_REF: {
175 InsetCommandParams p(REF_CODE, "ref");
176 p["reference"] = getParam("name");
177 cap::clearSelection();
178 cap::copyInset(cur, new InsetRef(*cur.buffer(), p), getParam("name"));
182 case LFUN_INSERT_LABEL_AS_REF: {
183 InsetCommandParams p(REF_CODE, "ref");
184 p["reference"] = getParam("name");
185 string const data = InsetCommand::params2string("ref", p);
186 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
191 InsetCommand::doDispatch(cur, cmd);
197 int InsetLabel::latex(odocstream & os, OutputParams const &) const
199 os << escape(getCommand());
204 int InsetLabel::plaintext(odocstream & os, OutputParams const &) const
206 docstring const str = getParam("name");
207 os << '<' << str << '>';
208 return 2 + str.size();
212 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
214 os << "<!-- anchor id=\""
215 << sgml::cleanID(buffer(), runparams, getParam("name"))