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 * ====================================================== */
16 #include "BufferView.h"
18 #include "lyxcursor.h"
20 #include "insets/inseterror.h"
22 #include "bufferlist.h"
23 #include "support/FileInfo.h"
24 #include "lyxscreen.h"
25 #include "support/filetools.h"
26 #include "frontends/Alert.h"
28 #include "BufferView_pimpl.h"
29 #include "insets/insetcommand.h" //ChangeRefs
30 #include "support/lyxfunctional.h" //equal_1st_in_pair
33 #include "undo_funcs.h"
35 #include "iterators.h"
37 extern BufferList bufferlist;
48 // Inserts a file into current document
49 bool BufferView::insertLyXFile(string const & filen)
51 // Copyright CHT Software Service GmbH
54 // Insert a Lyxformat - file into current buffer
56 // Moved from lyx_cb.C (Lgb)
58 if (filen.empty()) return false;
60 string const fname = MakeAbsPath(filen);
62 // check if file exist
63 FileInfo const fi(fname);
66 Alert::alert(_("Error!"),
67 _("Specified file is unreadable: "),
68 MakeDisplayPath(fname, 50));
74 ifstream ifs(fname.c_str());
76 Alert::alert(_("Error!"),
77 _("Cannot open specified file: "),
78 MakeDisplayPath(fname, 50));
82 int const c = ifs.peek();
90 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
91 res = buffer()->readFile(lex, text->cursor.par());
93 lyxerr[Debug::INFO] << "Will insert file without header"
95 res = buffer()->readLyXformat2(lex, text->cursor.par());
103 bool BufferView::removeAutoInsets()
105 LyXCursor tmpcursor = text->cursor;
109 ParIterator end = buffer()->par_iterator_end();
110 for (ParIterator it = buffer()->par_iterator_begin();
112 Paragraph * par = *it;
113 // this has to be done before the delete
114 if (par->autoDeleteInsets()) {
119 // The test it.size()==1 was needed to prevent crashes.
120 if (it.size() == 1) {
121 text->setCursor(this, cursor, par, 0);
122 text->redoParagraphs(this, cursor,
123 cursor.par()->next());
124 text->fullRebreak(this);
129 // avoid forbidden cursor positions caused by error removing
130 if (tmpcursor.pos() > tmpcursor.par()->size())
131 tmpcursor.pos(tmpcursor.par()->size());
133 text->setCursorIntern(this, tmpcursor.par(), tmpcursor.pos());
139 void BufferView::insertErrors(TeXErrors & terr)
141 // Save the cursor position
142 LyXCursor cursor = text->cursor;
144 for (TeXErrors::Errors::const_iterator cit = terr.begin();
147 string const desctext(cit->error_desc);
148 string const errortext(cit->error_text);
149 string const msgtxt = desctext + '\n' + errortext;
150 int const errorrow = cit->error_in_line;
152 // Insert error string for row number
156 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
157 buffer()->texrow.increasePos(tmpid, tmppos);
160 Paragraph * texrowpar = 0;
163 texrowpar = text->firstParagraph();
166 texrowpar = buffer()->getParFromID(tmpid);
172 InsetError * new_inset = new InsetError(msgtxt);
173 text->setCursorIntern(this, texrowpar, tmppos);
174 text->insertInset(this, new_inset);
175 text->fullRebreak(this);
177 // Restore the cursor position
178 text->setCursorIntern(this, cursor.par(), cursor.pos());
182 void BufferView::setCursorFromRow(int row)
187 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
189 Paragraph * texrowpar;
192 texrowpar = text->firstParagraph();
195 texrowpar = buffer()->getParFromID(tmpid);
197 text->setCursor(this, texrowpar, tmppos);
201 bool BufferView::insertInset(Inset * inset, string const & lout)
203 return pimpl_->insertInset(inset, lout);
207 /* This is also a buffer property (ale) */
208 // Not so sure about that. a goto Label function can not be buffer local, just
209 // think how this will work in a multiwindo/buffer environment, all the
210 // cursors in all the views showing this buffer will move. (Lgb)
211 // OK, then no cursor action should be allowed in buffer. (ale)
212 bool BufferView::gotoLabel(string const & label)
215 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
216 it != buffer()->inset_iterator_end(); ++it) {
217 vector<string> labels = (*it)->getLabelList();
218 if (find(labels.begin(),labels.end(),label)
221 text->setCursor(this, it.getPar(), it.getPos());
222 text->selection.cursor = text->cursor;
223 update(text, BufferView::SELECT|BufferView::FITCUR);
231 void BufferView::menuUndo()
234 owner()->message(_("Undo"));
237 update(text, BufferView::SELECT|BufferView::FITCUR);
239 owner()->message(_("No further undo information"));
241 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
247 void BufferView::menuRedo()
249 if (theLockingInset()) {
250 owner()->message(_("Redo not yet supported in math mode"));
255 owner()->message(_("Redo"));
258 update(text, BufferView::SELECT|BufferView::FITCUR);
260 owner()->message(_("No further redo information"));
262 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
268 void BufferView::copyEnvironment()
271 text->copyEnvironmentType();
272 owner()->message(_("Paragraph environment type copied"));
277 void BufferView::pasteEnvironment()
280 text->pasteEnvironmentType(this);
281 owner()->message(_("Paragraph environment type set"));
282 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
287 void BufferView::copy()
290 text->copySelection(this);
291 owner()->message(_("Copy"));
296 void BufferView::cut(bool realcut)
300 update(text, BufferView::SELECT|BufferView::FITCUR);
301 text->cutSelection(this, true, realcut);
302 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
303 owner()->message(_("Cut"));
308 void BufferView::paste()
310 if (!available()) return;
312 owner()->message(_("Paste"));
315 // clear the selection
317 text->clearSelection();
318 update(text, BufferView::SELECT|BufferView::FITCUR);
321 text->pasteSelection(this);
322 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
324 // clear the selection
326 text->clearSelection();
327 update(text, BufferView::SELECT|BufferView::FITCUR);
331 /* these functions are for the spellchecker */
332 string const BufferView::nextWord(float & value)
339 return text->selectNextWordToSpellcheck(this, value);
343 void BufferView::selectLastWord()
345 if (!available()) return;
347 LyXCursor cur = text->selection.cursor;
350 text->selection.cursor = cur;
351 text->selectSelectedWord(this);
352 toggleSelection(false);
353 update(text, BufferView::SELECT|BufferView::FITCUR);
357 void BufferView::endOfSpellCheck()
359 if (!available()) return;
363 text->selectSelectedWord(this);
364 text->clearSelection();
365 update(text, BufferView::SELECT|BufferView::FITCUR);
369 void BufferView::replaceWord(string const & replacestring)
371 if (!available()) return;
373 LyXText * tt = getLyXText();
375 update(tt, BufferView::SELECT|BufferView::FITCUR);
377 /* clear the selection (if there is any) */
378 toggleSelection(false);
379 update(tt, BufferView::SELECT|BufferView::FITCUR);
381 /* clear the selection (if there is any) */
382 toggleSelection(false);
383 tt->replaceSelectionWithString(this, replacestring);
385 tt->setSelectionOverString(this, replacestring);
387 // Go back so that replacement string is also spellchecked
388 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
389 tt->cursorLeft(this);
391 update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
393 // End of spellchecker stuff
396 bool BufferView::lockInset(UpdatableInset * inset)
398 // don't relock if we're already locked
399 if (theLockingInset() == inset)
401 if (!theLockingInset() && inset) {
402 theLockingInset(inset);
405 return theLockingInset()->lockInsetInInset(this, inset);
411 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
413 if (available() && theLockingInset()) {
414 LyXCursor cursor = text->cursor;
415 Inset * locking_inset = theLockingInset()->getLockingInset();
417 if ((cursor.pos() - 1 >= 0) &&
418 cursor.par()->isInset(cursor.pos() - 1) &&
419 (cursor.par()->getInset(cursor.pos() - 1) ==
421 text->setCursor(this, cursor,
422 cursor.par(), cursor.pos() - 1);
423 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
424 LyXText * txt = getLyXText();
425 if (locking_inset->isTextInset() &&
426 locking_inset->lyxCode() != Inset::ERT_CODE &&
427 (txt->real_current_font.language() !=
428 buffer()->params.language
429 || txt->real_current_font.isVisibleRightToLeft()
430 != buffer()->params.language->RightToLeft()))
431 shape = (txt->real_current_font.isVisibleRightToLeft())
432 ? LyXScreen::REVERSED_L_SHAPE
433 : LyXScreen::L_SHAPE;
434 y += cursor.y() + theLockingInset()->insetInInsetY();
435 pimpl_->screen_->showManualCursor(text, x, y, asc, desc,
441 void BufferView::hideLockedInsetCursor()
443 if (theLockingInset() && available()) {
444 pimpl_->screen_->hideCursor();
449 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
451 if (theLockingInset() && available()) {
452 y += text->cursor.y() + theLockingInset()->insetInInsetY();
453 if (pimpl_->screen_->fitManualCursor(text, this, x, y, asc, desc))
459 int BufferView::unlockInset(UpdatableInset * inset)
461 if (inset && theLockingInset() == inset) {
462 inset->insetUnlock(this);
464 // make sure we update the combo !
465 owner()->setLayout(getLyXText()->cursor.par()->getLayout());
468 } else if (inset && theLockingInset() &&
469 theLockingInset()->unlockInsetInInset(this, inset)) {
470 // owner inset has updated the layout combo
474 return bufferlist.unlockInset(inset);
478 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
480 if (!theLockingInset())
481 return; // shouldn't happen
482 if (kind == Undo::EDIT) // in this case insets would not be stored!
486 text->cursor.par()->next());
490 void BufferView::updateInset(Inset * inset, bool mark_dirty)
492 pimpl_->updateInset(inset, mark_dirty);
496 bool BufferView::ChangeInsets(Inset::Code code,
497 string const & from, string const & to)
499 bool need_update = false;
500 LyXCursor cursor = text->cursor;
501 LyXCursor tmpcursor = cursor;
502 cursor.par(tmpcursor.par());
503 cursor.pos(tmpcursor.pos());
505 ParIterator end = buffer()->par_iterator_end();
506 for (ParIterator it = buffer()->par_iterator_begin();
508 Paragraph * par = *it;
509 bool changed_inset = false;
510 for (Paragraph::inset_iterator it2 = par->inset_iterator_begin();
511 it2 != par->inset_iterator_end(); ++it2) {
512 if ((*it2)->lyxCode() == code) {
513 InsetCommand * inset = static_cast<InsetCommand *>(*it2);
514 if (inset->getContents() == from) {
515 inset->setContents(to);
516 changed_inset = true;
525 // The test it.size()==1 was needed to prevent crashes.
526 // How to set the cursor corretly when it.size()>1 ??
527 if (it.size() == 1) {
528 text->setCursorIntern(this, par, 0);
529 text->redoParagraphs(this, text->cursor,
530 text->cursor.par()->next());
531 text->fullRebreak(this);
535 text->setCursorIntern(this, cursor.par(), cursor.pos());
540 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
542 // Check if the label 'from' appears more than once
543 vector<string> labels = buffer()->getLabelList();
544 if (count(labels.begin(), labels.end(), from) > 1)
547 return ChangeInsets(Inset::REF_CODE, from, to);
551 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
554 vector<pair<string,string> > keys = buffer()->getBibkeyList();
555 if (count_if(keys.begin(), keys.end(),
556 lyx::equal_1st_in_pair<string,string>(from))
560 return ChangeInsets(Inset::CITE_CODE, from, to);
564 UpdatableInset * BufferView::theLockingInset() const
566 // If NULL is not allowed we should put an Assert here. (Lgb)
568 return text->the_locking_inset;
573 void BufferView::theLockingInset(UpdatableInset * inset)
575 text->the_locking_inset = inset;
579 LyXText * BufferView::getLyXText() const
581 if (theLockingInset()) {
582 LyXText * txt = theLockingInset()->getLyXText(this, true);
590 LyXText * BufferView::getParentText(Inset * inset) const
592 if (inset->owner()) {
593 LyXText * txt = inset->getLyXText(this);
594 inset = inset->owner();
595 while (inset && inset->getLyXText(this) == txt)
596 inset = inset->owner();
598 return inset->getLyXText(this);
604 Language const * BufferView::getParentLanguage(Inset * inset) const
606 LyXText * text = getParentText(inset);
607 return text->cursor.par()->getFontSettings(buffer()->params,
608 text->cursor.pos()).language();