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"
17 #include "frontends/LyXView.h"
18 #include "bufferlist.h"
19 #include "frontends/screen.h"
21 #include "BufferView_pimpl.h"
24 #include "undo_funcs.h"
26 #include "iterators.h"
28 #include "WordLangTuple.h"
30 #include "frontends/Alert.h"
31 #include "frontends/Dialogs.h"
33 #include "insets/insetcommand.h" //ChangeRefs
34 #include "insets/inseterror.h"
36 #include "support/FileInfo.h"
37 #include "support/filetools.h"
38 #include "support/lyxfunctional.h" //equal_1st_in_pair
39 #include "support/types.h"
40 #include "support/lyxalgo.h" // lyx_count
44 extern BufferList bufferlist;
56 // Inserts a file into current document
57 bool BufferView::insertLyXFile(string const & filen)
59 // Copyright CHT Software Service GmbH
62 // Insert a LyXformat - file into current buffer
64 // Moved from lyx_cb.C (Lgb)
66 if (filen.empty()) return false;
68 string const fname = MakeAbsPath(filen);
70 // check if file exist
71 FileInfo const fi(fname);
74 Alert::alert(_("Error!"),
75 _("Specified file is unreadable: "),
76 MakeDisplayPath(fname, 50));
82 ifstream ifs(fname.c_str());
84 Alert::alert(_("Error!"),
85 _("Cannot open specified file: "),
86 MakeDisplayPath(fname, 50));
90 int const c = ifs.peek();
98 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
99 res = buffer()->readFile(lex, text->cursor.par());
101 lyxerr[Debug::INFO] << "Will insert file without header"
103 res = buffer()->readLyXformat2(lex, text->cursor.par());
111 bool BufferView::removeAutoInsets()
113 // keep track of which pos and par the cursor was on
114 Paragraph * cursor_par = text->cursor.par();
115 Paragraph * cursor_par_prev = cursor_par ? cursor_par->previous() : 0;
116 Paragraph * cursor_par_next = cursor_par ? cursor_par->next() : 0;
117 pos_type cursor_pos = text->cursor.pos();
121 // Trap the deletion of the paragraph the cursor is in.
122 // Iterate until we find a paragraph that won't be immediately deleted.
123 // In reality this should mean we only execute the body of the while
124 // loop once at most. However for safety we iterate rather than just
125 // make this an if () conditional.
126 while ((cursor_par_prev || cursor_par_next)
127 && text->setCursor(this,
128 cursor_par_prev ? cursor_par_prev : cursor_par_next,
130 // We just removed cursor_par so have to fix the "cursor"
131 if (cursor_par_prev) {
135 cursor_par = cursor_par_prev;
136 cursor_pos = cursor_par->size();
140 cursor_par = cursor_par_next;
143 cursor_par_prev = cursor_par->previous();
144 cursor_par_next = cursor_par->next();
147 // Iterate through the paragraphs removing autoDelete insets as we go.
148 // If the paragraph ends up empty after all the autoDelete insets are
149 // removed that paragraph will be removed by the next setCursor() call.
150 ParIterator it = buffer()->par_iterator_begin();
151 ParIterator end = buffer()->par_iterator_end();
152 for (; it != end; ++it) {
153 Paragraph * par = *it;
154 Paragraph * par_prev = par ? par->previous() : 0;
155 bool removed = false;
157 if (text->setCursor(this, par, 0)
158 && cursor_par == par_prev) {
159 // The previous setCursor line was deleted and that
160 // was the cursor_par line. This can only happen if an
161 // error box was the sole item on cursor_par.
162 // It is possible for cursor_par_prev to be stray if
163 // the line it pointed to only had a error box on it
164 // so we have to set it to a known correct value.
165 // This is often the same value it already had.
166 cursor_par_prev = par->previous();
167 if (cursor_par_prev) {
168 // '|' = par, '.' = cursor_par, 'E' = error box
169 // First step below may occur before while{}
171 // E -> .E -> |.E -> . -> |b
174 cursor_par = cursor_par_prev;
175 cursor_pos = cursor_par_prev->size();
176 cursor_par_prev = cursor_par->previous();
177 // cursor_par_next remains the same
178 } else if (cursor_par_next) {
179 // First step below may occur before while{}
181 // E -> |.E -> |. -> . -> .|a
183 cursor_par = cursor_par_next;
185 // cursor_par_prev remains unset
186 cursor_par_next = cursor_par->next();
188 // I can't find a way to trigger this
189 // so it should be unreachable code
190 // unless the buffer is corrupted.
191 lyxerr << "BufferView::removeAutoInsets() is bad\n";
195 InsetList::iterator pit = par->insetlist.begin();
196 InsetList::iterator pend = par->insetlist.end();
198 while (pit != pend) {
199 if (pit.getInset()->autoDelete()) {
201 pos_type const pos = pit.getPos();
204 // We just invalidated par's inset iterators so
205 // we get the next valid iterator position
206 pit = par->insetlist.insetIterator(pos);
207 // and ensure we have a valid end iterator.
208 pend = par->insetlist.end();
210 if (cursor_par == par) {
211 // update the saved cursor position
212 if (cursor_pos > pos)
221 text->redoParagraph(this);
225 // It is possible that the last line is empty if it was cursor_par
226 // and/or only had an error inset on it. So we set the cursor to the
227 // start of the doc to force its removal and ensure a valid saved cursor
228 if (text->setCursor(this, text->ownerParagraph(), 0)
229 && 0 == cursor_par_next) {
230 cursor_par = cursor_par_prev;
231 cursor_pos = cursor_par->size();
232 } else if (cursor_pos > cursor_par->size()) {
233 // Some C-Enter lines were removed by the setCursor call which
234 // then invalidated cursor_pos. It could still be "wrong" because
235 // the cursor may appear to have jumped but since we collapsed
236 // some C-Enter lines this should be a reasonable compromise.
237 cursor_pos = cursor_par->size();
240 // restore the original cursor in its corrected location.
241 text->setCursorIntern(this, cursor_par, cursor_pos);
247 void BufferView::insertErrors(TeXErrors & terr)
249 // Save the cursor position
250 LyXCursor cursor = text->cursor;
252 TeXErrors::Errors::const_iterator cit = terr.begin();
253 TeXErrors::Errors::const_iterator end = terr.end();
254 for (; cit != end; ++cit) {
255 string const desctext(cit->error_desc);
256 string const errortext(cit->error_text);
257 string const msgtxt = desctext + '\n' + errortext;
258 int const errorrow = cit->error_in_line;
260 // Insert error string for row number
264 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
265 buffer()->texrow.increasePos(tmpid, tmppos);
268 Paragraph * texrowpar = 0;
271 texrowpar = text->ownerParagraph();
274 texrowpar = buffer()->getParFromID(tmpid);
281 InsetError * new_inset = new InsetError(msgtxt);
282 text->setCursorIntern(this, texrowpar, tmppos);
283 text->insertInset(this, new_inset);
284 text->fullRebreak(this);
287 // Restore the cursor position
288 text->setCursorIntern(this, cursor.par(), cursor.pos());
292 void BufferView::setCursorFromRow(int row)
297 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
299 Paragraph * texrowpar;
302 texrowpar = text->ownerParagraph();
305 texrowpar = buffer()->getParFromID(tmpid);
307 text->setCursor(this, texrowpar, tmppos);
311 bool BufferView::insertInset(Inset * inset, string const & lout)
313 return pimpl_->insertInset(inset, lout);
317 /* This is also a buffer property (ale) */
318 // Not so sure about that. a goto Label function can not be buffer local, just
319 // think how this will work in a multiwindo/buffer environment, all the
320 // cursors in all the views showing this buffer will move. (Lgb)
321 // OK, then no cursor action should be allowed in buffer. (ale)
322 bool BufferView::gotoLabel(string const & label)
325 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
326 it != buffer()->inset_iterator_end(); ++it) {
327 vector<string> labels = (*it)->getLabelList();
328 if (find(labels.begin(),labels.end(),label)
331 text->setCursor(this, it.getPar(), it.getPos());
332 text->selection.cursor = text->cursor;
333 update(text, BufferView::SELECT|BufferView::FITCUR);
341 void BufferView::menuUndo()
346 owner()->message(_("Undo"));
349 update(text, BufferView::SELECT|BufferView::FITCUR);
351 owner()->message(_("No further undo information"));
353 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
358 void BufferView::menuRedo()
363 owner()->message(_("Redo"));
366 update(text, BufferView::SELECT|BufferView::FITCUR);
368 owner()->message(_("No further redo information"));
370 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
375 void BufferView::copyEnvironment()
378 text->copyEnvironmentType();
379 owner()->message(_("Paragraph environment type copied"));
384 void BufferView::pasteEnvironment()
387 text->pasteEnvironmentType(this);
388 owner()->message(_("Paragraph environment type set"));
389 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
394 void BufferView::copy()
397 getLyXText()->copySelection(this);
398 owner()->message(_("Copy"));
403 void BufferView::cut(bool realcut)
407 update(text, BufferView::SELECT|BufferView::FITCUR);
408 text->cutSelection(this, true, realcut);
409 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
410 owner()->message(_("Cut"));
415 void BufferView::paste()
420 owner()->message(_("Paste"));
423 // clear the selection
425 text->clearSelection();
426 update(text, BufferView::SELECT|BufferView::FITCUR);
429 text->pasteSelection(this);
431 text->clearSelection();
432 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
433 // why fake a selection only I think it should be a real one and not only
434 // a painted one (Jug 20020318).
436 // clear the selection
438 text->clearSelection();
439 update(text, BufferView::SELECT|BufferView::FITCUR);
444 /* these functions are for the spellchecker */
445 WordLangTuple const BufferView::nextWord(float & value)
449 return WordLangTuple();
452 return text->selectNextWordToSpellcheck(this, value);
456 void BufferView::selectLastWord()
458 if (!available()) return;
460 LyXCursor cur = text->selection.cursor;
463 text->selection.cursor = cur;
464 text->selectSelectedWord(this);
465 toggleSelection(false);
466 update(text, BufferView::SELECT|BufferView::FITCUR);
470 void BufferView::endOfSpellCheck()
472 if (!available()) return;
476 text->selectSelectedWord(this);
477 text->clearSelection();
478 update(text, BufferView::SELECT|BufferView::FITCUR);
482 void BufferView::replaceWord(string const & replacestring)
484 if (!available()) return;
486 LyXText * tt = getLyXText();
488 update(tt, BufferView::SELECT|BufferView::FITCUR);
490 /* clear the selection (if there is any) */
491 toggleSelection(false);
492 update(tt, BufferView::SELECT|BufferView::FITCUR);
494 /* clear the selection (if there is any) */
495 toggleSelection(false);
496 tt->replaceSelectionWithString(this, replacestring);
498 tt->setSelectionOverString(this, replacestring);
500 // Go back so that replacement string is also spellchecked
501 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
502 tt->cursorLeft(this);
504 update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
506 // End of spellchecker stuff
509 bool BufferView::lockInset(UpdatableInset * inset)
513 // don't relock if we're already locked
514 if (theLockingInset() == inset)
516 if (!theLockingInset()) {
517 // first check if it's the inset under the cursor we want lock
518 // should be most of the time
519 char const c = text->cursor.par()->getChar(text->cursor.pos());
520 if (c == Paragraph::META_INSET) {
521 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
523 theLockingInset(inset);
527 // Then do a deep look of the inset and lock the right one
528 int const id = inset->id();
529 ParagraphList::iterator pit = buffer()->paragraphs.begin();
530 ParagraphList::iterator pend = buffer()->paragraphs.end();
531 for (; pit != pend; ++pit) {
532 InsetList::iterator it = pit->insetlist.begin();
533 InsetList::iterator end = pit->insetlist.end();
534 for (; it != end; ++it) {
535 if (it.getInset() == inset) {
536 text->setCursorIntern(this, &*pit, it.getPos());
537 theLockingInset(inset);
540 if (it.getInset()->getInsetFromID(id)) {
541 text->setCursorIntern(this, &*pit, it.getPos());
542 it.getInset()->edit(this);
543 return theLockingInset()->lockInsetInInset(this, inset);
549 return theLockingInset()->lockInsetInInset(this, inset);
553 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
555 if (available() && theLockingInset() && !theLockingInset()->nodraw()) {
556 LyXCursor cursor = text->cursor;
557 Inset * locking_inset = theLockingInset()->getLockingInset();
559 if ((cursor.pos() - 1 >= 0) &&
560 cursor.par()->isInset(cursor.pos() - 1) &&
561 (cursor.par()->getInset(cursor.pos() - 1) ==
563 text->setCursor(this, cursor,
564 cursor.par(), cursor.pos() - 1);
565 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
566 LyXText * txt = getLyXText();
567 if (locking_inset->isTextInset() &&
568 locking_inset->lyxCode() != Inset::ERT_CODE &&
569 (txt->real_current_font.language() !=
570 buffer()->params.language
571 || txt->real_current_font.isVisibleRightToLeft()
572 != buffer()->params.language->RightToLeft()))
573 shape = (txt->real_current_font.isVisibleRightToLeft())
574 ? LyXScreen::REVERSED_L_SHAPE
575 : LyXScreen::L_SHAPE;
576 y += cursor.iy() + theLockingInset()->insetInInsetY();
577 screen().showManualCursor(text, x, y, asc, desc,
583 void BufferView::hideLockedInsetCursor()
585 if (theLockingInset() && available()) {
586 screen().hideCursor();
591 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
593 if (theLockingInset() && available()) {
594 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
595 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
604 int BufferView::unlockInset(UpdatableInset * inset)
608 if (inset && theLockingInset() == inset) {
609 inset->insetUnlock(this);
611 // make sure we update the combo !
612 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
613 // Tell the paragraph dialog that we changed paragraph
614 owner()->getDialogs().updateParagraph();
617 } else if (inset && theLockingInset() &&
618 theLockingInset()->unlockInsetInInset(this, inset)) {
619 // Tell the paragraph dialog that we changed paragraph
620 owner()->getDialogs().updateParagraph();
621 // owner inset has updated the layout combo
625 return bufferlist.unlockInset(inset);
629 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
631 if (!theLockingInset())
632 return; // shouldn't happen
633 if (kind == Undo::EDIT) // in this case insets would not be stored!
637 text->cursor.par()->next());
641 void BufferView::updateInset(Inset * inset, bool mark_dirty)
643 pimpl_->updateInset(inset, mark_dirty);
647 bool BufferView::ChangeInsets(Inset::Code code,
648 string const & from, string const & to)
650 bool need_update = false;
651 LyXCursor cursor = text->cursor;
652 LyXCursor tmpcursor = cursor;
653 cursor.par(tmpcursor.par());
654 cursor.pos(tmpcursor.pos());
656 ParIterator end = buffer()->par_iterator_end();
657 for (ParIterator it = buffer()->par_iterator_begin();
659 Paragraph * par = *it;
660 bool changed_inset = false;
661 for (InsetList::iterator it2 = par->insetlist.begin();
662 it2 != par->insetlist.end(); ++it2) {
663 if (it2.getInset()->lyxCode() == code) {
664 InsetCommand * inset = static_cast<InsetCommand *>(it2.getInset());
665 if (inset->getContents() == from) {
666 inset->setContents(to);
667 changed_inset = true;
676 // The test it.size()==1 was needed to prevent crashes.
677 // How to set the cursor corretly when it.size()>1 ??
678 if (it.size() == 1) {
679 text->setCursorIntern(this, par, 0);
680 text->redoParagraphs(this, text->cursor,
681 text->cursor.par()->next());
682 text->fullRebreak(this);
686 text->setCursorIntern(this, cursor.par(), cursor.pos());
691 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
693 // Check if the label 'from' appears more than once
694 vector<string> labels = buffer()->getLabelList();
696 if (lyx::count(labels.begin(), labels.end(), from) > 1)
699 return ChangeInsets(Inset::REF_CODE, from, to);
703 bool BufferView::ChangeCitationsIfUnique(string const & from,
706 typedef pair<string, string> StringPair;
708 vector<StringPair> keys = buffer()->getBibkeyList();
709 if (count_if(keys.begin(), keys.end(),
710 lyx::equal_1st_in_pair<StringPair>(from))
714 return ChangeInsets(Inset::CITE_CODE, from, to);
718 UpdatableInset * BufferView::theLockingInset() const
720 // If NULL is not allowed we should put an Assert here. (Lgb)
722 return text->the_locking_inset;
727 void BufferView::theLockingInset(UpdatableInset * inset)
729 text->the_locking_inset = inset;
733 LyXText * BufferView::getLyXText() const
735 if (theLockingInset()) {
736 LyXText * txt = theLockingInset()->getLyXText(this, true);
744 LyXText * BufferView::getParentText(Inset * inset) const
746 if (inset->owner()) {
747 LyXText * txt = inset->getLyXText(this);
748 inset = inset->owner();
749 while (inset && inset->getLyXText(this) == txt)
750 inset = inset->owner();
752 return inset->getLyXText(this);
758 Language const * BufferView::getParentLanguage(Inset * inset) const
760 LyXText * text = getParentText(inset);
761 return text->cursor.par()->getFontSettings(buffer()->params,
762 text->cursor.pos()).language();