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 extern BufferList bufferlist;
46 // Inserts a file into current document
47 bool BufferView::insertLyXFile(string const & filen)
49 // Copyright CHT Software Service GmbH
52 // Insert a Lyxformat - file into current buffer
54 // Moved from lyx_cb.C (Lgb)
56 if (filen.empty()) return false;
58 string const fname = MakeAbsPath(filen);
60 // check if file exist
61 FileInfo const fi(fname);
64 WriteAlert(_("Error!"),
65 _("Specified file is unreadable: "),
66 MakeDisplayPath(fname, 50));
72 ifstream ifs(fname.c_str());
74 WriteAlert(_("Error!"),
75 _("Cannot open specified file: "),
76 MakeDisplayPath(fname, 50));
80 char const c = ifs.peek();
88 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
89 res = buffer()->readFile(lex, text->cursor.par());
91 lyxerr[Debug::INFO] << "Will insert file without header"
93 res = buffer()->readLyXformat2(lex, text->cursor.par());
101 bool BufferView::removeAutoInsets()
103 Paragraph * par = buffer()->paragraph;
105 LyXCursor tmpcursor = text->cursor;
111 // this has to be done before the delete
112 text->setCursor(this, cursor, par, 0);
113 if (par->autoDeleteInsets()){
115 text->redoParagraphs(this, cursor,
116 cursor.par()->next());
117 text->fullRebreak(this);
122 // avoid forbidden cursor positions caused by error removing
123 if (tmpcursor.pos() > tmpcursor.par()->size())
124 tmpcursor.pos(tmpcursor.par()->size());
126 text->setCursorIntern(this, tmpcursor.par(), tmpcursor.pos());
132 void BufferView::insertErrors(TeXErrors & terr)
134 // Save the cursor position
135 LyXCursor cursor = text->cursor;
137 for (TeXErrors::Errors::const_iterator cit = terr.begin();
140 string const desctext(cit->error_desc);
141 string const errortext(cit->error_text);
142 string const msgtxt = desctext + '\n' + errortext;
143 int const errorrow = cit->error_in_line;
145 // Insert error string for row number
149 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
150 buffer()->texrow.increasePos(tmpid, tmppos);
153 Paragraph * texrowpar = 0;
156 texrowpar = text->firstParagraph();
159 texrowpar = buffer()->getParFromID(tmpid);
165 InsetError * new_inset = new InsetError(msgtxt);
166 text->setCursorIntern(this, texrowpar, tmppos);
167 text->insertInset(this, new_inset);
168 text->fullRebreak(this);
170 // Restore the cursor position
171 text->setCursorIntern(this, cursor.par(), cursor.pos());
175 void BufferView::setCursorFromRow(int row)
180 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
182 Paragraph * texrowpar;
185 texrowpar = text->firstParagraph();
188 texrowpar = buffer()->getParFromID(tmpid);
190 text->setCursor(this, texrowpar, tmppos);
194 bool BufferView::insertInset(Inset * inset, string const & lout)
196 return pimpl_->insertInset(inset, lout);
200 /* This is also a buffer property (ale) */
201 // Not so sure about that. a goto Label function can not be buffer local, just
202 // think how this will work in a multiwindo/buffer environment, all the
203 // cursors in all the views showing this buffer will move. (Lgb)
204 // OK, then no cursor action should be allowed in buffer. (ale)
205 bool BufferView::gotoLabel(string const & label)
208 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
209 it != buffer()->inset_iterator_end(); ++it) {
210 vector<string> labels = (*it)->getLabelList();
211 if (find(labels.begin(),labels.end(),label)
214 text->setCursor(this, it.getPar(), it.getPos());
215 text->selection.cursor = text->cursor;
216 update(text, BufferView::SELECT|BufferView::FITCUR);
224 void BufferView::menuUndo()
227 owner()->message(_("Undo"));
230 update(text, BufferView::SELECT|BufferView::FITCUR);
232 owner()->message(_("No forther undo information"));
234 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
240 void BufferView::menuRedo()
242 if (theLockingInset()) {
243 owner()->message(_("Redo not yet supported in math mode"));
248 owner()->message(_("Redo"));
251 update(text, BufferView::SELECT|BufferView::FITCUR);
253 owner()->message(_("No further redo information"));
255 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
261 void BufferView::copyEnvironment()
264 text->copyEnvironmentType();
265 // clear the selection, even if mark_set
267 text->clearSelection(this);
268 update(text, BufferView::SELECT|BufferView::FITCUR);
269 owner()->message(_("Paragraph environment type copied"));
274 void BufferView::pasteEnvironment()
277 text->pasteEnvironmentType(this);
278 owner()->message(_("Paragraph environment type set"));
279 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
284 void BufferView::copy()
287 text->copySelection(this);
288 // clear the selection, even if mark_set
290 text->clearSelection(this);
291 update(text, BufferView::SELECT|BufferView::FITCUR);
292 owner()->message(_("Copy"));
297 void BufferView::cut()
301 update(text, BufferView::SELECT|BufferView::FITCUR);
302 text->cutSelection(this);
303 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
304 owner()->message(_("Cut"));
309 void BufferView::paste()
311 if (!available()) return;
313 owner()->message(_("Paste"));
316 // clear the selection
318 text->clearSelection(this);
319 update(text, BufferView::SELECT|BufferView::FITCUR);
322 text->pasteSelection(this);
323 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
325 // clear the selection
327 text->clearSelection(this);
328 update(text, BufferView::SELECT|BufferView::FITCUR);
334 void BufferView::insertCorrectQuote()
338 if (text->cursor.pos())
339 c = text->cursor.par()->getChar(text->cursor.pos() - 1);
343 insertInset(new InsetQuotes(c, buffer()->params));
347 /* these functions are for the spellchecker */
348 string const BufferView::nextWord(float & value)
355 return text->selectNextWord(this, value);
359 void BufferView::selectLastWord()
361 if (!available()) return;
365 text->selectSelectedWord(this);
366 toggleSelection(false);
367 update(text, BufferView::SELECT|BufferView::FITCUR);
371 void BufferView::endOfSpellCheck()
373 if (!available()) return;
377 text->selectSelectedWord(this);
378 text->clearSelection(this);
379 update(text, BufferView::SELECT|BufferView::FITCUR);
383 void BufferView::replaceWord(string const & replacestring)
385 if (!available()) return;
388 update(text, BufferView::SELECT|BufferView::FITCUR);
390 /* clear the selection (if there is any) */
391 toggleSelection(false);
392 update(text, BufferView::SELECT|BufferView::FITCUR);
394 /* clear the selection (if there is any) */
395 toggleSelection(false);
396 text->replaceSelectionWithString(this, replacestring);
398 text->setSelectionOverString(this, replacestring);
400 // Go back so that replacement string is also spellchecked
401 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
402 text->cursorLeft(this);
404 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
406 // End of spellchecker stuff
409 bool BufferView::lockInset(UpdatableInset * inset)
411 if (!theLockingInset() && inset) {
412 theLockingInset(inset);
415 return theLockingInset()->lockInsetInInset(this, inset);
421 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
423 if (theLockingInset() && available()) {
424 LyXCursor cursor = text->cursor;
425 if ((cursor.pos() - 1 >= 0) &&
426 (cursor.par()->getChar(cursor.pos() - 1) ==
427 Paragraph::META_INSET) &&
428 (cursor.par()->getInset(cursor.pos() - 1) ==
429 theLockingInset()->getLockingInset()))
430 text->setCursor(this, cursor,
431 cursor.par(), cursor.pos() - 1);
432 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
433 LyXText * txt = getLyXText();
434 if (theLockingInset()->getLockingInset()->lyxCode() ==
436 (txt->real_current_font.language() !=
437 buffer()->params.language
438 || txt->real_current_font.isVisibleRightToLeft()
439 != buffer()->params.language->RightToLeft()))
440 shape = (txt->real_current_font.isVisibleRightToLeft())
441 ? LyXScreen::REVERSED_L_SHAPE
442 : LyXScreen::L_SHAPE;
443 y += cursor.y() + theLockingInset()->insetInInsetY();
444 pimpl_->screen_->showManualCursor(text, x, y, asc, desc,
450 void BufferView::hideLockedInsetCursor()
452 if (theLockingInset() && available()) {
453 pimpl_->screen_->hideCursor();
458 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
460 if (theLockingInset() && available()) {
461 y += text->cursor.y() + theLockingInset()->insetInInsetY();
462 if (pimpl_->screen_->fitManualCursor(text, this, x, y, asc, desc))
468 int BufferView::unlockInset(UpdatableInset * inset)
470 if (inset && theLockingInset() == inset) {
471 inset->insetUnlock(this);
475 } else if (inset && theLockingInset() &&
476 theLockingInset()->unlockInsetInInset(this, inset)) {
480 return bufferlist.unlockInset(inset);
484 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
486 if (!theLockingInset())
487 return; // shouldn't happen
488 if (kind == Undo::EDIT) // in this case insets would not be stored!
492 text->cursor.par()->next());
496 void BufferView::updateInset(Inset * inset, bool mark_dirty)
498 pimpl_->updateInset(inset, mark_dirty);
502 bool BufferView::ChangeInsets(Inset::Code code,
503 string const & from, string const & to)
506 Paragraph * par = buffer()->paragraph;
507 LyXCursor cursor = text->cursor;
508 LyXCursor tmpcursor = cursor;
509 cursor.par(tmpcursor.par());
510 cursor.pos(tmpcursor.pos());
514 for (Paragraph::inset_iterator it = par->inset_iterator_begin();
515 it != par->inset_iterator_end(); ++it) {
516 if ((*it)->lyxCode() == code) {
517 InsetCommand * inset = static_cast<InsetCommand *>(*it);
518 if (inset->getContents() == from) {
519 inset->setContents(to);
526 // this is possible now, since SetCursor takes
527 // care about footnotes
528 text->setCursorIntern(this, par, 0);
529 text->redoParagraphs(this, text->cursor,
530 text->cursor.par()->next());
531 text->fullRebreak(this);
535 text->setCursorIntern(this, cursor.par(), cursor.pos());
540 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
542 // Check if the label 'from' appears more than once
543 vector<string> labels = buffer()->getLabelList();
544 if (count(labels.begin(), labels.end(), from) > 1)
547 return ChangeInsets(Inset::REF_CODE, from, to);
551 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
554 vector<pair<string,string> > keys = buffer()->getBibkeyList();
555 if (count_if(keys.begin(), keys.end(),
556 lyx::equal_1st_in_pair<string,string>(from))
560 return ChangeInsets(Inset::CITE_CODE, from, to);
564 UpdatableInset * BufferView::theLockingInset() const
566 // If NULL is not allowed we should put an Assert here. (Lgb)
568 return text->the_locking_inset;
573 void BufferView::theLockingInset(UpdatableInset * inset)
575 text->the_locking_inset = inset;
579 LyXText * BufferView::getLyXText() const
581 if (theLockingInset()) {
582 LyXText * txt = theLockingInset()->getLyXText(this, true);
590 LyXText * BufferView::getParentText(Inset * inset) const
592 if (inset->owner()) {
593 LyXText * txt = inset->getLyXText(this);
594 inset = inset->owner();
595 while (inset && inset->getLyXText(this) == txt)
596 inset = inset->owner();
598 return inset->getLyXText(this);
604 Language const * BufferView::getParentLanguage(Inset * inset) const
606 LyXText * text = getParentText(inset);
607 return text->cursor.par()->getFontSettings(buffer()->params,
608 text->cursor.pos()).language();