]> git.lyx.org Git - lyx.git/blob - src/paragraph_funcs.C
minimal ERT spacing patch (first patch sent to list plus a couple of fixes)
[lyx.git] / src / paragraph_funcs.C
1 /**
2  * \file paragraph_funcs.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS
9  */
10
11 #include <config.h>
12
13 #include "paragraph_funcs.h"
14 #include "paragraph_pimpl.h"
15 #include "buffer.h"
16 #include "ParagraphParameters.h"
17 #include "lyxtextclasslist.h"
18 #include "debug.h"
19 #include "gettext.h"
20 #include "language.h"
21 #include "encoding.h"
22 #include "lyxrc.h"
23 #include "lyxlex.h"
24 #include "factory.h"
25 #include "Lsstream.h"
26 #include "support/lstrings.h"
27 #include "insets/insetoptarg.h"
28 #include "insets/insetcommandparams.h"
29 #include "insets/insetbibitem.h"
30 #include "insets/insetspace.h"
31 #include "insets/insetspecialchar.h"
32 #include "insets/insetlatexaccent.h"
33 #include "insets/insettabular.h"
34 #include "insets/insethfill.h"
35 #include "insets/inseterror.h"
36 #include "insets/insetnewline.h"
37
38 extern string bibitemWidest(Buffer const *);
39
40 using lyx::pos_type;
41 //using lyx::layout_type;
42 using std::endl;
43 using std::ostream;
44
45
46 void breakParagraph(BufferParams const & bparams,
47                     ParagraphList & paragraphs,
48                     ParagraphList::iterator par,
49                     pos_type pos,
50                     int flag)
51 {
52         // create a new paragraph, and insert into the list
53         ParagraphList::iterator tmp = paragraphs.insert(boost::next(par),
54                                                         Paragraph());
55
56         // without doing that we get a crash when typing <Return> at the
57         // end of a paragraph
58         tmp->layout(bparams.getLyXTextClass().defaultLayout());
59         // remember to set the inset_owner
60         tmp->setInsetOwner(par->inInset());
61
62         if (bparams.tracking_changes)
63                 tmp->trackChanges();
64
65         // this is an idea for a more userfriendly layout handling, I will
66         // see what the users say
67
68         // layout stays the same with latex-environments
69         if (flag) {
70                 tmp->layout(par->layout());
71                 tmp->setLabelWidthString(par->params().labelWidthString());
72         }
73
74         bool const isempty = (par->allowEmpty() && par->empty());
75
76         if (!isempty && (par->size() > pos || par->empty() || flag == 2)) {
77                 tmp->layout(par->layout());
78                 tmp->params().align(par->params().align());
79                 tmp->setLabelWidthString(par->params().labelWidthString());
80
81                 tmp->params().lineBottom(par->params().lineBottom());
82                 par->params().lineBottom(false);
83                 tmp->params().pagebreakBottom(par->params().pagebreakBottom());
84                 par->params().pagebreakBottom(false);
85                 tmp->params().spaceBottom(par->params().spaceBottom());
86                 par->params().spaceBottom(VSpace(VSpace::NONE));
87
88                 tmp->params().depth(par->params().depth());
89                 tmp->params().noindent(par->params().noindent());
90
91                 // copy everything behind the break-position
92                 // to the new paragraph
93
94 #ifdef WITH_WARNINGS
95 #warning this seems wrong
96 #endif
97                 /* FIXME: if !keepempty, empty() == true, then we reach
98                  * here with size() == 0. So pos_end becomes - 1. Why
99                  * doesn't this cause problems ???
100                  */
101                 pos_type pos_end = par->size() - 1;
102                 pos_type i = pos;
103                 pos_type j = pos;
104
105                 for (; i <= pos_end; ++i) {
106                         Change::Type change(par->lookupChange(i));
107                         par->cutIntoMinibuffer(bparams, i);
108                         if (tmp->insertFromMinibuffer(j - pos)) {
109                                 tmp->setChange(j - pos, change);
110                                 ++j;
111                         }
112                 }
113                 for (i = pos_end; i >= pos; --i) {
114                         par->eraseIntern(i);
115                 }
116         }
117
118         if (pos)
119                 return;
120
121         tmp->params().lineTop(par->params().lineTop());
122         tmp->params().pagebreakTop(par->params().pagebreakTop());
123         tmp->params().spaceTop(par->params().spaceTop());
124         par->params().clear();
125
126         par->layout(bparams.getLyXTextClass().defaultLayout());
127
128         // layout stays the same with latex-environments
129         if (flag) {
130                 par->layout(tmp->layout());
131                 par->setLabelWidthString(tmp->params().labelWidthString());
132                 par->params().depth(tmp->params().depth());
133         }
134
135         // subtle, but needed to get empty pars working right
136         if (bparams.tracking_changes) {
137                 if (!par->size()) {
138                         par->cleanChanges();
139                 } else if (!tmp->size()) {
140                         tmp->cleanChanges();
141                 }
142         }
143 }
144
145
146 void breakParagraphConservative(BufferParams const & bparams,
147                                 ParagraphList & paragraphs,
148                                 ParagraphList::iterator par,
149                                 pos_type pos)
150 {
151         // create a new paragraph
152         ParagraphList::iterator tmp = paragraphs.insert(boost::next(par),
153                                                         Paragraph());
154         tmp->makeSameLayout(*par);
155
156         // When can pos > size()?
157         // I guess pos == size() is possible.
158         if (par->size() > pos) {
159                 // copy everything behind the break-position to the new
160                 // paragraph
161                 pos_type pos_end = par->size() - 1;
162
163                 for (pos_type i = pos, j = pos; i <= pos_end; ++i) {
164                         par->cutIntoMinibuffer(bparams, i);
165                         if (tmp->insertFromMinibuffer(j - pos))
166                                 ++j;
167                 }
168
169                 for (pos_type k = pos_end; k >= pos; --k) {
170                         par->erase(k);
171                 }
172         }
173 }
174
175
176 void mergeParagraph(BufferParams const & bparams,
177                     ParagraphList & paragraphs,
178                     ParagraphList::iterator par)
179 {
180         ParagraphList::iterator the_next = boost::next(par);
181
182         // first the DTP-stuff
183         par->params().lineBottom(the_next->params().lineBottom());
184         par->params().spaceBottom(the_next->params().spaceBottom());
185         par->params().pagebreakBottom(the_next->params().pagebreakBottom());
186
187         pos_type pos_end = the_next->size() - 1;
188         pos_type pos_insert = par->size();
189
190         // ok, now copy the paragraph
191         for (pos_type i = 0, j = 0; i <= pos_end; ++i) {
192                 the_next->cutIntoMinibuffer(bparams, i);
193                 if (par->insertFromMinibuffer(pos_insert + j))
194                         ++j;
195         }
196
197         paragraphs.erase(the_next);
198 }
199
200
201 ParagraphList::iterator depthHook(ParagraphList::iterator pit,
202                                   ParagraphList const & plist,
203                                   Paragraph::depth_type depth)
204 {
205         ParagraphList::iterator newpit = pit;
206         ParagraphList::iterator beg = const_cast<ParagraphList&>(plist).begin();
207
208         if (newpit != beg)
209                 --newpit;
210
211         while (newpit !=  beg && newpit->getDepth() > depth) {
212                 --newpit;
213         }
214
215         if (newpit->getDepth() > depth)
216                 return pit;
217
218         return newpit;
219 }
220
221
222 ParagraphList::iterator outerHook(ParagraphList::iterator pit,
223                                   ParagraphList const & plist)
224 {
225         if (!pit->getDepth())
226                 return const_cast<ParagraphList&>(plist).end();
227         return depthHook(pit, plist,
228                          Paragraph::depth_type(pit->getDepth() - 1));
229 }
230
231
232 bool isFirstInSequence(ParagraphList::iterator pit,
233                        ParagraphList const & plist)
234 {
235         ParagraphList::iterator dhook = depthHook(pit, plist, pit->getDepth());
236         return (dhook == pit
237                 || dhook->layout() != pit->layout()
238                 || dhook->getDepth() != pit->getDepth());
239 }
240
241
242 int getEndLabel(ParagraphList::iterator p, ParagraphList const & plist)
243 {
244         ParagraphList::iterator pit = p;
245         Paragraph::depth_type par_depth = p->getDepth();
246         while (pit != const_cast<ParagraphList&>(plist).end()) {
247                 LyXLayout_ptr const & layout = pit->layout();
248                 int const endlabeltype = layout->endlabeltype;
249
250                 if (endlabeltype != END_LABEL_NO_LABEL) {
251                         if (boost::next(p) == const_cast<ParagraphList&>(plist).end())
252                                 return endlabeltype;
253
254                         Paragraph::depth_type const next_depth = boost::next(p)->getDepth();
255                         if (par_depth > next_depth ||
256                             (par_depth == next_depth &&
257                              layout != boost::next(p)->layout()))
258                                 return endlabeltype;
259                         break;
260                 }
261                 if (par_depth == 0)
262                         break;
263                 pit = outerHook(pit, plist);
264                 if (pit != const_cast<ParagraphList&>(plist).end())
265                         par_depth = pit->getDepth();
266         }
267         return END_LABEL_NO_LABEL;
268 }
269
270
271 namespace {
272
273 ParagraphList::iterator
274 TeXEnvironment(Buffer const * buf,
275                ParagraphList const & paragraphs,
276                ParagraphList::iterator pit,
277                ostream & os, TexRow & texrow,
278                LatexRunParams const & runparams);
279
280 ParagraphList::iterator
281 TeXOnePar(Buffer const * buf,
282           ParagraphList const & paragraphs,
283           ParagraphList::iterator pit,
284           ostream & os, TexRow & texrow,
285           LatexRunParams const & runparams,
286           string const & everypar = string());
287
288
289 ParagraphList::iterator
290 TeXDeeper(Buffer const * buf,
291           ParagraphList const & paragraphs,
292           ParagraphList::iterator pit,
293           ostream & os, TexRow & texrow,
294           LatexRunParams const & runparams)
295 {
296         lyxerr[Debug::LATEX] << "TeXDeeper...     " << &*pit << endl;
297         ParagraphList::iterator par = pit;
298
299         while (par != const_cast<ParagraphList&>(paragraphs).end() &&
300                      par->params().depth() == pit->params().depth()) {
301                 if (par->layout()->isEnvironment()) {
302                         par = TeXEnvironment(buf, paragraphs, par,
303                                              os, texrow, runparams);
304                 } else {
305                         par = TeXOnePar(buf, paragraphs, par,
306                                              os, texrow, runparams);
307                 }
308         }
309         lyxerr[Debug::LATEX] << "TeXDeeper...done " << &*par << endl;
310
311         return par;
312 }
313
314
315 ParagraphList::iterator
316 TeXEnvironment(Buffer const * buf,
317                ParagraphList const & paragraphs,
318                ParagraphList::iterator pit,
319                ostream & os, TexRow & texrow,
320                LatexRunParams const & runparams)
321 {
322         lyxerr[Debug::LATEX] << "TeXEnvironment...     " << &*pit << endl;
323
324         BufferParams const & bparams = buf->params;
325
326         LyXLayout_ptr const & style = pit->layout();
327
328         Language const * language = pit->getParLanguage(bparams);
329         Language const * doc_language = bparams.language;
330         Language const * previous_language =
331                 (pit != const_cast<ParagraphList&>(paragraphs).begin())
332                 ? boost::prior(pit)->getParLanguage(bparams)
333                 : doc_language;
334         if (language->babel() != previous_language->babel()) {
335
336                 if (!lyxrc.language_command_end.empty() &&
337                     previous_language->babel() != doc_language->babel()) {
338                         os << subst(lyxrc.language_command_end, "$$lang",
339                                     previous_language->babel())
340                            << endl;
341                         texrow.newline();
342                 }
343
344                 if (lyxrc.language_command_end.empty() ||
345                     language->babel() != doc_language->babel()) {
346                         os << subst(lyxrc.language_command_begin, "$$lang",
347                                     language->babel())
348                            << endl;
349                         texrow.newline();
350                 }
351         }
352
353         bool leftindent_open = false;
354         if (!pit->params().leftIndent().zero()) {
355                 os << "\\begin{LyXParagraphLeftIndent}{" <<
356                         pit->params().leftIndent().asLatexString() << "}\n";
357                 texrow.newline();
358                 leftindent_open = true;
359         }
360
361         if (style->isEnvironment()) {
362                 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
363                         os << "\\begin{" << style->latexname() << "}{"
364                            << pit->params().labelWidthString() << "}\n";
365                 } else if (style->labeltype == LABEL_BIBLIO) {
366                         // ale970405
367                         os << "\\begin{" << style->latexname() << "}{"
368                            <<  bibitemWidest(buf)
369                            << "}\n";
370                 } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
371                         os << "\\begin{" << style->latexname() << '}'
372                            << style->latexparam() << '\n';
373                 } else
374                         os << "\\begin{" << style->latexname() << '}'
375                            << style->latexparam() << '\n';
376                 texrow.newline();
377         }
378         ParagraphList::iterator par = pit;
379         do {
380                 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
381
382                 if (par != const_cast<ParagraphList&>(paragraphs).end() && par->params().depth() > pit->params().depth()) {
383                             if (par->layout()->isParagraph()) {
384
385                             // Thinko!
386                             // How to handle this? (Lgb)
387                             //&& !suffixIs(os, "\n\n")
388                                     //) {
389                                 // There should be at least one '\n' already
390                                 // but we need there to be two for Standard
391                                 // paragraphs that are depth-increment'ed to be
392                                 // output correctly.  However, tables can
393                                 // also be paragraphs so don't adjust them.
394                                 // ARRae
395                                 // Thinkee:
396                                 // Will it ever harm to have one '\n' too
397                                 // many? i.e. that we sometimes will have
398                                 // three in a row. (Lgb)
399                                 os << '\n';
400                                 texrow.newline();
401                         }
402                         par = TeXDeeper(buf, paragraphs, par, os, texrow,
403                                         runparams);
404                 }
405         } while (par != const_cast<ParagraphList&>(paragraphs).end()
406                  && par->layout() == pit->layout()
407                  && par->params().depth() == pit->params().depth()
408                  && par->params().leftIndent() == pit->params().leftIndent());
409
410         if (style->isEnvironment()) {
411                 os << "\\end{" << style->latexname() << "}\n";
412                 texrow.newline();
413         }
414
415         if (leftindent_open) {
416                 os << "\\end{LyXParagraphLeftIndent}\n";
417                 texrow.newline();
418         }
419
420         lyxerr[Debug::LATEX] << "TeXEnvironment...done " << &*par << endl;
421         return par;  // ale970302
422 }
423
424
425 InsetOptArg * optArgInset(Paragraph const & par)
426 {
427         // Find the entry.
428         InsetList::const_iterator it = par.insetlist.begin();
429         InsetList::const_iterator end = par.insetlist.end();
430         for (; it != end; ++it) {
431                 Inset * ins = it->inset;
432                 if (ins->lyxCode() == Inset::OPTARG_CODE) {
433                         return static_cast<InsetOptArg *>(ins);
434                 }
435         }
436         return 0;
437 }
438
439
440 ParagraphList::iterator
441 TeXOnePar(Buffer const * buf,
442           ParagraphList const & paragraphs,
443           ParagraphList::iterator pit,
444           ostream & os, TexRow & texrow,
445           LatexRunParams const & runparams,
446           string const & everypar)
447 {
448         lyxerr[Debug::LATEX] << "TeXOnePar...     " << &*pit << " '" << everypar
449 << "'" << endl;
450         BufferParams const & bparams = buf->params;
451
452         Inset const * in = pit->inInset();
453         bool further_blank_line = false;
454         LyXLayout_ptr style;
455
456         // well we have to check if we are in an inset with unlimited
457         // lenght (all in one row) if that is true then we don't allow
458         // any special options in the paragraph and also we don't allow
459         // any environment other then "Standard" to be valid!
460         if ((in == 0) || !in->forceDefaultParagraphs(in)) {
461                 style = pit->layout();
462
463                 if (pit->params().startOfAppendix()) {
464                         os << "\\appendix\n";
465                         texrow.newline();
466                 }
467
468                 if (!pit->params().spacing().isDefault()
469                         && (pit == const_cast<ParagraphList&>(paragraphs).begin() || !boost::prior(pit)->hasSameLayout(*pit))) {
470                         os << pit->params().spacing().writeEnvirBegin() << '\n';
471                         texrow.newline();
472                 }
473
474                 if (style->isCommand()) {
475                         os << '\n';
476                         texrow.newline();
477                 }
478
479                 if (pit->params().pagebreakTop()) {
480                         os << "\\newpage";
481                         further_blank_line = true;
482                 }
483                 if (pit->params().spaceTop().kind() != VSpace::NONE) {
484                         os << pit->params().spaceTop().asLatexCommand(bparams);
485                         further_blank_line = true;
486                 }
487
488                 if (pit->params().lineTop()) {
489                         os << "\\lyxline{\\"
490                            << pit->getFont(bparams, 0, outerFont(pit, paragraphs)).latexSize()
491                            << '}'
492                            << "\\vspace{-1\\parskip}";
493                         further_blank_line = true;
494                 }
495
496                 if (further_blank_line) {
497                         os << '\n';
498                         texrow.newline();
499                 }
500         } else {
501                 style = bparams.getLyXTextClass().defaultLayout();
502         }
503
504         Language const * language = pit->getParLanguage(bparams);
505         Language const * doc_language = bparams.language;
506         Language const * previous_language =
507                 (pit != const_cast<ParagraphList&>(paragraphs).begin())
508                 ? boost::prior(pit)->getParLanguage(bparams)
509                 : doc_language;
510
511         if (language->babel() != previous_language->babel()
512             // check if we already put language command in TeXEnvironment()
513             && !(style->isEnvironment()
514                  && (pit == const_cast<ParagraphList&>(paragraphs).begin() ||
515                      (boost::prior(pit)->layout() != pit->layout() &&
516                       boost::prior(pit)->getDepth() <= pit->getDepth())
517                      || boost::prior(pit)->getDepth() < pit->getDepth())))
518         {
519                 if (!lyxrc.language_command_end.empty() &&
520                     previous_language->babel() != doc_language->babel())
521                 {
522                         os << subst(lyxrc.language_command_end, "$$lang",
523                                     previous_language->babel())
524                            << endl;
525                         texrow.newline();
526                 }
527
528                 if (lyxrc.language_command_end.empty() ||
529                     language->babel() != doc_language->babel())
530                 {
531                         os << subst(lyxrc.language_command_begin, "$$lang",
532                                     language->babel())
533                            << endl;
534                         texrow.newline();
535                 }
536         }
537
538         if (bparams.inputenc == "auto" &&
539             language->encoding() != previous_language->encoding()) {
540                 os << "\\inputencoding{"
541                    << language->encoding()->LatexName()
542                    << "}\n";
543                 texrow.newline();
544         }
545
546         switch (style->latextype) {
547         case LATEX_COMMAND:
548                 os << '\\' << style->latexname();
549
550                 // Separate handling of optional argument inset.
551                 if (style->optionalargs == 1) {
552                         InsetOptArg * it = optArgInset(*pit);
553                         if (it)
554                                 it->latexOptional(buf, os, runparams);
555                 }
556                 else
557                         os << style->latexparam();
558                 break;
559         case LATEX_ITEM_ENVIRONMENT:
560         case LATEX_LIST_ENVIRONMENT:
561                 os << "\\item ";
562                 break;
563         case LATEX_BIB_ENVIRONMENT:
564                 // ignore this, the inset will write itself
565                 break;
566         default:
567                 break;
568         }
569
570         os << everypar;
571         bool need_par = pit->simpleTeXOnePar(buf, bparams,
572                                              outerFont(pit, paragraphs),
573                                              os, texrow, runparams);
574
575         // Make sure that \\par is done with the font of the last
576         // character if this has another size as the default.
577         // This is necessary because LaTeX (and LyX on the screen)
578         // calculates the space between the baselines according
579         // to this font. (Matthias)
580         //
581         // Is this really needed ? (Dekel)
582         // We do not need to use to change the font for the last paragraph
583         // or for a command.
584         LyXFont const outerfont(outerFont(pit, paragraphs));
585
586         LyXFont const font =
587                 (pit->empty()
588                  ? pit->getLayoutFont(bparams, outerfont)
589                  : pit->getFont(bparams, pit->size() - 1, outerfont));
590
591         bool is_command = style->isCommand();
592
593         if (style->resfont.size() != font.size()
594             && boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()
595             && !is_command) {
596                 if (!need_par)
597                         os << '{';
598                 os << "\\" << font.latexSize() << " \\par}";
599         } else if (need_par) {
600                 os << "\\par}";
601         } else if (is_command)
602                 os << '}';
603
604         switch (style->latextype) {
605         case LATEX_ITEM_ENVIRONMENT:
606         case LATEX_LIST_ENVIRONMENT:
607                 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()
608                     && (pit->params().depth() < boost::next(pit)->params().depth())) {
609                         os << '\n';
610                         texrow.newline();
611                 }
612                 break;
613         case LATEX_ENVIRONMENT: {
614                 // if its the last paragraph of the current environment
615                 // skip it otherwise fall through
616                 ParagraphList::iterator next = boost::next(pit);
617
618                 if (next != const_cast<ParagraphList&>(paragraphs).end()
619                     && (next->layout() != pit->layout()
620                         || next->params().depth() != pit->params().depth()))
621                         break;
622         }
623
624                 // fall through possible
625         default:
626                 // we don't need it for the last paragraph!!!
627                 if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()) {
628                         os << '\n';
629                         texrow.newline();
630                 }
631         }
632
633         if ((in == 0) || !in->forceDefaultParagraphs(in)) {
634                 further_blank_line = false;
635                 if (pit->params().lineBottom()) {
636                         os << "\\lyxline{\\" << font.latexSize() << '}';
637                         further_blank_line = true;
638                 }
639
640                 if (pit->params().spaceBottom().kind() != VSpace::NONE) {
641                         os << pit->params().spaceBottom().asLatexCommand(bparams);
642                         further_blank_line = true;
643                 }
644
645                 if (pit->params().pagebreakBottom()) {
646                         os << "\\newpage";
647                         further_blank_line = true;
648                 }
649
650                 if (further_blank_line) {
651                         os << '\n';
652                         texrow.newline();
653                 }
654
655                 if (!pit->params().spacing().isDefault()
656                         && (boost::next(pit) == const_cast<ParagraphList&>(paragraphs).end()|| !boost::next(pit)->hasSameLayout(*pit))) {
657                         os << pit->params().spacing().writeEnvirEnd() << '\n';
658                         texrow.newline();
659                 }
660         }
661
662         // we don't need it for the last paragraph!!!
663         if (boost::next(pit) != const_cast<ParagraphList&>(paragraphs).end()) {
664                 os << '\n';
665                 texrow.newline();
666         } else {
667                 // Since \selectlanguage write the language to the aux file,
668                 // we need to reset the language at the end of footnote or
669                 // float.
670
671                 if (language->babel() != doc_language->babel()) {
672                         if (lyxrc.language_command_end.empty())
673                                 os << subst(lyxrc.language_command_begin,
674                                             "$$lang",
675                                             doc_language->babel())
676                                    << endl;
677                         else
678                                 os << subst(lyxrc.language_command_end,
679                                             "$$lang",
680                                             language->babel())
681                                    << endl;
682                         texrow.newline();
683                 }
684         }
685
686         lyxerr[Debug::LATEX] << "TeXOnePar...done " << &*boost::next(pit) << endl;
687         return ++pit;
688 }
689
690 } // anon namespace
691
692
693 //
694 // LaTeX all paragraphs from par to endpar, if endpar == 0 then to the end
695 //
696 void latexParagraphs(Buffer const * buf,
697                      ParagraphList const & paragraphs,
698                      ostream & os,
699                      TexRow & texrow,
700                      LatexRunParams const & runparams,
701                      string const & everypar)
702 {
703         bool was_title = false;
704         bool already_title = false;
705         LyXTextClass const & tclass = buf->params.getLyXTextClass();
706         ParagraphList::iterator par = const_cast<ParagraphList&>(paragraphs).begin();
707         ParagraphList::iterator endpar = const_cast<ParagraphList&>(paragraphs).end();
708
709         // if only_body
710         while (par != endpar) {
711                 Inset * in = par->inInset();
712                 // well we have to check if we are in an inset with unlimited
713                 // length (all in one row) if that is true then we don't allow
714                 // any special options in the paragraph and also we don't allow
715                 // any environment other then "Standard" to be valid!
716                 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
717                         LyXLayout_ptr const & layout = par->layout();
718
719                         if (layout->intitle) {
720                                 if (already_title) {
721                                         lyxerr <<"Error in latexParagraphs: You"
722                                                 " should not mix title layouts"
723                                                 " with normal ones." << endl;
724                                 } else if (!was_title) {
725                                         was_title = true;
726                                         if (tclass.titletype() == TITLE_ENVIRONMENT) {
727                                                 os << "\\begin{"
728                                                     << tclass.titlename()
729                                                     << "}\n";
730                                                 texrow.newline();
731                                         }
732                                 }
733                         } else if (was_title && !already_title) {
734                                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
735                                         os << "\\end{" << tclass.titlename()
736                                             << "}\n";
737                                 }
738                                 else {
739                                         os << "\\" << tclass.titlename()
740                                             << "\n";
741                                 }
742                                 texrow.newline();
743                                 already_title = true;
744                                 was_title = false;
745                         }
746
747                         if (layout->is_environment) {
748                                 par = TeXOnePar(buf, paragraphs, par, os, texrow,
749                                                 runparams, everypar);
750                         } else if (layout->isEnvironment() ||
751                                 !par->params().leftIndent().zero())
752                         {
753                                 par = TeXEnvironment(buf, paragraphs, par, os,
754                                                      texrow, runparams);
755                         } else {
756                                 par = TeXOnePar(buf, paragraphs, par, os, texrow,
757                                                 runparams, everypar);
758                         }
759                 } else {
760                         par = TeXOnePar(buf, paragraphs, par, os, texrow,
761                                         runparams, everypar);
762                 }
763         }
764         // It might be that we only have a title in this document
765         if (was_title && !already_title) {
766                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
767                         os << "\\end{" << tclass.titlename()
768                             << "}\n";
769                 }
770                 else {
771                         os << "\\" << tclass.titlename()
772                             << "\n";
773                                 }
774                 texrow.newline();
775         }
776 }
777
778
779 namespace {
780
781 int readParToken(Buffer & buf, Paragraph & par, LyXLex & lex, string const & token)
782 {
783         static LyXFont font;
784         static Change change;
785
786         BufferParams const & bp = buf.params;
787
788         if (token[0] != '\\') {
789                 string::const_iterator cit = token.begin();
790                 for (; cit != token.end(); ++cit) {
791                         par.insertChar(par.size(), (*cit), font, change);
792                 }
793         } else if (token == "\\layout") {
794                 lex.eatLine();
795                 string layoutname = lex.getString();
796
797                 font = LyXFont(LyXFont::ALL_INHERIT, bp.language);
798                 change = Change();
799
800                 LyXTextClass const & tclass = bp.getLyXTextClass();
801
802                 if (layoutname.empty()) {
803                         layoutname = tclass.defaultLayoutName();
804                 }
805
806                 bool hasLayout = tclass.hasLayout(layoutname);
807
808                 if (!hasLayout) {
809                         lyxerr << "Layout '" << layoutname << "' does not"
810                                << " exist in textclass '" << tclass.name()
811                                << "'." << endl;
812                         lyxerr << "Trying to use default layout instead."
813                                << endl;
814                         layoutname = tclass.defaultLayoutName();
815                 }
816
817 #ifdef USE_CAPTION
818                 // The is the compability reading of layout caption.
819                 if (compare_ascii_no_case(layoutname, "caption") == 0) {
820                         // We expect that the par we are now working on is
821                         // really inside a InsetText inside a InsetFloat.
822                         // We also know that captions can only be
823                         // one paragraph. (Lgb)
824
825                         // We should now read until the next "\layout"
826                         // is reached.
827                         // This is probably not good enough, what if the
828                         // caption is the last par in the document (Lgb)
829                         istream & ist = lex.getStream();
830                         stringstream ss;
831                         string line;
832                         int begin = 0;
833                         while (true) {
834                                 getline(ist, line);
835                                 if (prefixIs(line, "\\layout")) {
836                                         lex.pushToken(line);
837                                         break;
838                                 }
839                                 if (prefixIs(line, "\\begin_inset"))
840                                         ++begin;
841                                 if (prefixIs(line, "\\end_inset")) {
842                                         if (begin)
843                                                 --begin;
844                                         else {
845                                                 lex.pushToken(line);
846                                                 break;
847                                         }
848                                 }
849
850                                 ss << line << '\n';
851                         }
852
853                         // Now we should have the whole layout in ss
854                         // we should now be able to give this to the
855                         // caption inset.
856                         ss << "\\end_inset\n";
857
858                         // This seems like a bug in stringstream.
859                         // We really should be able to use ss
860                         // directly. (Lgb)
861                         istringstream is(ss.str());
862                         LyXLex tmplex(0, 0);
863                         tmplex.setStream(is);
864                         Inset * inset = new InsetCaption;
865                         inset->Read(this, tmplex);
866                         par.insertInset(pos, inset, font);
867                         ++pos;
868                 } else {
869 #endif
870                         par.layout(bp.getLyXTextClass()[layoutname]);
871
872                         // Test whether the layout is obsolete.
873                         LyXLayout_ptr const & layout = par.layout();
874                         if (!layout->obsoleted_by().empty())
875                                 par.layout(bp.getLyXTextClass()[layout->obsoleted_by()]);
876
877                         par.params().read(lex);
878 #if USE_CAPTION
879                 }
880 #endif
881
882         } else if (token == "\\end_inset") {
883                 lyxerr << "Solitary \\end_inset in line " << lex.getLineNo() << "\n"
884                        << "Missing \\begin_inset?.\n";
885                 // Simply ignore this. The insets do not have
886                 // to read this.
887                 // But insets should read it, it is a part of
888                 // the inset isn't it? Lgb.
889         } else if (token == "\\begin_inset") {
890                 Inset * i = readInset(lex, buf);
891                 par.insertInset(par.size(), i, font, change);
892         } else if (token == "\\family") {
893                 lex.next();
894                 font.setLyXFamily(lex.getString());
895         } else if (token == "\\series") {
896                 lex.next();
897                 font.setLyXSeries(lex.getString());
898         } else if (token == "\\shape") {
899                 lex.next();
900                 font.setLyXShape(lex.getString());
901         } else if (token == "\\size") {
902                 lex.next();
903                 font.setLyXSize(lex.getString());
904         } else if (token == "\\lang") {
905                 lex.next();
906                 string const tok = lex.getString();
907                 Language const * lang = languages.getLanguage(tok);
908                 if (lang) {
909                         font.setLanguage(lang);
910                 } else {
911                         font.setLanguage(bp.language);
912                         lex.printError("Unknown language `$$Token'");
913                 }
914         } else if (token == "\\numeric") {
915                 lex.next();
916                 font.setNumber(font.setLyXMisc(lex.getString()));
917         } else if (token == "\\emph") {
918                 lex.next();
919                 font.setEmph(font.setLyXMisc(lex.getString()));
920         } else if (token == "\\bar") {
921                 lex.next();
922                 string const tok = lex.getString();
923
924                 if (tok == "under")
925                         font.setUnderbar(LyXFont::ON);
926                 else if (tok == "no")
927                         font.setUnderbar(LyXFont::OFF);
928                 else if (tok == "default")
929                         font.setUnderbar(LyXFont::INHERIT);
930                 else
931                         lex.printError("Unknown bar font flag "
932                                        "`$$Token'");
933         } else if (token == "\\noun") {
934                 lex.next();
935                 font.setNoun(font.setLyXMisc(lex.getString()));
936         } else if (token == "\\color") {
937                 lex.next();
938                 font.setLyXColor(lex.getString());
939         } else if (token == "\\InsetSpace" || token == "\\SpecialChar") {
940
941                 // Insets don't make sense in a free-spacing context! ---Kayvan
942                 if (par.isFreeSpacing()) {
943                         if (token == "\\InsetSpace")
944                                 par.insertChar(par.size(), ' ', font, change);
945                         else if (lex.isOK()) {
946                                 lex.next();
947                                 string const next_token = lex.getString();
948                                 if (next_token == "\\-")
949                                         par.insertChar(par.size(), '-', font, change);
950                                 else {
951                                         lex.printError("Token `$$Token' "
952                                                        "is in free space "
953                                                        "paragraph layout!");
954                                 }
955                         }
956                 } else {
957                         Inset * inset = 0;
958                         if (token == "\\SpecialChar" )
959                                 inset = new InsetSpecialChar;
960                         else
961                                 inset = new InsetSpace;
962                         inset->read(&buf, lex);
963                         par.insertInset(par.size(), inset, font, change);
964                 }
965         } else if (token == "\\i") {
966                 Inset * inset = new InsetLatexAccent;
967                 inset->read(&buf, lex);
968                 par.insertInset(par.size(), inset, font, change);
969         } else if (token == "\\backslash") {
970                 par.insertChar(par.size(), '\\', font, change);
971         } else if (token == "\\newline") {
972                 Inset * inset = new InsetNewline;
973                 inset->read(&buf, lex);
974                 par.insertInset(par.size(), inset, font, change);
975         } else if (token == "\\LyXTable") {
976                 Inset * inset = new InsetTabular(buf);
977                 inset->read(&buf, lex);
978                 par.insertInset(par.size(), inset, font, change);
979         } else if (token == "\\bibitem") {
980                 InsetCommandParams p("bibitem", "dummy");
981                 InsetBibitem * inset = new InsetBibitem(p);
982                 inset->read(&buf, lex);
983                 par.insertInset(par.size(), inset, font, change);
984         } else if (token == "\\hfill") {
985                 par.insertInset(par.size(), new InsetHFill(), font, change);
986         } else if (token == "\\change_unchanged") {
987                 // Hack ! Needed for empty paragraphs :/
988                 // FIXME: is it still ??
989                 if (!par.size())
990                         par.cleanChanges();
991                 change = Change(Change::UNCHANGED);
992         } else if (token == "\\change_inserted") {
993                 lex.nextToken();
994                 istringstream is(STRCONV(lex.getString()));
995                 int aid;
996                 lyx::time_type ct;
997                 is >> aid >> ct;
998                 change = Change(Change::INSERTED, bp.author_map[aid], ct);
999         } else if (token == "\\change_deleted") {
1000                 lex.nextToken();
1001                 istringstream is(STRCONV(lex.getString()));
1002                 int aid;
1003                 lyx::time_type ct;
1004                 is >> aid >> ct;
1005                 change = Change(Change::DELETED, bp.author_map[aid], ct);
1006         } else {
1007                 lex.eatLine();
1008                 string const s = bformat(_("Unknown token: %1$s %2$s\n"),
1009                         token, lex.getString());
1010                 // we can do this here this way because we're actually reading
1011                 // the buffer and don't care about LyXText right now.
1012                 par.insertInset(par.size(), new InsetError(s), font);
1013                 return 1;
1014         }
1015         return 0;
1016 }
1017
1018 }
1019
1020
1021 int readParagraph(Buffer & buf, Paragraph & par, LyXLex & lex)
1022 {
1023         int unknown = 0;
1024
1025         lex.nextToken();
1026         string token = lex.getString();
1027
1028         while (lex.isOK()) {
1029
1030                 unknown += readParToken(buf, par, lex, token);
1031
1032                 lex.nextToken();
1033                 token = lex.getString();
1034
1035                 if (token.empty())
1036                         continue;
1037
1038                 lyxerr[Debug::PARSER] << "Handling paragraph token: `"
1039                                       << token << '\'' << endl;
1040
1041                 // reached the next paragraph. FIXME: really we should
1042                 // change the file format to indicate the end of a par
1043                 // clearly, but for now, this hack will do
1044                 if (token == "\\layout" || token == "\\the_end"
1045                     || token == "\\end_inset" || token == "\\begin_deeper"
1046                     || token == "\\end_deeper") {
1047                         lex.pushToken(token);
1048                         break;
1049                 }
1050         }
1051
1052         return unknown;
1053 }
1054
1055
1056 LyXFont const outerFont(ParagraphList::iterator pit,
1057                         ParagraphList const & plist)
1058 {
1059         Paragraph::depth_type par_depth = pit->getDepth();
1060         LyXFont tmpfont(LyXFont::ALL_INHERIT);
1061
1062         // Resolve against environment font information
1063         while (pit != const_cast<ParagraphList&>(plist).end() &&
1064                par_depth && !tmpfont.resolved()) {
1065                 pit = outerHook(pit, plist);
1066                 if (pit != const_cast<ParagraphList&>(plist).end()) {
1067                         tmpfont.realize(pit->layout()->font);
1068                         par_depth = pit->getDepth();
1069                 }
1070         }
1071
1072         return tmpfont;
1073 }
1074
1075
1076 LyXFont const realizeFont(LyXFont const & font,
1077                           BufferParams const & params)
1078 {
1079         LyXTextClass const & tclass = params.getLyXTextClass();
1080         LyXFont tmpfont(font);
1081         tmpfont.realize(tclass.defaultfont());
1082         return tmpfont;
1083 }