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