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