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