]> git.lyx.org Git - lyx.git/blob - src/BufferView2.C
Dekels tabular/textinset patches
[lyx.git] / src / BufferView2.C
1 // -*- C++ -*-
2 /* This file is part of
3  * ====================================================== 
4  * 
5  *           LyX, The Document Processor
6  *        
7  *           Copyright 1995 Matthias Ettrich
8  *           Copyright 1995-2000 The LyX Team.
9  *
10  * ====================================================== */
11
12 #include <config.h>
13
14 #include <fstream>
15 #include <algorithm>
16
17 #include "BufferView.h"
18 #include "buffer.h"
19 #include "lyxcursor.h"
20 #include "lyxtext.h"
21 #include "insets/inseterror.h"
22 #include "insets/insetinfo.h"
23 #include "insets/insetspecialchar.h"
24 #include "LyXView.h"
25 #include "minibuffer.h"
26 #include "bufferlist.h"
27 #include "support/FileInfo.h"
28 #include "lyxscreen.h"
29 #include "support/filetools.h"
30 #include "lyx_gui_misc.h"
31 #include "LaTeX.h"
32 #include "BufferView_pimpl.h"
33 #include "insets/insetcommand.h" //ChangeRefs
34
35 extern BufferList bufferlist;
36
37 using std::pair;
38 using std::endl;
39 using std::ifstream;
40 using std::vector;
41 using std::find;
42 using std::count;
43
44 // Inserts a file into current document
45 bool BufferView::insertLyXFile(string const & filen)
46         //
47         // Copyright CHT Software Service GmbH
48         // Uwe C. Schroeder
49         //
50         // Insert a Lyxformat - file into current buffer
51         //
52         // Moved from lyx_cb.C (Lgb)
53 {
54         if (filen.empty()) return false;
55
56         string const fname = MakeAbsPath(filen);
57
58         // check if file exist
59         FileInfo const fi(fname);
60
61         if (!fi.readable()) {
62                 WriteAlert(_("Error!"),
63                            _("Specified file is unreadable: "),
64                            MakeDisplayPath(fname, 50));
65                 return false;
66         }
67         
68         beforeChange();
69
70         ifstream ifs(fname.c_str());
71         if (!ifs) {
72                 WriteAlert(_("Error!"),
73                            _("Cannot open specified file: "),
74                            MakeDisplayPath(fname, 50));
75                 return false;
76         }
77         
78         char const c = ifs.peek();
79        
80         LyXLex lex(0, 0);
81         lex.setStream(ifs);
82
83         bool res = true;
84
85         if (c == '#') {
86                 lyxerr.debug() << "Will insert file with header" << endl;
87                 res = buffer()->readFile(lex, text->cursor.par());
88         } else {
89                 lyxerr.debug() << "Will insert file without header" << endl;
90                 res = buffer()->readLyXformat2(lex, text->cursor.par());
91         }
92
93         resize();
94         return res;
95 }
96
97
98 bool BufferView::removeAutoInsets()
99 {
100         LyXParagraph * par = buffer()->paragraph;
101
102         LyXCursor tmpcursor = text->cursor;
103         LyXCursor cursor;
104
105         bool a = false;
106         while (par) {
107                 // this has to be done before the delete
108 #ifndef NEW_INSETS
109                 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
110 #endif
111                         text->SetCursor(this, cursor, par, 0);
112                 if (par->AutoDeleteInsets()){
113                         a = true;
114 #ifndef NEW_INSETS
115                         if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
116 #endif
117                                 text->RedoParagraphs(this, cursor,
118                                                      cursor.par()->Next());
119                                 text->FullRebreak(this);
120 #ifndef NEW_INSETS
121                         }
122 #endif
123                 }
124                 par = par->next;
125         }
126         // avoid forbidden cursor positions caused by error removing
127         if (tmpcursor.pos() > tmpcursor.par()->Last())
128                 tmpcursor.pos(tmpcursor.par()->Last());
129         text->SetCursorIntern(this, tmpcursor.par(), tmpcursor.pos());
130
131         return a;
132 }
133
134
135 void BufferView::insertErrors(TeXErrors & terr)
136 {
137         // Save the cursor position
138         LyXCursor cursor = text->cursor;
139
140 #ifndef NEW_INSETS
141         // This is drastic, but it's the only fix, I could find. (Asger)
142         allFloats(1, 0);
143         allFloats(1, 1);
144 #endif
145
146         for (TeXErrors::Errors::const_iterator cit = terr.begin();
147              cit != terr.end();
148              ++cit) {
149                 string const desctext((*cit).error_desc);
150                 string const errortext((*cit).error_text);
151                 string const msgtxt = desctext + '\n' + errortext;
152                 int const errorrow = (*cit).error_in_line;
153
154                 // Insert error string for row number
155                 int tmpid = -1; 
156                 int tmppos = -1;
157
158                 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
159                         buffer()->texrow.increasePos(tmpid, tmppos);
160                 }
161                 
162                 LyXParagraph * texrowpar = 0;
163
164                 if (tmpid == -1) {
165                         texrowpar = text->FirstParagraph();
166                         tmppos = 0;
167                 } else {
168                         texrowpar = text->GetParFromID(tmpid);
169                 }
170
171                 if (texrowpar == 0)
172                         continue;
173
174                 InsetError * new_inset = new InsetError(msgtxt);
175                 text->SetCursorIntern(this, texrowpar, tmppos);
176                 text->InsertInset(this, new_inset);
177                 text->FullRebreak(this);
178         }
179         // Restore the cursor position
180         text->SetCursorIntern(this, cursor.par(), cursor.pos());
181 }
182
183
184 void BufferView::setCursorFromRow(int row)
185 {
186         int tmpid = -1; 
187         int tmppos = -1;
188
189         buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
190
191         LyXParagraph * texrowpar;
192
193         if (tmpid == -1) {
194                 texrowpar = text->FirstParagraph();
195                 tmppos = 0;
196         } else {
197                 texrowpar = text->GetParFromID(tmpid);
198         }
199         text->SetCursor(this, texrowpar, tmppos);
200 }
201
202
203 bool BufferView::insertInset(Inset * inset, string const & lout,
204                              bool /*no_table*/)
205 {
206         // if we are in a locking inset we should try to insert the
207         // inset there otherwise this is a illegal function now
208         if (theLockingInset()) {
209                 if (theLockingInset()->InsertInsetAllowed(inset))
210                     return theLockingInset()->InsertInset(this, inset);
211                 return false;
212         }
213
214         // not quite sure if we want this...
215         text->SetCursorParUndo(buffer());
216         text->FreezeUndo();
217         
218         beforeChange();
219         if (!lout.empty()) {
220                 update(BufferView::SELECT|BufferView::FITCUR);
221                 text->BreakParagraph(this);
222                 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
223                 
224                 if (text->cursor.par()->Last()) {
225                         text->CursorLeft(this);
226                         
227                         text->BreakParagraph(this);
228                         update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
229                 }
230
231                 pair<bool, LyXTextClass::size_type> lres =
232                         textclasslist.NumberOfLayout(buffer()->params
233                                                      .textclass, lout);
234                 LyXTextClass::size_type lay;
235                 if (lres.first != false) {
236                         // layout found
237                         lay = lres.second;
238                 } else {
239                         // layout not fount using default "Standard" (0)
240                         lay = 0;
241                 }
242                  
243                 text->SetLayout(this, lay);
244                 
245                 text->SetParagraph(this, 0, 0,
246                                    0, 0,
247                                    VSpace(VSpace::NONE), VSpace(VSpace::NONE),
248                                    LYX_ALIGN_LAYOUT, 
249                                    string(),
250                                    0);
251                 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
252                 
253                 text->current_font.setLatex(LyXFont::OFF);
254         }
255         
256         text->InsertInset(this, inset);
257         update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
258
259         text->UnFreezeUndo();
260         return true;
261 }
262
263
264 // Open and lock an updatable inset
265 bool BufferView::open_new_inset(UpdatableInset * new_inset, bool behind)
266 {
267         beforeChange();
268         text->FinishUndo();
269         if (!insertInset(new_inset)) {
270                 delete new_inset;
271                 return false;
272         }
273         if (behind) {
274                 LyXFont & font = getLyXText()->real_current_font;
275                 new_inset->Edit(this, new_inset->width(this, font), 0, 0);
276         } else
277                 new_inset->Edit(this, 0, 0, 0);
278         return true;
279 }
280
281 /* This is also a buffer property (ale) */
282 // Not so sure about that. a goto Label function can not be buffer local, just
283 // think how this will work in a multiwindo/buffer environment, all the
284 // cursors in all the views showing this buffer will move. (Lgb)
285 // OK, then no cursor action should be allowed in buffer. (ale)
286 bool BufferView::gotoLabel(string const & label)
287
288 {
289         for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
290              it != buffer()->inset_iterator_end(); ++it) {
291                 vector<string> labels = (*it)->getLabelList();
292                 if (find(labels.begin(),labels.end(),label)
293                      != labels.end()) {
294                         beforeChange();
295                         text->SetCursor(this, it.getPar(), it.getPos());
296                         text->sel_cursor = text->cursor;
297                         update(BufferView::SELECT|BufferView::FITCUR);
298                         return true;
299                 }
300         }
301         return false;
302 }
303
304
305 #ifndef NEW_INSETS
306 void BufferView::allFloats(char flag, char figmar)
307 {
308         if (!available()) return;
309
310         LyXCursor cursor = text->cursor;
311
312         if (!flag
313             && cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE
314             && ((figmar 
315                  && cursor.par()->footnotekind != LyXParagraph::FOOTNOTE 
316                  && cursor.par()->footnotekind != LyXParagraph::MARGIN
317                     )
318                 || (!figmar
319                     && cursor.par()->footnotekind != LyXParagraph::FIG 
320                     && cursor.par()->footnotekind != LyXParagraph::TAB
321                     && cursor.par()->footnotekind != LyXParagraph::WIDE_FIG 
322                     && cursor.par()->footnotekind != LyXParagraph::WIDE_TAB
323                     && cursor.par()->footnotekind != LyXParagraph::ALGORITHM)))
324                 toggleFloat();
325         else
326                 beforeChange();
327
328         LyXCursor tmpcursor = cursor;
329         cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
330         cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
331
332         LyXParagraph *par = buffer()->paragraph;
333         while (par) {
334                 if (flag) {
335                         if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
336                             && ((figmar 
337                                  && par->footnotekind != LyXParagraph::FOOTNOTE 
338                                  && par->footnotekind !=  LyXParagraph::MARGIN)
339                                 || (!figmar
340                                     && par->footnotekind != LyXParagraph::FIG 
341                                     && par->footnotekind != LyXParagraph::TAB
342                                     && par->footnotekind != LyXParagraph::WIDE_FIG 
343                                     && par->footnotekind != LyXParagraph::WIDE_TAB
344                                     && par->footnotekind != LyXParagraph::ALGORITHM
345                                         )
346                                     )
347                                 ) {
348                                 if (par->previous
349                                     && par->previous->footnoteflag != 
350                                     LyXParagraph::CLOSED_FOOTNOTE){ /* should be */ 
351                                         text->SetCursorIntern(this, 
352                                                               par->previous,
353                                                               0);
354                                         text->OpenFootnote(this);
355                                 }
356                         }
357                 } else {
358                         if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
359                             && (
360                                     (figmar 
361                                      &&
362                                      par->footnotekind != LyXParagraph::FOOTNOTE 
363                                      &&
364                                      par->footnotekind !=  LyXParagraph::MARGIN
365                                             )
366                                     ||
367                                     (!figmar
368                                      &&
369                                      par->footnotekind != LyXParagraph::FIG 
370                                      &&
371                                      par->footnotekind != LyXParagraph::TAB
372                                      &&
373                                      par->footnotekind != LyXParagraph::WIDE_FIG 
374                                      &&
375                                      par->footnotekind != LyXParagraph::WIDE_TAB
376                                      &&
377                                      par->footnotekind != LyXParagraph::ALGORITHM
378                                             )
379                                     )
380                                 ) {
381                                 text->SetCursorIntern(this, par, 0);
382                                 text->CloseFootnote(this);
383                         }
384                 }
385                 par = par->next;
386         }
387
388         text->SetCursorIntern(this, cursor.par(), cursor.pos());
389         redraw();
390         fitCursor(text);
391 }
392 #endif
393
394
395 void BufferView::insertNote()
396 {
397         InsetInfo * new_inset = new InsetInfo();
398         insertInset(new_inset);
399         new_inset->Edit(this, 0, 0, 0);
400 }
401
402
403 #ifndef NEW_INSETS
404 void BufferView::openStuff()
405 {
406         if (available()) {
407                 owner()->getMiniBuffer()->Set(_("Open/Close..."));
408                 hideCursor();
409                 beforeChange();
410                 update(BufferView::SELECT|BufferView::FITCUR);
411                 text->OpenStuff(this);
412                 update(BufferView::SELECT|BufferView::FITCUR);
413                 setState();
414         }
415 }
416
417
418 void BufferView::toggleFloat()
419 {
420         if (available()) {
421                 owner()->getMiniBuffer()->Set(_("Open/Close..."));
422                 hideCursor();
423                 beforeChange();
424                 update(BufferView::SELECT|BufferView::FITCUR);
425                 text->ToggleFootnote(this);
426                 update(BufferView::SELECT|BufferView::FITCUR);
427                 setState();
428         }
429 }
430 #endif
431
432 void BufferView::menuUndo()
433 {
434         if (available()) {
435                 owner()->getMiniBuffer()->Set(_("Undo"));
436                 hideCursor();
437                 beforeChange();
438                 update(BufferView::SELECT|BufferView::FITCUR);
439                 if (!text->TextUndo(this))
440                         owner()->getMiniBuffer()->Set(_("No further undo information"));
441                 else
442                         update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
443                 setState();
444         }
445 }
446
447
448 void BufferView::menuRedo()
449 {
450         if (theLockingInset()) {
451                 owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
452                 return;
453         }    
454    
455         if (available()) {
456                 owner()->getMiniBuffer()->Set(_("Redo"));
457                 hideCursor();
458                 beforeChange();
459                 update(BufferView::SELECT|BufferView::FITCUR);
460                 if (!text->TextRedo(this))
461                         owner()->getMiniBuffer()->Set(_("No further redo information"));
462                 else
463                         update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
464                 setState();
465         }
466 }
467
468
469 void BufferView::hyphenationPoint()
470 {
471         if (available()) {
472                 hideCursor();
473                 update(BufferView::SELECT|BufferView::FITCUR);
474                 InsetSpecialChar * new_inset = 
475                         new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
476                 insertInset(new_inset);
477         }
478 }
479
480
481 void BufferView::ldots()
482 {
483         if (available())  {
484                 hideCursor();
485                 update(BufferView::SELECT|BufferView::FITCUR);
486                 InsetSpecialChar * new_inset = 
487                         new InsetSpecialChar(InsetSpecialChar::LDOTS);
488                 insertInset(new_inset);
489         }
490 }
491
492
493 void BufferView::endOfSentenceDot()
494 {
495         if (available()) {
496                 hideCursor();
497                 update(BufferView::SELECT|BufferView::FITCUR);
498                 InsetSpecialChar * new_inset = 
499                         new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
500                 insertInset(new_inset);
501         }
502 }
503
504
505 void BufferView::menuSeparator()
506 {
507         if (available()) {
508                 hideCursor();
509                 update(BufferView::SELECT|BufferView::FITCUR);
510                 InsetSpecialChar * new_inset = 
511                         new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
512                 insertInset(new_inset);
513         }
514 }
515
516
517 void BufferView::newline()
518 {
519         if (available()) {
520                 hideCursor();
521                 update(BufferView::SELECT|BufferView::FITCUR);
522                 text->InsertChar(this, LyXParagraph::META_NEWLINE);
523                 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
524         }
525 }
526
527
528 void BufferView::protectedBlank()
529 {
530         if (available()) {
531                 hideCursor();
532                 update(BufferView::SELECT|BufferView::FITCUR);
533                 InsetSpecialChar * new_inset =
534                         new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
535                 insertInset(new_inset);
536         }
537 }
538
539
540 void BufferView::hfill()
541 {
542         if (available()) {
543                 hideCursor();
544                 update(BufferView::SELECT|BufferView::FITCUR);
545                 text->InsertChar(this, LyXParagraph::META_HFILL);
546                 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
547         }
548 }
549
550 void BufferView::copyEnvironment()
551 {
552         if (available()) {
553                 text->copyEnvironmentType();
554                 // clear the selection, even if mark_set
555                 toggleSelection();
556                 text->ClearSelection();
557                 update(BufferView::SELECT|BufferView::FITCUR);
558                 owner()->getMiniBuffer()->Set(_("Paragraph environment type copied"));
559         }
560 }
561
562
563 void BufferView::pasteEnvironment()
564 {
565         if (available()) {
566                 text->pasteEnvironmentType(this);
567                 owner()->getMiniBuffer()->Set(_("Paragraph environment type set"));
568                 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
569         }
570 }
571
572
573 void BufferView::copy()
574 {
575         if (available()) {
576                 text->CopySelection(this);
577                 // clear the selection, even if mark_set
578                 toggleSelection();
579                 text->ClearSelection();
580                 update(BufferView::SELECT|BufferView::FITCUR);
581                 owner()->getMiniBuffer()->Set(_("Copy"));
582         }
583 }
584
585 void BufferView::cut()
586 {
587         if (available()) {
588                 hideCursor();
589                 update(BufferView::SELECT|BufferView::FITCUR);
590                 text->CutSelection(this);
591                 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
592                 owner()->getMiniBuffer()->Set(_("Cut"));
593         }
594 }
595
596
597 void BufferView::paste()
598 {
599         if (!available()) return;
600         
601         owner()->getMiniBuffer()->Set(_("Paste"));
602         hideCursor();
603         // clear the selection
604         toggleSelection();
605         text->ClearSelection();
606         update(BufferView::SELECT|BufferView::FITCUR);
607         
608         // paste
609         text->PasteSelection(this);
610         update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
611         
612         // clear the selection 
613         toggleSelection();
614         text->ClearSelection();
615         update(BufferView::SELECT|BufferView::FITCUR);
616 }
617
618
619 void BufferView::gotoNote()
620 {
621         if (!available()) return;
622    
623         hideCursor();
624         beforeChange();
625         update(BufferView::SELECT|BufferView::FITCUR);
626         LyXCursor tmp;
627    
628         if (!text->GotoNextNote(this)) {
629                 if (text->cursor.pos() 
630                     || text->cursor.par() != text->FirstParagraph()) {
631                                 tmp = text->cursor;
632                                 text->cursor.par(text->FirstParagraph());
633                                 text->cursor.pos(0);
634                                 if (!text->GotoNextNote(this)) {
635                                         text->cursor = tmp;
636                                         owner()->getMiniBuffer()->Set(_("No more notes"));
637                                         LyXBell();
638                                 }
639                         } else {
640                                 owner()->getMiniBuffer()->Set(_("No more notes"));
641                                 LyXBell();
642                         }
643         }
644         update(BufferView::SELECT|BufferView::FITCUR);
645         text->sel_cursor = text->cursor;
646 }
647
648
649 void BufferView::insertCorrectQuote()
650 {
651         char c;
652
653         if (text->cursor.pos())
654                 c = text->cursor.par()->GetChar(text->cursor.pos() - 1);
655         else 
656                 c = ' ';
657
658         insertInset(new InsetQuotes(c, buffer()->params));
659 }
660
661
662 /* these functions are for the spellchecker */ 
663 string const BufferView::nextWord(float & value)
664 {
665         if (!available()) {
666                 value = 1;
667                 return string();
668         }
669
670         return text->SelectNextWord(this, value);
671 }
672
673   
674 void BufferView::selectLastWord()
675 {
676         if (!available()) return;
677    
678         hideCursor();
679         beforeChange();
680         text->SelectSelectedWord(this);
681         toggleSelection(false);
682         update(BufferView::SELECT|BufferView::FITCUR);
683 }
684
685
686 void BufferView::endOfSpellCheck()
687 {
688         if (!available()) return;
689    
690         hideCursor();
691         beforeChange();
692         text->SelectSelectedWord(this);
693         text->ClearSelection();
694         update(BufferView::SELECT|BufferView::FITCUR);
695 }
696
697
698 void BufferView::replaceWord(string const & replacestring)
699 {
700         if (!available()) return;
701
702         hideCursor();
703         update(BufferView::SELECT|BufferView::FITCUR);
704    
705         /* clear the selection (if there is any) */ 
706         toggleSelection(false);
707         update(BufferView::SELECT|BufferView::FITCUR);
708    
709         /* clear the selection (if there is any) */ 
710         toggleSelection(false);
711         text->ReplaceSelectionWithString(this, replacestring);
712    
713         text->SetSelectionOverString(this, replacestring);
714
715         // Go back so that replacement string is also spellchecked
716         for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
717                 text->CursorLeft(this);
718         }
719         update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
720 }
721 // End of spellchecker stuff
722
723
724 bool BufferView::lockInset(UpdatableInset * inset)
725 {
726         if (!theLockingInset() && inset) {
727                 theLockingInset(inset);
728                 return true;
729         } else if (inset) {
730             return theLockingInset()->LockInsetInInset(this, inset);
731         }
732         return false;
733 }
734
735
736 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
737 {
738         if (theLockingInset() && available()) {
739                 LyXCursor cursor = text->cursor;
740                 if ((cursor.pos() - 1 >= 0) &&
741                     (cursor.par()->GetChar(cursor.pos() - 1) ==
742                      LyXParagraph::META_INSET) &&
743                     (cursor.par()->GetInset(cursor.pos() - 1) ==
744                      theLockingInset()->GetLockingInset()))
745                         text->SetCursor(this, cursor,
746                                         cursor.par(), cursor.pos() - 1);
747                 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
748                 LyXText * txt = getLyXText();
749                 if (theLockingInset()->GetLockingInset()->LyxCode() ==
750                     Inset::TEXT_CODE &&
751                     (txt->real_current_font.language() !=
752                      buffer()->params.language
753                      || txt->real_current_font.isVisibleRightToLeft()
754                      != buffer()->params.language->RightToLeft()))
755                         shape = (txt->real_current_font.isVisibleRightToLeft())
756                                 ? LyXScreen::REVERSED_L_SHAPE
757                                 : LyXScreen::L_SHAPE;
758                 y += cursor.y() + theLockingInset()->InsetInInsetY();
759                 pimpl_->screen_->ShowManualCursor(text, x, y, asc, desc,
760                                                   shape);
761         }
762 }
763
764
765 void BufferView::hideLockedInsetCursor()
766 {
767         if (theLockingInset() && available()) {
768                 pimpl_->screen_->HideCursor();
769         }
770 }
771
772
773 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
774 {
775         if (theLockingInset() && available()) {
776                 y += text->cursor.y() + theLockingInset()->InsetInInsetY();
777                 if (pimpl_->screen_->FitManualCursor(text, this, x, y, asc, desc))
778                         updateScrollbar();
779         }
780 }
781
782
783 int BufferView::unlockInset(UpdatableInset * inset)
784 {
785         if (inset && theLockingInset() == inset) {
786                 inset->InsetUnlock(this);
787                 theLockingInset(0);
788                 text->FinishUndo();
789                 return 0;
790         } else if (inset && theLockingInset() &&
791                    theLockingInset()->UnlockInsetInInset(this, inset)) {
792                 text->FinishUndo();
793                 return 0;
794         }
795         return bufferlist.unlockInset(inset);
796 }
797
798
799 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
800 {
801         if (!theLockingInset())
802                 return; // shouldn't happen
803         if (kind == Undo::EDIT) // in this case insets would not be stored!
804                 kind = Undo::FINISH;
805         text->SetUndo(buffer(), kind,
806 #ifndef NEW_INSETS
807                       text->cursor.par()->
808                       ParFromPos(text->cursor.pos())->previous, 
809                       text->cursor.par()->
810                       ParFromPos(text->cursor.pos())->next
811 #else
812                       text->cursor.par()->previous, 
813                       text->cursor.par()->next
814 #endif
815                 );
816 }
817
818
819 void BufferView::updateInset(Inset * inset, bool mark_dirty)
820 {
821         if (!inset)
822                 return;
823
824         // first check for locking insets
825         if (theLockingInset()) {
826                 if (theLockingInset() == inset) {
827                         if (text->UpdateInset(this, inset)) {
828                                 update();
829                                 if (mark_dirty) {
830                                         if (buffer()->isLyxClean())
831                                                 owner()->getMiniBuffer()->
832                                                         setTimer(4);
833                                         buffer()->markDirty();
834                                 }
835                                 updateScrollbar();
836                                 return;
837                         }
838                 } else if (theLockingInset()->UpdateInsetInInset(this,inset)) {
839                         if (text->UpdateInset(this, theLockingInset())) {
840                                 update();
841                                 if (mark_dirty){
842                                         if (buffer()->isLyxClean())
843                                                 owner()->getMiniBuffer()->
844                                                         setTimer(4);
845                                         buffer()->markDirty();
846                                 }
847                                 updateScrollbar();
848                                 return;
849                         }
850                 }
851         }
852   
853         // then check the current buffer
854         if (available()) {
855                 hideCursor();
856                 update(BufferView::UPDATE);
857                 if (text->UpdateInset(this, inset)) {
858                         if (mark_dirty)
859                                 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
860                         else 
861                                 update(SELECT);
862                         return;
863                 }
864         }
865 }
866
867
868 bool BufferView::ChangeRefs(string const & from, string const & to)
869 {
870         bool flag = false;
871         LyXParagraph * par = buffer()->paragraph;
872         LyXCursor cursor = text->cursor;
873         LyXCursor tmpcursor = cursor;
874 #ifndef NEW_INSETS
875         cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
876         cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
877 #else
878         cursor.par(tmpcursor.par());
879         cursor.pos(tmpcursor.pos());
880 #endif
881
882         while (par) {
883                 bool flag2 = false;
884                 for (LyXParagraph::inset_iterator it = par->inset_iterator_begin();
885                      it != par->inset_iterator_end(); ++it) {
886                         if ((*it)->LyxCode() == Inset::REF_CODE) {
887                                 InsetCommand * inset = static_cast<InsetCommand *>(*it);
888                                 if (inset->getContents() == from) {
889                                         inset->setContents(to);
890                                         flag2 = true;
891                                 }
892                         }
893                 }
894                 if (flag2) {
895                         flag = true;
896 #ifndef NEW_INSETS
897                         if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
898 #endif
899                                 // this is possible now, since SetCursor takes
900                                 // care about footnotes
901                                 text->SetCursorIntern(this, par, 0);
902                                 text->RedoParagraphs(this, text->cursor,
903                                                      text->cursor.par()->Next());
904                                 text->FullRebreak(this);
905 #ifndef NEW_INSETS
906                         }
907 #endif
908                 }
909                 par = par->next;
910         }
911         text->SetCursorIntern(this, cursor.par(), cursor.pos());
912         return flag;
913 }
914
915
916 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
917 {
918         // Check if the label 'from' appears more than once
919         vector<string> labels = buffer()->getLabelList();
920         if (count(labels.begin(), labels.end(), from) > 1)
921                 return false;
922
923         return ChangeRefs(from, to);
924 }
925
926
927 UpdatableInset * BufferView::theLockingInset() const
928 {
929         // If NULL is not allowed we should put an Assert here. (Lgb)
930         if (text)
931                 return text->the_locking_inset;
932         return 0;
933 }
934
935
936 void BufferView::theLockingInset(UpdatableInset * inset)
937 {
938     text->the_locking_inset = inset;
939 }
940
941
942 LyXText * BufferView::getLyXText() const
943 {
944         if (theLockingInset()) {
945                 LyXText * txt = theLockingInset()->getLyXText(this);
946                 if (txt)
947                         return txt;
948         }
949         return text;
950 }
951
952
953 LyXText * BufferView::getParentText(Inset * inset) const
954 {
955         if (inset->owner()) {
956                 LyXText * txt = inset->getLyXText(this);
957                 inset = inset->owner();
958                 while (inset && inset->getLyXText(this) == txt)
959                         inset = inset->owner();
960                 if (inset)
961                         return inset->getLyXText(this);
962         }
963         return text;
964 }
965
966 Language const * BufferView::getParentLanguage(Inset * inset) const
967 {
968         LyXText * text = getParentText(inset);
969         return text->cursor.par()->GetFontSettings(buffer()->params,
970                                                    text->cursor.pos()).language();
971 }