]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
small stuff
[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: width: " << mi.base.textwidth << "\n";
279         if (mi.base.textwidth)
280                 textwidth_ = mi.base.textwidth;
281         BufferView * bv = mi.base.bv;
282         setViewCache(bv);
283         text_.metrics(mi, dim);
284         dim.asc += TEXT_TO_INSET_OFFSET;
285         dim.des += TEXT_TO_INSET_OFFSET;
286         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
287         dim.wid = max(dim.wid, 10);
288         dim_ = dim;
289 }
290
291
292 int InsetText::textWidth() const
293 {
294         return textwidth_;
295 }
296
297
298 void InsetText::draw(PainterInfo & pi, int x, int baseline) const
299 {
300         if (nodraw())
301                 return;
302
303         // update our idea of where we are. Clearly, we should
304         // not have to know this information.
305         top_x = x;
306
307         int const start_x = x;
308
309         BufferView * bv = pi.base.bv;
310         Painter & pain = pi.pain;
311
312         // repaint the background if needed
313         if (backgroundColor() != LColor::background)
314                 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, baseline);
315
316         // no draw is necessary !!!
317         if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
318                 top_baseline = baseline;
319                 need_update = NONE;
320                 return;
321         }
322
323         if (!owner())
324                 x += scroll();
325
326         top_baseline = baseline;
327         top_y = baseline - dim_.asc;
328
329         if (last_drawn_width != dim_.wid) {
330                 need_update |= FULL;
331                 last_drawn_width = dim_.wid;
332         }
333
334         if (the_locking_inset && cpar() == inset_par && cpos() == inset_pos) {
335                 inset_x = cix(bv) - x + drawTextXOffset;
336                 inset_y = ciy() + drawTextYOffset;
337         }
338
339         x += TEXT_TO_INSET_OFFSET;
340
341         RowList::iterator rowit = text_.rows().begin();
342         RowList::iterator end = text_.rows().end();
343
344         int y_offset = baseline - rowit->ascent_of_text();
345         int ph = pain.paperHeight();
346         int first = 0;
347         int y = y_offset;
348         while (rowit != end && y + rowit->height() <= 0) {
349                 y += rowit->height();
350                 first += rowit->height();
351                 ++rowit;
352         }
353         if (y_offset < 0) {
354                 text_.top_y(-y_offset);
355                 first = y;
356                 y_offset = 0;
357         } else {
358                 text_.top_y(first);
359                 first = 0;
360         }
361
362         int yf = y_offset + first;
363         y = 0;
364
365         bv->hideCursor();
366
367         while (rowit != end && yf < ph) {
368                 paintRows(*bv, text_, rowit,
369                         y + y_offset + first, int(x), y + text_.top_y());
370                 y += rowit->height();
371                 yf += rowit->height();
372                 ++rowit;
373         }
374
375         text_.clearPaint();
376
377         if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked)) {
378                 drawFrame(pain, int(start_x));
379         }
380
381         if (need_update != INIT) {
382                 need_update = NONE;
383         }
384 }
385
386
387 void InsetText::drawFrame(Painter & pain, int x) const
388 {
389         static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
390         frame_x = x + ttoD2;
391         frame_y = top_baseline - dim_.asc + ttoD2;
392         frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
393         frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
394         pain.rectangle(frame_x, frame_y, frame_w, frame_h,
395                        frame_color);
396 }
397
398
399 void InsetText::setUpdateStatus(int what) const
400 {
401         need_update |= what;
402         // we will to redraw us full if our LyXText wants it
403         if (text_.needRefresh())
404                 need_update |= FULL;
405
406         // this to not draw a selection when we redraw all of it!
407         if (need_update & CURSOR && !(need_update & SELECTION)) {
408                 if (text_.selection.set())
409                         need_update = FULL;
410                 text_.clearSelection();
411         }
412 }
413
414
415 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
416 {
417         if (!bv)
418                 return;
419
420         if (!autoBreakRows && paragraphs.size() > 1)
421                 collapseParagraphs(bv);
422
423         text_.partialRebreak();
424         setUpdateStatus(what);
425         bool flag = mark_dirty ||
426                 ((need_update != CURSOR && need_update != NONE) ||
427                  text_.needRefresh() || text_.selection.set());
428         if (!text_.selection.set())
429                 text_.selection.cursor = text_.cursor;
430
431         bv->fitCursor();
432
433         if (flag) {
434                 text_.postPaint();
435                 bv->updateInset(const_cast<InsetText *>(this));
436         }
437
438         if (need_update == CURSOR)
439                 need_update = NONE;
440         bv->owner()->view_state_changed();
441         bv->owner()->updateMenubar();
442         bv->owner()->updateToolbar();
443         if (old_par != cpar()) {
444                 bv->owner()->setLayout(cpar()->layout()->name());
445                 old_par = cpar();
446         }
447 }
448
449
450 string const InsetText::editMessage() const
451 {
452         return _("Opened Text Inset");
453 }
454
455
456 void InsetText::insetUnlock(BufferView * bv)
457 {
458         if (the_locking_inset) {
459                 the_locking_inset->insetUnlock(bv);
460                 the_locking_inset = 0;
461                 updateLocal(bv, CURSOR_PAR, false);
462         }
463         no_selection = true;
464         locked = false;
465         int code = NONE;
466
467         if (text_.selection.set()) {
468                 text_.clearSelection();
469                 code = FULL;
470         } else if (owner()) {
471                 bv->owner()->setLayout(owner()->getLyXText(bv)
472                                        ->cursor.par()->layout()->name());
473         } else
474                 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
475         // hack for deleteEmptyParMech
476         ParagraphList::iterator first_par = paragraphs.begin();
477         if (!first_par->empty()) {
478                 text_.setCursor(first_par, 0);
479         } else if (paragraphs.size() > 1) {
480                 text_.setCursor(boost::next(first_par), 0);
481         }
482 #if 0
483         updateLocal(bv, code, false);
484 #else
485         if (code != NONE)
486                 setUpdateStatus(code);
487 #endif
488 }
489
490
491 void InsetText::lockInset(BufferView * bv)
492 {
493         locked = true;
494         the_locking_inset = 0;
495         inset_pos = inset_x = inset_y = 0;
496         inset_boundary = false;
497         inset_par = paragraphs.end();
498         old_par = paragraphs.end();
499         text_.setCursorIntern(paragraphs.begin(), 0);
500         text_.clearSelection();
501         finishUndo();
502         // If the inset is empty set the language of the current font to the
503         // language to the surronding text (if different).
504         if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
505                 bv->getParentLanguage(this) != text_.current_font.language()) {
506                 LyXFont font(LyXFont::ALL_IGNORE);
507                 font.setLanguage(bv->getParentLanguage(this));
508                 setFont(bv, font, false);
509         }
510         int code = CURSOR;
511         if (drawFrame_ == LOCKED)
512                 code = CURSOR|DRAW_FRAME;
513         setUpdateStatus(code);
514 }
515
516
517 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
518 {
519         the_locking_inset = inset;
520         inset_x = cix(bv) - top_x + drawTextXOffset;
521         inset_y = ciy() + drawTextYOffset;
522         inset_pos = cpos();
523         inset_par = cpar();
524         inset_boundary = cboundary();
525         //updateLocal(bv, CURSOR, false);
526 }
527
528
529 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
530 {
531         lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
532                               << inset << "): ";
533         if (!inset)
534                 return false;
535         if (!the_locking_inset) {
536                 ParagraphList::iterator pit = paragraphs.begin();
537                 ParagraphList::iterator pend = paragraphs.end();
538
539                 int const id = inset->id();
540                 for (; pit != pend; ++pit) {
541                         InsetList::iterator it = pit->insetlist.begin();
542                         InsetList::iterator const end = pit->insetlist.end();
543                         for (; it != end; ++it) {
544                                 if (it->inset == inset) {
545                                         lyxerr << "InsetText::lockInsetInInset: 1 a\n";
546                                         text_.setCursorIntern(pit, it->pos);
547                                         lyxerr << "InsetText::lockInsetInInset: 1 b\n";
548                                         lyxerr << "bv: " << bv << " inset: " << inset << "\n";
549                                         lockInset(bv, inset);
550                                         lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
551                                         return true;
552                                 }
553                                 if (it->inset->getInsetFromID(id)) {
554                                         lyxerr << "InsetText::lockInsetInInset: 2\n";
555                                         text_.setCursorIntern(pit, it->pos);
556                                         it->inset->localDispatch(FuncRequest(bv, LFUN_INSET_EDIT));
557                                         return the_locking_inset->lockInsetInInset(bv, inset);
558                                 }
559                         }
560                 }
561                 lyxerr << "InsetText::lockInsetInInset: 3\n";
562                 return false;
563         }
564         if (inset == cpar()->getInset(cpos())) {
565                 lyxerr[Debug::INSETS] << "OK" << endl;
566                 lockInset(bv, inset);
567                 return true;
568         }
569
570         if (the_locking_inset && the_locking_inset == inset) {
571                 if (cpar() == inset_par && cpos() == inset_pos) {
572                         lyxerr[Debug::INSETS] << "OK" << endl;
573                         inset_x = cix(bv) - top_x + drawTextXOffset;
574                         inset_y = ciy() + drawTextYOffset;
575                 } else {
576                         lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
577                 }
578         } else if (the_locking_inset) {
579                 lyxerr[Debug::INSETS] << "MAYBE" << endl;
580                 return the_locking_inset->lockInsetInInset(bv, inset);
581         }
582         lyxerr[Debug::INSETS] << "NOT OK" << endl;
583         return false;
584 }
585
586
587 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
588                                    bool lr)
589 {
590         if (!the_locking_inset)
591                 return false;
592         if (the_locking_inset == inset) {
593                 the_locking_inset->insetUnlock(bv);
594                 getLyXText(bv)->updateInset(inset);
595                 the_locking_inset = 0;
596                 if (lr)
597                         moveRightIntern(bv, true, false);
598                 old_par = paragraphs.end(); // force layout setting
599                 if (scroll())
600                         scroll(bv, 0.0F);
601                 else
602                         updateLocal(bv, CURSOR, false);
603                 return true;
604         }
605         return the_locking_inset->unlockInsetInInset(bv, inset, lr);
606 }
607
608
609 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
610 {
611         if (!autoBreakRows && paragraphs.size() > 1)
612                 collapseParagraphs(bv);
613
614         if (inset == this)
615                 return true;
616
617         if (inset->owner() != this) {
618                 int ustat = CURSOR_PAR;
619                 bool found = false;
620                 UpdatableInset * tl_inset = the_locking_inset;
621                 if (tl_inset)
622                         found = tl_inset->updateInsetInInset(bv, inset);
623                 if (!found) {
624                         tl_inset = static_cast<UpdatableInset *>(inset);
625                         while(tl_inset->owner() && tl_inset->owner() != this)
626                                 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
627                         if (!tl_inset->owner())
628                                 return false;
629                         found = tl_inset->updateInsetInInset(bv, inset);
630                         ustat = FULL;
631                 }
632                 if (found)
633                         text_.updateInset(tl_inset);
634                 if (found)
635                         setUpdateStatus(ustat);
636                 return found;
637         }
638         bool found = text_.updateInset(inset);
639         if (found) {
640                 setUpdateStatus(CURSOR_PAR);
641                 if (the_locking_inset &&
642                     cpar() == inset_par && cpos() == inset_pos)
643                 {
644                         inset_x = cix(bv) - top_x + drawTextXOffset;
645                         inset_y = ciy() + drawTextYOffset;
646                 }
647         }
648         return found;
649 }
650
651
652 void InsetText::lfunMousePress(FuncRequest const & cmd)
653 {
654         no_selection = true;
655
656         // use this to check mouse motion for selection!
657         mouse_x = cmd.x;
658         mouse_y = cmd.y;
659
660         BufferView * bv = cmd.view();
661         FuncRequest cmd1 = cmd;
662         cmd1.x -= inset_x;
663         cmd1.y -= inset_y;
664         if (!locked)
665                 lockInset(bv);
666
667         int tmp_x = cmd.x - drawTextXOffset;
668         int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
669         Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
670
671         if (the_locking_inset) {
672                 if (the_locking_inset == inset) {
673                         the_locking_inset->localDispatch(cmd1);
674                         return;
675                 }
676                 // otherwise only unlock the_locking_inset
677                 the_locking_inset->insetUnlock(bv);
678                 the_locking_inset = 0;
679         }
680         if (!inset)
681                 no_selection = false;
682
683         if (bv->theLockingInset()) {
684                 if (isHighlyEditableInset(inset)) {
685                         // We just have to lock the inset before calling a
686                         // PressEvent on it!
687                         UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
688                         if (!bv->lockInset(uinset)) {
689                                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
690                         }
691                         inset->localDispatch(cmd1);
692                         if (the_locking_inset)
693                                 updateLocal(bv, CURSOR, false);
694                         return;
695                 }
696         }
697         if (!inset) {
698                 bool paste_internally = false;
699                 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
700                         localDispatch(FuncRequest(bv, LFUN_COPY));
701                         paste_internally = true;
702                 }
703                 int old_top_y = text_.top_y();
704
705                 text_.setCursorFromCoordinates(cmd.x - drawTextXOffset,
706                                              cmd.y + dim_.asc);
707                 // set the selection cursor!
708                 text_.selection.cursor = text_.cursor;
709                 text_.cursor.x_fix(text_.cursor.x());
710
711                 if (text_.selection.set()) {
712                         text_.clearSelection();
713                         updateLocal(bv, FULL, false);
714                 } else {
715                         text_.clearSelection();
716                         updateLocal(bv, CURSOR, false);
717                 }
718
719                 bv->owner()->setLayout(cpar()->layout()->name());
720
721                 // we moved the view we cannot do mouse selection in this case!
722                 if (getLyXText(bv)->top_y() != old_top_y)
723                         no_selection = true;
724                 old_par = cpar();
725                 // Insert primary selection with middle mouse
726                 // if there is a local selection in the current buffer,
727                 // insert this
728                 if (cmd.button() == mouse_button::button2) {
729                         if (paste_internally)
730                                 localDispatch(FuncRequest(bv, LFUN_PASTE));
731                         else
732                                 localDispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
733                 }
734         } else {
735                 getLyXText(bv)->clearSelection();
736         }
737 }
738
739
740 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
741 {
742         BufferView * bv = cmd.view();
743         FuncRequest cmd1 = cmd;
744         cmd1.x -= inset_x;
745         cmd1.y -= inset_y;
746
747         no_selection = true;
748         if (the_locking_inset)
749                 return the_locking_inset->localDispatch(cmd1);
750
751         int tmp_x = cmd.x - drawTextXOffset;
752         int tmp_y = cmd.y + dim_.asc - getLyXText(bv)->top_y();
753         Inset * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
754         bool ret = false;
755         if (inset) {
756 // This code should probably be removed now. Simple insets
757 // (!highlyEditable) can actually take the localDispatch,
758 // and turn it into edit() if necessary. But we still
759 // need to deal properly with the whole relative vs.
760 // absolute mouse co-ords thing in a realiable, sensible way
761 #if 0
762                 if (isHighlyEditableInset(inset))
763                         ret = inset->localDispatch(cmd1);
764                 else {
765                         inset_x = cix(bv) - top_x + drawTextXOffset;
766                         inset_y = ciy() + drawTextYOffset;
767                         cmd1.x = cmd.x - inset_x;
768                         cmd1.y = cmd.x - inset_y;
769                         inset->edit(bv, cmd1.x, cmd1.y, cmd.button());
770                         ret = true;
771                 }
772 #endif
773                 ret = inset->localDispatch(cmd1);
774                 updateLocal(bv, CURSOR_PAR, false);
775
776         }
777         return ret;
778 }
779
780
781 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
782 {
783         FuncRequest cmd1 = cmd;
784         cmd1.x -= inset_x;
785         cmd1.y -= inset_y;
786
787         if (the_locking_inset) {
788                 the_locking_inset->localDispatch(cmd1);
789                 return;
790         }
791
792         if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
793                 return;
794
795         BufferView * bv = cmd.view();
796         LyXCursor cur = text_.cursor;
797         text_.setCursorFromCoordinates
798                 (cmd.x - drawTextXOffset, cmd.y + dim_.asc);
799         text_.cursor.x_fix(text_.cursor.x());
800         if (cur == text_.cursor)
801                 return;
802         text_.setSelection();
803         bool flag = (text_.toggle_cursor.par() != text_.toggle_end_cursor.par() ||
804                                  text_.toggle_cursor.pos() != text_.toggle_end_cursor.pos());
805         if (flag) {
806                 updateLocal(bv, SELECTION, false);
807         }
808 }
809
810
811 Inset::RESULT InsetText::localDispatch(FuncRequest const & cmd)
812 {
813         BufferView * bv = cmd.view();
814         setViewCache(bv);
815
816         switch (cmd.action) {
817         case 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         case LFUN_MOUSE_PRESS:
882                 lfunMousePress(cmd);
883                 return DISPATCHED;
884
885         case LFUN_MOUSE_MOTION:
886                 lfunMouseMotion(cmd);
887                 return DISPATCHED;
888
889         case LFUN_MOUSE_RELEASE:
890                 return lfunMouseRelease(cmd) ? DISPATCHED : UNDISPATCHED;
891
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         // seems to be unneeded
1954 #if 1
1955         if (the_locking_inset) {
1956                 inset_x = cix(bv) - top_x + drawTextXOffset;
1957                 inset_y = ciy() + drawTextYOffset;
1958         }
1959
1960         text_.top_y(bv->screen().topCursorVisible(&text_));
1961         if (!owner()) {
1962                 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
1963                 // this will scroll the screen such that the cursor becomes visible
1964                 bv->updateScrollbar();
1965         } else {
1966                 need_update |= FULL;
1967         }
1968 #endif
1969 }
1970
1971
1972 void InsetText::reinitLyXText() const
1973 {
1974         BufferView * bv = text_.bv_owner;
1975
1976         if (!bv)
1977                 return;
1978
1979         saveLyXTextState();
1980
1981         for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1982                  const_cast<ParagraphList&>(paragraphs).end(),
1983                  boost::bind(&Paragraph::resizeInsetsLyXText, _1, bv));
1984
1985         text_.init(bv);
1986         restoreLyXTextState();
1987         if (the_locking_inset) {
1988                 inset_x = cix(bv) - top_x + drawTextXOffset;
1989                 inset_y = ciy() + drawTextYOffset;
1990         }
1991         text_.top_y(bv->screen().topCursorVisible(&text_));
1992         if (!owner()) {
1993                 const_cast<InsetText*>(this)->updateLocal(bv, FULL, false);
1994                 // this will scroll the screen such that the cursor becomes visible
1995                 bv->updateScrollbar();
1996         } else {
1997                 need_update = FULL;
1998         }
1999 }
2000
2001
2002 void InsetText::removeNewlines()
2003 {
2004         bool changed = false;
2005
2006         ParagraphList::iterator it = paragraphs.begin();
2007         ParagraphList::iterator end = paragraphs.end();
2008         for (; it != end; ++it) {
2009                 for (int i = 0; i < it->size(); ++i) {
2010                         if (it->isNewline(i)) {
2011                                 changed = true;
2012                                 it->erase(i);
2013                         }
2014                 }
2015         }
2016         if (changed)
2017                 reinitLyXText();
2018 }
2019
2020
2021 bool InsetText::nodraw() const
2022 {
2023         if (the_locking_inset)
2024                 return the_locking_inset->nodraw();
2025         return UpdatableInset::nodraw();
2026 }
2027
2028
2029 int InsetText::scroll(bool recursive) const
2030 {
2031         int sx = UpdatableInset::scroll(false);
2032
2033         if (recursive && the_locking_inset)
2034                 sx += the_locking_inset->scroll(recursive);
2035
2036         return sx;
2037 }
2038
2039
2040 void InsetText::clearSelection(BufferView * bv)
2041 {
2042         getLyXText(bv)->clearSelection();
2043 }
2044
2045
2046 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
2047 {
2048         Painter & pain = bv->painter();
2049         int w = dim_.wid;
2050         int h = dim_.asc + dim_.des;
2051         int ty = baseline - dim_.asc;
2052
2053         if (ty < 0) {
2054                 h += ty;
2055                 ty = 0;
2056         }
2057         if ((ty + h) > pain.paperHeight())
2058                 h = pain.paperHeight();
2059         if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2060                 w = pain.paperWidth();
2061         pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
2062         need_update = FULL;
2063 }
2064
2065
2066 ParagraphList * InsetText::getParagraphs(int i) const
2067 {
2068         return (i == 0) ? const_cast<ParagraphList*>(&paragraphs) : 0;
2069 }
2070
2071
2072 LyXCursor const & InsetText::cursor(BufferView * bv) const
2073 {
2074         if (the_locking_inset)
2075                 return the_locking_inset->cursor(bv);
2076         return getLyXText(bv)->cursor;
2077 }
2078
2079
2080 Inset * InsetText::getInsetFromID(int id_arg) const
2081 {
2082         if (id_arg == id())
2083                 return const_cast<InsetText *>(this);
2084
2085         ParagraphList::const_iterator pit = paragraphs.begin();
2086         ParagraphList::const_iterator pend = paragraphs.end();
2087         for (; pit != pend; ++pit) {
2088                 InsetList::const_iterator it = pit->insetlist.begin();
2089                 InsetList::const_iterator end = pit->insetlist.end();
2090                 for (; it != end; ++it) {
2091                         if (it->inset->id() == id_arg)
2092                                 return it->inset;
2093                         Inset * in = it->inset->getInsetFromID(id_arg);
2094                         if (in)
2095                                 return in;
2096                 }
2097         }
2098         return 0;
2099 }
2100
2101
2102 WordLangTuple const
2103 InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2104 {
2105         WordLangTuple word;
2106         if (the_locking_inset) {
2107                 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2108                 if (!word.word().empty()) {
2109                         value += cy();
2110                         return word;
2111                 }
2112                 // we have to go on checking so move cursor to the next char
2113                 text_.cursor.pos(text_.cursor.pos() + 1);
2114         }
2115         word = text_.selectNextWordToSpellcheck(value);
2116         if (word.word().empty())
2117                 bv->unlockInset(const_cast<InsetText *>(this));
2118         else
2119                 value = cy();
2120         return word;
2121 }
2122
2123
2124 void InsetText::selectSelectedWord(BufferView * bv)
2125 {
2126         if (the_locking_inset) {
2127                 the_locking_inset->selectSelectedWord(bv);
2128                 return;
2129         }
2130         getLyXText(bv)->selectSelectedWord();
2131         updateLocal(bv, SELECTION, false);
2132 }
2133
2134
2135 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2136 {
2137         if (the_locking_inset) {
2138                 the_locking_inset->toggleSelection(bv, kill_selection);
2139         }
2140
2141         int x = top_x + TEXT_TO_INSET_OFFSET;
2142
2143         RowList::iterator rowit = text_.rows().begin();
2144         RowList::iterator end = text_.rows().end();
2145         int y_offset = top_baseline - rowit->ascent_of_text();
2146         int y = y_offset;
2147         while ((rowit != end) && ((y + rowit->height()) <= 0)) {
2148                 y += rowit->height();
2149                 ++rowit;
2150         }
2151         if (y_offset < 0)
2152                 y_offset = y;
2153
2154         if (need_update & SELECTION)
2155                 need_update = NONE;
2156         bv->screen().toggleSelection(&text_, bv, kill_selection, y_offset, x);
2157 }
2158
2159
2160 bool InsetText::nextChange(BufferView * bv, lyx::pos_type & length)
2161 {
2162         if (the_locking_inset) {
2163                 if (the_locking_inset->nextChange(bv, length))
2164                         return true;
2165                 text_.cursorRight(true);
2166         }
2167         lyxfind::SearchResult result =
2168                 lyxfind::findNextChange(bv, &text_, length);
2169
2170         if (result == lyxfind::SR_FOUND) {
2171                 LyXCursor cur = text_.cursor;
2172                 bv->unlockInset(bv->theLockingInset());
2173                 if (bv->lockInset(this))
2174                         locked = true;
2175                 text_.cursor = cur;
2176                 text_.setSelectionRange(length);
2177                 updateLocal(bv, SELECTION, false);
2178         }
2179         return result != lyxfind::SR_NOT_FOUND;
2180 }
2181
2182
2183 bool InsetText::searchForward(BufferView * bv, string const & str,
2184                               bool cs, bool mw)
2185 {
2186         if (the_locking_inset) {
2187                 if (the_locking_inset->searchForward(bv, str, cs, mw))
2188                         return true;
2189                 text_.cursorRight(true);
2190         }
2191         lyxfind::SearchResult result =
2192                 lyxfind::LyXFind(bv, &text_, str, true, cs, mw);
2193
2194         if (result == lyxfind::SR_FOUND) {
2195                 LyXCursor cur = text_.cursor;
2196                 bv->unlockInset(bv->theLockingInset());
2197                 if (bv->lockInset(this))
2198                         locked = true;
2199                 text_.cursor = cur;
2200                 text_.setSelectionRange(str.length());
2201                 updateLocal(bv, SELECTION, false);
2202         }
2203         return (result != lyxfind::SR_NOT_FOUND);
2204 }
2205
2206 bool InsetText::searchBackward(BufferView * bv, string const & str,
2207                                bool cs, bool mw)
2208 {
2209         if (the_locking_inset) {
2210                 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2211                         return true;
2212         }
2213         if (!locked) {
2214                 ParagraphList::iterator pit = paragraphs.begin();
2215                 ParagraphList::iterator pend = paragraphs.end();
2216
2217                 while (boost::next(pit) != pend)
2218                         ++pit;
2219
2220                 text_.setCursor(pit, pit->size());
2221         }
2222         lyxfind::SearchResult result =
2223                 lyxfind::LyXFind(bv, &text_, str, false, cs, mw);
2224
2225         if (result == lyxfind::SR_FOUND) {
2226                 LyXCursor cur = text_.cursor;
2227                 bv->unlockInset(bv->theLockingInset());
2228                 if (bv->lockInset(this))
2229                         locked = true;
2230                 text_.cursor = cur;
2231                 text_.setSelectionRange(str.length());
2232                 updateLocal(bv, SELECTION, false);
2233         }
2234         return (result != lyxfind::SR_NOT_FOUND);
2235 }
2236
2237
2238 bool InsetText::checkInsertChar(LyXFont & font)
2239 {
2240         if (owner())
2241                 return owner()->checkInsertChar(font);
2242         return true;
2243 }
2244
2245
2246 void InsetText::collapseParagraphs(BufferView * bv)
2247 {
2248         while (paragraphs.size() > 1) {
2249                 ParagraphList::iterator first_par = paragraphs.begin();
2250                 ParagraphList::iterator next_par = boost::next(first_par);
2251                 size_t const first_par_size = first_par->size();
2252
2253                 if (!first_par->empty() &&
2254                     !next_par->empty() &&
2255                     !first_par->isSeparator(first_par_size - 1)) {
2256                         first_par->insertChar(first_par_size, ' ');
2257                 }
2258
2259                 if (text_.selection.set()) {
2260                         if (text_.selection.start.par() == next_par) {
2261                                 text_.selection.start.par(first_par);
2262                                 text_.selection.start.pos(
2263                                         text_.selection.start.pos() + first_par_size);
2264                         }
2265                         if (text_.selection.end.par() == next_par) {
2266                                 text_.selection.end.par(first_par);
2267                                 text_.selection.end.pos(
2268                                         text_.selection.end.pos() + first_par_size);
2269                         }
2270                 }
2271
2272                 mergeParagraph(bv->buffer()->params, paragraphs, first_par);
2273         }
2274         reinitLyXText();
2275 }
2276
2277
2278 void InsetText::getDrawFont(LyXFont & font) const
2279 {
2280         if (!owner())
2281                 return;
2282         owner()->getDrawFont(font);
2283 }
2284
2285
2286 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
2287 {
2288 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
2289 // And it probably does. You have to take a look at this John. (Lgb)
2290 #warning John, have a look here. (Lgb)
2291         ParagraphList::iterator pit = plist.begin();
2292         ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
2293         ++pit;
2294         mergeParagraph(buffer->params, paragraphs, boost::prior(ins));
2295
2296         ParagraphList::iterator pend = plist.end();
2297         for (; pit != pend; ++pit) {
2298                 paragraphs.push_back(*pit);
2299         }
2300
2301         reinitLyXText();
2302 }
2303
2304
2305 void InsetText::addPreview(PreviewLoader & loader) const
2306 {
2307         ParagraphList::const_iterator pit = paragraphs.begin();
2308         ParagraphList::const_iterator pend = paragraphs.end();
2309
2310         for (; pit != pend; ++pit) {
2311                 InsetList::const_iterator it  = pit->insetlist.begin();
2312                 InsetList::const_iterator end = pit->insetlist.end();
2313                 for (; it != end; ++it) {
2314                         it->inset->addPreview(loader);
2315                 }
2316         }
2317 }