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(this, cursor, par, 0);
107 if (par->AutoDeleteInsets()){
109 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
110 text->RedoParagraphs(this, cursor,
111 cursor.par()->Next());
112 text->FullRebreak(this);
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(this, 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(this, texrowpar, tmppos);
163 text->InsertInset(this, new_inset);
164 text->FullRebreak(this);
166 // Restore the cursor position
167 text->SetCursorIntern(this, 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(this, 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(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);
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))
262 text->CursorRight(this);
264 text->CursorLeft(this);
267 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
269 text->UnFreezeUndo();
274 // Open and lock an updatable inset
275 bool BufferView::open_new_inset(UpdatableInset * new_inset)
279 if (!insertInset(new_inset))
281 text->CursorLeft(this);
282 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
283 new_inset->Edit(this, 0, 0, 0);
286 /* This is also a buffer property (ale) */
287 // Not so sure about that. a goto Label function can not be buffer local, just
288 // think how this will work in a multiwindo/buffer environment, all the
289 // cursors in all the views showing this buffer will move. (Lgb)
290 // OK, then no cursor action should be allowed in buffer. (ale)
291 bool BufferView::gotoLabel(string const & label)
294 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
295 it != buffer()->inset_iterator_end(); ++it) {
296 vector<string> labels = (*it)->getLabelList();
297 if ( find(labels.begin(),labels.end(),label)
300 text->SetCursor(this, it.getPar(), it.getPos());
301 text->sel_cursor = text->cursor;
302 update(BufferView::SELECT|BufferView::FITCUR);
309 void BufferView::allFloats(char flag, char figmar)
311 if (!available()) return;
313 LyXCursor cursor = text->cursor;
315 if (!flag && cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE
317 && cursor.par()->footnotekind != LyXParagraph::FOOTNOTE
318 && cursor.par()->footnotekind != LyXParagraph::MARGIN)
320 && cursor.par()->footnotekind != LyXParagraph::FIG
321 && cursor.par()->footnotekind != LyXParagraph::TAB
322 && cursor.par()->footnotekind != LyXParagraph::WIDE_FIG
323 && cursor.par()->footnotekind != LyXParagraph::WIDE_TAB
324 && cursor.par()->footnotekind != LyXParagraph::ALGORITHM)))
329 LyXCursor tmpcursor = cursor;
330 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
331 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
333 LyXParagraph *par = buffer()->paragraph;
336 if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
338 && par->footnotekind != LyXParagraph::FOOTNOTE
339 && par->footnotekind != LyXParagraph::MARGIN)
341 && par->footnotekind != LyXParagraph::FIG
342 && par->footnotekind != LyXParagraph::TAB
343 && par->footnotekind != LyXParagraph::WIDE_FIG
344 && par->footnotekind != LyXParagraph::WIDE_TAB
345 && par->footnotekind != LyXParagraph::ALGORITHM
350 && par->previous->footnoteflag !=
351 LyXParagraph::CLOSED_FOOTNOTE){ /* should be */
352 text->SetCursorIntern(this,
355 text->OpenFootnote(this);
359 if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
363 par->footnotekind != LyXParagraph::FOOTNOTE
365 par->footnotekind != LyXParagraph::MARGIN
370 par->footnotekind != LyXParagraph::FIG
372 par->footnotekind != LyXParagraph::TAB
374 par->footnotekind != LyXParagraph::WIDE_FIG
376 par->footnotekind != LyXParagraph::WIDE_TAB
378 par->footnotekind != LyXParagraph::ALGORITHM
382 text->SetCursorIntern(this, par, 0);
383 text->CloseFootnote(this);
389 text->SetCursorIntern(this, cursor.par(), cursor.pos());
396 void BufferView::insertNote()
398 InsetInfo * new_inset = new InsetInfo();
399 insertInset(new_inset);
400 new_inset->Edit(this, 0, 0, 0);
404 void BufferView::openStuff()
407 owner()->getMiniBuffer()->Set(_("Open/Close..."));
410 update(BufferView::SELECT|BufferView::FITCUR);
411 text->OpenStuff(this);
412 update(BufferView::SELECT|BufferView::FITCUR);
418 void BufferView::toggleFloat()
421 owner()->getMiniBuffer()->Set(_("Open/Close..."));
424 update(BufferView::SELECT|BufferView::FITCUR);
425 text->ToggleFootnote(this);
426 update(BufferView::SELECT|BufferView::FITCUR);
431 void BufferView::menuUndo()
434 owner()->getMiniBuffer()->Set(_("Undo"));
437 update(BufferView::SELECT|BufferView::FITCUR);
438 if (!text->TextUndo(this))
439 owner()->getMiniBuffer()->Set(_("No further undo information"));
441 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
447 void BufferView::menuRedo()
449 if (the_locking_inset) {
450 owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
455 owner()->getMiniBuffer()->Set(_("Redo"));
458 update(BufferView::SELECT|BufferView::FITCUR);
459 if (!text->TextRedo(this))
460 owner()->getMiniBuffer()->Set(_("No further redo information"));
462 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
468 void BufferView::hyphenationPoint()
472 update(BufferView::SELECT|BufferView::FITCUR);
473 InsetSpecialChar * new_inset =
474 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
475 insertInset(new_inset);
480 void BufferView::ldots()
484 update(BufferView::SELECT|BufferView::FITCUR);
485 InsetSpecialChar * new_inset =
486 new InsetSpecialChar(InsetSpecialChar::LDOTS);
487 insertInset(new_inset);
492 void BufferView::endOfSentenceDot()
496 update(BufferView::SELECT|BufferView::FITCUR);
497 InsetSpecialChar * new_inset =
498 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
499 insertInset(new_inset);
504 void BufferView::menuSeparator()
508 update(BufferView::SELECT|BufferView::FITCUR);
509 InsetSpecialChar * new_inset =
510 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
511 insertInset(new_inset);
516 void BufferView::newline()
520 update(BufferView::SELECT|BufferView::FITCUR);
521 text->InsertChar(this, LyXParagraph::META_NEWLINE);
522 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
527 void BufferView::protectedBlank()
531 update(BufferView::SELECT|BufferView::FITCUR);
532 InsetSpecialChar * new_inset =
533 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
534 insertInset(new_inset);
539 void BufferView::hfill()
543 update(BufferView::SELECT|BufferView::FITCUR);
544 text->InsertChar(this, LyXParagraph::META_HFILL);
545 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
549 void BufferView::copyEnvironment()
552 text->copyEnvironmentType();
553 // clear the selection, even if mark_set
555 text->ClearSelection();
556 update(BufferView::SELECT|BufferView::FITCUR);
557 owner()->getMiniBuffer()->Set(_("Paragraph environment type copied"));
562 void BufferView::pasteEnvironment()
565 text->pasteEnvironmentType(this);
566 owner()->getMiniBuffer()->Set(_("Paragraph environment type set"));
567 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
572 void BufferView::copy()
575 text->CopySelection(this);
576 // clear the selection, even if mark_set
578 text->ClearSelection();
579 update(BufferView::SELECT|BufferView::FITCUR);
580 owner()->getMiniBuffer()->Set(_("Copy"));
584 void BufferView::cut()
588 update(BufferView::SELECT|BufferView::FITCUR);
589 text->CutSelection(this);
590 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
591 owner()->getMiniBuffer()->Set(_("Cut"));
596 void BufferView::paste()
598 if (!available()) return;
600 owner()->getMiniBuffer()->Set(_("Paste"));
602 // clear the selection
604 text->ClearSelection();
605 update(BufferView::SELECT|BufferView::FITCUR);
608 text->PasteSelection(this);
609 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
611 // clear the selection
613 text->ClearSelection();
614 update(BufferView::SELECT|BufferView::FITCUR);
618 void BufferView::gotoNote()
620 if (!available()) return;
624 update(BufferView::SELECT|BufferView::FITCUR);
627 if (!text->GotoNextNote(this)) {
628 if (text->cursor.pos()
629 || text->cursor.par() != text->FirstParagraph()) {
631 text->cursor.par(text->FirstParagraph());
633 if (!text->GotoNextNote(this)) {
635 owner()->getMiniBuffer()->Set(_("No more notes"));
639 owner()->getMiniBuffer()->Set(_("No more notes"));
643 update(BufferView::SELECT|BufferView::FITCUR);
644 text->sel_cursor = text->cursor;
648 void BufferView::insertCorrectQuote()
652 if (text->cursor.pos())
653 c = text->cursor.par()->GetChar(text->cursor.pos() - 1);
657 insertInset(new InsetQuotes(c, buffer()->params));
661 /* these functions are for the spellchecker */
662 char * BufferView::nextWord(float & value)
669 char * string = text->SelectNextWord(this, value);
675 void BufferView::selectLastWord()
677 if (!available()) return;
681 text->SelectSelectedWord(this);
682 toggleSelection(false);
683 update(BufferView::SELECT|BufferView::FITCUR);
687 void BufferView::endOfSpellCheck()
689 if (!available()) return;
693 text->SelectSelectedWord(this);
694 text->ClearSelection();
695 update(BufferView::SELECT|BufferView::FITCUR);
699 void BufferView::replaceWord(string const & replacestring)
701 if (!available()) return;
704 update(BufferView::SELECT|BufferView::FITCUR);
706 /* clear the selection (if there is any) */
707 toggleSelection(false);
708 update(BufferView::SELECT|BufferView::FITCUR);
710 /* clear the selection (if there is any) */
711 toggleSelection(false);
712 text->ReplaceSelectionWithString(this, replacestring.c_str());
714 text->SetSelectionOverString(this, replacestring.c_str());
716 // Go back so that replacement string is also spellchecked
717 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
718 text->CursorLeftIntern(this);
720 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
722 // End of spellchecker stuff
725 bool BufferView::lockInset(UpdatableInset * inset)
727 if (!the_locking_inset && inset) {
728 the_locking_inset = inset;
731 return the_locking_inset->LockInsetInInset(this, inset);
737 void BufferView::showLockedInsetCursor(long x, long y, int asc, int desc)
739 if (the_locking_inset && available()) {
740 LyXCursor cursor = text->cursor;
741 if ((cursor.pos() - 1 >= 0) &&
742 (cursor.par()->GetChar(cursor.pos() - 1) ==
743 LyXParagraph::META_INSET) &&
744 (cursor.par()->GetInset(cursor.pos() - 1) ==
745 the_locking_inset->GetLockingInset()))
746 text->SetCursor(this, cursor,
747 cursor.par(), cursor.pos() - 1);
748 y += cursor.y() + the_locking_inset->InsetInInsetY();
749 pimpl_->screen_->ShowManualCursor(text, 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(text, 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!
795 text->SetUndo(buffer(), kind,
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(this, inset)){
814 if (buffer()->isLyxClean())
815 owner()->getMiniBuffer()->
817 buffer()->markDirty();
822 } else if (the_locking_inset->UpdateInsetInInset(this,inset)) {
823 if (text->UpdateInset(this, the_locking_inset)) {
826 if (buffer()->isLyxClean())
827 owner()->getMiniBuffer()->
829 buffer()->markDirty();
837 // then check the current buffer
840 update(BufferView::UPDATE);
841 if (text->UpdateInset(this, inset)){
843 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
851 bool BufferView::ChangeRefs(string const & from, string const & to)
854 LyXParagraph * par = buffer()->paragraph;
855 LyXCursor cursor = text->cursor;
856 LyXCursor tmpcursor = cursor;
857 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
858 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
862 for (LyXParagraph::inset_iterator it = par->inset_iterator_begin();
863 it != par->inset_iterator_end(); ++it) {
864 if ((*it)->LyxCode() == Inset::REF_CODE) {
865 InsetCommand * inset = static_cast<InsetCommand *>(*it);
866 if (inset->getContents() == from) {
867 inset->setContents(to);
874 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
875 // this is possible now, since SetCursor takes
876 // care about footnotes
877 text->SetCursorIntern(this, par, 0);
878 text->RedoParagraphs(this, text->cursor,
879 text->cursor.par()->Next());
880 text->FullRebreak(this);
885 text->SetCursorIntern(this, cursor.par(), cursor.pos());