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"
29 #include "frontends/Alert.h"
30 #include "frontends/Dialogs.h"
32 #include "insets/insetcommand.h" //ChangeRefs
33 #include "insets/inseterror.h"
35 #include "support/FileInfo.h"
36 #include "support/filetools.h"
37 #include "support/lyxfunctional.h" //equal_1st_in_pair
38 #include "support/types.h"
39 #include "support/lyxalgo.h" // lyx_count
43 extern BufferList bufferlist;
55 // Inserts a file into current document
56 bool BufferView::insertLyXFile(string const & filen)
58 // Copyright CHT Software Service GmbH
61 // Insert a LyXformat - file into current buffer
63 // Moved from lyx_cb.C (Lgb)
65 if (filen.empty()) return false;
67 string const fname = MakeAbsPath(filen);
69 // check if file exist
70 FileInfo const fi(fname);
73 Alert::alert(_("Error!"),
74 _("Specified file is unreadable: "),
75 MakeDisplayPath(fname, 50));
81 ifstream ifs(fname.c_str());
83 Alert::alert(_("Error!"),
84 _("Cannot open specified file: "),
85 MakeDisplayPath(fname, 50));
89 int const c = ifs.peek();
97 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
98 res = buffer()->readFile(lex, text->cursor.par());
100 lyxerr[Debug::INFO] << "Will insert file without header"
102 res = buffer()->readLyXformat2(lex, text->cursor.par());
110 bool BufferView::removeAutoInsets()
112 // keep track of which pos and par the cursor was on
113 Paragraph * cursor_par = text->cursor.par();
114 Paragraph * cursor_par_prev = cursor_par ? cursor_par->previous() : 0;
115 Paragraph * cursor_par_next = cursor_par ? cursor_par->next() : 0;
116 pos_type cursor_pos = text->cursor.pos();
120 // Trap the deletion of the paragraph the cursor is in.
121 // Iterate until we find a paragraph that won't be immediately deleted.
122 // In reality this should mean we only execute the body of the while
123 // loop once at most. However for safety we iterate rather than just
124 // make this an if () conditional.
125 while ((cursor_par_prev || cursor_par_next)
126 && text->setCursor(this,
127 cursor_par_prev ? cursor_par_prev : cursor_par_next,
129 // We just removed cursor_par so have to fix the "cursor"
130 if (cursor_par_prev) {
134 cursor_par = cursor_par_prev;
135 cursor_pos = cursor_par->size();
139 cursor_par = cursor_par_next;
142 cursor_par_prev = cursor_par->previous();
143 cursor_par_next = cursor_par->next();
146 // Iterate through the paragraphs removing autoDelete insets as we go.
147 // If the paragraph ends up empty after all the autoDelete insets are
148 // removed that paragraph will be removed by the next setCursor() call.
149 ParIterator it = buffer()->par_iterator_begin();
150 ParIterator end = buffer()->par_iterator_end();
151 for (; it != end; ++it) {
152 Paragraph * par = *it;
153 Paragraph * par_prev = par ? par->previous() : 0;
154 bool removed = false;
156 if (text->setCursor(this, par, 0)
157 && cursor_par == par_prev) {
158 // The previous setCursor line was deleted and that
159 // was the cursor_par line. This can only happen if an
160 // error box was the sole item on cursor_par.
161 // It is possible for cursor_par_prev to be stray if
162 // the line it pointed to only had a error box on it
163 // so we have to set it to a known correct value.
164 // This is often the same value it already had.
165 cursor_par_prev = par->previous();
166 if (cursor_par_prev) {
167 // '|' = par, '.' = cursor_par, 'E' = error box
168 // First step below may occur before while{}
170 // E -> .E -> |.E -> . -> |b
173 cursor_par = cursor_par_prev;
174 cursor_pos = cursor_par_prev->size();
175 cursor_par_prev = cursor_par->previous();
176 // cursor_par_next remains the same
177 } else if (cursor_par_next) {
178 // First step below may occur before while{}
180 // E -> |.E -> |. -> . -> .|a
182 cursor_par = cursor_par_next;
184 // cursor_par_prev remains unset
185 cursor_par_next = cursor_par->next();
187 // I can't find a way to trigger this
188 // so it should be unreachable code
189 // unless the buffer is corrupted.
190 lyxerr << "BufferView::removeAutoInsets() is bad\n";
194 InsetList::iterator pit = par->insetlist.begin();
195 InsetList::iterator pend = par->insetlist.end();
197 while (pit != pend) {
198 if (pit.getInset()->autoDelete()) {
200 pos_type const pos = pit.getPos();
203 // We just invalidated par's inset iterators so
204 // we get the next valid iterator position
205 pit = par->insetlist.insetIterator(pos);
206 // and ensure we have a valid end iterator.
207 pend = par->insetlist.end();
209 if (cursor_par == par) {
210 // update the saved cursor position
211 if (cursor_pos > pos)
220 text->redoParagraph(this);
224 // It is possible that the last line is empty if it was cursor_par
225 // and/or only had an error inset on it. So we set the cursor to the
226 // start of the doc to force its removal and ensure a valid saved cursor
227 if (text->setCursor(this, text->ownerParagraph(), 0)
228 && 0 == cursor_par_next) {
229 cursor_par = cursor_par_prev;
230 cursor_pos = cursor_par->size();
231 } else if (cursor_pos > cursor_par->size()) {
232 // Some C-Enter lines were removed by the setCursor call which
233 // then invalidated cursor_pos. It could still be "wrong" because
234 // the cursor may appear to have jumped but since we collapsed
235 // some C-Enter lines this should be a reasonable compromise.
236 cursor_pos = cursor_par->size();
239 // restore the original cursor in its corrected location.
240 text->setCursorIntern(this, cursor_par, cursor_pos);
246 void BufferView::insertErrors(TeXErrors & terr)
248 // Save the cursor position
249 LyXCursor cursor = text->cursor;
251 TeXErrors::Errors::const_iterator cit = terr.begin();
252 TeXErrors::Errors::const_iterator end = terr.end();
253 for (; cit != end; ++cit) {
254 string const desctext(cit->error_desc);
255 string const errortext(cit->error_text);
256 string const msgtxt = desctext + '\n' + errortext;
257 int const errorrow = cit->error_in_line;
259 // Insert error string for row number
263 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
264 buffer()->texrow.increasePos(tmpid, tmppos);
267 Paragraph * texrowpar = 0;
270 texrowpar = text->ownerParagraph();
273 texrowpar = buffer()->getParFromID(tmpid);
279 InsetError * new_inset = new InsetError(msgtxt);
280 text->setCursorIntern(this, texrowpar, tmppos);
281 text->insertInset(this, new_inset);
282 text->fullRebreak(this);
284 // Restore the cursor position
285 text->setCursorIntern(this, cursor.par(), cursor.pos());
289 void BufferView::setCursorFromRow(int row)
294 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
296 Paragraph * texrowpar;
299 texrowpar = text->ownerParagraph();
302 texrowpar = buffer()->getParFromID(tmpid);
304 text->setCursor(this, texrowpar, tmppos);
308 bool BufferView::insertInset(Inset * inset, string const & lout)
310 return pimpl_->insertInset(inset, lout);
314 /* This is also a buffer property (ale) */
315 // Not so sure about that. a goto Label function can not be buffer local, just
316 // think how this will work in a multiwindo/buffer environment, all the
317 // cursors in all the views showing this buffer will move. (Lgb)
318 // OK, then no cursor action should be allowed in buffer. (ale)
319 bool BufferView::gotoLabel(string const & label)
322 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
323 it != buffer()->inset_iterator_end(); ++it) {
324 vector<string> labels = (*it)->getLabelList();
325 if (find(labels.begin(),labels.end(),label)
328 text->setCursor(this, it.getPar(), it.getPos());
329 text->selection.cursor = text->cursor;
330 update(text, BufferView::SELECT|BufferView::FITCUR);
338 void BufferView::menuUndo()
343 owner()->message(_("Undo"));
346 update(text, BufferView::SELECT|BufferView::FITCUR);
348 owner()->message(_("No further undo information"));
350 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
355 void BufferView::menuRedo()
360 owner()->message(_("Redo"));
363 update(text, BufferView::SELECT|BufferView::FITCUR);
365 owner()->message(_("No further redo information"));
367 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
372 void BufferView::copyEnvironment()
375 text->copyEnvironmentType();
376 owner()->message(_("Paragraph environment type copied"));
381 void BufferView::pasteEnvironment()
384 text->pasteEnvironmentType(this);
385 owner()->message(_("Paragraph environment type set"));
386 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
391 void BufferView::copy()
394 getLyXText()->copySelection(this);
395 owner()->message(_("Copy"));
400 void BufferView::cut(bool realcut)
404 update(text, BufferView::SELECT|BufferView::FITCUR);
405 text->cutSelection(this, true, realcut);
406 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
407 owner()->message(_("Cut"));
412 void BufferView::paste()
417 owner()->message(_("Paste"));
420 // clear the selection
422 text->clearSelection();
423 update(text, BufferView::SELECT|BufferView::FITCUR);
426 text->pasteSelection(this);
428 text->clearSelection();
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 WordLangTuple const BufferView::nextWord(float & value)
446 return WordLangTuple();
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 InsetList::iterator it =
529 par->insetlist.begin();
530 InsetList::iterator const end =
531 par->insetlist.end();
532 for (; it != end; ++it) {
533 if (it.getInset() == inset) {
534 text->setCursorIntern(this, par, it.getPos());
535 theLockingInset(inset);
538 if (it.getInset()->getInsetFromID(id)) {
539 text->setCursorIntern(this, par, it.getPos());
540 it.getInset()->edit(this);
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.iy() + theLockingInset()->insetInInsetY();
576 screen().showManualCursor(text, x, y, asc, desc,
582 void BufferView::hideLockedInsetCursor()
584 if (theLockingInset() && available()) {
585 screen().hideCursor();
590 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
592 if (theLockingInset() && available()) {
593 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
594 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
603 int BufferView::unlockInset(UpdatableInset * inset)
607 if (inset && theLockingInset() == inset) {
608 inset->insetUnlock(this);
610 // make sure we update the combo !
611 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
612 // Tell the paragraph dialog that we changed paragraph
613 owner()->getDialogs()->updateParagraph();
616 } else if (inset && theLockingInset() &&
617 theLockingInset()->unlockInsetInInset(this, inset)) {
618 // Tell the paragraph dialog that we changed paragraph
619 owner()->getDialogs()->updateParagraph();
620 // owner inset has updated the layout combo
624 return bufferlist.unlockInset(inset);
628 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
630 if (!theLockingInset())
631 return; // shouldn't happen
632 if (kind == Undo::EDIT) // in this case insets would not be stored!
636 text->cursor.par()->next());
640 void BufferView::updateInset(Inset * inset, bool mark_dirty)
642 pimpl_->updateInset(inset, mark_dirty);
646 bool BufferView::ChangeInsets(Inset::Code code,
647 string const & from, string const & to)
649 bool need_update = false;
650 LyXCursor cursor = text->cursor;
651 LyXCursor tmpcursor = cursor;
652 cursor.par(tmpcursor.par());
653 cursor.pos(tmpcursor.pos());
655 ParIterator end = buffer()->par_iterator_end();
656 for (ParIterator it = buffer()->par_iterator_begin();
658 Paragraph * par = *it;
659 bool changed_inset = false;
660 for (InsetList::iterator it2 = par->insetlist.begin();
661 it2 != par->insetlist.end(); ++it2) {
662 if (it2.getInset()->lyxCode() == code) {
663 InsetCommand * inset = static_cast<InsetCommand *>(it2.getInset());
664 if (inset->getContents() == from) {
665 inset->setContents(to);
666 changed_inset = true;
675 // The test it.size()==1 was needed to prevent crashes.
676 // How to set the cursor corretly when it.size()>1 ??
677 if (it.size() == 1) {
678 text->setCursorIntern(this, par, 0);
679 text->redoParagraphs(this, text->cursor,
680 text->cursor.par()->next());
681 text->fullRebreak(this);
685 text->setCursorIntern(this, cursor.par(), cursor.pos());
690 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
692 // Check if the label 'from' appears more than once
693 vector<string> labels = buffer()->getLabelList();
695 if (lyx::count(labels.begin(), labels.end(), from) > 1)
698 return ChangeInsets(Inset::REF_CODE, from, to);
702 bool BufferView::ChangeCitationsIfUnique(string const & from,
705 typedef pair<string, string> StringPair;
707 vector<StringPair> keys = buffer()->getBibkeyList();
708 if (count_if(keys.begin(), keys.end(),
709 lyx::equal_1st_in_pair<StringPair>(from))
713 return ChangeInsets(Inset::CITE_CODE, from, to);
717 UpdatableInset * BufferView::theLockingInset() const
719 // If NULL is not allowed we should put an Assert here. (Lgb)
721 return text->the_locking_inset;
726 void BufferView::theLockingInset(UpdatableInset * inset)
728 text->the_locking_inset = inset;
732 LyXText * BufferView::getLyXText() const
734 if (theLockingInset()) {
735 LyXText * txt = theLockingInset()->getLyXText(this, true);
743 LyXText * BufferView::getParentText(Inset * inset) const
745 if (inset->owner()) {
746 LyXText * txt = inset->getLyXText(this);
747 inset = inset->owner();
748 while (inset && inset->getLyXText(this) == txt)
749 inset = inset->owner();
751 return inset->getLyXText(this);
757 Language const * BufferView::getParentLanguage(Inset * inset) const
759 LyXText * text = getParentText(inset);
760 return text->cursor.par()->getFontSettings(buffer()->params,
761 text->cursor.pos()).language();