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