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 * ====================================================== */
16 #include "BufferView.h"
18 #include "lyxcursor.h"
20 #include "insets/inseterror.h"
21 #include "insets/insetinfo.h"
22 #include "insets/insetspecialchar.h"
24 #include "minibuffer.h"
25 #include "bufferlist.h"
26 #include "support/FileInfo.h"
27 #include "lyxscreen.h"
28 #include "support/filetools.h"
29 #include "lyx_gui_misc.h"
31 #include "BufferView_pimpl.h"
33 extern BufferList bufferlist;
39 // Inserts a file into current document
40 bool BufferView::insertLyXFile(string const & filen)
42 // Copyright CHT Software Service GmbH
45 // Insert a Lyxformat - file into current buffer
47 // Moved from lyx_cb.C (Lgb)
49 if (filen.empty()) return false;
51 string fname = MakeAbsPath(filen);
53 // check if file exist
57 WriteAlert(_("Error!"),
58 _("Specified file is unreadable: "),
59 MakeDisplayPath(fname, 50));
65 ifstream ifs(fname.c_str());
67 WriteAlert(_("Error!"),
68 _("Cannot open specified file: "),
69 MakeDisplayPath(fname, 50));
80 lyxerr.debug() << "Will insert file with header" << endl;
81 res = buffer()->readFile(lex, text->cursor.par);
83 lyxerr.debug() << "Will insert file without header" << endl;
84 res = buffer()->readLyXformat2(lex, text->cursor.par);
91 bool BufferView::removeAutoInsets()
93 LyXParagraph * par = buffer()->paragraph;
95 LyXCursor cursor = text->cursor;
96 LyXCursor tmpcursor = cursor;
97 cursor.par = tmpcursor.par->ParFromPos(tmpcursor.pos);
98 cursor.pos = tmpcursor.par->PositionInParFromPos(tmpcursor.pos);
102 if (par->AutoDeleteInsets()){
104 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
105 // this is possible now, since SetCursor takes
106 // care about footnotes
107 text->SetCursorIntern(par, 0);
108 text->RedoParagraphs(text->cursor,
109 text->cursor.par->Next());
115 // avoid forbidden cursor positions caused by error removing
116 if (cursor.pos > cursor.par->Last())
117 cursor.pos = cursor.par->Last();
118 text->SetCursorIntern(cursor.par, cursor.pos);
124 void BufferView::insertErrors(TeXErrors & terr)
126 // Save the cursor position
127 LyXCursor cursor = text->cursor;
129 // This is drastic, but it's the only fix, I could find. (Asger)
133 for (TeXErrors::Errors::const_iterator cit = terr.begin();
136 string desctext((*cit).error_desc);
137 string errortext((*cit).error_text);
138 string msgtxt = desctext + '\n' + errortext;
139 int errorrow = (*cit).error_in_line;
141 // Insert error string for row number
145 buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos);
147 LyXParagraph * texrowpar = 0;
150 texrowpar = text->FirstParagraph();
153 texrowpar = text->GetParFromID(tmpid);
159 InsetError * new_inset = new InsetError(msgtxt);
160 text->SetCursorIntern(texrowpar, tmppos);
161 text->InsertInset(new_inset);
164 // Restore the cursor position
165 text->SetCursorIntern(cursor.par, cursor.pos);
169 void BufferView::setCursorFromRow(int row)
174 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
176 LyXParagraph * texrowpar;
179 texrowpar = text->FirstParagraph();
182 texrowpar = text->GetParFromID(tmpid);
184 text->SetCursor(texrowpar, tmppos);
187 bool BufferView::insertInset(Inset * inset, string const & lout,
190 // if we are in a locking inset we should try to insert the
191 // inset there otherwise this is a illegal function now
192 if (the_locking_inset) {
193 if (the_locking_inset->InsertInsetAllowed(inset) &&
194 the_locking_inset->InsertInset(this, inset))
199 // check for table/list in tables
200 if (no_table && text->cursor.par->table){
201 WriteAlert(_("Impossible Operation!"),
202 _("Cannot insert table/list in table."),
207 // not quite sure if we want this...
208 text->SetCursorParUndo();
214 text->BreakParagraph();
217 if (text->cursor.par->Last()) {
220 text->BreakParagraph();
224 pair<bool, LyXTextClass::size_type> lres =
225 textclasslist.NumberOfLayout(buffer()->params
227 LyXTextClass::size_type lay;
228 if (lres.first != false) {
232 // layout not fount using default "Standard" (0)
236 text->SetLayout(lay);
238 text->SetParagraph(0, 0,
240 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
246 text->current_font.setLatex(LyXFont::OFF);
249 text->InsertInset(inset);
251 // if we enter a text-inset the cursor should be to the left side
252 // of it! This couldn't happen before as Undo was not handled inside
253 // inset now after the Undo LyX tries to call inset->Edit(...) again
254 // and cannot do this as the cursor is behind the inset and GetInset
255 // does not return the inset!
256 if (inset->IsTextInset()) {
257 if (text->cursor.par->isRightToLeftPar())
265 text->UnFreezeUndo();
270 // Open and lock an updatable inset
271 void BufferView::open_new_inset(UpdatableInset * new_inset)
275 insertInset(new_inset);
278 new_inset->Edit(this, 0, 0, 0);
281 /* This is also a buffer property (ale) */
282 // Not so sure about that. a goto Label function can not be buffer local, just
283 // think how this will work in a multiwindo/buffer environment, all the
284 // cursors in all the views showing this buffer will move. (Lgb)
285 // OK, then no cursor action should be allowed in buffer. (ale)
286 bool BufferView::gotoLabel(string const & label)
289 LyXParagraph * par = buffer()->paragraph;
290 LyXParagraph::size_type pos;
294 while ((inset = par->ReturnNextInsetPointer(pos))){
295 for (int i = 0; i < inset->GetNumberOfLabels(); ++i) {
296 if (label == inset->getLabel(i)) {
298 text->SetCursor(par, pos);
299 text->sel_cursor = text->cursor;
311 void BufferView::allFloats(char flag, char figmar)
313 if (!available()) return;
315 LyXCursor cursor = text->cursor;
317 if (!flag && cursor.par->footnoteflag != LyXParagraph::NO_FOOTNOTE
319 && cursor.par->footnotekind != LyXParagraph::FOOTNOTE
320 && cursor.par->footnotekind != LyXParagraph::MARGIN)
322 && cursor.par->footnotekind != LyXParagraph::FIG
323 && cursor.par->footnotekind != LyXParagraph::TAB
324 && cursor.par->footnotekind != LyXParagraph::WIDE_FIG
325 && cursor.par->footnotekind != LyXParagraph::WIDE_TAB
326 && cursor.par->footnotekind != LyXParagraph::ALGORITHM)))
331 LyXCursor tmpcursor = cursor;
332 cursor.par = tmpcursor.par->ParFromPos(tmpcursor.pos);
333 cursor.pos = tmpcursor.par->PositionInParFromPos(tmpcursor.pos);
335 LyXParagraph *par = buffer()->paragraph;
338 if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
340 && par->footnotekind != LyXParagraph::FOOTNOTE
341 && par->footnotekind != LyXParagraph::MARGIN)
343 && par->footnotekind != LyXParagraph::FIG
344 && par->footnotekind != LyXParagraph::TAB
345 && par->footnotekind != LyXParagraph::WIDE_FIG
346 && par->footnotekind != LyXParagraph::WIDE_TAB
347 && par->footnotekind != LyXParagraph::ALGORITHM
352 && par->previous->footnoteflag !=
353 LyXParagraph::CLOSED_FOOTNOTE){ /* should be */
354 text->SetCursorIntern(par->previous,
356 text->OpenFootnote();
360 if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
364 par->footnotekind != LyXParagraph::FOOTNOTE
366 par->footnotekind != LyXParagraph::MARGIN
371 par->footnotekind != LyXParagraph::FIG
373 par->footnotekind != LyXParagraph::TAB
375 par->footnotekind != LyXParagraph::WIDE_FIG
377 par->footnotekind != LyXParagraph::WIDE_TAB
379 par->footnotekind != LyXParagraph::ALGORITHM
383 text->SetCursorIntern(par, 0);
384 text->CloseFootnote();
390 text->SetCursorIntern(cursor.par, cursor.pos);
397 void BufferView::insertNote()
399 InsetInfo * new_inset = new InsetInfo();
400 insertInset(new_inset);
401 new_inset->Edit(this, 0, 0, 0);
405 void BufferView::openStuff()
408 owner()->getMiniBuffer()->Set(_("Open/Close..."));
419 void BufferView::toggleFloat()
422 owner()->getMiniBuffer()->Set(_("Open/Close..."));
426 text->ToggleFootnote();
432 void BufferView::menuUndo()
435 owner()->getMiniBuffer()->Set(_("Undo"));
439 if (!text->TextUndo())
440 owner()->getMiniBuffer()->Set(_("No further undo information"));
448 void BufferView::menuRedo()
450 if (the_locking_inset) {
451 owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
456 owner()->getMiniBuffer()->Set(_("Redo"));
460 if (!text->TextRedo())
461 owner()->getMiniBuffer()->Set(_("No further redo information"));
469 void BufferView::hyphenationPoint()
474 InsetSpecialChar * new_inset =
475 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
476 insertInset(new_inset);
481 void BufferView::ldots()
486 InsetSpecialChar * new_inset =
487 new InsetSpecialChar(InsetSpecialChar::LDOTS);
488 insertInset(new_inset);
493 void BufferView::endOfSentenceDot()
498 InsetSpecialChar * new_inset =
499 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
500 insertInset(new_inset);
505 void BufferView::menuSeparator()
510 InsetSpecialChar * new_inset =
511 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
512 insertInset(new_inset);
517 void BufferView::newline()
522 text->InsertChar(LyXParagraph::META_NEWLINE);
528 void BufferView::protectedBlank()
533 InsetSpecialChar * new_inset =
534 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
535 insertInset(new_inset);
540 void BufferView::hfill()
545 text->InsertChar(LyXParagraph::META_HFILL);
550 void BufferView::copyEnvironment()
553 text->copyEnvironmentType();
554 // clear the selection, even if mark_set
556 text->ClearSelection();
558 owner()->getMiniBuffer()->Set(_("Paragraph environment type copied"));
563 void BufferView::pasteEnvironment()
566 text->pasteEnvironmentType();
567 owner()->getMiniBuffer()->Set(_("Paragraph environment type set"));
573 void BufferView::copy()
576 text->CopySelection();
577 // clear the selection, even if mark_set
579 text->ClearSelection();
581 owner()->getMiniBuffer()->Set(_("Copy"));
585 void BufferView::cut()
590 text->CutSelection();
592 owner()->getMiniBuffer()->Set(_("Cut"));
597 void BufferView::paste()
599 if (!available()) return;
601 owner()->getMiniBuffer()->Set(_("Paste"));
603 // clear the selection
605 text->ClearSelection();
609 text->PasteSelection();
612 // clear the selection
614 text->ClearSelection();
619 void BufferView::gotoNote()
621 if (!available()) return;
628 if (!text->GotoNextNote()) {
630 || text->cursor.par != text->FirstParagraph()) {
632 text->cursor.par = text->FirstParagraph();
633 text->cursor.pos = 0;
634 if (!text->GotoNextNote()) {
636 owner()->getMiniBuffer()->Set(_("No more notes"));
640 owner()->getMiniBuffer()->Set(_("No more notes"));
645 text->sel_cursor = text->cursor;
649 void BufferView::insertCorrectQuote()
653 if (text->cursor.pos)
654 c = text->cursor.par->GetChar(text->cursor.pos - 1);
658 insertInset(new InsetQuotes(c, buffer()->params));
662 /* these functions are for the spellchecker */
663 char * BufferView::nextWord(float & value)
670 char * string = text->SelectNextWord(value);
676 void BufferView::selectLastWord()
678 if (!available()) return;
682 text->SelectSelectedWord();
683 toggleSelection(false);
688 void BufferView::endOfSpellCheck()
690 if (!available()) return;
694 text->SelectSelectedWord();
695 text->ClearSelection();
700 void BufferView::replaceWord(string const & replacestring)
702 if (!available()) return;
707 /* clear the selection (if there is any) */
708 toggleSelection(false);
711 /* clear the selection (if there is any) */
712 toggleSelection(false);
713 text->ReplaceSelectionWithString(replacestring.c_str());
715 text->SetSelectionOverString(replacestring.c_str());
717 // Go back so that replacement string is also spellchecked
718 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
719 text->CursorLeftIntern();
723 // End of spellchecker stuff
726 bool BufferView::lockInset(UpdatableInset * inset)
728 if (!the_locking_inset && inset) {
729 the_locking_inset = inset;
732 return the_locking_inset->LockInsetInInset(this, inset);
738 void BufferView::showLockedInsetCursor(long x, long y, int asc, int desc)
740 if (the_locking_inset && available()) {
741 LyXCursor cursor = text->cursor;
742 if ((cursor.pos - 1 >= 0) &&
743 (cursor.par->GetChar(cursor.pos-1) ==
744 LyXParagraph::META_INSET) &&
745 (cursor.par->GetInset(cursor.pos - 1) ==
746 the_locking_inset->GetLockingInset()))
747 text->SetCursor(cursor, cursor.par, cursor.pos-1);
748 y += cursor.y + the_locking_inset->InsetInInsetY();
749 pimpl_->screen->ShowManualCursor(x, y, asc, desc,
750 LyXScreen::BAR_SHAPE);
755 void BufferView::hideLockedInsetCursor()
757 if (the_locking_inset && available()) {
758 pimpl_->screen->HideCursor();
763 void BufferView::fitLockedInsetCursor(long x, long y, int asc, int desc)
765 if (the_locking_inset && available()){
766 y += text->cursor.y + the_locking_inset->InsetInInsetY();
767 if (pimpl_->screen->FitManualCursor(x, y, asc, desc))
773 int BufferView::unlockInset(UpdatableInset * inset)
775 if (inset && the_locking_inset == inset) {
776 inset->InsetUnlock(this);
777 the_locking_inset = 0;
780 } else if (inset && the_locking_inset &&
781 the_locking_inset->UnlockInsetInInset(this, inset)) {
785 return bufferlist.unlockInset(inset);
789 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
791 if (!the_locking_inset)
792 return; // shouldn't happen
793 if (kind == Undo::EDIT) // in this case insets would not be stored!
797 ParFromPos(text->cursor.pos)->previous,
799 ParFromPos(text->cursor.pos)->next);
803 void BufferView::updateInset(Inset * inset, bool mark_dirty)
808 // first check for locking insets
809 if (the_locking_inset) {
810 if (the_locking_inset == inset) {
811 if (text->UpdateInset(inset)){
814 if (buffer()->isLyxClean())
815 owner()->getMiniBuffer()->
817 buffer()->markDirty();
822 } else if (the_locking_inset->UpdateInsetInInset(this,inset)) {
823 if (text->UpdateInset(the_locking_inset)) {
826 if (buffer()->isLyxClean())
827 owner()->getMiniBuffer()->
829 buffer()->markDirty();
837 // then check the current buffer
841 if (text->UpdateInset(inset)){