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;
43 // Inserts a file into current document
44 bool BufferView::insertLyXFile(string const & filen)
46 // Copyright CHT Software Service GmbH
49 // Insert a Lyxformat - file into current buffer
51 // Moved from lyx_cb.C (Lgb)
53 if (filen.empty()) return false;
55 string fname = MakeAbsPath(filen);
57 // check if file exist
61 WriteAlert(_("Error!"),
62 _("Specified file is unreadable: "),
63 MakeDisplayPath(fname, 50));
69 ifstream ifs(fname.c_str());
71 WriteAlert(_("Error!"),
72 _("Cannot open specified file: "),
73 MakeDisplayPath(fname, 50));
85 lyxerr.debug() << "Will insert file with header" << endl;
86 res = buffer()->readFile(lex, text->cursor.par());
88 lyxerr.debug() << "Will insert file without header" << endl;
89 res = buffer()->readLyXformat2(lex, text->cursor.par());
96 bool BufferView::removeAutoInsets()
98 LyXParagraph * par = buffer()->paragraph;
100 LyXCursor tmpcursor = text->cursor;
105 // this has to be done before the delete
107 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
109 text->SetCursor(this, cursor, par, 0);
110 if (par->AutoDeleteInsets()){
113 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
115 text->RedoParagraphs(this, cursor,
116 cursor.par()->Next());
117 text->FullRebreak(this);
124 // avoid forbidden cursor positions caused by error removing
125 if (tmpcursor.pos() > tmpcursor.par()->Last())
126 tmpcursor.pos(tmpcursor.par()->Last());
127 text->SetCursorIntern(this, tmpcursor.par(), tmpcursor.pos());
133 void BufferView::insertErrors(TeXErrors & terr)
135 // Save the cursor position
136 LyXCursor cursor = text->cursor;
139 // This is drastic, but it's the only fix, I could find. (Asger)
144 for (TeXErrors::Errors::const_iterator cit = terr.begin();
147 string desctext((*cit).error_desc);
148 string errortext((*cit).error_text);
149 string msgtxt = desctext + '\n' + errortext;
150 int errorrow = (*cit).error_in_line;
152 // Insert error string for row number
156 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
157 buffer()->texrow.increasePos(tmpid, tmppos);
160 LyXParagraph * texrowpar = 0;
163 texrowpar = text->FirstParagraph();
166 texrowpar = text->GetParFromID(tmpid);
172 InsetError * new_inset = new InsetError(msgtxt);
173 text->SetCursorIntern(this, texrowpar, tmppos);
174 text->InsertInset(this, new_inset);
175 text->FullRebreak(this);
177 // Restore the cursor position
178 text->SetCursorIntern(this, cursor.par(), cursor.pos());
182 void BufferView::setCursorFromRow(int row)
187 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
189 LyXParagraph * texrowpar;
192 texrowpar = text->FirstParagraph();
195 texrowpar = text->GetParFromID(tmpid);
197 text->SetCursor(this, texrowpar, tmppos);
200 bool BufferView::insertInset(Inset * inset, string const & lout,
203 // if we are in a locking inset we should try to insert the
204 // inset there otherwise this is a illegal function now
205 if (theLockingInset()) {
206 if (theLockingInset()->InsertInsetAllowed(inset))
207 return theLockingInset()->InsertInset(this, inset);
211 // not quite sure if we want this...
212 text->SetCursorParUndo(buffer());
217 update(BufferView::SELECT|BufferView::FITCUR);
218 text->BreakParagraph(this);
219 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
221 if (text->cursor.par()->Last()) {
222 text->CursorLeft(this);
224 text->BreakParagraph(this);
225 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
228 pair<bool, LyXTextClass::size_type> lres =
229 textclasslist.NumberOfLayout(buffer()->params
231 LyXTextClass::size_type lay;
232 if (lres.first != false) {
236 // layout not fount using default "Standard" (0)
240 text->SetLayout(this, lay);
242 text->SetParagraph(this, 0, 0,
244 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
248 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
250 text->current_font.setLatex(LyXFont::OFF);
253 text->InsertInset(this, inset);
254 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
256 text->UnFreezeUndo();
261 // Open and lock an updatable inset
262 bool BufferView::open_new_inset(UpdatableInset * new_inset)
266 if (!insertInset(new_inset))
268 text->CursorLeft(this);
269 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
270 new_inset->Edit(this, 0, 0, 0);
274 /* This is also a buffer property (ale) */
275 // Not so sure about that. a goto Label function can not be buffer local, just
276 // think how this will work in a multiwindo/buffer environment, all the
277 // cursors in all the views showing this buffer will move. (Lgb)
278 // OK, then no cursor action should be allowed in buffer. (ale)
279 bool BufferView::gotoLabel(string const & label)
282 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
283 it != buffer()->inset_iterator_end(); ++it) {
284 vector<string> labels = (*it)->getLabelList();
285 if ( find(labels.begin(),labels.end(),label)
288 text->SetCursor(this, it.getPar(), it.getPos());
289 text->sel_cursor = text->cursor;
290 update(BufferView::SELECT|BufferView::FITCUR);
299 void BufferView::allFloats(char flag, char figmar)
301 if (!available()) return;
303 LyXCursor cursor = text->cursor;
306 && cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE
308 && cursor.par()->footnotekind != LyXParagraph::FOOTNOTE
309 && cursor.par()->footnotekind != LyXParagraph::MARGIN
312 && cursor.par()->footnotekind != LyXParagraph::FIG
313 && cursor.par()->footnotekind != LyXParagraph::TAB
314 && cursor.par()->footnotekind != LyXParagraph::WIDE_FIG
315 && cursor.par()->footnotekind != LyXParagraph::WIDE_TAB
316 && cursor.par()->footnotekind != LyXParagraph::ALGORITHM)))
321 LyXCursor tmpcursor = cursor;
322 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
323 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
325 LyXParagraph *par = buffer()->paragraph;
328 if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
330 && par->footnotekind != LyXParagraph::FOOTNOTE
331 && par->footnotekind != LyXParagraph::MARGIN)
333 && par->footnotekind != LyXParagraph::FIG
334 && par->footnotekind != LyXParagraph::TAB
335 && par->footnotekind != LyXParagraph::WIDE_FIG
336 && par->footnotekind != LyXParagraph::WIDE_TAB
337 && par->footnotekind != LyXParagraph::ALGORITHM
342 && par->previous->footnoteflag !=
343 LyXParagraph::CLOSED_FOOTNOTE){ /* should be */
344 text->SetCursorIntern(this,
347 text->OpenFootnote(this);
351 if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
355 par->footnotekind != LyXParagraph::FOOTNOTE
357 par->footnotekind != LyXParagraph::MARGIN
362 par->footnotekind != LyXParagraph::FIG
364 par->footnotekind != LyXParagraph::TAB
366 par->footnotekind != LyXParagraph::WIDE_FIG
368 par->footnotekind != LyXParagraph::WIDE_TAB
370 par->footnotekind != LyXParagraph::ALGORITHM
374 text->SetCursorIntern(this, par, 0);
375 text->CloseFootnote(this);
381 text->SetCursorIntern(this, cursor.par(), cursor.pos());
388 void BufferView::insertNote()
390 InsetInfo * new_inset = new InsetInfo();
391 insertInset(new_inset);
392 new_inset->Edit(this, 0, 0, 0);
397 void BufferView::openStuff()
400 owner()->getMiniBuffer()->Set(_("Open/Close..."));
403 update(BufferView::SELECT|BufferView::FITCUR);
404 text->OpenStuff(this);
405 update(BufferView::SELECT|BufferView::FITCUR);
411 void BufferView::toggleFloat()
414 owner()->getMiniBuffer()->Set(_("Open/Close..."));
417 update(BufferView::SELECT|BufferView::FITCUR);
418 text->ToggleFootnote(this);
419 update(BufferView::SELECT|BufferView::FITCUR);
425 void BufferView::menuUndo()
428 owner()->getMiniBuffer()->Set(_("Undo"));
431 update(BufferView::SELECT|BufferView::FITCUR);
432 if (!text->TextUndo(this))
433 owner()->getMiniBuffer()->Set(_("No further undo information"));
435 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
441 void BufferView::menuRedo()
443 if (theLockingInset()) {
444 owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
449 owner()->getMiniBuffer()->Set(_("Redo"));
452 update(BufferView::SELECT|BufferView::FITCUR);
453 if (!text->TextRedo(this))
454 owner()->getMiniBuffer()->Set(_("No further redo information"));
456 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
462 void BufferView::hyphenationPoint()
466 update(BufferView::SELECT|BufferView::FITCUR);
467 InsetSpecialChar * new_inset =
468 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
469 insertInset(new_inset);
474 void BufferView::ldots()
478 update(BufferView::SELECT|BufferView::FITCUR);
479 InsetSpecialChar * new_inset =
480 new InsetSpecialChar(InsetSpecialChar::LDOTS);
481 insertInset(new_inset);
486 void BufferView::endOfSentenceDot()
490 update(BufferView::SELECT|BufferView::FITCUR);
491 InsetSpecialChar * new_inset =
492 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
493 insertInset(new_inset);
498 void BufferView::menuSeparator()
502 update(BufferView::SELECT|BufferView::FITCUR);
503 InsetSpecialChar * new_inset =
504 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
505 insertInset(new_inset);
510 void BufferView::newline()
514 update(BufferView::SELECT|BufferView::FITCUR);
515 text->InsertChar(this, LyXParagraph::META_NEWLINE);
516 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
521 void BufferView::protectedBlank()
525 update(BufferView::SELECT|BufferView::FITCUR);
526 InsetSpecialChar * new_inset =
527 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
528 insertInset(new_inset);
533 void BufferView::hfill()
537 update(BufferView::SELECT|BufferView::FITCUR);
538 text->InsertChar(this, LyXParagraph::META_HFILL);
539 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
543 void BufferView::copyEnvironment()
546 text->copyEnvironmentType();
547 // clear the selection, even if mark_set
549 text->ClearSelection();
550 update(BufferView::SELECT|BufferView::FITCUR);
551 owner()->getMiniBuffer()->Set(_("Paragraph environment type copied"));
556 void BufferView::pasteEnvironment()
559 text->pasteEnvironmentType(this);
560 owner()->getMiniBuffer()->Set(_("Paragraph environment type set"));
561 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
566 void BufferView::copy()
569 text->CopySelection(this);
570 // clear the selection, even if mark_set
572 text->ClearSelection();
573 update(BufferView::SELECT|BufferView::FITCUR);
574 owner()->getMiniBuffer()->Set(_("Copy"));
578 void BufferView::cut()
582 update(BufferView::SELECT|BufferView::FITCUR);
583 text->CutSelection(this);
584 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
585 owner()->getMiniBuffer()->Set(_("Cut"));
590 void BufferView::paste()
592 if (!available()) return;
594 owner()->getMiniBuffer()->Set(_("Paste"));
596 // clear the selection
598 text->ClearSelection();
599 update(BufferView::SELECT|BufferView::FITCUR);
602 text->PasteSelection(this);
603 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
605 // clear the selection
607 text->ClearSelection();
608 update(BufferView::SELECT|BufferView::FITCUR);
612 void BufferView::gotoNote()
614 if (!available()) return;
618 update(BufferView::SELECT|BufferView::FITCUR);
621 if (!text->GotoNextNote(this)) {
622 if (text->cursor.pos()
623 || text->cursor.par() != text->FirstParagraph()) {
625 text->cursor.par(text->FirstParagraph());
627 if (!text->GotoNextNote(this)) {
629 owner()->getMiniBuffer()->Set(_("No more notes"));
633 owner()->getMiniBuffer()->Set(_("No more notes"));
637 update(BufferView::SELECT|BufferView::FITCUR);
638 text->sel_cursor = text->cursor;
642 void BufferView::insertCorrectQuote()
646 if (text->cursor.pos())
647 c = text->cursor.par()->GetChar(text->cursor.pos() - 1);
651 insertInset(new InsetQuotes(c, buffer()->params));
655 /* these functions are for the spellchecker */
656 string const BufferView::nextWord(float & value)
663 return text->SelectNextWord(this, value);
667 void BufferView::selectLastWord()
669 if (!available()) return;
673 text->SelectSelectedWord(this);
674 toggleSelection(false);
675 update(BufferView::SELECT|BufferView::FITCUR);
679 void BufferView::endOfSpellCheck()
681 if (!available()) return;
685 text->SelectSelectedWord(this);
686 text->ClearSelection();
687 update(BufferView::SELECT|BufferView::FITCUR);
691 void BufferView::replaceWord(string const & replacestring)
693 if (!available()) return;
696 update(BufferView::SELECT|BufferView::FITCUR);
698 /* clear the selection (if there is any) */
699 toggleSelection(false);
700 update(BufferView::SELECT|BufferView::FITCUR);
702 /* clear the selection (if there is any) */
703 toggleSelection(false);
704 text->ReplaceSelectionWithString(this, replacestring);
706 text->SetSelectionOverString(this, replacestring);
708 // Go back so that replacement string is also spellchecked
709 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
710 text->CursorLeft(this);
712 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
714 // End of spellchecker stuff
717 bool BufferView::lockInset(UpdatableInset * inset)
719 if (!theLockingInset() && inset) {
720 theLockingInset(inset);
723 return theLockingInset()->LockInsetInInset(this, inset);
729 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
731 if (theLockingInset() && available()) {
732 LyXCursor cursor = text->cursor;
733 if ((cursor.pos() - 1 >= 0) &&
734 (cursor.par()->GetChar(cursor.pos() - 1) ==
735 LyXParagraph::META_INSET) &&
736 (cursor.par()->GetInset(cursor.pos() - 1) ==
737 theLockingInset()->GetLockingInset()))
738 text->SetCursor(this, cursor,
739 cursor.par(), cursor.pos() - 1);
740 y += cursor.y() + theLockingInset()->InsetInInsetY();
741 pimpl_->screen_->ShowManualCursor(text, x, y, asc, desc,
742 LyXScreen::BAR_SHAPE);
747 void BufferView::hideLockedInsetCursor()
749 if (theLockingInset() && available()) {
750 pimpl_->screen_->HideCursor();
755 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
757 if (theLockingInset() && available()){
758 y += text->cursor.y() + theLockingInset()->InsetInInsetY();
759 if (pimpl_->screen_->FitManualCursor(text, x, y, asc, desc))
765 int BufferView::unlockInset(UpdatableInset * inset)
767 if (inset && theLockingInset() == inset) {
768 inset->InsetUnlock(this);
772 } else if (inset && theLockingInset() &&
773 theLockingInset()->UnlockInsetInInset(this, inset)) {
777 return bufferlist.unlockInset(inset);
781 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
783 if (!theLockingInset())
784 return; // shouldn't happen
785 if (kind == Undo::EDIT) // in this case insets would not be stored!
787 text->SetUndo(buffer(), kind,
790 ParFromPos(text->cursor.pos())->previous,
792 ParFromPos(text->cursor.pos())->next
794 text->cursor.par()->previous,
795 text->cursor.par()->next
801 void BufferView::updateInset(Inset * inset, bool mark_dirty)
806 // first check for locking insets
807 if (theLockingInset()) {
808 if (theLockingInset() == inset) {
809 if (text->UpdateInset(this, inset)){
812 if (buffer()->isLyxClean())
813 owner()->getMiniBuffer()->
815 buffer()->markDirty();
820 } else if (theLockingInset()->UpdateInsetInInset(this,inset)) {
821 if (text->UpdateInset(this, theLockingInset())) {
824 if (buffer()->isLyxClean())
825 owner()->getMiniBuffer()->
827 buffer()->markDirty();
835 // then check the current buffer
838 update(BufferView::UPDATE);
839 if (text->UpdateInset(this, inset)){
841 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
849 bool BufferView::ChangeRefs(string const & from, string const & to)
852 LyXParagraph * par = buffer()->paragraph;
853 LyXCursor cursor = text->cursor;
854 LyXCursor tmpcursor = cursor;
856 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
857 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
859 cursor.par(tmpcursor.par());
860 cursor.pos(tmpcursor.pos());
865 for (LyXParagraph::inset_iterator it = par->inset_iterator_begin();
866 it != par->inset_iterator_end(); ++it) {
867 if ((*it)->LyxCode() == Inset::REF_CODE) {
868 InsetCommand * inset = static_cast<InsetCommand *>(*it);
869 if (inset->getContents() == from) {
870 inset->setContents(to);
878 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
880 // this is possible now, since SetCursor takes
881 // care about footnotes
882 text->SetCursorIntern(this, par, 0);
883 text->RedoParagraphs(this, text->cursor,
884 text->cursor.par()->Next());
885 text->FullRebreak(this);
892 text->SetCursorIntern(this, cursor.par(), cursor.pos());
897 UpdatableInset * BufferView::theLockingInset() const
899 return text->the_locking_inset;
903 void BufferView::theLockingInset(UpdatableInset const * inset)
905 text->the_locking_inset = inset;