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