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,
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))
201 // check for table/list in tables
202 if (no_table && text->cursor.par->table){
203 WriteAlert(_("Impossible Operation!"),
204 _("Cannot insert table/list in table."),
209 // not quite sure if we want this...
210 text->SetCursorParUndo();
216 text->BreakParagraph();
219 if (text->cursor.par->Last()) {
222 text->BreakParagraph();
226 pair<bool, LyXTextClass::size_type> lres =
227 textclasslist.NumberOfLayout(buffer()->params
229 LyXTextClass::size_type lay;
230 if (lres.first != false) {
234 // layout not fount using default "Standard" (0)
238 text->SetLayout(lay);
240 text->SetParagraph(0, 0,
242 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
248 text->current_font.setLatex(LyXFont::OFF);
251 text->InsertInset(inset);
253 // if we enter a text-inset the cursor should be to the left side
254 // of it! This couldn't happen before as Undo was not handled inside
255 // inset now after the Undo LyX tries to call inset->Edit(...) again
256 // and cannot do this as the cursor is behind the inset and GetInset
257 // does not return the inset!
258 if (inset->IsTextInset()) {
259 if (text->cursor.par->isRightToLeftPar())
267 text->UnFreezeUndo();
272 // Open and lock an updatable inset
273 void BufferView::open_new_inset(UpdatableInset * new_inset)
277 insertInset(new_inset);
280 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(it.getPar(), it.getPos());
298 text->sel_cursor = text->cursor;
306 void BufferView::allFloats(char flag, char figmar)
308 if (!available()) return;
310 LyXCursor cursor = text->cursor;
312 if (!flag && cursor.par->footnoteflag != LyXParagraph::NO_FOOTNOTE
314 && cursor.par->footnotekind != LyXParagraph::FOOTNOTE
315 && cursor.par->footnotekind != LyXParagraph::MARGIN)
317 && cursor.par->footnotekind != LyXParagraph::FIG
318 && cursor.par->footnotekind != LyXParagraph::TAB
319 && cursor.par->footnotekind != LyXParagraph::WIDE_FIG
320 && cursor.par->footnotekind != LyXParagraph::WIDE_TAB
321 && cursor.par->footnotekind != LyXParagraph::ALGORITHM)))
326 LyXCursor tmpcursor = cursor;
327 cursor.par = tmpcursor.par->ParFromPos(tmpcursor.pos);
328 cursor.pos = tmpcursor.par->PositionInParFromPos(tmpcursor.pos);
330 LyXParagraph *par = buffer()->paragraph;
333 if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
335 && par->footnotekind != LyXParagraph::FOOTNOTE
336 && par->footnotekind != LyXParagraph::MARGIN)
338 && par->footnotekind != LyXParagraph::FIG
339 && par->footnotekind != LyXParagraph::TAB
340 && par->footnotekind != LyXParagraph::WIDE_FIG
341 && par->footnotekind != LyXParagraph::WIDE_TAB
342 && par->footnotekind != LyXParagraph::ALGORITHM
347 && par->previous->footnoteflag !=
348 LyXParagraph::CLOSED_FOOTNOTE){ /* should be */
349 text->SetCursorIntern(par->previous,
351 text->OpenFootnote();
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(par, 0);
379 text->CloseFootnote();
385 text->SetCursorIntern(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);
400 void BufferView::openStuff()
403 owner()->getMiniBuffer()->Set(_("Open/Close..."));
414 void BufferView::toggleFloat()
417 owner()->getMiniBuffer()->Set(_("Open/Close..."));
421 text->ToggleFootnote();
427 void BufferView::menuUndo()
430 owner()->getMiniBuffer()->Set(_("Undo"));
434 if (!text->TextUndo())
435 owner()->getMiniBuffer()->Set(_("No further undo information"));
443 void BufferView::menuRedo()
445 if (the_locking_inset) {
446 owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
451 owner()->getMiniBuffer()->Set(_("Redo"));
455 if (!text->TextRedo())
456 owner()->getMiniBuffer()->Set(_("No further redo information"));
464 void BufferView::hyphenationPoint()
469 InsetSpecialChar * new_inset =
470 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
471 insertInset(new_inset);
476 void BufferView::ldots()
481 InsetSpecialChar * new_inset =
482 new InsetSpecialChar(InsetSpecialChar::LDOTS);
483 insertInset(new_inset);
488 void BufferView::endOfSentenceDot()
493 InsetSpecialChar * new_inset =
494 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
495 insertInset(new_inset);
500 void BufferView::menuSeparator()
505 InsetSpecialChar * new_inset =
506 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
507 insertInset(new_inset);
512 void BufferView::newline()
517 text->InsertChar(LyXParagraph::META_NEWLINE);
523 void BufferView::protectedBlank()
528 InsetSpecialChar * new_inset =
529 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
530 insertInset(new_inset);
535 void BufferView::hfill()
540 text->InsertChar(LyXParagraph::META_HFILL);
545 void BufferView::copyEnvironment()
548 text->copyEnvironmentType();
549 // clear the selection, even if mark_set
551 text->ClearSelection();
553 owner()->getMiniBuffer()->Set(_("Paragraph environment type copied"));
558 void BufferView::pasteEnvironment()
561 text->pasteEnvironmentType();
562 owner()->getMiniBuffer()->Set(_("Paragraph environment type set"));
568 void BufferView::copy()
571 text->CopySelection();
572 // clear the selection, even if mark_set
574 text->ClearSelection();
576 owner()->getMiniBuffer()->Set(_("Copy"));
580 void BufferView::cut()
585 text->CutSelection();
587 owner()->getMiniBuffer()->Set(_("Cut"));
592 void BufferView::paste()
594 if (!available()) return;
596 owner()->getMiniBuffer()->Set(_("Paste"));
598 // clear the selection
600 text->ClearSelection();
604 text->PasteSelection();
607 // clear the selection
609 text->ClearSelection();
614 void BufferView::gotoNote()
616 if (!available()) return;
623 if (!text->GotoNextNote()) {
625 || text->cursor.par != text->FirstParagraph()) {
627 text->cursor.par = text->FirstParagraph();
628 text->cursor.pos = 0;
629 if (!text->GotoNextNote()) {
631 owner()->getMiniBuffer()->Set(_("No more notes"));
635 owner()->getMiniBuffer()->Set(_("No more notes"));
640 text->sel_cursor = text->cursor;
644 void BufferView::insertCorrectQuote()
648 if (text->cursor.pos)
649 c = text->cursor.par->GetChar(text->cursor.pos - 1);
653 insertInset(new InsetQuotes(c, buffer()->params));
657 /* these functions are for the spellchecker */
658 char * BufferView::nextWord(float & value)
665 char * string = text->SelectNextWord(value);
671 void BufferView::selectLastWord()
673 if (!available()) return;
677 text->SelectSelectedWord();
678 toggleSelection(false);
683 void BufferView::endOfSpellCheck()
685 if (!available()) return;
689 text->SelectSelectedWord();
690 text->ClearSelection();
695 void BufferView::replaceWord(string const & replacestring)
697 if (!available()) return;
702 /* clear the selection (if there is any) */
703 toggleSelection(false);
706 /* clear the selection (if there is any) */
707 toggleSelection(false);
708 text->ReplaceSelectionWithString(replacestring.c_str());
710 text->SetSelectionOverString(replacestring.c_str());
712 // Go back so that replacement string is also spellchecked
713 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
714 text->CursorLeftIntern();
718 // End of spellchecker stuff
721 bool BufferView::lockInset(UpdatableInset * inset)
723 if (!the_locking_inset && inset) {
724 the_locking_inset = inset;
727 return the_locking_inset->LockInsetInInset(this, inset);
733 void BufferView::showLockedInsetCursor(long x, long y, int asc, int desc)
735 if (the_locking_inset && 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 the_locking_inset->GetLockingInset()))
742 text->SetCursor(cursor, cursor.par, cursor.pos-1);
743 y += cursor.y + the_locking_inset->InsetInInsetY();
744 pimpl_->screen->ShowManualCursor(x, y, asc, desc,
745 LyXScreen::BAR_SHAPE);
750 void BufferView::hideLockedInsetCursor()
752 if (the_locking_inset && available()) {
753 pimpl_->screen->HideCursor();
758 void BufferView::fitLockedInsetCursor(long x, long y, int asc, int desc)
760 if (the_locking_inset && available()){
761 y += text->cursor.y + the_locking_inset->InsetInInsetY();
762 if (pimpl_->screen->FitManualCursor(x, y, asc, desc))
768 int BufferView::unlockInset(UpdatableInset * inset)
770 if (inset && the_locking_inset == inset) {
771 inset->InsetUnlock(this);
772 the_locking_inset = 0;
775 } else if (inset && the_locking_inset &&
776 the_locking_inset->UnlockInsetInInset(this, inset)) {
780 return bufferlist.unlockInset(inset);
784 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
786 if (!the_locking_inset)
787 return; // shouldn't happen
788 if (kind == Undo::EDIT) // in this case insets would not be stored!
792 ParFromPos(text->cursor.pos)->previous,
794 ParFromPos(text->cursor.pos)->next);
798 void BufferView::updateInset(Inset * inset, bool mark_dirty)
803 // first check for locking insets
804 if (the_locking_inset) {
805 if (the_locking_inset == inset) {
806 if (text->UpdateInset(inset)){
809 if (buffer()->isLyxClean())
810 owner()->getMiniBuffer()->
812 buffer()->markDirty();
817 } else if (the_locking_inset->UpdateInsetInInset(this,inset)) {
818 if (text->UpdateInset(the_locking_inset)) {
821 if (buffer()->isLyxClean())
822 owner()->getMiniBuffer()->
824 buffer()->markDirty();
832 // then check the current buffer
836 if (text->UpdateInset(inset)){
846 bool BufferView::ChangeRefs(string const & from, string const & to)
849 LyXParagraph * par = buffer()->paragraph;
850 LyXCursor cursor = text->cursor;
851 LyXCursor tmpcursor = cursor;
852 cursor.par = tmpcursor.par->ParFromPos(tmpcursor.pos);
853 cursor.pos = tmpcursor.par->PositionInParFromPos(tmpcursor.pos);
857 for (LyXParagraph::inset_iterator it = par->inset_iterator_begin();
858 it != par->inset_iterator_end(); ++it) {
859 if ((*it)->LyxCode() == Inset::REF_CODE) {
860 InsetCommand * inset = static_cast<InsetCommand *>(*it);
861 if (inset->getContents() == from) {
862 inset->setContents(to);
869 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
870 // this is possible now, since SetCursor takes
871 // care about footnotes
872 text->SetCursorIntern(par, 0);
873 text->RedoParagraphs(text->cursor,
874 text->cursor.par->Next());
880 text->SetCursorIntern(cursor.par, cursor.pos);