]> git.lyx.org Git - lyx.git/blob - src/insets/InsetText.cpp
Not sure why I introduced this test at r28378, but it causes pasting to
[lyx.git] / src / insets / InsetText.cpp
1 /**
2  * \file InsetText.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetText.h"
14
15 #include "insets/InsetArgument.h"
16
17 #include "buffer_funcs.h"
18 #include "Buffer.h"
19 #include "BufferParams.h"
20 #include "BufferView.h"
21 #include "CompletionList.h"
22 #include "CoordCache.h"
23 #include "Cursor.h"
24 #include "CutAndPaste.h"
25 #include "DispatchResult.h"
26 #include "ErrorList.h"
27 #include "FuncRequest.h"
28 #include "FuncStatus.h"
29 #include "InsetCaption.h"
30 #include "InsetList.h"
31 #include "Intl.h"
32 #include "Language.h"
33 #include "LaTeXFeatures.h"
34 #include "Lexer.h"
35 #include "lyxfind.h"
36 #include "LyXRC.h"
37 #include "MetricsInfo.h"
38 #include "output_docbook.h"
39 #include "output_latex.h"
40 #include "output_xhtml.h"
41 #include "OutputParams.h"
42 #include "output_plaintext.h"
43 #include "Paragraph.h"
44 #include "ParagraphParameters.h"
45 #include "ParIterator.h"
46 #include "Row.h"
47 #include "sgml.h"
48 #include "TexRow.h"
49 #include "TextClass.h"
50 #include "Text.h"
51 #include "TextMetrics.h"
52 #include "TocBackend.h"
53
54 #include "frontends/alert.h"
55 #include "frontends/Painter.h"
56
57 #include "support/debug.h"
58 #include "support/gettext.h"
59 #include "support/lstrings.h"
60
61 #include "support/bind.h"
62 #include "support/lassert.h"
63
64 #include <algorithm>
65
66
67 using namespace std;
68 using namespace lyx::support;
69
70
71 namespace lyx {
72
73 using graphics::PreviewLoader;
74
75
76 /////////////////////////////////////////////////////////////////////
77
78 InsetText::InsetText(Buffer * buf, UsePlain type)
79         : Inset(buf), drawFrame_(false), frame_color_(Color_insetframe),
80         text_(this, type == DefaultLayout)
81 {
82 }
83
84
85 InsetText::InsetText(InsetText const & in)
86         : Inset(in), text_(this, in.text_)
87 {
88         drawFrame_ = in.drawFrame_;
89         frame_color_ = in.frame_color_;
90 }
91
92
93 void InsetText::setBuffer(Buffer & buf)
94 {
95         ParagraphList::iterator end = paragraphs().end();
96         for (ParagraphList::iterator it = paragraphs().begin(); it != end; ++it)
97                 it->setBuffer(buf);
98         Inset::setBuffer(buf);
99 }
100
101
102 void InsetText::clear()
103 {
104         ParagraphList & pars = paragraphs();
105         LASSERT(!pars.empty(), /**/);
106
107         // This is a gross hack...
108         Layout const & old_layout = pars.begin()->layout();
109
110         pars.clear();
111         pars.push_back(Paragraph());
112         pars.begin()->setInsetOwner(this);
113         pars.begin()->setLayout(old_layout);
114 }
115
116
117 Dimension const InsetText::dimension(BufferView const & bv) const
118 {
119         TextMetrics const & tm = bv.textMetrics(&text_);
120         Dimension dim = tm.dimension();
121         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
122         dim.des += TEXT_TO_INSET_OFFSET;
123         dim.asc += TEXT_TO_INSET_OFFSET;
124         return dim;
125 }
126
127
128 void InsetText::write(ostream & os) const
129 {
130         os << "Text\n";
131         text_.write(os);
132 }
133
134
135 void InsetText::read(Lexer & lex)
136 {
137         clear();
138
139         // delete the initial paragraph
140         Paragraph oldpar = *paragraphs().begin();
141         paragraphs().clear();
142         ErrorList errorList;
143         lex.setContext("InsetText::read");
144         bool res = text_.read(lex, errorList, this);
145
146         if (!res)
147                 lex.printError("Missing \\end_inset at this point. ");
148
149         // sanity check
150         // ensure we have at least one paragraph.
151         if (paragraphs().empty())
152                 paragraphs().push_back(oldpar);
153         // Force default font, if so requested
154         // This avoids paragraphs in buffer language that would have a
155         // foreign language after a document language change, and it ensures
156         // that all new text in ERT and similar gets the "latex" language,
157         // since new text inherits the language from the last position of the
158         // existing text.  As a side effect this makes us also robust against
159         // bugs in LyX that might lead to font changes in ERT in .lyx files.
160         fixParagraphsFont();
161 }
162
163
164 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
165 {
166         TextMetrics & tm = mi.base.bv->textMetrics(&text_);
167
168         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
169
170         // Hand font through to contained lyxtext:
171         tm.font_.fontInfo() = mi.base.font;
172         mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
173
174         // This can happen when a layout has a left and right margin,
175         // and the view is made very narrow. We can't do better than 
176         // to draw it partly out of view (bug 5890).
177         if (mi.base.textwidth < 1)
178                 mi.base.textwidth = 1;
179
180         if (hasFixedWidth())
181                 tm.metrics(mi, dim, mi.base.textwidth);
182         else
183                 tm.metrics(mi, dim);
184         mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
185         dim.asc += TEXT_TO_INSET_OFFSET;
186         dim.des += TEXT_TO_INSET_OFFSET;
187         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
188 }
189
190
191 void InsetText::draw(PainterInfo & pi, int x, int y) const
192 {
193         TextMetrics & tm = pi.base.bv->textMetrics(&text_);
194
195         if (drawFrame_ || pi.full_repaint) {
196                 int const w = tm.width() + TEXT_TO_INSET_OFFSET;
197                 int const yframe = y - TEXT_TO_INSET_OFFSET - tm.ascent();
198                 int const h = tm.height() + 2 * TEXT_TO_INSET_OFFSET;
199                 int const xframe = x + TEXT_TO_INSET_OFFSET / 2;
200                 if (pi.full_repaint)
201                         pi.pain.fillRectangle(xframe, yframe, w, h,
202                                 pi.backgroundColor(this));
203
204                 if (drawFrame_)
205                         pi.pain.rectangle(xframe, yframe, w, h, frameColor());
206         }
207         ColorCode const old_color = pi.background_color;
208         pi.background_color = pi.backgroundColor(this, false);
209
210         tm.draw(pi, x + TEXT_TO_INSET_OFFSET, y);
211
212         pi.background_color = old_color;
213 }
214
215
216 void InsetText::edit(Cursor & cur, bool front, EntryDirection entry_from)
217 {
218         pit_type const pit = front ? 0 : paragraphs().size() - 1;
219         pos_type pos = front ? 0 : paragraphs().back().size();
220
221         // if visual information is not to be ignored, move to extreme right/left
222         if (entry_from != ENTRY_DIRECTION_IGNORE) {
223                 Cursor temp_cur = cur;
224                 temp_cur.pit() = pit;
225                 temp_cur.pos() = pos;
226                 temp_cur.posVisToRowExtremity(entry_from == ENTRY_DIRECTION_LEFT);
227                 pos = temp_cur.pos();
228         }
229
230         text_.setCursor(cur.top(), pit, pos);
231         cur.clearSelection();
232         cur.finishUndo();
233 }
234
235
236 Inset * InsetText::editXY(Cursor & cur, int x, int y)
237 {
238         return cur.bv().textMetrics(&text_).editXY(cur, x, y);
239 }
240
241
242 void InsetText::doDispatch(Cursor & cur, FuncRequest & cmd)
243 {
244         LYXERR(Debug::ACTION, "InsetText::doDispatch()"
245                 << " [ cmd.action() = " << cmd.action() << ']');
246
247         if (getLayout().isPassThru()) {
248                 // Force any new text to latex_language FIXME: This
249                 // should only be necessary in constructor, but new
250                 // paragraphs that are created by pressing enter at
251                 // the start of an existing paragraph get the buffer
252                 // language and not latex_language, so we take this
253                 // brute force approach.
254                 cur.current_font.setLanguage(latex_language);
255                 cur.real_current_font.setLanguage(latex_language);
256         }
257
258         switch (cmd.action()) {
259         case LFUN_PASTE:
260         case LFUN_CLIPBOARD_PASTE:
261         case LFUN_SELECTION_PASTE:
262         case LFUN_PRIMARY_SELECTION_PASTE:
263                 text_.dispatch(cur, cmd);
264                 // If we we can only store plain text, we must reset all
265                 // attributes.
266                 // FIXME: Change only the pasted paragraphs
267                 fixParagraphsFont();
268                 break;
269
270         case LFUN_INSET_DISSOLVE: {
271                 bool const main_inset = &buffer().inset() == this;
272                 bool const target_inset = cmd.argument().empty() 
273                         || cmd.getArg(0) == insetName(lyxCode());
274                 bool const one_cell = nargs() == 1;
275
276                 if (!main_inset && target_inset && one_cell) {
277                         // Text::dissolveInset assumes that the cursor
278                         // is inside the Inset.
279                         if (&cur.inset() != this)
280                                 cur.pushBackward(*this);
281                         cur.beginUndoGroup();
282                         text_.dispatch(cur, cmd);
283                         cur.endUndoGroup();
284                 } else
285                         cur.undispatched();
286                 break;
287         }
288
289         default:
290                 text_.dispatch(cur, cmd);
291         }
292         
293         if (!cur.result().dispatched())
294                 Inset::doDispatch(cur, cmd);
295 }
296
297
298 bool InsetText::getStatus(Cursor & cur, FuncRequest const & cmd,
299         FuncStatus & status) const
300 {
301         switch (cmd.action()) {
302         case LFUN_INSET_DISSOLVE: {
303                 bool const main_inset = &buffer().inset() == this;
304                 bool const target_inset = cmd.argument().empty() 
305                         || cmd.getArg(0) == insetName(lyxCode());
306                 bool const one_cell = nargs() == 1;
307
308                 if (target_inset)
309                         status.setEnabled(!main_inset && one_cell);
310                 return target_inset;
311         }
312
313         default:
314                 // Dispatch only to text_ if the cursor is inside
315                 // the text_. It is not for context menus (bug 5797).
316                 bool ret = false;
317                 if (cur.text() == &text_)
318                         ret = text_.getStatus(cur, cmd, status);
319                 
320                 if (!ret)
321                         ret = Inset::getStatus(cur, cmd, status);
322                 return ret;
323         }
324 }
325
326
327 void InsetText::fixParagraphsFont()
328 {
329         if (!getLayout().isPassThru())
330                 return;
331
332         Font font(inherit_font, buffer().params().language);
333         font.setLanguage(latex_language);
334         ParagraphList::iterator par = paragraphs().begin();
335         ParagraphList::iterator const end = paragraphs().end();
336         while (par != end) {
337                 par->resetFonts(font);
338                 par->params().clear();
339                 ++par;
340         }
341 }
342
343
344 void InsetText::setChange(Change const & change)
345 {
346         ParagraphList::iterator pit = paragraphs().begin();
347         ParagraphList::iterator end = paragraphs().end();
348         for (; pit != end; ++pit) {
349                 pit->setChange(change);
350         }
351 }
352
353
354 void InsetText::acceptChanges()
355 {
356         text_.acceptChanges();
357 }
358
359
360 void InsetText::rejectChanges()
361 {
362         text_.rejectChanges();
363 }
364
365
366 void InsetText::validate(LaTeXFeatures & features) const
367 {
368         features.useInsetLayout(getLayout());
369         for_each(paragraphs().begin(), paragraphs().end(),
370                  bind(&Paragraph::validate, _1, ref(features)));
371 }
372
373
374 int InsetText::latex(odocstream & os, OutputParams const & runparams) const
375 {
376         // This implements the standard way of handling the LaTeX
377         // output of a text inset, either a command or an
378         // environment. Standard collapsable insets should not
379         // redefine this, non-standard ones may call this.
380         InsetLayout const & il = getLayout();
381         int rows = 0;
382         if (!il.latexname().empty()) {
383                 if (il.latextype() == InsetLayout::COMMAND) {
384                         // FIXME UNICODE
385                         if (runparams.moving_arg)
386                                 os << "\\protect";
387                         os << '\\' << from_utf8(il.latexname());
388                         if (!il.latexparam().empty())
389                                 os << from_utf8(il.latexparam());
390                         os << '{';
391                 } else if (il.latextype() == InsetLayout::ENVIRONMENT) {
392                         os << "%\n\\begin{" << from_utf8(il.latexname()) << "}\n";
393                         if (!il.latexparam().empty())
394                                 os << from_utf8(il.latexparam());
395                         rows += 2;
396                 }
397         }
398         OutputParams rp = runparams;
399         if (il.isPassThru())
400                 rp.pass_thru = true;
401         if (il.isNeedProtect())
402                 rp.moving_arg = true;
403
404         // Output the contents of the inset
405         TexRow texrow;
406         latexParagraphs(buffer(), text_, os, texrow, rp);
407         rows += texrow.rows();
408         runparams.encoding = rp.encoding;
409
410         if (!il.latexname().empty()) {
411                 if (il.latextype() == InsetLayout::COMMAND) {
412                         os << "}";
413                 } else if (il.latextype() == InsetLayout::ENVIRONMENT) {
414                         os << "\n\\end{" << from_utf8(il.latexname()) << "}\n";
415                         rows += 2;
416                 }
417         }
418         return rows;
419 }
420
421
422 int InsetText::plaintext(odocstream & os, OutputParams const & runparams) const
423 {
424         ParagraphList::const_iterator beg = paragraphs().begin();
425         ParagraphList::const_iterator end = paragraphs().end();
426         ParagraphList::const_iterator it = beg;
427         bool ref_printed = false;
428         int len = 0;
429         for (; it != end; ++it) {
430                 if (it != beg) {
431                         os << '\n';
432                         if (runparams.linelen > 0)
433                                 os << '\n';
434                 }
435                 odocstringstream oss;
436                 writePlaintextParagraph(buffer(), *it, oss, runparams, ref_printed);
437                 docstring const str = oss.str();
438                 os << str;
439                 // FIXME: len is not computed fully correctly; in principle,
440                 // we have to count the characters after the last '\n'
441                 len = str.size();
442         }
443
444         return len;
445 }
446
447
448 int InsetText::docbook(odocstream & os, OutputParams const & runparams) const
449 {
450         ParagraphList::const_iterator const beg = paragraphs().begin();
451
452         if (!undefined())
453                 sgml::openTag(os, getLayout().latexname(),
454                               beg->getID(buffer(), runparams) + getLayout().latexparam());
455
456         docbookParagraphs(text_, buffer(), os, runparams);
457
458         if (!undefined())
459                 sgml::closeTag(os, getLayout().latexname());
460
461         return 0;
462 }
463
464
465 docstring InsetText::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
466 {
467         return insetAsXHTML(xs, runparams, WriteEverything);
468 }
469
470
471 // FIXME XHTML
472 // There are cases where we may need to close open fonts and such
473 // and then re-open them when we are done. This would be the case, e.g.,
474 // if we were otherwise about to write:
475 //              <em>word <div class='foot'>footnote text.</div> emph</em>
476 // The problem isn't so much that the footnote text will get emphasized:
477 // we can handle that with CSS. The problem is that this is invalid XHTML.
478 // One solution would be to make the footnote <span>, but the problem is
479 // completely general, and so we'd have to make absolutely everything into
480 // span. What I think will work is to check if we're about to write "div" and,
481 // if so, try to close fonts, etc. 
482 // There are probably limits to how well we can do here, though, and we will
483 // have to rely upon users not putting footnotes inside noun-type insets.
484 docstring InsetText::insetAsXHTML(XHTMLStream & xs, OutputParams const & runparams,
485                                   XHTMLOptions opts) const
486 {
487         if (undefined()) {
488                 xhtmlParagraphs(text_, buffer(), xs, runparams);
489                 return docstring();
490         }
491
492         InsetLayout const & il = getLayout();
493         if (opts & WriteOuterTag)
494                 xs << html::StartTag(il.htmltag(), il.htmlattr());
495         if ((opts & WriteLabel) && !il.counter().empty()) {
496                 BufferParams const & bp = buffer().masterBuffer()->params();
497                 Counters & cntrs = bp.documentClass().counters();
498                 cntrs.step(il.counter(), OutputUpdate);
499                 // FIXME: translate to paragraph language
500                 if (!il.htmllabel().empty()) {
501                         docstring const lbl = 
502                                 cntrs.counterLabel(from_utf8(il.htmllabel()), bp.language->code());
503                         // FIXME is this check necessary?
504                         if (!lbl.empty()) {
505                                 xs << html::StartTag(il.htmllabeltag(), il.htmllabelattr());
506                                 xs << lbl;
507                                 xs << html::EndTag(il.htmllabeltag());
508                         }
509                 }
510         }
511
512         if (opts & WriteInnerTag)
513                 xs << html::StartTag(il.htmlinnertag(), il.htmlinnerattr());
514         OutputParams ours = runparams;
515         if (!il.isMultiPar() || opts == JustText)
516                 ours.html_make_pars = false;
517         xhtmlParagraphs(text_, buffer(), xs, ours);
518         if (opts & WriteInnerTag)
519                 xs << html::EndTag(il.htmlinnertag());
520         if (opts & WriteOuterTag)
521                 xs << html::EndTag(il.htmltag());
522         return docstring();
523 }
524
525
526 void InsetText::cursorPos(BufferView const & bv,
527                 CursorSlice const & sl, bool boundary, int & x, int & y) const
528 {
529         x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
530         y = bv.textMetrics(&text_).cursorY(sl, boundary);
531 }
532
533
534 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
535 {
536         clear();
537         Paragraph & first = paragraphs().front();
538         for (unsigned int i = 0; i < data.length(); ++i)
539                 first.insertChar(i, data[i], font, trackChanges);
540 }
541
542
543 void InsetText::setAutoBreakRows(bool flag)
544 {
545         if (flag == text_.autoBreakRows_)
546                 return;
547
548         text_.autoBreakRows_ = flag;
549         if (flag)
550                 return;
551
552         // remove previously existing newlines
553         ParagraphList::iterator it = paragraphs().begin();
554         ParagraphList::iterator end = paragraphs().end();
555         for (; it != end; ++it)
556                 for (int i = 0; i < it->size(); ++i)
557                         if (it->isNewline(i))
558                                 // do not track the change, because the user
559                                 // is not allowed to revert/reject it
560                                 it->eraseChar(i, false);
561 }
562
563
564 void InsetText::setDrawFrame(bool flag)
565 {
566         drawFrame_ = flag;
567 }
568
569
570 ColorCode InsetText::frameColor() const
571 {
572         return frame_color_;
573 }
574
575
576 void InsetText::setFrameColor(ColorCode col)
577 {
578         frame_color_ = col;
579 }
580
581
582 void InsetText::appendParagraphs(ParagraphList & plist)
583 {
584         // There is little we can do here to keep track of changes.
585         // As of 2006/10/20, appendParagraphs is used exclusively by
586         // LyXTabular::setMultiColumn. In this context, the paragraph break
587         // is lost irreversibly and the appended text doesn't really change
588
589         ParagraphList & pl = paragraphs();
590
591         ParagraphList::iterator pit = plist.begin();
592         ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
593         ++pit;
594         mergeParagraph(buffer().params(), pl,
595                        distance(pl.begin(), ins) - 1);
596
597         for_each(pit, plist.end(),
598                  bind(&ParagraphList::push_back, ref(pl), _1));
599 }
600
601
602 void InsetText::addPreview(DocIterator const & text_inset_pos,
603         PreviewLoader & loader) const
604 {
605         ParagraphList::const_iterator pit = paragraphs().begin();
606         ParagraphList::const_iterator pend = paragraphs().end();
607         int pidx = 0;
608
609         DocIterator inset_pos = text_inset_pos;
610         inset_pos.push_back(CursorSlice(*const_cast<InsetText *>(this)));
611
612         for (; pit != pend; ++pit, ++pidx) {
613                 InsetList::const_iterator it  = pit->insetList().begin();
614                 InsetList::const_iterator end = pit->insetList().end();
615                 inset_pos.pit() = pidx;
616                 for (; it != end; ++it) {
617                         inset_pos.pos() = it->pos;
618                         it->inset->addPreview(inset_pos, loader);
619                 }
620         }
621 }
622
623
624 ParagraphList const & InsetText::paragraphs() const
625 {
626         return text_.paragraphs();
627 }
628
629
630 ParagraphList & InsetText::paragraphs()
631 {
632         return text_.paragraphs();
633 }
634
635
636 void InsetText::updateBuffer(ParIterator const & it, UpdateType utype)
637 {
638         ParIterator it2 = it;
639         it2.forwardPos();
640         LASSERT(&it2.inset() == this && it2.pit() == 0, return);
641         if (producesOutput()) {
642                 InsetLayout const & il = getLayout();
643                 bool const save_layouts = utype == OutputUpdate && il.htmlisblock();
644                 Counters & cnt = buffer().masterBuffer()->params().documentClass().counters();
645                 if (save_layouts) {
646                         // LYXERR0("Entering " << name());
647                         cnt.clearLastLayout();
648                         // FIXME cnt.saveLastCounter()?
649                 }
650                 buffer().updateBuffer(it2, utype);
651                 if (save_layouts) {
652                         // LYXERR0("Exiting " << name());
653                         cnt.restoreLastLayout();
654                         // FIXME cnt.restoreLastCounter()?
655                 }
656         } else {
657                 DocumentClass const & tclass = buffer().masterBuffer()->params().documentClass();
658                 // Note that we do not need to call:
659                 //      tclass.counters().clearLastLayout()
660                 // since we are saving and restoring the existing counters, etc.
661                 Counters const savecnt = tclass.counters();
662                 buffer().updateBuffer(it2, utype);
663                 tclass.counters() = savecnt;
664         }
665 }
666
667
668 void InsetText::tocString(odocstream & os) const
669 {
670         os << text().asString(0, 1, AS_STR_LABEL | AS_STR_INSETS);
671 }
672
673
674
675 void InsetText::addToToc(DocIterator const & cdit)
676 {
677         DocIterator dit = cdit;
678         dit.push_back(CursorSlice(*this));
679         Toc & toc = buffer().tocBackend().toc("tableofcontents");
680
681         BufferParams const & bufparams = buffer_->params();
682         const int min_toclevel = bufparams.documentClass().min_toclevel();
683
684         // For each paragraph, traverse its insets and let them add
685         // their toc items
686         ParagraphList & pars = paragraphs();
687         pit_type pend = paragraphs().size();
688         for (pit_type pit = 0; pit != pend; ++pit) {
689                 Paragraph const & par = pars[pit];
690                 dit.pit() = pit;
691                 // the string that goes to the toc (could be the optarg)
692                 docstring tocstring;
693                 InsetList::const_iterator it  = par.insetList().begin();
694                 InsetList::const_iterator end = par.insetList().end();
695                 for (; it != end; ++it) {
696                         Inset & inset = *it->inset;
697                         dit.pos() = it->pos;
698                         //lyxerr << (void*)&inset << " code: " << inset.lyxCode() << std::endl;
699                         inset.addToToc(dit);
700                         switch (inset.lyxCode()) {
701                         case ARG_CODE: {
702                                 if (!tocstring.empty())
703                                         break;
704                                 dit.pos() = 0;
705                                 Paragraph const & insetpar =
706                                         *static_cast<InsetArgument&>(inset).paragraphs().begin();
707                                 if (!par.labelString().empty())
708                                         tocstring = par.labelString() + ' ';
709                                 tocstring += insetpar.asString(AS_STR_INSETS);
710                                 break;
711                         }
712                         default:
713                                 break;
714                         }
715                 }
716                 // now the toc entry for the paragraph
717                 int const toclevel = par.layout().toclevel;
718                 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel) {
719                         dit.pos() = 0;
720                         // insert this into the table of contents
721                         if (tocstring.empty())
722                                 tocstring = par.asString(AS_STR_LABEL | AS_STR_INSETS);
723                         toc.push_back(TocItem(dit, toclevel - min_toclevel,
724                                 tocstring, tocstring));
725                 }
726                 
727                 // And now the list of changes.
728                 par.addChangesToToc(dit, buffer());
729         }
730 }
731
732
733 bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
734 {
735         if (buffer().isClean())
736                 return Inset::notifyCursorLeaves(old, cur);
737         
738         // find text inset in old cursor
739         Cursor insetCur = old;
740         int scriptSlice = insetCur.find(this);
741         LASSERT(scriptSlice != -1, /**/);
742         insetCur.cutOff(scriptSlice);
743         LASSERT(&insetCur.inset() == this, /**/);
744         
745         // update the old paragraph's words
746         insetCur.paragraph().updateWords();
747         
748         return Inset::notifyCursorLeaves(old, cur);
749 }
750
751
752 bool InsetText::completionSupported(Cursor const & cur) const
753 {
754         //LASSERT(&cur.bv().cursor().inset() != this, return false);
755         return text_.completionSupported(cur);
756 }
757
758
759 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
760 {
761         return completionSupported(cur);
762 }
763
764
765 bool InsetText::automaticInlineCompletion() const
766 {
767         return lyxrc.completion_inline_text;
768 }
769
770
771 bool InsetText::automaticPopupCompletion() const
772 {
773         return lyxrc.completion_popup_text;
774 }
775
776
777 bool InsetText::showCompletionCursor() const
778 {
779         return lyxrc.completion_cursor_text;
780 }
781
782
783 CompletionList const * InsetText::createCompletionList(Cursor const & cur) const
784 {
785         return completionSupported(cur) ? text_.createCompletionList(cur) : 0;
786 }
787
788
789 docstring InsetText::completionPrefix(Cursor const & cur) const
790 {
791         if (!completionSupported(cur))
792                 return docstring();
793         return text_.completionPrefix(cur);
794 }
795
796
797 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
798         bool finished)
799 {
800         if (!completionSupported(cur))
801                 return false;
802
803         return text_.insertCompletion(cur, s, finished);
804 }
805
806
807 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y, 
808         Dimension & dim) const
809 {
810         TextMetrics const & tm = cur.bv().textMetrics(&text_);
811         tm.completionPosAndDim(cur, x, y, dim);
812 }
813
814
815 docstring InsetText::contextMenu(BufferView const &, int, int) const
816 {
817         return from_ascii("context-edit");
818 }
819
820
821 docstring InsetText::toolTipText(docstring prefix) const
822 {
823         static unsigned int max_length = 400; // five 80 column lines
824         OutputParams rp(&buffer().params().encoding());
825         odocstringstream oss;
826         oss << prefix;
827
828         ParagraphList::const_iterator beg = paragraphs().begin();
829         ParagraphList::const_iterator end = paragraphs().end();
830         ParagraphList::const_iterator it = beg;
831         bool ref_printed = false;
832         docstring str;
833
834         for (; it != end; ++it) {
835                 if (it != beg)
836                         oss << '\n';
837                 writePlaintextParagraph(buffer(), *it, oss, rp, ref_printed);
838                 str = oss.str();
839                 if (str.length() > max_length)
840                         break;
841         }
842         return support::wrapParas(str, 4, 80, 5);
843 }
844
845
846 InsetCaption const * InsetText::getCaptionInset() const
847 {
848         ParagraphList::const_iterator pit = paragraphs().begin();
849         for (; pit != paragraphs().end(); ++pit) {
850                 InsetList::const_iterator it = pit->insetList().begin();
851                 for (; it != pit->insetList().end(); ++it) {
852                         Inset & inset = *it->inset;
853                         if (inset.lyxCode() == CAPTION_CODE) {
854                                 InsetCaption const * ins =
855                                         static_cast<InsetCaption const *>(it->inset);
856                                 return ins;
857                         }
858                 }
859         }
860         return 0;
861 }
862
863
864 docstring InsetText::getCaptionText(OutputParams const & runparams) const
865 {
866         InsetCaption const * ins = getCaptionInset();
867         if (ins == 0)
868                 return docstring();
869
870         odocstringstream ods;
871         ins->getCaptionAsPlaintext(ods, runparams);
872         return ods.str();
873 }
874
875
876 docstring InsetText::getCaptionHTML(OutputParams const & runparams) const
877 {
878         InsetCaption const * ins = getCaptionInset();
879         if (ins == 0)
880                 return docstring();
881
882         odocstringstream ods;
883         XHTMLStream xs(ods);
884         docstring def = ins->getCaptionAsHTML(xs, runparams);
885         if (!def.empty())
886                 // should already have been escaped
887                 xs << XHTMLStream::ESCAPE_NONE << def << '\n';
888         return ods.str();
889 }
890
891
892 InsetText::XHTMLOptions operator|(InsetText::XHTMLOptions a1, InsetText::XHTMLOptions a2)
893 {
894         return static_cast<InsetText::XHTMLOptions>((int)a1 | (int)a2);
895 }
896
897 } // namespace lyx