]> git.lyx.org Git - lyx.git/blob - src/paragraph.C
some integer type changes for inset unification
[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 "counters.h"
26 #include "encoding.h"
27 #include "debug.h"
28 #include "gettext.h"
29 #include "language.h"
30 #include "lyxfont.h"
31 #include "lyxrc.h"
32 #include "lyxrow.h"
33 #include "outputparams.h"
34 #include "paragraph_funcs.h"
35 #include "sgml.h"
36 #include "texrow.h"
37 #include "vspace.h"
38
39 #include "insets/insetbibitem.h"
40 #include "insets/insetoptarg.h"
41
42 #include "support/lstrings.h"
43 #include "support/std_sstream.h"
44 #include "support/textutils.h"
45 #include "support/tostr.h"
46
47 #include <boost/tuple/tuple.hpp>
48 #include <boost/bind.hpp>
49
50 #include <list>
51 #include <stack>
52
53 using lyx::pos_type;
54
55 using lyx::support::contains;
56 using lyx::support::subst;
57
58 using std::endl;
59 using std::list;
60 using std::stack;
61 using std::string;
62 using std::ostream;
63 using std::ostringstream;
64
65
66 Paragraph::Paragraph()
67         : y(0), height(0), begin_of_body_(0),
68           pimpl_(new Paragraph::Pimpl(this))
69 {
70         itemdepth = 0;
71         params().clear();
72 }
73
74
75 Paragraph::Paragraph(Paragraph const & par)
76         : y(0), height(0), text_(par.text_), begin_of_body_(par.begin_of_body_),
77           pimpl_(new Paragraph::Pimpl(*par.pimpl_, this))
78 {
79         itemdepth = 0;
80         // this is because of the dummy layout of the paragraphs that
81         // follow footnotes
82         layout_ = par.layout();
83
84         // copy everything behind the break-position to the new paragraph
85         insetlist = par.insetlist;
86         InsetList::iterator it = insetlist.begin();
87         InsetList::iterator end = insetlist.end();
88         for (; it != end; ++it) {
89                 // currently we hold Inset*, not InsetBase*
90                 it->inset = static_cast<InsetOld*>(it->inset->clone().release());
91         }
92 }
93
94
95 void Paragraph::operator=(Paragraph const & par)
96 {
97         // needed as we will destroy the pimpl_ before copying it
98         if (&par != this)
99                 return;
100
101         lyxerr << "Paragraph::operator=()" << endl;
102
103         text_ = par.text_;
104
105         delete pimpl_;
106         pimpl_ = new Pimpl(*par.pimpl_, this);
107
108         itemdepth = par.itemdepth;
109         // this is because of the dummy layout of the paragraphs that
110         // follow footnotes
111         layout_ = par.layout();
112
113         // copy everything behind the break-position to the new paragraph
114         insetlist = par.insetlist;
115         InsetList::iterator it = insetlist.begin();
116         InsetList::iterator end = insetlist.end();
117         for (; it != end; ++it) {
118                 it->inset = static_cast<InsetOld*>(it->inset->clone().release());
119         }
120 }
121
122
123 Paragraph::~Paragraph()
124 {
125         delete pimpl_;
126         //
127         //lyxerr << "Paragraph::paragraph_id = "
128         //       << Paragraph::paragraph_id << endl;
129 }
130
131
132 void Paragraph::write(Buffer const & buf, ostream & os,
133                           BufferParams const & bparams,
134                           depth_type & dth) const
135 {
136         // The beginning or end of a deeper (i.e. nested) area?
137         if (dth != params().depth()) {
138                 if (params().depth() > dth) {
139                         while (params().depth() > dth) {
140                                 os << "\n\\begin_deeper ";
141                                 ++dth;
142                         }
143                 } else {
144                         while (params().depth() < dth) {
145                                 os << "\n\\end_deeper ";
146                                 --dth;
147                         }
148                 }
149         }
150
151         // First write the layout
152         os << "\n\\begin_layout " << layout()->name() << '\n';
153
154         params().write(os);
155
156         LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
157
158         Change running_change = Change(Change::UNCHANGED);
159         lyx::time_type const curtime(lyx::current_time());
160
161         int column = 0;
162         for (pos_type i = 0; i < size(); ++i) {
163                 if (!i) {
164                         os << '\n';
165                         column = 0;
166                 }
167
168                 Change change = pimpl_->lookupChangeFull(i);
169                 Changes::lyxMarkChange(os, column, curtime, running_change, change);
170                 running_change = change;
171
172                 // Write font changes
173                 LyXFont font2 = getFontSettings(bparams, i);
174                 if (font2 != font1) {
175                         font2.lyxWriteChanges(font1, os);
176                         column = 0;
177                         font1 = font2;
178                 }
179
180                 value_type const c = getChar(i);
181                 switch (c) {
182                 case META_INSET:
183                 {
184                         InsetOld const * inset = getInset(i);
185                         if (inset)
186                                 if (inset->directWrite()) {
187                                         // international char, let it write
188                                         // code directly so it's shorter in
189                                         // the file
190                                         inset->write(buf, os);
191                                 } else {
192                                         os << "\n\\begin_inset ";
193                                         inset->write(buf, os);
194                                         os << "\n\\end_inset \n\n";
195                                         column = 0;
196                                 }
197                 }
198                 break;
199                 case '\\':
200                         os << "\n\\backslash \n";
201                         column = 0;
202                         break;
203                 case '.':
204                         if (i + 1 < size() && getChar(i + 1) == ' ') {
205                                 os << ".\n";
206                                 column = 0;
207                         } else
208                                 os << '.';
209                         break;
210                 default:
211                         if ((column > 70 && c == ' ')
212                             || column > 79) {
213                                 os << '\n';
214                                 column = 0;
215                         }
216                         // this check is to amend a bug. LyX sometimes
217                         // inserts '\0' this could cause problems.
218                         if (c != '\0')
219                                 os << c;
220                         else
221                                 lyxerr << "ERROR (Paragraph::writeFile):"
222                                         " NULL char in structure." << endl;
223                         ++column;
224                         break;
225                 }
226         }
227
228         // to make reading work properly
229         if (!size()) {
230                 running_change = pimpl_->lookupChange(0);
231                 Changes::lyxMarkChange(os, column, curtime,
232                         Change(Change::UNCHANGED), running_change);
233         }
234         Changes::lyxMarkChange(os, column, curtime,
235                 running_change, Change(Change::UNCHANGED));
236
237         os << "\n\\end_layout\n";
238 }
239
240
241 void Paragraph::validate(LaTeXFeatures & features) const
242 {
243         pimpl_->validate(features, *layout());
244 }
245
246
247 void Paragraph::eraseIntern(lyx::pos_type pos)
248 {
249         pimpl_->eraseIntern(pos);
250 }
251
252
253 bool Paragraph::erase(pos_type pos)
254 {
255         return pimpl_->erase(pos);
256 }
257
258
259 int Paragraph::erase(pos_type start, pos_type end)
260 {
261         return pimpl_->erase(start, end);
262 }
263
264
265 void Paragraph::insert(pos_type start, string const & str,
266                        LyXFont const & font)
267 {
268         int size = str.size();
269         for (int i = 0 ; i < size ; ++i)
270                 insertChar(start + i, str[i], font);
271 }
272
273
274 bool Paragraph::checkInsertChar(LyXFont &)
275 {
276         return true;
277 }
278
279
280 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
281 {
282         insertChar(pos, c, LyXFont(LyXFont::ALL_INHERIT));
283 }
284
285
286 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
287                            LyXFont const & font, Change change)
288 {
289         pimpl_->insertChar(pos, c, font, change);
290 }
291
292
293 void Paragraph::insertInset(pos_type pos, InsetOld * inset)
294 {
295         insertInset(pos, inset, LyXFont(LyXFont::ALL_INHERIT));
296 }
297
298
299 void Paragraph::insertInset(pos_type pos, InsetOld * inset,
300         LyXFont const & font, Change change)
301 {
302         pimpl_->insertInset(pos, inset, font, change);
303 }
304
305
306 bool Paragraph::insetAllowed(InsetOld_code code)
307 {
308         //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
309         if (pimpl_->inset_owner)
310                 return pimpl_->inset_owner->insetAllowed(code);
311         return true;
312 }
313
314
315 InsetOld * Paragraph::getInset(pos_type pos)
316 {
317         BOOST_ASSERT(pos < size());
318         return insetlist.get(pos);
319 }
320
321
322 InsetOld const * Paragraph::getInset(pos_type pos) const
323 {
324         BOOST_ASSERT(pos < size());
325         return insetlist.get(pos);
326 }
327
328
329 // Gets uninstantiated font setting at position.
330 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
331                                          pos_type pos) const
332 {
333         BOOST_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                         break;
340
341         if (cit != end)
342                 return cit->font();
343
344         if (pos == size() && !empty())
345                 return getFontSettings(bparams, pos - 1);
346
347         return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
348 }
349
350
351 lyx::pos_type Paragraph::getEndPosOfFontSpan(lyx::pos_type pos) const
352 {
353         BOOST_ASSERT(pos <= size());
354
355         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
356         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
357         for (; cit != end; ++cit)
358                 if (cit->pos() >= pos)
359                         return cit->pos();
360
361         // This should not happen, but if so, we take no chances.
362         //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
363         //      << endl;
364         return pos;
365 }
366
367
368 // Gets uninstantiated font setting at position 0
369 LyXFont const Paragraph::getFirstFontSettings() const
370 {
371         if (!empty() && !pimpl_->fontlist.empty())
372                 return pimpl_->fontlist[0].font();
373
374         return LyXFont(LyXFont::ALL_INHERIT);
375 }
376
377
378 // Gets the fully instantiated font at a given position in a paragraph
379 // This is basically the same function as LyXText::GetFont() in text2.C.
380 // The difference is that this one is used for generating the LaTeX file,
381 // and thus cosmetic "improvements" are disallowed: This has to deliver
382 // the true picture of the buffer. (Asger)
383 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
384                                  LyXFont const & outerfont) const
385 {
386         BOOST_ASSERT(pos >= 0);
387
388         LyXLayout_ptr const & lout = layout();
389
390         pos_type const body_pos = beginOfBody();
391
392         LyXFont layoutfont;
393         if (pos < body_pos)
394                 layoutfont = lout->labelfont;
395         else
396                 layoutfont = lout->font;
397
398         LyXFont font = getFontSettings(bparams, pos);
399         font.realize(layoutfont);
400         font.realize(outerfont);
401         font.realize(bparams.getLyXTextClass().defaultfont());
402
403         return font;
404 }
405
406
407 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams,
408                                       LyXFont const & outerfont) const
409 {
410         LyXFont tmpfont = layout()->labelfont;
411         tmpfont.setLanguage(getParLanguage(bparams));
412         tmpfont.realize(outerfont);
413         tmpfont.realize(bparams.getLyXTextClass().defaultfont());
414         return tmpfont;
415 }
416
417
418 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams,
419                                        LyXFont const & outerfont) const
420 {
421         LyXFont tmpfont = layout()->font;
422         tmpfont.setLanguage(getParLanguage(bparams));
423         tmpfont.realize(outerfont);
424         tmpfont.realize(bparams.getLyXTextClass().defaultfont());
425         return tmpfont;
426 }
427
428
429 /// Returns the height of the highest font in range
430 LyXFont_size
431 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
432                               LyXFont_size def_size) const
433 {
434         if (pimpl_->fontlist.empty())
435                 return def_size;
436
437         Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
438         Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
439         for (; end_it != end; ++end_it) {
440                 if (end_it->pos() >= endpos)
441                         break;
442         }
443
444         if (end_it != end)
445                 ++end_it;
446
447         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
448         for (; cit != end; ++cit) {
449                 if (cit->pos() >= startpos)
450                         break;
451         }
452
453         LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
454         for (; cit != end_it; ++cit) {
455                 LyXFont::FONT_SIZE size = cit->font().size();
456                 if (size == LyXFont::INHERIT_SIZE)
457                         size = def_size;
458                 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
459                         maxsize = size;
460         }
461         return maxsize;
462 }
463
464
465 Paragraph::value_type
466 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
467 {
468         value_type c = getChar(pos);
469         if (!lyxrc.rtl_support)
470                 return c;
471
472         value_type uc = c;
473         switch (c) {
474         case '(':
475                 uc = ')';
476                 break;
477         case ')':
478                 uc = '(';
479                 break;
480         case '[':
481                 uc = ']';
482                 break;
483         case ']':
484                 uc = '[';
485                 break;
486         case '{':
487                 uc = '}';
488                 break;
489         case '}':
490                 uc = '{';
491                 break;
492         case '<':
493                 uc = '>';
494                 break;
495         case '>':
496                 uc = '<';
497                 break;
498         }
499         if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
500                 return uc;
501         else
502                 return c;
503 }
504
505
506 void Paragraph::setFont(pos_type pos, LyXFont const & font)
507 {
508         BOOST_ASSERT(pos <= size());
509
510         // First, reduce font against layout/label font
511         // Update: The setCharFont() routine in text2.C already
512         // reduces font, so we don't need to do that here. (Asger)
513         // No need to simplify this because it will disappear
514         // in a new kernel. (Asger)
515         // Next search font table
516
517         Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
518         Pimpl::FontList::iterator it = beg;
519         Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
520         for (; it != endit; ++it) {
521                 if (it->pos() >= pos)
522                         break;
523         }
524         unsigned int i = std::distance(beg, it);
525         bool notfound = (it == endit);
526
527         if (!notfound && pimpl_->fontlist[i].font() == font)
528                 return;
529
530         bool begin = pos == 0 || notfound ||
531                 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
532         // Is position pos is a beginning of a font block?
533         bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
534         // Is position pos is the end of a font block?
535         if (begin && end) { // A single char block
536                 if (i + 1 < pimpl_->fontlist.size() &&
537                     pimpl_->fontlist[i + 1].font() == font) {
538                         // Merge the singleton block with the next block
539                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
540                         if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
541                                 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
542                 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
543                         // Merge the singleton block with the previous block
544                         pimpl_->fontlist[i - 1].pos(pos);
545                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
546                 } else
547                         pimpl_->fontlist[i].font(font);
548         } else if (begin) {
549                 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
550                         pimpl_->fontlist[i - 1].pos(pos);
551                 else
552                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
553                                         Pimpl::FontTable(pos, font));
554         } else if (end) {
555                 pimpl_->fontlist[i].pos(pos - 1);
556                 if (!(i + 1 < pimpl_->fontlist.size() &&
557                       pimpl_->fontlist[i + 1].font() == font))
558                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
559                                         Pimpl::FontTable(pos, font));
560         } else { // The general case. The block is splitted into 3 blocks
561                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
562                                 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
563                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
564                                 Pimpl::FontTable(pos, font));
565         }
566 }
567
568
569 void Paragraph::makeSameLayout(Paragraph const & par)
570 {
571         layout(par.layout());
572         // move to pimpl?
573         params() = par.params();
574 }
575
576
577 int Paragraph::stripLeadingSpaces()
578 {
579         if (isFreeSpacing())
580                 return 0;
581
582         int i = 0;
583         while (!empty() && (isNewline(0) || isLineSeparator(0))) {
584                 pimpl_->eraseIntern(0);
585                 ++i;
586         }
587
588         return i;
589 }
590
591
592 bool Paragraph::hasSameLayout(Paragraph const & par) const
593 {
594         return par.layout() == layout() && params().sameLayout(par.params());
595 }
596
597
598 Paragraph::depth_type Paragraph::getDepth() const
599 {
600         return params().depth();
601 }
602
603
604 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
605 {
606         if (layout()->isEnvironment())
607                 return params().depth() + 1;
608         else
609                 return params().depth();
610 }
611
612
613 char Paragraph::getAlign() const
614 {
615         return params().align();
616 }
617
618
619 string const & Paragraph::getLabelstring() const
620 {
621         return params().labelString();
622 }
623
624
625 // the next two functions are for the manual labels
626 string const Paragraph::getLabelWidthString() const
627 {
628         if (!params().labelWidthString().empty())
629                 return params().labelWidthString();
630         else
631                 return _("Senseless with this layout!");
632 }
633
634
635 void Paragraph::setLabelWidthString(string const & s)
636 {
637         params().labelWidthString(s);
638 }
639
640
641 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
642 {
643         layout(new_layout);
644         params().labelWidthString(string());
645         params().align(LYX_ALIGN_LAYOUT);
646         params().spacing(Spacing(Spacing::Default));
647 }
648
649
650 int Paragraph::beginOfBody() const
651 {
652         return begin_of_body_;
653 }
654
655
656 void Paragraph::setBeginOfBody()
657 {
658         if (layout()->labeltype != LABEL_MANUAL) {
659                 begin_of_body_ = 0;
660                 return;
661         }
662
663         // Unroll the first two cycles of the loop
664         // and remember the previous character to
665         // remove unnecessary getChar() calls
666         pos_type i = 0;
667         pos_type end = size();
668         if (i < end && !isNewline(i)) {
669                 ++i;
670                 char previous_char = 0;
671                 char temp = 0;
672                 if (i < end) {
673                         previous_char = text_[i];
674                         if (!isNewline(i)) {
675                                 ++i;
676                                 while (i < end && previous_char != ' ') {
677                                         temp = text_[i];
678                                         if (isNewline(i))
679                                                 break;
680                                         ++i;
681                                         previous_char = temp;
682                                 }
683                         }
684                 }
685         }
686
687         begin_of_body_ = i;
688 }
689
690
691 // returns -1 if inset not found
692 int Paragraph::getPositionOfInset(InsetOld const * inset) const
693 {
694         // Find the entry.
695         InsetList::const_iterator it = insetlist.begin();
696         InsetList::const_iterator end = insetlist.end();
697         for (; it != end; ++it)
698                 if (it->inset == inset)
699                         return it->pos;
700         return -1;
701 }
702
703
704 InsetBibitem * Paragraph::bibitem() const
705 {
706         if (!insetlist.empty()) {
707                 InsetOld * inset = insetlist.begin()->inset;
708                 if (inset->lyxCode() == InsetOld::BIBTEX_CODE)
709                         return static_cast<InsetBibitem *>(inset);
710         }
711         return 0;
712 }
713
714
715 namespace {
716
717 // paragraphs inside floats need different alignment tags to avoid
718 // unwanted space 
719
720 bool noTrivlistCentering(UpdatableInset const * inset)
721 {
722         if (inset && inset->owner()) {
723                 InsetOld::Code const code = inset->owner()->lyxCode();
724                 return code == InsetOld::FLOAT_CODE ||
725                         code == InsetOld::WRAP_CODE;
726         }
727         return false;
728 }
729
730
731 string correction(string const & orig)
732 {
733         if (orig == "flushleft")
734                 return "raggedright";
735         if (orig == "flushright")
736                 return "raggedleft";
737         if (orig == "center")
738                 return "centering";
739         return orig;
740 }
741
742
743 string const corrected_env(string const & suffix, string const & env,
744                            UpdatableInset const * inset)
745 {
746         string output = suffix + "{";
747         if (noTrivlistCentering(inset))
748                 output += correction(env);
749         else
750                 output += env;
751         return output + "}";
752 }
753
754 } // namespace anon
755
756
757 // This could go to ParagraphParameters if we want to
758 int Paragraph::startTeXParParams(BufferParams const & bparams,
759                                  ostream & os, bool moving_arg) const
760 {
761         int column = 0;
762
763         if (params().noindent()) {
764                 os << "\\noindent ";
765                 column += 10;
766         }
767
768         switch (params().align()) {
769         case LYX_ALIGN_NONE:
770         case LYX_ALIGN_BLOCK:
771         case LYX_ALIGN_LAYOUT:
772         case LYX_ALIGN_SPECIAL:
773                 break;
774         case LYX_ALIGN_LEFT:
775         case LYX_ALIGN_RIGHT:
776         case LYX_ALIGN_CENTER:
777                 if (moving_arg) {
778                         os << "\\protect";
779                         column = 8;
780                 }
781                 break;
782         }
783
784         switch (params().align()) {
785         case LYX_ALIGN_NONE:
786         case LYX_ALIGN_BLOCK:
787         case LYX_ALIGN_LAYOUT:
788         case LYX_ALIGN_SPECIAL:
789                 break;
790         case LYX_ALIGN_LEFT: {
791                 string output;
792                 UpdatableInset const * const inset = pimpl_->inset_owner;
793                 if (getParLanguage(bparams)->babel() != "hebrew")
794                         output = corrected_env("\\begin", "flushleft", inset);
795                 else
796                         output = corrected_env("\\begin", "flushright", inset);
797                 os << output;
798                 column += output.size();
799                 break;
800         } case LYX_ALIGN_RIGHT: {
801                 string output;
802                 UpdatableInset const * const inset = pimpl_->inset_owner;
803                 if (getParLanguage(bparams)->babel() != "hebrew")
804                         output = corrected_env("\\begin", "flushright", inset);
805                 else
806                         output = corrected_env("\\begin", "flushleft", inset);
807                 os << output;
808                 column += output.size();
809                 break;
810         } case LYX_ALIGN_CENTER: {
811                 string output;
812                 output = corrected_env("\\begin", "center", pimpl_->inset_owner);
813                 os << output;
814                 column += output.size();
815                 break;
816         }
817         }
818
819         return column;
820 }
821
822
823 // This could go to ParagraphParameters if we want to
824 int Paragraph::endTeXParParams(BufferParams const & bparams,
825                                ostream & os, bool moving_arg) const
826 {
827         int column = 0;
828
829         switch (params().align()) {
830         case LYX_ALIGN_NONE:
831         case LYX_ALIGN_BLOCK:
832         case LYX_ALIGN_LAYOUT:
833         case LYX_ALIGN_SPECIAL:
834                 break;
835         case LYX_ALIGN_LEFT:
836         case LYX_ALIGN_RIGHT:
837         case LYX_ALIGN_CENTER:
838                 if (moving_arg) {
839                         os << "\\protect";
840                         column = 8;
841                 }
842                 break;
843         }
844
845         switch (params().align()) {
846         case LYX_ALIGN_NONE:
847         case LYX_ALIGN_BLOCK:
848         case LYX_ALIGN_LAYOUT:
849         case LYX_ALIGN_SPECIAL:
850                 break;
851         case LYX_ALIGN_LEFT: {
852                 string output;
853                 UpdatableInset const * const inset = pimpl_->inset_owner;
854                 if (getParLanguage(bparams)->babel() != "hebrew")
855                         output = corrected_env("\\par\\end", "flushleft", inset);
856                 else
857                         output = corrected_env("\\par\\end", "flushright", inset);
858                 os << output;
859                 column += output.size();
860                 break;
861         } case LYX_ALIGN_RIGHT: {
862                 string output;
863                 UpdatableInset const * const inset = pimpl_->inset_owner;
864                 if (getParLanguage(bparams)->babel() != "hebrew")
865                         output = corrected_env("\\par\\end", "flushright", inset);
866                 else
867                         output = corrected_env("\\par\\end", "flushleft", inset);
868                 os << output;
869                 column += output.size();
870                 break;
871         } case LYX_ALIGN_CENTER: {
872                 string output;
873                 output = corrected_env("\\par\\end", "center", pimpl_->inset_owner);
874                 os << output;
875                 column += output.size();
876                 break;
877         }
878         }
879
880         return column;
881 }
882
883
884 // This one spits out the text of the paragraph
885 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
886                                 BufferParams const & bparams,
887                                 LyXFont const & outerfont,
888                                 ostream & os, TexRow & texrow,
889                                 OutputParams const & runparams)
890 {
891         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...     " << this << endl;
892
893         bool return_value = false;
894
895         LyXLayout_ptr style;
896
897         // well we have to check if we are in an inset with unlimited
898         // length (all in one row) if that is true then we don't allow
899         // any special options in the paragraph and also we don't allow
900         // any environment other then "Standard" to be valid!
901         bool asdefault =
902                 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
903
904         if (asdefault) {
905                 style = bparams.getLyXTextClass().defaultLayout();
906         } else {
907                 style = layout();
908         }
909
910         LyXFont basefont;
911
912         // Maybe we have to create a optional argument.
913         pos_type body_pos = beginOfBody();
914         unsigned int column = 0;
915
916         if (body_pos > 0) {
917                 os << '[';
918                 ++column;
919                 basefont = getLabelFont(bparams, outerfont);
920         } else {
921                 basefont = getLayoutFont(bparams, outerfont);
922         }
923
924         bool moving_arg = runparams.moving_arg;
925         moving_arg |= style->needprotect;
926
927         // Which font is currently active?
928         LyXFont running_font(basefont);
929         // Do we have an open font change?
930         bool open_font = false;
931
932         Change::Type running_change = Change::UNCHANGED;
933
934         texrow.start(id(), 0);
935
936         // if the paragraph is empty, the loop will not be entered at all
937         if (empty()) {
938                 if (style->isCommand()) {
939                         os << '{';
940                         ++column;
941                 }
942                 if (!asdefault)
943                         column += startTeXParParams(bparams, os, moving_arg);
944         }
945
946         for (pos_type i = 0; i < size(); ++i) {
947                 ++column;
948                 // First char in paragraph or after label?
949                 if (i == body_pos) {
950                         if (body_pos > 0) {
951                                 if (open_font) {
952                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
953                                         open_font = false;
954                                 }
955                                 basefont = getLayoutFont(bparams, outerfont);
956                                 running_font = basefont;
957                                 os << ']';
958                                 ++column;
959                         }
960                         if (style->isCommand()) {
961                                 os << '{';
962                                 ++column;
963                         }
964
965                         if (!asdefault)
966                                 column += startTeXParParams(bparams, os,
967                                                             moving_arg);
968                 }
969
970                 value_type c = getChar(i);
971
972                 // Fully instantiated font
973                 LyXFont font = getFont(bparams, i, outerfont);
974
975                 LyXFont const last_font = running_font;
976
977                 // Spaces at end of font change are simulated to be
978                 // outside font change, i.e. we write "\textXX{text} "
979                 // rather than "\textXX{text }". (Asger)
980                 if (open_font && c == ' ' && i <= size() - 2) {
981                         LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
982                         if (next_font != running_font && next_font != font) {
983                                 font = next_font;
984                         }
985                 }
986
987                 // We end font definition before blanks
988                 if (open_font &&
989                     (font != running_font ||
990                      font.language() != running_font.language()))
991                 {
992                         column += running_font.latexWriteEndChanges(os,
993                                                                     basefont,
994                                                                     (i == body_pos-1) ? basefont : font);
995                         running_font = basefont;
996                         open_font = false;
997                 }
998
999                 // Blanks are printed before start of fontswitch
1000                 if (c == ' ') {
1001                         // Do not print the separation of the optional argument
1002                         if (i != body_pos - 1) {
1003                                 pimpl_->simpleTeXBlanks(os, texrow, i,
1004                                                        column, font, *style);
1005                         }
1006                 }
1007
1008                 // Do we need to change font?
1009                 if ((font != running_font ||
1010                      font.language() != running_font.language()) &&
1011                         i != body_pos - 1)
1012                 {
1013                         column += font.latexWriteStartChanges(os, basefont,
1014                                                               last_font);
1015                         running_font = font;
1016                         open_font = true;
1017                 }
1018
1019                 Change::Type change = pimpl_->lookupChange(i);
1020
1021                 column += Changes::latexMarkChange(os, running_change, change);
1022                 running_change = change;
1023
1024                 OutputParams rp = runparams;
1025                 rp.moving_arg = moving_arg;
1026                 rp.free_spacing = style->free_spacing;
1027                 pimpl_->simpleTeXSpecialChars(buf, bparams,
1028                                               os, texrow, runparams,
1029                                               font, running_font,
1030                                               basefont, outerfont, open_font,
1031                                               running_change,
1032                                               *style, i, column, c);
1033         }
1034
1035         column += Changes::latexMarkChange(os,
1036                         running_change, Change::UNCHANGED);
1037
1038         // If we have an open font definition, we have to close it
1039         if (open_font) {
1040 #ifdef FIXED_LANGUAGE_END_DETECTION
1041                 if (next_) {
1042                         running_font
1043                                 .latexWriteEndChanges(os, basefont,
1044                                                       next_->getFont(bparams,
1045                                                       0, outerfont));
1046                 } else {
1047                         running_font.latexWriteEndChanges(os, basefont,
1048                                                           basefont);
1049                 }
1050 #else
1051 #ifdef WITH_WARNINGS
1052 //#warning For now we ALWAYS have to close the foreign font settings if they are
1053 //#warning there as we start another \selectlanguage with the next paragraph if
1054 //#warning we are in need of this. This should be fixed sometime (Jug)
1055 #endif
1056                 running_font.latexWriteEndChanges(os, basefont,  basefont);
1057 #endif
1058         }
1059
1060         // Needed if there is an optional argument but no contents.
1061         if (body_pos > 0 && body_pos == size()) {
1062                 os << "]~";
1063                 return_value = false;
1064         }
1065
1066         if (!asdefault) {
1067                 column += endTeXParParams(bparams, os, moving_arg);
1068         }
1069
1070         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1071         return return_value;
1072 }
1073
1074
1075 namespace {
1076
1077 // checks, if newcol chars should be put into this line
1078 // writes newline, if necessary.
1079 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1080                           string::size_type newcol)
1081 {
1082         colcount += newcol;
1083         if (colcount > lyxrc.ascii_linelen) {
1084                 os << "\n";
1085                 colcount = newcol; // assume write after this call
1086         }
1087 }
1088
1089 enum PAR_TAG {
1090         NONE=0,
1091         TT = 1,
1092         SF = 2,
1093         BF = 4,
1094         IT = 8,
1095         SL = 16,
1096         EM = 32
1097 };
1098
1099
1100 string tag_name(PAR_TAG const & pt) {
1101         switch (pt) {
1102         case NONE: return "!-- --";
1103         case TT: return "tt";
1104         case SF: return "sf";
1105         case BF: return "bf";
1106         case IT: return "it";
1107         case SL: return "sl";
1108         case EM: return "em";
1109         }
1110         return "";
1111 }
1112
1113
1114 inline
1115 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1116 {
1117         p1 = static_cast<PAR_TAG>(p1 | p2);
1118 }
1119
1120
1121 inline
1122 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1123 {
1124         p1 = static_cast<PAR_TAG>(p1 & ~p2);
1125 }
1126
1127 } // anon
1128
1129
1130 // Handle internal paragraph parsing -- layout already processed.
1131 void Paragraph::simpleLinuxDocOnePar(Buffer const & buf,
1132                                      ostream & os,
1133                                      LyXFont const & outerfont,
1134                                      OutputParams const & runparams,
1135                                      lyx::depth_type /*depth*/) const
1136 {
1137         LyXLayout_ptr const & style = layout();
1138
1139         string::size_type char_line_count = 5;     // Heuristic choice ;-)
1140
1141         // gets paragraph main font
1142         LyXFont font_old;
1143         bool desc_on;
1144         if (style->labeltype == LABEL_MANUAL) {
1145                 font_old = style->labelfont;
1146                 desc_on = true;
1147         } else {
1148                 font_old = style->font;
1149                 desc_on = false;
1150         }
1151
1152         LyXFont::FONT_FAMILY family_type = LyXFont::ROMAN_FAMILY;
1153         LyXFont::FONT_SERIES series_type = LyXFont::MEDIUM_SERIES;
1154         LyXFont::FONT_SHAPE  shape_type  = LyXFont::UP_SHAPE;
1155         bool is_em = false;
1156
1157         stack<PAR_TAG> tag_state;
1158         // parsing main loop
1159         for (pos_type i = 0; i < size(); ++i) {
1160
1161                 PAR_TAG tag_close = NONE;
1162                 list < PAR_TAG > tag_open;
1163
1164                 LyXFont const font = getFont(buf.params(), i, outerfont);
1165
1166                 if (font_old.family() != font.family()) {
1167                         switch (family_type) {
1168                         case LyXFont::SANS_FAMILY:
1169                                 tag_close |= SF;
1170                                 break;
1171                         case LyXFont::TYPEWRITER_FAMILY:
1172                                 tag_close |= TT;
1173                                 break;
1174                         default:
1175                                 break;
1176                         }
1177
1178                         family_type = font.family();
1179
1180                         switch (family_type) {
1181                         case LyXFont::SANS_FAMILY:
1182                                 tag_open.push_back(SF);
1183                                 break;
1184                         case LyXFont::TYPEWRITER_FAMILY:
1185                                 tag_open.push_back(TT);
1186                                 break;
1187                         default:
1188                                 break;
1189                         }
1190                 }
1191
1192                 if (font_old.series() != font.series()) {
1193                         switch (series_type) {
1194                         case LyXFont::BOLD_SERIES:
1195                                 tag_close |= BF;
1196                                 break;
1197                         default:
1198                                 break;
1199                         }
1200
1201                         series_type = font.series();
1202
1203                         switch (series_type) {
1204                         case LyXFont::BOLD_SERIES:
1205                                 tag_open.push_back(BF);
1206                                 break;
1207                         default:
1208                                 break;
1209                         }
1210
1211                 }
1212
1213                 if (font_old.shape() != font.shape()) {
1214                         switch (shape_type) {
1215                         case LyXFont::ITALIC_SHAPE:
1216                                 tag_close |= IT;
1217                                 break;
1218                         case LyXFont::SLANTED_SHAPE:
1219                                 tag_close |= SL;
1220                                 break;
1221                         default:
1222                                 break;
1223                         }
1224
1225                         shape_type = font.shape();
1226
1227                         switch (shape_type) {
1228                         case LyXFont::ITALIC_SHAPE:
1229                                 tag_open.push_back(IT);
1230                                 break;
1231                         case LyXFont::SLANTED_SHAPE:
1232                                 tag_open.push_back(SL);
1233                                 break;
1234                         default:
1235                                 break;
1236                         }
1237                 }
1238                 // handle <em> tag
1239                 if (font_old.emph() != font.emph()) {
1240                         if (font.emph() == LyXFont::ON) {
1241                                 tag_open.push_back(EM);
1242                                 is_em = true;
1243                         }
1244                         else if (is_em) {
1245                                 tag_close |= EM;
1246                                 is_em = false;
1247                         }
1248                 }
1249
1250                 list < PAR_TAG > temp;
1251                 while (!tag_state.empty() && tag_close) {
1252                         PAR_TAG k =  tag_state.top();
1253                         tag_state.pop();
1254                         os << "</" << tag_name(k) << '>';
1255                         if (tag_close & k)
1256                                 reset(tag_close,k);
1257                         else
1258                                 temp.push_back(k);
1259                 }
1260
1261                 for(list< PAR_TAG >::const_iterator j = temp.begin();
1262                     j != temp.end(); ++j) {
1263                         tag_state.push(*j);
1264                         os << '<' << tag_name(*j) << '>';
1265                 }
1266
1267                 for(list< PAR_TAG >::const_iterator j = tag_open.begin();
1268                     j != tag_open.end(); ++j) {
1269                         tag_state.push(*j);
1270                         os << '<' << tag_name(*j) << '>';
1271                 }
1272
1273                 char c = getChar(i);
1274
1275
1276                 if (c == Paragraph::META_INSET) {
1277                         getInset(i)->linuxdoc(buf, os, runparams);
1278                         font_old = font;
1279                         continue;
1280                 }
1281
1282                 if (style->latexparam() == "CDATA") {
1283                         // "TeX"-Mode on == > SGML-Mode on.
1284                         if (c != '\0')
1285                                 os << c;
1286                         ++char_line_count;
1287                 } else {
1288                         bool ws;
1289                         string str;
1290                         boost::tie(ws, str) = sgml::escapeChar(c);
1291                         if (ws && !isFreeSpacing()) {
1292                                 // in freespacing mode, spaces are
1293                                 // non-breaking characters
1294                                 if (desc_on) { // if char is ' ' then...
1295                                         ++char_line_count;
1296                                         sgmlLineBreak(os, char_line_count, 6);
1297                                         os << "</tag>";
1298                                         desc_on = false;
1299                                 } else  {
1300                                         sgmlLineBreak(os, char_line_count, 1);
1301                                         os << c;
1302                                 }
1303                         } else {
1304                                 os << str;
1305                                 char_line_count += str.length();
1306                         }
1307                 }
1308                 font_old = font;
1309         }
1310
1311         while (!tag_state.empty()) {
1312                 os << "</" << tag_name(tag_state.top()) << '>';
1313                 tag_state.pop();
1314         }
1315
1316         // resets description flag correctly
1317         if (desc_on) {
1318                 // <tag> not closed...
1319                 sgmlLineBreak(os, char_line_count, 6);
1320                 os << "</tag>";
1321         }
1322 }
1323
1324
1325 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1326                                     ostream & os,
1327                                     LyXFont const & outerfont,
1328                                     OutputParams const & runparams,
1329                                     lyx::depth_type depth,
1330                                     bool labelid) const
1331 {
1332         bool emph_flag = false;
1333
1334         LyXLayout_ptr const & style = layout();
1335         LyXLayout_ptr const & defaultstyle =
1336                 buf.params().getLyXTextClass().defaultLayout();
1337
1338         LyXFont font_old =
1339                 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1340
1341         int char_line_count = depth;
1342         bool label_closed = true;
1343         bool para_closed = true;
1344         
1345         if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
1346                 string ls = "";
1347                 Counters & counters = buf.params().getLyXTextClass().counters();
1348                 if (!style->free_spacing)
1349                         os << string(depth,' ');
1350                 if (!style->labeltag().empty()) {
1351                         os << "<" << style->labeltag() << ">\n";
1352                         label_closed = false;
1353                 } else {
1354                         if (!defaultstyle->latexparam().empty()) {
1355                                 counters.step("para");
1356                                 ls = tostr(counters.value("para"));
1357                                 ls = " id=\"" 
1358                                         + subst(defaultstyle->latexparam(), "#", ls) + '"';
1359                         }
1360                         os << "<" << style->itemtag() << ">\n" 
1361                            << string(depth, ' ') << "<" 
1362                            << defaultstyle->latexname() << ls << ">\n";
1363                         para_closed = false;
1364                 }
1365         }
1366
1367         // parsing main loop
1368         for (pos_type i = 0; i < size(); ++i) {
1369                 LyXFont font = getFont(buf.params(), i, outerfont);
1370
1371                 // handle <emphasis> tag
1372                 if (font_old.emph() != font.emph()) {
1373                         if (font.emph() == LyXFont::ON) {
1374                                 if (style->latexparam() == "CDATA")
1375                                         os << "]]>";
1376                                 os << "<emphasis>";
1377                                 if (style->latexparam() == "CDATA")
1378                                         os << "<![CDATA[";
1379                                 emph_flag = true;
1380                         } else if (i) {
1381                                 if (style->latexparam() == "CDATA")
1382                                         os << "]]>";
1383                                 os << "</emphasis>";
1384                                 if (style->latexparam() == "CDATA")
1385                                         os << "<![CDATA[";
1386                                 emph_flag = false;
1387                         }
1388                 }
1389
1390
1391                 if (isInset(i)) {
1392                         InsetOld const * inset = getInset(i);
1393                         // don't print the inset in position 0 if desc_on == 3 (label)
1394                         //if (i || desc_on != 3) {
1395                         if (!labelid) {
1396                                 if (style->latexparam() == "CDATA")
1397                                         os << "]]>";
1398                                 inset->docbook(buf, os, runparams);
1399                                 if (style->latexparam() == "CDATA")
1400                                         os << "<![CDATA[";
1401                         }
1402                 } else {
1403                         char c = getChar(i);
1404                         bool ws;
1405                         string str;
1406                         boost::tie(ws, str) = sgml::escapeChar(c);
1407
1408                         if (style->pass_thru) {
1409                                 os << c;
1410                         } else if (isFreeSpacing() || c != ' ') {
1411                                         os << str;
1412                         } else if (!style->labeltag().empty() && !label_closed) {
1413                                 ++char_line_count;
1414                                 os << "\n</" << style->labeltag() << "><" 
1415                                    << style->itemtag() << "><" 
1416                                    << defaultstyle->latexname() << ">";
1417                                 label_closed = true;
1418                                 para_closed = false;
1419                         } else {
1420                                 os << ' ';
1421                         }
1422                 }
1423                 font_old = font;
1424         }
1425
1426         if (emph_flag) {
1427                 if (style->latexparam() == "CDATA")
1428                         os << "]]>";
1429                 os << "</emphasis>";
1430                 if (style->latexparam() == "CDATA")
1431                         os << "<![CDATA[";
1432         }
1433
1434         // resets description flag correctly
1435         if (!label_closed) {
1436                 // <term> not closed...
1437                 os << "</" << style->labeltag() << ">\n<" 
1438                    << style->itemtag() << "><" 
1439                    << defaultstyle->latexname() << ">&nbsp;";
1440         }
1441         if (!para_closed) {
1442                 os << "\n" << string(depth, ' ') << "</" 
1443                    << defaultstyle->latexname() << ">\n";
1444         }
1445         if (style->free_spacing)
1446                 os << '\n';
1447 }
1448
1449
1450 namespace {
1451
1452 /// return true if the char is a meta-character for an inset
1453 inline
1454 bool IsInsetChar(char c)
1455 {
1456         return (c == Paragraph::META_INSET);
1457 }
1458
1459 } // namespace anon
1460
1461
1462
1463 bool Paragraph::isHfill(pos_type pos) const
1464 {
1465         return IsInsetChar(getChar(pos))
1466                && getInset(pos)->lyxCode() == InsetOld::HFILL_CODE;
1467 }
1468
1469
1470 bool Paragraph::isInset(pos_type pos) const
1471 {
1472         return IsInsetChar(getChar(pos));
1473 }
1474
1475
1476 bool Paragraph::isNewline(pos_type pos) const
1477 {
1478         return IsInsetChar(getChar(pos))
1479                && getInset(pos)->lyxCode() == InsetOld::NEWLINE_CODE;
1480 }
1481
1482
1483 bool Paragraph::isSeparator(pos_type pos) const
1484 {
1485         return IsSeparatorChar(getChar(pos));
1486 }
1487
1488
1489 bool Paragraph::isLineSeparator(pos_type pos) const
1490 {
1491         value_type const c = getChar(pos);
1492         return IsLineSeparatorChar(c)
1493                 || (IsInsetChar(c) && getInset(pos) &&
1494                 getInset(pos)->isLineSeparator());
1495 }
1496
1497
1498 bool Paragraph::isKomma(pos_type pos) const
1499 {
1500         return IsKommaChar(getChar(pos));
1501 }
1502
1503
1504 /// Used by the spellchecker
1505 bool Paragraph::isLetter(pos_type pos) const
1506 {
1507         value_type const c = getChar(pos);
1508         if (IsLetterChar(c))
1509                 return true;
1510         if (isInset(pos))
1511                 return getInset(pos)->isLetter();
1512         // We want to pass the ' and escape chars to ispell
1513         string const extra = lyxrc.isp_esc_chars + '\'';
1514         return contains(extra, c);
1515 }
1516
1517
1518 bool Paragraph::isWord(pos_type pos) const
1519 {
1520         unsigned char const c = getChar(pos);
1521         return !(IsSeparatorChar(c)
1522                   || IsKommaChar(c)
1523                   || IsInsetChar(c));
1524 }
1525
1526
1527 Language const *
1528 Paragraph::getParLanguage(BufferParams const & bparams) const
1529 {
1530         if (!empty())
1531                 return getFirstFontSettings().language();
1532 #warning FIXME we should check the prev par as well (Lgb)
1533         return bparams.language;
1534 }
1535
1536
1537 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1538 {
1539         return lyxrc.rtl_support
1540                 && getParLanguage(bparams)->RightToLeft()
1541                 && !(inInset() && inInset()->owner() &&
1542                      inInset()->owner()->lyxCode() == InsetOld::ERT_CODE);
1543 }
1544
1545
1546 void Paragraph::changeLanguage(BufferParams const & bparams,
1547                                Language const * from, Language const * to)
1548 {
1549         for (pos_type i = 0; i < size(); ++i) {
1550                 LyXFont font = getFontSettings(bparams, i);
1551                 if (font.language() == from) {
1552                         font.setLanguage(to);
1553                         setFont(i, font);
1554                 }
1555         }
1556 }
1557
1558
1559 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1560 {
1561         Language const * doc_language = bparams.language;
1562         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1563         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1564
1565         for (; cit != end; ++cit)
1566                 if (cit->font().language() != ignore_language &&
1567                     cit->font().language() != latex_language &&
1568                     cit->font().language() != doc_language)
1569                         return true;
1570         return false;
1571 }
1572
1573
1574 // Convert the paragraph to a string.
1575 // Used for building the table of contents
1576 string const Paragraph::asString(Buffer const & buffer,
1577                                  bool label) const
1578 {
1579         OutputParams runparams;
1580         return asString(buffer, runparams, label);
1581 }
1582
1583
1584 string const Paragraph::asString(Buffer const & buffer,
1585                                  OutputParams const & runparams,
1586                                  bool label) const
1587 {
1588 #if 0
1589         string s;
1590         if (label && !params().labelString().empty())
1591                 s += params().labelString() + ' ';
1592
1593         for (pos_type i = 0; i < size(); ++i) {
1594                 value_type c = getChar(i);
1595                 if (IsPrintable(c))
1596                         s += c;
1597                 else if (c == META_INSET &&
1598                          getInset(i)->lyxCode() == InsetOld::MATH_CODE) {
1599                         ostringstream os;
1600                         getInset(i)->plaintext(buffer, os, runparams);
1601                         s += subst(STRCONV(os.str()),'\n',' ');
1602                 }
1603         }
1604
1605         return s;
1606 #else
1607         // This should really be done by the caller and not here.
1608         string ret = asString(buffer, runparams, 0, size(), label);
1609         return subst(ret, '\n', ' ');
1610 #endif
1611 }
1612
1613
1614 string const Paragraph::asString(Buffer const & buffer,
1615                                  pos_type beg, pos_type end, bool label) const
1616 {
1617
1618         OutputParams const runparams;
1619         return asString(buffer, runparams, beg, end, label);
1620 }
1621
1622
1623 string const Paragraph::asString(Buffer const & buffer,
1624                                  OutputParams const & runparams,
1625                                  pos_type beg, pos_type end, bool label) const
1626 {
1627         ostringstream os;
1628
1629         if (beg == 0 && label && !params().labelString().empty())
1630                 os << params().labelString() << ' ';
1631
1632         for (pos_type i = beg; i < end; ++i) {
1633                 value_type const c = getUChar(buffer.params(), i);
1634                 if (IsPrintable(c))
1635                         os << c;
1636                 else if (c == META_INSET)
1637                         getInset(i)->plaintext(buffer, os, runparams);
1638         }
1639
1640         return os.str();
1641 }
1642
1643
1644 void Paragraph::setInsetOwner(UpdatableInset * inset)
1645 {
1646         pimpl_->inset_owner = inset;
1647         InsetList::iterator it = insetlist.begin();
1648         InsetList::iterator end = insetlist.end();
1649         for (; it != end; ++it)
1650                 if (it->inset)
1651                         it->inset->setOwner(inset);
1652 }
1653
1654
1655 void Paragraph::setContentsFromPar(Paragraph const & par)
1656 {
1657         pimpl_->setContentsFromPar(par);
1658 }
1659
1660
1661 void Paragraph::trackChanges(Change::Type type)
1662 {
1663         pimpl_->trackChanges(type);
1664 }
1665
1666
1667 void Paragraph::untrackChanges()
1668 {
1669         pimpl_->untrackChanges();
1670 }
1671
1672
1673 void Paragraph::cleanChanges()
1674 {
1675         pimpl_->cleanChanges();
1676 }
1677
1678
1679 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1680 {
1681         BOOST_ASSERT(!size() || pos < size());
1682         return pimpl_->lookupChange(pos);
1683 }
1684
1685
1686 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1687 {
1688         BOOST_ASSERT(!size() || pos < size());
1689         return pimpl_->lookupChangeFull(pos);
1690 }
1691
1692
1693 bool Paragraph::isChanged(pos_type start, pos_type end) const
1694 {
1695         return pimpl_->isChanged(start, end);
1696 }
1697
1698
1699 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1700 {
1701         return pimpl_->isChangeEdited(start, end);
1702 }
1703
1704
1705 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1706 {
1707         pimpl_->setChange(pos, type);
1708
1709 }
1710
1711
1712 void Paragraph::markErased()
1713 {
1714         pimpl_->markErased();
1715 }
1716
1717
1718 void Paragraph::acceptChange(pos_type start, pos_type end)
1719 {
1720         return pimpl_->acceptChange(start, end);
1721 }
1722
1723
1724 void Paragraph::rejectChange(pos_type start, pos_type end)
1725 {
1726         return pimpl_->rejectChange(start, end);
1727 }
1728
1729
1730 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1731 {
1732         // This is in the critical path!
1733         pos_type const siz = text_.size();
1734
1735         BOOST_ASSERT(0 <= pos);
1736         BOOST_ASSERT(pos <= siz);
1737
1738         if (pos == siz) {
1739                 lyxerr << "getChar() on pos " << pos << " in par id "
1740                        << id() << " of size " << siz
1741                        << "  is a bit silly !" << endl;
1742                 BOOST_ASSERT(false);
1743                 return '\0';
1744         }
1745
1746         return text_[pos];
1747 }
1748
1749
1750 int Paragraph::id() const
1751 {
1752         return pimpl_->id_;
1753 }
1754
1755
1756 LyXLayout_ptr const & Paragraph::layout() const
1757 {
1758 /*
1759         InsetOld * inset = inInset();
1760         if (inset && inset->lyxCode() == InsetOld::ENVIRONMENT_CODE)
1761                 return static_cast<InsetEnvironment*>(inset)->layout();
1762 */
1763         return layout_;
1764 }
1765
1766
1767 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1768 {
1769         layout_ = new_layout;
1770 }
1771
1772
1773 UpdatableInset * Paragraph::inInset() const
1774 {
1775         return pimpl_->inset_owner;
1776 }
1777
1778
1779 void Paragraph::clearContents()
1780 {
1781         text_.clear();
1782 }
1783
1784
1785 void Paragraph::setChar(pos_type pos, value_type c)
1786 {
1787         text_[pos] = c;
1788 }
1789
1790
1791 ParagraphParameters & Paragraph::params()
1792 {
1793         return pimpl_->params;
1794 }
1795
1796
1797 ParagraphParameters const & Paragraph::params() const
1798 {
1799         return pimpl_->params;
1800 }
1801
1802
1803 bool Paragraph::isFreeSpacing() const
1804 {
1805         if (layout()->free_spacing)
1806                 return true;
1807
1808         // for now we just need this, later should we need this in some
1809         // other way we can always add a function to InsetOld too.
1810         if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1811                 return pimpl_->inset_owner->owner()->lyxCode() == InsetOld::ERT_CODE;
1812         return false;
1813 }
1814
1815
1816 bool Paragraph::allowEmpty() const
1817 {
1818         if (layout()->keepempty)
1819                 return true;
1820         if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1821                 return pimpl_->inset_owner->owner()->lyxCode() == InsetOld::ERT_CODE;
1822         return false;
1823 }
1824
1825
1826 RowList::iterator Paragraph::getRow(pos_type pos)
1827 {
1828         RowList::iterator rit = rows.end();
1829         RowList::iterator const begin = rows.begin();
1830
1831         for (--rit; rit != begin && rit->pos() > pos; --rit)
1832                 ;
1833
1834         return rit;
1835 }
1836
1837
1838 unsigned char Paragraph::transformChar(unsigned char c, pos_type pos) const
1839 {
1840         if (!Encodings::is_arabic(c))
1841                 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && IsDigit(c))
1842                         return c + (0xb0 - '0');
1843                 else
1844                         return c;
1845
1846         unsigned char const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1847         unsigned char next_char = ' ';
1848
1849         for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1850                 unsigned char const par_char = getChar(i);
1851                 if (!Encodings::IsComposeChar_arabic(par_char)) {
1852                         next_char = par_char;
1853                         break;
1854                 }
1855         }
1856
1857         if (Encodings::is_arabic(next_char)) {
1858                 if (Encodings::is_arabic(prev_char) &&
1859                         !Encodings::is_arabic_special(prev_char))
1860                         return Encodings::TransformChar(c, Encodings::FORM_MEDIAL);
1861                 else
1862                         return Encodings::TransformChar(c, Encodings::FORM_INITIAL);
1863         } else {
1864                 if (Encodings::is_arabic(prev_char) &&
1865                         !Encodings::is_arabic_special(prev_char))
1866                         return Encodings::TransformChar(c, Encodings::FORM_FINAL);
1867                 else
1868                         return Encodings::TransformChar(c, Encodings::FORM_ISOLATED);
1869         }
1870 }