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 "bufferlist.h"
26 #include "support/FileInfo.h"
27 #include "lyxscreen.h"
28 #include "support/filetools.h"
29 #include "lyx_gui_misc.h"
31 #include "BufferView_pimpl.h"
32 #include "insets/insetcommand.h" //ChangeRefs
33 #include "support/lyxfunctional.h" //equal_1st_in_pair
38 extern BufferList bufferlist;
48 // Inserts a file into current document
49 bool BufferView::insertLyXFile(string const & filen)
51 // Copyright CHT Software Service GmbH
54 // Insert a Lyxformat - file into current buffer
56 // Moved from lyx_cb.C (Lgb)
58 if (filen.empty()) return false;
60 string const fname = MakeAbsPath(filen);
62 // check if file exist
63 FileInfo const fi(fname);
66 WriteAlert(_("Error!"),
67 _("Specified file is unreadable: "),
68 MakeDisplayPath(fname, 50));
74 ifstream ifs(fname.c_str());
76 WriteAlert(_("Error!"),
77 _("Cannot open specified file: "),
78 MakeDisplayPath(fname, 50));
82 char const c = ifs.peek();
90 lyxerr.debug() << "Will insert file with header" << endl;
91 res = buffer()->readFile(lex, text->cursor.par());
93 lyxerr.debug() << "Will insert file without header" << endl;
94 res = buffer()->readLyXformat2(lex, text->cursor.par());
102 bool BufferView::removeAutoInsets()
104 LyXParagraph * par = buffer()->paragraph;
106 LyXCursor tmpcursor = text->cursor;
112 // this has to be done before the delete
113 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
114 text->SetCursor(this, cursor, par, 0);
115 if (par->AutoDeleteInsets()){
117 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
118 text->RedoParagraphs(this, cursor,
119 cursor.par()->next());
120 text->FullRebreak(this);
126 // avoid forbidden cursor positions caused by error removing
127 if (tmpcursor.pos() > tmpcursor.par()->Last())
128 tmpcursor.pos(tmpcursor.par()->Last());
131 // this has to be done before the delete
132 text->SetCursor(this, cursor, par, 0);
133 if (par->AutoDeleteInsets()){
135 text->RedoParagraphs(this, cursor,
136 cursor.par()->next());
137 text->FullRebreak(this);
142 // avoid forbidden cursor positions caused by error removing
143 if (tmpcursor.pos() > tmpcursor.par()->size())
144 tmpcursor.pos(tmpcursor.par()->size());
146 text->SetCursorIntern(this, tmpcursor.par(), tmpcursor.pos());
152 void BufferView::insertErrors(TeXErrors & terr)
154 // Save the cursor position
155 LyXCursor cursor = text->cursor;
158 // This is drastic, but it's the only fix, I could find. (Asger)
163 for (TeXErrors::Errors::const_iterator cit = terr.begin();
166 string const desctext((*cit).error_desc);
167 string const errortext((*cit).error_text);
168 string const msgtxt = desctext + '\n' + errortext;
169 int const errorrow = (*cit).error_in_line;
171 // Insert error string for row number
175 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
176 buffer()->texrow.increasePos(tmpid, tmppos);
179 LyXParagraph * texrowpar = 0;
182 texrowpar = text->FirstParagraph();
185 texrowpar = text->GetParFromID(tmpid);
191 InsetError * new_inset = new InsetError(msgtxt);
192 text->SetCursorIntern(this, texrowpar, tmppos);
193 text->InsertInset(this, new_inset);
194 text->FullRebreak(this);
196 // Restore the cursor position
197 text->SetCursorIntern(this, cursor.par(), cursor.pos());
201 void BufferView::setCursorFromRow(int row)
206 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
208 LyXParagraph * texrowpar;
211 texrowpar = text->FirstParagraph();
214 texrowpar = text->GetParFromID(tmpid);
216 text->SetCursor(this, texrowpar, tmppos);
220 bool BufferView::insertInset(Inset * inset, string const & lout,
223 // if we are in a locking inset we should try to insert the
224 // inset there otherwise this is a illegal function now
225 if (theLockingInset()) {
226 if (theLockingInset()->InsertInsetAllowed(inset))
227 return theLockingInset()->InsertInset(this, inset);
231 // not quite sure if we want this...
232 text->SetCursorParUndo(buffer());
237 update(text, BufferView::SELECT|BufferView::FITCUR);
238 text->BreakParagraph(this);
239 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
242 if (text->cursor.par()->Last()) {
244 if (text->cursor.par()->size()) {
246 text->CursorLeft(this);
248 text->BreakParagraph(this);
249 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
252 pair<bool, LyXTextClass::size_type> lres =
253 textclasslist.NumberOfLayout(buffer()->params
255 LyXTextClass::size_type lay;
256 if (lres.first != false) {
260 // layout not fount using default "Standard" (0)
264 text->SetLayout(this, lay);
266 text->SetParagraph(this, 0, 0,
268 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
272 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
274 text->current_font.setLatex(LyXFont::OFF);
277 text->InsertInset(this, inset);
278 update(text, 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, bool behind)
290 if (!insertInset(new_inset)) {
295 LyXFont & font = getLyXText()->real_current_font;
296 new_inset->Edit(this, new_inset->width(this, font), 0, 0);
298 new_inset->Edit(this, 0, 0, 0);
302 /* This is also a buffer property (ale) */
303 // Not so sure about that. a goto Label function can not be buffer local, just
304 // think how this will work in a multiwindo/buffer environment, all the
305 // cursors in all the views showing this buffer will move. (Lgb)
306 // OK, then no cursor action should be allowed in buffer. (ale)
307 bool BufferView::gotoLabel(string const & label)
310 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
311 it != buffer()->inset_iterator_end(); ++it) {
312 vector<string> labels = (*it)->getLabelList();
313 if (find(labels.begin(),labels.end(),label)
316 text->SetCursor(this, it.getPar(), it.getPos());
317 text->sel_cursor = text->cursor;
318 update(text, BufferView::SELECT|BufferView::FITCUR);
327 void BufferView::allFloats(char flag, char figmar)
329 if (!available()) return;
331 LyXCursor cursor = text->cursor;
334 && cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE
336 && cursor.par()->footnotekind != LyXParagraph::FOOTNOTE
337 && cursor.par()->footnotekind != LyXParagraph::MARGIN
340 && cursor.par()->footnotekind != LyXParagraph::FIG
341 && cursor.par()->footnotekind != LyXParagraph::TAB
342 && cursor.par()->footnotekind != LyXParagraph::WIDE_FIG
343 && cursor.par()->footnotekind != LyXParagraph::WIDE_TAB
344 && cursor.par()->footnotekind != LyXParagraph::ALGORITHM)))
349 LyXCursor tmpcursor = cursor;
350 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
351 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
353 LyXParagraph *par = buffer()->paragraph;
356 if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
358 && par->footnotekind != LyXParagraph::FOOTNOTE
359 && par->footnotekind != LyXParagraph::MARGIN)
361 && par->footnotekind != LyXParagraph::FIG
362 && par->footnotekind != LyXParagraph::TAB
363 && par->footnotekind != LyXParagraph::WIDE_FIG
364 && par->footnotekind != LyXParagraph::WIDE_TAB
365 && par->footnotekind != LyXParagraph::ALGORITHM
370 && par->previous_->footnoteflag !=
371 LyXParagraph::CLOSED_FOOTNOTE){ /* should be */
372 text->SetCursorIntern(this,
375 text->OpenFootnote(this);
379 if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
383 par->footnotekind != LyXParagraph::FOOTNOTE
385 par->footnotekind != LyXParagraph::MARGIN
390 par->footnotekind != LyXParagraph::FIG
392 par->footnotekind != LyXParagraph::TAB
394 par->footnotekind != LyXParagraph::WIDE_FIG
396 par->footnotekind != LyXParagraph::WIDE_TAB
398 par->footnotekind != LyXParagraph::ALGORITHM
402 text->SetCursorIntern(this, par, 0);
403 text->CloseFootnote(this);
409 text->SetCursorIntern(this, cursor.par(), cursor.pos());
416 void BufferView::insertNote()
418 InsetInfo * new_inset = new InsetInfo();
419 insertInset(new_inset);
420 new_inset->Edit(this, 0, 0, 0);
425 void BufferView::openStuff()
428 owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE,
432 update(text, BufferView::SELECT|BufferView::FITCUR);
433 text->OpenStuff(this);
434 update(text, BufferView::SELECT|BufferView::FITCUR);
440 void BufferView::toggleFloat()
443 owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE,
447 update(text, BufferView::SELECT|BufferView::FITCUR);
448 text->ToggleFootnote(this);
449 update(text, BufferView::SELECT|BufferView::FITCUR);
455 void BufferView::menuUndo()
458 owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, _("Undo"));
461 update(text, BufferView::SELECT|BufferView::FITCUR);
462 if (!text->TextUndo(this))
463 owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE,
464 _("No forther undo information"));
466 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
472 void BufferView::menuRedo()
474 if (theLockingInset()) {
475 owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE,
476 _("Redo not yet supported in math mode"));
481 owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE,
485 update(text, BufferView::SELECT|BufferView::FITCUR);
486 if (!text->TextRedo(this))
487 owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE,
488 _("No further redo information"));
490 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
496 void BufferView::hyphenationPoint()
500 update(text, BufferView::SELECT|BufferView::FITCUR);
501 InsetSpecialChar * new_inset =
502 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
503 insertInset(new_inset);
508 void BufferView::ldots()
512 update(text, BufferView::SELECT|BufferView::FITCUR);
513 InsetSpecialChar * new_inset =
514 new InsetSpecialChar(InsetSpecialChar::LDOTS);
515 insertInset(new_inset);
520 void BufferView::endOfSentenceDot()
524 update(text, BufferView::SELECT|BufferView::FITCUR);
525 InsetSpecialChar * new_inset =
526 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
527 insertInset(new_inset);
532 void BufferView::menuSeparator()
536 update(text, BufferView::SELECT|BufferView::FITCUR);
537 InsetSpecialChar * new_inset =
538 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
539 insertInset(new_inset);
544 void BufferView::newline()
548 update(text, BufferView::SELECT|BufferView::FITCUR);
549 text->InsertChar(this, LyXParagraph::META_NEWLINE);
550 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
555 void BufferView::protectedBlank(LyXText * lt)
559 update(lt, BufferView::SELECT|BufferView::FITCUR);
560 InsetSpecialChar * new_inset =
561 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
562 if (!insertInset(new_inset))
565 updateInset(new_inset, true);
570 void BufferView::hfill()
574 update(text, BufferView::SELECT|BufferView::FITCUR);
575 text->InsertChar(this, LyXParagraph::META_HFILL);
576 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
581 void BufferView::copyEnvironment()
584 text->copyEnvironmentType();
585 // clear the selection, even if mark_set
587 text->ClearSelection(this);
588 update(text, BufferView::SELECT|BufferView::FITCUR);
589 owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE,
590 _("Paragraph environment type copied"));
595 void BufferView::pasteEnvironment()
598 text->pasteEnvironmentType(this);
599 owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE,
600 _("Paragraph environment type set"));
601 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
606 void BufferView::copy()
609 text->CopySelection(this);
610 // clear the selection, even if mark_set
612 text->ClearSelection(this);
613 update(text, BufferView::SELECT|BufferView::FITCUR);
614 owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, _("Copy"));
618 void BufferView::cut()
622 update(text, BufferView::SELECT|BufferView::FITCUR);
623 text->CutSelection(this);
624 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
625 owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, _("Cut"));
630 void BufferView::paste()
632 if (!available()) return;
634 owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, _("Paste"));
637 // clear the selection
639 text->ClearSelection(this);
640 update(text, BufferView::SELECT|BufferView::FITCUR);
643 text->PasteSelection(this);
644 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
646 // clear the selection
648 text->ClearSelection(this);
649 update(text, BufferView::SELECT|BufferView::FITCUR);
653 void BufferView::gotoInset(std::vector<Inset::Code> const & codes,
656 if (!available()) return;
660 update(text, BufferView::SELECT|BufferView::FITCUR);
664 text->cursor.par()->GetChar(text->cursor.pos()) == LyXParagraph::META_INSET) {
665 Inset const * inset = text->cursor.par()->GetInset(text->cursor.pos());
666 if (find(codes.begin(), codes.end(), inset->LyxCode())
669 static_cast<InsetCommand const *>(inset)->getContents();
672 if (!text->GotoNextInset(this, codes, contents)) {
673 if (text->cursor.pos()
674 || text->cursor.par() != text->FirstParagraph()) {
675 LyXCursor tmp = text->cursor;
676 text->cursor.par(text->FirstParagraph());
678 if (!text->GotoNextInset(this, codes, contents)) {
680 owner()->getLyXFunc()
681 ->Dispatch(LFUN_MESSAGE,
682 _("No more insets"));
685 owner()->getLyXFunc()
686 ->Dispatch(LFUN_MESSAGE,
687 _("No more insets"));
690 update(text, BufferView::SELECT|BufferView::FITCUR);
691 text->sel_cursor = text->cursor;
695 void BufferView::gotoInset(Inset::Code code, bool same_content)
697 gotoInset(vector<Inset::Code>(1, code), same_content);
701 void BufferView::insertCorrectQuote()
705 if (text->cursor.pos())
706 c = text->cursor.par()->GetChar(text->cursor.pos() - 1);
710 insertInset(new InsetQuotes(c, buffer()->params));
714 /* these functions are for the spellchecker */
715 string const BufferView::nextWord(float & value)
722 return text->SelectNextWord(this, value);
726 void BufferView::selectLastWord()
728 if (!available()) return;
732 text->SelectSelectedWord(this);
733 toggleSelection(false);
734 update(text, BufferView::SELECT|BufferView::FITCUR);
738 void BufferView::endOfSpellCheck()
740 if (!available()) return;
744 text->SelectSelectedWord(this);
745 text->ClearSelection(this);
746 update(text, BufferView::SELECT|BufferView::FITCUR);
750 void BufferView::replaceWord(string const & replacestring)
752 if (!available()) return;
755 update(text, BufferView::SELECT|BufferView::FITCUR);
757 /* clear the selection (if there is any) */
758 toggleSelection(false);
759 update(text, BufferView::SELECT|BufferView::FITCUR);
761 /* clear the selection (if there is any) */
762 toggleSelection(false);
763 text->ReplaceSelectionWithString(this, replacestring);
765 text->SetSelectionOverString(this, replacestring);
767 // Go back so that replacement string is also spellchecked
768 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
769 text->CursorLeft(this);
771 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
773 // End of spellchecker stuff
776 bool BufferView::lockInset(UpdatableInset * inset)
778 if (!theLockingInset() && inset) {
779 theLockingInset(inset);
782 return theLockingInset()->LockInsetInInset(this, inset);
788 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
790 if (theLockingInset() && available()) {
791 LyXCursor cursor = text->cursor;
792 if ((cursor.pos() - 1 >= 0) &&
793 (cursor.par()->GetChar(cursor.pos() - 1) ==
794 LyXParagraph::META_INSET) &&
795 (cursor.par()->GetInset(cursor.pos() - 1) ==
796 theLockingInset()->GetLockingInset()))
797 text->SetCursor(this, cursor,
798 cursor.par(), cursor.pos() - 1);
799 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
800 LyXText * txt = getLyXText();
801 if (theLockingInset()->GetLockingInset()->LyxCode() ==
803 (txt->real_current_font.language() !=
804 buffer()->params.language
805 || txt->real_current_font.isVisibleRightToLeft()
806 != buffer()->params.language->RightToLeft()))
807 shape = (txt->real_current_font.isVisibleRightToLeft())
808 ? LyXScreen::REVERSED_L_SHAPE
809 : LyXScreen::L_SHAPE;
810 y += cursor.y() + theLockingInset()->InsetInInsetY();
811 pimpl_->screen_->ShowManualCursor(text, x, y, asc, desc,
817 void BufferView::hideLockedInsetCursor()
819 if (theLockingInset() && available()) {
820 pimpl_->screen_->HideCursor();
825 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
827 if (theLockingInset() && available()) {
828 y += text->cursor.y() + theLockingInset()->InsetInInsetY();
829 if (pimpl_->screen_->FitManualCursor(text, this, x, y, asc, desc))
835 int BufferView::unlockInset(UpdatableInset * inset)
837 if (inset && theLockingInset() == inset) {
838 inset->InsetUnlock(this);
842 } else if (inset && theLockingInset() &&
843 theLockingInset()->UnlockInsetInInset(this, inset)) {
847 return bufferlist.unlockInset(inset);
851 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
853 if (!theLockingInset())
854 return; // shouldn't happen
855 if (kind == Undo::EDIT) // in this case insets would not be stored!
858 text->SetUndo(buffer(), kind,
860 ParFromPos(text->cursor.pos())->previous_,
862 ParFromPos(text->cursor.pos())->next_);
864 text->SetUndo(buffer(), kind,
865 text->cursor.par()->previous(),
866 text->cursor.par()->next());
871 void BufferView::updateInset(Inset * inset, bool mark_dirty)
876 // first check for locking insets
877 if (theLockingInset()) {
878 if (theLockingInset() == inset) {
879 if (text->UpdateInset(this, inset)) {
882 buffer()->markDirty();
887 } else if (theLockingInset()->UpdateInsetInInset(this,inset)) {
888 if (text->UpdateInset(this, theLockingInset())) {
891 buffer()->markDirty();
899 // then check the current buffer
902 update(text, BufferView::UPDATE);
903 if (text->UpdateInset(this, inset)) {
905 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
907 update(text, SELECT);
914 bool BufferView::ChangeInsets(Inset::Code code,
915 string const & from, string const & to)
918 LyXParagraph * par = buffer()->paragraph;
919 LyXCursor cursor = text->cursor;
920 LyXCursor tmpcursor = cursor;
922 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
923 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
925 cursor.par(tmpcursor.par());
926 cursor.pos(tmpcursor.pos());
931 for (LyXParagraph::inset_iterator it = par->inset_iterator_begin();
932 it != par->inset_iterator_end(); ++it) {
933 if ((*it)->LyxCode() == code) {
934 InsetCommand * inset = static_cast<InsetCommand *>(*it);
935 if (inset->getContents() == from) {
936 inset->setContents(to);
944 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
945 // this is possible now, since SetCursor takes
946 // care about footnotes
947 text->SetCursorIntern(this, par, 0);
948 text->RedoParagraphs(this, text->cursor,
949 text->cursor.par()->next());
950 text->FullRebreak(this);
957 // this is possible now, since SetCursor takes
958 // care about footnotes
959 text->SetCursorIntern(this, par, 0);
960 text->RedoParagraphs(this, text->cursor,
961 text->cursor.par()->next());
962 text->FullRebreak(this);
967 text->SetCursorIntern(this, cursor.par(), cursor.pos());
972 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
974 // Check if the label 'from' appears more than once
975 vector<string> labels = buffer()->getLabelList();
976 if (count(labels.begin(), labels.end(), from) > 1)
979 return ChangeInsets(Inset::REF_CODE, from, to);
983 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
986 vector<pair<string,string> > keys = buffer()->getBibkeyList();
987 if (count_if(keys.begin(), keys.end(),
988 lyx::equal_1st_in_pair<string,string>(from))
992 return ChangeInsets(Inset::CITE_CODE, from, to);
995 UpdatableInset * BufferView::theLockingInset() const
997 // If NULL is not allowed we should put an Assert here. (Lgb)
999 return text->the_locking_inset;
1004 void BufferView::theLockingInset(UpdatableInset * inset)
1006 text->the_locking_inset = inset;
1010 LyXText * BufferView::getLyXText() const
1012 if (theLockingInset()) {
1013 LyXText * txt = theLockingInset()->getLyXText(this, true);
1021 LyXText * BufferView::getParentText(Inset * inset) const
1023 if (inset->owner()) {
1024 LyXText * txt = inset->getLyXText(this);
1025 inset = inset->owner();
1026 while (inset && inset->getLyXText(this) == txt)
1027 inset = inset->owner();
1029 return inset->getLyXText(this);
1035 Language const * BufferView::getParentLanguage(Inset * inset) const
1037 LyXText * text = getParentText(inset);
1038 return text->cursor.par()->GetFontSettings(buffer()->params,
1039 text->cursor.pos()).language();