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