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 "lyx_gui_misc.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 WriteAlert(_("Error!"),
67 _("Specified file is unreadable: "),
68 MakeDisplayPath(fname, 50));
74 ifstream ifs(fname.c_str());
76 WriteAlert(_("Error!"),
77 _("Cannot open specified file: "),
78 MakeDisplayPath(fname, 50));
82 char 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 forther 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);
290 // clear the selection, even if mark_set
292 text->clearSelection();
293 update(text, BufferView::SELECT|BufferView::FITCUR);
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->selectNextWord(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 if (!theLockingInset() && inset) {
402 theLockingInset(inset);
405 return theLockingInset()->lockInsetInInset(this, inset);
411 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
413 if (available() && theLockingInset()) {
414 LyXCursor cursor = text->cursor;
415 Inset * locking_inset = theLockingInset()->getLockingInset();
417 if ((cursor.pos() - 1 >= 0) &&
418 (cursor.par()->getChar(cursor.pos() - 1) ==
419 Paragraph::META_INSET) &&
420 (cursor.par()->getInset(cursor.pos() - 1) ==
422 text->setCursor(this, cursor,
423 cursor.par(), cursor.pos() - 1);
424 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
425 LyXText * txt = getLyXText();
426 if (locking_inset->isTextInset() &&
427 locking_inset->lyxCode() != Inset::ERT_CODE &&
428 (txt->real_current_font.language() !=
429 buffer()->params.language
430 || txt->real_current_font.isVisibleRightToLeft()
431 != buffer()->params.language->RightToLeft()))
432 shape = (txt->real_current_font.isVisibleRightToLeft())
433 ? LyXScreen::REVERSED_L_SHAPE
434 : LyXScreen::L_SHAPE;
435 y += cursor.y() + theLockingInset()->insetInInsetY();
436 pimpl_->screen_->showManualCursor(text, x, y, asc, desc,
442 void BufferView::hideLockedInsetCursor()
444 if (theLockingInset() && available()) {
445 pimpl_->screen_->hideCursor();
450 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
452 if (theLockingInset() && available()) {
453 y += text->cursor.y() + theLockingInset()->insetInInsetY();
454 if (pimpl_->screen_->fitManualCursor(text, this, x, y, asc, desc))
460 int BufferView::unlockInset(UpdatableInset * inset)
462 if (inset && theLockingInset() == inset) {
463 inset->insetUnlock(this);
467 } else if (inset && theLockingInset() &&
468 theLockingInset()->unlockInsetInInset(this, inset)) {
472 return bufferlist.unlockInset(inset);
476 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
478 if (!theLockingInset())
479 return; // shouldn't happen
480 if (kind == Undo::EDIT) // in this case insets would not be stored!
484 text->cursor.par()->next());
488 void BufferView::updateInset(Inset * inset, bool mark_dirty)
490 pimpl_->updateInset(inset, mark_dirty);
494 bool BufferView::ChangeInsets(Inset::Code code,
495 string const & from, string const & to)
498 LyXCursor cursor = text->cursor;
499 LyXCursor tmpcursor = cursor;
500 cursor.par(tmpcursor.par());
501 cursor.pos(tmpcursor.pos());
503 ParIterator end = buffer()->par_iterator_end();
504 for (ParIterator it = buffer()->par_iterator_begin();
506 Paragraph * par = *it;
508 for (Paragraph::inset_iterator it2 = par->inset_iterator_begin();
509 it2 != par->inset_iterator_end(); ++it2) {
510 if ((*it2)->lyxCode() == code) {
511 InsetCommand * inset = static_cast<InsetCommand *>(*it2);
512 if (inset->getContents() == from) {
513 inset->setContents(to);
521 // The test it.size()==1 was needed to prevent crashes.
522 // How to set the cursor corretly when it.size()>1 ??
523 if (it.size() == 1) {
524 text->setCursorIntern(this, par, 0);
525 text->redoParagraphs(this, text->cursor,
526 text->cursor.par()->next());
527 text->fullRebreak(this);
531 text->setCursorIntern(this, cursor.par(), cursor.pos());
536 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
538 // Check if the label 'from' appears more than once
539 vector<string> labels = buffer()->getLabelList();
540 if (count(labels.begin(), labels.end(), from) > 1)
543 return ChangeInsets(Inset::REF_CODE, from, to);
547 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
550 vector<pair<string,string> > keys = buffer()->getBibkeyList();
551 if (count_if(keys.begin(), keys.end(),
552 lyx::equal_1st_in_pair<string,string>(from))
556 return ChangeInsets(Inset::CITE_CODE, from, to);
560 UpdatableInset * BufferView::theLockingInset() const
562 // If NULL is not allowed we should put an Assert here. (Lgb)
564 return text->the_locking_inset;
569 void BufferView::theLockingInset(UpdatableInset * inset)
571 text->the_locking_inset = inset;
575 LyXText * BufferView::getLyXText() const
577 if (theLockingInset()) {
578 LyXText * txt = theLockingInset()->getLyXText(this, true);
586 LyXText * BufferView::getParentText(Inset * inset) const
588 if (inset->owner()) {
589 LyXText * txt = inset->getLyXText(this);
590 inset = inset->owner();
591 while (inset && inset->getLyXText(this) == txt)
592 inset = inset->owner();
594 return inset->getLyXText(this);
600 Language const * BufferView::getParentLanguage(Inset * inset) const
602 LyXText * text = getParentText(inset);
603 return text->cursor.par()->getFontSettings(buffer()->params,
604 text->cursor.pos()).language();