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