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