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
106 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE)
108 text->SetCursor(this, cursor, par, 0);
109 if (par->AutoDeleteInsets()){
112 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
114 text->RedoParagraphs(this, cursor,
115 cursor.par()->Next());
116 text->FullRebreak(this);
123 // avoid forbidden cursor positions caused by error removing
124 if (tmpcursor.pos() > tmpcursor.par()->Last())
125 tmpcursor.pos(tmpcursor.par()->Last());
126 text->SetCursorIntern(this, tmpcursor.par(), tmpcursor.pos());
132 void BufferView::insertErrors(TeXErrors & terr)
134 // Save the cursor position
135 LyXCursor cursor = text->cursor;
138 // This is drastic, but it's the only fix, I could find. (Asger)
143 for (TeXErrors::Errors::const_iterator cit = terr.begin();
146 string desctext((*cit).error_desc);
147 string errortext((*cit).error_text);
148 string msgtxt = desctext + '\n' + errortext;
149 int errorrow = (*cit).error_in_line;
151 // Insert error string for row number
155 buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos);
157 LyXParagraph * texrowpar = 0;
160 texrowpar = text->FirstParagraph();
163 texrowpar = text->GetParFromID(tmpid);
169 InsetError * new_inset = new InsetError(msgtxt);
170 text->SetCursorIntern(this, texrowpar, tmppos);
171 text->InsertInset(this, new_inset);
172 text->FullRebreak(this);
174 // Restore the cursor position
175 text->SetCursorIntern(this, cursor.par(), cursor.pos());
179 void BufferView::setCursorFromRow(int row)
184 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
186 LyXParagraph * texrowpar;
189 texrowpar = text->FirstParagraph();
192 texrowpar = text->GetParFromID(tmpid);
194 text->SetCursor(this, texrowpar, tmppos);
197 bool BufferView::insertInset(Inset * inset, string const & lout,
200 // if we are in a locking inset we should try to insert the
201 // inset there otherwise this is a illegal function now
202 if (the_locking_inset) {
203 if (the_locking_inset->InsertInsetAllowed(inset) &&
204 the_locking_inset->InsertInset(this, inset))
210 // check for table/list in tables
211 if (no_table && text->cursor.par()->table){
212 WriteAlert(_("Impossible Operation!"),
213 _("Cannot insert table/list in table."),
219 // not quite sure if we want this...
220 text->SetCursorParUndo(buffer());
225 update(BufferView::SELECT|BufferView::FITCUR);
226 text->BreakParagraph(this);
227 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
229 if (text->cursor.par()->Last()) {
230 text->CursorLeft(this);
232 text->BreakParagraph(this);
233 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
236 pair<bool, LyXTextClass::size_type> lres =
237 textclasslist.NumberOfLayout(buffer()->params
239 LyXTextClass::size_type lay;
240 if (lres.first != false) {
244 // layout not fount using default "Standard" (0)
248 text->SetLayout(this, lay);
250 text->SetParagraph(this, 0, 0,
252 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
256 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
258 text->current_font.setLatex(LyXFont::OFF);
261 text->InsertInset(this, inset);
263 // if we enter a text-inset the cursor should be to the left side
264 // of it! This couldn't happen before as Undo was not handled inside
265 // inset now after the Undo LyX tries to call inset->Edit(...) again
266 // and cannot do this as the cursor is behind the inset and GetInset
267 // does not return the inset!
268 if (inset->IsTextInset()) {
269 if (text->cursor.par()->isRightToLeftPar(buffer()->params))
270 text->CursorRight(this);
272 text->CursorLeft(this);
275 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
277 text->UnFreezeUndo();
282 // Open and lock an updatable inset
283 bool BufferView::open_new_inset(UpdatableInset * new_inset)
287 if (!insertInset(new_inset))
289 text->CursorLeft(this);
290 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
291 new_inset->Edit(this, 0, 0, 0);
295 /* This is also a buffer property (ale) */
296 // Not so sure about that. a goto Label function can not be buffer local, just
297 // think how this will work in a multiwindo/buffer environment, all the
298 // cursors in all the views showing this buffer will move. (Lgb)
299 // OK, then no cursor action should be allowed in buffer. (ale)
300 bool BufferView::gotoLabel(string const & label)
303 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
304 it != buffer()->inset_iterator_end(); ++it) {
305 vector<string> labels = (*it)->getLabelList();
306 if ( find(labels.begin(),labels.end(),label)
309 text->SetCursor(this, it.getPar(), it.getPos());
310 text->sel_cursor = text->cursor;
311 update(BufferView::SELECT|BufferView::FITCUR);
320 void BufferView::allFloats(char flag, char figmar)
322 if (!available()) return;
324 LyXCursor cursor = text->cursor;
327 && cursor.par()->footnoteflag != LyXParagraph::NO_FOOTNOTE
329 && cursor.par()->footnotekind != LyXParagraph::FOOTNOTE
330 && cursor.par()->footnotekind != LyXParagraph::MARGIN
333 && cursor.par()->footnotekind != LyXParagraph::FIG
334 && cursor.par()->footnotekind != LyXParagraph::TAB
335 && cursor.par()->footnotekind != LyXParagraph::WIDE_FIG
336 && cursor.par()->footnotekind != LyXParagraph::WIDE_TAB
337 && cursor.par()->footnotekind != LyXParagraph::ALGORITHM)))
342 LyXCursor tmpcursor = cursor;
343 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
344 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
346 LyXParagraph *par = buffer()->paragraph;
349 if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
351 && par->footnotekind != LyXParagraph::FOOTNOTE
352 && par->footnotekind != LyXParagraph::MARGIN)
354 && par->footnotekind != LyXParagraph::FIG
355 && par->footnotekind != LyXParagraph::TAB
356 && par->footnotekind != LyXParagraph::WIDE_FIG
357 && par->footnotekind != LyXParagraph::WIDE_TAB
358 && par->footnotekind != LyXParagraph::ALGORITHM
363 && par->previous->footnoteflag !=
364 LyXParagraph::CLOSED_FOOTNOTE){ /* should be */
365 text->SetCursorIntern(this,
368 text->OpenFootnote(this);
372 if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
376 par->footnotekind != LyXParagraph::FOOTNOTE
378 par->footnotekind != LyXParagraph::MARGIN
383 par->footnotekind != LyXParagraph::FIG
385 par->footnotekind != LyXParagraph::TAB
387 par->footnotekind != LyXParagraph::WIDE_FIG
389 par->footnotekind != LyXParagraph::WIDE_TAB
391 par->footnotekind != LyXParagraph::ALGORITHM
395 text->SetCursorIntern(this, par, 0);
396 text->CloseFootnote(this);
402 text->SetCursorIntern(this, cursor.par(), cursor.pos());
410 void BufferView::insertNote()
412 InsetInfo * new_inset = new InsetInfo();
413 insertInset(new_inset);
414 new_inset->Edit(this, 0, 0, 0);
419 void BufferView::openStuff()
422 owner()->getMiniBuffer()->Set(_("Open/Close..."));
425 update(BufferView::SELECT|BufferView::FITCUR);
426 text->OpenStuff(this);
427 update(BufferView::SELECT|BufferView::FITCUR);
433 void BufferView::toggleFloat()
436 owner()->getMiniBuffer()->Set(_("Open/Close..."));
439 update(BufferView::SELECT|BufferView::FITCUR);
440 text->ToggleFootnote(this);
441 update(BufferView::SELECT|BufferView::FITCUR);
447 void BufferView::menuUndo()
450 owner()->getMiniBuffer()->Set(_("Undo"));
453 update(BufferView::SELECT|BufferView::FITCUR);
454 if (!text->TextUndo(this))
455 owner()->getMiniBuffer()->Set(_("No further undo information"));
457 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
463 void BufferView::menuRedo()
465 if (the_locking_inset) {
466 owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
471 owner()->getMiniBuffer()->Set(_("Redo"));
474 update(BufferView::SELECT|BufferView::FITCUR);
475 if (!text->TextRedo(this))
476 owner()->getMiniBuffer()->Set(_("No further redo information"));
478 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
484 void BufferView::hyphenationPoint()
488 update(BufferView::SELECT|BufferView::FITCUR);
489 InsetSpecialChar * new_inset =
490 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
491 insertInset(new_inset);
496 void BufferView::ldots()
500 update(BufferView::SELECT|BufferView::FITCUR);
501 InsetSpecialChar * new_inset =
502 new InsetSpecialChar(InsetSpecialChar::LDOTS);
503 insertInset(new_inset);
508 void BufferView::endOfSentenceDot()
512 update(BufferView::SELECT|BufferView::FITCUR);
513 InsetSpecialChar * new_inset =
514 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
515 insertInset(new_inset);
520 void BufferView::menuSeparator()
524 update(BufferView::SELECT|BufferView::FITCUR);
525 InsetSpecialChar * new_inset =
526 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
527 insertInset(new_inset);
532 void BufferView::newline()
536 update(BufferView::SELECT|BufferView::FITCUR);
537 text->InsertChar(this, LyXParagraph::META_NEWLINE);
538 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
543 void BufferView::protectedBlank()
547 update(BufferView::SELECT|BufferView::FITCUR);
548 InsetSpecialChar * new_inset =
549 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
550 insertInset(new_inset);
555 void BufferView::hfill()
559 update(BufferView::SELECT|BufferView::FITCUR);
560 text->InsertChar(this, LyXParagraph::META_HFILL);
561 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
565 void BufferView::copyEnvironment()
568 text->copyEnvironmentType();
569 // clear the selection, even if mark_set
571 text->ClearSelection();
572 update(BufferView::SELECT|BufferView::FITCUR);
573 owner()->getMiniBuffer()->Set(_("Paragraph environment type copied"));
578 void BufferView::pasteEnvironment()
581 text->pasteEnvironmentType(this);
582 owner()->getMiniBuffer()->Set(_("Paragraph environment type set"));
583 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
588 void BufferView::copy()
591 text->CopySelection(this);
592 // clear the selection, even if mark_set
594 text->ClearSelection();
595 update(BufferView::SELECT|BufferView::FITCUR);
596 owner()->getMiniBuffer()->Set(_("Copy"));
600 void BufferView::cut()
604 update(BufferView::SELECT|BufferView::FITCUR);
605 text->CutSelection(this);
606 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
607 owner()->getMiniBuffer()->Set(_("Cut"));
612 void BufferView::paste()
614 if (!available()) return;
616 owner()->getMiniBuffer()->Set(_("Paste"));
618 // clear the selection
620 text->ClearSelection();
621 update(BufferView::SELECT|BufferView::FITCUR);
624 text->PasteSelection(this);
625 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
627 // clear the selection
629 text->ClearSelection();
630 update(BufferView::SELECT|BufferView::FITCUR);
634 void BufferView::gotoNote()
636 if (!available()) return;
640 update(BufferView::SELECT|BufferView::FITCUR);
643 if (!text->GotoNextNote(this)) {
644 if (text->cursor.pos()
645 || text->cursor.par() != text->FirstParagraph()) {
647 text->cursor.par(text->FirstParagraph());
649 if (!text->GotoNextNote(this)) {
651 owner()->getMiniBuffer()->Set(_("No more notes"));
655 owner()->getMiniBuffer()->Set(_("No more notes"));
659 update(BufferView::SELECT|BufferView::FITCUR);
660 text->sel_cursor = text->cursor;
664 void BufferView::insertCorrectQuote()
668 if (text->cursor.pos())
669 c = text->cursor.par()->GetChar(text->cursor.pos() - 1);
673 insertInset(new InsetQuotes(c, buffer()->params));
677 /* these functions are for the spellchecker */
678 char * BufferView::nextWord(float & value)
685 char * string = text->SelectNextWord(this, value);
691 void BufferView::selectLastWord()
693 if (!available()) return;
697 text->SelectSelectedWord(this);
698 toggleSelection(false);
699 update(BufferView::SELECT|BufferView::FITCUR);
703 void BufferView::endOfSpellCheck()
705 if (!available()) return;
709 text->SelectSelectedWord(this);
710 text->ClearSelection();
711 update(BufferView::SELECT|BufferView::FITCUR);
715 void BufferView::replaceWord(string const & replacestring)
717 if (!available()) return;
720 update(BufferView::SELECT|BufferView::FITCUR);
722 /* clear the selection (if there is any) */
723 toggleSelection(false);
724 update(BufferView::SELECT|BufferView::FITCUR);
726 /* clear the selection (if there is any) */
727 toggleSelection(false);
728 text->ReplaceSelectionWithString(this, replacestring.c_str());
730 text->SetSelectionOverString(this, replacestring.c_str());
732 // Go back so that replacement string is also spellchecked
733 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
734 text->CursorLeftIntern(this);
736 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
738 // End of spellchecker stuff
741 bool BufferView::lockInset(UpdatableInset * inset)
743 if (!the_locking_inset && inset) {
744 the_locking_inset = inset;
747 return the_locking_inset->LockInsetInInset(this, inset);
753 void BufferView::showLockedInsetCursor(long x, long y, int asc, int desc)
755 if (the_locking_inset && available()) {
756 LyXCursor cursor = text->cursor;
757 if ((cursor.pos() - 1 >= 0) &&
758 (cursor.par()->GetChar(cursor.pos() - 1) ==
759 LyXParagraph::META_INSET) &&
760 (cursor.par()->GetInset(cursor.pos() - 1) ==
761 the_locking_inset->GetLockingInset()))
762 text->SetCursor(this, cursor,
763 cursor.par(), cursor.pos() - 1);
764 y += cursor.y() + the_locking_inset->InsetInInsetY();
765 pimpl_->screen_->ShowManualCursor(text, x, y, asc, desc,
766 LyXScreen::BAR_SHAPE);
771 void BufferView::hideLockedInsetCursor()
773 if (the_locking_inset && available()) {
774 pimpl_->screen_->HideCursor();
779 void BufferView::fitLockedInsetCursor(long x, long y, int asc, int desc)
781 if (the_locking_inset && available()){
782 y += text->cursor.y() + the_locking_inset->InsetInInsetY();
783 if (pimpl_->screen_->FitManualCursor(text, x, y, asc, desc))
789 int BufferView::unlockInset(UpdatableInset * inset)
791 if (inset && the_locking_inset == inset) {
792 inset->InsetUnlock(this);
793 the_locking_inset = 0;
796 } else if (inset && the_locking_inset &&
797 the_locking_inset->UnlockInsetInInset(this, inset)) {
801 return bufferlist.unlockInset(inset);
805 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
807 if (!the_locking_inset)
808 return; // shouldn't happen
809 if (kind == Undo::EDIT) // in this case insets would not be stored!
811 text->SetUndo(buffer(), kind,
814 ParFromPos(text->cursor.pos())->previous,
816 ParFromPos(text->cursor.pos())->next
818 text->cursor.par()->previous,
819 text->cursor.par()->next
825 void BufferView::updateInset(Inset * inset, bool mark_dirty)
830 // first check for locking insets
831 if (the_locking_inset) {
832 if (the_locking_inset == inset) {
833 if (text->UpdateInset(this, inset)){
836 if (buffer()->isLyxClean())
837 owner()->getMiniBuffer()->
839 buffer()->markDirty();
844 } else if (the_locking_inset->UpdateInsetInInset(this,inset)) {
845 if (text->UpdateInset(this, the_locking_inset)) {
848 if (buffer()->isLyxClean())
849 owner()->getMiniBuffer()->
851 buffer()->markDirty();
859 // then check the current buffer
862 update(BufferView::UPDATE);
863 if (text->UpdateInset(this, inset)){
865 update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
873 bool BufferView::ChangeRefs(string const & from, string const & to)
876 LyXParagraph * par = buffer()->paragraph;
877 LyXCursor cursor = text->cursor;
878 LyXCursor tmpcursor = cursor;
880 cursor.par(tmpcursor.par()->ParFromPos(tmpcursor.pos()));
881 cursor.pos(tmpcursor.par()->PositionInParFromPos(tmpcursor.pos()));
883 cursor.par(tmpcursor.par());
884 cursor.pos(tmpcursor.pos());
889 for (LyXParagraph::inset_iterator it = par->inset_iterator_begin();
890 it != par->inset_iterator_end(); ++it) {
891 if ((*it)->LyxCode() == Inset::REF_CODE) {
892 InsetCommand * inset = static_cast<InsetCommand *>(*it);
893 if (inset->getContents() == from) {
894 inset->setContents(to);
902 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
904 // this is possible now, since SetCursor takes
905 // care about footnotes
906 text->SetCursorIntern(this, par, 0);
907 text->RedoParagraphs(this, text->cursor,
908 text->cursor.par()->Next());
909 text->FullRebreak(this);
916 text->SetCursorIntern(this, cursor.par(), cursor.pos());