]> git.lyx.org Git - lyx.git/blob - src/paragraph.C
parlist-10-a.diff + a bit more
[lyx.git] / src / paragraph.C
1 /* This file is part of
2  * ======================================================
3  *
4  *           LyX, The Document Processor
5  *
6  *           Copyright 1995 Matthias Ettrich
7  *           Copyright 1995-2001 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #include "paragraph.h"
14 #include "paragraph_pimpl.h"
15 #include "lyxrc.h"
16 #include "layout.h"
17 #include "language.h"
18 #include "buffer.h"
19 #include "bufferparams.h"
20 #include "debug.h"
21 #include "texrow.h"
22 #include "BufferView.h"
23 #include "encoding.h"
24 #include "ParameterStruct.h"
25 #include "gettext.h"
26 #include "changes.h"
27 #include "paragraph_funcs.h"
28
29 #include "insets/insetbibitem.h"
30 #include "insets/insetoptarg.h"
31
32 #include "support/filetools.h"
33 #include "support/lstrings.h"
34 #include "support/lyxmanip.h"
35 #include "support/FileInfo.h"
36 #include "support/LAssert.h"
37 #include "support/textutils.h"
38
39 #include <algorithm>
40 #include <fstream>
41 #include <csignal>
42 #include <ctime>
43
44 using std::ostream;
45 using std::endl;
46 using std::fstream;
47 using std::ios;
48 using std::lower_bound;
49 using std::upper_bound;
50
51 using lyx::pos_type;
52
53
54 // this is a minibuffer
55
56 namespace {
57
58 char minibuffer_char;
59 LyXFont minibuffer_font;
60 Inset * minibuffer_inset;
61
62 } // namespace anon
63
64
65 extern BufferView * current_view;
66
67
68 Paragraph::Paragraph()
69         : pimpl_(new Paragraph::Pimpl(this))
70 {
71 #ifndef NO_NEXT
72         next_ = 0;
73         previous_ = 0;
74 #endif
75         enumdepth = 0;
76         itemdepth = 0;
77         params().clear();
78 }
79
80
81 #ifndef NO_NEXT
82 // This constructor inserts the new paragraph in a list.
83 // It is placed after par.
84 Paragraph::Paragraph(Paragraph * par)
85         : pimpl_(new Paragraph::Pimpl(this))
86 {
87         enumdepth = 0;
88         itemdepth = 0;
89
90         // double linked list begin
91         next_ = par->next_;
92         if (next_)
93                 next_->previous_ = this;
94         previous_ = par;
95         previous_->next_ = this;
96         // end
97
98         params().clear();
99 }
100 #endif
101
102
103 Paragraph::Paragraph(Paragraph const & lp, bool same_ids)
104         : pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this, same_ids))
105 {
106         enumdepth = 0;
107         itemdepth = 0;
108 #ifndef NO_NEXT
109         next_     = 0;
110         previous_ = 0;
111 #endif
112         // this is because of the dummy layout of the paragraphs that
113         // follow footnotes
114         layout_ = lp.layout();
115
116         // copy everything behind the break-position to the new paragraph
117         insetlist = lp.insetlist;
118         InsetList::iterator it = insetlist.begin();
119         InsetList::iterator end = insetlist.end();
120         for (; it != end; ++it) {
121                 it.setInset(it.getInset()->clone(*current_view->buffer(),
122                                                  same_ids));
123                 // tell the new inset who is the boss now
124                 it.getInset()->parOwner(this);
125         }
126 }
127
128
129 // the destructor removes the new paragraph from the list
130 Paragraph::~Paragraph()
131 {
132 #ifndef NO_NEXT
133         if (previous_)
134                 previous_->next_ = next_;
135         if (next_)
136                 next_->previous_ = previous_;
137 #endif
138
139         delete pimpl_;
140         //
141         //lyxerr << "Paragraph::paragraph_id = "
142         //       << Paragraph::paragraph_id << endl;
143 }
144
145
146 void Paragraph::write(Buffer const * buf, ostream & os,
147                           BufferParams const & bparams,
148                           depth_type & dth) const
149 {
150         // The beginning or end of a deeper (i.e. nested) area?
151         if (dth != params().depth()) {
152                 if (params().depth() > dth) {
153                         while (params().depth() > dth) {
154                                 os << "\n\\begin_deeper ";
155                                 ++dth;
156                         }
157                 } else {
158                         while (params().depth() < dth) {
159                                 os << "\n\\end_deeper ";
160                                 --dth;
161                         }
162                 }
163         }
164
165         // First write the layout
166         os << "\n\\layout " << layout()->name() << '\n';
167
168         params().write(os);
169
170         LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
171
172         Change running_change = Change(Change::UNCHANGED);
173         lyx::time_type const curtime(lyx::current_time());
174
175         int column = 0;
176         for (pos_type i = 0; i < size(); ++i) {
177                 if (!i) {
178                         os << '\n';
179                         column = 0;
180                 }
181
182                 Change change = pimpl_->lookupChangeFull(i);
183                 Changes::lyxMarkChange(os, column, curtime, running_change, change);
184                 running_change = change;
185
186                 // Write font changes
187                 LyXFont font2 = getFontSettings(bparams, i);
188                 if (font2 != font1) {
189                         font2.lyxWriteChanges(font1, os);
190                         column = 0;
191                         font1 = font2;
192                 }
193
194                 value_type const c = getChar(i);
195                 switch (c) {
196                 case META_INSET:
197                 {
198                         Inset const * inset = getInset(i);
199                         if (inset)
200                                 if (inset->directWrite()) {
201                                         // international char, let it write
202                                         // code directly so it's shorter in
203                                         // the file
204                                         inset->write(buf, os);
205                                 } else {
206                                         os << "\n\\begin_inset ";
207                                         inset->write(buf, os);
208                                         os << "\n\\end_inset \n\n";
209                                         column = 0;
210                                 }
211                 }
212                 break;
213                 case '\\':
214                         os << "\n\\backslash \n";
215                         column = 0;
216                         break;
217                 case '.':
218                         if (i + 1 < size() && getChar(i + 1) == ' ') {
219                                 os << ".\n";
220                                 column = 0;
221                         } else
222                                 os << '.';
223                         break;
224                 default:
225                         if ((column > 70 && c == ' ')
226                             || column > 79) {
227                                 os << '\n';
228                                 column = 0;
229                         }
230                         // this check is to amend a bug. LyX sometimes
231                         // inserts '\0' this could cause problems.
232                         if (c != '\0')
233                                 os << c;
234                         else
235                                 lyxerr << "ERROR (Paragraph::writeFile):"
236                                         " NULL char in structure." << endl;
237                         ++column;
238                         break;
239                 }
240         }
241
242         // to make reading work properly
243         if (!size()) {
244                 running_change = pimpl_->lookupChange(0);
245                 Changes::lyxMarkChange(os, column, curtime,
246                         Change(Change::UNCHANGED), running_change);
247         }
248         Changes::lyxMarkChange(os, column, curtime,
249                 running_change, Change(Change::UNCHANGED));
250 }
251
252
253 void Paragraph::validate(LaTeXFeatures & features) const
254 {
255         pimpl_->validate(features, *layout());
256 }
257
258
259 // First few functions needed for cut and paste and paragraph breaking.
260 void Paragraph::copyIntoMinibuffer(Buffer const & buffer, pos_type pos) const
261 {
262         BufferParams bparams = buffer.params;
263
264         minibuffer_char = getChar(pos);
265         minibuffer_font = getFontSettings(bparams, pos);
266         minibuffer_inset = 0;
267         if (minibuffer_char == Paragraph::META_INSET) {
268                 if (getInset(pos)) {
269                         minibuffer_inset = getInset(pos)->clone(buffer);
270                 } else {
271                         minibuffer_inset = 0;
272                         minibuffer_char = ' ';
273                         // This reflects what GetInset() does (ARRae)
274                 }
275         }
276 }
277
278
279 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams, pos_type pos)
280 {
281         minibuffer_char = getChar(pos);
282         minibuffer_font = getFontSettings(bparams, pos);
283         minibuffer_inset = 0;
284         if (minibuffer_char == Paragraph::META_INSET) {
285                 if (getInset(pos)) {
286                         // the inset is not in a paragraph anymore
287                         minibuffer_inset = insetlist.release(pos);
288                         minibuffer_inset->parOwner(0);
289                 } else {
290                         minibuffer_inset = 0;
291                         minibuffer_char = ' ';
292                         // This reflects what GetInset() does (ARRae)
293                 }
294
295         }
296
297         // Erase(pos); now the caller is responsible for that.
298 }
299
300
301 bool Paragraph::insertFromMinibuffer(pos_type pos)
302 {
303         if (minibuffer_char == Paragraph::META_INSET) {
304                 if (!insetAllowed(minibuffer_inset->lyxCode())) {
305                         return false;
306                 }
307                 insertInset(pos, minibuffer_inset, minibuffer_font);
308         } else {
309                 LyXFont f = minibuffer_font;
310                 if (!checkInsertChar(f)) {
311                         return false;
312                 }
313                 insertChar(pos, minibuffer_char, f);
314         }
315         return true;
316 }
317
318 // end of minibuffer
319
320
321 void Paragraph::eraseIntern(lyx::pos_type pos)
322 {
323         pimpl_->eraseIntern(pos);
324 }
325
326
327 void Paragraph::erase(pos_type pos)
328 {
329         pimpl_->erase(pos);
330 }
331
332
333 bool Paragraph::erase(pos_type start, pos_type end)
334 {
335         return pimpl_->erase(start, end);
336 }
337
338
339 bool Paragraph::checkInsertChar(LyXFont & font)
340 {
341         if (pimpl_->inset_owner)
342                 return pimpl_->inset_owner->checkInsertChar(font);
343         return true;
344 }
345
346
347 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
348 {
349         LyXFont const f(LyXFont::ALL_INHERIT);
350         insertChar(pos, c, f);
351 }
352
353
354 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
355                            LyXFont const & font, Change change)
356 {
357         pimpl_->insertChar(pos, c, font, change);
358 }
359
360
361 void Paragraph::insertInset(pos_type pos, Inset * inset)
362 {
363         LyXFont const f(LyXFont::ALL_INHERIT);
364         insertInset(pos, inset, f);
365 }
366
367
368 void Paragraph::insertInset(pos_type pos, Inset * inset, LyXFont const & font, Change change)
369 {
370         pimpl_->insertInset(pos, inset, font, change);
371 }
372
373
374 bool Paragraph::insetAllowed(Inset::Code code)
375 {
376         //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
377         if (pimpl_->inset_owner)
378                 return pimpl_->inset_owner->insetAllowed(code);
379         return true;
380 }
381
382
383 Inset * Paragraph::getInset(pos_type pos)
384 {
385         lyx::Assert(pos < size());
386         return insetlist.get(pos);
387 }
388
389
390 Inset const * Paragraph::getInset(pos_type pos) const
391 {
392         lyx::Assert(pos < size());
393         return insetlist.get(pos);
394 }
395
396
397 // Gets uninstantiated font setting at position.
398 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
399                                          pos_type pos) const
400 {
401         lyx::Assert(pos <= size());
402
403         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
404         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
405         for (; cit != end; ++cit) {
406                 if (cit->pos() >= pos)
407                         break;
408         }
409
410         LyXFont retfont;
411         if (cit != end)
412                 retfont = cit->font();
413         else if (pos == size() && !empty())
414                 retfont = getFontSettings(bparams, pos - 1);
415         else
416                 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
417
418         return retfont;
419 }
420
421
422 // Gets uninstantiated font setting at position 0
423 LyXFont const Paragraph::getFirstFontSettings() const
424 {
425         if (!empty() && !pimpl_->fontlist.empty())
426                 return pimpl_->fontlist[0].font();
427
428         return LyXFont(LyXFont::ALL_INHERIT);
429 }
430
431
432 // Gets the fully instantiated font at a given position in a paragraph
433 // This is basically the same function as LyXText::GetFont() in text2.C.
434 // The difference is that this one is used for generating the LaTeX file,
435 // and thus cosmetic "improvements" are disallowed: This has to deliver
436 // the true picture of the buffer. (Asger)
437 LyXFont const Paragraph::getFont(BufferParams const & bparams,
438                                  pos_type pos) const
439 {
440         lyx::Assert(pos >= 0);
441
442         LyXLayout_ptr const & lout = layout();
443
444         pos_type const body_pos = beginningOfBody();
445
446         LyXFont layoutfont;
447         if (pos < body_pos)
448                 layoutfont = lout->labelfont;
449         else
450                 layoutfont = lout->font;
451
452         LyXFont tmpfont = getFontSettings(bparams, pos);
453         tmpfont.realize(layoutfont);
454
455         return pimpl_->realizeFont(tmpfont, bparams);
456 }
457
458
459 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
460 {
461         LyXLayout_ptr const & lout = layout();
462
463         LyXFont tmpfont = lout->labelfont;
464         tmpfont.setLanguage(getParLanguage(bparams));
465
466         return pimpl_->realizeFont(tmpfont, bparams);
467 }
468
469
470 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
471 {
472         LyXLayout_ptr const & lout = layout();
473
474         LyXFont tmpfont = lout->font;
475         tmpfont.setLanguage(getParLanguage(bparams));
476
477         return pimpl_->realizeFont(tmpfont, bparams);
478 }
479
480
481 /// Returns the height of the highest font in range
482 LyXFont::FONT_SIZE
483 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
484                               LyXFont::FONT_SIZE const def_size) const
485 {
486         if (pimpl_->fontlist.empty())
487                 return def_size;
488
489         Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
490         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
491         for (; end_it != end; ++end_it) {
492                 if (end_it->pos() >= endpos)
493                         break;
494         }
495
496         if (end_it != end)
497                 ++end_it;
498
499         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
500         for (; cit != end; ++cit) {
501                 if (cit->pos() >= startpos)
502                         break;
503         }
504
505         LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
506         for (; cit != end_it; ++cit) {
507                 LyXFont::FONT_SIZE size = cit->font().size();
508                 if (size == LyXFont::INHERIT_SIZE)
509                         size = def_size;
510                 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
511                         maxsize = size;
512         }
513         return maxsize;
514 }
515
516
517 Paragraph::value_type
518 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
519 {
520         value_type c = getChar(pos);
521         if (!lyxrc.rtl_support)
522                 return c;
523
524         value_type uc = c;
525         switch (c) {
526         case '(':
527                 uc = ')';
528                 break;
529         case ')':
530                 uc = '(';
531                 break;
532         case '[':
533                 uc = ']';
534                 break;
535         case ']':
536                 uc = '[';
537                 break;
538         case '{':
539                 uc = '}';
540                 break;
541         case '}':
542                 uc = '{';
543                 break;
544         case '<':
545                 uc = '>';
546                 break;
547         case '>':
548                 uc = '<';
549                 break;
550         }
551         if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
552                 return uc;
553         else
554                 return c;
555 }
556
557
558 void Paragraph::setFont(pos_type pos, LyXFont const & font)
559 {
560         lyx::Assert(pos <= size());
561
562         // First, reduce font against layout/label font
563         // Update: The SetCharFont() routine in text2.C already
564         // reduces font, so we don't need to do that here. (Asger)
565         // No need to simplify this because it will disappear
566         // in a new kernel. (Asger)
567         // Next search font table
568
569         Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
570         Pimpl::FontList::iterator it = beg;
571         Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
572         for (; it != endit; ++it) {
573                 if (it->pos() >= pos)
574                         break;
575         }
576         unsigned int i = std::distance(beg, it);
577         bool notfound = (it == endit);
578
579         if (!notfound && pimpl_->fontlist[i].font() == font)
580                 return;
581
582         bool begin = pos == 0 || notfound ||
583                 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
584         // Is position pos is a beginning of a font block?
585         bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
586         // Is position pos is the end of a font block?
587         if (begin && end) { // A single char block
588                 if (i + 1 < pimpl_->fontlist.size() &&
589                     pimpl_->fontlist[i + 1].font() == font) {
590                         // Merge the singleton block with the next block
591                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
592                         if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
593                                 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i - 1);
594                 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
595                         // Merge the singleton block with the previous block
596                         pimpl_->fontlist[i - 1].pos(pos);
597                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
598                 } else
599                         pimpl_->fontlist[i].font(font);
600         } else if (begin) {
601                 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
602                         pimpl_->fontlist[i - 1].pos(pos);
603                 else
604                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
605                                         Pimpl::FontTable(pos, font));
606         } else if (end) {
607                 pimpl_->fontlist[i].pos(pos - 1);
608                 if (!(i + 1 < pimpl_->fontlist.size() &&
609                       pimpl_->fontlist[i + 1].font() == font))
610                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
611                                         Pimpl::FontTable(pos, font));
612         } else { // The general case. The block is splitted into 3 blocks
613                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
614                                 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
615                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
616                                 Pimpl::FontTable(pos, font));
617         }
618 }
619
620
621 #ifndef NO_NEXT
622 void Paragraph::next(Paragraph * p)
623 {
624         next_ = p;
625 }
626
627
628 // This function is able to hide closed footnotes.
629 Paragraph * Paragraph::next()
630 {
631         return next_;
632 }
633
634
635 Paragraph const * Paragraph::next() const
636 {
637         return next_;
638 }
639
640
641 void Paragraph::previous(Paragraph * p)
642 {
643         previous_ = p;
644 }
645
646
647 // This function is able to hide closed footnotes.
648 Paragraph * Paragraph::previous()
649 {
650         return previous_;
651 }
652
653
654 // This function is able to hide closed footnotes.
655 Paragraph const * Paragraph::previous() const
656 {
657         return previous_;
658 }
659 #endif
660
661
662 void Paragraph::makeSameLayout(Paragraph const * par)
663 {
664         layout(par->layout());
665         // move to pimpl?
666         params() = par->params();
667 }
668
669
670 int Paragraph::stripLeadingSpaces()
671 {
672         if (layout()->free_spacing || isFreeSpacing())
673                 return 0;
674
675         int i = 0;
676         while (!empty() && (isNewline(0) || isLineSeparator(0))) {
677                 pimpl_->eraseIntern(0);
678                 ++i;
679         }
680
681         return i;
682 }
683
684
685 bool Paragraph::hasSameLayout(Paragraph const * par) const
686 {
687         return
688                 par->layout() == layout() &&
689                 params().sameLayout(par->params());
690 }
691
692
693 Paragraph::depth_type Paragraph::getDepth() const
694 {
695         return params().depth();
696 }
697
698
699 Paragraph::depth_type Paragraph::getMaxDepthAfter() const
700 {
701         if (layout()->isEnvironment())
702                 return params().depth() + 1;
703         else
704                 return params().depth();
705 }
706
707
708 char Paragraph::getAlign() const
709 {
710         return params().align();
711 }
712
713
714 string const & Paragraph::getLabelstring() const
715 {
716         return params().labelString();
717 }
718
719
720 // the next two functions are for the manual labels
721 string const Paragraph::getLabelWidthString() const
722 {
723         if (!params().labelWidthString().empty())
724                 return params().labelWidthString();
725         else
726                 return _("Senseless with this layout!");
727 }
728
729
730 void Paragraph::setLabelWidthString(string const & s)
731 {
732         params().labelWidthString(s);
733 }
734
735
736 void Paragraph::applyLayout(LyXLayout_ptr const & new_layout)
737 {
738         layout(new_layout);
739         params().labelWidthString(string());
740         params().align(LYX_ALIGN_LAYOUT);
741         params().spaceTop(VSpace(VSpace::NONE));
742         params().spaceBottom(VSpace(VSpace::NONE));
743         params().spacing(Spacing(Spacing::Default));
744 }
745
746
747 int Paragraph::beginningOfBody() const
748 {
749         if (layout()->labeltype != LABEL_MANUAL)
750                 return 0;
751
752         // Unroll the first two cycles of the loop
753         // and remember the previous character to
754         // remove unnecessary GetChar() calls
755         pos_type i = 0;
756         if (i < size() && !isNewline(i)) {
757                 ++i;
758                 char previous_char = 0;
759                 char temp = 0;
760                 if (i < size()) {
761                         previous_char = getChar(i);
762                         if (!isNewline(i)) {
763                                 ++i;
764                                 while (i < size() && previous_char != ' ') {
765                                         temp = getChar(i);
766                                         if (isNewline(i))
767                                                 break;
768
769                                         ++i;
770                                         previous_char = temp;
771                                 }
772                         }
773                 }
774         }
775
776         return i;
777 }
778
779
780 // returns -1 if inset not found
781 int Paragraph::getPositionOfInset(Inset const * inset) const
782 {
783         // Find the entry.
784         InsetList::iterator it = insetlist.begin();
785         InsetList::iterator end = insetlist.end();
786         for (; it != end; ++it)
787                 if (it.getInset() == inset)
788                         return it.getPos();
789         return -1;
790 }
791
792
793 InsetBibitem * Paragraph::bibitem()
794 {
795         InsetList::iterator it = insetlist.begin();
796         if (it != insetlist.end() && it.getInset()->lyxCode() == Inset::BIBTEX_CODE)
797                 return static_cast<InsetBibitem *>(it.getInset());
798         return 0;
799 }
800
801
802
803 // This could go to ParagraphParameters if we want to
804 int Paragraph::startTeXParParams(BufferParams const & bparams,
805                                  ostream & os, bool moving_arg) const
806 {
807         int column = 0;
808
809         if (params().noindent()) {
810                 os << "\\noindent ";
811                 column += 10;
812         }
813
814         switch (params().align()) {
815         case LYX_ALIGN_NONE:
816         case LYX_ALIGN_BLOCK:
817         case LYX_ALIGN_LAYOUT:
818         case LYX_ALIGN_SPECIAL:
819                 break;
820         case LYX_ALIGN_LEFT:
821         case LYX_ALIGN_RIGHT:
822         case LYX_ALIGN_CENTER:
823                 if (moving_arg) {
824                         os << "\\protect";
825                         column = 8;
826                 }
827                 break;
828         }
829
830         switch (params().align()) {
831         case LYX_ALIGN_NONE:
832         case LYX_ALIGN_BLOCK:
833         case LYX_ALIGN_LAYOUT:
834         case LYX_ALIGN_SPECIAL:
835                 break;
836         case LYX_ALIGN_LEFT:
837                 if (getParLanguage(bparams)->babel() != "hebrew") {
838                         os << "\\begin{flushleft}";
839                         column += 17;
840                 } else {
841                         os << "\\begin{flushright}";
842                         column += 18;
843                 }
844                 break;
845         case LYX_ALIGN_RIGHT:
846                 if (getParLanguage(bparams)->babel() != "hebrew") {
847                         os << "\\begin{flushright}";
848                         column += 18;
849                 } else {
850                         os << "\\begin{flushleft}";
851                         column += 17;
852                 }
853                 break;
854         case LYX_ALIGN_CENTER:
855                 os << "\\begin{center}";
856                 column += 14;
857                 break;
858         }
859
860         return column;
861 }
862
863
864 // This could go to ParagraphParameters if we want to
865 int Paragraph::endTeXParParams(BufferParams const & bparams,
866                                ostream & os, bool moving_arg) const
867 {
868         int column = 0;
869
870         switch (params().align()) {
871         case LYX_ALIGN_NONE:
872         case LYX_ALIGN_BLOCK:
873         case LYX_ALIGN_LAYOUT:
874         case LYX_ALIGN_SPECIAL:
875                 break;
876         case LYX_ALIGN_LEFT:
877         case LYX_ALIGN_RIGHT:
878         case LYX_ALIGN_CENTER:
879                 if (moving_arg) {
880                         os << "\\protect";
881                         column = 8;
882                 }
883                 break;
884         }
885
886         switch (params().align()) {
887         case LYX_ALIGN_NONE:
888         case LYX_ALIGN_BLOCK:
889         case LYX_ALIGN_LAYOUT:
890         case LYX_ALIGN_SPECIAL:
891                 break;
892         case LYX_ALIGN_LEFT:
893                 if (getParLanguage(bparams)->babel() != "hebrew") {
894                         os << "\\end{flushleft}";
895                         column = 15;
896                 } else {
897                         os << "\\end{flushright}";
898                         column = 16;
899                 }
900                 break;
901         case LYX_ALIGN_RIGHT:
902                 if (getParLanguage(bparams)->babel() != "hebrew") {
903                         os << "\\end{flushright}";
904                         column+= 16;
905                 } else {
906                         os << "\\end{flushleft}";
907                         column = 15;
908                 }
909                 break;
910         case LYX_ALIGN_CENTER:
911                 os << "\\end{center}";
912                 column = 12;
913                 break;
914         }
915         return column;
916 }
917
918
919 // This one spits out the text of the paragraph
920 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
921                                 BufferParams const & bparams,
922                                 ostream & os, TexRow & texrow,
923                                 bool moving_arg)
924 {
925         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...     " << this << endl;
926
927         bool return_value = false;
928
929         LyXLayout_ptr style;
930
931         // well we have to check if we are in an inset with unlimited
932         // lenght (all in one row) if that is true then we don't allow
933         // any special options in the paragraph and also we don't allow
934         // any environment other then "Standard" to be valid!
935         bool asdefault =
936                 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
937
938         if (asdefault) {
939                 style = bparams.getLyXTextClass().defaultLayout();
940         } else {
941                 style = layout();
942         }
943
944         LyXFont basefont;
945
946         // Maybe we have to create a optional argument.
947         pos_type body_pos;
948
949         // FIXME: can we actually skip this check and just call
950         // beginningOfBody() ??
951         if (style->labeltype != LABEL_MANUAL) {
952                 body_pos = 0;
953         } else {
954                 body_pos = beginningOfBody();
955         }
956
957         unsigned int column = 0;
958
959         if (body_pos > 0) {
960                 os << '[';
961                 ++column;
962                 basefont = getLabelFont(bparams);
963         } else {
964                 basefont = getLayoutFont(bparams);
965         }
966
967         moving_arg |= style->needprotect;
968
969         // Which font is currently active?
970         LyXFont running_font(basefont);
971         // Do we have an open font change?
972         bool open_font = false;
973
974         Change::Type running_change = Change::UNCHANGED;
975
976         texrow.start(this, 0);
977
978         // if the paragraph is empty, the loop will not be entered at all
979         if (empty()) {
980                 if (style->isCommand()) {
981                         os << '{';
982                         ++column;
983                 }
984                 if (!asdefault)
985                         column += startTeXParParams(bparams, os, moving_arg);
986
987         }
988
989         for (pos_type i = 0; i < size(); ++i) {
990                 ++column;
991                 // First char in paragraph or after label?
992                 if (i == body_pos) {
993                         if (body_pos > 0) {
994                                 if (open_font) {
995                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
996                                         open_font = false;
997                                 }
998                                 basefont = getLayoutFont(bparams);
999                                 running_font = basefont;
1000                                 os << ']';
1001                                 ++column;
1002                         }
1003                         if (style->isCommand()) {
1004                                 os << '{';
1005                                 ++column;
1006                         }
1007
1008                         if (!asdefault)
1009                                 column += startTeXParParams(bparams, os,
1010                                                             moving_arg);
1011                 }
1012
1013                 value_type c = getChar(i);
1014
1015                 // Fully instantiated font
1016                 LyXFont font = getFont(bparams, i);
1017
1018                 LyXFont const last_font = running_font;
1019
1020                 // Spaces at end of font change are simulated to be
1021                 // outside font change, i.e. we write "\textXX{text} "
1022                 // rather than "\textXX{text }". (Asger)
1023                 if (open_font && c == ' ' && i <= size() - 2) {
1024                         LyXFont const & next_font = getFont(bparams, i + 1);
1025                         if (next_font != running_font
1026                             && next_font != font) {
1027                                 font = next_font;
1028                         }
1029                 }
1030
1031                 // We end font definition before blanks
1032                 if (open_font &&
1033                     (font != running_font ||
1034                      font.language() != running_font.language()))
1035                 {
1036                         column += running_font.latexWriteEndChanges(os,
1037                                                                     basefont,
1038                                                                     (i == body_pos-1) ? basefont : font);
1039                         running_font = basefont;
1040                         open_font = false;
1041                 }
1042
1043                 // Blanks are printed before start of fontswitch
1044                 if (c == ' ') {
1045                         // Do not print the separation of the optional argument
1046                         if (i != body_pos - 1) {
1047                                 pimpl_->simpleTeXBlanks(os, texrow, i,
1048                                                        column, font, *style);
1049                         }
1050                 }
1051
1052                 // Do we need to change font?
1053                 if ((font != running_font ||
1054                      font.language() != running_font.language()) &&
1055                         i != body_pos - 1)
1056                 {
1057                         column += font.latexWriteStartChanges(os, basefont,
1058                                                               last_font);
1059                         running_font = font;
1060                         open_font = true;
1061                 }
1062
1063                 Change::Type change = pimpl_->lookupChange(i);
1064
1065                 column += Changes::latexMarkChange(os, running_change, change);
1066                 running_change = change;
1067
1068                 pimpl_->simpleTeXSpecialChars(buf, bparams,
1069                                               os, texrow, moving_arg,
1070                                               font, running_font,
1071                                               basefont, open_font,
1072                                               running_change,
1073                                               *style, i, column, c);
1074         }
1075
1076         column += Changes::latexMarkChange(os,
1077                         running_change, Change::UNCHANGED);
1078
1079         // If we have an open font definition, we have to close it
1080         if (open_font) {
1081 #ifdef FIXED_LANGUAGE_END_DETECTION
1082                 if (next_) {
1083                         running_font
1084                                 .latexWriteEndChanges(os, basefont,
1085                                                       next_->getFont(bparams,
1086                                                       0));
1087                 } else {
1088                         running_font.latexWriteEndChanges(os, basefont,
1089                                                           basefont);
1090                 }
1091 #else
1092 #ifdef WITH_WARNINGS
1093 //#warning For now we ALWAYS have to close the foreign font settings if they are
1094 //#warning there as we start another \selectlanguage with the next paragraph if
1095 //#warning we are in need of this. This should be fixed sometime (Jug)
1096 #endif
1097                 running_font.latexWriteEndChanges(os, basefont,  basefont);
1098 #endif
1099         }
1100
1101         // Needed if there is an optional argument but no contents.
1102         if (body_pos > 0 && body_pos == size()) {
1103                 os << "]~";
1104                 return_value = false;
1105         }
1106
1107         if (!asdefault) {
1108                 column += endTeXParParams(bparams, os, moving_arg);
1109         }
1110
1111         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1112         return return_value;
1113 }
1114
1115
1116
1117
1118 bool Paragraph::isHfill(pos_type pos) const
1119 {
1120         return IsInsetChar(getChar(pos))
1121                && getInset(pos)->lyxCode() == Inset::HFILL_CODE;
1122 }
1123
1124
1125 bool Paragraph::isInset(pos_type pos) const
1126 {
1127         return IsInsetChar(getChar(pos));
1128 }
1129
1130
1131 bool Paragraph::isNewline(pos_type pos) const
1132 {
1133         return IsInsetChar(getChar(pos))
1134                && getInset(pos)->lyxCode() == Inset::NEWLINE_CODE;
1135 }
1136
1137
1138 bool Paragraph::isSeparator(pos_type pos) const
1139 {
1140         return IsSeparatorChar(getChar(pos));
1141 }
1142
1143
1144 bool Paragraph::isLineSeparator(pos_type pos) const
1145 {
1146         value_type const c = getChar(pos);
1147         return IsLineSeparatorChar(c)
1148                 || (IsInsetChar(c) && getInset(pos) &&
1149                 getInset(pos)->isLineSeparator());
1150 }
1151
1152
1153 bool Paragraph::isKomma(pos_type pos) const
1154 {
1155         return IsKommaChar(getChar(pos));
1156 }
1157
1158
1159 /// Used by the spellchecker
1160 bool Paragraph::isLetter(pos_type pos) const
1161 {
1162         value_type const c = getChar(pos);
1163         if (IsLetterChar(c))
1164                 return true;
1165         if (isInset(pos))
1166                 return getInset(pos)->isLetter();
1167         // We want to pass the ' and escape chars to ispell
1168         string const extra = lyxrc.isp_esc_chars + '\'';
1169         return contains(extra, c);
1170 }
1171
1172
1173 bool Paragraph::isWord(pos_type pos) const
1174 {
1175         return IsWordChar(getChar(pos)) ;
1176 }
1177
1178
1179 Language const *
1180 Paragraph::getParLanguage(BufferParams const & bparams) const
1181 {
1182         if (!empty()) {
1183                 return getFirstFontSettings().language();
1184         } else if (previous_)
1185                 return previous_->getParLanguage(bparams);
1186         else
1187                 return bparams.language;
1188 }
1189
1190
1191 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
1192 {
1193         return lyxrc.rtl_support
1194                 && getParLanguage(bparams)->RightToLeft()
1195                 && !(inInset() && inInset()->owner() &&
1196                      inInset()->owner()->lyxCode() == Inset::ERT_CODE);
1197 }
1198
1199
1200 void Paragraph::changeLanguage(BufferParams const & bparams,
1201                                   Language const * from, Language const * to)
1202 {
1203         for (pos_type i = 0; i < size(); ++i) {
1204                 LyXFont font = getFontSettings(bparams, i);
1205                 if (font.language() == from) {
1206                         font.setLanguage(to);
1207                         setFont(i, font);
1208                 }
1209         }
1210 }
1211
1212
1213 bool Paragraph::isMultiLingual(BufferParams const & bparams)
1214 {
1215         Language const * doc_language = bparams.language;
1216         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
1217         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
1218
1219         for (; cit != end; ++cit)
1220                 if (cit->font().language() != ignore_language &&
1221                     cit->font().language() != latex_language &&
1222                         cit->font().language() != doc_language)
1223                         return true;
1224         return false;
1225 }
1226
1227
1228 // Convert the paragraph to a string.
1229 // Used for building the table of contents
1230 string const Paragraph::asString(Buffer const * buffer, bool label) const
1231 {
1232         string s;
1233         if (label && !params().labelString().empty())
1234                 s += params().labelString() + ' ';
1235
1236         for (pos_type i = 0; i < size(); ++i) {
1237                 value_type c = getChar(i);
1238                 if (IsPrintable(c))
1239                         s += c;
1240                 else if (c == META_INSET &&
1241                          getInset(i)->lyxCode() == Inset::MATH_CODE) {
1242                         ostringstream ost;
1243                         getInset(i)->ascii(buffer, ost);
1244                         s += subst(STRCONV(ost.str()),'\n',' ');
1245                 }
1246         }
1247
1248         return s;
1249 }
1250
1251
1252 string const Paragraph::asString(Buffer const * buffer,
1253                                  pos_type beg, pos_type end, bool label) const
1254 {
1255         ostringstream os;
1256
1257         if (beg == 0 && label && !params().labelString().empty())
1258                 os << params().labelString() << ' ';
1259
1260         for (pos_type i = beg; i < end; ++i) {
1261                 value_type const c = getUChar(buffer->params, i);
1262                 if (IsPrintable(c))
1263                         os << c;
1264                 else if (c == META_INSET)
1265                         getInset(i)->ascii(buffer, os);
1266         }
1267
1268         return STRCONV(os.str());
1269 }
1270
1271
1272 void Paragraph::setInsetOwner(Inset * i)
1273 {
1274         pimpl_->inset_owner = i;
1275         InsetList::iterator it = insetlist.begin();
1276         InsetList::iterator end = insetlist.end();
1277         for (; it != end; ++it)
1278                 if (it.getInset())
1279                         it.getInset()->setOwner(i);
1280 }
1281
1282
1283 void Paragraph::deleteInsetsLyXText(BufferView * bv)
1284 {
1285         // then the insets
1286         insetlist.deleteInsetsLyXText(bv);
1287 }
1288
1289
1290 void Paragraph::resizeInsetsLyXText(BufferView * bv)
1291 {
1292         // then the insets
1293         insetlist.resizeInsetsLyXText(bv);
1294 }
1295
1296
1297 void Paragraph::setContentsFromPar(Paragraph * par)
1298 {
1299         pimpl_->setContentsFromPar(par);
1300 }
1301
1302
1303 void Paragraph::trackChanges(Change::Type type)
1304 {
1305         pimpl_->trackChanges(type);
1306 }
1307
1308
1309 void Paragraph::untrackChanges()
1310 {
1311         pimpl_->untrackChanges();
1312 }
1313
1314
1315 void Paragraph::cleanChanges()
1316 {
1317         pimpl_->cleanChanges();
1318 }
1319
1320
1321 Change::Type Paragraph::lookupChange(lyx::pos_type pos) const
1322 {
1323         lyx::Assert(!size() || pos < size());
1324         return pimpl_->lookupChange(pos);
1325 }
1326
1327
1328 Change const Paragraph::lookupChangeFull(lyx::pos_type pos) const
1329 {
1330         lyx::Assert(!size() || pos < size());
1331         return pimpl_->lookupChangeFull(pos);
1332 }
1333
1334
1335 bool Paragraph::isChanged(pos_type start, pos_type end) const
1336 {
1337         return pimpl_->isChanged(start, end);
1338 }
1339
1340
1341 bool Paragraph::isChangeEdited(pos_type start, pos_type end) const
1342 {
1343         return pimpl_->isChangeEdited(start, end);
1344 }
1345
1346
1347 void Paragraph::setChange(lyx::pos_type pos, Change::Type type)
1348 {
1349         pimpl_->setChange(pos, type);
1350
1351 }
1352
1353
1354 void Paragraph::markErased()
1355 {
1356         pimpl_->markErased();
1357 }
1358
1359
1360 void Paragraph::acceptChange(pos_type start, pos_type end)
1361 {
1362         return pimpl_->acceptChange(start, end);
1363 }
1364
1365
1366 void Paragraph::rejectChange(pos_type start, pos_type end)
1367 {
1368         return pimpl_->rejectChange(start, end);
1369 }
1370
1371
1372 lyx::pos_type Paragraph::size() const
1373 {
1374         return pimpl_->size();
1375 }
1376
1377
1378 bool Paragraph::empty() const
1379 {
1380         return pimpl_->empty();
1381 }
1382
1383
1384 Paragraph::value_type Paragraph::getChar(pos_type pos) const
1385 {
1386         return pimpl_->getChar(pos);
1387 }
1388
1389
1390 int Paragraph::id() const
1391 {
1392         return pimpl_->id_;
1393 }
1394
1395
1396 LyXLayout_ptr const & Paragraph::layout() const
1397 {
1398         return layout_;
1399 }
1400
1401
1402 void Paragraph::layout(LyXLayout_ptr const & new_layout)
1403 {
1404         layout_ = new_layout;
1405 }
1406
1407
1408 Inset * Paragraph::inInset() const
1409 {
1410         return pimpl_->inset_owner;
1411 }
1412
1413
1414 void Paragraph::clearContents()
1415 {
1416         pimpl_->clear();
1417 }
1418
1419 void Paragraph::setChar(pos_type pos, value_type c)
1420 {
1421         pimpl_->setChar(pos, c);
1422 }
1423
1424
1425 ParagraphParameters & Paragraph::params()
1426 {
1427         return pimpl_->params;
1428 }
1429
1430
1431 ParagraphParameters const & Paragraph::params() const
1432 {
1433         return pimpl_->params;
1434 }
1435
1436
1437 bool Paragraph::isFreeSpacing() const
1438 {
1439         // for now we just need this, later should we need this in some
1440         // other way we can always add a function to Inset::() too.
1441         if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
1442                 return (pimpl_->inset_owner->owner()->lyxCode() == Inset::ERT_CODE);
1443         return false;
1444 }