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