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