1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2001 The LyX Team.
9 * ====================================================== */
13 #include "BufferView.h"
15 #include "lyxcursor.h"
18 #include "bufferlist.h"
19 #include "lyxscreen.h"
21 #include "BufferView_pimpl.h"
24 #include "undo_funcs.h"
26 #include "iterators.h"
28 #include "frontends/Alert.h"
30 #include "insets/insetcommand.h" //ChangeRefs
31 #include "insets/inseterror.h"
33 #include "support/FileInfo.h"
34 #include "support/filetools.h"
35 #include "support/lyxfunctional.h" //equal_1st_in_pair
36 #include "support/types.h"
37 #include "support/lyxalgo.h" // lyx_count
41 extern BufferList bufferlist;
53 // Inserts a file into current document
54 bool BufferView::insertLyXFile(string const & filen)
56 // Copyright CHT Software Service GmbH
59 // Insert a Lyxformat - file into current buffer
61 // Moved from lyx_cb.C (Lgb)
63 if (filen.empty()) return false;
65 string const fname = MakeAbsPath(filen);
67 // check if file exist
68 FileInfo const fi(fname);
71 Alert::alert(_("Error!"),
72 _("Specified file is unreadable: "),
73 MakeDisplayPath(fname, 50));
79 ifstream ifs(fname.c_str());
81 Alert::alert(_("Error!"),
82 _("Cannot open specified file: "),
83 MakeDisplayPath(fname, 50));
87 int const c = ifs.peek();
95 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
96 res = buffer()->readFile(lex, text->cursor.par());
98 lyxerr[Debug::INFO] << "Will insert file without header"
100 res = buffer()->readLyXformat2(lex, text->cursor.par());
108 bool BufferView::removeAutoInsets()
110 // keep track of which pos and par the cursor was on
111 Paragraph * cursor_par = text->cursor.par();
112 Paragraph * cursor_par_prev = cursor_par ? cursor_par->previous() : 0;
113 Paragraph * cursor_par_next = cursor_par ? cursor_par->next() : 0;
114 pos_type cursor_pos = text->cursor.pos();
118 // Trap the deletion of the paragraph the cursor is in.
119 // Iterate until we find a paragraph that won't be immediately deleted.
120 // In reality this should mean we only execute the body of the while
121 // loop once at most. However for safety we iterate rather than just
122 // make this an if () conditional.
123 while ((cursor_par_prev || cursor_par_next)
124 && text->setCursor(this,
125 cursor_par_prev ? cursor_par_prev : cursor_par_next,
127 // We just removed cursor_par so have to fix the "cursor"
128 if (cursor_par_prev) {
132 cursor_par = cursor_par_prev;
133 cursor_pos = cursor_par->size();
137 cursor_par = cursor_par_next;
140 cursor_par_prev = cursor_par->previous();
141 cursor_par_next = cursor_par->next();
144 // Iterate through the paragraphs removing autoDelete insets as we go.
145 // If the paragraph ends up empty after all the autoDelete insets are
146 // removed that paragraph will be removed by the next setCursor() call.
147 ParIterator it = buffer()->par_iterator_begin();
148 ParIterator end = buffer()->par_iterator_end();
149 for (; it != end; ++it) {
150 Paragraph * par = *it;
151 Paragraph * par_prev = par ? par->previous() : 0;
152 bool removed = false;
154 if (text->setCursor(this, par, 0)
155 && cursor_par == par_prev) {
156 // The previous setCursor line was deleted and that
157 // was the cursor_par line. This can only happen if an
158 // error box was the sole item on cursor_par.
159 // It is possible for cursor_par_prev to be stray if
160 // the line it pointed to only had a error box on it
161 // so we have to set it to a known correct value.
162 // This is often the same value it already had.
163 cursor_par_prev = par->previous();
164 if (cursor_par_prev) {
165 // '|' = par, '.' = cursor_par, 'E' = error box
166 // First step below may occur before while{}
168 // E -> .E -> |.E -> . -> |b
171 cursor_par = cursor_par_prev;
172 cursor_pos = cursor_par_prev->size();
173 cursor_par_prev = cursor_par->previous();
174 // cursor_par_next remains the same
175 } else if (cursor_par_next) {
176 // First step below may occur before while{}
178 // E -> |.E -> |. -> . -> .|a
180 cursor_par = cursor_par_next;
182 // cursor_par_prev remains unset
183 cursor_par_next = cursor_par->next();
185 // I can't find a way to trigger this
186 // so it should be unreachable code
187 // unless the buffer is corrupted.
188 lyxerr << "BufferView::removeAutoInsets() is bad\n";
192 Paragraph::inset_iterator pit = par->inset_iterator_begin();
193 Paragraph::inset_iterator pend = par->inset_iterator_end();
194 while (pit != pend) {
195 if (pit->autoDelete()) {
197 pos_type const pos = pit.getPos();
200 // We just invalidated par's inset iterators so
201 // we get the next valid iterator position
202 pit = par->InsetIterator(pos);
203 // and ensure we have a valid end iterator.
204 pend = par->inset_iterator_end();
206 if (cursor_par == par) {
207 // update the saved cursor position
208 if (cursor_pos > pos)
217 text->redoParagraph(this);
221 // It is possible that the last line is empty if it was cursor_par
222 // and/or only had an error inset on it. So we set the cursor to the
223 // start of the doc to force its removal and ensure a valid saved cursor
224 if (text->setCursor(this, text->ownerParagraph(), 0)
225 && 0 == cursor_par_next) {
226 cursor_par = cursor_par_prev;
227 cursor_pos = cursor_par->size();
228 } else if (cursor_pos > cursor_par->size()) {
229 // Some C-Enter lines were removed by the setCursor call which
230 // then invalidated cursor_pos. It could still be "wrong" because
231 // the cursor may appear to have jumped but since we collapsed
232 // some C-Enter lines this should be a reasonable compromise.
233 cursor_pos = cursor_par->size();
236 // restore the original cursor in its corrected location.
237 text->setCursorIntern(this, cursor_par, cursor_pos);
243 void BufferView::insertErrors(TeXErrors & terr)
245 // Save the cursor position
246 LyXCursor cursor = text->cursor;
248 TeXErrors::Errors::const_iterator cit = terr.begin();
249 TeXErrors::Errors::const_iterator end = terr.end();
250 for (; cit != end; ++cit) {
251 string const desctext(cit->error_desc);
252 string const errortext(cit->error_text);
253 string const msgtxt = desctext + '\n' + errortext;
254 int const errorrow = cit->error_in_line;
256 // Insert error string for row number
260 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
261 buffer()->texrow.increasePos(tmpid, tmppos);
264 Paragraph * texrowpar = 0;
267 texrowpar = text->ownerParagraph();
270 texrowpar = buffer()->getParFromID(tmpid);
276 InsetError * new_inset = new InsetError(msgtxt);
277 text->setCursorIntern(this, texrowpar, tmppos);
278 text->insertInset(this, new_inset);
279 text->fullRebreak(this);
281 // Restore the cursor position
282 text->setCursorIntern(this, cursor.par(), cursor.pos());
286 void BufferView::setCursorFromRow(int row)
291 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
293 Paragraph * texrowpar;
296 texrowpar = text->ownerParagraph();
299 texrowpar = buffer()->getParFromID(tmpid);
301 text->setCursor(this, texrowpar, tmppos);
305 bool BufferView::insertInset(Inset * inset, string const & lout)
307 return pimpl_->insertInset(inset, lout);
311 /* This is also a buffer property (ale) */
312 // Not so sure about that. a goto Label function can not be buffer local, just
313 // think how this will work in a multiwindo/buffer environment, all the
314 // cursors in all the views showing this buffer will move. (Lgb)
315 // OK, then no cursor action should be allowed in buffer. (ale)
316 bool BufferView::gotoLabel(string const & label)
319 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
320 it != buffer()->inset_iterator_end(); ++it) {
321 vector<string> labels = (*it)->getLabelList();
322 if (find(labels.begin(),labels.end(),label)
325 text->setCursor(this, it.getPar(), it.getPos());
326 text->selection.cursor = text->cursor;
327 update(text, BufferView::SELECT|BufferView::FITCUR);
335 void BufferView::menuUndo()
338 owner()->message(_("Undo"));
341 update(text, BufferView::SELECT|BufferView::FITCUR);
343 owner()->message(_("No further undo information"));
345 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
351 void BufferView::menuRedo()
353 #if 0 // this should not be here (Jug 20011206)
354 if (theLockingInset()) {
355 owner()->message(_("Redo not yet supported in math mode"));
361 owner()->message(_("Redo"));
364 update(text, BufferView::SELECT|BufferView::FITCUR);
366 owner()->message(_("No further redo information"));
368 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
374 void BufferView::copyEnvironment()
377 text->copyEnvironmentType();
378 owner()->message(_("Paragraph environment type copied"));
383 void BufferView::pasteEnvironment()
386 text->pasteEnvironmentType(this);
387 owner()->message(_("Paragraph environment type set"));
388 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
393 void BufferView::copy()
396 getLyXText()->copySelection(this);
397 owner()->message(_("Copy"));
402 void BufferView::cut(bool realcut)
406 update(text, BufferView::SELECT|BufferView::FITCUR);
407 text->cutSelection(this, true, realcut);
408 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
409 owner()->message(_("Cut"));
414 void BufferView::paste()
419 owner()->message(_("Paste"));
422 // clear the selection
424 text->clearSelection();
425 update(text, BufferView::SELECT|BufferView::FITCUR);
428 text->pasteSelection(this);
429 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
430 // why fake a selection only I think it should be a real one and not only
431 // a painted one (Jug 20020318).
433 // clear the selection
435 text->clearSelection();
436 update(text, BufferView::SELECT|BufferView::FITCUR);
441 /* these functions are for the spellchecker */
442 string const BufferView::nextWord(float & value)
449 return text->selectNextWordToSpellcheck(this, value);
453 void BufferView::selectLastWord()
455 if (!available()) return;
457 LyXCursor cur = text->selection.cursor;
460 text->selection.cursor = cur;
461 text->selectSelectedWord(this);
462 toggleSelection(false);
463 update(text, BufferView::SELECT|BufferView::FITCUR);
467 void BufferView::endOfSpellCheck()
469 if (!available()) return;
473 text->selectSelectedWord(this);
474 text->clearSelection();
475 update(text, BufferView::SELECT|BufferView::FITCUR);
479 void BufferView::replaceWord(string const & replacestring)
481 if (!available()) return;
483 LyXText * tt = getLyXText();
485 update(tt, BufferView::SELECT|BufferView::FITCUR);
487 /* clear the selection (if there is any) */
488 toggleSelection(false);
489 update(tt, BufferView::SELECT|BufferView::FITCUR);
491 /* clear the selection (if there is any) */
492 toggleSelection(false);
493 tt->replaceSelectionWithString(this, replacestring);
495 tt->setSelectionOverString(this, replacestring);
497 // Go back so that replacement string is also spellchecked
498 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
499 tt->cursorLeft(this);
501 update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
503 // End of spellchecker stuff
506 bool BufferView::lockInset(UpdatableInset * inset)
510 // don't relock if we're already locked
511 if (theLockingInset() == inset)
513 if (!theLockingInset()) {
514 // first check if it's the inset under the cursor we want lock
515 // should be most of the time
516 char const c = text->cursor.par()->getChar(text->cursor.pos());
517 if (c == Paragraph::META_INSET) {
518 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
520 theLockingInset(inset);
524 // Then do a deep look of the inset and lock the right one
525 Paragraph * par = buffer()->paragraph;
526 int const id = inset->id();
528 Paragraph::inset_iterator it =
529 par->inset_iterator_begin();
530 Paragraph::inset_iterator const end =
531 par->inset_iterator_end();
532 for (; it != end; ++it) {
533 if ((*it) == inset) {
534 text->setCursorIntern(this, par, it.getPos());
535 theLockingInset(inset);
538 if ((*it)->getInsetFromID(id)) {
539 text->setCursorIntern(this, par, it.getPos());
541 return theLockingInset()->lockInsetInInset(this, inset);
548 return theLockingInset()->lockInsetInInset(this, inset);
552 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
554 if (available() && theLockingInset() && !theLockingInset()->nodraw()) {
555 LyXCursor cursor = text->cursor;
556 Inset * locking_inset = theLockingInset()->getLockingInset();
558 if ((cursor.pos() - 1 >= 0) &&
559 cursor.par()->isInset(cursor.pos() - 1) &&
560 (cursor.par()->getInset(cursor.pos() - 1) ==
562 text->setCursor(this, cursor,
563 cursor.par(), cursor.pos() - 1);
564 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
565 LyXText * txt = getLyXText();
566 if (locking_inset->isTextInset() &&
567 locking_inset->lyxCode() != Inset::ERT_CODE &&
568 (txt->real_current_font.language() !=
569 buffer()->params.language
570 || txt->real_current_font.isVisibleRightToLeft()
571 != buffer()->params.language->RightToLeft()))
572 shape = (txt->real_current_font.isVisibleRightToLeft())
573 ? LyXScreen::REVERSED_L_SHAPE
574 : LyXScreen::L_SHAPE;
575 y += cursor.y() + theLockingInset()->insetInInsetY();
576 pimpl_->screen_->showManualCursor(text, x, y, asc, desc,
582 void BufferView::hideLockedInsetCursor()
584 if (theLockingInset() && available()) {
585 pimpl_->screen_->hideCursor();
590 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
592 if (theLockingInset() && available()) {
593 y += text->cursor.y() + theLockingInset()->insetInInsetY();
594 if (pimpl_->screen_->fitManualCursor(text, this, x, y, asc, desc))
600 int BufferView::unlockInset(UpdatableInset * inset)
604 if (inset && theLockingInset() == inset) {
605 inset->insetUnlock(this);
607 // make sure we update the combo !
608 owner()->setLayout(getLyXText()->cursor.par()->layout());
611 } else if (inset && theLockingInset() &&
612 theLockingInset()->unlockInsetInInset(this, inset)) {
613 // owner inset has updated the layout combo
617 return bufferlist.unlockInset(inset);
621 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
623 if (!theLockingInset())
624 return; // shouldn't happen
625 if (kind == Undo::EDIT) // in this case insets would not be stored!
629 text->cursor.par()->next());
633 void BufferView::updateInset(Inset * inset, bool mark_dirty)
635 pimpl_->updateInset(inset, mark_dirty);
639 bool BufferView::ChangeInsets(Inset::Code code,
640 string const & from, string const & to)
642 bool need_update = false;
643 LyXCursor cursor = text->cursor;
644 LyXCursor tmpcursor = cursor;
645 cursor.par(tmpcursor.par());
646 cursor.pos(tmpcursor.pos());
648 ParIterator end = buffer()->par_iterator_end();
649 for (ParIterator it = buffer()->par_iterator_begin();
651 Paragraph * par = *it;
652 bool changed_inset = false;
653 for (Paragraph::inset_iterator it2 = par->inset_iterator_begin();
654 it2 != par->inset_iterator_end(); ++it2) {
655 if ((*it2)->lyxCode() == code) {
656 InsetCommand * inset = static_cast<InsetCommand *>(*it2);
657 if (inset->getContents() == from) {
658 inset->setContents(to);
659 changed_inset = true;
668 // The test it.size()==1 was needed to prevent crashes.
669 // How to set the cursor corretly when it.size()>1 ??
670 if (it.size() == 1) {
671 text->setCursorIntern(this, par, 0);
672 text->redoParagraphs(this, text->cursor,
673 text->cursor.par()->next());
674 text->fullRebreak(this);
678 text->setCursorIntern(this, cursor.par(), cursor.pos());
683 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
685 // Check if the label 'from' appears more than once
686 vector<string> labels = buffer()->getLabelList();
688 if (lyx::count(labels.begin(), labels.end(), from) > 1)
691 return ChangeInsets(Inset::REF_CODE, from, to);
695 bool BufferView::ChangeCitationsIfUnique(string const & from,
698 typedef pair<string, string> StringPair;
700 vector<StringPair> keys = buffer()->getBibkeyList();
701 if (count_if(keys.begin(), keys.end(),
702 lyx::equal_1st_in_pair<StringPair>(from))
706 return ChangeInsets(Inset::CITE_CODE, from, to);
710 UpdatableInset * BufferView::theLockingInset() const
712 // If NULL is not allowed we should put an Assert here. (Lgb)
714 return text->the_locking_inset;
719 void BufferView::theLockingInset(UpdatableInset * inset)
721 text->the_locking_inset = inset;
725 LyXText * BufferView::getLyXText() const
727 if (theLockingInset()) {
728 LyXText * txt = theLockingInset()->getLyXText(this, true);
736 LyXText * BufferView::getParentText(Inset * inset) const
738 if (inset->owner()) {
739 LyXText * txt = inset->getLyXText(this);
740 inset = inset->owner();
741 while (inset && inset->getLyXText(this) == txt)
742 inset = inset->owner();
744 return inset->getLyXText(this);
750 Language const * BufferView::getParentLanguage(Inset * inset) const
752 LyXText * text = getParentText(inset);
753 return text->cursor.par()->getFontSettings(buffer()->params,
754 text->cursor.pos()).language();