]> git.lyx.org Git - lyx.git/blob - src/insets/InsetText.cpp
How about if we write a script to do some of this and stop doing it
[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_LAYOUT:
302                 status.setEnabled(!forcePlainLayout());
303                 return true;
304
305         case LFUN_LAYOUT_PARAGRAPH:
306         case LFUN_PARAGRAPH_PARAMS:
307         case LFUN_PARAGRAPH_PARAMS_APPLY:
308         case LFUN_PARAGRAPH_UPDATE:
309                 status.setEnabled(allowParagraphCustomization());
310                 return true;
311
312         case LFUN_INSET_DISSOLVE: {
313                 bool const main_inset = &buffer().inset() == this;
314                 bool const target_inset = cmd.argument().empty() 
315                         || cmd.getArg(0) == insetName(lyxCode());
316                 bool const one_cell = nargs() == 1;
317
318                 if (target_inset)
319                         status.setEnabled(!main_inset && one_cell);
320                 return target_inset;
321         }
322
323         default:
324                 // Dispatch only to text_ if the cursor is inside
325                 // the text_. It is not for context menus (bug 5797).
326                 bool ret = false;
327                 if (cur.text() == &text_)
328                         ret = text_.getStatus(cur, cmd, status);
329                 
330                 if (!ret)
331                         ret = Inset::getStatus(cur, cmd, status);
332                 return ret;
333         }
334 }
335
336
337 void InsetText::fixParagraphsFont()
338 {
339         if (!getLayout().isPassThru())
340                 return;
341
342         Font font(inherit_font, buffer().params().language);
343         font.setLanguage(latex_language);
344         ParagraphList::iterator par = paragraphs().begin();
345         ParagraphList::iterator const end = paragraphs().end();
346         while (par != end) {
347                 par->resetFonts(font);
348                 par->params().clear();
349                 ++par;
350         }
351 }
352
353
354 void InsetText::setChange(Change const & change)
355 {
356         ParagraphList::iterator pit = paragraphs().begin();
357         ParagraphList::iterator end = paragraphs().end();
358         for (; pit != end; ++pit) {
359                 pit->setChange(change);
360         }
361 }
362
363
364 void InsetText::acceptChanges()
365 {
366         text_.acceptChanges();
367 }
368
369
370 void InsetText::rejectChanges()
371 {
372         text_.rejectChanges();
373 }
374
375
376 void InsetText::validate(LaTeXFeatures & features) const
377 {
378         features.useInsetLayout(getLayout());
379         for_each(paragraphs().begin(), paragraphs().end(),
380                  bind(&Paragraph::validate, _1, ref(features)));
381 }
382
383
384 int InsetText::latex(odocstream & os, OutputParams const & runparams) const
385 {
386         // This implements the standard way of handling the LaTeX
387         // output of a text inset, either a command or an
388         // environment. Standard collapsable insets should not
389         // redefine this, non-standard ones may call this.
390         InsetLayout const & il = getLayout();
391         int rows = 0;
392         if (!il.latexname().empty()) {
393                 if (il.latextype() == InsetLayout::COMMAND) {
394                         // FIXME UNICODE
395                         if (runparams.moving_arg)
396                                 os << "\\protect";
397                         os << '\\' << from_utf8(il.latexname());
398                         if (!il.latexparam().empty())
399                                 os << from_utf8(il.latexparam());
400                         os << '{';
401                 } else if (il.latextype() == InsetLayout::ENVIRONMENT) {
402                         os << "%\n\\begin{" << from_utf8(il.latexname()) << "}\n";
403                         if (!il.latexparam().empty())
404                                 os << from_utf8(il.latexparam());
405                         rows += 2;
406                 }
407         }
408         OutputParams rp = runparams;
409         if (il.isPassThru())
410                 rp.verbatim = true;
411         if (il.isNeedProtect())
412                 rp.moving_arg = true;
413
414         // Output the contents of the inset
415         TexRow texrow;
416         latexParagraphs(buffer(), text_, os, texrow, rp);
417         rows += texrow.rows();
418
419         if (!il.latexname().empty()) {
420                 if (il.latextype() == InsetLayout::COMMAND) {
421                         os << "}";
422                 } else if (il.latextype() == InsetLayout::ENVIRONMENT) {
423                         os << "\n\\end{" << from_utf8(il.latexname()) << "}\n";
424                         rows += 2;
425                 }
426         }
427         return rows;
428 }
429
430
431 int InsetText::plaintext(odocstream & os, OutputParams const & runparams) const
432 {
433         ParagraphList::const_iterator beg = paragraphs().begin();
434         ParagraphList::const_iterator end = paragraphs().end();
435         ParagraphList::const_iterator it = beg;
436         bool ref_printed = false;
437         int len = 0;
438         for (; it != end; ++it) {
439                 if (it != beg) {
440                         os << '\n';
441                         if (runparams.linelen > 0)
442                                 os << '\n';
443                 }
444                 odocstringstream oss;
445                 writePlaintextParagraph(buffer(), *it, oss, runparams, ref_printed);
446                 docstring const str = oss.str();
447                 os << str;
448                 // FIXME: len is not computed fully correctly; in principle,
449                 // we have to count the characters after the last '\n'
450                 len = str.size();
451         }
452
453         return len;
454 }
455
456
457 int InsetText::docbook(odocstream & os, OutputParams const & runparams) const
458 {
459         ParagraphList::const_iterator const beg = paragraphs().begin();
460
461         if (!undefined())
462                 sgml::openTag(os, getLayout().latexname(),
463                               beg->getID(buffer(), runparams) + getLayout().latexparam());
464
465         docbookParagraphs(text_, buffer(), os, runparams);
466
467         if (!undefined())
468                 sgml::closeTag(os, getLayout().latexname());
469
470         return 0;
471 }
472
473
474 docstring InsetText::xhtml(XHTMLStream & xs, OutputParams const & runparams) const
475 {
476         return insetAsXHTML(xs, runparams, WriteEverything);
477 }
478
479
480 // FIXME XHTML
481 // There are cases where we may need to close open fonts and such
482 // and then re-open them when we are done. This would be the case, e.g.,
483 // if we were otherwise about to write:
484 //              <em>word <div class='foot'>footnote text.</div> emph</em>
485 // The problem isn't so much that the footnote text will get emphasized:
486 // we can handle that with CSS. The problem is that this is invalid XHTML.
487 // One solution would be to make the footnote <span>, but the problem is
488 // completely general, and so we'd have to make absolutely everything into
489 // span. What I think will work is to check if we're about to write "div" and,
490 // if so, try to close fonts, etc. 
491 // There are probably limits to how well we can do here, though, and we will
492 // have to rely upon users not putting footnotes inside noun-type insets.
493 docstring InsetText::insetAsXHTML(XHTMLStream & xs, OutputParams const & runparams,
494                                   XHTMLOptions opts) const
495 {
496         if (undefined()) {
497                 xhtmlParagraphs(text_, buffer(), xs, runparams);
498                 return docstring();
499         }
500
501         InsetLayout const & il = getLayout();
502         if (opts & WriteOuterTag)
503                 xs << StartTag(il.htmltag(), il.htmlattr());
504         if ((opts & WriteLabel) && !il.counter().empty()) {
505                 BufferParams const & bp = buffer().masterBuffer()->params();
506                 Counters & cntrs = bp.documentClass().counters();
507                 cntrs.step(il.counter());
508                 // FIXME: translate to paragraph language
509                 if (!il.htmllabel().empty()) {
510                         docstring const lbl = 
511                                 cntrs.counterLabel(from_utf8(il.htmllabel()), bp.language->code());
512                         // FIXME is this check necessary?
513                         if (!lbl.empty()) {
514                                 xs << StartTag(il.htmllabeltag(), il.htmllabelattr());
515                                 xs << lbl;
516                                 xs << EndTag(il.htmllabeltag());
517                         }
518                 }
519         }
520
521         if (opts & WriteInnerTag)
522                 xs << StartTag(il.htmlinnertag(), il.htmlinnerattr());
523         if (il.isMultiPar())
524                 xhtmlParagraphs(text_, buffer(), xs, runparams);
525         else {
526                 OutputParams ours = runparams;
527                 ours.html_make_pars = false;
528                 xhtmlParagraphs(text_, buffer(), xs, ours);
529         }
530         if (opts & WriteInnerTag)
531                 xs << EndTag(il.htmlinnertag());
532         if (opts & WriteOuterTag)
533                 xs << EndTag(il.htmltag());
534         return docstring();
535 }
536
537
538 void InsetText::cursorPos(BufferView const & bv,
539                 CursorSlice const & sl, bool boundary, int & x, int & y) const
540 {
541         x = bv.textMetrics(&text_).cursorX(sl, boundary) + TEXT_TO_INSET_OFFSET;
542         y = bv.textMetrics(&text_).cursorY(sl, boundary);
543 }
544
545
546 bool InsetText::showInsetDialog(BufferView *) const
547 {
548         return false;
549 }
550
551
552 void InsetText::setText(docstring const & data, Font const & font, bool trackChanges)
553 {
554         clear();
555         Paragraph & first = paragraphs().front();
556         for (unsigned int i = 0; i < data.length(); ++i)
557                 first.insertChar(i, data[i], font, trackChanges);
558 }
559
560
561 void InsetText::setAutoBreakRows(bool flag)
562 {
563         if (flag == text_.autoBreakRows_)
564                 return;
565
566         text_.autoBreakRows_ = flag;
567         if (flag)
568                 return;
569
570         // remove previously existing newlines
571         ParagraphList::iterator it = paragraphs().begin();
572         ParagraphList::iterator end = paragraphs().end();
573         for (; it != end; ++it)
574                 for (int i = 0; i < it->size(); ++i)
575                         if (it->isNewline(i))
576                                 // do not track the change, because the user
577                                 // is not allowed to revert/reject it
578                                 it->eraseChar(i, false);
579 }
580
581
582 void InsetText::setDrawFrame(bool flag)
583 {
584         drawFrame_ = flag;
585 }
586
587
588 ColorCode InsetText::frameColor() const
589 {
590         return frame_color_;
591 }
592
593
594 void InsetText::setFrameColor(ColorCode col)
595 {
596         frame_color_ = col;
597 }
598
599
600 void InsetText::appendParagraphs(ParagraphList & plist)
601 {
602         // There is little we can do here to keep track of changes.
603         // As of 2006/10/20, appendParagraphs is used exclusively by
604         // LyXTabular::setMultiColumn. In this context, the paragraph break
605         // is lost irreversibly and the appended text doesn't really change
606
607         ParagraphList & pl = paragraphs();
608
609         ParagraphList::iterator pit = plist.begin();
610         ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
611         ++pit;
612         mergeParagraph(buffer().params(), pl,
613                        distance(pl.begin(), ins) - 1);
614
615         for_each(pit, plist.end(),
616                  bind(&ParagraphList::push_back, ref(pl), _1));
617 }
618
619
620 void InsetText::addPreview(DocIterator const & text_inset_pos,
621         PreviewLoader & loader) const
622 {
623         ParagraphList::const_iterator pit = paragraphs().begin();
624         ParagraphList::const_iterator pend = paragraphs().end();
625         int pidx = 0;
626
627         DocIterator inset_pos = text_inset_pos;
628         inset_pos.push_back(CursorSlice(*const_cast<InsetText *>(this)));
629
630         for (; pit != pend; ++pit, ++pidx) {
631                 InsetList::const_iterator it  = pit->insetList().begin();
632                 InsetList::const_iterator end = pit->insetList().end();
633                 inset_pos.pit() = pidx;
634                 for (; it != end; ++it) {
635                         inset_pos.pos() = it->pos;
636                         it->inset->addPreview(inset_pos, loader);
637                 }
638         }
639 }
640
641
642 ParagraphList const & InsetText::paragraphs() const
643 {
644         return text_.paragraphs();
645 }
646
647
648 ParagraphList & InsetText::paragraphs()
649 {
650         return text_.paragraphs();
651 }
652
653
654 void InsetText::updateLabels(ParIterator const & it, bool out)
655 {
656         ParIterator it2 = it;
657         it2.forwardPos();
658         LASSERT(&it2.inset() == this && it2.pit() == 0, return);
659         if (producesOutput())
660                 buffer().updateLabels(it2, out);
661         else {
662                 DocumentClass const & tclass = buffer().masterBuffer()->params().documentClass();
663                 Counters const savecnt = tclass.counters();
664                 buffer().updateLabels(it2, out);
665                 tclass.counters() = savecnt;
666         }
667 }
668
669
670 void InsetText::tocString(odocstream & os) const
671 {
672         if (!getLayout().isInToc())
673                 return;
674         os << text().asString(0, 1, AS_STR_LABEL | AS_STR_INSETS);
675 }
676
677
678
679 void InsetText::addToToc(DocIterator const & cdit)
680 {
681         DocIterator dit = cdit;
682         dit.push_back(CursorSlice(*this));
683         Toc & toc = buffer().tocBackend().toc("tableofcontents");
684
685         BufferParams const & bufparams = buffer_->params();
686         const int min_toclevel = bufparams.documentClass().min_toclevel();
687
688         // For each paragraph, traverse its insets and let them add
689         // their toc items
690         ParagraphList & pars = paragraphs();
691         pit_type pend = paragraphs().size();
692         for (pit_type pit = 0; pit != pend; ++pit) {
693                 Paragraph const & par = pars[pit];
694                 dit.pit() = pit;
695                 // the string that goes to the toc (could be the optarg)
696                 docstring tocstring;
697                 InsetList::const_iterator it  = par.insetList().begin();
698                 InsetList::const_iterator end = par.insetList().end();
699                 for (; it != end; ++it) {
700                         Inset & inset = *it->inset;
701                         dit.pos() = it->pos;
702                         //lyxerr << (void*)&inset << " code: " << inset.lyxCode() << std::endl;
703                         inset.addToToc(dit);
704                         switch (inset.lyxCode()) {
705                         case OPTARG_CODE: {
706                                 if (!tocstring.empty())
707                                         break;
708                                 dit.pos() = 0;
709                                 Paragraph const & insetpar =
710                                         *static_cast<InsetOptArg&>(inset).paragraphs().begin();
711                                 if (!par.labelString().empty())
712                                         tocstring = par.labelString() + ' ';
713                                 tocstring += insetpar.asString(AS_STR_INSETS);
714                                 break;
715                         }
716                         default:
717                                 break;
718                         }
719                 }
720                 // now the toc entry for the paragraph
721                 int const toclevel = par.layout().toclevel;
722                 if (toclevel != Layout::NOT_IN_TOC && toclevel >= min_toclevel) {
723                         dit.pos() = 0;
724                         // insert this into the table of contents
725                         if (tocstring.empty())
726                                 tocstring = par.asString(AS_STR_LABEL | AS_STR_INSETS);
727                         toc.push_back(TocItem(dit, toclevel - min_toclevel, tocstring));
728                 }
729                 
730                 // And now the list of changes.
731                 par.addChangesToToc(dit, buffer());
732         }
733 }
734
735
736 bool InsetText::notifyCursorLeaves(Cursor const & old, Cursor & cur)
737 {
738         if (buffer().isClean())
739                 return Inset::notifyCursorLeaves(old, cur);
740         
741         // find text inset in old cursor
742         Cursor insetCur = old;
743         int scriptSlice = insetCur.find(this);
744         LASSERT(scriptSlice != -1, /**/);
745         insetCur.cutOff(scriptSlice);
746         LASSERT(&insetCur.inset() == this, /**/);
747         
748         // update the old paragraph's words
749         insetCur.paragraph().updateWords();
750         
751         return Inset::notifyCursorLeaves(old, cur);
752 }
753
754
755 bool InsetText::completionSupported(Cursor const & cur) const
756 {
757         //LASSERT(&cur.bv().cursor().inset() != this, return false);
758         return text_.completionSupported(cur);
759 }
760
761
762 bool InsetText::inlineCompletionSupported(Cursor const & cur) const
763 {
764         return completionSupported(cur);
765 }
766
767
768 bool InsetText::automaticInlineCompletion() const
769 {
770         return lyxrc.completion_inline_text;
771 }
772
773
774 bool InsetText::automaticPopupCompletion() const
775 {
776         return lyxrc.completion_popup_text;
777 }
778
779
780 bool InsetText::showCompletionCursor() const
781 {
782         return lyxrc.completion_cursor_text;
783 }
784
785
786 CompletionList const * InsetText::createCompletionList(Cursor const & cur) const
787 {
788         return completionSupported(cur) ? text_.createCompletionList(cur) : 0;
789 }
790
791
792 docstring InsetText::completionPrefix(Cursor const & cur) const
793 {
794         if (!completionSupported(cur))
795                 return docstring();
796         return text_.completionPrefix(cur);
797 }
798
799
800 bool InsetText::insertCompletion(Cursor & cur, docstring const & s,
801         bool finished)
802 {
803         if (!completionSupported(cur))
804                 return false;
805
806         return text_.insertCompletion(cur, s, finished);
807 }
808
809
810 void InsetText::completionPosAndDim(Cursor const & cur, int & x, int & y, 
811         Dimension & dim) const
812 {
813         TextMetrics const & tm = cur.bv().textMetrics(&text_);
814         tm.completionPosAndDim(cur, x, y, dim);
815 }
816
817
818 docstring InsetText::contextMenu(BufferView const &, int, int) const
819 {
820         return from_ascii("context-edit");
821 }
822
823
824 InsetCaption const * InsetText::getCaptionInset() const
825 {
826         ParagraphList::const_iterator pit = paragraphs().begin();
827         for (; pit != paragraphs().end(); ++pit) {
828                 InsetList::const_iterator it = pit->insetList().begin();
829                 for (; it != pit->insetList().end(); ++it) {
830                         Inset & inset = *it->inset;
831                         if (inset.lyxCode() == CAPTION_CODE) {
832                                 InsetCaption const * ins =
833                                         static_cast<InsetCaption const *>(it->inset);
834                                 return ins;
835                         }
836                 }
837         }
838         return 0;
839 }
840
841
842 docstring InsetText::getCaptionText(OutputParams const & runparams) const
843 {
844         InsetCaption const * ins = getCaptionInset();
845         if (ins == 0)
846                 return docstring();
847
848         odocstringstream ods;
849         ins->getCaptionAsPlaintext(ods, runparams);
850         return ods.str();
851 }
852
853
854 docstring InsetText::getCaptionHTML(OutputParams const & runparams) const
855 {
856         InsetCaption const * ins = getCaptionInset();
857         if (ins == 0)
858                 return docstring();
859
860         odocstringstream ods;
861         XHTMLStream xs(ods);
862         docstring def = ins->getCaptionAsHTML(xs, runparams);
863         if (!def.empty())
864                 // should already have been escaped
865                 xs << XHTMLStream::NextRaw() << def << '\n';
866         return ods.str();
867 }
868
869
870 InsetText::XHTMLOptions operator|(InsetText::XHTMLOptions a1, InsetText::XHTMLOptions a2)
871 {
872         return static_cast<InsetText::XHTMLOptions>((int)a1 | (int)a2);
873 }
874
875 } // namespace lyx