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::gotoNote()
621 if (!available()) return;
625 update(BufferView::SELECT|BufferView::FITCUR);
628 if (!text->GotoNextNote(this)) {
629 if (text->cursor.pos()
630 || text->cursor.par() != text->FirstParagraph()) {
632 text->cursor.par(text->FirstParagraph());
634 if (!text->GotoNextNote(this)) {
636 owner()->getMiniBuffer()->Set(_("No more notes"));
640 owner()->getMiniBuffer()->Set(_("No more notes"));
644 update(BufferView::SELECT|BufferView::FITCUR);
645 text->sel_cursor = text->cursor;
649 void BufferView::insertCorrectQuote()
653 if (text->cursor.pos())
654 c = text->cursor.par()->GetChar(text->cursor.pos() - 1);
658 insertInset(new InsetQuotes(c, buffer()->params));
662 /* these functions are for the spellchecker */
663 string const BufferView::nextWord(float & value)
670 return text->SelectNextWord(this, value);
674 void BufferView::selectLastWord()
676 if (!available()) return;
680 text->SelectSelectedWord(this);
681 toggleSelection(false);
682 update(BufferView::SELECT|BufferView::FITCUR);
686 void BufferView::endOfSpellCheck()
688 if (!available()) return;
692 text->SelectSelectedWord(this);
693 text->ClearSelection();
694 update(BufferView::SELECT|BufferView::FITCUR);
698 void BufferView::replaceWord(string const & replacestring)
700 if (!available()) return;
703 update(BufferView::SELECT|BufferView::FITCUR);
705 /* clear the selection (if there is any) */
706 toggleSelection(false);
707 update(BufferView::SELECT|BufferView::FITCUR);
709 /* clear the selection (if there is any) */
710 toggleSelection(false);
711 text->ReplaceSelectionWithString(this, replacestring);
713 text->SetSelectionOverString(this, replacestring);
715 // Go back so that replacement string is also spellchecked
716 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
717 text->CursorLeft(this);
719 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
721 // End of spellchecker stuff
724 bool BufferView::lockInset(UpdatableInset * inset)
726 if (!theLockingInset() && inset) {
727 theLockingInset(inset);
730 return theLockingInset()->LockInsetInInset(this, inset);
736 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
738 if (theLockingInset() && 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 theLockingInset()->GetLockingInset()))
745 text->SetCursor(this, cursor,
746 cursor.par(), cursor.pos() - 1);
747 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
748 LyXText * txt = getLyXText();
749 if (theLockingInset()->GetLockingInset()->LyxCode() ==
751 (txt->real_current_font.language() !=
752 buffer()->params.language
753 || txt->real_current_font.isVisibleRightToLeft()
754 != buffer()->params.language->RightToLeft()))
755 shape = (txt->real_current_font.isVisibleRightToLeft())
756 ? LyXScreen::REVERSED_L_SHAPE
757 : LyXScreen::L_SHAPE;
758 y += cursor.y() + theLockingInset()->InsetInInsetY();
759 pimpl_->screen_->ShowManualCursor(text, x, y, asc, desc,
765 void BufferView::hideLockedInsetCursor()
767 if (theLockingInset() && available()) {
768 pimpl_->screen_->HideCursor();
773 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
775 if (theLockingInset() && available()) {
776 y += text->cursor.y() + theLockingInset()->InsetInInsetY();
777 if (pimpl_->screen_->FitManualCursor(text, this, x, y, asc, desc))
783 int BufferView::unlockInset(UpdatableInset * inset)
785 if (inset && theLockingInset() == inset) {
786 inset->InsetUnlock(this);
790 } else if (inset && theLockingInset() &&
791 theLockingInset()->UnlockInsetInInset(this, inset)) {
795 return bufferlist.unlockInset(inset);
799 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
801 if (!theLockingInset())
802 return; // shouldn't happen
803 if (kind == Undo::EDIT) // in this case insets would not be stored!
805 text->SetUndo(buffer(), kind,
808 ParFromPos(text->cursor.pos())->previous,
810 ParFromPos(text->cursor.pos())->next
812 text->cursor.par()->previous,
813 text->cursor.par()->next
819 void BufferView::updateInset(Inset * inset, bool mark_dirty)
824 // first check for locking insets
825 if (theLockingInset()) {
826 if (theLockingInset() == inset) {
827 if (text->UpdateInset(this, inset)) {
830 if (buffer()->isLyxClean())
831 owner()->getMiniBuffer()->
833 buffer()->markDirty();
838 } else if (theLockingInset()->UpdateInsetInInset(this,inset)) {
839 if (text->UpdateInset(this, theLockingInset())) {
842 if (buffer()->isLyxClean())
843 owner()->getMiniBuffer()->
845 buffer()->markDirty();
853 // then check the current buffer
856 update(BufferView::UPDATE);
857 if (text->UpdateInset(this, inset)) {
859 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
868 bool BufferView::ChangeRefs(string const & from, string const & to)
871 LyXParagraph * par = buffer()->paragraph;
872 LyXCursor cursor = text->cursor;
873 LyXCursor tmpcursor = cursor;
875 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
876 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
878 cursor.par(tmpcursor.par());
879 cursor.pos(tmpcursor.pos());
884 for (LyXParagraph::inset_iterator it = par->inset_iterator_begin();
885 it != par->inset_iterator_end(); ++it) {
886 if ((*it)->LyxCode() == Inset::REF_CODE) {
887 InsetCommand * inset = static_cast<InsetCommand *>(*it);
888 if (inset->getContents() == from) {
889 inset->setContents(to);
897 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
899 // this is possible now, since SetCursor takes
900 // care about footnotes
901 text->SetCursorIntern(this, par, 0);
902 text->RedoParagraphs(this, text->cursor,
903 text->cursor.par()->Next());
904 text->FullRebreak(this);
911 text->SetCursorIntern(this, cursor.par(), cursor.pos());
916 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
918 // Check if the label 'from' appears more than once
919 vector<string> labels = buffer()->getLabelList();
920 if (count(labels.begin(), labels.end(), from) > 1)
923 return ChangeRefs(from, to);
927 UpdatableInset * BufferView::theLockingInset() const
929 // If NULL is not allowed we should put an Assert here. (Lgb)
931 return text->the_locking_inset;
936 void BufferView::theLockingInset(UpdatableInset * inset)
938 text->the_locking_inset = inset;
942 LyXText * BufferView::getLyXText() const
944 if (theLockingInset()) {
945 LyXText * txt = theLockingInset()->getLyXText(this);
953 LyXText * BufferView::getParentText(Inset * inset) const
955 if (inset->owner()) {
956 LyXText * txt = inset->getLyXText(this);
957 inset = inset->owner();
958 while (inset && inset->getLyXText(this) == txt)
959 inset = inset->owner();
961 return inset->getLyXText(this);
966 Language const * BufferView::getParentLanguage(Inset * inset) const
968 LyXText * text = getParentText(inset);
969 return text->cursor.par()->GetFontSettings(buffer()->params,
970 text->cursor.pos()).language();