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 * ====================================================== */
14 #include "BufferView.h"
16 #include "lyxcursor.h"
18 #include "insets/inseterror.h"
19 #include "insets/insetinfo.h"
20 #include "insets/insetspecialchar.h"
22 #include "minibuffer.h"
23 #include "bufferlist.h"
24 #include "support/FileInfo.h"
25 #include "lyxscreen.h"
27 extern BufferList bufferlist;
32 // Inserts a file into current document
33 bool BufferView::insertLyXFile(string const & filen)
35 // Copyright CHT Software Service GmbH
38 // Insert a Lyxformat - file into current buffer
40 // Moved from lyx_cb.C (Lgb)
42 if (filen.empty()) return false;
44 string fname = MakeAbsPath(filen);
46 // check if file exist
50 WriteAlert(_("Error!"),
51 _("Specified file is unreadable: "),
52 MakeDisplayPath(fname, 50));
58 ifstream ifs(fname.c_str());
60 WriteAlert(_("Error!"),
61 _("Cannot open specified file: "),
62 MakeDisplayPath(fname, 50));
73 lyxerr.debug() << "Will insert file with header" << endl;
74 res = buffer()->readFile(lex, text->cursor.par);
76 lyxerr.debug() << "Will insert file without header" << endl;
77 res = buffer()->readLyXformat2(lex, text->cursor.par);
84 bool BufferView::removeAutoInsets()
86 LyXParagraph * par = buffer()->paragraph;
88 LyXCursor cursor = text->cursor;
89 LyXCursor tmpcursor = cursor;
90 cursor.par = tmpcursor.par->ParFromPos(tmpcursor.pos);
91 cursor.pos = tmpcursor.par->PositionInParFromPos(tmpcursor.pos);
95 if (par->AutoDeleteInsets()){
97 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
98 // this is possible now, since SetCursor takes
99 // care about footnotes
100 text->SetCursorIntern(par, 0);
101 text->RedoParagraphs(text->cursor,
102 text->cursor.par->Next());
108 // avoid forbidden cursor positions caused by error removing
109 if (cursor.pos > cursor.par->Last())
110 cursor.pos = cursor.par->Last();
111 text->SetCursorIntern(cursor.par, cursor.pos);
117 void BufferView::insertErrors(TeXErrors & terr)
119 // Save the cursor position
120 LyXCursor cursor = text->cursor;
122 // This is drastic, but it's the only fix, I could find. (Asger)
126 for (TeXErrors::Errors::const_iterator cit = terr.begin();
129 string desctext((*cit).error_desc);
130 string errortext((*cit).error_text);
131 string msgtxt = desctext + '\n' + errortext;
132 int errorrow = (*cit).error_in_line;
134 // Insert error string for row number
138 buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos);
140 LyXParagraph * texrowpar = 0;
143 texrowpar = text->FirstParagraph();
146 texrowpar = text->GetParFromID(tmpid);
152 InsetError * new_inset = new InsetError(msgtxt);
153 text->SetCursorIntern(texrowpar, tmppos);
154 text->InsertInset(new_inset);
157 // Restore the cursor position
158 text->SetCursorIntern(cursor.par, cursor.pos);
162 void BufferView::setCursorFromRow(int row)
167 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
169 LyXParagraph * texrowpar;
172 texrowpar = text->FirstParagraph();
175 texrowpar = text->GetParFromID(tmpid);
177 text->SetCursor(texrowpar, tmppos);
180 void BufferView::insertInset(Inset * inset, string const & lout,
183 // check for table/list in tables
184 if (no_table && text->cursor.par->table){
185 WriteAlert(_("Impossible Operation!"),
186 _("Cannot insert table/list in table."),
190 // not quite sure if we want this...
191 text->SetCursorParUndo();
197 text->BreakParagraph();
200 if (text->cursor.par->Last()) {
203 text->BreakParagraph();
207 pair<bool, LyXTextClass::size_type> lres =
208 textclasslist.NumberOfLayout(buffer()->params
210 LyXTextClass::size_type lay;
211 if (lres.first != false) {
215 // layout not fount using default "Standard" (0)
219 text->SetLayout(lay);
221 text->SetParagraph(0, 0,
223 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
229 text->current_font.setLatex(LyXFont::OFF);
232 text->InsertInset(inset);
234 // if we enter a text-inset the cursor should be to the left side
235 // of it! This couldn't happen before as Undo was not handled inside
236 // inset now after the Undo LyX tries to call inset->Edit(...) again
237 // and cannot do this as the cursor is behind the inset and GetInset
238 // does not return the inset!
239 if (inset->IsTextInset()) {
240 if (text->cursor.par->getParDirection()==LYX_DIR_LEFT_TO_RIGHT)
248 text->UnFreezeUndo();
252 // Open and lock an updatable inset
253 void BufferView::open_new_inset(UpdatableInset * new_inset)
257 insertInset(new_inset);
260 new_inset->Edit(this, 0, 0, 0);
263 /* This is also a buffer property (ale) */
264 // Not so sure about that. a goto Label function can not be buffer local, just
265 // think how this will work in a multiwindo/buffer environment, all the
266 // cursors in all the views showing this buffer will move. (Lgb)
267 // OK, then no cursor action should be allowed in buffer. (ale)
268 bool BufferView::gotoLabel(string const & label)
271 LyXParagraph * par = buffer()->paragraph;
272 LyXParagraph::size_type pos;
276 while ((inset = par->ReturnNextInsetPointer(pos))){
277 for (int i = 0; i < inset->GetNumberOfLabels(); ++i) {
278 if (label == inset->getLabel(i)) {
280 text->SetCursor(par, pos);
281 text->sel_cursor = text->cursor;
293 void BufferView::allFloats(char flag, char figmar)
295 if (!available()) return;
297 LyXCursor cursor = text->cursor;
299 if (!flag && cursor.par->footnoteflag != LyXParagraph::NO_FOOTNOTE
301 && cursor.par->footnotekind != LyXParagraph::FOOTNOTE
302 && cursor.par->footnotekind != LyXParagraph::MARGIN)
304 && cursor.par->footnotekind != LyXParagraph::FIG
305 && cursor.par->footnotekind != LyXParagraph::TAB
306 && cursor.par->footnotekind != LyXParagraph::WIDE_FIG
307 && cursor.par->footnotekind != LyXParagraph::WIDE_TAB
308 && cursor.par->footnotekind != LyXParagraph::ALGORITHM)))
313 LyXCursor tmpcursor = cursor;
314 cursor.par = tmpcursor.par->ParFromPos(tmpcursor.pos);
315 cursor.pos = tmpcursor.par->PositionInParFromPos(tmpcursor.pos);
317 LyXParagraph *par = buffer()->paragraph;
320 if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
322 && par->footnotekind != LyXParagraph::FOOTNOTE
323 && par->footnotekind != LyXParagraph::MARGIN)
325 && par->footnotekind != LyXParagraph::FIG
326 && par->footnotekind != LyXParagraph::TAB
327 && par->footnotekind != LyXParagraph::WIDE_FIG
328 && par->footnotekind != LyXParagraph::WIDE_TAB
329 && par->footnotekind != LyXParagraph::ALGORITHM
334 && par->previous->footnoteflag !=
335 LyXParagraph::CLOSED_FOOTNOTE){ /* should be */
336 text->SetCursorIntern(par->previous,
338 text->OpenFootnote();
342 if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
346 par->footnotekind != LyXParagraph::FOOTNOTE
348 par->footnotekind != LyXParagraph::MARGIN
353 par->footnotekind != LyXParagraph::FIG
355 par->footnotekind != LyXParagraph::TAB
357 par->footnotekind != LyXParagraph::WIDE_FIG
359 par->footnotekind != LyXParagraph::WIDE_TAB
361 par->footnotekind != LyXParagraph::ALGORITHM
365 text->SetCursorIntern(par, 0);
366 text->CloseFootnote();
372 text->SetCursorIntern(cursor.par, cursor.pos);
379 void BufferView::insertNote()
381 InsetInfo * new_inset = new InsetInfo();
382 insertInset(new_inset);
383 new_inset->Edit(this, 0, 0, 0);
387 void BufferView::openStuff()
390 owner()->getMiniBuffer()->Set(_("Open/Close..."));
401 void BufferView::toggleFloat()
404 owner()->getMiniBuffer()->Set(_("Open/Close..."));
408 text->ToggleFootnote();
414 void BufferView::menuUndo()
417 owner()->getMiniBuffer()->Set(_("Undo"));
421 if (!text->TextUndo())
422 owner()->getMiniBuffer()->Set(_("No further undo information"));
430 void BufferView::menuRedo()
432 if (the_locking_inset) {
433 owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
438 owner()->getMiniBuffer()->Set(_("Redo"));
442 if (!text->TextRedo())
443 owner()->getMiniBuffer()->Set(_("No further redo information"));
451 void BufferView::hyphenationPoint()
456 InsetSpecialChar * new_inset =
457 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
458 insertInset(new_inset);
463 void BufferView::ldots()
468 InsetSpecialChar * new_inset =
469 new InsetSpecialChar(InsetSpecialChar::LDOTS);
470 insertInset(new_inset);
475 void BufferView::endOfSentenceDot()
480 InsetSpecialChar * new_inset =
481 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
482 insertInset(new_inset);
487 void BufferView::menuSeparator()
492 InsetSpecialChar * new_inset =
493 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
494 insertInset(new_inset);
499 void BufferView::newline()
505 InsetSpecialChar * new_inset =
506 new InsetSpecialChar(InsetSpecialChar::NEWLINE);
507 insertInset(new_inset);
509 text->InsertChar(LyXParagraph::META_NEWLINE);
516 void BufferView::protectedBlank()
522 InsetSpecialChar * new_inset =
523 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
524 insertInset(new_inset);
526 text->InsertChar(LyXParagraph::META_PROTECTED_SEPARATOR);
533 void BufferView::hfill()
538 text->InsertChar(LyXParagraph::META_HFILL);
543 void BufferView::copyEnvironment()
546 text->copyEnvironmentType();
547 // clear the selection, even if mark_set
549 text->ClearSelection();
551 owner()->getMiniBuffer()->Set(_("Paragraph environment type copied"));
556 void BufferView::pasteEnvironment()
559 text->pasteEnvironmentType();
560 owner()->getMiniBuffer()->Set(_("Paragraph environment type set"));
566 void BufferView::copy()
569 text->CopySelection();
570 // clear the selection, even if mark_set
572 text->ClearSelection();
574 owner()->getMiniBuffer()->Set(_("Copy"));
578 void BufferView::cut()
583 text->CutSelection();
585 owner()->getMiniBuffer()->Set(_("Cut"));
590 void BufferView::paste()
592 if (!available()) return;
594 owner()->getMiniBuffer()->Set(_("Paste"));
596 // clear the selection
598 text->ClearSelection();
602 text->PasteSelection();
605 // clear the selection
607 text->ClearSelection();
612 void BufferView::gotoNote()
614 if (!available()) return;
621 if (!text->GotoNextNote()) {
623 || text->cursor.par != text->FirstParagraph()) {
625 text->cursor.par = text->FirstParagraph();
626 text->cursor.pos = 0;
627 if (!text->GotoNextNote()) {
629 owner()->getMiniBuffer()->Set(_("No more notes"));
633 owner()->getMiniBuffer()->Set(_("No more notes"));
638 text->sel_cursor = text->cursor;
642 void BufferView::insertCorrectQuote()
646 if (text->cursor.pos)
647 c = text->cursor.par->GetChar(text->cursor.pos - 1);
651 insertInset(new InsetQuotes(c, buffer()->params));
655 /* these functions are for the spellchecker */
656 char * BufferView::nextWord(float & value)
663 char * string = text->SelectNextWord(value);
669 void BufferView::selectLastWord()
671 if (!available()) return;
675 text->SelectSelectedWord();
676 toggleSelection(false);
681 void BufferView::endOfSpellCheck()
683 if (!available()) return;
687 text->SelectSelectedWord();
688 text->ClearSelection();
693 void BufferView::replaceWord(string const & replacestring)
695 if (!available()) return;
700 /* clear the selection (if there is any) */
701 toggleSelection(false);
704 /* clear the selection (if there is any) */
705 toggleSelection(false);
706 text->ReplaceSelectionWithString(replacestring.c_str());
708 text->SetSelectionOverString(replacestring.c_str());
710 // Go back so that replacement string is also spellchecked
711 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
712 text->CursorLeftIntern();
716 // End of spellchecker stuff
720 /* these functions return 1 if an error occured,
722 int BufferView::lockInset(UpdatableInset * inset)
724 if (!the_locking_inset && inset){
725 the_locking_inset = inset;
732 void BufferView::showLockedInsetCursor(long x, long y, int asc, int desc)
734 if (the_locking_inset && available()) {
736 screen->ShowManualCursor(x, y, asc, desc,
737 LyXScreen::BAR_SHAPE);
742 void BufferView::hideLockedInsetCursor()
744 if (the_locking_inset && available()) {
745 screen->HideCursor();
750 void BufferView::fitLockedInsetCursor(long x, long y, int asc, int desc)
752 if (the_locking_inset && available()){
754 if (screen->FitManualCursor(x, y, asc, desc))
760 int BufferView::unlockInset(UpdatableInset * inset)
762 if (inset && the_locking_inset == inset) {
763 inset->InsetUnlock(this);
764 the_locking_inset = 0;
768 return bufferlist.unlockInset(inset);
772 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
774 if (!the_locking_inset)
775 return; // shouldn't happen
776 if (kind == Undo::EDIT) // in this case insets would not be stored!
780 ParFromPos(text->cursor.pos)->previous,
782 ParFromPos(text->cursor.pos)->next);
786 void BufferView::updateInset(Inset * inset, bool mark_dirty)
791 // first check for locking insets
792 if (the_locking_inset) {
793 if (the_locking_inset == inset) {
794 if (text->UpdateInset(inset)){
797 if (buffer()->isLyxClean())
798 owner()->getMiniBuffer()->
800 buffer()->markDirty();
805 } else if (the_locking_inset->UpdateInsetInInset(this,inset)) {
806 if (text->UpdateInset(the_locking_inset)) {
809 if (buffer()->isLyxClean())
810 owner()->getMiniBuffer()->
812 buffer()->markDirty();
820 // then check the current buffer
824 if (text->UpdateInset(inset)){