1 /*
2     SPDX-FileCopyrightText: 2008 Erlend Hamberg <ehamberg@gmail.com>
3 
4     SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include <vimode/command.h>
8 #include <vimode/keyparser.h>
9 
10 #include <QRegularExpression>
11 
12 using namespace KateVi;
13 
Command(NormalViMode * parent,const QString & pattern,bool (NormalViMode::* commandMethod)(),unsigned int flags)14 Command::Command(NormalViMode *parent, const QString &pattern, bool (NormalViMode::*commandMethod)(), unsigned int flags)
15 {
16     m_parent = parent;
17     m_pattern = KeyParser::self()->encodeKeySequence(pattern);
18     m_flags = flags;
19     m_ptr2commandMethod = commandMethod;
20 }
21 
22 Command::~Command() = default;
23 
execute() const24 bool Command::execute() const
25 {
26     return (m_parent->*m_ptr2commandMethod)();
27 }
28 
matches(const QString & pattern) const29 bool Command::matches(const QString &pattern) const
30 {
31     if (!(m_flags & REGEX_PATTERN)) {
32         return m_pattern.startsWith(pattern);
33     } else {
34         const QRegularExpression re(m_pattern, QRegularExpression::UseUnicodePropertiesOption);
35         const auto match = re.match(pattern, 0, QRegularExpression::PartialPreferFirstMatch);
36         // Partial matching could lead to a complete match, in that case hasPartialMatch() will return false, and hasMatch() will return true
37         return match.hasPartialMatch() || match.hasMatch();
38     }
39 }
40 
matchesExact(const QString & pattern) const41 bool Command::matchesExact(const QString &pattern) const
42 {
43     if (!(m_flags & REGEX_PATTERN)) {
44         return (m_pattern == pattern);
45     } else {
46         const QRegularExpression re(QRegularExpression::anchoredPattern(m_pattern), QRegularExpression::UseUnicodePropertiesOption);
47         return re.match(pattern).hasMatch();
48     }
49 }
50