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
34 #include "support/lyxfunctional.h" //equal_1st_in_pair
36 extern BufferList bufferlist;
46 // Inserts a file into current document
47 bool BufferView::insertLyXFile(string const & filen)
49 // Copyright CHT Software Service GmbH
52 // Insert a Lyxformat - file into current buffer
54 // Moved from lyx_cb.C (Lgb)
56 if (filen.empty()) return false;
58 string const fname = MakeAbsPath(filen);
60 // check if file exist
61 FileInfo const fi(fname);
64 WriteAlert(_("Error!"),
65 _("Specified file is unreadable: "),
66 MakeDisplayPath(fname, 50));
72 ifstream ifs(fname.c_str());
74 WriteAlert(_("Error!"),
75 _("Cannot open specified file: "),
76 MakeDisplayPath(fname, 50));
80 char const c = ifs.peek();
88 lyxerr.debug() << "Will insert file with header" << endl;
89 res = buffer()->readFile(lex, text->cursor.par());
91 lyxerr.debug() << "Will insert file without header" << endl;
92 res = buffer()->readLyXformat2(lex, text->cursor.par());
100 bool BufferView::removeAutoInsets()
102 LyXParagraph * par = buffer()->paragraph;
104 LyXCursor tmpcursor = text->cursor;
109 // this has to be done before the delete
111 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
113 text->SetCursor(this, cursor, par, 0);
114 if (par->AutoDeleteInsets()){
117 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
119 text->RedoParagraphs(this, cursor,
120 cursor.par()->Next());
121 text->FullRebreak(this);
128 // avoid forbidden cursor positions caused by error removing
129 if (tmpcursor.pos() > tmpcursor.par()->Last())
130 tmpcursor.pos(tmpcursor.par()->Last());
131 text->SetCursorIntern(this, tmpcursor.par(), tmpcursor.pos());
137 void BufferView::insertErrors(TeXErrors & terr)
139 // Save the cursor position
140 LyXCursor cursor = text->cursor;
143 // This is drastic, but it's the only fix, I could find. (Asger)
148 for (TeXErrors::Errors::const_iterator cit = terr.begin();
151 string const desctext((*cit).error_desc);
152 string const errortext((*cit).error_text);
153 string const msgtxt = desctext + '\n' + errortext;
154 int const errorrow = (*cit).error_in_line;
156 // Insert error string for row number
160 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
161 buffer()->texrow.increasePos(tmpid, tmppos);
164 LyXParagraph * texrowpar = 0;
167 texrowpar = text->FirstParagraph();
170 texrowpar = text->GetParFromID(tmpid);
176 InsetError * new_inset = new InsetError(msgtxt);
177 text->SetCursorIntern(this, texrowpar, tmppos);
178 text->InsertInset(this, new_inset);
179 text->FullRebreak(this);
181 // Restore the cursor position
182 text->SetCursorIntern(this, cursor.par(), cursor.pos());
186 void BufferView::setCursorFromRow(int row)
191 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
193 LyXParagraph * texrowpar;
196 texrowpar = text->FirstParagraph();
199 texrowpar = text->GetParFromID(tmpid);
201 text->SetCursor(this, texrowpar, tmppos);
205 bool BufferView::insertInset(Inset * inset, string const & lout,
208 // if we are in a locking inset we should try to insert the
209 // inset there otherwise this is a illegal function now
210 if (theLockingInset()) {
211 if (theLockingInset()->InsertInsetAllowed(inset))
212 return theLockingInset()->InsertInset(this, inset);
216 // not quite sure if we want this...
217 text->SetCursorParUndo(buffer());
222 update(BufferView::SELECT|BufferView::FITCUR);
223 text->BreakParagraph(this);
224 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
226 if (text->cursor.par()->Last()) {
227 text->CursorLeft(this);
229 text->BreakParagraph(this);
230 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
233 pair<bool, LyXTextClass::size_type> lres =
234 textclasslist.NumberOfLayout(buffer()->params
236 LyXTextClass::size_type lay;
237 if (lres.first != false) {
241 // layout not fount using default "Standard" (0)
245 text->SetLayout(this, lay);
247 text->SetParagraph(this, 0, 0,
249 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
253 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
255 text->current_font.setLatex(LyXFont::OFF);
258 text->InsertInset(this, inset);
259 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
261 text->UnFreezeUndo();
266 // Open and lock an updatable inset
267 bool BufferView::open_new_inset(UpdatableInset * new_inset, bool behind)
271 if (!insertInset(new_inset)) {
276 LyXFont & font = getLyXText()->real_current_font;
277 new_inset->Edit(this, new_inset->width(this, font), 0, 0);
279 new_inset->Edit(this, 0, 0, 0);
283 /* This is also a buffer property (ale) */
284 // Not so sure about that. a goto Label function can not be buffer local, just
285 // think how this will work in a multiwindo/buffer environment, all the
286 // cursors in all the views showing this buffer will move. (Lgb)
287 // OK, then no cursor action should be allowed in buffer. (ale)
288 bool BufferView::gotoLabel(string const & label)
291 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
292 it != buffer()->inset_iterator_end(); ++it) {
293 vector<string> labels = (*it)->getLabelList();
294 if (find(labels.begin(),labels.end(),label)
297 text->SetCursor(this, it.getPar(), it.getPos());
298 text->sel_cursor = text->cursor;
299 update(BufferView::SELECT|BufferView::FITCUR);
308 void BufferView::allFloats(char flag, char figmar)
310 if (!available()) return;
312 LyXCursor cursor = text->cursor;
315 && cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE
317 && cursor.par()->footnotekind != LyXParagraph::FOOTNOTE
318 && cursor.par()->footnotekind != LyXParagraph::MARGIN
321 && cursor.par()->footnotekind != LyXParagraph::FIG
322 && cursor.par()->footnotekind != LyXParagraph::TAB
323 && cursor.par()->footnotekind != LyXParagraph::WIDE_FIG
324 && cursor.par()->footnotekind != LyXParagraph::WIDE_TAB
325 && cursor.par()->footnotekind != LyXParagraph::ALGORITHM)))
330 LyXCursor tmpcursor = cursor;
331 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
332 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
334 LyXParagraph *par = buffer()->paragraph;
337 if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
339 && par->footnotekind != LyXParagraph::FOOTNOTE
340 && par->footnotekind != LyXParagraph::MARGIN)
342 && par->footnotekind != LyXParagraph::FIG
343 && par->footnotekind != LyXParagraph::TAB
344 && par->footnotekind != LyXParagraph::WIDE_FIG
345 && par->footnotekind != LyXParagraph::WIDE_TAB
346 && par->footnotekind != LyXParagraph::ALGORITHM
351 && par->previous->footnoteflag !=
352 LyXParagraph::CLOSED_FOOTNOTE){ /* should be */
353 text->SetCursorIntern(this,
356 text->OpenFootnote(this);
360 if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
364 par->footnotekind != LyXParagraph::FOOTNOTE
366 par->footnotekind != LyXParagraph::MARGIN
371 par->footnotekind != LyXParagraph::FIG
373 par->footnotekind != LyXParagraph::TAB
375 par->footnotekind != LyXParagraph::WIDE_FIG
377 par->footnotekind != LyXParagraph::WIDE_TAB
379 par->footnotekind != LyXParagraph::ALGORITHM
383 text->SetCursorIntern(this, par, 0);
384 text->CloseFootnote(this);
390 text->SetCursorIntern(this, cursor.par(), cursor.pos());
397 void BufferView::insertNote()
399 InsetInfo * new_inset = new InsetInfo();
400 insertInset(new_inset);
401 new_inset->Edit(this, 0, 0, 0);
406 void BufferView::openStuff()
409 owner()->getMiniBuffer()->Set(_("Open/Close..."));
412 update(BufferView::SELECT|BufferView::FITCUR);
413 text->OpenStuff(this);
414 update(BufferView::SELECT|BufferView::FITCUR);
420 void BufferView::toggleFloat()
423 owner()->getMiniBuffer()->Set(_("Open/Close..."));
426 update(BufferView::SELECT|BufferView::FITCUR);
427 text->ToggleFootnote(this);
428 update(BufferView::SELECT|BufferView::FITCUR);
434 void BufferView::menuUndo()
437 owner()->getMiniBuffer()->Set(_("Undo"));
440 update(BufferView::SELECT|BufferView::FITCUR);
441 if (!text->TextUndo(this))
442 owner()->getMiniBuffer()->Set(_("No further undo information"));
444 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
450 void BufferView::menuRedo()
452 if (theLockingInset()) {
453 owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
458 owner()->getMiniBuffer()->Set(_("Redo"));
461 update(BufferView::SELECT|BufferView::FITCUR);
462 if (!text->TextRedo(this))
463 owner()->getMiniBuffer()->Set(_("No further redo information"));
465 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
471 void BufferView::hyphenationPoint()
475 update(BufferView::SELECT|BufferView::FITCUR);
476 InsetSpecialChar * new_inset =
477 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
478 insertInset(new_inset);
483 void BufferView::ldots()
487 update(BufferView::SELECT|BufferView::FITCUR);
488 InsetSpecialChar * new_inset =
489 new InsetSpecialChar(InsetSpecialChar::LDOTS);
490 insertInset(new_inset);
495 void BufferView::endOfSentenceDot()
499 update(BufferView::SELECT|BufferView::FITCUR);
500 InsetSpecialChar * new_inset =
501 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
502 insertInset(new_inset);
507 void BufferView::menuSeparator()
511 update(BufferView::SELECT|BufferView::FITCUR);
512 InsetSpecialChar * new_inset =
513 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
514 insertInset(new_inset);
519 void BufferView::newline()
523 update(BufferView::SELECT|BufferView::FITCUR);
524 text->InsertChar(this, LyXParagraph::META_NEWLINE);
525 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
530 void BufferView::protectedBlank()
534 update(BufferView::SELECT|BufferView::FITCUR);
535 InsetSpecialChar * new_inset =
536 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
537 insertInset(new_inset);
542 void BufferView::hfill()
546 update(BufferView::SELECT|BufferView::FITCUR);
547 text->InsertChar(this, LyXParagraph::META_HFILL);
548 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
552 void BufferView::copyEnvironment()
555 text->copyEnvironmentType();
556 // clear the selection, even if mark_set
558 text->ClearSelection();
559 update(BufferView::SELECT|BufferView::FITCUR);
560 owner()->getMiniBuffer()->Set(_("Paragraph environment type copied"));
565 void BufferView::pasteEnvironment()
568 text->pasteEnvironmentType(this);
569 owner()->getMiniBuffer()->Set(_("Paragraph environment type set"));
570 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
575 void BufferView::copy()
578 text->CopySelection(this);
579 // clear the selection, even if mark_set
581 text->ClearSelection();
582 update(BufferView::SELECT|BufferView::FITCUR);
583 owner()->getMiniBuffer()->Set(_("Copy"));
587 void BufferView::cut()
591 update(BufferView::SELECT|BufferView::FITCUR);
592 text->CutSelection(this);
593 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
594 owner()->getMiniBuffer()->Set(_("Cut"));
599 void BufferView::paste()
601 if (!available()) return;
603 owner()->getMiniBuffer()->Set(_("Paste"));
605 // clear the selection
607 text->ClearSelection();
608 update(BufferView::SELECT|BufferView::FITCUR);
611 text->PasteSelection(this);
612 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
614 // clear the selection
616 text->ClearSelection();
617 update(BufferView::SELECT|BufferView::FITCUR);
621 void BufferView::gotoInset(std::vector<Inset::Code> const & codes,
624 if (!available()) return;
628 update(BufferView::SELECT|BufferView::FITCUR);
632 text->cursor.par()->GetChar(text->cursor.pos()) == LyXParagraph::META_INSET) {
633 Inset const * inset = text->cursor.par()->GetInset(text->cursor.pos());
634 if (find(codes.begin(), codes.end(), inset->LyxCode())
637 static_cast<InsetCommand const *>(inset)->getContents();
640 if (!text->GotoNextInset(this, codes, contents)) {
641 if (text->cursor.pos()
642 || text->cursor.par() != text->FirstParagraph()) {
643 LyXCursor tmp = text->cursor;
644 text->cursor.par(text->FirstParagraph());
646 if (!text->GotoNextInset(this, codes, contents)) {
648 owner()->getMiniBuffer()->Set(_("No more insets"));
651 owner()->getMiniBuffer()->Set(_("No more insets"));
654 update(BufferView::SELECT|BufferView::FITCUR);
655 text->sel_cursor = text->cursor;
659 void BufferView::gotoInset(Inset::Code code, bool same_content)
661 gotoInset(vector<Inset::Code>(1, code), same_content);
665 void BufferView::insertCorrectQuote()
669 if (text->cursor.pos())
670 c = text->cursor.par()->GetChar(text->cursor.pos() - 1);
674 insertInset(new InsetQuotes(c, buffer()->params));
678 /* these functions are for the spellchecker */
679 string const BufferView::nextWord(float & value)
686 return text->SelectNextWord(this, value);
690 void BufferView::selectLastWord()
692 if (!available()) return;
696 text->SelectSelectedWord(this);
697 toggleSelection(false);
698 update(BufferView::SELECT|BufferView::FITCUR);
702 void BufferView::endOfSpellCheck()
704 if (!available()) return;
708 text->SelectSelectedWord(this);
709 text->ClearSelection();
710 update(BufferView::SELECT|BufferView::FITCUR);
714 void BufferView::replaceWord(string const & replacestring)
716 if (!available()) return;
719 update(BufferView::SELECT|BufferView::FITCUR);
721 /* clear the selection (if there is any) */
722 toggleSelection(false);
723 update(BufferView::SELECT|BufferView::FITCUR);
725 /* clear the selection (if there is any) */
726 toggleSelection(false);
727 text->ReplaceSelectionWithString(this, replacestring);
729 text->SetSelectionOverString(this, replacestring);
731 // Go back so that replacement string is also spellchecked
732 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
733 text->CursorLeft(this);
735 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
737 // End of spellchecker stuff
740 bool BufferView::lockInset(UpdatableInset * inset)
742 if (!theLockingInset() && inset) {
743 theLockingInset(inset);
746 return theLockingInset()->LockInsetInInset(this, inset);
752 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
754 if (theLockingInset() && available()) {
755 LyXCursor cursor = text->cursor;
756 if ((cursor.pos() - 1 >= 0) &&
757 (cursor.par()->GetChar(cursor.pos() - 1) ==
758 LyXParagraph::META_INSET) &&
759 (cursor.par()->GetInset(cursor.pos() - 1) ==
760 theLockingInset()->GetLockingInset()))
761 text->SetCursor(this, cursor,
762 cursor.par(), cursor.pos() - 1);
763 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
764 LyXText * txt = getLyXText();
765 if (theLockingInset()->GetLockingInset()->LyxCode() ==
767 (txt->real_current_font.language() !=
768 buffer()->params.language
769 || txt->real_current_font.isVisibleRightToLeft()
770 != buffer()->params.language->RightToLeft()))
771 shape = (txt->real_current_font.isVisibleRightToLeft())
772 ? LyXScreen::REVERSED_L_SHAPE
773 : LyXScreen::L_SHAPE;
774 y += cursor.y() + theLockingInset()->InsetInInsetY();
775 pimpl_->screen_->ShowManualCursor(text, x, y, asc, desc,
781 void BufferView::hideLockedInsetCursor()
783 if (theLockingInset() && available()) {
784 pimpl_->screen_->HideCursor();
789 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
791 if (theLockingInset() && available()) {
792 y += text->cursor.y() + theLockingInset()->InsetInInsetY();
793 if (pimpl_->screen_->FitManualCursor(text, this, x, y, asc, desc))
799 int BufferView::unlockInset(UpdatableInset * inset)
801 if (inset && theLockingInset() == inset) {
802 inset->InsetUnlock(this);
806 } else if (inset && theLockingInset() &&
807 theLockingInset()->UnlockInsetInInset(this, inset)) {
811 return bufferlist.unlockInset(inset);
815 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
817 if (!theLockingInset())
818 return; // shouldn't happen
819 if (kind == Undo::EDIT) // in this case insets would not be stored!
821 text->SetUndo(buffer(), kind,
824 ParFromPos(text->cursor.pos())->previous,
826 ParFromPos(text->cursor.pos())->next
828 text->cursor.par()->previous,
829 text->cursor.par()->next
835 void BufferView::updateInset(Inset * inset, bool mark_dirty)
840 // first check for locking insets
841 if (theLockingInset()) {
842 if (theLockingInset() == inset) {
843 if (text->UpdateInset(this, inset)) {
846 if (buffer()->isLyxClean())
847 owner()->getMiniBuffer()->
849 buffer()->markDirty();
854 } else if (theLockingInset()->UpdateInsetInInset(this,inset)) {
855 if (text->UpdateInset(this, theLockingInset())) {
858 if (buffer()->isLyxClean())
859 owner()->getMiniBuffer()->
861 buffer()->markDirty();
869 // then check the current buffer
872 update(BufferView::UPDATE);
873 if (text->UpdateInset(this, inset)) {
875 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
884 bool BufferView::ChangeInsets(Inset::Code code, string const & from, string const & to)
887 LyXParagraph * par = buffer()->paragraph;
888 LyXCursor cursor = text->cursor;
889 LyXCursor tmpcursor = cursor;
891 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
892 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
894 cursor.par(tmpcursor.par());
895 cursor.pos(tmpcursor.pos());
900 for (LyXParagraph::inset_iterator it = par->inset_iterator_begin();
901 it != par->inset_iterator_end(); ++it) {
902 if ((*it)->LyxCode() == code) {
903 InsetCommand * inset = static_cast<InsetCommand *>(*it);
904 if (inset->getContents() == from) {
905 inset->setContents(to);
913 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
915 // this is possible now, since SetCursor takes
916 // care about footnotes
917 text->SetCursorIntern(this, par, 0);
918 text->RedoParagraphs(this, text->cursor,
919 text->cursor.par()->Next());
920 text->FullRebreak(this);
927 text->SetCursorIntern(this, cursor.par(), cursor.pos());
932 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
934 // Check if the label 'from' appears more than once
935 vector<string> labels = buffer()->getLabelList();
936 if (count(labels.begin(), labels.end(), from) > 1)
939 return ChangeInsets(Inset::REF_CODE, from, to);
943 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
946 vector<pair<string,string> > keys = buffer()->getBibkeyList();
947 if (count_if(keys.begin(), keys.end(),
948 equal_1st_in_pair<string,string>(from))
952 return ChangeInsets(Inset::CITE_CODE, from, to);
955 UpdatableInset * BufferView::theLockingInset() const
957 // If NULL is not allowed we should put an Assert here. (Lgb)
959 return text->the_locking_inset;
964 void BufferView::theLockingInset(UpdatableInset * inset)
966 text->the_locking_inset = inset;
970 LyXText * BufferView::getLyXText() const
972 if (theLockingInset()) {
973 LyXText * txt = theLockingInset()->getLyXText(this);
981 LyXText * BufferView::getParentText(Inset * inset) const
983 if (inset->owner()) {
984 LyXText * txt = inset->getLyXText(this);
985 inset = inset->owner();
986 while (inset && inset->getLyXText(this) == txt)
987 inset = inset->owner();
989 return inset->getLyXText(this);
994 Language const * BufferView::getParentLanguage(Inset * inset) const
996 LyXText * text = getParentText(inset);
997 return text->cursor.par()->GetFontSettings(buffer()->params,
998 text->cursor.pos()).language();