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"
36 extern BufferList bufferlist;
47 // Inserts a file into current document
48 bool BufferView::insertLyXFile(string const & filen)
50 // Copyright CHT Software Service GmbH
53 // Insert a Lyxformat - file into current buffer
55 // Moved from lyx_cb.C (Lgb)
57 if (filen.empty()) return false;
59 string const fname = MakeAbsPath(filen);
61 // check if file exist
62 FileInfo const fi(fname);
65 WriteAlert(_("Error!"),
66 _("Specified file is unreadable: "),
67 MakeDisplayPath(fname, 50));
73 ifstream ifs(fname.c_str());
75 WriteAlert(_("Error!"),
76 _("Cannot open specified file: "),
77 MakeDisplayPath(fname, 50));
81 char const c = ifs.peek();
89 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
90 res = buffer()->readFile(lex, text->cursor.par());
92 lyxerr[Debug::INFO] << "Will insert file without header"
94 res = buffer()->readLyXformat2(lex, text->cursor.par());
102 bool BufferView::removeAutoInsets()
104 Paragraph * par = buffer()->paragraph;
106 LyXCursor tmpcursor = text->cursor;
112 // this has to be done before the delete
113 text->setCursor(this, cursor, par, 0);
114 if (par->autoDeleteInsets()){
116 text->redoParagraphs(this, cursor,
117 cursor.par()->next());
118 text->fullRebreak(this);
123 // avoid forbidden cursor positions caused by error removing
124 if (tmpcursor.pos() > tmpcursor.par()->size())
125 tmpcursor.pos(tmpcursor.par()->size());
127 text->setCursorIntern(this, tmpcursor.par(), tmpcursor.pos());
133 void BufferView::insertErrors(TeXErrors & terr)
135 // Save the cursor position
136 LyXCursor cursor = text->cursor;
138 for (TeXErrors::Errors::const_iterator cit = terr.begin();
141 string const desctext(cit->error_desc);
142 string const errortext(cit->error_text);
143 string const msgtxt = desctext + '\n' + errortext;
144 int const errorrow = cit->error_in_line;
146 // Insert error string for row number
150 if (buffer()->texrow.getIdFromRow(errorrow, tmpid, tmppos)) {
151 buffer()->texrow.increasePos(tmpid, tmppos);
154 Paragraph * texrowpar = 0;
157 texrowpar = text->firstParagraph();
160 texrowpar = buffer()->getParFromID(tmpid);
166 InsetError * new_inset = new InsetError(msgtxt);
167 text->setCursorIntern(this, texrowpar, tmppos);
168 text->insertInset(this, new_inset);
169 text->fullRebreak(this);
171 // Restore the cursor position
172 text->setCursorIntern(this, cursor.par(), cursor.pos());
176 void BufferView::setCursorFromRow(int row)
181 buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
183 Paragraph * texrowpar;
186 texrowpar = text->firstParagraph();
189 texrowpar = buffer()->getParFromID(tmpid);
191 text->setCursor(this, texrowpar, tmppos);
195 bool BufferView::insertInset(Inset * inset, string const & lout)
197 return pimpl_->insertInset(inset, lout);
201 /* This is also a buffer property (ale) */
202 // Not so sure about that. a goto Label function can not be buffer local, just
203 // think how this will work in a multiwindo/buffer environment, all the
204 // cursors in all the views showing this buffer will move. (Lgb)
205 // OK, then no cursor action should be allowed in buffer. (ale)
206 bool BufferView::gotoLabel(string const & label)
209 for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
210 it != buffer()->inset_iterator_end(); ++it) {
211 vector<string> labels = (*it)->getLabelList();
212 if (find(labels.begin(),labels.end(),label)
215 text->setCursor(this, it.getPar(), it.getPos());
216 text->selection.cursor = text->cursor;
217 update(text, BufferView::SELECT|BufferView::FITCUR);
225 void BufferView::menuUndo()
228 owner()->message(_("Undo"));
231 update(text, BufferView::SELECT|BufferView::FITCUR);
233 owner()->message(_("No forther undo information"));
235 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
241 void BufferView::menuRedo()
243 if (theLockingInset()) {
244 owner()->message(_("Redo not yet supported in math mode"));
249 owner()->message(_("Redo"));
252 update(text, BufferView::SELECT|BufferView::FITCUR);
254 owner()->message(_("No further redo information"));
256 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
262 void BufferView::copyEnvironment()
265 text->copyEnvironmentType();
266 // clear the selection, even if mark_set
268 text->clearSelection();
269 update(text, BufferView::SELECT|BufferView::FITCUR);
270 owner()->message(_("Paragraph environment type copied"));
275 void BufferView::pasteEnvironment()
278 text->pasteEnvironmentType(this);
279 owner()->message(_("Paragraph environment type set"));
280 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
285 void BufferView::copy()
288 text->copySelection(this);
289 // clear the selection, even if mark_set
291 text->clearSelection();
292 update(text, BufferView::SELECT|BufferView::FITCUR);
293 owner()->message(_("Copy"));
298 void BufferView::cut(bool realcut)
302 update(text, BufferView::SELECT|BufferView::FITCUR);
303 text->cutSelection(this, true, realcut);
304 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
305 owner()->message(_("Cut"));
310 void BufferView::paste()
312 if (!available()) return;
314 owner()->message(_("Paste"));
317 // clear the selection
319 text->clearSelection();
320 update(text, BufferView::SELECT|BufferView::FITCUR);
323 text->pasteSelection(this);
324 update(text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
326 // clear the selection
328 text->clearSelection();
329 update(text, BufferView::SELECT|BufferView::FITCUR);
333 /* these functions are for the spellchecker */
334 string const BufferView::nextWord(float & value)
341 return text->selectNextWord(this, value);
345 void BufferView::selectLastWord()
347 if (!available()) return;
351 text->selectSelectedWord(this);
352 toggleSelection(false);
353 update(text, BufferView::SELECT|BufferView::FITCUR);
357 void BufferView::endOfSpellCheck()
359 if (!available()) return;
363 text->selectSelectedWord(this);
364 text->clearSelection();
365 update(text, BufferView::SELECT|BufferView::FITCUR);
369 void BufferView::replaceWord(string const & replacestring)
371 if (!available()) return;
373 LyXText * tt = getLyXText();
375 update(tt, BufferView::SELECT|BufferView::FITCUR);
377 /* clear the selection (if there is any) */
378 toggleSelection(false);
379 update(tt, BufferView::SELECT|BufferView::FITCUR);
381 /* clear the selection (if there is any) */
382 toggleSelection(false);
383 tt->replaceSelectionWithString(this, replacestring);
385 tt->setSelectionOverString(this, replacestring);
387 // Go back so that replacement string is also spellchecked
388 for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
389 tt->cursorLeft(this);
391 update(tt, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
393 // End of spellchecker stuff
396 bool BufferView::lockInset(UpdatableInset * inset)
398 if (!theLockingInset() && inset) {
399 theLockingInset(inset);
402 return theLockingInset()->lockInsetInInset(this, inset);
408 void BufferView::showLockedInsetCursor(int x, int y, int asc, int desc)
410 if (available() && theLockingInset()) {
411 LyXCursor cursor = text->cursor;
412 if ((cursor.pos() - 1 >= 0) &&
413 (cursor.par()->getChar(cursor.pos() - 1) ==
414 Paragraph::META_INSET) &&
415 (cursor.par()->getInset(cursor.pos() - 1) ==
416 theLockingInset()->getLockingInset()))
417 text->setCursor(this, cursor,
418 cursor.par(), cursor.pos() - 1);
419 LyXScreen::Cursor_Shape shape = LyXScreen::BAR_SHAPE;
420 LyXText * txt = getLyXText();
421 if (theLockingInset()->getLockingInset()->lyxCode() ==
423 (txt->real_current_font.language() !=
424 buffer()->params.language
425 || txt->real_current_font.isVisibleRightToLeft()
426 != buffer()->params.language->RightToLeft()))
427 shape = (txt->real_current_font.isVisibleRightToLeft())
428 ? LyXScreen::REVERSED_L_SHAPE
429 : LyXScreen::L_SHAPE;
430 y += cursor.y() + theLockingInset()->insetInInsetY();
431 pimpl_->screen_->showManualCursor(text, x, y, asc, desc,
437 void BufferView::hideLockedInsetCursor()
439 if (theLockingInset() && available()) {
440 pimpl_->screen_->hideCursor();
445 void BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
447 if (theLockingInset() && available()) {
448 y += text->cursor.y() + theLockingInset()->insetInInsetY();
449 if (pimpl_->screen_->fitManualCursor(text, this, x, y, asc, desc))
455 int BufferView::unlockInset(UpdatableInset * inset)
457 if (inset && theLockingInset() == inset) {
458 inset->insetUnlock(this);
462 } else if (inset && theLockingInset() &&
463 theLockingInset()->unlockInsetInInset(this, inset)) {
467 return bufferlist.unlockInset(inset);
471 void BufferView::lockedInsetStoreUndo(Undo::undo_kind kind)
473 if (!theLockingInset())
474 return; // shouldn't happen
475 if (kind == Undo::EDIT) // in this case insets would not be stored!
479 text->cursor.par()->next());
483 void BufferView::updateInset(Inset * inset, bool mark_dirty)
485 pimpl_->updateInset(inset, mark_dirty);
489 bool BufferView::ChangeInsets(Inset::Code code,
490 string const & from, string const & to)
493 Paragraph * par = buffer()->paragraph;
494 LyXCursor cursor = text->cursor;
495 LyXCursor tmpcursor = cursor;
496 cursor.par(tmpcursor.par());
497 cursor.pos(tmpcursor.pos());
501 for (Paragraph::inset_iterator it = par->inset_iterator_begin();
502 it != par->inset_iterator_end(); ++it) {
503 if ((*it)->lyxCode() == code) {
504 InsetCommand * inset = static_cast<InsetCommand *>(*it);
505 if (inset->getContents() == from) {
506 inset->setContents(to);
513 // this is possible now, since SetCursor takes
514 // care about footnotes
515 text->setCursorIntern(this, par, 0);
516 text->redoParagraphs(this, text->cursor,
517 text->cursor.par()->next());
518 text->fullRebreak(this);
522 text->setCursorIntern(this, cursor.par(), cursor.pos());
527 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
529 // Check if the label 'from' appears more than once
530 vector<string> labels = buffer()->getLabelList();
531 if (count(labels.begin(), labels.end(), from) > 1)
534 return ChangeInsets(Inset::REF_CODE, from, to);
538 bool BufferView::ChangeCitationsIfUnique(string const & from, string const & to)
541 vector<pair<string,string> > keys = buffer()->getBibkeyList();
542 if (count_if(keys.begin(), keys.end(),
543 lyx::equal_1st_in_pair<string,string>(from))
547 return ChangeInsets(Inset::CITE_CODE, from, to);
551 UpdatableInset * BufferView::theLockingInset() const
553 // If NULL is not allowed we should put an Assert here. (Lgb)
555 return text->the_locking_inset;
560 void BufferView::theLockingInset(UpdatableInset * inset)
562 text->the_locking_inset = inset;
566 LyXText * BufferView::getLyXText() const
568 if (theLockingInset()) {
569 LyXText * txt = theLockingInset()->getLyXText(this, true);
577 LyXText * BufferView::getParentText(Inset * inset) const
579 if (inset->owner()) {
580 LyXText * txt = inset->getLyXText(this);
581 inset = inset->owner();
582 while (inset && inset->getLyXText(this) == txt)
583 inset = inset->owner();
585 return inset->getLyXText(this);
591 Language const * BufferView::getParentLanguage(Inset * inset) const
593 LyXText * text = getParentText(inset);
594 return text->cursor.par()->getFontSettings(buffer()->params,
595 text->cursor.pos()).language();