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"
29 #include "frontends/Dialogs.h"
31 #include "insets/insetcommand.h" //ChangeRefs
32 #include "insets/inseterror.h"
34 #include "support/FileInfo.h"
35 #include "support/filetools.h"
36 #include "support/lyxfunctional.h" //equal_1st_in_pair
37 #include "support/types.h"
38 #include "support/lyxalgo.h" // lyx_count
42 extern BufferList bufferlist;
54 // Inserts a file into current document
55 bool BufferView::insertLyXFile(string const & filen)
57 // Copyright CHT Software Service GmbH
60 // Insert a Lyxformat - file into current buffer
62 // Moved from lyx_cb.C (Lgb)
64 if (filen.empty()) return false;
66 string const fname = MakeAbsPath(filen);
68 // check if file exist
69 FileInfo const fi(fname);
72 Alert::alert(_("Error!"),
73 _("Specified file is unreadable: "),
74 MakeDisplayPath(fname, 50));
80 ifstream ifs(fname.c_str());
82 Alert::alert(_("Error!"),
83 _("Cannot open specified file: "),
84 MakeDisplayPath(fname, 50));
88 int const c = ifs.peek();
96 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
97 res = buffer()->readFile(lex, text->cursor.par());
99 lyxerr[Debug::INFO] << "Will insert file without header"
101 res = buffer()->readLyXformat2(lex, text->cursor.par());
109 bool BufferView::removeAutoInsets()
111 // keep track of which pos and par the cursor was on
112 Paragraph * cursor_par = text->cursor.par();
113 Paragraph * cursor_par_prev = cursor_par ? cursor_par->previous() : 0;
114 Paragraph * cursor_par_next = cursor_par ? cursor_par->next() : 0;
115 pos_type cursor_pos = text->cursor.pos();
119 // Trap the deletion of the paragraph the cursor is in.
120 // Iterate until we find a paragraph that won't be immediately deleted.
121 // In reality this should mean we only execute the body of the while
122 // loop once at most. However for safety we iterate rather than just
123 // make this an if () conditional.
124 while ((cursor_par_prev || cursor_par_next)
125 && text->setCursor(this,
126 cursor_par_prev ? cursor_par_prev : cursor_par_next,
128 // We just removed cursor_par so have to fix the "cursor"
129 if (cursor_par_prev) {
133 cursor_par = cursor_par_prev;
134 cursor_pos = cursor_par->size();
138 cursor_par = cursor_par_next;
141 cursor_par_prev = cursor_par->previous();
142 cursor_par_next = cursor_par->next();
145 // Iterate through the paragraphs removing autoDelete insets as we go.
146 // If the paragraph ends up empty after all the autoDelete insets are
147 // removed that paragraph will be removed by the next setCursor() call.
148 ParIterator it = buffer()->par_iterator_begin();
149 ParIterator end = buffer()->par_iterator_end();
150 for (; it != end; ++it) {
151 Paragraph * par = *it;
152 Paragraph * par_prev = par ? par->previous() : 0;
153 bool removed = false;
155 if (text->setCursor(this, par, 0)
156 && cursor_par == par_prev) {
157 // The previous setCursor line was deleted and that
158 // was the cursor_par line. This can only happen if an
159 // error box was the sole item on cursor_par.
160 // It is possible for cursor_par_prev to be stray if
161 // the line it pointed to only had a error box on it
162 // so we have to set it to a known correct value.
163 // This is often the same value it already had.
164 cursor_par_prev = par->previous();
165 if (cursor_par_prev) {
166 // '|' = par, '.' = cursor_par, 'E' = error box
167 // First step below may occur before while{}
169 // E -> .E -> |.E -> . -> |b
172 cursor_par = cursor_par_prev;
173 cursor_pos = cursor_par_prev->size();
174 cursor_par_prev = cursor_par->previous();
175 // cursor_par_next remains the same
176 } else if (cursor_par_next) {
177 // First step below may occur before while{}
179 // E -> |.E -> |. -> . -> .|a
181 cursor_par = cursor_par_next;
183 // cursor_par_prev remains unset
184 cursor_par_next = cursor_par->next();
186 // I can't find a way to trigger this
187 // so it should be unreachable code
188 // unless the buffer is corrupted.
189 lyxerr << "BufferView::removeAutoInsets() is bad\n";
193 Paragraph::inset_iterator pit = par->inset_iterator_begin();
194 Paragraph::inset_iterator pend = par->inset_iterator_end();
195 while (pit != pend) {
196 if (pit->autoDelete()) {
198 pos_type const pos = pit.getPos();
201 // We just invalidated par's inset iterators so
202 // we get the next valid iterator position
203 pit = par->InsetIterator(pos);
204 // and ensure we have a valid end iterator.
205 pend = par->inset_iterator_end();
207 if (cursor_par == par) {
208 // update the saved cursor position
209 if (cursor_pos > pos)
218 text->redoParagraph(this);
222 // It is possible that the last line is empty if it was cursor_par
223 // and/or only had an error inset on it. So we set the cursor to the
224 // start of the doc to force its removal and ensure a valid saved cursor
225 if (text->setCursor(this, text->ownerParagraph(), 0)
226 && 0 == cursor_par_next) {
227 cursor_par = cursor_par_prev;
228 cursor_pos = cursor_par->size();
229 } else if (cursor_pos > cursor_par->size()) {
230 // Some C-Enter lines were removed by the setCursor call which
231 // then invalidated cursor_pos. It could still be "wrong" because
232 // the cursor may appear to have jumped but since we collapsed
233 // some C-Enter lines this should be a reasonable compromise.
234 cursor_pos = cursor_par->size();
237 // restore the original cursor in its corrected location.
238 text->setCursorIntern(this, cursor_par, cursor_pos);
244 void BufferView::insertErrors(TeXErrors & terr)
246 // Save the cursor position
247 LyXCursor cursor = text->cursor;
249 TeXErrors::Errors::const_iterator cit = terr.begin();
250 TeXErrors::Errors::const_iterator end = terr.end();
251 for (; cit != end; ++cit) {
252 string const desctext(cit->error_desc);
253 string const errortext(cit->error_text);
254 string const msgtxt = desctext + '\n' + errortext;
255 int const errorrow = cit->error_in_line;
257 // Insert error string for row number
261 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
262 buffer()->texrow.increasePos(tmpid, tmppos);
265 Paragraph * texrowpar = 0;
268 texrowpar = text->ownerParagraph();
271 texrowpar = buffer()->getParFromID(tmpid);
277 InsetError * new_inset = new InsetError(msgtxt);
278 text->setCursorIntern(this, texrowpar, tmppos);
279 text->insertInset(this, new_inset);
280 text->fullRebreak(this);
282 // Restore the cursor position
283 text->setCursorIntern(this, cursor.par(), cursor.pos());
287 void BufferView::setCursorFromRow(int row)
292 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
294 Paragraph * texrowpar;
297 texrowpar = text->ownerParagraph();
300 texrowpar = buffer()->getParFromID(tmpid);
302 text->setCursor(this, texrowpar, tmppos);
306 bool BufferView::insertInset(Inset * inset, string const & lout)
308 return pimpl_->insertInset(inset, lout);
312 /* This is also a buffer property (ale) */
313 // Not so sure about that. a goto Label function can not be buffer local, just
314 // think how this will work in a multiwindo/buffer environment, all the
315 // cursors in all the views showing this buffer will move. (Lgb)
316 // OK, then no cursor action should be allowed in buffer. (ale)
317 bool BufferView::gotoLabel(string const & label)
320 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
321 it != buffer()->inset_iterator_end(); ++it) {
322 vector<string> labels = (*it)->getLabelList();
323 if (find(labels.begin(),labels.end(),label)
326 text->setCursor(this, it.getPar(), it.getPos());
327 text->selection.cursor = text->cursor;
328 update(text, BufferView::SELECT|BufferView::FITCUR);
336 void BufferView::menuUndo()
339 owner()->message(_("Undo"));
342 update(text, BufferView::SELECT|BufferView::FITCUR);
344 owner()->message(_("No further undo information"));
346 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
352 void BufferView::menuRedo()
354 #if 0 // this should not be here (Jug 20011206)
355 if (theLockingInset()) {
356 owner()->message(_("Redo not yet supported in math mode"));
362 owner()->message(_("Redo"));
365 update(text, BufferView::SELECT|BufferView::FITCUR);
367 owner()->message(_("No further redo information"));
369 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 string const BufferView::nextWord(float & value)
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 Paragraph * par = buffer()->paragraph;
529 int const id = inset->id();
531 Paragraph::inset_iterator it =
532 par->inset_iterator_begin();
533 Paragraph::inset_iterator const end =
534 par->inset_iterator_end();
535 for (; it != end; ++it) {
536 if ((*it) == inset) {
537 text->setCursorIntern(this, par, it.getPos());
538 theLockingInset(inset);
541 if ((*it)->getInsetFromID(id)) {
542 text->setCursorIntern(this, par, it.getPos());
544 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 pimpl_->screen_->showManualCursor(text, x, y, asc, desc,
585 void BufferView::hideLockedInsetCursor()
587 if (theLockingInset() && available()) {
588 pimpl_->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 (pimpl_->screen_->fitManualCursor(text, this, 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());
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 (Paragraph::inset_iterator it2 = par->inset_iterator_begin();
664 it2 != par->inset_iterator_end(); ++it2) {
665 if ((*it2)->lyxCode() == code) {
666 InsetCommand * inset = static_cast<InsetCommand *>(*it2);
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();