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