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
36 #include "support/types.h"
41 extern BufferList bufferlist;
54 // Inserts a file into current document
55 bool BufferView::insertLyXFile(string const & filen)
57 // Copyright CHT Software Service GmbH
60 // Insert a Lyxformat - file into current buffer
62 // Moved from lyx_cb.C (Lgb)
64 if (filen.empty()) return false;
66 string const fname = MakeAbsPath(filen);
68 // check if file exist
69 FileInfo const fi(fname);
72 Alert::alert(_("Error!"),
73 _("Specified file is unreadable: "),
74 MakeDisplayPath(fname, 50));
80 ifstream ifs(fname.c_str());
82 Alert::alert(_("Error!"),
83 _("Cannot open specified file: "),
84 MakeDisplayPath(fname, 50));
88 int const c = ifs.peek();
96 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
97 res = buffer()->readFile(lex, text->cursor.par());
99 lyxerr[Debug::INFO] << "Will insert file without header"
101 res = buffer()->readLyXformat2(lex, text->cursor.par());
109 bool BufferView::removeAutoInsets()
111 LyXCursor tmpcursor = text->cursor;
112 Paragraph * cur_par = tmpcursor.par();
113 Paragraph * cur_par_prev = cur_par ? cur_par->previous() : 0;
114 Paragraph * cur_par_next = cur_par ? cur_par->next() : 0;
115 pos_type cur_pos = tmpcursor.pos();
119 // Trap the deletion of the paragraph the cursor is in.
120 // It should be almost impossible for the new cursor par to be
121 // deleted later on in this function.
122 // This is the way to segfault this now. Although you may have to do this
123 // multiple times: Have an InsetERT with an unknown command in it.
124 // View->DVI, move cursor between Error box and InsetERT and hit <Enter>,
125 // <down-arrow>, <Enter> again, View->DVI, BANG!
127 while ((cur_par_prev || cur_par_next)
128 && text->setCursor(this,
129 cur_par_prev ? cur_par_prev : cur_par_next,
131 // we just removed cur_par so have to fix the "cursor"
133 cur_par = cur_par_prev;
134 cur_pos = cur_par->size();
136 cur_par = cur_par_next;
139 cur_par_prev = cur_par->previous();
140 cur_par_next = cur_par->next();
143 ParIterator it = buffer()->par_iterator_begin();
144 ParIterator end = buffer()->par_iterator_end();
145 for (; it != end; ++it) {
146 Paragraph * par = *it;
147 Paragraph * par_prev = par ? par->previous() : 0;
148 bool removed = false;
150 if (text->setCursor(this, par, 0)
151 && cur_par == par_prev) {
152 // The previous setCursor line was deleted and that
153 // was the cur_par line. This can only happen if an
154 // error box was the sole item on cur_par.
156 // '|' = par, '.' = cur_par, 'E' = error box
157 // First step below may occur before while{}
159 // E -> .E -> |.E -> . -> |b
162 cur_par = cur_par_prev;
163 cur_pos = cur_par_prev->size();
164 cur_par_prev = cur_par->previous();
165 // cur_par_next remains the same
166 } else if (cur_par_next) {
167 // First step below may occur before while{}
169 // E -> |.E -> |. -> . -> .|a
171 cur_par = cur_par_next;
173 // cur_par_prev remains unset
174 cur_par_next = cur_par->next();
176 // I can't find a way to trigger this
177 // so it should be unreachable code
178 // unless the buffer is corrupted.
179 lyxerr << "BufferView::removeAutoInsets() is bad\n";
183 Paragraph::inset_iterator pit = par->inset_iterator_begin();
184 Paragraph::inset_iterator pend = par->inset_iterator_end();
185 while (pit != pend) {
186 if (pit->autoDelete()) {
188 pos_type const pos = pit.getPos();
191 if (cur_par == par) {
201 text->redoParagraph(this);
205 text->setCursorIntern(this, cur_par, cur_pos);
211 void BufferView::insertErrors(TeXErrors & terr)
213 // Save the cursor position
214 LyXCursor cursor = text->cursor;
216 TeXErrors::Errors::const_iterator cit = terr.begin();
217 TeXErrors::Errors::const_iterator end = terr.end();
218 for (; cit != end; ++cit) {
219 string const desctext(cit->error_desc);
220 string const errortext(cit->error_text);
221 string const msgtxt = desctext + '\n' + errortext;
222 int const errorrow = cit->error_in_line;
224 // Insert error string for row number
228 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
229 buffer()->texrow.increasePos(tmpid, tmppos);
232 Paragraph * texrowpar = 0;
235 texrowpar = text->ownerParagraph();
238 texrowpar = buffer()->getParFromID(tmpid);
244 InsetError * new_inset = new InsetError(msgtxt);
245 text->setCursorIntern(this, texrowpar, tmppos);
246 text->insertInset(this, new_inset);
247 text->fullRebreak(this);
249 // Restore the cursor position
250 text->setCursorIntern(this, cursor.par(), cursor.pos());
254 void BufferView::setCursorFromRow(int row)
259 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
261 Paragraph * texrowpar;
264 texrowpar = text->ownerParagraph();
267 texrowpar = buffer()->getParFromID(tmpid);
269 text->setCursor(this, texrowpar, tmppos);
273 bool BufferView::insertInset(Inset * inset, string const & lout)
275 return pimpl_->insertInset(inset, lout);
279 /* This is also a buffer property (ale) */
280 // Not so sure about that. a goto Label function can not be buffer local, just
281 // think how this will work in a multiwindo/buffer environment, all the
282 // cursors in all the views showing this buffer will move. (Lgb)
283 // OK, then no cursor action should be allowed in buffer. (ale)
284 bool BufferView::gotoLabel(string const & label)
287 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
288 it != buffer()->inset_iterator_end(); ++it) {
289 vector<string> labels = (*it)->getLabelList();
290 if (find(labels.begin(),labels.end(),label)
293 text->setCursor(this, it.getPar(), it.getPos());
294 text->selection.cursor = text->cursor;
295 update(text, BufferView::SELECT|BufferView::FITCUR);
303 void BufferView::menuUndo()
306 owner()->message(_("Undo"));
309 update(text, BufferView::SELECT|BufferView::FITCUR);
311 owner()->message(_("No further undo information"));
313 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
319 void BufferView::menuRedo()
321 #if 0 // this should not be here (Jug 20011206)
322 if (theLockingInset()) {
323 owner()->message(_("Redo not yet supported in math mode"));
329 owner()->message(_("Redo"));
332 update(text, BufferView::SELECT|BufferView::FITCUR);
334 owner()->message(_("No further redo information"));
336 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
342 void BufferView::copyEnvironment()
345 text->copyEnvironmentType();
346 owner()->message(_("Paragraph environment type copied"));
351 void BufferView::pasteEnvironment()
354 text->pasteEnvironmentType(this);
355 owner()->message(_("Paragraph environment type set"));
356 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
361 void BufferView::copy()
364 text->copySelection(this);
365 owner()->message(_("Copy"));
370 void BufferView::cut(bool realcut)
374 update(text, BufferView::SELECT|BufferView::FITCUR);
375 text->cutSelection(this, true, realcut);
376 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
377 owner()->message(_("Cut"));
382 void BufferView::paste()
384 if (!available()) return;
386 owner()->message(_("Paste"));
389 // clear the selection
391 text->clearSelection();
392 update(text, BufferView::SELECT|BufferView::FITCUR);
395 text->pasteSelection(this);
396 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
398 // clear the selection
400 text->clearSelection();
401 update(text, BufferView::SELECT|BufferView::FITCUR);
405 /* these functions are for the spellchecker */
406 string const BufferView::nextWord(float & value)
413 return text->selectNextWordToSpellcheck(this, value);
417 void BufferView::selectLastWord()
419 if (!available()) return;
421 LyXCursor cur = text->selection.cursor;
424 text->selection.cursor = cur;
425 text->selectSelectedWord(this);
426 toggleSelection(false);
427 update(text, BufferView::SELECT|BufferView::FITCUR);
431 void BufferView::endOfSpellCheck()
433 if (!available()) return;
437 text->selectSelectedWord(this);
438 text->clearSelection();
439 update(text, BufferView::SELECT|BufferView::FITCUR);
443 void BufferView::replaceWord(string const & replacestring)
445 if (!available()) return;
447 LyXText * tt = getLyXText();
449 update(tt, BufferView::SELECT|BufferView::FITCUR);
451 /* clear the selection (if there is any) */
452 toggleSelection(false);
453 update(tt, BufferView::SELECT|BufferView::FITCUR);
455 /* clear the selection (if there is any) */
456 toggleSelection(false);
457 tt->replaceSelectionWithString(this, replacestring);
459 tt->setSelectionOverString(this, replacestring);
461 // Go back so that replacement string is also spellchecked
462 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
463 tt->cursorLeft(this);
465 update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
467 // End of spellchecker stuff
470 bool BufferView::lockInset(UpdatableInset * inset)
474 // don't relock if we're already locked
475 if (theLockingInset() == inset)
477 if (!theLockingInset()) {
478 // first check if it's the inset under the cursor we want lock
479 // should be most of the time
480 char const c = text->cursor.par()->getChar(text->cursor.pos());
481 if (c == Paragraph::META_INSET) {
482 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
484 theLockingInset(inset);
488 // Then do a deep look of the inset and lock the right one
489 Paragraph * par = buffer()->paragraph;
490 int const id = inset->id();
492 Paragraph::inset_iterator it =
493 par->inset_iterator_begin();
494 Paragraph::inset_iterator const end =
495 par->inset_iterator_end();
496 for (; it != end; ++it) {
497 if ((*it) == inset) {
498 text->setCursorIntern(this, par, it.getPos());
499 theLockingInset(inset);
502 if ((*it)->getInsetFromID(id)) {
503 text->setCursorIntern(this, par, it.getPos());
504 theLockingInset(static_cast<UpdatableInset *>(*it));
505 return theLockingInset()->lockInsetInInset(this, inset);
512 return theLockingInset()->lockInsetInInset(this, inset);
516 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
518 if (available() && theLockingInset() && !theLockingInset()->nodraw()) {
519 LyXCursor cursor = text->cursor;
520 Inset * locking_inset = theLockingInset()->getLockingInset();
522 if ((cursor.pos() - 1 >= 0) &&
523 cursor.par()->isInset(cursor.pos() - 1) &&
524 (cursor.par()->getInset(cursor.pos() - 1) ==
526 text->setCursor(this, cursor,
527 cursor.par(), cursor.pos() - 1);
528 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
529 LyXText * txt = getLyXText();
530 if (locking_inset->isTextInset() &&
531 locking_inset->lyxCode() != Inset::ERT_CODE &&
532 (txt->real_current_font.language() !=
533 buffer()->params.language
534 || txt->real_current_font.isVisibleRightToLeft()
535 != buffer()->params.language->RightToLeft()))
536 shape = (txt->real_current_font.isVisibleRightToLeft())
537 ? LyXScreen::REVERSED_L_SHAPE
538 : LyXScreen::L_SHAPE;
539 y += cursor.y() + theLockingInset()->insetInInsetY();
540 pimpl_->screen_->showManualCursor(text, x, y, asc, desc,
546 void BufferView::hideLockedInsetCursor()
548 if (theLockingInset() && available()) {
549 pimpl_->screen_->hideCursor();
554 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
556 if (theLockingInset() && available()) {
557 y += text->cursor.y() + theLockingInset()->insetInInsetY();
558 if (pimpl_->screen_->fitManualCursor(text, this, x, y, asc, desc))
564 int BufferView::unlockInset(UpdatableInset * inset)
568 if (inset && theLockingInset() == inset) {
569 inset->insetUnlock(this);
571 // make sure we update the combo !
572 owner()->setLayout(getLyXText()->cursor.par()->getLayout());
575 } else if (inset && theLockingInset() &&
576 theLockingInset()->unlockInsetInInset(this, inset)) {
577 // owner inset has updated the layout combo
581 return bufferlist.unlockInset(inset);
585 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
587 if (!theLockingInset())
588 return; // shouldn't happen
589 if (kind == Undo::EDIT) // in this case insets would not be stored!
593 text->cursor.par()->next());
597 void BufferView::updateInset(Inset * inset, bool mark_dirty)
599 pimpl_->updateInset(inset, mark_dirty);
603 bool BufferView::ChangeInsets(Inset::Code code,
604 string const & from, string const & to)
606 bool need_update = false;
607 LyXCursor cursor = text->cursor;
608 LyXCursor tmpcursor = cursor;
609 cursor.par(tmpcursor.par());
610 cursor.pos(tmpcursor.pos());
612 ParIterator end = buffer()->par_iterator_end();
613 for (ParIterator it = buffer()->par_iterator_begin();
615 Paragraph * par = *it;
616 bool changed_inset = false;
617 for (Paragraph::inset_iterator it2 = par->inset_iterator_begin();
618 it2 != par->inset_iterator_end(); ++it2) {
619 if ((*it2)->lyxCode() == code) {
620 InsetCommand * inset = static_cast<InsetCommand *>(*it2);
621 if (inset->getContents() == from) {
622 inset->setContents(to);
623 changed_inset = true;
632 // The test it.size()==1 was needed to prevent crashes.
633 // How to set the cursor corretly when it.size()>1 ??
634 if (it.size() == 1) {
635 text->setCursorIntern(this, par, 0);
636 text->redoParagraphs(this, text->cursor,
637 text->cursor.par()->next());
638 text->fullRebreak(this);
642 text->setCursorIntern(this, cursor.par(), cursor.pos());
647 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
649 // Check if the label 'from' appears more than once
650 vector<string> labels = buffer()->getLabelList();
651 // count is broken on some systems, so use the HP version
653 count(labels.begin(), labels.end(), from, res);
657 return ChangeInsets(Inset::REF_CODE, from, to);
661 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
664 vector<pair<string,string> > keys = buffer()->getBibkeyList();
665 if (count_if(keys.begin(), keys.end(),
666 lyx::equal_1st_in_pair<string,string>(from))
670 return ChangeInsets(Inset::CITE_CODE, from, to);
674 UpdatableInset * BufferView::theLockingInset() const
676 // If NULL is not allowed we should put an Assert here. (Lgb)
678 return text->the_locking_inset;
683 void BufferView::theLockingInset(UpdatableInset * inset)
685 text->the_locking_inset = inset;
689 LyXText * BufferView::getLyXText() const
691 if (theLockingInset()) {
692 LyXText * txt = theLockingInset()->getLyXText(this, true);
700 LyXText * BufferView::getParentText(Inset * inset) const
702 if (inset->owner()) {
703 LyXText * txt = inset->getLyXText(this);
704 inset = inset->owner();
705 while (inset && inset->getLyXText(this) == txt)
706 inset = inset->owner();
708 return inset->getLyXText(this);
714 Language const * BufferView::getParentLanguage(Inset * inset) const
716 LyXText * text = getParentText(inset);
717 return text->cursor.par()->getFontSettings(buffer()->params,
718 text->cursor.pos()).language();