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