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