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