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