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