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))
212 // not quite sure if we want this...
213 text->SetCursorParUndo(buffer());
218 update(BufferView::SELECT|BufferView::FITCUR);
219 text->BreakParagraph(this);
220 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
222 if (text->cursor.par()->Last()) {
223 text->CursorLeft(this);
225 text->BreakParagraph(this);
226 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
229 pair<bool, LyXTextClass::size_type> lres =
230 textclasslist.NumberOfLayout(buffer()->params
232 LyXTextClass::size_type lay;
233 if (lres.first != false) {
237 // layout not fount using default "Standard" (0)
241 text->SetLayout(this, lay);
243 text->SetParagraph(this, 0, 0,
245 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
249 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
251 text->current_font.setLatex(LyXFont::OFF);
254 text->InsertInset(this, inset);
256 // if we enter a text-inset the cursor should be to the left side
257 // of it! This couldn't happen before as Undo was not handled inside
258 // inset now after the Undo LyX tries to call inset->Edit(...) again
259 // and cannot do this as the cursor is behind the inset and GetInset
260 // does not return the inset!
261 if (inset->IsTextInset()) {
262 if (text->cursor.par()->isRightToLeftPar(buffer()->params))
263 text->CursorRight(this);
265 text->CursorLeft(this);
268 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
270 text->UnFreezeUndo();
275 // Open and lock an updatable inset
276 bool BufferView::open_new_inset(UpdatableInset * new_inset)
280 if (!insertInset(new_inset))
282 text->CursorLeft(this);
283 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
284 new_inset->Edit(this, 0, 0, 0);
288 /* This is also a buffer property (ale) */
289 // Not so sure about that. a goto Label function can not be buffer local, just
290 // think how this will work in a multiwindo/buffer environment, all the
291 // cursors in all the views showing this buffer will move. (Lgb)
292 // OK, then no cursor action should be allowed in buffer. (ale)
293 bool BufferView::gotoLabel(string const & label)
296 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
297 it != buffer()->inset_iterator_end(); ++it) {
298 vector<string> labels = (*it)->getLabelList();
299 if ( find(labels.begin(),labels.end(),label)
302 text->SetCursor(this, it.getPar(), it.getPos());
303 text->sel_cursor = text->cursor;
304 update(BufferView::SELECT|BufferView::FITCUR);
313 void BufferView::allFloats(char flag, char figmar)
315 if (!available()) return;
317 LyXCursor cursor = text->cursor;
320 && cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE
322 && cursor.par()->footnotekind != LyXParagraph::FOOTNOTE
323 && cursor.par()->footnotekind != LyXParagraph::MARGIN
326 && cursor.par()->footnotekind != LyXParagraph::FIG
327 && cursor.par()->footnotekind != LyXParagraph::TAB
328 && cursor.par()->footnotekind != LyXParagraph::WIDE_FIG
329 && cursor.par()->footnotekind != LyXParagraph::WIDE_TAB
330 && cursor.par()->footnotekind != LyXParagraph::ALGORITHM)))
335 LyXCursor tmpcursor = cursor;
336 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
337 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
339 LyXParagraph *par = buffer()->paragraph;
342 if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
344 && par->footnotekind != LyXParagraph::FOOTNOTE
345 && par->footnotekind != LyXParagraph::MARGIN)
347 && par->footnotekind != LyXParagraph::FIG
348 && par->footnotekind != LyXParagraph::TAB
349 && par->footnotekind != LyXParagraph::WIDE_FIG
350 && par->footnotekind != LyXParagraph::WIDE_TAB
351 && par->footnotekind != LyXParagraph::ALGORITHM
356 && par->previous->footnoteflag !=
357 LyXParagraph::CLOSED_FOOTNOTE){ /* should be */
358 text->SetCursorIntern(this,
361 text->OpenFootnote(this);
365 if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
369 par->footnotekind != LyXParagraph::FOOTNOTE
371 par->footnotekind != LyXParagraph::MARGIN
376 par->footnotekind != LyXParagraph::FIG
378 par->footnotekind != LyXParagraph::TAB
380 par->footnotekind != LyXParagraph::WIDE_FIG
382 par->footnotekind != LyXParagraph::WIDE_TAB
384 par->footnotekind != LyXParagraph::ALGORITHM
388 text->SetCursorIntern(this, par, 0);
389 text->CloseFootnote(this);
395 text->SetCursorIntern(this, cursor.par(), cursor.pos());
403 void BufferView::insertNote()
405 InsetInfo * new_inset = new InsetInfo();
406 insertInset(new_inset);
407 new_inset->Edit(this, 0, 0, 0);
412 void BufferView::openStuff()
415 owner()->getMiniBuffer()->Set(_("Open/Close..."));
418 update(BufferView::SELECT|BufferView::FITCUR);
419 text->OpenStuff(this);
420 update(BufferView::SELECT|BufferView::FITCUR);
426 void BufferView::toggleFloat()
429 owner()->getMiniBuffer()->Set(_("Open/Close..."));
432 update(BufferView::SELECT|BufferView::FITCUR);
433 text->ToggleFootnote(this);
434 update(BufferView::SELECT|BufferView::FITCUR);
440 void BufferView::menuUndo()
443 owner()->getMiniBuffer()->Set(_("Undo"));
446 update(BufferView::SELECT|BufferView::FITCUR);
447 if (!text->TextUndo(this))
448 owner()->getMiniBuffer()->Set(_("No further undo information"));
450 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
456 void BufferView::menuRedo()
458 if (the_locking_inset) {
459 owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
464 owner()->getMiniBuffer()->Set(_("Redo"));
467 update(BufferView::SELECT|BufferView::FITCUR);
468 if (!text->TextRedo(this))
469 owner()->getMiniBuffer()->Set(_("No further redo information"));
471 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
477 void BufferView::hyphenationPoint()
481 update(BufferView::SELECT|BufferView::FITCUR);
482 InsetSpecialChar * new_inset =
483 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
484 insertInset(new_inset);
489 void BufferView::ldots()
493 update(BufferView::SELECT|BufferView::FITCUR);
494 InsetSpecialChar * new_inset =
495 new InsetSpecialChar(InsetSpecialChar::LDOTS);
496 insertInset(new_inset);
501 void BufferView::endOfSentenceDot()
505 update(BufferView::SELECT|BufferView::FITCUR);
506 InsetSpecialChar * new_inset =
507 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
508 insertInset(new_inset);
513 void BufferView::menuSeparator()
517 update(BufferView::SELECT|BufferView::FITCUR);
518 InsetSpecialChar * new_inset =
519 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
520 insertInset(new_inset);
525 void BufferView::newline()
529 update(BufferView::SELECT|BufferView::FITCUR);
530 text->InsertChar(this, LyXParagraph::META_NEWLINE);
531 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
536 void BufferView::protectedBlank()
540 update(BufferView::SELECT|BufferView::FITCUR);
541 InsetSpecialChar * new_inset =
542 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
543 insertInset(new_inset);
548 void BufferView::hfill()
552 update(BufferView::SELECT|BufferView::FITCUR);
553 text->InsertChar(this, LyXParagraph::META_HFILL);
554 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
558 void BufferView::copyEnvironment()
561 text->copyEnvironmentType();
562 // clear the selection, even if mark_set
564 text->ClearSelection();
565 update(BufferView::SELECT|BufferView::FITCUR);
566 owner()->getMiniBuffer()->Set(_("Paragraph environment type copied"));
571 void BufferView::pasteEnvironment()
574 text->pasteEnvironmentType(this);
575 owner()->getMiniBuffer()->Set(_("Paragraph environment type set"));
576 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
581 void BufferView::copy()
584 text->CopySelection(this);
585 // clear the selection, even if mark_set
587 text->ClearSelection();
588 update(BufferView::SELECT|BufferView::FITCUR);
589 owner()->getMiniBuffer()->Set(_("Copy"));
593 void BufferView::cut()
597 update(BufferView::SELECT|BufferView::FITCUR);
598 text->CutSelection(this);
599 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
600 owner()->getMiniBuffer()->Set(_("Cut"));
605 void BufferView::paste()
607 if (!available()) return;
609 owner()->getMiniBuffer()->Set(_("Paste"));
611 // clear the selection
613 text->ClearSelection();
614 update(BufferView::SELECT|BufferView::FITCUR);
617 text->PasteSelection(this);
618 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
620 // clear the selection
622 text->ClearSelection();
623 update(BufferView::SELECT|BufferView::FITCUR);
627 void BufferView::gotoNote()
629 if (!available()) return;
633 update(BufferView::SELECT|BufferView::FITCUR);
636 if (!text->GotoNextNote(this)) {
637 if (text->cursor.pos()
638 || text->cursor.par() != text->FirstParagraph()) {
640 text->cursor.par(text->FirstParagraph());
642 if (!text->GotoNextNote(this)) {
644 owner()->getMiniBuffer()->Set(_("No more notes"));
648 owner()->getMiniBuffer()->Set(_("No more notes"));
652 update(BufferView::SELECT|BufferView::FITCUR);
653 text->sel_cursor = text->cursor;
657 void BufferView::insertCorrectQuote()
661 if (text->cursor.pos())
662 c = text->cursor.par()->GetChar(text->cursor.pos() - 1);
666 insertInset(new InsetQuotes(c, buffer()->params));
670 /* these functions are for the spellchecker */
671 string const BufferView::nextWord(float & value)
678 return text->SelectNextWord(this, value);
682 void BufferView::selectLastWord()
684 if (!available()) return;
688 text->SelectSelectedWord(this);
689 toggleSelection(false);
690 update(BufferView::SELECT|BufferView::FITCUR);
694 void BufferView::endOfSpellCheck()
696 if (!available()) return;
700 text->SelectSelectedWord(this);
701 text->ClearSelection();
702 update(BufferView::SELECT|BufferView::FITCUR);
706 void BufferView::replaceWord(string const & replacestring)
708 if (!available()) return;
711 update(BufferView::SELECT|BufferView::FITCUR);
713 /* clear the selection (if there is any) */
714 toggleSelection(false);
715 update(BufferView::SELECT|BufferView::FITCUR);
717 /* clear the selection (if there is any) */
718 toggleSelection(false);
719 text->ReplaceSelectionWithString(this, replacestring);
721 text->SetSelectionOverString(this, replacestring);
723 // Go back so that replacement string is also spellchecked
724 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
725 text->CursorLeft(this);
727 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
729 // End of spellchecker stuff
732 bool BufferView::lockInset(UpdatableInset * inset)
734 if (!the_locking_inset && inset) {
735 the_locking_inset = inset;
738 return the_locking_inset->LockInsetInInset(this, inset);
744 void BufferView::showLockedInsetCursor(long x, long y, int asc, int desc)
746 if (the_locking_inset && available()) {
747 LyXCursor cursor = text->cursor;
748 if ((cursor.pos() - 1 >= 0) &&
749 (cursor.par()->GetChar(cursor.pos() - 1) ==
750 LyXParagraph::META_INSET) &&
751 (cursor.par()->GetInset(cursor.pos() - 1) ==
752 the_locking_inset->GetLockingInset()))
753 text->SetCursor(this, cursor,
754 cursor.par(), cursor.pos() - 1);
755 y += cursor.y() + the_locking_inset->InsetInInsetY();
756 pimpl_->screen_->ShowManualCursor(text, x, y, asc, desc,
757 LyXScreen::BAR_SHAPE);
762 void BufferView::hideLockedInsetCursor()
764 if (the_locking_inset && available()) {
765 pimpl_->screen_->HideCursor();
770 void BufferView::fitLockedInsetCursor(long x, long y, int asc, int desc)
772 if (the_locking_inset && available()){
773 y += text->cursor.y() + the_locking_inset->InsetInInsetY();
774 if (pimpl_->screen_->FitManualCursor(text, x, y, asc, desc))
780 int BufferView::unlockInset(UpdatableInset * inset)
782 if (inset && the_locking_inset == inset) {
783 inset->InsetUnlock(this);
784 the_locking_inset = 0;
787 } else if (inset && the_locking_inset &&
788 the_locking_inset->UnlockInsetInInset(this, inset)) {
792 return bufferlist.unlockInset(inset);
796 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
798 if (!the_locking_inset)
799 return; // shouldn't happen
800 if (kind == Undo::EDIT) // in this case insets would not be stored!
802 text->SetUndo(buffer(), kind,
805 ParFromPos(text->cursor.pos())->previous,
807 ParFromPos(text->cursor.pos())->next
809 text->cursor.par()->previous,
810 text->cursor.par()->next
816 void BufferView::updateInset(Inset * inset, bool mark_dirty)
821 // first check for locking insets
822 if (the_locking_inset) {
823 if (the_locking_inset == inset) {
824 if (text->UpdateInset(this, inset)){
827 if (buffer()->isLyxClean())
828 owner()->getMiniBuffer()->
830 buffer()->markDirty();
835 } else if (the_locking_inset->UpdateInsetInInset(this,inset)) {
836 if (text->UpdateInset(this, the_locking_inset)) {
839 if (buffer()->isLyxClean())
840 owner()->getMiniBuffer()->
842 buffer()->markDirty();
850 // then check the current buffer
853 update(BufferView::UPDATE);
854 if (text->UpdateInset(this, inset)){
856 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
864 bool BufferView::ChangeRefs(string const & from, string const & to)
867 LyXParagraph * par = buffer()->paragraph;
868 LyXCursor cursor = text->cursor;
869 LyXCursor tmpcursor = cursor;
871 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
872 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
874 cursor.par(tmpcursor.par());
875 cursor.pos(tmpcursor.pos());
880 for (LyXParagraph::inset_iterator it = par->inset_iterator_begin();
881 it != par->inset_iterator_end(); ++it) {
882 if ((*it)->LyxCode() == Inset::REF_CODE) {
883 InsetCommand * inset = static_cast<InsetCommand *>(*it);
884 if (inset->getContents() == from) {
885 inset->setContents(to);
893 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
895 // this is possible now, since SetCursor takes
896 // care about footnotes
897 text->SetCursorIntern(this, par, 0);
898 text->RedoParagraphs(this, text->cursor,
899 text->cursor.par()->Next());
900 text->FullRebreak(this);
907 text->SetCursorIntern(this, cursor.par(), cursor.pos());