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