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 "frontends/Alert.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"
35 #include "iterators.h"
37 extern BufferList bufferlist;
48 // Inserts a file into current document
49 bool BufferView::insertLyXFile(string const & filen)
51 // Copyright CHT Software Service GmbH
54 // Insert a Lyxformat - file into current buffer
56 // Moved from lyx_cb.C (Lgb)
58 if (filen.empty()) return false;
60 string const fname = MakeAbsPath(filen);
62 // check if file exist
63 FileInfo const fi(fname);
66 Alert::alert(_("Error!"),
67 _("Specified file is unreadable: "),
68 MakeDisplayPath(fname, 50));
74 ifstream ifs(fname.c_str());
76 Alert::alert(_("Error!"),
77 _("Cannot open specified file: "),
78 MakeDisplayPath(fname, 50));
82 int const c = ifs.peek();
90 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
91 res = buffer()->readFile(lex, text->cursor.par());
93 lyxerr[Debug::INFO] << "Will insert file without header"
95 res = buffer()->readLyXformat2(lex, text->cursor.par());
103 bool BufferView::removeAutoInsets()
105 Paragraph * par = buffer()->paragraph;
107 LyXCursor tmpcursor = text->cursor;
113 // this has to be done before the delete
114 text->setCursor(this, cursor, par, 0);
115 if (par->autoDeleteInsets()){
117 text->redoParagraphs(this, cursor,
118 cursor.par()->next());
119 text->fullRebreak(this);
124 // avoid forbidden cursor positions caused by error removing
125 if (tmpcursor.pos() > tmpcursor.par()->size())
126 tmpcursor.pos(tmpcursor.par()->size());
128 text->setCursorIntern(this, tmpcursor.par(), tmpcursor.pos());
134 void BufferView::insertErrors(TeXErrors & terr)
136 // Save the cursor position
137 LyXCursor cursor = text->cursor;
139 for (TeXErrors::Errors::const_iterator cit = terr.begin();
142 string const desctext(cit->error_desc);
143 string const errortext(cit->error_text);
144 string const msgtxt = desctext + '\n' + errortext;
145 int const errorrow = cit->error_in_line;
147 // Insert error string for row number
151 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
152 buffer()->texrow.increasePos(tmpid, tmppos);
155 Paragraph * texrowpar = 0;
158 texrowpar = text->firstParagraph();
161 texrowpar = buffer()->getParFromID(tmpid);
167 InsetError * new_inset = new InsetError(msgtxt);
168 text->setCursorIntern(this, texrowpar, tmppos);
169 text->insertInset(this, new_inset);
170 text->fullRebreak(this);
172 // Restore the cursor position
173 text->setCursorIntern(this, cursor.par(), cursor.pos());
177 void BufferView::setCursorFromRow(int row)
182 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
184 Paragraph * texrowpar;
187 texrowpar = text->firstParagraph();
190 texrowpar = buffer()->getParFromID(tmpid);
192 text->setCursor(this, texrowpar, tmppos);
196 bool BufferView::insertInset(Inset * inset, string const & lout)
198 return pimpl_->insertInset(inset, lout);
202 /* This is also a buffer property (ale) */
203 // Not so sure about that. a goto Label function can not be buffer local, just
204 // think how this will work in a multiwindo/buffer environment, all the
205 // cursors in all the views showing this buffer will move. (Lgb)
206 // OK, then no cursor action should be allowed in buffer. (ale)
207 bool BufferView::gotoLabel(string const & label)
210 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
211 it != buffer()->inset_iterator_end(); ++it) {
212 vector<string> labels = (*it)->getLabelList();
213 if (find(labels.begin(),labels.end(),label)
216 text->setCursor(this, it.getPar(), it.getPos());
217 text->selection.cursor = text->cursor;
218 update(text, BufferView::SELECT|BufferView::FITCUR);
226 void BufferView::menuUndo()
229 owner()->message(_("Undo"));
232 update(text, BufferView::SELECT|BufferView::FITCUR);
234 owner()->message(_("No further undo information"));
236 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
242 void BufferView::menuRedo()
244 if (theLockingInset()) {
245 owner()->message(_("Redo not yet supported in math mode"));
250 owner()->message(_("Redo"));
253 update(text, BufferView::SELECT|BufferView::FITCUR);
255 owner()->message(_("No further redo information"));
257 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
263 void BufferView::copyEnvironment()
266 text->copyEnvironmentType();
267 // clear the selection, even if mark_set
269 text->clearSelection();
270 update(text, BufferView::SELECT|BufferView::FITCUR);
271 owner()->message(_("Paragraph environment type copied"));
276 void BufferView::pasteEnvironment()
279 text->pasteEnvironmentType(this);
280 owner()->message(_("Paragraph environment type set"));
281 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
286 void BufferView::copy()
289 text->copySelection(this);
291 // clear the selection, even if mark_set
293 text->clearSelection();
294 update(text, BufferView::SELECT|BufferView::FITCUR);
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)
403 if (!theLockingInset() && inset) {
404 theLockingInset(inset);
407 return theLockingInset()->lockInsetInInset(this, inset);
413 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
415 if (available() && theLockingInset()) {
416 LyXCursor cursor = text->cursor;
417 Inset * locking_inset = theLockingInset()->getLockingInset();
419 if ((cursor.pos() - 1 >= 0) &&
420 cursor.par()->isInset(cursor.pos() - 1) &&
421 (cursor.par()->getInset(cursor.pos() - 1) ==
423 text->setCursor(this, cursor,
424 cursor.par(), cursor.pos() - 1);
425 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
426 LyXText * txt = getLyXText();
427 if (locking_inset->isTextInset() &&
428 locking_inset->lyxCode() != Inset::ERT_CODE &&
429 (txt->real_current_font.language() !=
430 buffer()->params.language
431 || txt->real_current_font.isVisibleRightToLeft()
432 != buffer()->params.language->RightToLeft()))
433 shape = (txt->real_current_font.isVisibleRightToLeft())
434 ? LyXScreen::REVERSED_L_SHAPE
435 : LyXScreen::L_SHAPE;
436 y += cursor.y() + theLockingInset()->insetInInsetY();
437 pimpl_->screen_->showManualCursor(text, x, y, asc, desc,
443 void BufferView::hideLockedInsetCursor()
445 if (theLockingInset() && available()) {
446 pimpl_->screen_->hideCursor();
451 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
453 if (theLockingInset() && available()) {
454 y += text->cursor.y() + theLockingInset()->insetInInsetY();
455 if (pimpl_->screen_->fitManualCursor(text, this, x, y, asc, desc))
461 int BufferView::unlockInset(UpdatableInset * inset)
463 if (inset && theLockingInset() == inset) {
464 inset->insetUnlock(this);
468 } else if (inset && theLockingInset() &&
469 theLockingInset()->unlockInsetInInset(this, inset)) {
473 return bufferlist.unlockInset(inset);
477 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
479 if (!theLockingInset())
480 return; // shouldn't happen
481 if (kind == Undo::EDIT) // in this case insets would not be stored!
485 text->cursor.par()->next());
489 void BufferView::updateInset(Inset * inset, bool mark_dirty)
491 pimpl_->updateInset(inset, mark_dirty);
495 bool BufferView::ChangeInsets(Inset::Code code,
496 string const & from, string const & to)
499 LyXCursor cursor = text->cursor;
500 LyXCursor tmpcursor = cursor;
501 cursor.par(tmpcursor.par());
502 cursor.pos(tmpcursor.pos());
504 ParIterator end = buffer()->par_iterator_end();
505 for (ParIterator it = buffer()->par_iterator_begin();
507 Paragraph * par = *it;
509 for (Paragraph::inset_iterator it2 = par->inset_iterator_begin();
510 it2 != par->inset_iterator_end(); ++it2) {
511 if ((*it2)->lyxCode() == code) {
512 InsetCommand * inset = static_cast<InsetCommand *>(*it2);
513 if (inset->getContents() == from) {
514 inset->setContents(to);
522 // The test it.size()==1 was needed to prevent crashes.
523 // How to set the cursor corretly when it.size()>1 ??
524 if (it.size() == 1) {
525 text->setCursorIntern(this, par, 0);
526 text->redoParagraphs(this, text->cursor,
527 text->cursor.par()->next());
528 text->fullRebreak(this);
532 text->setCursorIntern(this, cursor.par(), cursor.pos());
537 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
539 // Check if the label 'from' appears more than once
540 vector<string> labels = buffer()->getLabelList();
541 if (count(labels.begin(), labels.end(), from) > 1)
544 return ChangeInsets(Inset::REF_CODE, from, to);
548 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
551 vector<pair<string,string> > keys = buffer()->getBibkeyList();
552 if (count_if(keys.begin(), keys.end(),
553 lyx::equal_1st_in_pair<string,string>(from))
557 return ChangeInsets(Inset::CITE_CODE, from, to);
561 UpdatableInset * BufferView::theLockingInset() const
563 // If NULL is not allowed we should put an Assert here. (Lgb)
565 return text->the_locking_inset;
570 void BufferView::theLockingInset(UpdatableInset * inset)
572 text->the_locking_inset = inset;
576 LyXText * BufferView::getLyXText() const
578 if (theLockingInset()) {
579 LyXText * txt = theLockingInset()->getLyXText(this, true);
587 LyXText * BufferView::getParentText(Inset * inset) const
589 if (inset->owner()) {
590 LyXText * txt = inset->getLyXText(this);
591 inset = inset->owner();
592 while (inset && inset->getLyXText(this) == txt)
593 inset = inset->owner();
595 return inset->getLyXText(this);
601 Language const * BufferView::getParentLanguage(Inset * inset) const
603 LyXText * text = getParentText(inset);
604 return text->cursor.par()->getFontSettings(buffer()->params,
605 text->cursor.pos()).language();