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 pos_type cur_pos = tmpcursor.pos();
117 ParIterator it = buffer()->par_iterator_begin();
118 ParIterator end = buffer()->par_iterator_end();
119 for (; it != end; ++it) {
120 Paragraph * par = *it;
121 bool removed = false;
123 text->setCursor(this, par, 0);
125 Paragraph::inset_iterator pit = par->inset_iterator_begin();
126 Paragraph::inset_iterator pend = par->inset_iterator_end();
127 while (pit != pend) {
128 if (pit->autoDelete()) {
130 pos_type const pos = pit.getPos();
133 if (cur_par == par) {
143 text->redoParagraph(this);
147 text->setCursorIntern(this, cur_par, cur_pos);
153 void BufferView::insertErrors(TeXErrors & terr)
155 // Save the cursor position
156 LyXCursor cursor = text->cursor;
158 TeXErrors::Errors::const_iterator cit = terr.begin();
159 TeXErrors::Errors::const_iterator end = terr.end();
160 for (; cit != end; ++cit) {
161 string const desctext(cit->error_desc);
162 string const errortext(cit->error_text);
163 string const msgtxt = desctext + '\n' + errortext;
164 int const errorrow = cit->error_in_line;
166 // Insert error string for row number
170 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
171 buffer()->texrow.increasePos(tmpid, tmppos);
174 Paragraph * texrowpar = 0;
177 texrowpar = text->firstParagraph();
180 texrowpar = buffer()->getParFromID(tmpid);
186 InsetError * new_inset = new InsetError(msgtxt);
187 text->setCursorIntern(this, texrowpar, tmppos);
188 text->insertInset(this, new_inset);
189 text->fullRebreak(this);
191 // Restore the cursor position
192 text->setCursorIntern(this, cursor.par(), cursor.pos());
196 void BufferView::setCursorFromRow(int row)
201 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
203 Paragraph * texrowpar;
206 texrowpar = text->firstParagraph();
209 texrowpar = buffer()->getParFromID(tmpid);
211 text->setCursor(this, texrowpar, tmppos);
215 bool BufferView::insertInset(Inset * inset, string const & lout)
217 return pimpl_->insertInset(inset, lout);
221 /* This is also a buffer property (ale) */
222 // Not so sure about that. a goto Label function can not be buffer local, just
223 // think how this will work in a multiwindo/buffer environment, all the
224 // cursors in all the views showing this buffer will move. (Lgb)
225 // OK, then no cursor action should be allowed in buffer. (ale)
226 bool BufferView::gotoLabel(string const & label)
229 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
230 it != buffer()->inset_iterator_end(); ++it) {
231 vector<string> labels = (*it)->getLabelList();
232 if (find(labels.begin(),labels.end(),label)
235 text->setCursor(this, it.getPar(), it.getPos());
236 text->selection.cursor = text->cursor;
237 update(text, BufferView::SELECT|BufferView::FITCUR);
245 void BufferView::menuUndo()
248 owner()->message(_("Undo"));
251 update(text, BufferView::SELECT|BufferView::FITCUR);
253 owner()->message(_("No further undo information"));
255 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
261 void BufferView::menuRedo()
263 #if 0 // this should not be here (Jug 20011206)
264 if (theLockingInset()) {
265 owner()->message(_("Redo not yet supported in math mode"));
271 owner()->message(_("Redo"));
274 update(text, BufferView::SELECT|BufferView::FITCUR);
276 owner()->message(_("No further redo information"));
278 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
284 void BufferView::copyEnvironment()
287 text->copyEnvironmentType();
288 owner()->message(_("Paragraph environment type copied"));
293 void BufferView::pasteEnvironment()
296 text->pasteEnvironmentType(this);
297 owner()->message(_("Paragraph environment type set"));
298 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
303 void BufferView::copy()
306 text->copySelection(this);
307 owner()->message(_("Copy"));
312 void BufferView::cut(bool realcut)
316 update(text, BufferView::SELECT|BufferView::FITCUR);
317 text->cutSelection(this, true, realcut);
318 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
319 owner()->message(_("Cut"));
324 void BufferView::paste()
326 if (!available()) return;
328 owner()->message(_("Paste"));
331 // clear the selection
333 text->clearSelection();
334 update(text, BufferView::SELECT|BufferView::FITCUR);
337 text->pasteSelection(this);
338 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
340 // clear the selection
342 text->clearSelection();
343 update(text, BufferView::SELECT|BufferView::FITCUR);
347 /* these functions are for the spellchecker */
348 string const BufferView::nextWord(float & value)
355 return text->selectNextWordToSpellcheck(this, value);
359 void BufferView::selectLastWord()
361 if (!available()) return;
363 LyXCursor cur = text->selection.cursor;
366 text->selection.cursor = cur;
367 text->selectSelectedWord(this);
368 toggleSelection(false);
369 update(text, BufferView::SELECT|BufferView::FITCUR);
373 void BufferView::endOfSpellCheck()
375 if (!available()) return;
379 text->selectSelectedWord(this);
380 text->clearSelection();
381 update(text, BufferView::SELECT|BufferView::FITCUR);
385 void BufferView::replaceWord(string const & replacestring)
387 if (!available()) return;
389 LyXText * tt = getLyXText();
391 update(tt, BufferView::SELECT|BufferView::FITCUR);
393 /* clear the selection (if there is any) */
394 toggleSelection(false);
395 update(tt, BufferView::SELECT|BufferView::FITCUR);
397 /* clear the selection (if there is any) */
398 toggleSelection(false);
399 tt->replaceSelectionWithString(this, replacestring);
401 tt->setSelectionOverString(this, replacestring);
403 // Go back so that replacement string is also spellchecked
404 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
405 tt->cursorLeft(this);
407 update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
409 // End of spellchecker stuff
412 bool BufferView::lockInset(UpdatableInset * inset)
416 // don't relock if we're already locked
417 if (theLockingInset() == inset)
419 if (!theLockingInset()) {
420 // first check if it's the inset under the cursor we want lock
421 // should be most of the time
422 char const c = text->cursor.par()->getChar(text->cursor.pos());
423 if (c == Paragraph::META_INSET) {
424 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
426 theLockingInset(inset);
430 // Then do a deep look of the inset and lock the right one
431 Paragraph * par = buffer()->paragraph;
432 int const id = inset->id();
434 Paragraph::inset_iterator it =
435 par->inset_iterator_begin();
436 Paragraph::inset_iterator const end =
437 par->inset_iterator_end();
438 for (; it != end; ++it) {
439 if ((*it) == inset) {
440 text->setCursorIntern(this, par, it.getPos());
441 theLockingInset(inset);
444 if ((*it)->getInsetFromID(id)) {
445 text->setCursorIntern(this, par, it.getPos());
446 theLockingInset(static_cast<UpdatableInset *>(*it));
447 return theLockingInset()->lockInsetInInset(this, inset);
454 return theLockingInset()->lockInsetInInset(this, inset);
458 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
460 if (available() && theLockingInset() && !theLockingInset()->nodraw()) {
461 LyXCursor cursor = text->cursor;
462 Inset * locking_inset = theLockingInset()->getLockingInset();
464 if ((cursor.pos() - 1 >= 0) &&
465 cursor.par()->isInset(cursor.pos() - 1) &&
466 (cursor.par()->getInset(cursor.pos() - 1) ==
468 text->setCursor(this, cursor,
469 cursor.par(), cursor.pos() - 1);
470 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
471 LyXText * txt = getLyXText();
472 if (locking_inset->isTextInset() &&
473 locking_inset->lyxCode() != Inset::ERT_CODE &&
474 (txt->real_current_font.language() !=
475 buffer()->params.language
476 || txt->real_current_font.isVisibleRightToLeft()
477 != buffer()->params.language->RightToLeft()))
478 shape = (txt->real_current_font.isVisibleRightToLeft())
479 ? LyXScreen::REVERSED_L_SHAPE
480 : LyXScreen::L_SHAPE;
481 y += cursor.y() + theLockingInset()->insetInInsetY();
482 pimpl_->screen_->showManualCursor(text, x, y, asc, desc,
488 void BufferView::hideLockedInsetCursor()
490 if (theLockingInset() && available()) {
491 pimpl_->screen_->hideCursor();
496 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
498 if (theLockingInset() && available()) {
499 y += text->cursor.y() + theLockingInset()->insetInInsetY();
500 if (pimpl_->screen_->fitManualCursor(text, this, x, y, asc, desc))
506 int BufferView::unlockInset(UpdatableInset * inset)
510 if (inset && theLockingInset() == inset) {
511 inset->insetUnlock(this);
513 // make sure we update the combo !
514 owner()->setLayout(getLyXText()->cursor.par()->getLayout());
517 } else if (inset && theLockingInset() &&
518 theLockingInset()->unlockInsetInInset(this, inset)) {
519 // owner inset has updated the layout combo
523 return bufferlist.unlockInset(inset);
527 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
529 if (!theLockingInset())
530 return; // shouldn't happen
531 if (kind == Undo::EDIT) // in this case insets would not be stored!
535 text->cursor.par()->next());
539 void BufferView::updateInset(Inset * inset, bool mark_dirty)
541 pimpl_->updateInset(inset, mark_dirty);
545 bool BufferView::ChangeInsets(Inset::Code code,
546 string const & from, string const & to)
548 bool need_update = false;
549 LyXCursor cursor = text->cursor;
550 LyXCursor tmpcursor = cursor;
551 cursor.par(tmpcursor.par());
552 cursor.pos(tmpcursor.pos());
554 ParIterator end = buffer()->par_iterator_end();
555 for (ParIterator it = buffer()->par_iterator_begin();
557 Paragraph * par = *it;
558 bool changed_inset = false;
559 for (Paragraph::inset_iterator it2 = par->inset_iterator_begin();
560 it2 != par->inset_iterator_end(); ++it2) {
561 if ((*it2)->lyxCode() == code) {
562 InsetCommand * inset = static_cast<InsetCommand *>(*it2);
563 if (inset->getContents() == from) {
564 inset->setContents(to);
565 changed_inset = true;
574 // The test it.size()==1 was needed to prevent crashes.
575 // How to set the cursor corretly when it.size()>1 ??
576 if (it.size() == 1) {
577 text->setCursorIntern(this, par, 0);
578 text->redoParagraphs(this, text->cursor,
579 text->cursor.par()->next());
580 text->fullRebreak(this);
584 text->setCursorIntern(this, cursor.par(), cursor.pos());
589 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
591 // Check if the label 'from' appears more than once
592 vector<string> labels = buffer()->getLabelList();
593 if (count(labels.begin(), labels.end(), from) > 1)
596 return ChangeInsets(Inset::REF_CODE, from, to);
600 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
603 vector<pair<string,string> > keys = buffer()->getBibkeyList();
604 if (count_if(keys.begin(), keys.end(),
605 lyx::equal_1st_in_pair<string,string>(from))
609 return ChangeInsets(Inset::CITE_CODE, from, to);
613 UpdatableInset * BufferView::theLockingInset() const
615 // If NULL is not allowed we should put an Assert here. (Lgb)
617 return text->the_locking_inset;
622 void BufferView::theLockingInset(UpdatableInset * inset)
624 text->the_locking_inset = inset;
628 LyXText * BufferView::getLyXText() const
630 if (theLockingInset()) {
631 LyXText * txt = theLockingInset()->getLyXText(this, true);
639 LyXText * BufferView::getParentText(Inset * inset) const
641 if (inset->owner()) {
642 LyXText * txt = inset->getLyXText(this);
643 inset = inset->owner();
644 while (inset && inset->getLyXText(this) == txt)
645 inset = inset->owner();
647 return inset->getLyXText(this);
653 Language const * BufferView::getParentLanguage(Inset * inset) const
655 LyXText * text = getParentText(inset);
656 return text->cursor.par()->getFontSettings(buffer()->params,
657 text->cursor.pos()).language();