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