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