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.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
13 #include "paragraph_funcs.h"
16 #include "bufferparams.h"
20 #include "errorlist.h"
23 #include "iterators.h"
27 #include "outputparams.h"
28 #include "paragraph_pimpl.h"
33 #include "insets/insetbibitem.h"
34 #include "insets/insethfill.h"
35 #include "insets/insetlatexaccent.h"
36 #include "insets/insetline.h"
37 #include "insets/insetnewline.h"
38 #include "insets/insetpagebreak.h"
39 #include "insets/insetoptarg.h"
40 #include "insets/insetspace.h"
41 #include "insets/insetspecialchar.h"
42 #include "insets/insettabular.h"
44 #include "support/filetools.h"
45 #include "support/lstrings.h"
46 #include "support/lyxlib.h"
47 #include "support/std_sstream.h"
53 using lyx::support::ascii_lowercase;
54 using lyx::support::atoi;
55 using lyx::support::bformat;
56 using lyx::support::compare_ascii_no_case;
57 using lyx::support::compare_no_case;
58 using lyx::support::contains;
59 using lyx::support::split;
60 using lyx::support::subst;
66 using std::istringstream;
73 bool moveItem(Paragraph & from, Paragraph & to,
74 BufferParams const & params, pos_type i, pos_type j)
76 char const tmpchar = from.getChar(i);
77 LyXFont tmpfont = from.getFontSettings(params, i);
79 if (tmpchar == Paragraph::META_INSET) {
80 InsetOld * tmpinset = 0;
81 if (from.getInset(i)) {
82 // the inset is not in a paragraph anymore
83 tmpinset = from.insetlist.release(i);
86 if (!to.insetAllowed(tmpinset->lyxCode()))
88 to.insertInset(j, tmpinset, tmpfont);
90 if (!to.checkInsertChar(tmpfont))
92 to.insertChar(j, tmpchar, tmpfont);
100 void breakParagraph(BufferParams const & bparams,
101 ParagraphList & paragraphs,
102 ParagraphList::iterator par,
106 // create a new paragraph, and insert into the list
107 ParagraphList::iterator tmp = paragraphs.insert(boost::next(par),
110 // without doing that we get a crash when typing <Return> at the
111 // end of a paragraph
112 tmp->layout(bparams.getLyXTextClass().defaultLayout());
113 // remember to set the inset_owner
114 tmp->setInsetOwner(par->inInset());
116 if (bparams.tracking_changes)
119 // this is an idea for a more userfriendly layout handling, I will
120 // see what the users say
122 // layout stays the same with latex-environments
124 tmp->layout(par->layout());
125 tmp->setLabelWidthString(par->params().labelWidthString());
128 bool const isempty = (par->allowEmpty() && par->empty());
130 if (!isempty && (par->size() > pos || par->empty() || flag == 2)) {
131 tmp->layout(par->layout());
132 tmp->params().align(par->params().align());
133 tmp->setLabelWidthString(par->params().labelWidthString());
135 tmp->params().spaceBottom(par->params().spaceBottom());
136 par->params().spaceBottom(VSpace(VSpace::NONE));
138 tmp->params().depth(par->params().depth());
139 tmp->params().noindent(par->params().noindent());
141 // copy everything behind the break-position
142 // to the new paragraph
145 #warning this seems wrong
147 /* FIXME: if !keepempty, empty() == true, then we reach
148 * here with size() == 0. So pos_end becomes - 1. Why
149 * doesn't this cause problems ???
151 pos_type pos_end = par->size() - 1;
155 for (; i <= pos_end; ++i) {
156 Change::Type change = par->lookupChange(i);
157 if (moveItem(*par, *tmp, bparams, i, j - pos)) {
158 tmp->setChange(j - pos, change);
163 for (i = pos_end; i >= pos; --i)
170 tmp->params().spaceTop(par->params().spaceTop());
171 par->params().clear();
173 par->layout(bparams.getLyXTextClass().defaultLayout());
175 // layout stays the same with latex-environments
177 par->layout(tmp->layout());
178 par->setLabelWidthString(tmp->params().labelWidthString());
179 par->params().depth(tmp->params().depth());
182 // subtle, but needed to get empty pars working right
183 if (bparams.tracking_changes) {
186 } else if (!tmp->size()) {
193 void breakParagraphConservative(BufferParams const & bparams,
194 ParagraphList & paragraphs,
195 ParagraphList::iterator par,
198 // create a new paragraph
199 ParagraphList::iterator tmp = paragraphs.insert(boost::next(par),
201 tmp->makeSameLayout(*par);
203 // When can pos > size()?
204 // I guess pos == size() is possible.
205 if (par->size() > pos) {
206 // copy everything behind the break-position to the new
208 pos_type pos_end = par->size() - 1;
210 for (pos_type i = pos, j = pos; i <= pos_end; ++i)
211 if (moveItem(*par, *tmp, bparams, i, j - pos))
214 for (pos_type k = pos_end; k >= pos; --k)
220 void mergeParagraph(BufferParams const & bparams,
221 ParagraphList & paragraphs,
222 ParagraphList::iterator par)
224 ParagraphList::iterator the_next = boost::next(par);
226 // first the DTP-stuff
227 par->params().spaceBottom(the_next->params().spaceBottom());
229 pos_type pos_end = the_next->size() - 1;
230 pos_type pos_insert = par->size();
232 // ok, now copy the paragraph
233 for (pos_type i = 0, j = 0; i <= pos_end; ++i)
234 if (moveItem(*the_next, *par, bparams, i, pos_insert + j))
237 paragraphs.erase(the_next);
241 ParagraphList::iterator depthHook(ParagraphList::iterator pit,
242 ParagraphList const & plist,
243 Paragraph::depth_type depth)
245 ParagraphList::iterator newpit = pit;
246 ParagraphList::iterator beg = const_cast<ParagraphList&>(plist).begin();
251 while (newpit != beg && newpit->getDepth() > depth) {
255 if (newpit->getDepth() > depth)
262 ParagraphList::iterator outerHook(ParagraphList::iterator pit,
263 ParagraphList const & plist)
265 if (!pit->getDepth())
266 return const_cast<ParagraphList&>(plist).end();
267 return depthHook(pit, plist,
268 Paragraph::depth_type(pit->getDepth() - 1));
272 bool isFirstInSequence(ParagraphList::iterator pit,
273 ParagraphList const & plist)
275 ParagraphList::iterator dhook = depthHook(pit, plist, pit->getDepth());
277 || dhook->layout() != pit->layout()
278 || dhook->getDepth() != pit->getDepth());
282 int getEndLabel(ParagraphList::iterator p, ParagraphList const & plist)
284 ParagraphList::iterator pit = p;
285 Paragraph::depth_type par_depth = p->getDepth();
286 while (pit != const_cast<ParagraphList&>(plist).end()) {
287 LyXLayout_ptr const & layout = pit->layout();
288 int const endlabeltype = layout->endlabeltype;
290 if (endlabeltype != END_LABEL_NO_LABEL) {
291 if (boost::next(p) == const_cast<ParagraphList&>(plist).end())
294 Paragraph::depth_type const next_depth = boost::next(p)->getDepth();
295 if (par_depth > next_depth ||
296 (par_depth == next_depth &&
297 layout != boost::next(p)->layout()))
303 pit = outerHook(pit, plist);
304 if (pit != const_cast<ParagraphList&>(plist).end())
305 par_depth = pit->getDepth();
307 return END_LABEL_NO_LABEL;
313 int readParToken(Buffer & buf, Paragraph & par, LyXLex & lex, string const & token)
316 static Change change;
318 BufferParams const & bp = buf.params();
320 if (token[0] != '\\') {
321 string::const_iterator cit = token.begin();
322 for (; cit != token.end(); ++cit) {
323 par.insertChar(par.size(), (*cit), font, change);
325 } else if (token == "\\begin_layout") {
327 string layoutname = lex.getString();
329 font = LyXFont(LyXFont::ALL_INHERIT, bp.language);
332 LyXTextClass const & tclass = bp.getLyXTextClass();
334 if (layoutname.empty()) {
335 layoutname = tclass.defaultLayoutName();
338 bool hasLayout = tclass.hasLayout(layoutname);
341 lyxerr << "Layout '" << layoutname << "' does not"
342 << " exist in textclass '" << tclass.name()
344 lyxerr << "Trying to use default layout instead."
346 layoutname = tclass.defaultLayoutName();
349 par.layout(bp.getLyXTextClass()[layoutname]);
351 // Test whether the layout is obsolete.
352 LyXLayout_ptr const & layout = par.layout();
353 if (!layout->obsoleted_by().empty())
354 par.layout(bp.getLyXTextClass()[layout->obsoleted_by()]);
356 par.params().read(lex);
358 } else if (token == "\\end_layout") {
359 lyxerr << "Solitary \\end_layout in line " << lex.getLineNo() << "\n"
360 << "Missing \\begin_layout?.\n";
361 } else if (token == "\\end_inset") {
362 lyxerr << "Solitary \\end_inset in line " << lex.getLineNo() << "\n"
363 << "Missing \\begin_inset?.\n";
364 } else if (token == "\\begin_inset") {
365 InsetOld * inset = readInset(lex, buf);
367 par.insertInset(par.size(), inset, font, change);
370 string line = lex.getString();
371 buf.error(ErrorItem(_("Unknown Inset"), line,
372 par.id(), 0, par.size()));
375 } else if (token == "\\family") {
377 font.setLyXFamily(lex.getString());
378 } else if (token == "\\series") {
380 font.setLyXSeries(lex.getString());
381 } else if (token == "\\shape") {
383 font.setLyXShape(lex.getString());
384 } else if (token == "\\size") {
386 font.setLyXSize(lex.getString());
387 } else if (token == "\\lang") {
389 string const tok = lex.getString();
390 Language const * lang = languages.getLanguage(tok);
392 font.setLanguage(lang);
394 font.setLanguage(bp.language);
395 lex.printError("Unknown language `$$Token'");
397 } else if (token == "\\numeric") {
399 font.setNumber(font.setLyXMisc(lex.getString()));
400 } else if (token == "\\emph") {
402 font.setEmph(font.setLyXMisc(lex.getString()));
403 } else if (token == "\\bar") {
405 string const tok = lex.getString();
408 font.setUnderbar(LyXFont::ON);
409 else if (tok == "no")
410 font.setUnderbar(LyXFont::OFF);
411 else if (tok == "default")
412 font.setUnderbar(LyXFont::INHERIT);
414 lex.printError("Unknown bar font flag "
416 } else if (token == "\\noun") {
418 font.setNoun(font.setLyXMisc(lex.getString()));
419 } else if (token == "\\color") {
421 font.setLyXColor(lex.getString());
422 } else if (token == "\\InsetSpace" || token == "\\SpecialChar") {
424 // Insets don't make sense in a free-spacing context! ---Kayvan
425 if (par.isFreeSpacing()) {
426 if (token == "\\InsetSpace")
427 par.insertChar(par.size(), ' ', font, change);
428 else if (lex.isOK()) {
430 string const next_token = lex.getString();
431 if (next_token == "\\-")
432 par.insertChar(par.size(), '-', font, change);
434 lex.printError("Token `$$Token' "
436 "paragraph layout!");
440 auto_ptr<InsetOld> inset;
441 if (token == "\\SpecialChar" )
442 inset.reset(new InsetSpecialChar);
444 inset.reset(new InsetSpace);
445 inset->read(buf, lex);
446 par.insertInset(par.size(), inset.release(),
449 } else if (token == "\\i") {
450 auto_ptr<InsetOld> inset(new InsetLatexAccent);
451 inset->read(buf, lex);
452 par.insertInset(par.size(), inset.release(), font, change);
453 } else if (token == "\\backslash") {
454 par.insertChar(par.size(), '\\', font, change);
455 } else if (token == "\\newline") {
456 auto_ptr<InsetOld> inset(new InsetNewline);
457 inset->read(buf, lex);
458 par.insertInset(par.size(), inset.release(), font, change);
459 } else if (token == "\\LyXTable") {
460 auto_ptr<InsetOld> inset(new InsetTabular(buf));
461 inset->read(buf, lex);
462 par.insertInset(par.size(), inset.release(), font, change);
463 } else if (token == "\\bibitem") {
464 InsetCommandParams p("bibitem", "dummy");
465 auto_ptr<InsetBibitem> inset(new InsetBibitem(p));
466 inset->read(buf, lex);
467 par.insertInset(par.size(), inset.release(), font, change);
468 } else if (token == "\\hfill") {
469 par.insertInset(par.size(), new InsetHFill, font, change);
470 } else if (token == "\\lyxline") {
471 par.insertInset(par.size(), new InsetLine, font, change);
472 } else if (token == "\\newpage") {
473 par.insertInset(par.size(), new InsetPagebreak, font, change);
474 } else if (token == "\\change_unchanged") {
475 // Hack ! Needed for empty paragraphs :/
476 // FIXME: is it still ??
479 change = Change(Change::UNCHANGED);
480 } else if (token == "\\change_inserted") {
482 istringstream is(lex.getString());
486 change = Change(Change::INSERTED, bp.author_map[aid], ct);
487 } else if (token == "\\change_deleted") {
489 istringstream is(lex.getString());
493 change = Change(Change::DELETED, bp.author_map[aid], ct);
496 string const s = bformat(_("Unknown token: %1$s %2$s\n"),
497 token, lex.getString());
499 buf.error(ErrorItem(_("Unknown token"), s,
500 par.id(), 0, par.size()));
509 int readParagraph(Buffer & buf, Paragraph & par, LyXLex & lex)
514 string token = lex.getString();
518 unknown += readParToken(buf, par, lex, token);
521 token = lex.getString();
526 if (token == "\\end_layout") {
527 //Ok, paragraph finished
531 lyxerr[Debug::PARSER] << "Handling paragraph token: `"
532 << token << '\'' << endl;
533 if (token == "\\begin_layout" || token == "\\end_document"
534 || token == "\\end_inset" || token == "\\begin_deeper"
535 || token == "\\end_deeper") {
536 lex.pushToken(token);
537 lyxerr << "Paragraph ended in line "
538 << lex.getLineNo() << "\n"
539 << "Missing \\end_layout.\n";
548 LyXFont const outerFont(ParagraphList::iterator pit,
549 ParagraphList const & plist)
551 Paragraph::depth_type par_depth = pit->getDepth();
552 LyXFont tmpfont(LyXFont::ALL_INHERIT);
554 // Resolve against environment font information
555 while (pit != const_cast<ParagraphList&>(plist).end() &&
556 par_depth && !tmpfont.resolved()) {
557 pit = outerHook(pit, plist);
558 if (pit != const_cast<ParagraphList&>(plist).end()) {
559 tmpfont.realize(pit->layout()->font);
560 par_depth = pit->getDepth();
568 ParagraphList::iterator outerPar(Buffer const & buf, InsetOld const * inset)
570 ParIterator pit = const_cast<Buffer &>(buf).par_iterator_begin();
571 ParIterator end = const_cast<Buffer &>(buf).par_iterator_end();
572 for ( ; pit != end; ++pit) {
574 ParagraphList * plist;
575 // the second '=' below is intentional
576 for (int i = 0; (plist = inset->getParagraphs(i)); ++i)
577 if (plist == &pit.plist())
578 return pit.outerPar();
580 InsetList::iterator ii = pit->insetlist.begin();
581 InsetList::iterator iend = pit->insetlist.end();
582 for ( ; ii != iend; ++ii)
583 if (ii->inset == inset)
584 return pit.outerPar();
586 lyxerr << "outerPar: should not happen" << endl;
588 return const_cast<Buffer &>(buf).paragraphs().end(); // shut up compiler
592 Paragraph const & ownerPar(Buffer const & buf, InsetOld const * inset)
594 ParConstIterator pit = buf.par_iterator_begin();
595 ParConstIterator end = buf.par_iterator_end();
596 for ( ; pit != end; ++pit) {
597 ParagraphList * plist;
598 // the second '=' below is intentional
599 for (int i = 0; (plist = inset->getParagraphs(i)); ++i)
600 if (plist == &pit.plist())
603 InsetList::const_iterator ii = pit->insetlist.begin();
604 InsetList::const_iterator iend = pit->insetlist.end();
605 for ( ; ii != iend; ++ii)
606 if (ii->inset == inset)
609 lyxerr << "ownerPar: should not happen" << endl;
611 return buf.paragraphs().front(); // shut up compiler
615 void getParsInRange(ParagraphList & pl,
616 int ystart, int yend,
617 ParagraphList::iterator & beg,
618 ParagraphList::iterator & end)
620 ParagraphList::iterator const endpar = pl.end();
621 ParagraphList::iterator const begpar = pl.begin();
623 BOOST_ASSERT(begpar != endpar);
626 for (--beg; beg != begpar && beg->y > ystart; --beg)
629 for (end = beg ; end != endpar && end->y <= yend; ++end)