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)
269 if (!insertInset(new_inset)) {
273 new_inset->Edit(this, 0, 0, 0);
277 /* This is also a buffer property (ale) */
278 // Not so sure about that. a goto Label function can not be buffer local, just
279 // think how this will work in a multiwindo/buffer environment, all the
280 // cursors in all the views showing this buffer will move. (Lgb)
281 // OK, then no cursor action should be allowed in buffer. (ale)
282 bool BufferView::gotoLabel(string const & label)
285 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
286 it != buffer()->inset_iterator_end(); ++it) {
287 vector<string> labels = (*it)->getLabelList();
288 if (find(labels.begin(),labels.end(),label)
291 text->SetCursor(this, it.getPar(), it.getPos());
292 text->sel_cursor = text->cursor;
293 update(BufferView::SELECT|BufferView::FITCUR);
302 void BufferView::allFloats(char flag, char figmar)
304 if (!available()) return;
306 LyXCursor cursor = text->cursor;
309 && cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE
311 && cursor.par()->footnotekind != LyXParagraph::FOOTNOTE
312 && cursor.par()->footnotekind != LyXParagraph::MARGIN
315 && cursor.par()->footnotekind != LyXParagraph::FIG
316 && cursor.par()->footnotekind != LyXParagraph::TAB
317 && cursor.par()->footnotekind != LyXParagraph::WIDE_FIG
318 && cursor.par()->footnotekind != LyXParagraph::WIDE_TAB
319 && cursor.par()->footnotekind != LyXParagraph::ALGORITHM)))
324 LyXCursor tmpcursor = cursor;
325 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
326 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
328 LyXParagraph *par = buffer()->paragraph;
331 if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
333 && par->footnotekind != LyXParagraph::FOOTNOTE
334 && par->footnotekind != LyXParagraph::MARGIN)
336 && par->footnotekind != LyXParagraph::FIG
337 && par->footnotekind != LyXParagraph::TAB
338 && par->footnotekind != LyXParagraph::WIDE_FIG
339 && par->footnotekind != LyXParagraph::WIDE_TAB
340 && par->footnotekind != LyXParagraph::ALGORITHM
345 && par->previous->footnoteflag !=
346 LyXParagraph::CLOSED_FOOTNOTE){ /* should be */
347 text->SetCursorIntern(this,
350 text->OpenFootnote(this);
354 if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
358 par->footnotekind != LyXParagraph::FOOTNOTE
360 par->footnotekind != LyXParagraph::MARGIN
365 par->footnotekind != LyXParagraph::FIG
367 par->footnotekind != LyXParagraph::TAB
369 par->footnotekind != LyXParagraph::WIDE_FIG
371 par->footnotekind != LyXParagraph::WIDE_TAB
373 par->footnotekind != LyXParagraph::ALGORITHM
377 text->SetCursorIntern(this, par, 0);
378 text->CloseFootnote(this);
384 text->SetCursorIntern(this, cursor.par(), cursor.pos());
391 void BufferView::insertNote()
393 InsetInfo * new_inset = new InsetInfo();
394 insertInset(new_inset);
395 new_inset->Edit(this, 0, 0, 0);
400 void BufferView::openStuff()
403 owner()->getMiniBuffer()->Set(_("Open/Close..."));
406 update(BufferView::SELECT|BufferView::FITCUR);
407 text->OpenStuff(this);
408 update(BufferView::SELECT|BufferView::FITCUR);
414 void BufferView::toggleFloat()
417 owner()->getMiniBuffer()->Set(_("Open/Close..."));
420 update(BufferView::SELECT|BufferView::FITCUR);
421 text->ToggleFootnote(this);
422 update(BufferView::SELECT|BufferView::FITCUR);
428 void BufferView::menuUndo()
431 owner()->getMiniBuffer()->Set(_("Undo"));
434 update(BufferView::SELECT|BufferView::FITCUR);
435 if (!text->TextUndo(this))
436 owner()->getMiniBuffer()->Set(_("No further undo information"));
438 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
444 void BufferView::menuRedo()
446 if (theLockingInset()) {
447 owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
452 owner()->getMiniBuffer()->Set(_("Redo"));
455 update(BufferView::SELECT|BufferView::FITCUR);
456 if (!text->TextRedo(this))
457 owner()->getMiniBuffer()->Set(_("No further redo information"));
459 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
465 void BufferView::hyphenationPoint()
469 update(BufferView::SELECT|BufferView::FITCUR);
470 InsetSpecialChar * new_inset =
471 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
472 insertInset(new_inset);
477 void BufferView::ldots()
481 update(BufferView::SELECT|BufferView::FITCUR);
482 InsetSpecialChar * new_inset =
483 new InsetSpecialChar(InsetSpecialChar::LDOTS);
484 insertInset(new_inset);
489 void BufferView::endOfSentenceDot()
493 update(BufferView::SELECT|BufferView::FITCUR);
494 InsetSpecialChar * new_inset =
495 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
496 insertInset(new_inset);
501 void BufferView::menuSeparator()
505 update(BufferView::SELECT|BufferView::FITCUR);
506 InsetSpecialChar * new_inset =
507 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
508 insertInset(new_inset);
513 void BufferView::newline()
517 update(BufferView::SELECT|BufferView::FITCUR);
518 text->InsertChar(this, LyXParagraph::META_NEWLINE);
519 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
524 void BufferView::protectedBlank()
528 update(BufferView::SELECT|BufferView::FITCUR);
529 InsetSpecialChar * new_inset =
530 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
531 insertInset(new_inset);
536 void BufferView::hfill()
540 update(BufferView::SELECT|BufferView::FITCUR);
541 text->InsertChar(this, LyXParagraph::META_HFILL);
542 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
546 void BufferView::copyEnvironment()
549 text->copyEnvironmentType();
550 // clear the selection, even if mark_set
552 text->ClearSelection();
553 update(BufferView::SELECT|BufferView::FITCUR);
554 owner()->getMiniBuffer()->Set(_("Paragraph environment type copied"));
559 void BufferView::pasteEnvironment()
562 text->pasteEnvironmentType(this);
563 owner()->getMiniBuffer()->Set(_("Paragraph environment type set"));
564 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
569 void BufferView::copy()
572 text->CopySelection(this);
573 // clear the selection, even if mark_set
575 text->ClearSelection();
576 update(BufferView::SELECT|BufferView::FITCUR);
577 owner()->getMiniBuffer()->Set(_("Copy"));
581 void BufferView::cut()
585 update(BufferView::SELECT|BufferView::FITCUR);
586 text->CutSelection(this);
587 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
588 owner()->getMiniBuffer()->Set(_("Cut"));
593 void BufferView::paste()
595 if (!available()) return;
597 owner()->getMiniBuffer()->Set(_("Paste"));
599 // clear the selection
601 text->ClearSelection();
602 update(BufferView::SELECT|BufferView::FITCUR);
605 text->PasteSelection(this);
606 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
608 // clear the selection
610 text->ClearSelection();
611 update(BufferView::SELECT|BufferView::FITCUR);
615 void BufferView::gotoNote()
617 if (!available()) return;
621 update(BufferView::SELECT|BufferView::FITCUR);
624 if (!text->GotoNextNote(this)) {
625 if (text->cursor.pos()
626 || text->cursor.par() != text->FirstParagraph()) {
628 text->cursor.par(text->FirstParagraph());
630 if (!text->GotoNextNote(this)) {
632 owner()->getMiniBuffer()->Set(_("No more notes"));
636 owner()->getMiniBuffer()->Set(_("No more notes"));
640 update(BufferView::SELECT|BufferView::FITCUR);
641 text->sel_cursor = text->cursor;
645 void BufferView::insertCorrectQuote()
649 if (text->cursor.pos())
650 c = text->cursor.par()->GetChar(text->cursor.pos() - 1);
654 insertInset(new InsetQuotes(c, buffer()->params));
658 /* these functions are for the spellchecker */
659 string const BufferView::nextWord(float & value)
666 return text->SelectNextWord(this, value);
670 void BufferView::selectLastWord()
672 if (!available()) return;
676 text->SelectSelectedWord(this);
677 toggleSelection(false);
678 update(BufferView::SELECT|BufferView::FITCUR);
682 void BufferView::endOfSpellCheck()
684 if (!available()) return;
688 text->SelectSelectedWord(this);
689 text->ClearSelection();
690 update(BufferView::SELECT|BufferView::FITCUR);
694 void BufferView::replaceWord(string const & replacestring)
696 if (!available()) return;
699 update(BufferView::SELECT|BufferView::FITCUR);
701 /* clear the selection (if there is any) */
702 toggleSelection(false);
703 update(BufferView::SELECT|BufferView::FITCUR);
705 /* clear the selection (if there is any) */
706 toggleSelection(false);
707 text->ReplaceSelectionWithString(this, replacestring);
709 text->SetSelectionOverString(this, replacestring);
711 // Go back so that replacement string is also spellchecked
712 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
713 text->CursorLeft(this);
715 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
717 // End of spellchecker stuff
720 bool BufferView::lockInset(UpdatableInset * inset)
722 if (!theLockingInset() && inset) {
723 theLockingInset(inset);
726 return theLockingInset()->LockInsetInInset(this, inset);
732 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
734 if (theLockingInset() && available()) {
735 LyXCursor cursor = text->cursor;
736 if ((cursor.pos() - 1 >= 0) &&
737 (cursor.par()->GetChar(cursor.pos() - 1) ==
738 LyXParagraph::META_INSET) &&
739 (cursor.par()->GetInset(cursor.pos() - 1) ==
740 theLockingInset()->GetLockingInset()))
741 text->SetCursor(this, cursor,
742 cursor.par(), cursor.pos() - 1);
743 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
744 LyXText * txt = getLyXText();
745 if (theLockingInset()->GetLockingInset()->LyxCode() ==
747 (txt->real_current_font.language() !=
748 buffer()->params.language
749 || txt->real_current_font.isVisibleRightToLeft()
750 != buffer()->params.language->RightToLeft()))
751 shape = (txt->real_current_font.isVisibleRightToLeft())
752 ? LyXScreen::REVERSED_L_SHAPE
753 : LyXScreen::L_SHAPE;
754 y += cursor.y() + theLockingInset()->InsetInInsetY();
755 pimpl_->screen_->ShowManualCursor(text, x, y, asc, desc,
761 void BufferView::hideLockedInsetCursor()
763 if (theLockingInset() && available()) {
764 pimpl_->screen_->HideCursor();
769 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
771 if (theLockingInset() && available()) {
772 y += text->cursor.y() + theLockingInset()->InsetInInsetY();
773 if (pimpl_->screen_->FitManualCursor(text, this, x, y, asc, desc))
779 int BufferView::unlockInset(UpdatableInset * inset)
781 if (inset && theLockingInset() == inset) {
782 inset->InsetUnlock(this);
786 } else if (inset && theLockingInset() &&
787 theLockingInset()->UnlockInsetInInset(this, inset)) {
791 return bufferlist.unlockInset(inset);
795 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
797 if (!theLockingInset())
798 return; // shouldn't happen
799 if (kind == Undo::EDIT) // in this case insets would not be stored!
801 text->SetUndo(buffer(), kind,
804 ParFromPos(text->cursor.pos())->previous,
806 ParFromPos(text->cursor.pos())->next
808 text->cursor.par()->previous,
809 text->cursor.par()->next
815 void BufferView::updateInset(Inset * inset, bool mark_dirty)
820 // first check for locking insets
821 if (theLockingInset()) {
822 if (theLockingInset() == inset) {
823 if (text->UpdateInset(this, inset)) {
826 if (buffer()->isLyxClean())
827 owner()->getMiniBuffer()->
829 buffer()->markDirty();
834 } else if (theLockingInset()->UpdateInsetInInset(this,inset)) {
835 if (text->UpdateInset(this, theLockingInset())) {
838 if (buffer()->isLyxClean())
839 owner()->getMiniBuffer()->
841 buffer()->markDirty();
849 // then check the current buffer
852 update(BufferView::UPDATE);
853 if (text->UpdateInset(this, inset)) {
855 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);
923 UpdatableInset * BufferView::theLockingInset() const
925 // If NULL is not allowed we should put an Assert here. (Lgb)
927 return text->the_locking_inset;
932 void BufferView::theLockingInset(UpdatableInset * inset)
934 text->the_locking_inset = inset;
938 LyXText * BufferView::getLyXText() const
940 if (theLockingInset()) {
941 LyXText * txt = theLockingInset()->getLyXText(this);