diff --git a/src/vmdeditoperations.cpp b/src/vmdeditoperations.cpp index df7b3c92..1f3c148c 100644 --- a/src/vmdeditoperations.cpp +++ b/src/vmdeditoperations.cpp @@ -249,6 +249,14 @@ bool VMdEditOperations::handleKeyPressEvent(QKeyEvent *p_event) break; } + case Qt::Key_O: + { + if (handleKeyO(p_event)) { + return true; + } + break; + } + case Qt::Key_U: { if (handleKeyU(p_event)) { @@ -515,6 +523,50 @@ bool VMdEditOperations::handleKeyI(QKeyEvent *p_event) return false; } +bool VMdEditOperations::handleKeyO(QKeyEvent *p_event) +{ + if (p_event->modifiers() == Qt::ControlModifier) { + // Ctrl+O, inline codeblock. + QTextCursor cursor = m_editor->textCursor(); + if (cursor.hasSelection()) { + // Insert ` around the selected text. + int start = cursor.selectionStart(); + int end = cursor.selectionEnd(); + cursor.beginEditBlock(); + cursor.clearSelection(); + cursor.setPosition(start, QTextCursor::MoveAnchor); + cursor.insertText("`"); + cursor.setPosition(end + 1, QTextCursor::MoveAnchor); + cursor.insertText("`"); + cursor.endEditBlock(); + m_editor->setTextCursor(cursor); + } else { + // Insert `` and place cursor in the middle. + // Or if there are one ` after current cursor, just skip it. + cursor.beginEditBlock(); + int pos = cursor.positionInBlock(); + bool hasBackquote = false; + QString text = cursor.block().text(); + if (pos <= text.size() - 1) { + if (text[pos] == '`') { + hasBackquote = true; + } + } + if (hasBackquote) { + cursor.movePosition(QTextCursor::Right, QTextCursor::MoveAnchor, 1); + } else { + cursor.insertText("``"); + cursor.movePosition(QTextCursor::Left, QTextCursor::MoveAnchor, 1); + } + cursor.endEditBlock(); + m_editor->setTextCursor(cursor); + } + p_event->accept(); + return true; + } + return false; +} + bool VMdEditOperations::handleKeyU(QKeyEvent *p_event) { if (p_event->modifiers() == Qt::ControlModifier) { diff --git a/src/vmdeditoperations.h b/src/vmdeditoperations.h index 70f5db7b..57d8c1d7 100644 --- a/src/vmdeditoperations.h +++ b/src/vmdeditoperations.h @@ -36,6 +36,7 @@ private: bool handleKeyD(QKeyEvent *p_event); bool handleKeyH(QKeyEvent *p_event); bool handleKeyI(QKeyEvent *p_event); + bool handleKeyO(QKeyEvent *p_event); bool handleKeyU(QKeyEvent *p_event); bool handleKeyW(QKeyEvent *p_event); bool handleKeyEsc(QKeyEvent *p_event);