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 fname = MakeAbsPath(filen);
58 // check if file exist
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));
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());
97 bool BufferView::removeAutoInsets()
99 LyXParagraph * par = buffer()->paragraph;
101 LyXCursor tmpcursor = text->cursor;
106 // this has to be done before the delete
108 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
110 text->SetCursor(this, cursor, par, 0);
111 if (par->AutoDeleteInsets()){
114 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
116 text->RedoParagraphs(this, cursor,
117 cursor.par()->Next());
118 text->FullRebreak(this);
125 // avoid forbidden cursor positions caused by error removing
126 if (tmpcursor.pos() > tmpcursor.par()->Last())
127 tmpcursor.pos(tmpcursor.par()->Last());
128 text->SetCursorIntern(this, tmpcursor.par(), tmpcursor.pos());
134 void BufferView::insertErrors(TeXErrors & terr)
136 // Save the cursor position
137 LyXCursor cursor = text->cursor;
140 // This is drastic, but it's the only fix, I could find. (Asger)
145 for (TeXErrors::Errors::const_iterator cit = terr.begin();
148 string desctext((*cit).error_desc);
149 string errortext((*cit).error_text);
150 string msgtxt = desctext + '\n' + errortext;
151 int errorrow = (*cit).error_in_line;
153 // Insert error string for row number
157 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
158 buffer()->texrow.increasePos(tmpid, tmppos);
161 LyXParagraph * texrowpar = 0;
164 texrowpar = text->FirstParagraph();
167 texrowpar = text->GetParFromID(tmpid);
173 InsetError * new_inset = new InsetError(msgtxt);
174 text->SetCursorIntern(this, texrowpar, tmppos);
175 text->InsertInset(this, new_inset);
176 text->FullRebreak(this);
178 // Restore the cursor position
179 text->SetCursorIntern(this, cursor.par(), cursor.pos());
183 void BufferView::setCursorFromRow(int row)
188 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
190 LyXParagraph * texrowpar;
193 texrowpar = text->FirstParagraph();
196 texrowpar = text->GetParFromID(tmpid);
198 text->SetCursor(this, texrowpar, tmppos);
202 bool BufferView::insertInset(Inset * inset, string const & lout,
205 // if we are in a locking inset we should try to insert the
206 // inset there otherwise this is a illegal function now
207 if (theLockingInset()) {
208 if (theLockingInset()->InsertInsetAllowed(inset))
209 return theLockingInset()->InsertInset(this, inset);
213 // not quite sure if we want this...
214 text->SetCursorParUndo(buffer());
219 update(BufferView::SELECT|BufferView::FITCUR);
220 text->BreakParagraph(this);
221 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
223 if (text->cursor.par()->Last()) {
224 text->CursorLeft(this);
226 text->BreakParagraph(this);
227 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
230 pair<bool, LyXTextClass::size_type> lres =
231 textclasslist.NumberOfLayout(buffer()->params
233 LyXTextClass::size_type lay;
234 if (lres.first != false) {
238 // layout not fount using default "Standard" (0)
242 text->SetLayout(this, lay);
244 text->SetParagraph(this, 0, 0,
246 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
250 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
252 text->current_font.setLatex(LyXFont::OFF);
255 text->InsertInset(this, inset);
256 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
258 text->UnFreezeUndo();
263 // Open and lock an updatable inset
264 bool BufferView::open_new_inset(UpdatableInset * new_inset)
268 if (!insertInset(new_inset)) {
272 // text->CursorLeft(this);
273 // update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
274 new_inset->Edit(this, 0, 0, 0);
278 /* This is also a buffer property (ale) */
279 // Not so sure about that. a goto Label function can not be buffer local, just
280 // think how this will work in a multiwindo/buffer environment, all the
281 // cursors in all the views showing this buffer will move. (Lgb)
282 // OK, then no cursor action should be allowed in buffer. (ale)
283 bool BufferView::gotoLabel(string const & label)
286 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
287 it != buffer()->inset_iterator_end(); ++it) {
288 vector<string> labels = (*it)->getLabelList();
289 if (find(labels.begin(),labels.end(),label)
292 text->SetCursor(this, it.getPar(), it.getPos());
293 text->sel_cursor = text->cursor;
294 update(BufferView::SELECT|BufferView::FITCUR);
303 void BufferView::allFloats(char flag, char figmar)
305 if (!available()) return;
307 LyXCursor cursor = text->cursor;
310 && cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE
312 && cursor.par()->footnotekind != LyXParagraph::FOOTNOTE
313 && cursor.par()->footnotekind != LyXParagraph::MARGIN
316 && cursor.par()->footnotekind != LyXParagraph::FIG
317 && cursor.par()->footnotekind != LyXParagraph::TAB
318 && cursor.par()->footnotekind != LyXParagraph::WIDE_FIG
319 && cursor.par()->footnotekind != LyXParagraph::WIDE_TAB
320 && cursor.par()->footnotekind != LyXParagraph::ALGORITHM)))
325 LyXCursor tmpcursor = cursor;
326 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
327 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
329 LyXParagraph *par = buffer()->paragraph;
332 if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
334 && par->footnotekind != LyXParagraph::FOOTNOTE
335 && par->footnotekind != LyXParagraph::MARGIN)
337 && par->footnotekind != LyXParagraph::FIG
338 && par->footnotekind != LyXParagraph::TAB
339 && par->footnotekind != LyXParagraph::WIDE_FIG
340 && par->footnotekind != LyXParagraph::WIDE_TAB
341 && par->footnotekind != LyXParagraph::ALGORITHM
346 && par->previous->footnoteflag !=
347 LyXParagraph::CLOSED_FOOTNOTE){ /* should be */
348 text->SetCursorIntern(this,
351 text->OpenFootnote(this);
355 if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
359 par->footnotekind != LyXParagraph::FOOTNOTE
361 par->footnotekind != LyXParagraph::MARGIN
366 par->footnotekind != LyXParagraph::FIG
368 par->footnotekind != LyXParagraph::TAB
370 par->footnotekind != LyXParagraph::WIDE_FIG
372 par->footnotekind != LyXParagraph::WIDE_TAB
374 par->footnotekind != LyXParagraph::ALGORITHM
378 text->SetCursorIntern(this, par, 0);
379 text->CloseFootnote(this);
385 text->SetCursorIntern(this, cursor.par(), cursor.pos());
392 void BufferView::insertNote()
394 InsetInfo * new_inset = new InsetInfo();
395 insertInset(new_inset);
396 new_inset->Edit(this, 0, 0, 0);
401 void BufferView::openStuff()
404 owner()->getMiniBuffer()->Set(_("Open/Close..."));
407 update(BufferView::SELECT|BufferView::FITCUR);
408 text->OpenStuff(this);
409 update(BufferView::SELECT|BufferView::FITCUR);
415 void BufferView::toggleFloat()
418 owner()->getMiniBuffer()->Set(_("Open/Close..."));
421 update(BufferView::SELECT|BufferView::FITCUR);
422 text->ToggleFootnote(this);
423 update(BufferView::SELECT|BufferView::FITCUR);
429 void BufferView::menuUndo()
432 owner()->getMiniBuffer()->Set(_("Undo"));
435 update(BufferView::SELECT|BufferView::FITCUR);
436 if (!text->TextUndo(this))
437 owner()->getMiniBuffer()->Set(_("No further undo information"));
439 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
445 void BufferView::menuRedo()
447 if (theLockingInset()) {
448 owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
453 owner()->getMiniBuffer()->Set(_("Redo"));
456 update(BufferView::SELECT|BufferView::FITCUR);
457 if (!text->TextRedo(this))
458 owner()->getMiniBuffer()->Set(_("No further redo information"));
460 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
466 void BufferView::hyphenationPoint()
470 update(BufferView::SELECT|BufferView::FITCUR);
471 InsetSpecialChar * new_inset =
472 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
473 insertInset(new_inset);
478 void BufferView::ldots()
482 update(BufferView::SELECT|BufferView::FITCUR);
483 InsetSpecialChar * new_inset =
484 new InsetSpecialChar(InsetSpecialChar::LDOTS);
485 insertInset(new_inset);
490 void BufferView::endOfSentenceDot()
494 update(BufferView::SELECT|BufferView::FITCUR);
495 InsetSpecialChar * new_inset =
496 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
497 insertInset(new_inset);
502 void BufferView::menuSeparator()
506 update(BufferView::SELECT|BufferView::FITCUR);
507 InsetSpecialChar * new_inset =
508 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
509 insertInset(new_inset);
514 void BufferView::newline()
518 update(BufferView::SELECT|BufferView::FITCUR);
519 text->InsertChar(this, LyXParagraph::META_NEWLINE);
520 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
525 void BufferView::protectedBlank()
529 update(BufferView::SELECT|BufferView::FITCUR);
530 InsetSpecialChar * new_inset =
531 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
532 insertInset(new_inset);
537 void BufferView::hfill()
541 update(BufferView::SELECT|BufferView::FITCUR);
542 text->InsertChar(this, LyXParagraph::META_HFILL);
543 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
547 void BufferView::copyEnvironment()
550 text->copyEnvironmentType();
551 // clear the selection, even if mark_set
553 text->ClearSelection();
554 update(BufferView::SELECT|BufferView::FITCUR);
555 owner()->getMiniBuffer()->Set(_("Paragraph environment type copied"));
560 void BufferView::pasteEnvironment()
563 text->pasteEnvironmentType(this);
564 owner()->getMiniBuffer()->Set(_("Paragraph environment type set"));
565 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
570 void BufferView::copy()
573 text->CopySelection(this);
574 // clear the selection, even if mark_set
576 text->ClearSelection();
577 update(BufferView::SELECT|BufferView::FITCUR);
578 owner()->getMiniBuffer()->Set(_("Copy"));
582 void BufferView::cut()
586 update(BufferView::SELECT|BufferView::FITCUR);
587 text->CutSelection(this);
588 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
589 owner()->getMiniBuffer()->Set(_("Cut"));
594 void BufferView::paste()
596 if (!available()) return;
598 owner()->getMiniBuffer()->Set(_("Paste"));
600 // clear the selection
602 text->ClearSelection();
603 update(BufferView::SELECT|BufferView::FITCUR);
606 text->PasteSelection(this);
607 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
609 // clear the selection
611 text->ClearSelection();
612 update(BufferView::SELECT|BufferView::FITCUR);
616 void BufferView::gotoNote()
618 if (!available()) return;
622 update(BufferView::SELECT|BufferView::FITCUR);
625 if (!text->GotoNextNote(this)) {
626 if (text->cursor.pos()
627 || text->cursor.par() != text->FirstParagraph()) {
629 text->cursor.par(text->FirstParagraph());
631 if (!text->GotoNextNote(this)) {
633 owner()->getMiniBuffer()->Set(_("No more notes"));
637 owner()->getMiniBuffer()->Set(_("No more notes"));
641 update(BufferView::SELECT|BufferView::FITCUR);
642 text->sel_cursor = text->cursor;
646 void BufferView::insertCorrectQuote()
650 if (text->cursor.pos())
651 c = text->cursor.par()->GetChar(text->cursor.pos() - 1);
655 insertInset(new InsetQuotes(c, buffer()->params));
659 /* these functions are for the spellchecker */
660 string const BufferView::nextWord(float & value)
667 return text->SelectNextWord(this, value);
671 void BufferView::selectLastWord()
673 if (!available()) return;
677 text->SelectSelectedWord(this);
678 toggleSelection(false);
679 update(BufferView::SELECT|BufferView::FITCUR);
683 void BufferView::endOfSpellCheck()
685 if (!available()) return;
689 text->SelectSelectedWord(this);
690 text->ClearSelection();
691 update(BufferView::SELECT|BufferView::FITCUR);
695 void BufferView::replaceWord(string const & replacestring)
697 if (!available()) return;
700 update(BufferView::SELECT|BufferView::FITCUR);
702 /* clear the selection (if there is any) */
703 toggleSelection(false);
704 update(BufferView::SELECT|BufferView::FITCUR);
706 /* clear the selection (if there is any) */
707 toggleSelection(false);
708 text->ReplaceSelectionWithString(this, replacestring);
710 text->SetSelectionOverString(this, replacestring);
712 // Go back so that replacement string is also spellchecked
713 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
714 text->CursorLeft(this);
716 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
718 // End of spellchecker stuff
721 bool BufferView::lockInset(UpdatableInset * inset)
723 if (!theLockingInset() && inset) {
724 theLockingInset(inset);
727 return theLockingInset()->LockInsetInInset(this, inset);
733 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
735 if (theLockingInset() && available()) {
736 LyXCursor cursor = text->cursor;
737 if ((cursor.pos() - 1 >= 0) &&
738 (cursor.par()->GetChar(cursor.pos() - 1) ==
739 LyXParagraph::META_INSET) &&
740 (cursor.par()->GetInset(cursor.pos() - 1) ==
741 theLockingInset()->GetLockingInset()))
742 text->SetCursor(this, cursor,
743 cursor.par(), cursor.pos() - 1);
744 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
745 LyXText * txt = getLyXText();
746 if (theLockingInset()->GetLockingInset()->LyxCode() ==
748 (txt->real_current_font.language() !=
749 buffer()->params.language
750 || txt->real_current_font.isVisibleRightToLeft()
751 != buffer()->params.language->RightToLeft()))
752 shape = (txt->real_current_font.isVisibleRightToLeft())
753 ? LyXScreen::REVERSED_L_SHAPE
754 : LyXScreen::L_SHAPE;
755 y += cursor.y() + theLockingInset()->InsetInInsetY();
756 pimpl_->screen_->ShowManualCursor(text, x, y, asc, desc,
762 void BufferView::hideLockedInsetCursor()
764 if (theLockingInset() && available()) {
765 pimpl_->screen_->HideCursor();
770 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
772 if (theLockingInset() && available()){
773 y += text->cursor.y() + theLockingInset()->InsetInInsetY();
774 if (pimpl_->screen_->FitManualCursor(text, x, y, asc, desc))
780 int BufferView::unlockInset(UpdatableInset * inset)
782 if (inset && theLockingInset() == inset) {
783 inset->InsetUnlock(this);
787 } else if (inset && theLockingInset() &&
788 theLockingInset()->UnlockInsetInInset(this, inset)) {
792 return bufferlist.unlockInset(inset);
796 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
798 if (!theLockingInset())
799 return; // shouldn't happen
800 if (kind == Undo::EDIT) // in this case insets would not be stored!
802 text->SetUndo(buffer(), kind,
805 ParFromPos(text->cursor.pos())->previous,
807 ParFromPos(text->cursor.pos())->next
809 text->cursor.par()->previous,
810 text->cursor.par()->next
816 void BufferView::updateInset(Inset * inset, bool mark_dirty)
821 // first check for locking insets
822 if (theLockingInset()) {
823 if (theLockingInset() == inset) {
824 if (text->UpdateInset(this, inset)){
827 if (buffer()->isLyxClean())
828 owner()->getMiniBuffer()->
830 buffer()->markDirty();
835 } else if (theLockingInset()->UpdateInsetInInset(this,inset)) {
836 if (text->UpdateInset(this, theLockingInset())) {
839 if (buffer()->isLyxClean())
840 owner()->getMiniBuffer()->
842 buffer()->markDirty();
850 // then check the current buffer
853 update(BufferView::UPDATE);
854 if (text->UpdateInset(this, inset)){
856 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
864 bool BufferView::ChangeRefs(string const & from, string const & to)
867 LyXParagraph * par = buffer()->paragraph;
868 LyXCursor cursor = text->cursor;
869 LyXCursor tmpcursor = cursor;
871 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
872 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
874 cursor.par(tmpcursor.par());
875 cursor.pos(tmpcursor.pos());
880 for (LyXParagraph::inset_iterator it = par->inset_iterator_begin();
881 it != par->inset_iterator_end(); ++it) {
882 if ((*it)->LyxCode() == Inset::REF_CODE) {
883 InsetCommand * inset = static_cast<InsetCommand *>(*it);
884 if (inset->getContents() == from) {
885 inset->setContents(to);
893 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
895 // this is possible now, since SetCursor takes
896 // care about footnotes
897 text->SetCursorIntern(this, par, 0);
898 text->RedoParagraphs(this, text->cursor,
899 text->cursor.par()->Next());
900 text->FullRebreak(this);
907 text->SetCursorIntern(this, cursor.par(), cursor.pos());
912 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
914 // Check if the label 'from' appears more than once
915 vector<string> labels = buffer()->getLabelList();
916 if (count(labels.begin(), labels.end(), from) > 1)
919 return ChangeRefs(from, to);
922 UpdatableInset * BufferView::theLockingInset() const
924 // If NULL is not allowed we should put an Assert here. (Lgb)
926 return text->the_locking_inset;
931 void BufferView::theLockingInset(UpdatableInset * inset)
933 text->the_locking_inset = inset;
937 LyXText * BufferView::getLyXText() const
939 if (theLockingInset()) {
940 LyXText * txt = theLockingInset()->getLyXText(this);