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 // Iterate until we find a paragraph that won't be immediately deleted.
121 // In reality this should mean we only execute the body of the while
122 // loop once at most. However for safety we iterate rather than just
123 // make this an if() conditional.
124 while ((cur_par_prev || cur_par_next)
125 && text->setCursor(this,
126 cur_par_prev ? cur_par_prev : cur_par_next,
128 // We just removed cur_par so have to fix the "cursor"
133 cur_par = cur_par_prev;
134 cur_pos = cur_par->size();
138 cur_par = cur_par_next;
141 cur_par_prev = cur_par->previous();
142 cur_par_next = cur_par->next();
145 ParIterator it = buffer()->par_iterator_begin();
146 ParIterator end = buffer()->par_iterator_end();
147 for (; it != end; ++it) {
148 Paragraph * par = *it;
149 Paragraph * par_prev = par ? par->previous() : 0;
150 bool removed = false;
152 if (text->setCursor(this, par, 0)
153 && cur_par == par_prev) {
154 // The previous setCursor line was deleted and that
155 // was the cur_par line. This can only happen if an
156 // error box was the sole item on cur_par.
158 // '|' = par, '.' = cur_par, 'E' = error box
159 // First step below may occur before while{}
161 // E -> .E -> |.E -> . -> |b
164 cur_par = cur_par_prev;
165 cur_pos = cur_par_prev->size();
166 cur_par_prev = cur_par->previous();
167 // cur_par_next remains the same
168 } else if (cur_par_next) {
169 // First step below may occur before while{}
171 // E -> |.E -> |. -> . -> .|a
173 cur_par = cur_par_next;
175 // cur_par_prev remains unset
176 cur_par_next = cur_par->next();
178 // I can't find a way to trigger this
179 // so it should be unreachable code
180 // unless the buffer is corrupted.
181 lyxerr << "BufferView::removeAutoInsets() is bad\n";
185 Paragraph::inset_iterator pit = par->inset_iterator_begin();
186 Paragraph::inset_iterator pend = par->inset_iterator_end();
187 while (pit != pend) {
188 if (pit->autoDelete()) {
190 pos_type const pos = pit.getPos();
193 if (cur_par == par) {
203 text->redoParagraph(this);
207 text->setCursorIntern(this, cur_par, cur_pos);
213 void BufferView::insertErrors(TeXErrors & terr)
215 // Save the cursor position
216 LyXCursor cursor = text->cursor;
218 TeXErrors::Errors::const_iterator cit = terr.begin();
219 TeXErrors::Errors::const_iterator end = terr.end();
220 for (; cit != end; ++cit) {
221 string const desctext(cit->error_desc);
222 string const errortext(cit->error_text);
223 string const msgtxt = desctext + '\n' + errortext;
224 int const errorrow = cit->error_in_line;
226 // Insert error string for row number
230 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
231 buffer()->texrow.increasePos(tmpid, tmppos);
234 Paragraph * texrowpar = 0;
237 texrowpar = text->ownerParagraph();
240 texrowpar = buffer()->getParFromID(tmpid);
246 InsetError * new_inset = new InsetError(msgtxt);
247 text->setCursorIntern(this, texrowpar, tmppos);
248 text->insertInset(this, new_inset);
249 text->fullRebreak(this);
251 // Restore the cursor position
252 text->setCursorIntern(this, cursor.par(), cursor.pos());
256 void BufferView::setCursorFromRow(int row)
261 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
263 Paragraph * texrowpar;
266 texrowpar = text->ownerParagraph();
269 texrowpar = buffer()->getParFromID(tmpid);
271 text->setCursor(this, texrowpar, tmppos);
275 bool BufferView::insertInset(Inset * inset, string const & lout)
277 return pimpl_->insertInset(inset, lout);
281 /* This is also a buffer property (ale) */
282 // Not so sure about that. a goto Label function can not be buffer local, just
283 // think how this will work in a multiwindo/buffer environment, all the
284 // cursors in all the views showing this buffer will move. (Lgb)
285 // OK, then no cursor action should be allowed in buffer. (ale)
286 bool BufferView::gotoLabel(string const & label)
289 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
290 it != buffer()->inset_iterator_end(); ++it) {
291 vector<string> labels = (*it)->getLabelList();
292 if (find(labels.begin(),labels.end(),label)
295 text->setCursor(this, it.getPar(), it.getPos());
296 text->selection.cursor = text->cursor;
297 update(text, BufferView::SELECT|BufferView::FITCUR);
305 void BufferView::menuUndo()
308 owner()->message(_("Undo"));
311 update(text, BufferView::SELECT|BufferView::FITCUR);
313 owner()->message(_("No further undo information"));
315 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
321 void BufferView::menuRedo()
323 #if 0 // this should not be here (Jug 20011206)
324 if (theLockingInset()) {
325 owner()->message(_("Redo not yet supported in math mode"));
331 owner()->message(_("Redo"));
334 update(text, BufferView::SELECT|BufferView::FITCUR);
336 owner()->message(_("No further redo information"));
338 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
344 void BufferView::copyEnvironment()
347 text->copyEnvironmentType();
348 owner()->message(_("Paragraph environment type copied"));
353 void BufferView::pasteEnvironment()
356 text->pasteEnvironmentType(this);
357 owner()->message(_("Paragraph environment type set"));
358 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
363 void BufferView::copy()
366 text->copySelection(this);
367 owner()->message(_("Copy"));
372 void BufferView::cut(bool realcut)
376 update(text, BufferView::SELECT|BufferView::FITCUR);
377 text->cutSelection(this, true, realcut);
378 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
379 owner()->message(_("Cut"));
384 void BufferView::paste()
386 if (!available()) return;
388 owner()->message(_("Paste"));
391 // clear the selection
393 text->clearSelection();
394 update(text, BufferView::SELECT|BufferView::FITCUR);
397 text->pasteSelection(this);
398 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
400 // clear the selection
402 text->clearSelection();
403 update(text, BufferView::SELECT|BufferView::FITCUR);
407 /* these functions are for the spellchecker */
408 string const BufferView::nextWord(float & value)
415 return text->selectNextWordToSpellcheck(this, value);
419 void BufferView::selectLastWord()
421 if (!available()) return;
423 LyXCursor cur = text->selection.cursor;
426 text->selection.cursor = cur;
427 text->selectSelectedWord(this);
428 toggleSelection(false);
429 update(text, BufferView::SELECT|BufferView::FITCUR);
433 void BufferView::endOfSpellCheck()
435 if (!available()) return;
439 text->selectSelectedWord(this);
440 text->clearSelection();
441 update(text, BufferView::SELECT|BufferView::FITCUR);
445 void BufferView::replaceWord(string const & replacestring)
447 if (!available()) return;
449 LyXText * tt = getLyXText();
451 update(tt, BufferView::SELECT|BufferView::FITCUR);
453 /* clear the selection (if there is any) */
454 toggleSelection(false);
455 update(tt, BufferView::SELECT|BufferView::FITCUR);
457 /* clear the selection (if there is any) */
458 toggleSelection(false);
459 tt->replaceSelectionWithString(this, replacestring);
461 tt->setSelectionOverString(this, replacestring);
463 // Go back so that replacement string is also spellchecked
464 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
465 tt->cursorLeft(this);
467 update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
469 // End of spellchecker stuff
472 bool BufferView::lockInset(UpdatableInset * inset)
476 // don't relock if we're already locked
477 if (theLockingInset() == inset)
479 if (!theLockingInset()) {
480 // first check if it's the inset under the cursor we want lock
481 // should be most of the time
482 char const c = text->cursor.par()->getChar(text->cursor.pos());
483 if (c == Paragraph::META_INSET) {
484 Inset * in = text->cursor.par()->getInset(text->cursor.pos());
486 theLockingInset(inset);
490 // Then do a deep look of the inset and lock the right one
491 Paragraph * par = buffer()->paragraph;
492 int const id = inset->id();
494 Paragraph::inset_iterator it =
495 par->inset_iterator_begin();
496 Paragraph::inset_iterator const end =
497 par->inset_iterator_end();
498 for (; it != end; ++it) {
499 if ((*it) == inset) {
500 text->setCursorIntern(this, par, it.getPos());
501 theLockingInset(inset);
504 if ((*it)->getInsetFromID(id)) {
505 text->setCursorIntern(this, par, it.getPos());
506 theLockingInset(static_cast<UpdatableInset *>(*it));
507 return theLockingInset()->lockInsetInInset(this, inset);
514 return theLockingInset()->lockInsetInInset(this, inset);
518 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
520 if (available() && theLockingInset() && !theLockingInset()->nodraw()) {
521 LyXCursor cursor = text->cursor;
522 Inset * locking_inset = theLockingInset()->getLockingInset();
524 if ((cursor.pos() - 1 >= 0) &&
525 cursor.par()->isInset(cursor.pos() - 1) &&
526 (cursor.par()->getInset(cursor.pos() - 1) ==
528 text->setCursor(this, cursor,
529 cursor.par(), cursor.pos() - 1);
530 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
531 LyXText * txt = getLyXText();
532 if (locking_inset->isTextInset() &&
533 locking_inset->lyxCode() != Inset::ERT_CODE &&
534 (txt->real_current_font.language() !=
535 buffer()->params.language
536 || txt->real_current_font.isVisibleRightToLeft()
537 != buffer()->params.language->RightToLeft()))
538 shape = (txt->real_current_font.isVisibleRightToLeft())
539 ? LyXScreen::REVERSED_L_SHAPE
540 : LyXScreen::L_SHAPE;
541 y += cursor.y() + theLockingInset()->insetInInsetY();
542 pimpl_->screen_->showManualCursor(text, x, y, asc, desc,
548 void BufferView::hideLockedInsetCursor()
550 if (theLockingInset() && available()) {
551 pimpl_->screen_->hideCursor();
556 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
558 if (theLockingInset() && available()) {
559 y += text->cursor.y() + theLockingInset()->insetInInsetY();
560 if (pimpl_->screen_->fitManualCursor(text, this, x, y, asc, desc))
566 int BufferView::unlockInset(UpdatableInset * inset)
570 if (inset && theLockingInset() == inset) {
571 inset->insetUnlock(this);
573 // make sure we update the combo !
574 owner()->setLayout(getLyXText()->cursor.par()->getLayout());
577 } else if (inset && theLockingInset() &&
578 theLockingInset()->unlockInsetInInset(this, inset)) {
579 // owner inset has updated the layout combo
583 return bufferlist.unlockInset(inset);
587 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
589 if (!theLockingInset())
590 return; // shouldn't happen
591 if (kind == Undo::EDIT) // in this case insets would not be stored!
595 text->cursor.par()->next());
599 void BufferView::updateInset(Inset * inset, bool mark_dirty)
601 pimpl_->updateInset(inset, mark_dirty);
605 bool BufferView::ChangeInsets(Inset::Code code,
606 string const & from, string const & to)
608 bool need_update = false;
609 LyXCursor cursor = text->cursor;
610 LyXCursor tmpcursor = cursor;
611 cursor.par(tmpcursor.par());
612 cursor.pos(tmpcursor.pos());
614 ParIterator end = buffer()->par_iterator_end();
615 for (ParIterator it = buffer()->par_iterator_begin();
617 Paragraph * par = *it;
618 bool changed_inset = false;
619 for (Paragraph::inset_iterator it2 = par->inset_iterator_begin();
620 it2 != par->inset_iterator_end(); ++it2) {
621 if ((*it2)->lyxCode() == code) {
622 InsetCommand * inset = static_cast<InsetCommand *>(*it2);
623 if (inset->getContents() == from) {
624 inset->setContents(to);
625 changed_inset = true;
634 // The test it.size()==1 was needed to prevent crashes.
635 // How to set the cursor corretly when it.size()>1 ??
636 if (it.size() == 1) {
637 text->setCursorIntern(this, par, 0);
638 text->redoParagraphs(this, text->cursor,
639 text->cursor.par()->next());
640 text->fullRebreak(this);
644 text->setCursorIntern(this, cursor.par(), cursor.pos());
649 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
651 // Check if the label 'from' appears more than once
652 vector<string> labels = buffer()->getLabelList();
653 // count is broken on some systems, so use the HP version
655 count(labels.begin(), labels.end(), from, res);
659 return ChangeInsets(Inset::REF_CODE, from, to);
663 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
666 vector<pair<string,string> > keys = buffer()->getBibkeyList();
667 if (count_if(keys.begin(), keys.end(),
668 lyx::equal_1st_in_pair<string,string>(from))
672 return ChangeInsets(Inset::CITE_CODE, from, to);
676 UpdatableInset * BufferView::theLockingInset() const
678 // If NULL is not allowed we should put an Assert here. (Lgb)
680 return text->the_locking_inset;
685 void BufferView::theLockingInset(UpdatableInset * inset)
687 text->the_locking_inset = inset;
691 LyXText * BufferView::getLyXText() const
693 if (theLockingInset()) {
694 LyXText * txt = theLockingInset()->getLyXText(this, true);
702 LyXText * BufferView::getParentText(Inset * inset) const
704 if (inset->owner()) {
705 LyXText * txt = inset->getLyXText(this);
706 inset = inset->owner();
707 while (inset && inset->getLyXText(this) == txt)
708 inset = inset->owner();
710 return inset->getLyXText(this);
716 Language const * BufferView::getParentLanguage(Inset * inset) const
718 LyXText * text = getParentText(inset);
719 return text->cursor.par()->getFontSettings(buffer()->params,
720 text->cursor.pos()).language();