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"
41 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 LyXCursor tmpcursor = text->cursor;
112 Paragraph * cur_par = tmpcursor.par();
113 Paragraph * cur_par_prev = cur_par ? cur_par->previous() : 0;
114 Paragraph * cur_par_next = cur_par ? cur_par->next() : 0;
115 pos_type cur_pos = tmpcursor.pos();
119 // Trap the deletion of the paragraph the cursor is in.
120 // It should be almost impossible for the new cursor par to be
121 // deleted later on in this function.
122 // This is the way to segfault this now. Although you may have to do this
123 // multiple times: Have an InsetERT with an unknown command in it.
124 // View->DVI, move cursor between Error box and InsetERT and hit <Enter>,
125 // <down-arrow>, <Enter> again, View->DVI, BANG!
127 while ((cur_par_prev || cur_par_next)
128 && text->setCursor(this,
129 cur_par_prev ? cur_par_prev : cur_par_next,
131 // we just removed cur_par so have to fix the "cursor"
133 cur_par = cur_par_prev;
134 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 bool dead = text->setCursor(this, par, 0);
152 Paragraph::inset_iterator pit = par->inset_iterator_begin();
153 Paragraph::inset_iterator pend = par->inset_iterator_end();
154 while (pit != pend) {
155 if (pit->autoDelete()) {
157 pos_type const pos = pit.getPos();
160 if (cur_par == par) {
170 text->redoParagraph(this);
173 // Error box paragraphs appear to have a bad next_ pointer
174 // especially when that's all that's in the paragraph..
175 // Then if you are in an empty paragraph after an error box
176 // which is in its own paragraph this will fail because
177 // cur_prev_par was the one just deleted (I think).
178 // That's the main reason why this clause makes almost no difference.
179 // Feel free to delete this whole clause as my brain is asleep now.
180 while ((cur_par_prev || cur_par_next)
181 && text->setCursor(this,
182 cur_par_prev ? cur_par_prev : cur_par_next,
184 // we just removed cur_par so have to fix the "cursor"
185 if (cur_par_prev == par) {
186 // attempting to solve the "prev par was deleted
187 // because it had only an error inset in it" problem
190 cur_pos = cur_par->size();
192 cur_par = cur_par_next;
195 } else if (cur_par_prev) {
196 cur_par = cur_par_prev;
197 cur_pos = cur_par->size();
199 cur_par = cur_par_next;
202 cur_par_prev = cur_par->previous();
203 cur_par_next = cur_par->next();
208 text->setCursorIntern(this, cur_par, cur_pos);
214 void BufferView::insertErrors(TeXErrors & terr)
216 // Save the cursor position
217 LyXCursor cursor = text->cursor;
219 TeXErrors::Errors::const_iterator cit = terr.begin();
220 TeXErrors::Errors::const_iterator end = terr.end();
221 for (; cit != end; ++cit) {
222 string const desctext(cit->error_desc);
223 string const errortext(cit->error_text);
224 string const msgtxt = desctext + '\n' + errortext;
225 int const errorrow = cit->error_in_line;
227 // Insert error string for row number
231 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
232 buffer()->texrow.increasePos(tmpid, tmppos);
235 Paragraph * texrowpar = 0;
238 texrowpar = text->ownerParagraph();
241 texrowpar = buffer()->getParFromID(tmpid);
247 InsetError * new_inset = new InsetError(msgtxt);
248 text->setCursorIntern(this, texrowpar, tmppos);
249 text->insertInset(this, new_inset);
250 text->fullRebreak(this);
252 // Restore the cursor position
253 text->setCursorIntern(this, cursor.par(), cursor.pos());
257 void BufferView::setCursorFromRow(int row)
262 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
264 Paragraph * texrowpar;
267 texrowpar = text->ownerParagraph();
270 texrowpar = buffer()->getParFromID(tmpid);
272 text->setCursor(this, texrowpar, tmppos);
276 bool BufferView::insertInset(Inset * inset, string const & lout)
278 return pimpl_->insertInset(inset, lout);
282 /* This is also a buffer property (ale) */
283 // Not so sure about that. a goto Label function can not be buffer local, just
284 // think how this will work in a multiwindo/buffer environment, all the
285 // cursors in all the views showing this buffer will move. (Lgb)
286 // OK, then no cursor action should be allowed in buffer. (ale)
287 bool BufferView::gotoLabel(string const & label)
290 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
291 it != buffer()->inset_iterator_end(); ++it) {
292 vector<string> labels = (*it)->getLabelList();
293 if (find(labels.begin(),labels.end(),label)
296 text->setCursor(this, it.getPar(), it.getPos());
297 text->selection.cursor = text->cursor;
298 update(text, BufferView::SELECT|BufferView::FITCUR);
306 void BufferView::menuUndo()
309 owner()->message(_("Undo"));
312 update(text, BufferView::SELECT|BufferView::FITCUR);
314 owner()->message(_("No further undo information"));
316 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
322 void BufferView::menuRedo()
324 #if 0 // this should not be here (Jug 20011206)
325 if (theLockingInset()) {
326 owner()->message(_("Redo not yet supported in math mode"));
332 owner()->message(_("Redo"));
335 update(text, BufferView::SELECT|BufferView::FITCUR);
337 owner()->message(_("No further redo information"));
339 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
345 void BufferView::copyEnvironment()
348 text->copyEnvironmentType();
349 owner()->message(_("Paragraph environment type copied"));
354 void BufferView::pasteEnvironment()
357 text->pasteEnvironmentType(this);
358 owner()->message(_("Paragraph environment type set"));
359 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
364 void BufferView::copy()
367 text->copySelection(this);
368 owner()->message(_("Copy"));
373 void BufferView::cut(bool realcut)
377 update(text, BufferView::SELECT|BufferView::FITCUR);
378 text->cutSelection(this, true, realcut);
379 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
380 owner()->message(_("Cut"));
385 void BufferView::paste()
387 if (!available()) return;
389 owner()->message(_("Paste"));
392 // clear the selection
394 text->clearSelection();
395 update(text, BufferView::SELECT|BufferView::FITCUR);
398 text->pasteSelection(this);
399 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
401 // clear the selection
403 text->clearSelection();
404 update(text, BufferView::SELECT|BufferView::FITCUR);
408 /* these functions are for the spellchecker */
409 string const BufferView::nextWord(float & value)
416 return text->selectNextWordToSpellcheck(this, value);
420 void BufferView::selectLastWord()
422 if (!available()) return;
424 LyXCursor cur = text->selection.cursor;
427 text->selection.cursor = cur;
428 text->selectSelectedWord(this);
429 toggleSelection(false);
430 update(text, BufferView::SELECT|BufferView::FITCUR);
434 void BufferView::endOfSpellCheck()
436 if (!available()) return;
440 text->selectSelectedWord(this);
441 text->clearSelection();
442 update(text, BufferView::SELECT|BufferView::FITCUR);
446 void BufferView::replaceWord(string const & replacestring)
448 if (!available()) return;
450 LyXText * tt = getLyXText();
452 update(tt, BufferView::SELECT|BufferView::FITCUR);
454 /* clear the selection (if there is any) */
455 toggleSelection(false);
456 update(tt, BufferView::SELECT|BufferView::FITCUR);
458 /* clear the selection (if there is any) */
459 toggleSelection(false);
460 tt->replaceSelectionWithString(this, replacestring);
462 tt->setSelectionOverString(this, replacestring);
464 // Go back so that replacement string is also spellchecked
465 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
466 tt->cursorLeft(this);
468 update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
470 // End of spellchecker stuff
473 bool BufferView::lockInset(UpdatableInset * inset)
477 // don't relock if we're already locked
478 if (theLockingInset() == inset)
480 if (!theLockingInset()) {
481 // first check if it's the inset under the cursor we want lock
482 // should be most of the time
483 char const c = text->cursor.par()->getChar(text->cursor.pos());
484 if (c == Paragraph::META_INSET) {
485 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
487 theLockingInset(inset);
491 // Then do a deep look of the inset and lock the right one
492 Paragraph * par = buffer()->paragraph;
493 int const id = inset->id();
495 Paragraph::inset_iterator it =
496 par->inset_iterator_begin();
497 Paragraph::inset_iterator const end =
498 par->inset_iterator_end();
499 for (; it != end; ++it) {
500 if ((*it) == inset) {
501 text->setCursorIntern(this, par, it.getPos());
502 theLockingInset(inset);
505 if ((*it)->getInsetFromID(id)) {
506 text->setCursorIntern(this, par, it.getPos());
507 theLockingInset(static_cast<UpdatableInset *>(*it));
508 return theLockingInset()->lockInsetInInset(this, inset);
515 return theLockingInset()->lockInsetInInset(this, inset);
519 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
521 if (available() && theLockingInset() && !theLockingInset()->nodraw()) {
522 LyXCursor cursor = text->cursor;
523 Inset * locking_inset = theLockingInset()->getLockingInset();
525 if ((cursor.pos() - 1 >= 0) &&
526 cursor.par()->isInset(cursor.pos() - 1) &&
527 (cursor.par()->getInset(cursor.pos() - 1) ==
529 text->setCursor(this, cursor,
530 cursor.par(), cursor.pos() - 1);
531 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
532 LyXText * txt = getLyXText();
533 if (locking_inset->isTextInset() &&
534 locking_inset->lyxCode() != Inset::ERT_CODE &&
535 (txt->real_current_font.language() !=
536 buffer()->params.language
537 || txt->real_current_font.isVisibleRightToLeft()
538 != buffer()->params.language->RightToLeft()))
539 shape = (txt->real_current_font.isVisibleRightToLeft())
540 ? LyXScreen::REVERSED_L_SHAPE
541 : LyXScreen::L_SHAPE;
542 y += cursor.y() + theLockingInset()->insetInInsetY();
543 pimpl_->screen_->showManualCursor(text, x, y, asc, desc,
549 void BufferView::hideLockedInsetCursor()
551 if (theLockingInset() && available()) {
552 pimpl_->screen_->hideCursor();
557 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
559 if (theLockingInset() && available()) {
560 y += text->cursor.y() + theLockingInset()->insetInInsetY();
561 if (pimpl_->screen_->fitManualCursor(text, this, x, y, asc, desc))
567 int BufferView::unlockInset(UpdatableInset * inset)
571 if (inset && theLockingInset() == inset) {
572 inset->insetUnlock(this);
574 // make sure we update the combo !
575 owner()->setLayout(getLyXText()->cursor.par()->getLayout());
578 } else if (inset && theLockingInset() &&
579 theLockingInset()->unlockInsetInInset(this, inset)) {
580 // owner inset has updated the layout combo
584 return bufferlist.unlockInset(inset);
588 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
590 if (!theLockingInset())
591 return; // shouldn't happen
592 if (kind == Undo::EDIT) // in this case insets would not be stored!
596 text->cursor.par()->next());
600 void BufferView::updateInset(Inset * inset, bool mark_dirty)
602 pimpl_->updateInset(inset, mark_dirty);
606 bool BufferView::ChangeInsets(Inset::Code code,
607 string const & from, string const & to)
609 bool need_update = false;
610 LyXCursor cursor = text->cursor;
611 LyXCursor tmpcursor = cursor;
612 cursor.par(tmpcursor.par());
613 cursor.pos(tmpcursor.pos());
615 ParIterator end = buffer()->par_iterator_end();
616 for (ParIterator it = buffer()->par_iterator_begin();
618 Paragraph * par = *it;
619 bool changed_inset = false;
620 for (Paragraph::inset_iterator it2 = par->inset_iterator_begin();
621 it2 != par->inset_iterator_end(); ++it2) {
622 if ((*it2)->lyxCode() == code) {
623 InsetCommand * inset = static_cast<InsetCommand *>(*it2);
624 if (inset->getContents() == from) {
625 inset->setContents(to);
626 changed_inset = true;
635 // The test it.size()==1 was needed to prevent crashes.
636 // How to set the cursor corretly when it.size()>1 ??
637 if (it.size() == 1) {
638 text->setCursorIntern(this, par, 0);
639 text->redoParagraphs(this, text->cursor,
640 text->cursor.par()->next());
641 text->fullRebreak(this);
645 text->setCursorIntern(this, cursor.par(), cursor.pos());
650 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
652 // Check if the label 'from' appears more than once
653 vector<string> labels = buffer()->getLabelList();
654 // count is broken on some systems, so use the HP version
656 count(labels.begin(), labels.end(), from, res);
660 return ChangeInsets(Inset::REF_CODE, from, to);
664 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
667 vector<pair<string,string> > keys = buffer()->getBibkeyList();
668 if (count_if(keys.begin(), keys.end(),
669 lyx::equal_1st_in_pair<string,string>(from))
673 return ChangeInsets(Inset::CITE_CODE, from, to);
677 UpdatableInset * BufferView::theLockingInset() const
679 // If NULL is not allowed we should put an Assert here. (Lgb)
681 return text->the_locking_inset;
686 void BufferView::theLockingInset(UpdatableInset * inset)
688 text->the_locking_inset = inset;
692 LyXText * BufferView::getLyXText() const
694 if (theLockingInset()) {
695 LyXText * txt = theLockingInset()->getLyXText(this, true);
703 LyXText * BufferView::getParentText(Inset * inset) const
705 if (inset->owner()) {
706 LyXText * txt = inset->getLyXText(this);
707 inset = inset->owner();
708 while (inset && inset->getLyXText(this) == txt)
709 inset = inset->owner();
711 return inset->getLyXText(this);
717 Language const * BufferView::getParentLanguage(Inset * inset) const
719 LyXText * text = getParentText(inset);
720 return text->cursor.par()->getFontSettings(buffer()->params,
721 text->cursor.pos()).language();