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;
33 // Inserts a file into current document
34 bool BufferView::insertLyXFile(string const & filen)
36 // Copyright CHT Software Service GmbH
39 // Insert a Lyxformat - file into current buffer
41 // Moved from lyx_cb.C (Lgb)
43 if (filen.empty()) return false;
45 string fname = MakeAbsPath(filen);
47 // check if file exist
51 WriteAlert(_("Error!"),
52 _("Specified file is unreadable: "),
53 MakeDisplayPath(fname, 50));
59 ifstream ifs(fname.c_str());
61 WriteAlert(_("Error!"),
62 _("Cannot open specified file: "),
63 MakeDisplayPath(fname, 50));
74 lyxerr.debug() << "Will insert file with header" << endl;
75 res = buffer()->readFile(lex, text->cursor.par);
77 lyxerr.debug() << "Will insert file without header" << endl;
78 res = buffer()->readLyXformat2(lex, text->cursor.par);
85 bool BufferView::removeAutoInsets()
87 LyXParagraph * par = buffer()->paragraph;
89 LyXCursor cursor = text->cursor;
90 LyXCursor tmpcursor = cursor;
91 cursor.par = tmpcursor.par->ParFromPos(tmpcursor.pos);
92 cursor.pos = tmpcursor.par->PositionInParFromPos(tmpcursor.pos);
96 if (par->AutoDeleteInsets()){
98 if (par->footnoteflag != LyXParagraph::CLOSED_FOOTNOTE){
99 // this is possible now, since SetCursor takes
100 // care about footnotes
101 text->SetCursorIntern(par, 0);
102 text->RedoParagraphs(text->cursor,
103 text->cursor.par->Next());
109 // avoid forbidden cursor positions caused by error removing
110 if (cursor.pos > cursor.par->Last())
111 cursor.pos = cursor.par->Last();
112 text->SetCursorIntern(cursor.par, cursor.pos);
118 void BufferView::insertErrors(TeXErrors & terr)
120 // Save the cursor position
121 LyXCursor cursor = text->cursor;
123 // This is drastic, but it's the only fix, I could find. (Asger)
127 for (TeXErrors::Errors::const_iterator cit = terr.begin();
130 string desctext((*cit).error_desc);
131 string errortext((*cit).error_text);
132 string msgtxt = desctext + '\n' + errortext;
133 int errorrow = (*cit).error_in_line;
135 // Insert error string for row number
139 buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos);
141 LyXParagraph * texrowpar = 0;
144 texrowpar = text->FirstParagraph();
147 texrowpar = text->GetParFromID(tmpid);
153 InsetError * new_inset = new InsetError(msgtxt);
154 text->SetCursorIntern(texrowpar, tmppos);
155 text->InsertInset(new_inset);
158 // Restore the cursor position
159 text->SetCursorIntern(cursor.par, cursor.pos);
163 void BufferView::setCursorFromRow(int row)
168 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
170 LyXParagraph * texrowpar;
173 texrowpar = text->FirstParagraph();
176 texrowpar = text->GetParFromID(tmpid);
178 text->SetCursor(texrowpar, tmppos);
181 void BufferView::insertInset(Inset * inset, string const & lout,
184 // check for table/list in tables
185 if (no_table && text->cursor.par->table){
186 WriteAlert(_("Impossible Operation!"),
187 _("Cannot insert table/list in table."),
191 // not quite sure if we want this...
192 text->SetCursorParUndo();
198 text->BreakParagraph();
201 if (text->cursor.par->Last()) {
204 text->BreakParagraph();
208 pair<bool, LyXTextClass::size_type> lres =
209 textclasslist.NumberOfLayout(buffer()->params
211 LyXTextClass::size_type lay;
212 if (lres.first != false) {
216 // layout not fount using default "Standard" (0)
220 text->SetLayout(lay);
222 text->SetParagraph(0, 0,
224 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
230 text->current_font.setLatex(LyXFont::OFF);
233 text->InsertInset(inset);
235 // if we enter a text-inset the cursor should be to the left side
236 // of it! This couldn't happen before as Undo was not handled inside
237 // inset now after the Undo LyX tries to call inset->Edit(...) again
238 // and cannot do this as the cursor is behind the inset and GetInset
239 // does not return the inset!
240 if (inset->IsTextInset()) {
241 if (text->cursor.par->getParDirection()==LYX_DIR_LEFT_TO_RIGHT)
249 text->UnFreezeUndo();
253 // Open and lock an updatable inset
254 void BufferView::open_new_inset(UpdatableInset * new_inset)
258 insertInset(new_inset);
261 new_inset->Edit(this, 0, 0, 0);
264 /* This is also a buffer property (ale) */
265 // Not so sure about that. a goto Label function can not be buffer local, just
266 // think how this will work in a multiwindo/buffer environment, all the
267 // cursors in all the views showing this buffer will move. (Lgb)
268 // OK, then no cursor action should be allowed in buffer. (ale)
269 bool BufferView::gotoLabel(string const & label)
272 LyXParagraph * par = buffer()->paragraph;
273 LyXParagraph::size_type pos;
277 while ((inset = par->ReturnNextInsetPointer(pos))){
278 for (int i = 0; i < inset->GetNumberOfLabels(); ++i) {
279 if (label == inset->getLabel(i)) {
281 text->SetCursor(par, pos);
282 text->sel_cursor = text->cursor;
294 void BufferView::allFloats(char flag, char figmar)
296 if (!available()) return;
298 LyXCursor cursor = text->cursor;
300 if (!flag && cursor.par->footnoteflag != LyXParagraph::NO_FOOTNOTE
302 && cursor.par->footnotekind != LyXParagraph::FOOTNOTE
303 && cursor.par->footnotekind != LyXParagraph::MARGIN)
305 && cursor.par->footnotekind != LyXParagraph::FIG
306 && cursor.par->footnotekind != LyXParagraph::TAB
307 && cursor.par->footnotekind != LyXParagraph::WIDE_FIG
308 && cursor.par->footnotekind != LyXParagraph::WIDE_TAB
309 && cursor.par->footnotekind != LyXParagraph::ALGORITHM)))
314 LyXCursor tmpcursor = cursor;
315 cursor.par = tmpcursor.par->ParFromPos(tmpcursor.pos);
316 cursor.pos = tmpcursor.par->PositionInParFromPos(tmpcursor.pos);
318 LyXParagraph *par = buffer()->paragraph;
321 if (par->footnoteflag == LyXParagraph::CLOSED_FOOTNOTE
323 && par->footnotekind != LyXParagraph::FOOTNOTE
324 && par->footnotekind != LyXParagraph::MARGIN)
326 && par->footnotekind != LyXParagraph::FIG
327 && par->footnotekind != LyXParagraph::TAB
328 && par->footnotekind != LyXParagraph::WIDE_FIG
329 && par->footnotekind != LyXParagraph::WIDE_TAB
330 && par->footnotekind != LyXParagraph::ALGORITHM
335 && par->previous->footnoteflag !=
336 LyXParagraph::CLOSED_FOOTNOTE){ /* should be */
337 text->SetCursorIntern(par->previous,
339 text->OpenFootnote();
343 if (par->footnoteflag == LyXParagraph::OPEN_FOOTNOTE
347 par->footnotekind != LyXParagraph::FOOTNOTE
349 par->footnotekind != LyXParagraph::MARGIN
354 par->footnotekind != LyXParagraph::FIG
356 par->footnotekind != LyXParagraph::TAB
358 par->footnotekind != LyXParagraph::WIDE_FIG
360 par->footnotekind != LyXParagraph::WIDE_TAB
362 par->footnotekind != LyXParagraph::ALGORITHM
366 text->SetCursorIntern(par, 0);
367 text->CloseFootnote();
373 text->SetCursorIntern(cursor.par, cursor.pos);
380 void BufferView::insertNote()
382 InsetInfo * new_inset = new InsetInfo();
383 insertInset(new_inset);
384 new_inset->Edit(this, 0, 0, 0);
388 void BufferView::openStuff()
391 owner()->getMiniBuffer()->Set(_("Open/Close..."));
402 void BufferView::toggleFloat()
405 owner()->getMiniBuffer()->Set(_("Open/Close..."));
409 text->ToggleFootnote();
415 void BufferView::menuUndo()
418 owner()->getMiniBuffer()->Set(_("Undo"));
422 if (!text->TextUndo())
423 owner()->getMiniBuffer()->Set(_("No further undo information"));
431 void BufferView::menuRedo()
433 if (the_locking_inset) {
434 owner()->getMiniBuffer()->Set(_("Redo not yet supported in math mode"));
439 owner()->getMiniBuffer()->Set(_("Redo"));
443 if (!text->TextRedo())
444 owner()->getMiniBuffer()->Set(_("No further redo information"));
452 void BufferView::hyphenationPoint()
457 InsetSpecialChar * new_inset =
458 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
459 insertInset(new_inset);
464 void BufferView::ldots()
469 InsetSpecialChar * new_inset =
470 new InsetSpecialChar(InsetSpecialChar::LDOTS);
471 insertInset(new_inset);
476 void BufferView::endOfSentenceDot()
481 InsetSpecialChar * new_inset =
482 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
483 insertInset(new_inset);
488 void BufferView::menuSeparator()
493 InsetSpecialChar * new_inset =
494 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
495 insertInset(new_inset);
500 void BufferView::newline()
506 InsetSpecialChar * new_inset =
507 new InsetSpecialChar(InsetSpecialChar::NEWLINE);
508 insertInset(new_inset);
510 text->InsertChar(LyXParagraph::META_NEWLINE);
517 void BufferView::protectedBlank()
523 InsetSpecialChar * new_inset =
524 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
525 insertInset(new_inset);
527 text->InsertChar(LyXParagraph::META_PROTECTED_SEPARATOR);
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
721 /* these functions return 1 if an error occured,
723 int BufferView::lockInset(UpdatableInset * inset)
725 if (!the_locking_inset && inset){
726 the_locking_inset = inset;
733 void BufferView::showLockedInsetCursor(long x, long y, int asc, int desc)
735 if (the_locking_inset && available()) {
737 screen->ShowManualCursor(x, y, asc, desc,
738 LyXScreen::BAR_SHAPE);
743 void BufferView::hideLockedInsetCursor()
745 if (the_locking_inset && available()) {
746 screen->HideCursor();
751 void BufferView::fitLockedInsetCursor(long x, long y, int asc, int desc)
753 if (the_locking_inset && available()){
755 if (screen->FitManualCursor(x, y, asc, desc))
761 int BufferView::unlockInset(UpdatableInset * inset)
763 if (inset && the_locking_inset == inset) {
764 inset->InsetUnlock(this);
765 the_locking_inset = 0;
769 return bufferlist.unlockInset(inset);
773 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
775 if (!the_locking_inset)
776 return; // shouldn't happen
777 if (kind == Undo::EDIT) // in this case insets would not be stored!
781 ParFromPos(text->cursor.pos)->previous,
783 ParFromPos(text->cursor.pos)->next);
787 void BufferView::updateInset(Inset * inset, bool mark_dirty)
792 // first check for locking insets
793 if (the_locking_inset) {
794 if (the_locking_inset == inset) {
795 if (text->UpdateInset(inset)){
798 if (buffer()->isLyxClean())
799 owner()->getMiniBuffer()->
801 buffer()->markDirty();
806 } else if (the_locking_inset->UpdateInsetInInset(this,inset)) {
807 if (text->UpdateInset(the_locking_inset)) {
810 if (buffer()->isLyxClean())
811 owner()->getMiniBuffer()->
813 buffer()->markDirty();
821 // then check the current buffer
825 if (text->UpdateInset(inset)){