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)
69 string const fname = MakeAbsPath(filen);
71 // check if file exist
72 FileInfo const fi(fname);
75 Alert::alert(_("Error!"),
76 _("Specified file is unreadable: "),
77 MakeDisplayPath(fname, 50));
83 ifstream ifs(fname.c_str());
85 Alert::alert(_("Error!"),
86 _("Cannot open specified file: "),
87 MakeDisplayPath(fname, 50));
91 int const c = ifs.peek();
99 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
100 res = buffer()->readFile(lex, text->cursor.par());
102 lyxerr[Debug::INFO] << "Will insert file without header"
104 res = buffer()->readLyXformat2(lex, text->cursor.par());
112 bool BufferView::removeAutoInsets()
114 // keep track of which pos and par the cursor was on
115 Paragraph * cursor_par = text->cursor.par();
116 Paragraph * cursor_par_prev = cursor_par ? cursor_par->previous() : 0;
117 Paragraph * cursor_par_next = cursor_par ? cursor_par->next() : 0;
118 pos_type cursor_pos = text->cursor.pos();
122 // Trap the deletion of the paragraph the cursor is in.
123 // Iterate until we find a paragraph that won't be immediately deleted.
124 // In reality this should mean we only execute the body of the while
125 // loop once at most. However for safety we iterate rather than just
126 // make this an if () conditional.
127 while ((cursor_par_prev || cursor_par_next)
128 && text->setCursor(this,
129 cursor_par_prev ? cursor_par_prev : cursor_par_next,
131 // We just removed cursor_par so have to fix the "cursor"
132 if (cursor_par_prev) {
136 cursor_par = cursor_par_prev;
137 cursor_pos = cursor_par->size();
141 cursor_par = cursor_par_next;
144 cursor_par_prev = cursor_par->previous();
145 cursor_par_next = cursor_par->next();
148 // Iterate through the paragraphs removing autoDelete insets as we go.
149 // If the paragraph ends up empty after all the autoDelete insets are
150 // removed that paragraph will be removed by the next setCursor() call.
151 ParIterator it = buffer()->par_iterator_begin();
152 ParIterator end = buffer()->par_iterator_end();
153 for (; it != end; ++it) {
154 Paragraph * par = *it;
155 Paragraph * par_prev = par ? par->previous() : 0;
156 bool removed = false;
158 if (text->setCursor(this, par, 0)
159 && cursor_par == par_prev) {
160 // The previous setCursor line was deleted and that
161 // was the cursor_par line. This can only happen if an
162 // error box was the sole item on cursor_par.
163 // It is possible for cursor_par_prev to be stray if
164 // the line it pointed to only had a error box on it
165 // so we have to set it to a known correct value.
166 // This is often the same value it already had.
167 cursor_par_prev = par->previous();
168 if (cursor_par_prev) {
169 // '|' = par, '.' = cursor_par, 'E' = error box
170 // First step below may occur before while{}
172 // E -> .E -> |.E -> . -> |b
175 cursor_par = cursor_par_prev;
176 cursor_pos = cursor_par_prev->size();
177 cursor_par_prev = cursor_par->previous();
178 // cursor_par_next remains the same
179 } else if (cursor_par_next) {
180 // First step below may occur before while{}
182 // E -> |.E -> |. -> . -> .|a
184 cursor_par = cursor_par_next;
186 // cursor_par_prev remains unset
187 cursor_par_next = cursor_par->next();
189 // I can't find a way to trigger this
190 // so it should be unreachable code
191 // unless the buffer is corrupted.
192 lyxerr << "BufferView::removeAutoInsets() is bad\n";
196 InsetList::iterator pit = par->insetlist.begin();
197 InsetList::iterator pend = par->insetlist.end();
199 while (pit != pend) {
200 if (pit.getInset()->autoDelete()) {
202 pos_type const pos = pit.getPos();
205 // We just invalidated par's inset iterators so
206 // we get the next valid iterator position
207 pit = par->insetlist.insetIterator(pos);
208 // and ensure we have a valid end iterator.
209 pend = par->insetlist.end();
211 if (cursor_par == par) {
212 // update the saved cursor position
213 if (cursor_pos > pos)
222 text->redoParagraph(this);
226 // It is possible that the last line is empty if it was cursor_par
227 // and/or only had an error inset on it. So we set the cursor to the
228 // start of the doc to force its removal and ensure a valid saved cursor
229 if (text->setCursor(this, text->ownerParagraph(), 0)
230 && 0 == cursor_par_next) {
231 cursor_par = cursor_par_prev;
232 cursor_pos = cursor_par->size();
233 } else if (cursor_pos > cursor_par->size()) {
234 // Some C-Enter lines were removed by the setCursor call which
235 // then invalidated cursor_pos. It could still be "wrong" because
236 // the cursor may appear to have jumped but since we collapsed
237 // some C-Enter lines this should be a reasonable compromise.
238 cursor_pos = cursor_par->size();
241 // restore the original cursor in its corrected location.
242 text->setCursorIntern(this, cursor_par, cursor_pos);
248 void BufferView::insertErrors(TeXErrors & terr)
250 // Save the cursor position
251 LyXCursor cursor = text->cursor;
253 TeXErrors::Errors::const_iterator cit = terr.begin();
254 TeXErrors::Errors::const_iterator end = terr.end();
255 for (; cit != end; ++cit) {
256 string const desctext(cit->error_desc);
257 string const errortext(cit->error_text);
258 string const msgtxt = desctext + '\n' + errortext;
259 int const errorrow = cit->error_in_line;
261 // Insert error string for row number
265 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
266 buffer()->texrow.increasePos(tmpid, tmppos);
269 Paragraph * texrowpar = 0;
272 texrowpar = text->ownerParagraph();
275 texrowpar = buffer()->getParFromID(tmpid);
282 InsetError * new_inset = new InsetError(msgtxt);
283 text->setCursorIntern(this, texrowpar, tmppos);
284 text->insertInset(this, new_inset);
285 text->fullRebreak(this);
288 // Restore the cursor position
289 text->setCursorIntern(this, cursor.par(), cursor.pos());
293 void BufferView::setCursorFromRow(int row)
298 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
300 Paragraph * texrowpar;
303 texrowpar = text->ownerParagraph();
306 texrowpar = buffer()->getParFromID(tmpid);
308 text->setCursor(this, texrowpar, tmppos);
312 bool BufferView::insertInset(Inset * inset, string const & lout)
314 return pimpl_->insertInset(inset, lout);
318 // This is also a buffer property (ale)
319 // Not so sure about that. a goto Label function can not be buffer local, just
320 // think how this will work in a multiwindow/buffer environment, all the
321 // cursors in all the views showing this buffer will move. (Lgb)
322 // OK, then no cursor action should be allowed in buffer. (ale)
323 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()
461 LyXCursor cur = text->selection.cursor;
464 text->selection.cursor = cur;
465 text->selectSelectedWord(this);
466 toggleSelection(false);
467 update(text, BufferView::SELECT|BufferView::FITCUR);
471 void BufferView::endOfSpellCheck()
473 if (!available()) return;
477 text->selectSelectedWord(this);
478 text->clearSelection();
479 update(text, BufferView::SELECT|BufferView::FITCUR);
483 void BufferView::replaceWord(string const & replacestring)
488 LyXText * tt = getLyXText();
490 update(tt, BufferView::SELECT|BufferView::FITCUR);
492 // clear the selection (if there is any)
493 toggleSelection(false);
494 update(tt, BufferView::SELECT|BufferView::FITCUR);
496 // clear the selection (if there is any)
497 toggleSelection(false);
498 tt->replaceSelectionWithString(this, replacestring);
500 tt->setSelectionOverString(this, replacestring);
502 // Go back so that replacement string is also spellchecked
503 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
504 tt->cursorLeft(this);
506 update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
508 // End of spellchecker stuff
511 bool BufferView::lockInset(UpdatableInset * inset)
515 // don't relock if we're already locked
516 if (theLockingInset() == inset)
518 if (!theLockingInset()) {
519 // first check if it's the inset under the cursor we want lock
520 // should be most of the time
521 char const c = text->cursor.par()->getChar(text->cursor.pos());
522 if (c == Paragraph::META_INSET) {
523 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
525 theLockingInset(inset);
529 // Then do a deep look of the inset and lock the right one
530 int const id = inset->id();
531 ParagraphList::iterator pit = buffer()->paragraphs.begin();
532 ParagraphList::iterator pend = buffer()->paragraphs.end();
533 for (; pit != pend; ++pit) {
534 InsetList::iterator it = pit->insetlist.begin();
535 InsetList::iterator end = pit->insetlist.end();
536 for (; it != end; ++it) {
537 if (it.getInset() == inset) {
538 text->setCursorIntern(this, &*pit, it.getPos());
539 theLockingInset(inset);
542 if (it.getInset()->getInsetFromID(id)) {
543 text->setCursorIntern(this, &*pit, it.getPos());
544 it.getInset()->edit(this);
545 return theLockingInset()->lockInsetInInset(this, inset);
551 return theLockingInset()->lockInsetInInset(this, inset);
555 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
557 if (available() && theLockingInset() && !theLockingInset()->nodraw()) {
558 LyXCursor cursor = text->cursor;
559 Inset * locking_inset = theLockingInset()->getLockingInset();
561 if ((cursor.pos() - 1 >= 0) &&
562 cursor.par()->isInset(cursor.pos() - 1) &&
563 (cursor.par()->getInset(cursor.pos() - 1) ==
565 text->setCursor(this, cursor,
566 cursor.par(), cursor.pos() - 1);
567 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
568 LyXText * txt = getLyXText();
569 if (locking_inset->isTextInset() &&
570 locking_inset->lyxCode() != Inset::ERT_CODE &&
571 (txt->real_current_font.language() !=
572 buffer()->params.language
573 || txt->real_current_font.isVisibleRightToLeft()
574 != buffer()->params.language->RightToLeft()))
575 shape = (txt->real_current_font.isVisibleRightToLeft())
576 ? LyXScreen::REVERSED_L_SHAPE
577 : LyXScreen::L_SHAPE;
578 y += cursor.iy() + theLockingInset()->insetInInsetY();
579 screen().showManualCursor(text, x, y, asc, desc,
585 void BufferView::hideLockedInsetCursor()
587 if (theLockingInset() && available()) {
588 screen().hideCursor();
593 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
595 if (theLockingInset() && available()) {
596 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
597 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
606 int BufferView::unlockInset(UpdatableInset * inset)
610 if (inset && theLockingInset() == inset) {
611 inset->insetUnlock(this);
613 // make sure we update the combo !
614 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
615 // Tell the paragraph dialog that we changed paragraph
616 owner()->getDialogs().updateParagraph();
619 } else if (inset && theLockingInset() &&
620 theLockingInset()->unlockInsetInInset(this, inset)) {
621 // Tell the paragraph dialog that we changed paragraph
622 owner()->getDialogs().updateParagraph();
623 // owner inset has updated the layout combo
627 return bufferlist.unlockInset(inset);
631 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
633 if (!theLockingInset())
634 return; // shouldn't happen
635 if (kind == Undo::EDIT) // in this case insets would not be stored!
639 text->cursor.par()->next());
643 void BufferView::updateInset(Inset * inset, bool mark_dirty)
645 pimpl_->updateInset(inset, mark_dirty);
649 bool BufferView::ChangeInsets(Inset::Code code,
650 string const & from, string const & to)
652 bool need_update = false;
653 LyXCursor cursor = text->cursor;
654 LyXCursor tmpcursor = cursor;
655 cursor.par(tmpcursor.par());
656 cursor.pos(tmpcursor.pos());
658 ParIterator end = buffer()->par_iterator_end();
659 for (ParIterator it = buffer()->par_iterator_begin();
661 Paragraph * par = *it;
662 bool changed_inset = false;
663 for (InsetList::iterator it2 = par->insetlist.begin();
664 it2 != par->insetlist.end(); ++it2) {
665 if (it2.getInset()->lyxCode() == code) {
666 InsetCommand * inset = static_cast<InsetCommand *>(it2.getInset());
667 if (inset->getContents() == from) {
668 inset->setContents(to);
669 changed_inset = true;
678 // The test it.size()==1 was needed to prevent crashes.
679 // How to set the cursor corretly when it.size()>1 ??
680 if (it.size() == 1) {
681 text->setCursorIntern(this, par, 0);
682 text->redoParagraphs(this, text->cursor,
683 text->cursor.par()->next());
684 text->fullRebreak(this);
688 text->setCursorIntern(this, cursor.par(), cursor.pos());
693 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
695 // Check if the label 'from' appears more than once
696 vector<string> labels = buffer()->getLabelList();
698 if (lyx::count(labels.begin(), labels.end(), from) > 1)
701 return ChangeInsets(Inset::REF_CODE, from, to);
705 bool BufferView::ChangeCitationsIfUnique(string const & from,
708 typedef pair<string, string> StringPair;
710 vector<StringPair> keys = buffer()->getBibkeyList();
711 if (count_if(keys.begin(), keys.end(),
712 lyx::equal_1st_in_pair<StringPair>(from))
716 return ChangeInsets(Inset::CITE_CODE, from, to);
720 UpdatableInset * BufferView::theLockingInset() const
722 // If NULL is not allowed we should put an Assert here. (Lgb)
724 return text->the_locking_inset;
729 void BufferView::theLockingInset(UpdatableInset * inset)
731 text->the_locking_inset = inset;
735 LyXText * BufferView::getLyXText() const
737 if (theLockingInset()) {
738 LyXText * txt = theLockingInset()->getLyXText(this, true);
746 LyXText * BufferView::getParentText(Inset * inset) const
748 if (inset->owner()) {
749 LyXText * txt = inset->getLyXText(this);
750 inset = inset->owner();
751 while (inset && inset->getLyXText(this) == txt)
752 inset = inset->owner();
754 return inset->getLyXText(this);
760 Language const * BufferView::getParentLanguage(Inset * inset) const
762 LyXText * text = getParentText(inset);
763 return text->cursor.par()->getFontSettings(buffer()->params,
764 text->cursor.pos()).language();