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);
235 text->UnFreezeUndo();
239 // Open and lock an updatable inset
240 void BufferView::open_new_inset(UpdatableInset * new_inset)
244 insertInset(new_inset);
247 new_inset->Edit(this, 0, 0, 0);
250 /* This is also a buffer property (ale) */
251 // Not so sure about that. a goto Label function can not be buffer local, just
252 // think how this will work in a multiwindo/buffer environment, all the
253 // cursors in all the views showing this buffer will move. (Lgb)
254 // OK, then no cursor action should be allowed in buffer. (ale)
255 bool BufferView::gotoLabel(string const & label)
258 LyXParagraph * par = buffer()->paragraph;
259 LyXParagraph::size_type pos;
263 while ((inset = par->ReturnNextInsetPointer(pos))){
264 for (int i = 0; i < inset->GetNumberOfLabels(); ++i) {
265 if (label == inset->getLabel(i)) {
267 text->SetCursor(par, pos);
268 text->sel_cursor = text->cursor;
280 void BufferView::allFloats(char flag, char figmar)
282 if (!available()) return;
284 LyXCursor cursor = text->cursor;
286 if (!flag && cursor.par->footnoteflag != LyXParagraph::NO_FOOTNOTE
288 && cursor.par->footnotekind != LyXParagraph::FOOTNOTE
289 && cursor.par->footnotekind != LyXParagraph::MARGIN)
291 && cursor.par->footnotekind != LyXParagraph::FIG
292 && cursor.par->footnotekind != LyXParagraph::TAB
293 && cursor.par->footnotekind != LyXParagraph::WIDE_FIG
294 && cursor.par->footnotekind != LyXParagraph::WIDE_TAB
295 && cursor.par->footnotekind != LyXParagraph::ALGORITHM)))
300 LyXCursor tmpcursor = cursor;
301 cursor.par = tmpcursor.par->ParFromPos(tmpcursor.pos);
302 cursor.pos = tmpcursor.par->PositionInParFromPos(tmpcursor.pos);
304 LyXParagraph *par = buffer()->paragraph;
307 if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
309 && par->footnotekind != LyXParagraph::FOOTNOTE
310 && par->footnotekind != LyXParagraph::MARGIN)
312 && par->footnotekind != LyXParagraph::FIG
313 && par->footnotekind != LyXParagraph::TAB
314 && par->footnotekind != LyXParagraph::WIDE_FIG
315 && par->footnotekind != LyXParagraph::WIDE_TAB
316 && par->footnotekind != LyXParagraph::ALGORITHM
321 && par->previous->footnoteflag !=
322 LyXParagraph::CLOSED_FOOTNOTE){ /* should be */
323 text->SetCursorIntern(par->previous,
325 text->OpenFootnote();
329 if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
333 par->footnotekind != LyXParagraph::FOOTNOTE
335 par->footnotekind != LyXParagraph::MARGIN
340 par->footnotekind != LyXParagraph::FIG
342 par->footnotekind != LyXParagraph::TAB
344 par->footnotekind != LyXParagraph::WIDE_FIG
346 par->footnotekind != LyXParagraph::WIDE_TAB
348 par->footnotekind != LyXParagraph::ALGORITHM
352 text->SetCursorIntern(par, 0);
353 text->CloseFootnote();
359 text->SetCursorIntern(cursor.par, cursor.pos);
366 void BufferView::insertNote()
368 InsetInfo * new_inset = new InsetInfo();
369 insertInset(new_inset);
370 new_inset->Edit(this, 0, 0, 0);
374 void BufferView::openStuff()
377 owner()->getMiniBuffer()->Set(_("Open/Close..."));
388 void BufferView::toggleFloat()
391 owner()->getMiniBuffer()->Set(_("Open/Close..."));
395 text->ToggleFootnote();
401 void BufferView::menuUndo()
404 owner()->getMiniBuffer()->Set(_("Undo"));
408 if (!text->TextUndo())
409 owner()->getMiniBuffer()->Set(_("No further undo information"));
417 void BufferView::menuRedo()
419 if (the_locking_inset) {
420 owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
425 owner()->getMiniBuffer()->Set(_("Redo"));
429 if (!text->TextRedo())
430 owner()->getMiniBuffer()->Set(_("No further redo information"));
438 void BufferView::hyphenationPoint()
443 InsetSpecialChar * new_inset =
444 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
445 insertInset(new_inset);
450 void BufferView::ldots()
455 InsetSpecialChar * new_inset =
456 new InsetSpecialChar(InsetSpecialChar::LDOTS);
457 insertInset(new_inset);
462 void BufferView::endOfSentenceDot()
467 InsetSpecialChar * new_inset =
468 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
469 insertInset(new_inset);
474 void BufferView::menuSeparator()
479 InsetSpecialChar * new_inset =
480 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
481 insertInset(new_inset);
486 void BufferView::newline()
492 InsetSpecialChar * new_inset =
493 new InsetSpecialChar(InsetSpecialChar::NEWLINE);
494 insertInset(new_inset);
496 text->InsertChar(LyXParagraph::META_NEWLINE);
503 void BufferView::protectedBlank()
509 InsetSpecialChar * new_inset =
510 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
511 insertInset(new_inset);
513 text->InsertChar(LyXParagraph::META_PROTECTED_SEPARATOR);
520 void BufferView::hfill()
525 text->InsertChar(LyXParagraph::META_HFILL);
530 void BufferView::copyEnvironment()
533 text->copyEnvironmentType();
534 // clear the selection, even if mark_set
536 text->ClearSelection();
538 owner()->getMiniBuffer()->Set(_("Paragraph environment type copied"));
543 void BufferView::pasteEnvironment()
546 text->pasteEnvironmentType();
547 owner()->getMiniBuffer()->Set(_("Paragraph environment type set"));
553 void BufferView::copy()
556 text->CopySelection();
557 // clear the selection, even if mark_set
559 text->ClearSelection();
561 owner()->getMiniBuffer()->Set(_("Copy"));
565 void BufferView::cut()
570 text->CutSelection();
572 owner()->getMiniBuffer()->Set(_("Cut"));
577 void BufferView::paste()
579 if (!available()) return;
581 owner()->getMiniBuffer()->Set(_("Paste"));
583 // clear the selection
585 text->ClearSelection();
589 text->PasteSelection();
592 // clear the selection
594 text->ClearSelection();
599 void BufferView::gotoNote()
601 if (!available()) return;
608 if (!text->GotoNextNote()) {
610 || text->cursor.par != text->FirstParagraph()) {
612 text->cursor.par = text->FirstParagraph();
613 text->cursor.pos = 0;
614 if (!text->GotoNextNote()) {
616 owner()->getMiniBuffer()->Set(_("No more notes"));
620 owner()->getMiniBuffer()->Set(_("No more notes"));
625 text->sel_cursor = text->cursor;
629 void BufferView::insertCorrectQuote()
633 if (text->cursor.pos)
634 c = text->cursor.par->GetChar(text->cursor.pos - 1);
638 insertInset(new InsetQuotes(c, buffer()->params));
642 /* these functions are for the spellchecker */
643 char * BufferView::nextWord(float & value)
650 char * string = text->SelectNextWord(value);
656 void BufferView::selectLastWord()
658 if (!available()) return;
662 text->SelectSelectedWord();
663 toggleSelection(false);
668 void BufferView::endOfSpellCheck()
670 if (!available()) return;
674 text->SelectSelectedWord();
675 text->ClearSelection();
680 void BufferView::replaceWord(string const & replacestring)
682 if (!available()) return;
687 /* clear the selection (if there is any) */
688 toggleSelection(false);
691 /* clear the selection (if there is any) */
692 toggleSelection(false);
693 text->ReplaceSelectionWithString(replacestring.c_str());
695 text->SetSelectionOverString(replacestring.c_str());
697 // Go back so that replacement string is also spellchecked
698 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
699 text->CursorLeftIntern();
703 // End of spellchecker stuff
707 /* these functions return 1 if an error occured,
709 int BufferView::lockInset(UpdatableInset * inset)
711 if (!the_locking_inset && inset){
712 the_locking_inset = inset;
719 void BufferView::showLockedInsetCursor(long x, long y, int asc, int desc)
721 if (the_locking_inset && available()) {
723 screen->ShowManualCursor(x, y, asc, desc,
724 LyXScreen::BAR_SHAPE);
729 void BufferView::hideLockedInsetCursor()
731 if (the_locking_inset && available()) {
732 screen->HideCursor();
737 void BufferView::fitLockedInsetCursor(long x, long y, int asc, int desc)
739 if (the_locking_inset && available()){
741 if (screen->FitManualCursor(x, y, asc, desc))
747 int BufferView::unlockInset(UpdatableInset * inset)
749 if (inset && the_locking_inset == inset) {
750 inset->InsetUnlock(this);
751 the_locking_inset = 0;
755 return bufferlist.unlockInset(inset);
759 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
761 if (!the_locking_inset)
762 return; // shouldn't happen
763 if (kind == Undo::EDIT) // in this case insets would not be stored!
767 ParFromPos(text->cursor.pos)->previous,
769 ParFromPos(text->cursor.pos)->next);
773 void BufferView::updateInset(Inset * inset, bool mark_dirty)
778 // first check for locking insets
779 if (the_locking_inset) {
780 if (the_locking_inset == inset) {
781 if (text->UpdateInset(inset)){
784 if (buffer()->isLyxClean())
785 owner()->getMiniBuffer()->
787 buffer()->markDirty();
792 } else if (the_locking_inset->UpdateInsetInInset(this,inset)) {
793 if (text->UpdateInset(the_locking_inset)) {
796 if (buffer()->isLyxClean())
797 owner()->getMiniBuffer()->
799 buffer()->markDirty();
807 // then check the current buffer
811 if (text->UpdateInset(inset)){