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);
332 /* these functions are for the spellchecker */
333 string const BufferView::nextWord(float & value)
340 return text->selectNextWord(this, value);
344 void BufferView::selectLastWord()
346 if (!available()) return;
350 text->selectSelectedWord(this);
351 toggleSelection(false);
352 update(text, BufferView::SELECT|BufferView::FITCUR);
356 void BufferView::endOfSpellCheck()
358 if (!available()) return;
362 text->selectSelectedWord(this);
363 text->clearSelection(this);
364 update(text, BufferView::SELECT|BufferView::FITCUR);
368 void BufferView::replaceWord(string const & replacestring)
370 if (!available()) return;
372 LyXText * tt = getLyXText();
374 update(tt, BufferView::SELECT|BufferView::FITCUR);
376 /* clear the selection (if there is any) */
377 toggleSelection(false);
378 update(tt, BufferView::SELECT|BufferView::FITCUR);
380 /* clear the selection (if there is any) */
381 toggleSelection(false);
382 tt->replaceSelectionWithString(this, replacestring);
384 tt->setSelectionOverString(this, replacestring);
386 // Go back so that replacement string is also spellchecked
387 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
388 tt->cursorLeft(this);
390 update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
392 // End of spellchecker stuff
395 bool BufferView::lockInset(UpdatableInset * inset)
397 if (!theLockingInset() && inset) {
398 theLockingInset(inset);
401 return theLockingInset()->lockInsetInInset(this, inset);
407 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
409 if (theLockingInset() && available()) {
410 LyXCursor cursor = text->cursor;
411 if ((cursor.pos() - 1 >= 0) &&
412 (cursor.par()->getChar(cursor.pos() - 1) ==
413 Paragraph::META_INSET) &&
414 (cursor.par()->getInset(cursor.pos() - 1) ==
415 theLockingInset()->getLockingInset()))
416 text->setCursor(this, cursor,
417 cursor.par(), cursor.pos() - 1);
418 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
419 LyXText * txt = getLyXText();
420 if (theLockingInset()->getLockingInset()->lyxCode() ==
422 (txt->real_current_font.language() !=
423 buffer()->params.language
424 || txt->real_current_font.isVisibleRightToLeft()
425 != buffer()->params.language->RightToLeft()))
426 shape = (txt->real_current_font.isVisibleRightToLeft())
427 ? LyXScreen::REVERSED_L_SHAPE
428 : LyXScreen::L_SHAPE;
429 y += cursor.y() + theLockingInset()->insetInInsetY();
430 pimpl_->screen_->showManualCursor(text, x, y, asc, desc,
436 void BufferView::hideLockedInsetCursor()
438 if (theLockingInset() && available()) {
439 pimpl_->screen_->hideCursor();
444 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
446 if (theLockingInset() && available()) {
447 y += text->cursor.y() + theLockingInset()->insetInInsetY();
448 if (pimpl_->screen_->fitManualCursor(text, this, x, y, asc, desc))
454 int BufferView::unlockInset(UpdatableInset * inset)
456 if (inset && theLockingInset() == inset) {
457 inset->insetUnlock(this);
461 } else if (inset && theLockingInset() &&
462 theLockingInset()->unlockInsetInInset(this, inset)) {
466 return bufferlist.unlockInset(inset);
470 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
472 if (!theLockingInset())
473 return; // shouldn't happen
474 if (kind == Undo::EDIT) // in this case insets would not be stored!
478 text->cursor.par()->next());
482 void BufferView::updateInset(Inset * inset, bool mark_dirty)
484 pimpl_->updateInset(inset, mark_dirty);
488 bool BufferView::ChangeInsets(Inset::Code code,
489 string const & from, string const & to)
492 Paragraph * par = buffer()->paragraph;
493 LyXCursor cursor = text->cursor;
494 LyXCursor tmpcursor = cursor;
495 cursor.par(tmpcursor.par());
496 cursor.pos(tmpcursor.pos());
500 for (Paragraph::inset_iterator it = par->inset_iterator_begin();
501 it != par->inset_iterator_end(); ++it) {
502 if ((*it)->lyxCode() == code) {
503 InsetCommand * inset = static_cast<InsetCommand *>(*it);
504 if (inset->getContents() == from) {
505 inset->setContents(to);
512 // this is possible now, since SetCursor takes
513 // care about footnotes
514 text->setCursorIntern(this, par, 0);
515 text->redoParagraphs(this, text->cursor,
516 text->cursor.par()->next());
517 text->fullRebreak(this);
521 text->setCursorIntern(this, cursor.par(), cursor.pos());
526 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
528 // Check if the label 'from' appears more than once
529 vector<string> labels = buffer()->getLabelList();
530 if (count(labels.begin(), labels.end(), from) > 1)
533 return ChangeInsets(Inset::REF_CODE, from, to);
537 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
540 vector<pair<string,string> > keys = buffer()->getBibkeyList();
541 if (count_if(keys.begin(), keys.end(),
542 lyx::equal_1st_in_pair<string,string>(from))
546 return ChangeInsets(Inset::CITE_CODE, from, to);
550 UpdatableInset * BufferView::theLockingInset() const
552 // If NULL is not allowed we should put an Assert here. (Lgb)
554 return text->the_locking_inset;
559 void BufferView::theLockingInset(UpdatableInset * inset)
561 text->the_locking_inset = inset;
565 LyXText * BufferView::getLyXText() const
567 if (theLockingInset()) {
568 LyXText * txt = theLockingInset()->getLyXText(this, true);
576 LyXText * BufferView::getParentText(Inset * inset) const
578 if (inset->owner()) {
579 LyXText * txt = inset->getLyXText(this);
580 inset = inset->owner();
581 while (inset && inset->getLyXText(this) == txt)
582 inset = inset->owner();
584 return inset->getLyXText(this);
590 Language const * BufferView::getParentLanguage(Inset * inset) const
592 LyXText * text = getParentText(inset);
593 return text->cursor.par()->getFontSettings(buffer()->params,
594 text->cursor.pos()).language();