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 void BufferView::open_new_inset(UpdatableInset * new_inset)
279 insertInset(new_inset);
280 text->CursorLeft(this);
281 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
282 new_inset->Edit(this, 0, 0, 0);
285 /* This is also a buffer property (ale) */
286 // Not so sure about that. a goto Label function can not be buffer local, just
287 // think how this will work in a multiwindo/buffer environment, all the
288 // cursors in all the views showing this buffer will move. (Lgb)
289 // OK, then no cursor action should be allowed in buffer. (ale)
290 bool BufferView::gotoLabel(string const & label)
293 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
294 it != buffer()->inset_iterator_end(); ++it) {
295 vector<string> labels = (*it)->getLabelList();
296 if ( find(labels.begin(),labels.end(),label)
299 text->SetCursor(this, it.getPar(), it.getPos());
300 text->sel_cursor = text->cursor;
301 update(BufferView::SELECT|BufferView::FITCUR);
308 void BufferView::allFloats(char flag, char figmar)
310 if (!available()) return;
312 LyXCursor cursor = text->cursor;
314 if (!flag && cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE
316 && cursor.par()->footnotekind != LyXParagraph::FOOTNOTE
317 && cursor.par()->footnotekind != LyXParagraph::MARGIN)
319 && cursor.par()->footnotekind != LyXParagraph::FIG
320 && cursor.par()->footnotekind != LyXParagraph::TAB
321 && cursor.par()->footnotekind != LyXParagraph::WIDE_FIG
322 && cursor.par()->footnotekind != LyXParagraph::WIDE_TAB
323 && cursor.par()->footnotekind != LyXParagraph::ALGORITHM)))
328 LyXCursor tmpcursor = cursor;
329 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
330 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
332 LyXParagraph *par = buffer()->paragraph;
335 if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
337 && par->footnotekind != LyXParagraph::FOOTNOTE
338 && par->footnotekind != LyXParagraph::MARGIN)
340 && par->footnotekind != LyXParagraph::FIG
341 && par->footnotekind != LyXParagraph::TAB
342 && par->footnotekind != LyXParagraph::WIDE_FIG
343 && par->footnotekind != LyXParagraph::WIDE_TAB
344 && par->footnotekind != LyXParagraph::ALGORITHM
349 && par->previous->footnoteflag !=
350 LyXParagraph::CLOSED_FOOTNOTE){ /* should be */
351 text->SetCursorIntern(this,
354 text->OpenFootnote(this);
358 if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
362 par->footnotekind != LyXParagraph::FOOTNOTE
364 par->footnotekind != LyXParagraph::MARGIN
369 par->footnotekind != LyXParagraph::FIG
371 par->footnotekind != LyXParagraph::TAB
373 par->footnotekind != LyXParagraph::WIDE_FIG
375 par->footnotekind != LyXParagraph::WIDE_TAB
377 par->footnotekind != LyXParagraph::ALGORITHM
381 text->SetCursorIntern(this, par, 0);
382 text->CloseFootnote(this);
388 text->SetCursorIntern(this, cursor.par(), cursor.pos());
395 void BufferView::insertNote()
397 InsetInfo * new_inset = new InsetInfo();
398 insertInset(new_inset);
399 new_inset->Edit(this, 0, 0, 0);
403 void BufferView::openStuff()
406 owner()->getMiniBuffer()->Set(_("Open/Close..."));
409 update(BufferView::SELECT|BufferView::FITCUR);
410 text->OpenStuff(this);
411 update(BufferView::SELECT|BufferView::FITCUR);
417 void BufferView::toggleFloat()
420 owner()->getMiniBuffer()->Set(_("Open/Close..."));
423 update(BufferView::SELECT|BufferView::FITCUR);
424 text->ToggleFootnote(this);
425 update(BufferView::SELECT|BufferView::FITCUR);
430 void BufferView::menuUndo()
433 owner()->getMiniBuffer()->Set(_("Undo"));
436 update(BufferView::SELECT|BufferView::FITCUR);
437 if (!text->TextUndo(this))
438 owner()->getMiniBuffer()->Set(_("No further undo information"));
440 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
446 void BufferView::menuRedo()
448 if (the_locking_inset) {
449 owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
454 owner()->getMiniBuffer()->Set(_("Redo"));
457 update(BufferView::SELECT|BufferView::FITCUR);
458 if (!text->TextRedo(this))
459 owner()->getMiniBuffer()->Set(_("No further redo information"));
461 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
467 void BufferView::hyphenationPoint()
471 update(BufferView::SELECT|BufferView::FITCUR);
472 InsetSpecialChar * new_inset =
473 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
474 insertInset(new_inset);
479 void BufferView::ldots()
483 update(BufferView::SELECT|BufferView::FITCUR);
484 InsetSpecialChar * new_inset =
485 new InsetSpecialChar(InsetSpecialChar::LDOTS);
486 insertInset(new_inset);
491 void BufferView::endOfSentenceDot()
495 update(BufferView::SELECT|BufferView::FITCUR);
496 InsetSpecialChar * new_inset =
497 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
498 insertInset(new_inset);
503 void BufferView::menuSeparator()
507 update(BufferView::SELECT|BufferView::FITCUR);
508 InsetSpecialChar * new_inset =
509 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
510 insertInset(new_inset);
515 void BufferView::newline()
519 update(BufferView::SELECT|BufferView::FITCUR);
520 text->InsertChar(this, LyXParagraph::META_NEWLINE);
521 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
526 void BufferView::protectedBlank()
530 update(BufferView::SELECT|BufferView::FITCUR);
531 InsetSpecialChar * new_inset =
532 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
533 insertInset(new_inset);
538 void BufferView::hfill()
542 update(BufferView::SELECT|BufferView::FITCUR);
543 text->InsertChar(this, LyXParagraph::META_HFILL);
544 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
548 void BufferView::copyEnvironment()
551 text->copyEnvironmentType();
552 // clear the selection, even if mark_set
554 text->ClearSelection();
555 update(BufferView::SELECT|BufferView::FITCUR);
556 owner()->getMiniBuffer()->Set(_("Paragraph environment type copied"));
561 void BufferView::pasteEnvironment()
564 text->pasteEnvironmentType(this);
565 owner()->getMiniBuffer()->Set(_("Paragraph environment type set"));
566 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
571 void BufferView::copy()
574 text->CopySelection(this);
575 // clear the selection, even if mark_set
577 text->ClearSelection();
578 update(BufferView::SELECT|BufferView::FITCUR);
579 owner()->getMiniBuffer()->Set(_("Copy"));
583 void BufferView::cut()
587 update(BufferView::SELECT|BufferView::FITCUR);
588 text->CutSelection(this);
589 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
590 owner()->getMiniBuffer()->Set(_("Cut"));
595 void BufferView::paste()
597 if (!available()) return;
599 owner()->getMiniBuffer()->Set(_("Paste"));
601 // clear the selection
603 text->ClearSelection();
604 update(BufferView::SELECT|BufferView::FITCUR);
607 text->PasteSelection(this);
608 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
610 // clear the selection
612 text->ClearSelection();
613 update(BufferView::SELECT|BufferView::FITCUR);
617 void BufferView::gotoNote()
619 if (!available()) return;
623 update(BufferView::SELECT|BufferView::FITCUR);
626 if (!text->GotoNextNote(this)) {
627 if (text->cursor.pos()
628 || text->cursor.par() != text->FirstParagraph()) {
630 text->cursor.par(text->FirstParagraph());
632 if (!text->GotoNextNote(this)) {
634 owner()->getMiniBuffer()->Set(_("No more notes"));
638 owner()->getMiniBuffer()->Set(_("No more notes"));
642 update(BufferView::SELECT|BufferView::FITCUR);
643 text->sel_cursor = text->cursor;
647 void BufferView::insertCorrectQuote()
651 if (text->cursor.pos())
652 c = text->cursor.par()->GetChar(text->cursor.pos() - 1);
656 insertInset(new InsetQuotes(c, buffer()->params));
660 /* these functions are for the spellchecker */
661 char * BufferView::nextWord(float & value)
668 char * string = text->SelectNextWord(this, value);
674 void BufferView::selectLastWord()
676 if (!available()) return;
680 text->SelectSelectedWord(this);
681 toggleSelection(false);
682 update(BufferView::SELECT|BufferView::FITCUR);
686 void BufferView::endOfSpellCheck()
688 if (!available()) return;
692 text->SelectSelectedWord(this);
693 text->ClearSelection();
694 update(BufferView::SELECT|BufferView::FITCUR);
698 void BufferView::replaceWord(string const & replacestring)
700 if (!available()) return;
703 update(BufferView::SELECT|BufferView::FITCUR);
705 /* clear the selection (if there is any) */
706 toggleSelection(false);
707 update(BufferView::SELECT|BufferView::FITCUR);
709 /* clear the selection (if there is any) */
710 toggleSelection(false);
711 text->ReplaceSelectionWithString(this, replacestring.c_str());
713 text->SetSelectionOverString(this, replacestring.c_str());
715 // Go back so that replacement string is also spellchecked
716 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
717 text->CursorLeftIntern(this);
719 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
721 // End of spellchecker stuff
724 bool BufferView::lockInset(UpdatableInset * inset)
726 if (!the_locking_inset && inset) {
727 the_locking_inset = inset;
730 return the_locking_inset->LockInsetInInset(this, inset);
736 void BufferView::showLockedInsetCursor(long x, long y, int asc, int desc)
738 if (the_locking_inset && available()) {
739 LyXCursor cursor = text->cursor;
740 if ((cursor.pos() - 1 >= 0) &&
741 (cursor.par()->GetChar(cursor.pos() - 1) ==
742 LyXParagraph::META_INSET) &&
743 (cursor.par()->GetInset(cursor.pos() - 1) ==
744 the_locking_inset->GetLockingInset()))
745 text->SetCursor(this, cursor,
746 cursor.par(), cursor.pos() - 1);
747 y += cursor.y() + the_locking_inset->InsetInInsetY();
748 pimpl_->screen_->ShowManualCursor(text, x, y, asc, desc,
749 LyXScreen::BAR_SHAPE);
754 void BufferView::hideLockedInsetCursor()
756 if (the_locking_inset && available()) {
757 pimpl_->screen_->HideCursor();
762 void BufferView::fitLockedInsetCursor(long x, long y, int asc, int desc)
764 if (the_locking_inset && available()){
765 y += text->cursor.y() + the_locking_inset->InsetInInsetY();
766 if (pimpl_->screen_->FitManualCursor(text, x, y, asc, desc))
772 int BufferView::unlockInset(UpdatableInset * inset)
774 if (inset && the_locking_inset == inset) {
775 inset->InsetUnlock(this);
776 the_locking_inset = 0;
779 } else if (inset && the_locking_inset &&
780 the_locking_inset->UnlockInsetInInset(this, inset)) {
784 return bufferlist.unlockInset(inset);
788 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
790 if (!the_locking_inset)
791 return; // shouldn't happen
792 if (kind == Undo::EDIT) // in this case insets would not be stored!
794 text->SetUndo(buffer(), kind,
796 ParFromPos(text->cursor.pos())->previous,
798 ParFromPos(text->cursor.pos())->next);
802 void BufferView::updateInset(Inset * inset, bool mark_dirty)
807 // first check for locking insets
808 if (the_locking_inset) {
809 if (the_locking_inset == inset) {
810 if (text->UpdateInset(this, inset)){
813 if (buffer()->isLyxClean())
814 owner()->getMiniBuffer()->
816 buffer()->markDirty();
821 } else if (the_locking_inset->UpdateInsetInInset(this,inset)) {
822 if (text->UpdateInset(this, the_locking_inset)) {
825 if (buffer()->isLyxClean())
826 owner()->getMiniBuffer()->
828 buffer()->markDirty();
836 // then check the current buffer
839 update(BufferView::UPDATE);
840 if (text->UpdateInset(this, inset)){
842 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
850 bool BufferView::ChangeRefs(string const & from, string const & to)
853 LyXParagraph * par = buffer()->paragraph;
854 LyXCursor cursor = text->cursor;
855 LyXCursor tmpcursor = cursor;
856 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
857 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
861 for (LyXParagraph::inset_iterator it = par->inset_iterator_begin();
862 it != par->inset_iterator_end(); ++it) {
863 if ((*it)->LyxCode() == Inset::REF_CODE) {
864 InsetCommand * inset = static_cast<InsetCommand *>(*it);
865 if (inset->getContents() == from) {
866 inset->setContents(to);
873 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
874 // this is possible now, since SetCursor takes
875 // care about footnotes
876 text->SetCursorIntern(this, par, 0);
877 text->RedoParagraphs(this, text->cursor,
878 text->cursor.par()->Next());
879 text->FullRebreak(this);
884 text->SetCursorIntern(this, cursor.par(), cursor.pos());