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 Paragraph * par = buffer()->paragraph;
107 LyXCursor tmpcursor = text->cursor;
113 // this has to be done before the delete
114 text->setCursor(this, cursor, par, 0);
115 if (par->autoDeleteInsets()){
117 text->redoParagraphs(this, cursor,
118 cursor.par()->next());
119 text->fullRebreak(this);
124 // avoid forbidden cursor positions caused by error removing
125 if (tmpcursor.pos() > tmpcursor.par()->size())
126 tmpcursor.pos(tmpcursor.par()->size());
128 text->setCursorIntern(this, tmpcursor.par(), tmpcursor.pos());
134 void BufferView::insertErrors(TeXErrors & terr)
136 // Save the cursor position
137 LyXCursor cursor = text->cursor;
139 for (TeXErrors::Errors::const_iterator cit = terr.begin();
142 string const desctext(cit->error_desc);
143 string const errortext(cit->error_text);
144 string const msgtxt = desctext + '\n' + errortext;
145 int const errorrow = cit->error_in_line;
147 // Insert error string for row number
151 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
152 buffer()->texrow.increasePos(tmpid, tmppos);
155 Paragraph * texrowpar = 0;
158 texrowpar = text->firstParagraph();
161 texrowpar = buffer()->getParFromID(tmpid);
167 InsetError * new_inset = new InsetError(msgtxt);
168 text->setCursorIntern(this, texrowpar, tmppos);
169 text->insertInset(this, new_inset);
170 text->fullRebreak(this);
172 // Restore the cursor position
173 text->setCursorIntern(this, cursor.par(), cursor.pos());
177 void BufferView::setCursorFromRow(int row)
182 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
184 Paragraph * texrowpar;
187 texrowpar = text->firstParagraph();
190 texrowpar = buffer()->getParFromID(tmpid);
192 text->setCursor(this, texrowpar, tmppos);
196 bool BufferView::insertInset(Inset * inset, string const & lout)
198 return pimpl_->insertInset(inset, lout);
202 /* This is also a buffer property (ale) */
203 // Not so sure about that. a goto Label function can not be buffer local, just
204 // think how this will work in a multiwindo/buffer environment, all the
205 // cursors in all the views showing this buffer will move. (Lgb)
206 // OK, then no cursor action should be allowed in buffer. (ale)
207 bool BufferView::gotoLabel(string const & label)
210 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
211 it != buffer()->inset_iterator_end(); ++it) {
212 vector<string> labels = (*it)->getLabelList();
213 if (find(labels.begin(),labels.end(),label)
216 text->setCursor(this, it.getPar(), it.getPos());
217 text->selection.cursor = text->cursor;
218 update(text, BufferView::SELECT|BufferView::FITCUR);
226 void BufferView::menuUndo()
229 owner()->message(_("Undo"));
232 update(text, BufferView::SELECT|BufferView::FITCUR);
234 owner()->message(_("No further undo information"));
236 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
242 void BufferView::menuRedo()
244 if (theLockingInset()) {
245 owner()->message(_("Redo not yet supported in math mode"));
250 owner()->message(_("Redo"));
253 update(text, BufferView::SELECT|BufferView::FITCUR);
255 owner()->message(_("No further redo information"));
257 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
263 void BufferView::copyEnvironment()
266 text->copyEnvironmentType();
267 owner()->message(_("Paragraph environment type copied"));
272 void BufferView::pasteEnvironment()
275 text->pasteEnvironmentType(this);
276 owner()->message(_("Paragraph environment type set"));
277 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
282 void BufferView::copy()
285 text->copySelection(this);
286 owner()->message(_("Copy"));
291 void BufferView::cut(bool realcut)
295 update(text, BufferView::SELECT|BufferView::FITCUR);
296 text->cutSelection(this, true, realcut);
297 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
298 owner()->message(_("Cut"));
303 void BufferView::paste()
305 if (!available()) return;
307 owner()->message(_("Paste"));
310 // clear the selection
312 text->clearSelection();
313 update(text, BufferView::SELECT|BufferView::FITCUR);
316 text->pasteSelection(this);
317 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
319 // clear the selection
321 text->clearSelection();
322 update(text, BufferView::SELECT|BufferView::FITCUR);
326 /* these functions are for the spellchecker */
327 string const BufferView::nextWord(float & value)
334 return text->selectNextWordToSpellcheck(this, value);
338 void BufferView::selectLastWord()
340 if (!available()) return;
342 LyXCursor cur = text->selection.cursor;
345 text->selection.cursor = cur;
346 text->selectSelectedWord(this);
347 toggleSelection(false);
348 update(text, BufferView::SELECT|BufferView::FITCUR);
352 void BufferView::endOfSpellCheck()
354 if (!available()) return;
358 text->selectSelectedWord(this);
359 text->clearSelection();
360 update(text, BufferView::SELECT|BufferView::FITCUR);
364 void BufferView::replaceWord(string const & replacestring)
366 if (!available()) return;
368 LyXText * tt = getLyXText();
370 update(tt, BufferView::SELECT|BufferView::FITCUR);
372 /* clear the selection (if there is any) */
373 toggleSelection(false);
374 update(tt, BufferView::SELECT|BufferView::FITCUR);
376 /* clear the selection (if there is any) */
377 toggleSelection(false);
378 tt->replaceSelectionWithString(this, replacestring);
380 tt->setSelectionOverString(this, replacestring);
382 // Go back so that replacement string is also spellchecked
383 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
384 tt->cursorLeft(this);
386 update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
388 // End of spellchecker stuff
391 bool BufferView::lockInset(UpdatableInset * inset)
393 if (!theLockingInset() && inset) {
394 theLockingInset(inset);
397 return theLockingInset()->lockInsetInInset(this, inset);
403 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
405 if (available() && theLockingInset()) {
406 LyXCursor cursor = text->cursor;
407 Inset * locking_inset = theLockingInset()->getLockingInset();
409 if ((cursor.pos() - 1 >= 0) &&
410 cursor.par()->isInset(cursor.pos() - 1) &&
411 (cursor.par()->getInset(cursor.pos() - 1) ==
413 text->setCursor(this, cursor,
414 cursor.par(), cursor.pos() - 1);
415 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
416 LyXText * txt = getLyXText();
417 if (locking_inset->isTextInset() &&
418 locking_inset->lyxCode() != Inset::ERT_CODE &&
419 (txt->real_current_font.language() !=
420 buffer()->params.language
421 || txt->real_current_font.isVisibleRightToLeft()
422 != buffer()->params.language->RightToLeft()))
423 shape = (txt->real_current_font.isVisibleRightToLeft())
424 ? LyXScreen::REVERSED_L_SHAPE
425 : LyXScreen::L_SHAPE;
426 y += cursor.y() + theLockingInset()->insetInInsetY();
427 pimpl_->screen_->showManualCursor(text, x, y, asc, desc,
433 void BufferView::hideLockedInsetCursor()
435 if (theLockingInset() && available()) {
436 pimpl_->screen_->hideCursor();
441 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
443 if (theLockingInset() && available()) {
444 y += text->cursor.y() + theLockingInset()->insetInInsetY();
445 if (pimpl_->screen_->fitManualCursor(text, this, x, y, asc, desc))
451 int BufferView::unlockInset(UpdatableInset * inset)
453 if (inset && theLockingInset() == inset) {
454 inset->insetUnlock(this);
458 } else if (inset && theLockingInset() &&
459 theLockingInset()->unlockInsetInInset(this, inset)) {
463 return bufferlist.unlockInset(inset);
467 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
469 if (!theLockingInset())
470 return; // shouldn't happen
471 if (kind == Undo::EDIT) // in this case insets would not be stored!
475 text->cursor.par()->next());
479 void BufferView::updateInset(Inset * inset, bool mark_dirty)
481 pimpl_->updateInset(inset, mark_dirty);
485 bool BufferView::ChangeInsets(Inset::Code code,
486 string const & from, string const & to)
489 LyXCursor cursor = text->cursor;
490 LyXCursor tmpcursor = cursor;
491 cursor.par(tmpcursor.par());
492 cursor.pos(tmpcursor.pos());
494 ParIterator end = buffer()->par_iterator_end();
495 for (ParIterator it = buffer()->par_iterator_begin();
497 Paragraph * par = *it;
499 for (Paragraph::inset_iterator it2 = par->inset_iterator_begin();
500 it2 != par->inset_iterator_end(); ++it2) {
501 if ((*it2)->lyxCode() == code) {
502 InsetCommand * inset = static_cast<InsetCommand *>(*it2);
503 if (inset->getContents() == from) {
504 inset->setContents(to);
512 // The test it.size()==1 was needed to prevent crashes.
513 // How to set the cursor corretly when it.size()>1 ??
514 if (it.size() == 1) {
515 text->setCursorIntern(this, par, 0);
516 text->redoParagraphs(this, text->cursor,
517 text->cursor.par()->next());
518 text->fullRebreak(this);
522 text->setCursorIntern(this, cursor.par(), cursor.pos());
527 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
529 // Check if the label 'from' appears more than once
530 vector<string> labels = buffer()->getLabelList();
531 if (count(labels.begin(), labels.end(), from) > 1)
534 return ChangeInsets(Inset::REF_CODE, from, to);
538 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
541 vector<pair<string,string> > keys = buffer()->getBibkeyList();
542 if (count_if(keys.begin(), keys.end(),
543 lyx::equal_1st_in_pair<string,string>(from))
547 return ChangeInsets(Inset::CITE_CODE, from, to);
551 UpdatableInset * BufferView::theLockingInset() const
553 // If NULL is not allowed we should put an Assert here. (Lgb)
555 return text->the_locking_inset;
560 void BufferView::theLockingInset(UpdatableInset * inset)
562 text->the_locking_inset = inset;
566 LyXText * BufferView::getLyXText() const
568 if (theLockingInset()) {
569 LyXText * txt = theLockingInset()->getLyXText(this, true);
577 LyXText * BufferView::getParentText(Inset * inset) const
579 if (inset->owner()) {
580 LyXText * txt = inset->getLyXText(this);
581 inset = inset->owner();
582 while (inset && inset->getLyXText(this) == txt)
583 inset = inset->owner();
585 return inset->getLyXText(this);
591 Language const * BufferView::getParentLanguage(Inset * inset) const
593 LyXText * text = getParentText(inset);
594 return text->cursor.par()->getFontSettings(buffer()->params,
595 text->cursor.pos()).language();