]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
Always use std::endl with lyxerr
[lyx.git] / src / insets / insettext.C
1 /**
2  * \file insettext.C
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 "buffer.h"
16 #include "BufferView.h"
17 #include "bufferview_funcs.h"
18 #include "CutAndPaste.h"
19 #include "debug.h"
20 #include "dimension.h"
21 #include "funcrequest.h"
22 #include "gettext.h"
23 #include "errorlist.h"
24 #include "intl.h"
25 #include "LaTeXFeatures.h"
26 #include "LColor.h"
27 #include "Lsstream.h"
28 #include "lyxfont.h"
29 #include "lyxcursor.h"
30 #include "lyxfind.h"
31 #include "lyxlex.h"
32 #include "lyxrow.h"
33 #include "lyxrc.h"
34 #include "lyxtext.h"
35 #include "paragraph.h"
36 #include "ParagraphParameters.h"
37 #include "trans_mgr.h"
38 #include "undo_funcs.h"
39 #include "WordLangTuple.h"
40 #include "paragraph_funcs.h"
41 #include "sgml.h"
42 #include "rowpainter.h"
43 #include "insetnewline.h"
44 #include "metricsinfo.h"
45
46 #include "frontends/Alert.h"
47 #include "frontends/Dialogs.h"
48 #include "frontends/font_metrics.h"
49 #include "frontends/LyXView.h"
50 #include "frontends/Painter.h"
51 #include "frontends/screen.h"
52
53 #include "support/textutils.h"
54 #include "support/LAssert.h"
55 #include "support/lstrings.h"
56 #include "support/lyxalgo.h" // lyx::count
57
58 #include <boost/bind.hpp>
59
60 #include <fstream>
61 #include <algorithm>
62 #include <cstdlib>
63
64 using std::ostream;
65 using std::ifstream;
66 using std::endl;
67 using std::min;
68 using std::max;
69 using std::make_pair;
70 using std::vector;
71 using std::pair;
72 using std::for_each;
73 using std::auto_ptr;
74
75 using namespace lyx::support;
76 using namespace lyx::graphics;
77 using namespace bv_funcs;
78
79 using lyx::pos_type;
80 using lyx::textclass_type;
81
82
83 // we should get rid of these (Andre')
84
85 void InsetText::saveLyXTextState() const
86 {
87         // check if my paragraphs are still valid
88         ParagraphList::iterator it = const_cast<ParagraphList&>(paragraphs).begin();
89         ParagraphList::iterator end = const_cast<ParagraphList&>(paragraphs).end();
90         for (; it != end; ++it)
91                 if (it == text_.cursor.par())
92                         break;
93
94         if (it != end && text_.cursor.pos() <= it->size())
95                 sstate = text_; // slicing intended
96         else
97                 sstate.cursor.par(end);
98 }
99
100
101 void InsetText::restoreLyXTextState() const
102 {
103         if (sstate.cursor.par() == const_cast<ParagraphList&>(paragraphs).end())
104                 return;
105
106         text_.selection.set(true);
107         // at this point just to avoid the DEPM when setting the cursor
108         text_.selection.mark(sstate.selection.mark());
109         if (sstate.selection.set()) {
110                 text_.setCursor(sstate.selection.start.par(),
111                         sstate.selection.start.pos(),
112                         true, sstate.selection.start.boundary());
113                 text_.selection.cursor = text_.cursor;
114                 text_.setCursor(sstate.selection.end.par(), sstate.selection.end.pos(),
115                         true, sstate.selection.end.boundary());
116                 text_.setSelection();
117                 text_.setCursor(sstate.cursor.par(), sstate.cursor.pos());
118         } else {
119                 text_.setCursor(sstate.cursor.par(), sstate.cursor.pos(),
120                         true, sstate.cursor.boundary());
121                 text_.selection.cursor = text_.cursor;
122                 text_.selection.set(false);
123         }
124 }
125
126
127 InsetText::InsetText(BufferParams const & bp)
128         : UpdatableInset(), text_(0, this)
129 {
130         paragraphs.push_back(Paragraph());
131         paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
132         if (bp.tracking_changes)
133                 paragraphs.begin()->trackChanges();
134         init(0);
135 }
136
137
138 InsetText::InsetText(InsetText const & in)
139         : UpdatableInset(in), text_(0, this)
140 {
141         init(&in);
142 }
143
144
145 InsetText & InsetText::operator=(InsetText const & it)
146 {
147         init(&it);
148         return *this;
149 }
150
151
152 void InsetText::init(InsetText const * ins)
153 {
154         if (ins) {
155                 textwidth_ = ins->textwidth_;
156                 text_.bv_owner = ins->text_.bv_owner;
157                 setParagraphData(ins->paragraphs);
158                 autoBreakRows = ins->autoBreakRows;
159                 drawFrame_ = ins->drawFrame_;
160                 frame_color = ins->frame_color;
161         } else {
162                 textwidth_ = 0; // broken
163                 drawFrame_ = NEVER;
164                 frame_color = LColor::insetframe;
165                 autoBreakRows = false;
166         }
167         the_locking_inset = 0;
168         for_each(paragraphs.begin(), paragraphs.end(),
169                  boost::bind(&Paragraph::setInsetOwner, _1, this));
170         top_y = 0;
171         no_selection = true;
172         need_update = FULL;
173         drawTextXOffset = 0;
174         drawTextYOffset = 0;
175         locked = false;
176         old_par = paragraphs.end();
177         last_drawn_width = -1;
178         sstate.cursor.par(paragraphs.end());
179         in_insetAllowed = false;
180 }
181
182
183 void InsetText::clear(bool just_mark_erased)
184 {
185         if (just_mark_erased) {
186                 ParagraphList::iterator it = paragraphs.begin();
187                 ParagraphList::iterator end = paragraphs.end();
188                 for (; it != end; ++it) {
189                         it->markErased();
190                 }
191                 need_update = FULL;
192                 return;
193         }
194
195         // This is a gross hack...
196         LyXLayout_ptr old_layout = paragraphs.begin()->layout();
197
198         paragraphs.clear();
199         paragraphs.push_back(Paragraph());
200         paragraphs.begin()->setInsetOwner(this);
201         paragraphs.begin()->layout(old_layout);
202
203         reinitLyXText();
204         need_update = INIT;
205 }
206
207
208 auto_ptr<InsetBase> InsetText::clone() const
209 {
210         return auto_ptr<InsetBase>(new InsetText(*this));
211 }
212
213
214 void InsetText::write(Buffer const * buf, ostream & os) const
215 {
216         os << "Text\n";
217         writeParagraphData(buf, os);
218 }
219
220
221 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
222 {
223         ParagraphList::const_iterator it = paragraphs.begin();
224         ParagraphList::const_iterator end = paragraphs.end();
225         Paragraph::depth_type dth = 0;
226         for (; it != end; ++it) {
227                 it->write(buf, os, buf->params, dth);
228         }
229 }
230
231
232 void InsetText::read(Buffer const * buf, LyXLex & lex)
233 {
234         string token;
235         Paragraph::depth_type depth = 0;
236
237         clear(false);
238
239         if (buf->params.tracking_changes)
240                 paragraphs.begin()->trackChanges();
241
242         // delete the initial paragraph
243         paragraphs.clear();
244         ParagraphList::iterator pit = paragraphs.begin();
245
246         while (lex.isOK()) {
247                 lex.nextToken();
248                 token = lex.getString();
249                 if (token.empty())
250                         continue;
251                 if (token == "\\end_inset") {
252                         break;
253                 }
254
255                 if (token == "\\end_document") {
256                         lex.printError("\\end_document read in inset! Error in document!");
257                         return;
258                 }
259
260                 // FIXME: ugly.
261                 const_cast<Buffer*>(buf)->readParagraph(lex, token, paragraphs, pit, depth);
262         }
263
264         pit = paragraphs.begin();
265         ParagraphList::iterator const end = paragraphs.end();
266         for (; pit != end; ++pit)
267                 pit->setInsetOwner(this);
268
269         if (token != "\\end_inset") {
270                 lex.printError("Missing \\end_inset at this point. "
271                                            "Read: `$$Token'");
272         }
273         need_update = FULL;
274 }
275
276
277 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
278 {
279         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
280
281         textwidth_ = mi.base.textwidth;
282         BufferView * bv = mi.base.bv;
283         setViewCache(bv);
284         text_.metrics(mi, dim);
285         dim.asc += TEXT_TO_INSET_OFFSET;
286         dim.des += TEXT_TO_INSET_OFFSET;
287         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
288         dim.wid = max(dim.wid, 10);
289         dim_ = dim;
290 }
291
292
293 int InsetText::textWidth() const
294 {
295         return textwidth_;
296 }
297
298
299 void InsetText::draw(PainterInfo & pi, int x, int baseline) const
300 {
301         // update our idea of where we are. Clearly, we should
302         // not have to know this information.
303         top_x = x;
304
305         int const start_x = x;
306
307         BufferView * bv = pi.base.bv;
308         Painter & pain = pi.pain;
309
310         // repaint the background if needed
311         if (backgroundColor() != LColor::background)
312                 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
313
314         // no draw is necessary !!!
315         if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
316                 top_baseline = baseline;
317                 need_update = NONE;
318                 return;
319         }
320
321         bv->hideCursor();
322
323         if (!owner())
324                 x += scroll();
325
326         top_baseline = baseline;
327         top_y = baseline - dim_.asc;
328
329         if (last_drawn_width != dim_.wid) {
330                 need_update |= FULL;
331                 last_drawn_width = dim_.wid;
332         }
333
334         if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
335                 inset_x = cix() - x + drawTextXOffset;
336                 inset_y = ciy() + drawTextYOffset;
337         }
338
339         x += TEXT_TO_INSET_OFFSET;
340
341         RowList::iterator rit = text_.rows().begin();
342         RowList::iterator end = text_.rows().end();
343
344         int y_offset = baseline - rit->ascent_of_text();
345         int y2 = pain.paperHeight();
346         int first = 0;
347         int y = y_offset;
348         while (rit != end && y + rit->height() <= 0) {
349                 y += rit->height();
350                 first += rit->height();
351                 ++rit;
352         }
353         if (y_offset < 0) {
354                 text_.top_y(-y_offset);
355                 first = y;
356                 y_offset = 0;
357         } else {
358                 text_.top_y(first);
359                 first = 0;
360         }
361
362         int yo = y_offset + first;
363
364         paintRows(*bv, text_, rit, x, 0, yo, y2, yo);
365
366         if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
367                 drawFrame(pain, int(start_x));
368
369         if (need_update != INIT)
370                 need_update = NONE;
371 }
372
373
374 void InsetText::drawFrame(Painter & pain, int x) const
375 {
376         static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
377         frame_x = x + ttoD2;
378         frame_y = top_baseline - dim_.asc + ttoD2;
379         frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
380         frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
381         pain.rectangle(frame_x, frame_y, frame_w, frame_h,
382                        frame_color);
383 }
384
385
386 void InsetText::setUpdateStatus(int what) const
387 {
388         need_update |= what;
389         // this to not draw a selection when we redraw all of it!
390         if (need_update & CURSOR && !(need_update & SELECTION)) {
391                 if (text_.selection.set())
392                         need_update = FULL;
393                 text_.clearSelection();
394         }
395 }
396
397
398 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
399 {
400         if (!bv)
401                 return;
402
403         if (!autoBreakRows && paragraphs.size() > 1)
404                 collapseParagraphs(bv);
405
406         text_.partialRebreak();
407         setUpdateStatus(what);
408         bool flag = mark_dirty ||
409                 (need_update != CURSOR && need_update != NONE) ||
410                 text_.selection.set();
411         if (!text_.selection.set())
412                 text_.selection.cursor = text_.cursor;
413
414         bv->fitCursor();
415
416         if (flag)
417                 bv->updateInset(const_cast<InsetText *>(this));
418
419         if (need_update == CURSOR)
420                 need_update = NONE;
421         bv->owner()->view_state_changed();
422         bv->owner()->updateMenubar();
423         bv->owner()->updateToolbar();
424         if (old_par != cpar()) {
425                 bv->owner()->setLayout(cpar()->layout()->name());
426                 old_par = cpar();
427         }
428 }
429
430
431 string const InsetText::editMessage() const
432 {
433         return _("Opened Text Inset");
434 }
435
436
437 void InsetText::insetUnlock(BufferView * bv)
438 {
439         if (the_locking_inset) {
440                 the_locking_inset->insetUnlock(bv);
441                 the_locking_inset = 0;
442                 updateLocal(bv, CURSOR_PAR, false);
443         }
444         no_selection = true;
445         locked = false;
446         int code = NONE;
447
448         if (text_.selection.set()) {
449                 text_.clearSelection();
450                 code = FULL;
451         } else if (owner()) {
452                 bv->owner()->setLayout(owner()->getLyXText(bv)
453                                        ->cursor.par()->layout()->name());
454         } else
455                 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
456         // hack for deleteEmptyParMech
457         ParagraphList::iterator first_par = paragraphs.begin();
458         if (!first_par->empty()) {
459                 text_.setCursor(first_par, 0);
460         } else if (paragraphs.size() > 1) {
461                 text_.setCursor(boost::next(first_par), 0);
462         }
463 #if 0
464         updateLocal(bv, code, false);
465 #else
466         if (code != NONE)
467                 setUpdateStatus(code);
468 #endif
469 }
470
471
472 void InsetText::lockInset(BufferView * bv)
473 {
474         locked = true;
475         the_locking_inset = 0;
476         inset_pos = inset_x = inset_y = 0;
477         inset_boundary = false;
478         inset_par = paragraphs.end();
479         old_par = paragraphs.end();
480         text_.setCursorIntern(paragraphs.begin(), 0);
481         text_.clearSelection();
482         finishUndo();
483         // If the inset is empty set the language of the current font to the
484         // language to the surronding text (if different).
485         if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
486                 bv->getParentLanguage(this) != text_.current_font.language()) {
487                 LyXFont font(LyXFont::ALL_IGNORE);
488                 font.setLanguage(bv->getParentLanguage(this));
489                 setFont(bv, font, false);
490         }
491         int code = CURSOR;
492         if (drawFrame_ == LOCKED)
493                 code = CURSOR|DRAW_FRAME;
494         setUpdateStatus(code);
495 }
496
497
498 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
499 {
500         the_locking_inset = inset;
501         inset_x = cix() - top_x + drawTextXOffset;
502         inset_y = ciy() + drawTextYOffset;
503         inset_pos = cpos();
504         inset_par = cpar();
505         inset_boundary = cboundary();
506         //updateLocal(bv, CURSOR, false);
507 }
508
509
510 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
511 {
512         lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
513                               << inset << "): " << endl;
514         if (!inset)
515                 return false;
516         if (!the_locking_inset) {
517                 ParagraphList::iterator pit = paragraphs.begin();
518                 ParagraphList::iterator pend = paragraphs.end();
519
520                 int const id = inset->id();
521                 for (; pit != pend; ++pit) {
522                         InsetList::iterator it = pit->insetlist.begin();
523                         InsetList::iterator const end = pit->insetlist.end();
524                         for (; it != end; ++it) {
525                                 if (it->inset == inset) {
526                                         lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
527                                         text_.setCursorIntern(pit, it->pos);
528                                         lyxerr << "InsetText::lockInsetInInset: 1 b" << endl;
529                                         lyxerr << "bv: " << bv << " inset: " << inset << endl;
530                                         lockInset(bv, inset);
531                                         lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
532                                         return true;
533                                 }
534                                 if (it->inset->getInsetFromID(id)) {
535                                         lyxerr << "InsetText::lockInsetInInset: 2" << endl;
536                                         text_.setCursorIntern(pit, it->pos);
537                                         it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
538                                         return the_locking_inset->lockInsetInInset(bv, inset);
539                                 }
540                         }
541                 }
542                 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
543                 return false;
544         }
545         if (inset == cpar()->getInset(cpos())) {
546                 lyxerr[Debug::INSETS] << "OK" << endl;
547                 lockInset(bv, inset);
548                 return true;
549         }
550
551         if (the_locking_inset && the_locking_inset == inset) {
552                 if (cpar() == inset_par && cpos() == inset_pos) {
553                         lyxerr[Debug::INSETS] << "OK" << endl;
554                         inset_x = cix() - top_x + drawTextXOffset;
555                         inset_y = ciy() + drawTextYOffset;
556                 } else {
557                         lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
558                 }
559         } else if (the_locking_inset) {
560                 lyxerr[Debug::INSETS] << "MAYBE" << endl;
561                 return the_locking_inset->lockInsetInInset(bv, inset);
562         }
563         lyxerr[Debug::INSETS] << "NOT OK" << endl;
564         return false;
565 }
566
567
568 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
569                                    bool lr)
570 {
571         if (!the_locking_inset)
572                 return false;
573         if (the_locking_inset == inset) {
574                 the_locking_inset->insetUnlock(bv);
575                 getLyXText(bv)->updateInset(inset);
576                 the_locking_inset = 0;
577                 if (lr)
578                         moveRightIntern(bv, true, false);
579                 old_par = paragraphs.end(); // force layout setting
580                 if (scroll())
581                         scroll(bv, 0.0F);
582                 else
583                         updateLocal(bv, CURSOR, false);
584                 return true;
585         }
586         return the_locking_inset->unlockInsetInInset(bv, inset, lr);
587 }
588
589
590 bool InsetText::updateInsetInInset(BufferView * bv, InsetOld * inset)
591 {
592         if (!autoBreakRows && paragraphs.size() > 1)
593                 collapseParagraphs(bv);
594
595         if (inset == this)
596                 return true;
597
598         if (inset->owner() != this) {
599                 int ustat = CURSOR_PAR;
600                 bool found = false;
601                 UpdatableInset * tl_inset = the_locking_inset;
602                 if (tl_inset)
603                         found = tl_inset->updateInsetInInset(bv, inset);
604                 if (!found) {
605                         tl_inset = static_cast<UpdatableInset *>(inset);
606                         while(tl_inset->owner() && tl_inset->owner() != this)
607                                 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
608                         if (!tl_inset->owner())
609                                 return false;
610                         found = tl_inset->updateInsetInInset(bv, inset);
611                         ustat = FULL;
612                 } else {
613                         text_.updateInset(tl_inset);
614                         setUpdateStatus(ustat);
615                 }
616                 return found;
617         }
618         bool found = text_.updateInset(inset);
619         if (found) {
620                 setUpdateStatus(CURSOR_PAR);
621                 if (the_locking_inset &&
622                     cpar() == inset_par && cpos() == inset_pos)
623                 {
624                         inset_x = cix() - top_x + drawTextXOffset;
625                         inset_y = ciy() + drawTextYOffset;
626                 }
627         }
628         return found;
629 }
630
631
632 void InsetText::lfunMousePress(FuncRequest const & cmd)
633 {
634         no_selection = true;
635
636         // use this to check mouse motion for selection!
637         mouse_x = cmd.x;
638         mouse_y = cmd.y;
639
640         BufferView * bv = cmd.view();
641         FuncRequest cmd1 = cmd;
642         cmd1.x -= inset_x;
643         cmd1.y -= inset_y;
644         if (!locked)
645                 lockInset(bv);
646
647         int tmp_x = cmd.x - drawTextXOffset;
648         int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
649         InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
650
651         if (the_locking_inset) {
652                 if (the_locking_inset == inset) {
653                         the_locking_inset->localDispatch(cmd1);
654                         return;
655                 }
656                 // otherwise only unlock the_locking_inset
657                 the_locking_inset->insetUnlock(bv);
658                 the_locking_inset = 0;
659         }
660         if (!inset)
661                 no_selection = false;
662
663         if (bv->theLockingInset()) {
664                 if (isHighlyEditableInset(inset)) {
665                         // We just have to lock the inset before calling a
666                         // PressEvent on it!
667                         UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
668                         if (!bv->lockInset(uinset)) {
669                                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
670                         }
671                         inset->localDispatch(cmd1);
672                         if (the_locking_inset)
673                                 updateLocal(bv, CURSOR, false);
674                         return;
675                 }
676         }
677         if (!inset) {
678                 bool paste_internally = false;
679                 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
680                         localDispatch(FuncRequest(bv, LFUN_COPY));
681                         paste_internally = true;
682                 }
683                 int old_top_y = text_.top_y();
684
685                 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
686                                              cmd.y + dim_.asc);
687                 // set the selection cursor!
688                 text_.selection.cursor = text_.cursor;
689                 text_.cursor.x_fix(text_.cursor.x());
690
691                 if (text_.selection.set()) {
692                         text_.clearSelection();
693                         updateLocal(bv, FULL, false);
694                 } else {
695                         text_.clearSelection();
696                         updateLocal(bv, CURSOR, false);
697                 }
698
699                 bv->owner()->setLayout(cpar()->layout()->name());
700
701                 // we moved the view we cannot do mouse selection in this case!
702                 if (getLyXText(bv)->top_y() != old_top_y)
703                         no_selection = true;
704                 old_par = cpar();
705                 // Insert primary selection with middle mouse
706                 // if there is a local selection in the current buffer,
707                 // insert this
708                 if (cmd.button() == mouse_button::button2) {
709                         if (paste_internally)
710                                 localDispatch(FuncRequest(bv, LFUN_PASTE));
711                         else
712                                 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
713                 }
714         } else {
715                 getLyXText(bv)->clearSelection();
716         }
717 }
718
719
720 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
721 {
722         BufferView * bv = cmd.view();
723         FuncRequest cmd1 = cmd;
724         cmd1.x -= inset_x;
725         cmd1.y -= inset_y;
726
727         no_selection = true;
728         if (the_locking_inset)
729                 return the_locking_inset->localDispatch(cmd1);
730
731         int tmp_x = cmd.x - drawTextXOffset;
732         int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
733         InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
734         bool ret = false;
735         if (inset) {
736 // This code should probably be removed now. Simple insets
737 // (!highlyEditable) can actually take the localDispatch,
738 // and turn it into edit() if necessary. But we still
739 // need to deal properly with the whole relative vs.
740 // absolute mouse co-ords thing in a realiable, sensible way
741 #if 0
742                 if (isHighlyEditableInset(inset))
743                         ret = inset->localDispatch(cmd1);
744                 else {
745                         inset_x = cix(bv) - top_x + drawTextXOffset;
746                         inset_y = ciy() + drawTextYOffset;
747                         cmd1.x = cmd.x - inset_x;
748                         cmd1.y = cmd.x - inset_y;
749                         inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
750                         ret = true;
751                 }
752 #endif
753                 ret = inset->localDispatch(cmd1);
754                 updateLocal(bv, CURSOR_PAR, false);
755
756         }
757         return ret;
758 }
759
760
761 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
762 {
763         FuncRequest cmd1 = cmd;
764         cmd1.x -= inset_x;
765         cmd1.y -= inset_y;
766
767         if (the_locking_inset) {
768                 the_locking_inset->localDispatch(cmd1);
769                 return;
770         }
771
772         if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
773                 return;
774
775         BufferView * bv = cmd.view();
776         LyXCursor cur = text_.cursor;
777         text_.setCursorFromCoordinates
778                 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
779         text_.cursor.x_fix(text_.cursor.x());
780         if (cur == text_.cursor)
781                 return;
782         text_.setSelection();
783         updateLocal(bv, SELECTION, false);
784 }
785
786
787 InsetOld::RESULT InsetText::localDispatch(FuncRequest const & cmd)
788 {
789         BufferView * bv = cmd.view();
790         setViewCache(bv);
791
792         switch (cmd.action) {
793         case LFUN_INSET_EDIT: {
794                 UpdatableInset::localDispatch(cmd);
795
796                 if (!bv->lockInset(this)) {
797                         lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
798                         return DISPATCHED;
799                 }
800
801                 locked = true;
802                 the_locking_inset = 0;
803                 inset_pos = 0;
804                 inset_x = 0;
805                 inset_y = 0;
806                 inset_boundary = false;
807                 inset_par = paragraphs.end();
808                 old_par = paragraphs.end();
809
810
811                 if (cmd.argument.size()) {
812                         if (cmd.argument == "left")
813                                 text_.setCursorIntern(paragraphs.begin(), 0);
814                         else {
815                                 ParagraphList::iterator it = boost::prior(paragraphs.end());
816                                 text_.setCursor(it, it->size());
817                         }
818                 } else {
819                         int tmp_y = (cmd.y < 0) ? 0 : cmd.y;
820                         // we put here -1 and not button as now the button in the
821                         // edit call should not be needed we will fix this in 1.3.x
822                         // cycle hopefully (Jug 20020509)
823                         // FIXME: GUII I've changed this to none: probably WRONG
824                         if (!checkAndActivateInset(bv, cmd.x, tmp_y, mouse_button::none)) {
825                                 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
826                                                                         cmd.y + dim_.asc);
827                                 text_.cursor.x_fix(text_.cursor.x());
828                         }
829                 }
830
831                 text_.clearSelection();
832                 finishUndo();
833
834                 // If the inset is empty set the language of the current font to the
835                 // language to the surronding text (if different).
836                 if (paragraphs.begin()->empty() &&
837                     paragraphs.size() == 1 &&
838                     bv->getParentLanguage(this) != text_.current_font.language())
839                 {
840                         LyXFont font(LyXFont::ALL_IGNORE);
841                         font.setLanguage(bv->getParentLanguage(this));
842                         setFont(bv, font, false);
843                 }
844
845                 int code = CURSOR;
846                 if (drawFrame_ == LOCKED)
847                         code = CURSOR | DRAW_FRAME;
848
849                 updateLocal(bv, code, false);
850                 // Tell the paragraph dialog that we've entered an insettext.
851                 bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
852                 return DISPATCHED;
853         }
854
855         case LFUN_MOUSE_PRESS:
856                 lfunMousePress(cmd);
857                 return DISPATCHED;
858
859         case LFUN_MOUSE_MOTION:
860                 lfunMouseMotion(cmd);
861                 return DISPATCHED;
862
863         case LFUN_MOUSE_RELEASE:
864                 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
865
866         default:
867                 break;
868         }
869
870         bool was_empty = (paragraphs.begin()->empty() &&
871                           paragraphs.size() == 1);
872
873         no_selection = false;
874         RESULT result = UpdatableInset::localDispatch(cmd);
875         if (result != UNDISPATCHED)
876                 return DISPATCHED;
877
878         result = DISPATCHED;
879         if (cmd.action < 0 && cmd.argument.empty())
880                 return FINISHED;
881
882         if (the_locking_inset) {
883                 result = the_locking_inset->localDispatch(cmd);
884                 if (result == DISPATCHED_NOUPDATE)
885                         return result;
886                 else if (result == DISPATCHED) {
887                         updateLocal(bv, CURSOR_PAR, false);
888                         return result;
889                 } else if (result >= FINISHED) {
890                         switch (result) {
891                         case FINISHED_RIGHT:
892                                 moveRightIntern(bv, false, false);
893                                 result = DISPATCHED;
894                                 break;
895                         case FINISHED_UP:
896                                 if ((result = moveUp(bv)) >= FINISHED) {
897                                         updateLocal(bv, CURSOR, false);
898                                         bv->unlockInset(this);
899                                 }
900                                 break;
901                         case FINISHED_DOWN:
902                                 if ((result = moveDown(bv)) >= FINISHED) {
903                                         updateLocal(bv, CURSOR, false);
904                                         bv->unlockInset(this);
905                                 }
906                                 break;
907                         default:
908                                 result = DISPATCHED;
909                                 break;
910                         }
911                         the_locking_inset = 0;
912                         updateLocal(bv, CURSOR, false);
913                         // make sure status gets reset immediately
914                         bv->owner()->clearMessage();
915                         return result;
916                 }
917         }
918         int updwhat = 0;
919         int updflag = false;
920
921         // what type of update to do on a cursor movement
922         int cursor_update = CURSOR;
923
924         if (text_.selection.set())
925                 cursor_update = SELECTION;
926
927         switch (cmd.action) {
928
929         // Normal chars
930         case LFUN_SELFINSERT:
931                 if (bv->buffer()->isReadonly()) {
932 //          setErrorMessage(N_("Document is read only"));
933                         break;
934                 }
935                 if (!cmd.argument.empty()) {
936                         /* Automatically delete the currently selected
937                          * text and replace it with what is being
938                          * typed in now. Depends on lyxrc settings
939                          * "auto_region_delete", which defaults to
940                          * true (on). */
941 #if 0
942                         // This should not be needed here and is also WRONG!
943                         recordUndo(bv, Undo::INSERT, text_.cursor.par());
944 #endif
945                         bv->switchKeyMap();
946                         if (lyxrc.auto_region_delete) {
947                                 if (text_.selection.set()) {
948                                         text_.cutSelection(false, false);
949                                 }
950                         }
951                         text_.clearSelection();
952                         for (string::size_type i = 0; i < cmd.argument.length(); ++i) {
953                                 bv->owner()->getIntl().getTransManager().
954                                         TranslateAndInsert(cmd.argument[i], &text_);
955                         }
956                 }
957                 text_.selection.cursor = text_.cursor;
958                 updwhat = CURSOR | CURSOR_PAR;
959                 updflag = true;
960                 result = DISPATCHED_NOUPDATE;
961                 break;
962
963         // cursor movements that need special handling
964
965         case LFUN_RIGHT:
966                 result = moveRight(bv);
967                 finishUndo();
968                 updwhat = cursor_update;
969                 break;
970         case LFUN_LEFT:
971                 finishUndo();
972                 result = moveLeft(bv);
973                 updwhat = cursor_update;
974                 break;
975         case LFUN_DOWN:
976                 finishUndo();
977                 result = moveDown(bv);
978                 updwhat = cursor_update;
979                 break;
980         case LFUN_UP:
981                 finishUndo();
982                 result = moveUp(bv);
983                 updwhat = cursor_update;
984                 break;
985
986         case LFUN_PRIOR:
987                 if (crow() == text_.rows().begin())
988                         result = FINISHED_UP;
989                 else {
990                         text_.cursorPrevious();
991                         text_.clearSelection();
992                         result = DISPATCHED_NOUPDATE;
993                 }
994                 updwhat = cursor_update;
995                 break;
996
997         case LFUN_NEXT:
998                 if (boost::next(crow()) == text_.rows().end())
999                         result = FINISHED_DOWN;
1000                 else {
1001                         text_.cursorNext();
1002                         text_.clearSelection();
1003                         result = DISPATCHED_NOUPDATE;
1004                 }
1005                 updwhat = cursor_update;
1006                 break;
1007
1008         case LFUN_BACKSPACE: {
1009                 if (text_.selection.set())
1010                         text_.cutSelection(true, false);
1011                 else
1012                         text_.backspace();
1013                 updwhat = CURSOR_PAR;
1014                 updflag = true;
1015                 break;
1016         }
1017
1018         case LFUN_DELETE: {
1019                 if (text_.selection.set()) {
1020                         text_.cutSelection(true, false);
1021                 } else {
1022                         text_.Delete();
1023                 }
1024                 updwhat = CURSOR_PAR;
1025                 updflag = true;
1026                 break;
1027         }
1028
1029         case LFUN_CUT: {
1030                 text_.cutSelection(true, true);
1031                 updwhat = CURSOR_PAR;
1032                 updflag = true;
1033                 break;
1034         }
1035
1036         case LFUN_COPY:
1037                 finishUndo();
1038                 text_.copySelection();
1039                 updwhat = CURSOR_PAR;
1040                 break;
1041
1042         case LFUN_PASTESELECTION:
1043         {
1044                 string const clip(bv->getClipboard());
1045
1046                 if (clip.empty())
1047                         break;
1048                 if (cmd.argument == "paragraph") {
1049                         text_.insertStringAsParagraphs(clip);
1050                 } else {
1051                         text_.insertStringAsLines(clip);
1052                 }
1053                 // bug 393
1054                 text_.clearSelection();
1055
1056                 updwhat = CURSOR_PAR;
1057                 updflag = true;
1058                 break;
1059         }
1060
1061         case LFUN_PASTE: {
1062                 if (!autoBreakRows) {
1063                         if (CutAndPaste::nrOfParagraphs() > 1) {
1064 #ifdef WITH_WARNINGS
1065 #warning FIXME horrendously bad UI
1066 #endif
1067                                 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
1068                                 break;
1069                         }
1070                 }
1071
1072                 replaceSelection(bv->getLyXText());
1073                 size_t sel_index = 0;
1074                 string const & arg = cmd.argument;
1075                 if (isStrUnsignedInt(arg)) {
1076                         size_t const paste_arg = strToUnsignedInt(arg);
1077 #warning FIXME Check if the arg is in the domain of available selections.
1078                         sel_index = paste_arg;
1079                 }
1080                 text_.pasteSelection(sel_index);
1081                 // bug 393
1082                 text_.clearSelection();
1083                 updwhat = CURSOR_PAR;
1084                 updflag = true;
1085                 break;
1086         }
1087
1088         case LFUN_BREAKPARAGRAPH:
1089                 if (!autoBreakRows) {
1090                         result = DISPATCHED;
1091                         break;
1092                 }
1093                 replaceSelection(bv->getLyXText());
1094                 text_.breakParagraph(paragraphs, 0);
1095                 updwhat = CURSOR | FULL;
1096                 updflag = true;
1097                 break;
1098
1099         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1100                 if (!autoBreakRows) {
1101                         result = DISPATCHED;
1102                         break;
1103                 }
1104                 replaceSelection(bv->getLyXText());
1105                 text_.breakParagraph(paragraphs, 1);
1106                 updwhat = CURSOR | FULL;
1107                 updflag = true;
1108                 break;
1109
1110         case LFUN_BREAKLINE: {
1111                 if (!autoBreakRows) {
1112                         result = DISPATCHED;
1113                         break;
1114                 }
1115
1116                 replaceSelection(bv->getLyXText());
1117                 text_.insertInset(new InsetNewline);
1118                 updwhat = CURSOR | CURSOR_PAR;
1119                 updflag = true;
1120                 break;
1121         }
1122
1123         case LFUN_LAYOUT:
1124                 // do not set layouts on non breakable textinsets
1125                 if (autoBreakRows) {
1126                         string cur_layout = cpar()->layout()->name();
1127
1128                         // Derive layout number from given argument (string)
1129                         // and current buffer's textclass (number). */
1130                         LyXTextClass const & tclass =
1131                                 bv->buffer()->params.getLyXTextClass();
1132                         string layout = cmd.argument;
1133                         bool hasLayout = tclass.hasLayout(layout);
1134
1135                         // If the entry is obsolete, use the new one instead.
1136                         if (hasLayout) {
1137                                 string const & obs =
1138                                         tclass[layout]->obsoleted_by();
1139                                 if (!obs.empty())
1140                                         layout = obs;
1141                         }
1142
1143                         // see if we found the layout number:
1144                         if (!hasLayout) {
1145                                 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
1146                                 bv->owner()->dispatch(lf);
1147                                 break;
1148                         }
1149
1150                         if (cur_layout != layout) {
1151                                 cur_layout = layout;
1152                                 text_.setLayout(layout);
1153                                 bv->owner()->setLayout(cpar()->layout()->name());
1154                                 updwhat = CURSOR_PAR;
1155                                 updflag = true;
1156                         }
1157                 } else {
1158                         // reset the layout box
1159                         bv->owner()->setLayout(cpar()->layout()->name());
1160                 }
1161                 break;
1162         case LFUN_PARAGRAPH_SPACING:
1163                 // This one is absolutely not working. When fiddling with this
1164                 // it also seems to me that the paragraphs inside the insettext
1165                 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1166                 // FIXME: how old is this comment ? ...
1167         {
1168                 ParagraphList::iterator pit = text_.cursor.par();
1169                 Spacing::Space cur_spacing = pit->params().spacing().getSpace();
1170                 float cur_value = 1.0;
1171                 if (cur_spacing == Spacing::Other) {
1172                         cur_value = pit->params().spacing().getValue();
1173                 }
1174
1175                 istringstream istr(STRCONV(cmd.argument));
1176                 string tmp;
1177                 istr >> tmp;
1178                 Spacing::Space new_spacing = cur_spacing;
1179                 float new_value = cur_value;
1180                 if (tmp.empty()) {
1181                         lyxerr << "Missing argument to `paragraph-spacing'"
1182                                    << endl;
1183                 } else if (tmp == "single") {
1184                         new_spacing = Spacing::Single;
1185                 } else if (tmp == "onehalf") {
1186                         new_spacing = Spacing::Onehalf;
1187                 } else if (tmp == "double") {
1188                         new_spacing = Spacing::Double;
1189                 } else if (tmp == "other") {
1190                         new_spacing = Spacing::Other;
1191                         float tmpval = 0.0;
1192                         istr >> tmpval;
1193                         lyxerr << "new_value = " << tmpval << endl;
1194                         if (tmpval != 0.0)
1195                                 new_value = tmpval;
1196                 } else if (tmp == "default") {
1197                         new_spacing = Spacing::Default;
1198                 } else {
1199                         lyxerr << _("Unknown spacing argument: ")
1200                                    << cmd.argument << endl;
1201                 }
1202                 if (cur_spacing != new_spacing || cur_value != new_value) {
1203                         pit->params().spacing(Spacing(new_spacing, new_value));
1204                         updwhat = CURSOR_PAR;
1205                         updflag = true;
1206                 }
1207         }
1208         break;
1209
1210         // These need to do repaints but don't require
1211         // special handling otherwise. A *lot* of the
1212         // above could probably be done similarly ...
1213
1214         case LFUN_HOME:
1215         case LFUN_END:
1216         case LFUN_WORDLEFT:
1217         case LFUN_WORDRIGHT:
1218         // these two are really unhandled ...
1219         case LFUN_ENDBUF:
1220         case LFUN_BEGINNINGBUF:
1221                 updwhat = cursor_update;
1222                 if (!bv->dispatch(cmd))
1223                         result = UNDISPATCHED;
1224                 break;
1225
1226         case LFUN_RIGHTSEL:
1227         case LFUN_UPSEL:
1228         case LFUN_DOWNSEL:
1229         case LFUN_LEFTSEL:
1230         case LFUN_HOMESEL:
1231         case LFUN_ENDSEL:
1232         case LFUN_WORDLEFTSEL:
1233         case LFUN_WORDRIGHTSEL:
1234                 updwhat = SELECTION;
1235
1236                 // fallthrough
1237
1238         default:
1239                 if (!bv->dispatch(cmd))
1240                         result = UNDISPATCHED;
1241                 break;
1242         }
1243
1244         if (updwhat > 0)
1245                 updateLocal(bv, updwhat, updflag);
1246         /// If the action has deleted all text in the inset, we need to change the
1247         // language to the language of the surronding text.
1248         if (!was_empty && paragraphs.begin()->empty() &&
1249             paragraphs.size() == 1) {
1250                 LyXFont font(LyXFont::ALL_IGNORE);
1251                 font.setLanguage(bv->getParentLanguage(this));
1252                 setFont(bv, font, false);
1253         }
1254
1255         if (result >= FINISHED)
1256                 bv->unlockInset(this);
1257
1258         if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1259                 result = DISPATCHED;
1260         return result;
1261 }
1262
1263
1264 int InsetText::latex(Buffer const * buf, ostream & os,
1265                      LatexRunParams const & runparams) const
1266 {
1267         TexRow texrow;
1268         latexParagraphs(buf, paragraphs, os, texrow, runparams);
1269         return texrow.rows();
1270 }
1271
1272
1273 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1274 {
1275         unsigned int lines = 0;
1276
1277         ParagraphList::const_iterator beg = paragraphs.begin();
1278         ParagraphList::const_iterator end = paragraphs.end();
1279         ParagraphList::const_iterator it = beg;
1280         for (; it != end; ++it) {
1281                 string const tmp = buf->asciiParagraph(*it, linelen, it == beg);
1282                 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1283                 os << tmp;
1284         }
1285         return lines;
1286 }
1287
1288
1289 int InsetText::linuxdoc(Buffer const * buf, ostream & os) const
1290 {
1291         ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1292         ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1293
1294         // There is a confusion between the empty paragraph and the default paragraph
1295         // The default paragraph is <p></p>, the empty paragraph is *empty*
1296         // Since none of the floats of linuxdoc accepts standard paragraphs
1297         // I disable them. I don't expect problems. (jamatos 2003/07/27)
1298         for (; pit != pend; ++pit) {
1299                 const string name = pit->layout()->latexname();
1300                 if (name != "p")
1301                         sgml::openTag(os, 1, 0, name);
1302                 buf->simpleLinuxDocOnePar(os, pit, 0);
1303                 if (name != "p")
1304                         sgml::closeTag(os, 1, 0, name);
1305         }
1306         return 0;
1307 }
1308
1309
1310 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1311 {
1312         unsigned int lines = 0;
1313
1314         vector<string> environment_stack(10);
1315         vector<string> environment_inner(10);
1316
1317         int const command_depth = 0;
1318         string item_name;
1319
1320         Paragraph::depth_type depth = 0; // paragraph depth
1321
1322         ParagraphList::iterator pit = const_cast<ParagraphList&>(paragraphs).begin();
1323         ParagraphList::iterator pend = const_cast<ParagraphList&>(paragraphs).end();
1324
1325         for (; pit != pend; ++pit) {
1326                 string sgmlparam;
1327                 int desc_on = 0; // description mode
1328
1329                 LyXLayout_ptr const & style = pit->layout();
1330
1331                 // environment tag closing
1332                 for (; depth > pit->params().depth(); --depth) {
1333                         if (environment_inner[depth] != "!-- --") {
1334                                 item_name = "listitem";
1335                                 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1336                                 if (environment_inner[depth] == "varlistentry")
1337                                         lines += sgml::closeTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1338                         }
1339                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1340                         environment_stack[depth].erase();
1341                         environment_inner[depth].erase();
1342                 }
1343
1344                 if (depth == pit->params().depth()
1345                    && environment_stack[depth] != style->latexname()
1346                    && !environment_stack[depth].empty()) {
1347                         if (environment_inner[depth] != "!-- --") {
1348                                 item_name= "listitem";
1349                                 lines += sgml::closeTag(os, command_depth+depth, mixcont, item_name);
1350                                 if (environment_inner[depth] == "varlistentry")
1351                                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1352                         }
1353
1354                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1355
1356                         environment_stack[depth].erase();
1357                         environment_inner[depth].erase();
1358                 }
1359
1360                 // Write opening SGML tags.
1361                 switch (style->latextype) {
1362                 case LATEX_PARAGRAPH:
1363                         lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1364                         break;
1365
1366                 case LATEX_COMMAND:
1367                         buf->error(ErrorItem(_("Error"), _("LatexType Command not allowed here.\n"), pit->id(), 0, pit->size()));
1368                         return -1;
1369                         break;
1370
1371                 case LATEX_ENVIRONMENT:
1372                 case LATEX_ITEM_ENVIRONMENT:
1373                         if (depth < pit->params().depth()) {
1374                                 depth = pit->params().depth();
1375                                 environment_stack[depth].erase();
1376                         }
1377
1378                         if (environment_stack[depth] != style->latexname()) {
1379                                 if (environment_stack.size() == depth + 1) {
1380                                         environment_stack.push_back("!-- --");
1381                                         environment_inner.push_back("!-- --");
1382                                 }
1383                                 environment_stack[depth] = style->latexname();
1384                                 environment_inner[depth] = "!-- --";
1385                                 lines += sgml::openTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1386                         } else {
1387                                 if (environment_inner[depth] != "!-- --") {
1388                                         item_name= "listitem";
1389                                         lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1390                                         if (environment_inner[depth] == "varlistentry")
1391                                                 lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1392                                 }
1393                         }
1394
1395                         if (style->latextype == LATEX_ENVIRONMENT) {
1396                                 if (!style->latexparam().empty()) {
1397                                         if (style->latexparam() == "CDATA")
1398                                                 os << "<![CDATA[";
1399                                         else
1400                                           lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexparam());
1401                                 }
1402                                 break;
1403                         }
1404
1405                         desc_on = (style->labeltype == LABEL_MANUAL);
1406
1407                         environment_inner[depth] = desc_on ? "varlistentry" : "listitem";
1408                         lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1409
1410                         item_name = desc_on ? "term" : "para";
1411                         lines += sgml::openTag(os, depth + 1 + command_depth, mixcont, item_name);
1412
1413                         break;
1414                 default:
1415                         lines += sgml::openTag(os, depth + command_depth, mixcont, style->latexname());
1416                         break;
1417                 }
1418
1419                 buf->simpleDocBookOnePar(os, pit, desc_on, depth + 1 + command_depth);
1420
1421                 string end_tag;
1422                 // write closing SGML tags
1423                 switch (style->latextype) {
1424                 case LATEX_ENVIRONMENT:
1425                         if (!style->latexparam().empty()) {
1426                                 if (style->latexparam() == "CDATA")
1427                                         os << "]]>";
1428                                 else
1429                                         lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexparam());
1430                         }
1431                         break;
1432                 case LATEX_ITEM_ENVIRONMENT:
1433                         if (desc_on == 1) break;
1434                         end_tag= "para";
1435                         lines += sgml::closeTag(os, depth + 1 + command_depth, mixcont, end_tag);
1436                         break;
1437                 case LATEX_PARAGRAPH:
1438                         lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1439                         break;
1440                 default:
1441                         lines += sgml::closeTag(os, depth + command_depth, mixcont, style->latexname());
1442                         break;
1443                 }
1444         }
1445
1446         // Close open tags
1447         for (int d = depth; d >= 0; --d) {
1448                 if (!environment_stack[depth].empty()) {
1449                         if (environment_inner[depth] != "!-- --") {
1450                                 item_name = "listitem";
1451                                 lines += sgml::closeTag(os, command_depth + depth, mixcont, item_name);
1452                                if (environment_inner[depth] == "varlistentry")
1453                                        lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1454                         }
1455
1456                         lines += sgml::closeTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1457                 }
1458         }
1459
1460         return lines;
1461 }
1462
1463
1464 void InsetText::validate(LaTeXFeatures & features) const
1465 {
1466         for_each(paragraphs.begin(), paragraphs.end(),
1467                  boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1468 }
1469
1470
1471 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1472 {
1473         if (the_locking_inset) {
1474                 the_locking_inset->getCursor(bv, x, y);
1475                 return;
1476         }
1477         x = cx();
1478         y = cy() + InsetText::y();
1479 }
1480
1481
1482 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1483 {
1484         if (the_locking_inset) {
1485                 the_locking_inset->getCursorPos(bv, x, y);
1486                 return;
1487         }
1488         x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1489         y = cy() - TEXT_TO_INSET_OFFSET;
1490 }
1491
1492
1493 int InsetText::insetInInsetY() const
1494 {
1495         if (!the_locking_inset)
1496                 return 0;
1497
1498         return inset_y + the_locking_inset->insetInInsetY();
1499 }
1500
1501
1502 void InsetText::fitInsetCursor(BufferView * bv) const
1503 {
1504         if (the_locking_inset) {
1505                 the_locking_inset->fitInsetCursor(bv);
1506                 return;
1507         }
1508
1509         LyXFont const font = text_.getFont(cpar(), cpos());
1510
1511         int const asc = font_metrics::maxAscent(font);
1512         int const desc = font_metrics::maxDescent(font);
1513
1514         if (bv->fitLockedInsetCursor(cx(), cy(), asc, desc))
1515                 need_update |= FULL;
1516 }
1517
1518
1519 InsetOld::RESULT
1520 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1521 {
1522         if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1523                 return moveLeftIntern(bv, false, activate_inset, selecting);
1524         else
1525                 return moveRightIntern(bv, true, activate_inset, selecting);
1526 }
1527
1528
1529 InsetOld::RESULT
1530 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1531 {
1532         if (text_.cursor.par()->isRightToLeftPar(bv->buffer()->params))
1533                 return moveRightIntern(bv, true, activate_inset, selecting);
1534         else
1535                 return moveLeftIntern(bv, false, activate_inset, selecting);
1536 }
1537
1538
1539 InsetOld::RESULT
1540 InsetText::moveRightIntern(BufferView * bv, bool front,
1541                            bool activate_inset, bool selecting)
1542 {
1543         ParagraphList::iterator c_par = cpar();
1544
1545         if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size())
1546                 return FINISHED_RIGHT;
1547         if (activate_inset && checkAndActivateInset(bv, front))
1548                 return DISPATCHED;
1549         text_.cursorRight(bv);
1550         if (!selecting)
1551                 text_.clearSelection();
1552         return DISPATCHED_NOUPDATE;
1553 }
1554
1555
1556 InsetOld::RESULT
1557 InsetText::moveLeftIntern(BufferView * bv, bool front,
1558                           bool activate_inset, bool selecting)
1559 {
1560         if (cpar() == paragraphs.begin() && cpos() <= 0)
1561                 return FINISHED;
1562         text_.cursorLeft(bv);
1563         if (!selecting)
1564                 text_.clearSelection();
1565         if (activate_inset && checkAndActivateInset(bv, front))
1566                 return DISPATCHED;
1567         return DISPATCHED_NOUPDATE;
1568 }
1569
1570
1571 InsetOld::RESULT InsetText::moveUp(BufferView * bv)
1572 {
1573         if (crow() == text_.rows().begin())
1574                 return FINISHED_UP;
1575         text_.cursorUp(bv);
1576         text_.clearSelection();
1577         return DISPATCHED_NOUPDATE;
1578 }
1579
1580
1581 InsetOld::RESULT InsetText::moveDown(BufferView * bv)
1582 {
1583         if (boost::next(crow()) == text_.rows().end())
1584                 return FINISHED_DOWN;
1585         text_.cursorDown(bv);
1586         text_.clearSelection();
1587         return DISPATCHED_NOUPDATE;
1588 }
1589
1590
1591 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1592 {
1593         if (the_locking_inset) {
1594                 if (the_locking_inset->insetAllowed(inset))
1595                         return the_locking_inset->insertInset(bv, inset);
1596                 return false;
1597         }
1598         inset->setOwner(this);
1599         text_.insertInset(inset);
1600         bv->fitCursor();
1601         updateLocal(bv, CURSOR_PAR|CURSOR, true);
1602         return true;
1603 }
1604
1605
1606 bool InsetText::insetAllowed(InsetOld::Code code) const
1607 {
1608         // in_insetAllowed is a really gross hack,
1609         // to allow us to call the owner's insetAllowed
1610         // without stack overflow, which can happen
1611         // when the owner uses InsetCollapsable::insetAllowed()
1612         bool ret = true;
1613         if (in_insetAllowed)
1614                 return ret;
1615         in_insetAllowed = true;
1616         if (the_locking_inset)
1617                 ret = the_locking_inset->insetAllowed(code);
1618         else if (owner())
1619                 ret = owner()->insetAllowed(code);
1620         in_insetAllowed = false;
1621         return ret;
1622 }
1623
1624
1625 UpdatableInset * InsetText::getLockingInset() const
1626 {
1627         return the_locking_inset ? the_locking_inset->getLockingInset() :
1628                 const_cast<InsetText *>(this);
1629 }
1630
1631
1632 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1633 {
1634         if (c == lyxCode())
1635                 return this;
1636         if (the_locking_inset)
1637                 return the_locking_inset->getFirstLockingInsetOfType(c);
1638         return 0;
1639 }
1640
1641
1642 bool InsetText::showInsetDialog(BufferView * bv) const
1643 {
1644         if (the_locking_inset)
1645                 return the_locking_inset->showInsetDialog(bv);
1646         return false;
1647 }
1648
1649
1650 void InsetText::getLabelList(std::vector<string> & list) const
1651 {
1652         ParagraphList::const_iterator pit = paragraphs.begin();
1653         ParagraphList::const_iterator pend = paragraphs.end();
1654         for (; pit != pend; ++pit) {
1655                 InsetList::const_iterator beg = pit->insetlist.begin();
1656                 InsetList::const_iterator end = pit->insetlist.end();
1657                 for (; beg != end; ++beg)
1658                         beg->inset->getLabelList(list);
1659         }
1660 }
1661
1662
1663 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1664                         bool selectall)
1665 {
1666         if (the_locking_inset) {
1667                 the_locking_inset->setFont(bv, font, toggleall, selectall);
1668                 return;
1669         }
1670
1671         if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1672             || cpar()->empty()) {
1673                 text_.setFont(font, toggleall);
1674                 return;
1675         }
1676
1677
1678         if (text_.selection.set())
1679                 recordUndo(bv, Undo::ATOMIC, text_.cursor.par());
1680
1681         if (selectall) {
1682                 text_.cursorTop();
1683                 text_.selection.cursor = text_.cursor;
1684                 text_.cursorBottom();
1685                 text_.setSelection();
1686         }
1687
1688         text_.toggleFree(font, toggleall);
1689
1690         if (selectall)
1691                 text_.clearSelection();
1692
1693         bv->fitCursor();
1694
1695         bool flag = (selectall || text_.selection.set());
1696
1697         if (flag)
1698                 updateLocal(bv, FULL, true);
1699         else
1700                 updateLocal(bv, CURSOR_PAR, true);
1701 }
1702
1703
1704 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1705 {
1706         InsetOld * inset = cpar()->getInset(cpos());
1707         if (!isHighlyEditableInset(inset))
1708                 return false;
1709         FuncRequest cmd(bv, LFUN_INSET_EDIT, front ? "left" : "right");
1710         inset->localDispatch(cmd);
1711         if (!the_locking_inset)
1712                 return false;
1713         updateLocal(bv, CURSOR, false);
1714         return true;
1715 }
1716
1717
1718 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1719                                       mouse_button::state button)
1720 {
1721         x -= drawTextXOffset;
1722         int dummyx = x;
1723         int dummyy = y + dim_.asc;
1724         InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1725         // we only do the edit() call if the inset was hit by the mouse
1726         // or if it is a highly editable inset. So we should call this
1727         // function from our own edit with button < 0.
1728         // FIXME: GUII jbl. I've changed this to ::none for now which is probably
1729         // WRONG
1730         if (button == mouse_button::none && !isHighlyEditableInset(inset))
1731                 return false;
1732
1733         if (!inset)
1734                 return false;
1735         if (x < 0)
1736                 x = dim_.wid;
1737         if (y < 0)
1738                 y = dim_.des;
1739         inset_x = cix() - top_x + drawTextXOffset;
1740         inset_y = ciy() + drawTextYOffset;
1741         FuncRequest cmd(bv, LFUN_INSET_EDIT, x - inset_x, y - inset_y, button);
1742         inset->localDispatch(cmd);
1743         if (!the_locking_inset)
1744                 return false;
1745         updateLocal(bv, CURSOR, false);
1746         return true;
1747 }
1748
1749
1750 void InsetText::setParagraphData(ParagraphList const & plist)
1751 {
1752         // we have to unlock any locked inset otherwise we're in troubles
1753         the_locking_inset = 0;
1754
1755         // But it it makes no difference that is a lot better.
1756 #warning FIXME.
1757         // See if this can be simplified when std::list is in effect.
1758         paragraphs.clear();
1759
1760         ParagraphList::const_iterator it = plist.begin();
1761         ParagraphList::const_iterator end = plist.end();
1762         for (; it != end; ++it) {
1763                 paragraphs.push_back(*it);
1764                 paragraphs.back().setInsetOwner(this);
1765         }
1766
1767         reinitLyXText();
1768         need_update = INIT;
1769 }
1770
1771
1772 void InsetText::markNew(bool track_changes)
1773 {
1774         ParagraphList::iterator pit = paragraphs.begin();
1775         ParagraphList::iterator pend = paragraphs.end();
1776         for (; pit != pend; ++pit) {
1777                 if (track_changes) {
1778                         pit->trackChanges();
1779                 } else {
1780                         // no-op when not tracking
1781                         pit->cleanChanges();
1782                 }
1783         }
1784 }
1785
1786
1787 void InsetText::setText(string const & data, LyXFont const & font)
1788 {
1789         clear(false);
1790         for (unsigned int i = 0; i < data.length(); ++i)
1791                 paragraphs.begin()->insertChar(i, data[i], font);
1792         reinitLyXText();
1793 }
1794
1795
1796 void InsetText::setAutoBreakRows(bool flag)
1797 {
1798         if (flag != autoBreakRows) {
1799                 autoBreakRows = flag;
1800                 if (!flag)
1801                         removeNewlines();
1802                 need_update = INIT;
1803         }
1804 }
1805
1806
1807 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1808 {
1809         if (how != drawFrame_) {
1810                 drawFrame_ = how;
1811                 if (bv)
1812                         updateLocal(bv, DRAW_FRAME, false);
1813         }
1814 }
1815
1816
1817 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1818 {
1819         if (frame_color != col) {
1820                 frame_color = col;
1821                 if (bv)
1822                         updateLocal(bv, DRAW_FRAME, false);
1823         }
1824 }
1825
1826
1827 int InsetText::cx() const
1828 {
1829         int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1830         if (the_locking_inset) {
1831                 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1832                 if (font.isVisibleRightToLeft())
1833                         x -= the_locking_inset->width();
1834         }
1835         return x;
1836 }
1837
1838
1839 int InsetText::cix() const
1840 {
1841         int x = text_.cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
1842         if (the_locking_inset) {
1843                 LyXFont font = text_.getFont(text_.cursor.par(), text_.cursor.pos());
1844                 if (font.isVisibleRightToLeft())
1845                         x -= the_locking_inset->width();
1846         }
1847         return x;
1848 }
1849
1850
1851 int InsetText::cy() const
1852 {
1853         return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1854 }
1855
1856
1857 int InsetText::ciy() const
1858 {
1859         return text_.cursor.iy() - dim_.asc + TEXT_TO_INSET_OFFSET;
1860 }
1861
1862
1863 pos_type InsetText::cpos() const
1864 {
1865         return text_.cursor.pos();
1866 }
1867
1868
1869 ParagraphList::iterator InsetText::cpar() const
1870 {
1871         return text_.cursor.par();
1872 }
1873
1874
1875 bool InsetText::cboundary() const
1876 {
1877         return text_.cursor.boundary();
1878 }
1879
1880
1881 RowList::iterator InsetText::crow() const
1882 {
1883         return text_.cursorRow();
1884 }
1885
1886
1887 LyXText * InsetText::getLyXText(BufferView const * bv,
1888                                 bool const recursive) const
1889 {
1890         setViewCache(bv);
1891         if (recursive && the_locking_inset)
1892                 return the_locking_inset->getLyXText(bv, true);
1893         return &text_;
1894 }
1895
1896
1897 void InsetText::setViewCache(BufferView const * bv) const
1898 {
1899         if (bv)
1900                 text_.bv_owner = const_cast<BufferView *>(bv);
1901 }
1902
1903
1904 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1905 {
1906         if (recursive) {
1907                 /// then remove all LyXText in text-insets
1908                 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1909                          const_cast<ParagraphList&>(paragraphs).end(),
1910                          boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1911         }
1912 }
1913
1914
1915 void InsetText::resizeLyXText(BufferView * bv, bool /*force*/) const
1916 {
1917         if (paragraphs.size() == 1 && paragraphs.begin()->empty())
1918                 return;
1919
1920         if (!bv)
1921                 return;
1922
1923         Assert(bv);
1924         setViewCache(bv);
1925
1926         for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1927                  const_cast<ParagraphList&>(paragraphs).end(),
1928                  boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1929
1930         saveLyXTextState();
1931         text_.init(bv);
1932         restoreLyXTextState();
1933
1934         // seems to be unneeded
1935 #if 1
1936         if (the_locking_inset) {
1937                 inset_x = cix() - top_x + drawTextXOffset;
1938                 inset_y = ciy() + drawTextYOffset;
1939         }
1940
1941         text_.top_y(bv->screen().topCursorVisible(&text_));
1942         if (!owner()) {
1943                 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
1944                 // this will scroll the screen such that the cursor becomes visible
1945                 bv->updateScrollbar();
1946         } else {
1947                 need_update |= FULL;
1948         }
1949 #endif
1950 }
1951
1952
1953 void InsetText::reinitLyXText() const
1954 {
1955         BufferView * bv = text_.bv_owner;
1956
1957         if (!bv)
1958                 return;
1959
1960         saveLyXTextState();
1961
1962         for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1963                  const_cast<ParagraphList&>(paragraphs).end(),
1964                  boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1965
1966         text_.init(bv);
1967         restoreLyXTextState();
1968         if (the_locking_inset) {
1969                 inset_x = cix() - top_x + drawTextXOffset;
1970                 inset_y = ciy() + drawTextYOffset;
1971         }
1972         text_.top_y(bv->screen().topCursorVisible(&text_));
1973         if (!owner()) {
1974                 // this will scroll the screen such that the cursor becomes visible
1975                 bv->updateScrollbar();
1976         } else {
1977                 need_update = FULL;
1978         }
1979 }
1980
1981
1982 void InsetText::removeNewlines()
1983 {
1984         bool changed = false;
1985
1986         ParagraphList::iterator it = paragraphs.begin();
1987         ParagraphList::iterator end = paragraphs.end();
1988         for (; it != end; ++it) {
1989                 for (int i = 0; i < it->size(); ++i) {
1990                         if (it->isNewline(i)) {
1991                                 changed = true;
1992                                 it->erase(i);
1993                         }
1994                 }
1995         }
1996         if (changed)
1997                 reinitLyXText();
1998 }
1999
2000
2001 int InsetText::scroll(bool recursive) const
2002 {
2003         int sx = UpdatableInset::scroll(false);
2004
2005         if (recursive && the_locking_inset)
2006                 sx += the_locking_inset->scroll(recursive);
2007
2008         return sx;
2009 }
2010
2011
2012 void InsetText::clearSelection(BufferView * bv)
2013 {
2014         getLyXText(bv)->clearSelection();
2015 }
2016
2017
2018 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2019 {
2020         Painter & pain = bv->painter();
2021         int w = dim_.wid;
2022         int h = dim_.asc + dim_.des;
2023         int ty = baseline - dim_.asc;
2024
2025         if (ty < 0) {
2026                 h += ty;
2027                 ty = 0;
2028         }
2029         if (ty + h > pain.paperHeight())
2030                 h = pain.paperHeight();
2031         if (top_x + drawTextXOffset + w > pain.paperWidth())
2032                 w = pain.paperWidth();
2033         pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2034         need_update = FULL;
2035 }
2036
2037
2038 ParagraphList * InsetText::getParagraphs(int i) const
2039 {
2040         return (i == 0) ? const_cast<ParagraphList*>(&paragraphs) : 0;
2041 }
2042
2043
2044 LyXCursor const & InsetText::cursor(BufferView * bv) const
2045 {
2046         if (the_locking_inset)
2047                 return the_locking_inset->cursor(bv);
2048         return getLyXText(bv)->cursor;
2049 }
2050
2051
2052 InsetOld * InsetText::getInsetFromID(int id_arg) const
2053 {
2054         if (id_arg == id())
2055                 return const_cast<InsetText *>(this);
2056
2057         ParagraphList::const_iterator pit = paragraphs.begin();
2058         ParagraphList::const_iterator pend = paragraphs.end();
2059         for (; pit != pend; ++pit) {
2060                 InsetList::const_iterator it = pit->insetlist.begin();
2061                 InsetList::const_iterator end = pit->insetlist.end();
2062                 for (; it != end; ++it) {
2063                         if (it->inset->id() == id_arg)
2064                                 return it->inset;
2065                         InsetOld * in = it->inset->getInsetFromID(id_arg);
2066                         if (in)
2067                                 return in;
2068                 }
2069         }
2070         return 0;
2071 }
2072
2073
2074 WordLangTuple const
2075 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2076 {
2077         WordLangTuple word;
2078         if (the_locking_inset) {
2079                 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2080                 if (!word.word().empty()) {
2081                         value += cy();
2082                         return word;
2083                 }
2084                 // we have to go on checking so move cursor to the next char
2085                 text_.cursor.pos(text_.cursor.pos() + 1);
2086         }
2087         word = text_.selectNextWordToSpellcheck(value);
2088         if (word.word().empty())
2089                 bv->unlockInset(const_cast<InsetText *>(this));
2090         else
2091                 value = cy();
2092         return word;
2093 }
2094
2095
2096 void InsetText::selectSelectedWord(BufferView * bv)
2097 {
2098         if (the_locking_inset) {
2099                 the_locking_inset->selectSelectedWord(bv);
2100                 return;
2101         }
2102         getLyXText(bv)->selectSelectedWord();
2103         updateLocal(bv, SELECTION, false);
2104 }
2105
2106
2107 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2108 {
2109         if (the_locking_inset) {
2110                 if (the_locking_inset->nextChange(bv, length))
2111                         return true;
2112                 text_.cursorRight(true);
2113         }
2114         lyx::find::SearchResult result =
2115                 lyx::find::findNextChange(bv, &text_, length);
2116
2117         if (result == lyx::find::SR_FOUND) {
2118                 LyXCursor cur = text_.cursor;
2119                 bv->unlockInset(bv->theLockingInset());
2120                 if (bv->lockInset(this))
2121                         locked = true;
2122                 text_.cursor = cur;
2123                 text_.setSelectionRange(length);
2124                 updateLocal(bv, SELECTION, false);
2125         }
2126         return result != lyx::find::SR_NOT_FOUND;
2127 }
2128
2129
2130 bool InsetText::searchForward(BufferView * bv, string const & str,
2131                               bool cs, bool mw)
2132 {
2133         if (the_locking_inset) {
2134                 if (the_locking_inset->searchForward(bv, str, cs, mw))
2135                         return true;
2136                 text_.cursorRight(true);
2137         }
2138         lyx::find::SearchResult result =
2139                 lyx::find::find(bv, &text_, str, true, cs, mw);
2140
2141         if (result == lyx::find::SR_FOUND) {
2142                 LyXCursor cur = text_.cursor;
2143                 bv->unlockInset(bv->theLockingInset());
2144                 if (bv->lockInset(this))
2145                         locked = true;
2146                 text_.cursor = cur;
2147                 text_.setSelectionRange(str.length());
2148                 updateLocal(bv, SELECTION, false);
2149         }
2150         return result != lyx::find::SR_NOT_FOUND;
2151 }
2152
2153
2154 bool InsetText::searchBackward(BufferView * bv, string const & str,
2155                                bool cs, bool mw)
2156 {
2157         if (the_locking_inset) {
2158                 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2159                         return true;
2160         }
2161         if (!locked) {
2162                 ParagraphList::iterator pit = paragraphs.begin();
2163                 ParagraphList::iterator pend = paragraphs.end();
2164
2165                 while (boost::next(pit) != pend)
2166                         ++pit;
2167
2168                 text_.setCursor(pit, pit->size());
2169         }
2170         lyx::find::SearchResult result =
2171                 lyx::find::find(bv, &text_, str, false, cs, mw);
2172
2173         if (result == lyx::find::SR_FOUND) {
2174                 LyXCursor cur = text_.cursor;
2175                 bv->unlockInset(bv->theLockingInset());
2176                 if (bv->lockInset(this))
2177                         locked = true;
2178                 text_.cursor = cur;
2179                 text_.setSelectionRange(str.length());
2180                 updateLocal(bv, SELECTION, false);
2181         }
2182         return result != lyx::find::SR_NOT_FOUND;
2183 }
2184
2185
2186 bool InsetText::checkInsertChar(LyXFont & font)
2187 {
2188         if (owner())
2189                 return owner()->checkInsertChar(font);
2190         return true;
2191 }
2192
2193
2194 void InsetText::collapseParagraphs(BufferView * bv)
2195 {
2196         while (paragraphs.size() > 1) {
2197                 ParagraphList::iterator first_par = paragraphs.begin();
2198                 ParagraphList::iterator next_par = boost::next(first_par);
2199                 size_t const first_par_size = first_par->size();
2200
2201                 if (!first_par->empty() &&
2202                     !next_par->empty() &&
2203                     !first_par->isSeparator(first_par_size - 1)) {
2204                         first_par->insertChar(first_par_size, ' ');
2205                 }
2206
2207                 if (text_.selection.set()) {
2208                         if (text_.selection.start.par() == next_par) {
2209                                 text_.selection.start.par(first_par);
2210                                 text_.selection.start.pos(
2211                                         text_.selection.start.pos() + first_par_size);
2212                         }
2213                         if (text_.selection.end.par() == next_par) {
2214                                 text_.selection.end.par(first_par);
2215                                 text_.selection.end.pos(
2216                                         text_.selection.end.pos() + first_par_size);
2217                         }
2218                 }
2219
2220                 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2221         }
2222         reinitLyXText();
2223 }
2224
2225
2226 void InsetText::getDrawFont(LyXFont & font) const
2227 {
2228         if (!owner())
2229                 return;
2230         owner()->getDrawFont(font);
2231 }
2232
2233
2234 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2235 {
2236 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2237 // And it probably does. You have to take a look at this John. (Lgb)
2238 #warning John, have a look here. (Lgb)
2239         ParagraphList::iterator pit = plist.begin();
2240         ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2241         ++pit;
2242         mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2243
2244         ParagraphList::iterator pend = plist.end();
2245         for (; pit != pend; ++pit) {
2246                 paragraphs.push_back(*pit);
2247         }
2248
2249         reinitLyXText();
2250 }
2251
2252
2253 void InsetText::addPreview(PreviewLoader & loader) const
2254 {
2255         ParagraphList::const_iterator pit = paragraphs.begin();
2256         ParagraphList::const_iterator pend = paragraphs.end();
2257
2258         for (; pit != pend; ++pit) {
2259                 InsetList::const_iterator it  = pit->insetlist.begin();
2260                 InsetList::const_iterator end = pit->insetlist.end();
2261                 for (; it != end; ++it) {
2262                         it->inset->addPreview(loader);
2263                 }
2264         }
2265 }