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