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