8#include "katedocument.h"
10#include <vimode/inputmodemanager.h>
11#include <vimode/modes/normalvimode.h>
13#include <KLocalizedString>
15using namespace KateVi;
35Marks::Marks(InputModeManager *imm)
36 : m_inputModeManager(imm)
37 , m_doc(imm->view()->doc())
38 , m_settingMark(false)
45 QStringList marks = config.
readEntry(
"ViMarks", QStringList());
46 for (
int i = 0; i + 2 < marks.
size(); i += 3) {
47 KTextEditor::Cursor c(marks.
at(i + 1).toInt(), marks.
at(i + 2).toInt());
48 setMark(marks.
at(i).at(0), c);
51 syncViMarksAndBookmarks();
54void Marks::writeSessionConfig(KConfigGroup &config)
const
56 if (m_marks.isEmpty()) {
62 for (
const auto &[key, value] : m_marks.asKeyValueRange()) {
68void Marks::setMark(
const QChar &_mark,
const KTextEditor::Cursor pos)
72 const bool moveoninsert = _mark != BeginEditYanked;
77 const QChar mark = (_mark == BeforeJumpAlter) ? BeforeJump : _mark;
80 bool needToAdjustVisibleMark =
true;
81 if (KTextEditor::MovingCursor *oldCursor = m_marks.value(mark)) {
83 needToAdjustVisibleMark = oldCursor->line() != pos.
line();
84 if (needToAdjustVisibleMark) {
85 int number_of_marks = 0;
86 const auto keys = m_marks.keys();
87 for (QChar c : keys) {
88 if (m_marks.value(c)->line() == oldCursor->line()) {
92 if (number_of_marks == 1) {
98 oldCursor->setPosition(pos);
103 m_marks.insert(mark, m_doc->newMovingCursor(pos, behavior));
107 if (isShowable(mark)) {
114 if (m_doc->activeView() == m_inputModeManager->view()) {
115 m_inputModeManager->getViNormalMode()->message(
i18n(
"Mark set: %1", mark));
120 m_settingMark =
false;
123KTextEditor::Cursor Marks::getMarkPosition(
const QChar &mark)
const
125 if (m_marks.contains(mark)) {
126 KTextEditor::MovingCursor *c = m_marks.value(mark);
127 return KTextEditor::Cursor(c->
line(), c->
column());
137 if (mark.
type != KTextEditor::Document::Bookmark || m_settingMark) {
142 const auto keys = m_marks.keys();
143 for (QChar markerChar : keys) {
144 if (m_marks.value(markerChar)->line() == mark.
line) {
145 m_marks.remove(markerChar);
149 bool freeMarkerCharFound =
false;
151 for (
const QChar &markerChar : UserMarks) {
152 if (!m_marks.value(markerChar)) {
153 setMark(markerChar, KTextEditor::Cursor(mark.
line, 0));
154 freeMarkerCharFound =
true;
161 m_inputModeManager->getViNormalMode()->error(
i18n(
"There are no more chars for the next bookmark."));
166void Marks::syncViMarksAndBookmarks()
168 const QHash<int, KTextEditor::Mark *> &marks = m_doc->marks();
171 for (
auto mark : marks) {
176 bool thereIsViMarkForThisLine =
false;
177 for (
auto cursor : std::as_const(m_marks)) {
178 if (cursor->line() == mark->
line) {
179 thereIsViMarkForThisLine =
true;
184 if (thereIsViMarkForThisLine) {
188 for (
const QChar &markerChar : UserMarks) {
189 if (!m_marks.value(markerChar)) {
190 setMark(markerChar, KTextEditor::Cursor(mark->
line, 0));
197 const auto keys = m_marks.keys();
198 for (QChar markChar : keys) {
199 if (!isShowable(markChar)) {
203 bool thereIsKateMarkForThisLine =
false;
204 for (
auto mark : marks) {
209 if (m_marks.value(markChar)->line() == mark->
line) {
210 thereIsKateMarkForThisLine =
true;
215 if (!thereIsKateMarkForThisLine) {
221QString Marks::getMarksOnTheLine(
int line)
const
224 const auto keys = m_marks.keys();
225 for (QChar markerChar : keys) {
226 if (m_marks.value(markerChar)->line() == line) {
227 res += markerChar + QLatin1Char(
':') +
QString::number(m_marks.value(markerChar)->column()) + QLatin1Char(
' ');
234bool Marks::isShowable(
const QChar &mark)
236 return FirstUserMark <= mark && mark <= LastUserMark;
239void Marks::setStartEditYanked(
const KTextEditor::Cursor pos)
241 setMark(BeginEditYanked, pos);
244void Marks::setFinishEditYanked(
const KTextEditor::Cursor pos)
246 setMark(EndEditYanked, pos);
249void Marks::setLastChange(
const KTextEditor::Cursor pos)
251 setMark(LastChange, pos);
254void Marks::setInsertStopped(
const KTextEditor::Cursor pos)
256 setMark(InsertStopped, pos);
259void Marks::setSelectionStart(
const KTextEditor::Cursor pos)
261 setMark(SelectionBegin, pos);
264void Marks::setSelectionFinish(
const KTextEditor::Cursor pos)
266 setMark(SelectionEnd, pos);
269void Marks::setUserMark(
const QChar &mark,
const KTextEditor::Cursor pos)
271 Q_ASSERT(FirstUserMark <= mark && mark <= LastUserMark);
275KTextEditor::Cursor Marks::getStartEditYanked()
const
277 return getMarkPosition(BeginEditYanked);
280KTextEditor::Cursor Marks::getFinishEditYanked()
const
282 return getMarkPosition(EndEditYanked);
285KTextEditor::Cursor Marks::getSelectionStart()
const
287 return getMarkPosition(SelectionBegin);
290KTextEditor::Cursor Marks::getSelectionFinish()
const
292 return getMarkPosition(SelectionEnd);
295KTextEditor::Cursor Marks::getLastChange()
const
297 return getMarkPosition(LastChange);
300KTextEditor::Cursor Marks::getInsertStopped()
const
302 return getMarkPosition(InsertStopped);
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
QString readEntry(const char *key, const char *aDefault=nullptr) const
constexpr int line() const noexcept
Retrieve the line on which this cursor is situated.
static constexpr Cursor invalid() noexcept
Returns an invalid cursor.
MarkChangeAction
Possible actions on a mark.
@ MarkAdded
action: a mark was added.
@ MarkRemoved
action: a mark was removed.
void markChanged(KTextEditor::Document *document, KTextEditor::Mark mark, KTextEditor::Document::MarkChangeAction action)
The document emits this signal whenever the mark changes.
uint type
The mark types in the line, combined with logical OR.
int line
The line that contains the mark.
virtual int column() const =0
Retrieve the column on which this cursor is situated.
InsertBehavior
Insert behavior of this cursor, should it stay if text is insert at its position or should it move.
@ StayOnInsert
stay on insert
@ MoveOnInsert
move on insert
virtual int line() const =0
Retrieve the line on which this cursor is situated.
QString i18n(const char *text, const TYPE &arg...)
const_reference at(qsizetype i) const const
void reserve(qsizetype size)
qsizetype size() const const
QString number(double n, char format, int precision)