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 "BufferParams.h"
20 #include "BufferView.h"
21 #include "CutAndPaste.h"
22 #include "DispatchResult.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
25 #include "InsetIterator.h"
28 #include "output_xhtml.h"
29 #include "ParIterator.h"
32 #include "TextClass.h"
33 #include "TocBackend.h"
35 #include "mathed/InsetMathHull.h"
36 #include "mathed/InsetMathRef.h"
38 #include "frontends/alert.h"
40 #include "support/convert.h"
41 #include "support/gettext.h"
42 #include "support/lstrings.h"
43 #include "support/lyxalgo.h"
46 using namespace lyx::support;
51 InsetLabel::InsetLabel(Buffer * buf, InsetCommandParams const & p)
52 : InsetCommand(buf, p)
56 void InsetLabel::initView()
58 // This seems to be used only for inset creation.
59 // Therefore we do not update refs here, since this would
60 // erroneously change refs from existing duplicate labels
62 updateLabel(getParam("name"));
66 void InsetLabel::uniqueLabel(docstring & label) const
68 docstring const new_label = label;
70 while (buffer().insetLabel(label)) {
71 label = new_label + '-' + convert<docstring>(i);
74 if (label != new_label) {
75 // Warn the user that the label has been changed to something else.
76 frontend::Alert::warning(_("Label names must be unique!"),
77 bformat(_("The label %1$s already exists,\n"
78 "it will be changed to %2$s."), new_label, label));
83 void InsetLabel::updateLabel(docstring const & new_label)
85 docstring label = new_label;
87 setParam("name", label);
91 void InsetLabel::updateLabelAndRefs(docstring const & new_label,
94 docstring const old_label = getParam("name");
95 docstring label = new_label;
97 if (label == old_label)
100 // This handles undo groups automagically
101 UndoGroupHelper ugh(&buffer());
103 cursor->recordUndo();
104 setParam("name", label);
105 updateReferences(old_label, label);
109 void InsetLabel::updateReferences(docstring const & old_label,
110 docstring const & new_label)
113 for (auto const & p: buffer().references(old_label)) {
114 ugh.resetBuffer(p.second.buffer());
115 CursorData(p.second).recordUndo();
116 if (p.first->lyxCode() == MATH_REF_CODE) {
117 InsetMathRef * mi = p.first->asInsetMath()->asRefInset();
118 mi->changeTarget(new_label);
120 InsetCommand * ref = p.first->asInsetCommand();
121 ref->setParam("reference", new_label);
127 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
129 static ParamInfo param_info_;
130 if (param_info_.empty())
131 param_info_.add("name", ParamInfo::LATEX_REQUIRED,
132 ParamInfo::HANDLING_ESCAPE);
137 docstring InsetLabel::screenLabel() const
139 return screen_label_;
143 void InsetLabel::updateBuffer(ParIterator const & par, UpdateType utype)
145 docstring const & label = getParam("name");
146 if (buffer().insetLabel(label)) {
147 // Problem: We already have an InsetLabel with the same name!
148 screen_label_ = _("DUPLICATE: ") + label;
151 buffer().setInsetLabel(label, this);
152 screen_label_ = label;
154 if (utype == OutputUpdate) {
155 // save info on the active counter
156 Counters const & cnts =
157 buffer().masterBuffer()->params().documentClass().counters();
158 active_counter_ = cnts.currentCounter();
159 Language const * lang = par->getParLanguage(buffer().params());
160 if (lang && !active_counter_.empty()) {
161 counter_value_ = cnts.theCounter(active_counter_, lang->code());
162 pretty_counter_ = cnts.prettyCounter(active_counter_, lang->code());
164 counter_value_ = from_ascii("#");
165 pretty_counter_ = from_ascii("#");
171 void InsetLabel::addToToc(DocIterator const & cpit, bool output_active,
172 UpdateType, TocBackend & backend) const
174 docstring const & label = getParam("name");
175 shared_ptr<Toc> toc = backend.toc("label");
176 if (buffer().insetLabel(label) != this) {
177 toc->push_back(TocItem(cpit, 0, screen_label_, output_active));
179 toc->push_back(TocItem(cpit, 0, screen_label_, output_active));
180 for (auto const & p : buffer().references(label)) {
181 DocIterator const ref_pit(p.second);
182 if (p.first->lyxCode() == MATH_REF_CODE)
183 toc->push_back(TocItem(ref_pit, 1,
184 p.first->asInsetMath()->asRefInset()->screenLabel(),
187 toc->push_back(TocItem(ref_pit, 1,
188 static_cast<InsetRef *>(p.first)->getTOCString(),
195 bool InsetLabel::getStatus(Cursor & cur, FuncRequest const & cmd,
196 FuncStatus & status) const
199 switch (cmd.action()) {
200 case LFUN_LABEL_INSERT_AS_REFERENCE:
201 case LFUN_LABEL_COPY_AS_REFERENCE:
204 case LFUN_INSET_MODIFY:
205 if (cmd.getArg(0) == "changetype") {
206 // this is handled by InsetCommand,
207 // but not by InsetLabel.
214 return InsetCommand::getStatus(cur, cmd, status);
217 status.setEnabled(enabled);
222 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
224 switch (cmd.action()) {
226 case LFUN_INSET_MODIFY: {
227 // the only other option here is "changetype", and we
228 // do not have different types.
229 if (cmd.getArg(0) != "label") {
233 InsetCommandParams p(LABEL_CODE);
235 InsetCommand::string2params(to_utf8(cmd.argument()), p);
236 if (p.getCmdName().empty()) {
237 cur.noScreenUpdate();
240 if (p["name"] != params()["name"]) {
241 // undo is handled in updateLabelAndRefs
242 updateLabelAndRefs(p["name"], &cur);
244 cur.forceBufferUpdate();
248 case LFUN_LABEL_COPY_AS_REFERENCE: {
249 InsetCommandParams p(REF_CODE, "ref");
250 p["reference"] = getParam("name");
251 cap::clearSelection();
252 cap::copyInset(cur, new InsetRef(buffer_, p), getParam("name"));
256 case LFUN_LABEL_INSERT_AS_REFERENCE: {
257 InsetCommandParams p(REF_CODE, "ref");
258 p["reference"] = getParam("name");
259 string const data = InsetCommand::params2string(p);
260 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
265 InsetCommand::doDispatch(cur, cmd);
271 int InsetLabel::plaintext(odocstringstream & os,
272 OutputParams const &, size_t) const
274 docstring const str = getParam("name");
275 os << '<' << str << '>';
276 return 2 + str.size();
280 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
282 os << "<!-- anchor id=\""
283 << sgml::cleanID(buffer(), runparams, getParam("name"))
289 docstring InsetLabel::xhtml(XHTMLStream & xs, OutputParams const &) const
292 // Unfortunately, the name attribute has been deprecated, so we have to use
293 // id here to get the document to validate as XHTML 1.1. This will cause a
294 // problem with some browsers, though, I'm sure. (Guess which!) So we will
295 // have to figure out what to do about this later.
296 docstring const attr = "id=\"" + html::cleanAttr(getParam("name")) + '"';
297 xs << html::CompTag("a", to_utf8(attr));