]> git.lyx.org Git - lyx.git/blob - src/paragraph.C
* change LFUN file-insert-ascii to file-insert-plaintext
[lyx.git] / src / paragraph.C
1 /**
2  * \file paragraph.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author Angus Leeming
10  * \author John Levon
11  * \author André Pönitz
12  * \author Dekel Tsur
13  * \author Jürgen Vigna
14  *
15  * Full author contact details are available in file CREDITS.
16  */
17
18 #include <config.h>
19
20 #include "paragraph.h"
21 #include "paragraph_pimpl.h"
22
23 #include "buffer.h"
24 #include "bufferparams.h"
25 #include "counters.h"
26 #include "encoding.h"
27 #include "debug.h"
28 #include "gettext.h"
29 #include "language.h"
30 #include "LaTeXFeatures.h"
31 #include "lyxfont.h"
32 #include "lyxrc.h"
33 #include "lyxrow.h"
34 #include "messages.h"
35 #include "outputparams.h"
36 #include "paragraph_funcs.h"
37 #include "ParagraphList_fwd.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 int Paragraph::stripLeadingSpaces(bool trackChanges)
565 {
566         if (isFreeSpacing())
567                 return 0;
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;
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 int adjust_column_count(string const & str, int oldcol)
797 {
798         if (!contains(str, "\n"))
799                 return oldcol + str.size();
800         else {
801                 string tmp;
802                 return rsplit(str, tmp, '\n').size();
803         }
804 }
805
806 } // namespace anon
807
808
809 // This could go to ParagraphParameters if we want to
810 int Paragraph::startTeXParParams(BufferParams const & bparams,
811                                  odocstream & os, bool moving_arg) const
812 {
813         int column = 0;
814
815         if (params().noindent()) {
816                 os << "\\noindent ";
817                 column += 10;
818         }
819
820         switch (params().align()) {
821         case LYX_ALIGN_NONE:
822         case LYX_ALIGN_BLOCK:
823         case LYX_ALIGN_LAYOUT:
824         case LYX_ALIGN_SPECIAL:
825                 break;
826         case LYX_ALIGN_LEFT:
827         case LYX_ALIGN_RIGHT:
828         case LYX_ALIGN_CENTER:
829                 if (moving_arg) {
830                         os << "\\protect";
831                         column += 8;
832                 }
833                 break;
834         }
835
836         switch (params().align()) {
837         case LYX_ALIGN_NONE:
838         case LYX_ALIGN_BLOCK:
839         case LYX_ALIGN_LAYOUT:
840         case LYX_ALIGN_SPECIAL:
841                 break;
842         case LYX_ALIGN_LEFT: {
843                 string output;
844                 if (getParLanguage(bparams)->babel() != "hebrew")
845                         output = corrected_env("\\begin", "flushleft", ownerCode());
846                 else
847                         output = corrected_env("\\begin", "flushright", ownerCode());
848                 os << from_ascii(output);
849                 column = adjust_column_count(output, column);
850                 break;
851         } case LYX_ALIGN_RIGHT: {
852                 string output;
853                 if (getParLanguage(bparams)->babel() != "hebrew")
854                         output = corrected_env("\\begin", "flushright", ownerCode());
855                 else
856                         output = corrected_env("\\begin", "flushleft", ownerCode());
857                 os << from_ascii(output);
858                 column = adjust_column_count(output, column);
859                 break;
860         } case LYX_ALIGN_CENTER: {
861                 string output;
862                 output = corrected_env("\\begin", "center", ownerCode());
863                 os << from_ascii(output);
864                 column = adjust_column_count(output, column);
865                 break;
866         }
867         }
868
869         return column;
870 }
871
872
873 // This could go to ParagraphParameters if we want to
874 int Paragraph::endTeXParParams(BufferParams const & bparams,
875                                odocstream & os, bool moving_arg) const
876 {
877         int column = 0;
878
879         switch (params().align()) {
880         case LYX_ALIGN_NONE:
881         case LYX_ALIGN_BLOCK:
882         case LYX_ALIGN_LAYOUT:
883         case LYX_ALIGN_SPECIAL:
884                 break;
885         case LYX_ALIGN_LEFT:
886         case LYX_ALIGN_RIGHT:
887         case LYX_ALIGN_CENTER:
888                 if (moving_arg) {
889                         os << "\\protect";
890                         column = 8;
891                 }
892                 break;
893         }
894
895         switch (params().align()) {
896         case LYX_ALIGN_NONE:
897         case LYX_ALIGN_BLOCK:
898         case LYX_ALIGN_LAYOUT:
899         case LYX_ALIGN_SPECIAL:
900                 break;
901         case LYX_ALIGN_LEFT: {
902                 string output;
903                 if (getParLanguage(bparams)->babel() != "hebrew")
904                         output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
905                 else
906                         output = corrected_env("\n\\par\\end", "flushright", ownerCode());
907                 os << from_ascii(output);
908                 column = adjust_column_count(output, column);
909                 break;
910         } case LYX_ALIGN_RIGHT: {
911                 string output;
912                 if (getParLanguage(bparams)->babel() != "hebrew")
913                         output = corrected_env("\n\\par\\end", "flushright", ownerCode());
914                 else
915                         output = corrected_env("\n\\par\\end", "flushleft", ownerCode());
916                 os << from_ascii(output);
917                 column = adjust_column_count(output, column);
918                 break;
919         } case LYX_ALIGN_CENTER: {
920                 string output;
921                 output = corrected_env("\n\\par\\end", "center", ownerCode());
922                 os << from_ascii(output);
923                 column = adjust_column_count(output, column);
924                 break;
925         }
926         }
927
928         return column;
929 }
930
931
932 // This one spits out the text of the paragraph
933 bool Paragraph::simpleTeXOnePar(Buffer const & buf,
934                                 BufferParams const & bparams,
935                                 LyXFont const & outerfont,
936                                 odocstream & os, TexRow & texrow,
937                                 OutputParams const & runparams) const
938 {
939         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...     " << this << endl;
940
941         bool return_value = false;
942
943         LyXLayout_ptr style;
944
945         // well we have to check if we are in an inset with unlimited
946         // length (all in one row) if that is true then we don't allow
947         // any special options in the paragraph and also we don't allow
948         // any environment other than "Standard" to be valid!
949         bool asdefault = forceDefaultParagraphs();
950
951         if (asdefault) {
952                 style = bparams.getLyXTextClass().defaultLayout();
953         } else {
954                 style = layout();
955         }
956
957         // Current base font for all inherited font changes, without any
958         // change caused by an individual character, except for the language:
959         // It is set to the language of the first character.
960         // As long as we are in the label, this font is the base font of the
961         // label. Before the first body character it is set to the base font
962         // of the body.
963         // This must be identical to basefont in TeXOnePar().
964         LyXFont basefont;
965
966         LaTeXFeatures features(buf, bparams, runparams);
967
968         // output change tracking marks only if desired,
969         // if dvipost is installed,
970         // and with dvi/ps (other formats don't work)
971         bool const output = bparams.outputChanges
972                 && runparams.flavor == OutputParams::LATEX
973                 && features.isAvailable("dvipost");
974
975         // Maybe we have to create a optional argument.
976         pos_type body_pos = beginOfBody();
977         unsigned int column = 0;
978
979         if (body_pos > 0) {
980                 // the optional argument is kept in curly brackets in
981                 // case it contains a ']'
982                 os << "[{";
983                 column += 2;
984                 basefont = getLabelFont(bparams, outerfont);
985         } else {
986                 basefont = getLayoutFont(bparams, outerfont);
987         }
988
989         // Which font is currently active?
990         LyXFont running_font(basefont);
991         // Do we have an open font change?
992         bool open_font = false;
993
994         Change::Type runningChangeType = Change::UNCHANGED;
995
996         texrow.start(id(), 0);
997
998         // if the paragraph is empty, the loop will not be entered at all
999         if (empty()) {
1000                 if (style->isCommand()) {
1001                         os << '{';
1002                         ++column;
1003                 }
1004                 if (!asdefault)
1005                         column += startTeXParParams(bparams, os,
1006                                                     runparams.moving_arg);
1007         }
1008
1009         for (pos_type i = 0; i < size(); ++i) {
1010                 ++column;
1011                 // First char in paragraph or after label?
1012                 if (i == body_pos) {
1013                         if (body_pos > 0) {
1014                                 if (open_font) {
1015                                         column += running_font.latexWriteEndChanges(
1016                                                 os, basefont, basefont, bparams);
1017                                         open_font = false;
1018                                 }
1019                                 basefont = getLayoutFont(bparams, outerfont);
1020                                 running_font = basefont;
1021                                 os << "}] ";
1022                                 column +=3;
1023                         }
1024                         if (style->isCommand()) {
1025                                 os << '{';
1026                                 ++column;
1027                         }
1028
1029                         if (!asdefault)
1030                                 column += startTeXParParams(bparams, os,
1031                                                             runparams.moving_arg);
1032                 }
1033
1034                 value_type c = getChar(i);
1035
1036                 // Fully instantiated font
1037                 LyXFont font = getFont(bparams, i, outerfont);
1038
1039                 LyXFont const last_font = running_font;
1040
1041                 // Spaces at end of font change are simulated to be
1042                 // outside font change, i.e. we write "\textXX{text} "
1043                 // rather than "\textXX{text }". (Asger)
1044                 if (open_font && c == ' ' && i <= size() - 2) {
1045                         LyXFont const & next_font = getFont(bparams, i + 1, outerfont);
1046                         if (next_font != running_font && next_font != font) {
1047                                 font = next_font;
1048                         }
1049                 }
1050
1051                 // We end font definition before blanks
1052                 if (open_font &&
1053                     (font != running_font ||
1054                      font.language() != running_font.language()))
1055                 {
1056                         column += running_font.latexWriteEndChanges(
1057                                         os, basefont,
1058                                         (i == body_pos-1) ? basefont : font,
1059                                         bparams);
1060                         running_font = basefont;
1061                         open_font = false;
1062                 }
1063
1064                 // Blanks are printed before start of fontswitch
1065                 if (c == ' ') {
1066                         // Do not print the separation of the optional argument
1067                         if (i != body_pos - 1) {
1068                                 pimpl_->simpleTeXBlanks(os, texrow, i,
1069                                                        column, font, *style);
1070                         }
1071                 }
1072
1073                 // Do we need to change font?
1074                 if ((font != running_font ||
1075                      font.language() != running_font.language()) &&
1076                         i != body_pos - 1)
1077                 {
1078                         column += font.latexWriteStartChanges(
1079                                         os, basefont, last_font, bparams);
1080                         running_font = font;
1081                         open_font = true;
1082                 }
1083
1084                 Change::Type changeType = pimpl_->lookupChange(i).type;
1085
1086                 column += Changes::latexMarkChange(os, runningChangeType,
1087                         changeType, output);
1088                 runningChangeType = changeType;
1089
1090                 // do not output text which is marked deleted
1091                 // if change tracking output is not desired
1092                 if (output || runningChangeType != Change::DELETED) {
1093                         OutputParams rp = runparams;
1094                         rp.free_spacing = style->free_spacing;
1095                         rp.local_font = &font;
1096                         rp.intitle = style->intitle;
1097                         pimpl_->simpleTeXSpecialChars(buf, bparams,
1098                                                 os, texrow, rp,
1099                                                 font, running_font,
1100                                                 basefont, outerfont, open_font,
1101                                                 runningChangeType,
1102                                                 *style, i, column, c);
1103                 }
1104         }
1105
1106         column += Changes::latexMarkChange(os,
1107                         runningChangeType, Change::UNCHANGED, output);
1108
1109         // If we have an open font definition, we have to close it
1110         if (open_font) {
1111 #ifdef FIXED_LANGUAGE_END_DETECTION
1112                 if (next_) {
1113                         running_font
1114                                 .latexWriteEndChanges(os, basefont,
1115                                         next_->getFont(bparams, 0, outerfont),
1116                                         bparams);
1117                 } else {
1118                         running_font.latexWriteEndChanges(os, basefont,
1119                                                           basefont, bparams);
1120                 }
1121 #else
1122 #ifdef WITH_WARNINGS
1123 //#warning For now we ALWAYS have to close the foreign font settings if they are
1124 //#warning there as we start another \selectlanguage with the next paragraph if
1125 //#warning we are in need of this. This should be fixed sometime (Jug)
1126 #endif
1127                 running_font.latexWriteEndChanges(os, basefont, basefont,
1128                                                   bparams);
1129 #endif
1130         }
1131
1132         // Needed if there is an optional argument but no contents.
1133         if (body_pos > 0 && body_pos == size()) {
1134                 os << "}]~";
1135                 return_value = false;
1136         }
1137
1138         if (!asdefault) {
1139                 column += endTeXParParams(bparams, os, runparams.moving_arg);
1140         }
1141
1142         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1143         return return_value;
1144 }
1145
1146
1147 namespace {
1148
1149 // checks, if newcol chars should be put into this line
1150 // writes newline, if necessary.
1151 void sgmlLineBreak(ostream & os, string::size_type & colcount,
1152                           string::size_type newcol)
1153 {
1154         colcount += newcol;
1155         if (colcount > lyxrc.plaintext_linelen) {
1156                 os << "\n";
1157                 colcount = newcol; // assume write after this call
1158         }
1159 }
1160
1161 enum PAR_TAG {
1162         PAR_NONE=0,
1163         TT = 1,
1164         SF = 2,
1165         BF = 4,
1166         IT = 8,
1167         SL = 16,
1168         EM = 32
1169 };
1170
1171
1172 string tag_name(PAR_TAG const & pt) {
1173         switch (pt) {
1174         case PAR_NONE: return "!-- --";
1175         case TT: return "tt";
1176         case SF: return "sf";
1177         case BF: return "bf";
1178         case IT: return "it";
1179         case SL: return "sl";
1180         case EM: return "em";
1181         }
1182         return "";
1183 }
1184
1185
1186 inline
1187 void operator|=(PAR_TAG & p1, PAR_TAG const & p2)
1188 {
1189         p1 = static_cast<PAR_TAG>(p1 | p2);
1190 }
1191
1192
1193 inline
1194 void reset(PAR_TAG & p1, PAR_TAG const & p2)
1195 {
1196         p1 = static_cast<PAR_TAG>(p1 & ~p2);
1197 }
1198
1199 } // anon
1200
1201
1202 bool Paragraph::emptyTag() const
1203 {
1204         for (pos_type i = 0; i < size(); ++i) {
1205                 if (isInset(i)) {
1206                         InsetBase const * inset = getInset(i);
1207                         InsetBase::Code lyx_code = inset->lyxCode();
1208                         if (lyx_code != InsetBase::TOC_CODE &&
1209                             lyx_code != InsetBase::INCLUDE_CODE &&
1210                             lyx_code != InsetBase::GRAPHICS_CODE &&
1211                             lyx_code != InsetBase::ERT_CODE &&
1212                             lyx_code != InsetBase::FLOAT_CODE &&
1213                             lyx_code != InsetBase::TABULAR_CODE) {
1214                                 return false;
1215                         }
1216                 } else {
1217                         value_type c = getChar(i);
1218                         if (c != ' ' && c != '\t')
1219                                 return false;
1220                 }
1221         }
1222         return true;
1223 }
1224
1225
1226 string Paragraph::getID(Buffer const & buf, OutputParams const & runparams) const
1227 {
1228         for (pos_type i = 0; i < size(); ++i) {
1229                 if (isInset(i)) {
1230                         InsetBase const * inset = getInset(i);
1231                         InsetBase::Code lyx_code = inset->lyxCode();
1232                         if (lyx_code == InsetBase::LABEL_CODE) {
1233                                 string const id = static_cast<InsetCommand const *>(inset)->getContents();
1234                                 return "id='" + to_utf8(sgml::cleanID(buf, runparams, from_utf8(id))) + "'";
1235                         }
1236                 }
1237
1238         }
1239         return string();
1240 }
1241
1242
1243 pos_type Paragraph::getFirstWord(Buffer const & buf, odocstream & os, OutputParams const & runparams) const
1244 {
1245         pos_type i;
1246         for (i = 0; i < size(); ++i) {
1247                 if (isInset(i)) {
1248                         InsetBase const * inset = getInset(i);
1249                         inset->docbook(buf, os, runparams);
1250                 } else {
1251                         value_type c = getChar(i);
1252                         if (c == ' ')
1253                                 break;
1254                         os << sgml::escapeChar(c);
1255                 }
1256         }
1257         return i;
1258 }
1259
1260
1261 bool Paragraph::onlyText(Buffer const & buf, LyXFont const & outerfont, pos_type initial) const
1262 {
1263         LyXFont font_old;
1264
1265         for (pos_type i = initial; i < size(); ++i) {
1266                 LyXFont font = getFont(buf.params(), i, outerfont);
1267                 if (isInset(i))
1268                         return false;
1269                 if (i != initial && font != font_old)
1270                         return false;
1271                 font_old = font;
1272         }
1273
1274         return true;
1275 }
1276
1277
1278 void Paragraph::simpleDocBookOnePar(Buffer const & buf,
1279                                     odocstream & os,
1280                                     OutputParams const & runparams,
1281                                     LyXFont const & outerfont,
1282                                     pos_type initial) const
1283 {
1284         bool emph_flag = false;
1285
1286         LyXLayout_ptr const & style = layout();
1287         LyXFont font_old =
1288                 style->labeltype == LABEL_MANUAL ? style->labelfont : style->font;
1289
1290         if (style->pass_thru && !onlyText(buf, outerfont, initial))
1291                 os << "]]>";
1292
1293         // parsing main loop
1294         for (pos_type i = initial; i < size(); ++i) {
1295                 LyXFont font = getFont(buf.params(), i, outerfont);
1296
1297                 // handle <emphasis> tag
1298                 if (font_old.emph() != font.emph()) {
1299                         if (font.emph() == LyXFont::ON) {
1300                                 os << "<emphasis>";
1301                                 emph_flag = true;
1302                         } else if (i != initial) {
1303                                 os << "</emphasis>";
1304                                 emph_flag = false;
1305                         }
1306                 }
1307
1308                 if (isInset(i)) {
1309                         InsetBase const * inset = getInset(i);
1310                         inset->docbook(buf, os, runparams);
1311                 } else {
1312                         value_type c = getChar(i);
1313
1314                         if (style->pass_thru)
1315                                 os.put(c);
1316                         else
1317                                 os << sgml::escapeChar(c);
1318                 }
1319                 font_old = font;
1320         }
1321
1322         if (emph_flag) {
1323                 os << "</emphasis>";
1324         }
1325
1326         if (style->free_spacing)
1327                 os << '\n';
1328         if (style->pass_thru && !onlyText(buf, outerfont, initial))
1329                 os << "<![CDATA[";
1330 }
1331
1332
1333 bool Paragraph::isNewline(pos_type pos) const
1334 {
1335         return isInset(pos)
1336                 && getInset(pos)->lyxCode() == InsetBase::NEWLINE_CODE;
1337 }
1338
1339
1340 bool Paragraph::isLineSeparator(pos_type pos) const
1341 {
1342         value_type const c = getChar(pos);
1343         return isLineSeparatorChar(c)
1344                 || (c == Paragraph::META_INSET && getInset(pos) &&
1345                 getInset(pos)->isLineSeparator());
1346 }
1347
1348
1349 /// Used by the spellchecker
1350 bool Paragraph::isLetter(pos_type pos) const
1351 {
1352         if (isInset(pos))
1353                 return getInset(pos)->isLetter();
1354         else {
1355                 value_type const c = getChar(pos);
1356                 return isLetterChar(c) || isDigit(c);
1357         }
1358 }
1359
1360
1361 Language const *
1362 Paragraph::getParLanguage(BufferParams const & bparams) const
1363 {
1364         if (!empty())
1365                 return getFirstFontSettings(bparams).language();
1366 #ifdef WITH_WARNINGS
1367 #warning FIXME we should check the prev par as well (Lgb)
1368 #endif
1369         return bparams.language;
1370 }
1371
1372
1373 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1374 {
1375         return lyxrc.rtl_support
1376                 && getParLanguage(bparams)->rightToLeft()
1377                 && ownerCode() != InsetBase::ERT_CODE;
1378 }
1379
1380
1381 void Paragraph::changeLanguage(BufferParams const & bparams,
1382                                Language const * from, Language const * to)
1383 {
1384         // change language including dummy font change at the end
1385         for (pos_type i = 0; i <= size(); ++i) {
1386                 LyXFont font = getFontSettings(bparams, i);
1387                 if (font.language() == from) {
1388                         font.setLanguage(to);
1389                         setFont(i, font);
1390                 }
1391         }
1392 }
1393
1394
1395 bool Paragraph::isMultiLingual(BufferParams const & bparams) const
1396 {
1397         Language const * doc_language = bparams.language;
1398         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1399         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1400
1401         for (; cit != end; ++cit)
1402                 if (cit->font().language() != ignore_language &&
1403                     cit->font().language() != latex_language &&
1404                     cit->font().language() != doc_language)
1405                         return true;
1406         return false;
1407 }
1408
1409
1410 // Convert the paragraph to a string.
1411 // Used for building the table of contents
1412 docstring const Paragraph::asString(Buffer const & buffer, bool label) const
1413 {
1414         OutputParams runparams;
1415         return asString(buffer, runparams, label);
1416 }
1417
1418
1419 docstring const Paragraph::asString(Buffer const & buffer,
1420                                  OutputParams const & runparams,
1421                                  bool label) const
1422 {
1423 #if 0
1424         string s;
1425         if (label && !params().labelString().empty())
1426                 s += params().labelString() + ' ';
1427
1428         for (pos_type i = 0; i < size(); ++i) {
1429                 value_type c = getChar(i);
1430                 if (isPrintable(c))
1431                         s += c;
1432                 else if (c == META_INSET &&
1433                          getInset(i)->lyxCode() == InsetBase::MATH_CODE) {
1434                         ostringstream os;
1435                         getInset(i)->plaintext(buffer, os, runparams);
1436                         s += subst(STRCONV(os.str()),'\n',' ');
1437                 }
1438         }
1439
1440         return s;
1441 #else
1442         // This should really be done by the caller and not here.
1443         docstring ret = asString(buffer, runparams, 0, size(), label);
1444         return subst(ret, '\n', ' ');
1445 #endif
1446 }
1447
1448
1449 docstring const Paragraph::asString(Buffer const & buffer,
1450                                  pos_type beg, pos_type end, bool label) const
1451 {
1452
1453         OutputParams const runparams;
1454         return asString(buffer, runparams, beg, end, label);
1455 }
1456
1457
1458 docstring const Paragraph::asString(Buffer const & buffer,
1459                                  OutputParams const & runparams,
1460                                  pos_type beg, pos_type end, bool label) const
1461 {
1462         lyx::odocstringstream os;
1463
1464         if (beg == 0 && label && !params().labelString().empty())
1465                 os << params().labelString() << ' ';
1466
1467         for (pos_type i = beg; i < end; ++i) {
1468                 value_type const c = getUChar(buffer.params(), i);
1469                 if (isPrintable(c))
1470                         os.put(c);
1471                 else if (c == META_INSET)
1472                         getInset(i)->textString(buffer, os, runparams);
1473         }
1474
1475         return os.str();
1476 }
1477
1478
1479 void Paragraph::setInsetOwner(InsetBase * inset)
1480 {
1481         pimpl_->inset_owner = inset;
1482 }
1483
1484
1485 Change const & Paragraph::lookupChange(pos_type pos) const
1486 {
1487         BOOST_ASSERT(pos <= size());
1488         return pimpl_->lookupChange(pos);
1489 }
1490
1491
1492 bool Paragraph::isChanged(pos_type start, pos_type end) const
1493 {
1494         return pimpl_->isChanged(start, end);
1495 }
1496
1497
1498 bool Paragraph::isMergedOnEndOfParDeletion(bool trackChanges) const
1499 {
1500         return pimpl_->isMergedOnEndOfParDeletion(trackChanges);
1501 }
1502
1503
1504 void Paragraph::setChange(Change const & change)
1505 {
1506         pimpl_->setChange(change);
1507 }
1508
1509
1510 void Paragraph::setChange(pos_type pos, Change const & change)
1511 {
1512         pimpl_->setChange(pos, change);
1513 }
1514
1515
1516 void Paragraph::acceptChanges(pos_type start, pos_type end)
1517 {
1518         return pimpl_->acceptChanges(start, end);
1519 }
1520
1521
1522 void Paragraph::rejectChanges(pos_type start, pos_type end)
1523 {
1524         return pimpl_->rejectChanges(start, end);
1525 }
1526
1527
1528 int Paragraph::id() const
1529 {
1530         return pimpl_->id_;
1531 }
1532
1533
1534 LyXLayout_ptr const & Paragraph::layout() const
1535 {
1536         return layout_;
1537 }
1538
1539
1540 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1541 {
1542         layout_ = new_layout;
1543 }
1544
1545
1546 InsetBase * Paragraph::inInset() const
1547 {
1548         return pimpl_->inset_owner;
1549 }
1550
1551
1552 InsetBase::Code Paragraph::ownerCode() const
1553 {
1554         return pimpl_->inset_owner
1555                 ? pimpl_->inset_owner->lyxCode() : InsetBase::NO_CODE;
1556 }
1557
1558
1559 void Paragraph::clearContents()
1560 {
1561         text_.clear();
1562 }
1563
1564
1565 ParagraphParameters & Paragraph::params()
1566 {
1567         return pimpl_->params;
1568 }
1569
1570
1571 ParagraphParameters const & Paragraph::params() const
1572 {
1573         return pimpl_->params;
1574 }
1575
1576
1577 bool Paragraph::isFreeSpacing() const
1578 {
1579         if (layout()->free_spacing)
1580                 return true;
1581
1582         // for now we just need this, later should we need this in some
1583         // other way we can always add a function to InsetBase too.
1584         return ownerCode() == InsetBase::ERT_CODE;
1585 }
1586
1587
1588 bool Paragraph::allowEmpty() const
1589 {
1590         if (layout()->keepempty)
1591                 return true;
1592         return ownerCode() == InsetBase::ERT_CODE;
1593 }
1594
1595
1596 char_type Paragraph::transformChar(char_type c, pos_type pos) const
1597 {
1598         if (!Encodings::is_arabic(c))
1599                 if (lyxrc.font_norm_type == LyXRC::ISO_8859_6_8 && isDigit(c))
1600                         // FIXME UNICODE What does this do?
1601                         return c + (0xb0 - '0');
1602                 else
1603                         return c;
1604
1605         value_type const prev_char = pos > 0 ? getChar(pos - 1) : ' ';
1606         value_type next_char = ' ';
1607
1608         for (pos_type i = pos + 1, end = size(); i < end; ++i) {
1609                 value_type const par_char = getChar(i);
1610                 if (!Encodings::isComposeChar_arabic(par_char)) {
1611                         next_char = par_char;
1612                         break;
1613                 }
1614         }
1615
1616         if (Encodings::is_arabic(next_char)) {
1617                 if (Encodings::is_arabic(prev_char) &&
1618                         !Encodings::is_arabic_special(prev_char))
1619                         return Encodings::transformChar(c, Encodings::FORM_MEDIAL);
1620                 else
1621                         return Encodings::transformChar(c, Encodings::FORM_INITIAL);
1622         } else {
1623                 if (Encodings::is_arabic(prev_char) &&
1624                         !Encodings::is_arabic_special(prev_char))
1625                         return Encodings::transformChar(c, Encodings::FORM_FINAL);
1626                 else
1627                         return Encodings::transformChar(c, Encodings::FORM_ISOLATED);
1628         }
1629 }
1630
1631
1632 bool Paragraph::hfillExpansion(Row const & row, pos_type pos) const
1633 {
1634         if (!isHfill(pos))
1635                 return false;
1636
1637         BOOST_ASSERT(pos >= row.pos() && pos < row.endpos());
1638
1639         // expand at the end of a row only if there is another hfill on the same row
1640         if (pos == row.endpos() - 1) {
1641                 for (pos_type i = row.pos(); i < pos; i++) {
1642                         if (isHfill(i))
1643                                 return true;
1644                 }
1645                 return false;
1646         }
1647
1648         // expand at the beginning of a row only if it is the first row of a paragraph
1649         if (pos == row.pos()) {
1650                 return pos == 0;
1651         }
1652
1653         // do not expand in some labels
1654         if (layout()->margintype != MARGIN_MANUAL && pos < beginOfBody())
1655                 return false;
1656
1657         // if there is anything between the first char of the row and
1658         // the specified position that is neither a newline nor an hfill,
1659         // the hfill will be expanded, otherwise it won't
1660         for (pos_type i = row.pos(); i < pos; i++) {
1661                 if (!isNewline(i) && !isHfill(i))
1662                         return true;
1663         }
1664         return false;
1665 }
1666
1667
1668 bool Paragraph::checkBiblio(bool track_changes)
1669 {
1670         // Add bibitem insets if necessary
1671         if (layout()->labeltype != LABEL_BIBLIO)
1672                 return false;
1673
1674         bool hasbibitem = !insetlist.empty()
1675                 // Insist on it being in pos 0
1676                 && getChar(0) == Paragraph::META_INSET
1677                 && insetlist.begin()->inset->lyxCode() == InsetBase::BIBITEM_CODE;
1678
1679         if (hasbibitem)
1680                 return false;
1681
1682         InsetBibitem * inset(new InsetBibitem(InsetCommandParams("bibitem")));
1683         insertInset(0, static_cast<InsetBase *>(inset),
1684                 Change(track_changes ? Change::INSERTED : Change::UNCHANGED));
1685
1686         return true;
1687 }
1688
1689 } // namespace lyx