]> git.lyx.org Git - lyx.git/blob - src/insets/InsetERT.cpp
Introduce max_length parameter for plaintext() output routines,
[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 "ParagraphParameters.h"
29 #include "Paragraph.h"
30 #include "TextClass.h"
31
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
34
35 #include <sstream>
36
37 using namespace std;
38 using namespace lyx::support;
39
40 namespace lyx {
41
42 InsetERT::InsetERT(Buffer * buf, CollapseStatus status)
43         : InsetCollapsable(buf)
44 {
45         status_ = status;
46 }
47
48
49 void InsetERT::write(ostream & os) const
50 {
51         os << "ERT" << "\n";
52         InsetCollapsable::write(os);
53 }
54
55
56 int InsetERT::plaintext(odocstringstream & os,
57         OutputParams const & rp, size_t max_length) const
58 {
59         if (!rp.inIndexEntry)
60                 // do not output TeX code
61                 return 0;
62
63         ParagraphList::const_iterator par = paragraphs().begin();
64         ParagraphList::const_iterator end = paragraphs().end();
65
66         while (par != end && os.str().size() <= max_length) {
67                 pos_type siz = par->size();
68                 for (pos_type i = 0; i < siz; ++i) {
69                         char_type const c = par->getChar(i);
70                         // output the active characters
71                         switch (c) {
72                         case '|':
73                         case '!':
74                         case '@':
75                                 os.put(c);
76                                 break;
77                         default:
78                                 break;
79                         }
80                 }
81                 ++par;
82         }
83         return 0;
84 }
85
86
87 int InsetERT::docbook(odocstream & os, OutputParams const &) const
88 {
89         // FIXME can we do the same thing here as for LaTeX?
90         ParagraphList::const_iterator par = paragraphs().begin();
91         ParagraphList::const_iterator end = paragraphs().end();
92
93         int lines = 0;
94         while (par != end) {
95                 pos_type siz = par->size();
96                 for (pos_type i = 0; i < siz; ++i)
97                         os.put(par->getChar(i));
98                 ++par;
99                 if (par != end) {
100                         os << "\n";
101                         ++lines;
102                 }
103         }
104
105         return lines;
106 }
107
108
109 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
110 {
111         switch (cmd.action()) {
112         case LFUN_INSET_MODIFY:
113                 if (cmd.getArg(0) == "ert") {
114                         cur.recordUndoInset(ATOMIC_UNDO, this);
115                         setStatus(cur, string2params(to_utf8(cmd.argument())));
116                         break;
117                 }
118                 //fall-through
119         default:
120                 InsetCollapsable::doDispatch(cur, cmd);
121                 break;
122         }
123
124 }
125
126
127 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
128         FuncStatus & status) const
129 {
130         switch (cmd.action()) {
131         case LFUN_INSET_MODIFY:
132                 if (cmd.getArg(0) == "ert") {
133                         status.setEnabled(true);
134                         return true;
135                 }
136                 //fall through
137
138         default:
139                 return InsetCollapsable::getStatus(cur, cmd, status);
140         }
141 }
142
143
144 docstring const InsetERT::buttonLabel(BufferView const & bv) const
145 {
146         if (decoration() == InsetLayout::CLASSIC)
147                 return isOpen(bv) ? _("ERT") : getNewLabel(_("ERT"));
148         else
149                 return getNewLabel(_("ERT"));
150 }
151
152
153 InsetCollapsable::CollapseStatus InsetERT::string2params(string const & in)
154 {
155         if (in.empty())
156                 return Collapsed;
157         istringstream data(in);
158         Lexer lex;
159         lex.setStream(data);
160         lex.setContext("InsetERT::string2params");
161         lex >> "ert";
162         int s;
163         lex >> s;
164         return static_cast<CollapseStatus>(s);
165 }
166
167
168 string InsetERT::params2string(CollapseStatus status)
169 {
170         ostringstream data;
171         data << "ert" << ' ' << status;
172         return data.str();
173 }
174
175
176 docstring InsetERT::xhtml(XHTMLStream &, OutputParams const &) const
177 {
178         return docstring();
179 }
180
181 } // namespace lyx