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