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