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 void BufferView::insertInset(Inset * inset, string const & lout,
190 // check for table/list in tables
191 if (no_table && text->cursor.par->table){
192 WriteAlert(_("Impossible Operation!"),
193 _("Cannot insert table/list in table."),
197 // not quite sure if we want this...
198 text->SetCursorParUndo();
204 text->BreakParagraph();
207 if (text->cursor.par->Last()) {
210 text->BreakParagraph();
214 pair<bool, LyXTextClass::size_type> lres =
215 textclasslist.NumberOfLayout(buffer()->params
217 LyXTextClass::size_type lay;
218 if (lres.first != false) {
222 // layout not fount using default "Standard" (0)
226 text->SetLayout(lay);
228 text->SetParagraph(0, 0,
230 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
236 text->current_font.setLatex(LyXFont::OFF);
239 text->InsertInset(inset);
241 // if we enter a text-inset the cursor should be to the left side
242 // of it! This couldn't happen before as Undo was not handled inside
243 // inset now after the Undo LyX tries to call inset->Edit(...) again
244 // and cannot do this as the cursor is behind the inset and GetInset
245 // does not return the inset!
246 if (inset->IsTextInset()) {
247 if (text->cursor.par->getParDirection()==LYX_DIR_LEFT_TO_RIGHT)
255 text->UnFreezeUndo();
259 // Open and lock an updatable inset
260 void BufferView::open_new_inset(UpdatableInset * new_inset)
264 insertInset(new_inset);
267 new_inset->Edit(this, 0, 0, 0);
270 /* This is also a buffer property (ale) */
271 // Not so sure about that. a goto Label function can not be buffer local, just
272 // think how this will work in a multiwindo/buffer environment, all the
273 // cursors in all the views showing this buffer will move. (Lgb)
274 // OK, then no cursor action should be allowed in buffer. (ale)
275 bool BufferView::gotoLabel(string const & label)
278 LyXParagraph * par = buffer()->paragraph;
279 LyXParagraph::size_type pos;
283 while ((inset = par->ReturnNextInsetPointer(pos))){
284 for (int i = 0; i < inset->GetNumberOfLabels(); ++i) {
285 if (label == inset->getLabel(i)) {
287 text->SetCursor(par, pos);
288 text->sel_cursor = text->cursor;
300 void BufferView::allFloats(char flag, char figmar)
302 if (!available()) return;
304 LyXCursor cursor = text->cursor;
306 if (!flag && cursor.par->footnoteflag != LyXParagraph::NO_FOOTNOTE
308 && cursor.par->footnotekind != LyXParagraph::FOOTNOTE
309 && cursor.par->footnotekind != LyXParagraph::MARGIN)
311 && cursor.par->footnotekind != LyXParagraph::FIG
312 && cursor.par->footnotekind != LyXParagraph::TAB
313 && cursor.par->footnotekind != LyXParagraph::WIDE_FIG
314 && cursor.par->footnotekind != LyXParagraph::WIDE_TAB
315 && cursor.par->footnotekind != LyXParagraph::ALGORITHM)))
320 LyXCursor tmpcursor = cursor;
321 cursor.par = tmpcursor.par->ParFromPos(tmpcursor.pos);
322 cursor.pos = tmpcursor.par->PositionInParFromPos(tmpcursor.pos);
324 LyXParagraph *par = buffer()->paragraph;
327 if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
329 && par->footnotekind != LyXParagraph::FOOTNOTE
330 && par->footnotekind != LyXParagraph::MARGIN)
332 && par->footnotekind != LyXParagraph::FIG
333 && par->footnotekind != LyXParagraph::TAB
334 && par->footnotekind != LyXParagraph::WIDE_FIG
335 && par->footnotekind != LyXParagraph::WIDE_TAB
336 && par->footnotekind != LyXParagraph::ALGORITHM
341 && par->previous->footnoteflag !=
342 LyXParagraph::CLOSED_FOOTNOTE){ /* should be */
343 text->SetCursorIntern(par->previous,
345 text->OpenFootnote();
349 if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
353 par->footnotekind != LyXParagraph::FOOTNOTE
355 par->footnotekind != LyXParagraph::MARGIN
360 par->footnotekind != LyXParagraph::FIG
362 par->footnotekind != LyXParagraph::TAB
364 par->footnotekind != LyXParagraph::WIDE_FIG
366 par->footnotekind != LyXParagraph::WIDE_TAB
368 par->footnotekind != LyXParagraph::ALGORITHM
372 text->SetCursorIntern(par, 0);
373 text->CloseFootnote();
379 text->SetCursorIntern(cursor.par, cursor.pos);
386 void BufferView::insertNote()
388 InsetInfo * new_inset = new InsetInfo();
389 insertInset(new_inset);
390 new_inset->Edit(this, 0, 0, 0);
394 void BufferView::openStuff()
397 owner()->getMiniBuffer()->Set(_("Open/Close..."));
408 void BufferView::toggleFloat()
411 owner()->getMiniBuffer()->Set(_("Open/Close..."));
415 text->ToggleFootnote();
421 void BufferView::menuUndo()
424 owner()->getMiniBuffer()->Set(_("Undo"));
428 if (!text->TextUndo())
429 owner()->getMiniBuffer()->Set(_("No further undo information"));
437 void BufferView::menuRedo()
439 if (the_locking_inset) {
440 owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
445 owner()->getMiniBuffer()->Set(_("Redo"));
449 if (!text->TextRedo())
450 owner()->getMiniBuffer()->Set(_("No further redo information"));
458 void BufferView::hyphenationPoint()
463 InsetSpecialChar * new_inset =
464 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
465 insertInset(new_inset);
470 void BufferView::ldots()
475 InsetSpecialChar * new_inset =
476 new InsetSpecialChar(InsetSpecialChar::LDOTS);
477 insertInset(new_inset);
482 void BufferView::endOfSentenceDot()
487 InsetSpecialChar * new_inset =
488 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
489 insertInset(new_inset);
494 void BufferView::menuSeparator()
499 InsetSpecialChar * new_inset =
500 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
501 insertInset(new_inset);
506 void BufferView::newline()
512 InsetSpecialChar * new_inset =
513 new InsetSpecialChar(InsetSpecialChar::NEWLINE);
514 insertInset(new_inset);
516 text->InsertChar(LyXParagraph::META_NEWLINE);
523 void BufferView::protectedBlank()
529 InsetSpecialChar * new_inset =
530 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
531 insertInset(new_inset);
533 text->InsertChar(LyXParagraph::META_PROTECTED_SEPARATOR);
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
727 /* these functions return 1 if an error occured,
729 int BufferView::lockInset(UpdatableInset * inset)
731 if (!the_locking_inset && inset){
732 the_locking_inset = inset;
739 void BufferView::showLockedInsetCursor(long x, long y, int asc, int desc)
741 if (the_locking_inset && available()) {
743 pimpl_->screen->ShowManualCursor(x, y, asc, desc,
744 LyXScreen::BAR_SHAPE);
749 void BufferView::hideLockedInsetCursor()
751 if (the_locking_inset && available()) {
752 pimpl_->screen->HideCursor();
757 void BufferView::fitLockedInsetCursor(long x, long y, int asc, int desc)
759 if (the_locking_inset && available()){
761 if (pimpl_->screen->FitManualCursor(x, y, asc, desc))
767 int BufferView::unlockInset(UpdatableInset * inset)
769 if (inset && the_locking_inset == inset) {
770 inset->InsetUnlock(this);
771 the_locking_inset = 0;
774 } else if (inset && the_locking_inset &&
775 the_locking_inset->UnlockInsetInInset(this, inset)) {
779 return bufferlist.unlockInset(inset);
783 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
785 if (!the_locking_inset)
786 return; // shouldn't happen
787 if (kind == Undo::EDIT) // in this case insets would not be stored!
791 ParFromPos(text->cursor.pos)->previous,
793 ParFromPos(text->cursor.pos)->next);
797 void BufferView::updateInset(Inset * inset, bool mark_dirty)
802 // first check for locking insets
803 if (the_locking_inset) {
804 if (the_locking_inset == inset) {
805 if (text->UpdateInset(inset)){
808 if (buffer()->isLyxClean())
809 owner()->getMiniBuffer()->
811 buffer()->markDirty();
816 } else if (the_locking_inset->UpdateInsetInInset(this,inset)) {
817 if (text->UpdateInset(the_locking_inset)) {
820 if (buffer()->isLyxClean())
821 owner()->getMiniBuffer()->
823 buffer()->markDirty();
831 // then check the current buffer
835 if (text->UpdateInset(inset)){