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 text->SetCursor(cursor, par, 0);
106 if (par->AutoDeleteInsets()){
108 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
109 text->RedoParagraphs(cursor,
116 // avoid forbidden cursor positions caused by error removing
117 if (tmpcursor.pos > tmpcursor.par->Last())
118 tmpcursor.pos = tmpcursor.par->Last();
119 text->SetCursorIntern(tmpcursor.par, tmpcursor.pos);
125 void BufferView::insertErrors(TeXErrors & terr)
127 // Save the cursor position
128 LyXCursor cursor = text->cursor;
130 // This is drastic, but it's the only fix, I could find. (Asger)
134 for (TeXErrors::Errors::const_iterator cit = terr.begin();
137 string desctext((*cit).error_desc);
138 string errortext((*cit).error_text);
139 string msgtxt = desctext + '\n' + errortext;
140 int errorrow = (*cit).error_in_line;
142 // Insert error string for row number
146 buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos);
148 LyXParagraph * texrowpar = 0;
151 texrowpar = text->FirstParagraph();
154 texrowpar = text->GetParFromID(tmpid);
160 InsetError * new_inset = new InsetError(msgtxt);
161 text->SetCursorIntern(texrowpar, tmppos);
162 text->InsertInset(new_inset);
165 // Restore the cursor position
166 text->SetCursorIntern(cursor.par, cursor.pos);
170 void BufferView::setCursorFromRow(int row)
175 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
177 LyXParagraph * texrowpar;
180 texrowpar = text->FirstParagraph();
183 texrowpar = text->GetParFromID(tmpid);
185 text->SetCursor(texrowpar, tmppos);
188 bool BufferView::insertInset(Inset * inset, string const & lout,
191 // if we are in a locking inset we should try to insert the
192 // inset there otherwise this is a illegal function now
193 if (the_locking_inset) {
194 if (the_locking_inset->InsertInsetAllowed(inset) &&
195 the_locking_inset->InsertInset(this, inset))
200 // check for table/list in tables
201 if (no_table && text->cursor.par->table){
202 WriteAlert(_("Impossible Operation!"),
203 _("Cannot insert table/list in table."),
208 // not quite sure if we want this...
209 text->SetCursorParUndo();
215 text->BreakParagraph();
218 if (text->cursor.par->Last()) {
221 text->BreakParagraph();
225 pair<bool, LyXTextClass::size_type> lres =
226 textclasslist.NumberOfLayout(buffer()->params
228 LyXTextClass::size_type lay;
229 if (lres.first != false) {
233 // layout not fount using default "Standard" (0)
237 text->SetLayout(lay);
239 text->SetParagraph(0, 0,
241 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
247 text->current_font.setLatex(LyXFont::OFF);
250 text->InsertInset(inset);
252 // if we enter a text-inset the cursor should be to the left side
253 // of it! This couldn't happen before as Undo was not handled inside
254 // inset now after the Undo LyX tries to call inset->Edit(...) again
255 // and cannot do this as the cursor is behind the inset and GetInset
256 // does not return the inset!
257 if (inset->IsTextInset()) {
258 if (text->cursor.par->isRightToLeftPar())
266 text->UnFreezeUndo();
271 // Open and lock an updatable inset
272 void BufferView::open_new_inset(UpdatableInset * new_inset)
276 insertInset(new_inset);
279 new_inset->Edit(this, 0, 0, 0);
282 /* This is also a buffer property (ale) */
283 // Not so sure about that. a goto Label function can not be buffer local, just
284 // think how this will work in a multiwindo/buffer environment, all the
285 // cursors in all the views showing this buffer will move. (Lgb)
286 // OK, then no cursor action should be allowed in buffer. (ale)
287 bool BufferView::gotoLabel(string const & label)
290 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
291 it != buffer()->inset_iterator_end(); ++it) {
292 vector<string> labels = (*it)->getLabelList();
293 if ( find(labels.begin(),labels.end(),label)
296 text->SetCursor(it.getPar(), it.getPos());
297 text->sel_cursor = text->cursor;
305 void BufferView::allFloats(char flag, char figmar)
307 if (!available()) return;
309 LyXCursor cursor = text->cursor;
311 if (!flag && cursor.par->footnoteflag != LyXParagraph::NO_FOOTNOTE
313 && cursor.par->footnotekind != LyXParagraph::FOOTNOTE
314 && cursor.par->footnotekind != LyXParagraph::MARGIN)
316 && cursor.par->footnotekind != LyXParagraph::FIG
317 && cursor.par->footnotekind != LyXParagraph::TAB
318 && cursor.par->footnotekind != LyXParagraph::WIDE_FIG
319 && cursor.par->footnotekind != LyXParagraph::WIDE_TAB
320 && cursor.par->footnotekind != LyXParagraph::ALGORITHM)))
325 LyXCursor tmpcursor = cursor;
326 cursor.par = tmpcursor.par->ParFromPos(tmpcursor.pos);
327 cursor.pos = tmpcursor.par->PositionInParFromPos(tmpcursor.pos);
329 LyXParagraph *par = buffer()->paragraph;
332 if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
334 && par->footnotekind != LyXParagraph::FOOTNOTE
335 && par->footnotekind != LyXParagraph::MARGIN)
337 && par->footnotekind != LyXParagraph::FIG
338 && par->footnotekind != LyXParagraph::TAB
339 && par->footnotekind != LyXParagraph::WIDE_FIG
340 && par->footnotekind != LyXParagraph::WIDE_TAB
341 && par->footnotekind != LyXParagraph::ALGORITHM
346 && par->previous->footnoteflag !=
347 LyXParagraph::CLOSED_FOOTNOTE){ /* should be */
348 text->SetCursorIntern(par->previous,
350 text->OpenFootnote();
354 if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
358 par->footnotekind != LyXParagraph::FOOTNOTE
360 par->footnotekind != LyXParagraph::MARGIN
365 par->footnotekind != LyXParagraph::FIG
367 par->footnotekind != LyXParagraph::TAB
369 par->footnotekind != LyXParagraph::WIDE_FIG
371 par->footnotekind != LyXParagraph::WIDE_TAB
373 par->footnotekind != LyXParagraph::ALGORITHM
377 text->SetCursorIntern(par, 0);
378 text->CloseFootnote();
384 text->SetCursorIntern(cursor.par, cursor.pos);
391 void BufferView::insertNote()
393 InsetInfo * new_inset = new InsetInfo();
394 insertInset(new_inset);
395 new_inset->Edit(this, 0, 0, 0);
399 void BufferView::openStuff()
402 owner()->getMiniBuffer()->Set(_("Open/Close..."));
413 void BufferView::toggleFloat()
416 owner()->getMiniBuffer()->Set(_("Open/Close..."));
420 text->ToggleFootnote();
426 void BufferView::menuUndo()
429 owner()->getMiniBuffer()->Set(_("Undo"));
433 if (!text->TextUndo())
434 owner()->getMiniBuffer()->Set(_("No further undo information"));
442 void BufferView::menuRedo()
444 if (the_locking_inset) {
445 owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
450 owner()->getMiniBuffer()->Set(_("Redo"));
454 if (!text->TextRedo())
455 owner()->getMiniBuffer()->Set(_("No further redo information"));
463 void BufferView::hyphenationPoint()
468 InsetSpecialChar * new_inset =
469 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
470 insertInset(new_inset);
475 void BufferView::ldots()
480 InsetSpecialChar * new_inset =
481 new InsetSpecialChar(InsetSpecialChar::LDOTS);
482 insertInset(new_inset);
487 void BufferView::endOfSentenceDot()
492 InsetSpecialChar * new_inset =
493 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
494 insertInset(new_inset);
499 void BufferView::menuSeparator()
504 InsetSpecialChar * new_inset =
505 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
506 insertInset(new_inset);
511 void BufferView::newline()
516 text->InsertChar(LyXParagraph::META_NEWLINE);
522 void BufferView::protectedBlank()
527 InsetSpecialChar * new_inset =
528 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
529 insertInset(new_inset);
534 void BufferView::hfill()
539 text->InsertChar(LyXParagraph::META_HFILL);
544 void BufferView::copyEnvironment()
547 text->copyEnvironmentType();
548 // clear the selection, even if mark_set
550 text->ClearSelection();
552 owner()->getMiniBuffer()->Set(_("Paragraph environment type copied"));
557 void BufferView::pasteEnvironment()
560 text->pasteEnvironmentType();
561 owner()->getMiniBuffer()->Set(_("Paragraph environment type set"));
567 void BufferView::copy()
570 text->CopySelection();
571 // clear the selection, even if mark_set
573 text->ClearSelection();
575 owner()->getMiniBuffer()->Set(_("Copy"));
579 void BufferView::cut()
584 text->CutSelection();
586 owner()->getMiniBuffer()->Set(_("Cut"));
591 void BufferView::paste()
593 if (!available()) return;
595 owner()->getMiniBuffer()->Set(_("Paste"));
597 // clear the selection
599 text->ClearSelection();
603 text->PasteSelection();
606 // clear the selection
608 text->ClearSelection();
613 void BufferView::gotoNote()
615 if (!available()) return;
622 if (!text->GotoNextNote()) {
624 || text->cursor.par != text->FirstParagraph()) {
626 text->cursor.par = text->FirstParagraph();
627 text->cursor.pos = 0;
628 if (!text->GotoNextNote()) {
630 owner()->getMiniBuffer()->Set(_("No more notes"));
634 owner()->getMiniBuffer()->Set(_("No more notes"));
639 text->sel_cursor = text->cursor;
643 void BufferView::insertCorrectQuote()
647 if (text->cursor.pos)
648 c = text->cursor.par->GetChar(text->cursor.pos - 1);
652 insertInset(new InsetQuotes(c, buffer()->params));
656 /* these functions are for the spellchecker */
657 char * BufferView::nextWord(float & value)
664 char * string = text->SelectNextWord(value);
670 void BufferView::selectLastWord()
672 if (!available()) return;
676 text->SelectSelectedWord();
677 toggleSelection(false);
682 void BufferView::endOfSpellCheck()
684 if (!available()) return;
688 text->SelectSelectedWord();
689 text->ClearSelection();
694 void BufferView::replaceWord(string const & replacestring)
696 if (!available()) return;
701 /* clear the selection (if there is any) */
702 toggleSelection(false);
705 /* clear the selection (if there is any) */
706 toggleSelection(false);
707 text->ReplaceSelectionWithString(replacestring.c_str());
709 text->SetSelectionOverString(replacestring.c_str());
711 // Go back so that replacement string is also spellchecked
712 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
713 text->CursorLeftIntern();
717 // End of spellchecker stuff
720 bool BufferView::lockInset(UpdatableInset * inset)
722 if (!the_locking_inset && inset) {
723 the_locking_inset = inset;
726 return the_locking_inset->LockInsetInInset(this, inset);
732 void BufferView::showLockedInsetCursor(long x, long y, int asc, int desc)
734 if (the_locking_inset && available()) {
735 LyXCursor cursor = text->cursor;
736 if ((cursor.pos - 1 >= 0) &&
737 (cursor.par->GetChar(cursor.pos-1) ==
738 LyXParagraph::META_INSET) &&
739 (cursor.par->GetInset(cursor.pos - 1) ==
740 the_locking_inset->GetLockingInset()))
741 text->SetCursor(cursor, cursor.par, cursor.pos-1);
742 y += cursor.y + the_locking_inset->InsetInInsetY();
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()){
760 y += text->cursor.y + the_locking_inset->InsetInInsetY();
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)){
845 bool BufferView::ChangeRefs(string const & from, string const & to)
848 LyXParagraph * par = buffer()->paragraph;
849 LyXCursor cursor = text->cursor;
850 LyXCursor tmpcursor = cursor;
851 cursor.par = tmpcursor.par->ParFromPos(tmpcursor.pos);
852 cursor.pos = tmpcursor.par->PositionInParFromPos(tmpcursor.pos);
856 for (LyXParagraph::inset_iterator it = par->inset_iterator_begin();
857 it != par->inset_iterator_end(); ++it) {
858 if ((*it)->LyxCode() == Inset::REF_CODE) {
859 InsetCommand * inset = static_cast<InsetCommand *>(*it);
860 if (inset->getContents() == from) {
861 inset->setContents(to);
868 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
869 // this is possible now, since SetCursor takes
870 // care about footnotes
871 text->SetCursorIntern(par, 0);
872 text->RedoParagraphs(text->cursor,
873 text->cursor.par->Next());
879 text->SetCursorIntern(cursor.par, cursor.pos);