]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathScript.cpp
Fix unwanted curly braces in formula (bug #8679)
[lyx.git] / src / mathed / InsetMathScript.cpp
1 /**
2  * \file InsetMathScript.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "BufferView.h"
14 #include "Cursor.h"
15 #include "DispatchResult.h"
16 #include "FuncRequest.h"
17 #include "FuncStatus.h"
18 #include "InsetMathBrace.h"
19 #include "InsetMathScript.h"
20 #include "InsetMathSymbol.h"
21 #include "LaTeXFeatures.h"
22 #include "MathData.h"
23 #include "MathStream.h"
24 #include "MathSupport.h"
25
26 #include "support/debug.h"
27 #include "support/gettext.h"
28
29 #include "support/lassert.h"
30
31
32 using namespace std;
33
34 namespace lyx {
35
36
37 InsetMathScript::InsetMathScript(Buffer * buf)
38         : InsetMathNest(buf, 1), cell_1_is_up_(false), limits_(0)
39 {}
40
41
42 InsetMathScript::InsetMathScript(Buffer * buf, bool up)
43         : InsetMathNest(buf, 2), cell_1_is_up_(up), limits_(0)
44 {}
45
46
47 InsetMathScript::InsetMathScript(Buffer * buf, MathAtom const & at, bool up)
48         : InsetMathNest(buf, 2), cell_1_is_up_(up), limits_(0)
49 {
50         LATTEST(nargs() >= 1);
51         cell(0).push_back(at);
52 }
53
54
55 Inset * InsetMathScript::clone() const
56 {
57         return new InsetMathScript(*this);
58 }
59
60
61 InsetMathScript const * InsetMathScript::asScriptInset() const
62 {
63         return this;
64 }
65
66
67 InsetMathScript * InsetMathScript::asScriptInset()
68 {
69         return this;
70 }
71
72
73 bool InsetMathScript::idxFirst(Cursor & cur) const
74 {
75         cur.idx() = 0;
76         cur.pos() = 0;
77         return true;
78 }
79
80
81 bool InsetMathScript::idxLast(Cursor & cur) const
82 {
83         cur.idx() = 0;
84         cur.pos() = nuc().size();
85         return true;
86 }
87
88
89 MathData const & InsetMathScript::down() const
90 {
91         if (nargs() == 3)
92                 return cell(2);
93         LBUFERR(nargs() > 1);
94         return cell(1);
95 }
96
97
98 MathData & InsetMathScript::down()
99 {
100         if (nargs() == 3)
101                 return cell(2);
102         LBUFERR(nargs() > 1);
103         return cell(1);
104 }
105
106
107 MathData const & InsetMathScript::up() const
108 {
109         LBUFERR(nargs() > 1);
110         return cell(1);
111 }
112
113
114 MathData & InsetMathScript::up()
115 {
116         LBUFERR(nargs() > 1);
117         return cell(1);
118 }
119
120
121 void InsetMathScript::ensure(bool up)
122 {
123         if (nargs() == 1) {
124                 // just nucleus so far
125                 cells_.push_back(MathData());
126                 cell_1_is_up_ = up;
127         } else if (nargs() == 2 && !has(up)) {
128                 if (up) {
129                         cells_.push_back(cell(1));
130                         cell(1).clear();
131                 } else {
132                         cells_.push_back(MathData());
133                 }
134         }
135 }
136
137
138 MathData const & InsetMathScript::nuc() const
139 {
140         return cell(0);
141 }
142
143
144 MathData & InsetMathScript::nuc()
145 {
146         return cell(0);
147 }
148
149
150 int InsetMathScript::dy01(BufferView const & bv, int asc, int des, int what) const
151 {
152         int dasc = 0;
153         int slevel = 0;
154         bool isCharBox = !nuc().empty() ? isAlphaSymbol(nuc().back()) : false;
155         if (hasDown()) {
156                 Dimension const & dimdown = down().dimension(bv);
157                 dasc = dimdown.ascent();
158                 slevel = nuc().slevel();
159                 int ascdrop = dasc - slevel;
160                 int desdrop = isCharBox ? 0 : des + nuc().sshift();
161                 int mindes = nuc().mindes();
162                 des = max(desdrop, ascdrop);
163                 des = max(mindes, des);
164         }
165         if (hasUp()) {
166                 Dimension const & dimup = up().dimension(bv);
167                 int minasc = nuc().minasc();
168                 int ascdrop = isCharBox ? 0 : asc - up().mindes();
169                 int udes = dimup.descent();
170                 asc = udes + nuc().sshift();
171                 asc = max(ascdrop, asc);
172                 asc = max(minasc, asc);
173                 if (hasDown()) {
174                         int del = asc - udes - dasc;
175                         if (del + des <= 2) {
176                                 int newdes = 2 - del;
177                                 del = slevel - asc + udes;
178                                 if (del > 0) {
179                                         asc += del;
180                                         newdes -= del;
181                                 }
182                                 des = max(des, newdes);
183                         }
184                 }
185         }
186         return what ? asc : des;
187 }
188
189
190 int InsetMathScript::dy0(BufferView const & bv) const
191 {
192         int nd = ndes(bv);
193         if (!hasDown())
194                 return nd;
195         int des = down().dimension(bv).ascent();
196         if (hasLimits())
197                 des += nd + 2;
198         else {
199                 int na = nasc(bv);
200                 des = dy01(bv, na, nd, 0);
201         }
202         return des;
203 }
204
205
206 int InsetMathScript::dy1(BufferView const & bv) const
207 {
208         int na = nasc(bv);
209         if (!hasUp())
210                 return na;
211         int asc = up().dimension(bv).descent();
212         if (hasLimits())
213                 asc += na + 2;
214         else {
215                 int nd = ndes(bv);
216                 asc = dy01(bv, na, nd, 1);
217         }
218         asc = max(asc, 5);
219         return asc;
220 }
221
222
223 int InsetMathScript::dx0(BufferView const & bv) const
224 {
225         LASSERT(hasDown(), return 0);
226         Dimension const dim = dimension(bv);
227         return hasLimits() ? (dim.wid - down().dimension(bv).width()) / 2 : nwid(bv);
228 }
229
230
231 int InsetMathScript::dx1(BufferView const & bv) const
232 {
233         LASSERT(hasUp(), return 0);
234         Dimension const dim = dimension(bv);
235         return hasLimits() ? (dim.wid - up().dimension(bv).width()) / 2 : nwid(bv) + nker(&bv);
236 }
237
238
239 int InsetMathScript::dxx(BufferView const & bv) const
240 {
241         Dimension const dim = dimension(bv);
242         return hasLimits() ? (dim.wid - nwid(bv)) / 2  :  0;
243 }
244
245
246 int InsetMathScript::nwid(BufferView const & bv) const
247 {
248         return !nuc().empty() ? nuc().dimension(bv).width() : 2;
249 }
250
251
252 int InsetMathScript::nasc(BufferView const & bv) const
253 {
254         return !nuc().empty() ? nuc().dimension(bv).ascent() : 5;
255 }
256
257
258 int InsetMathScript::ndes(BufferView const & bv) const
259 {
260         return !nuc().empty() ? nuc().dimension(bv).descent() : 0;
261 }
262
263
264 int InsetMathScript::nker(BufferView const * bv) const
265 {
266         if (!nuc().empty()) {
267                 int kerning = nuc().kerning(bv);
268                 return kerning > 0 ? kerning : 0;
269         }
270         return 0;
271 }
272
273
274 void InsetMathScript::metrics(MetricsInfo & mi, Dimension & dim) const
275 {
276         Dimension dim0;
277         Dimension dim1;
278         Dimension dim2;
279         cell(0).metrics(mi, dim0);
280         ScriptChanger dummy(mi.base);
281         if (nargs() > 1)
282                 cell(1).metrics(mi, dim1);
283         if (nargs() > 2)
284                 cell(2).metrics(mi, dim2);
285
286         dim.wid = 0;
287         BufferView & bv = *mi.base.bv;
288         // FIXME: data copying... not very efficient.
289         Dimension dimup;
290         Dimension dimdown;
291         if (hasUp())
292                 dimup = up().dimension(bv);
293         if (hasDown())
294                 dimdown = down().dimension(bv);
295
296         if (hasLimits()) {
297                 dim.wid = nwid(bv);
298                 if (hasUp())
299                         dim.wid = max(dim.wid, dimup.width());
300                 if (hasDown())
301                         dim.wid = max(dim.wid, dimdown.width());
302         } else {
303                 if (hasUp())
304                         dim.wid = max(dim.wid, nker(mi.base.bv) + dimup.width());
305                 if (hasDown())
306                         dim.wid = max(dim.wid, dimdown.width());
307                 dim.wid += nwid(bv);
308         }
309         int na = nasc(bv);
310         if (hasUp()) {
311                 int asc = dy1(bv) + dimup.ascent();
312                 dim.asc = max(na, asc);
313         } else
314                 dim.asc = na;
315         int nd = ndes(bv);
316         if (hasDown()) {
317                 int des = dy0(bv) + dimdown.descent();
318                 dim.des = max(nd, des);
319         } else
320                 dim.des = nd;
321         metricsMarkers(dim);
322 }
323
324
325 void InsetMathScript::draw(PainterInfo & pi, int x, int y) const
326 {
327         BufferView & bv = *pi.base.bv;
328         if (!nuc().empty())
329                 nuc().draw(pi, x + dxx(bv), y);
330         else {
331                 nuc().setXY(bv, x + dxx(bv), y);
332                 if (editing(&bv))
333                         pi.draw(x + dxx(bv), y, char_type('.'));
334         }
335         ScriptChanger dummy(pi.base);
336         if (hasUp())
337                 up().draw(pi, x + dx1(bv), y - dy1(bv));
338         if (hasDown())
339                 down().draw(pi, x + dx0(bv), y + dy0(bv));
340         drawMarkers(pi, x, y);
341 }
342
343
344 void InsetMathScript::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
345 {
346         if (hasUp())
347                 up().metricsT(mi, dim);
348         if (hasDown())
349                 down().metricsT(mi, dim);
350         nuc().metricsT(mi, dim);
351 }
352
353
354 void InsetMathScript::drawT(TextPainter & pain, int x, int y) const
355 {
356         // FIXME: BROKEN
357         if (!nuc().empty())
358                 nuc().drawT(pain, x + 1, y);
359         if (hasUp())
360                 up().drawT(pain, x + 1, y - 1 /*dy1()*/);
361         if (hasDown())
362                 down().drawT(pain, x + 1, y + 1 /*dy0()*/);
363 }
364
365
366
367 bool InsetMathScript::hasLimits() const
368 {
369         // obvious cases
370         if (limits_ == 1)
371                 return true;
372         if (limits_ == -1)
373                 return false;
374
375         // we can only display limits if the nucleus wants some
376         if (nuc().empty())
377                 return false;
378         if (!nuc().back()->isScriptable())
379                 return false;
380
381         if (nuc().back()->asSymbolInset()) {
382                 // \intop is an alias for \int\limits, \ointop == \oint\limits
383                 if (nuc().back()->asSymbolInset()->name().find(from_ascii("intop")) != string::npos)
384                         return true;
385                 // per default \int has limits beside the \int even in displayed formulas
386                 if (nuc().back()->asSymbolInset()->name().find(from_ascii("int")) != string::npos)
387                         return false;
388         }
389
390         // assume "real" limits for everything else
391         return true;
392 }
393
394
395 void InsetMathScript::removeScript(bool up)
396 {
397         if (nargs() == 2) {
398                 if (up == cell_1_is_up_)
399                         cells_.pop_back();
400         } else if (nargs() == 3) {
401                 if (up == true) {
402                         swap(cells_[1], cells_[2]);
403                         cell_1_is_up_ = false;
404                 } else {
405                         cell_1_is_up_ = true;
406                 }
407                 cells_.pop_back();
408         }
409 }
410
411
412 bool InsetMathScript::has(bool up) const
413 {
414         return idxOfScript(up);
415 }
416
417
418 bool InsetMathScript::hasUp() const
419 {
420         //lyxerr << "1up: " << bool(cell_1_is_up_));
421         //lyxerr << "hasUp: " << bool(idxOfScript(true)));
422         return idxOfScript(true);
423 }
424
425
426 bool InsetMathScript::hasDown() const
427 {
428         //LYXERR0("1up: " << bool(cell_1_is_up_));
429         //LYXERR0("hasDown: " << bool(idxOfScript(false)));
430         return idxOfScript(false);
431 }
432
433
434 Inset::idx_type InsetMathScript::idxOfScript(bool up) const
435 {
436         if (nargs() == 1)
437                 return 0;
438         if (nargs() == 2)
439                 return (cell_1_is_up_ == up) ? 1 : 0;
440         if (nargs() == 3)
441                 return up ? 1 : 2;
442         LASSERT(false, return 0);
443 }
444
445
446 bool InsetMathScript::idxForward(Cursor &) const
447 {
448         return false;
449 }
450
451
452 bool InsetMathScript::idxBackward(Cursor &) const
453 {
454         return false;
455 }
456
457
458 bool InsetMathScript::idxUpDown(Cursor & cur, bool up) const
459 {
460         // in nucleus?
461         if (cur.idx() == 0) {
462                 // don't go up/down if there is no cell in this direction
463                 if (!has(up))
464                         return false;
465                 // go up/down only if in the last position
466                 // or in the first position of something with displayed limits
467                 if (cur.pos() == cur.lastpos() || (cur.pos() == 0 && hasLimits())) {
468                         cur.idx() = idxOfScript(up);
469                         cur.pos() = 0;
470                         return true;
471                 }
472                 return false;
473         }
474
475         // Are we 'up'?
476         if (cur.idx() == idxOfScript(true)) {
477                 // can't go further up
478                 if (up)
479                         return false;
480                 // otherwise go to last position in the nucleus
481                 cur.idx() = 0;
482                 cur.pos() = cur.lastpos();
483                 return true;
484         }
485
486         // Are we 'down'?
487         if (cur.idx() == idxOfScript(false)) {
488                 // can't go further down
489                 if (!up)
490                         return false;
491                 // otherwise go to last position in the nucleus
492                 cur.idx() = 0;
493                 cur.pos() = cur.lastpos();
494                 return true;
495         }
496
497         return false;
498 }
499
500
501 void InsetMathScript::write(WriteStream & os) const
502 {
503         MathEnsurer ensurer(os);
504
505         if (!nuc().empty()) {
506                 os << nuc();
507                 //if (nuc().back()->takesLimits()) {
508                         if (limits_ == -1)
509                                 os << "\\nolimits ";
510                         if (limits_ == 1)
511                                 os << "\\limits ";
512                 //}
513         } else {
514                 if (os.firstitem())
515                         LYXERR(Debug::MATHED, "suppressing {} when writing");
516                 else
517                         os << "{}";
518         }
519
520         if (hasDown() /*&& !down().empty()*/)
521                 os << "_{" << down() << '}';
522
523         if (hasUp() /*&& !up().empty()*/) {
524                 // insert space if up() is empty or an empty brace inset
525                 // (see bug 8305)
526                 if (os.latex() && (up().empty() ||
527                     (up().size() == 1 && up().back()->asBraceInset() &&
528                      up().back()->asBraceInset()->cell(0).empty())))
529                         os << "^ {}";
530                 else
531                         os << "^{" << up() << '}';
532         }
533
534         if (lock_ && !os.latex())
535                 os << "\\lyxlock ";
536 }
537
538
539 void InsetMathScript::normalize(NormalStream & os) const
540 {
541         bool d = hasDown() && !down().empty();
542         bool u = hasUp() && !up().empty();
543
544         if (u && d)
545                 os << "[subsup ";
546         else if (u)
547                 os << "[sup ";
548         else if (d)
549                 os << "[sub ";
550
551         if (!nuc().empty())
552                 os << nuc() << ' ';
553         else
554                 os << "[par]";
555
556         if (u && d)
557                 os << down() << ' ' << up() << ']';
558         else if (d)
559                 os << down() << ']';
560         else if (u)
561                 os << up() << ']';
562 }
563
564
565 void InsetMathScript::maple(MapleStream & os) const
566 {
567         if (!nuc().empty())
568                 os << nuc();
569         if (hasDown() && !down().empty())
570                 os << '[' << down() << ']';
571         if (hasUp() && !up().empty())
572                 os << "^(" << up() << ')';
573 }
574
575
576 void InsetMathScript::mathematica(MathematicaStream & os) const
577 {
578         bool d = hasDown() && !down().empty();
579         bool u = hasUp() && !up().empty();
580
581         if (!nuc().empty()) {
582                 if (d)
583                         os << "Subscript[" << nuc();
584                 else
585                         os << nuc();
586         }
587
588         if (u)
589                 os << "^(" << up() << ')';
590
591         if (!nuc().empty()) {
592                 if (d)
593                         os << ',' << down() << ']';
594         }
595 }
596
597
598 void InsetMathScript::mathmlize(MathStream & os) const
599 {
600         bool d = hasDown() && !down().empty();
601         bool u = hasUp() && !up().empty();
602         bool l = hasLimits();
603
604         if (u && d)
605                 os << MTag(l ? "munderover" : "msubsup");
606         else if (u)
607                 os << MTag(l ? "mover" : "msup");
608         else if (d)
609                 os << MTag(l ? "munder" : "msub");
610
611         if (!nuc().empty())
612                 os << MTag("mrow") << nuc() << ETag("mrow");
613         else
614                 os << "<mrow />";
615
616         if (u && d)
617                 os << MTag("mrow") << down() << ETag("mrow") 
618                    << MTag("mrow") << up() << ETag("mrow") 
619                    << ETag(l ? "munderover" : "msubsup");
620         else if (u)
621                 os << MTag("mrow") << up() << ETag("mrow") << ETag(l ? "mover" : "msup");
622         else if (d)
623                 os << MTag("mrow") << down() << ETag("mrow") << ETag(l ? "munder" : "msub");
624 }
625
626
627 void InsetMathScript::htmlize(HtmlStream & os) const
628 {
629         bool d = hasDown() && !down().empty();
630         bool u = hasUp() && !up().empty();
631
632         if (!nuc().empty())
633                 os << nuc();
634
635         if (u && d)
636                 os << MTag("span", "class='scripts'")
637                          << MTag("span") << up() << ETag("span")
638                          << MTag("span") << down() << ETag("span")
639                          << ETag("span");
640         else if (u)
641                 os << MTag("sup", "class='math'") << up() << ETag("sup");
642         else if (d)
643                 os << MTag("sub", "class='math'") << down() << ETag("sub");
644 }
645
646
647 void InsetMathScript::octave(OctaveStream & os) const
648 {
649         if (!nuc().empty())
650                 os << nuc();
651         if (hasDown() && !down().empty())
652                 os << '[' << down() << ']';
653         if (hasUp() && !up().empty())
654                 os << "^(" << up() << ')';
655 }
656
657
658 void InsetMathScript::infoize(odocstream & os) const
659 {
660         os << "Scripts";
661 }
662
663
664 void InsetMathScript::infoize2(odocstream & os) const
665 {
666         if (limits_)
667                 os << from_ascii(limits_ == 1 ? ", Displayed limits" : ", Inlined limits");
668 }
669
670
671 bool InsetMathScript::notifyCursorLeaves(Cursor const & old, Cursor & cur)
672 {
673         InsetMathNest::notifyCursorLeaves(old, cur);
674
675         //LYXERR0("InsetMathScript::notifyCursorLeaves: 1 " << cur);
676
677         // Remove empty scripts if possible:
678
679         // The case of two scripts, but only one got empty (1 = super, 2 = sub).
680         // We keep the script inset, but remove the empty script.
681         if (nargs() > 2 && (!cell(1).empty() || !cell(2).empty())) {
682                 if (cell(2).empty()) {
683                         // must be a subscript...
684                         old.recordUndoInset();
685                         removeScript(false);
686                         cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
687                         return true;
688                 } else if (cell(1).empty()) {
689                         // must be a superscript...
690                         old.recordUndoInset();
691                         removeScript(true);
692                         cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
693                         return true;
694                 }
695         }
696         // Now the two suicide cases:
697         // * we have only one script which is empty
698         // * we have two scripts which are both empty.
699         // The script inset is removed completely.
700         if ((nargs() == 2 && cell(1).empty())
701             || (nargs() == 3 && cell(1).empty() && cell(2).empty())) {
702                 // Make undo step. We cannot use cur for this because
703                 // it does not necessarily point to us anymore. But we
704                 // should be on top of the cursor old.
705                 Cursor insetCur = old;
706                 int scriptSlice = insetCur.find(this);
707                 LASSERT(scriptSlice != -1, /**/);
708                 insetCur.cutOff(scriptSlice);
709                 insetCur.recordUndoInset();
710
711                 // Let the script inset commit suicide. This is
712                 // modelled on Cursor.pullArg(), but tries not to
713                 // invoke notifyCursorLeaves again and does not touch
714                 // cur (since the top slice will be deleted
715                 // afterwards)
716                 MathData ar = cell(0);
717                 insetCur.pop();
718                 insetCur.cell().erase(insetCur.pos());
719                 insetCur.cell().insert(insetCur.pos(), ar);
720
721                 // redraw
722                 cur.screenUpdateFlags(cur.result().screenUpdate() | Update::SinglePar);
723                 return true;
724         }
725
726         //LYXERR0("InsetMathScript::notifyCursorLeaves: 2 " << cur);
727         return false;
728 }
729
730
731 void InsetMathScript::doDispatch(Cursor & cur, FuncRequest & cmd)
732 {
733         //LYXERR("InsetMathScript: request: " << cmd);
734
735         if (cmd.action() == LFUN_MATH_LIMITS) {
736                 cur.recordUndoInset();
737                 if (!cmd.argument().empty()) {
738                         if (cmd.argument() == "limits")
739                                 limits_ = 1;
740                         else if (cmd.argument() == "nolimits")
741                                 limits_ = -1;
742                         else
743                                 limits_ = 0;
744                 } else if (limits_ == 0)
745                         limits_ = hasLimits() ? -1 : 1;
746                 else
747                         limits_ = 0;
748                 return;
749         }
750
751         InsetMathNest::doDispatch(cur, cmd);
752 }
753
754
755 bool InsetMathScript::getStatus(Cursor & cur, FuncRequest const & cmd,
756                                 FuncStatus & flag) const
757 {
758         if (cmd.action() == LFUN_MATH_LIMITS) {
759                 if (!cmd.argument().empty()) {
760                         if (cmd.argument() == "limits")
761                                 flag.setOnOff(limits_ == 1);
762                         else if (cmd.argument() == "nolimits")
763                                 flag.setOnOff(limits_ == -1);
764                         else
765                                 flag.setOnOff(limits_ == 0);
766                 } 
767                 flag.setEnabled(true);
768                 return true;
769         }
770
771         return InsetMathNest::getStatus(cur, cmd, flag);
772 }
773
774
775 // the idea for dual scripts came from the eLyXer code
776 void InsetMathScript::validate(LaTeXFeatures & features) const
777 {
778         if (features.runparams().math_flavor == OutputParams::MathAsHTML)
779                 features.addCSSSnippet(
780                         "span.scripts{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
781                         "span.scripts span {display: block;}\n"
782                         "sub.math{font-size: 75%;}\n"
783                         "sup.math{font-size: 75%;}");
784         InsetMathNest::validate(features);
785 }
786
787 } // namespace lyx