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