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 "bufferlist.h"
26 #include "support/FileInfo.h"
27 #include "lyxscreen.h"
28 #include "support/filetools.h"
29 #include "lyx_gui_misc.h"
31 #include "BufferView_pimpl.h"
32 #include "insets/insetcommand.h" //ChangeRefs
33 #include "support/lyxfunctional.h" //equal_1st_in_pair
37 extern BufferList bufferlist;
47 // Inserts a file into current document
48 bool BufferView::insertLyXFile(string const & filen)
50 // Copyright CHT Software Service GmbH
53 // Insert a Lyxformat - file into current buffer
55 // Moved from lyx_cb.C (Lgb)
57 if (filen.empty()) return false;
59 string const fname = MakeAbsPath(filen);
61 // check if file exist
62 FileInfo const fi(fname);
65 WriteAlert(_("Error!"),
66 _("Specified file is unreadable: "),
67 MakeDisplayPath(fname, 50));
73 ifstream ifs(fname.c_str());
75 WriteAlert(_("Error!"),
76 _("Cannot open specified file: "),
77 MakeDisplayPath(fname, 50));
81 char const c = ifs.peek();
89 lyxerr.debug() << "Will insert file with header" << endl;
90 res = buffer()->readFile(lex, text->cursor.par());
92 lyxerr.debug() << "Will insert file without header" << endl;
93 res = buffer()->readLyXformat2(lex, text->cursor.par());
101 bool BufferView::removeAutoInsets()
103 LyXParagraph * par = buffer()->paragraph;
105 LyXCursor tmpcursor = text->cursor;
111 // this has to be done before the delete
112 text->SetCursor(this, cursor, par, 0);
113 if (par->AutoDeleteInsets()){
115 text->RedoParagraphs(this, cursor,
116 cursor.par()->next());
117 text->FullRebreak(this);
122 // avoid forbidden cursor positions caused by error removing
123 if (tmpcursor.pos() > tmpcursor.par()->size())
124 tmpcursor.pos(tmpcursor.par()->size());
126 text->SetCursorIntern(this, tmpcursor.par(), tmpcursor.pos());
132 void BufferView::insertErrors(TeXErrors & terr)
134 // Save the cursor position
135 LyXCursor cursor = text->cursor;
137 for (TeXErrors::Errors::const_iterator cit = terr.begin();
140 string const desctext((*cit).error_desc);
141 string const errortext((*cit).error_text);
142 string const msgtxt = desctext + '\n' + errortext;
143 int const errorrow = (*cit).error_in_line;
145 // Insert error string for row number
149 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
150 buffer()->texrow.increasePos(tmpid, tmppos);
153 LyXParagraph * texrowpar = 0;
156 texrowpar = text->FirstParagraph();
159 texrowpar = text->GetParFromID(tmpid);
165 InsetError * new_inset = new InsetError(msgtxt);
166 text->SetCursorIntern(this, texrowpar, tmppos);
167 text->InsertInset(this, new_inset);
168 text->FullRebreak(this);
170 // Restore the cursor position
171 text->SetCursorIntern(this, cursor.par(), cursor.pos());
175 void BufferView::setCursorFromRow(int row)
180 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
182 LyXParagraph * texrowpar;
185 texrowpar = text->FirstParagraph();
188 texrowpar = text->GetParFromID(tmpid);
190 text->SetCursor(this, texrowpar, tmppos);
194 bool BufferView::insertInset(Inset * inset, string const & lout,
197 // if we are in a locking inset we should try to insert the
198 // inset there otherwise this is a illegal function now
199 if (theLockingInset()) {
200 if (theLockingInset()->InsertInsetAllowed(inset))
201 return theLockingInset()->InsertInset(this, inset);
205 // not quite sure if we want this...
206 text->SetCursorParUndo(buffer());
211 update(text, BufferView::SELECT|BufferView::FITCUR);
212 text->BreakParagraph(this);
213 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
215 if (text->cursor.par()->size()) {
216 text->CursorLeft(this);
218 text->BreakParagraph(this);
219 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
222 pair<bool, LyXTextClass::size_type> lres =
223 textclasslist.NumberOfLayout(buffer()->params
225 LyXTextClass::size_type lay;
226 if (lres.first != false) {
230 // layout not fount using default "Standard" (0)
234 text->SetLayout(this, lay);
236 text->SetParagraph(this, 0, 0,
238 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
242 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
244 text->current_font.setLatex(LyXFont::OFF);
247 text->InsertInset(this, inset);
248 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
250 text->UnFreezeUndo();
255 // Open and lock an updatable inset
256 bool BufferView::open_new_inset(UpdatableInset * new_inset, bool behind)
260 if (!insertInset(new_inset)) {
265 LyXFont & font = getLyXText()->real_current_font;
266 new_inset->Edit(this, new_inset->width(this, font), 0, 0);
268 new_inset->Edit(this, 0, 0, 0);
272 /* This is also a buffer property (ale) */
273 // Not so sure about that. a goto Label function can not be buffer local, just
274 // think how this will work in a multiwindo/buffer environment, all the
275 // cursors in all the views showing this buffer will move. (Lgb)
276 // OK, then no cursor action should be allowed in buffer. (ale)
277 bool BufferView::gotoLabel(string const & label)
280 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
281 it != buffer()->inset_iterator_end(); ++it) {
282 vector<string> labels = (*it)->getLabelList();
283 if (find(labels.begin(),labels.end(),label)
286 text->SetCursor(this, it.getPar(), it.getPos());
287 text->sel_cursor = text->cursor;
288 update(text, BufferView::SELECT|BufferView::FITCUR);
296 void BufferView::insertNote()
298 InsetInfo * new_inset = new InsetInfo();
299 insertInset(new_inset);
300 new_inset->Edit(this, 0, 0, 0);
304 void BufferView::menuUndo()
307 owner()->message(_("Undo"));
310 update(text, BufferView::SELECT|BufferView::FITCUR);
311 if (!text->TextUndo(this))
312 owner()->message(_("No forther undo information"));
314 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
320 void BufferView::menuRedo()
322 if (theLockingInset()) {
323 owner()->message(_("Redo not yet supported in math mode"));
328 owner()->message(_("Redo"));
331 update(text, BufferView::SELECT|BufferView::FITCUR);
332 if (!text->TextRedo(this))
333 owner()->message(_("No further redo information"));
335 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
341 void BufferView::hyphenationPoint()
345 update(text, BufferView::SELECT|BufferView::FITCUR);
346 InsetSpecialChar * new_inset =
347 new InsetSpecialChar(InsetSpecialChar::HYPHENATION);
348 insertInset(new_inset);
353 void BufferView::ldots()
357 update(text, BufferView::SELECT|BufferView::FITCUR);
358 InsetSpecialChar * new_inset =
359 new InsetSpecialChar(InsetSpecialChar::LDOTS);
360 insertInset(new_inset);
365 void BufferView::endOfSentenceDot()
369 update(text, BufferView::SELECT|BufferView::FITCUR);
370 InsetSpecialChar * new_inset =
371 new InsetSpecialChar(InsetSpecialChar::END_OF_SENTENCE);
372 insertInset(new_inset);
377 void BufferView::menuSeparator()
381 update(text, BufferView::SELECT|BufferView::FITCUR);
382 InsetSpecialChar * new_inset =
383 new InsetSpecialChar(InsetSpecialChar::MENU_SEPARATOR);
384 insertInset(new_inset);
389 void BufferView::newline()
393 update(text, BufferView::SELECT|BufferView::FITCUR);
394 text->InsertChar(this, LyXParagraph::META_NEWLINE);
395 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
400 void BufferView::protectedBlank(LyXText * lt)
404 update(lt, BufferView::SELECT|BufferView::FITCUR);
405 InsetSpecialChar * new_inset =
406 new InsetSpecialChar(InsetSpecialChar::PROTECTED_SEPARATOR);
407 if (!insertInset(new_inset))
410 updateInset(new_inset, true);
415 void BufferView::hfill()
419 update(text, BufferView::SELECT|BufferView::FITCUR);
420 text->InsertChar(this, LyXParagraph::META_HFILL);
421 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
426 void BufferView::copyEnvironment()
429 text->copyEnvironmentType();
430 // clear the selection, even if mark_set
432 text->ClearSelection(this);
433 update(text, BufferView::SELECT|BufferView::FITCUR);
434 owner()->message(_("Paragraph environment type copied"));
439 void BufferView::pasteEnvironment()
442 text->pasteEnvironmentType(this);
443 owner()->message(_("Paragraph environment type set"));
444 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
449 void BufferView::copy()
452 text->CopySelection(this);
453 // clear the selection, even if mark_set
455 text->ClearSelection(this);
456 update(text, BufferView::SELECT|BufferView::FITCUR);
457 owner()->message(_("Copy"));
461 void BufferView::cut()
465 update(text, BufferView::SELECT|BufferView::FITCUR);
466 text->CutSelection(this);
467 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
468 owner()->message(_("Cut"));
473 void BufferView::paste()
475 if (!available()) return;
477 owner()->message(_("Paste"));
480 // clear the selection
482 text->ClearSelection(this);
483 update(text, BufferView::SELECT|BufferView::FITCUR);
486 text->PasteSelection(this);
487 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
489 // clear the selection
491 text->ClearSelection(this);
492 update(text, BufferView::SELECT|BufferView::FITCUR);
496 void BufferView::gotoInset(std::vector<Inset::Code> const & codes,
499 if (!available()) return;
503 update(text, BufferView::SELECT|BufferView::FITCUR);
507 text->cursor.par()->GetChar(text->cursor.pos()) == LyXParagraph::META_INSET) {
508 Inset const * inset = text->cursor.par()->GetInset(text->cursor.pos());
509 if (find(codes.begin(), codes.end(), inset->LyxCode())
512 static_cast<InsetCommand const *>(inset)->getContents();
515 if (!text->GotoNextInset(this, codes, contents)) {
516 if (text->cursor.pos()
517 || text->cursor.par() != text->FirstParagraph()) {
518 LyXCursor tmp = text->cursor;
519 text->cursor.par(text->FirstParagraph());
521 if (!text->GotoNextInset(this, codes, contents)) {
523 owner()->message(_("No more insets"));
526 owner()->message(_("No more insets"));
529 update(text, BufferView::SELECT|BufferView::FITCUR);
530 text->sel_cursor = text->cursor;
534 void BufferView::gotoInset(Inset::Code code, bool same_content)
536 gotoInset(vector<Inset::Code>(1, code), same_content);
540 void BufferView::insertCorrectQuote()
544 if (text->cursor.pos())
545 c = text->cursor.par()->GetChar(text->cursor.pos() - 1);
549 insertInset(new InsetQuotes(c, buffer()->params));
553 /* these functions are for the spellchecker */
554 string const BufferView::nextWord(float & value)
561 return text->SelectNextWord(this, value);
565 void BufferView::selectLastWord()
567 if (!available()) return;
571 text->SelectSelectedWord(this);
572 toggleSelection(false);
573 update(text, BufferView::SELECT|BufferView::FITCUR);
577 void BufferView::endOfSpellCheck()
579 if (!available()) return;
583 text->SelectSelectedWord(this);
584 text->ClearSelection(this);
585 update(text, BufferView::SELECT|BufferView::FITCUR);
589 void BufferView::replaceWord(string const & replacestring)
591 if (!available()) return;
594 update(text, BufferView::SELECT|BufferView::FITCUR);
596 /* clear the selection (if there is any) */
597 toggleSelection(false);
598 update(text, BufferView::SELECT|BufferView::FITCUR);
600 /* clear the selection (if there is any) */
601 toggleSelection(false);
602 text->ReplaceSelectionWithString(this, replacestring);
604 text->SetSelectionOverString(this, replacestring);
606 // Go back so that replacement string is also spellchecked
607 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
608 text->CursorLeft(this);
610 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
612 // End of spellchecker stuff
615 bool BufferView::lockInset(UpdatableInset * inset)
617 if (!theLockingInset() && inset) {
618 theLockingInset(inset);
621 return theLockingInset()->LockInsetInInset(this, inset);
627 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
629 if (theLockingInset() && available()) {
630 LyXCursor cursor = text->cursor;
631 if ((cursor.pos() - 1 >= 0) &&
632 (cursor.par()->GetChar(cursor.pos() - 1) ==
633 LyXParagraph::META_INSET) &&
634 (cursor.par()->GetInset(cursor.pos() - 1) ==
635 theLockingInset()->GetLockingInset()))
636 text->SetCursor(this, cursor,
637 cursor.par(), cursor.pos() - 1);
638 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
639 LyXText * txt = getLyXText();
640 if (theLockingInset()->GetLockingInset()->LyxCode() ==
642 (txt->real_current_font.language() !=
643 buffer()->params.language
644 || txt->real_current_font.isVisibleRightToLeft()
645 != buffer()->params.language->RightToLeft()))
646 shape = (txt->real_current_font.isVisibleRightToLeft())
647 ? LyXScreen::REVERSED_L_SHAPE
648 : LyXScreen::L_SHAPE;
649 y += cursor.y() + theLockingInset()->InsetInInsetY();
650 pimpl_->screen_->ShowManualCursor(text, x, y, asc, desc,
656 void BufferView::hideLockedInsetCursor()
658 if (theLockingInset() && available()) {
659 pimpl_->screen_->HideCursor();
664 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
666 if (theLockingInset() && available()) {
667 y += text->cursor.y() + theLockingInset()->InsetInInsetY();
668 if (pimpl_->screen_->FitManualCursor(text, this, x, y, asc, desc))
674 int BufferView::unlockInset(UpdatableInset * inset)
676 if (inset && theLockingInset() == inset) {
677 inset->InsetUnlock(this);
681 } else if (inset && theLockingInset() &&
682 theLockingInset()->UnlockInsetInInset(this, inset)) {
686 return bufferlist.unlockInset(inset);
690 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
692 if (!theLockingInset())
693 return; // shouldn't happen
694 if (kind == Undo::EDIT) // in this case insets would not be stored!
696 text->SetUndo(buffer(), kind,
697 text->cursor.par()->previous(),
698 text->cursor.par()->next());
702 void BufferView::updateInset(Inset * inset, bool mark_dirty)
707 // first check for locking insets
708 if (theLockingInset()) {
709 if (theLockingInset() == inset) {
710 if (text->UpdateInset(this, inset)) {
713 buffer()->markDirty();
718 } else if (theLockingInset()->UpdateInsetInInset(this,inset)) {
719 if (text->UpdateInset(this, theLockingInset())) {
722 buffer()->markDirty();
730 // then check the current buffer
733 update(text, BufferView::UPDATE);
734 if (text->UpdateInset(this, inset)) {
736 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
738 update(text, SELECT);
745 bool BufferView::ChangeInsets(Inset::Code code,
746 string const & from, string const & to)
749 LyXParagraph * par = buffer()->paragraph;
750 LyXCursor cursor = text->cursor;
751 LyXCursor tmpcursor = cursor;
752 cursor.par(tmpcursor.par());
753 cursor.pos(tmpcursor.pos());
757 for (LyXParagraph::inset_iterator it = par->inset_iterator_begin();
758 it != par->inset_iterator_end(); ++it) {
759 if ((*it)->LyxCode() == code) {
760 InsetCommand * inset = static_cast<InsetCommand *>(*it);
761 if (inset->getContents() == from) {
762 inset->setContents(to);
769 // this is possible now, since SetCursor takes
770 // care about footnotes
771 text->SetCursorIntern(this, par, 0);
772 text->RedoParagraphs(this, text->cursor,
773 text->cursor.par()->next());
774 text->FullRebreak(this);
778 text->SetCursorIntern(this, cursor.par(), cursor.pos());
783 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
785 // Check if the label 'from' appears more than once
786 vector<string> labels = buffer()->getLabelList();
787 if (count(labels.begin(), labels.end(), from) > 1)
790 return ChangeInsets(Inset::REF_CODE, from, to);
794 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
797 vector<pair<string,string> > keys = buffer()->getBibkeyList();
798 if (count_if(keys.begin(), keys.end(),
799 lyx::equal_1st_in_pair<string,string>(from))
803 return ChangeInsets(Inset::CITE_CODE, from, to);
806 UpdatableInset * BufferView::theLockingInset() const
808 // If NULL is not allowed we should put an Assert here. (Lgb)
810 return text->the_locking_inset;
815 void BufferView::theLockingInset(UpdatableInset * inset)
817 text->the_locking_inset = inset;
821 LyXText * BufferView::getLyXText() const
823 if (theLockingInset()) {
824 LyXText * txt = theLockingInset()->getLyXText(this, true);
832 LyXText * BufferView::getParentText(Inset * inset) const
834 if (inset->owner()) {
835 LyXText * txt = inset->getLyXText(this);
836 inset = inset->owner();
837 while (inset && inset->getLyXText(this) == txt)
838 inset = inset->owner();
840 return inset->getLyXText(this);
846 Language const * BufferView::getParentLanguage(Inset * inset) const
848 LyXText * text = getParentText(inset);
849 return text->cursor.par()->GetFontSettings(buffer()->params,
850 text->cursor.pos()).language();