]> git.lyx.org Git - lyx.git/blob - src/texstream.h
Account for old versions of Pygments
[lyx.git] / src / texstream.h
1 // -*- C++ -*-
2 /**
3  * \file texstream.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Enrico Forestieri
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef LYX_TEXSTREAM_H
13 #define LYX_TEXSTREAM_H
14
15 #include "support/docstream.h"
16 #include "support/unique_ptr.h"
17
18 namespace lyx {
19
20 class TexRow;
21 struct TexString;
22
23
24 /** Wrapper class for odocstream.
25     This class is used to automatically count the lines of the exported latex
26     code.
27   */
28
29 class otexrowstream {
30 public:
31         ///
32         explicit otexrowstream(odocstream & os);
33         /// defaulted
34         ~otexrowstream();
35         ///
36         odocstream & os() { return os_; }
37         ///
38         TexRow & texrow() { return *texrow_; }
39         ///
40         unique_ptr<TexRow> releaseTexRow();
41         ///
42         void put(char_type const & c);
43         ///
44         void append(TexString ts);
45 private:
46         ///
47         odocstream & os_;
48         ///
49         unique_ptr<TexRow> texrow_;
50 };
51
52 ///
53 otexrowstream & operator<<(otexrowstream &, odocstream_manip);
54 ///
55 otexrowstream & operator<<(otexrowstream &, TexString);
56 ///
57 otexrowstream & operator<<(otexrowstream &, docstring const &);
58 ///
59 otexrowstream & operator<<(otexrowstream &, std::string const &);
60 ///
61 otexrowstream & operator<<(otexrowstream &, char const *);
62 ///
63 otexrowstream & operator<<(otexrowstream &, char);
64 ///
65 template <typename Type>
66 otexrowstream & operator<<(otexrowstream & ots, Type value);
67
68
69 /** Subclass for otexrowstream.
70     This class is used to ensure that no blank lines may be inadvertently output.
71     To this end, use the special variables "breakln" and "safebreakln" as if
72     they were iomanip's to ensure that the next output will start at the
73     beginning of a line. Using "breakln", a '\n' char will be output if needed,
74     while using "safebreakln", "%\n" will be output if needed.
75     The class also records the last output character and can tell whether
76     a paragraph break was just output.
77   */
78
79 class otexstream : public otexrowstream {
80 public:
81         ///
82         explicit otexstream(odocstream & os)
83                 : otexrowstream(os), canbreakline_(false),
84                   protectspace_(false), terminate_command_(false),
85                   parbreak_(true), lastchar_(0) {}
86         ///
87         void put(char_type const & c);
88         ///
89         void append(TexString ts);
90         ///
91         void canBreakLine(bool breakline) { canbreakline_ = breakline; }
92         ///
93         bool canBreakLine() const { return canbreakline_; }
94         ///
95         void protectSpace(bool protectspace) { protectspace_ = protectspace; }
96         ///
97         bool protectSpace() const { return protectspace_; }
98         ///
99         void terminateCommand(bool terminate) { terminate_command_ = terminate; }
100         ///
101         bool terminateCommand() const { return terminate_command_; }
102         ///
103         void lastChar(char_type const & c)
104         {
105                 parbreak_ = (!canbreakline_ && c == '\n');
106                 canbreakline_ = (c != '\n');
107                 lastchar_ = c;
108         }
109         ///
110         char_type lastChar() const { return lastchar_; }
111         ///
112         bool afterParbreak() const { return parbreak_; }
113 private:
114         ///
115         bool canbreakline_;
116         ///
117         bool protectspace_;
118         ///
119         bool terminate_command_;
120         ///
121         bool parbreak_;
122         ///
123         char_type lastchar_;
124 };
125
126
127 /// because we need to pass ods_ to the base class
128 struct otexstringstream_helper { odocstringstream ods_; };
129
130 /// otexstringstream : a odocstringstream with tex/row correspondence
131 class otexstringstream : otexstringstream_helper, public otexstream {
132 public:
133         otexstringstream() : otexstringstream_helper(), otexstream(ods_) {}
134         ///
135         docstring str() const { return ods_.str(); }
136         ///
137         size_t length();
138         ///
139         bool empty() { return 0 == length(); }
140         /// move-returns the contents and reset the texstream
141         TexString release();
142 };
143
144
145 /// Helper structs for breaking a line
146 struct BreakLine {
147         char n;
148 };
149
150 struct SafeBreakLine {
151         char n;
152 };
153
154 /// Helper structs for terminating a command
155 struct TerminateCommand {
156         char n;
157 };
158
159 extern BreakLine breakln;
160 extern SafeBreakLine safebreakln;
161 extern TerminateCommand termcmd;
162
163 ///
164 otexstream & operator<<(otexstream &, BreakLine);
165 ///
166 otexstream & operator<<(otexstream &, SafeBreakLine);
167 ///
168 otexstream & operator<<(otexstream &, TerminateCommand);
169 ///
170 otexstream & operator<<(otexstream &, odocstream_manip);
171 ///
172 otexstream & operator<<(otexstream &, TexString);
173 ///
174 otexstream & operator<<(otexstream &, docstring const &);
175 ///
176 otexstream & operator<<(otexstream &, std::string const &);
177 ///
178 otexstream & operator<<(otexstream &, char const *);
179 ///
180 otexstream & operator<<(otexstream &, char);
181 ///
182 template <typename Type>
183 otexstream & operator<<(otexstream & ots, Type value);
184
185
186 }
187
188 #endif