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 buf.paragraphs().back().id(),
376 } else if (token == "\\family") {
378 font.setLyXFamily(lex.getString());
379 } else if (token == "\\series") {
381 font.setLyXSeries(lex.getString());
382 } else if (token == "\\shape") {
384 font.setLyXShape(lex.getString());
385 } else if (token == "\\size") {
387 font.setLyXSize(lex.getString());
388 } else if (token == "\\lang") {
390 string const tok = lex.getString();
391 Language const * lang = languages.getLanguage(tok);
393 font.setLanguage(lang);
395 font.setLanguage(bp.language);
396 lex.printError("Unknown language `$$Token'");
398 } else if (token == "\\numeric") {
400 font.setNumber(font.setLyXMisc(lex.getString()));
401 } else if (token == "\\emph") {
403 font.setEmph(font.setLyXMisc(lex.getString()));
404 } else if (token == "\\bar") {
406 string const tok = lex.getString();
409 font.setUnderbar(LyXFont::ON);
410 else if (tok == "no")
411 font.setUnderbar(LyXFont::OFF);
412 else if (tok == "default")
413 font.setUnderbar(LyXFont::INHERIT);
415 lex.printError("Unknown bar font flag "
417 } else if (token == "\\noun") {
419 font.setNoun(font.setLyXMisc(lex.getString()));
420 } else if (token == "\\color") {
422 font.setLyXColor(lex.getString());
423 } else if (token == "\\InsetSpace" || token == "\\SpecialChar") {
425 // Insets don't make sense in a free-spacing context! ---Kayvan
426 if (par.isFreeSpacing()) {
427 if (token == "\\InsetSpace")
428 par.insertChar(par.size(), ' ', font, change);
429 else if (lex.isOK()) {
431 string const next_token = lex.getString();
432 if (next_token == "\\-")
433 par.insertChar(par.size(), '-', font, change);
435 lex.printError("Token `$$Token' "
437 "paragraph layout!");
441 auto_ptr<InsetOld> inset;
442 if (token == "\\SpecialChar" )
443 inset.reset(new InsetSpecialChar);
445 inset.reset(new InsetSpace);
446 inset->read(buf, lex);
447 par.insertInset(par.size(), inset.release(),
450 } else if (token == "\\i") {
451 auto_ptr<InsetOld> inset(new InsetLatexAccent);
452 inset->read(buf, lex);
453 par.insertInset(par.size(), inset.release(), font, change);
454 } else if (token == "\\backslash") {
455 par.insertChar(par.size(), '\\', font, change);
456 } else if (token == "\\newline") {
457 auto_ptr<InsetOld> inset(new InsetNewline);
458 inset->read(buf, lex);
459 par.insertInset(par.size(), inset.release(), font, change);
460 } else if (token == "\\LyXTable") {
461 auto_ptr<InsetOld> inset(new InsetTabular(buf));
462 inset->read(buf, lex);
463 par.insertInset(par.size(), inset.release(), font, change);
464 } else if (token == "\\bibitem") {
465 InsetCommandParams p("bibitem", "dummy");
466 auto_ptr<InsetBibitem> inset(new InsetBibitem(p));
467 inset->read(buf, lex);
468 par.insertInset(par.size(), inset.release(), font, change);
469 } else if (token == "\\hfill") {
470 par.insertInset(par.size(), new InsetHFill, font, change);
471 } else if (token == "\\lyxline") {
472 par.insertInset(par.size(), new InsetLine, font, change);
473 } else if (token == "\\newpage") {
474 par.insertInset(par.size(), new InsetPagebreak, font, change);
475 } else if (token == "\\change_unchanged") {
476 // Hack ! Needed for empty paragraphs :/
477 // FIXME: is it still ??
480 change = Change(Change::UNCHANGED);
481 } else if (token == "\\change_inserted") {
483 istringstream is(lex.getString());
487 change = Change(Change::INSERTED, bp.author_map[aid], ct);
488 } else if (token == "\\change_deleted") {
490 istringstream is(lex.getString());
494 change = Change(Change::DELETED, bp.author_map[aid], ct);
497 string const s = bformat(_("Unknown token: %1$s %2$s\n"),
498 token, lex.getString());
500 buf.error(ErrorItem(_("Unknown token"), s,
501 buf.paragraphs().back().id(),
511 int readParagraph(Buffer & buf, Paragraph & par, LyXLex & lex)
516 string token = lex.getString();
520 unknown += readParToken(buf, par, lex, token);
523 token = lex.getString();
528 if (token == "\\end_layout") {
529 //Ok, paragraph finished
533 lyxerr[Debug::PARSER] << "Handling paragraph token: `"
534 << token << '\'' << endl;
535 if (token == "\\begin_layout" || token == "\\end_document"
536 || token == "\\end_inset" || token == "\\begin_deeper"
537 || token == "\\end_deeper") {
538 lex.pushToken(token);
539 lyxerr << "Paragraph ended in line "
540 << lex.getLineNo() << "\n"
541 << "Missing \\end_layout.\n";
550 LyXFont const outerFont(ParagraphList::iterator pit,
551 ParagraphList const & plist)
553 Paragraph::depth_type par_depth = pit->getDepth();
554 LyXFont tmpfont(LyXFont::ALL_INHERIT);
556 // Resolve against environment font information
557 while (pit != const_cast<ParagraphList&>(plist).end() &&
558 par_depth && !tmpfont.resolved()) {
559 pit = outerHook(pit, plist);
560 if (pit != const_cast<ParagraphList&>(plist).end()) {
561 tmpfont.realize(pit->layout()->font);
562 par_depth = pit->getDepth();
570 ParagraphList::iterator outerPar(Buffer const & buf, InsetOld const * inset)
572 ParIterator pit = const_cast<Buffer &>(buf).par_iterator_begin();
573 ParIterator end = const_cast<Buffer &>(buf).par_iterator_end();
574 for ( ; pit != end; ++pit) {
576 ParagraphList * plist;
577 // the second '=' below is intentional
578 for (int i = 0; (plist = inset->getParagraphs(i)); ++i)
579 if (plist == &pit.plist())
580 return pit.outerPar();
582 InsetList::iterator ii = pit->insetlist.begin();
583 InsetList::iterator iend = pit->insetlist.end();
584 for ( ; ii != iend; ++ii)
585 if (ii->inset == inset)
586 return pit.outerPar();
588 lyxerr << "outerPar: should not happen" << endl;
590 return const_cast<Buffer &>(buf).paragraphs().end(); // shut up compiler
594 Paragraph const & ownerPar(Buffer const & buf, InsetOld const * inset)
596 ParConstIterator pit = buf.par_iterator_begin();
597 ParConstIterator end = buf.par_iterator_end();
598 for ( ; pit != end; ++pit) {
599 ParagraphList * plist;
600 // the second '=' below is intentional
601 for (int i = 0; (plist = inset->getParagraphs(i)); ++i)
602 if (plist == &pit.plist())
605 InsetList::const_iterator ii = pit->insetlist.begin();
606 InsetList::const_iterator iend = pit->insetlist.end();
607 for ( ; ii != iend; ++ii)
608 if (ii->inset == inset)
611 lyxerr << "ownerPar: should not happen" << endl;
613 return buf.paragraphs().front(); // shut up compiler