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 Paragraph * cur_par = text->cursor.par();
111 Paragraph * cur_par_prev = cur_par ? cur_par->previous() : 0;
112 Paragraph * cur_par_next = cur_par ? cur_par->next() : 0;
113 pos_type cur_pos = text->cursor.pos();
117 // Trap the deletion of the paragraph the cursor is in.
118 // Iterate until we find a paragraph that won't be immediately deleted.
119 // In reality this should mean we only execute the body of the while
120 // loop once at most. However for safety we iterate rather than just
121 // make this an if () conditional.
122 while ((cur_par_prev || cur_par_next)
123 && text->setCursor(this,
124 cur_par_prev ? cur_par_prev : cur_par_next,
126 // We just removed cur_par so have to fix the "cursor"
131 cur_par = cur_par_prev;
132 cur_pos = cur_par->size();
136 cur_par = cur_par_next;
139 cur_par_prev = cur_par->previous();
140 cur_par_next = cur_par->next();
143 ParIterator it = buffer()->par_iterator_begin();
144 ParIterator end = buffer()->par_iterator_end();
145 for (; it != end; ++it) {
146 Paragraph * par = *it;
147 Paragraph * par_prev = par ? par->previous() : 0;
148 bool removed = false;
150 if (text->setCursor(this, par, 0)
151 && cur_par == par_prev) {
152 // The previous setCursor line was deleted and that
153 // was the cur_par line. This can only happen if an
154 // error box was the sole item on cur_par.
155 // It is possible for cur_par_prev to be stray if
156 // the line it pointed to only had a error box on it
157 // so we have to set it to a known correct value.
158 // This is often the same value it already had.
159 cur_par_prev = par->previous();
161 // '|' = par, '.' = cur_par, 'E' = error box
162 // First step below may occur before while{}
164 // E -> .E -> |.E -> . -> |b
167 cur_par = cur_par_prev;
168 cur_pos = cur_par_prev->size();
169 cur_par_prev = cur_par->previous();
170 // cur_par_next remains the same
171 } else if (cur_par_next) {
172 // First step below may occur before while{}
174 // E -> |.E -> |. -> . -> .|a
176 cur_par = cur_par_next;
178 // cur_par_prev remains unset
179 cur_par_next = cur_par->next();
181 // I can't find a way to trigger this
182 // so it should be unreachable code
183 // unless the buffer is corrupted.
184 lyxerr << "BufferView::removeAutoInsets() is bad\n";
188 Paragraph::inset_iterator pit = par->inset_iterator_begin();
189 Paragraph::inset_iterator pend = par->inset_iterator_end();
190 while (pit != pend) {
191 if (pit->autoDelete()) {
193 pos_type const pos = pit.getPos();
196 if (cur_par == par) {
206 text->redoParagraph(this);
210 // It is possible that the last line is empty if it was cur_par and/or
211 // only had an error inset on it.
212 if (text->setCursor(this, text->ownerParagraph(), 0)
213 && 0 == cur_par_next) {
214 cur_par = cur_par_prev;
215 cur_pos = cur_par->size();
216 } else if (cur_pos > cur_par->size()) {
217 // Some C-Enter lines were removed by the setCursor call which
218 // then invalidated cur_pos. It could still be "wrong" because
219 // the cursor may appear to have jumped but since we collapsed
220 // some C-Enter lines this should be a reasonable compromise.
221 cur_pos = cur_par->size();
224 text->setCursorIntern(this, cur_par, cur_pos);
230 void BufferView::insertErrors(TeXErrors & terr)
232 // Save the cursor position
233 LyXCursor cursor = text->cursor;
235 TeXErrors::Errors::const_iterator cit = terr.begin();
236 TeXErrors::Errors::const_iterator end = terr.end();
237 for (; cit != end; ++cit) {
238 string const desctext(cit->error_desc);
239 string const errortext(cit->error_text);
240 string const msgtxt = desctext + '\n' + errortext;
241 int const errorrow = cit->error_in_line;
243 // Insert error string for row number
247 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
248 buffer()->texrow.increasePos(tmpid, tmppos);
251 Paragraph * texrowpar = 0;
254 texrowpar = text->ownerParagraph();
257 texrowpar = buffer()->getParFromID(tmpid);
263 InsetError * new_inset = new InsetError(msgtxt);
264 text->setCursorIntern(this, texrowpar, tmppos);
265 text->insertInset(this, new_inset);
266 text->fullRebreak(this);
268 // Restore the cursor position
269 text->setCursorIntern(this, cursor.par(), cursor.pos());
273 void BufferView::setCursorFromRow(int row)
278 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
280 Paragraph * texrowpar;
283 texrowpar = text->ownerParagraph();
286 texrowpar = buffer()->getParFromID(tmpid);
288 text->setCursor(this, texrowpar, tmppos);
292 bool BufferView::insertInset(Inset * inset, string const & lout)
294 return pimpl_->insertInset(inset, lout);
298 /* This is also a buffer property (ale) */
299 // Not so sure about that. a goto Label function can not be buffer local, just
300 // think how this will work in a multiwindo/buffer environment, all the
301 // cursors in all the views showing this buffer will move. (Lgb)
302 // OK, then no cursor action should be allowed in buffer. (ale)
303 bool BufferView::gotoLabel(string const & label)
306 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
307 it != buffer()->inset_iterator_end(); ++it) {
308 vector<string> labels = (*it)->getLabelList();
309 if (find(labels.begin(),labels.end(),label)
312 text->setCursor(this, it.getPar(), it.getPos());
313 text->selection.cursor = text->cursor;
314 update(text, BufferView::SELECT|BufferView::FITCUR);
322 void BufferView::menuUndo()
325 owner()->message(_("Undo"));
328 update(text, BufferView::SELECT|BufferView::FITCUR);
330 owner()->message(_("No further undo information"));
332 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
338 void BufferView::menuRedo()
340 #if 0 // this should not be here (Jug 20011206)
341 if (theLockingInset()) {
342 owner()->message(_("Redo not yet supported in math mode"));
348 owner()->message(_("Redo"));
351 update(text, BufferView::SELECT|BufferView::FITCUR);
353 owner()->message(_("No further redo information"));
355 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
361 void BufferView::copyEnvironment()
364 text->copyEnvironmentType();
365 owner()->message(_("Paragraph environment type copied"));
370 void BufferView::pasteEnvironment()
373 text->pasteEnvironmentType(this);
374 owner()->message(_("Paragraph environment type set"));
375 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
380 void BufferView::copy()
383 text->copySelection(this);
384 owner()->message(_("Copy"));
389 void BufferView::cut(bool realcut)
393 update(text, BufferView::SELECT|BufferView::FITCUR);
394 text->cutSelection(this, true, realcut);
395 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
396 owner()->message(_("Cut"));
401 void BufferView::paste()
403 if (!available()) return;
405 owner()->message(_("Paste"));
408 // clear the selection
410 text->clearSelection();
411 update(text, BufferView::SELECT|BufferView::FITCUR);
414 text->pasteSelection(this);
415 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
417 // clear the selection
419 text->clearSelection();
420 update(text, BufferView::SELECT|BufferView::FITCUR);
424 /* these functions are for the spellchecker */
425 string const BufferView::nextWord(float & value)
432 return text->selectNextWordToSpellcheck(this, value);
436 void BufferView::selectLastWord()
438 if (!available()) return;
440 LyXCursor cur = text->selection.cursor;
443 text->selection.cursor = cur;
444 text->selectSelectedWord(this);
445 toggleSelection(false);
446 update(text, BufferView::SELECT|BufferView::FITCUR);
450 void BufferView::endOfSpellCheck()
452 if (!available()) return;
456 text->selectSelectedWord(this);
457 text->clearSelection();
458 update(text, BufferView::SELECT|BufferView::FITCUR);
462 void BufferView::replaceWord(string const & replacestring)
464 if (!available()) return;
466 LyXText * tt = getLyXText();
468 update(tt, BufferView::SELECT|BufferView::FITCUR);
470 /* clear the selection (if there is any) */
471 toggleSelection(false);
472 update(tt, BufferView::SELECT|BufferView::FITCUR);
474 /* clear the selection (if there is any) */
475 toggleSelection(false);
476 tt->replaceSelectionWithString(this, replacestring);
478 tt->setSelectionOverString(this, replacestring);
480 // Go back so that replacement string is also spellchecked
481 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
482 tt->cursorLeft(this);
484 update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
486 // End of spellchecker stuff
489 bool BufferView::lockInset(UpdatableInset * inset)
493 // don't relock if we're already locked
494 if (theLockingInset() == inset)
496 if (!theLockingInset()) {
497 // first check if it's the inset under the cursor we want lock
498 // should be most of the time
499 char const c = text->cursor.par()->getChar(text->cursor.pos());
500 if (c == Paragraph::META_INSET) {
501 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
503 theLockingInset(inset);
507 // Then do a deep look of the inset and lock the right one
508 Paragraph * par = buffer()->paragraph;
509 int const id = inset->id();
511 Paragraph::inset_iterator it =
512 par->inset_iterator_begin();
513 Paragraph::inset_iterator const end =
514 par->inset_iterator_end();
515 for (; it != end; ++it) {
516 if ((*it) == inset) {
517 text->setCursorIntern(this, par, it.getPos());
518 theLockingInset(inset);
521 if ((*it)->getInsetFromID(id)) {
522 text->setCursorIntern(this, par, it.getPos());
524 return theLockingInset()->lockInsetInInset(this, inset);
531 return theLockingInset()->lockInsetInInset(this, inset);
535 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
537 if (available() && theLockingInset() && !theLockingInset()->nodraw()) {
538 LyXCursor cursor = text->cursor;
539 Inset * locking_inset = theLockingInset()->getLockingInset();
541 if ((cursor.pos() - 1 >= 0) &&
542 cursor.par()->isInset(cursor.pos() - 1) &&
543 (cursor.par()->getInset(cursor.pos() - 1) ==
545 text->setCursor(this, cursor,
546 cursor.par(), cursor.pos() - 1);
547 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
548 LyXText * txt = getLyXText();
549 if (locking_inset->isTextInset() &&
550 locking_inset->lyxCode() != Inset::ERT_CODE &&
551 (txt->real_current_font.language() !=
552 buffer()->params.language
553 || txt->real_current_font.isVisibleRightToLeft()
554 != buffer()->params.language->RightToLeft()))
555 shape = (txt->real_current_font.isVisibleRightToLeft())
556 ? LyXScreen::REVERSED_L_SHAPE
557 : LyXScreen::L_SHAPE;
558 y += cursor.y() + theLockingInset()->insetInInsetY();
559 pimpl_->screen_->showManualCursor(text, x, y, asc, desc,
565 void BufferView::hideLockedInsetCursor()
567 if (theLockingInset() && available()) {
568 pimpl_->screen_->hideCursor();
573 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
575 if (theLockingInset() && available()) {
576 y += text->cursor.y() + theLockingInset()->insetInInsetY();
577 if (pimpl_->screen_->fitManualCursor(text, this, x, y, asc, desc))
583 int BufferView::unlockInset(UpdatableInset * inset)
587 if (inset && theLockingInset() == inset) {
588 inset->insetUnlock(this);
590 // make sure we update the combo !
591 owner()->setLayout(getLyXText()->cursor.par()->getLayout());
594 } else if (inset && theLockingInset() &&
595 theLockingInset()->unlockInsetInInset(this, inset)) {
596 // owner inset has updated the layout combo
600 return bufferlist.unlockInset(inset);
604 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
606 if (!theLockingInset())
607 return; // shouldn't happen
608 if (kind == Undo::EDIT) // in this case insets would not be stored!
612 text->cursor.par()->next());
616 void BufferView::updateInset(Inset * inset, bool mark_dirty)
618 pimpl_->updateInset(inset, mark_dirty);
622 bool BufferView::ChangeInsets(Inset::Code code,
623 string const & from, string const & to)
625 bool need_update = false;
626 LyXCursor cursor = text->cursor;
627 LyXCursor tmpcursor = cursor;
628 cursor.par(tmpcursor.par());
629 cursor.pos(tmpcursor.pos());
631 ParIterator end = buffer()->par_iterator_end();
632 for (ParIterator it = buffer()->par_iterator_begin();
634 Paragraph * par = *it;
635 bool changed_inset = false;
636 for (Paragraph::inset_iterator it2 = par->inset_iterator_begin();
637 it2 != par->inset_iterator_end(); ++it2) {
638 if ((*it2)->lyxCode() == code) {
639 InsetCommand * inset = static_cast<InsetCommand *>(*it2);
640 if (inset->getContents() == from) {
641 inset->setContents(to);
642 changed_inset = true;
651 // The test it.size()==1 was needed to prevent crashes.
652 // How to set the cursor corretly when it.size()>1 ??
653 if (it.size() == 1) {
654 text->setCursorIntern(this, par, 0);
655 text->redoParagraphs(this, text->cursor,
656 text->cursor.par()->next());
657 text->fullRebreak(this);
661 text->setCursorIntern(this, cursor.par(), cursor.pos());
666 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
668 // Check if the label 'from' appears more than once
669 vector<string> labels = buffer()->getLabelList();
671 if (lyx::count(labels.begin(), labels.end(), from) > 1)
674 return ChangeInsets(Inset::REF_CODE, from, to);
678 bool BufferView::ChangeCitationsIfUnique(string const & from,
681 typedef pair<string, string> StringPair;
683 vector<StringPair> keys = buffer()->getBibkeyList();
684 if (count_if(keys.begin(), keys.end(),
685 lyx::equal_1st_in_pair<StringPair>(from))
689 return ChangeInsets(Inset::CITE_CODE, from, to);
693 UpdatableInset * BufferView::theLockingInset() const
695 // If NULL is not allowed we should put an Assert here. (Lgb)
697 return text->the_locking_inset;
702 void BufferView::theLockingInset(UpdatableInset * inset)
704 text->the_locking_inset = inset;
708 LyXText * BufferView::getLyXText() const
710 if (theLockingInset()) {
711 LyXText * txt = theLockingInset()->getLyXText(this, true);
719 LyXText * BufferView::getParentText(Inset * inset) const
721 if (inset->owner()) {
722 LyXText * txt = inset->getLyXText(this);
723 inset = inset->owner();
724 while (inset && inset->getLyXText(this) == txt)
725 inset = inset->owner();
727 return inset->getLyXText(this);
733 Language const * BufferView::getParentLanguage(Inset * inset) const
735 LyXText * text = getParentText(inset);
736 return text->cursor.par()->getFontSettings(buffer()->params,
737 text->cursor.pos()).language();