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 bool ambiguous = false;
71 while (buffer().activeLabel(label)) {
72 label = new_label + '-' + convert<docstring>(i);
77 // Warn the user that the label has been changed to something else.
78 frontend::Alert::warning(_("Label names must be unique!"),
79 bformat(_("The label %1$s already exists,\n"
80 "it will be changed to %2$s."), new_label, label));
85 void InsetLabel::updateLabel(docstring const & new_label)
87 docstring label = new_label;
89 setParam("name", label);
93 void InsetLabel::updateLabelAndRefs(docstring const & new_label,
96 docstring const old_label = getParam("name");
97 docstring label = new_label;
99 if (label == old_label)
102 // This handles undo groups automagically
103 UndoGroupHelper ugh(&buffer());
105 cursor->recordUndo();
106 setParam("name", label);
107 updateReferences(old_label, label);
111 void InsetLabel::updateReferences(docstring const & old_label,
112 docstring const & new_label)
115 for (auto const & p: buffer().references(old_label)) {
116 ugh.resetBuffer(p.second.buffer());
117 CursorData(p.second).recordUndo();
118 if (p.first->lyxCode() == MATH_REF_CODE) {
119 InsetMathRef * mi = p.first->asInsetMath()->asRefInset();
120 mi->changeTarget(new_label);
122 InsetCommand * ref = p.first->asInsetCommand();
123 ref->setParam("reference", new_label);
129 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
131 static ParamInfo param_info_;
132 if (param_info_.empty())
133 param_info_.add("name", ParamInfo::LATEX_REQUIRED,
134 ParamInfo::HANDLING_ESCAPE);
139 docstring InsetLabel::screenLabel() const
141 return screen_label_;
145 void InsetLabel::updateBuffer(ParIterator const & par, UpdateType utype)
147 docstring const & label = getParam("name");
149 // Check if this one is deleted (ct)
150 Paragraph const & para = par.paragraph();
151 bool const active = !para.isDeleted(par.pos());
153 if (buffer().activeLabel(label) && active) {
154 // Problem: We already have an active InsetLabel with the same name!
155 screen_label_ = _("DUPLICATE: ") + label;
158 buffer().setInsetLabel(label, this, active);
159 screen_label_ = label;
161 if (utype == OutputUpdate) {
162 // save info on the active counter
163 Counters const & cnts =
164 buffer().masterBuffer()->params().documentClass().counters();
165 active_counter_ = cnts.currentCounter();
166 Language const * lang = par->getParLanguage(buffer().params());
167 if (lang && !active_counter_.empty()) {
168 counter_value_ = cnts.theCounter(active_counter_, lang->code());
169 pretty_counter_ = cnts.prettyCounter(active_counter_, lang->code());
171 counter_value_ = from_ascii("#");
172 pretty_counter_ = from_ascii("#");
178 void InsetLabel::addToToc(DocIterator const & cpit, bool output_active,
179 UpdateType, TocBackend & backend) const
181 docstring const & label = getParam("name");
182 if (!buffer().activeLabel(label))
185 shared_ptr<Toc> toc = backend.toc("label");
186 if (buffer().insetLabel(label, true) != this) {
187 toc->push_back(TocItem(cpit, 0, screen_label_, output_active));
189 toc->push_back(TocItem(cpit, 0, screen_label_, output_active));
190 for (auto const & p : buffer().references(label)) {
191 DocIterator const ref_pit(p.second);
192 if (p.first->lyxCode() == MATH_REF_CODE)
193 toc->push_back(TocItem(ref_pit, 1,
194 p.first->asInsetMath()->asRefInset()->screenLabel(),
197 toc->push_back(TocItem(ref_pit, 1,
198 static_cast<InsetRef *>(p.first)->getTOCString(),
205 bool InsetLabel::getStatus(Cursor & cur, FuncRequest const & cmd,
206 FuncStatus & status) const
209 switch (cmd.action()) {
210 case LFUN_LABEL_INSERT_AS_REFERENCE:
211 case LFUN_LABEL_COPY_AS_REFERENCE:
214 case LFUN_INSET_MODIFY:
215 if (cmd.getArg(0) == "changetype") {
216 // this is handled by InsetCommand,
217 // but not by InsetLabel.
224 return InsetCommand::getStatus(cur, cmd, status);
227 status.setEnabled(enabled);
232 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
234 switch (cmd.action()) {
236 case LFUN_INSET_MODIFY: {
237 // the only other option here is "changetype", and we
238 // do not have different types.
239 if (cmd.getArg(0) != "label") {
243 InsetCommandParams p(LABEL_CODE);
245 InsetCommand::string2params(to_utf8(cmd.argument()), p);
246 if (p.getCmdName().empty()) {
247 cur.noScreenUpdate();
250 if (p["name"] != params()["name"]) {
251 // undo is handled in updateLabelAndRefs
252 updateLabelAndRefs(p["name"], &cur);
254 cur.forceBufferUpdate();
258 case LFUN_LABEL_COPY_AS_REFERENCE: {
259 InsetCommandParams p(REF_CODE, "ref");
260 p["reference"] = getParam("name");
261 cap::clearSelection();
262 cap::copyInset(cur, new InsetRef(buffer_, p), getParam("name"));
266 case LFUN_LABEL_INSERT_AS_REFERENCE: {
267 InsetCommandParams p(REF_CODE, "ref");
268 p["reference"] = getParam("name");
269 string const data = InsetCommand::params2string(p);
270 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
275 InsetCommand::doDispatch(cur, cmd);
281 int InsetLabel::plaintext(odocstringstream & os,
282 OutputParams const &, size_t) const
284 docstring const str = getParam("name");
285 os << '<' << str << '>';
286 return 2 + str.size();
290 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
292 os << "<!-- anchor id=\""
293 << sgml::cleanID(buffer(), runparams, getParam("name"))
299 docstring InsetLabel::xhtml(XHTMLStream & xs, OutputParams const &) const
302 // Unfortunately, the name attribute has been deprecated, so we have to use
303 // id here to get the document to validate as XHTML 1.1. This will cause a
304 // problem with some browsers, though, I'm sure. (Guess which!) So we will
305 // have to figure out what to do about this later.
306 docstring const attr = "id=\"" + html::cleanAttr(getParam("name")) + '"';
307 xs << html::CompTag("a", to_utf8(attr));