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