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