]> git.lyx.org Git - lyx.git/blob - src/insets/InsetERT.cpp
Remove unused Counters::copy
[lyx.git] / src / insets / InsetERT.cpp
1 /**
2  * \file InsetERT.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetERT.h"
15
16 #include "Cursor.h"
17 #include "FuncRequest.h"
18 #include "FuncStatus.h"
19 #include "Language.h"
20 #include "Lexer.h"
21 #include "OutputParams.h"
22 #include "xml.h"
23 #include "ParagraphParameters.h"
24 #include "Paragraph.h"
25 #include <output_docbook.h>
26
27 #include "support/docstream.h"
28 #include "support/gettext.h"
29 #include "support/lstrings.h"
30 #include "support/TempFile.h"
31
32 #include <sstream>
33
34 using namespace std;
35 using namespace lyx::support;
36
37 namespace lyx {
38
39 InsetERT::InsetERT(Buffer * buf, CollapseStatus status)
40         : InsetCollapsible(buf)
41 {
42         status_ = status;
43 }
44
45
46 InsetERT::InsetERT(InsetERT const & old)
47         : InsetCollapsible(old)
48 {}
49
50
51 void InsetERT::write(ostream & os) const
52 {
53         os << "ERT" << "\n";
54         InsetCollapsible::write(os);
55 }
56
57
58 int InsetERT::plaintext(odocstringstream & os,
59         OutputParams const & rp, size_t max_length) const
60 {
61         if (!rp.inIndexEntry)
62                 // do not output TeX code
63                 return 0;
64
65         ParagraphList::const_iterator par = paragraphs().begin();
66         ParagraphList::const_iterator end = paragraphs().end();
67
68         while (par != end && os.str().size() <= max_length) {
69                 pos_type siz = par->size();
70                 for (pos_type i = 0; i < siz; ++i) {
71                         char_type const c = par->getChar(i);
72                         // output the active characters
73                         switch (c) {
74                         case '|':
75                         case '!':
76                         case '@':
77                                 os.put(c);
78                                 break;
79                         default:
80                                 break;
81                         }
82                 }
83                 ++par;
84         }
85         return 0;
86 }
87
88
89 void InsetERT::docbook(XMLStream & xs, OutputParams const & runparams) const
90 {
91         auto const begin = paragraphs().begin();
92         auto par = begin;
93         auto const end = paragraphs().end();
94
95         odocstringstream os; // No need for XML handling here.
96
97         // Recreate the logic of makeParagraph in output_docbook.cpp, but much simplified: never open <para>
98         // in an ERT, use simple line breaks.
99         // New line after each paragraph of the ERT, save the last one.
100         while (true) { // For each paragraph in the ERT...
101                 auto pars = par->simpleDocBookOnePar(buffer(), runparams, text().outerFont(distance(begin, par)), 0, false, true);
102                 auto p = pars.begin();
103                 while (true) { // For each line of this ERT paragraph...
104                         os << *p;
105                         ++p;
106                         if (p != pars.end())
107                                 os << "\n";
108                         else
109                                 break;
110                 }
111
112                 ++par;
113                 if (par != end)
114                         os << "\n";
115                 else
116                         break;
117         }
118
119 //      // Implement the special case of \and: split the current item.
120 //      if (os.str() == "\\and" || os.str() == "\\and ") {
121 //              auto lay = getLayout();
122 //      }
123
124         // Output the ERT as a comment with the appropriate escaping.
125         xs << XMLStream::ESCAPE_NONE << "<!-- ";
126         xs << XMLStream::ESCAPE_COMMENTS << os.str();
127         xs << XMLStream::ESCAPE_NONE << " -->";
128 }
129
130
131 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
132 {
133         switch (cmd.action()) {
134         case LFUN_INSET_MODIFY:
135                 if (cmd.getArg(0) == "ert") {
136                         cur.recordUndoInset(this);
137                         setStatus(cur, string2params(to_utf8(cmd.argument())));
138                         break;
139                 }
140                 //fall-through
141         default:
142                 InsetCollapsible::doDispatch(cur, cmd);
143                 break;
144         }
145
146 }
147
148
149 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
150         FuncStatus & status) const
151 {
152         switch (cmd.action()) {
153         case LFUN_INSET_INSERT:
154                 status.setEnabled(false);
155                 return true;
156         case LFUN_INSET_MODIFY:
157                 if (cmd.getArg(0) == "ert") {
158                         status.setEnabled(true);
159                         return true;
160                 }
161                 //fall through
162
163         default:
164                 return InsetCollapsible::getStatus(cur, cmd, status);
165         }
166 }
167
168
169
170 docstring const InsetERT::buttonLabel(BufferView const & bv) const
171 {
172         if (decoration() == InsetLayout::CLASSIC)
173                 return isOpen(bv) ? _("ERT") : getNewLabel(_("ERT"));
174         else
175                 return getNewLabel(_("ERT"));
176 }
177
178
179 InsetCollapsible::CollapseStatus InsetERT::string2params(string const & in)
180 {
181         if (in.empty())
182                 return Collapsed;
183         istringstream data(in);
184         Lexer lex;
185         lex.setStream(data);
186         lex.setContext("InsetERT::string2params");
187         lex >> "ert";
188         int s;
189         lex >> s;
190         return static_cast<CollapseStatus>(s);
191 }
192
193
194 string InsetERT::params2string(CollapseStatus status)
195 {
196         ostringstream data;
197         data << "ert" << ' ' << status;
198         return data.str();
199 }
200
201
202 docstring InsetERT::xhtml(XMLStream &, OutputParams const &) const
203 {
204         return docstring();
205 }
206
207 } // namespace lyx