]> git.lyx.org Git - lyx.git/blob - src/paragraph.C
minimal effort implementation of:
[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 #include "support/unicode.h"
48
49 #include <boost/tuple/tuple.hpp>
50 #include <boost/bind.hpp>
51
52 #include <algorithm>
53 #include <list>
54 #include <stack>
55 #include <sstream>
56
57 using lyx::pos_type;
58 using lyx::char_type;
59
60 using lyx::support::subst;
61
62 using std::distance;
63 using std::endl;
64 using std::list;
65 using std::stack;
66 using std::string;
67 using std::ostream;
68 using std::ostringstream;
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_), rowSignature_(par.rowSignature_),
83                 layout_(par.layout_),
84                 text_(par.text_), begin_of_body_(par.begin_of_body_),
85           pimpl_(new Paragraph::Pimpl(*par.pimpl_, this))
86 {
87         //lyxerr << "Paragraph::Paragraph(Paragraph const&)" << endl;
88         InsetList::iterator it = insetlist.begin();
89         InsetList::iterator end = insetlist.end();
90         for (; it != end; ++it)
91                 it->inset = it->inset->clone().release();
92 }
93
94
95 Paragraph & Paragraph::operator=(Paragraph const & par)
96 {
97         // needed as we will destroy the pimpl_ before copying it
98         if (&par != this) {
99                 itemdepth = par.itemdepth;
100
101                 insetlist = par.insetlist;
102                 InsetList::iterator it = insetlist.begin();
103                 InsetList::iterator end = insetlist.end();
104                 for (; it != end; ++it)
105                         it->inset = it->inset->clone().release();
106
107                 rows_ = par.rows_;
108                 dim_ = par.dim_;
109                 rowSignature_ = par.rowSignature_;
110                 layout_ = par.layout();
111                 text_ = par.text_;
112                 begin_of_body_ = par.begin_of_body_;
113
114                 delete pimpl_;
115                 pimpl_ = new Pimpl(*par.pimpl_, this);
116         }
117         return *this;
118 }
119
120
121 Paragraph::~Paragraph()
122 {
123         delete pimpl_;
124         //
125         //lyxerr << "Paragraph::paragraph_id = "
126         //       << Paragraph::paragraph_id << endl;
127 }
128
129
130 void Paragraph::write(Buffer const & buf, ostream & os,
131                           BufferParams const & bparams,
132                           depth_type & dth) const
133 {
134         // The beginning or end of a deeper (i.e. nested) area?
135         if (dth != params().depth()) {
136                 if (params().depth() > dth) {
137                         while (params().depth() > dth) {
138                                 os << "\n\\begin_deeper";
139                                 ++dth;
140                         }
141                 } else {
142                         while (params().depth() < dth) {
143                                 os << "\n\\end_deeper";
144                                 --dth;
145                         }
146                 }
147         }
148
149         // First write the layout
150         os << "\n\\begin_layout " << layout()->name() << '\n';
151
152         params().write(os);
153
154         LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
155
156         Change running_change = Change(Change::UNCHANGED);
157         lyx::time_type const curtime(lyx::current_time());
158
159         int column = 0;
160         for (pos_type i = 0; i <= size(); ++i) {
161
162                 Change change = pimpl_->lookupChange(i);
163                 Changes::lyxMarkChange(os, column, curtime, running_change, change);
164                 running_change = change;
165
166                 if (i == size())
167                         break;
168
169                 // Write font changes
170                 LyXFont font2 = getFontSettings(bparams, i);
171                 if (font2 != font1) {
172                         font2.lyxWriteChanges(font1, os);
173                         column = 0;
174                         font1 = font2;
175                 }
176
177                 value_type const c = getChar(i);
178                 switch (c) {
179                 case META_INSET:
180                 {
181                         InsetBase const * inset = getInset(i);
182                         if (inset)
183                                 if (inset->directWrite()) {
184                                         // international char, let it write
185                                         // code directly so it's shorter in
186                                         // the file
187                                         inset->write(buf, os);
188                                 } else {
189                                         if (i)
190                                                 os << '\n';
191                                         os << "\\begin_inset ";
192                                         inset->write(buf, os);
193                                         os << "\n\\end_inset\n\n";
194                                         column = 0;
195                                 }
196                 }
197                 break;
198                 case '\\':
199                         os << "\n\\backslash\n";
200                         column = 0;
201                         break;
202                 case '.':
203                         if (i + 1 < size() && getChar(i + 1) == ' ') {
204                                 os << ".\n";
205                                 column = 0;
206                         } else
207                                 os << '.';
208                         break;
209                 default:
210                         if ((column > 70 && c == ' ')
211                             || column > 79) {
212                                 os << '\n';
213                                 column = 0;
214                         }
215                         // this check is to amend a bug. LyX sometimes
216                         // inserts '\0' this could cause problems.
217                         if (c != '\0') {
218                                 std::vector<char> tmp = ucs4_to_utf8(c);
219                                 tmp.push_back('\0');
220                                 os << &tmp[0];
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.getFont());
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.getFont());
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.getFont());
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                 && (lookupChange(0).type != Change::DELETED)) {
568                 erase(0);
569                 ++i;
570         }
571
572         return i;
573 }
574
575
576 bool Paragraph::hasSameLayout(Paragraph const & par) const
577 {
578         return par.layout() == layout() && params().sameLayout(par.params());
579 }
580
581
582 Paragraph::depth_type Paragraph::getDepth() const
583 {
584         return params().depth();
585 }
586
587
588 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
589 {
590         if (layout()->isEnvironment())
591                 return params().depth() + 1;
592         else
593                 return params().depth();
594 }
595
596
597 char Paragraph::getAlign() const
598 {
599         return params().align();
600 }
601
602
603 string const & Paragraph::getLabelstring() const
604 {
605         return params().labelString();
606 }
607
608
609 // the next two functions are for the manual labels
610 string const Paragraph::getLabelWidthString() const
611 {
612         if (!params().labelWidthString().empty())
613                 return params().labelWidthString();
614         else
615                 return _("Senseless with this layout!");
616 }
617
618
619 void Paragraph::setLabelWidthString(string const & s)
620 {
621         params().labelWidthString(s);
622 }
623
624
625 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
626 {
627         layout(new_layout);
628         params().labelWidthString(string());
629         params().align(LYX_ALIGN_LAYOUT);
630         params().spacing(Spacing(Spacing::Default));
631 }
632
633
634 pos_type Paragraph::beginOfBody() const
635 {
636         return begin_of_body_;
637 }
638
639
640 void Paragraph::setBeginOfBody()
641 {
642         if (layout()->labeltype != LABEL_MANUAL) {
643                 begin_of_body_ = 0;
644                 return;
645         }
646
647         // Unroll the first two cycles of the loop
648         // and remember the previous character to
649         // remove unnecessary getChar() calls
650         pos_type i = 0;
651         pos_type end = size();
652         if (i < end && !isNewline(i)) {
653                 ++i;
654                 char previous_char = 0;
655                 char temp = 0;
656                 if (i < end) {
657                         previous_char = text_[i];
658                         if (!isNewline(i)) {
659                                 ++i;
660                                 while (i < end && previous_char != ' ') {
661                                         temp = text_[i];
662                                         if (isNewline(i))
663                                                 break;
664                                         ++i;
665                                         previous_char = temp;
666                                 }
667                         }
668                 }
669         }
670
671         begin_of_body_ = i;
672 }
673
674
675 // returns -1 if inset not found
676 int Paragraph::getPositionOfInset(InsetBase const * inset) const
677 {
678         // Find the entry.
679         InsetList::const_iterator it = insetlist.begin();
680         InsetList::const_iterator end = insetlist.end();
681         for (; it != end; ++it)
682                 if (it->inset == inset)
683                         return it->pos;
684         return -1;
685 }
686
687
688 InsetBibitem * Paragraph::bibitem() const
689 {
690         if (!insetlist.empty()) {
691                 InsetBase * inset = insetlist.begin()->inset;
692                 if (inset->lyxCode() == InsetBase::BIBITEM_CODE)
693                         return static_cast<InsetBibitem *>(inset);
694         }
695         return 0;
696 }
697
698
699 bool Paragraph::forceDefaultParagraphs() const
700 {
701         return inInset() && inInset()->forceDefaultParagraphs(0);
702 }
703
704
705 namespace {
706
707 // paragraphs inside floats need different alignment tags to avoid
708 // unwanted space
709
710 bool noTrivlistCentering(InsetBase::Code code)
711 {
712         return code == InsetBase::FLOAT_CODE || code == InsetBase::WRAP_CODE;
713 }
714
715
716 string correction(string const & orig)
717 {
718         if (orig == "flushleft")
719                 return "raggedright";
720         if (orig == "flushright")
721                 return "raggedleft";
722         if (orig == "center")
723                 return "centering";
724         return orig;
725 }
726
727
728 string const corrected_env(string const & suffix, string const & env,
729         InsetBase::Code code)
730 {
731         string output = suffix + "{";
732         if (noTrivlistCentering(code))
733                 output += correction(env);
734         else
735                 output += env;
736         return output + "}";
737 }
738
739 } // namespace anon
740
741
742 // This could go to ParagraphParameters if we want to
743 int Paragraph::startTeXParParams(BufferParams const & bparams,
744                                  ostream & os, bool moving_arg) const
745 {
746         int column = 0;
747
748         if (params().noindent()) {
749                 os << "\\noindent ";
750                 column += 10;
751         }
752
753         switch (params().align()) {
754         case LYX_ALIGN_NONE:
755         case LYX_ALIGN_BLOCK:
756         case LYX_ALIGN_LAYOUT:
757         case LYX_ALIGN_SPECIAL:
758                 break;
759         case LYX_ALIGN_LEFT:
760         case LYX_ALIGN_RIGHT:
761         case LYX_ALIGN_CENTER:
762                 if (moving_arg) {
763                         os << "\\protect";
764                         column += 8;
765                 }
766                 break;
767         }
768
769         switch (params().align()) {
770         case LYX_ALIGN_NONE:
771         case LYX_ALIGN_BLOCK:
772         case LYX_ALIGN_LAYOUT:
773         case LYX_ALIGN_SPECIAL:
774                 break;
775         case LYX_ALIGN_LEFT: {
776                 string output;
777                 if (getParLanguage(bparams)->babel() != "hebrew")
778                         output = corrected_env("\\begin", "flushleft", ownerCode());
779                 else
780                         output = corrected_env("\\begin", "flushright", ownerCode());
781                 os << output;
782                 column += output.size();
783                 break;
784         } case LYX_ALIGN_RIGHT: {
785                 string output;
786                 if (getParLanguage(bparams)->babel() != "hebrew")
787                         output = corrected_env("\\begin", "flushright", ownerCode());
788                 else
789                         output = corrected_env("\\begin", "flushleft", ownerCode());
790                 os << output;
791                 column += output.size();
792                 break;
793         } case LYX_ALIGN_CENTER: {
794                 string output;
795                 output = corrected_env("\\begin", "center", ownerCode());
796                 os << output;
797                 column += output.size();
798                 break;
799         }
800         }
801
802         return column;
803 }
804
805
806 // This could go to ParagraphParameters if we want to
807 int Paragraph::endTeXParParams(BufferParams const & bparams,
808                                ostream & os, bool moving_arg) const
809 {
810         int column = 0;
811
812         switch (params().align()) {
813         case LYX_ALIGN_NONE:
814         case LYX_ALIGN_BLOCK:
815         case LYX_ALIGN_LAYOUT:
816         case LYX_ALIGN_SPECIAL:
817                 break;
818         case LYX_ALIGN_LEFT:
819         case LYX_ALIGN_RIGHT:
820         case LYX_ALIGN_CENTER:
821                 if (moving_arg) {
822                         os << "\\protect";
823                         column = 8;
824                 }
825                 break;
826         }
827
828         switch (params().align()) {
829         case LYX_ALIGN_NONE:
830         case LYX_ALIGN_BLOCK:
831         case LYX_ALIGN_LAYOUT:
832         case LYX_ALIGN_SPECIAL:
833                 break;
834         case LYX_ALIGN_LEFT: {
835                 string output;
836                 if (getParLanguage(bparams)->babel() != "hebrew")
837                         output = corrected_env("\\par\\end", "flushleft", ownerCode());
838                 else
839                         output = corrected_env("\\par\\end", "flushright", ownerCode());
840                 os << output;
841                 column += output.size();
842                 break;
843         } case LYX_ALIGN_RIGHT: {
844                 string output;
845                 if (getParLanguage(bparams)->babel() != "hebrew")
846                         output = corrected_env("\\par\\end", "flushright", ownerCode());
847                 else
848                         output = corrected_env("\\par\\end", "flushleft", ownerCode());
849                 os << output;
850                 column += output.size();
851                 break;
852         } case LYX_ALIGN_CENTER: {
853                 string output;
854                 output = corrected_env("\\par\\end", "center", ownerCode());
855                 os << output;
856                 column += output.size();
857                 break;
858         }
859         }
860
861         return column;
862 }
863
864
865 // This one spits out the text of the paragraph
866 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
867                                 BufferParams const & bparams,
868                                 LyXFont const & outerfont,
869                                 ostream & os, TexRow & texrow,
870                                 OutputParams const & runparams) const
871 {
872         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...     " << this << endl;
873
874         bool return_value = false;
875
876         LyXLayout_ptr style;
877
878         // well we have to check if we are in an inset with unlimited
879         // length (all in one row) if that is true then we don't allow
880         // any special options in the paragraph and also we don't allow
881         // any environment other then "Standard" to be valid!
882         bool asdefault = forceDefaultParagraphs();
883
884         if (asdefault) {
885                 style = bparams.getLyXTextClass().defaultLayout();
886         } else {
887                 style = layout();
888         }
889
890         LyXFont basefont;
891
892         LaTeXFeatures features(buf, bparams, runparams);
893
894         // output change tracking marks only if desired,
895         // if dvipost is installed,
896         // and with dvi/ps (other formats don't work)
897         bool const output = bparams.output_changes
898                 && runparams.flavor == OutputParams::LATEX
899                 && features.isAvailable("dvipost");
900
901         // Maybe we have to create a optional argument.
902         pos_type body_pos = beginOfBody();
903         unsigned int column = 0;
904
905         if (body_pos > 0) {
906                 // the optional argument is kept in curly brackets in
907                 // case it contains a ']'
908                 os << "[{";
909                 column += 2;
910                 basefont = getLabelFont(bparams, outerfont);
911         } else {
912                 basefont = getLayoutFont(bparams, outerfont);
913         }
914
915         // Which font is currently active?
916         LyXFont running_font(basefont);
917         // Do we have an open font change?
918         bool open_font = false;
919
920         Change::Type running_change = Change::UNCHANGED;
921
922         texrow.start(id(), 0);
923
924         // if the paragraph is empty, the loop will not be entered at all
925         if (empty()) {
926                 if (style->isCommand()) {
927                         os << '{';
928                         ++column;
929                 }
930                 if (!asdefault)
931                         column += startTeXParParams(bparams, os,
932                                                     runparams.moving_arg);
933         }
934
935         for (pos_type i = 0; i < size(); ++i) {
936                 ++column;
937                 // First char in paragraph or after label?
938                 if (i == body_pos) {
939                         if (body_pos > 0) {
940                                 if (open_font) {
941                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
942                                         open_font = false;
943                                 }
944                                 basefont = getLayoutFont(bparams, outerfont);
945                                 running_font = basefont;
946                                 os << "}] ";
947                                 column +=3;
948                         }
949                         if (style->isCommand()) {
950                                 os << '{';
951                                 ++column;
952                         }
953
954                         if (!asdefault)
955                                 column += startTeXParParams(bparams, os,
956                                                             runparams.moving_arg);
957                 }
958
959                 value_type c = getChar(i);
960
961                 // Fully instantiated font
962                 LyXFont font = getFont(bparams, i, outerfont);
963
964                 LyXFont const last_font = running_font;
965
966                 // Spaces at end of font change are simulated to be
967                 // outside font change, i.e. we write "\textXX{text} "
968                 // rather than "\textXX{text }". (Asger)
969                 if (open_font && c == ' ' && i <= size() - 2) {
970                         LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
971                         if (next_font != running_font && next_font != font) {
972                                 font = next_font;
973                         }
974                 }
975
976                 // We end font definition before blanks
977                 if (open_font &&
978                     (font != running_font ||
979                      font.language() != running_font.language()))
980                 {
981                         column += running_font.latexWriteEndChanges(os,
982                                                                     basefont,
983                                                                     (i == body_pos-1) ? basefont : font);
984                         running_font = basefont;
985                         open_font = false;
986                 }
987
988                 // Blanks are printed before start of fontswitch
989                 if (c == ' ') {
990                         // Do not print the separation of the optional argument
991                         if (i != body_pos - 1) {
992                                 pimpl_->simpleTeXBlanks(os, texrow, i,
993                                                        column, font, *style);
994                         }
995                 }
996
997                 // Do we need to change font?
998                 if ((font != running_font ||
999                      font.language() != running_font.language()) &&
1000                         i != body_pos - 1)
1001                 {
1002                         column += font.latexWriteStartChanges(os, basefont,
1003                                                               last_font);
1004                         running_font = font;
1005                         open_font = true;
1006                 }
1007
1008                 Change::Type change = pimpl_->lookupChange(i).type;
1009
1010                 column += Changes::latexMarkChange(os, running_change,
1011                         change, output);
1012                 running_change = change;
1013
1014                 // do not output text which is marked deleted
1015                 // if change tracking output is not desired
1016                 if (output || running_change != Change::DELETED) {
1017                         OutputParams rp = runparams;
1018                         rp.free_spacing = style->free_spacing;
1019                         rp.local_font = &font;
1020                         rp.intitle = style->intitle;
1021                         pimpl_->simpleTeXSpecialChars(buf, bparams,
1022                                                 os, texrow, rp,
1023                                                 font, running_font,
1024                                                 basefont, outerfont, open_font,
1025                                                 running_change,
1026                                                 *style, i, column, c);
1027                 }
1028         }
1029
1030         column += Changes::latexMarkChange(os,
1031                         running_change, Change::UNCHANGED, output);
1032
1033         // If we have an open font definition, we have to close it
1034         if (open_font) {
1035 #ifdef FIXED_LANGUAGE_END_DETECTION
1036                 if (next_) {
1037                         running_font
1038                                 .latexWriteEndChanges(os, basefont,
1039                                                       next_->getFont(bparams,
1040                                                       0, outerfont));
1041                 } else {
1042                         running_font.latexWriteEndChanges(os, basefont,
1043                                                           basefont);
1044                 }
1045 #else
1046 #ifdef WITH_WARNINGS
1047 //#warning For now we ALWAYS have to close the foreign font settings if they are
1048 //#warning there as we start another \selectlanguage with the next paragraph if
1049 //#warning we are in need of this. This should be fixed sometime (Jug)
1050 #endif
1051                 running_font.latexWriteEndChanges(os, basefont,  basefont);
1052 #endif
1053         }
1054
1055         // Needed if there is an optional argument but no contents.
1056         if (body_pos > 0 && body_pos == size()) {
1057                 os << "}]~";
1058                 return_value = false;
1059         }
1060
1061         if (!asdefault) {
1062                 column += endTeXParParams(bparams, os, runparams.moving_arg);
1063         }
1064
1065         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1066         return return_value;
1067 }
1068
1069
1070 namespace {
1071
1072 // checks, if newcol chars should be put into this line
1073 // writes newline, if necessary.
1074 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1075                           string::size_type newcol)
1076 {
1077         colcount += newcol;
1078         if (colcount > lyxrc.ascii_linelen) {
1079                 os << "\n";
1080                 colcount = newcol; // assume write after this call
1081         }
1082 }
1083
1084 enum PAR_TAG {
1085         PAR_NONE=0,
1086         TT = 1,
1087         SF = 2,
1088         BF = 4,
1089         IT = 8,
1090         SL = 16,
1091         EM = 32
1092 };
1093
1094
1095 string tag_name(PAR_TAG const & pt) {
1096         switch (pt) {
1097         case PAR_NONE: return "!-- --";
1098         case TT: return "tt";
1099         case SF: return "sf";
1100         case BF: return "bf";
1101         case IT: return "it";
1102         case SL: return "sl";
1103         case EM: return "em";
1104         }
1105         return "";
1106 }
1107
1108
1109 inline
1110 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1111 {
1112         p1 = static_cast<PAR_TAG>(p1 | p2);
1113 }
1114
1115
1116 inline
1117 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1118 {
1119         p1 = static_cast<PAR_TAG>(p1 & ~p2);
1120 }
1121
1122 } // anon
1123
1124
1125 bool Paragraph::emptyTag() const
1126 {
1127         for (pos_type i = 0; i < size(); ++i) {
1128                 if (isInset(i)) {
1129                         InsetBase const * inset = getInset(i);
1130                         InsetBase::Code lyx_code = inset->lyxCode();
1131                         if (lyx_code != InsetBase::TOC_CODE &&
1132                             lyx_code != InsetBase::INCLUDE_CODE &&
1133                             lyx_code != InsetBase::GRAPHICS_CODE &&
1134                             lyx_code != InsetBase::ERT_CODE &&
1135                             lyx_code != InsetBase::FLOAT_CODE &&
1136                             lyx_code != InsetBase::TABULAR_CODE) {
1137                                 return false;
1138                         }
1139                 } else {
1140                         value_type c = getChar(i);
1141                         if (c != ' ' && c != '\t')
1142                                 return false;
1143                 }
1144         }
1145         return true;
1146 }
1147
1148
1149 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
1150 {
1151         for (pos_type i = 0; i < size(); ++i) {
1152                 if (isInset(i)) {
1153                         InsetBase const * inset = getInset(i);
1154                         InsetBase::Code lyx_code = inset->lyxCode();
1155                         if (lyx_code == InsetBase::LABEL_CODE) {
1156                                 string const id = static_cast<InsetCommand const *>(inset)->getContents();
1157                                 return "id=\"" + sgml::cleanID(buf, runparams, id) + "\"";
1158                         }
1159                 }
1160
1161         }
1162         return string();
1163 }
1164
1165
1166 pos_type Paragraph::getFirstWord(Buffer const & buf, ostream & os, OutputParams const & runparams) const
1167 {
1168         pos_type i;
1169         for (i = 0; i < size(); ++i) {
1170                 if (isInset(i)) {
1171                         InsetBase const * inset = getInset(i);
1172                         inset->docbook(buf, os, runparams);
1173                 } else {
1174                         value_type c = getChar(i);
1175                         if (c == ' ')
1176                                 break;
1177                         bool ws;
1178                         string str;
1179                         boost::tie(ws, str) = sgml::escapeChar(c);
1180
1181                         os << str;
1182                 }
1183         }
1184         return i;
1185 }
1186
1187
1188 bool Paragraph::onlyText(Buffer const & buf, LyXFont const & outerfont, pos_type initial) const
1189 {
1190         LyXFont font_old;
1191
1192         for (pos_type i = initial; i < size(); ++i) {
1193                 LyXFont font = getFont(buf.params(), i, outerfont);
1194                 if (isInset(i))
1195                         return false;
1196                 if (i != initial && font != font_old)
1197                         return false;
1198                 font_old = font;
1199         }
1200
1201         return true;
1202 }
1203
1204
1205 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1206                                     ostream & os,
1207                                     OutputParams const & runparams,
1208                                     LyXFont const & outerfont,
1209                                     pos_type initial) const
1210 {
1211         bool emph_flag = false;
1212
1213         LyXLayout_ptr const & style = layout();
1214         LyXFont font_old =
1215                 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1216
1217         if (style->pass_thru && !onlyText(buf, outerfont, initial))
1218                 os << "]]>";
1219
1220         // parsing main loop
1221         for (pos_type i = initial; i < size(); ++i) {
1222                 LyXFont font = getFont(buf.params(), i, outerfont);
1223
1224                 // handle <emphasis> tag
1225                 if (font_old.emph() != font.emph()) {
1226                         if (font.emph() == LyXFont::ON) {
1227                                 os << "<emphasis>";
1228                                 emph_flag = true;
1229                         } else if (i != initial) {
1230                                 os << "</emphasis>";
1231                                 emph_flag = false;
1232                         }
1233                 }
1234
1235                 if (isInset(i)) {
1236                         InsetBase const * inset = getInset(i);
1237                         inset->docbook(buf, os, runparams);
1238                 } else {
1239                         value_type c = getChar(i);
1240                         bool ws;
1241                         string str;
1242                         boost::tie(ws, str) = sgml::escapeChar(c);
1243
1244                         if (style->pass_thru)
1245                                 os << c;
1246                         else
1247                                 os << str;
1248                 }
1249                 font_old = font;
1250         }
1251
1252         if (emph_flag) {
1253                 os << "</emphasis>";
1254         }
1255
1256         if (style->free_spacing)
1257                 os << '\n';
1258         if (style->pass_thru && !onlyText(buf, outerfont, initial))
1259                 os << "<![CDATA[";
1260 }
1261
1262
1263 bool Paragraph::isNewline(pos_type pos) const
1264 {
1265         return isInset(pos)
1266                 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1267 }
1268
1269
1270 bool Paragraph::isLineSeparator(pos_type pos) const
1271 {
1272         value_type const c = getChar(pos);
1273         return isLineSeparatorChar(c)
1274                 || (c == Paragraph::META_INSET && getInset(pos) &&
1275                 getInset(pos)->isLineSeparator());
1276 }
1277
1278
1279 /// Used by the spellchecker
1280 bool Paragraph::isLetter(pos_type pos) const
1281 {
1282         if (isInset(pos))
1283                 return getInset(pos)->isLetter();
1284         else {
1285                 value_type const c = getChar(pos);
1286                 return isLetterChar(c) || isDigit(c);
1287         }
1288 }
1289
1290
1291 Language const *
1292 Paragraph::getParLanguage(BufferParams const & bparams) const
1293 {
1294         if (!empty())
1295                 return getFirstFontSettings(bparams).language();
1296 #ifdef WITH_WARNINGS
1297 #warning FIXME we should check the prev par as well (Lgb)
1298 #endif
1299         return bparams.language;
1300 }
1301
1302
1303 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1304 {
1305         return lyxrc.rtl_support
1306                 && getParLanguage(bparams)->rightToLeft()
1307                 && ownerCode() != InsetBase::ERT_CODE;
1308 }
1309
1310
1311 void Paragraph::changeLanguage(BufferParams const & bparams,
1312                                Language const * from, Language const * to)
1313 {
1314         for (pos_type i = 0; i < size(); ++i) {
1315                 LyXFont font = getFontSettings(bparams, i);
1316                 if (font.language() == from) {
1317                         font.setLanguage(to);
1318                         setFont(i, font);
1319                 }
1320         }
1321 }
1322
1323
1324 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1325 {
1326         Language const * doc_language = bparams.language;
1327         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1328         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1329
1330         for (; cit != end; ++cit)
1331                 if (cit->font().language() != ignore_language &&
1332                     cit->font().language() != latex_language &&
1333                     cit->font().language() != doc_language)
1334                         return true;
1335         return false;
1336 }
1337
1338
1339 // Convert the paragraph to a string.
1340 // Used for building the table of contents
1341 string const Paragraph::asString(Buffer const & buffer, bool label) const
1342 {
1343         OutputParams runparams;
1344         return asString(buffer, runparams, label);
1345 }
1346
1347
1348 string const Paragraph::asString(Buffer const & buffer,
1349                                  OutputParams const & runparams,
1350                                  bool label) const
1351 {
1352 #if 0
1353         string s;
1354         if (label && !params().labelString().empty())
1355                 s += params().labelString() + ' ';
1356
1357         for (pos_type i = 0; i < size(); ++i) {
1358                 value_type c = getChar(i);
1359                 if (isPrintable(c))
1360                         s += c;
1361                 else if (c == META_INSET &&
1362                          getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1363                         ostringstream os;
1364                         getInset(i)->plaintext(buffer, os, runparams);
1365                         s += subst(STRCONV(os.str()),'\n',' ');
1366                 }
1367         }
1368
1369         return s;
1370 #else
1371         // This should really be done by the caller and not here.
1372         string ret = asString(buffer, runparams, 0, size(), label);
1373         return subst(ret, '\n', ' ');
1374 #endif
1375 }
1376
1377
1378 string const Paragraph::asString(Buffer const & buffer,
1379                                  pos_type beg, pos_type end, bool label) const
1380 {
1381
1382         OutputParams const runparams;
1383         return asString(buffer, runparams, beg, end, label);
1384 }
1385
1386
1387 string const Paragraph::asString(Buffer const & buffer,
1388                                  OutputParams const & runparams,
1389                                  pos_type beg, pos_type end, bool label) const
1390 {
1391         ostringstream os;
1392
1393         if (beg == 0 && label && !params().labelString().empty())
1394                 os << params().labelString() << ' ';
1395
1396         for (pos_type i = beg; i < end; ++i) {
1397                 value_type const c = getUChar(buffer.params(), i);
1398                 if (isPrintable(c))
1399                         os << c;
1400                 else if (c == META_INSET)
1401                         getInset(i)->textString(buffer, os, runparams);
1402         }
1403
1404         return os.str();
1405 }
1406
1407
1408 void Paragraph::setInsetOwner(InsetBase * inset)
1409 {
1410         pimpl_->inset_owner = inset;
1411 }
1412
1413
1414 void Paragraph::setContentsFromPar(Paragraph const & par)
1415 {
1416         pimpl_->setContentsFromPar(par);
1417 }
1418
1419
1420 void Paragraph::trackChanges(Change::Type type)
1421 {
1422         pimpl_->trackChanges(type);
1423 }
1424
1425
1426 void Paragraph::untrackChanges()
1427 {
1428         pimpl_->untrackChanges();
1429 }
1430
1431
1432 void Paragraph::cleanChanges(ChangeTracking ct)
1433 {
1434         pimpl_->cleanChanges(ct);
1435 }
1436
1437
1438 Change const Paragraph::lookupChange(lyx::pos_type pos) const
1439 {
1440         BOOST_ASSERT(pos <= size());
1441         return pimpl_->lookupChange(pos);
1442 }
1443
1444
1445 bool Paragraph::isChanged(pos_type start, pos_type end) const
1446 {
1447         return pimpl_->isChanged(start, end);
1448 }
1449
1450
1451 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1452 {
1453         return pimpl_->isChangeEdited(start, end);
1454 }
1455
1456
1457 void Paragraph::setChangeType(lyx::pos_type pos, Change::Type type)
1458 {
1459         pimpl_->setChangeType(pos, type);
1460 }
1461
1462
1463 void Paragraph::setChange(lyx::pos_type pos, Change change)
1464 {
1465         pimpl_->setChange(pos, change);
1466 }
1467
1468
1469 void Paragraph::markErased(bool erased)
1470 {
1471         pimpl_->markErased(erased);
1472 }
1473
1474
1475 void Paragraph::acceptChange(pos_type start, pos_type end)
1476 {
1477         return pimpl_->acceptChange(start, end);
1478 }
1479
1480
1481 void Paragraph::rejectChange(pos_type start, pos_type end)
1482 {
1483         return pimpl_->rejectChange(start, end);
1484 }
1485
1486
1487 int Paragraph::id() const
1488 {
1489         return pimpl_->id_;
1490 }
1491
1492
1493 LyXLayout_ptr const & Paragraph::layout() const
1494 {
1495         return layout_;
1496 }
1497
1498
1499 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1500 {
1501         layout_ = new_layout;
1502 }
1503
1504
1505 InsetBase * Paragraph::inInset() const
1506 {
1507         return pimpl_->inset_owner;
1508 }
1509
1510
1511 InsetBase::Code Paragraph::ownerCode() const
1512 {
1513         return pimpl_->inset_owner
1514                 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1515 }
1516
1517
1518 void Paragraph::clearContents()
1519 {
1520         text_.clear();
1521 }
1522
1523
1524 void Paragraph::setChar(pos_type pos, value_type c)
1525 {
1526         text_[pos] = c;
1527 }
1528
1529
1530 ParagraphParameters & Paragraph::params()
1531 {
1532         return pimpl_->params;
1533 }
1534
1535
1536 ParagraphParameters const & Paragraph::params() const
1537 {
1538         return pimpl_->params;
1539 }
1540
1541
1542 bool Paragraph::isFreeSpacing() const
1543 {
1544         if (layout()->free_spacing)
1545                 return true;
1546
1547         // for now we just need this, later should we need this in some
1548         // other way we can always add a function to InsetBase too.
1549         return ownerCode() == InsetBase::ERT_CODE;
1550 }
1551
1552
1553 bool Paragraph::allowEmpty() const
1554 {
1555         if (layout()->keepempty)
1556                 return true;
1557         return ownerCode() == InsetBase::ERT_CODE;
1558 }
1559
1560
1561 Row & Paragraph::getRow(pos_type pos, bool boundary)
1562 {
1563         BOOST_ASSERT(!rows().empty());
1564
1565         // If boundary is set we should return the row on which
1566         // the character before is inside.
1567         if (pos > 0 && boundary)
1568                 --pos;
1569
1570         RowList::iterator rit = rows_.end();
1571         RowList::iterator const begin = rows_.begin();
1572
1573         for (--rit; rit != begin && rit->pos() > pos; --rit)
1574                 ;
1575
1576         return *rit;
1577 }
1578
1579
1580 Row const & Paragraph::getRow(pos_type pos, bool boundary) const
1581 {
1582         BOOST_ASSERT(!rows().empty());
1583
1584         // If boundary is set we should return the row on which
1585         // the character before is inside.
1586         if (pos > 0 && boundary)
1587                 --pos;
1588
1589         RowList::const_iterator rit = rows_.end();
1590         RowList::const_iterator const begin = rows_.begin();
1591
1592         for (--rit; rit != begin && rit->pos() > pos; --rit)
1593                 ;
1594
1595         return *rit;
1596 }
1597
1598
1599 size_t Paragraph::pos2row(pos_type pos) const
1600 {
1601         BOOST_ASSERT(!rows().empty());
1602
1603         RowList::const_iterator rit = rows_.end();
1604         RowList::const_iterator const begin = rows_.begin();
1605
1606         for (--rit; rit != begin && rit->pos() > pos; --rit)
1607                 ;
1608
1609         return rit - begin;
1610 }
1611
1612
1613 char_type Paragraph::transformChar(char_type c, pos_type pos) const
1614 {
1615         if (!Encodings::is_arabic(c))
1616                 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isDigit(c))
1617                         return c + (0xb0 - '0');
1618                 else
1619                         return c;
1620
1621         value_type const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1622         value_type next_char = ' ';
1623
1624         for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1625                 value_type const par_char = getChar(i);
1626                 if (!Encodings::isComposeChar_arabic(par_char)) {
1627                         next_char = par_char;
1628                         break;
1629                 }
1630         }
1631
1632         if (Encodings::is_arabic(next_char)) {
1633                 if (Encodings::is_arabic(prev_char) &&
1634                         !Encodings::is_arabic_special(prev_char))
1635                         return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
1636                 else
1637                         return Encodings::transformChar(c, Encodings::FORM_INITIAL);
1638         } else {
1639                 if (Encodings::is_arabic(prev_char) &&
1640                         !Encodings::is_arabic_special(prev_char))
1641                         return Encodings::transformChar(c, Encodings::FORM_FINAL);
1642                 else
1643                         return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
1644         }
1645 }
1646
1647
1648 void Paragraph::dump() const
1649 {
1650         lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
1651         for (size_t i = 0; i != rows_.size(); ++i) {
1652                 lyxerr << "  row " << i << ":   ";
1653                 rows_[i].dump();
1654         }
1655 }