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));
84 lyxerr.debug() << "Will insert file with header" << endl;
85 res = buffer()->readFile(lex, text->cursor.par());
87 lyxerr.debug() << "Will insert file without header" << endl;
88 res = buffer()->readLyXformat2(lex, text->cursor.par());
95 bool BufferView::removeAutoInsets()
97 LyXParagraph * par = buffer()->paragraph;
99 LyXCursor tmpcursor = text->cursor;
104 // this has to be done before the delete
105 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
106 text->SetCursor(cursor, par, 0);
107 if (par->AutoDeleteInsets()){
109 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
110 text->RedoParagraphs(cursor,
111 cursor.par()->Next());
117 // avoid forbidden cursor positions caused by error removing
118 if (tmpcursor.pos() > tmpcursor.par()->Last())
119 tmpcursor.pos(tmpcursor.par()->Last());
120 text->SetCursorIntern(tmpcursor.par(), tmpcursor.pos());
126 void BufferView::insertErrors(TeXErrors & terr)
128 // Save the cursor position
129 LyXCursor cursor = text->cursor;
131 // This is drastic, but it's the only fix, I could find. (Asger)
135 for (TeXErrors::Errors::const_iterator cit = terr.begin();
138 string desctext((*cit).error_desc);
139 string errortext((*cit).error_text);
140 string msgtxt = desctext + '\n' + errortext;
141 int errorrow = (*cit).error_in_line;
143 // Insert error string for row number
147 buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos);
149 LyXParagraph * texrowpar = 0;
152 texrowpar = text->FirstParagraph();
155 texrowpar = text->GetParFromID(tmpid);
161 InsetError * new_inset = new InsetError(msgtxt);
162 text->SetCursorIntern(texrowpar, tmppos);
163 text->InsertInset(new_inset);
166 // Restore the cursor position
167 text->SetCursorIntern(cursor.par(), cursor.pos());
171 void BufferView::setCursorFromRow(int row)
176 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
178 LyXParagraph * texrowpar;
181 texrowpar = text->FirstParagraph();
184 texrowpar = text->GetParFromID(tmpid);
186 text->SetCursor(texrowpar, tmppos);
189 bool BufferView::insertInset(Inset * inset, string const & lout,
192 // if we are in a locking inset we should try to insert the
193 // inset there otherwise this is a illegal function now
194 if (the_locking_inset) {
195 if (the_locking_inset->InsertInsetAllowed(inset) &&
196 the_locking_inset->InsertInset(this, inset))
202 // check for table/list in tables
203 if (no_table && text->cursor.par()->table){
204 WriteAlert(_("Impossible Operation!"),
205 _("Cannot insert table/list in table."),
211 // not quite sure if we want this...
212 text->SetCursorParUndo();
218 text->BreakParagraph();
221 if (text->cursor.par()->Last()) {
224 text->BreakParagraph();
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(lay);
242 text->SetParagraph(0, 0,
244 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
250 text->current_font.setLatex(LyXFont::OFF);
253 text->InsertInset(inset);
255 // if we enter a text-inset the cursor should be to the left side
256 // of it! This couldn't happen before as Undo was not handled inside
257 // inset now after the Undo LyX tries to call inset->Edit(...) again
258 // and cannot do this as the cursor is behind the inset and GetInset
259 // does not return the inset!
260 if (inset->IsTextInset()) {
261 if (text->cursor.par()->isRightToLeftPar(buffer()->params))
269 text->UnFreezeUndo();
274 // Open and lock an updatable inset
275 void BufferView::open_new_inset(UpdatableInset * new_inset)
279 insertInset(new_inset);
282 new_inset->Edit(this, 0, 0, 0);
285 /* This is also a buffer property (ale) */
286 // Not so sure about that. a goto Label function can not be buffer local, just
287 // think how this will work in a multiwindo/buffer environment, all the
288 // cursors in all the views showing this buffer will move. (Lgb)
289 // OK, then no cursor action should be allowed in buffer. (ale)
290 bool BufferView::gotoLabel(string const & label)
293 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
294 it != buffer()->inset_iterator_end(); ++it) {
295 vector<string> labels = (*it)->getLabelList();
296 if ( find(labels.begin(),labels.end(),label)
299 text->SetCursor(it.getPar(), it.getPos());
300 text->sel_cursor = text->cursor;
308 void BufferView::allFloats(char flag, char figmar)
310 if (!available()) return;
312 LyXCursor cursor = text->cursor;
314 if (!flag && cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE
316 && cursor.par()->footnotekind != LyXParagraph::FOOTNOTE
317 && cursor.par()->footnotekind != LyXParagraph::MARGIN)
319 && cursor.par()->footnotekind != LyXParagraph::FIG
320 && cursor.par()->footnotekind != LyXParagraph::TAB
321 && cursor.par()->footnotekind != LyXParagraph::WIDE_FIG
322 && cursor.par()->footnotekind != LyXParagraph::WIDE_TAB
323 && cursor.par()->footnotekind != LyXParagraph::ALGORITHM)))
328 LyXCursor tmpcursor = cursor;
329 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
330 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
332 LyXParagraph *par = buffer()->paragraph;
335 if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
337 && par->footnotekind != LyXParagraph::FOOTNOTE
338 && par->footnotekind != LyXParagraph::MARGIN)
340 && par->footnotekind != LyXParagraph::FIG
341 && par->footnotekind != LyXParagraph::TAB
342 && par->footnotekind != LyXParagraph::WIDE_FIG
343 && par->footnotekind != LyXParagraph::WIDE_TAB
344 && par->footnotekind != LyXParagraph::ALGORITHM
349 && par->previous->footnoteflag !=
350 LyXParagraph::CLOSED_FOOTNOTE){ /* should be */
351 text->SetCursorIntern(par->previous,
353 text->OpenFootnote();
357 if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
361 par->footnotekind != LyXParagraph::FOOTNOTE
363 par->footnotekind != LyXParagraph::MARGIN
368 par->footnotekind != LyXParagraph::FIG
370 par->footnotekind != LyXParagraph::TAB
372 par->footnotekind != LyXParagraph::WIDE_FIG
374 par->footnotekind != LyXParagraph::WIDE_TAB
376 par->footnotekind != LyXParagraph::ALGORITHM
380 text->SetCursorIntern(par, 0);
381 text->CloseFootnote();
387 text->SetCursorIntern(cursor.par(), cursor.pos());
394 void BufferView::insertNote()
396 InsetInfo * new_inset = new InsetInfo();
397 insertInset(new_inset);
398 new_inset->Edit(this, 0, 0, 0);
402 void BufferView::openStuff()
405 owner()->getMiniBuffer()->Set(_("Open/Close..."));
416 void BufferView::toggleFloat()
419 owner()->getMiniBuffer()->Set(_("Open/Close..."));
423 text->ToggleFootnote();
429 void BufferView::menuUndo()
432 owner()->getMiniBuffer()->Set(_("Undo"));
436 if (!text->TextUndo())
437 owner()->getMiniBuffer()->Set(_("No further undo information"));
445 void BufferView::menuRedo()
447 if (the_locking_inset) {
448 owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
453 owner()->getMiniBuffer()->Set(_("Redo"));
457 if (!text->TextRedo())
458 owner()->getMiniBuffer()->Set(_("No further redo information"));
466 void BufferView::hyphenationPoint()
471 InsetSpecialChar * new_inset =
472 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
473 insertInset(new_inset);
478 void BufferView::ldots()
483 InsetSpecialChar * new_inset =
484 new InsetSpecialChar(InsetSpecialChar::LDOTS);
485 insertInset(new_inset);
490 void BufferView::endOfSentenceDot()
495 InsetSpecialChar * new_inset =
496 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
497 insertInset(new_inset);
502 void BufferView::menuSeparator()
507 InsetSpecialChar * new_inset =
508 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
509 insertInset(new_inset);
514 void BufferView::newline()
519 text->InsertChar(LyXParagraph::META_NEWLINE);
525 void BufferView::protectedBlank()
530 InsetSpecialChar * new_inset =
531 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
532 insertInset(new_inset);
537 void BufferView::hfill()
542 text->InsertChar(LyXParagraph::META_HFILL);
547 void BufferView::copyEnvironment()
550 text->copyEnvironmentType();
551 // clear the selection, even if mark_set
553 text->ClearSelection();
555 owner()->getMiniBuffer()->Set(_("Paragraph environment type copied"));
560 void BufferView::pasteEnvironment()
563 text->pasteEnvironmentType();
564 owner()->getMiniBuffer()->Set(_("Paragraph environment type set"));
570 void BufferView::copy()
573 text->CopySelection();
574 // clear the selection, even if mark_set
576 text->ClearSelection();
578 owner()->getMiniBuffer()->Set(_("Copy"));
582 void BufferView::cut()
587 text->CutSelection();
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();
606 text->PasteSelection();
609 // clear the selection
611 text->ClearSelection();
616 void BufferView::gotoNote()
618 if (!available()) return;
625 if (!text->GotoNextNote()) {
626 if (text->cursor.pos()
627 || text->cursor.par() != text->FirstParagraph()) {
629 text->cursor.par(text->FirstParagraph());
631 if (!text->GotoNextNote()) {
633 owner()->getMiniBuffer()->Set(_("No more notes"));
637 owner()->getMiniBuffer()->Set(_("No more notes"));
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 char * BufferView::nextWord(float & value)
667 char * string = text->SelectNextWord(value);
673 void BufferView::selectLastWord()
675 if (!available()) return;
679 text->SelectSelectedWord();
680 toggleSelection(false);
685 void BufferView::endOfSpellCheck()
687 if (!available()) return;
691 text->SelectSelectedWord();
692 text->ClearSelection();
697 void BufferView::replaceWord(string const & replacestring)
699 if (!available()) return;
704 /* clear the selection (if there is any) */
705 toggleSelection(false);
708 /* clear the selection (if there is any) */
709 toggleSelection(false);
710 text->ReplaceSelectionWithString(replacestring.c_str());
712 text->SetSelectionOverString(replacestring.c_str());
714 // Go back so that replacement string is also spellchecked
715 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
716 text->CursorLeftIntern();
720 // End of spellchecker stuff
723 bool BufferView::lockInset(UpdatableInset * inset)
725 if (!the_locking_inset && inset) {
726 the_locking_inset = inset;
729 return the_locking_inset->LockInsetInInset(this, inset);
735 void BufferView::showLockedInsetCursor(long x, long y, int asc, int desc)
737 if (the_locking_inset && available()) {
738 LyXCursor cursor = text->cursor;
739 if ((cursor.pos() - 1 >= 0) &&
740 (cursor.par()->GetChar(cursor.pos() - 1) ==
741 LyXParagraph::META_INSET) &&
742 (cursor.par()->GetInset(cursor.pos() - 1) ==
743 the_locking_inset->GetLockingInset()))
744 text->SetCursor(cursor,
745 cursor.par(), cursor.pos() - 1);
746 y += cursor.y() + the_locking_inset->InsetInInsetY();
747 pimpl_->screen->ShowManualCursor(x, y, asc, desc,
748 LyXScreen::BAR_SHAPE);
753 void BufferView::hideLockedInsetCursor()
755 if (the_locking_inset && available()) {
756 pimpl_->screen->HideCursor();
761 void BufferView::fitLockedInsetCursor(long x, long y, int asc, int desc)
763 if (the_locking_inset && available()){
764 y += text->cursor.y() + the_locking_inset->InsetInInsetY();
765 if (pimpl_->screen->FitManualCursor(x, y, asc, desc))
771 int BufferView::unlockInset(UpdatableInset * inset)
773 if (inset && the_locking_inset == inset) {
774 inset->InsetUnlock(this);
775 the_locking_inset = 0;
778 } else if (inset && the_locking_inset &&
779 the_locking_inset->UnlockInsetInInset(this, inset)) {
783 return bufferlist.unlockInset(inset);
787 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
789 if (!the_locking_inset)
790 return; // shouldn't happen
791 if (kind == Undo::EDIT) // in this case insets would not be stored!
795 ParFromPos(text->cursor.pos())->previous,
797 ParFromPos(text->cursor.pos())->next);
801 void BufferView::updateInset(Inset * inset, bool mark_dirty)
806 // first check for locking insets
807 if (the_locking_inset) {
808 if (the_locking_inset == inset) {
809 if (text->UpdateInset(inset)){
812 if (buffer()->isLyxClean())
813 owner()->getMiniBuffer()->
815 buffer()->markDirty();
820 } else if (the_locking_inset->UpdateInsetInInset(this,inset)) {
821 if (text->UpdateInset(the_locking_inset)) {
824 if (buffer()->isLyxClean())
825 owner()->getMiniBuffer()->
827 buffer()->markDirty();
835 // then check the current buffer
839 if (text->UpdateInset(inset)){
849 bool BufferView::ChangeRefs(string const & from, string const & to)
852 LyXParagraph * par = buffer()->paragraph;
853 LyXCursor cursor = text->cursor;
854 LyXCursor tmpcursor = cursor;
855 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
856 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
860 for (LyXParagraph::inset_iterator it = par->inset_iterator_begin();
861 it != par->inset_iterator_end(); ++it) {
862 if ((*it)->LyxCode() == Inset::REF_CODE) {
863 InsetCommand * inset = static_cast<InsetCommand *>(*it);
864 if (inset->getContents() == from) {
865 inset->setContents(to);
872 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
873 // this is possible now, since SetCursor takes
874 // care about footnotes
875 text->SetCursorIntern(par, 0);
876 text->RedoParagraphs(text->cursor,
877 text->cursor.par()->Next());
883 text->SetCursorIntern(cursor.par(), cursor.pos());