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
40 extern BufferList bufferlist;
51 // Inserts a file into current document
52 bool BufferView::insertLyXFile(string const & filen)
54 // Copyright CHT Software Service GmbH
57 // Insert a Lyxformat - file into current buffer
59 // Moved from lyx_cb.C (Lgb)
61 if (filen.empty()) return false;
63 string const fname = MakeAbsPath(filen);
65 // check if file exist
66 FileInfo const fi(fname);
69 Alert::alert(_("Error!"),
70 _("Specified file is unreadable: "),
71 MakeDisplayPath(fname, 50));
77 ifstream ifs(fname.c_str());
79 Alert::alert(_("Error!"),
80 _("Cannot open specified file: "),
81 MakeDisplayPath(fname, 50));
85 int const c = ifs.peek();
93 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
94 res = buffer()->readFile(lex, text->cursor.par());
96 lyxerr[Debug::INFO] << "Will insert file without header"
98 res = buffer()->readLyXformat2(lex, text->cursor.par());
106 bool BufferView::removeAutoInsets()
108 LyXCursor tmpcursor = text->cursor;
112 ParIterator end = buffer()->par_iterator_end();
113 for (ParIterator it = buffer()->par_iterator_begin();
115 Paragraph * par = *it;
116 // this has to be done before the delete
117 if (par->autoDeleteInsets()) {
122 // The test it.size()==1 was needed to prevent crashes.
123 if (it.size() == 1) {
124 text->setCursor(this, cursor, par, 0);
125 text->redoParagraphs(this, cursor,
126 cursor.par()->next());
127 text->fullRebreak(this);
132 // avoid forbidden cursor positions caused by error removing
133 if (tmpcursor.pos() > tmpcursor.par()->size())
134 tmpcursor.pos(tmpcursor.par()->size());
136 text->setCursorIntern(this, tmpcursor.par(), tmpcursor.pos());
142 void BufferView::insertErrors(TeXErrors & terr)
144 // Save the cursor position
145 LyXCursor cursor = text->cursor;
147 for (TeXErrors::Errors::const_iterator cit = terr.begin();
150 string const desctext(cit->error_desc);
151 string const errortext(cit->error_text);
152 string const msgtxt = desctext + '\n' + errortext;
153 int const errorrow = cit->error_in_line;
155 // Insert error string for row number
159 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
160 buffer()->texrow.increasePos(tmpid, tmppos);
163 Paragraph * texrowpar = 0;
166 texrowpar = text->firstParagraph();
169 texrowpar = buffer()->getParFromID(tmpid);
175 InsetError * new_inset = new InsetError(msgtxt);
176 text->setCursorIntern(this, texrowpar, tmppos);
177 text->insertInset(this, new_inset);
178 text->fullRebreak(this);
180 // Restore the cursor position
181 text->setCursorIntern(this, cursor.par(), cursor.pos());
185 void BufferView::setCursorFromRow(int row)
190 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
192 Paragraph * texrowpar;
195 texrowpar = text->firstParagraph();
198 texrowpar = buffer()->getParFromID(tmpid);
200 text->setCursor(this, texrowpar, tmppos);
204 bool BufferView::insertInset(Inset * inset, string const & lout)
206 return pimpl_->insertInset(inset, lout);
210 /* This is also a buffer property (ale) */
211 // Not so sure about that. a goto Label function can not be buffer local, just
212 // think how this will work in a multiwindo/buffer environment, all the
213 // cursors in all the views showing this buffer will move. (Lgb)
214 // OK, then no cursor action should be allowed in buffer. (ale)
215 bool BufferView::gotoLabel(string const & label)
218 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
219 it != buffer()->inset_iterator_end(); ++it) {
220 vector<string> labels = (*it)->getLabelList();
221 if (find(labels.begin(),labels.end(),label)
224 text->setCursor(this, it.getPar(), it.getPos());
225 text->selection.cursor = text->cursor;
226 update(text, BufferView::SELECT|BufferView::FITCUR);
234 void BufferView::menuUndo()
237 owner()->message(_("Undo"));
240 update(text, BufferView::SELECT|BufferView::FITCUR);
242 owner()->message(_("No further undo information"));
244 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
250 void BufferView::menuRedo()
252 if (theLockingInset()) {
253 owner()->message(_("Redo not yet supported in math mode"));
258 owner()->message(_("Redo"));
261 update(text, BufferView::SELECT|BufferView::FITCUR);
263 owner()->message(_("No further redo information"));
265 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
271 void BufferView::copyEnvironment()
274 text->copyEnvironmentType();
275 owner()->message(_("Paragraph environment type copied"));
280 void BufferView::pasteEnvironment()
283 text->pasteEnvironmentType(this);
284 owner()->message(_("Paragraph environment type set"));
285 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
290 void BufferView::copy()
293 text->copySelection(this);
294 owner()->message(_("Copy"));
299 void BufferView::cut(bool realcut)
303 update(text, BufferView::SELECT|BufferView::FITCUR);
304 text->cutSelection(this, true, realcut);
305 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
306 owner()->message(_("Cut"));
311 void BufferView::paste()
313 if (!available()) return;
315 owner()->message(_("Paste"));
318 // clear the selection
320 text->clearSelection();
321 update(text, BufferView::SELECT|BufferView::FITCUR);
324 text->pasteSelection(this);
325 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
327 // clear the selection
329 text->clearSelection();
330 update(text, BufferView::SELECT|BufferView::FITCUR);
334 /* these functions are for the spellchecker */
335 string const BufferView::nextWord(float & value)
342 return text->selectNextWordToSpellcheck(this, value);
346 void BufferView::selectLastWord()
348 if (!available()) return;
350 LyXCursor cur = text->selection.cursor;
353 text->selection.cursor = cur;
354 text->selectSelectedWord(this);
355 toggleSelection(false);
356 update(text, BufferView::SELECT|BufferView::FITCUR);
360 void BufferView::endOfSpellCheck()
362 if (!available()) return;
366 text->selectSelectedWord(this);
367 text->clearSelection();
368 update(text, BufferView::SELECT|BufferView::FITCUR);
372 void BufferView::replaceWord(string const & replacestring)
374 if (!available()) return;
376 LyXText * tt = getLyXText();
378 update(tt, BufferView::SELECT|BufferView::FITCUR);
380 /* clear the selection (if there is any) */
381 toggleSelection(false);
382 update(tt, BufferView::SELECT|BufferView::FITCUR);
384 /* clear the selection (if there is any) */
385 toggleSelection(false);
386 tt->replaceSelectionWithString(this, replacestring);
388 tt->setSelectionOverString(this, replacestring);
390 // Go back so that replacement string is also spellchecked
391 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
392 tt->cursorLeft(this);
394 update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
396 // End of spellchecker stuff
399 bool BufferView::lockInset(UpdatableInset * inset)
401 // don't relock if we're already locked
402 if (theLockingInset() == inset)
404 if (!theLockingInset() && inset) {
405 theLockingInset(inset);
408 return theLockingInset()->lockInsetInInset(this, inset);
414 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
416 if (available() && theLockingInset()) {
417 LyXCursor cursor = text->cursor;
418 Inset * locking_inset = theLockingInset()->getLockingInset();
420 if ((cursor.pos() - 1 >= 0) &&
421 cursor.par()->isInset(cursor.pos() - 1) &&
422 (cursor.par()->getInset(cursor.pos() - 1) ==
424 text->setCursor(this, cursor,
425 cursor.par(), cursor.pos() - 1);
426 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
427 LyXText * txt = getLyXText();
428 if (locking_inset->isTextInset() &&
429 locking_inset->lyxCode() != Inset::ERT_CODE &&
430 (txt->real_current_font.language() !=
431 buffer()->params.language
432 || txt->real_current_font.isVisibleRightToLeft()
433 != buffer()->params.language->RightToLeft()))
434 shape = (txt->real_current_font.isVisibleRightToLeft())
435 ? LyXScreen::REVERSED_L_SHAPE
436 : LyXScreen::L_SHAPE;
437 y += cursor.y() + theLockingInset()->insetInInsetY();
438 pimpl_->screen_->showManualCursor(text, x, y, asc, desc,
444 void BufferView::hideLockedInsetCursor()
446 if (theLockingInset() && available()) {
447 pimpl_->screen_->hideCursor();
452 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
454 if (theLockingInset() && available()) {
455 y += text->cursor.y() + theLockingInset()->insetInInsetY();
456 if (pimpl_->screen_->fitManualCursor(text, this, x, y, asc, desc))
462 int BufferView::unlockInset(UpdatableInset * inset)
464 if (inset && theLockingInset() == inset) {
465 inset->insetUnlock(this);
467 // make sure we update the combo !
468 owner()->setLayout(getLyXText()->cursor.par()->getLayout());
471 } else if (inset && theLockingInset() &&
472 theLockingInset()->unlockInsetInInset(this, inset)) {
473 // owner inset has updated the layout combo
477 return bufferlist.unlockInset(inset);
481 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
483 if (!theLockingInset())
484 return; // shouldn't happen
485 if (kind == Undo::EDIT) // in this case insets would not be stored!
489 text->cursor.par()->next());
493 void BufferView::updateInset(Inset * inset, bool mark_dirty)
495 pimpl_->updateInset(inset, mark_dirty);
499 bool BufferView::ChangeInsets(Inset::Code code,
500 string const & from, string const & to)
502 bool need_update = false;
503 LyXCursor cursor = text->cursor;
504 LyXCursor tmpcursor = cursor;
505 cursor.par(tmpcursor.par());
506 cursor.pos(tmpcursor.pos());
508 ParIterator end = buffer()->par_iterator_end();
509 for (ParIterator it = buffer()->par_iterator_begin();
511 Paragraph * par = *it;
512 bool changed_inset = false;
513 for (Paragraph::inset_iterator it2 = par->inset_iterator_begin();
514 it2 != par->inset_iterator_end(); ++it2) {
515 if ((*it2)->lyxCode() == code) {
516 InsetCommand * inset = static_cast<InsetCommand *>(*it2);
517 if (inset->getContents() == from) {
518 inset->setContents(to);
519 changed_inset = true;
528 // The test it.size()==1 was needed to prevent crashes.
529 // How to set the cursor corretly when it.size()>1 ??
530 if (it.size() == 1) {
531 text->setCursorIntern(this, par, 0);
532 text->redoParagraphs(this, text->cursor,
533 text->cursor.par()->next());
534 text->fullRebreak(this);
538 text->setCursorIntern(this, cursor.par(), cursor.pos());
543 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
545 // Check if the label 'from' appears more than once
546 vector<string> labels = buffer()->getLabelList();
547 if (count(labels.begin(), labels.end(), from) > 1)
550 return ChangeInsets(Inset::REF_CODE, from, to);
554 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
557 vector<pair<string,string> > keys = buffer()->getBibkeyList();
558 if (count_if(keys.begin(), keys.end(),
559 lyx::equal_1st_in_pair<string,string>(from))
563 return ChangeInsets(Inset::CITE_CODE, from, to);
567 UpdatableInset * BufferView::theLockingInset() const
569 // If NULL is not allowed we should put an Assert here. (Lgb)
571 return text->the_locking_inset;
576 void BufferView::theLockingInset(UpdatableInset * inset)
578 text->the_locking_inset = inset;
582 LyXText * BufferView::getLyXText() const
584 if (theLockingInset()) {
585 LyXText * txt = theLockingInset()->getLyXText(this, true);
593 LyXText * BufferView::getParentText(Inset * inset) const
595 if (inset->owner()) {
596 LyXText * txt = inset->getLyXText(this);
597 inset = inset->owner();
598 while (inset && inset->getLyXText(this) == txt)
599 inset = inset->owner();
601 return inset->getLyXText(this);
607 Language const * BufferView::getParentLanguage(Inset * inset) const
609 LyXText * text = getParentText(inset);
610 return text->cursor.par()->getFontSettings(buffer()->params,
611 text->cursor.pos()).language();