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