]> git.lyx.org Git - lyx.git/blob - src/mathed/MathStream.h
Correctly terminate a user math macro in certain circumstances
[lyx.git] / src / mathed / MathStream.h
1 // -*- C++ -*-
2 /**
3  * \file MathStream.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef MATH_MATHMLSTREAM_H
13 #define MATH_MATHMLSTREAM_H
14
15 #include "InsetMath.h"
16
17 #include "TexRow.h"
18 #include "texstream.h"
19
20 #include "support/Changer.h"
21 #include "support/strfwd.h"
22
23
24 namespace lyx {
25
26 class Encoding;
27 class MathAtom;
28 class MathData;
29
30 //
31 // LaTeX/LyX
32 //
33
34 class TeXMathStream {
35 public:
36         ///
37         enum OutputType {
38                 wsDefault,
39                 wsDryrun,
40                 wsPreview
41         };
42         ///
43         enum UlemCmdType {
44                 NONE,
45                 UNDERLINE,
46                 STRIKEOUT
47         };
48         ///
49         explicit TeXMathStream(otexrowstream & os, bool fragile = false,
50                                bool latex = false, OutputType output = wsDefault,
51                                Encoding const * encoding = nullptr);
52         ///
53         ~TeXMathStream();
54         ///
55         int line() const { return line_; }
56         ///
57         bool fragile() const { return fragile_; }
58         ///
59         bool latex() const { return latex_; }
60         ///
61         OutputType output() const { return output_; }
62         ///
63         otexrowstream & os() { return os_; }
64         ///
65         TexRow & texrow() { return os_.texrow(); }
66         ///
67         bool & firstitem() { return firstitem_; }
68         ///
69         void addlines(unsigned int);
70         /// record whether we can write an immediately following newline char
71         void canBreakLine(bool breakline) { canbreakline_ = breakline; }
72         /// tell whether we can write an immediately following newline char
73         bool canBreakLine() const { return canbreakline_; }
74         /// record whether we have to take care for striking out display math
75         void strikeoutMath(bool mathsout) { mathsout_ = mathsout; }
76         /// tell whether we have to take care for striking out display math
77         bool strikeoutMath() const { return mathsout_; }
78         /// record which ulem command type we are inside
79         void ulemCmd(UlemCmdType ulemcmd) { ulemcmd_ = ulemcmd; }
80         /// tell which ulem command type we are inside
81         UlemCmdType ulemCmd() const { return ulemcmd_; }
82         /// writes space if next thing is isalpha()
83         void pendingSpace(bool space);
84         /// writes space if next thing is isalpha()
85         bool pendingSpace() const { return pendingspace_; }
86         /// write braces if a space is pending and next char is [
87         void useBraces(bool braces);
88         /// write braces if a space is pending and next char is [
89         bool useBraces() const { return usebraces_; }
90         /// tell whether to write the closing brace of \ensuremath
91         void pendingBrace(bool brace);
92         /// tell whether to write the closing brace of \ensuremath
93         bool pendingBrace() const { return pendingbrace_; }
94         /// tell whether we are in text mode or not when producing latex code
95         void textMode(bool textmode);
96         /// tell whether we are in text mode or not when producing latex code
97         bool textMode() const { return textmode_; }
98         /// tell whether we are allowed to switch mode when producing latex code
99         void lockedMode(bool locked);
100         /// tell whether we are allowed to switch mode when producing latex code
101         bool lockedMode() const { return locked_; }
102         /// tell whether to use only ascii chars when producing latex code
103         void asciiOnly(bool ascii);
104         /// tell whether to use only ascii chars when producing latex code
105         bool asciiOnly() const { return ascii_; }
106         /// tell whether we are in a MathClass inset
107         void inMathClass(bool mathclass) { mathclass_ = mathclass; };
108         /// tell whether we are in a MathClass inset
109         bool inMathClass() const { return mathclass_; }
110         /// LaTeX encoding
111         Encoding const * encoding() const { return encoding_; }
112
113         /// Temporarily change the TexRow information about the outer row entry.
114         Changer changeRowEntry(TexRow::RowEntry entry);
115         /// TexRow::starts the innermost outer math inset
116         /// returns true if the outer row entry will appear at this line
117         bool startOuterRow();
118 private:
119         ///
120         otexrowstream & os_;
121         /// do we have to write \\protect sometimes
122         bool fragile_ = false;
123         /// are we at the beginning of an MathData?
124         bool firstitem_ = false;
125         /// are we writing to .tex?
126         int latex_ = false;
127         /// output type (default, source preview, instant preview)?
128         OutputType output_ = wsDefault;
129         /// do we have a space pending?
130         bool pendingspace_ = false;
131         /// do we have to write braces when a space is pending and [ follows?
132         bool usebraces_ = false;
133         /// do we have a brace pending?
134         bool pendingbrace_ = false;
135         /// are we in text mode when producing latex code?
136         bool textmode_ = false;
137         /// are we allowed to switch mode when producing latex code?
138         bool locked_ = false;
139         /// should we use only ascii chars when producing latex code?
140         bool ascii_ = false;
141         /// are we allowed to output an immediately following newline?
142         bool canbreakline_ = true;
143         /// should we take care for striking out display math?
144         bool mathsout_ = false;
145         /// what ulem command are we inside (none, underline, strikeout)?
146         UlemCmdType ulemcmd_ = NONE;
147         ///
148         int line_ = 0;
149         ///
150         Encoding const * encoding_ = nullptr;
151         /// Row entry we are in
152         TexRow::RowEntry row_entry_ = TexRow::row_none;
153         /// whether we are in a MathClass inset
154         bool mathclass_ = false;
155 };
156
157 ///
158 TeXMathStream & operator<<(TeXMathStream &, MathAtom const &);
159 ///
160 TeXMathStream & operator<<(TeXMathStream &, MathData const &);
161 ///
162 TeXMathStream & operator<<(TeXMathStream &, docstring const &);
163 ///
164 TeXMathStream & operator<<(TeXMathStream &, char const * const);
165 ///
166 TeXMathStream & operator<<(TeXMathStream &, char);
167 ///
168 TeXMathStream & operator<<(TeXMathStream &, int);
169 ///
170 TeXMathStream & operator<<(TeXMathStream &, unsigned int);
171
172 /// ensure correct mode, possibly by opening \ensuremath or \lyxmathsym
173 bool ensureMath(TeXMathStream & os, bool needs_mathmode = true,
174                 bool macro = false, bool textmode_macro = false);
175
176 /// ensure the requested mode, possibly by closing \ensuremath or \lyxmathsym
177 int ensureMode(TeXMathStream & os, InsetMath::mode_type mode, bool locked, bool ascii);
178
179
180 /**
181  * MathEnsurer - utility class for ensuring math mode
182  *
183  * A local variable of this type can be used to either ensure math mode
184  * or delay the writing of a pending brace when outputting LaTeX.
185  * A LyX InsetMathMacro is always assumed needing a math mode environment, while
186  * no assumption is made for macros defined through \newcommand or \def.
187  *
188  * Example 1:
189  *
190  *      MathEnsurer ensurer(os);
191  *
192  * If not already in math mode, inserts an \ensuremath command followed
193  * by an open brace. This brace will be automatically closed when exiting
194  * math mode. Math mode is automatically exited when writing something
195  * that doesn't explicitly require math mode.
196  *
197  * Example 2:
198  *
199  *      MathEnsurer ensurer(os, false);
200  *
201  * Simply suspend writing a closing brace until the end of ensurer's scope.
202  *
203  * Example 3:
204  *
205  *      MathEnsurer ensurer(os, needs_mathmode, true, textmode_macro);
206  *
207  * This form is mainly used for math macros as they are treated specially.
208  * In essence, the macros defined in the lib/symbols file and tagged as
209  * textmode will be enclosed in \lyxmathsym if they appear in a math mode
210  * environment, while macros defined in the preamble or ERT are left as is.
211  * The third parameter must be set to true and the fourth parameter has also
212  * to be specified. Only the following 3 different cases are handled.
213  *
214  * When the needs_mathmode parameter is true the behavior is as in Example 1.
215  * This is the case for a LyX InsetMathMacro or a macro not tagged as textmode.
216  *
217  * When the needs_mathmode and textmode_macro parameters are both false the
218  * macro is left in the same (text or math mode) environment it was entered.
219  * This is because it is assumed that the macro was designed for that mode
220  * and we have no way to tell the contrary.
221  * This is the case for macros defined by using \newcommand or \def in ERT.
222  *
223  * When the needs_mathmode parameter is false while textmode_macro is true the
224  * macro will be enclosed in \lyxmathsym if it appears in a math mode environment.
225  * This is the case for the macros tagged as textmode in lib/symbols.
226  */
227 class MathEnsurer
228 {
229 public:
230         ///
231         explicit MathEnsurer(TeXMathStream & os, bool needs_mathmode = true,
232                              bool macro = false, bool textmode_macro = false)
233                 : os_(os), brace_(ensureMath(os, needs_mathmode, macro, textmode_macro)) {}
234         ///
235         ~MathEnsurer() { os_.pendingBrace(brace_); }
236 private:
237         ///
238         TeXMathStream & os_;
239         ///
240         bool brace_;
241 };
242
243
244 /**
245  * ModeSpecifier - utility class for specifying a given mode (math or text)
246  *
247  * A local variable of this type can be used to specify that a command or
248  * environment works in a given mode. For example, \mbox works in text
249  * mode, but \boxed works in math mode. Note that no mode changing commands
250  * are needed, but we have to track the current mode, hence this class.
251  * This is only used when exporting to latex and helps determining whether
252  * the mode needs being temporarily switched when a command would not work
253  * in the current mode. As there are cases where this switching is to be
254  * avoided, the optional third parameter can be used to lock the mode.
255  * When the mode is locked, the optional fourth parameter specifies whether
256  * strings are to be output by using a suitable ascii representation.
257  *
258  * Example 1:
259  *
260  *      ModeSpecifier specifier(os, TEXT_MODE);
261  *
262  * Sets the current mode to text mode and allows mode switching.
263  *
264  * Example 2:
265  *
266  *      ModeSpecifier specifier(os, TEXT_MODE, true);
267  *
268  * Sets the current mode to text mode and disallows mode switching.
269  *
270  * Example 3:
271  *
272  *      ModeSpecifier specifier(os, TEXT_MODE, true, true);
273  *
274  * Sets the current mode to text mode, disallows mode switching, and outputs
275  * strings as ascii only.
276  *
277  * At the end of specifier's scope the mode is reset to its previous value.
278  */
279 class ModeSpecifier
280 {
281 public:
282         ///
283         explicit ModeSpecifier(TeXMathStream & os, InsetMath::mode_type mode,
284                                bool locked = false, bool ascii = false)
285                 : os_(os), oldmodes_(ensureMode(os, mode, locked, ascii)) {}
286         ///
287         ~ModeSpecifier()
288         {
289                 os_.textMode(oldmodes_ & 0x01);
290                 os_.lockedMode(oldmodes_ & 0x02);
291                 os_.asciiOnly(oldmodes_ & 0x04);
292         }
293 private:
294         ///
295         TeXMathStream & os_;
296         ///
297         int oldmodes_;
298 };
299
300
301
302 //
303 //  MathML
304 //
305
306
307 /// Start tag.
308 class MTag {
309 public:
310         ///
311         MTag(char const * const tag, std::string const & attr = std::string())
312                 : tag_(tag), attr_(attr) {}
313         ///
314         char const * const tag_;
315         ///
316         std::string attr_;
317 };
318
319
320 /// End tag.
321 class ETag {
322 public:
323         ///
324         explicit ETag(char const * const tag) : tag_(tag) {}
325         ///
326         char const * const tag_;
327 };
328
329
330 /// Compound tag (no content, directly closed).
331 class CTag {
332 public:
333         ///
334         CTag(char const * const tag, std::string const & attr = "")
335             : tag_(tag), attr_(attr) {}
336         ///
337         char const * const tag_;
338     ///
339     std::string attr_;
340 };
341
342
343 /// Throw MathExportException to signal that the attempt to export
344 /// some math in the current format did not succeed. E.g., we can't
345 /// export xymatrix as MathML, so that will throw, and we'll fall back
346 /// to images.
347 class MathExportException : public std::exception {};
348
349
350 class MathMLStream {
351 public:
352         /// Builds a stream proxy for os; the MathML namespace is given by xmlns (supposed to be already defined elsewhere in the document).
353         explicit MathMLStream(odocstream & os, std::string const & xmlns = "", bool xmlMode = false);
354         ///
355         void cr();
356         ///
357         odocstream & os() { return os_; }
358         ///
359         int line() const { return line_; }
360         ///
361         int & tab() { return tab_; }
362         ///
363         friend MathMLStream & operator<<(MathMLStream &, char const *);
364         ///
365         void defer(docstring const &);
366         ///
367         void defer(std::string const &);
368         ///
369         docstring deferred() const;
370         ///
371         bool inText() const { return in_text_; }
372         ///
373         std::string xmlns() const { return xmlns_; }
374         ///
375         bool xmlMode() const { return xml_mode_; }
376         /// Returns the tag name prefixed by the name space if needed.
377         std::string namespacedTag(std::string const & tag) const {
378                 return (xmlns().empty() ? "" : xmlns() + ":") + tag;
379         }
380 private:
381         ///
382         void setTextMode(bool t) { in_text_ = t; }
383         ///
384         odocstream & os_;
385         ///
386         int tab_;
387         ///
388         int line_;
389         ///
390         bool in_text_;
391         ///
392         odocstringstream deferred_;
393         ///
394         std::string xmlns_;
395         ///
396         bool xml_mode_;
397         ///
398         friend class SetMode;
399 };
400
401 ///
402 MathMLStream & operator<<(MathMLStream &, MathAtom const &);
403 ///
404 MathMLStream & operator<<(MathMLStream &, MathData const &);
405 ///
406 MathMLStream & operator<<(MathMLStream &, docstring const &);
407 ///
408 MathMLStream & operator<<(MathMLStream &, char const *);
409 ///
410 MathMLStream & operator<<(MathMLStream &, char);
411 ///
412 MathMLStream & operator<<(MathMLStream &, char_type);
413 ///
414 MathMLStream & operator<<(MathMLStream &, MTag const &);
415 ///
416 MathMLStream & operator<<(MathMLStream &, ETag const &);
417 ///
418 MathMLStream & operator<<(MathMLStream &, CTag const &);
419
420
421 /// A simpler version of ModeSpecifier, for MathML
422 class SetMode {
423 public:
424         ///
425         explicit SetMode(MathMLStream & ms, bool text);
426         ///
427         ~SetMode();
428 private:
429         ///
430         MathMLStream & ms_;
431         ///
432         bool was_text_;
433 };
434
435
436 class HtmlStream {
437 public:
438         ///
439         explicit HtmlStream(odocstream & os);
440         ///
441         void cr();
442         ///
443         odocstream & os() { return os_; }
444         ///
445         int line() const { return line_; }
446         ///
447         int & tab() { return tab_; }
448         ///
449         friend HtmlStream & operator<<(HtmlStream &, char const *);
450         ///
451         void defer(docstring const &);
452         ///
453         void defer(std::string const &);
454         ///
455         docstring deferred() const;
456         ///
457         bool inText() const { return in_text_; }
458 private:
459         ///
460         void setTextMode(bool t) { in_text_ = t; }
461         ///
462         odocstream & os_;
463         ///
464         int tab_;
465         ///
466         int line_;
467         ///
468         bool in_text_;
469         ///
470         odocstringstream deferred_;
471         ///
472         friend class SetHTMLMode;
473 };
474
475 ///
476 HtmlStream & operator<<(HtmlStream &, MathAtom const &);
477 ///
478 HtmlStream & operator<<(HtmlStream &, MathData const &);
479 ///
480 HtmlStream & operator<<(HtmlStream &, docstring const &);
481 ///
482 HtmlStream & operator<<(HtmlStream &, char const *);
483 ///
484 HtmlStream & operator<<(HtmlStream &, char);
485 ///
486 HtmlStream & operator<<(HtmlStream &, char_type);
487 ///
488 HtmlStream & operator<<(HtmlStream &, MTag const &);
489 ///
490 HtmlStream & operator<<(HtmlStream &, ETag const &);
491
492
493 class SetHTMLMode {
494 public:
495         ///
496         explicit SetHTMLMode(HtmlStream & os, bool text);
497         ///
498         ~SetHTMLMode();
499 private:
500         ///
501         HtmlStream & os_;
502         ///
503         bool was_text_;
504 };
505
506
507 //
508 // Debugging
509 //
510
511 class NormalStream {
512 public:
513         ///
514         explicit NormalStream(odocstream & os) : os_(os) {}
515         ///
516         odocstream & os() { return os_; }
517 private:
518         ///
519         odocstream & os_;
520 };
521
522 ///
523 NormalStream & operator<<(NormalStream &, MathAtom const &);
524 ///
525 NormalStream & operator<<(NormalStream &, MathData const &);
526 ///
527 NormalStream & operator<<(NormalStream &, docstring const &);
528 ///
529 NormalStream & operator<<(NormalStream &, char const *);
530 ///
531 NormalStream & operator<<(NormalStream &, char);
532 ///
533 NormalStream & operator<<(NormalStream &, int);
534
535
536 //
537 // Maple
538 //
539
540
541 class MapleStream {
542 public:
543         ///
544         explicit MapleStream(odocstream & os) : os_(os) {}
545         ///
546         odocstream & os() { return os_; }
547 private:
548         ///
549         odocstream & os_;
550 };
551
552
553 ///
554 MapleStream & operator<<(MapleStream &, MathAtom const &);
555 ///
556 MapleStream & operator<<(MapleStream &, MathData const &);
557 ///
558 MapleStream & operator<<(MapleStream &, docstring const &);
559 ///
560 MapleStream & operator<<(MapleStream &, char_type);
561 ///
562 MapleStream & operator<<(MapleStream &, char const *);
563 ///
564 MapleStream & operator<<(MapleStream &, char);
565 ///
566 MapleStream & operator<<(MapleStream &, int);
567
568
569 //
570 // Maxima
571 //
572
573
574 class MaximaStream {
575 public:
576         ///
577         explicit MaximaStream(odocstream & os) : os_(os) {}
578         ///
579         odocstream & os() { return os_; }
580 private:
581         ///
582         odocstream & os_;
583 };
584
585
586 ///
587 MaximaStream & operator<<(MaximaStream &, MathAtom const &);
588 ///
589 MaximaStream & operator<<(MaximaStream &, MathData const &);
590 ///
591 MaximaStream & operator<<(MaximaStream &, docstring const &);
592 ///
593 MaximaStream & operator<<(MaximaStream &, char_type);
594 ///
595 MaximaStream & operator<<(MaximaStream &, char const *);
596 ///
597 MaximaStream & operator<<(MaximaStream &, char);
598 ///
599 MaximaStream & operator<<(MaximaStream &, int);
600
601
602 //
603 // Mathematica
604 //
605
606
607 class MathematicaStream {
608 public:
609         ///
610         explicit MathematicaStream(odocstream & os) : os_(os) {}
611         ///
612         odocstream & os() { return os_; }
613 private:
614         ///
615         odocstream & os_;
616 };
617
618
619 ///
620 MathematicaStream & operator<<(MathematicaStream &, MathAtom const &);
621 ///
622 MathematicaStream & operator<<(MathematicaStream &, MathData const &);
623 ///
624 MathematicaStream & operator<<(MathematicaStream &, docstring const &);
625 ///
626 MathematicaStream & operator<<(MathematicaStream &, char const *);
627 ///
628 MathematicaStream & operator<<(MathematicaStream &, char);
629 ///
630 MathematicaStream & operator<<(MathematicaStream &, int);
631
632
633 //
634 // Octave
635 //
636
637
638 class OctaveStream {
639 public:
640         ///
641         explicit OctaveStream(odocstream & os) : os_(os) {}
642         ///
643         odocstream & os() { return os_; }
644 private:
645         ///
646         odocstream & os_;
647 };
648
649 ///
650 OctaveStream & operator<<(OctaveStream &, MathAtom const &);
651 ///
652 OctaveStream & operator<<(OctaveStream &, MathData const &);
653 ///
654 OctaveStream & operator<<(OctaveStream &, docstring const &);
655 ///
656 OctaveStream & operator<<(OctaveStream &, char_type);
657 ///
658 OctaveStream & operator<<(OctaveStream &, char const *);
659 ///
660 OctaveStream & operator<<(OctaveStream &, char);
661 ///
662 OctaveStream & operator<<(OctaveStream &, int);
663
664
665 docstring convertDelimToXMLEscape(docstring const & name, bool xmlmode);
666
667 } // namespace lyx
668
669 #endif