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 "frontends/alert.h"
37 #include "support/convert.h"
38 #include "support/gettext.h"
39 #include "support/lstrings.h"
40 #include "support/lyxalgo.h"
43 using namespace lyx::support;
48 InsetLabel::InsetLabel(Buffer * buf, InsetCommandParams const & p)
49 : InsetCommand(buf, p, "label")
53 void InsetLabel::initView()
55 updateCommand(getParam("name"));
59 void InsetLabel::updateCommand(docstring const & new_label, bool updaterefs)
61 docstring const old_label = getParam("name");
62 docstring label = new_label;
64 while (buffer().insetLabel(label)) {
65 label = new_label + '-' + convert<docstring>(i);
69 if (label != new_label) {
70 // Warn the user that the label has been changed to something else.
71 frontend::Alert::warning(_("Label names must be unique!"),
72 bformat(_("The label %1$s already exists,\n"
73 "it will be changed to %2$s."), new_label, label));
76 buffer().undo().beginUndoGroup();
77 setParam("name", label);
80 Buffer::References & refs = buffer().references(old_label);
81 Buffer::References::iterator it = refs.begin();
82 Buffer::References::iterator end = refs.end();
83 for (; it != end; ++it) {
84 buffer().undo().recordUndo(it->second);
85 it->first->setParam("reference", label);
88 buffer().undo().endUndoGroup();
90 // We need an update of the Buffer reference cache. This is achieved by
92 buffer().updateLabels();
96 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
98 static ParamInfo param_info_;
99 if (param_info_.empty())
100 param_info_.add("name", ParamInfo::LATEX_REQUIRED);
105 docstring InsetLabel::screenLabel() const
107 return screen_label_;
111 void InsetLabel::updateLabels(ParIterator const & par, bool out)
113 docstring const & label = getParam("name");
114 if (buffer().insetLabel(label)) {
115 // Problem: We already have an InsetLabel with the same name!
116 screen_label_ = _("DUPLICATE: ") + label;
119 buffer().setInsetLabel(label, this);
120 screen_label_ = label;
123 // save info on the active counter
124 Counters const & cnts =
125 buffer().masterBuffer()->params().documentClass().counters();
126 active_counter_ = cnts.currentCounter();
127 Language const * lang = par->getParLanguage(buffer().params());
128 if (lang && !active_counter_.empty())
129 counter_value_ = cnts.theCounter(active_counter_, lang->code());
131 counter_value_ = _("(unknown)");
136 void InsetLabel::addToToc(DocIterator const & cpit)
138 docstring const & label = getParam("name");
139 Toc & toc = buffer().tocBackend().toc("label");
140 if (buffer().insetLabel(label) != this) {
141 toc.push_back(TocItem(cpit, 0, screen_label_));
144 toc.push_back(TocItem(cpit, 0, screen_label_));
145 Buffer::References const & refs = buffer().references(label);
146 Buffer::References::const_iterator it = refs.begin();
147 Buffer::References::const_iterator end = refs.end();
148 for (; it != end; ++it) {
149 DocIterator const ref_pit(it->second);
150 toc.push_back(TocItem(ref_pit, 1, it->first->screenLabel()));
155 bool InsetLabel::getStatus(Cursor & cur, FuncRequest const & cmd,
156 FuncStatus & status) const
159 switch (cmd.action) {
160 case LFUN_LABEL_INSERT_AS_REF:
161 case LFUN_LABEL_COPY_AS_REF:
165 return InsetCommand::getStatus(cur, cmd, status);
168 status.setEnabled(enabled);
173 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
175 switch (cmd.action) {
177 case LFUN_INSET_MODIFY: {
178 InsetCommandParams p(LABEL_CODE);
180 InsetCommand::string2params("label", to_utf8(cmd.argument()), p);
181 if (p.getCmdName().empty()) {
185 if (p["name"] != params()["name"])
186 updateCommand(p["name"]);
190 case LFUN_LABEL_COPY_AS_REF: {
191 InsetCommandParams p(REF_CODE, "ref");
192 p["reference"] = getParam("name");
193 cap::clearSelection();
194 cap::copyInset(cur, new InsetRef(buffer_, p), getParam("name"));
198 case LFUN_LABEL_INSERT_AS_REF: {
199 InsetCommandParams p(REF_CODE, "ref");
200 p["reference"] = getParam("name");
201 string const data = InsetCommand::params2string("ref", p);
202 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
207 InsetCommand::doDispatch(cur, cmd);
213 int InsetLabel::latex(odocstream & os, OutputParams const &) const
215 os << escape(getCommand());
220 int InsetLabel::plaintext(odocstream & os, OutputParams const &) const
222 docstring const str = getParam("name");
223 os << '<' << str << '>';
224 return 2 + str.size();
228 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
230 os << "<!-- anchor id=\""
231 << sgml::cleanID(buffer(), runparams, getParam("name"))
237 docstring InsetLabel::xhtml(XHTMLStream & xs, OutputParams const &) const
240 // Unfortunately, the name attribute has been deprecated, so we have to use
241 // id here to get the document to validate as XHTML 1.1. This will cause a
242 // problem with some browsers, though, I'm sure. (Guess which!) So we will
243 // have to figure out what to do about this later.
244 string const attr = "id=\"" + html::cleanAttr(to_utf8(getParam("name"))) + "\"";
245 xs << html::CompTag("a", attr);