]> git.lyx.org Git - lyx.git/blob - src/BufferView2.C
FILMagain changes (will need some work)
[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         LyXLex lex(0, 0);
77         lex.setStream(ifs);
78         char c; ifs.get(c);
79         ifs.putback(c);
80
81         bool res = true;
82
83         if (c == '#') {
84                 lyxerr.debug() << "Will insert file with header" << endl;
85                 res = buffer()->readFile(lex, text->cursor.par());
86         } else {
87                 lyxerr.debug() << "Will insert file without header" << endl;
88                 res = buffer()->readLyXformat2(lex, text->cursor.par());
89         }
90
91         resize();
92         return res;
93 }
94
95 bool BufferView::removeAutoInsets()
96 {
97         LyXParagraph * par = buffer()->paragraph;
98
99         LyXCursor tmpcursor = text->cursor;
100         LyXCursor cursor;
101
102         bool a = false;
103         while (par) {
104                 // this has to be done before the delete
105                 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
106                         text->SetCursor(this, cursor, par, 0);
107                 if (par->AutoDeleteInsets()){
108                         a = true;
109                         if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
110                                 text->RedoParagraphs(this, cursor,
111                                                      cursor.par()->Next());
112                                 text->FullRebreak(this);
113                         }
114                 }
115                 par = par->next;
116         }
117         // avoid forbidden cursor positions caused by error removing
118         if (tmpcursor.pos() > tmpcursor.par()->Last())
119                 tmpcursor.pos(tmpcursor.par()->Last());
120         text->SetCursorIntern(this, tmpcursor.par(), tmpcursor.pos());
121
122         return a;
123 }
124
125
126 void BufferView::insertErrors(TeXErrors & terr)
127 {
128         // Save the cursor position
129         LyXCursor cursor = text->cursor;
130
131         // This is drastic, but it's the only fix, I could find. (Asger)
132         allFloats(1, 0);
133         allFloats(1, 1);
134
135         for (TeXErrors::Errors::const_iterator cit = terr.begin();
136              cit != terr.end();
137              ++cit) {
138                 string desctext((*cit).error_desc);
139                 string errortext((*cit).error_text);
140                 string msgtxt = desctext + '\n' + errortext;
141                 int errorrow = (*cit).error_in_line;
142
143                 // Insert error string for row number
144                 int tmpid = -1; 
145                 int tmppos = -1;
146
147                 buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos);
148
149                 LyXParagraph * texrowpar = 0;
150
151                 if (tmpid == -1) {
152                         texrowpar = text->FirstParagraph();
153                         tmppos = 0;
154                 } else {
155                         texrowpar = text->GetParFromID(tmpid);
156                 }
157
158                 if (texrowpar == 0)
159                         continue;
160
161                 InsetError * new_inset = new InsetError(msgtxt);
162                 text->SetCursorIntern(this, texrowpar, tmppos);
163                 text->InsertInset(this, new_inset);
164                 text->FullRebreak(this);
165         }
166         // Restore the cursor position
167         text->SetCursorIntern(this, cursor.par(), cursor.pos());
168 }
169
170
171 void BufferView::setCursorFromRow(int row)
172 {
173         int tmpid = -1; 
174         int tmppos = -1;
175
176         buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
177
178         LyXParagraph * texrowpar;
179
180         if (tmpid == -1) {
181                 texrowpar = text->FirstParagraph();
182                 tmppos = 0;
183         } else {
184                 texrowpar = text->GetParFromID(tmpid);
185         }
186         text->SetCursor(this, texrowpar, tmppos);
187 }
188
189 bool BufferView::insertInset(Inset * inset, string const & lout,
190                          bool no_table)
191 {
192         // if we are in a locking inset we should try to insert the
193         // inset there otherwise this is a illegal function now
194         if (the_locking_inset) {
195                 if (the_locking_inset->InsertInsetAllowed(inset) &&
196                     the_locking_inset->InsertInset(this, inset))
197                         return true;
198                 return false;
199         }
200
201 #ifndef NEW_TABULAR
202         // check for table/list in tables
203         if (no_table && text->cursor.par()->table){
204                 WriteAlert(_("Impossible Operation!"),
205                            _("Cannot insert table/list in table."),
206                            _("Sorry."));
207                 return false;
208         }
209 #endif
210
211         // not quite sure if we want this...
212         text->SetCursorParUndo(buffer());
213         text->FreezeUndo();
214         
215         beforeChange();
216         if (!lout.empty()) {
217                 update(BufferView::SELECT|BufferView::FITCUR);
218                 text->BreakParagraph(this);
219                 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
220                 
221                 if (text->cursor.par()->Last()) {
222                         text->CursorLeft(this);
223                         
224                         text->BreakParagraph(this);
225                         update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
226                 }
227
228                 pair<bool, LyXTextClass::size_type> lres =
229                         textclasslist.NumberOfLayout(buffer()->params
230                                                      .textclass, lout);
231                 LyXTextClass::size_type lay;
232                 if (lres.first != false) {
233                         // layout found
234                         lay = lres.second;
235                 } else {
236                         // layout not fount using default "Standard" (0)
237                         lay = 0;
238                 }
239                  
240                 text->SetLayout(this, lay);
241                 
242                 text->SetParagraph(this, 0, 0,
243                                    0, 0,
244                                    VSpace(VSpace::NONE), VSpace(VSpace::NONE),
245                                    LYX_ALIGN_LAYOUT, 
246                                    string(),
247                                    0);
248                 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
249                 
250                 text->current_font.setLatex(LyXFont::OFF);
251         }
252         
253         text->InsertInset(this, inset);
254 #if 1
255         // if we enter a text-inset the cursor should be to the left side
256         // of it! This couldn't happen before as Undo was not handled inside
257         // inset now after the Undo LyX tries to call inset->Edit(...) again
258         // and cannot do this as the cursor is behind the inset and GetInset
259         // does not return the inset!
260         if (inset->IsTextInset()) {
261                 if (text->cursor.par()->isRightToLeftPar(buffer()->params))
262                         text->CursorRight(this);
263                 else
264                         text->CursorLeft(this);
265         }
266 #endif
267         update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
268
269         text->UnFreezeUndo();
270         return true;
271 }
272
273
274 // Open and lock an updatable inset
275 void BufferView::open_new_inset(UpdatableInset * new_inset)
276 {
277         beforeChange();
278         text->FinishUndo();
279         insertInset(new_inset);
280         text->CursorLeft(this);
281         update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
282         new_inset->Edit(this, 0, 0, 0);
283 }
284
285 /* This is also a buffer property (ale) */
286 // Not so sure about that. a goto Label function can not be buffer local, just
287 // think how this will work in a multiwindo/buffer environment, all the
288 // cursors in all the views showing this buffer will move. (Lgb)
289 // OK, then no cursor action should be allowed in buffer. (ale)
290 bool BufferView::gotoLabel(string const & label)
291
292 {
293         for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
294              it != buffer()->inset_iterator_end(); ++it) {
295                 vector<string> labels = (*it)->getLabelList();
296                 if ( find(labels.begin(),labels.end(),label)
297                      != labels.end()) {
298                         beforeChange();
299                         text->SetCursor(this, it.getPar(), it.getPos());
300                         text->sel_cursor = text->cursor;
301                         update(BufferView::SELECT|BufferView::FITCUR);
302                         return true;
303                 }
304         }
305         return false;
306 }
307
308 void BufferView::allFloats(char flag, char figmar)
309 {
310         if (!available()) return;
311
312         LyXCursor cursor = text->cursor;
313
314         if (!flag && cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE
315             && ((figmar 
316                  && cursor.par()->footnotekind != LyXParagraph::FOOTNOTE 
317                  && cursor.par()->footnotekind != LyXParagraph::MARGIN)
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();
391         //updateScrollbar();
392 }
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 void BufferView::openStuff()
404 {
405         if (available()) {
406                 owner()->getMiniBuffer()->Set(_("Open/Close..."));
407                 hideCursor();
408                 beforeChange();
409                 update(BufferView::SELECT|BufferView::FITCUR);
410                 text->OpenStuff(this);
411                 update(BufferView::SELECT|BufferView::FITCUR);
412                 setState();
413         }
414 }
415
416
417 void BufferView::toggleFloat()
418 {
419         if (available()) {
420                 owner()->getMiniBuffer()->Set(_("Open/Close..."));
421                 hideCursor();
422                 beforeChange();
423                 update(BufferView::SELECT|BufferView::FITCUR);
424                 text->ToggleFootnote(this);
425                 update(BufferView::SELECT|BufferView::FITCUR);
426                 setState();
427         }
428 }
429
430 void BufferView::menuUndo()
431 {
432         if (available()) {
433                 owner()->getMiniBuffer()->Set(_("Undo"));
434                 hideCursor();
435                 beforeChange();
436                 update(BufferView::SELECT|BufferView::FITCUR);
437                 if (!text->TextUndo(this))
438                         owner()->getMiniBuffer()->Set(_("No further undo information"));
439                 else
440                         update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
441                 setState();
442         }
443 }
444
445
446 void BufferView::menuRedo()
447 {
448         if (the_locking_inset) {
449                 owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
450                 return;
451         }    
452    
453         if (available()) {
454                 owner()->getMiniBuffer()->Set(_("Redo"));
455                 hideCursor();
456                 beforeChange();
457                 update(BufferView::SELECT|BufferView::FITCUR);
458                 if (!text->TextRedo(this))
459                         owner()->getMiniBuffer()->Set(_("No further redo information"));
460                 else
461                         update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
462                 setState();
463         }
464 }
465
466
467 void BufferView::hyphenationPoint()
468 {
469         if (available()) {
470                 hideCursor();
471                 update(BufferView::SELECT|BufferView::FITCUR);
472                 InsetSpecialChar * new_inset = 
473                         new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
474                 insertInset(new_inset);
475         }
476 }
477
478
479 void BufferView::ldots()
480 {
481         if (available())  {
482                 hideCursor();
483                 update(BufferView::SELECT|BufferView::FITCUR);
484                 InsetSpecialChar * new_inset = 
485                         new InsetSpecialChar(InsetSpecialChar::LDOTS);
486                 insertInset(new_inset);
487         }
488 }
489
490
491 void BufferView::endOfSentenceDot()
492 {
493         if (available()) {
494                 hideCursor();
495                 update(BufferView::SELECT|BufferView::FITCUR);
496                 InsetSpecialChar * new_inset = 
497                         new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
498                 insertInset(new_inset);
499         }
500 }
501
502
503 void BufferView::menuSeparator()
504 {
505         if (available()) {
506                 hideCursor();
507                 update(BufferView::SELECT|BufferView::FITCUR);
508                 InsetSpecialChar * new_inset = 
509                         new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
510                 insertInset(new_inset);
511         }
512 }
513
514
515 void BufferView::newline()
516 {
517         if (available()) {
518                 hideCursor();
519                 update(BufferView::SELECT|BufferView::FITCUR);
520                 text->InsertChar(this, LyXParagraph::META_NEWLINE);
521                 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
522         }
523 }
524
525
526 void BufferView::protectedBlank()
527 {
528         if (available()) {
529                 hideCursor();
530                 update(BufferView::SELECT|BufferView::FITCUR);
531                 InsetSpecialChar * new_inset =
532                         new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
533                 insertInset(new_inset);
534         }
535 }
536
537
538 void BufferView::hfill()
539 {
540         if (available()) {
541                 hideCursor();
542                 update(BufferView::SELECT|BufferView::FITCUR);
543                 text->InsertChar(this, LyXParagraph::META_HFILL);
544                 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
545         }
546 }
547
548 void BufferView::copyEnvironment()
549 {
550         if (available()) {
551                 text->copyEnvironmentType();
552                 // clear the selection, even if mark_set
553                 toggleSelection();
554                 text->ClearSelection();
555                 update(BufferView::SELECT|BufferView::FITCUR);
556                 owner()->getMiniBuffer()->Set(_("Paragraph environment type copied"));
557         }
558 }
559
560
561 void BufferView::pasteEnvironment()
562 {
563         if (available()) {
564                 text->pasteEnvironmentType(this);
565                 owner()->getMiniBuffer()->Set(_("Paragraph environment type set"));
566                 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
567         }
568 }
569
570
571 void BufferView::copy()
572 {
573         if (available()) {
574                 text->CopySelection(buffer());
575                 // clear the selection, even if mark_set
576                 toggleSelection();
577                 text->ClearSelection();
578                 update(BufferView::SELECT|BufferView::FITCUR);
579                 owner()->getMiniBuffer()->Set(_("Copy"));
580         }
581 }
582
583 void BufferView::cut()
584 {
585         if (available()) {
586                 hideCursor();
587                 update(BufferView::SELECT|BufferView::FITCUR);
588                 text->CutSelection(this);
589                 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
590                 owner()->getMiniBuffer()->Set(_("Cut"));
591         }
592 }
593
594
595 void BufferView::paste()
596 {
597         if (!available()) return;
598         
599         owner()->getMiniBuffer()->Set(_("Paste"));
600         hideCursor();
601         // clear the selection
602         toggleSelection();
603         text->ClearSelection();
604         update(BufferView::SELECT|BufferView::FITCUR);
605         
606         // paste
607         text->PasteSelection(this);
608         update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
609         
610         // clear the selection 
611         toggleSelection();
612         text->ClearSelection();
613         update(BufferView::SELECT|BufferView::FITCUR);
614 }
615
616
617 void BufferView::gotoNote()
618 {
619         if (!available()) return;
620    
621         hideCursor();
622         beforeChange();
623         update(BufferView::SELECT|BufferView::FITCUR);
624         LyXCursor tmp;
625    
626         if (!text->GotoNextNote(this)) {
627                 if (text->cursor.pos() 
628                     || text->cursor.par() != text->FirstParagraph()) {
629                                 tmp = text->cursor;
630                                 text->cursor.par(text->FirstParagraph());
631                                 text->cursor.pos(0);
632                                 if (!text->GotoNextNote(this)) {
633                                         text->cursor = tmp;
634                                         owner()->getMiniBuffer()->Set(_("No more notes"));
635                                         LyXBell();
636                                 }
637                         } else {
638                                 owner()->getMiniBuffer()->Set(_("No more notes"));
639                                 LyXBell();
640                         }
641         }
642         update(BufferView::SELECT|BufferView::FITCUR);
643         text->sel_cursor = text->cursor;
644 }
645
646
647 void BufferView::insertCorrectQuote()
648 {
649         char c;
650
651         if (text->cursor.pos())
652                 c = text->cursor.par()->GetChar(text->cursor.pos() - 1);
653         else 
654                 c = ' ';
655
656         insertInset(new InsetQuotes(c, buffer()->params));
657 }
658
659
660 /* these functions are for the spellchecker */ 
661 char * BufferView::nextWord(float & value)
662 {
663         if (!available()) {
664                 value = 1;
665                 return 0;
666         }
667
668         char * string = text->SelectNextWord(this, value);
669
670         return string;
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.c_str());
712    
713         text->SetSelectionOverString(this, replacestring.c_str());
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->CursorLeftIntern(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 (!the_locking_inset && inset) {
727                 the_locking_inset = inset;
728                 return true;
729         } else if (inset) {
730             return the_locking_inset->LockInsetInInset(this, inset);
731         }
732         return false;
733 }
734
735
736 void BufferView::showLockedInsetCursor(long x, long y, int asc, int desc)
737 {
738         if (the_locking_inset && 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                      the_locking_inset->GetLockingInset()))
745                         text->SetCursor(this, cursor,
746                                         cursor.par(), cursor.pos() - 1);
747                 y += cursor.y() + the_locking_inset->InsetInInsetY();
748                 pimpl_->screen->ShowManualCursor(x, y, asc, desc,
749                                          LyXScreen::BAR_SHAPE);
750         }
751 }
752
753
754 void BufferView::hideLockedInsetCursor()
755 {
756         if (the_locking_inset && available()) {
757                 pimpl_->screen->HideCursor();
758         }
759 }
760
761
762 void BufferView::fitLockedInsetCursor(long x, long y, int asc, int desc)
763 {
764         if (the_locking_inset && available()){
765                 y += text->cursor.y() + the_locking_inset->InsetInInsetY();
766                 if (pimpl_->screen->FitManualCursor(x, y, asc, desc))
767                         updateScrollbar();
768         }
769 }
770
771
772 int BufferView::unlockInset(UpdatableInset * inset)
773 {
774         if (inset && the_locking_inset == inset) {
775                 inset->InsetUnlock(this);
776                 the_locking_inset = 0;
777                 text->FinishUndo();
778                 return 0;
779         } else if (inset && the_locking_inset &&
780                    the_locking_inset->UnlockInsetInInset(this, inset)) {
781                 text->FinishUndo();
782                 return 0;
783         }
784         return bufferlist.unlockInset(inset);
785 }
786
787
788 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
789 {
790         if (!the_locking_inset)
791                 return; // shouldn't happen
792         if (kind == Undo::EDIT) // in this case insets would not be stored!
793                 kind = Undo::FINISH;
794         text->SetUndo(buffer(), kind,
795                       text->cursor.par()->
796                       ParFromPos(text->cursor.pos())->previous, 
797                       text->cursor.par()->
798                       ParFromPos(text->cursor.pos())->next);
799 }
800
801
802 void BufferView::updateInset(Inset * inset, bool mark_dirty)
803 {
804         if (!inset)
805                 return;
806
807         // first check for locking insets
808         if (the_locking_inset) {
809                 if (the_locking_inset == inset) {
810                         if (text->UpdateInset(this, inset)){
811                                 update();
812                                 if (mark_dirty){
813                                         if (buffer()->isLyxClean())
814                                                 owner()->getMiniBuffer()->
815                                                         setTimer(4);
816                                         buffer()->markDirty();
817                                 }
818                                 updateScrollbar();
819                                 return;
820                         }
821                 } else if (the_locking_inset->UpdateInsetInInset(this,inset)) {
822                         if (text->UpdateInset(this, the_locking_inset)) {
823                                 update();
824                                 if (mark_dirty){
825                                         if (buffer()->isLyxClean())
826                                                 owner()->getMiniBuffer()->
827                                                         setTimer(4);
828                                         buffer()->markDirty();
829                                 }
830                                 updateScrollbar();
831                                 return;
832                         }
833                 }
834         }
835   
836         // then check the current buffer
837         if (available()) {
838                 hideCursor();
839                 update(BufferView::UPDATE);
840                 if (text->UpdateInset(this, inset)){
841                         if (mark_dirty)
842                                 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
843                         else 
844                                 update(SELECT);
845                         return;
846                 }
847         }
848 }
849
850 bool BufferView::ChangeRefs(string const & from, string const & to)
851 {
852         bool flag = false;
853         LyXParagraph * par = buffer()->paragraph;
854         LyXCursor cursor = text->cursor;
855         LyXCursor tmpcursor = cursor;
856         cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
857         cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
858
859         while (par) {
860                 bool flag2 = false;
861                 for (LyXParagraph::inset_iterator it = par->inset_iterator_begin();
862                      it != par->inset_iterator_end(); ++it) {
863                         if ((*it)->LyxCode() == Inset::REF_CODE) {
864                                 InsetCommand * inset = static_cast<InsetCommand *>(*it);
865                                 if (inset->getContents() == from) {
866                                         inset->setContents(to);
867                                         flag2 = true;
868                                 }
869                         }
870                 }
871                 if (flag2) {
872                         flag = true;
873                         if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
874                                 // this is possible now, since SetCursor takes
875                                 // care about footnotes
876                                 text->SetCursorIntern(this, par, 0);
877                                 text->RedoParagraphs(this, text->cursor,
878                                                      text->cursor.par()->Next());
879                                 text->FullRebreak(this);
880                         }
881                 }
882                 par = par->next;
883         }
884         text->SetCursorIntern(this, cursor.par(), cursor.pos());
885         return flag;
886 }