]> git.lyx.org Git - features.git/blob - src/paragraph.C
Make buffer's member variables private; use accessor functions.
[features.git] / src / paragraph.C
1 /**
2  * \file paragraph.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author Angus Leeming
10  * \author John Levon
11  * \author André Pönitz
12  * \author Dekel Tsur
13  * \author Jürgen Vigna
14  *
15  * Full author contact details are available in file CREDITS.
16  */
17
18 #include <config.h>
19
20 #include "paragraph.h"
21 #include "paragraph_pimpl.h"
22
23 #include "buffer.h"
24 #include "encoding.h"
25 #include "debug.h"
26 #include "gettext.h"
27 #include "language.h"
28 #include "latexrunparams.h"
29 #include "lyxrc.h"
30 #include "lyxrow.h"
31
32 #include "insets/insetbibitem.h"
33 #include "insets/insetoptarg.h"
34
35 #include "support/lstrings.h"
36 #include "support/LAssert.h"
37 #include "support/textutils.h"
38
39 #include "support/std_sstream.h"
40
41
42 using namespace lyx::support;
43
44 using std::ostream;
45 using std::ostringstream;
46 using std::endl;
47
48 using lyx::pos_type;
49
50
51 Paragraph::Paragraph()
52         : y(0), pimpl_(new Paragraph::Pimpl(this))
53 {
54         enumdepth = 0;
55         itemdepth = 0;
56         params().clear();
57 }
58
59
60 Paragraph::Paragraph(Paragraph const & lp)
61         : y(0), pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this))
62 {
63         enumdepth = 0;
64         itemdepth = 0;
65         // this is because of the dummy layout of the paragraphs that
66         // follow footnotes
67         layout_ = lp.layout();
68
69         // copy everything behind the break-position to the new paragraph
70         insetlist = lp.insetlist;
71         InsetList::iterator it = insetlist.begin();
72         InsetList::iterator end = insetlist.end();
73         for (; it != end; ++it) {
74                 // currently we hold Inset*, not InsetBase*
75                 it->inset = static_cast<InsetOld*>(it->inset->clone().release());
76         }
77 }
78
79
80 void Paragraph::operator=(Paragraph const & lp)
81 {
82         // needed as we will destroy the pimpl_ before copying it
83         if (&lp != this)
84                 return;
85
86         lyxerr << "Paragraph::operator=()" << endl;
87
88         delete pimpl_;
89         pimpl_ = new Pimpl(*lp.pimpl_, this);
90
91         enumdepth = lp.enumdepth;
92         itemdepth = lp.itemdepth;
93         // this is because of the dummy layout of the paragraphs that
94         // follow footnotes
95         layout_ = lp.layout();
96
97         // copy everything behind the break-position to the new paragraph
98         insetlist = lp.insetlist;
99         InsetList::iterator it = insetlist.begin();
100         InsetList::iterator end = insetlist.end();
101         for (; it != end; ++it) {
102                 it->inset = static_cast<InsetOld*>(it->inset->clone().release());
103         }
104 }
105
106 // the destructor removes the new paragraph from the list
107 Paragraph::~Paragraph()
108 {
109         delete pimpl_;
110         //
111         //lyxerr << "Paragraph::paragraph_id = "
112         //       << Paragraph::paragraph_id << endl;
113 }
114
115
116 void Paragraph::write(Buffer const & buf, ostream & os,
117                           BufferParams const & bparams,
118                           depth_type & dth) const
119 {
120         // The beginning or end of a deeper (i.e. nested) area?
121         if (dth != params().depth()) {
122                 if (params().depth() > dth) {
123                         while (params().depth() > dth) {
124                                 os << "\n\\begin_deeper ";
125                                 ++dth;
126                         }
127                 } else {
128                         while (params().depth() < dth) {
129                                 os << "\n\\end_deeper ";
130                                 --dth;
131                         }
132                 }
133         }
134
135         // First write the layout
136         os << "\n\\begin_layout " << layout()->name() << '\n';
137
138         params().write(os);
139
140         LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
141
142         Change running_change = Change(Change::UNCHANGED);
143         lyx::time_type const curtime(lyx::current_time());
144
145         int column = 0;
146         for (pos_type i = 0; i < size(); ++i) {
147                 if (!i) {
148                         os << '\n';
149                         column = 0;
150                 }
151
152                 Change change = pimpl_->lookupChangeFull(i);
153                 Changes::lyxMarkChange(os, column, curtime, running_change, change);
154                 running_change = change;
155
156                 // Write font changes
157                 LyXFont font2 = getFontSettings(bparams, i);
158                 if (font2 != font1) {
159                         font2.lyxWriteChanges(font1, os);
160                         column = 0;
161                         font1 = font2;
162                 }
163
164                 value_type const c = getChar(i);
165                 switch (c) {
166                 case META_INSET:
167                 {
168                         InsetOld const * inset = getInset(i);
169                         if (inset)
170                                 if (inset->directWrite()) {
171                                         // international char, let it write
172                                         // code directly so it's shorter in
173                                         // the file
174                                         inset->write(buf, os);
175                                 } else {
176                                         os << "\n\\begin_inset ";
177                                         inset->write(buf, os);
178                                         os << "\n\\end_inset \n\n";
179                                         column = 0;
180                                 }
181                 }
182                 break;
183                 case '\\':
184                         os << "\n\\backslash \n";
185                         column = 0;
186                         break;
187                 case '.':
188                         if (i + 1 < size() && getChar(i + 1) == ' ') {
189                                 os << ".\n";
190                                 column = 0;
191                         } else
192                                 os << '.';
193                         break;
194                 default:
195                         if ((column > 70 && c == ' ')
196                             || column > 79) {
197                                 os << '\n';
198                                 column = 0;
199                         }
200                         // this check is to amend a bug. LyX sometimes
201                         // inserts '\0' this could cause problems.
202                         if (c != '\0')
203                                 os << c;
204                         else
205                                 lyxerr << "ERROR (Paragraph::writeFile):"
206                                         " NULL char in structure." << endl;
207                         ++column;
208                         break;
209                 }
210         }
211
212         // to make reading work properly
213         if (!size()) {
214                 running_change = pimpl_->lookupChange(0);
215                 Changes::lyxMarkChange(os, column, curtime,
216                         Change(Change::UNCHANGED), running_change);
217         }
218         Changes::lyxMarkChange(os, column, curtime,
219                 running_change, Change(Change::UNCHANGED));
220
221         os << "\n\\end_layout\n";
222 }
223
224
225 void Paragraph::validate(LaTeXFeatures & features) const
226 {
227         pimpl_->validate(features, *layout());
228 }
229
230
231 void Paragraph::eraseIntern(lyx::pos_type pos)
232 {
233         pimpl_->eraseIntern(pos);
234 }
235
236
237 bool Paragraph::erase(pos_type pos)
238 {
239         return pimpl_->erase(pos);
240 }
241
242
243 int Paragraph::erase(pos_type start, pos_type end)
244 {
245         return pimpl_->erase(start, end);
246 }
247
248
249 bool Paragraph::checkInsertChar(LyXFont & font)
250 {
251         if (pimpl_->inset_owner)
252                 return pimpl_->inset_owner->checkInsertChar(font);
253         return true;
254 }
255
256
257 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
258 {
259         insertChar(pos, c, LyXFont(LyXFont::ALL_INHERIT));
260 }
261
262
263 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
264                            LyXFont const & font, Change change)
265 {
266         pimpl_->insertChar(pos, c, font, change);
267 }
268
269
270 void Paragraph::insertInset(pos_type pos, InsetOld * inset)
271 {
272         insertInset(pos, inset, LyXFont(LyXFont::ALL_INHERIT));
273 }
274
275
276 void Paragraph::insertInset(pos_type pos, InsetOld * inset,
277         LyXFont const & font, Change change)
278 {
279         pimpl_->insertInset(pos, inset, font, change);
280 }
281
282
283 bool Paragraph::insetAllowed(InsetOld::Code code)
284 {
285         //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
286         if (pimpl_->inset_owner)
287                 return pimpl_->inset_owner->insetAllowed(code);
288         return true;
289 }
290
291
292 InsetOld * Paragraph::getInset(pos_type pos)
293 {
294         Assert(pos < size());
295         return insetlist.get(pos);
296 }
297
298
299 InsetOld const * Paragraph::getInset(pos_type pos) const
300 {
301         Assert(pos < size());
302         return insetlist.get(pos);
303 }
304
305
306 // Gets uninstantiated font setting at position.
307 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
308                                          pos_type pos) const
309 {
310         Assert(pos <= size());
311
312         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
313         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
314         for (; cit != end; ++cit)
315                 if (cit->pos() >= pos)
316                         break;
317
318         if (cit != end)
319                 return cit->font();
320
321         if (pos == size() && !empty())
322                 return getFontSettings(bparams, pos - 1);
323
324         return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
325 }
326
327
328 lyx::pos_type Paragraph::getEndPosOfFontSpan(lyx::pos_type pos) const
329 {
330         Assert(pos <= size());
331
332         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
333         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
334         for (; cit != end; ++cit)
335                 if (cit->pos() >= pos)
336                         return cit->pos();
337
338         // This should not happen, but if so, we take no chances.
339         //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
340         //      << endl;
341         return pos;
342 }
343
344
345 // Gets uninstantiated font setting at position 0
346 LyXFont const Paragraph::getFirstFontSettings() const
347 {
348         if (!empty() && !pimpl_->fontlist.empty())
349                 return pimpl_->fontlist[0].font();
350
351         return LyXFont(LyXFont::ALL_INHERIT);
352 }
353
354
355 // Gets the fully instantiated font at a given position in a paragraph
356 // This is basically the same function as LyXText::GetFont() in text2.C.
357 // The difference is that this one is used for generating the LaTeX file,
358 // and thus cosmetic "improvements" are disallowed: This has to deliver
359 // the true picture of the buffer. (Asger)
360 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
361                                  LyXFont const & outerfont) const
362 {
363         Assert(pos >= 0);
364
365         LyXLayout_ptr const & lout = layout();
366
367         pos_type const body_pos = beginningOfBody();
368
369         LyXFont layoutfont;
370         if (pos < body_pos)
371                 layoutfont = lout->labelfont;
372         else
373                 layoutfont = lout->font;
374
375         LyXFont tmpfont = getFontSettings(bparams, pos);
376         tmpfont.realize(layoutfont);
377         tmpfont.realize(outerfont);
378         tmpfont.realize(bparams.getLyXTextClass().defaultfont());
379
380         return tmpfont;
381 }
382
383
384 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams,
385                                       LyXFont const & outerfont) const
386 {
387         LyXFont tmpfont = layout()->labelfont;
388         tmpfont.setLanguage(getParLanguage(bparams));
389         tmpfont.realize(outerfont);
390         tmpfont.realize(bparams.getLyXTextClass().defaultfont());
391         return tmpfont;
392 }
393
394
395 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams,
396                                        LyXFont const & outerfont) const
397 {
398         LyXFont tmpfont = layout()->font;
399         tmpfont.setLanguage(getParLanguage(bparams));
400         tmpfont.realize(outerfont);
401         tmpfont.realize(bparams.getLyXTextClass().defaultfont());
402         return tmpfont;
403 }
404
405
406 /// Returns the height of the highest font in range
407 LyXFont::FONT_SIZE
408 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
409                               LyXFont::FONT_SIZE const def_size) const
410 {
411         if (pimpl_->fontlist.empty())
412                 return def_size;
413
414         Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
415         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
416         for (; end_it != end; ++end_it) {
417                 if (end_it->pos() >= endpos)
418                         break;
419         }
420
421         if (end_it != end)
422                 ++end_it;
423
424         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
425         for (; cit != end; ++cit) {
426                 if (cit->pos() >= startpos)
427                         break;
428         }
429
430         LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
431         for (; cit != end_it; ++cit) {
432                 LyXFont::FONT_SIZE size = cit->font().size();
433                 if (size == LyXFont::INHERIT_SIZE)
434                         size = def_size;
435                 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
436                         maxsize = size;
437         }
438         return maxsize;
439 }
440
441
442 Paragraph::value_type
443 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
444 {
445         value_type c = getChar(pos);
446         if (!lyxrc.rtl_support)
447                 return c;
448
449         value_type uc = c;
450         switch (c) {
451         case '(':
452                 uc = ')';
453                 break;
454         case ')':
455                 uc = '(';
456                 break;
457         case '[':
458                 uc = ']';
459                 break;
460         case ']':
461                 uc = '[';
462                 break;
463         case '{':
464                 uc = '}';
465                 break;
466         case '}':
467                 uc = '{';
468                 break;
469         case '<':
470                 uc = '>';
471                 break;
472         case '>':
473                 uc = '<';
474                 break;
475         }
476         if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
477                 return uc;
478         else
479                 return c;
480 }
481
482
483 void Paragraph::setFont(pos_type pos, LyXFont const & font)
484 {
485         Assert(pos <= size());
486
487         // First, reduce font against layout/label font
488         // Update: The SetCharFont() routine in text2.C already
489         // reduces font, so we don't need to do that here. (Asger)
490         // No need to simplify this because it will disappear
491         // in a new kernel. (Asger)
492         // Next search font table
493
494         Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
495         Pimpl::FontList::iterator it = beg;
496         Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
497         for (; it != endit; ++it) {
498                 if (it->pos() >= pos)
499                         break;
500         }
501         unsigned int i = std::distance(beg, it);
502         bool notfound = (it == endit);
503
504         if (!notfound && pimpl_->fontlist[i].font() == font)
505                 return;
506
507         bool begin = pos == 0 || notfound ||
508                 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
509         // Is position pos is a beginning of a font block?
510         bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
511         // Is position pos is the end of a font block?
512         if (begin && end) { // A single char block
513                 if (i + 1 < pimpl_->fontlist.size() &&
514                     pimpl_->fontlist[i + 1].font() == font) {
515                         // Merge the singleton block with the next block
516                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
517                         if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
518                                 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
519                 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
520                         // Merge the singleton block with the previous block
521                         pimpl_->fontlist[i - 1].pos(pos);
522                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
523                 } else
524                         pimpl_->fontlist[i].font(font);
525         } else if (begin) {
526                 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
527                         pimpl_->fontlist[i - 1].pos(pos);
528                 else
529                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
530                                         Pimpl::FontTable(pos, font));
531         } else if (end) {
532                 pimpl_->fontlist[i].pos(pos - 1);
533                 if (!(i + 1 < pimpl_->fontlist.size() &&
534                       pimpl_->fontlist[i + 1].font() == font))
535                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
536                                         Pimpl::FontTable(pos, font));
537         } else { // The general case. The block is splitted into 3 blocks
538                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
539                                 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
540                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
541                                 Pimpl::FontTable(pos, font));
542         }
543 }
544
545
546 void Paragraph::makeSameLayout(Paragraph const & par)
547 {
548         layout(par.layout());
549         // move to pimpl?
550         params() = par.params();
551 }
552
553
554 int Paragraph::stripLeadingSpaces()
555 {
556         if (isFreeSpacing())
557                 return 0;
558
559         int i = 0;
560         while (!empty() && (isNewline(0) || isLineSeparator(0))) {
561                 pimpl_->eraseIntern(0);
562                 ++i;
563         }
564
565         return i;
566 }
567
568
569 bool Paragraph::hasSameLayout(Paragraph const & par) const
570 {
571         return
572                 par.layout() == layout() &&
573                 params().sameLayout(par.params());
574 }
575
576
577 Paragraph::depth_type Paragraph::getDepth() const
578 {
579         return params().depth();
580 }
581
582
583 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
584 {
585         if (layout()->isEnvironment())
586                 return params().depth() + 1;
587         else
588                 return params().depth();
589 }
590
591
592 char Paragraph::getAlign() const
593 {
594         return params().align();
595 }
596
597
598 string const & Paragraph::getLabelstring() const
599 {
600         return params().labelString();
601 }
602
603
604 // the next two functions are for the manual labels
605 string const Paragraph::getLabelWidthString() const
606 {
607         if (!params().labelWidthString().empty())
608                 return params().labelWidthString();
609         else
610                 return _("Senseless with this layout!");
611 }
612
613
614 void Paragraph::setLabelWidthString(string const & s)
615 {
616         params().labelWidthString(s);
617 }
618
619
620 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
621 {
622         layout(new_layout);
623         params().labelWidthString(string());
624         params().align(LYX_ALIGN_LAYOUT);
625         params().spaceTop(VSpace(VSpace::NONE));
626         params().spaceBottom(VSpace(VSpace::NONE));
627         params().spacing(Spacing(Spacing::Default));
628 }
629
630
631 int Paragraph::beginningOfBody() const
632 {
633         if (layout()->labeltype != LABEL_MANUAL)
634                 return 0;
635
636         // Unroll the first two cycles of the loop
637         // and remember the previous character to
638         // remove unnecessary GetChar() calls
639         pos_type i = 0;
640         if (i < size() && !isNewline(i)) {
641                 ++i;
642                 char previous_char = 0;
643                 char temp = 0;
644                 if (i < size()) {
645                         previous_char = getChar(i);
646                         if (!isNewline(i)) {
647                                 ++i;
648                                 while (i < size() && previous_char != ' ') {
649                                         temp = getChar(i);
650                                         if (isNewline(i))
651                                                 break;
652
653                                         ++i;
654                                         previous_char = temp;
655                                 }
656                         }
657                 }
658         }
659
660         return i;
661 }
662
663
664 // returns -1 if inset not found
665 int Paragraph::getPositionOfInset(InsetOld const * inset) const
666 {
667         // Find the entry.
668         InsetList::const_iterator it = insetlist.begin();
669         InsetList::const_iterator end = insetlist.end();
670         for (; it != end; ++it)
671                 if (it->inset == inset)
672                         return it->pos;
673         return -1;
674 }
675
676
677 InsetBibitem * Paragraph::bibitem() const
678 {
679         InsetList::const_iterator it = insetlist.begin();
680         if (it != insetlist.end() && it->inset->lyxCode() == InsetOld::BIBTEX_CODE)
681                 return static_cast<InsetBibitem *>(it->inset);
682         return 0;
683 }
684
685
686
687 // This could go to ParagraphParameters if we want to
688 int Paragraph::startTeXParParams(BufferParams const & bparams,
689                                  ostream & os, bool moving_arg) const
690 {
691         int column = 0;
692
693         if (params().noindent()) {
694                 os << "\\noindent ";
695                 column += 10;
696         }
697
698         switch (params().align()) {
699         case LYX_ALIGN_NONE:
700         case LYX_ALIGN_BLOCK:
701         case LYX_ALIGN_LAYOUT:
702         case LYX_ALIGN_SPECIAL:
703                 break;
704         case LYX_ALIGN_LEFT:
705         case LYX_ALIGN_RIGHT:
706         case LYX_ALIGN_CENTER:
707                 if (moving_arg) {
708                         os << "\\protect";
709                         column = 8;
710                 }
711                 break;
712         }
713
714         switch (params().align()) {
715         case LYX_ALIGN_NONE:
716         case LYX_ALIGN_BLOCK:
717         case LYX_ALIGN_LAYOUT:
718         case LYX_ALIGN_SPECIAL:
719                 break;
720         case LYX_ALIGN_LEFT:
721                 if (getParLanguage(bparams)->babel() != "hebrew") {
722                         os << "\\begin{flushleft}";
723                         column += 17;
724                 } else {
725                         os << "\\begin{flushright}";
726                         column += 18;
727                 }
728                 break;
729         case LYX_ALIGN_RIGHT:
730                 if (getParLanguage(bparams)->babel() != "hebrew") {
731                         os << "\\begin{flushright}";
732                         column += 18;
733                 } else {
734                         os << "\\begin{flushleft}";
735                         column += 17;
736                 }
737                 break;
738         case LYX_ALIGN_CENTER:
739                 os << "\\begin{center}";
740                 column += 14;
741                 break;
742         }
743
744         return column;
745 }
746
747
748 // This could go to ParagraphParameters if we want to
749 int Paragraph::endTeXParParams(BufferParams const & bparams,
750                                ostream & os, bool moving_arg) const
751 {
752         int column = 0;
753
754         switch (params().align()) {
755         case LYX_ALIGN_NONE:
756         case LYX_ALIGN_BLOCK:
757         case LYX_ALIGN_LAYOUT:
758         case LYX_ALIGN_SPECIAL:
759                 break;
760         case LYX_ALIGN_LEFT:
761         case LYX_ALIGN_RIGHT:
762         case LYX_ALIGN_CENTER:
763                 if (moving_arg) {
764                         os << "\\protect";
765                         column = 8;
766                 }
767                 break;
768         }
769
770         switch (params().align()) {
771         case LYX_ALIGN_NONE:
772         case LYX_ALIGN_BLOCK:
773         case LYX_ALIGN_LAYOUT:
774         case LYX_ALIGN_SPECIAL:
775                 break;
776         case LYX_ALIGN_LEFT:
777                 if (getParLanguage(bparams)->babel() != "hebrew") {
778                         os << "\\end{flushleft}";
779                         column = 15;
780                 } else {
781                         os << "\\end{flushright}";
782                         column = 16;
783                 }
784                 break;
785         case LYX_ALIGN_RIGHT:
786                 if (getParLanguage(bparams)->babel() != "hebrew") {
787                         os << "\\end{flushright}";
788                         column+= 16;
789                 } else {
790                         os << "\\end{flushleft}";
791                         column = 15;
792                 }
793                 break;
794         case LYX_ALIGN_CENTER:
795                 os << "\\end{center}";
796                 column = 12;
797                 break;
798         }
799         return column;
800 }
801
802
803 // This one spits out the text of the paragraph
804 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
805                                 BufferParams const & bparams,
806                                 LyXFont const & outerfont,
807                                 ostream & os, TexRow & texrow,
808                                 LatexRunParams const & runparams)
809 {
810         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...     " << this << endl;
811
812         bool return_value = false;
813
814         LyXLayout_ptr style;
815
816         // well we have to check if we are in an inset with unlimited
817         // lenght (all in one row) if that is true then we don't allow
818         // any special options in the paragraph and also we don't allow
819         // any environment other then "Standard" to be valid!
820         bool asdefault =
821                 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
822
823         if (asdefault) {
824                 style = bparams.getLyXTextClass().defaultLayout();
825         } else {
826                 style = layout();
827         }
828
829         LyXFont basefont;
830
831         // Maybe we have to create a optional argument.
832         pos_type body_pos;
833
834         // FIXME: can we actually skip this check and just call
835         // beginningOfBody() ??
836         if (style->labeltype != LABEL_MANUAL) {
837                 body_pos = 0;
838         } else {
839                 body_pos = beginningOfBody();
840         }
841
842         unsigned int column = 0;
843
844         if (body_pos > 0) {
845                 os << '[';
846                 ++column;
847                 basefont = getLabelFont(bparams, outerfont);
848         } else {
849                 basefont = getLayoutFont(bparams, outerfont);
850         }
851
852         bool moving_arg = runparams.moving_arg;
853         moving_arg |= style->needprotect;
854
855         // Which font is currently active?
856         LyXFont running_font(basefont);
857         // Do we have an open font change?
858         bool open_font = false;
859
860         Change::Type running_change = Change::UNCHANGED;
861
862         texrow.start(id(), 0);
863
864         // if the paragraph is empty, the loop will not be entered at all
865         if (empty()) {
866                 if (style->isCommand()) {
867                         os << '{';
868                         ++column;
869                 }
870                 if (!asdefault)
871                         column += startTeXParParams(bparams, os, moving_arg);
872
873         }
874
875         for (pos_type i = 0; i < size(); ++i) {
876                 ++column;
877                 // First char in paragraph or after label?
878                 if (i == body_pos) {
879                         if (body_pos > 0) {
880                                 if (open_font) {
881                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
882                                         open_font = false;
883                                 }
884                                 basefont = getLayoutFont(bparams, outerfont);
885                                 running_font = basefont;
886                                 os << ']';
887                                 ++column;
888                         }
889                         if (style->isCommand()) {
890                                 os << '{';
891                                 ++column;
892                         }
893
894                         if (!asdefault)
895                                 column += startTeXParParams(bparams, os,
896                                                             moving_arg);
897                 }
898
899                 value_type c = getChar(i);
900
901                 // Fully instantiated font
902                 LyXFont font = getFont(bparams, i, outerfont);
903
904                 LyXFont const last_font = running_font;
905
906                 // Spaces at end of font change are simulated to be
907                 // outside font change, i.e. we write "\textXX{text} "
908                 // rather than "\textXX{text }". (Asger)
909                 if (open_font && c == ' ' && i <= size() - 2) {
910                         LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
911                         if (next_font != running_font
912                             && next_font != font) {
913                                 font = next_font;
914                         }
915                 }
916
917                 // We end font definition before blanks
918                 if (open_font &&
919                     (font != running_font ||
920                      font.language() != running_font.language()))
921                 {
922                         column += running_font.latexWriteEndChanges(os,
923                                                                     basefont,
924                                                                     (i == body_pos-1) ? basefont : font);
925                         running_font = basefont;
926                         open_font = false;
927                 }
928
929                 // Blanks are printed before start of fontswitch
930                 if (c == ' ') {
931                         // Do not print the separation of the optional argument
932                         if (i != body_pos - 1) {
933                                 pimpl_->simpleTeXBlanks(os, texrow, i,
934                                                        column, font, *style);
935                         }
936                 }
937
938                 // Do we need to change font?
939                 if ((font != running_font ||
940                      font.language() != running_font.language()) &&
941                         i != body_pos - 1)
942                 {
943                         column += font.latexWriteStartChanges(os, basefont,
944                                                               last_font);
945                         running_font = font;
946                         open_font = true;
947                 }
948
949                 Change::Type change = pimpl_->lookupChange(i);
950
951                 column += Changes::latexMarkChange(os, running_change, change);
952                 running_change = change;
953
954                 LatexRunParams rp = runparams;
955                 rp.moving_arg = moving_arg;
956                 rp.free_spacing = style->free_spacing;
957                 pimpl_->simpleTeXSpecialChars(buf, bparams,
958                                               os, texrow, runparams,
959                                               font, running_font,
960                                               basefont, outerfont, open_font,
961                                               running_change,
962                                               *style, i, column, c);
963         }
964
965         column += Changes::latexMarkChange(os,
966                         running_change, Change::UNCHANGED);
967
968         // If we have an open font definition, we have to close it
969         if (open_font) {
970 #ifdef FIXED_LANGUAGE_END_DETECTION
971                 if (next_) {
972                         running_font
973                                 .latexWriteEndChanges(os, basefont,
974                                                       next_->getFont(bparams,
975                                                       0, outerfont));
976                 } else {
977                         running_font.latexWriteEndChanges(os, basefont,
978                                                           basefont);
979                 }
980 #else
981 #ifdef WITH_WARNINGS
982 //#warning For now we ALWAYS have to close the foreign font settings if they are
983 //#warning there as we start another \selectlanguage with the next paragraph if
984 //#warning we are in need of this. This should be fixed sometime (Jug)
985 #endif
986                 running_font.latexWriteEndChanges(os, basefont,  basefont);
987 #endif
988         }
989
990         // Needed if there is an optional argument but no contents.
991         if (body_pos > 0 && body_pos == size()) {
992                 os << "]~";
993                 return_value = false;
994         }
995
996         if (!asdefault) {
997                 column += endTeXParParams(bparams, os, moving_arg);
998         }
999
1000         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1001         return return_value;
1002 }
1003
1004
1005
1006
1007 bool Paragraph::isHfill(pos_type pos) const
1008 {
1009         return IsInsetChar(getChar(pos))
1010                && getInset(pos)->lyxCode() == InsetOld::HFILL_CODE;
1011 }
1012
1013
1014 bool Paragraph::isInset(pos_type pos) const
1015 {
1016         return IsInsetChar(getChar(pos));
1017 }
1018
1019
1020 bool Paragraph::isNewline(pos_type pos) const
1021 {
1022         return IsInsetChar(getChar(pos))
1023                && getInset(pos)->lyxCode() == InsetOld::NEWLINE_CODE;
1024 }
1025
1026
1027 bool Paragraph::isSeparator(pos_type pos) const
1028 {
1029         return IsSeparatorChar(getChar(pos));
1030 }
1031
1032
1033 bool Paragraph::isLineSeparator(pos_type pos) const
1034 {
1035         value_type const c = getChar(pos);
1036         return IsLineSeparatorChar(c)
1037                 || (IsInsetChar(c) && getInset(pos) &&
1038                 getInset(pos)->isLineSeparator());
1039 }
1040
1041
1042 bool Paragraph::isKomma(pos_type pos) const
1043 {
1044         return IsKommaChar(getChar(pos));
1045 }
1046
1047
1048 /// Used by the spellchecker
1049 bool Paragraph::isLetter(pos_type pos) const
1050 {
1051         value_type const c = getChar(pos);
1052         if (IsLetterChar(c))
1053                 return true;
1054         if (isInset(pos))
1055                 return getInset(pos)->isLetter();
1056         // We want to pass the ' and escape chars to ispell
1057         string const extra = lyxrc.isp_esc_chars + '\'';
1058         return contains(extra, c);
1059 }
1060
1061
1062 bool Paragraph::isWord(pos_type pos) const
1063 {
1064         return IsWordChar(getChar(pos)) ;
1065 }
1066
1067
1068 Language const *
1069 Paragraph::getParLanguage(BufferParams const & bparams) const
1070 {
1071         if (!empty())
1072                 return getFirstFontSettings().language();
1073 #warning FIXME we should check the prev par as well (Lgb)
1074         return bparams.language;
1075 }
1076
1077
1078 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1079 {
1080         return lyxrc.rtl_support
1081                 && getParLanguage(bparams)->RightToLeft()
1082                 && !(inInset() && inInset()->owner() &&
1083                      inInset()->owner()->lyxCode() == InsetOld::ERT_CODE);
1084 }
1085
1086
1087 void Paragraph::changeLanguage(BufferParams const & bparams,
1088                                Language const * from, Language const * to)
1089 {
1090         for (pos_type i = 0; i < size(); ++i) {
1091                 LyXFont font = getFontSettings(bparams, i);
1092                 if (font.language() == from) {
1093                         font.setLanguage(to);
1094                         setFont(i, font);
1095                 }
1096         }
1097 }
1098
1099
1100 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1101 {
1102         Language const * doc_language = bparams.language;
1103         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1104         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1105
1106         for (; cit != end; ++cit)
1107                 if (cit->font().language() != ignore_language &&
1108                     cit->font().language() != latex_language &&
1109                     cit->font().language() != doc_language)
1110                         return true;
1111         return false;
1112 }
1113
1114
1115 // Convert the paragraph to a string.
1116 // Used for building the table of contents
1117 string const Paragraph::asString(Buffer const & buffer, bool label) const
1118 {
1119 #if 0
1120         string s;
1121         if (label && !params().labelString().empty())
1122                 s += params().labelString() + ' ';
1123
1124         for (pos_type i = 0; i < size(); ++i) {
1125                 value_type c = getChar(i);
1126                 if (IsPrintable(c))
1127                         s += c;
1128                 else if (c == META_INSET &&
1129                          getInset(i)->lyxCode() == InsetOld::MATH_CODE) {
1130                         ostringstream ost;
1131                         getInset(i)->ascii(buffer, ost);
1132                         s += subst(STRCONV(ost.str()),'\n',' ');
1133                 }
1134         }
1135
1136         return s;
1137 #else
1138         // This should really be done by the caller and not here.
1139         string ret(asString(buffer, 0, size(), label));
1140         return subst(ret, '\n', ' ');
1141 #endif
1142 }
1143
1144
1145 string const Paragraph::asString(Buffer const & buffer,
1146                                  pos_type beg, pos_type end, bool label) const
1147 {
1148         ostringstream os;
1149
1150         if (beg == 0 && label && !params().labelString().empty())
1151                 os << params().labelString() << ' ';
1152
1153         for (pos_type i = beg; i < end; ++i) {
1154                 value_type const c = getUChar(buffer.params(), i);
1155                 if (IsPrintable(c))
1156                         os << c;
1157                 else if (c == META_INSET)
1158                         getInset(i)->ascii(buffer, os);
1159         }
1160
1161         return STRCONV(os.str());
1162 }
1163
1164
1165 void Paragraph::setInsetOwner(UpdatableInset * inset)
1166 {
1167         pimpl_->inset_owner = inset;
1168         InsetList::iterator it = insetlist.begin();
1169         InsetList::iterator end = insetlist.end();
1170         for (; it != end; ++it)
1171                 if (it->inset)
1172                         it->inset->setOwner(inset);
1173 }
1174
1175
1176 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1177 {
1178         insetlist.deleteInsetsLyXText(bv);
1179 }
1180
1181
1182 void Paragraph::setContentsFromPar(Paragraph const & par)
1183 {
1184         pimpl_->setContentsFromPar(par);
1185 }
1186
1187
1188 void Paragraph::trackChanges(Change::Type type)
1189 {
1190         pimpl_->trackChanges(type);
1191 }
1192
1193
1194 void Paragraph::untrackChanges()
1195 {
1196         pimpl_->untrackChanges();
1197 }
1198
1199
1200 void Paragraph::cleanChanges()
1201 {
1202         pimpl_->cleanChanges();
1203 }
1204
1205
1206 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1207 {
1208         Assert(!size() || pos < size());
1209         return pimpl_->lookupChange(pos);
1210 }
1211
1212
1213 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1214 {
1215         Assert(!size() || pos < size());
1216         return pimpl_->lookupChangeFull(pos);
1217 }
1218
1219
1220 bool Paragraph::isChanged(pos_type start, pos_type end) const
1221 {
1222         return pimpl_->isChanged(start, end);
1223 }
1224
1225
1226 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1227 {
1228         return pimpl_->isChangeEdited(start, end);
1229 }
1230
1231
1232 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1233 {
1234         pimpl_->setChange(pos, type);
1235
1236 }
1237
1238
1239 void Paragraph::markErased()
1240 {
1241         pimpl_->markErased();
1242 }
1243
1244
1245 void Paragraph::acceptChange(pos_type start, pos_type end)
1246 {
1247         return pimpl_->acceptChange(start, end);
1248 }
1249
1250
1251 void Paragraph::rejectChange(pos_type start, pos_type end)
1252 {
1253         return pimpl_->rejectChange(start, end);
1254 }
1255
1256
1257 lyx::pos_type Paragraph::size() const
1258 {
1259         return pimpl_->size();
1260 }
1261
1262
1263 bool Paragraph::empty() const
1264 {
1265         return pimpl_->empty();
1266 }
1267
1268
1269 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1270 {
1271         return pimpl_->getChar(pos);
1272 }
1273
1274
1275 int Paragraph::id() const
1276 {
1277         return pimpl_->id_;
1278 }
1279
1280
1281 void Paragraph::id(int i)
1282 {
1283         pimpl_->id_ = i;
1284 }
1285
1286
1287 LyXLayout_ptr const & Paragraph::layout() const
1288 {
1289 /*
1290         InsetOld * inset = inInset();
1291         if (inset && inset->lyxCode() == InsetOld::ENVIRONMENT_CODE)
1292                 return static_cast<InsetEnvironment*>(inset)->layout();
1293 */
1294         return layout_;
1295 }
1296
1297
1298 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1299 {
1300         layout_ = new_layout;
1301 }
1302
1303
1304 UpdatableInset * Paragraph::inInset() const
1305 {
1306         return pimpl_->inset_owner;
1307 }
1308
1309
1310 void Paragraph::clearContents()
1311 {
1312         pimpl_->clear();
1313 }
1314
1315 void Paragraph::setChar(pos_type pos, value_type c)
1316 {
1317         pimpl_->setChar(pos, c);
1318 }
1319
1320
1321 ParagraphParameters & Paragraph::params()
1322 {
1323         return pimpl_->params;
1324 }
1325
1326
1327 ParagraphParameters const & Paragraph::params() const
1328 {
1329         return pimpl_->params;
1330 }
1331
1332
1333 bool Paragraph::isFreeSpacing() const
1334 {
1335         if (layout()->free_spacing)
1336                 return true;
1337
1338         // for now we just need this, later should we need this in some
1339         // other way we can always add a function to InsetOld too.
1340         if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1341                 return pimpl_->inset_owner->owner()->lyxCode() == InsetOld::ERT_CODE;
1342         return false;
1343 }
1344
1345
1346 bool Paragraph::allowEmpty() const
1347 {
1348         if (layout()->keepempty)
1349                 return true;
1350         if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1351                 return pimpl_->inset_owner->owner()->lyxCode() == InsetOld::ERT_CODE;
1352         return false;
1353 }