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