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(text, BufferView::SELECT|BufferView::FITCUR);
223 text->BreakParagraph(this);
224 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
226 if (text->cursor.par()->Last()) {
227 text->CursorLeft(this);
229 text->BreakParagraph(this);
230 update(text, 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(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
255 text->current_font.setLatex(LyXFont::OFF);
258 text->InsertInset(this, inset);
259 update(text, 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(text, 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(text, BufferView::SELECT|BufferView::FITCUR);
413 text->OpenStuff(this);
414 update(text, BufferView::SELECT|BufferView::FITCUR);
420 void BufferView::toggleFloat()
423 owner()->getMiniBuffer()->Set(_("Open/Close..."));
426 update(text, BufferView::SELECT|BufferView::FITCUR);
427 text->ToggleFootnote(this);
428 update(text, BufferView::SELECT|BufferView::FITCUR);
434 void BufferView::menuUndo()
437 owner()->getMiniBuffer()->Set(_("Undo"));
440 update(text, BufferView::SELECT|BufferView::FITCUR);
441 if (!text->TextUndo(this))
442 owner()->getMiniBuffer()->Set(_("No further undo information"));
444 update(text, 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(text, BufferView::SELECT|BufferView::FITCUR);
462 if (!text->TextRedo(this))
463 owner()->getMiniBuffer()->Set(_("No further redo information"));
465 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
471 void BufferView::hyphenationPoint()
475 update(text, BufferView::SELECT|BufferView::FITCUR);
476 InsetSpecialChar * new_inset =
477 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
478 insertInset(new_inset);
483 void BufferView::ldots()
487 update(text, BufferView::SELECT|BufferView::FITCUR);
488 InsetSpecialChar * new_inset =
489 new InsetSpecialChar(InsetSpecialChar::LDOTS);
490 insertInset(new_inset);
495 void BufferView::endOfSentenceDot()
499 update(text, 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(text, BufferView::SELECT|BufferView::FITCUR);
512 InsetSpecialChar * new_inset =
513 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
514 insertInset(new_inset);
519 void BufferView::newline()
523 update(text, BufferView::SELECT|BufferView::FITCUR);
524 text->InsertChar(this, LyXParagraph::META_NEWLINE);
525 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
530 void BufferView::protectedBlank(LyXText * lt)
534 update(lt, BufferView::SELECT|BufferView::FITCUR);
535 InsetSpecialChar * new_inset =
536 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
537 if (!insertInset(new_inset))
540 updateInset(new_inset, true);
545 void BufferView::hfill()
549 update(text, BufferView::SELECT|BufferView::FITCUR);
550 text->InsertChar(this, LyXParagraph::META_HFILL);
551 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
555 void BufferView::copyEnvironment()
558 text->copyEnvironmentType();
559 // clear the selection, even if mark_set
561 text->ClearSelection(this);
562 update(text, BufferView::SELECT|BufferView::FITCUR);
563 owner()->getMiniBuffer()->Set(_("Paragraph environment type copied"));
568 void BufferView::pasteEnvironment()
571 text->pasteEnvironmentType(this);
572 owner()->getMiniBuffer()->Set(_("Paragraph environment type set"));
573 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
578 void BufferView::copy()
581 text->CopySelection(this);
582 // clear the selection, even if mark_set
584 text->ClearSelection(this);
585 update(text, BufferView::SELECT|BufferView::FITCUR);
586 owner()->getMiniBuffer()->Set(_("Copy"));
590 void BufferView::cut()
594 update(text, BufferView::SELECT|BufferView::FITCUR);
595 text->CutSelection(this);
596 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
597 owner()->getMiniBuffer()->Set(_("Cut"));
602 void BufferView::paste()
604 if (!available()) return;
606 owner()->getMiniBuffer()->Set(_("Paste"));
608 // clear the selection
610 text->ClearSelection(this);
611 update(text, BufferView::SELECT|BufferView::FITCUR);
614 text->PasteSelection(this);
615 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
617 // clear the selection
619 text->ClearSelection(this);
620 update(text, BufferView::SELECT|BufferView::FITCUR);
624 void BufferView::gotoInset(std::vector<Inset::Code> const & codes,
627 if (!available()) return;
631 update(text, BufferView::SELECT|BufferView::FITCUR);
635 text->cursor.par()->GetChar(text->cursor.pos()) == LyXParagraph::META_INSET) {
636 Inset const * inset = text->cursor.par()->GetInset(text->cursor.pos());
637 if (find(codes.begin(), codes.end(), inset->LyxCode())
640 static_cast<InsetCommand const *>(inset)->getContents();
643 if (!text->GotoNextInset(this, codes, contents)) {
644 if (text->cursor.pos()
645 || text->cursor.par() != text->FirstParagraph()) {
646 LyXCursor tmp = text->cursor;
647 text->cursor.par(text->FirstParagraph());
649 if (!text->GotoNextInset(this, codes, contents)) {
651 owner()->getMiniBuffer()->Set(_("No more insets"));
654 owner()->getMiniBuffer()->Set(_("No more insets"));
657 update(text, BufferView::SELECT|BufferView::FITCUR);
658 text->sel_cursor = text->cursor;
662 void BufferView::gotoInset(Inset::Code code, bool same_content)
664 gotoInset(vector<Inset::Code>(1, code), same_content);
668 void BufferView::insertCorrectQuote()
672 if (text->cursor.pos())
673 c = text->cursor.par()->GetChar(text->cursor.pos() - 1);
677 insertInset(new InsetQuotes(c, buffer()->params));
681 /* these functions are for the spellchecker */
682 string const BufferView::nextWord(float & value)
689 return text->SelectNextWord(this, value);
693 void BufferView::selectLastWord()
695 if (!available()) return;
699 text->SelectSelectedWord(this);
700 toggleSelection(false);
701 update(text, BufferView::SELECT|BufferView::FITCUR);
705 void BufferView::endOfSpellCheck()
707 if (!available()) return;
711 text->SelectSelectedWord(this);
712 text->ClearSelection(this);
713 update(text, BufferView::SELECT|BufferView::FITCUR);
717 void BufferView::replaceWord(string const & replacestring)
719 if (!available()) return;
722 update(text, BufferView::SELECT|BufferView::FITCUR);
724 /* clear the selection (if there is any) */
725 toggleSelection(false);
726 update(text, BufferView::SELECT|BufferView::FITCUR);
728 /* clear the selection (if there is any) */
729 toggleSelection(false);
730 text->ReplaceSelectionWithString(this, replacestring);
732 text->SetSelectionOverString(this, replacestring);
734 // Go back so that replacement string is also spellchecked
735 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
736 text->CursorLeft(this);
738 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
740 // End of spellchecker stuff
743 bool BufferView::lockInset(UpdatableInset * inset)
745 if (!theLockingInset() && inset) {
746 theLockingInset(inset);
749 return theLockingInset()->LockInsetInInset(this, inset);
755 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
757 if (theLockingInset() && available()) {
758 LyXCursor cursor = text->cursor;
759 if ((cursor.pos() - 1 >= 0) &&
760 (cursor.par()->GetChar(cursor.pos() - 1) ==
761 LyXParagraph::META_INSET) &&
762 (cursor.par()->GetInset(cursor.pos() - 1) ==
763 theLockingInset()->GetLockingInset()))
764 text->SetCursor(this, cursor,
765 cursor.par(), cursor.pos() - 1);
766 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
767 LyXText * txt = getLyXText();
768 if (theLockingInset()->GetLockingInset()->LyxCode() ==
770 (txt->real_current_font.language() !=
771 buffer()->params.language
772 || txt->real_current_font.isVisibleRightToLeft()
773 != buffer()->params.language->RightToLeft()))
774 shape = (txt->real_current_font.isVisibleRightToLeft())
775 ? LyXScreen::REVERSED_L_SHAPE
776 : LyXScreen::L_SHAPE;
777 y += cursor.y() + theLockingInset()->InsetInInsetY();
778 pimpl_->screen_->ShowManualCursor(text, x, y, asc, desc,
784 void BufferView::hideLockedInsetCursor()
786 if (theLockingInset() && available()) {
787 pimpl_->screen_->HideCursor();
792 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
794 if (theLockingInset() && available()) {
795 y += text->cursor.y() + theLockingInset()->InsetInInsetY();
796 if (pimpl_->screen_->FitManualCursor(text, this, x, y, asc, desc))
802 int BufferView::unlockInset(UpdatableInset * inset)
804 if (inset && theLockingInset() == inset) {
805 inset->InsetUnlock(this);
809 } else if (inset && theLockingInset() &&
810 theLockingInset()->UnlockInsetInInset(this, inset)) {
814 return bufferlist.unlockInset(inset);
818 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
820 if (!theLockingInset())
821 return; // shouldn't happen
822 if (kind == Undo::EDIT) // in this case insets would not be stored!
824 text->SetUndo(buffer(), kind,
827 ParFromPos(text->cursor.pos())->previous,
829 ParFromPos(text->cursor.pos())->next
831 text->cursor.par()->previous,
832 text->cursor.par()->next
838 void BufferView::updateInset(Inset * inset, bool mark_dirty)
843 // first check for locking insets
844 if (theLockingInset()) {
845 if (theLockingInset() == inset) {
846 if (text->UpdateInset(this, inset)) {
849 if (buffer()->isLyxClean())
850 owner()->getMiniBuffer()->
852 buffer()->markDirty();
857 } else if (theLockingInset()->UpdateInsetInInset(this,inset)) {
858 if (text->UpdateInset(this, theLockingInset())) {
861 if (buffer()->isLyxClean())
862 owner()->getMiniBuffer()->
864 buffer()->markDirty();
872 // then check the current buffer
875 update(text, BufferView::UPDATE);
876 if (text->UpdateInset(this, inset)) {
878 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
880 update(text, SELECT);
887 bool BufferView::ChangeInsets(Inset::Code code, string const & from, string const & to)
890 LyXParagraph * par = buffer()->paragraph;
891 LyXCursor cursor = text->cursor;
892 LyXCursor tmpcursor = cursor;
894 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
895 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
897 cursor.par(tmpcursor.par());
898 cursor.pos(tmpcursor.pos());
903 for (LyXParagraph::inset_iterator it = par->inset_iterator_begin();
904 it != par->inset_iterator_end(); ++it) {
905 if ((*it)->LyxCode() == code) {
906 InsetCommand * inset = static_cast<InsetCommand *>(*it);
907 if (inset->getContents() == from) {
908 inset->setContents(to);
916 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
918 // this is possible now, since SetCursor takes
919 // care about footnotes
920 text->SetCursorIntern(this, par, 0);
921 text->RedoParagraphs(this, text->cursor,
922 text->cursor.par()->Next());
923 text->FullRebreak(this);
930 text->SetCursorIntern(this, cursor.par(), cursor.pos());
935 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
937 // Check if the label 'from' appears more than once
938 vector<string> labels = buffer()->getLabelList();
939 if (count(labels.begin(), labels.end(), from) > 1)
942 return ChangeInsets(Inset::REF_CODE, from, to);
946 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
949 vector<pair<string,string> > keys = buffer()->getBibkeyList();
950 if (count_if(keys.begin(), keys.end(),
951 equal_1st_in_pair<string,string>(from))
955 return ChangeInsets(Inset::CITE_CODE, from, to);
958 UpdatableInset * BufferView::theLockingInset() const
960 // If NULL is not allowed we should put an Assert here. (Lgb)
962 return text->the_locking_inset;
967 void BufferView::theLockingInset(UpdatableInset * inset)
969 text->the_locking_inset = inset;
973 LyXText * BufferView::getLyXText() const
975 if (theLockingInset()) {
976 LyXText * txt = theLockingInset()->getLyXText(this, true);
984 LyXText * BufferView::getParentText(Inset * inset) const
986 if (inset->owner()) {
987 LyXText * txt = inset->getLyXText(this);
988 inset = inset->owner();
989 while (inset && inset->getLyXText(this) == txt)
990 inset = inset->owner();
992 return inset->getLyXText(this);
997 Language const * BufferView::getParentLanguage(Inset * inset) const
999 LyXText * text = getParentText(inset);
1000 return text->cursor.par()->GetFontSettings(buffer()->params,
1001 text->cursor.pos()).language();