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 0 // this should not be here (Jug 20011206)
253 if (theLockingInset()) {
254 owner()->message(_("Redo not yet supported in math mode"));
260 owner()->message(_("Redo"));
263 update(text, BufferView::SELECT|BufferView::FITCUR);
265 owner()->message(_("No further redo information"));
267 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
273 void BufferView::copyEnvironment()
276 text->copyEnvironmentType();
277 owner()->message(_("Paragraph environment type copied"));
282 void BufferView::pasteEnvironment()
285 text->pasteEnvironmentType(this);
286 owner()->message(_("Paragraph environment type set"));
287 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
292 void BufferView::copy()
295 text->copySelection(this);
296 owner()->message(_("Copy"));
301 void BufferView::cut(bool realcut)
305 update(text, BufferView::SELECT|BufferView::FITCUR);
306 text->cutSelection(this, true, realcut);
307 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
308 owner()->message(_("Cut"));
313 void BufferView::paste()
315 if (!available()) return;
317 owner()->message(_("Paste"));
320 // clear the selection
322 text->clearSelection();
323 update(text, BufferView::SELECT|BufferView::FITCUR);
326 text->pasteSelection(this);
327 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
329 // clear the selection
331 text->clearSelection();
332 update(text, BufferView::SELECT|BufferView::FITCUR);
336 /* these functions are for the spellchecker */
337 string const BufferView::nextWord(float & value)
344 return text->selectNextWordToSpellcheck(this, value);
348 void BufferView::selectLastWord()
350 if (!available()) return;
352 LyXCursor cur = text->selection.cursor;
355 text->selection.cursor = cur;
356 text->selectSelectedWord(this);
357 toggleSelection(false);
358 update(text, BufferView::SELECT|BufferView::FITCUR);
362 void BufferView::endOfSpellCheck()
364 if (!available()) return;
368 text->selectSelectedWord(this);
369 text->clearSelection();
370 update(text, BufferView::SELECT|BufferView::FITCUR);
374 void BufferView::replaceWord(string const & replacestring)
376 if (!available()) return;
378 LyXText * tt = getLyXText();
380 update(tt, BufferView::SELECT|BufferView::FITCUR);
382 /* clear the selection (if there is any) */
383 toggleSelection(false);
384 update(tt, BufferView::SELECT|BufferView::FITCUR);
386 /* clear the selection (if there is any) */
387 toggleSelection(false);
388 tt->replaceSelectionWithString(this, replacestring);
390 tt->setSelectionOverString(this, replacestring);
392 // Go back so that replacement string is also spellchecked
393 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
394 tt->cursorLeft(this);
396 update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
398 // End of spellchecker stuff
401 bool BufferView::lockInset(UpdatableInset * inset)
405 // don't relock if we're already locked
406 if (theLockingInset() == inset)
408 if (!theLockingInset()) {
409 // first check if it's the inset under the cursor we want lock
410 // should be most of the time
411 char const c = text->cursor.par()->getChar(text->cursor.pos());
412 if (c == Paragraph::META_INSET) {
413 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
415 theLockingInset(inset);
419 // Then do a deep look of the inset and lock the right one
420 Paragraph * par = buffer()->paragraph;
421 int const id = inset->id();
423 Paragraph::inset_iterator it =
424 par->inset_iterator_begin();
425 Paragraph::inset_iterator const end =
426 par->inset_iterator_end();
427 for (; it != end; ++it) {
428 if ((*it) == inset) {
429 theLockingInset(inset);
432 if ((*it)->getInsetFromID(id)) {
433 text->setCursorIntern(this, par, it.getPos());
434 theLockingInset(static_cast<UpdatableInset *>(*it));
435 return theLockingInset()->lockInsetInInset(this, inset);
442 return theLockingInset()->lockInsetInInset(this, inset);
446 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
448 if (available() && theLockingInset()) {
449 LyXCursor cursor = text->cursor;
450 Inset * locking_inset = theLockingInset()->getLockingInset();
452 if ((cursor.pos() - 1 >= 0) &&
453 cursor.par()->isInset(cursor.pos() - 1) &&
454 (cursor.par()->getInset(cursor.pos() - 1) ==
456 text->setCursor(this, cursor,
457 cursor.par(), cursor.pos() - 1);
458 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
459 LyXText * txt = getLyXText();
460 if (locking_inset->isTextInset() &&
461 locking_inset->lyxCode() != Inset::ERT_CODE &&
462 (txt->real_current_font.language() !=
463 buffer()->params.language
464 || txt->real_current_font.isVisibleRightToLeft()
465 != buffer()->params.language->RightToLeft()))
466 shape = (txt->real_current_font.isVisibleRightToLeft())
467 ? LyXScreen::REVERSED_L_SHAPE
468 : LyXScreen::L_SHAPE;
469 y += cursor.y() + theLockingInset()->insetInInsetY();
470 pimpl_->screen_->showManualCursor(text, x, y, asc, desc,
476 void BufferView::hideLockedInsetCursor()
478 if (theLockingInset() && available()) {
479 pimpl_->screen_->hideCursor();
484 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
486 if (theLockingInset() && available()) {
487 y += text->cursor.y() + theLockingInset()->insetInInsetY();
488 if (pimpl_->screen_->fitManualCursor(text, this, x, y, asc, desc))
494 int BufferView::unlockInset(UpdatableInset * inset)
498 if (inset && theLockingInset() == inset) {
499 inset->insetUnlock(this);
501 // make sure we update the combo !
502 owner()->setLayout(getLyXText()->cursor.par()->getLayout());
505 } else if (inset && theLockingInset() &&
506 theLockingInset()->unlockInsetInInset(this, inset)) {
507 // owner inset has updated the layout combo
511 return bufferlist.unlockInset(inset);
515 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
517 if (!theLockingInset())
518 return; // shouldn't happen
519 if (kind == Undo::EDIT) // in this case insets would not be stored!
523 text->cursor.par()->next());
527 void BufferView::updateInset(Inset * inset, bool mark_dirty)
529 pimpl_->updateInset(inset, mark_dirty);
533 bool BufferView::ChangeInsets(Inset::Code code,
534 string const & from, string const & to)
536 bool need_update = false;
537 LyXCursor cursor = text->cursor;
538 LyXCursor tmpcursor = cursor;
539 cursor.par(tmpcursor.par());
540 cursor.pos(tmpcursor.pos());
542 ParIterator end = buffer()->par_iterator_end();
543 for (ParIterator it = buffer()->par_iterator_begin();
545 Paragraph * par = *it;
546 bool changed_inset = false;
547 for (Paragraph::inset_iterator it2 = par->inset_iterator_begin();
548 it2 != par->inset_iterator_end(); ++it2) {
549 if ((*it2)->lyxCode() == code) {
550 InsetCommand * inset = static_cast<InsetCommand *>(*it2);
551 if (inset->getContents() == from) {
552 inset->setContents(to);
553 changed_inset = true;
562 // The test it.size()==1 was needed to prevent crashes.
563 // How to set the cursor corretly when it.size()>1 ??
564 if (it.size() == 1) {
565 text->setCursorIntern(this, par, 0);
566 text->redoParagraphs(this, text->cursor,
567 text->cursor.par()->next());
568 text->fullRebreak(this);
572 text->setCursorIntern(this, cursor.par(), cursor.pos());
577 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
579 // Check if the label 'from' appears more than once
580 vector<string> labels = buffer()->getLabelList();
581 if (count(labels.begin(), labels.end(), from) > 1)
584 return ChangeInsets(Inset::REF_CODE, from, to);
588 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
591 vector<pair<string,string> > keys = buffer()->getBibkeyList();
592 if (count_if(keys.begin(), keys.end(),
593 lyx::equal_1st_in_pair<string,string>(from))
597 return ChangeInsets(Inset::CITE_CODE, from, to);
601 UpdatableInset * BufferView::theLockingInset() const
603 // If NULL is not allowed we should put an Assert here. (Lgb)
605 return text->the_locking_inset;
610 void BufferView::theLockingInset(UpdatableInset * inset)
612 text->the_locking_inset = inset;
616 LyXText * BufferView::getLyXText() const
618 if (theLockingInset()) {
619 LyXText * txt = theLockingInset()->getLyXText(this, true);
627 LyXText * BufferView::getParentText(Inset * inset) const
629 if (inset->owner()) {
630 LyXText * txt = inset->getLyXText(this);
631 inset = inset->owner();
632 while (inset && inset->getLyXText(this) == txt)
633 inset = inset->owner();
635 return inset->getLyXText(this);
641 Language const * BufferView::getParentLanguage(Inset * inset) const
643 LyXText * text = getParentText(inset);
644 return text->cursor.par()->getFontSettings(buffer()->params,
645 text->cursor.pos()).language();