]> git.lyx.org Git - lyx.git/blob - src/insets/insetlabel.C
9c2ba9e99480e3c78e022f77f16335e27fcc8466
[lyx.git] / src / insets / insetlabel.C
1 /**
2  * \file insetlabel.C
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 "buffer.h"
16 #include "BufferView.h"
17 #include "dispatchresult.h"
18 #include "funcrequest.h"
19 #include "InsetList.h"
20 #include "lyxtext.h"
21 #include "paragraph.h"
22 #include "pariterator.h"
23 #include "sgml.h"
24
25 #include "frontends/LyXView.h"
26
27 #include "support/lstrings.h"
28 #include "support/lyxalgo.h"
29 #include "support/std_ostream.h"
30
31 using lyx::support::escape;
32
33 using std::string;
34 using std::ostream;
35 using std::vector;
36
37
38 InsetLabel::InsetLabel(InsetCommandParams const & p)
39         : InsetCommand(p, "label")
40 {}
41
42
43 std::auto_ptr<InsetBase> InsetLabel::doClone() const
44 {
45         return std::auto_ptr<InsetBase>(new InsetLabel(params()));
46 }
47
48
49 void InsetLabel::getLabelList(Buffer const &, std::vector<string> & list) const
50 {
51         list.push_back(getContents());
52 }
53
54
55 string const InsetLabel::getScreenLabel(Buffer const &) const
56 {
57         return getContents();
58 }
59
60
61 namespace {
62
63 void changeRefsIfUnique(BufferView & bv, string const & from, string const & to)
64 {
65         // Check if the label 'from' appears more than once
66         vector<string> labels;
67         bv.buffer()->getLabelList(labels);
68
69         if (lyx::count(labels.begin(), labels.end(), from) > 1)
70                 return;
71
72         InsetBase::Code code = InsetBase::REF_CODE;
73
74         ParIterator it = bv.buffer()->par_iterator_begin();
75         ParIterator end = bv.buffer()->par_iterator_end();
76         for ( ; it != end; ++it) {
77                 for (InsetList::iterator it2 = it->insetlist.begin();
78                      it2 != it->insetlist.end(); ++it2) {
79                         if (it2->inset->lyxCode() == code) {
80                                 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
81                                 if (inset->getContents() == from) {
82                                         inset->setContents(to);
83                                 }
84                         }
85                 }
86         }
87 }
88
89 } // namespace anon
90
91
92 void InsetLabel::doDispatch(LCursor & cur, FuncRequest & cmd)
93 {
94         switch (cmd.action) {
95
96         case LFUN_INSET_MODIFY: {
97                 InsetCommandParams p;
98                 InsetCommandMailer::string2params("label", cmd.argument, p);
99                 if (p.getCmdName().empty()) {
100                         cur.noUpdate();
101                         break;
102                 }
103                 if (p.getContents() != params().getContents())
104                         changeRefsIfUnique(cur.bv(), params().getContents(),
105                                                        p.getContents());
106                 setParams(p);
107                 break;
108         }
109
110         default:
111                 InsetCommand::doDispatch(cur, cmd);
112                 break;
113         }
114 }
115
116
117 int InsetLabel::latex(Buffer const &, ostream & os,
118                       OutputParams const &) const
119 {
120         os << escape(getCommand());
121         return 0;
122 }
123
124
125 int InsetLabel::plaintext(Buffer const &, ostream & os,
126                       OutputParams const &) const
127 {
128         os << '<' << getContents()  << '>';
129         return 0;
130 }
131
132
133 int InsetLabel::linuxdoc(Buffer const & buf, ostream & os,
134                          OutputParams const & runparams) const
135 {
136         os << "<label id=\"" << sgml::cleanID(buf, runparams, getContents()) << "\" >";
137         return 0;
138 }
139
140
141 int InsetLabel::docbook(Buffer const & buf, ostream & os,
142                         OutputParams const & runparams) const
143 {
144         os << "<!-- anchor id=\"" << sgml::cleanID(buf, runparams, getContents()) << "\" -->";
145         return 0;
146 }