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