2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1995 Matthias Ettrich
8 * Copyright 1995-2000 The LyX Team.
10 * ====================================================== */
17 #include "BufferView.h"
19 #include "lyxcursor.h"
21 #include "insets/inseterror.h"
22 #include "insets/insetinfo.h"
23 #include "insets/insetspecialchar.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"
32 #include "BufferView_pimpl.h"
33 #include "insets/insetcommand.h" //ChangeRefs
35 extern BufferList bufferlist;
43 // Inserts a file into current document
44 bool BufferView::insertLyXFile(string const & filen)
46 // Copyright CHT Software Service GmbH
49 // Insert a Lyxformat - file into current buffer
51 // Moved from lyx_cb.C (Lgb)
53 if (filen.empty()) return false;
55 string fname = MakeAbsPath(filen);
57 // check if file exist
61 WriteAlert(_("Error!"),
62 _("Specified file is unreadable: "),
63 MakeDisplayPath(fname, 50));
69 ifstream ifs(fname.c_str());
71 WriteAlert(_("Error!"),
72 _("Cannot open specified file: "),
73 MakeDisplayPath(fname, 50));
85 lyxerr.debug() << "Will insert file with header" << endl;
86 res = buffer()->readFile(lex, text->cursor.par());
88 lyxerr.debug() << "Will insert file without header" << endl;
89 res = buffer()->readLyXformat2(lex, text->cursor.par());
96 bool BufferView::removeAutoInsets()
98 LyXParagraph * par = buffer()->paragraph;
100 LyXCursor tmpcursor = text->cursor;
105 // this has to be done before the delete
107 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
109 text->SetCursor(this, cursor, par, 0);
110 if (par->AutoDeleteInsets()){
113 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
115 text->RedoParagraphs(this, cursor,
116 cursor.par()->Next());
117 text->FullRebreak(this);
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());
133 void BufferView::insertErrors(TeXErrors & terr)
135 // Save the cursor position
136 LyXCursor cursor = text->cursor;
139 // This is drastic, but it's the only fix, I could find. (Asger)
144 for (TeXErrors::Errors::const_iterator cit = terr.begin();
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;
152 // Insert error string for row number
156 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
157 buffer()->texrow.increasePos(tmpid, tmppos);
160 LyXParagraph * texrowpar = 0;
163 texrowpar = text->FirstParagraph();
166 texrowpar = text->GetParFromID(tmpid);
172 InsetError * new_inset = new InsetError(msgtxt);
173 text->SetCursorIntern(this, texrowpar, tmppos);
174 text->InsertInset(this, new_inset);
175 text->FullRebreak(this);
177 // Restore the cursor position
178 text->SetCursorIntern(this, cursor.par(), cursor.pos());
182 void BufferView::setCursorFromRow(int row)
187 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
189 LyXParagraph * texrowpar;
192 texrowpar = text->FirstParagraph();
195 texrowpar = text->GetParFromID(tmpid);
197 text->SetCursor(this, texrowpar, tmppos);
200 bool BufferView::insertInset(Inset * inset, string const & lout,
203 // if we are in a locking inset we should try to insert the
204 // inset there otherwise this is a illegal function now
205 if (the_locking_inset) {
206 if (the_locking_inset->InsertInsetAllowed(inset) &&
207 the_locking_inset->InsertInset(this, inset))
213 // check for table/list in tables
214 if (no_table && text->cursor.par()->table){
215 WriteAlert(_("Impossible Operation!"),
216 _("Cannot insert table/list in table."),
222 // not quite sure if we want this...
223 text->SetCursorParUndo(buffer());
228 update(BufferView::SELECT|BufferView::FITCUR);
229 text->BreakParagraph(this);
230 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
232 if (text->cursor.par()->Last()) {
233 text->CursorLeft(this);
235 text->BreakParagraph(this);
236 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
239 pair<bool, LyXTextClass::size_type> lres =
240 textclasslist.NumberOfLayout(buffer()->params
242 LyXTextClass::size_type lay;
243 if (lres.first != false) {
247 // layout not fount using default "Standard" (0)
251 text->SetLayout(this, lay);
253 text->SetParagraph(this, 0, 0,
255 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
259 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
261 text->current_font.setLatex(LyXFont::OFF);
264 text->InsertInset(this, inset);
266 // if we enter a text-inset the cursor should be to the left side
267 // of it! This couldn't happen before as Undo was not handled inside
268 // inset now after the Undo LyX tries to call inset->Edit(...) again
269 // and cannot do this as the cursor is behind the inset and GetInset
270 // does not return the inset!
271 if (inset->IsTextInset()) {
272 if (text->cursor.par()->isRightToLeftPar(buffer()->params))
273 text->CursorRight(this);
275 text->CursorLeft(this);
278 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
280 text->UnFreezeUndo();
285 // Open and lock an updatable inset
286 bool BufferView::open_new_inset(UpdatableInset * new_inset)
290 if (!insertInset(new_inset))
292 text->CursorLeft(this);
293 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
294 new_inset->Edit(this, 0, 0, 0);
298 /* This is also a buffer property (ale) */
299 // Not so sure about that. a goto Label function can not be buffer local, just
300 // think how this will work in a multiwindo/buffer environment, all the
301 // cursors in all the views showing this buffer will move. (Lgb)
302 // OK, then no cursor action should be allowed in buffer. (ale)
303 bool BufferView::gotoLabel(string const & label)
306 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
307 it != buffer()->inset_iterator_end(); ++it) {
308 vector<string> labels = (*it)->getLabelList();
309 if ( find(labels.begin(),labels.end(),label)
312 text->SetCursor(this, it.getPar(), it.getPos());
313 text->sel_cursor = text->cursor;
314 update(BufferView::SELECT|BufferView::FITCUR);
323 void BufferView::allFloats(char flag, char figmar)
325 if (!available()) return;
327 LyXCursor cursor = text->cursor;
330 && cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE
332 && cursor.par()->footnotekind != LyXParagraph::FOOTNOTE
333 && cursor.par()->footnotekind != LyXParagraph::MARGIN
336 && cursor.par()->footnotekind != LyXParagraph::FIG
337 && cursor.par()->footnotekind != LyXParagraph::TAB
338 && cursor.par()->footnotekind != LyXParagraph::WIDE_FIG
339 && cursor.par()->footnotekind != LyXParagraph::WIDE_TAB
340 && cursor.par()->footnotekind != LyXParagraph::ALGORITHM)))
345 LyXCursor tmpcursor = cursor;
346 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
347 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
349 LyXParagraph *par = buffer()->paragraph;
352 if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
354 && par->footnotekind != LyXParagraph::FOOTNOTE
355 && par->footnotekind != LyXParagraph::MARGIN)
357 && par->footnotekind != LyXParagraph::FIG
358 && par->footnotekind != LyXParagraph::TAB
359 && par->footnotekind != LyXParagraph::WIDE_FIG
360 && par->footnotekind != LyXParagraph::WIDE_TAB
361 && par->footnotekind != LyXParagraph::ALGORITHM
366 && par->previous->footnoteflag !=
367 LyXParagraph::CLOSED_FOOTNOTE){ /* should be */
368 text->SetCursorIntern(this,
371 text->OpenFootnote(this);
375 if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
379 par->footnotekind != LyXParagraph::FOOTNOTE
381 par->footnotekind != LyXParagraph::MARGIN
386 par->footnotekind != LyXParagraph::FIG
388 par->footnotekind != LyXParagraph::TAB
390 par->footnotekind != LyXParagraph::WIDE_FIG
392 par->footnotekind != LyXParagraph::WIDE_TAB
394 par->footnotekind != LyXParagraph::ALGORITHM
398 text->SetCursorIntern(this, par, 0);
399 text->CloseFootnote(this);
405 text->SetCursorIntern(this, cursor.par(), cursor.pos());
413 void BufferView::insertNote()
415 InsetInfo * new_inset = new InsetInfo();
416 insertInset(new_inset);
417 new_inset->Edit(this, 0, 0, 0);
422 void BufferView::openStuff()
425 owner()->getMiniBuffer()->Set(_("Open/Close..."));
428 update(BufferView::SELECT|BufferView::FITCUR);
429 text->OpenStuff(this);
430 update(BufferView::SELECT|BufferView::FITCUR);
436 void BufferView::toggleFloat()
439 owner()->getMiniBuffer()->Set(_("Open/Close..."));
442 update(BufferView::SELECT|BufferView::FITCUR);
443 text->ToggleFootnote(this);
444 update(BufferView::SELECT|BufferView::FITCUR);
450 void BufferView::menuUndo()
453 owner()->getMiniBuffer()->Set(_("Undo"));
456 update(BufferView::SELECT|BufferView::FITCUR);
457 if (!text->TextUndo(this))
458 owner()->getMiniBuffer()->Set(_("No further undo information"));
460 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
466 void BufferView::menuRedo()
468 if (the_locking_inset) {
469 owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
474 owner()->getMiniBuffer()->Set(_("Redo"));
477 update(BufferView::SELECT|BufferView::FITCUR);
478 if (!text->TextRedo(this))
479 owner()->getMiniBuffer()->Set(_("No further redo information"));
481 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
487 void BufferView::hyphenationPoint()
491 update(BufferView::SELECT|BufferView::FITCUR);
492 InsetSpecialChar * new_inset =
493 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
494 insertInset(new_inset);
499 void BufferView::ldots()
503 update(BufferView::SELECT|BufferView::FITCUR);
504 InsetSpecialChar * new_inset =
505 new InsetSpecialChar(InsetSpecialChar::LDOTS);
506 insertInset(new_inset);
511 void BufferView::endOfSentenceDot()
515 update(BufferView::SELECT|BufferView::FITCUR);
516 InsetSpecialChar * new_inset =
517 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
518 insertInset(new_inset);
523 void BufferView::menuSeparator()
527 update(BufferView::SELECT|BufferView::FITCUR);
528 InsetSpecialChar * new_inset =
529 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
530 insertInset(new_inset);
535 void BufferView::newline()
539 update(BufferView::SELECT|BufferView::FITCUR);
540 text->InsertChar(this, LyXParagraph::META_NEWLINE);
541 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
546 void BufferView::protectedBlank()
550 update(BufferView::SELECT|BufferView::FITCUR);
551 InsetSpecialChar * new_inset =
552 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
553 insertInset(new_inset);
558 void BufferView::hfill()
562 update(BufferView::SELECT|BufferView::FITCUR);
563 text->InsertChar(this, LyXParagraph::META_HFILL);
564 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
568 void BufferView::copyEnvironment()
571 text->copyEnvironmentType();
572 // clear the selection, even if mark_set
574 text->ClearSelection();
575 update(BufferView::SELECT|BufferView::FITCUR);
576 owner()->getMiniBuffer()->Set(_("Paragraph environment type copied"));
581 void BufferView::pasteEnvironment()
584 text->pasteEnvironmentType(this);
585 owner()->getMiniBuffer()->Set(_("Paragraph environment type set"));
586 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
591 void BufferView::copy()
594 text->CopySelection(this);
595 // clear the selection, even if mark_set
597 text->ClearSelection();
598 update(BufferView::SELECT|BufferView::FITCUR);
599 owner()->getMiniBuffer()->Set(_("Copy"));
603 void BufferView::cut()
607 update(BufferView::SELECT|BufferView::FITCUR);
608 text->CutSelection(this);
609 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
610 owner()->getMiniBuffer()->Set(_("Cut"));
615 void BufferView::paste()
617 if (!available()) return;
619 owner()->getMiniBuffer()->Set(_("Paste"));
621 // clear the selection
623 text->ClearSelection();
624 update(BufferView::SELECT|BufferView::FITCUR);
627 text->PasteSelection(this);
628 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
630 // clear the selection
632 text->ClearSelection();
633 update(BufferView::SELECT|BufferView::FITCUR);
637 void BufferView::gotoNote()
639 if (!available()) return;
643 update(BufferView::SELECT|BufferView::FITCUR);
646 if (!text->GotoNextNote(this)) {
647 if (text->cursor.pos()
648 || text->cursor.par() != text->FirstParagraph()) {
650 text->cursor.par(text->FirstParagraph());
652 if (!text->GotoNextNote(this)) {
654 owner()->getMiniBuffer()->Set(_("No more notes"));
658 owner()->getMiniBuffer()->Set(_("No more notes"));
662 update(BufferView::SELECT|BufferView::FITCUR);
663 text->sel_cursor = text->cursor;
667 void BufferView::insertCorrectQuote()
671 if (text->cursor.pos())
672 c = text->cursor.par()->GetChar(text->cursor.pos() - 1);
676 insertInset(new InsetQuotes(c, buffer()->params));
680 /* these functions are for the spellchecker */
681 char * BufferView::nextWord(float & value)
688 char * string = text->SelectNextWord(this, value);
694 void BufferView::selectLastWord()
696 if (!available()) return;
700 text->SelectSelectedWord(this);
701 toggleSelection(false);
702 update(BufferView::SELECT|BufferView::FITCUR);
706 void BufferView::endOfSpellCheck()
708 if (!available()) return;
712 text->SelectSelectedWord(this);
713 text->ClearSelection();
714 update(BufferView::SELECT|BufferView::FITCUR);
718 void BufferView::replaceWord(string const & replacestring)
720 if (!available()) return;
723 update(BufferView::SELECT|BufferView::FITCUR);
725 /* clear the selection (if there is any) */
726 toggleSelection(false);
727 update(BufferView::SELECT|BufferView::FITCUR);
729 /* clear the selection (if there is any) */
730 toggleSelection(false);
731 text->ReplaceSelectionWithString(this, replacestring.c_str());
733 text->SetSelectionOverString(this, replacestring.c_str());
735 // Go back so that replacement string is also spellchecked
736 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
737 text->CursorLeftIntern(this);
739 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
741 // End of spellchecker stuff
744 bool BufferView::lockInset(UpdatableInset * inset)
746 if (!the_locking_inset && inset) {
747 the_locking_inset = inset;
750 return the_locking_inset->LockInsetInInset(this, inset);
756 void BufferView::showLockedInsetCursor(long x, long y, int asc, int desc)
758 if (the_locking_inset && available()) {
759 LyXCursor cursor = text->cursor;
760 if ((cursor.pos() - 1 >= 0) &&
761 (cursor.par()->GetChar(cursor.pos() - 1) ==
762 LyXParagraph::META_INSET) &&
763 (cursor.par()->GetInset(cursor.pos() - 1) ==
764 the_locking_inset->GetLockingInset()))
765 text->SetCursor(this, cursor,
766 cursor.par(), cursor.pos() - 1);
767 y += cursor.y() + the_locking_inset->InsetInInsetY();
768 pimpl_->screen_->ShowManualCursor(text, x, y, asc, desc,
769 LyXScreen::BAR_SHAPE);
774 void BufferView::hideLockedInsetCursor()
776 if (the_locking_inset && available()) {
777 pimpl_->screen_->HideCursor();
782 void BufferView::fitLockedInsetCursor(long x, long y, int asc, int desc)
784 if (the_locking_inset && available()){
785 y += text->cursor.y() + the_locking_inset->InsetInInsetY();
786 if (pimpl_->screen_->FitManualCursor(text, x, y, asc, desc))
792 int BufferView::unlockInset(UpdatableInset * inset)
794 if (inset && the_locking_inset == inset) {
795 inset->InsetUnlock(this);
796 the_locking_inset = 0;
799 } else if (inset && the_locking_inset &&
800 the_locking_inset->UnlockInsetInInset(this, inset)) {
804 return bufferlist.unlockInset(inset);
808 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
810 if (!the_locking_inset)
811 return; // shouldn't happen
812 if (kind == Undo::EDIT) // in this case insets would not be stored!
814 text->SetUndo(buffer(), kind,
817 ParFromPos(text->cursor.pos())->previous,
819 ParFromPos(text->cursor.pos())->next
821 text->cursor.par()->previous,
822 text->cursor.par()->next
828 void BufferView::updateInset(Inset * inset, bool mark_dirty)
833 // first check for locking insets
834 if (the_locking_inset) {
835 if (the_locking_inset == inset) {
836 if (text->UpdateInset(this, inset)){
839 if (buffer()->isLyxClean())
840 owner()->getMiniBuffer()->
842 buffer()->markDirty();
847 } else if (the_locking_inset->UpdateInsetInInset(this,inset)) {
848 if (text->UpdateInset(this, the_locking_inset)) {
851 if (buffer()->isLyxClean())
852 owner()->getMiniBuffer()->
854 buffer()->markDirty();
862 // then check the current buffer
865 update(BufferView::UPDATE);
866 if (text->UpdateInset(this, inset)){
868 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
876 bool BufferView::ChangeRefs(string const & from, string const & to)
879 LyXParagraph * par = buffer()->paragraph;
880 LyXCursor cursor = text->cursor;
881 LyXCursor tmpcursor = cursor;
883 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
884 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
886 cursor.par(tmpcursor.par());
887 cursor.pos(tmpcursor.pos());
892 for (LyXParagraph::inset_iterator it = par->inset_iterator_begin();
893 it != par->inset_iterator_end(); ++it) {
894 if ((*it)->LyxCode() == Inset::REF_CODE) {
895 InsetCommand * inset = static_cast<InsetCommand *>(*it);
896 if (inset->getContents() == from) {
897 inset->setContents(to);
905 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
907 // this is possible now, since SetCursor takes
908 // care about footnotes
909 text->SetCursorIntern(this, par, 0);
910 text->RedoParagraphs(this, text->cursor,
911 text->cursor.par()->Next());
912 text->FullRebreak(this);
919 text->SetCursorIntern(this, cursor.par(), cursor.pos());