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