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"
31 #include "texstream.h"
33 #include "TextClass.h"
34 #include "TocBackend.h"
36 #include "mathed/InsetMathHull.h"
37 #include "mathed/InsetMathRef.h"
39 #include "frontends/alert.h"
41 #include "support/convert.h"
42 #include "support/gettext.h"
43 #include "support/lstrings.h"
44 #include "support/lyxalgo.h"
47 using namespace lyx::support;
52 InsetLabel::InsetLabel(Buffer * buf, InsetCommandParams const & p)
53 : InsetCommand(buf, p)
57 void InsetLabel::initView()
59 // This seems to be used only for inset creation.
60 // Therefore we do not update refs here, since this would
61 // erroneously change refs from existing duplicate labels
63 updateLabel(getParam("name"));
67 void InsetLabel::uniqueLabel(docstring & label) const
69 docstring const new_label = label;
71 bool ambiguous = false;
72 while (buffer().activeLabel(label)) {
73 label = new_label + '-' + convert<docstring>(i);
78 // Warn the user that the label has been changed to something else.
79 frontend::Alert::warning(_("Label names must be unique!"),
80 bformat(_("The label %1$s already exists,\n"
81 "it will be changed to %2$s."), new_label, label));
86 void InsetLabel::updateLabel(docstring const & new_label, bool const active)
88 docstring label = new_label;
91 setParam("name", label);
95 void InsetLabel::updateLabelAndRefs(docstring const & new_label,
98 docstring const old_label = getParam("name");
99 docstring label = new_label;
101 if (label == old_label)
104 // This handles undo groups automagically
105 UndoGroupHelper ugh(&buffer());
107 cursor->recordUndo();
108 setParam("name", label);
109 updateReferences(old_label, label);
113 void InsetLabel::updateReferences(docstring const & old_label,
114 docstring const & new_label)
117 for (auto const & p: buffer().references(old_label)) {
118 ugh.resetBuffer(p.second.buffer());
119 CursorData(p.second).recordUndo();
120 if (p.first->lyxCode() == MATH_REF_CODE) {
121 InsetMathRef * mi = p.first->asInsetMath()->asRefInset();
122 mi->changeTarget(new_label);
124 InsetCommand * ref = p.first->asInsetCommand();
125 ref->setParam("reference", new_label);
131 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
133 static ParamInfo param_info_;
134 if (param_info_.empty())
135 param_info_.add("name", ParamInfo::LATEX_REQUIRED,
136 ParamInfo::HANDLING_ESCAPE);
141 docstring InsetLabel::screenLabel() const
143 return screen_label_;
147 void InsetLabel::updateBuffer(ParIterator const & par, UpdateType utype)
149 docstring const & label = getParam("name");
151 // Check if this one is active (i.e., neither deleted with change-tracking
152 // nor in an inset that does not produce output, such as notes or inactive branches)
153 Paragraph const & para = par.paragraph();
154 bool active = !para.isDeleted(par.pos()) && para.inInset().producesOutput();
155 // If not, check whether we are in a deleted/non-outputting inset
157 for (size_type sl = 0 ; sl < par.depth() ; ++sl) {
158 Paragraph const & outer_par = par[sl].paragraph();
159 if (outer_par.isDeleted(par[sl].pos())
160 || !outer_par.inInset().producesOutput()) {
167 if (buffer().activeLabel(label) && active) {
168 // Problem: We already have an active InsetLabel with the same name!
169 screen_label_ = _("DUPLICATE: ") + label;
172 buffer().setInsetLabel(label, this, active);
173 screen_label_ = label;
175 if (utype == OutputUpdate) {
176 // save info on the active counter
177 Counters const & cnts =
178 buffer().masterBuffer()->params().documentClass().counters();
179 active_counter_ = cnts.currentCounter();
180 Language const * lang = par->getParLanguage(buffer().params());
181 if (lang && !active_counter_.empty()) {
182 counter_value_ = cnts.theCounter(active_counter_, lang->code());
183 pretty_counter_ = cnts.prettyCounter(active_counter_, lang->code());
185 counter_value_ = from_ascii("#");
186 pretty_counter_ = from_ascii("#");
192 void InsetLabel::addToToc(DocIterator const & cpit, bool output_active,
193 UpdateType, TocBackend & backend) const
195 docstring const & label = getParam("name");
197 // inactive labels get a cross mark
198 if (buffer().insetLabel(label, true) != this)
199 output_active = false;
201 // We put both active and inactive labels to the outliner
202 shared_ptr<Toc> toc = backend.toc("label");
203 toc->push_back(TocItem(cpit, 0, screen_label_, output_active));
204 // The refs get assigned only to the active label. If no active one exists,
205 // assign the (BROKEN) refs to the first inactive one.
206 if (buffer().insetLabel(label, true) == this || !buffer().activeLabel(label)) {
207 for (auto const & p : buffer().references(label)) {
208 DocIterator const ref_pit(p.second);
209 if (p.first->lyxCode() == MATH_REF_CODE)
210 toc->push_back(TocItem(ref_pit, 1,
211 p.first->asInsetMath()->asRefInset()->screenLabel(),
214 toc->push_back(TocItem(ref_pit, 1,
215 static_cast<InsetRef *>(p.first)->getTOCString(),
222 bool InsetLabel::getStatus(Cursor & cur, FuncRequest const & cmd,
223 FuncStatus & status) const
226 switch (cmd.action()) {
227 case LFUN_LABEL_INSERT_AS_REFERENCE:
228 case LFUN_LABEL_COPY_AS_REFERENCE:
231 case LFUN_INSET_MODIFY:
232 if (cmd.getArg(0) == "changetype") {
233 // this is handled by InsetCommand,
234 // but not by InsetLabel.
241 return InsetCommand::getStatus(cur, cmd, status);
244 status.setEnabled(enabled);
249 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
251 switch (cmd.action()) {
253 case LFUN_INSET_MODIFY: {
254 // the only other option here is "changetype", and we
255 // do not have different types.
256 if (cmd.getArg(0) != "label") {
260 InsetCommandParams p(LABEL_CODE);
262 InsetCommand::string2params(to_utf8(cmd.argument()), p);
263 if (p.getCmdName().empty()) {
264 cur.noScreenUpdate();
267 if (p["name"] != params()["name"]) {
268 // undo is handled in updateLabelAndRefs
269 updateLabelAndRefs(p["name"], &cur);
271 cur.forceBufferUpdate();
275 case LFUN_LABEL_COPY_AS_REFERENCE: {
276 InsetCommandParams p(REF_CODE, "ref");
277 p["reference"] = getParam("name");
278 cap::clearSelection();
279 cap::copyInset(cur, new InsetRef(buffer_, p), getParam("name"));
283 case LFUN_LABEL_INSERT_AS_REFERENCE: {
284 InsetCommandParams p(REF_CODE, "ref");
285 p["reference"] = getParam("name");
286 string const data = InsetCommand::params2string(p);
287 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
292 InsetCommand::doDispatch(cur, cmd);
298 void InsetLabel::latex(otexstream & os, OutputParams const & runparams_in) const
300 OutputParams runparams = runparams_in;
301 docstring command = getCommand(runparams);
302 // In macros with moving arguments, such as \section,
303 // we store the label and output it after the macro (#2154)
304 if (runparams_in.postpone_fragile_stuff)
305 runparams_in.post_macro += command;
307 if (runparams.moving_arg)
314 int InsetLabel::plaintext(odocstringstream & os,
315 OutputParams const &, size_t) const
317 docstring const str = getParam("name");
318 os << '<' << str << '>';
319 return 2 + str.size();
323 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
325 os << "<!-- anchor id=\""
326 << sgml::cleanID(buffer(), runparams, getParam("name"))
332 docstring InsetLabel::xhtml(XHTMLStream & xs, OutputParams const &) const
335 // Unfortunately, the name attribute has been deprecated, so we have to use
336 // id here to get the document to validate as XHTML 1.1. This will cause a
337 // problem with some browsers, though, I'm sure. (Guess which!) So we will
338 // have to figure out what to do about this later.
339 docstring const attr = "id=\"" + html::cleanAttr(getParam("name")) + '"';
340 xs << html::CompTag("a", to_utf8(attr));