]> git.lyx.org Git - features.git/blob - src/insets/InsetLabel.cpp
Avoid duplicate labels when the user change an InsetLabel.
[features.git] / src / insets / InsetLabel.cpp
1 /**
2  * \file InsetLabel.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetLabel.h"
14
15 #include "InsetRef.h"
16
17 #include "buffer_funcs.h"
18 #include "Buffer.h"
19 #include "BufferView.h"
20 #include "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "InsetIterator.h"
23 #include "ParIterator.h"
24 #include "sgml.h"
25 #include "Text.h"
26 #include "TocBackend.h"
27
28 #include "frontends/alert.h"
29
30 #include "support/convert.h"
31 #include "support/lyxalgo.h"
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
34
35 using namespace std;
36 using namespace lyx::support;
37
38 namespace lyx {
39
40
41 InsetLabel::InsetLabel(InsetCommandParams const & p)
42         : InsetCommand(p, "label")
43 {}
44
45
46 void InsetLabel::update(docstring const & new_label)
47 {
48         docstring const old_label = getParam("name");
49         if (old_label == new_label)
50                 return;
51
52         docstring label = new_label;
53         int i = 0;
54         while (buffer().insetLabel(label)) {
55                 label = new_label + '-' + convert<docstring>(i);
56                 ++i;
57         }
58         setParam("name", label);
59
60         Buffer::References const & refs = buffer().references(old_label);
61         Buffer::References::const_iterator it = refs.begin();
62         Buffer::References::const_iterator end = refs.end();
63         for (; it != end; ++it)
64                 it->first->setParam("reference", label);
65
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 been changed to %2$s."), new_label, label));
71         }
72         // We need an update of the Buffer reference cache. This is achieved by
73         // updateLabel().
74         lyx::updateLabels(buffer());
75 }
76
77
78 ParamInfo const & InsetLabel::findInfo(string const & /* cmdName */)
79 {
80         static ParamInfo param_info_;
81         if (param_info_.empty()) {
82                 param_info_.add("name", ParamInfo::LATEX_REQUIRED);
83         }
84         return param_info_;
85 }
86
87
88 void InsetLabel::getLabelList(vector<docstring> & list) const
89 {
90         list.push_back(getParam("name"));
91 }
92
93
94 docstring InsetLabel::screenLabel() const
95 {
96         return getParam("name");
97 }
98
99
100 void InsetLabel::updateLabels(ParIterator const & it)
101 {
102         docstring const & label = getParam("name");
103         if (buffer().insetLabel(label))
104                 // Problem: We already have an InsetLabel with the same name!
105                 return;
106         buffer().setInsetLabel(label, this);
107 }
108
109
110 void InsetLabel::addToToc(ParConstIterator const & cpit) const
111 {
112         docstring const & label = getParam("name");
113         Toc & toc = buffer().tocBackend().toc("label");
114         if (buffer().insetLabel(label) != this) {
115                 toc.push_back(TocItem(cpit, 0, _("DUPLICATE: ") + label));
116                 return;
117         }
118         toc.push_back(TocItem(cpit, 0, label));
119         Buffer::References const & refs = buffer().references(label);
120         Buffer::References::const_iterator it = refs.begin();
121         Buffer::References::const_iterator end = refs.end();
122         for (; it != end; ++it) {
123                 ParConstIterator const ref_pit(it->second);
124                 toc.push_back(TocItem(ref_pit, 1, it->first->screenLabel()));
125         }
126 }
127
128
129 void InsetLabel::doDispatch(Cursor & cur, FuncRequest & cmd)
130 {
131         switch (cmd.action) {
132
133         case LFUN_INSET_MODIFY: {
134                 InsetCommandParams p(LABEL_CODE);
135                 // FIXME UNICODE
136                 InsetCommandMailer::string2params("label", to_utf8(cmd.argument()), p);
137                 if (p.getCmdName().empty()) {
138                         cur.noUpdate();
139                         break;
140                 }
141                 update(p["name"]);
142                 break;
143         }
144
145         default:
146                 InsetCommand::doDispatch(cur, cmd);
147                 break;
148         }
149 }
150
151
152 int InsetLabel::latex(odocstream & os, OutputParams const &) const
153 {
154         os << escape(getCommand());
155         return 0;
156 }
157
158
159 int InsetLabel::plaintext(odocstream & os, OutputParams const &) const
160 {
161         docstring const str = getParam("name");
162         os << '<' << str << '>';
163         return 2 + str.size();
164 }
165
166
167 int InsetLabel::docbook(odocstream & os, OutputParams const & runparams) const
168 {
169         os << "<!-- anchor id=\""
170            << sgml::cleanID(buffer(), runparams, getParam("name"))
171            << "\" -->";
172         return 0;
173 }
174
175
176 } // namespace lyx