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 text->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()
416 if (!available()) return;
418 owner()->message(_("Paste"));
421 // clear the selection
423 text->clearSelection();
424 update(text, BufferView::SELECT|BufferView::FITCUR);
427 text->pasteSelection(this);
428 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
430 // clear the selection
432 text->clearSelection();
433 update(text, BufferView::SELECT|BufferView::FITCUR);
437 /* these functions are for the spellchecker */
438 string const BufferView::nextWord(float & value)
445 return text->selectNextWordToSpellcheck(this, value);
449 void BufferView::selectLastWord()
451 if (!available()) return;
453 LyXCursor cur = text->selection.cursor;
456 text->selection.cursor = cur;
457 text->selectSelectedWord(this);
458 toggleSelection(false);
459 update(text, BufferView::SELECT|BufferView::FITCUR);
463 void BufferView::endOfSpellCheck()
465 if (!available()) return;
469 text->selectSelectedWord(this);
470 text->clearSelection();
471 update(text, BufferView::SELECT|BufferView::FITCUR);
475 void BufferView::replaceWord(string const & replacestring)
477 if (!available()) return;
479 LyXText * tt = getLyXText();
481 update(tt, BufferView::SELECT|BufferView::FITCUR);
483 /* clear the selection (if there is any) */
484 toggleSelection(false);
485 update(tt, BufferView::SELECT|BufferView::FITCUR);
487 /* clear the selection (if there is any) */
488 toggleSelection(false);
489 tt->replaceSelectionWithString(this, replacestring);
491 tt->setSelectionOverString(this, replacestring);
493 // Go back so that replacement string is also spellchecked
494 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
495 tt->cursorLeft(this);
497 update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
499 // End of spellchecker stuff
502 bool BufferView::lockInset(UpdatableInset * inset)
506 // don't relock if we're already locked
507 if (theLockingInset() == inset)
509 if (!theLockingInset()) {
510 // first check if it's the inset under the cursor we want lock
511 // should be most of the time
512 char const c = text->cursor.par()->getChar(text->cursor.pos());
513 if (c == Paragraph::META_INSET) {
514 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
516 theLockingInset(inset);
520 // Then do a deep look of the inset and lock the right one
521 Paragraph * par = buffer()->paragraph;
522 int const id = inset->id();
524 Paragraph::inset_iterator it =
525 par->inset_iterator_begin();
526 Paragraph::inset_iterator const end =
527 par->inset_iterator_end();
528 for (; it != end; ++it) {
529 if ((*it) == inset) {
530 text->setCursorIntern(this, par, it.getPos());
531 theLockingInset(inset);
534 if ((*it)->getInsetFromID(id)) {
535 text->setCursorIntern(this, par, it.getPos());
537 return theLockingInset()->lockInsetInInset(this, inset);
544 return theLockingInset()->lockInsetInInset(this, inset);
548 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
550 if (available() && theLockingInset() && !theLockingInset()->nodraw()) {
551 LyXCursor cursor = text->cursor;
552 Inset * locking_inset = theLockingInset()->getLockingInset();
554 if ((cursor.pos() - 1 >= 0) &&
555 cursor.par()->isInset(cursor.pos() - 1) &&
556 (cursor.par()->getInset(cursor.pos() - 1) ==
558 text->setCursor(this, cursor,
559 cursor.par(), cursor.pos() - 1);
560 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
561 LyXText * txt = getLyXText();
562 if (locking_inset->isTextInset() &&
563 locking_inset->lyxCode() != Inset::ERT_CODE &&
564 (txt->real_current_font.language() !=
565 buffer()->params.language
566 || txt->real_current_font.isVisibleRightToLeft()
567 != buffer()->params.language->RightToLeft()))
568 shape = (txt->real_current_font.isVisibleRightToLeft())
569 ? LyXScreen::REVERSED_L_SHAPE
570 : LyXScreen::L_SHAPE;
571 y += cursor.y() + theLockingInset()->insetInInsetY();
572 pimpl_->screen_->showManualCursor(text, x, y, asc, desc,
578 void BufferView::hideLockedInsetCursor()
580 if (theLockingInset() && available()) {
581 pimpl_->screen_->hideCursor();
586 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
588 if (theLockingInset() && available()) {
589 y += text->cursor.y() + theLockingInset()->insetInInsetY();
590 if (pimpl_->screen_->fitManualCursor(text, this, x, y, asc, desc))
596 int BufferView::unlockInset(UpdatableInset * inset)
600 if (inset && theLockingInset() == inset) {
601 inset->insetUnlock(this);
603 // make sure we update the combo !
604 owner()->setLayout(getLyXText()->cursor.par()->layout());
607 } else if (inset && theLockingInset() &&
608 theLockingInset()->unlockInsetInInset(this, inset)) {
609 // owner inset has updated the layout combo
613 return bufferlist.unlockInset(inset);
617 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
619 if (!theLockingInset())
620 return; // shouldn't happen
621 if (kind == Undo::EDIT) // in this case insets would not be stored!
625 text->cursor.par()->next());
629 void BufferView::updateInset(Inset * inset, bool mark_dirty)
631 pimpl_->updateInset(inset, mark_dirty);
635 bool BufferView::ChangeInsets(Inset::Code code,
636 string const & from, string const & to)
638 bool need_update = false;
639 LyXCursor cursor = text->cursor;
640 LyXCursor tmpcursor = cursor;
641 cursor.par(tmpcursor.par());
642 cursor.pos(tmpcursor.pos());
644 ParIterator end = buffer()->par_iterator_end();
645 for (ParIterator it = buffer()->par_iterator_begin();
647 Paragraph * par = *it;
648 bool changed_inset = false;
649 for (Paragraph::inset_iterator it2 = par->inset_iterator_begin();
650 it2 != par->inset_iterator_end(); ++it2) {
651 if ((*it2)->lyxCode() == code) {
652 InsetCommand * inset = static_cast<InsetCommand *>(*it2);
653 if (inset->getContents() == from) {
654 inset->setContents(to);
655 changed_inset = true;
664 // The test it.size()==1 was needed to prevent crashes.
665 // How to set the cursor corretly when it.size()>1 ??
666 if (it.size() == 1) {
667 text->setCursorIntern(this, par, 0);
668 text->redoParagraphs(this, text->cursor,
669 text->cursor.par()->next());
670 text->fullRebreak(this);
674 text->setCursorIntern(this, cursor.par(), cursor.pos());
679 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
681 // Check if the label 'from' appears more than once
682 vector<string> labels = buffer()->getLabelList();
684 if (lyx::count(labels.begin(), labels.end(), from) > 1)
687 return ChangeInsets(Inset::REF_CODE, from, to);
691 bool BufferView::ChangeCitationsIfUnique(string const & from,
694 typedef pair<string, string> StringPair;
696 vector<StringPair> keys = buffer()->getBibkeyList();
697 if (count_if(keys.begin(), keys.end(),
698 lyx::equal_1st_in_pair<StringPair>(from))
702 return ChangeInsets(Inset::CITE_CODE, from, to);
706 UpdatableInset * BufferView::theLockingInset() const
708 // If NULL is not allowed we should put an Assert here. (Lgb)
710 return text->the_locking_inset;
715 void BufferView::theLockingInset(UpdatableInset * inset)
717 text->the_locking_inset = inset;
721 LyXText * BufferView::getLyXText() const
723 if (theLockingInset()) {
724 LyXText * txt = theLockingInset()->getLyXText(this, true);
732 LyXText * BufferView::getParentText(Inset * inset) const
734 if (inset->owner()) {
735 LyXText * txt = inset->getLyXText(this);
736 inset = inset->owner();
737 while (inset && inset->getLyXText(this) == txt)
738 inset = inset->owner();
740 return inset->getLyXText(this);
746 Language const * BufferView::getParentLanguage(Inset * inset) const
748 LyXText * text = getParentText(inset);
749 return text->cursor.par()->getFontSettings(buffer()->params,
750 text->cursor.pos()).language();