]> git.lyx.org Git - lyx.git/blob - src/paragraph.C
Back out potentially buggy patch.
[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 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "paragraph.h"
18 #include "paragraph_pimpl.h"
19 #include "lyxrc.h"
20 #include "layout.h"
21 #include "language.h"
22 #include "tex-strings.h"
23 #include "buffer.h"
24 #include "bufferparams.h"
25 #include "debug.h"
26 #include "LaTeXFeatures.h"
27 #include "texrow.h"
28 #include "BufferView.h"
29 #include "encoding.h"
30 #include "ParameterStruct.h"
31 #include "gettext.h"
32 #include "lyxtextclasslist.h"
33
34 #include "insets/insetinclude.h"
35 #include "insets/insetbib.h"
36 #include "insets/insettext.h"
37
38 #include "support/filetools.h"
39 #include "support/lstrings.h"
40 #include "support/lyxmanip.h"
41 #include "support/FileInfo.h"
42 #include "support/LAssert.h"
43 #include "support/textutils.h"
44
45 #include <algorithm>
46 #include <fstream>
47 #include <csignal>
48
49 using std::ostream;
50 using std::endl;
51 using std::fstream;
52 using std::ios;
53 using std::lower_bound;
54 using std::upper_bound;
55 using std::reverse;
56
57 using lyx::pos_type;
58
59 int tex_code_break_column = 72;  // needs non-zero initialization. set later.
60 // this is a bad idea, but how can Paragraph find its buffer to get
61 // parameters? (JMarc)
62
63 extern string bibitemWidest(Buffer const *);
64
65 // this is a minibuffer
66
67 namespace {
68
69 char minibuffer_char;
70 LyXFont minibuffer_font;
71 Inset * minibuffer_inset;
72
73 } // namespace anon
74
75
76 extern BufferView * current_view;
77
78
79 Paragraph::Paragraph()
80         : pimpl_(new Paragraph::Pimpl(this))
81 {
82         for (int i = 0; i < 10; ++i)
83                 setCounter(i, 0);
84         next_ = 0;
85         previous_ = 0;
86         enumdepth = 0;
87         itemdepth = 0;
88         bibkey = 0; // ale970302
89         params().clear();
90 }
91
92
93 // This constructor inserts the new paragraph in a list.
94 Paragraph::Paragraph(Paragraph * par)
95         : pimpl_(new Paragraph::Pimpl(this))
96 {
97         for (int i = 0; i < 10; ++i)
98                 setCounter(i, 0);
99         enumdepth = 0;
100         itemdepth = 0;
101
102         // double linked list begin
103         next_ = par->next_;
104         if (next_)
105                 next_->previous_ = this;
106         previous_ = par;
107         previous_->next_ = this;
108         // end
109
110         bibkey = 0; // ale970302
111         params().clear();
112 }
113
114
115 Paragraph::Paragraph(Paragraph const & lp, bool same_ids)
116         : pimpl_(new Paragraph::Pimpl(*lp.pimpl_, this, same_ids))
117 {
118         for (int i = 0; i < 10; ++i)
119                 setCounter(i, 0);
120         enumdepth = 0;
121         itemdepth = 0;
122         next_     = 0;
123         previous_ = 0;
124
125         // this is because of the dummy layout of the paragraphs that
126         // follow footnotes
127         layout_ = lp.layout();
128
129         // ale970302
130         if (lp.bibkey) {
131                 bibkey = static_cast<InsetBibKey *>
132                         (lp.bibkey->clone(*current_view->buffer()));
133         } else {
134                 bibkey = 0;
135         }
136
137         // copy everything behind the break-position to the new paragraph
138         insetlist = lp.insetlist;
139         for (InsetList::iterator it = insetlist.begin();
140              it != insetlist.end(); ++it)
141         {
142                 it->inset = it->inset->clone(*current_view->buffer(), same_ids);
143                 // tell the new inset who is the boss now
144                 it->inset->parOwner(this);
145         }
146 }
147
148
149 // the destructor removes the new paragraph from the list
150 Paragraph::~Paragraph()
151 {
152         if (previous_)
153                 previous_->next_ = next_;
154         if (next_)
155                 next_->previous_ = previous_;
156
157         for (InsetList::iterator it = insetlist.begin();
158              it != insetlist.end(); ++it) {
159                 delete it->inset;
160         }
161
162         // ale970302
163         delete bibkey;
164
165         delete pimpl_;
166         //
167         //lyxerr << "Paragraph::paragraph_id = "
168         //       << Paragraph::paragraph_id << endl;
169 }
170
171
172 void Paragraph::writeFile(Buffer const * buf, ostream & os,
173                           BufferParams const & bparams,
174                           depth_type dth) const
175 {
176         // The beginning or end of a deeper (i.e. nested) area?
177         if (dth != params().depth()) {
178                 if (params().depth() > dth) {
179                         while (params().depth() > dth) {
180                                 os << "\n\\begin_deeper ";
181                                 ++dth;
182                         }
183                 } else {
184                         while (params().depth() < dth) {
185                                 os << "\n\\end_deeper ";
186                                 --dth;
187                         }
188                 }
189         }
190
191         // First write the layout
192         string lay = layout();
193         if (lay.empty()) {
194                 lay = textclasslist[bparams.textclass].defaultLayoutName();
195         }
196
197         os << "\n\\layout " << layout() << "\n";
198
199         // Maybe some vertical spaces.
200         if (params().spaceTop().kind() != VSpace::NONE)
201                 os << "\\added_space_top "
202                    << params().spaceTop().asLyXCommand() << " ";
203         if (params().spaceBottom().kind() != VSpace::NONE)
204                 os << "\\added_space_bottom "
205                    << params().spaceBottom().asLyXCommand() << " ";
206
207         // Maybe the paragraph has special spacing
208         params().spacing().writeFile(os, true);
209
210         // The labelwidth string used in lists.
211         if (!params().labelWidthString().empty())
212                 os << "\\labelwidthstring "
213                    << params().labelWidthString() << '\n';
214
215         // Lines above or below?
216         if (params().lineTop())
217                 os << "\\line_top ";
218         if (params().lineBottom())
219                 os << "\\line_bottom ";
220
221         // Pagebreaks above or below?
222         if (params().pagebreakTop())
223                 os << "\\pagebreak_top ";
224         if (params().pagebreakBottom())
225                 os << "\\pagebreak_bottom ";
226
227         // Start of appendix?
228         if (params().startOfAppendix())
229                 os << "\\start_of_appendix ";
230
231         // Noindent?
232         if (params().noindent())
233                 os << "\\noindent ";
234         
235         // Do we have a manual left indent?
236         if (!params().leftIndent().zero())
237                 os << "\\leftindent " << params().leftIndent().asString() << " ";
238         
239         // Alignment?
240         if (params().align() != LYX_ALIGN_LAYOUT) {
241                 int h = 0;
242                 switch (params().align()) {
243                 case LYX_ALIGN_LEFT: h = 1; break;
244                 case LYX_ALIGN_RIGHT: h = 2; break;
245                 case LYX_ALIGN_CENTER: h = 3; break;
246                 default: h = 0; break;
247                 }
248                 os << "\\align " << string_align[h] << " ";
249         }
250
251         // bibitem  ale970302
252         if (bibkey)
253                 bibkey->write(buf, os);
254
255         LyXFont font1(LyXFont::ALL_INHERIT, bparams.language);
256
257         int column = 0;
258         for (pos_type i = 0; i < size(); ++i) {
259                 if (!i) {
260                         os << "\n";
261                         column = 0;
262                 }
263
264                 // Write font changes
265                 LyXFont font2 = getFontSettings(bparams, i);
266                 if (font2 != font1) {
267 #ifndef INHERIT_LANGUAGE
268                         font2.lyxWriteChanges(font1, os);
269 #else
270                         font2.lyxWriteChanges(font1, bparams.language, os);
271 #endif
272                         column = 0;
273                         font1 = font2;
274                 }
275
276                 value_type const c = getChar(i);
277                 switch (c) {
278                 case META_INSET:
279                 {
280                         Inset const * inset = getInset(i);
281                         if (inset)
282                                 if (inset->directWrite()) {
283                                         // international char, let it write
284                                         // code directly so it's shorter in
285                                         // the file
286                                         inset->write(buf, os);
287                                 } else {
288                                         os << "\n\\begin_inset ";
289                                         inset->write(buf, os);
290                                         os << "\n\\end_inset \n\n";
291                                         column = 0;
292                                 }
293                 }
294                 break;
295                 case META_NEWLINE:
296                         os << "\n\\newline \n";
297                         column = 0;
298                         break;
299                 case META_HFILL:
300                         os << "\n\\hfill \n";
301                         column = 0;
302                         break;
303                 case '\\':
304                         os << "\n\\backslash \n";
305                         column = 0;
306                         break;
307                 case '.':
308                         if (i + 1 < size() && getChar(i + 1) == ' ') {
309                                 os << ".\n";
310                                 column = 0;
311                         } else
312                                 os << ".";
313                         break;
314                 default:
315                         if ((column > 70 && c == ' ')
316                             || column > 79) {
317                                 os << "\n";
318                                 column = 0;
319                         }
320                         // this check is to amend a bug. LyX sometimes
321                         // inserts '\0' this could cause problems.
322                         if (c != '\0')
323                                 os << c;
324                         else
325                                 lyxerr << "ERROR (Paragraph::writeFile):"
326                                         " NULL char in structure." << endl;
327                         ++column;
328                         break;
329                 }
330         }
331
332         // now write the next paragraph
333         if (next_)
334                 next_->writeFile(buf, os, bparams, dth);
335 }
336
337
338 void Paragraph::validate(LaTeXFeatures & features) const
339 {
340         BufferParams const & bparams = features.bufferParams();
341
342         // check the params.
343         if (params().lineTop() || params().lineBottom())
344                 features.require("lyxline");
345         if (!params().spacing().isDefault())
346                 features.require("setspace");
347
348         // then the layouts
349         features.useLayout(layout());
350
351         // then the fonts
352         Language const * doc_language = bparams.language;
353
354         Pimpl::FontList::const_iterator fcit = pimpl_->fontlist.begin();
355         Pimpl::FontList::const_iterator fend = pimpl_->fontlist.end();
356         for (; fcit != fend; ++fcit) {
357                 if (fcit->font().noun() == LyXFont::ON) {
358                         lyxerr[Debug::LATEX] << "font.noun: "
359                                              << fcit->font().noun()
360                                              << endl;
361                         features.require("noun");
362                         lyxerr[Debug::LATEX] << "Noun enabled. Font: "
363                                              << fcit->font().stateText(0)
364                                              << endl;
365                 }
366                 switch (fcit->font().color()) {
367                 case LColor::none:
368                 case LColor::inherit:
369                 case LColor::ignore:
370                         // probably we should put here all interface colors used for
371                         // font displaying! For now I just add this ones I know of (Jug)
372                 case LColor::latex:
373                 case LColor::note:
374                         break;
375                 default:
376                         features.require("color");
377                         lyxerr[Debug::LATEX] << "Color enabled. Font: "
378                                              << fcit->font().stateText(0)
379                                              << endl;
380                 }
381
382                 Language const * language = fcit->font().language();
383                 if (language->babel() != doc_language->babel() &&
384                     language != ignore_language &&
385 #ifdef INHERIT_LANGUAGE
386                     language != inherit_language &&
387 #endif
388                     language != latex_language)
389                 {
390                         features.useLanguage(language);
391                         lyxerr[Debug::LATEX] << "Found language "
392                                              << language->babel() << endl;
393                 }
394         }
395
396         if (!params().leftIndent().zero())
397                 features.require("ParagraphLeftIndent");
398
399         // then the insets
400         LyXLayout const & lout = textclasslist[bparams.textclass][layout()];
401
402         InsetList::const_iterator icit = insetlist.begin();
403         InsetList::const_iterator iend = insetlist.end();
404         for (; icit != iend; ++icit) {
405                 if (icit->inset) {
406                         icit->inset->validate(features);
407                         if (lout.needprotect &&
408                             icit->inset->lyxCode() == Inset::FOOT_CODE)
409                                 features.require("NeedLyXFootnoteCode");
410                 }
411         }
412 }
413
414
415 // First few functions needed for cut and paste and paragraph breaking.
416 void Paragraph::copyIntoMinibuffer(Buffer const & buffer, pos_type pos) const
417 {
418         BufferParams bparams = buffer.params;
419
420         minibuffer_char = getChar(pos);
421         minibuffer_font = getFontSettings(bparams, pos);
422         minibuffer_inset = 0;
423         if (minibuffer_char == Paragraph::META_INSET) {
424                 if (getInset(pos)) {
425                         minibuffer_inset = getInset(pos)->clone(buffer);
426                 } else {
427                         minibuffer_inset = 0;
428                         minibuffer_char = ' ';
429                         // This reflects what GetInset() does (ARRae)
430                 }
431         }
432 }
433
434
435 void Paragraph::cutIntoMinibuffer(BufferParams const & bparams, pos_type pos)
436 {
437         minibuffer_char = getChar(pos);
438         minibuffer_font = getFontSettings(bparams, pos);
439         minibuffer_inset = 0;
440         if (minibuffer_char == Paragraph::META_INSET) {
441                 if (getInset(pos)) {
442                         minibuffer_inset = getInset(pos);
443                         // This is a little hack since I want exactly
444                         // the inset, not just a clone. Otherwise
445                         // the inset would be deleted when calling Erase(pos)
446                         // find the entry
447                         InsetList::iterator it = insetlist.begin();
448                         InsetList::iterator end = insetlist.end();
449                         for (; it != end; ++it) {
450                                 if (it->pos == pos)
451                                         break;
452                         }
453
454                         if (it != end && it->pos == pos)
455                                 it->inset = 0;
456                         // the inset is not in a paragraph anymore
457                         minibuffer_inset->parOwner(0);
458                 } else {
459                         minibuffer_inset = 0;
460                         minibuffer_char = ' ';
461                         // This reflects what GetInset() does (ARRae)
462                 }
463
464         }
465
466         // Erase(pos); now the caller is responsible for that.
467 }
468
469
470 bool Paragraph::insertFromMinibuffer(pos_type pos)
471 {
472         if (minibuffer_char == Paragraph::META_INSET) {
473                 if (!insetAllowed(minibuffer_inset->lyxCode())) {
474                         return false;
475                 }
476                 insertInset(pos, minibuffer_inset, minibuffer_font);
477         } else {
478                 LyXFont f = minibuffer_font;
479                 if (!checkInsertChar(f)) {
480                         return false;
481                 }
482                 insertChar(pos, minibuffer_char, f);
483         }
484         return true;
485 }
486
487 // end of minibuffer
488
489
490 void Paragraph::erase(pos_type pos)
491 {
492         pimpl_->erase(pos);
493 }
494
495
496 bool Paragraph::checkInsertChar(LyXFont & font)
497 {
498         if (pimpl_->inset_owner)
499                 return pimpl_->inset_owner->checkInsertChar(font);
500         return true;
501 }
502
503
504 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c)
505 {
506         LyXFont const f(LyXFont::ALL_INHERIT);
507         insertChar(pos, c, f);
508 }
509
510
511 void Paragraph::insertChar(pos_type pos, Paragraph::value_type c,
512                            LyXFont const & font)
513 {
514         pimpl_->insertChar(pos, c, font);
515 }
516
517
518 void Paragraph::insertInset(pos_type pos, Inset * inset)
519 {
520         LyXFont const f(LyXFont::ALL_INHERIT);
521         insertInset(pos, inset, f);
522 }
523
524
525 void Paragraph::insertInset(pos_type pos, Inset * inset, LyXFont const & font)
526 {
527         pimpl_->insertInset(pos, inset, font);
528 }
529
530
531 bool Paragraph::insetAllowed(Inset::Code code)
532 {
533         //lyxerr << "Paragraph::InsertInsetAllowed" << endl;
534
535         if (pimpl_->inset_owner)
536                 return pimpl_->inset_owner->insetAllowed(code);
537         return true;
538 }
539
540
541 Inset * Paragraph::getInset(pos_type pos)
542 {
543         lyx::Assert(pos < size());
544
545         // Find the inset.
546         InsetList::iterator it = insetlist.begin();
547         InsetList::iterator end = insetlist.end();
548         for (; it != end; ++it) {
549                 if (it->pos == pos)
550                         break;
551         }
552
553         if (it != end && it->pos == pos)
554                 return it->inset;
555
556         lyxerr << "ERROR (Paragraph::getInset): "
557                << "Inset does not exist: " << pos << endl;
558         //::raise(SIGSTOP);
559
560         // text[pos] = ' '; // WHY!!! does this set the pos to ' '????
561         // Did this commenting out introduce a bug? So far I have not
562         // see any, please enlighten me. (Lgb)
563         // My guess is that since the inset does not exist, we might
564         // as well replace it with a space to prevent craches. (Asger)
565         return 0;
566 }
567
568
569 Inset const * Paragraph::getInset(pos_type pos) const
570 {
571         lyx::Assert(pos < size());
572
573         // Find the inset.
574         InsetList::const_iterator cit = insetlist.begin();
575         InsetList::const_iterator end = insetlist.end();
576         for (; cit != end; ++cit) {
577                 if (cit->pos == pos)
578                         break;
579         }
580
581         if (cit != end && cit->pos == pos)
582                 return cit->inset;
583
584         lyxerr << "ERROR (Paragraph::getInset): "
585                << "Inset does not exist: " << pos << endl;
586         //::raise(SIGSTOP);
587         //text[pos] = ' '; // WHY!!! does this set the pos to ' '????
588         // Did this commenting out introduce a bug? So far I have not
589         // see any, please enlighten me. (Lgb)
590         // My guess is that since the inset does not exist, we might
591         // as well replace it with a space to prevent craches. (Asger)
592         return 0;
593 }
594
595
596 // Gets uninstantiated font setting at position.
597 LyXFont const Paragraph::getFontSettings(BufferParams const & bparams,
598                                          pos_type pos) const
599 {
600         lyx::Assert(pos <= size());
601
602         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
603         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
604         for (; cit != end; ++cit) {
605                 if (cit->pos() >= pos)
606                         break;
607         }
608
609         LyXFont retfont;
610         if (cit != end) {
611                 retfont = cit->font();
612         } else if (pos == size() && size()) {
613                 retfont = getFontSettings(bparams, pos - 1);
614         } else
615                 retfont = LyXFont(LyXFont::ALL_INHERIT, getParLanguage(bparams));
616 #ifdef INHERIT_LANGUAGE
617         if (retfont.language() == inherit_language)
618                 retfont.setLanguage(bparams.language);
619 #endif
620
621         return retfont;
622 }
623
624
625 // Gets uninstantiated font setting at position 0
626 LyXFont const Paragraph::getFirstFontSettings() const
627 {
628         if (size() > 0 && !pimpl_->fontlist.empty())
629                 return pimpl_->fontlist[0].font();
630
631         return LyXFont(LyXFont::ALL_INHERIT);
632 }
633
634
635 // Gets the fully instantiated font at a given position in a paragraph
636 // This is basically the same function as LyXText::GetFont() in text2.C.
637 // The difference is that this one is used for generating the LaTeX file,
638 // and thus cosmetic "improvements" are disallowed: This has to deliver
639 // the true picture of the buffer. (Asger)
640 // If position is -1, we get the layout font of the paragraph.
641 // If position is -2, we get the font of the manual label of the paragraph.
642 LyXFont const Paragraph::getFont(BufferParams const & bparams,
643                                  pos_type pos) const
644 {
645         lyx::Assert(pos >= 0);
646
647         LyXLayout const & lout =
648                 textclasslist[bparams.textclass][layout()];
649         pos_type main_body = 0;
650         if (lout.labeltype == LABEL_MANUAL)
651                 main_body = beginningOfMainBody();
652
653         LyXFont layoutfont;
654         if (pos < main_body)
655                 layoutfont = lout.labelfont;
656         else
657                 layoutfont = lout.font;
658
659         LyXFont tmpfont = getFontSettings(bparams, pos);
660 #ifndef INHERIT_LANGUAGE
661         tmpfont.realize(layoutfont);
662 #else
663         tmpfont.realize(layoutfont, bparams.language);
664 #endif
665
666         return pimpl_->realizeFont(tmpfont, bparams);
667 }
668
669
670 LyXFont const Paragraph::getLabelFont(BufferParams const & bparams) const
671 {
672         LyXLayout const & lout =
673                 textclasslist[bparams.textclass][layout()];
674
675         LyXFont tmpfont = lout.labelfont;
676         tmpfont.setLanguage(getParLanguage(bparams));
677
678         return pimpl_->realizeFont(tmpfont, bparams);
679 }
680
681
682 LyXFont const Paragraph::getLayoutFont(BufferParams const & bparams) const
683 {
684         LyXLayout const & lout =
685                 textclasslist[bparams.textclass][layout()];
686
687         LyXFont tmpfont = lout.font;
688         tmpfont.setLanguage(getParLanguage(bparams));
689
690         return pimpl_->realizeFont(tmpfont, bparams);
691 }
692
693
694 /// Returns the height of the highest font in range
695 LyXFont::FONT_SIZE
696 Paragraph::highestFontInRange(pos_type startpos, pos_type endpos,
697                               LyXFont::FONT_SIZE const def_size) const
698 {
699         if (pimpl_->fontlist.empty())
700                 return def_size;
701
702         Pimpl::FontList::const_iterator end_it = pimpl_->fontlist.begin();
703         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
704         for (; end_it != end; ++end_it) {
705                 if (end_it->pos() >= endpos)
706                         break;
707         }
708
709         if (end_it != end)
710                 ++end_it;
711
712         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
713         for (; cit != end; ++cit) {
714                 if (cit->pos() >= startpos)
715                         break;
716         }
717
718         LyXFont::FONT_SIZE maxsize = LyXFont::SIZE_TINY;
719         for (; cit != end_it; ++cit) {
720                 LyXFont::FONT_SIZE size = cit->font().size();
721                 if (size == LyXFont::INHERIT_SIZE)
722                         size = def_size;
723                 if (size > maxsize && size <= LyXFont::SIZE_HUGER)
724                         maxsize = size;
725         }
726         return maxsize;
727 }
728
729
730 Paragraph::value_type
731 Paragraph::getUChar(BufferParams const & bparams, pos_type pos) const
732 {
733         value_type c = getChar(pos);
734         if (!lyxrc.rtl_support)
735                 return c;
736
737         value_type uc = c;
738         switch (c) {
739         case '(':
740                 uc = ')';
741                 break;
742         case ')':
743                 uc = '(';
744                 break;
745         case '[':
746                 uc = ']';
747                 break;
748         case ']':
749                 uc = '[';
750                 break;
751         case '{':
752                 uc = '}';
753                 break;
754         case '}':
755                 uc = '{';
756                 break;
757         case '<':
758                 uc = '>';
759                 break;
760         case '>':
761                 uc = '<';
762                 break;
763         }
764         if (uc != c && getFontSettings(bparams, pos).isRightToLeft())
765                 return uc;
766         else
767                 return c;
768 }
769
770
771 void Paragraph::setFont(pos_type pos, LyXFont const & font)
772 {
773         lyx::Assert(pos <= size());
774
775         // First, reduce font against layout/label font
776         // Update: The SetCharFont() routine in text2.C already
777         // reduces font, so we don't need to do that here. (Asger)
778         // No need to simplify this because it will disappear
779         // in a new kernel. (Asger)
780         // Next search font table
781
782         Pimpl::FontList::iterator beg = pimpl_->fontlist.begin();
783         Pimpl::FontList::iterator it = beg;
784         Pimpl::FontList::iterator endit = pimpl_->fontlist.end();
785         for (; it != endit; ++it) {
786                 if (it->pos() >= pos)
787                         break;
788         }
789         unsigned int i = std::distance(beg, it);
790         bool notfound = (it == endit);
791
792         if (!notfound && pimpl_->fontlist[i].font() == font)
793                 return;
794
795         bool begin = pos == 0 || notfound ||
796                 (i > 0 && pimpl_->fontlist[i - 1].pos() == pos - 1);
797         // Is position pos is a beginning of a font block?
798         bool end = !notfound && pimpl_->fontlist[i].pos() == pos;
799         // Is position pos is the end of a font block?
800         if (begin && end) { // A single char block
801                 if (i + 1 < pimpl_->fontlist.size() &&
802                     pimpl_->fontlist[i + 1].font() == font) {
803                         // Merge the singleton block with the next block
804                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
805                         if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
806                                 pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i-1);
807                 } else if (i > 0 && pimpl_->fontlist[i - 1].font() == font) {
808                         // Merge the singleton block with the previous block
809                         pimpl_->fontlist[i - 1].pos(pos);
810                         pimpl_->fontlist.erase(pimpl_->fontlist.begin() + i);
811                 } else
812                         pimpl_->fontlist[i].font(font);
813         } else if (begin) {
814                 if (i > 0 && pimpl_->fontlist[i - 1].font() == font)
815                         pimpl_->fontlist[i - 1].pos(pos);
816                 else
817                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
818                                         Pimpl::FontTable(pos, font));
819         } else if (end) {
820                 pimpl_->fontlist[i].pos(pos - 1);
821                 if (!(i + 1 < pimpl_->fontlist.size() &&
822                       pimpl_->fontlist[i + 1].font() == font))
823                         pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
824                                         Pimpl::FontTable(pos, font));
825         } else { // The general case. The block is splitted into 3 blocks
826                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i,
827                                 Pimpl::FontTable(pos - 1, pimpl_->fontlist[i].font()));
828                 pimpl_->fontlist.insert(pimpl_->fontlist.begin() + i + 1,
829                                 Pimpl::FontTable(pos, font));
830         }
831 }
832
833
834
835 void Paragraph::next(Paragraph * p)
836 {
837         next_ = p;
838 }
839
840
841 // This function is able to hide closed footnotes.
842 Paragraph * Paragraph::next()
843 {
844         return next_;
845 }
846
847
848 Paragraph const * Paragraph::next() const
849 {
850         return next_;
851 }
852
853
854 void Paragraph::previous(Paragraph * p)
855 {
856         previous_ = p;
857 }
858
859
860 // This function is able to hide closed footnotes.
861 Paragraph * Paragraph::previous()
862 {
863         return previous_;
864 }
865
866
867 // This function is able to hide closed footnotes.
868 Paragraph const * Paragraph::previous() const
869 {
870         return previous_;
871 }
872
873
874 void Paragraph::breakParagraph(BufferParams const & bparams,
875                                   pos_type pos,
876                                   int flag)
877 {
878         // create a new paragraph
879         Paragraph * tmp = new Paragraph(this);
880         tmp->layout(textclasslist[bparams.textclass].defaultLayoutName());
881
882         // remember to set the inset_owner
883         tmp->setInsetOwner(inInset());
884
885         // this is an idea for a more userfriendly layout handling, I will
886         // see what the users say
887
888         // layout stays the same with latex-environments
889         if (flag) {
890                 tmp->layout(layout());
891                 tmp->setLabelWidthString(params().labelWidthString());
892         }
893
894         bool isempty = textclasslist[bparams.textclass][layout()].keepempty;
895         if (!isempty && (size() > pos || !size() || flag == 2)) {
896                 tmp->layout(layout());
897                 tmp->params().align(params().align());
898                 tmp->setLabelWidthString(params().labelWidthString());
899
900                 tmp->params().lineBottom(params().lineBottom());
901                 params().lineBottom(false);
902                 tmp->params().pagebreakBottom(params().pagebreakBottom());
903                 params().pagebreakBottom(false);
904                 tmp->params().spaceBottom(params().spaceBottom());
905                 params().spaceBottom(VSpace(VSpace::NONE));
906
907                 tmp->params().depth(params().depth());
908                 tmp->params().noindent(params().noindent());
909
910                 // copy everything behind the break-position
911                 // to the new paragraph
912                 pos_type pos_end = size() - 1;
913                 pos_type i = pos;
914                 pos_type j = pos;
915                 for (; i <= pos_end; ++i) {
916                         cutIntoMinibuffer(bparams, i);
917                         if (tmp->insertFromMinibuffer(j - pos))
918                                 ++j;
919                 }
920                 for (i = pos_end; i >= pos; --i) {
921                         erase(i);
922                 }
923         }
924
925         // just an idea of me
926         if (!isempty && !pos) {
927                 tmp->params().lineTop(params().lineTop());
928                 tmp->params().pagebreakTop(params().pagebreakTop());
929                 tmp->params().spaceTop(params().spaceTop());
930                 tmp->bibkey = bibkey;
931
932                 bibkey = 0;
933                 params().clear();
934                 layout(textclasslist[bparams.textclass].defaultLayoutName());
935
936                 // layout stays the same with latex-environments
937                 if (flag) {
938                         layout(tmp->layout());
939                         setLabelWidthString(tmp->params().labelWidthString());
940                         params().depth(tmp->params().depth());
941                 }
942         }
943 }
944
945
946 void Paragraph::makeSameLayout(Paragraph const * par)
947 {
948         layout(par->layout());
949         // move to pimpl?
950         params() = par->params();
951 }
952
953
954 int Paragraph::stripLeadingSpaces(lyx::textclass_type tclass)
955 {
956         if (textclasslist[tclass][layout()].free_spacing ||
957             isFreeSpacing()) {
958                 return 0;
959         }
960
961         int i = 0;
962         while (size() && (isNewline(0) || isLineSeparator(0))) {
963                 erase(0);
964                 ++i;
965         }
966
967         return i;
968 }
969
970
971 bool Paragraph::hasSameLayout(Paragraph const * par) const
972 {
973         return
974                 par->layout() == layout() &&
975                 params().sameLayout(par->params());
976 }
977
978
979 void Paragraph::breakParagraphConservative(BufferParams const & bparams,
980                                            pos_type pos)
981 {
982         // create a new paragraph
983         Paragraph * tmp = new Paragraph(this);
984         tmp->makeSameLayout(this);
985
986         // When can pos > Last()?
987         // I guess pos == Last() is possible.
988         if (size() > pos) {
989                 // copy everything behind the break-position to the new
990                 // paragraph
991                 pos_type pos_end = size() - 1;
992
993                 //pos_type i = pos;
994                 //pos_type j = pos;
995                 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
996                         cutIntoMinibuffer(bparams, i);
997                         if (tmp->insertFromMinibuffer(j - pos))
998                                 ++j;
999                 }
1000
1001                 for (pos_type k = pos_end; k >= pos; --k) {
1002                         erase(k);
1003                 }
1004         }
1005 }
1006
1007
1008 // Be carefull, this does not make any check at all.
1009 // This method has wrong name, it combined this par with the next par.
1010 // In that sense it is the reverse of break paragraph. (Lgb)
1011 void Paragraph::pasteParagraph(BufferParams const & bparams)
1012 {
1013         // copy the next paragraph to this one
1014         Paragraph * the_next = next();
1015
1016         // first the DTP-stuff
1017         params().lineBottom(the_next->params().lineBottom());
1018         params().spaceBottom(the_next->params().spaceBottom());
1019         params().pagebreakBottom(the_next->params().pagebreakBottom());
1020
1021         pos_type pos_end = the_next->pimpl_->size() - 1;
1022         pos_type pos_insert = size();
1023
1024         // ok, now copy the paragraph
1025         for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
1026                 the_next->cutIntoMinibuffer(bparams, i);
1027                 if (insertFromMinibuffer(pos_insert + j))
1028                         ++j;
1029         }
1030
1031         // delete the next paragraph
1032         Paragraph * ppar = the_next->previous_;
1033         Paragraph * npar = the_next->next_;
1034         delete the_next;
1035         ppar->next(npar);
1036 }
1037
1038
1039 int Paragraph::getEndLabel(BufferParams const & bparams) const
1040 {
1041         Paragraph const * par = this;
1042         depth_type par_depth = getDepth();
1043         while (par) {
1044                 string const & layout = par->layout();
1045                 int const endlabeltype =
1046                         textclasslist[bparams.textclass][layout].endlabeltype;
1047                 if (endlabeltype != END_LABEL_NO_LABEL) {
1048                         if (!next_)
1049                                 return endlabeltype;
1050
1051                         depth_type const next_depth = next_->getDepth();
1052                         if (par_depth > next_depth ||
1053                             (par_depth == next_depth
1054                              && layout != next_->layout()))
1055                                 return endlabeltype;
1056                         break;
1057                 }
1058                 if (par_depth == 0)
1059                         break;
1060                 par = par->outerHook();
1061                 if (par)
1062                         par_depth = par->getDepth();
1063         }
1064         return END_LABEL_NO_LABEL;
1065 }
1066
1067
1068 Paragraph::depth_type Paragraph::getDepth() const
1069 {
1070         return params().depth();
1071 }
1072
1073
1074 Paragraph::depth_type Paragraph::getMaxDepthAfter(Buffer const * buffer) const
1075 {
1076         bool const isenv = textclasslist[buffer->params.textclass][layout()].isEnvironment();
1077
1078         if (isenv)
1079                 return params().depth() + 1;
1080         else
1081                 return params().depth();
1082
1083 }
1084
1085 char Paragraph::getAlign() const
1086 {
1087         return params().align();
1088 }
1089
1090
1091 string const & Paragraph::getLabelstring() const
1092 {
1093         return params().labelString();
1094 }
1095
1096
1097 int Paragraph::getFirstCounter(int i) const
1098 {
1099         return pimpl_->counter_[i];
1100 }
1101
1102
1103 // the next two functions are for the manual labels
1104 string const Paragraph::getLabelWidthString() const
1105 {
1106         if (!params().labelWidthString().empty())
1107                 return params().labelWidthString();
1108         else
1109                 return _("Senseless with this layout!");
1110 }
1111
1112
1113 void Paragraph::setLabelWidthString(string const & s)
1114 {
1115         params().labelWidthString(s);
1116 }
1117
1118
1119 void Paragraph::applyLayout(string const & new_layout)
1120 {
1121         layout(new_layout);
1122         params().labelWidthString(string());
1123         params().align(LYX_ALIGN_LAYOUT);
1124         params().spaceTop(VSpace(VSpace::NONE));
1125         params().spaceBottom(VSpace(VSpace::NONE));
1126         params().spacing(Spacing(Spacing::Default));
1127 }
1128
1129
1130 // if the layout of a paragraph contains a manual label, the beginning of the
1131 // main body is the beginning of the second word. This is what the par-
1132 // function returns. If the layout does not contain a label, the main
1133 // body always starts with position 0. This differentiation is necessary,
1134 // because there cannot be a newline or a blank <= the beginning of the
1135 // main body in TeX.
1136
1137 int Paragraph::beginningOfMainBody() const
1138 {
1139         // Unroll the first two cycles of the loop
1140         // and remember the previous character to
1141         // remove unnecessary GetChar() calls
1142         pos_type i = 0;
1143         if (i < size()
1144             && getChar(i) != Paragraph::META_NEWLINE) {
1145                 ++i;
1146                 char previous_char = 0;
1147                 char temp = 0;
1148                 if (i < size()
1149                     && (previous_char = getChar(i)) != Paragraph::META_NEWLINE) {
1150                         // Yes, this  ^ is supposed to be "= " not "=="
1151                         ++i;
1152                         while (i < size()
1153                                && previous_char != ' '
1154                                && (temp = getChar(i)) != Paragraph::META_NEWLINE) {
1155                                 ++i;
1156                                 previous_char = temp;
1157                         }
1158                 }
1159         }
1160
1161         return i;
1162 }
1163
1164
1165 Paragraph * Paragraph::depthHook(depth_type depth)
1166 {
1167         Paragraph * newpar = this;
1168
1169         do {
1170                 newpar = newpar->previous();
1171         } while (newpar && newpar->getDepth() > depth);
1172
1173         if (!newpar) {
1174                 if (previous() || getDepth())
1175                         lyxerr << "ERROR (Paragraph::DepthHook): "
1176                                 "no hook." << endl;
1177                 newpar = this;
1178         }
1179
1180         return newpar;
1181 }
1182
1183
1184 Paragraph const * Paragraph::depthHook(depth_type depth) const
1185 {
1186         Paragraph const * newpar = this;
1187
1188         do {
1189                 newpar = newpar->previous();
1190         } while (newpar && newpar->getDepth() > depth);
1191
1192         if (!newpar) {
1193                 if (previous() || getDepth())
1194                         lyxerr << "ERROR (Paragraph::DepthHook): "
1195                                 "no hook." << endl;
1196                 newpar = this;
1197         }
1198
1199         return newpar;
1200 }
1201
1202 Paragraph * Paragraph::outerHook()
1203 {
1204         if (!getDepth())
1205                 return 0;
1206         return depthHook(depth_type(getDepth() - 1));
1207 }
1208
1209 Paragraph const * Paragraph::outerHook() const
1210 {
1211         if (!getDepth())
1212                 return 0;
1213         return depthHook(depth_type(getDepth() - 1));
1214 }
1215
1216
1217 Paragraph::inset_iterator
1218 Paragraph::InsetIterator(pos_type pos)
1219 {
1220         InsetList::iterator it = insetlist.begin();
1221         InsetList::iterator end = insetlist.end();
1222         for (; it != end; ++it) {
1223                 if (it->pos == pos)
1224                         break;
1225         }
1226
1227         return inset_iterator(it);
1228 }
1229
1230
1231 // returns -1 if inset not found
1232 int Paragraph::getPositionOfInset(Inset const * inset) const
1233 {
1234         // Find the entry.
1235         InsetList::const_iterator cit = insetlist.begin();
1236         InsetList::const_iterator end = insetlist.end();
1237         for (; cit != end; ++cit) {
1238                 if (cit->inset == inset) {
1239                         return cit->pos;
1240                 }
1241         }
1242         if (inset == bibkey)
1243                 return 0;
1244
1245         return -1;
1246 }
1247
1248
1249 Paragraph * Paragraph::TeXOnePar(Buffer const * buf,
1250                                  BufferParams const & bparams,
1251                                  ostream & os, TexRow & texrow,
1252                                  bool moving_arg)
1253 {
1254         lyxerr[Debug::LATEX] << "TeXOnePar...     " << this << endl;
1255         Inset const * in = inInset();
1256         bool further_blank_line = false;
1257         LyXLayout style;
1258
1259         // well we have to check if we are in an inset with unlimited
1260         // lenght (all in one row) if that is true then we don't allow
1261         // any special options in the paragraph and also we don't allow
1262         // any environment other then "Standard" to be valid!
1263         if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1264                 style = textclasslist[bparams.textclass][layout()];
1265
1266                 if (params().startOfAppendix()) {
1267                         os << "\\appendix\n";
1268                         texrow.newline();
1269                 }
1270
1271                 if (!params().spacing().isDefault()
1272                         && (!previous() || !previous()->hasSameLayout(this))) {
1273                         os << params().spacing().writeEnvirBegin() << "\n";
1274                         texrow.newline();
1275                 }
1276
1277                 if (tex_code_break_column && style.isCommand()) {
1278                         os << '\n';
1279                         texrow.newline();
1280                 }
1281
1282                 if (params().pagebreakTop()) {
1283                         os << "\\newpage";
1284                         further_blank_line = true;
1285                 }
1286                 if (params().spaceTop().kind() != VSpace::NONE) {
1287                         os << params().spaceTop().asLatexCommand(bparams);
1288                         further_blank_line = true;
1289                 }
1290
1291                 if (params().lineTop()) {
1292                         os << "\\lyxline{\\" << getFont(bparams, 0).latexSize() << '}'
1293                            << "\\vspace{-1\\parskip}";
1294                         further_blank_line = true;
1295                 }
1296
1297                 if (further_blank_line) {
1298                         os << '\n';
1299                         texrow.newline();
1300                 }
1301         } else {
1302                 style = textclasslist[bparams.textclass].defaultLayout();
1303         }
1304
1305         Language const * language = getParLanguage(bparams);
1306         Language const * doc_language = bparams.language;
1307         Language const * previous_language = previous()
1308                 ? previous()->getParLanguage(bparams) : doc_language;
1309
1310         if (language->babel() != previous_language->babel()
1311             // check if we already put language command in TeXEnvironment()
1312             && !(style.isEnvironment()
1313                  && (!previous() || previous()->layout() != layout() ||
1314                          previous()->params().depth() != params().depth())))
1315         {
1316                 if (!lyxrc.language_command_end.empty() &&
1317                     previous_language->babel() != doc_language->babel())
1318                 {
1319                         os << subst(lyxrc.language_command_end, "$$lang",
1320                                     previous_language->babel())
1321                            << endl;
1322                         texrow.newline();
1323                 }
1324
1325                 if (lyxrc.language_command_end.empty() ||
1326                     language->babel() != doc_language->babel())
1327                 {
1328                         os << subst(lyxrc.language_command_begin, "$$lang",
1329                                     language->babel())
1330                            << endl;
1331                         texrow.newline();
1332                 }
1333         }
1334
1335         if (bparams.inputenc == "auto" &&
1336             language->encoding() != previous_language->encoding()) {
1337                 os << "\\inputencoding{"
1338                    << language->encoding()->LatexName()
1339                    << "}" << endl;
1340                 texrow.newline();
1341         }
1342
1343         switch (style.latextype) {
1344         case LATEX_COMMAND:
1345                 os << '\\'
1346                    << style.latexname()
1347                    << style.latexparam();
1348                 break;
1349         case LATEX_ITEM_ENVIRONMENT:
1350                 if (bibkey) {
1351                         bibkey->latex(buf, os, false, false);
1352                 } else
1353                         os << "\\item ";
1354                 break;
1355         case LATEX_LIST_ENVIRONMENT:
1356                 os << "\\item ";
1357                 break;
1358         default:
1359                 break;
1360         }
1361
1362         bool need_par = simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
1363
1364         // Make sure that \\par is done with the font of the last
1365         // character if this has another size as the default.
1366         // This is necessary because LaTeX (and LyX on the screen)
1367         // calculates the space between the baselines according
1368         // to this font. (Matthias)
1369         //
1370         // Is this really needed ? (Dekel)
1371         // We do not need to use to change the font for the last paragraph
1372         // or for a command.
1373         LyXFont const font =
1374                 (size() == 0
1375                  ? getLayoutFont(bparams) : getFont(bparams, size() - 1));
1376
1377         bool is_command = style.isCommand();
1378
1379         if (style.resfont.size() != font.size() && next_ && !is_command) {
1380                 if (!need_par)
1381                         os << "{";
1382                 os << "\\" << font.latexSize() << " \\par}";
1383         } else if (need_par) {
1384                 os << "\\par}";
1385         } else if (is_command)
1386                 os << "}";
1387
1388         switch (style.latextype) {
1389         case LATEX_ITEM_ENVIRONMENT:
1390         case LATEX_LIST_ENVIRONMENT:
1391                 if (next_ && (params().depth() < next_->params().depth())) {
1392                         os << '\n';
1393                         texrow.newline();
1394                 }
1395                 break;
1396         case LATEX_ENVIRONMENT:
1397                 // if its the last paragraph of the current environment
1398                 // skip it otherwise fall through
1399                 if (next_
1400                     && (next_->layout() != layout()
1401                         || next_->params().depth() != params().depth()))
1402                         break;
1403                 // fall through possible
1404         default:
1405                 // we don't need it for the last paragraph!!!
1406                 if (next_) {
1407                         os << '\n';
1408                         texrow.newline();
1409                 }
1410         }
1411
1412         if ((in == 0) || !in->forceDefaultParagraphs(in)) {
1413                 further_blank_line = false;
1414                 if (params().lineBottom()) {
1415                         os << "\\lyxline{\\" << font.latexSize() << '}';
1416                         further_blank_line = true;
1417                 }
1418
1419                 if (params().spaceBottom().kind() != VSpace::NONE) {
1420                         os << params().spaceBottom().asLatexCommand(bparams);
1421                         further_blank_line = true;
1422                 }
1423
1424                 if (params().pagebreakBottom()) {
1425                         os << "\\newpage";
1426                         further_blank_line = true;
1427                 }
1428
1429                 if (further_blank_line) {
1430                         os << '\n';
1431                         texrow.newline();
1432                 }
1433
1434                 if (!params().spacing().isDefault()
1435                         && (!next_ || !next_->hasSameLayout(this))) {
1436                         os << params().spacing().writeEnvirEnd() << "\n";
1437                         texrow.newline();
1438                 }
1439         }
1440
1441         // we don't need it for the last paragraph!!!
1442         if (next_) {
1443                 os << '\n';
1444                 texrow.newline();
1445         } else {
1446                 // Since \selectlanguage write the language to the aux file,
1447                 // we need to reset the language at the end of footnote or
1448                 // float.
1449
1450                 if (language->babel() != doc_language->babel()) {
1451                         if (lyxrc.language_command_end.empty())
1452                                 os << subst(lyxrc.language_command_begin,
1453                                             "$$lang",
1454                                             doc_language->babel())
1455                                    << endl;
1456                         else
1457                                 os << subst(lyxrc.language_command_end,
1458                                             "$$lang",
1459                                             language->babel())
1460                                    << endl;
1461                         texrow.newline();
1462                 }
1463         }
1464
1465         lyxerr[Debug::LATEX] << "TeXOnePar...done " << next_ << endl;
1466         return next_;
1467 }
1468
1469 // This could go to ParagraphParameters if we want to
1470 int Paragraph::startTeXParParams(BufferParams const & bparams,
1471                                  ostream & os) const
1472 {
1473         int column = 0;
1474
1475         if (params().noindent()) {
1476                 os << "\\noindent ";
1477                 column += 10;
1478         }
1479
1480         switch (params().align()) {
1481         case LYX_ALIGN_NONE:
1482         case LYX_ALIGN_BLOCK:
1483         case LYX_ALIGN_LAYOUT:
1484         case LYX_ALIGN_SPECIAL:
1485                 break;
1486         case LYX_ALIGN_LEFT:
1487                 if (getParLanguage(bparams)->babel() != "hebrew") {
1488                         os << "\\begin{flushleft}";
1489                         column += 17;
1490                 } else {
1491                         os << "\\begin{flushright}";
1492                         column += 18;
1493                 }
1494                 break;
1495         case LYX_ALIGN_RIGHT:
1496                 if (getParLanguage(bparams)->babel() != "hebrew") {
1497                         os << "\\begin{flushright}";
1498                         column += 18;
1499                 } else {
1500                         os << "\\begin{flushleft}";
1501                         column += 17;
1502                 }
1503                 break;
1504         case LYX_ALIGN_CENTER:
1505                 os << "\\begin{center}";
1506                 column += 14;
1507                 break;
1508         }
1509
1510         return column;
1511 }
1512
1513 // This could go to ParagraphParameters if we want to
1514 int Paragraph::endTeXParParams(BufferParams const & bparams,
1515                                ostream & os) const
1516 {
1517         int column = 0;
1518
1519         switch (params().align()) {
1520         case LYX_ALIGN_NONE:
1521         case LYX_ALIGN_BLOCK:
1522         case LYX_ALIGN_LAYOUT:
1523         case LYX_ALIGN_SPECIAL:
1524                 break;
1525         case LYX_ALIGN_LEFT:
1526                 if (getParLanguage(bparams)->babel() != "hebrew") {
1527                         os << "\\end{flushleft}";
1528                         column = 15;
1529                 } else {
1530                         os << "\\end{flushright}";
1531                         column = 16;
1532                 }
1533                 break;
1534         case LYX_ALIGN_RIGHT:
1535                 if (getParLanguage(bparams)->babel() != "hebrew") {
1536                         os << "\\end{flushright}";
1537                         column+= 16;
1538                 } else {
1539                         os << "\\end{flushleft}";
1540                         column = 15;
1541                 }
1542                 break;
1543         case LYX_ALIGN_CENTER:
1544                 os << "\\end{center}";
1545                 column = 12;
1546                 break;
1547         }
1548         return column;
1549 }
1550
1551
1552 // This one spits out the text of the paragraph
1553 bool Paragraph::simpleTeXOnePar(Buffer const * buf,
1554                                 BufferParams const & bparams,
1555                                 ostream & os, TexRow & texrow,
1556                                 bool moving_arg)
1557 {
1558         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...     " << this << endl;
1559
1560         bool return_value = false;
1561
1562         LyXLayout style;
1563
1564         // well we have to check if we are in an inset with unlimited
1565         // lenght (all in one row) if that is true then we don't allow
1566         // any special options in the paragraph and also we don't allow
1567         // any environment other then "Standard" to be valid!
1568         bool asdefault =
1569                 (inInset() && inInset()->forceDefaultParagraphs(inInset()));
1570
1571         if (asdefault) {
1572                 style = textclasslist[bparams.textclass].defaultLayout();
1573         } else {
1574                 style = textclasslist[bparams.textclass][layout()];
1575         }
1576
1577         LyXFont basefont;
1578
1579         // Maybe we have to create a optional argument.
1580         pos_type main_body;
1581         if (style.labeltype != LABEL_MANUAL)
1582                 main_body = 0;
1583         else
1584                 main_body = beginningOfMainBody();
1585
1586         int column = 0;
1587
1588         if (main_body > 0) {
1589                 os << '[';
1590                 ++column;
1591                 basefont = getLabelFont(bparams);
1592         } else {
1593                 basefont = getLayoutFont(bparams);
1594         }
1595
1596         moving_arg |= style.needprotect;
1597
1598         // Which font is currently active?
1599         LyXFont running_font(basefont);
1600         // Do we have an open font change?
1601         bool open_font = false;
1602
1603         texrow.start(this, 0);
1604
1605         // if the paragraph is empty, the loop will not be entered at all
1606         if (!size()) {
1607                 if (style.isCommand()) {
1608                         os << '{';
1609                         ++column;
1610                 }
1611                 if (!asdefault)
1612                         column += startTeXParParams(bparams, os);
1613
1614         }
1615
1616         for (pos_type i = 0; i < size(); ++i) {
1617                 ++column;
1618                 // First char in paragraph or after label?
1619                 if (i == main_body) {
1620                         if (main_body > 0) {
1621                                 if (open_font) {
1622                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
1623                                         open_font = false;
1624                                 }
1625                                 basefont = getLayoutFont(bparams);
1626                                 running_font = basefont;
1627                                 os << ']';
1628                                 ++column;
1629                         }
1630                         if (style.isCommand()) {
1631                                 os << '{';
1632                                 ++column;
1633                         }
1634
1635                         if (!asdefault)
1636                                 column += startTeXParParams(bparams, os);
1637                 }
1638
1639                 value_type c = getChar(i);
1640
1641                 // Fully instantiated font
1642                 LyXFont font = getFont(bparams, i);
1643
1644                 LyXFont const last_font = running_font;
1645
1646                 // Spaces at end of font change are simulated to be
1647                 // outside font change, i.e. we write "\textXX{text} "
1648                 // rather than "\textXX{text }". (Asger)
1649                 if (open_font && c == ' ' && i <= size() - 2) {
1650                         LyXFont const & next_font = getFont(bparams, i + 1);
1651                         if (next_font != running_font
1652                             && next_font != font) {
1653                                 font = next_font;
1654                         }
1655                 }
1656
1657                 // We end font definition before blanks
1658                 if (open_font &&
1659                     (font != running_font ||
1660                      font.language() != running_font.language()))
1661                 {
1662                         column += running_font.latexWriteEndChanges(os,
1663                                                                     basefont,
1664                                                                     (i == main_body-1) ? basefont : font);
1665                         running_font = basefont;
1666                         open_font = false;
1667                 }
1668
1669                 // Blanks are printed before start of fontswitch
1670                 if (c == ' ') {
1671                         // Do not print the separation of the optional argument
1672                         if (i != main_body - 1) {
1673                                 pimpl_->simpleTeXBlanks(os, texrow, i,
1674                                                        column, font, style);
1675                         }
1676                 }
1677
1678                 // Do we need to change font?
1679                 if ((font != running_font ||
1680                      font.language() != running_font.language()) &&
1681                         i != main_body - 1)
1682                 {
1683                         column += font.latexWriteStartChanges(os, basefont,
1684                                                               last_font);
1685                         running_font = font;
1686                         open_font = true;
1687                 }
1688
1689                 if (c == Paragraph::META_NEWLINE) {
1690                         // newlines are handled differently here than
1691                         // the default in SimpleTeXSpecialChars().
1692                         if (!style.newline_allowed) {
1693                                 os << '\n';
1694                         } else {
1695                                 if (open_font) {
1696                                         column += running_font.latexWriteEndChanges(os, basefont, basefont);
1697                                         open_font = false;
1698                                 }
1699                                 basefont = getLayoutFont(bparams);
1700                                 running_font = basefont;
1701                                 if (font.family() ==
1702                                     LyXFont::TYPEWRITER_FAMILY) {
1703                                         os << "~";
1704                                 }
1705                                 if (moving_arg)
1706                                         os << "\\protect ";
1707                                 os << "\\\\\n";
1708                         }
1709                         texrow.newline();
1710                         texrow.start(this, i + 1);
1711                         column = 0;
1712                 } else {
1713                         pimpl_->simpleTeXSpecialChars(buf, bparams,
1714                                                       os, texrow, moving_arg,
1715                                                       font, running_font,
1716                                                       basefont, open_font,
1717                                                       style, i, column, c);
1718                 }
1719         }
1720
1721         // If we have an open font definition, we have to close it
1722         if (open_font) {
1723 #ifdef FIXED_LANGUAGE_END_DETECTION
1724                 if (next_) {
1725                         running_font
1726                                 .latexWriteEndChanges(os, basefont,
1727                                                       next_->getFont(bparams,
1728                                                       0));
1729                 } else {
1730                         running_font.latexWriteEndChanges(os, basefont,
1731                                                           basefont);
1732                 }
1733 #else
1734 #ifdef WITH_WARNINGS
1735 //#warning For now we ALWAYS have to close the foreign font settings if they are
1736 //#warning there as we start another \selectlanguage with the next paragraph if
1737 //#warning we are in need of this. This should be fixed sometime (Jug)
1738 #endif
1739                 running_font.latexWriteEndChanges(os, basefont,  basefont);
1740 #endif
1741         }
1742
1743         // Needed if there is an optional argument but no contents.
1744         if (main_body > 0 && main_body == size()) {
1745                 os << "]~";
1746                 return_value = false;
1747         }
1748
1749         if (!asdefault) {
1750                 column += endTeXParParams(bparams, os);
1751         }
1752
1753         lyxerr[Debug::LATEX] << "SimpleTeXOnePar...done " << this << endl;
1754         return return_value;
1755 }
1756
1757
1758 bool Paragraph::sgmlConvertChar(char c, string & sgml_string)
1759 {
1760         bool retval = false;
1761         switch (c) {
1762         case Paragraph::META_HFILL:
1763                 sgml_string.erase();
1764                 break;
1765         case Paragraph::META_NEWLINE:
1766                 sgml_string = '\n';
1767                 break;
1768         case '&':
1769                 sgml_string = "&amp;";
1770                 break;
1771         case '<':
1772                 sgml_string = "&lt;";
1773                 break;
1774         case '>':
1775                 sgml_string = "&gt;";
1776                 break;
1777         case '$':
1778                 sgml_string = "&dollar;";
1779                 break;
1780         case '#':
1781                 sgml_string = "&num;";
1782                 break;
1783         case '%':
1784                 sgml_string = "&percnt;";
1785                 break;
1786         case '[':
1787                 sgml_string = "&lsqb;";
1788                 break;
1789         case ']':
1790                 sgml_string = "&rsqb;";
1791                 break;
1792         case '{':
1793                 sgml_string = "&lcub;";
1794                 break;
1795         case '}':
1796                 sgml_string = "&rcub;";
1797                 break;
1798         case '~':
1799                 sgml_string = "&tilde;";
1800                 break;
1801         case '"':
1802                 sgml_string = "&quot;";
1803                 break;
1804         case '\\':
1805                 sgml_string = "&bsol;";
1806                 break;
1807         case ' ':
1808                 retval = true;
1809                 sgml_string = ' ';
1810                 break;
1811         case '\0': // Ignore :-)
1812                 sgml_string.erase();
1813                 break;
1814         default:
1815                 sgml_string = c;
1816                 break;
1817         }
1818         return retval;
1819 }
1820
1821
1822 Paragraph * Paragraph::TeXEnvironment(Buffer const * buf,
1823                                             BufferParams const & bparams,
1824                                             ostream & os, TexRow & texrow)
1825 {
1826         lyxerr[Debug::LATEX] << "TeXEnvironment...     " << this << endl;
1827
1828         LyXLayout const & style =
1829                 textclasslist[bparams.textclass][layout()];
1830
1831         Language const * language = getParLanguage(bparams);
1832         Language const * doc_language = bparams.language;
1833         Language const * previous_language = previous_
1834                 ? previous_->getParLanguage(bparams) : doc_language;
1835         if (language->babel() != previous_language->babel()) {
1836
1837                 if (!lyxrc.language_command_end.empty() &&
1838                     previous_language->babel() != doc_language->babel()) {
1839                         os << subst(lyxrc.language_command_end, "$$lang",
1840                                     previous_language->babel())
1841                            << endl;
1842                         texrow.newline();
1843                 }
1844
1845                 if (lyxrc.language_command_end.empty() ||
1846                     language->babel() != doc_language->babel()) {
1847                         os << subst(lyxrc.language_command_begin, "$$lang",
1848                                     language->babel())
1849                            << endl;
1850                         texrow.newline();
1851                 }
1852         }
1853         
1854         bool leftindent_open = false;
1855         if (!params().leftIndent().zero()) {
1856                 os << "\\begin{LyXParagraphLeftIndent}{" <<
1857                         params().leftIndent().asLatexString() << "}\n";
1858                 texrow.newline();
1859                 leftindent_open = true;
1860         }
1861
1862         if (style.isEnvironment()) {
1863                 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
1864                         os << "\\begin{" << style.latexname() << "}{"
1865                            << params().labelWidthString() << "}\n";
1866                 } else if (style.labeltype == LABEL_BIBLIO) {
1867                         // ale970405
1868                         os << "\\begin{" << style.latexname() << "}{"
1869                            <<  bibitemWidest(buf)
1870                            << "}\n";
1871                 } else if (style.latextype == LATEX_ITEM_ENVIRONMENT) {
1872                         os << "\\begin{" << style.latexname() << '}'
1873                            << style.latexparam() << '\n';
1874                 } else
1875                         os << "\\begin{" << style.latexname() << '}'
1876                            << style.latexparam() << '\n';
1877                 texrow.newline();
1878         }
1879         Paragraph * par = this;
1880         do {
1881                 par = par->TeXOnePar(buf, bparams, os, texrow, false);
1882
1883                 if (par && par->params().depth() > params().depth()) {
1884                         if (textclasslist[bparams.textclass][par->layout()].isParagraph()
1885                             // Thinko!
1886                             // How to handle this? (Lgb)
1887                             //&& !suffixIs(os, "\n\n")
1888                                 ) {
1889                                 // There should be at least one '\n' already
1890                                 // but we need there to be two for Standard
1891                                 // paragraphs that are depth-increment'ed to be
1892                                 // output correctly.  However, tables can
1893                                 // also be paragraphs so don't adjust them.
1894                                 // ARRae
1895                                 // Thinkee:
1896                                 // Will it ever harm to have one '\n' too
1897                                 // many? i.e. that we sometimes will have
1898                                 // three in a row. (Lgb)
1899                                 os << '\n';
1900                                 texrow.newline();
1901                         }
1902                         par = par->pimpl_->TeXDeeper(buf, bparams, os, texrow);
1903                 }
1904         } while (par
1905                  && par->layout() == layout()
1906                  && par->params().depth() == params().depth()
1907                  && par->params().leftIndent() == params().leftIndent());
1908
1909         if (style.isEnvironment()) {
1910                 os << "\\end{" << style.latexname() << "}\n";
1911                 texrow.newline();
1912         }
1913
1914         if (leftindent_open) {
1915                 os << "\\end{LyXParagraphLeftIndent}\n";
1916                 texrow.newline();
1917         }
1918         
1919         lyxerr[Debug::LATEX] << "TeXEnvironment...done " << par << endl;
1920         return par;  // ale970302
1921 }
1922
1923
1924 bool Paragraph::isHfill(pos_type pos) const
1925 {
1926         return IsHfillChar(getChar(pos));
1927 }
1928
1929
1930 bool Paragraph::isInset(pos_type pos) const
1931 {
1932         return IsInsetChar(getChar(pos));
1933 }
1934
1935
1936 bool Paragraph::isNewline(pos_type pos) const
1937 {
1938         return pos >= 0 && IsNewlineChar(getChar(pos));
1939 }
1940
1941
1942 bool Paragraph::isSeparator(pos_type pos) const
1943 {
1944         return IsSeparatorChar(getChar(pos));
1945 }
1946
1947
1948 bool Paragraph::isLineSeparator(pos_type pos) const
1949 {
1950         value_type const c = getChar(pos);
1951         return IsLineSeparatorChar(c)
1952                 || (IsInsetChar(c) && getInset(pos) &&
1953                 getInset(pos)->isLineSeparator());
1954 }
1955
1956
1957 bool Paragraph::isKomma(pos_type pos) const
1958 {
1959         return IsKommaChar(getChar(pos));
1960 }
1961
1962
1963 /// Used by the spellchecker
1964 bool Paragraph::isLetter(pos_type pos) const
1965 {
1966         value_type const c = getChar(pos);
1967         if (IsLetterChar(c))
1968                 return true;
1969         if (isInset(pos))
1970                 return getInset(pos)->isLetter();
1971         // We want to pass the ' and escape chars to ispell
1972         string const extra = lyxrc.isp_esc_chars + '\'';
1973         return contains(extra, c);
1974 }
1975
1976
1977 bool Paragraph::isWord(pos_type pos) const
1978 {
1979         return IsWordChar(getChar(pos)) ;
1980 }
1981
1982
1983 Language const *
1984 Paragraph::getParLanguage(BufferParams const & bparams) const
1985 {
1986         if (size() > 0) {
1987 #ifndef INHERIT_LANGUAGE
1988                 return getFirstFontSettings().language();
1989 #else
1990                 Language const * lang = getFirstFontSettings().language();
1991 #ifdef WITH_WARNINGS
1992 #warning We should make this somewhat better, any ideas? (Jug)
1993 #endif
1994                 if (lang == inherit_language || lang == ignore_language)
1995                         lang = bparams.language;
1996                 return lang;
1997 #endif
1998         } else if (previous_)
1999                 return previous_->getParLanguage(bparams);
2000         else
2001                 return bparams.language;
2002 }
2003
2004
2005 bool Paragraph::isRightToLeftPar(BufferParams const & bparams) const
2006 {
2007         return lyxrc.rtl_support
2008                 && getParLanguage(bparams)->RightToLeft()
2009                 && !(inInset() && inInset()->owner() &&
2010                      inInset()->owner()->lyxCode() == Inset::ERT_CODE);
2011 }
2012
2013
2014 void Paragraph::changeLanguage(BufferParams const & bparams,
2015                                   Language const * from, Language const * to)
2016 {
2017         for (pos_type i = 0; i < size(); ++i) {
2018                 LyXFont font = getFontSettings(bparams, i);
2019                 if (font.language() == from) {
2020                         font.setLanguage(to);
2021                         setFont(i, font);
2022                 }
2023         }
2024 }
2025
2026
2027 bool Paragraph::isMultiLingual(BufferParams const & bparams)
2028 {
2029         Language const * doc_language = bparams.language;
2030         Pimpl::FontList::const_iterator cit = pimpl_->fontlist.begin();
2031         Pimpl::FontList::const_iterator end = pimpl_->fontlist.end();
2032
2033         for (; cit != end; ++cit)
2034                 if (cit->font().language() != ignore_language &&
2035                     cit->font().language() != latex_language &&
2036 #ifdef INHERIT_LANGUAGE
2037                         cit->font().language() != inherit_language &&
2038 #endif
2039                         cit->font().language() != doc_language)
2040                         return true;
2041         return false;
2042 }
2043
2044
2045 // Convert the paragraph to a string.
2046 // Used for building the table of contents
2047 string const Paragraph::asString(Buffer const * buffer, bool label)
2048 {
2049         BufferParams const & bparams = buffer->params;
2050         string s;
2051         if (label && !params().labelString().empty())
2052                 s += params().labelString() + ' ';
2053         string::size_type const len = s.size();
2054
2055         for (pos_type i = 0; i < size(); ++i) {
2056                 value_type c = getChar(i);
2057                 if (IsPrintable(c))
2058                         s += c;
2059                 else if (c == META_INSET &&
2060                          getInset(i)->lyxCode() == Inset::MATH_CODE) {
2061                         ostringstream ost;
2062                         getInset(i)->ascii(buffer, ost);
2063                         s += subst(ost.str().c_str(),'\n',' ');
2064                 }
2065         }
2066
2067         if (isRightToLeftPar(bparams))
2068                 reverse(s.begin() + len,s.end());
2069
2070         return s;
2071 }
2072
2073
2074 string const Paragraph::asString(Buffer const * buffer,
2075                                  pos_type beg, pos_type end, bool label)
2076 {
2077         ostringstream ost;
2078
2079         if (beg == 0 && label && !params().labelString().empty())
2080                 ost << params().labelString() << ' ';
2081
2082         for (pos_type i = beg; i < end; ++i) {
2083                 value_type const c = getUChar(buffer->params, i);
2084                 if (IsPrintable(c))
2085                         ost << c;
2086                 else if (c == META_NEWLINE)
2087                         ost << '\n';
2088                 else if (c == META_HFILL)
2089                         ost << '\t';
2090                 else if (c == META_INSET) {
2091                         getInset(i)->ascii(buffer, ost);
2092                 }
2093         }
2094
2095         return ost.str().c_str();
2096 }
2097
2098
2099 void Paragraph::setInsetOwner(Inset * i)
2100 {
2101         pimpl_->inset_owner = i;
2102         InsetList::const_iterator cit = insetlist.begin();
2103         InsetList::const_iterator end = insetlist.end();
2104         for (; cit != end; ++cit) {
2105                 if (cit->inset)
2106                         cit->inset->setOwner(i);
2107         }
2108 }
2109
2110
2111 void Paragraph::deleteInsetsLyXText(BufferView * bv)
2112 {
2113         // then the insets
2114         InsetList::const_iterator cit = insetlist.begin();
2115         InsetList::const_iterator end = insetlist.end();
2116         for (; cit != end; ++cit) {
2117                 if (cit->inset && cit->inset->isTextInset()) {
2118                         static_cast<UpdatableInset *>
2119                                 (cit->inset)->deleteLyXText(bv, true);
2120                 }
2121         }
2122 }
2123
2124
2125 void Paragraph::resizeInsetsLyXText(BufferView * bv)
2126 {
2127         // then the insets
2128         InsetList::const_iterator cit = insetlist.begin();
2129         InsetList::const_iterator end = insetlist.end();
2130         for (; cit != end; ++cit) {
2131                 if (cit->inset) {
2132                         if (cit->inset->isTextInset()) {
2133                                 static_cast<UpdatableInset *>
2134                                         (cit->inset)->resizeLyXText(bv, true);
2135                         }
2136                 }
2137         }
2138 }
2139
2140
2141 void Paragraph::setContentsFromPar(Paragraph * par)
2142 {
2143         pimpl_->setContentsFromPar(par);
2144 }
2145
2146
2147 lyx::pos_type Paragraph::size() const
2148 {
2149         return pimpl_->size();
2150 }
2151
2152
2153 Paragraph::value_type Paragraph::getChar(pos_type pos) const
2154 {
2155         return pimpl_->getChar(pos);
2156 }
2157
2158
2159 int Paragraph::id() const
2160 {
2161         return pimpl_->id_;
2162 }
2163
2164
2165 void  Paragraph::id(int id_arg)
2166 {
2167         pimpl_->id_ = id_arg;
2168 }
2169
2170
2171 string const & Paragraph::layout() const
2172 {
2173         return layout_;
2174 }
2175
2176
2177 void Paragraph::layout(string const & new_layout)
2178 {
2179         lyx::Assert(!new_layout.empty());
2180
2181         layout_ = new_layout;
2182 }
2183
2184
2185 bool Paragraph::isFirstInSequence() const
2186 {
2187         Paragraph const * dhook = depthHook(getDepth());
2188         return (dhook == this
2189                 || dhook->layout() != layout()
2190                 || dhook->getDepth() != getDepth());
2191 }
2192
2193
2194 Inset * Paragraph::inInset() const
2195 {
2196         return pimpl_->inset_owner;
2197 }
2198
2199
2200 void Paragraph::clearContents()
2201 {
2202         pimpl_->clear();
2203 }
2204
2205
2206 void Paragraph::setCounter(int i, int v)
2207 {
2208         pimpl_->counter_[i] = v;
2209 }
2210
2211
2212 int Paragraph::getCounter(int i) const
2213 {
2214         return pimpl_->counter_[i];
2215 }
2216
2217
2218 void Paragraph::incCounter(int i)
2219 {
2220         pimpl_->counter_[i]++;
2221 }
2222
2223
2224 void Paragraph::setChar(pos_type pos, value_type c)
2225 {
2226         pimpl_->setChar(pos, c);
2227 }
2228
2229
2230 Paragraph::inset_iterator::inset_iterator(Paragraph::InsetList::iterator const & iter)
2231  : it(iter)
2232 {}
2233
2234
2235 Paragraph::inset_iterator Paragraph::inset_iterator_begin()
2236 {
2237         return inset_iterator(insetlist.begin());
2238 }
2239
2240
2241 Paragraph::inset_iterator Paragraph::inset_iterator_end()
2242 {
2243         return inset_iterator(insetlist.end());
2244 }
2245
2246
2247 ParagraphParameters & Paragraph::params()
2248 {
2249         return pimpl_->params;
2250 }
2251
2252
2253 ParagraphParameters const & Paragraph::params() const
2254 {
2255         return pimpl_->params;
2256 }
2257
2258
2259 Paragraph * Paragraph::getParFromID(int id) const
2260 {
2261         return pimpl_->getParFromID(id);
2262 }
2263
2264
2265 bool Paragraph::isFreeSpacing() const
2266 {
2267         // for now we just need this, later should we need this in some
2268         // other way we can always add a function to Inset::() too.
2269         if (pimpl_->inset_owner && pimpl_->inset_owner->owner())
2270                 return (pimpl_->inset_owner->owner()->lyxCode() == Inset::ERT_CODE);
2271         return false;
2272 }