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 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
157 buffer()->texrow.increasePos(tmpid, tmppos);
160 LyXParagraph * texrowpar = 0;
163 texrowpar = text->FirstParagraph();
166 texrowpar = text->GetParFromID(tmpid);
172 InsetError * new_inset = new InsetError(msgtxt);
173 text->SetCursorIntern(this, texrowpar, tmppos);
174 text->InsertInset(this, new_inset);
175 text->FullRebreak(this);
177 // Restore the cursor position
178 text->SetCursorIntern(this, cursor.par(), cursor.pos());
182 void BufferView::setCursorFromRow(int row)
187 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
189 LyXParagraph * texrowpar;
192 texrowpar = text->FirstParagraph();
195 texrowpar = text->GetParFromID(tmpid);
197 text->SetCursor(this, texrowpar, tmppos);
201 bool BufferView::insertInset(Inset * inset, string const & lout,
204 // if we are in a locking inset we should try to insert the
205 // inset there otherwise this is a illegal function now
206 if (theLockingInset()) {
207 if (theLockingInset()->InsertInsetAllowed(inset))
208 return theLockingInset()->InsertInset(this, inset);
212 // not quite sure if we want this...
213 text->SetCursorParUndo(buffer());
218 update(BufferView::SELECT|BufferView::FITCUR);
219 text->BreakParagraph(this);
220 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
222 if (text->cursor.par()->Last()) {
223 text->CursorLeft(this);
225 text->BreakParagraph(this);
226 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
229 pair<bool, LyXTextClass::size_type> lres =
230 textclasslist.NumberOfLayout(buffer()->params
232 LyXTextClass::size_type lay;
233 if (lres.first != false) {
237 // layout not fount using default "Standard" (0)
241 text->SetLayout(this, lay);
243 text->SetParagraph(this, 0, 0,
245 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
249 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
251 text->current_font.setLatex(LyXFont::OFF);
254 text->InsertInset(this, inset);
255 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
257 text->UnFreezeUndo();
262 // Open and lock an updatable inset
263 bool BufferView::open_new_inset(UpdatableInset * new_inset)
267 if (!insertInset(new_inset)) {
271 // text->CursorLeft(this);
272 // update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
273 new_inset->Edit(this, 0, 0, 0);
277 /* This is also a buffer property (ale) */
278 // Not so sure about that. a goto Label function can not be buffer local, just
279 // think how this will work in a multiwindo/buffer environment, all the
280 // cursors in all the views showing this buffer will move. (Lgb)
281 // OK, then no cursor action should be allowed in buffer. (ale)
282 bool BufferView::gotoLabel(string const & label)
285 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
286 it != buffer()->inset_iterator_end(); ++it) {
287 vector<string> labels = (*it)->getLabelList();
288 if (find(labels.begin(),labels.end(),label)
291 text->SetCursor(this, it.getPar(), it.getPos());
292 text->sel_cursor = text->cursor;
293 update(BufferView::SELECT|BufferView::FITCUR);
302 void BufferView::allFloats(char flag, char figmar)
304 if (!available()) return;
306 LyXCursor cursor = text->cursor;
309 && cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE
311 && cursor.par()->footnotekind != LyXParagraph::FOOTNOTE
312 && cursor.par()->footnotekind != LyXParagraph::MARGIN
315 && cursor.par()->footnotekind != LyXParagraph::FIG
316 && cursor.par()->footnotekind != LyXParagraph::TAB
317 && cursor.par()->footnotekind != LyXParagraph::WIDE_FIG
318 && cursor.par()->footnotekind != LyXParagraph::WIDE_TAB
319 && cursor.par()->footnotekind != LyXParagraph::ALGORITHM)))
324 LyXCursor tmpcursor = cursor;
325 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
326 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
328 LyXParagraph *par = buffer()->paragraph;
331 if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
333 && par->footnotekind != LyXParagraph::FOOTNOTE
334 && par->footnotekind != LyXParagraph::MARGIN)
336 && par->footnotekind != LyXParagraph::FIG
337 && par->footnotekind != LyXParagraph::TAB
338 && par->footnotekind != LyXParagraph::WIDE_FIG
339 && par->footnotekind != LyXParagraph::WIDE_TAB
340 && par->footnotekind != LyXParagraph::ALGORITHM
345 && par->previous->footnoteflag !=
346 LyXParagraph::CLOSED_FOOTNOTE){ /* should be */
347 text->SetCursorIntern(this,
350 text->OpenFootnote(this);
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(this, par, 0);
378 text->CloseFootnote(this);
384 text->SetCursorIntern(this, 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);
400 void BufferView::openStuff()
403 owner()->getMiniBuffer()->Set(_("Open/Close..."));
406 update(BufferView::SELECT|BufferView::FITCUR);
407 text->OpenStuff(this);
408 update(BufferView::SELECT|BufferView::FITCUR);
414 void BufferView::toggleFloat()
417 owner()->getMiniBuffer()->Set(_("Open/Close..."));
420 update(BufferView::SELECT|BufferView::FITCUR);
421 text->ToggleFootnote(this);
422 update(BufferView::SELECT|BufferView::FITCUR);
428 void BufferView::menuUndo()
431 owner()->getMiniBuffer()->Set(_("Undo"));
434 update(BufferView::SELECT|BufferView::FITCUR);
435 if (!text->TextUndo(this))
436 owner()->getMiniBuffer()->Set(_("No further undo information"));
438 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
444 void BufferView::menuRedo()
446 if (theLockingInset()) {
447 owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
452 owner()->getMiniBuffer()->Set(_("Redo"));
455 update(BufferView::SELECT|BufferView::FITCUR);
456 if (!text->TextRedo(this))
457 owner()->getMiniBuffer()->Set(_("No further redo information"));
459 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
465 void BufferView::hyphenationPoint()
469 update(BufferView::SELECT|BufferView::FITCUR);
470 InsetSpecialChar * new_inset =
471 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
472 insertInset(new_inset);
477 void BufferView::ldots()
481 update(BufferView::SELECT|BufferView::FITCUR);
482 InsetSpecialChar * new_inset =
483 new InsetSpecialChar(InsetSpecialChar::LDOTS);
484 insertInset(new_inset);
489 void BufferView::endOfSentenceDot()
493 update(BufferView::SELECT|BufferView::FITCUR);
494 InsetSpecialChar * new_inset =
495 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
496 insertInset(new_inset);
501 void BufferView::menuSeparator()
505 update(BufferView::SELECT|BufferView::FITCUR);
506 InsetSpecialChar * new_inset =
507 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
508 insertInset(new_inset);
513 void BufferView::newline()
517 update(BufferView::SELECT|BufferView::FITCUR);
518 text->InsertChar(this, LyXParagraph::META_NEWLINE);
519 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
524 void BufferView::protectedBlank()
528 update(BufferView::SELECT|BufferView::FITCUR);
529 InsetSpecialChar * new_inset =
530 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
531 insertInset(new_inset);
536 void BufferView::hfill()
540 update(BufferView::SELECT|BufferView::FITCUR);
541 text->InsertChar(this, LyXParagraph::META_HFILL);
542 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
546 void BufferView::copyEnvironment()
549 text->copyEnvironmentType();
550 // clear the selection, even if mark_set
552 text->ClearSelection();
553 update(BufferView::SELECT|BufferView::FITCUR);
554 owner()->getMiniBuffer()->Set(_("Paragraph environment type copied"));
559 void BufferView::pasteEnvironment()
562 text->pasteEnvironmentType(this);
563 owner()->getMiniBuffer()->Set(_("Paragraph environment type set"));
564 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
569 void BufferView::copy()
572 text->CopySelection(this);
573 // clear the selection, even if mark_set
575 text->ClearSelection();
576 update(BufferView::SELECT|BufferView::FITCUR);
577 owner()->getMiniBuffer()->Set(_("Copy"));
581 void BufferView::cut()
585 update(BufferView::SELECT|BufferView::FITCUR);
586 text->CutSelection(this);
587 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
588 owner()->getMiniBuffer()->Set(_("Cut"));
593 void BufferView::paste()
595 if (!available()) return;
597 owner()->getMiniBuffer()->Set(_("Paste"));
599 // clear the selection
601 text->ClearSelection();
602 update(BufferView::SELECT|BufferView::FITCUR);
605 text->PasteSelection(this);
606 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
608 // clear the selection
610 text->ClearSelection();
611 update(BufferView::SELECT|BufferView::FITCUR);
615 void BufferView::gotoNote()
617 if (!available()) return;
621 update(BufferView::SELECT|BufferView::FITCUR);
624 if (!text->GotoNextNote(this)) {
625 if (text->cursor.pos()
626 || text->cursor.par() != text->FirstParagraph()) {
628 text->cursor.par(text->FirstParagraph());
630 if (!text->GotoNextNote(this)) {
632 owner()->getMiniBuffer()->Set(_("No more notes"));
636 owner()->getMiniBuffer()->Set(_("No more notes"));
640 update(BufferView::SELECT|BufferView::FITCUR);
641 text->sel_cursor = text->cursor;
645 void BufferView::insertCorrectQuote()
649 if (text->cursor.pos())
650 c = text->cursor.par()->GetChar(text->cursor.pos() - 1);
654 insertInset(new InsetQuotes(c, buffer()->params));
658 /* these functions are for the spellchecker */
659 string const BufferView::nextWord(float & value)
666 return text->SelectNextWord(this, value);
670 void BufferView::selectLastWord()
672 if (!available()) return;
676 text->SelectSelectedWord(this);
677 toggleSelection(false);
678 update(BufferView::SELECT|BufferView::FITCUR);
682 void BufferView::endOfSpellCheck()
684 if (!available()) return;
688 text->SelectSelectedWord(this);
689 text->ClearSelection();
690 update(BufferView::SELECT|BufferView::FITCUR);
694 void BufferView::replaceWord(string const & replacestring)
696 if (!available()) return;
699 update(BufferView::SELECT|BufferView::FITCUR);
701 /* clear the selection (if there is any) */
702 toggleSelection(false);
703 update(BufferView::SELECT|BufferView::FITCUR);
705 /* clear the selection (if there is any) */
706 toggleSelection(false);
707 text->ReplaceSelectionWithString(this, replacestring);
709 text->SetSelectionOverString(this, replacestring);
711 // Go back so that replacement string is also spellchecked
712 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
713 text->CursorLeft(this);
715 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
717 // End of spellchecker stuff
720 bool BufferView::lockInset(UpdatableInset * inset)
722 if (!theLockingInset() && inset) {
723 theLockingInset(inset);
726 return theLockingInset()->LockInsetInInset(this, inset);
732 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
734 if (theLockingInset() && 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 theLockingInset()->GetLockingInset()))
741 text->SetCursor(this, cursor,
742 cursor.par(), cursor.pos() - 1);
743 y += cursor.y() + theLockingInset()->InsetInInsetY();
744 pimpl_->screen_->ShowManualCursor(text, x, y, asc, desc,
745 LyXScreen::BAR_SHAPE);
750 void BufferView::hideLockedInsetCursor()
752 if (theLockingInset() && available()) {
753 pimpl_->screen_->HideCursor();
758 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
760 if (theLockingInset() && available()){
761 y += text->cursor.y() + theLockingInset()->InsetInInsetY();
762 if (pimpl_->screen_->FitManualCursor(text, x, y, asc, desc))
768 int BufferView::unlockInset(UpdatableInset * inset)
770 if (inset && theLockingInset() == inset) {
771 inset->InsetUnlock(this);
775 } else if (inset && theLockingInset() &&
776 theLockingInset()->UnlockInsetInInset(this, inset)) {
780 return bufferlist.unlockInset(inset);
784 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
786 if (!theLockingInset())
787 return; // shouldn't happen
788 if (kind == Undo::EDIT) // in this case insets would not be stored!
790 text->SetUndo(buffer(), kind,
793 ParFromPos(text->cursor.pos())->previous,
795 ParFromPos(text->cursor.pos())->next
797 text->cursor.par()->previous,
798 text->cursor.par()->next
804 void BufferView::updateInset(Inset * inset, bool mark_dirty)
809 // first check for locking insets
810 if (theLockingInset()) {
811 if (theLockingInset() == inset) {
812 if (text->UpdateInset(this, inset)){
815 if (buffer()->isLyxClean())
816 owner()->getMiniBuffer()->
818 buffer()->markDirty();
823 } else if (theLockingInset()->UpdateInsetInInset(this,inset)) {
824 if (text->UpdateInset(this, theLockingInset())) {
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;
859 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
860 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
862 cursor.par(tmpcursor.par());
863 cursor.pos(tmpcursor.pos());
868 for (LyXParagraph::inset_iterator it = par->inset_iterator_begin();
869 it != par->inset_iterator_end(); ++it) {
870 if ((*it)->LyxCode() == Inset::REF_CODE) {
871 InsetCommand * inset = static_cast<InsetCommand *>(*it);
872 if (inset->getContents() == from) {
873 inset->setContents(to);
881 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
883 // this is possible now, since SetCursor takes
884 // care about footnotes
885 text->SetCursorIntern(this, par, 0);
886 text->RedoParagraphs(this, text->cursor,
887 text->cursor.par()->Next());
888 text->FullRebreak(this);
895 text->SetCursorIntern(this, cursor.par(), cursor.pos());
900 UpdatableInset * BufferView::theLockingInset() const
902 return text->the_locking_inset;
906 void BufferView::theLockingInset(UpdatableInset * inset)
908 text->the_locking_inset = inset;