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