]> git.lyx.org Git - lyx.git/blob - src/paragraph.C
Use UTF8 for LaTeX export.
[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::docstring;
58 using lyx::odocstream;
59 using lyx::pos_type;
60 using lyx::char_type;
61
62 using lyx::support::subst;
63
64 using std::distance;
65 using std::endl;
66 using std::list;
67 using std::stack;
68 using std::string;
69 using std::ostream;
70 using std::ostringstream;
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_->lookupChange(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                                 std::vector<char> tmp = ucs4_to_utf8(c);
221                                 tmp.push_back('\0');
222                                 os << &tmp[0];
223                         } else
224                                 lyxerr << "ERROR (Paragraph::writeFile):"
225                                         " NULL char in structure." << endl;
226                         ++column;
227                         break;
228                 }
229         }
230
231         os << "\n\\end_layout\n";
232 }
233
234
235 void Paragraph::validate(LaTeXFeatures & features) const
236 {
237         pimpl_->validate(features, *layout());
238 }
239
240
241 void Paragraph::eraseIntern(lyx::pos_type pos)
242 {
243         pimpl_->eraseIntern(pos);
244 }
245
246
247 bool Paragraph::erase(pos_type pos)
248 {
249         return pimpl_->erase(pos);
250 }
251
252
253 int Paragraph::erase(pos_type start, pos_type end)
254 {
255         return pimpl_->erase(start, end);
256 }
257
258
259 void Paragraph::insert(pos_type start, string const & str,
260                        LyXFont const & font)
261 {
262         for (size_t i = 0, n = str.size(); i != n ; ++i)
263                 // FIXME: change tracking (MG)
264                 insertChar(start + i, str[i], font, Change(Change::INSERTED));
265 }
266
267
268 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
269                            Change const & change)
270 {
271         pimpl_->insertChar(pos, c, change);
272 }
273
274
275 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
276                            LyXFont const & font, Change const & change)
277 {
278         pimpl_->insertChar(pos, c, change);
279         setFont(pos, font);
280 }
281
282
283 void Paragraph::insertInset(pos_type pos, InsetBase * inset, Change const & change)
284 {
285         pimpl_->insertInset(pos, inset, change);
286 }
287
288
289 void Paragraph::insertInset(pos_type pos, InsetBase * inset,
290                             LyXFont const & font, Change const & change)
291 {
292         pimpl_->insertInset(pos, inset, change);
293         setFont(pos, font);
294 }
295
296
297 bool Paragraph::insetAllowed(InsetBase_code code)
298 {
299         return !pimpl_->inset_owner || pimpl_->inset_owner->insetAllowed(code);
300 }
301
302
303 // Gets uninstantiated font setting at position.
304 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
305                                          pos_type pos) const
306 {
307         if (pos > size()) {
308                 lyxerr << " pos: " << pos << " size: " << size() << endl;
309                 BOOST_ASSERT(pos <= size());
310         }
311
312         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
313         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
314         for (; cit != end; ++cit)
315                 if (cit->pos() >= pos)
316                         break;
317
318         if (cit != end)
319                 return cit->font();
320
321         if (pos == size() && !empty())
322                 return getFontSettings(bparams, pos - 1);
323
324         return LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
325 }
326
327
328 FontSpan Paragraph::fontSpan(lyx::pos_type pos) const
329 {
330         BOOST_ASSERT(pos <= size());
331         lyx::pos_type start = 0;
332
333         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
334         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
335         for (; cit != end; ++cit) {
336                 if (cit->pos() >= pos) {
337                         if (pos >= beginOfBody())
338                                 return FontSpan(std::max(start, beginOfBody()),
339                                                 cit->pos());
340                         else
341                                 return FontSpan(start,
342                                                 std::min(beginOfBody() - 1,
343                                                          cit->pos()));
344                 }
345                 start = cit->pos() + 1;
346         }
347
348         // This should not happen, but if so, we take no chances.
349         //lyxerr << "Paragraph::getEndPosOfFontSpan: This should not happen!"
350         //      << endl;
351         return FontSpan(pos, pos);
352 }
353
354
355 // Gets uninstantiated font setting at position 0
356 LyXFont const Paragraph::getFirstFontSettings(BufferParams const & bparams) const
357 {
358         if (!empty() && !pimpl_->fontlist.empty())
359                 return pimpl_->fontlist[0].font();
360
361         return LyXFont(LyXFont::ALL_INHERIT, bparams.language);
362 }
363
364
365 // Gets the fully instantiated font at a given position in a paragraph
366 // This is basically the same function as LyXText::GetFont() in text2.C.
367 // The difference is that this one is used for generating the LaTeX file,
368 // and thus cosmetic "improvements" are disallowed: This has to deliver
369 // the true picture of the buffer. (Asger)
370 LyXFont const Paragraph::getFont(BufferParams const & bparams, pos_type pos,
371                                  LyXFont const & outerfont) const
372 {
373         BOOST_ASSERT(pos >= 0);
374
375         LyXLayout_ptr const & lout = layout();
376
377         pos_type const body_pos = beginOfBody();
378
379         LyXFont layoutfont;
380         if (pos < body_pos)
381                 layoutfont = lout->labelfont;
382         else
383                 layoutfont = lout->font;
384
385         LyXFont font = getFontSettings(bparams, pos);
386         font.realize(layoutfont);
387         font.realize(outerfont);
388         font.realize(bparams.getFont());
389
390         return font;
391 }
392
393
394 LyXFont const Paragraph::getLabelFont
395         (BufferParams const & bparams, LyXFont const & outerfont) const
396 {
397         LyXFont tmpfont = layout()->labelfont;
398         tmpfont.setLanguage(getParLanguage(bparams));
399         tmpfont.realize(outerfont);
400         tmpfont.realize(bparams.getFont());
401         return tmpfont;
402 }
403
404
405 LyXFont const Paragraph::getLayoutFont
406         (BufferParams const & bparams, LyXFont const & outerfont) const
407 {
408         LyXFont tmpfont = layout()->font;
409         tmpfont.setLanguage(getParLanguage(bparams));
410         tmpfont.realize(outerfont);
411         tmpfont.realize(bparams.getFont());
412         return tmpfont;
413 }
414
415
416 /// Returns the height of the highest font in range
417 LyXFont_size Paragraph::highestFontInRange
418         (pos_type startpos, pos_type endpos, LyXFont_size def_size) const
419 {
420         if (pimpl_->fontlist.empty())
421                 return def_size;
422
423         Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
424         Pimpl::FontList::const_iterator const end = pimpl_->fontlist.end();
425         for (; end_it != end; ++end_it) {
426                 if (end_it->pos() >= endpos)
427                         break;
428         }
429
430         if (end_it != end)
431                 ++end_it;
432
433         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
434         for (; cit != end; ++cit) {
435                 if (cit->pos() >= startpos)
436                         break;
437         }
438
439         LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
440         for (; cit != end_it; ++cit) {
441                 LyXFont::FONT_SIZE size = cit->font().size();
442                 if (size == LyXFont::INHERIT_SIZE)
443                         size = def_size;
444                 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
445                         maxsize = size;
446         }
447         return maxsize;
448 }
449
450
451 Paragraph::value_type
452 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
453 {
454         value_type c = getChar(pos);
455         if (!lyxrc.rtl_support)
456                 return c;
457
458         value_type uc = c;
459         switch (c) {
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         case '>':
482                 uc = '<';
483                 break;
484         }
485         if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
486                 return uc;
487         else
488                 return c;
489 }
490
491
492 void Paragraph::setFont(pos_type pos, LyXFont const & font)
493 {
494         BOOST_ASSERT(pos <= size());
495
496         // First, reduce font against layout/label font
497         // Update: The setCharFont() routine in text2.C already
498         // reduces font, so we don't need to do that here. (Asger)
499         // No need to simplify this because it will disappear
500         // in a new kernel. (Asger)
501         // Next search font table
502
503         Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
504         Pimpl::FontList::iterator it = beg;
505         Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
506         for (; it != endit; ++it) {
507                 if (it->pos() >= pos)
508                         break;
509         }
510         size_t const i = distance(beg, it);
511         bool notfound = (it == endit);
512
513         if (!notfound && pimpl_->fontlist[i].font() == font)
514                 return;
515
516         bool begin = pos == 0 || notfound ||
517                 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
518         // Is position pos is a beginning of a font block?
519         bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
520         // Is position pos is the end of a font block?
521         if (begin && end) { // A single char block
522                 if (i + 1 < pimpl_->fontlist.size() &&
523                     pimpl_->fontlist[i + 1].font() == font) {
524                         // Merge the singleton block with the next block
525                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
526                         if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
527                                 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
528                 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
529                         // Merge the singleton block with the previous block
530                         pimpl_->fontlist[i - 1].pos(pos);
531                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
532                 } else
533                         pimpl_->fontlist[i].font(font);
534         } else if (begin) {
535                 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
536                         pimpl_->fontlist[i - 1].pos(pos);
537                 else
538                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
539                                         Pimpl::FontTable(pos, font));
540         } else if (end) {
541                 pimpl_->fontlist[i].pos(pos - 1);
542                 if (!(i + 1 < pimpl_->fontlist.size() &&
543                       pimpl_->fontlist[i + 1].font() == font))
544                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
545                                         Pimpl::FontTable(pos, font));
546         } else { // The general case. The block is splitted into 3 blocks
547                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
548                                 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
549                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
550                                 Pimpl::FontTable(pos, font));
551         }
552 }
553
554
555 void Paragraph::makeSameLayout(Paragraph const & par)
556 {
557         layout(par.layout());
558         // move to pimpl?
559         params() = par.params();
560 }
561
562
563 int Paragraph::stripLeadingSpaces()
564 {
565         if (isFreeSpacing())
566                 return 0;
567
568         int i = 0;
569         while (!empty() && (isNewline(0) || isLineSeparator(0))
570                 && (lookupChange(0).type != Change::DELETED)) {
571                 erase(0);
572                 ++i;
573         }
574
575         return i;
576 }
577
578
579 bool Paragraph::hasSameLayout(Paragraph const & par) const
580 {
581         return par.layout() == layout() && params().sameLayout(par.params());
582 }
583
584
585 Paragraph::depth_type Paragraph::getDepth() const
586 {
587         return params().depth();
588 }
589
590
591 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
592 {
593         if (layout()->isEnvironment())
594                 return params().depth() + 1;
595         else
596                 return params().depth();
597 }
598
599
600 char Paragraph::getAlign() const
601 {
602         return params().align();
603 }
604
605
606 string const & Paragraph::getLabelstring() const
607 {
608         return params().labelString();
609 }
610
611
612 // the next two functions are for the manual labels
613 string const Paragraph::getLabelWidthString() const
614 {
615         if (!params().labelWidthString().empty())
616                 return params().labelWidthString();
617         else
618                 return lyx::to_utf8(_("Senseless with this layout!"));
619 }
620
621
622 void Paragraph::setLabelWidthString(string const & s)
623 {
624         params().labelWidthString(s);
625 }
626
627
628 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
629 {
630         layout(new_layout);
631         params().labelWidthString(string());
632         params().align(LYX_ALIGN_LAYOUT);
633         params().spacing(Spacing(Spacing::Default));
634 }
635
636
637 pos_type Paragraph::beginOfBody() const
638 {
639         return begin_of_body_;
640 }
641
642
643 void Paragraph::setBeginOfBody()
644 {
645         if (layout()->labeltype != LABEL_MANUAL) {
646                 begin_of_body_ = 0;
647                 return;
648         }
649
650         // Unroll the first two cycles of the loop
651         // and remember the previous character to
652         // remove unnecessary getChar() calls
653         pos_type i = 0;
654         pos_type end = size();
655         if (i < end && !isNewline(i)) {
656                 ++i;
657                 char previous_char = 0;
658                 char temp = 0;
659                 if (i < end) {
660                         previous_char = text_[i];
661                         if (!isNewline(i)) {
662                                 ++i;
663                                 while (i < end && previous_char != ' ') {
664                                         temp = text_[i];
665                                         if (isNewline(i))
666                                                 break;
667                                         ++i;
668                                         previous_char = temp;
669                                 }
670                         }
671                 }
672         }
673
674         begin_of_body_ = i;
675 }
676
677
678 // returns -1 if inset not found
679 int Paragraph::getPositionOfInset(InsetBase const * inset) const
680 {
681         // Find the entry.
682         InsetList::const_iterator it = insetlist.begin();
683         InsetList::const_iterator end = insetlist.end();
684         for (; it != end; ++it)
685                 if (it->inset == inset)
686                         return it->pos;
687         return -1;
688 }
689
690
691 InsetBibitem * Paragraph::bibitem() const
692 {
693         if (!insetlist.empty()) {
694                 InsetBase * inset = insetlist.begin()->inset;
695                 if (inset->lyxCode() == InsetBase::BIBITEM_CODE)
696                         return static_cast<InsetBibitem *>(inset);
697         }
698         return 0;
699 }
700
701
702 bool Paragraph::forceDefaultParagraphs() const
703 {
704         return inInset() && inInset()->forceDefaultParagraphs(0);
705 }
706
707
708 namespace {
709
710 // paragraphs inside floats need different alignment tags to avoid
711 // unwanted space
712
713 bool noTrivlistCentering(InsetBase::Code code)
714 {
715         return code == InsetBase::FLOAT_CODE || code == InsetBase::WRAP_CODE;
716 }
717
718
719 string correction(string const & orig)
720 {
721         if (orig == "flushleft")
722                 return "raggedright";
723         if (orig == "flushright")
724                 return "raggedleft";
725         if (orig == "center")
726                 return "centering";
727         return orig;
728 }
729
730
731 string const corrected_env(string const & suffix, string const & env,
732         InsetBase::Code code)
733 {
734         string output = suffix + "{";
735         if (noTrivlistCentering(code))
736                 output += correction(env);
737         else
738                 output += env;
739         return output + "}";
740 }
741
742 } // namespace anon
743
744
745 // This could go to ParagraphParameters if we want to
746 int Paragraph::startTeXParParams(BufferParams const & bparams,
747                                  odocstream & os, bool moving_arg) const
748 {
749         int column = 0;
750
751         if (params().noindent()) {
752                 os << "\\noindent ";
753                 column += 10;
754         }
755
756         switch (params().align()) {
757         case LYX_ALIGN_NONE:
758         case LYX_ALIGN_BLOCK:
759         case LYX_ALIGN_LAYOUT:
760         case LYX_ALIGN_SPECIAL:
761                 break;
762         case LYX_ALIGN_LEFT:
763         case LYX_ALIGN_RIGHT:
764         case LYX_ALIGN_CENTER:
765                 if (moving_arg) {
766                         os << "\\protect";
767                         column += 8;
768                 }
769                 break;
770         }
771
772         switch (params().align()) {
773         case LYX_ALIGN_NONE:
774         case LYX_ALIGN_BLOCK:
775         case LYX_ALIGN_LAYOUT:
776         case LYX_ALIGN_SPECIAL:
777                 break;
778         case LYX_ALIGN_LEFT: {
779                 string output;
780                 if (getParLanguage(bparams)->babel() != "hebrew")
781                         output = corrected_env("\\begin", "flushleft", ownerCode());
782                 else
783                         output = corrected_env("\\begin", "flushright", ownerCode());
784                 os << lyx::from_ascii(output);
785                 column += output.size();
786                 break;
787         } case LYX_ALIGN_RIGHT: {
788                 string output;
789                 if (getParLanguage(bparams)->babel() != "hebrew")
790                         output = corrected_env("\\begin", "flushright", ownerCode());
791                 else
792                         output = corrected_env("\\begin", "flushleft", ownerCode());
793                 os << lyx::from_ascii(output);
794                 column += output.size();
795                 break;
796         } case LYX_ALIGN_CENTER: {
797                 string output;
798                 output = corrected_env("\\begin", "center", ownerCode());
799                 os << lyx::from_ascii(output);
800                 column += output.size();
801                 break;
802         }
803         }
804
805         return column;
806 }
807
808
809 // This could go to ParagraphParameters if we want to
810 int Paragraph::endTeXParParams(BufferParams const & bparams,
811                                odocstream & os, bool moving_arg) const
812 {
813         int column = 0;
814
815         switch (params().align()) {
816         case LYX_ALIGN_NONE:
817         case LYX_ALIGN_BLOCK:
818         case LYX_ALIGN_LAYOUT:
819         case LYX_ALIGN_SPECIAL:
820                 break;
821         case LYX_ALIGN_LEFT:
822         case LYX_ALIGN_RIGHT:
823         case LYX_ALIGN_CENTER:
824                 if (moving_arg) {
825                         os << "\\protect";
826                         column = 8;
827                 }
828                 break;
829         }
830
831         switch (params().align()) {
832         case LYX_ALIGN_NONE:
833         case LYX_ALIGN_BLOCK:
834         case LYX_ALIGN_LAYOUT:
835         case LYX_ALIGN_SPECIAL:
836                 break;
837         case LYX_ALIGN_LEFT: {
838                 string output;
839                 if (getParLanguage(bparams)->babel() != "hebrew")
840                         output = corrected_env("\\par\\end", "flushleft", ownerCode());
841                 else
842                         output = corrected_env("\\par\\end", "flushright", ownerCode());
843                 os << lyx::from_ascii(output);
844                 column += output.size();
845                 break;
846         } case LYX_ALIGN_RIGHT: {
847                 string output;
848                 if (getParLanguage(bparams)->babel() != "hebrew")
849                         output = corrected_env("\\par\\end", "flushright", ownerCode());
850                 else
851                         output = corrected_env("\\par\\end", "flushleft", ownerCode());
852                 os << lyx::from_ascii(output);
853                 column += output.size();
854                 break;
855         } case LYX_ALIGN_CENTER: {
856                 string output;
857                 output = corrected_env("\\par\\end", "center", ownerCode());
858                 os << lyx::from_ascii(output);
859                 column += output.size();
860                 break;
861         }
862         }
863
864         return column;
865 }
866
867
868 // This one spits out the text of the paragraph
869 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
870                                 BufferParams const & bparams,
871                                 LyXFont const & outerfont,
872                                 odocstream & os, TexRow & texrow,
873                                 OutputParams const & runparams) const
874 {
875         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...     " << this << endl;
876
877         bool return_value = false;
878
879         LyXLayout_ptr style;
880
881         // well we have to check if we are in an inset with unlimited
882         // length (all in one row) if that is true then we don't allow
883         // any special options in the paragraph and also we don't allow
884         // any environment other then "Standard" to be valid!
885         bool asdefault = forceDefaultParagraphs();
886
887         if (asdefault) {
888                 style = bparams.getLyXTextClass().defaultLayout();
889         } else {
890                 style = layout();
891         }
892
893         LyXFont basefont;
894
895         LaTeXFeatures features(buf, bparams, runparams);
896
897         // output change tracking marks only if desired,
898         // if dvipost is installed,
899         // and with dvi/ps (other formats don't work)
900         bool const output = bparams.outputChanges
901                 && runparams.flavor == OutputParams::LATEX
902                 && features.isAvailable("dvipost");
903
904         // Maybe we have to create a optional argument.
905         pos_type body_pos = beginOfBody();
906         unsigned int column = 0;
907
908         if (body_pos > 0) {
909                 // the optional argument is kept in curly brackets in
910                 // case it contains a ']'
911                 os << "[{";
912                 column += 2;
913                 basefont = getLabelFont(bparams, outerfont);
914         } else {
915                 basefont = getLayoutFont(bparams, outerfont);
916         }
917
918         // Which font is currently active?
919         LyXFont running_font(basefont);
920         // Do we have an open font change?
921         bool open_font = false;
922
923         Change::Type running_change = Change::UNCHANGED;
924
925         texrow.start(id(), 0);
926
927         // if the paragraph is empty, the loop will not be entered at all
928         if (empty()) {
929                 if (style->isCommand()) {
930                         os << '{';
931                         ++column;
932                 }
933                 if (!asdefault)
934                         column += startTeXParParams(bparams, os,
935                                                     runparams.moving_arg);
936         }
937
938         for (pos_type i = 0; i < size(); ++i) {
939                 ++column;
940                 // First char in paragraph or after label?
941                 if (i == body_pos) {
942                         if (body_pos > 0) {
943                                 if (open_font) {
944                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
945                                         open_font = false;
946                                 }
947                                 basefont = getLayoutFont(bparams, outerfont);
948                                 running_font = basefont;
949                                 os << "}] ";
950                                 column +=3;
951                         }
952                         if (style->isCommand()) {
953                                 os << '{';
954                                 ++column;
955                         }
956
957                         if (!asdefault)
958                                 column += startTeXParParams(bparams, os,
959                                                             runparams.moving_arg);
960                 }
961
962                 value_type c = getChar(i);
963
964                 // Fully instantiated font
965                 LyXFont font = getFont(bparams, i, outerfont);
966
967                 LyXFont const last_font = running_font;
968
969                 // Spaces at end of font change are simulated to be
970                 // outside font change, i.e. we write "\textXX{text} "
971                 // rather than "\textXX{text }". (Asger)
972                 if (open_font && c == ' ' && i <= size() - 2) {
973                         LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
974                         if (next_font != running_font && next_font != font) {
975                                 font = next_font;
976                         }
977                 }
978
979                 // We end font definition before blanks
980                 if (open_font &&
981                     (font != running_font ||
982                      font.language() != running_font.language()))
983                 {
984                         column += running_font.latexWriteEndChanges(os,
985                                                                     basefont,
986                                                                     (i == body_pos-1) ? basefont : font);
987                         running_font = basefont;
988                         open_font = false;
989                 }
990
991                 // Blanks are printed before start of fontswitch
992                 if (c == ' ') {
993                         // Do not print the separation of the optional argument
994                         if (i != body_pos - 1) {
995                                 pimpl_->simpleTeXBlanks(os, texrow, i,
996                                                        column, font, *style);
997                         }
998                 }
999
1000                 // Do we need to change font?
1001                 if ((font != running_font ||
1002                      font.language() != running_font.language()) &&
1003                         i != body_pos - 1)
1004                 {
1005                         column += font.latexWriteStartChanges(os, basefont,
1006                                                               last_font);
1007                         running_font = font;
1008                         open_font = true;
1009                 }
1010
1011                 Change::Type change = pimpl_->lookupChange(i).type;
1012
1013                 column += Changes::latexMarkChange(os, running_change,
1014                         change, output);
1015                 running_change = change;
1016
1017                 // do not output text which is marked deleted
1018                 // if change tracking output is not desired
1019                 if (output || running_change != Change::DELETED) {
1020                         OutputParams rp = runparams;
1021                         rp.free_spacing = style->free_spacing;
1022                         rp.local_font = &font;
1023                         rp.intitle = style->intitle;
1024                         pimpl_->simpleTeXSpecialChars(buf, bparams,
1025                                                 os, texrow, rp,
1026                                                 font, running_font,
1027                                                 basefont, outerfont, open_font,
1028                                                 running_change,
1029                                                 *style, i, column, c);
1030                 }
1031         }
1032
1033         column += Changes::latexMarkChange(os,
1034                         running_change, Change::UNCHANGED, output);
1035
1036         // If we have an open font definition, we have to close it
1037         if (open_font) {
1038 #ifdef FIXED_LANGUAGE_END_DETECTION
1039                 if (next_) {
1040                         running_font
1041                                 .latexWriteEndChanges(os, basefont,
1042                                                       next_->getFont(bparams,
1043                                                       0, outerfont));
1044                 } else {
1045                         running_font.latexWriteEndChanges(os, basefont,
1046                                                           basefont);
1047                 }
1048 #else
1049 #ifdef WITH_WARNINGS
1050 //#warning For now we ALWAYS have to close the foreign font settings if they are
1051 //#warning there as we start another \selectlanguage with the next paragraph if
1052 //#warning we are in need of this. This should be fixed sometime (Jug)
1053 #endif
1054                 running_font.latexWriteEndChanges(os, basefont,  basefont);
1055 #endif
1056         }
1057
1058         // Needed if there is an optional argument but no contents.
1059         if (body_pos > 0 && body_pos == size()) {
1060                 os << "}]~";
1061                 return_value = false;
1062         }
1063
1064         if (!asdefault) {
1065                 column += endTeXParParams(bparams, os, runparams.moving_arg);
1066         }
1067
1068         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1069         return return_value;
1070 }
1071
1072
1073 namespace {
1074
1075 // checks, if newcol chars should be put into this line
1076 // writes newline, if necessary.
1077 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1078                           string::size_type newcol)
1079 {
1080         colcount += newcol;
1081         if (colcount > lyxrc.ascii_linelen) {
1082                 os << "\n";
1083                 colcount = newcol; // assume write after this call
1084         }
1085 }
1086
1087 enum PAR_TAG {
1088         PAR_NONE=0,
1089         TT = 1,
1090         SF = 2,
1091         BF = 4,
1092         IT = 8,
1093         SL = 16,
1094         EM = 32
1095 };
1096
1097
1098 string tag_name(PAR_TAG const & pt) {
1099         switch (pt) {
1100         case PAR_NONE: return "!-- --";
1101         case TT: return "tt";
1102         case SF: return "sf";
1103         case BF: return "bf";
1104         case IT: return "it";
1105         case SL: return "sl";
1106         case EM: return "em";
1107         }
1108         return "";
1109 }
1110
1111
1112 inline
1113 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1114 {
1115         p1 = static_cast<PAR_TAG>(p1 | p2);
1116 }
1117
1118
1119 inline
1120 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1121 {
1122         p1 = static_cast<PAR_TAG>(p1 & ~p2);
1123 }
1124
1125 } // anon
1126
1127
1128 bool Paragraph::emptyTag() const
1129 {
1130         for (pos_type i = 0; i < size(); ++i) {
1131                 if (isInset(i)) {
1132                         InsetBase const * inset = getInset(i);
1133                         InsetBase::Code lyx_code = inset->lyxCode();
1134                         if (lyx_code != InsetBase::TOC_CODE &&
1135                             lyx_code != InsetBase::INCLUDE_CODE &&
1136                             lyx_code != InsetBase::GRAPHICS_CODE &&
1137                             lyx_code != InsetBase::ERT_CODE &&
1138                             lyx_code != InsetBase::FLOAT_CODE &&
1139                             lyx_code != InsetBase::TABULAR_CODE) {
1140                                 return false;
1141                         }
1142                 } else {
1143                         value_type c = getChar(i);
1144                         if (c != ' ' && c != '\t')
1145                                 return false;
1146                 }
1147         }
1148         return true;
1149 }
1150
1151
1152 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
1153 {
1154         for (pos_type i = 0; i < size(); ++i) {
1155                 if (isInset(i)) {
1156                         InsetBase const * inset = getInset(i);
1157                         InsetBase::Code lyx_code = inset->lyxCode();
1158                         if (lyx_code == InsetBase::LABEL_CODE) {
1159                                 string const id = static_cast<InsetCommand const *>(inset)->getContents();
1160                                 return "id=\"" + sgml::cleanID(buf, runparams, id) + "\"";
1161                         }
1162                 }
1163
1164         }
1165         return string();
1166 }
1167
1168
1169 pos_type Paragraph::getFirstWord(Buffer const & buf, ostream & os, OutputParams const & runparams) const
1170 {
1171         pos_type i;
1172         for (i = 0; i < size(); ++i) {
1173                 if (isInset(i)) {
1174                         InsetBase const * inset = getInset(i);
1175                         inset->docbook(buf, os, runparams);
1176                 } else {
1177                         value_type c = getChar(i);
1178                         if (c == ' ')
1179                                 break;
1180                         bool ws;
1181                         string str;
1182                         // FIXME UNICODE
1183                         // sgml::escapeChar takes a char, not lyx::char_type
1184                         boost::tie(ws, str) = sgml::escapeChar(c);
1185
1186                         os << str;
1187                 }
1188         }
1189         return i;
1190 }
1191
1192
1193 bool Paragraph::onlyText(Buffer const & buf, LyXFont const & outerfont, pos_type initial) const
1194 {
1195         LyXFont font_old;
1196
1197         for (pos_type i = initial; i < size(); ++i) {
1198                 LyXFont font = getFont(buf.params(), i, outerfont);
1199                 if (isInset(i))
1200                         return false;
1201                 if (i != initial && font != font_old)
1202                         return false;
1203                 font_old = font;
1204         }
1205
1206         return true;
1207 }
1208
1209
1210 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1211                                     ostream & os,
1212                                     OutputParams const & runparams,
1213                                     LyXFont const & outerfont,
1214                                     pos_type initial) const
1215 {
1216         bool emph_flag = false;
1217
1218         LyXLayout_ptr const & style = layout();
1219         LyXFont font_old =
1220                 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1221
1222         if (style->pass_thru && !onlyText(buf, outerfont, initial))
1223                 os << "]]>";
1224
1225         // parsing main loop
1226         for (pos_type i = initial; i < size(); ++i) {
1227                 LyXFont font = getFont(buf.params(), i, outerfont);
1228
1229                 // handle <emphasis> tag
1230                 if (font_old.emph() != font.emph()) {
1231                         if (font.emph() == LyXFont::ON) {
1232                                 os << "<emphasis>";
1233                                 emph_flag = true;
1234                         } else if (i != initial) {
1235                                 os << "</emphasis>";
1236                                 emph_flag = false;
1237                         }
1238                 }
1239
1240                 if (isInset(i)) {
1241                         InsetBase const * inset = getInset(i);
1242                         inset->docbook(buf, os, runparams);
1243                 } else {
1244                         value_type c = getChar(i);
1245                         bool ws;
1246                         string str;
1247                         // FIXME UNICODE
1248                         // sgml::escapeChar takes a char, not lyx::char_type
1249                         boost::tie(ws, str) = sgml::escapeChar(c);
1250
1251                         if (style->pass_thru)
1252                                 os << c;
1253                         else
1254                                 os << str;
1255                 }
1256                 font_old = font;
1257         }
1258
1259         if (emph_flag) {
1260                 os << "</emphasis>";
1261         }
1262
1263         if (style->free_spacing)
1264                 os << '\n';
1265         if (style->pass_thru && !onlyText(buf, outerfont, initial))
1266                 os << "<![CDATA[";
1267 }
1268
1269
1270 bool Paragraph::isNewline(pos_type pos) const
1271 {
1272         return isInset(pos)
1273                 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1274 }
1275
1276
1277 bool Paragraph::isLineSeparator(pos_type pos) const
1278 {
1279         value_type const c = getChar(pos);
1280         return isLineSeparatorChar(c)
1281                 || (c == Paragraph::META_INSET && getInset(pos) &&
1282                 getInset(pos)->isLineSeparator());
1283 }
1284
1285
1286 /// Used by the spellchecker
1287 bool Paragraph::isLetter(pos_type pos) const
1288 {
1289         if (isInset(pos))
1290                 return getInset(pos)->isLetter();
1291         else {
1292                 value_type const c = getChar(pos);
1293                 return isLetterChar(c) || isDigit(c);
1294         }
1295 }
1296
1297
1298 Language const *
1299 Paragraph::getParLanguage(BufferParams const & bparams) const
1300 {
1301         if (!empty())
1302                 return getFirstFontSettings(bparams).language();
1303 #ifdef WITH_WARNINGS
1304 #warning FIXME we should check the prev par as well (Lgb)
1305 #endif
1306         return bparams.language;
1307 }
1308
1309
1310 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1311 {
1312         return lyxrc.rtl_support
1313                 && getParLanguage(bparams)->rightToLeft()
1314                 && ownerCode() != InsetBase::ERT_CODE;
1315 }
1316
1317
1318 void Paragraph::changeLanguage(BufferParams const & bparams,
1319                                Language const * from, Language const * to)
1320 {
1321         for (pos_type i = 0; i < size(); ++i) {
1322                 LyXFont font = getFontSettings(bparams, i);
1323                 if (font.language() == from) {
1324                         font.setLanguage(to);
1325                         setFont(i, font);
1326                 }
1327         }
1328 }
1329
1330
1331 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1332 {
1333         Language const * doc_language = bparams.language;
1334         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1335         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1336
1337         for (; cit != end; ++cit)
1338                 if (cit->font().language() != ignore_language &&
1339                     cit->font().language() != latex_language &&
1340                     cit->font().language() != doc_language)
1341                         return true;
1342         return false;
1343 }
1344
1345
1346 // Convert the paragraph to a string.
1347 // Used for building the table of contents
1348 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
1349 {
1350         OutputParams runparams;
1351         return asString(buffer, runparams, label);
1352 }
1353
1354
1355 docstring const Paragraph::asString(Buffer const & buffer,
1356                                  OutputParams const & runparams,
1357                                  bool label) const
1358 {
1359 #if 0
1360         string s;
1361         if (label && !params().labelString().empty())
1362                 s += params().labelString() + ' ';
1363
1364         for (pos_type i = 0; i < size(); ++i) {
1365                 value_type c = getChar(i);
1366                 if (isPrintable(c))
1367                         s += c;
1368                 else if (c == META_INSET &&
1369                          getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1370                         ostringstream os;
1371                         getInset(i)->plaintext(buffer, os, runparams);
1372                         s += subst(STRCONV(os.str()),'\n',' ');
1373                 }
1374         }
1375
1376         return s;
1377 #else
1378         // This should really be done by the caller and not here.
1379         docstring ret = asString(buffer, runparams, 0, size(), label);
1380         return subst(ret, '\n', ' ');
1381 #endif
1382 }
1383
1384
1385 docstring const Paragraph::asString(Buffer const & buffer,
1386                                  pos_type beg, pos_type end, bool label) const
1387 {
1388
1389         OutputParams const runparams;
1390         return asString(buffer, runparams, beg, end, label);
1391 }
1392
1393
1394 docstring const Paragraph::asString(Buffer const & buffer,
1395                                  OutputParams const & runparams,
1396                                  pos_type beg, pos_type end, bool label) const
1397 {
1398         lyx::odocstringstream os;
1399
1400         if (beg == 0 && label && !params().labelString().empty())
1401                 // FIXME UNICODE
1402                 os << lyx::from_utf8(params().labelString()) << ' ';
1403
1404         for (pos_type i = beg; i < end; ++i) {
1405                 value_type const c = getUChar(buffer.params(), i);
1406                 // FIXME: isPrintable does not work for lyx::char_type
1407                 if (isPrintable(c))
1408                         os.put(c);
1409                 else if (c == META_INSET)
1410                         getInset(i)->textString(buffer, os, runparams);
1411         }
1412
1413         return os.str();
1414 }
1415
1416
1417 void Paragraph::setInsetOwner(InsetBase * inset)
1418 {
1419         pimpl_->inset_owner = inset;
1420 }
1421
1422
1423 void Paragraph::setContentsFromPar(Paragraph const & par)
1424 {
1425         pimpl_->setContentsFromPar(par);
1426 }
1427
1428
1429 Change const Paragraph::lookupChange(lyx::pos_type pos) const
1430 {
1431         BOOST_ASSERT(pos <= size());
1432         return pimpl_->lookupChange(pos);
1433 }
1434
1435
1436 bool Paragraph::isChanged(pos_type start, pos_type end) const
1437 {
1438         return pimpl_->isChanged(start, end);
1439 }
1440
1441
1442 void Paragraph::setChange(Change const & change)
1443 {
1444         pimpl_->setChange(change);
1445 }
1446
1447
1448 void Paragraph::setChange(lyx::pos_type pos, Change const & change)
1449 {
1450         pimpl_->setChange(pos, change);
1451 }
1452
1453
1454 void Paragraph::acceptChange(pos_type start, pos_type end)
1455 {
1456         return pimpl_->acceptChange(start, end);
1457 }
1458
1459
1460 void Paragraph::rejectChange(pos_type start, pos_type end)
1461 {
1462         return pimpl_->rejectChange(start, end);
1463 }
1464
1465
1466 int Paragraph::id() const
1467 {
1468         return pimpl_->id_;
1469 }
1470
1471
1472 LyXLayout_ptr const & Paragraph::layout() const
1473 {
1474         return layout_;
1475 }
1476
1477
1478 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1479 {
1480         layout_ = new_layout;
1481 }
1482
1483
1484 InsetBase * Paragraph::inInset() const
1485 {
1486         return pimpl_->inset_owner;
1487 }
1488
1489
1490 InsetBase::Code Paragraph::ownerCode() const
1491 {
1492         return pimpl_->inset_owner
1493                 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1494 }
1495
1496
1497 void Paragraph::clearContents()
1498 {
1499         text_.clear();
1500 }
1501
1502
1503 void Paragraph::setChar(pos_type pos, value_type c)
1504 {
1505         text_[pos] = c;
1506 }
1507
1508
1509 ParagraphParameters & Paragraph::params()
1510 {
1511         return pimpl_->params;
1512 }
1513
1514
1515 ParagraphParameters const & Paragraph::params() const
1516 {
1517         return pimpl_->params;
1518 }
1519
1520
1521 bool Paragraph::isFreeSpacing() const
1522 {
1523         if (layout()->free_spacing)
1524                 return true;
1525
1526         // for now we just need this, later should we need this in some
1527         // other way we can always add a function to InsetBase too.
1528         return ownerCode() == InsetBase::ERT_CODE;
1529 }
1530
1531
1532 bool Paragraph::allowEmpty() const
1533 {
1534         if (layout()->keepempty)
1535                 return true;
1536         return ownerCode() == InsetBase::ERT_CODE;
1537 }
1538
1539
1540 Row & Paragraph::getRow(pos_type pos, bool boundary)
1541 {
1542         BOOST_ASSERT(!rows().empty());
1543
1544         // If boundary is set we should return the row on which
1545         // the character before is inside.
1546         if (pos > 0 && boundary)
1547                 --pos;
1548
1549         RowList::iterator rit = rows_.end();
1550         RowList::iterator const begin = rows_.begin();
1551
1552         for (--rit; rit != begin && rit->pos() > pos; --rit)
1553                 ;
1554
1555         return *rit;
1556 }
1557
1558
1559 Row const & Paragraph::getRow(pos_type pos, bool boundary) const
1560 {
1561         BOOST_ASSERT(!rows().empty());
1562
1563         // If boundary is set we should return the row on which
1564         // the character before is inside.
1565         if (pos > 0 && boundary)
1566                 --pos;
1567
1568         RowList::const_iterator rit = rows_.end();
1569         RowList::const_iterator const begin = rows_.begin();
1570
1571         for (--rit; rit != begin && rit->pos() > pos; --rit)
1572                 ;
1573
1574         return *rit;
1575 }
1576
1577
1578 size_t Paragraph::pos2row(pos_type pos) const
1579 {
1580         BOOST_ASSERT(!rows().empty());
1581
1582         RowList::const_iterator rit = rows_.end();
1583         RowList::const_iterator const begin = rows_.begin();
1584
1585         for (--rit; rit != begin && rit->pos() > pos; --rit)
1586                 ;
1587
1588         return rit - begin;
1589 }
1590
1591
1592 char_type Paragraph::transformChar(char_type c, pos_type pos) const
1593 {
1594         if (!Encodings::is_arabic(c))
1595                 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isDigit(c))
1596                         return c + (0xb0 - '0');
1597                 else
1598                         return c;
1599
1600         value_type const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1601         value_type next_char = ' ';
1602
1603         for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1604                 value_type const par_char = getChar(i);
1605                 if (!Encodings::isComposeChar_arabic(par_char)) {
1606                         next_char = par_char;
1607                         break;
1608                 }
1609         }
1610
1611         if (Encodings::is_arabic(next_char)) {
1612                 if (Encodings::is_arabic(prev_char) &&
1613                         !Encodings::is_arabic_special(prev_char))
1614                         return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
1615                 else
1616                         return Encodings::transformChar(c, Encodings::FORM_INITIAL);
1617         } else {
1618                 if (Encodings::is_arabic(prev_char) &&
1619                         !Encodings::is_arabic_special(prev_char))
1620                         return Encodings::transformChar(c, Encodings::FORM_FINAL);
1621                 else
1622                         return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
1623         }
1624 }
1625
1626
1627 void Paragraph::dump() const
1628 {
1629         lyxerr << "Paragraph::dump: rows.size(): " << rows_.size() << endl;
1630         for (size_t i = 0; i != rows_.size(); ++i) {
1631                 lyxerr << "  row " << i << ":   ";
1632                 rows_[i].dump();
1633         }
1634 }