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