Ejemplo n.º 1
0
void GuiSearch::wrap_dispatch(const FuncRequest & func, bool forward)
{
	dispatch(func);

	BufferView * bv = const_cast<BufferView *>(bufferview());

	if (!bv->cursor().result().dispatched()) {
		GuiView & lv = *const_cast<GuiView *>(&lyxview());
		DocIterator cur_orig(bv->cursor());
		docstring q;
		if (forward)
			q = _("End of file reached while searching forward.\n"
			  "Continue searching from the beginning?");
		else
			q = _("Beginning of file reached while searching backward.\n"
			  "Continue searching from the end?");
		int wrap_answer = frontend::Alert::prompt(_("Wrap search?"),
			q, 0, 1, _("&Yes"), _("&No"));
		if (wrap_answer == 0) {
			if (forward) {
				bv->cursor().clear();
				bv->cursor().push_back(CursorSlice(bv->buffer().inset()));
			} else {
				bv->cursor().setCursor(doc_iterator_end(&bv->buffer()));
				bv->cursor().backwardPos();
			}
			bv->clearSelection();
			dispatch(func);
			if (bv->cursor().result().dispatched())
				return;
		}
		bv->cursor().setCursor(cur_orig);
		lv.message(_("String not found."));
	}
}
Ejemplo n.º 2
0
/** Copy selected elements from bv's BufferParams to the dest_bv's one
 ** We don't want to copy'em all, e.g., not the default master **/
static void copy_params(BufferView const & bv, BufferView & dest_bv) {
	Buffer const & doc_buf = bv.buffer();
	BufferParams const & doc_bp = doc_buf.params();
	string const & lang = doc_bp.language->lang();
	string const & doc_class = doc_bp.documentClass().name();
	Buffer & dest_buf = dest_bv.buffer();
	dest_buf.params().setLanguage(lang);
	dest_buf.params().setBaseClass(doc_class);
	dest_buf.params().makeDocumentClass();
	dest_bv.cursor().current_font.setLanguage(doc_bp.language);
}
Ejemplo n.º 3
0
void SpellcheckerWidget::Private::hide() const
{
	BufferView * bv = gv_->documentBufferView();
	Cursor & bvcur = bv->cursor();
	dv_->hide();
	if (isCurrentBuffer(bvcur)) {
		if (!begin_.empty() && !end_.empty()) {
			// restore previous selection
			setSelection(begin_, end_);
		} else {
			// restore cursor position
			bvcur.setCursor(start_);
			bvcur.clearSelection();
			bv->processUpdateFlags(Update::Force | Update::FitCursor);	
		}
	}
}
Ejemplo n.º 4
0
void SpellcheckerWidget::Private::setSelection(
	DocIterator const & from, DocIterator const & to) const
{
	BufferView * bv = gv_->documentBufferView();
	DocIterator end = to;

	if (from.pit() != end.pit()) {
		// there are multiple paragraphs in selection 
		Cursor & bvcur = bv->cursor();
		bvcur.setCursor(from);
		bvcur.clearSelection();
		bvcur.setSelection(true);
		bvcur.setCursor(end);
		bvcur.setSelection(true);
	} else {
		// FIXME LFUN
		// If we used a LFUN, dispatch would do all of this for us
		int const size = end.pos() - from.pos();
		bv->putSelectionAt(from, size, false);
	}
	bv->processUpdateFlags(Update::Force | Update::FitCursor);	
}
Ejemplo n.º 5
0
/// Return true if a match was found
bool FindAndReplaceWidget::findAndReplaceScope(FindAndReplaceOptions & opt, bool replace_all)
{
	BufferView * bv = view_.documentBufferView();
	if (!bv)
		return false;
	Buffer * buf = &bv->buffer();
	Buffer * buf_orig = &bv->buffer();
	DocIterator cur_orig(bv->cursor());
	int wrap_answer = -1;
	ostringstream oss;
	oss << opt;
	FuncRequest cmd(LFUN_WORD_FINDADV, from_utf8(oss.str()));

	view_.setBusy(true);
	if (opt.scope == FindAndReplaceOptions::S_ALL_MANUALS) {
		vector<string> const & v = allManualsFiles();
		if (std::find(v.begin(), v.end(), buf->absFileName()) == v.end()) {
			FileName const & fname = FileName(*v.begin());
			if (!theBufferList().exists(fname)) {
				guiApp->currentView()->setBusy(false);
				guiApp->currentView()->loadDocument(fname, false);
				guiApp->currentView()->setBusy(true);
			}
			buf = theBufferList().getBuffer(fname);
			lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
						  buf->absFileName()));
			bv = view_.documentBufferView();
			bv->cursor().clear();
			bv->cursor().push_back(CursorSlice(buf->inset()));
		}
	}

	do {
		LYXERR(Debug::FIND, "Dispatching LFUN_WORD_FINDADV");
		dispatch(cmd);
		LYXERR(Debug::FIND, "dispatched");
		if (bv->cursor().result().dispatched()) {
			// New match found and selected (old selection replaced if needed)
			if (replace_all)
				continue;
			view_.setBusy(false);
			return true;
		}

		// No match found in current buffer (however old selection might have been replaced)
		// select next buffer in scope, if any
		bool prompt = nextPrevBuffer(buf, opt);
		if (prompt) {
			if (wrap_answer != -1)
				break;
			docstring q = getQuestionString(opt);
			view_.setBusy(false);
			wrap_answer = frontend::Alert::prompt(
				_("Wrap search?"), q,
				0, 1, _("&Yes"), _("&No"));
			view_.setBusy(true);
			if (wrap_answer == 1)
				break;
		}
		if (buf != &view_.documentBufferView()->buffer())
			lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
						  buf->absFileName()));
		bv = view_.documentBufferView();
		if (opt.forward) {
			bv->cursor().clear();
			bv->cursor().push_back(CursorSlice(buf->inset()));
		} else {
			//lyx::dispatch(FuncRequest(LFUN_BUFFER_END));
			bv->cursor().setCursor(doc_iterator_end(buf));
			bv->cursor().backwardPos();
			LYXERR(Debug::FIND, "findBackAdv5: cur: "
				<< bv->cursor());
		}
		bv->clearSelection();
	} while (wrap_answer != 1);
	if (buf_orig != &view_.documentBufferView()->buffer())
		lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH,
					  buf_orig->absFileName()));
	bv = view_.documentBufferView();
	// This may happen after a replace occurred
	if (cur_orig.pos() > cur_orig.lastpos())
		cur_orig.pos() = cur_orig.lastpos();
	bv->cursor().setCursor(cur_orig);
	view_.setBusy(false);
	return false;
}
Ejemplo n.º 6
0
void GuiCompleter::tab()
{
	BufferView * bv = &gui_->bufferView();
	Cursor cur = bv->cursor();
	cur.screenUpdateFlags(Update::None);
	
	// check that inline completion is active
	if (!inlineVisible() && !uniqueCompletionAvailable()) {
		// try to activate the inline completion
		if (cur.inset().inlineCompletionSupported(cur)) {
			showInline();
			
			// show popup without delay because the completion was not unique
			if (lyxrc.completion_popup_after_complete
			    && !popupVisible()
			    && popup()->model()->rowCount() > 1)
				popup_timer_.start(0);

			return;
		}
		// or try popup
		if (!popupVisible() && cur.inset().completionSupported(cur)) {
			showPopup();
			return;
		}
		
		return;
	}
	
	// Make undo possible
	cur.recordUndo();

	// If completion is active, at least complete by one character
	docstring prefix = cur.inset().completionPrefix(cur);
	docstring completion = qstring_to_ucs4(currentCompletion());
	if (completion.size() <= prefix.size()) {
		// finalize completion
		cur.inset().insertCompletion(cur, docstring(), true);
		
		// hide popup and inline completion
		hidePopup(cur);
		hideInline(cur);
		updateVisibility(false, false);
		return;
	}
	docstring nextchar = completion.substr(prefix.size(), 1);
	if (!cur.inset().insertCompletion(cur, nextchar, false))
		return;
	updatePrefix(cur);

	// try to complete as far as it is unique
	docstring longestCompletion = longestUniqueCompletion();
	prefix = cur.inset().completionPrefix(cur);
	docstring postfix = longestCompletion.substr(min(longestCompletion.size(), prefix.size()));
	cur.inset().insertCompletion(cur, postfix, false);
	old_cursor_ = bv->cursor();
	updatePrefix(cur);

	// show popup without delay because the completion was not unique
	if (lyxrc.completion_popup_after_complete
	    && !popupVisible()
	    && popup()->model()->rowCount() > 1)
		popup_timer_.start(0);

	// redraw if needed
	if (cur.result().update())
		gui_->bufferView().processUpdateFlags(cur.result().update());
}
Ejemplo n.º 7
0
void SpellcheckerWidget::Private::check()
{
	BufferView * bv = gv_->documentBufferView();
	if (!bv || bv->buffer().text().empty())
		return;

	fixPositionsIfBroken();
	
	SpellChecker * speller = theSpellChecker();
	if (speller && !speller->hasDictionary(bv->buffer().language())) {
		int dsize = speller->numDictionaries();
		if (0 == dsize) {
			hide();
			QMessageBox::information(p,
				qt_("Spell Checker"),
				qt_("Spell checker has no dictionaries."));
			return;
		}
	}

	DocIterator from = bv->cursor();
	DocIterator to = isCurrentBuffer(from) ? end_ : doc_iterator_end(&bv->buffer());
	WordLangTuple word_lang;
	docstring_list suggestions;

	LYXERR(Debug::GUI, "Spellchecker: start check at " << from);
	try {
		bv->buffer().spellCheck(from, to, word_lang, suggestions);
	} catch (ExceptionMessage const & message) {
		if (message.type_ == WarningException) {
			Alert::warning(message.title_, message.details_);
			return;
		}
		throw message;
	}

	// end of document or selection?
	if (atLastPos(from)) {
		if (isWrapAround()) {
			hide();
			return;
		}
		if (continueFromBeginning())
			check();
		return;
	}

	if (isWrapAround(from)) {
		hide();
		return;
	}

	word_ = word_lang;

	// set suggestions
	updateSuggestions(suggestions);
	// set language
	if (!word_lang.lang())
		return;
	setLanguage(word_lang.lang());
	// mark misspelled word
	setSelection(from, to);
}
Ejemplo n.º 8
0
DocIterator const SpellcheckerWidget::Private::cursor() const
{
	BufferView * bv = gv_->documentBufferView();
	return bv ? bv->cursor() : DocIterator();
}