]> git.lyx.org Git - lyx.git/blob - src/paragraph_funcs.C
8fc450284698137aad8c61699ac963b6389c2f48
[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 ParagraphList::iterator
266 TeXDeeper(Buffer const * buf,
267           BufferParams const & bparams,
268           ParagraphList const & paragraphs,
269           ParagraphList::iterator pit,
270           ostream & os, TexRow & texrow)
271 {
272         lyxerr[Debug::LATEX] << "TeXDeeper...     " << &*pit << endl;
273         ParagraphList::iterator par = pit;
274
275         while (par != paragraphs.end()&& par->params().depth() == pit->params().depth()) {
276                 if (par->layout()->isEnvironment()) {
277                         par = TeXEnvironment(buf, bparams, paragraphs, par,
278                                                   os, texrow);
279                 } else {
280                         par = TeXOnePar(buf, bparams, paragraphs, par,
281                                              os, texrow, false);
282                 }
283         }
284         lyxerr[Debug::LATEX] << "TeXDeeper...done " << &*par << endl;
285
286         return par;
287 }
288
289
290 ParagraphList::iterator
291 TeXEnvironment(Buffer const * buf,
292                BufferParams const & bparams,
293                ParagraphList const & paragraphs,
294                ParagraphList::iterator pit,
295                ostream & os, TexRow & texrow)
296 {
297         lyxerr[Debug::LATEX] << "TeXEnvironment...     " << &*pit << endl;
298
299         LyXLayout_ptr const & style = pit->layout();
300
301         Language const * language = pit->getParLanguage(bparams);
302         Language const * doc_language = bparams.language;
303         Language const * previous_language =
304                 (pit != paragraphs.begin())
305                 ? boost::prior(pit)->getParLanguage(bparams)
306                 : doc_language;
307         if (language->babel() != previous_language->babel()) {
308
309                 if (!lyxrc.language_command_end.empty() &&
310                     previous_language->babel() != doc_language->babel()) {
311                         os << subst(lyxrc.language_command_end, "$$lang",
312                                     previous_language->babel())
313                            << endl;
314                         texrow.newline();
315                 }
316
317                 if (lyxrc.language_command_end.empty() ||
318                     language->babel() != doc_language->babel()) {
319                         os << subst(lyxrc.language_command_begin, "$$lang",
320                                     language->babel())
321                            << endl;
322                         texrow.newline();
323                 }
324         }
325
326         bool leftindent_open = false;
327         if (!pit->params().leftIndent().zero()) {
328                 os << "\\begin{LyXParagraphLeftIndent}{" <<
329                         pit->params().leftIndent().asLatexString() << "}\n";
330                 texrow.newline();
331                 leftindent_open = true;
332         }
333
334         if (style->isEnvironment()) {
335                 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
336                         os << "\\begin{" << style->latexname() << "}{"
337                            << pit->params().labelWidthString() << "}\n";
338                 } else if (style->labeltype == LABEL_BIBLIO) {
339                         // ale970405
340                         os << "\\begin{" << style->latexname() << "}{"
341                            <<  bibitemWidest(buf)
342                            << "}\n";
343                 } else if (style->latextype == LATEX_ITEM_ENVIRONMENT) {
344                         os << "\\begin{" << style->latexname() << '}'
345                            << style->latexparam() << '\n';
346                 } else
347                         os << "\\begin{" << style->latexname() << '}'
348                            << style->latexparam() << '\n';
349                 texrow.newline();
350         }
351         ParagraphList::iterator par = pit;
352         do {
353                 par = TeXOnePar(buf, bparams, paragraphs, par, os, texrow, false);
354
355                 if (par != paragraphs.end()&& par->params().depth() > pit->params().depth()) {
356                             if (par->layout()->isParagraph()) {
357
358                             // Thinko!
359                             // How to handle this? (Lgb)
360                             //&& !suffixIs(os, "\n\n")
361                                     //) {
362                                 // There should be at least one '\n' already
363                                 // but we need there to be two for Standard
364                                 // paragraphs that are depth-increment'ed to be
365                                 // output correctly.  However, tables can
366                                 // also be paragraphs so don't adjust them.
367                                 // ARRae
368                                 // Thinkee:
369                                 // Will it ever harm to have one '\n' too
370                                 // many? i.e. that we sometimes will have
371                                 // three in a row. (Lgb)
372                                 os << '\n';
373                                 texrow.newline();
374                         }
375                         par = TeXDeeper(buf, bparams, paragraphs, par, os, texrow);
376                 }
377         } while (par != paragraphs.end()
378                  && par->layout() == pit->layout()
379                  && par->params().depth() == pit->params().depth()
380                  && par->params().leftIndent() == pit->params().leftIndent());
381
382         if (style->isEnvironment()) {
383                 os << "\\end{" << style->latexname() << "}\n";
384                 texrow.newline();
385         }
386
387         if (leftindent_open) {
388                 os << "\\end{LyXParagraphLeftIndent}\n";
389                 texrow.newline();
390         }
391
392         lyxerr[Debug::LATEX] << "TeXEnvironment...done " << &*par << endl;
393         return par;  // ale970302
394 }
395
396
397 namespace {
398
399 InsetOptArg * optArgInset(Paragraph const & par)
400 {
401         // Find the entry.
402         InsetList::iterator it = par.insetlist.begin();
403         InsetList::iterator end = par.insetlist.end();
404         for (; it != end; ++it) {
405                 Inset * ins = it.getInset();
406                 if (ins->lyxCode() == Inset::OPTARG_CODE) {
407                         return static_cast<InsetOptArg *>(ins);
408                 }
409         }
410         return 0;
411 }
412
413 } // end namespace
414
415
416 ParagraphList::iterator
417 TeXOnePar(Buffer const * buf,
418           BufferParams const & bparams,
419           ParagraphList const & paragraphs,
420           ParagraphList::iterator pit,
421           ostream & os, TexRow & texrow,
422           bool moving_arg)
423 {
424         lyxerr[Debug::LATEX] << "TeXOnePar...     " << &*pit << endl;
425         Inset const * in = pit->inInset();
426         bool further_blank_line = false;
427         LyXLayout_ptr style;
428
429         // well we have to check if we are in an inset with unlimited
430         // lenght (all in one row) if that is true then we don't allow
431         // any special options in the paragraph and also we don't allow
432         // any environment other then "Standard" to be valid!
433         if ((in == 0) || !in->forceDefaultParagraphs(in)) {
434                 style = pit->layout();
435
436                 if (pit->params().startOfAppendix()) {
437                         os << "\\appendix\n";
438                         texrow.newline();
439                 }
440
441                 if (!pit->params().spacing().isDefault()
442                         && (pit == paragraphs.begin() || !boost::prior(pit)->hasSameLayout(&*pit))) {
443                         os << pit->params().spacing().writeEnvirBegin() << '\n';
444                         texrow.newline();
445                 }
446
447                 if (style->isCommand()) {
448                         os << '\n';
449                         texrow.newline();
450                 }
451
452                 if (pit->params().pagebreakTop()) {
453                         os << "\\newpage";
454                         further_blank_line = true;
455                 }
456                 if (pit->params().spaceTop().kind() != VSpace::NONE) {
457                         os << pit->params().spaceTop().asLatexCommand(bparams);
458                         further_blank_line = true;
459                 }
460
461                 if (pit->params().lineTop()) {
462                         os << "\\lyxline{\\" << pit->getFont(bparams, 0).latexSize() << '}'
463                            << "\\vspace{-1\\parskip}";
464                         further_blank_line = true;
465                 }
466
467                 if (further_blank_line) {
468                         os << '\n';
469                         texrow.newline();
470                 }
471         } else {
472                 style = bparams.getLyXTextClass().defaultLayout();
473         }
474
475         Language const * language = pit->getParLanguage(bparams);
476         Language const * doc_language = bparams.language;
477         Language const * previous_language =
478                 (pit != paragraphs.begin())
479                 ? boost::prior(pit)->getParLanguage(bparams)
480                 : doc_language;
481
482         if (language->babel() != previous_language->babel()
483             // check if we already put language command in TeXEnvironment()
484             && !(style->isEnvironment()
485                  && (pit == paragraphs.begin() ||
486                      (boost::prior(pit)->layout() != pit->layout() &&
487                       boost::prior(pit)->getDepth() <= pit->getDepth())
488                      || boost::prior(pit)->getDepth() < pit->getDepth())))
489         {
490                 if (!lyxrc.language_command_end.empty() &&
491                     previous_language->babel() != doc_language->babel())
492                 {
493                         os << subst(lyxrc.language_command_end, "$$lang",
494                                     previous_language->babel())
495                            << endl;
496                         texrow.newline();
497                 }
498
499                 if (lyxrc.language_command_end.empty() ||
500                     language->babel() != doc_language->babel())
501                 {
502                         os << subst(lyxrc.language_command_begin, "$$lang",
503                                     language->babel())
504                            << endl;
505                         texrow.newline();
506                 }
507         }
508
509         if (bparams.inputenc == "auto" &&
510             language->encoding() != previous_language->encoding()) {
511                 os << "\\inputencoding{"
512                    << language->encoding()->LatexName()
513                    << "}\n";
514                 texrow.newline();
515         }
516
517         switch (style->latextype) {
518         case LATEX_COMMAND:
519                 os << '\\' << style->latexname();
520
521                 // Separate handling of optional argument inset.
522                 if (style->optionalargs == 1) {
523                         InsetOptArg * it = optArgInset(*pit);
524                         if (it)
525                                 it->latexOptional(buf, os, false, false);
526                 }
527                 else
528                         os << style->latexparam();
529                 break;
530         case LATEX_ITEM_ENVIRONMENT:
531         case LATEX_LIST_ENVIRONMENT:
532                 os << "\\item ";
533                 break;
534         case LATEX_BIB_ENVIRONMENT:
535                 // ignore this, the inset will write itself
536                 break;
537         default:
538                 break;
539         }
540
541         bool need_par = pit->simpleTeXOnePar(buf, bparams, os, texrow, moving_arg);
542
543         // Make sure that \\par is done with the font of the last
544         // character if this has another size as the default.
545         // This is necessary because LaTeX (and LyX on the screen)
546         // calculates the space between the baselines according
547         // to this font. (Matthias)
548         //
549         // Is this really needed ? (Dekel)
550         // We do not need to use to change the font for the last paragraph
551         // or for a command.
552         LyXFont const font =
553                 (pit->empty()
554                  ? pit->getLayoutFont(bparams) : pit->getFont(bparams, pit->size() - 1));
555
556         bool is_command = style->isCommand();
557
558         if (style->resfont.size() != font.size()
559             && boost::next(pit) != paragraphs.end()
560             && !is_command) {
561                 if (!need_par)
562                         os << '{';
563                 os << "\\" << font.latexSize() << " \\par}";
564         } else if (need_par) {
565                 os << "\\par}";
566         } else if (is_command)
567                 os << '}';
568
569         switch (style->latextype) {
570         case LATEX_ITEM_ENVIRONMENT:
571         case LATEX_LIST_ENVIRONMENT:
572                 if (boost::next(pit) != paragraphs.end()
573                     && (pit->params().depth() < boost::next(pit)->params().depth())) {
574                         os << '\n';
575                         texrow.newline();
576                 }
577                 break;
578         case LATEX_ENVIRONMENT:
579                 // if its the last paragraph of the current environment
580                 // skip it otherwise fall through
581                 if (boost::next(pit) != paragraphs.end()
582                     && (boost::next(pit)->layout() != pit->layout()
583                         || boost::next(pit)->params().depth() != pit->params().depth()))
584                         break;
585                 // fall through possible
586         default:
587                 // we don't need it for the last paragraph!!!
588                 if (boost::next(pit) != paragraphs.end()) {
589                         os << '\n';
590                         texrow.newline();
591                 }
592         }
593
594         if ((in == 0) || !in->forceDefaultParagraphs(in)) {
595                 further_blank_line = false;
596                 if (pit->params().lineBottom()) {
597                         os << "\\lyxline{\\" << font.latexSize() << '}';
598                         further_blank_line = true;
599                 }
600
601                 if (pit->params().spaceBottom().kind() != VSpace::NONE) {
602                         os << pit->params().spaceBottom().asLatexCommand(bparams);
603                         further_blank_line = true;
604                 }
605
606                 if (pit->params().pagebreakBottom()) {
607                         os << "\\newpage";
608                         further_blank_line = true;
609                 }
610
611                 if (further_blank_line) {
612                         os << '\n';
613                         texrow.newline();
614                 }
615
616                 if (!pit->params().spacing().isDefault()
617                         && (boost::next(pit) == paragraphs.end()|| !boost::next(pit)->hasSameLayout(&*pit))) {
618                         os << pit->params().spacing().writeEnvirEnd() << '\n';
619                         texrow.newline();
620                 }
621         }
622
623         // we don't need it for the last paragraph!!!
624         if (boost::next(pit) != paragraphs.end()) {
625                 os << '\n';
626                 texrow.newline();
627         } else {
628                 // Since \selectlanguage write the language to the aux file,
629                 // we need to reset the language at the end of footnote or
630                 // float.
631
632                 if (language->babel() != doc_language->babel()) {
633                         if (lyxrc.language_command_end.empty())
634                                 os << subst(lyxrc.language_command_begin,
635                                             "$$lang",
636                                             doc_language->babel())
637                                    << endl;
638                         else
639                                 os << subst(lyxrc.language_command_end,
640                                             "$$lang",
641                                             language->babel())
642                                    << endl;
643                         texrow.newline();
644                 }
645         }
646
647         lyxerr[Debug::LATEX] << "TeXOnePar...done " << &*boost::next(pit) << endl;
648         return ++pit;
649 }
650
651
652 //
653 // LaTeX all paragraphs from par to endpar, if endpar == 0 then to the end
654 //
655 void latexParagraphs(Buffer const * buf,
656                      ParagraphList const & paragraphs,
657                      ParagraphList::iterator par,
658                      ParagraphList::iterator endpar,
659                      ostream & ofs,
660                      TexRow & texrow,
661                      bool moving_arg)
662 {
663         bool was_title = false;
664         bool already_title = false;
665         LyXTextClass const & tclass = buf->params.getLyXTextClass();
666
667         // if only_body
668         while (par != endpar) {
669                 Inset * in = par->inInset();
670                 // well we have to check if we are in an inset with unlimited
671                 // length (all in one row) if that is true then we don't allow
672                 // any special options in the paragraph and also we don't allow
673                 // any environment other then "Standard" to be valid!
674                 if ((in == 0) || !in->forceDefaultParagraphs(in)) {
675                         LyXLayout_ptr const & layout = par->layout();
676
677                         if (layout->intitle) {
678                                 if (already_title) {
679                                         lyxerr <<"Error in latexParagraphs: You"
680                                                 " should not mix title layouts"
681                                                 " with normal ones." << endl;
682                                 } else if (!was_title) {
683                                         was_title = true;
684                                         if (tclass.titletype() == TITLE_ENVIRONMENT) {
685                                                 ofs << "\\begin{"
686                                                     << tclass.titlename()
687                                                     << "}\n";
688                                                 texrow.newline();
689                                         }
690                                 }
691                         } else if (was_title && !already_title) {
692                                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
693                                         ofs << "\\end{" << tclass.titlename()
694                                             << "}\n";
695                                 }
696                                 else {
697                                         ofs << "\\" << tclass.titlename()
698                                             << "\n";
699                                 }
700                                 texrow.newline();
701                                 already_title = true;
702                                 was_title = false;
703                         }
704
705                         if (layout->isEnvironment() ||
706                                 !par->params().leftIndent().zero())
707                         {
708                                 par = TeXEnvironment(buf, buf->params, paragraphs, par, ofs, texrow);
709                         } else {
710                                 par = TeXOnePar(buf, buf->params, paragraphs, par, ofs, texrow, moving_arg);
711                         }
712                 } else {
713                         par = TeXOnePar(buf, buf->params, paragraphs, par, ofs, texrow, moving_arg);
714                 }
715         }
716         // It might be that we only have a title in this document
717         if (was_title && !already_title) {
718                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
719                         ofs << "\\end{" << tclass.titlename()
720                             << "}\n";
721                 }
722                 else {
723                         ofs << "\\" << tclass.titlename()
724                             << "\n";
725                                 }
726                 texrow.newline();
727         }
728 }
729
730
731 namespace {
732
733 int readParToken(Buffer & buf, Paragraph & par, LyXLex & lex, string const & token)
734 {
735         static LyXFont font;
736         static Change change;
737
738         BufferParams const & bp = buf.params;
739
740         if (token[0] != '\\') {
741                 string::const_iterator cit = token.begin();
742                 for (; cit != token.end(); ++cit) {
743                         par.insertChar(par.size(), (*cit), font, change);
744                 }
745         } else if (token == "\\layout") {
746                 lex.eatLine();
747                 string layoutname = lex.getString();
748
749                 font = LyXFont(LyXFont::ALL_INHERIT, bp.language);
750                 change = Change();
751
752                 LyXTextClass const & tclass = bp.getLyXTextClass();
753
754                 if (layoutname.empty()) {
755                         layoutname = tclass.defaultLayoutName();
756                 }
757
758                 bool hasLayout = tclass.hasLayout(layoutname);
759
760                 if (!hasLayout) {
761                         lyxerr << "Layout '" << layoutname << "' does not"
762                                << " exist in textclass '" << tclass.name()
763                                << "'." << endl;
764                         lyxerr << "Trying to use default layout instead."
765                                << endl;
766                         layoutname = tclass.defaultLayoutName();
767                 }
768
769 #ifdef USE_CAPTION
770                 // The is the compability reading of layout caption.
771                 if (compare_ascii_no_case(layoutname, "caption") == 0) {
772                         // We expect that the par we are now working on is
773                         // really inside a InsetText inside a InsetFloat.
774                         // We also know that captions can only be
775                         // one paragraph. (Lgb)
776
777                         // We should now read until the next "\layout"
778                         // is reached.
779                         // This is probably not good enough, what if the
780                         // caption is the last par in the document (Lgb)
781                         istream & ist = lex.getStream();
782                         stringstream ss;
783                         string line;
784                         int begin = 0;
785                         while (true) {
786                                 getline(ist, line);
787                                 if (prefixIs(line, "\\layout")) {
788                                         lex.pushToken(line);
789                                         break;
790                                 }
791                                 if (prefixIs(line, "\\begin_inset"))
792                                         ++begin;
793                                 if (prefixIs(line, "\\end_inset")) {
794                                         if (begin)
795                                                 --begin;
796                                         else {
797                                                 lex.pushToken(line);
798                                                 break;
799                                         }
800                                 }
801
802                                 ss << line << '\n';
803                         }
804
805                         // Now we should have the whole layout in ss
806                         // we should now be able to give this to the
807                         // caption inset.
808                         ss << "\\end_inset\n";
809
810                         // This seems like a bug in stringstream.
811                         // We really should be able to use ss
812                         // directly. (Lgb)
813                         istringstream is(ss.str());
814                         LyXLex tmplex(0, 0);
815                         tmplex.setStream(is);
816                         Inset * inset = new InsetCaption;
817                         inset->Read(this, tmplex);
818                         par.insertInset(pos, inset, font);
819                         ++pos;
820                 } else {
821 #endif
822                         par.layout(bp.getLyXTextClass()[layoutname]);
823
824                         // Test whether the layout is obsolete.
825                         LyXLayout_ptr const & layout = par.layout();
826                         if (!layout->obsoleted_by().empty())
827                                 par.layout(bp.getLyXTextClass()[layout->obsoleted_by()]);
828
829                         par.params().read(lex);
830 #if USE_CAPTION
831                 }
832 #endif
833
834         } else if (token == "\\end_inset") {
835                 lyxerr << "Solitary \\end_inset in line " << lex.getLineNo() << "\n"
836                        << "Missing \\begin_inset?.\n";
837                 // Simply ignore this. The insets do not have
838                 // to read this.
839                 // But insets should read it, it is a part of
840                 // the inset isn't it? Lgb.
841         } else if (token == "\\begin_inset") {
842                 Inset * i = readInset(lex, buf);
843                 par.insertInset(par.size(), i, font, change);
844         } else if (token == "\\family") {
845                 lex.next();
846                 font.setLyXFamily(lex.getString());
847         } else if (token == "\\series") {
848                 lex.next();
849                 font.setLyXSeries(lex.getString());
850         } else if (token == "\\shape") {
851                 lex.next();
852                 font.setLyXShape(lex.getString());
853         } else if (token == "\\size") {
854                 lex.next();
855                 font.setLyXSize(lex.getString());
856         } else if (token == "\\lang") {
857                 lex.next();
858                 string const tok = lex.getString();
859                 Language const * lang = languages.getLanguage(tok);
860                 if (lang) {
861                         font.setLanguage(lang);
862                 } else {
863                         font.setLanguage(bp.language);
864                         lex.printError("Unknown language `$$Token'");
865                 }
866         } else if (token == "\\numeric") {
867                 lex.next();
868                 font.setNumber(font.setLyXMisc(lex.getString()));
869         } else if (token == "\\emph") {
870                 lex.next();
871                 font.setEmph(font.setLyXMisc(lex.getString()));
872         } else if (token == "\\bar") {
873                 lex.next();
874                 string const tok = lex.getString();
875
876                 if (tok == "under")
877                         font.setUnderbar(LyXFont::ON);
878                 else if (tok == "no")
879                         font.setUnderbar(LyXFont::OFF);
880                 else if (tok == "default")
881                         font.setUnderbar(LyXFont::INHERIT);
882                 else
883                         lex.printError("Unknown bar font flag "
884                                        "`$$Token'");
885         } else if (token == "\\noun") {
886                 lex.next();
887                 font.setNoun(font.setLyXMisc(lex.getString()));
888         } else if (token == "\\color") {
889                 lex.next();
890                 font.setLyXColor(lex.getString());
891         } else if (token == "\\SpecialChar") {
892                 LyXLayout_ptr const & layout = par.layout();
893
894                 // Insets don't make sense in a free-spacing context! ---Kayvan
895                 if (layout->free_spacing || par.isFreeSpacing()) {
896                         if (lex.isOK()) {
897                                 lex.next();
898                                 string const next_token = lex.getString();
899                                 if (next_token == "\\-") {
900                                         par.insertChar(par.size(), '-', font, change);
901                                 } else if (next_token == "~") {
902                                         par.insertChar(par.size(), ' ', font, change);
903                                 } else {
904                                         lex.printError("Token `$$Token' "
905                                                        "is in free space "
906                                                        "paragraph layout!");
907                                 }
908                         }
909                 } else {
910                         Inset * inset = new InsetSpecialChar;
911                         inset->read(&buf, lex);
912                         par.insertInset(par.size(), inset, font, change);
913                 }
914         } else if (token == "\\i") {
915                 Inset * inset = new InsetLatexAccent;
916                 inset->read(&buf, lex);
917                 par.insertInset(par.size(), inset, font, change);
918         } else if (token == "\\backslash") {
919                 par.insertChar(par.size(), '\\', font, change);
920         } else if (token == "\\newline") {
921                 Inset * inset = new InsetNewline;
922                 inset->read(&buf, lex);
923                 par.insertInset(par.size(), inset, font, change);
924         } else if (token == "\\LyXTable") {
925                 Inset * inset = new InsetTabular(buf);
926                 inset->read(&buf, lex);
927                 par.insertInset(par.size(), inset, font, change);
928         } else if (token == "\\bibitem") {
929                 InsetCommandParams p("bibitem", "dummy");
930                 InsetBibitem * inset = new InsetBibitem(p);
931                 inset->read(&buf, lex);
932                 par.insertInset(par.size(), inset, font, change);
933         } else if (token == "\\hfill") {
934                 par.insertInset(par.size(), new InsetHFill(), font, change);
935         } else if (token == "\\change_unchanged") {
936                 // Hack ! Needed for empty paragraphs :/
937                 // FIXME: is it still ??
938                 if (!par.size())
939                         par.cleanChanges();
940                 change = Change(Change::UNCHANGED);
941         } else if (token == "\\change_inserted") {
942                 lex.nextToken();
943                 istringstream istr(lex.getString());
944                 int aid;
945                 lyx::time_type ct;
946                 istr >> aid;
947                 istr >> ct;
948                 change = Change(Change::INSERTED, aid, ct);
949         } else if (token == "\\change_deleted") {
950                 lex.nextToken();
951                 istringstream istr(lex.getString());
952                 int aid;
953                 lyx::time_type ct;
954                 istr >> aid;
955                 istr >> ct;
956                 change = Change(Change::DELETED, aid, ct);
957         } else {
958                 lex.eatLine();
959 #if USE_BOOST_FORMAT
960                 boost::format fmt(_("Unknown token: %1$s %2$s\n"));
961                 fmt % token % lex.text();
962                 string const s = fmt.str();
963 #else
964                 string const s = _("Unknown token: ") + token
965                         + ' ' + lex.text() + '\n';
966 #endif
967                 // we can do this here this way because we're actually reading
968                 // the buffer and don't care about LyXText right now.
969                 InsetError * inset = new InsetError(s);
970                 par.insertInset(par.size(), inset, font);
971                 return 1;
972         }
973         return 0;
974 }
975
976 }
977
978
979 int readParagraph(Buffer & buf, Paragraph & par, LyXLex & lex)
980 {
981         int unknown = 0;
982
983         lex.nextToken();
984         string token = lex.getString();
985
986         while (lex.isOK()) {
987
988                 unknown += readParToken(buf, par, lex, token);
989
990                 lex.nextToken();
991                 token = lex.getString();
992
993                 if (token.empty())
994                         continue;
995
996                 lyxerr[Debug::PARSER] << "Handling paragraph token: `"
997                                       << token << '\'' << endl;
998
999                 // reached the next paragraph. FIXME: really we should
1000                 // change the file format to indicate the end of a par
1001                 // clearly, but for now, this hack will do
1002                 if (token == "\\layout" || token == "\\the_end"
1003                     || token == "\\end_inset" || token == "\\begin_deeper"
1004                     || token == "\\end_deeper") {
1005                         lex.pushToken(token);
1006                         break;
1007                 }
1008         }
1009
1010         return unknown;
1011 }