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