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));
85 lyxerr.debug() << "Will insert file with header" << endl;
86 res = buffer()->readFile(lex, text->cursor.par());
88 lyxerr.debug() << "Will insert file without header" << endl;
89 res = buffer()->readLyXformat2(lex, text->cursor.par());
96 bool BufferView::removeAutoInsets()
98 LyXParagraph * par = buffer()->paragraph;
100 LyXCursor tmpcursor = text->cursor;
105 // this has to be done before the delete
107 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
109 text->SetCursor(this, cursor, par, 0);
110 if (par->AutoDeleteInsets()){
113 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
115 text->RedoParagraphs(this, cursor,
116 cursor.par()->Next());
117 text->FullRebreak(this);
124 // avoid forbidden cursor positions caused by error removing
125 if (tmpcursor.pos() > tmpcursor.par()->Last())
126 tmpcursor.pos(tmpcursor.par()->Last());
127 text->SetCursorIntern(this, tmpcursor.par(), tmpcursor.pos());
133 void BufferView::insertErrors(TeXErrors & terr)
135 // Save the cursor position
136 LyXCursor cursor = text->cursor;
139 // This is drastic, but it's the only fix, I could find. (Asger)
144 for (TeXErrors::Errors::const_iterator cit = terr.begin();
147 string desctext((*cit).error_desc);
148 string errortext((*cit).error_text);
149 string msgtxt = desctext + '\n' + errortext;
150 int errorrow = (*cit).error_in_line;
152 // Insert error string for row number
156 buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos);
158 LyXParagraph * texrowpar = 0;
161 texrowpar = text->FirstParagraph();
164 texrowpar = text->GetParFromID(tmpid);
170 InsetError * new_inset = new InsetError(msgtxt);
171 text->SetCursorIntern(this, texrowpar, tmppos);
172 text->InsertInset(this, new_inset);
173 text->FullRebreak(this);
175 // Restore the cursor position
176 text->SetCursorIntern(this, cursor.par(), cursor.pos());
180 void BufferView::setCursorFromRow(int row)
185 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
187 LyXParagraph * texrowpar;
190 texrowpar = text->FirstParagraph();
193 texrowpar = text->GetParFromID(tmpid);
195 text->SetCursor(this, texrowpar, tmppos);
198 bool BufferView::insertInset(Inset * inset, string const & lout,
201 // if we are in a locking inset we should try to insert the
202 // inset there otherwise this is a illegal function now
203 if (the_locking_inset) {
204 if (the_locking_inset->InsertInsetAllowed(inset) &&
205 the_locking_inset->InsertInset(this, inset))
211 // check for table/list in tables
212 if (no_table && text->cursor.par()->table){
213 WriteAlert(_("Impossible Operation!"),
214 _("Cannot insert table/list in table."),
220 // not quite sure if we want this...
221 text->SetCursorParUndo(buffer());
226 update(BufferView::SELECT|BufferView::FITCUR);
227 text->BreakParagraph(this);
228 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
230 if (text->cursor.par()->Last()) {
231 text->CursorLeft(this);
233 text->BreakParagraph(this);
234 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
237 pair<bool, LyXTextClass::size_type> lres =
238 textclasslist.NumberOfLayout(buffer()->params
240 LyXTextClass::size_type lay;
241 if (lres.first != false) {
245 // layout not fount using default "Standard" (0)
249 text->SetLayout(this, lay);
251 text->SetParagraph(this, 0, 0,
253 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
257 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
259 text->current_font.setLatex(LyXFont::OFF);
262 text->InsertInset(this, inset);
264 // if we enter a text-inset the cursor should be to the left side
265 // of it! This couldn't happen before as Undo was not handled inside
266 // inset now after the Undo LyX tries to call inset->Edit(...) again
267 // and cannot do this as the cursor is behind the inset and GetInset
268 // does not return the inset!
269 if (inset->IsTextInset()) {
270 if (text->cursor.par()->isRightToLeftPar(buffer()->params))
271 text->CursorRight(this);
273 text->CursorLeft(this);
276 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
278 text->UnFreezeUndo();
283 // Open and lock an updatable inset
284 bool BufferView::open_new_inset(UpdatableInset * new_inset)
288 if (!insertInset(new_inset))
290 text->CursorLeft(this);
291 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
292 new_inset->Edit(this, 0, 0, 0);
296 /* This is also a buffer property (ale) */
297 // Not so sure about that. a goto Label function can not be buffer local, just
298 // think how this will work in a multiwindo/buffer environment, all the
299 // cursors in all the views showing this buffer will move. (Lgb)
300 // OK, then no cursor action should be allowed in buffer. (ale)
301 bool BufferView::gotoLabel(string const & label)
304 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
305 it != buffer()->inset_iterator_end(); ++it) {
306 vector<string> labels = (*it)->getLabelList();
307 if ( find(labels.begin(),labels.end(),label)
310 text->SetCursor(this, it.getPar(), it.getPos());
311 text->sel_cursor = text->cursor;
312 update(BufferView::SELECT|BufferView::FITCUR);
321 void BufferView::allFloats(char flag, char figmar)
323 if (!available()) return;
325 LyXCursor cursor = text->cursor;
328 && cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE
330 && cursor.par()->footnotekind != LyXParagraph::FOOTNOTE
331 && cursor.par()->footnotekind != LyXParagraph::MARGIN
334 && cursor.par()->footnotekind != LyXParagraph::FIG
335 && cursor.par()->footnotekind != LyXParagraph::TAB
336 && cursor.par()->footnotekind != LyXParagraph::WIDE_FIG
337 && cursor.par()->footnotekind != LyXParagraph::WIDE_TAB
338 && cursor.par()->footnotekind != LyXParagraph::ALGORITHM)))
343 LyXCursor tmpcursor = cursor;
344 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
345 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
347 LyXParagraph *par = buffer()->paragraph;
350 if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
352 && par->footnotekind != LyXParagraph::FOOTNOTE
353 && par->footnotekind != LyXParagraph::MARGIN)
355 && par->footnotekind != LyXParagraph::FIG
356 && par->footnotekind != LyXParagraph::TAB
357 && par->footnotekind != LyXParagraph::WIDE_FIG
358 && par->footnotekind != LyXParagraph::WIDE_TAB
359 && par->footnotekind != LyXParagraph::ALGORITHM
364 && par->previous->footnoteflag !=
365 LyXParagraph::CLOSED_FOOTNOTE){ /* should be */
366 text->SetCursorIntern(this,
369 text->OpenFootnote(this);
373 if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
377 par->footnotekind != LyXParagraph::FOOTNOTE
379 par->footnotekind != LyXParagraph::MARGIN
384 par->footnotekind != LyXParagraph::FIG
386 par->footnotekind != LyXParagraph::TAB
388 par->footnotekind != LyXParagraph::WIDE_FIG
390 par->footnotekind != LyXParagraph::WIDE_TAB
392 par->footnotekind != LyXParagraph::ALGORITHM
396 text->SetCursorIntern(this, par, 0);
397 text->CloseFootnote(this);
403 text->SetCursorIntern(this, cursor.par(), cursor.pos());
411 void BufferView::insertNote()
413 InsetInfo * new_inset = new InsetInfo();
414 insertInset(new_inset);
415 new_inset->Edit(this, 0, 0, 0);
420 void BufferView::openStuff()
423 owner()->getMiniBuffer()->Set(_("Open/Close..."));
426 update(BufferView::SELECT|BufferView::FITCUR);
427 text->OpenStuff(this);
428 update(BufferView::SELECT|BufferView::FITCUR);
434 void BufferView::toggleFloat()
437 owner()->getMiniBuffer()->Set(_("Open/Close..."));
440 update(BufferView::SELECT|BufferView::FITCUR);
441 text->ToggleFootnote(this);
442 update(BufferView::SELECT|BufferView::FITCUR);
448 void BufferView::menuUndo()
451 owner()->getMiniBuffer()->Set(_("Undo"));
454 update(BufferView::SELECT|BufferView::FITCUR);
455 if (!text->TextUndo(this))
456 owner()->getMiniBuffer()->Set(_("No further undo information"));
458 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
464 void BufferView::menuRedo()
466 if (the_locking_inset) {
467 owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
472 owner()->getMiniBuffer()->Set(_("Redo"));
475 update(BufferView::SELECT|BufferView::FITCUR);
476 if (!text->TextRedo(this))
477 owner()->getMiniBuffer()->Set(_("No further redo information"));
479 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
485 void BufferView::hyphenationPoint()
489 update(BufferView::SELECT|BufferView::FITCUR);
490 InsetSpecialChar * new_inset =
491 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
492 insertInset(new_inset);
497 void BufferView::ldots()
501 update(BufferView::SELECT|BufferView::FITCUR);
502 InsetSpecialChar * new_inset =
503 new InsetSpecialChar(InsetSpecialChar::LDOTS);
504 insertInset(new_inset);
509 void BufferView::endOfSentenceDot()
513 update(BufferView::SELECT|BufferView::FITCUR);
514 InsetSpecialChar * new_inset =
515 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
516 insertInset(new_inset);
521 void BufferView::menuSeparator()
525 update(BufferView::SELECT|BufferView::FITCUR);
526 InsetSpecialChar * new_inset =
527 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
528 insertInset(new_inset);
533 void BufferView::newline()
537 update(BufferView::SELECT|BufferView::FITCUR);
538 text->InsertChar(this, LyXParagraph::META_NEWLINE);
539 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
544 void BufferView::protectedBlank()
548 update(BufferView::SELECT|BufferView::FITCUR);
549 InsetSpecialChar * new_inset =
550 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
551 insertInset(new_inset);
556 void BufferView::hfill()
560 update(BufferView::SELECT|BufferView::FITCUR);
561 text->InsertChar(this, LyXParagraph::META_HFILL);
562 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
566 void BufferView::copyEnvironment()
569 text->copyEnvironmentType();
570 // clear the selection, even if mark_set
572 text->ClearSelection();
573 update(BufferView::SELECT|BufferView::FITCUR);
574 owner()->getMiniBuffer()->Set(_("Paragraph environment type copied"));
579 void BufferView::pasteEnvironment()
582 text->pasteEnvironmentType(this);
583 owner()->getMiniBuffer()->Set(_("Paragraph environment type set"));
584 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
589 void BufferView::copy()
592 text->CopySelection(this);
593 // clear the selection, even if mark_set
595 text->ClearSelection();
596 update(BufferView::SELECT|BufferView::FITCUR);
597 owner()->getMiniBuffer()->Set(_("Copy"));
601 void BufferView::cut()
605 update(BufferView::SELECT|BufferView::FITCUR);
606 text->CutSelection(this);
607 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
608 owner()->getMiniBuffer()->Set(_("Cut"));
613 void BufferView::paste()
615 if (!available()) return;
617 owner()->getMiniBuffer()->Set(_("Paste"));
619 // clear the selection
621 text->ClearSelection();
622 update(BufferView::SELECT|BufferView::FITCUR);
625 text->PasteSelection(this);
626 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
628 // clear the selection
630 text->ClearSelection();
631 update(BufferView::SELECT|BufferView::FITCUR);
635 void BufferView::gotoNote()
637 if (!available()) return;
641 update(BufferView::SELECT|BufferView::FITCUR);
644 if (!text->GotoNextNote(this)) {
645 if (text->cursor.pos()
646 || text->cursor.par() != text->FirstParagraph()) {
648 text->cursor.par(text->FirstParagraph());
650 if (!text->GotoNextNote(this)) {
652 owner()->getMiniBuffer()->Set(_("No more notes"));
656 owner()->getMiniBuffer()->Set(_("No more notes"));
660 update(BufferView::SELECT|BufferView::FITCUR);
661 text->sel_cursor = text->cursor;
665 void BufferView::insertCorrectQuote()
669 if (text->cursor.pos())
670 c = text->cursor.par()->GetChar(text->cursor.pos() - 1);
674 insertInset(new InsetQuotes(c, buffer()->params));
678 /* these functions are for the spellchecker */
679 char * BufferView::nextWord(float & value)
686 char * string = text->SelectNextWord(this, value);
692 void BufferView::selectLastWord()
694 if (!available()) return;
698 text->SelectSelectedWord(this);
699 toggleSelection(false);
700 update(BufferView::SELECT|BufferView::FITCUR);
704 void BufferView::endOfSpellCheck()
706 if (!available()) return;
710 text->SelectSelectedWord(this);
711 text->ClearSelection();
712 update(BufferView::SELECT|BufferView::FITCUR);
716 void BufferView::replaceWord(string const & replacestring)
718 if (!available()) return;
721 update(BufferView::SELECT|BufferView::FITCUR);
723 /* clear the selection (if there is any) */
724 toggleSelection(false);
725 update(BufferView::SELECT|BufferView::FITCUR);
727 /* clear the selection (if there is any) */
728 toggleSelection(false);
729 text->ReplaceSelectionWithString(this, replacestring.c_str());
731 text->SetSelectionOverString(this, replacestring.c_str());
733 // Go back so that replacement string is also spellchecked
734 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
735 text->CursorLeftIntern(this);
737 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
739 // End of spellchecker stuff
742 bool BufferView::lockInset(UpdatableInset * inset)
744 if (!the_locking_inset && inset) {
745 the_locking_inset = inset;
748 return the_locking_inset->LockInsetInInset(this, inset);
754 void BufferView::showLockedInsetCursor(long x, long y, int asc, int desc)
756 if (the_locking_inset && available()) {
757 LyXCursor cursor = text->cursor;
758 if ((cursor.pos() - 1 >= 0) &&
759 (cursor.par()->GetChar(cursor.pos() - 1) ==
760 LyXParagraph::META_INSET) &&
761 (cursor.par()->GetInset(cursor.pos() - 1) ==
762 the_locking_inset->GetLockingInset()))
763 text->SetCursor(this, cursor,
764 cursor.par(), cursor.pos() - 1);
765 y += cursor.y() + the_locking_inset->InsetInInsetY();
766 pimpl_->screen_->ShowManualCursor(text, x, y, asc, desc,
767 LyXScreen::BAR_SHAPE);
772 void BufferView::hideLockedInsetCursor()
774 if (the_locking_inset && available()) {
775 pimpl_->screen_->HideCursor();
780 void BufferView::fitLockedInsetCursor(long x, long y, int asc, int desc)
782 if (the_locking_inset && available()){
783 y += text->cursor.y() + the_locking_inset->InsetInInsetY();
784 if (pimpl_->screen_->FitManualCursor(text, x, y, asc, desc))
790 int BufferView::unlockInset(UpdatableInset * inset)
792 if (inset && the_locking_inset == inset) {
793 inset->InsetUnlock(this);
794 the_locking_inset = 0;
797 } else if (inset && the_locking_inset &&
798 the_locking_inset->UnlockInsetInInset(this, inset)) {
802 return bufferlist.unlockInset(inset);
806 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
808 if (!the_locking_inset)
809 return; // shouldn't happen
810 if (kind == Undo::EDIT) // in this case insets would not be stored!
812 text->SetUndo(buffer(), kind,
815 ParFromPos(text->cursor.pos())->previous,
817 ParFromPos(text->cursor.pos())->next
819 text->cursor.par()->previous,
820 text->cursor.par()->next
826 void BufferView::updateInset(Inset * inset, bool mark_dirty)
831 // first check for locking insets
832 if (the_locking_inset) {
833 if (the_locking_inset == inset) {
834 if (text->UpdateInset(this, inset)){
837 if (buffer()->isLyxClean())
838 owner()->getMiniBuffer()->
840 buffer()->markDirty();
845 } else if (the_locking_inset->UpdateInsetInInset(this,inset)) {
846 if (text->UpdateInset(this, the_locking_inset)) {
849 if (buffer()->isLyxClean())
850 owner()->getMiniBuffer()->
852 buffer()->markDirty();
860 // then check the current buffer
863 update(BufferView::UPDATE);
864 if (text->UpdateInset(this, inset)){
866 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
874 bool BufferView::ChangeRefs(string const & from, string const & to)
877 LyXParagraph * par = buffer()->paragraph;
878 LyXCursor cursor = text->cursor;
879 LyXCursor tmpcursor = cursor;
881 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
882 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
884 cursor.par(tmpcursor.par());
885 cursor.pos(tmpcursor.pos());
890 for (LyXParagraph::inset_iterator it = par->inset_iterator_begin();
891 it != par->inset_iterator_end(); ++it) {
892 if ((*it)->LyxCode() == Inset::REF_CODE) {
893 InsetCommand * inset = static_cast<InsetCommand *>(*it);
894 if (inset->getContents() == from) {
895 inset->setContents(to);
903 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
905 // this is possible now, since SetCursor takes
906 // care about footnotes
907 text->SetCursorIntern(this, par, 0);
908 text->RedoParagraphs(this, text->cursor,
909 text->cursor.par()->Next());
910 text->FullRebreak(this);
917 text->SetCursorIntern(this, cursor.par(), cursor.pos());