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