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 "output_xhtml.h"
27 #include "ParIterator.h"
30 #include "TocBackend.h"
32 #include "frontends/alert.h"
34 #include "support/convert.h"
35 #include "support/gettext.h"
36 #include "support/lstrings.h"
37 #include "support/lyxalgo.h"
40 using namespace lyx::support;
45 InsetLabel::InsetLabel(InsetCommandParams const & p)
46 : InsetCommand(p, "label")
50 void InsetLabel::initView()
52 updateCommand(getParam("name"));
56 void InsetLabel::updateCommand(docstring const & new_label, bool updaterefs)
58 docstring const old_label = getParam("name");
59 docstring label = new_label;
61 while (buffer().insetLabel(label)) {
62 label = new_label + '-' + convert<docstring>(i);
66 if (label != new_label) {
67 // Warn the user that the label has been changed to something else.
68 frontend::Alert::warning(_("Label names must be unique!"),
69 bformat(_("The label %1$s already exists,\n"
70 "it will be changed to %2$s."), new_label, label));
73 buffer().undo().beginUndoGroup();
74 setParam("name", label);
77 Buffer::References & refs = buffer().references(old_label);
78 Buffer::References::iterator it = refs.begin();
79 Buffer::References::iterator end = refs.end();
80 for (; it != end; ++it) {
81 buffer().undo().recordUndo(it->second);
82 it->first->setParam("reference", label);
85 buffer().undo().endUndoGroup();
87 // We need an update of the Buffer reference cache. This is achieved by
89 buffer().updateLabels();
93 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
95 static ParamInfo param_info_;
96 if (param_info_.empty())
97 param_info_.add("name", ParamInfo::LATEX_REQUIRED);
102 docstring InsetLabel::screenLabel() const
104 return screen_label_;
108 void InsetLabel::updateLabels(ParIterator const &)
110 docstring const & label = getParam("name");
111 if (buffer().insetLabel(label)) {
112 // Problem: We already have an InsetLabel with the same name!
113 screen_label_ = _("DUPLICATE: ") + label;
116 buffer().setInsetLabel(label, this);
117 screen_label_ = label;
121 void InsetLabel::addToToc(DocIterator const & cpit)
123 docstring const & label = getParam("name");
124 Toc & toc = buffer().tocBackend().toc("label");
125 if (buffer().insetLabel(label) != this) {
126 toc.push_back(TocItem(cpit, 0, screen_label_));
129 toc.push_back(TocItem(cpit, 0, screen_label_));
130 Buffer::References const & refs = buffer().references(label);
131 Buffer::References::const_iterator it = refs.begin();
132 Buffer::References::const_iterator end = refs.end();
133 for (; it != end; ++it) {
134 DocIterator const ref_pit(it->second);
135 toc.push_back(TocItem(ref_pit, 1, it->first->screenLabel()));
140 bool InsetLabel::getStatus(Cursor & cur, FuncRequest const & cmd,
141 FuncStatus & status) const
144 switch (cmd.action) {
145 case LFUN_LABEL_INSERT_AS_REF:
146 case LFUN_LABEL_COPY_AS_REF:
150 return InsetCommand::getStatus(cur, cmd, status);
153 status.setEnabled(enabled);
158 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
160 switch (cmd.action) {
162 case LFUN_INSET_MODIFY: {
163 InsetCommandParams p(LABEL_CODE);
165 InsetCommand::string2params("label", to_utf8(cmd.argument()), p);
166 if (p.getCmdName().empty()) {
170 if (p["name"] != params()["name"])
171 updateCommand(p["name"]);
175 case LFUN_LABEL_COPY_AS_REF: {
176 InsetCommandParams p(REF_CODE, "ref");
177 p["reference"] = getParam("name");
178 cap::clearSelection();
179 cap::copyInset(cur, new InsetRef(buffer(), p), getParam("name"));
183 case LFUN_LABEL_INSERT_AS_REF: {
184 InsetCommandParams p(REF_CODE, "ref");
185 p["reference"] = getParam("name");
186 string const data = InsetCommand::params2string("ref", p);
187 lyx::dispatch(FuncRequest(LFUN_INSET_INSERT, data));
192 InsetCommand::doDispatch(cur, cmd);
198 int InsetLabel::latex(odocstream & os, OutputParams const &) const
200 os << escape(getCommand());
205 int InsetLabel::plaintext(odocstream & os, OutputParams const &) const
207 docstring const str = getParam("name");
208 os << '<' << str << '>';
209 return 2 + str.size();
213 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
215 os << "<!-- anchor id=\""
216 << sgml::cleanID(buffer(), runparams, getParam("name"))
222 docstring InsetLabel::xhtml(odocstream & os, OutputParams const &) const
224 os << "<a name=\"" << html::htmlize(getParam("name")) << "\"></a>";