]> git.lyx.org Git - lyx.git/blob - src/paragraph.C
remove unused includes
[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 "messages.h"
35 #include "outputparams.h"
36 #include "paragraph_funcs.h"
37
38 #include "rowpainter.h"
39
40 #include "sgml.h"
41 #include "texrow.h"
42 #include "vspace.h"
43
44 #include "frontends/FontMetrics.h"
45
46 #include "insets/insetbibitem.h"
47 #include "insets/insetoptarg.h"
48
49 #include "support/lstrings.h"
50 #include "support/textutils.h"
51 #include "support/convert.h"
52 #include "support/unicode.h"
53
54 #include <boost/bind.hpp>
55
56 #include <algorithm>
57 #include <list>
58 #include <stack>
59 #include <sstream>
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 namespace lyx {
70
71 using support::contains;
72 using support::rsplit;
73 using support::subst;
74
75 Paragraph::Paragraph()
76         : begin_of_body_(0), pimpl_(new Paragraph::Pimpl(this))
77 {
78         itemdepth = 0;
79         params().clear();
80 }
81
82
83 Paragraph::Paragraph(Paragraph const & par)
84         : itemdepth(par.itemdepth), insetlist(par.insetlist),
85         layout_(par.layout_),
86         text_(par.text_), begin_of_body_(par.begin_of_body_),
87         pimpl_(new Paragraph::Pimpl(*par.pimpl_, this))
88 {
89         //lyxerr << "Paragraph::Paragraph(Paragraph const&)" << endl;
90         InsetList::iterator it = insetlist.begin();
91         InsetList::iterator end = insetlist.end();
92         for (; it != end; ++it)
93                 it->inset = it->inset->clone().release();
94 }
95
96
97 Paragraph & Paragraph::operator=(Paragraph const & par)
98 {
99         // needed as we will destroy the pimpl_ before copying it
100         if (&par != this) {
101                 itemdepth = par.itemdepth;
102
103                 insetlist = par.insetlist;
104                 InsetList::iterator it = insetlist.begin();
105                 InsetList::iterator end = insetlist.end();
106                 for (; it != end; ++it)
107                         it->inset = it->inset->clone().release();
108
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 bool Paragraph::stripLeadingSpaces(bool trackChanges)
564 {
565         if (isFreeSpacing())
566                 return false;
567
568         int pos = 0;
569         int count = 0;
570
571         while (pos < size() && (isNewline(pos) || isLineSeparator(pos))) {
572                 if (eraseChar(pos, trackChanges))
573                         ++count;
574                 else
575                         ++pos;
576         }
577
578         return count > 0 || pos > 0;
579 }
580
581
582 bool Paragraph::hasSameLayout(Paragraph const & par) const
583 {
584         return par.layout() == layout() && params().sameLayout(par.params());
585 }
586
587
588 depth_type Paragraph::getDepth() const
589 {
590         return params().depth();
591 }
592
593
594 depth_type Paragraph::getMaxDepthAfter() const
595 {
596         if (layout()->isEnvironment())
597                 return params().depth() + 1;
598         else
599                 return params().depth();
600 }
601
602
603 char Paragraph::getAlign() const
604 {
605         if (params().align() == LYX_ALIGN_LAYOUT)
606                 return layout()->align;
607         else
608                 return params().align();
609 }
610
611
612 docstring const & Paragraph::getLabelstring() const
613 {
614         return params().labelString();
615 }
616
617
618 // the next two functions are for the manual labels
619 docstring const Paragraph::getLabelWidthString() const
620 {
621         if (!params().labelWidthString().empty())
622                 return params().labelWidthString();
623         else
624                 return _("Senseless with this layout!");
625 }
626
627
628 void Paragraph::setLabelWidthString(docstring const & s)
629 {
630         params().labelWidthString(s);
631 }
632
633
634 docstring const Paragraph::translateIfPossible(docstring const & s,
635                 BufferParams const & bparams) const
636 {
637         if (!support::isAscii(s) || s.empty()) {
638                 // This must be a user defined layout. We cannot translate
639                 // this, since gettext accepts only ascii keys.
640                 return s;
641         }
642         // Probably standard layout, try to translate
643         Messages & m = getMessages(getParLanguage(bparams)->code());
644         return m.get(to_ascii(s));
645 }
646
647
648 docstring Paragraph::expandLabel(LyXLayout_ptr const & layout,
649                 BufferParams const & bparams, bool process_appendix) const
650 {
651         LyXTextClass const & tclass = bparams.getLyXTextClass();
652
653         docstring fmt;
654         if (process_appendix && params().appendix())
655                 fmt = translateIfPossible(layout->labelstring_appendix(),
656                         bparams);
657         else
658                 fmt = translateIfPossible(layout->labelstring(), bparams);
659
660         // handle 'inherited level parts' in 'fmt',
661         // i.e. the stuff between '@' in   '@Section@.\arabic{subsection}'
662         size_t const i = fmt.find('@', 0);
663         if (i != docstring::npos) {
664                 size_t const j = fmt.find('@', i + 1);
665                 if (j != docstring::npos) {
666                         docstring parent(fmt, i + 1, j - i - 1);
667                         // FIXME UNICODE
668                         docstring label = expandLabel(tclass[to_utf8(parent)], bparams);
669                         fmt = docstring(fmt, 0, i) + label + docstring(fmt, j + 1, docstring::npos);
670                 }
671         }
672
673         return tclass.counters().counterLabel(fmt);
674 }
675
676
677 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
678 {
679         layout(new_layout);
680         params().labelWidthString(docstring());
681         params().align(LYX_ALIGN_LAYOUT);
682         params().spacing(Spacing(Spacing::Default));
683 }
684
685
686 pos_type Paragraph::beginOfBody() const
687 {
688         return begin_of_body_;
689 }
690
691
692 void Paragraph::setBeginOfBody()
693 {
694         if (layout()->labeltype != LABEL_MANUAL) {
695                 begin_of_body_ = 0;
696                 return;
697         }
698
699         // Unroll the first two cycles of the loop
700         // and remember the previous character to
701         // remove unnecessary getChar() calls
702         pos_type i = 0;
703         pos_type end = size();
704         if (i < end && !isNewline(i)) {
705                 ++i;
706                 char_type previous_char = 0;
707                 char_type temp = 0;
708                 if (i < end) {
709                         previous_char = text_[i];
710                         if (!isNewline(i)) {
711                                 ++i;
712                                 while (i < end && previous_char != ' ') {
713                                         temp = text_[i];
714                                         if (isNewline(i))
715                                                 break;
716                                         ++i;
717                                         previous_char = temp;
718                                 }
719                         }
720                 }
721         }
722
723         begin_of_body_ = i;
724 }
725
726
727 // returns -1 if inset not found
728 int Paragraph::getPositionOfInset(InsetBase const * inset) const
729 {
730         // Find the entry.
731         InsetList::const_iterator it = insetlist.begin();
732         InsetList::const_iterator end = insetlist.end();
733         for (; it != end; ++it)
734                 if (it->inset == inset)
735                         return it->pos;
736         return -1;
737 }
738
739
740 InsetBibitem * Paragraph::bibitem() const
741 {
742         if (!insetlist.empty()) {
743                 InsetBase * inset = insetlist.begin()->inset;
744                 if (inset->lyxCode() == InsetBase::BIBITEM_CODE)
745                         return static_cast<InsetBibitem *>(inset);
746         }
747         return 0;
748 }
749
750
751 bool Paragraph::forceDefaultParagraphs() const
752 {
753         return inInset() && inInset()->forceDefaultParagraphs(0);
754 }
755
756
757 namespace {
758
759 // paragraphs inside floats need different alignment tags to avoid
760 // unwanted space
761
762 bool noTrivlistCentering(InsetBase::Code code)
763 {
764         return code == InsetBase::FLOAT_CODE || code == InsetBase::WRAP_CODE;
765 }
766
767
768 string correction(string const & orig)
769 {
770         if (orig == "flushleft")
771                 return "raggedright";
772         if (orig == "flushright")
773                 return "raggedleft";
774         if (orig == "center")
775                 return "centering";
776         return orig;
777 }
778
779
780 string const corrected_env(string const & suffix, string const & env,
781         InsetBase::Code code)
782 {
783         string output = suffix + "{";
784         if (noTrivlistCentering(code))
785                 output += correction(env);
786         else
787                 output += env;
788         output += "}";
789         if (suffix == "\\begin")
790                 output += "\n";
791         return output;
792 }
793
794
795 int adjust_column_count(string const & str, int oldcol)
796 {
797         if (!contains(str, "\n"))
798                 return oldcol + str.size();
799         else {
800                 string tmp;
801                 return rsplit(str, tmp, '\n').size();
802         }
803 }
804
805 } // namespace anon
806
807
808 // This could go to ParagraphParameters if we want to
809 int Paragraph::startTeXParParams(BufferParams const & bparams,
810                                  odocstream & os, bool moving_arg) const
811 {
812         int column = 0;
813
814         if (params().noindent()) {
815                 os << "\\noindent ";
816                 column += 10;
817         }
818
819         switch (params().align()) {
820         case LYX_ALIGN_NONE:
821         case LYX_ALIGN_BLOCK:
822         case LYX_ALIGN_LAYOUT:
823         case LYX_ALIGN_SPECIAL:
824                 break;
825         case LYX_ALIGN_LEFT:
826         case LYX_ALIGN_RIGHT:
827         case LYX_ALIGN_CENTER:
828                 if (moving_arg) {
829                         os << "\\protect";
830                         column += 8;
831                 }
832                 break;
833         }
834
835         switch (params().align()) {
836         case LYX_ALIGN_NONE:
837         case LYX_ALIGN_BLOCK:
838         case LYX_ALIGN_LAYOUT:
839         case LYX_ALIGN_SPECIAL:
840                 break;
841         case LYX_ALIGN_LEFT: {
842                 string output;
843                 if (getParLanguage(bparams)->babel() != "hebrew")
844                         output = corrected_env("\\begin", "flushleft", ownerCode());
845                 else
846                         output = corrected_env("\\begin", "flushright", ownerCode());
847                 os << from_ascii(output);
848                 column = adjust_column_count(output, column);
849                 break;
850         } case LYX_ALIGN_RIGHT: {
851                 string output;
852                 if (getParLanguage(bparams)->babel() != "hebrew")
853                         output = corrected_env("\\begin", "flushright", ownerCode());
854                 else
855                         output = corrected_env("\\begin", "flushleft", ownerCode());
856                 os << from_ascii(output);
857                 column = adjust_column_count(output, column);
858                 break;
859         } case LYX_ALIGN_CENTER: {
860                 string output;
861                 output = corrected_env("\\begin", "center", ownerCode());
862                 os << from_ascii(output);
863                 column = adjust_column_count(output, column);
864                 break;
865         }
866         }
867
868         return column;
869 }
870
871
872 // This could go to ParagraphParameters if we want to
873 int Paragraph::endTeXParParams(BufferParams const & bparams,
874                                odocstream & os, bool moving_arg) const
875 {
876         int column = 0;
877
878         switch (params().align()) {
879         case LYX_ALIGN_NONE:
880         case LYX_ALIGN_BLOCK:
881         case LYX_ALIGN_LAYOUT:
882         case LYX_ALIGN_SPECIAL:
883                 break;
884         case LYX_ALIGN_LEFT:
885         case LYX_ALIGN_RIGHT:
886         case LYX_ALIGN_CENTER:
887                 if (moving_arg) {
888                         os << "\\protect";
889                         column = 8;
890                 }
891                 break;
892         }
893
894         switch (params().align()) {
895         case LYX_ALIGN_NONE:
896         case LYX_ALIGN_BLOCK:
897         case LYX_ALIGN_LAYOUT:
898         case LYX_ALIGN_SPECIAL:
899                 break;
900         case LYX_ALIGN_LEFT: {
901                 string output;
902                 if (getParLanguage(bparams)->babel() != "hebrew")
903                         output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
904                 else
905                         output = corrected_env("\n\\par\\end", "flushright", ownerCode());
906                 os << from_ascii(output);
907                 column = adjust_column_count(output, column);
908                 break;
909         } case LYX_ALIGN_RIGHT: {
910                 string output;
911                 if (getParLanguage(bparams)->babel() != "hebrew")
912                         output = corrected_env("\n\\par\\end", "flushright", ownerCode());
913                 else
914                         output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
915                 os << from_ascii(output);
916                 column = adjust_column_count(output, column);
917                 break;
918         } case LYX_ALIGN_CENTER: {
919                 string output;
920                 output = corrected_env("\n\\par\\end", "center", ownerCode());
921                 os << from_ascii(output);
922                 column = adjust_column_count(output, column);
923                 break;
924         }
925         }
926
927         return column;
928 }
929
930
931 // This one spits out the text of the paragraph
932 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
933                                 BufferParams const & bparams,
934                                 LyXFont const & outerfont,
935                                 odocstream & os, TexRow & texrow,
936                                 OutputParams const & runparams) const
937 {
938         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...     " << this << endl;
939
940         bool return_value = false;
941
942         LyXLayout_ptr style;
943
944         // well we have to check if we are in an inset with unlimited
945         // length (all in one row) if that is true then we don't allow
946         // any special options in the paragraph and also we don't allow
947         // any environment other than the default layout of the text class
948         // to be valid!
949         bool asdefault = forceDefaultParagraphs();
950
951         if (asdefault) {
952                 style = bparams.getLyXTextClass().defaultLayout();
953         } else {
954                 style = layout();
955         }
956
957         // Current base font for all inherited font changes, without any
958         // change caused by an individual character, except for the language:
959         // It is set to the language of the first character.
960         // As long as we are in the label, this font is the base font of the
961         // label. Before the first body character it is set to the base font
962         // of the body.
963         // This must be identical to basefont in TeXOnePar().
964         LyXFont basefont;
965
966         // output change tracking marks only if desired,
967         // if dvipost is installed,
968         // and with dvi/ps (other formats don't work)
969         bool const output = bparams.outputChanges
970                 && runparams.flavor == OutputParams::LATEX
971                 && LaTeXFeatures::isAvailable("dvipost");
972
973         // Maybe we have to create a optional argument.
974         pos_type body_pos = beginOfBody();
975         unsigned int column = 0;
976
977         if (body_pos > 0) {
978                 // the optional argument is kept in curly brackets in
979                 // case it contains a ']'
980                 os << "[{";
981                 column += 2;
982                 basefont = getLabelFont(bparams, outerfont);
983         } else {
984                 basefont = getLayoutFont(bparams, outerfont);
985         }
986
987         // Which font is currently active?
988         LyXFont running_font(basefont);
989         // Do we have an open font change?
990         bool open_font = false;
991
992         Change::Type runningChangeType = Change::UNCHANGED;
993
994         texrow.start(id(), 0);
995
996         // if the paragraph is empty, the loop will not be entered at all
997         if (empty()) {
998                 if (style->isCommand()) {
999                         os << '{';
1000                         ++column;
1001                 }
1002                 if (!asdefault)
1003                         column += startTeXParParams(bparams, os,
1004                                                     runparams.moving_arg);
1005         }
1006
1007         // Computed only once per paragraph since bparams.encoding() is expensive
1008         Encoding const & doc_encoding = bparams.encoding();
1009         for (pos_type i = 0; i < size(); ++i) {
1010                 // First char in paragraph or after label?
1011                 if (i == body_pos) {
1012                         if (body_pos > 0) {
1013                                 if (open_font) {
1014                                         column += running_font.latexWriteEndChanges(
1015                                                 os, basefont, basefont, bparams);
1016                                         open_font = false;
1017                                 }
1018                                 basefont = getLayoutFont(bparams, outerfont);
1019                                 running_font = basefont;
1020
1021                                 column += Changes::latexMarkChange(os,
1022                                                 runningChangeType, Change::UNCHANGED, output);
1023                                 runningChangeType = Change::UNCHANGED;
1024
1025                                 os << "}] ";
1026                                 column +=3;
1027                         }
1028                         if (style->isCommand()) {
1029                                 os << '{';
1030                                 ++column;
1031                         }
1032
1033                         if (!asdefault)
1034                                 column += startTeXParParams(bparams, os,
1035                                                             runparams.moving_arg);
1036                 }
1037
1038                 Change::Type changeType = pimpl_->lookupChange(i).type;
1039
1040                 // do not output text which is marked deleted
1041                 // if change tracking output is disabled
1042                 if (!output && changeType == Change::DELETED) {
1043                         runningChangeType = changeType;
1044                         continue;
1045                 }
1046
1047                 ++column;
1048                 
1049                 column += Changes::latexMarkChange(os, runningChangeType,
1050                         changeType, output);
1051                 runningChangeType = changeType;
1052
1053                 value_type c = getChar(i);
1054
1055                 // Fully instantiated font
1056                 LyXFont font = getFont(bparams, i, outerfont);
1057
1058                 LyXFont const last_font = running_font;
1059
1060                 // Do we need to close the previous font?
1061                 if (open_font &&
1062                     (font != running_font ||
1063                      font.language() != running_font.language()))
1064                 {
1065                         column += running_font.latexWriteEndChanges(
1066                                         os, basefont,
1067                                         (i == body_pos-1) ? basefont : font,
1068                                         bparams);
1069                         running_font = basefont;
1070                         open_font = false;
1071                 }
1072
1073                 // Do we need to change font?
1074                 if ((font != running_font ||
1075                      font.language() != running_font.language()) &&
1076                         i != body_pos - 1)
1077                 {
1078                         column += font.latexWriteStartChanges(
1079                                         os, basefont, last_font, bparams);
1080                         running_font = font;
1081                         open_font = true;
1082                 }
1083
1084                 if (c == ' ') {
1085                         // Do not print the separation of the optional argument
1086                         if (i != body_pos - 1) {
1087                                 if (pimpl_->simpleTeXBlanks(bparams,
1088                                                 doc_encoding, os, texrow,
1089                                                 i, column, font, *style))
1090                                         // A surrogate pair was output. We
1091                                         // must not call simpleTeXSpecialChars
1092                                         // in this iteration, since
1093                                         // simpleTeXBlanks incremented i, and
1094                                         // simpleTeXSpecialChars would output
1095                                         // the combining character again.
1096                                         continue;
1097                         }
1098                 }
1099
1100                 OutputParams rp = runparams;
1101                 rp.free_spacing = style->free_spacing;
1102                 rp.local_font = &font;
1103                 rp.intitle = style->intitle;
1104                 pimpl_->simpleTeXSpecialChars(buf, bparams, doc_encoding, os,
1105                                         texrow, rp, font, running_font,
1106                                         basefont, outerfont, open_font,
1107                                         runningChangeType, *style, i, column, c);
1108         }
1109
1110         // If we have an open font definition, we have to close it
1111         if (open_font) {
1112 #ifdef FIXED_LANGUAGE_END_DETECTION
1113                 if (next_) {
1114                         running_font
1115                                 .latexWriteEndChanges(os, basefont,
1116                                         next_->getFont(bparams, 0, outerfont),
1117                                         bparams);
1118                 } else {
1119                         running_font.latexWriteEndChanges(os, basefont,
1120                                                           basefont, bparams);
1121                 }
1122 #else
1123 #ifdef WITH_WARNINGS
1124 //#warning For now we ALWAYS have to close the foreign font settings if they are
1125 //#warning there as we start another \selectlanguage with the next paragraph if
1126 //#warning we are in need of this. This should be fixed sometime (Jug)
1127 #endif
1128                 running_font.latexWriteEndChanges(os, basefont, basefont,
1129                                                   bparams);
1130 #endif
1131         }
1132
1133         column += Changes::latexMarkChange(os,
1134                         runningChangeType, Change::UNCHANGED, output);
1135
1136         // Needed if there is an optional argument but no contents.
1137         if (body_pos > 0 && body_pos == size()) {
1138                 os << "}]~";
1139                 return_value = false;
1140         }
1141
1142         if (!asdefault) {
1143                 column += endTeXParParams(bparams, os, runparams.moving_arg);
1144         }
1145
1146         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1147         return return_value;
1148 }
1149
1150
1151 namespace {
1152
1153 enum PAR_TAG {
1154         PAR_NONE=0,
1155         TT = 1,
1156         SF = 2,
1157         BF = 4,
1158         IT = 8,
1159         SL = 16,
1160         EM = 32
1161 };
1162
1163
1164 string tag_name(PAR_TAG const & pt) {
1165         switch (pt) {
1166         case PAR_NONE: return "!-- --";
1167         case TT: return "tt";
1168         case SF: return "sf";
1169         case BF: return "bf";
1170         case IT: return "it";
1171         case SL: return "sl";
1172         case EM: return "em";
1173         }
1174         return "";
1175 }
1176
1177
1178 inline
1179 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1180 {
1181         p1 = static_cast<PAR_TAG>(p1 | p2);
1182 }
1183
1184
1185 inline
1186 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1187 {
1188         p1 = static_cast<PAR_TAG>(p1 & ~p2);
1189 }
1190
1191 } // anon
1192
1193
1194 bool Paragraph::emptyTag() const
1195 {
1196         for (pos_type i = 0; i < size(); ++i) {
1197                 if (isInset(i)) {
1198                         InsetBase const * inset = getInset(i);
1199                         InsetBase::Code lyx_code = inset->lyxCode();
1200                         if (lyx_code != InsetBase::TOC_CODE &&
1201                             lyx_code != InsetBase::INCLUDE_CODE &&
1202                             lyx_code != InsetBase::GRAPHICS_CODE &&
1203                             lyx_code != InsetBase::ERT_CODE &&
1204                             lyx_code != InsetBase::FLOAT_CODE &&
1205                             lyx_code != InsetBase::TABULAR_CODE) {
1206                                 return false;
1207                         }
1208                 } else {
1209                         value_type c = getChar(i);
1210                         if (c != ' ' && c != '\t')
1211                                 return false;
1212                 }
1213         }
1214         return true;
1215 }
1216
1217
1218 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
1219 {
1220         for (pos_type i = 0; i < size(); ++i) {
1221                 if (isInset(i)) {
1222                         InsetBase const * inset = getInset(i);
1223                         InsetBase::Code lyx_code = inset->lyxCode();
1224                         if (lyx_code == InsetBase::LABEL_CODE) {
1225                                 string const id = static_cast<InsetCommand const *>(inset)->getContents();
1226                                 return "id='" + to_utf8(sgml::cleanID(buf, runparams, from_utf8(id))) + "'";
1227                         }
1228                 }
1229
1230         }
1231         return string();
1232 }
1233
1234
1235 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
1236 {
1237         pos_type i;
1238         for (i = 0; i < size(); ++i) {
1239                 if (isInset(i)) {
1240                         InsetBase const * inset = getInset(i);
1241                         inset->docbook(buf, os, runparams);
1242                 } else {
1243                         value_type c = getChar(i);
1244                         if (c == ' ')
1245                                 break;
1246                         os << sgml::escapeChar(c);
1247                 }
1248         }
1249         return i;
1250 }
1251
1252
1253 bool Paragraph::onlyText(Buffer const & buf, LyXFont const & outerfont, pos_type initial) const
1254 {
1255         LyXFont font_old;
1256
1257         for (pos_type i = initial; i < size(); ++i) {
1258                 LyXFont font = getFont(buf.params(), i, outerfont);
1259                 if (isInset(i))
1260                         return false;
1261                 if (i != initial && font != font_old)
1262                         return false;
1263                 font_old = font;
1264         }
1265
1266         return true;
1267 }
1268
1269
1270 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1271                                     odocstream & os,
1272                                     OutputParams const & runparams,
1273                                     LyXFont const & outerfont,
1274                                     pos_type initial) const
1275 {
1276         bool emph_flag = false;
1277
1278         LyXLayout_ptr const & style = layout();
1279         LyXFont font_old =
1280                 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1281
1282         if (style->pass_thru && !onlyText(buf, outerfont, initial))
1283                 os << "]]>";
1284
1285         // parsing main loop
1286         for (pos_type i = initial; i < size(); ++i) {
1287                 LyXFont font = getFont(buf.params(), i, outerfont);
1288
1289                 // handle <emphasis> tag
1290                 if (font_old.emph() != font.emph()) {
1291                         if (font.emph() == LyXFont::ON) {
1292                                 os << "<emphasis>";
1293                                 emph_flag = true;
1294                         } else if (i != initial) {
1295                                 os << "</emphasis>";
1296                                 emph_flag = false;
1297                         }
1298                 }
1299
1300                 if (isInset(i)) {
1301                         InsetBase const * inset = getInset(i);
1302                         inset->docbook(buf, os, runparams);
1303                 } else {
1304                         value_type c = getChar(i);
1305
1306                         if (style->pass_thru)
1307                                 os.put(c);
1308                         else
1309                                 os << sgml::escapeChar(c);
1310                 }
1311                 font_old = font;
1312         }
1313
1314         if (emph_flag) {
1315                 os << "</emphasis>";
1316         }
1317
1318         if (style->free_spacing)
1319                 os << '\n';
1320         if (style->pass_thru && !onlyText(buf, outerfont, initial))
1321                 os << "<![CDATA[";
1322 }
1323
1324
1325 bool Paragraph::isNewline(pos_type pos) const
1326 {
1327         return isInset(pos)
1328                 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1329 }
1330
1331
1332 bool Paragraph::isLineSeparator(pos_type pos) const
1333 {
1334         value_type const c = getChar(pos);
1335         return isLineSeparatorChar(c)
1336                 || (c == Paragraph::META_INSET && getInset(pos) &&
1337                 getInset(pos)->isLineSeparator());
1338 }
1339
1340
1341 /// Used by the spellchecker
1342 bool Paragraph::isLetter(pos_type pos) const
1343 {
1344         if (isInset(pos))
1345                 return getInset(pos)->isLetter();
1346         else {
1347                 value_type const c = getChar(pos);
1348                 return isLetterChar(c) || isDigit(c);
1349         }
1350 }
1351
1352
1353 Language const *
1354 Paragraph::getParLanguage(BufferParams const & bparams) const
1355 {
1356         if (!empty())
1357                 return getFirstFontSettings(bparams).language();
1358 #ifdef WITH_WARNINGS
1359 #warning FIXME we should check the prev par as well (Lgb)
1360 #endif
1361         return bparams.language;
1362 }
1363
1364
1365 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1366 {
1367         return lyxrc.rtl_support
1368                 && getParLanguage(bparams)->rightToLeft()
1369                 && ownerCode() != InsetBase::ERT_CODE;
1370 }
1371
1372
1373 void Paragraph::changeLanguage(BufferParams const & bparams,
1374                                Language const * from, Language const * to)
1375 {
1376         // change language including dummy font change at the end
1377         for (pos_type i = 0; i <= size(); ++i) {
1378                 LyXFont font = getFontSettings(bparams, i);
1379                 if (font.language() == from) {
1380                         font.setLanguage(to);
1381                         setFont(i, font);
1382                 }
1383         }
1384 }
1385
1386
1387 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1388 {
1389         Language const * doc_language = bparams.language;
1390         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1391         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1392
1393         for (; cit != end; ++cit)
1394                 if (cit->font().language() != ignore_language &&
1395                     cit->font().language() != latex_language &&
1396                     cit->font().language() != doc_language)
1397                         return true;
1398         return false;
1399 }
1400
1401
1402 // Convert the paragraph to a string.
1403 // Used for building the table of contents
1404 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
1405 {
1406         return asString(buffer, 0, size(), label);
1407 }
1408
1409
1410 docstring const Paragraph::asString(Buffer const & buffer,
1411                                  pos_type beg, pos_type end, bool label) const
1412 {
1413
1414         odocstringstream os;
1415
1416         if (beg == 0 && label && !params().labelString().empty())
1417                 os << params().labelString() << ' ';
1418
1419         for (pos_type i = beg; i < end; ++i) {
1420                 value_type const c = getUChar(buffer.params(), i);
1421                 if (isPrintable(c))
1422                         os.put(c);
1423                 else if (c == META_INSET)
1424                         getInset(i)->textString(buffer, os);
1425         }
1426
1427         return os.str();
1428 }
1429
1430
1431 void Paragraph::setInsetOwner(InsetBase * inset)
1432 {
1433         pimpl_->inset_owner = inset;
1434 }
1435
1436
1437 Change const & Paragraph::lookupChange(pos_type pos) const
1438 {
1439         BOOST_ASSERT(pos <= size());
1440         return pimpl_->lookupChange(pos);
1441 }
1442
1443
1444 bool Paragraph::isChanged(pos_type start, pos_type end) const
1445 {
1446         return pimpl_->isChanged(start, end);
1447 }
1448
1449
1450 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const
1451 {
1452         return pimpl_->isMergedOnEndOfParDeletion(trackChanges);
1453 }
1454
1455
1456 void Paragraph::setChange(Change const & change)
1457 {
1458         pimpl_->setChange(change);
1459 }
1460
1461
1462 void Paragraph::setChange(pos_type pos, Change const & change)
1463 {
1464         pimpl_->setChange(pos, change);
1465 }
1466
1467
1468 void Paragraph::acceptChanges(BufferParams const & bparams, pos_type start, pos_type end)
1469 {
1470         return pimpl_->acceptChanges(bparams, start, end);
1471 }
1472
1473
1474 void Paragraph::rejectChanges(BufferParams const & bparams, pos_type start, pos_type end)
1475 {
1476         return pimpl_->rejectChanges(bparams, start, end);
1477 }
1478
1479
1480 int Paragraph::id() const
1481 {
1482         return pimpl_->id_;
1483 }
1484
1485
1486 LyXLayout_ptr const & Paragraph::layout() const
1487 {
1488         return layout_;
1489 }
1490
1491
1492 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1493 {
1494         layout_ = new_layout;
1495 }
1496
1497
1498 InsetBase * Paragraph::inInset() const
1499 {
1500         return pimpl_->inset_owner;
1501 }
1502
1503
1504 InsetBase::Code Paragraph::ownerCode() const
1505 {
1506         return pimpl_->inset_owner
1507                 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1508 }
1509
1510
1511 ParagraphParameters & Paragraph::params()
1512 {
1513         return pimpl_->params;
1514 }
1515
1516
1517 ParagraphParameters const & Paragraph::params() const
1518 {
1519         return pimpl_->params;
1520 }
1521
1522
1523 bool Paragraph::isFreeSpacing() const
1524 {
1525         if (layout()->free_spacing)
1526                 return true;
1527
1528         // for now we just need this, later should we need this in some
1529         // other way we can always add a function to InsetBase too.
1530         return ownerCode() == InsetBase::ERT_CODE;
1531 }
1532
1533
1534 bool Paragraph::allowEmpty() const
1535 {
1536         if (layout()->keepempty)
1537                 return true;
1538         return ownerCode() == InsetBase::ERT_CODE;
1539 }
1540
1541
1542 char_type Paragraph::transformChar(char_type c, pos_type pos) const
1543 {
1544         if (!Encodings::is_arabic(c))
1545                 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isDigit(c))
1546                         // FIXME UNICODE What does this do?
1547                         return c + (0xb0 - '0');
1548                 else
1549                         return c;
1550
1551         value_type const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1552         value_type next_char = ' ';
1553
1554         for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1555                 value_type const par_char = getChar(i);
1556                 if (!Encodings::isComposeChar_arabic(par_char)) {
1557                         next_char = par_char;
1558                         break;
1559                 }
1560         }
1561
1562         if (Encodings::is_arabic(next_char)) {
1563                 if (Encodings::is_arabic(prev_char) &&
1564                         !Encodings::is_arabic_special(prev_char))
1565                         return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
1566                 else
1567                         return Encodings::transformChar(c, Encodings::FORM_INITIAL);
1568         } else {
1569                 if (Encodings::is_arabic(prev_char) &&
1570                         !Encodings::is_arabic_special(prev_char))
1571                         return Encodings::transformChar(c, Encodings::FORM_FINAL);
1572                 else
1573                         return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
1574         }
1575 }
1576
1577
1578 bool Paragraph::hfillExpansion(Row const & row, pos_type pos) const
1579 {
1580         if (!isHfill(pos))
1581                 return false;
1582
1583         BOOST_ASSERT(pos >= row.pos() && pos < row.endpos());
1584
1585         // expand at the end of a row only if there is another hfill on the same row
1586         if (pos == row.endpos() - 1) {
1587                 for (pos_type i = row.pos(); i < pos; i++) {
1588                         if (isHfill(i))
1589                                 return true;
1590                 }
1591                 return false;
1592         }
1593
1594         // expand at the beginning of a row only if it is the first row of a paragraph
1595         if (pos == row.pos()) {
1596                 return pos == 0;
1597         }
1598
1599         // do not expand in some labels
1600         if (layout()->margintype != MARGIN_MANUAL && pos < beginOfBody())
1601                 return false;
1602
1603         // if there is anything between the first char of the row and
1604         // the specified position that is neither a newline nor an hfill,
1605         // the hfill will be expanded, otherwise it won't
1606         for (pos_type i = row.pos(); i < pos; i++) {
1607                 if (!isNewline(i) && !isHfill(i))
1608                         return true;
1609         }
1610         return false;
1611 }
1612
1613
1614 bool Paragraph::checkBiblio(bool track_changes)
1615 {
1616         // Add bibitem insets if necessary
1617         if (layout()->labeltype != LABEL_BIBLIO)
1618                 return false;
1619
1620         bool hasbibitem = !insetlist.empty()
1621                 // Insist on it being in pos 0
1622                 && getChar(0) == Paragraph::META_INSET
1623                 && insetlist.begin()->inset->lyxCode() == InsetBase::BIBITEM_CODE;
1624
1625         if (hasbibitem)
1626                 return false;
1627
1628         InsetBibitem * inset(new InsetBibitem(InsetCommandParams("bibitem")));
1629         insertInset(0, static_cast<InsetBase *>(inset),
1630                 Change(track_changes ? Change::INSERTED : Change::UNCHANGED));
1631
1632         return true;
1633 }
1634
1635 } // namespace lyx