3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetText.h"
15 #include "insets/InsetArgument.h"
16 #include "insets/InsetLayout.h"
18 #include "buffer_funcs.h"
20 #include "BufferParams.h"
21 #include "BufferView.h"
22 #include "CompletionList.h"
23 #include "CoordCache.h"
25 #include "CutAndPaste.h"
26 #include "DispatchResult.h"
27 #include "ErrorList.h"
28 #include "FuncRequest.h"
29 #include "FuncStatus.h"
30 #include "InsetList.h"
34 #include "LaTeXFeatures.h"
38 #include "MetricsInfo.h"
39 #include "output_docbook.h"
40 #include "output_latex.h"
41 #include "output_xhtml.h"
42 #include "OutputParams.h"
43 #include "output_plaintext.h"
44 #include "Paragraph.h"
45 #include "ParagraphParameters.h"
46 #include "ParIterator.h"
50 #include "texstream.h"
51 #include "TextClass.h"
53 #include "TextMetrics.h"
54 #include "TocBackend.h"
56 #include "frontends/alert.h"
57 #include "frontends/Painter.h"
59 #include "support/bind.h"
60 #include "support/convert.h"
61 #include "support/debug.h"
62 #include "support/gettext.h"
63 #include "support/lassert.h"
64 #include "support/lstrings.h"
65 #include "support/RefChanger.h"
71 using namespace lyx::support;
76 using graphics::PreviewLoader;
79 /////////////////////////////////////////////////////////////////////
81 InsetText::InsetText(Buffer * buf, UsePlain type)
82 : Inset(buf), drawFrame_(false), frame_color_(Color_insetframe),
83 text_(this, type == DefaultLayout)
88 InsetText::InsetText(InsetText const & in)
89 : Inset(in), text_(this, in.text_)
91 drawFrame_ = in.drawFrame_;
92 frame_color_ = in.frame_color_;
96 void InsetText::setBuffer(Buffer & buf)
98 ParagraphList::iterator end = paragraphs().end();
99 for (ParagraphList::iterator it = paragraphs().begin(); it != end; ++it)
101 Inset::setBuffer(buf);
105 void InsetText::setMacrocontextPositionRecursive(DocIterator const & pos)
107 text_.setMacrocontextPosition(pos);
109 ParagraphList::const_iterator pit = paragraphs().begin();
110 ParagraphList::const_iterator pend = paragraphs().end();
111 for (; pit != pend; ++pit) {
112 InsetList::const_iterator iit = pit->insetList().begin();
113 InsetList::const_iterator end = pit->insetList().end();
114 for (; iit != end; ++iit) {
115 if (InsetText * txt = iit->inset->asInsetText()) {
116 DocIterator ppos(pos);
117 ppos.push_back(CursorSlice(*txt));
118 iit->inset->asInsetText()->setMacrocontextPositionRecursive(ppos);
125 void InsetText::clear()
127 ParagraphList & pars = paragraphs();
128 LBUFERR(!pars.empty());
130 // This is a gross hack...
131 Layout const & old_layout = pars.begin()->layout();
134 pars.push_back(Paragraph());
135 pars.begin()->setInsetOwner(this);
136 pars.begin()->setLayout(old_layout);
140 Dimension const InsetText::dimensionHelper(BufferView const & bv) const
142 TextMetrics const & tm = bv.textMetrics(&text_);
143 Dimension dim = tm.dimension();
144 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
145 dim.des += TEXT_TO_INSET_OFFSET;
146 dim.asc += TEXT_TO_INSET_OFFSET;
151 void InsetText::write(ostream & os) const
158 void InsetText::read(Lexer & lex)
162 // delete the initial paragraph
163 Paragraph oldpar = *paragraphs().begin();
164 paragraphs().clear();
166 lex.setContext("InsetText::read");
167 bool res = text_.read(lex, errorList, this);
170 lex.printError("Missing \\end_inset at this point. ");
173 // ensure we have at least one paragraph.
174 if (paragraphs().empty())
175 paragraphs().push_back(oldpar);
176 // Force default font, if so requested
177 // This avoids paragraphs in buffer language that would have a
178 // foreign language after a document language change, and it ensures
179 // that all new text in ERT and similar gets the "latex" language,
180 // since new text inherits the language from the last position of the
181 // existing text. As a side effect this makes us also robust against
182 // bugs in LyX that might lead to font changes in ERT in .lyx files.
187 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
189 TextMetrics & tm = mi.base.bv->textMetrics(&text_);
191 //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
193 // Hand font through to contained lyxtext:
194 tm.font_.fontInfo() = mi.base.font;
195 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
197 // This can happen when a layout has a left and right margin,
198 // and the view is made very narrow. We can't do better than
199 // to draw it partly out of view (bug 5890).
200 if (mi.base.textwidth < 1)
201 mi.base.textwidth = 1;
204 tm.metrics(mi, dim, mi.base.textwidth);
207 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
208 dim.asc += TEXT_TO_INSET_OFFSET;
209 dim.des += TEXT_TO_INSET_OFFSET;
210 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
214 void InsetText::draw(PainterInfo & pi, int x, int y) const
216 TextMetrics & tm = pi.base.bv->textMetrics(&text_);
218 int const w = tm.width() + TEXT_TO_INSET_OFFSET;
219 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
220 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
221 int const xframe = x + TEXT_TO_INSET_OFFSET / 2;
222 bool change_drawn = false;
223 if (drawFrame_ || pi.full_repaint) {
225 pi.pain.fillRectangle(xframe, yframe, w, h,
226 pi.backgroundColor(this));
228 // Change color of the frame in tracked changes, like for tabulars.
229 // Only do so if the color is not custom. But do so even if RowPainter
230 // handles the strike-through already.
232 if (pi.change_.changed()
233 // Originally, these are the colors with role Text, from role() in
234 // ColorCache.cpp. The code is duplicated to avoid depending on Qt
235 // types, and also maybe it need not match in the future.
236 && (frameColor() == Color_foreground
237 || frameColor() == Color_cursor
238 || frameColor() == Color_preview
239 || frameColor() == Color_tabularline
240 || frameColor() == Color_previewframe)) {
241 c = pi.change_.color();
246 pi.pain.rectangle(xframe, yframe, w, h, c);
249 Changer dummy = make_change(pi.background_color,
250 pi.backgroundColor(this, false));
251 // The change tracking cue must not be inherited
252 Changer dummy2 = make_change(pi.change_, Change());
253 tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
255 if (canPaintChange(*pi.base.bv) && (!change_drawn || pi.change_.deleted()))
256 // Do not draw the change tracking cue if already done by RowPainter and
257 // do not draw the cue for INSERTED if the information is already in the
258 // color of the frame
259 pi.change_.paintCue(pi, xframe, yframe, xframe + w, yframe + h);
263 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
265 pit_type const pit = front ? 0 : paragraphs().size() - 1;
266 pos_type pos = front ? 0 : paragraphs().back().size();
268 // if visual information is not to be ignored, move to extreme right/left
269 if (entry_from != ENTRY_DIRECTION_IGNORE) {
270 Cursor temp_cur = cur;
271 temp_cur.pit() = pit;
272 temp_cur.pos() = pos;
273 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
274 pos = temp_cur.pos();
277 cur.top().setPitPos(pit, pos);
282 Inset * InsetText::editXY(Cursor & cur, int x, int y)
284 return cur.bv().textMetrics(&text_).editXY(cur, x, y);
288 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
290 LYXERR(Debug::ACTION, "InsetText::doDispatch(): cmd: " << cmd);
292 // See bug #9042, for instance.
294 // Force any new text to latex_language FIXME: This
295 // should only be necessary in constructor, but new
296 // paragraphs that are created by pressing enter at
297 // the start of an existing paragraph get the buffer
298 // language and not latex_language, so we take this
299 // brute force approach.
300 cur.current_font.setLanguage(latex_language);
301 cur.real_current_font.setLanguage(latex_language);
304 switch (cmd.action()) {
306 case LFUN_CLIPBOARD_PASTE:
307 case LFUN_SELECTION_PASTE:
308 case LFUN_PRIMARY_SELECTION_PASTE:
309 text_.dispatch(cur, cmd);
310 // If we we can only store plain text, we must reset all
312 // FIXME: Change only the pasted paragraphs
316 case LFUN_INSET_DISSOLVE: {
317 bool const main_inset = text_.isMainText();
318 bool const target_inset = cmd.argument().empty()
319 || cmd.getArg(0) == insetName(lyxCode());
320 // cur.inset() is the tabular when this is a single cell (bug #9954)
321 bool const one_cell = cur.inset().nargs() == 1;
323 if (!main_inset && target_inset && one_cell) {
324 // Text::dissolveInset assumes that the cursor
325 // is inside the Inset.
326 if (&cur.inset() != this)
327 cur.pushBackward(*this);
328 cur.beginUndoGroup();
329 text_.dispatch(cur, cmd);
337 text_.dispatch(cur, cmd);
340 if (!cur.result().dispatched())
341 Inset::doDispatch(cur, cmd);
345 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
346 FuncStatus & status) const
348 switch (cmd.action()) {
349 case LFUN_INSET_DISSOLVE: {
350 bool const main_inset = text_.isMainText();
351 bool const target_inset = cmd.argument().empty()
352 || cmd.getArg(0) == insetName(lyxCode());
353 // cur.inset() is the tabular when this is a single cell (bug #9954)
354 bool const one_cell = cur.inset().nargs() == 1;
357 status.setEnabled(!main_inset && one_cell);
361 case LFUN_ARGUMENT_INSERT: {
362 string const arg = cmd.getArg(0);
364 status.setEnabled(false);
367 if (text_.isMainText() || !cur.paragraph().layout().args().empty())
368 return text_.getStatus(cur, cmd, status);
370 Layout::LaTeXArgMap args = getLayout().args();
371 Layout::LaTeXArgMap::const_iterator const lait = args.find(arg);
372 if (lait != args.end()) {
373 status.setEnabled(true);
374 for (Paragraph const & par : paragraphs())
375 for (auto const & table : par.insetList())
376 if (InsetArgument const * ins = table.inset->asInsetArgument())
377 if (ins->name() == arg) {
378 // we have this already
379 status.setEnabled(false);
383 status.setEnabled(false);
388 // Dispatch only to text_ if the cursor is inside
389 // the text_. It is not for context menus (bug 5797).
391 if (cur.text() == &text_)
392 ret = text_.getStatus(cur, cmd, status);
395 ret = Inset::getStatus(cur, cmd, status);
401 void InsetText::fixParagraphsFont()
403 Font font(inherit_font, buffer().params().language);
404 font.setLanguage(latex_language);
405 ParagraphList::iterator par = paragraphs().begin();
406 ParagraphList::iterator const end = paragraphs().end();
408 if (par->isPassThru())
409 par->resetFonts(font);
410 if (!par->allowParagraphCustomization())
411 par->params().clear();
417 void InsetText::setChange(Change const & change)
419 ParagraphList::iterator pit = paragraphs().begin();
420 ParagraphList::iterator end = paragraphs().end();
421 for (; pit != end; ++pit) {
422 pit->setChange(change);
427 void InsetText::acceptChanges()
429 text_.acceptChanges();
433 void InsetText::rejectChanges()
435 text_.rejectChanges();
439 void InsetText::validate(LaTeXFeatures & features) const
441 features.useInsetLayout(getLayout());
442 for (Paragraph const & p : paragraphs())
443 p.validate(features);
447 void InsetText::latex(otexstream & os, OutputParams const & runparams) const
449 // This implements the standard way of handling the LaTeX
450 // output of a text inset, either a command or an
451 // environment. Standard collapsable insets should not
452 // redefine this, non-standard ones may call this.
453 InsetLayout const & il = getLayout();
454 if (il.forceOwnlines())
456 if (!il.latexname().empty()) {
457 if (il.latextype() == InsetLayout::COMMAND) {
459 // FIXME \protect should only be used for fragile
460 // commands, but we do not provide this information yet.
461 if (runparams.moving_arg)
463 os << '\\' << from_utf8(il.latexname());
464 if (!il.latexargs().empty())
465 getArgs(os, runparams);
466 if (!il.latexparam().empty())
467 os << from_utf8(il.latexparam());
469 } else if (il.latextype() == InsetLayout::ENVIRONMENT) {
474 if (runparams.lastid != -1)
475 os.texrow().start(runparams.lastid,
477 os << "\\begin{" << from_utf8(il.latexname()) << "}";
478 if (!il.latexargs().empty())
479 getArgs(os, runparams);
480 if (!il.latexparam().empty())
481 os << from_utf8(il.latexparam());
485 if (!il.latexargs().empty())
486 getArgs(os, runparams);
487 if (!il.latexparam().empty())
488 os << from_utf8(il.latexparam());
491 if (!il.leftdelim().empty())
492 os << il.leftdelim();
494 OutputParams rp = runparams;
497 if (il.isNeedProtect())
498 rp.moving_arg = true;
499 if (!il.passThruChars().empty())
500 rp.pass_thru_chars += il.passThruChars();
502 rp.par_end = paragraphs().size();
504 // Output the contents of the inset
505 latexParagraphs(buffer(), text_, os, rp);
506 runparams.encoding = rp.encoding;
508 if (!il.rightdelim().empty())
509 os << il.rightdelim();
511 if (!il.latexname().empty()) {
512 if (il.latextype() == InsetLayout::COMMAND) {
514 if (!il.postcommandargs().empty())
515 getArgs(os, runparams, true);
516 } else if (il.latextype() == InsetLayout::ENVIRONMENT) {
517 // A comment environment doesn't need a % before \n\end
518 if (il.isDisplay() || runparams.inComment)
522 os << "\\end{" << from_utf8(il.latexname()) << "}" << breakln;
524 os.protectSpace(true);
527 if (il.forceOwnlines())
532 int InsetText::plaintext(odocstringstream & os,
533 OutputParams const & runparams, size_t max_length) const
535 ParagraphList::const_iterator beg = paragraphs().begin();
536 ParagraphList::const_iterator end = paragraphs().end();
537 ParagraphList::const_iterator it = beg;
538 bool ref_printed = false;
540 for (; it != end; ++it) {
543 if (runparams.linelen > 0)
546 odocstringstream oss;
547 writePlaintextParagraph(buffer(), *it, oss, runparams, ref_printed, max_length);
548 docstring const str = oss.str();
550 // FIXME: len is not computed fully correctly; in principle,
551 // we have to count the characters after the last '\n'
553 if (os.str().size() >= max_length)
561 int InsetText::docbook(odocstream & os, OutputParams const & runparams) const
563 ParagraphList::const_iterator const beg = paragraphs().begin();
566 sgml::openTag(os, getLayout().latexname(),
567 beg->getID(buffer(), runparams) + getLayout().latexparam());
569 docbookParagraphs(text_, buffer(), os, runparams);
572 sgml::closeTag(os, getLayout().latexname());
578 docstring InsetText::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
580 return insetAsXHTML(xs, runparams, WriteEverything);
585 // There are cases where we may need to close open fonts and such
586 // and then re-open them when we are done. This would be the case, e.g.,
587 // if we were otherwise about to write:
588 // <em>word <div class='foot'>footnote text.</div> emph</em>
589 // The problem isn't so much that the footnote text will get emphasized:
590 // we can handle that with CSS. The problem is that this is invalid XHTML.
591 // One solution would be to make the footnote <span>, but the problem is
592 // completely general, and so we'd have to make absolutely everything into
593 // span. What I think will work is to check if we're about to write "div" and,
594 // if so, try to close fonts, etc.
595 // There are probably limits to how well we can do here, though, and we will
596 // have to rely upon users not putting footnotes inside noun-type insets.
597 docstring InsetText::insetAsXHTML(XHTMLStream & xs, OutputParams const & rp,
598 XHTMLOptions opts) const
600 // we will always want to output all our paragraphs when we are
602 OutputParams runparams = rp;
603 runparams.par_begin = 0;
604 runparams.par_end = text().paragraphs().size();
607 xs.startDivision(false);
608 xhtmlParagraphs(text_, buffer(), xs, runparams);
613 InsetLayout const & il = getLayout();
614 if (opts & WriteOuterTag)
615 xs << html::StartTag(il.htmltag(), il.htmlattr());
617 if ((opts & WriteLabel) && !il.counter().empty()) {
618 BufferParams const & bp = buffer().masterBuffer()->params();
619 Counters & cntrs = bp.documentClass().counters();
620 cntrs.step(il.counter(), OutputUpdate);
621 // FIXME: translate to paragraph language
622 if (!il.htmllabel().empty()) {
623 docstring const lbl =
624 cntrs.counterLabel(from_utf8(il.htmllabel()), bp.language->code());
625 // FIXME is this check necessary?
627 xs << html::StartTag(il.htmllabeltag(), il.htmllabelattr());
629 xs << html::EndTag(il.htmllabeltag());
634 if (opts & WriteInnerTag)
635 xs << html::StartTag(il.htmlinnertag(), il.htmlinnerattr());
637 // we will eventually lose information about the containing inset
638 if (!allowMultiPar() || opts == JustText)
639 runparams.html_make_pars = false;
641 runparams.pass_thru = true;
643 xs.startDivision(false);
644 xhtmlParagraphs(text_, buffer(), xs, runparams);
647 if (opts & WriteInnerTag)
648 xs << html::EndTag(il.htmlinnertag());
650 if (opts & WriteOuterTag)
651 xs << html::EndTag(il.htmltag());
656 void InsetText::getArgs(otexstream & os, OutputParams const & runparams_in,
657 bool const post) const
659 OutputParams runparams = runparams_in;
660 runparams.local_font =
661 ¶graphs()[0].getFirstFontSettings(buffer().masterBuffer()->params());
663 runparams.pass_thru = true;
665 latexArgInsets(paragraphs(), paragraphs().begin(), os, runparams, getLayout().postcommandargs(), "post:");
667 latexArgInsets(paragraphs(), paragraphs().begin(), os, runparams, getLayout().latexargs());
671 void InsetText::cursorPos(BufferView const & bv,
672 CursorSlice const & sl, bool boundary, int & x, int & y) const
674 x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
675 y = bv.textMetrics(&text_).cursorY(sl, boundary);
679 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
682 Paragraph & first = paragraphs().front();
683 for (unsigned int i = 0; i < data.length(); ++i)
684 first.insertChar(i, data[i], font, trackChanges);
688 void InsetText::setDrawFrame(bool flag)
694 ColorCode InsetText::frameColor() const
700 void InsetText::setFrameColor(ColorCode col)
706 void InsetText::appendParagraphs(ParagraphList & plist)
708 // There is little we can do here to keep track of changes.
709 // As of 2006/10/20, appendParagraphs is used exclusively by
710 // LyXTabular::setMultiColumn. In this context, the paragraph break
711 // is lost irreversibly and the appended text doesn't really change
713 ParagraphList & pl = paragraphs();
715 ParagraphList::iterator pit = plist.begin();
716 ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
718 mergeParagraph(buffer().params(), pl,
719 distance(pl.begin(), ins) - 1);
721 for_each(pit, plist.end(),
722 bind(&ParagraphList::push_back, ref(pl), _1));
726 void InsetText::addPreview(DocIterator const & text_inset_pos,
727 PreviewLoader & loader) const
729 ParagraphList::const_iterator pit = paragraphs().begin();
730 ParagraphList::const_iterator pend = paragraphs().end();
733 DocIterator inset_pos = text_inset_pos;
734 inset_pos.push_back(CursorSlice(*const_cast<InsetText *>(this)));
736 for (; pit != pend; ++pit, ++pidx) {
737 InsetList::const_iterator it = pit->insetList().begin();
738 InsetList::const_iterator end = pit->insetList().end();
739 inset_pos.pit() = pidx;
740 for (; it != end; ++it) {
741 inset_pos.pos() = it->pos;
742 it->inset->addPreview(inset_pos, loader);
748 ParagraphList const & InsetText::paragraphs() const
750 return text_.paragraphs();
754 ParagraphList & InsetText::paragraphs()
756 return text_.paragraphs();
760 bool InsetText::insetAllowed(InsetCode code) const
763 // Arguments and (plain) quotes are also allowed in PassThru insets
768 return !isPassThru();
773 void InsetText::updateBuffer(ParIterator const & it, UpdateType utype)
775 ParIterator it2 = it;
777 LASSERT(&it2.inset() == this && it2.pit() == 0, return);
778 if (producesOutput()) {
779 InsetLayout const & il = getLayout();
780 bool const save_layouts = utype == OutputUpdate && il.htmlisblock();
781 Counters & cnt = buffer().masterBuffer()->params().documentClass().counters();
783 // LYXERR0("Entering " << name());
784 cnt.clearLastLayout();
785 // FIXME cnt.saveLastCounter()?
787 buffer().updateBuffer(it2, utype);
789 // LYXERR0("Exiting " << name());
790 cnt.restoreLastLayout();
791 // FIXME cnt.restoreLastCounter()?
794 DocumentClass const & tclass = buffer().masterBuffer()->params().documentClass();
795 // Note that we do not need to call:
796 // tclass.counters().clearLastLayout()
797 // since we are saving and restoring the existing counters, etc.
798 Counters const savecnt = tclass.counters();
799 tclass.counters().reset();
800 // we need float information even in note insets (#9760)
801 tclass.counters().current_float(savecnt.current_float());
802 tclass.counters().isSubfloat(savecnt.isSubfloat());
803 buffer().updateBuffer(it2, utype);
804 tclass.counters() = savecnt;
809 void InsetText::toString(odocstream & os) const
811 os << text().asString(0, 1, AS_STR_LABEL | AS_STR_INSETS);
815 void InsetText::forOutliner(docstring & os, size_t const maxlen,
816 bool const shorten) const
818 if (!getLayout().isInToc())
820 text().forOutliner(os, maxlen, shorten);
824 void InsetText::addToToc(DocIterator const & cdit, bool output_active,
825 UpdateType utype, TocBackend & backend) const
827 DocIterator dit = cdit;
828 dit.push_back(CursorSlice(const_cast<InsetText &>(*this)));
829 iterateForToc(dit, output_active, utype, backend);
833 void InsetText::iterateForToc(DocIterator const & cdit, bool output_active,
834 UpdateType utype, TocBackend & backend) const
836 DocIterator dit = cdit;
837 // This also ensures that any document has a table of contents
838 shared_ptr<Toc> toc = backend.toc("tableofcontents");
840 BufferParams const & bufparams = buffer_->params();
841 int const min_toclevel = bufparams.documentClass().min_toclevel();
842 // we really should have done this before we got here, but it
843 // can't hurt too much to do it again
844 bool const doing_output = output_active && producesOutput();
846 // For each paragraph,
847 // * Add a toc item for the paragraph if it is AddToToc--merging adjacent
848 // paragraphs as needed.
849 // * Traverse its insets and let them add their toc items
850 // * Compute the main table of contents (this is hardcoded)
851 // * Add the list of changes
852 ParagraphList const & pars = paragraphs();
853 pit_type pend = paragraphs().size();
854 // Record pairs {start,end} of where a toc item was opened for a paragraph
855 // and where it must be closed
856 stack<pair<pit_type, pit_type>> addtotoc_stack;
858 for (pit_type pit = 0; pit != pend; ++pit) {
859 Paragraph const & par = pars[pit];
863 // Custom AddToToc in paragraph layouts (i.e. theorems)
864 if (par.layout().addToToc() && text().isFirstInSequence(pit)) {
866 openAddToTocForParagraph(pit, dit, output_active, backend);
867 addtotoc_stack.push({pit, end});
870 // if we find an optarg, we'll save it for use later.
871 InsetArgument const * arginset = nullptr;
872 for (auto const & table : par.insetList()) {
873 dit.pos() = table.pos;
874 table.inset->addToToc(dit, doing_output, utype, backend);
875 if (InsetArgument const * x = table.inset->asInsetArgument())
879 // End custom AddToToc in paragraph layouts
880 while (!addtotoc_stack.empty() && addtotoc_stack.top().second == pit) {
881 // execute the closing function
882 closeAddToTocForParagraph(addtotoc_stack.top().first,
883 addtotoc_stack.top().second, backend);
884 addtotoc_stack.pop();
887 // now the toc entry for the paragraph in the main table of contents
888 int const toclevel = text().getTocLevel(pit);
889 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel) {
890 // insert this into the table of contents
892 int const length = (doing_output && utype == OutputUpdate) ?
893 INT_MAX : TOC_ENTRY_LENGTH;
895 tocstring = par.labelString();
896 if (!tocstring.empty())
898 arginset->text().forOutliner(tocstring, length);
900 par.forOutliner(tocstring, length);
902 toc->push_back(TocItem(dit, toclevel - min_toclevel,
903 tocstring, doing_output));
906 // And now the list of changes.
907 par.addChangesToToc(dit, buffer(), doing_output, backend);
912 pit_type InsetText::openAddToTocForParagraph(pit_type pit,
913 DocIterator const & dit,
915 TocBackend & backend) const
917 Paragraph const & par = paragraphs()[pit];
918 TocBuilder & b = backend.builder(par.layout().tocType());
919 docstring const label = par.labelString();
920 b.pushItem(dit, label + (label.empty() ? "" : " "), output_active);
921 return text().lastInSequence(pit);
925 void InsetText::closeAddToTocForParagraph(pit_type start, pit_type end,
926 TocBackend & backend) const
928 Paragraph const & par = paragraphs()[start];
929 TocBuilder & b = backend.builder(par.layout().tocType());
930 if (par.layout().isTocCaption()) {
932 text().forOutliner(str, TOC_ENTRY_LENGTH, start, end);
939 bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
941 if (buffer().isClean())
942 return Inset::notifyCursorLeaves(old, cur);
944 // find text inset in old cursor
945 Cursor insetCur = old;
946 int scriptSlice = insetCur.find(this);
947 // we can try to continue here. returning true means
948 // the cursor is "now" invalid. which it was.
949 LASSERT(scriptSlice != -1, return true);
950 insetCur.cutOff(scriptSlice);
951 LASSERT(&insetCur.inset() == this, return true);
953 // update the old paragraph's words
954 insetCur.paragraph().updateWords();
956 return Inset::notifyCursorLeaves(old, cur);
960 bool InsetText::completionSupported(Cursor const & cur) const
962 //LASSERT(&cur.bv().cursor().inset() == this, return false);
963 return text_.completionSupported(cur);
967 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
969 return completionSupported(cur);
973 bool InsetText::automaticInlineCompletion() const
975 return lyxrc.completion_inline_text;
979 bool InsetText::automaticPopupCompletion() const
981 return lyxrc.completion_popup_text;
985 bool InsetText::showCompletionCursor() const
987 return lyxrc.completion_cursor_text;
991 CompletionList const * InsetText::createCompletionList(Cursor const & cur) const
993 return completionSupported(cur) ? text_.createCompletionList(cur) : 0;
997 docstring InsetText::completionPrefix(Cursor const & cur) const
999 if (!completionSupported(cur))
1001 return text_.completionPrefix(cur);
1005 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
1008 if (!completionSupported(cur))
1011 return text_.insertCompletion(cur, s, finished);
1015 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y,
1016 Dimension & dim) const
1018 TextMetrics const & tm = cur.bv().textMetrics(&text_);
1019 tm.completionPosAndDim(cur, x, y, dim);
1023 string InsetText::contextMenu(BufferView const &, int, int) const
1025 string context_menu = contextMenuName();
1026 if (context_menu != InsetText::contextMenuName())
1027 context_menu += ";" + InsetText::contextMenuName();
1028 return context_menu;
1032 string InsetText::contextMenuName() const
1034 return "context-edit";
1038 docstring InsetText::toolTipText(docstring prefix, size_t const len) const
1040 OutputParams rp(&buffer().params().encoding());
1041 rp.for_tooltip = true;
1042 odocstringstream oss;
1045 ParagraphList::const_iterator beg = paragraphs().begin();
1046 ParagraphList::const_iterator end = paragraphs().end();
1047 ParagraphList::const_iterator it = beg;
1048 bool ref_printed = false;
1050 for (; it != end; ++it) {
1053 writePlaintextParagraph(buffer(), *it, oss, rp, ref_printed, len);
1054 if (oss.tellp() >= 0 && size_t(oss.tellp()) > len)
1057 docstring str = oss.str();
1058 support::truncateWithEllipsis(str, len);
1063 InsetText::XHTMLOptions operator|(InsetText::XHTMLOptions a1, InsetText::XHTMLOptions a2)
1065 return static_cast<InsetText::XHTMLOptions>((int)a1 | (int)a2);