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);
287 /* This is also a buffer property (ale) */
288 // Not so sure about that. a goto Label function can not be buffer local, just
289 // think how this will work in a multiwindo/buffer environment, all the
290 // cursors in all the views showing this buffer will move. (Lgb)
291 // OK, then no cursor action should be allowed in buffer. (ale)
292 bool BufferView::gotoLabel(string const & label)
295 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
296 it != buffer()->inset_iterator_end(); ++it) {
297 vector<string> labels = (*it)->getLabelList();
298 if ( find(labels.begin(),labels.end(),label)
301 text->SetCursor(this, it.getPar(), it.getPos());
302 text->sel_cursor = text->cursor;
303 update(BufferView::SELECT|BufferView::FITCUR);
310 void BufferView::allFloats(char flag, char figmar)
312 if (!available()) return;
314 LyXCursor cursor = text->cursor;
316 if (!flag && cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE
318 && cursor.par()->footnotekind != LyXParagraph::FOOTNOTE
319 && cursor.par()->footnotekind != LyXParagraph::MARGIN)
321 && cursor.par()->footnotekind != LyXParagraph::FIG
322 && cursor.par()->footnotekind != LyXParagraph::TAB
323 && cursor.par()->footnotekind != LyXParagraph::WIDE_FIG
324 && cursor.par()->footnotekind != LyXParagraph::WIDE_TAB
325 && cursor.par()->footnotekind != LyXParagraph::ALGORITHM)))
330 LyXCursor tmpcursor = cursor;
331 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
332 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
334 LyXParagraph *par = buffer()->paragraph;
337 if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
339 && par->footnotekind != LyXParagraph::FOOTNOTE
340 && par->footnotekind != LyXParagraph::MARGIN)
342 && par->footnotekind != LyXParagraph::FIG
343 && par->footnotekind != LyXParagraph::TAB
344 && par->footnotekind != LyXParagraph::WIDE_FIG
345 && par->footnotekind != LyXParagraph::WIDE_TAB
346 && par->footnotekind != LyXParagraph::ALGORITHM
351 && par->previous->footnoteflag !=
352 LyXParagraph::CLOSED_FOOTNOTE){ /* should be */
353 text->SetCursorIntern(this,
356 text->OpenFootnote(this);
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(this, par, 0);
384 text->CloseFootnote(this);
390 text->SetCursorIntern(this, 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..."));
411 update(BufferView::SELECT|BufferView::FITCUR);
412 text->OpenStuff(this);
413 update(BufferView::SELECT|BufferView::FITCUR);
419 void BufferView::toggleFloat()
422 owner()->getMiniBuffer()->Set(_("Open/Close..."));
425 update(BufferView::SELECT|BufferView::FITCUR);
426 text->ToggleFootnote(this);
427 update(BufferView::SELECT|BufferView::FITCUR);
432 void BufferView::menuUndo()
435 owner()->getMiniBuffer()->Set(_("Undo"));
438 update(BufferView::SELECT|BufferView::FITCUR);
439 if (!text->TextUndo(this))
440 owner()->getMiniBuffer()->Set(_("No further undo information"));
442 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
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"));
459 update(BufferView::SELECT|BufferView::FITCUR);
460 if (!text->TextRedo(this))
461 owner()->getMiniBuffer()->Set(_("No further redo information"));
463 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
469 void BufferView::hyphenationPoint()
473 update(BufferView::SELECT|BufferView::FITCUR);
474 InsetSpecialChar * new_inset =
475 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
476 insertInset(new_inset);
481 void BufferView::ldots()
485 update(BufferView::SELECT|BufferView::FITCUR);
486 InsetSpecialChar * new_inset =
487 new InsetSpecialChar(InsetSpecialChar::LDOTS);
488 insertInset(new_inset);
493 void BufferView::endOfSentenceDot()
497 update(BufferView::SELECT|BufferView::FITCUR);
498 InsetSpecialChar * new_inset =
499 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
500 insertInset(new_inset);
505 void BufferView::menuSeparator()
509 update(BufferView::SELECT|BufferView::FITCUR);
510 InsetSpecialChar * new_inset =
511 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
512 insertInset(new_inset);
517 void BufferView::newline()
521 update(BufferView::SELECT|BufferView::FITCUR);
522 text->InsertChar(this, LyXParagraph::META_NEWLINE);
523 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
528 void BufferView::protectedBlank()
532 update(BufferView::SELECT|BufferView::FITCUR);
533 InsetSpecialChar * new_inset =
534 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
535 insertInset(new_inset);
540 void BufferView::hfill()
544 update(BufferView::SELECT|BufferView::FITCUR);
545 text->InsertChar(this, LyXParagraph::META_HFILL);
546 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
550 void BufferView::copyEnvironment()
553 text->copyEnvironmentType();
554 // clear the selection, even if mark_set
556 text->ClearSelection();
557 update(BufferView::SELECT|BufferView::FITCUR);
558 owner()->getMiniBuffer()->Set(_("Paragraph environment type copied"));
563 void BufferView::pasteEnvironment()
566 text->pasteEnvironmentType(this);
567 owner()->getMiniBuffer()->Set(_("Paragraph environment type set"));
568 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
573 void BufferView::copy()
576 text->CopySelection(this);
577 // clear the selection, even if mark_set
579 text->ClearSelection();
580 update(BufferView::SELECT|BufferView::FITCUR);
581 owner()->getMiniBuffer()->Set(_("Copy"));
585 void BufferView::cut()
589 update(BufferView::SELECT|BufferView::FITCUR);
590 text->CutSelection(this);
591 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
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();
606 update(BufferView::SELECT|BufferView::FITCUR);
609 text->PasteSelection(this);
610 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
612 // clear the selection
614 text->ClearSelection();
615 update(BufferView::SELECT|BufferView::FITCUR);
619 void BufferView::gotoNote()
621 if (!available()) return;
625 update(BufferView::SELECT|BufferView::FITCUR);
628 if (!text->GotoNextNote(this)) {
629 if (text->cursor.pos()
630 || text->cursor.par() != text->FirstParagraph()) {
632 text->cursor.par(text->FirstParagraph());
634 if (!text->GotoNextNote(this)) {
636 owner()->getMiniBuffer()->Set(_("No more notes"));
640 owner()->getMiniBuffer()->Set(_("No more notes"));
644 update(BufferView::SELECT|BufferView::FITCUR);
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(this, value);
676 void BufferView::selectLastWord()
678 if (!available()) return;
682 text->SelectSelectedWord(this);
683 toggleSelection(false);
684 update(BufferView::SELECT|BufferView::FITCUR);
688 void BufferView::endOfSpellCheck()
690 if (!available()) return;
694 text->SelectSelectedWord(this);
695 text->ClearSelection();
696 update(BufferView::SELECT|BufferView::FITCUR);
700 void BufferView::replaceWord(string const & replacestring)
702 if (!available()) return;
705 update(BufferView::SELECT|BufferView::FITCUR);
707 /* clear the selection (if there is any) */
708 toggleSelection(false);
709 update(BufferView::SELECT|BufferView::FITCUR);
711 /* clear the selection (if there is any) */
712 toggleSelection(false);
713 text->ReplaceSelectionWithString(this, replacestring.c_str());
715 text->SetSelectionOverString(this, 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(this);
721 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
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(this, cursor,
748 cursor.par(), cursor.pos() - 1);
749 y += cursor.y() + the_locking_inset->InsetInInsetY();
750 pimpl_->screen_->ShowManualCursor(text, x, y, asc, desc,
751 LyXScreen::BAR_SHAPE);
756 void BufferView::hideLockedInsetCursor()
758 if (the_locking_inset && available()) {
759 pimpl_->screen_->HideCursor();
764 void BufferView::fitLockedInsetCursor(long x, long y, int asc, int desc)
766 if (the_locking_inset && available()){
767 y += text->cursor.y() + the_locking_inset->InsetInInsetY();
768 if (pimpl_->screen_->FitManualCursor(text, x, y, asc, desc))
774 int BufferView::unlockInset(UpdatableInset * inset)
776 if (inset && the_locking_inset == inset) {
777 inset->InsetUnlock(this);
778 the_locking_inset = 0;
781 } else if (inset && the_locking_inset &&
782 the_locking_inset->UnlockInsetInInset(this, inset)) {
786 return bufferlist.unlockInset(inset);
790 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
792 if (!the_locking_inset)
793 return; // shouldn't happen
794 if (kind == Undo::EDIT) // in this case insets would not be stored!
796 text->SetUndo(buffer(), kind,
798 ParFromPos(text->cursor.pos())->previous,
800 ParFromPos(text->cursor.pos())->next);
804 void BufferView::updateInset(Inset * inset, bool mark_dirty)
809 // first check for locking insets
810 if (the_locking_inset) {
811 if (the_locking_inset == inset) {
812 if (text->UpdateInset(this, inset)){
815 if (buffer()->isLyxClean())
816 owner()->getMiniBuffer()->
818 buffer()->markDirty();
823 } else if (the_locking_inset->UpdateInsetInInset(this,inset)) {
824 if (text->UpdateInset(this, the_locking_inset)) {
827 if (buffer()->isLyxClean())
828 owner()->getMiniBuffer()->
830 buffer()->markDirty();
838 // then check the current buffer
841 update(BufferView::UPDATE);
842 if (text->UpdateInset(this, inset)){
844 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
852 bool BufferView::ChangeRefs(string const & from, string const & to)
855 LyXParagraph * par = buffer()->paragraph;
856 LyXCursor cursor = text->cursor;
857 LyXCursor tmpcursor = cursor;
858 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
859 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
863 for (LyXParagraph::inset_iterator it = par->inset_iterator_begin();
864 it != par->inset_iterator_end(); ++it) {
865 if ((*it)->LyxCode() == Inset::REF_CODE) {
866 InsetCommand * inset = static_cast<InsetCommand *>(*it);
867 if (inset->getContents() == from) {
868 inset->setContents(to);
875 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
876 // this is possible now, since SetCursor takes
877 // care about footnotes
878 text->SetCursorIntern(this, par, 0);
879 text->RedoParagraphs(this, text->cursor,
880 text->cursor.par()->Next());
881 text->FullRebreak(this);
886 text->SetCursorIntern(this, cursor.par(), cursor.pos());