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 // get the next valid iterator position
197 pit = par->InsetIterator(pos);
198 // ensure we have a valid end iterator
199 pend = par->inset_iterator_end();
201 if (cur_par == par) {
211 text->redoParagraph(this);
215 // It is possible that the last line is empty if it was cur_par and/or
216 // only had an error inset on it.
217 if (text->setCursor(this, text->ownerParagraph(), 0)
218 && 0 == cur_par_next) {
219 cur_par = cur_par_prev;
220 cur_pos = cur_par->size();
221 } else if (cur_pos > cur_par->size()) {
222 // Some C-Enter lines were removed by the setCursor call which
223 // then invalidated cur_pos. It could still be "wrong" because
224 // the cursor may appear to have jumped but since we collapsed
225 // some C-Enter lines this should be a reasonable compromise.
226 cur_pos = cur_par->size();
229 text->setCursorIntern(this, cur_par, cur_pos);
235 void BufferView::insertErrors(TeXErrors & terr)
237 // Save the cursor position
238 LyXCursor cursor = text->cursor;
240 TeXErrors::Errors::const_iterator cit = terr.begin();
241 TeXErrors::Errors::const_iterator end = terr.end();
242 for (; cit != end; ++cit) {
243 string const desctext(cit->error_desc);
244 string const errortext(cit->error_text);
245 string const msgtxt = desctext + '\n' + errortext;
246 int const errorrow = cit->error_in_line;
248 // Insert error string for row number
252 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
253 buffer()->texrow.increasePos(tmpid, tmppos);
256 Paragraph * texrowpar = 0;
259 texrowpar = text->ownerParagraph();
262 texrowpar = buffer()->getParFromID(tmpid);
268 InsetError * new_inset = new InsetError(msgtxt);
269 text->setCursorIntern(this, texrowpar, tmppos);
270 text->insertInset(this, new_inset);
271 text->fullRebreak(this);
273 // Restore the cursor position
274 text->setCursorIntern(this, cursor.par(), cursor.pos());
278 void BufferView::setCursorFromRow(int row)
283 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
285 Paragraph * texrowpar;
288 texrowpar = text->ownerParagraph();
291 texrowpar = buffer()->getParFromID(tmpid);
293 text->setCursor(this, texrowpar, tmppos);
297 bool BufferView::insertInset(Inset * inset, string const & lout)
299 return pimpl_->insertInset(inset, lout);
303 /* This is also a buffer property (ale) */
304 // Not so sure about that. a goto Label function can not be buffer local, just
305 // think how this will work in a multiwindo/buffer environment, all the
306 // cursors in all the views showing this buffer will move. (Lgb)
307 // OK, then no cursor action should be allowed in buffer. (ale)
308 bool BufferView::gotoLabel(string const & label)
311 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
312 it != buffer()->inset_iterator_end(); ++it) {
313 vector<string> labels = (*it)->getLabelList();
314 if (find(labels.begin(),labels.end(),label)
317 text->setCursor(this, it.getPar(), it.getPos());
318 text->selection.cursor = text->cursor;
319 update(text, BufferView::SELECT|BufferView::FITCUR);
327 void BufferView::menuUndo()
330 owner()->message(_("Undo"));
333 update(text, BufferView::SELECT|BufferView::FITCUR);
335 owner()->message(_("No further undo information"));
337 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
343 void BufferView::menuRedo()
345 #if 0 // this should not be here (Jug 20011206)
346 if (theLockingInset()) {
347 owner()->message(_("Redo not yet supported in math mode"));
353 owner()->message(_("Redo"));
356 update(text, BufferView::SELECT|BufferView::FITCUR);
358 owner()->message(_("No further redo information"));
360 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
366 void BufferView::copyEnvironment()
369 text->copyEnvironmentType();
370 owner()->message(_("Paragraph environment type copied"));
375 void BufferView::pasteEnvironment()
378 text->pasteEnvironmentType(this);
379 owner()->message(_("Paragraph environment type set"));
380 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
385 void BufferView::copy()
388 text->copySelection(this);
389 owner()->message(_("Copy"));
394 void BufferView::cut(bool realcut)
398 update(text, BufferView::SELECT|BufferView::FITCUR);
399 text->cutSelection(this, true, realcut);
400 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
401 owner()->message(_("Cut"));
406 void BufferView::paste()
408 if (!available()) return;
410 owner()->message(_("Paste"));
413 // clear the selection
415 text->clearSelection();
416 update(text, BufferView::SELECT|BufferView::FITCUR);
419 text->pasteSelection(this);
420 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
422 // clear the selection
424 text->clearSelection();
425 update(text, BufferView::SELECT|BufferView::FITCUR);
429 /* these functions are for the spellchecker */
430 string const BufferView::nextWord(float & value)
437 return text->selectNextWordToSpellcheck(this, value);
441 void BufferView::selectLastWord()
443 if (!available()) return;
445 LyXCursor cur = text->selection.cursor;
448 text->selection.cursor = cur;
449 text->selectSelectedWord(this);
450 toggleSelection(false);
451 update(text, BufferView::SELECT|BufferView::FITCUR);
455 void BufferView::endOfSpellCheck()
457 if (!available()) return;
461 text->selectSelectedWord(this);
462 text->clearSelection();
463 update(text, BufferView::SELECT|BufferView::FITCUR);
467 void BufferView::replaceWord(string const & replacestring)
469 if (!available()) return;
471 LyXText * tt = getLyXText();
473 update(tt, BufferView::SELECT|BufferView::FITCUR);
475 /* clear the selection (if there is any) */
476 toggleSelection(false);
477 update(tt, BufferView::SELECT|BufferView::FITCUR);
479 /* clear the selection (if there is any) */
480 toggleSelection(false);
481 tt->replaceSelectionWithString(this, replacestring);
483 tt->setSelectionOverString(this, replacestring);
485 // Go back so that replacement string is also spellchecked
486 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
487 tt->cursorLeft(this);
489 update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
491 // End of spellchecker stuff
494 bool BufferView::lockInset(UpdatableInset * inset)
498 // don't relock if we're already locked
499 if (theLockingInset() == inset)
501 if (!theLockingInset()) {
502 // first check if it's the inset under the cursor we want lock
503 // should be most of the time
504 char const c = text->cursor.par()->getChar(text->cursor.pos());
505 if (c == Paragraph::META_INSET) {
506 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
508 theLockingInset(inset);
512 // Then do a deep look of the inset and lock the right one
513 Paragraph * par = buffer()->paragraph;
514 int const id = inset->id();
516 Paragraph::inset_iterator it =
517 par->inset_iterator_begin();
518 Paragraph::inset_iterator const end =
519 par->inset_iterator_end();
520 for (; it != end; ++it) {
521 if ((*it) == inset) {
522 text->setCursorIntern(this, par, it.getPos());
523 theLockingInset(inset);
526 if ((*it)->getInsetFromID(id)) {
527 text->setCursorIntern(this, par, it.getPos());
529 return theLockingInset()->lockInsetInInset(this, inset);
536 return theLockingInset()->lockInsetInInset(this, inset);
540 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
542 if (available() && theLockingInset() && !theLockingInset()->nodraw()) {
543 LyXCursor cursor = text->cursor;
544 Inset * locking_inset = theLockingInset()->getLockingInset();
546 if ((cursor.pos() - 1 >= 0) &&
547 cursor.par()->isInset(cursor.pos() - 1) &&
548 (cursor.par()->getInset(cursor.pos() - 1) ==
550 text->setCursor(this, cursor,
551 cursor.par(), cursor.pos() - 1);
552 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
553 LyXText * txt = getLyXText();
554 if (locking_inset->isTextInset() &&
555 locking_inset->lyxCode() != Inset::ERT_CODE &&
556 (txt->real_current_font.language() !=
557 buffer()->params.language
558 || txt->real_current_font.isVisibleRightToLeft()
559 != buffer()->params.language->RightToLeft()))
560 shape = (txt->real_current_font.isVisibleRightToLeft())
561 ? LyXScreen::REVERSED_L_SHAPE
562 : LyXScreen::L_SHAPE;
563 y += cursor.y() + theLockingInset()->insetInInsetY();
564 pimpl_->screen_->showManualCursor(text, x, y, asc, desc,
570 void BufferView::hideLockedInsetCursor()
572 if (theLockingInset() && available()) {
573 pimpl_->screen_->hideCursor();
578 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
580 if (theLockingInset() && available()) {
581 y += text->cursor.y() + theLockingInset()->insetInInsetY();
582 if (pimpl_->screen_->fitManualCursor(text, this, x, y, asc, desc))
588 int BufferView::unlockInset(UpdatableInset * inset)
592 if (inset && theLockingInset() == inset) {
593 inset->insetUnlock(this);
595 // make sure we update the combo !
596 owner()->setLayout(getLyXText()->cursor.par()->getLayout());
599 } else if (inset && theLockingInset() &&
600 theLockingInset()->unlockInsetInInset(this, inset)) {
601 // owner inset has updated the layout combo
605 return bufferlist.unlockInset(inset);
609 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
611 if (!theLockingInset())
612 return; // shouldn't happen
613 if (kind == Undo::EDIT) // in this case insets would not be stored!
617 text->cursor.par()->next());
621 void BufferView::updateInset(Inset * inset, bool mark_dirty)
623 pimpl_->updateInset(inset, mark_dirty);
627 bool BufferView::ChangeInsets(Inset::Code code,
628 string const & from, string const & to)
630 bool need_update = false;
631 LyXCursor cursor = text->cursor;
632 LyXCursor tmpcursor = cursor;
633 cursor.par(tmpcursor.par());
634 cursor.pos(tmpcursor.pos());
636 ParIterator end = buffer()->par_iterator_end();
637 for (ParIterator it = buffer()->par_iterator_begin();
639 Paragraph * par = *it;
640 bool changed_inset = false;
641 for (Paragraph::inset_iterator it2 = par->inset_iterator_begin();
642 it2 != par->inset_iterator_end(); ++it2) {
643 if ((*it2)->lyxCode() == code) {
644 InsetCommand * inset = static_cast<InsetCommand *>(*it2);
645 if (inset->getContents() == from) {
646 inset->setContents(to);
647 changed_inset = true;
656 // The test it.size()==1 was needed to prevent crashes.
657 // How to set the cursor corretly when it.size()>1 ??
658 if (it.size() == 1) {
659 text->setCursorIntern(this, par, 0);
660 text->redoParagraphs(this, text->cursor,
661 text->cursor.par()->next());
662 text->fullRebreak(this);
666 text->setCursorIntern(this, cursor.par(), cursor.pos());
671 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
673 // Check if the label 'from' appears more than once
674 vector<string> labels = buffer()->getLabelList();
676 if (lyx::count(labels.begin(), labels.end(), from) > 1)
679 return ChangeInsets(Inset::REF_CODE, from, to);
683 bool BufferView::ChangeCitationsIfUnique(string const & from,
686 typedef pair<string, string> StringPair;
688 vector<StringPair> keys = buffer()->getBibkeyList();
689 if (count_if(keys.begin(), keys.end(),
690 lyx::equal_1st_in_pair<StringPair>(from))
694 return ChangeInsets(Inset::CITE_CODE, from, to);
698 UpdatableInset * BufferView::theLockingInset() const
700 // If NULL is not allowed we should put an Assert here. (Lgb)
702 return text->the_locking_inset;
707 void BufferView::theLockingInset(UpdatableInset * inset)
709 text->the_locking_inset = inset;
713 LyXText * BufferView::getLyXText() const
715 if (theLockingInset()) {
716 LyXText * txt = theLockingInset()->getLyXText(this, true);
724 LyXText * BufferView::getParentText(Inset * inset) const
726 if (inset->owner()) {
727 LyXText * txt = inset->getLyXText(this);
728 inset = inset->owner();
729 while (inset && inset->getLyXText(this) == txt)
730 inset = inset->owner();
732 return inset->getLyXText(this);
738 Language const * BufferView::getParentLanguage(Inset * inset) const
740 LyXText * text = getParentText(inset);
741 return text->cursor.par()->getFontSettings(buffer()->params,
742 text->cursor.pos()).language();