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;
44 // Inserts a file into current document
45 bool BufferView::insertLyXFile(string const & filen)
47 // Copyright CHT Software Service GmbH
50 // Insert a Lyxformat - file into current buffer
52 // Moved from lyx_cb.C (Lgb)
54 if (filen.empty()) return false;
56 string const fname = MakeAbsPath(filen);
58 // check if file exist
59 FileInfo const fi(fname);
62 WriteAlert(_("Error!"),
63 _("Specified file is unreadable: "),
64 MakeDisplayPath(fname, 50));
70 ifstream ifs(fname.c_str());
72 WriteAlert(_("Error!"),
73 _("Cannot open specified file: "),
74 MakeDisplayPath(fname, 50));
78 char const c = ifs.peek();
86 lyxerr.debug() << "Will insert file with header" << endl;
87 res = buffer()->readFile(lex, text->cursor.par());
89 lyxerr.debug() << "Will insert file without header" << endl;
90 res = buffer()->readLyXformat2(lex, text->cursor.par());
98 bool BufferView::removeAutoInsets()
100 LyXParagraph * par = buffer()->paragraph;
102 LyXCursor tmpcursor = text->cursor;
107 // this has to be done before the delete
109 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
111 text->SetCursor(this, cursor, par, 0);
112 if (par->AutoDeleteInsets()){
115 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
117 text->RedoParagraphs(this, cursor,
118 cursor.par()->Next());
119 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());
129 text->SetCursorIntern(this, tmpcursor.par(), tmpcursor.pos());
135 void BufferView::insertErrors(TeXErrors & terr)
137 // Save the cursor position
138 LyXCursor cursor = text->cursor;
141 // This is drastic, but it's the only fix, I could find. (Asger)
146 for (TeXErrors::Errors::const_iterator cit = terr.begin();
149 string const desctext((*cit).error_desc);
150 string const errortext((*cit).error_text);
151 string const msgtxt = desctext + '\n' + errortext;
152 int const errorrow = (*cit).error_in_line;
154 // Insert error string for row number
158 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
159 buffer()->texrow.increasePos(tmpid, tmppos);
162 LyXParagraph * texrowpar = 0;
165 texrowpar = text->FirstParagraph();
168 texrowpar = text->GetParFromID(tmpid);
174 InsetError * new_inset = new InsetError(msgtxt);
175 text->SetCursorIntern(this, texrowpar, tmppos);
176 text->InsertInset(this, new_inset);
177 text->FullRebreak(this);
179 // Restore the cursor position
180 text->SetCursorIntern(this, cursor.par(), cursor.pos());
184 void BufferView::setCursorFromRow(int row)
189 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
191 LyXParagraph * texrowpar;
194 texrowpar = text->FirstParagraph();
197 texrowpar = text->GetParFromID(tmpid);
199 text->SetCursor(this, texrowpar, tmppos);
203 bool BufferView::insertInset(Inset * inset, string const & lout,
206 // if we are in a locking inset we should try to insert the
207 // inset there otherwise this is a illegal function now
208 if (theLockingInset()) {
209 if (theLockingInset()->InsertInsetAllowed(inset))
210 return theLockingInset()->InsertInset(this, inset);
214 // not quite sure if we want this...
215 text->SetCursorParUndo(buffer());
220 update(BufferView::SELECT|BufferView::FITCUR);
221 text->BreakParagraph(this);
222 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
224 if (text->cursor.par()->Last()) {
225 text->CursorLeft(this);
227 text->BreakParagraph(this);
228 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
231 pair<bool, LyXTextClass::size_type> lres =
232 textclasslist.NumberOfLayout(buffer()->params
234 LyXTextClass::size_type lay;
235 if (lres.first != false) {
239 // layout not fount using default "Standard" (0)
243 text->SetLayout(this, lay);
245 text->SetParagraph(this, 0, 0,
247 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
251 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
253 text->current_font.setLatex(LyXFont::OFF);
256 text->InsertInset(this, inset);
257 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
259 text->UnFreezeUndo();
264 // Open and lock an updatable inset
265 bool BufferView::open_new_inset(UpdatableInset * new_inset, bool behind)
269 if (!insertInset(new_inset)) {
274 LyXFont & font = getLyXText()->real_current_font;
275 new_inset->Edit(this, new_inset->width(this, font), 0, 0);
277 new_inset->Edit(this, 0, 0, 0);
281 /* This is also a buffer property (ale) */
282 // Not so sure about that. a goto Label function can not be buffer local, just
283 // think how this will work in a multiwindo/buffer environment, all the
284 // cursors in all the views showing this buffer will move. (Lgb)
285 // OK, then no cursor action should be allowed in buffer. (ale)
286 bool BufferView::gotoLabel(string const & label)
289 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
290 it != buffer()->inset_iterator_end(); ++it) {
291 vector<string> labels = (*it)->getLabelList();
292 if (find(labels.begin(),labels.end(),label)
295 text->SetCursor(this, it.getPar(), it.getPos());
296 text->sel_cursor = text->cursor;
297 update(BufferView::SELECT|BufferView::FITCUR);
306 void BufferView::allFloats(char flag, char figmar)
308 if (!available()) return;
310 LyXCursor cursor = text->cursor;
313 && cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE
315 && cursor.par()->footnotekind != LyXParagraph::FOOTNOTE
316 && cursor.par()->footnotekind != LyXParagraph::MARGIN
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)))
328 LyXCursor tmpcursor = cursor;
329 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
330 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
332 LyXParagraph *par = buffer()->paragraph;
335 if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
337 && par->footnotekind != LyXParagraph::FOOTNOTE
338 && par->footnotekind != LyXParagraph::MARGIN)
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
349 && par->previous->footnoteflag !=
350 LyXParagraph::CLOSED_FOOTNOTE){ /* should be */
351 text->SetCursorIntern(this,
354 text->OpenFootnote(this);
358 if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
362 par->footnotekind != LyXParagraph::FOOTNOTE
364 par->footnotekind != LyXParagraph::MARGIN
369 par->footnotekind != LyXParagraph::FIG
371 par->footnotekind != LyXParagraph::TAB
373 par->footnotekind != LyXParagraph::WIDE_FIG
375 par->footnotekind != LyXParagraph::WIDE_TAB
377 par->footnotekind != LyXParagraph::ALGORITHM
381 text->SetCursorIntern(this, par, 0);
382 text->CloseFootnote(this);
388 text->SetCursorIntern(this, cursor.par(), cursor.pos());
395 void BufferView::insertNote()
397 InsetInfo * new_inset = new InsetInfo();
398 insertInset(new_inset);
399 new_inset->Edit(this, 0, 0, 0);
404 void BufferView::openStuff()
407 owner()->getMiniBuffer()->Set(_("Open/Close..."));
410 update(BufferView::SELECT|BufferView::FITCUR);
411 text->OpenStuff(this);
412 update(BufferView::SELECT|BufferView::FITCUR);
418 void BufferView::toggleFloat()
421 owner()->getMiniBuffer()->Set(_("Open/Close..."));
424 update(BufferView::SELECT|BufferView::FITCUR);
425 text->ToggleFootnote(this);
426 update(BufferView::SELECT|BufferView::FITCUR);
432 void BufferView::menuUndo()
435 owner()->getMiniBuffer()->Set(_("Undo"));
438 update(BufferView::SELECT|BufferView::FITCUR);
439 if (!text->TextUndo(this))
440 owner()->getMiniBuffer()->Set(_("No further undo information"));
442 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
448 void BufferView::menuRedo()
450 if (theLockingInset()) {
451 owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
456 owner()->getMiniBuffer()->Set(_("Redo"));
459 update(BufferView::SELECT|BufferView::FITCUR);
460 if (!text->TextRedo(this))
461 owner()->getMiniBuffer()->Set(_("No further redo information"));
463 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
469 void BufferView::hyphenationPoint()
473 update(BufferView::SELECT|BufferView::FITCUR);
474 InsetSpecialChar * new_inset =
475 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
476 insertInset(new_inset);
481 void BufferView::ldots()
485 update(BufferView::SELECT|BufferView::FITCUR);
486 InsetSpecialChar * new_inset =
487 new InsetSpecialChar(InsetSpecialChar::LDOTS);
488 insertInset(new_inset);
493 void BufferView::endOfSentenceDot()
497 update(BufferView::SELECT|BufferView::FITCUR);
498 InsetSpecialChar * new_inset =
499 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
500 insertInset(new_inset);
505 void BufferView::menuSeparator()
509 update(BufferView::SELECT|BufferView::FITCUR);
510 InsetSpecialChar * new_inset =
511 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
512 insertInset(new_inset);
517 void BufferView::newline()
521 update(BufferView::SELECT|BufferView::FITCUR);
522 text->InsertChar(this, LyXParagraph::META_NEWLINE);
523 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
528 void BufferView::protectedBlank()
532 update(BufferView::SELECT|BufferView::FITCUR);
533 InsetSpecialChar * new_inset =
534 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
535 insertInset(new_inset);
540 void BufferView::hfill()
544 update(BufferView::SELECT|BufferView::FITCUR);
545 text->InsertChar(this, LyXParagraph::META_HFILL);
546 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
550 void BufferView::copyEnvironment()
553 text->copyEnvironmentType();
554 // clear the selection, even if mark_set
556 text->ClearSelection();
557 update(BufferView::SELECT|BufferView::FITCUR);
558 owner()->getMiniBuffer()->Set(_("Paragraph environment type copied"));
563 void BufferView::pasteEnvironment()
566 text->pasteEnvironmentType(this);
567 owner()->getMiniBuffer()->Set(_("Paragraph environment type set"));
568 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
573 void BufferView::copy()
576 text->CopySelection(this);
577 // clear the selection, even if mark_set
579 text->ClearSelection();
580 update(BufferView::SELECT|BufferView::FITCUR);
581 owner()->getMiniBuffer()->Set(_("Copy"));
585 void BufferView::cut()
589 update(BufferView::SELECT|BufferView::FITCUR);
590 text->CutSelection(this);
591 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
592 owner()->getMiniBuffer()->Set(_("Cut"));
597 void BufferView::paste()
599 if (!available()) return;
601 owner()->getMiniBuffer()->Set(_("Paste"));
603 // clear the selection
605 text->ClearSelection();
606 update(BufferView::SELECT|BufferView::FITCUR);
609 text->PasteSelection(this);
610 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
612 // clear the selection
614 text->ClearSelection();
615 update(BufferView::SELECT|BufferView::FITCUR);
619 void BufferView::gotoInset(std::vector<Inset::Code> const & codes,
622 if (!available()) return;
626 update(BufferView::SELECT|BufferView::FITCUR);
630 text->cursor.par()->GetChar(text->cursor.pos()) == LyXParagraph::META_INSET) {
631 Inset const * inset = text->cursor.par()->GetInset(text->cursor.pos());
632 if (find(codes.begin(), codes.end(), inset->LyxCode())
635 static_cast<InsetCommand const *>(inset)->getContents();
638 if (!text->GotoNextInset(this, codes, contents)) {
639 if (text->cursor.pos()
640 || text->cursor.par() != text->FirstParagraph()) {
641 LyXCursor tmp = text->cursor;
642 text->cursor.par(text->FirstParagraph());
644 if (!text->GotoNextInset(this, codes, contents)) {
646 owner()->getMiniBuffer()->Set(_("No more insets"));
649 owner()->getMiniBuffer()->Set(_("No more insets"));
652 update(BufferView::SELECT|BufferView::FITCUR);
653 text->sel_cursor = text->cursor;
657 void BufferView::gotoInset(Inset::Code code, bool same_content)
659 gotoInset(vector<Inset::Code>(1, code), same_content);
663 void BufferView::insertCorrectQuote()
667 if (text->cursor.pos())
668 c = text->cursor.par()->GetChar(text->cursor.pos() - 1);
672 insertInset(new InsetQuotes(c, buffer()->params));
676 /* these functions are for the spellchecker */
677 string const BufferView::nextWord(float & value)
684 return text->SelectNextWord(this, value);
688 void BufferView::selectLastWord()
690 if (!available()) return;
694 text->SelectSelectedWord(this);
695 toggleSelection(false);
696 update(BufferView::SELECT|BufferView::FITCUR);
700 void BufferView::endOfSpellCheck()
702 if (!available()) return;
706 text->SelectSelectedWord(this);
707 text->ClearSelection();
708 update(BufferView::SELECT|BufferView::FITCUR);
712 void BufferView::replaceWord(string const & replacestring)
714 if (!available()) return;
717 update(BufferView::SELECT|BufferView::FITCUR);
719 /* clear the selection (if there is any) */
720 toggleSelection(false);
721 update(BufferView::SELECT|BufferView::FITCUR);
723 /* clear the selection (if there is any) */
724 toggleSelection(false);
725 text->ReplaceSelectionWithString(this, replacestring);
727 text->SetSelectionOverString(this, replacestring);
729 // Go back so that replacement string is also spellchecked
730 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
731 text->CursorLeft(this);
733 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
735 // End of spellchecker stuff
738 bool BufferView::lockInset(UpdatableInset * inset)
740 if (!theLockingInset() && inset) {
741 theLockingInset(inset);
744 return theLockingInset()->LockInsetInInset(this, inset);
750 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
752 if (theLockingInset() && available()) {
753 LyXCursor cursor = text->cursor;
754 if ((cursor.pos() - 1 >= 0) &&
755 (cursor.par()->GetChar(cursor.pos() - 1) ==
756 LyXParagraph::META_INSET) &&
757 (cursor.par()->GetInset(cursor.pos() - 1) ==
758 theLockingInset()->GetLockingInset()))
759 text->SetCursor(this, cursor,
760 cursor.par(), cursor.pos() - 1);
761 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
762 LyXText * txt = getLyXText();
763 if (theLockingInset()->GetLockingInset()->LyxCode() ==
765 (txt->real_current_font.language() !=
766 buffer()->params.language
767 || txt->real_current_font.isVisibleRightToLeft()
768 != buffer()->params.language->RightToLeft()))
769 shape = (txt->real_current_font.isVisibleRightToLeft())
770 ? LyXScreen::REVERSED_L_SHAPE
771 : LyXScreen::L_SHAPE;
772 y += cursor.y() + theLockingInset()->InsetInInsetY();
773 pimpl_->screen_->ShowManualCursor(text, x, y, asc, desc,
779 void BufferView::hideLockedInsetCursor()
781 if (theLockingInset() && available()) {
782 pimpl_->screen_->HideCursor();
787 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
789 if (theLockingInset() && available()) {
790 y += text->cursor.y() + theLockingInset()->InsetInInsetY();
791 if (pimpl_->screen_->FitManualCursor(text, this, x, y, asc, desc))
797 int BufferView::unlockInset(UpdatableInset * inset)
799 if (inset && theLockingInset() == inset) {
800 inset->InsetUnlock(this);
804 } else if (inset && theLockingInset() &&
805 theLockingInset()->UnlockInsetInInset(this, inset)) {
809 return bufferlist.unlockInset(inset);
813 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
815 if (!theLockingInset())
816 return; // shouldn't happen
817 if (kind == Undo::EDIT) // in this case insets would not be stored!
819 text->SetUndo(buffer(), kind,
822 ParFromPos(text->cursor.pos())->previous,
824 ParFromPos(text->cursor.pos())->next
826 text->cursor.par()->previous,
827 text->cursor.par()->next
833 void BufferView::updateInset(Inset * inset, bool mark_dirty)
838 // first check for locking insets
839 if (theLockingInset()) {
840 if (theLockingInset() == inset) {
841 if (text->UpdateInset(this, inset)) {
844 if (buffer()->isLyxClean())
845 owner()->getMiniBuffer()->
847 buffer()->markDirty();
852 } else if (theLockingInset()->UpdateInsetInInset(this,inset)) {
853 if (text->UpdateInset(this, theLockingInset())) {
856 if (buffer()->isLyxClean())
857 owner()->getMiniBuffer()->
859 buffer()->markDirty();
867 // then check the current buffer
870 update(BufferView::UPDATE);
871 if (text->UpdateInset(this, inset)) {
873 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
882 bool BufferView::ChangeRefs(string const & from, string const & to)
885 LyXParagraph * par = buffer()->paragraph;
886 LyXCursor cursor = text->cursor;
887 LyXCursor tmpcursor = cursor;
889 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
890 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
892 cursor.par(tmpcursor.par());
893 cursor.pos(tmpcursor.pos());
898 for (LyXParagraph::inset_iterator it = par->inset_iterator_begin();
899 it != par->inset_iterator_end(); ++it) {
900 if ((*it)->LyxCode() == Inset::REF_CODE) {
901 InsetCommand * inset = static_cast<InsetCommand *>(*it);
902 if (inset->getContents() == from) {
903 inset->setContents(to);
911 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
913 // this is possible now, since SetCursor takes
914 // care about footnotes
915 text->SetCursorIntern(this, par, 0);
916 text->RedoParagraphs(this, text->cursor,
917 text->cursor.par()->Next());
918 text->FullRebreak(this);
925 text->SetCursorIntern(this, cursor.par(), cursor.pos());
930 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
932 // Check if the label 'from' appears more than once
933 vector<string> labels = buffer()->getLabelList();
934 if (count(labels.begin(), labels.end(), from) > 1)
937 return ChangeRefs(from, to);
941 UpdatableInset * BufferView::theLockingInset() const
943 // If NULL is not allowed we should put an Assert here. (Lgb)
945 return text->the_locking_inset;
950 void BufferView::theLockingInset(UpdatableInset * inset)
952 text->the_locking_inset = inset;
956 LyXText * BufferView::getLyXText() const
958 if (theLockingInset()) {
959 LyXText * txt = theLockingInset()->getLyXText(this);
967 LyXText * BufferView::getParentText(Inset * inset) const
969 if (inset->owner()) {
970 LyXText * txt = inset->getLyXText(this);
971 inset = inset->owner();
972 while (inset && inset->getLyXText(this) == txt)
973 inset = inset->owner();
975 return inset->getLyXText(this);
980 Language const * BufferView::getParentLanguage(Inset * inset) const
982 LyXText * text = getParentText(inset);
983 return text->cursor.par()->GetFontSettings(buffer()->params,
984 text->cursor.pos()).language();