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 "lyx_gui_misc.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"
36 extern BufferList bufferlist;
47 // Inserts a file into current document
48 bool BufferView::insertLyXFile(string const & filen)
50 // Copyright CHT Software Service GmbH
53 // Insert a Lyxformat - file into current buffer
55 // Moved from lyx_cb.C (Lgb)
57 if (filen.empty()) return false;
59 string const fname = MakeAbsPath(filen);
61 // check if file exist
62 FileInfo const fi(fname);
65 WriteAlert(_("Error!"),
66 _("Specified file is unreadable: "),
67 MakeDisplayPath(fname, 50));
73 ifstream ifs(fname.c_str());
75 WriteAlert(_("Error!"),
76 _("Cannot open specified file: "),
77 MakeDisplayPath(fname, 50));
81 char const c = ifs.peek();
89 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
90 res = buffer()->readFile(lex, text->cursor.par());
92 lyxerr[Debug::INFO] << "Will insert file without header"
94 res = buffer()->readLyXformat2(lex, text->cursor.par());
102 bool BufferView::removeAutoInsets()
104 Paragraph * par = buffer()->paragraph;
106 LyXCursor tmpcursor = text->cursor;
112 // this has to be done before the delete
113 text->setCursor(this, cursor, par, 0);
114 if (par->autoDeleteInsets()){
116 text->redoParagraphs(this, cursor,
117 cursor.par()->next());
118 text->fullRebreak(this);
123 // avoid forbidden cursor positions caused by error removing
124 if (tmpcursor.pos() > tmpcursor.par()->size())
125 tmpcursor.pos(tmpcursor.par()->size());
127 text->setCursorIntern(this, tmpcursor.par(), tmpcursor.pos());
133 void BufferView::insertErrors(TeXErrors & terr)
135 // Save the cursor position
136 LyXCursor cursor = text->cursor;
138 for (TeXErrors::Errors::const_iterator cit = terr.begin();
141 string const desctext(cit->error_desc);
142 string const errortext(cit->error_text);
143 string const msgtxt = desctext + '\n' + errortext;
144 int const errorrow = cit->error_in_line;
146 // Insert error string for row number
150 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
151 buffer()->texrow.increasePos(tmpid, tmppos);
154 Paragraph * texrowpar = 0;
157 texrowpar = text->firstParagraph();
160 texrowpar = buffer()->getParFromID(tmpid);
166 InsetError * new_inset = new InsetError(msgtxt);
167 text->setCursorIntern(this, texrowpar, tmppos);
168 text->insertInset(this, new_inset);
169 text->fullRebreak(this);
171 // Restore the cursor position
172 text->setCursorIntern(this, cursor.par(), cursor.pos());
176 void BufferView::setCursorFromRow(int row)
181 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
183 Paragraph * texrowpar;
186 texrowpar = text->firstParagraph();
189 texrowpar = buffer()->getParFromID(tmpid);
191 text->setCursor(this, texrowpar, tmppos);
195 bool BufferView::insertInset(Inset * inset, string const & lout)
197 return pimpl_->insertInset(inset, lout);
201 /* This is also a buffer property (ale) */
202 // Not so sure about that. a goto Label function can not be buffer local, just
203 // think how this will work in a multiwindo/buffer environment, all the
204 // cursors in all the views showing this buffer will move. (Lgb)
205 // OK, then no cursor action should be allowed in buffer. (ale)
206 bool BufferView::gotoLabel(string const & label)
209 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
210 it != buffer()->inset_iterator_end(); ++it) {
211 vector<string> labels = (*it)->getLabelList();
212 if (find(labels.begin(),labels.end(),label)
215 text->setCursor(this, it.getPar(), it.getPos());
216 text->selection.cursor = text->cursor;
217 update(text, BufferView::SELECT|BufferView::FITCUR);
225 void BufferView::menuUndo()
228 owner()->message(_("Undo"));
231 update(text, BufferView::SELECT|BufferView::FITCUR);
233 owner()->message(_("No forther undo information"));
235 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
241 void BufferView::menuRedo()
243 if (theLockingInset()) {
244 owner()->message(_("Redo not yet supported in math mode"));
249 owner()->message(_("Redo"));
252 update(text, BufferView::SELECT|BufferView::FITCUR);
254 owner()->message(_("No further redo information"));
256 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
262 void BufferView::copyEnvironment()
265 text->copyEnvironmentType();
266 // clear the selection, even if mark_set
268 text->clearSelection();
269 update(text, BufferView::SELECT|BufferView::FITCUR);
270 owner()->message(_("Paragraph environment type copied"));
275 void BufferView::pasteEnvironment()
278 text->pasteEnvironmentType(this);
279 owner()->message(_("Paragraph environment type set"));
280 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
285 void BufferView::copy()
288 text->copySelection(this);
289 // clear the selection, even if mark_set
291 text->clearSelection();
292 update(text, BufferView::SELECT|BufferView::FITCUR);
293 owner()->message(_("Copy"));
298 void BufferView::cut(bool realcut)
302 update(text, BufferView::SELECT|BufferView::FITCUR);
303 text->cutSelection(this, true, realcut);
304 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
305 owner()->message(_("Cut"));
310 void BufferView::paste()
312 if (!available()) return;
314 owner()->message(_("Paste"));
317 // clear the selection
319 text->clearSelection();
320 update(text, BufferView::SELECT|BufferView::FITCUR);
323 text->pasteSelection(this);
324 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
326 // clear the selection
328 text->clearSelection();
329 update(text, BufferView::SELECT|BufferView::FITCUR);
333 /* these functions are for the spellchecker */
334 string const BufferView::nextWord(float & value)
341 return text->selectNextWord(this, value);
345 void BufferView::selectLastWord()
347 if (!available()) return;
349 LyXCursor cur = text->selection.cursor;
352 text->selection.cursor = cur;
353 text->selectSelectedWord(this);
354 toggleSelection(false);
355 update(text, BufferView::SELECT|BufferView::FITCUR);
359 void BufferView::endOfSpellCheck()
361 if (!available()) return;
365 text->selectSelectedWord(this);
366 text->clearSelection();
367 update(text, BufferView::SELECT|BufferView::FITCUR);
371 void BufferView::replaceWord(string const & replacestring)
373 if (!available()) return;
375 LyXText * tt = getLyXText();
377 update(tt, BufferView::SELECT|BufferView::FITCUR);
379 /* clear the selection (if there is any) */
380 toggleSelection(false);
381 update(tt, BufferView::SELECT|BufferView::FITCUR);
383 /* clear the selection (if there is any) */
384 toggleSelection(false);
385 tt->replaceSelectionWithString(this, replacestring);
387 tt->setSelectionOverString(this, replacestring);
389 // Go back so that replacement string is also spellchecked
390 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
391 tt->cursorLeft(this);
393 update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
395 // End of spellchecker stuff
398 bool BufferView::lockInset(UpdatableInset * inset)
400 if (!theLockingInset() && inset) {
401 theLockingInset(inset);
404 return theLockingInset()->lockInsetInInset(this, inset);
410 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
412 if (available() && theLockingInset()) {
413 LyXCursor cursor = text->cursor;
414 Inset * locking_inset = theLockingInset()->getLockingInset();
416 if ((cursor.pos() - 1 >= 0) &&
417 (cursor.par()->getChar(cursor.pos() - 1) ==
418 Paragraph::META_INSET) &&
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);
466 } else if (inset && theLockingInset() &&
467 theLockingInset()->unlockInsetInInset(this, inset)) {
471 return bufferlist.unlockInset(inset);
475 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
477 if (!theLockingInset())
478 return; // shouldn't happen
479 if (kind == Undo::EDIT) // in this case insets would not be stored!
483 text->cursor.par()->next());
487 void BufferView::updateInset(Inset * inset, bool mark_dirty)
489 pimpl_->updateInset(inset, mark_dirty);
493 bool BufferView::ChangeInsets(Inset::Code code,
494 string const & from, string const & to)
497 Paragraph * par = buffer()->paragraph;
498 LyXCursor cursor = text->cursor;
499 LyXCursor tmpcursor = cursor;
500 cursor.par(tmpcursor.par());
501 cursor.pos(tmpcursor.pos());
505 for (Paragraph::inset_iterator it = par->inset_iterator_begin();
506 it != par->inset_iterator_end(); ++it) {
507 if ((*it)->lyxCode() == code) {
508 InsetCommand * inset = static_cast<InsetCommand *>(*it);
509 if (inset->getContents() == from) {
510 inset->setContents(to);
517 // this is possible now, since SetCursor takes
518 // care about footnotes
519 text->setCursorIntern(this, par, 0);
520 text->redoParagraphs(this, text->cursor,
521 text->cursor.par()->next());
522 text->fullRebreak(this);
526 text->setCursorIntern(this, cursor.par(), cursor.pos());
531 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
533 // Check if the label 'from' appears more than once
534 vector<string> labels = buffer()->getLabelList();
535 if (count(labels.begin(), labels.end(), from) > 1)
538 return ChangeInsets(Inset::REF_CODE, from, to);
542 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
545 vector<pair<string,string> > keys = buffer()->getBibkeyList();
546 if (count_if(keys.begin(), keys.end(),
547 lyx::equal_1st_in_pair<string,string>(from))
551 return ChangeInsets(Inset::CITE_CODE, from, to);
555 UpdatableInset * BufferView::theLockingInset() const
557 // If NULL is not allowed we should put an Assert here. (Lgb)
559 return text->the_locking_inset;
564 void BufferView::theLockingInset(UpdatableInset * inset)
566 text->the_locking_inset = inset;
570 LyXText * BufferView::getLyXText() const
572 if (theLockingInset()) {
573 LyXText * txt = theLockingInset()->getLyXText(this, true);
581 LyXText * BufferView::getParentText(Inset * inset) const
583 if (inset->owner()) {
584 LyXText * txt = inset->getLyXText(this);
585 inset = inset->owner();
586 while (inset && inset->getLyXText(this) == txt)
587 inset = inset->owner();
589 return inset->getLyXText(this);
595 Language const * BufferView::getParentLanguage(Inset * inset) const
597 LyXText * text = getParentText(inset);
598 return text->cursor.par()->getFontSettings(buffer()->params,
599 text->cursor.pos()).language();