]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
more global cursor work
[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 #include "insetnewline.h"
15
16 #include "buffer.h"
17 #include "bufferparams.h"
18 #include "BufferView.h"
19 #include "CutAndPaste.h"
20 #include "cursor.h"
21 #include "debug.h"
22 #include "dispatchresult.h"
23 #include "errorlist.h"
24 #include "funcrequest.h"
25 #include "gettext.h"
26 #include "intl.h"
27 #include "LColor.h"
28 #include "lyxfind.h"
29 #include "lyxlex.h"
30 #include "lyxrc.h"
31 #include "metricsinfo.h"
32 #include "output_docbook.h"
33 #include "output_latex.h"
34 #include "output_linuxdoc.h"
35 #include "output_plaintext.h"
36 #include "paragraph.h"
37 #include "paragraph_funcs.h"
38 #include "ParagraphParameters.h"
39 #include "rowpainter.h"
40 #include "sgml.h"
41 #include "texrow.h"
42 #include "undo.h"
43
44 #include "frontends/Alert.h"
45 #include "frontends/font_metrics.h"
46 #include "frontends/LyXView.h"
47 #include "frontends/Painter.h"
48
49 #include "support/lyxalgo.h" // lyx::count
50
51 #include <boost/bind.hpp>
52
53 using bv_funcs::replaceSelection;
54
55 using lyx::pos_type;
56
57 using lyx::graphics::PreviewLoader;
58
59 using lyx::support::isStrUnsignedInt;
60 using lyx::support::strToUnsignedInt;
61
62 using std::endl;
63 using std::for_each;
64 using std::max;
65 using std::string;
66 using std::auto_ptr;
67 using std::ostream;
68 using std::vector;
69
70
71 InsetText::InsetText(BufferParams const & bp)
72         : UpdatableInset(),
73           paragraphs(1),
74           autoBreakRows_(false),
75           drawFrame_(NEVER),
76           frame_color_(LColor::insetframe),
77           text_(0, this, true, paragraphs)
78 {
79         textwidth_ = 0; // broken
80         paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
81         if (bp.tracking_changes)
82                 paragraphs.begin()->trackChanges();
83         init();
84 }
85
86
87 InsetText::InsetText(InsetText const & in)
88         : UpdatableInset(in),
89           text_(in.text_.bv_owner, this, true, paragraphs)
90 {
91         // this is ugly...
92         operator=(in);
93 }
94
95
96 void InsetText::operator=(InsetText const & in)
97 {
98         UpdatableInset::operator=(in);
99         paragraphs = in.paragraphs;
100         autoBreakRows_ = in.autoBreakRows_;
101         drawFrame_ = in.drawFrame_;
102         frame_color_ = in.frame_color_;
103         textwidth_ = in.textwidth_;
104         text_ = LyXText(in.text_.bv_owner, this, true, paragraphs);
105         init();
106 }
107
108
109 void InsetText::init()
110 {
111         ParagraphList::iterator pit = paragraphs.begin();
112         ParagraphList::iterator end = paragraphs.end();
113         for (; pit != end; ++pit)
114                 pit->setInsetOwner(this);
115         text_.paragraphs_ = &paragraphs;
116
117         locked = false;
118         inset_x = 0;
119         inset_y = 0;
120         no_selection = true;
121         the_locking_inset = 0;
122         old_par = -1;
123         in_insetAllowed = false;
124         mouse_x = 0;
125         mouse_y = 0;
126 }
127
128
129 void InsetText::clear(bool just_mark_erased)
130 {
131         if (just_mark_erased) {
132                 ParagraphList::iterator it = paragraphs.begin();
133                 ParagraphList::iterator end = paragraphs.end();
134                 for (; it != end; ++it)
135                         it->markErased();
136                 return;
137         }
138
139         // This is a gross hack...
140         LyXLayout_ptr old_layout = paragraphs.begin()->layout();
141
142         paragraphs.clear();
143         paragraphs.push_back(Paragraph());
144         paragraphs.begin()->setInsetOwner(this);
145         paragraphs.begin()->layout(old_layout);
146 }
147
148
149 auto_ptr<InsetBase> InsetText::clone() const
150 {
151         return auto_ptr<InsetBase>(new InsetText(*this));
152 }
153
154
155 void InsetText::write(Buffer const & buf, ostream & os) const
156 {
157         os << "Text\n";
158         writeParagraphData(buf, os);
159 }
160
161
162 void InsetText::writeParagraphData(Buffer const & buf, ostream & os) const
163 {
164         ParagraphList::const_iterator it = paragraphs.begin();
165         ParagraphList::const_iterator end = paragraphs.end();
166         Paragraph::depth_type dth = 0;
167         for (; it != end; ++it) {
168                 it->write(buf, os, buf.params(), dth);
169         }
170 }
171
172
173 void InsetText::read(Buffer const & buf, LyXLex & lex)
174 {
175         string token;
176         Paragraph::depth_type depth = 0;
177
178         clear(false);
179
180 #warning John, look here. Doesnt make much sense.
181         if (buf.params().tracking_changes)
182                 paragraphs.begin()->trackChanges();
183
184         // delete the initial paragraph
185         Paragraph oldpar = *paragraphs.begin();
186         paragraphs.clear();
187         ParagraphList::iterator pit = paragraphs.begin();
188
189         while (lex.isOK()) {
190                 lex.nextToken();
191                 token = lex.getString();
192                 if (token.empty())
193                         continue;
194                 if (token == "\\end_inset") {
195                         break;
196                 }
197
198                 if (token == "\\end_document") {
199                         lex.printError("\\end_document read in inset! Error in document!");
200                         return;
201                 }
202
203                 // FIXME: ugly.
204                 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs, pit, depth);
205         }
206
207         pit = paragraphs.begin();
208         ParagraphList::iterator const end = paragraphs.end();
209         for (; pit != end; ++pit)
210                 pit->setInsetOwner(this);
211
212         if (token != "\\end_inset") {
213                 lex.printError("Missing \\end_inset at this point. "
214                                            "Read: `$$Token'");
215         }
216
217         // sanity check
218         // ensure we have at least one par.
219         if (paragraphs.empty())
220                 paragraphs.push_back(oldpar);
221 }
222
223
224 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
225 {
226         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
227         textwidth_ = mi.base.textwidth - 30;
228         BufferView * bv = mi.base.bv;
229         setViewCache(bv);
230         text_.metrics(mi, dim);
231         dim.asc += TEXT_TO_INSET_OFFSET;
232         dim.des += TEXT_TO_INSET_OFFSET;
233         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
234         dim.wid = max(dim.wid, 10);
235         dim_ = dim;
236 }
237
238
239 int InsetText::textWidth() const
240 {
241         return textwidth_;
242 }
243
244
245 void InsetText::draw(PainterInfo & pi, int x, int y) const
246 {
247         // update our idea of where we are. Clearly, we should
248         // not have to know this information.
249         top_x = x;
250
251         int const start_x = x;
252
253         BufferView * bv = pi.base.bv;
254         Painter & pain = pi.pain;
255
256         // repaint the background if needed
257         if (backgroundColor() != LColor::background)
258                 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, y);
259
260         // no draw is necessary !!!
261         if (drawFrame_ == LOCKED && !locked && paragraphs.begin()->empty()) {
262                 top_baseline = y;
263                 return;
264         }
265
266         bv->hideCursor();
267
268         if (!owner())
269                 x += scroll();
270
271         top_baseline = y;
272         inset_x = cx() - x;
273         inset_y = cy();
274
275         x += TEXT_TO_INSET_OFFSET;
276
277         paintTextInset(*bv, text_, x, y);
278
279         if (drawFrame_ == ALWAYS || (drawFrame_ == LOCKED && locked))
280                 drawFrame(pain, start_x);
281 }
282
283
284 void InsetText::drawFrame(Painter & pain, int x) const
285 {
286         int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
287         int const frame_x = x + ttoD2;
288         int const frame_y = top_baseline - dim_.asc + ttoD2;
289         int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
290         int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
291         pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
292 }
293
294
295 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
296 {
297         if (!bv)
298                 return;
299
300         if (!autoBreakRows_ && paragraphs.size() > 1)
301                 collapseParagraphs(bv);
302
303         if (!text_.selection.set())
304                 text_.selection.cursor = text_.cursor;
305
306         bv->fitCursor();
307         bv->updateInset(this);
308         bv->owner()->view_state_changed();
309         bv->owner()->updateMenubar();
310         bv->owner()->updateToolbar();
311         if (old_par != text_.cursor.par()) {
312                 bv->owner()->setLayout(cpar()->layout()->name());
313                 old_par = text_.cursor.par();
314         }
315 }
316
317
318 string const InsetText::editMessage() const
319 {
320         return _("Opened Text Inset");
321 }
322
323
324 void InsetText::insetUnlock(BufferView * bv)
325 {
326         if (the_locking_inset) {
327                 the_locking_inset->insetUnlock(bv);
328                 the_locking_inset = 0;
329                 updateLocal(bv, false);
330         }
331         no_selection = true;
332         locked = false;
333
334         if (text_.selection.set())
335                 text_.clearSelection();
336         else if (owner())
337                 bv->owner()->setLayout(owner()->getLyXText(bv)
338                                        ->cursorPar()->layout()->name());
339         else
340                 bv->owner()->setLayout(bv->text->cursorPar()->layout()->name());
341         // hack for deleteEmptyParMech
342         if (!paragraphs.begin()->empty())
343                 text_.setCursor(0, 0);
344         else if (paragraphs.size() > 1)
345                 text_.setCursor(1, 0);
346 }
347
348
349 void InsetText::lockInset(BufferView * bv)
350 {
351         locked = true;
352         the_locking_inset = 0;
353         inset_x = inset_y = 0;
354         inset_boundary = false;
355         old_par = -1;
356         text_.setCursorIntern(0, 0);
357         text_.clearSelection();
358         finishUndo();
359         // If the inset is empty set the language of the current font to the
360         // language to the surronding text (if different).
361         if (paragraphs.begin()->empty() && paragraphs.size() == 1 &&
362                 bv->getParentLanguage(this) != text_.current_font.language()) {
363                 LyXFont font(LyXFont::ALL_IGNORE);
364                 font.setLanguage(bv->getParentLanguage(this));
365                 setFont(bv, font, false);
366         }
367 }
368
369
370 void InsetText::lockInset(BufferView * /*bv*/, UpdatableInset * inset)
371 {
372         the_locking_inset = inset;
373         inset_x = cx() - top_x;
374         inset_y = cy();
375         inset_boundary = cboundary();
376 }
377
378
379 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
380 {
381         lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
382                               << inset << "): " << endl;
383         if (!inset)
384                 return false;
385         if (!the_locking_inset) {
386                 ParagraphList::iterator pit = paragraphs.begin();
387                 ParagraphList::iterator pend = paragraphs.end();
388
389                 for (; pit != pend; ++pit) {
390                         InsetList::iterator it = pit->insetlist.begin();
391                         InsetList::iterator const end = pit->insetlist.end();
392                         for (; it != end; ++it) {
393                                 if (it->inset == inset) {
394                                         lyxerr << "InsetText::lockInsetInInset: 1 a" << endl;
395                                         text_.setCursorIntern(
396                                                 std::distance(paragraphs.begin(), pit), it->pos);
397                                         lyxerr << "InsetText::lockInsetInInset: 1 b" << endl;
398                                         lyxerr << "bv: " << bv << " inset: " << inset << endl;
399                                         lockInset(bv, inset);
400                                         lyxerr << "InsetText::lockInsetInInset: 1 c" << endl;
401                                         return true;
402                                 }
403                         }
404                 }
405                 lyxerr << "InsetText::lockInsetInInset: 3" << endl;
406                 return false;
407         }
408         if (inset == cpar()->getInset(cpos())) {
409                 lyxerr[Debug::INSETS] << "OK" << endl;
410                 lockInset(bv, inset);
411                 return true;
412         }
413
414         if (the_locking_inset && the_locking_inset == inset) {
415                 inset_x = cx() - top_x;
416                 inset_y = cy();
417         } else if (the_locking_inset) {
418                 lyxerr[Debug::INSETS] << "MAYBE" << endl;
419                 return the_locking_inset->lockInsetInInset(bv, inset);
420         }
421         lyxerr[Debug::INSETS] << "NOT OK" << endl;
422         return false;
423 }
424
425
426 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
427                                    bool lr)
428 {
429         if (!the_locking_inset)
430                 return false;
431         if (the_locking_inset == inset) {
432                 the_locking_inset->insetUnlock(bv);
433                 the_locking_inset = 0;
434                 if (lr)
435                         moveRightIntern(bv, true, false);
436                 old_par = -1; // force layout setting
437                 if (scroll())
438                         scroll(bv, 0.0F);
439                 else
440                         updateLocal(bv, false);
441                 return true;
442         }
443         return the_locking_inset->unlockInsetInInset(bv, inset, lr);
444 }
445
446
447 void InsetText::lfunMousePress(FuncRequest const & cmd)
448 {
449         no_selection = true;
450
451         // use this to check mouse motion for selection!
452         mouse_x = cmd.x;
453         mouse_y = cmd.y;
454
455         BufferView * bv = cmd.view();
456         FuncRequest cmd1 = cmd;
457         cmd1.x -= inset_x;
458         cmd1.y -= inset_y;
459         if (!locked)
460                 lockInset(bv);
461
462         int tmp_x = cmd.x;
463         int tmp_y = cmd.y + dim_.asc - bv->top_y();
464         InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
465
466         if (the_locking_inset) {
467                 if (the_locking_inset == inset) {
468                         the_locking_inset->dispatch(cmd1);
469                         return;
470                 }
471                 // otherwise only unlock the_locking_inset
472                 the_locking_inset->insetUnlock(bv);
473                 the_locking_inset = 0;
474         }
475         if (!inset)
476                 no_selection = false;
477
478         if (bv->theLockingInset()) {
479                 if (isHighlyEditableInset(inset)) {
480                         // We just have to lock the inset before calling a
481                         // PressEvent on it!
482                         UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
483                         if (!bv->lockInset(uinset)) {
484                                 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
485                         }
486                         inset->dispatch(cmd1);
487                         if (the_locking_inset)
488                                 updateLocal(bv, false);
489                         return;
490                 }
491         }
492         if (!inset) {
493                 bool paste_internally = false;
494                 if (cmd.button() == mouse_button::button2 && getLyXText(bv)->selection.set()) {
495                         dispatch(FuncRequest(bv, LFUN_COPY));
496                         paste_internally = true;
497                 }
498                 int old_top_y = bv->top_y();
499
500                 text_.setCursorFromCoordinates(cmd.x, cmd.y + dim_.asc);
501                 // set the selection cursor!
502                 text_.selection.cursor = text_.cursor;
503                 bv->x_target(text_.cursor.x());
504
505                 text_.clearSelection();
506                 updateLocal(bv, false);
507
508                 bv->owner()->setLayout(cpar()->layout()->name());
509
510                 // we moved the view we cannot do mouse selection in this case!
511                 if (bv->top_y() != old_top_y)
512                         no_selection = true;
513                 old_par = text_.cursor.par();
514                 // Insert primary selection with middle mouse
515                 // if there is a local selection in the current buffer,
516                 // insert this
517                 if (cmd.button() == mouse_button::button2) {
518                         if (paste_internally)
519                                 dispatch(FuncRequest(bv, LFUN_PASTE));
520                         else
521                                 dispatch(FuncRequest(bv, LFUN_PASTESELECTION, "paragraph"));
522                 }
523         } else {
524                 getLyXText(bv)->clearSelection();
525         }
526 }
527
528
529 bool InsetText::lfunMouseRelease(FuncRequest const & cmd)
530 {
531         BufferView * bv = cmd.view();
532         FuncRequest cmd1 = cmd;
533         cmd1.x -= inset_x;
534         cmd1.y -= inset_y;
535
536         no_selection = true;
537         if (the_locking_inset) {
538                 DispatchResult const res = the_locking_inset->dispatch(cmd1);
539                 return res.dispatched();
540         }
541
542         int tmp_x = cmd.x;
543         int tmp_y = cmd.y + dim_.asc - bv->top_y();
544         InsetOld * inset = getLyXText(bv)->checkInsetHit(tmp_x, tmp_y);
545         if (!inset)
546                 return false;
547
548         // We still need to deal properly with the whole relative vs.
549         // absolute mouse co-ords thing in a realiable, sensible way
550         DispatchResult const res = inset->dispatch(cmd1);
551         bool const ret = res.dispatched();
552         updateLocal(bv, false);
553         return ret;
554 }
555
556
557 void InsetText::lfunMouseMotion(FuncRequest const & cmd)
558 {
559         FuncRequest cmd1 = cmd;
560         cmd1.x -= inset_x;
561         cmd1.y -= inset_y;
562
563         if (the_locking_inset) {
564                 the_locking_inset->dispatch(cmd1);
565                 return;
566         }
567
568         if (no_selection || (mouse_x == cmd.x && mouse_y == cmd.y))
569                 return;
570
571         BufferView * bv = cmd.view();
572         LyXCursor cur = text_.cursor;
573         text_.setCursorFromCoordinates (cmd.x, cmd.y + dim_.asc);
574         bv->x_target(text_.cursor.x());
575         if (cur == text_.cursor)
576                 return;
577         text_.setSelection();
578         updateLocal(bv, false);
579 }
580
581
582 void InsetText::edit(BufferView * bv, bool left)
583 {
584         setViewCache(bv);
585         
586         if (!bv->lockInset(this)) {
587                 lyxerr << "Cannot lock inset" << endl;
588                 return;
589         }
590         lyxerr << "InsetText: edit left/right" << endl;
591
592         locked = true;
593         the_locking_inset = 0;
594         inset_x = 0;
595         inset_y = 0;
596         inset_boundary = false;
597         old_par = -1;
598
599         if (left)
600                 text_.setCursorIntern(0, 0);
601         else
602                 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
603
604         // If the inset is empty set the language of the current font to the
605         // language to the surronding text (if different).
606         if (paragraphs.begin()->empty() &&
607                         paragraphs.size() == 1 &&
608                         bv->getParentLanguage(this) != text_.current_font.language())
609         {
610                 LyXFont font(LyXFont::ALL_IGNORE);
611                 font.setLanguage(bv->getParentLanguage(this));
612                 setFont(bv, font, false);
613         }
614
615         updateLocal(bv, false);
616         // Tell the paragraph dialog that we've entered an insettext.
617         bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
618 }
619
620
621 void InsetText::edit(BufferView * bv, int x, int y)
622 {
623         if (!bv->lockInset(this)) {
624                 lyxerr << "Cannot lock inset" << endl;
625                 return;
626         }
627         lyxerr << "InsetText: edit xy" << endl;
628
629         locked = true;
630         the_locking_inset = 0;
631         inset_x = 0;
632         inset_y = 0;
633         inset_boundary = false;
634         old_par = -1;
635
636         int tmp_y = (y < 0) ? 0 : y;
637         // we put here -1 and not button as now the button in the
638         // edit call should not be needed we will fix this in 1.3.x
639         // cycle hopefully (Jug 20020509)
640         // FIXME: GUII I've changed this to none: probably WRONG
641         if (!checkAndActivateInset(bv, x, tmp_y)) {
642                 text_.setCursorFromCoordinates(x, y + dim_.asc);
643                 text_.cursor.x(text_.cursor.x());
644                 bv->x_target(text_.cursor.x());
645         }
646
647         text_.clearSelection();
648         finishUndo();
649
650         // If the inset is empty set the language of the current font to the
651         // language to the surronding text (if different).
652         if (paragraphs.begin()->empty() &&
653                         paragraphs.size() == 1 &&
654                         bv->getParentLanguage(this) != text_.current_font.language())
655         {
656                 LyXFont font(LyXFont::ALL_IGNORE);
657                 font.setLanguage(bv->getParentLanguage(this));
658                 setFont(bv, font, false);
659         }
660
661         updateLocal(bv, false);
662         // Tell the paragraph dialog that we've entered an insettext.
663         bv->dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
664 }
665
666
667 DispatchResult
668 InsetText::priv_dispatch(FuncRequest const & cmd,
669                          idx_type & idx, pos_type & pos)
670 {
671         BufferView * bv = cmd.view();
672         setViewCache(bv);
673
674         switch (cmd.action) {
675         case LFUN_MOUSE_PRESS:
676                 lfunMousePress(cmd);
677                 return DispatchResult(true, true);
678
679         case LFUN_MOUSE_MOTION:
680                 lfunMouseMotion(cmd);
681                 return DispatchResult(true, true);
682
683         case LFUN_MOUSE_RELEASE:
684                 return DispatchResult(lfunMouseRelease(cmd));
685
686         default:
687                 break;
688         }
689
690         bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
691         no_selection = false;
692
693         DispatchResult result = UpdatableInset::priv_dispatch(cmd, idx, pos);
694         if (result.dispatched())
695                 return result;
696
697 #if 0
698         // This looks utterly strange. (Lgb)
699         if (cmd.action == LFUN_UNKNOWN_ACTION && cmd.argument.empty())
700                 return DispatchResult(false, FINISHED);
701 #endif
702
703         if (the_locking_inset) {
704                 DispatchResult result = the_locking_inset->dispatch(cmd);
705
706                 if (result.dispatched()) {
707                         if (result.update()) {
708                                 result.update(false);
709                                 updateLocal(bv, false);
710                         }
711                         return result;
712                 }
713
714                 switch (result.val()) {
715                 case FINISHED_RIGHT:
716                         moveRightIntern(bv, false, false);
717                         result.dispatched(true);
718                         result.update(true);
719                         break;
720                 case FINISHED_UP:
721                         result = moveUp(bv);
722                         if (result.val() >= FINISHED) {
723                                 updateLocal(bv, false);
724                                 bv->unlockInset(this);
725                         }
726                         break;
727                 case FINISHED_DOWN:
728                         result = moveDown(bv);
729                         if (result.val() >= FINISHED) {
730                                 updateLocal(bv, false);
731                                 bv->unlockInset(this);
732                         }
733                         break;
734                 default:
735                         result.dispatched(true);
736                         result.update(true);
737                         break;
738                 }
739                 the_locking_inset = 0;
740                 updateLocal(bv, false);
741                 // make sure status gets reset immediately
742                 bv->owner()->clearMessage();
743                 return result;
744         }
745
746         switch (cmd.action) {
747         // Normal chars
748         case LFUN_SELFINSERT:
749                 if (bv->buffer()->isReadonly()) {
750 //          setErrorMessage(N_("Document is read only"));
751                         break;
752                 }
753                 if (!cmd.argument.empty()) {
754                         /* Automatically delete the currently selected
755                          * text and replace it with what is being
756                          * typed in now. Depends on lyxrc settings
757                          * "auto_region_delete", which defaults to
758                          * true (on). */
759 #if 0
760                         // This should not be needed here and is also WRONG!
761                         recordUndo(bv, Undo::INSERT, text_.cursorPar());
762 #endif
763                         bv->switchKeyMap();
764
765                         if (lyxrc.auto_region_delete && text_.selection.set())
766                                 text_.cutSelection(false, false);
767                         text_.clearSelection();
768
769                         for (string::size_type i = 0; i < cmd.argument.length(); ++i)
770                                 bv->owner()->getIntl().getTransManager().
771                                         TranslateAndInsert(cmd.argument[i], &text_);
772                 }
773                 text_.selection.cursor = text_.cursor;
774                 result.dispatched(true);
775                 result.update(true);
776                 break;
777
778         // cursor movements that need special handling
779
780         case LFUN_RIGHT:
781                 result = moveRight(bv);
782                 finishUndo();
783                 break;
784         case LFUN_LEFT:
785                 finishUndo();
786                 result = moveLeft(bv);
787                 break;
788         case LFUN_DOWN:
789                 finishUndo();
790                 result = moveDown(bv);
791                 break;
792         case LFUN_UP:
793                 finishUndo();
794                 result = moveUp(bv);
795                 break;
796
797         case LFUN_PRIOR:
798                 if (crow() == text_.firstRow()) {
799                         result.val(FINISHED_UP);
800                         lyxerr << "InsetText: cursor pop 1" << endl;
801                         bv->cursor().pop();
802                 } else {
803                         text_.cursorPrevious();
804                         text_.clearSelection();
805                         result.dispatched(true);
806                 }
807                 break;
808
809         case LFUN_NEXT:
810                 if (crow() == text_.lastRow()) {
811                         result.val(FINISHED_DOWN);
812                         lyxerr << "InsetText: cursor pop 2" << endl;
813                         bv->cursor().pop();
814                 } else {
815                         text_.cursorNext();
816                         text_.clearSelection();
817                         result.dispatched(true);
818                 }
819                 break;
820
821         case LFUN_BACKSPACE:
822                 if (text_.selection.set())
823                         text_.cutSelection(true, false);
824                 else
825                         text_.backspace();
826 #warning should be also set dispatched here?
827                 result.update(true);
828                 break;
829
830         case LFUN_DELETE:
831                 if (text_.selection.set())
832                         text_.cutSelection(true, false);
833                 else
834                         text_.Delete();
835 #warning should be also set dispatched here?
836                 result.update(true);
837                 break;
838
839         case LFUN_PASTE:
840                 if (!autoBreakRows_) {
841                         if (CutAndPaste::nrOfParagraphs() > 1) {
842 #ifdef WITH_WARNINGS
843 #warning FIXME horrendously bad UI
844 #endif
845                                 Alert::error(_("Paste failed"), _("Cannot include more than one paragraph."));
846                         }
847                 } else {
848                         replaceSelection(bv->getLyXText());
849                         size_t sel_index = 0;
850                         string const & arg = cmd.argument;
851                         if (isStrUnsignedInt(arg)) {
852 #warning FIXME Check if the arg is in the domain of available selections.
853                                 sel_index = strToUnsignedInt(arg);
854                         }
855                         text_.pasteSelection(sel_index);
856                         // bug 393
857                         text_.clearSelection();
858 #warning should be also set dispatched here?
859                         result.update(true);
860                 }
861                 break;
862
863         case LFUN_BREAKPARAGRAPH:
864                 if (!autoBreakRows_) {
865                         result.dispatched(true);
866                         result.update(true);
867                 } else {
868                         replaceSelection(bv->getLyXText());
869                         text_.breakParagraph(paragraphs, 0);
870 #warning should be also set dispatched here?
871                         result.update(true);
872                 }
873                 break;
874
875         case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
876                 if (!autoBreakRows_) {
877                         result.dispatched(true);
878                         result.update(true);
879                 } else {
880                         replaceSelection(bv->getLyXText());
881                         text_.breakParagraph(paragraphs, 1);
882 #warning should be also set dispatched here?
883                         result.update(true);
884                 }
885                 break;
886
887         case LFUN_BREAKLINE: {
888                 if (!autoBreakRows_) {
889                         result.dispatched(true);
890                         result.update(true);
891                 } else {
892                         replaceSelection(bv->getLyXText());
893                         auto_ptr<InsetNewline> ins(new InsetNewline);
894                         text_.insertInset(ins.release());
895 #warning should be also set dispatched here?
896                         result.update(true);
897                 }
898                 break;
899         }
900
901         case LFUN_LAYOUT:
902                 // do not set layouts on non breakable textinsets
903                 if (autoBreakRows_) {
904                         string cur_layout = cpar()->layout()->name();
905
906                         // Derive layout number from given argument (string)
907                         // and current buffer's textclass (number).
908                         LyXTextClass const & tclass =
909                                 bv->buffer()->params().getLyXTextClass();
910                         string layout = cmd.argument;
911                         bool hasLayout = tclass.hasLayout(layout);
912
913                         // If the entry is obsolete, use the new one instead.
914                         if (hasLayout) {
915                                 string const & obs = tclass[layout]->obsoleted_by();
916                                 if (!obs.empty())
917                                         layout = obs;
918                         }
919
920                         // see if we found the layout number:
921                         if (!hasLayout) {
922                                 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + cmd.argument + N_(" not known"));
923                                 bv->owner()->dispatch(lf);
924                                 break;
925                         }
926
927                         if (cur_layout != layout) {
928                                 cur_layout = layout;
929                                 text_.setLayout(layout);
930                                 bv->owner()->setLayout(cpar()->layout()->name());
931 #warning should be also set dispatched here?
932                                 result.update(true);
933                         }
934                 } else {
935                         // reset the layout box
936                         bv->owner()->setLayout(cpar()->layout()->name());
937                 }
938                 break;
939
940         default:
941                 break;
942         }
943
944         if (result.update()) {
945                 result.update(false);
946                 updateLocal(bv, true);
947         }
948
949         /// If the action has deleted all text in the inset, we need to change the
950         // language to the language of the surronding text.
951         if (!was_empty && paragraphs.begin()->empty() &&
952             paragraphs.size() == 1) {
953                 LyXFont font(LyXFont::ALL_IGNORE);
954                 font.setLanguage(bv->getParentLanguage(this));
955                 setFont(bv, font, false);
956         }
957
958         if (result.val() >= FINISHED) {
959                 result.val(NONE);
960                 bv->unlockInset(this);
961         }
962
963         return result;
964 }
965
966
967 int InsetText::latex(Buffer const & buf, ostream & os,
968                      OutputParams const & runparams) const
969 {
970         TexRow texrow;
971         latexParagraphs(buf, paragraphs, os, texrow, runparams);
972         return texrow.rows();
973 }
974
975
976 int InsetText::plaintext(Buffer const & buf, ostream & os,
977                      OutputParams const & runparams) const
978 {
979         ParagraphList::const_iterator beg = paragraphs.begin();
980         ParagraphList::const_iterator end = paragraphs.end();
981         ParagraphList::const_iterator it = beg;
982         for (; it != end; ++it)
983                 asciiParagraph(buf, *it, os, runparams, it == beg);
984
985         //FIXME: Give the total numbers of lines
986         return 0;
987 }
988
989
990 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
991                         OutputParams const & runparams) const
992 {
993         linuxdocParagraphs(buf, paragraphs, os, runparams);
994         return 0;
995 }
996
997
998 int InsetText::docbook(Buffer const & buf, ostream & os,
999                        OutputParams const & runparams) const
1000 {
1001         docbookParagraphs(buf, paragraphs, os, runparams);
1002         return 0;
1003 }
1004
1005
1006 void InsetText::validate(LaTeXFeatures & features) const
1007 {
1008         for_each(paragraphs.begin(), paragraphs.end(),
1009                  boost::bind(&Paragraph::validate, _1, boost::ref(features)));
1010 }
1011
1012
1013 void InsetText::getCursor(BufferView & bv, int & x, int & y) const
1014 {
1015         if (the_locking_inset) {
1016                 the_locking_inset->getCursor(bv, x, y);
1017                 return;
1018         }
1019         x = cx();
1020         y = cy() + InsetText::y();
1021 }
1022
1023
1024 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1025 {
1026         if (the_locking_inset) {
1027                 the_locking_inset->getCursorPos(bv, x, y);
1028                 return;
1029         }
1030         x = cx() - top_x - TEXT_TO_INSET_OFFSET;
1031         y = cy() - TEXT_TO_INSET_OFFSET;
1032 }
1033
1034
1035 int InsetText::insetInInsetY() const
1036 {
1037         if (!the_locking_inset)
1038                 return 0;
1039
1040         return inset_y + the_locking_inset->insetInInsetY();
1041 }
1042
1043
1044 void InsetText::fitInsetCursor(BufferView * bv) const
1045 {
1046         if (the_locking_inset) {
1047                 the_locking_inset->fitInsetCursor(bv);
1048                 return;
1049         }
1050
1051         LyXFont const font = text_.getFont(cpar(), cpos());
1052
1053         int const asc = font_metrics::maxAscent(font);
1054         int const desc = font_metrics::maxDescent(font);
1055
1056         bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1057 }
1058
1059
1060 DispatchResult InsetText::moveRight(BufferView * bv)
1061 {
1062         if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1063                 return moveLeftIntern(bv, false, true, false);
1064         else
1065                 return moveRightIntern(bv, true, true, false);
1066 }
1067
1068
1069 DispatchResult InsetText::moveLeft(BufferView * bv)
1070 {
1071         if (text_.cursorPar()->isRightToLeftPar(bv->buffer()->params()))
1072                 return moveRightIntern(bv, true, true, false);
1073         else
1074                 return moveLeftIntern(bv, false, true, false);
1075 }
1076
1077
1078 DispatchResult
1079 InsetText::moveRightIntern(BufferView * bv, bool front,
1080                            bool activate_inset, bool selecting)
1081 {
1082         ParagraphList::iterator c_par = cpar();
1083
1084         if (boost::next(c_par) == paragraphs.end() && cpos() >= c_par->size()) {
1085                 lyxerr << "InsetText: cursor pop 3" << endl;
1086                 bv->cursor().pop();
1087                 return DispatchResult(false, FINISHED_RIGHT);
1088         }
1089         if (activate_inset && checkAndActivateInset(bv, front))
1090                 return DispatchResult(true, true);
1091         text_.cursorRight(bv);
1092         if (!selecting)
1093                 text_.clearSelection();
1094         return DispatchResult(true);
1095 }
1096
1097
1098 DispatchResult
1099 InsetText::moveLeftIntern(BufferView * bv, bool front,
1100                           bool activate_inset, bool selecting)
1101 {
1102         if (cpar() == paragraphs.begin() && cpos() <= 0) {
1103                 lyxerr << "InsetText: cursor pop 4" << endl;
1104                 bv->cursor().pop();
1105                 return DispatchResult(false, FINISHED);
1106         }
1107         text_.cursorLeft(bv);
1108         if (!selecting)
1109                 text_.clearSelection();
1110         if (activate_inset && checkAndActivateInset(bv, front))
1111                 return DispatchResult(true, true);
1112         return DispatchResult(true);
1113 }
1114
1115
1116 DispatchResult InsetText::moveUp(BufferView * bv)
1117 {
1118         if (crow() == text_.firstRow()) {
1119                 lyxerr << "InsetText: cursor pop 5" << endl;
1120                 bv->cursor().pop();
1121                 return DispatchResult(false, FINISHED_UP);
1122         }
1123         text_.cursorUp(bv);
1124         text_.clearSelection();
1125         return DispatchResult(true);
1126 }
1127
1128
1129 DispatchResult InsetText::moveDown(BufferView * bv)
1130 {
1131         if (crow() == text_.lastRow()) {
1132                 lyxerr << "InsetText: cursor pop 6" << endl;
1133                 bv->cursor().pop();
1134                 return DispatchResult(false, FINISHED_DOWN);
1135         }
1136         text_.cursorDown(bv);
1137         text_.clearSelection();
1138         return DispatchResult(true);
1139 }
1140
1141
1142 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
1143 {
1144         if (the_locking_inset) {
1145                 if (the_locking_inset->insetAllowed(inset))
1146                         return the_locking_inset->insertInset(bv, inset);
1147                 return false;
1148         }
1149         inset->setOwner(this);
1150         text_.insertInset(inset);
1151         bv->fitCursor();
1152         updateLocal(bv, true);
1153         return true;
1154 }
1155
1156
1157 bool InsetText::insetAllowed(InsetOld::Code code) const
1158 {
1159         // in_insetAllowed is a really gross hack,
1160         // to allow us to call the owner's insetAllowed
1161         // without stack overflow, which can happen
1162         // when the owner uses InsetCollapsable::insetAllowed()
1163         bool ret = true;
1164         if (in_insetAllowed)
1165                 return ret;
1166         in_insetAllowed = true;
1167         if (the_locking_inset)
1168                 ret = the_locking_inset->insetAllowed(code);
1169         else if (owner())
1170                 ret = owner()->insetAllowed(code);
1171         in_insetAllowed = false;
1172         return ret;
1173 }
1174
1175
1176 UpdatableInset * InsetText::getLockingInset() const
1177 {
1178         return the_locking_inset ? the_locking_inset->getLockingInset() :
1179                 const_cast<InsetText *>(this);
1180 }
1181
1182
1183 UpdatableInset * InsetText::getFirstLockingInsetOfType(InsetOld::Code c)
1184 {
1185         if (c == lyxCode())
1186                 return this;
1187         if (the_locking_inset)
1188                 return the_locking_inset->getFirstLockingInsetOfType(c);
1189         return 0;
1190 }
1191
1192
1193 bool InsetText::showInsetDialog(BufferView * bv) const
1194 {
1195         if (the_locking_inset)
1196                 return the_locking_inset->showInsetDialog(bv);
1197         return false;
1198 }
1199
1200
1201 void InsetText::getLabelList(Buffer const & buffer,
1202                              std::vector<string> & list) const
1203 {
1204         ParagraphList::const_iterator pit = paragraphs.begin();
1205         ParagraphList::const_iterator pend = paragraphs.end();
1206         for (; pit != pend; ++pit) {
1207                 InsetList::const_iterator beg = pit->insetlist.begin();
1208                 InsetList::const_iterator end = pit->insetlist.end();
1209                 for (; beg != end; ++beg)
1210                         beg->inset->getLabelList(buffer, list);
1211         }
1212 }
1213
1214
1215 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1216                         bool selectall)
1217 {
1218         if (the_locking_inset) {
1219                 the_locking_inset->setFont(bv, font, toggleall, selectall);
1220                 return;
1221         }
1222
1223         if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
1224             || cpar()->empty()) {
1225                 text_.setFont(font, toggleall);
1226                 return;
1227         }
1228
1229         if (text_.selection.set())
1230                 text_.recUndo(text_.cursor.par());
1231
1232         if (selectall) {
1233                 text_.cursorTop();
1234                 text_.selection.cursor = text_.cursor;
1235                 text_.cursorBottom();
1236                 text_.setSelection();
1237         }
1238
1239         text_.toggleFree(font, toggleall);
1240
1241         if (selectall)
1242                 text_.clearSelection();
1243
1244         bv->fitCursor();
1245         updateLocal(bv, true);
1246 }
1247
1248
1249 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
1250 {
1251         if (cpos() == cpar()->size())
1252                 return false;
1253         InsetOld * inset = cpar()->getInset(cpos());
1254         if (!isHighlyEditableInset(inset))
1255                 return false;
1256         inset->edit(bv, front);
1257         if (!the_locking_inset)
1258                 return false;
1259         updateLocal(bv, false);
1260         return true;
1261 }
1262
1263
1264 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y)
1265 {
1266         int dummyx = x;
1267         int dummyy = y + dim_.asc;
1268         InsetOld * inset = getLyXText(bv)->checkInsetHit(dummyx, dummyy);
1269         if (!inset)
1270                 return false;
1271         if (!isHighlyEditableInset(inset))
1272                 return false;
1273         if (x < 0)
1274                 x = dim_.wid;
1275         if (y < 0)
1276                 y = dim_.des;
1277         inset_x = cx() - top_x;
1278         inset_y = cy();
1279         inset->edit(bv, x - inset_x, y - inset_y);
1280         if (!the_locking_inset)
1281                 return false;
1282         updateLocal(bv, false);
1283         return true;
1284 }
1285
1286
1287 void InsetText::markNew(bool track_changes)
1288 {
1289         ParagraphList::iterator pit = paragraphs.begin();
1290         ParagraphList::iterator end = paragraphs.end();
1291         for (; pit != end; ++pit) {
1292                 if (track_changes) {
1293                         pit->trackChanges();
1294                 } else {
1295                         // no-op when not tracking
1296                         pit->cleanChanges();
1297                 }
1298         }
1299 }
1300
1301
1302 void InsetText::setText(string const & data, LyXFont const & font)
1303 {
1304         clear(false);
1305         for (unsigned int i = 0; i < data.length(); ++i)
1306                 paragraphs.begin()->insertChar(i, data[i], font);
1307 }
1308
1309
1310 void InsetText::setAutoBreakRows(bool flag)
1311 {
1312         if (flag != autoBreakRows_) {
1313                 autoBreakRows_ = flag;
1314                 if (!flag)
1315                         removeNewlines();
1316         }
1317 }
1318
1319
1320 void InsetText::setDrawFrame(DrawFrame how)
1321 {
1322         drawFrame_ = how;
1323 }
1324
1325
1326 LColor_color InsetText::frameColor() const
1327 {
1328         return LColor::color(frame_color_);
1329 }
1330
1331
1332 void InsetText::setFrameColor(LColor_color col)
1333 {
1334         frame_color_ = col;
1335 }
1336
1337
1338 int InsetText::cx() const
1339 {
1340         int x = text_.cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1341         if (the_locking_inset) {
1342                 LyXFont font = text_.getFont(text_.cursorPar(), text_.cursor.pos());
1343                 if (font.isVisibleRightToLeft())
1344                         x -= the_locking_inset->width();
1345         }
1346         return x;
1347 }
1348
1349
1350 int InsetText::cy() const
1351 {
1352         return text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
1353 }
1354
1355
1356 pos_type InsetText::cpos() const
1357 {
1358         return text_.cursor.pos();
1359 }
1360
1361
1362 ParagraphList::iterator InsetText::cpar() const
1363 {
1364         return text_.cursorPar();
1365 }
1366
1367
1368 bool InsetText::cboundary() const
1369 {
1370         return text_.cursor.boundary();
1371 }
1372
1373
1374 RowList::iterator InsetText::crow() const
1375 {
1376         return cpar()->getRow(cpos());
1377 }
1378
1379
1380 LyXText * InsetText::getLyXText(BufferView const * bv,
1381                                 bool const recursive) const
1382 {
1383         setViewCache(bv);
1384         if (recursive && the_locking_inset)
1385                 return the_locking_inset->getLyXText(bv, true);
1386         return &text_;
1387 }
1388
1389
1390 void InsetText::setViewCache(BufferView const * bv) const
1391 {
1392         if (bv) {
1393                 if (bv != text_.bv_owner) {
1394                         //lyxerr << "setting view cache from "
1395                         //      << text_.bv_owner << " to " << bv << "\n";
1396                         text_.init(const_cast<BufferView *>(bv));
1397                 }
1398                 text_.bv_owner = const_cast<BufferView *>(bv);
1399         }
1400 }
1401
1402
1403 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1404 {
1405         if (recursive) {
1406                 /// then remove all LyXText in text-insets
1407                 for_each(const_cast<ParagraphList&>(paragraphs).begin(),
1408                          const_cast<ParagraphList&>(paragraphs).end(),
1409                          boost::bind(&Paragraph::deleteInsetsLyXText, _1, bv));
1410         }
1411 }
1412
1413
1414 void InsetText::removeNewlines()
1415 {
1416         ParagraphList::iterator it = paragraphs.begin();
1417         ParagraphList::iterator end = paragraphs.end();
1418         for (; it != end; ++it)
1419                 for (int i = 0; i < it->size(); ++i)
1420                         if (it->isNewline(i))
1421                                 it->erase(i);
1422 }
1423
1424
1425 int InsetText::scroll(bool recursive) const
1426 {
1427         int sx = UpdatableInset::scroll(false);
1428
1429         if (recursive && the_locking_inset)
1430                 sx += the_locking_inset->scroll(recursive);
1431
1432         return sx;
1433 }
1434
1435
1436 void InsetText::clearSelection(BufferView *)
1437 {
1438         text_.clearSelection();
1439 }
1440
1441
1442 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
1443 {
1444         Painter & pain = bv->painter();
1445         int w = dim_.wid;
1446         int h = dim_.asc + dim_.des;
1447         int ty = baseline - dim_.asc;
1448
1449         if (ty < 0) {
1450                 h += ty;
1451                 ty = 0;
1452         }
1453         if (ty + h > pain.paperHeight())
1454                 h = pain.paperHeight();
1455         if (top_x + w > pain.paperWidth())
1456                 w = pain.paperWidth();
1457         pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
1458 }
1459
1460
1461 ParagraphList * InsetText::getParagraphs(int i) const
1462 {
1463         return (i == 0) ? const_cast<ParagraphList*>(&paragraphs) : 0;
1464 }
1465
1466
1467 LyXText * InsetText::getText(int i) const
1468 {
1469         return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
1470 }
1471
1472
1473 LyXCursor const & InsetText::cursor(BufferView * bv) const
1474 {
1475         if (the_locking_inset)
1476                 return the_locking_inset->cursor(bv);
1477         return getLyXText(bv)->cursor;
1478 }
1479
1480
1481 bool InsetText::checkInsertChar(LyXFont & font)
1482 {
1483         return owner() ? owner()->checkInsertChar(font) : true;
1484 }
1485
1486
1487 void InsetText::collapseParagraphs(BufferView * bv)
1488 {
1489         while (paragraphs.size() > 1) {
1490                 ParagraphList::iterator const first = paragraphs.begin();
1491                 ParagraphList::iterator second = first;
1492                 advance(second, 1);
1493                 size_t const first_par_size = first->size();
1494
1495                 if (!first->empty() &&
1496                     !second->empty() &&
1497                     !first->isSeparator(first_par_size - 1)) {
1498                         first->insertChar(first_par_size, ' ');
1499                 }
1500
1501 #warning probably broken
1502                 if (text_.selection.set()) {
1503                         if (text_.selection.start.par() == 1) {
1504                                 text_.selection.start.par(1);
1505                                 text_.selection.start.pos(text_.selection.start.pos() + first_par_size);
1506                         }
1507                         if (text_.selection.end.par() == 2) {
1508                                 text_.selection.end.par(1);
1509                                 text_.selection.end.pos(text_.selection.end.pos() + first_par_size);
1510                         }
1511                 }
1512
1513                 mergeParagraph(bv->buffer()->params(), paragraphs, first);
1514         }
1515 }
1516
1517
1518 void InsetText::getDrawFont(LyXFont & font) const
1519 {
1520         if (!owner())
1521                 return;
1522         owner()->getDrawFont(font);
1523 }
1524
1525
1526 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
1527 {
1528 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
1529 // And it probably does. You have to take a look at this John. (Lgb)
1530 #warning John, have a look here. (Lgb)
1531         ParagraphList::iterator pit = plist.begin();
1532         ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
1533         ++pit;
1534         mergeParagraph(buffer->params(), paragraphs, boost::prior(ins));
1535
1536         ParagraphList::iterator pend = plist.end();
1537         for (; pit != pend; ++pit)
1538                 paragraphs.push_back(*pit);
1539 }
1540
1541
1542 void InsetText::addPreview(PreviewLoader & loader) const
1543 {
1544         ParagraphList::const_iterator pit = paragraphs.begin();
1545         ParagraphList::const_iterator pend = paragraphs.end();
1546
1547         for (; pit != pend; ++pit) {
1548                 InsetList::const_iterator it  = pit->insetlist.begin();
1549                 InsetList::const_iterator end = pit->insetlist.end();
1550                 for (; it != end; ++it)
1551                         it->inset->addPreview(loader);
1552         }
1553 }