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