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 // don't relock if we're already locked
394 if (theLockingInset() == inset)
396 if (!theLockingInset() && inset) {
397 theLockingInset(inset);
400 return theLockingInset()->lockInsetInInset(this, inset);
406 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
408 if (available() && theLockingInset()) {
409 LyXCursor cursor = text->cursor;
410 Inset * locking_inset = theLockingInset()->getLockingInset();
412 if ((cursor.pos() - 1 >= 0) &&
413 cursor.par()->isInset(cursor.pos() - 1) &&
414 (cursor.par()->getInset(cursor.pos() - 1) ==
416 text->setCursor(this, cursor,
417 cursor.par(), cursor.pos() - 1);
418 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
419 LyXText * txt = getLyXText();
420 if (locking_inset->isTextInset() &&
421 locking_inset->lyxCode() != Inset::ERT_CODE &&
422 (txt->real_current_font.language() !=
423 buffer()->params.language
424 || txt->real_current_font.isVisibleRightToLeft()
425 != buffer()->params.language->RightToLeft()))
426 shape = (txt->real_current_font.isVisibleRightToLeft())
427 ? LyXScreen::REVERSED_L_SHAPE
428 : LyXScreen::L_SHAPE;
429 y += cursor.y() + theLockingInset()->insetInInsetY();
430 pimpl_->screen_->showManualCursor(text, x, y, asc, desc,
436 void BufferView::hideLockedInsetCursor()
438 if (theLockingInset() && available()) {
439 pimpl_->screen_->hideCursor();
444 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
446 if (theLockingInset() && available()) {
447 y += text->cursor.y() + theLockingInset()->insetInInsetY();
448 if (pimpl_->screen_->fitManualCursor(text, this, x, y, asc, desc))
454 int BufferView::unlockInset(UpdatableInset * inset)
456 if (inset && theLockingInset() == inset) {
457 inset->insetUnlock(this);
461 } else if (inset && theLockingInset() &&
462 theLockingInset()->unlockInsetInInset(this, inset)) {
466 return bufferlist.unlockInset(inset);
470 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
472 if (!theLockingInset())
473 return; // shouldn't happen
474 if (kind == Undo::EDIT) // in this case insets would not be stored!
478 text->cursor.par()->next());
482 void BufferView::updateInset(Inset * inset, bool mark_dirty)
484 pimpl_->updateInset(inset, mark_dirty);
488 bool BufferView::ChangeInsets(Inset::Code code,
489 string const & from, string const & to)
492 LyXCursor cursor = text->cursor;
493 LyXCursor tmpcursor = cursor;
494 cursor.par(tmpcursor.par());
495 cursor.pos(tmpcursor.pos());
497 ParIterator end = buffer()->par_iterator_end();
498 for (ParIterator it = buffer()->par_iterator_begin();
500 Paragraph * par = *it;
502 for (Paragraph::inset_iterator it2 = par->inset_iterator_begin();
503 it2 != par->inset_iterator_end(); ++it2) {
504 if ((*it2)->lyxCode() == code) {
505 InsetCommand * inset = static_cast<InsetCommand *>(*it2);
506 if (inset->getContents() == from) {
507 inset->setContents(to);
515 // The test it.size()==1 was needed to prevent crashes.
516 // How to set the cursor corretly when it.size()>1 ??
517 if (it.size() == 1) {
518 text->setCursorIntern(this, par, 0);
519 text->redoParagraphs(this, text->cursor,
520 text->cursor.par()->next());
521 text->fullRebreak(this);
525 text->setCursorIntern(this, cursor.par(), cursor.pos());
530 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
532 // Check if the label 'from' appears more than once
533 vector<string> labels = buffer()->getLabelList();
534 if (count(labels.begin(), labels.end(), from) > 1)
537 return ChangeInsets(Inset::REF_CODE, from, to);
541 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
544 vector<pair<string,string> > keys = buffer()->getBibkeyList();
545 if (count_if(keys.begin(), keys.end(),
546 lyx::equal_1st_in_pair<string,string>(from))
550 return ChangeInsets(Inset::CITE_CODE, from, to);
554 UpdatableInset * BufferView::theLockingInset() const
556 // If NULL is not allowed we should put an Assert here. (Lgb)
558 return text->the_locking_inset;
563 void BufferView::theLockingInset(UpdatableInset * inset)
565 text->the_locking_inset = inset;
569 LyXText * BufferView::getLyXText() const
571 if (theLockingInset()) {
572 LyXText * txt = theLockingInset()->getLyXText(this, true);
580 LyXText * BufferView::getParentText(Inset * inset) const
582 if (inset->owner()) {
583 LyXText * txt = inset->getLyXText(this);
584 inset = inset->owner();
585 while (inset && inset->getLyXText(this) == txt)
586 inset = inset->owner();
588 return inset->getLyXText(this);
594 Language const * BufferView::getParentLanguage(Inset * inset) const
596 LyXText * text = getParentText(inset);
597 return text->cursor.par()->getFontSettings(buffer()->params,
598 text->cursor.pos()).language();