File: normal.py

package info (click to toggle)
frescobaldi 3.0.0~git20161001.0.eec60717%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 19,792 kB
  • ctags: 5,843
  • sloc: python: 37,853; sh: 180; makefile: 69
file content (115 lines) | stat: -rw-r--r-- 3,589 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# vimode -- Vi Mode package for QPlainTextEdit
#
# Copyright (c) 2012 by Wilbert Berendsen
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
# See http://www.gnu.org/licenses/ for more information.

"""
Normal ViMode.
"""


import re

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QTextCursor

from . import handlerbase

commands = []


class NormalMode(handlerbase.Handler):
    def __init__(self, vimode):
        super(NormalMode, self).__init__(vimode)
        self._command = []
        self._count = 0
        
    def enter(self):
        self.vimode.textEdit().setCursorWidth(0)
    
    def updateCursorPosition(self):
        cursor = self.textCursor()
        if cursor.hasSelection() and cursor.position() > cursor.anchor():
            cursor.clearSelection()
            cursor.movePosition(QTextCursor.PreviousCharacter, QTextCursor.KeepAnchor)
        else:
            cursor.clearSelection()
            cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor)
        self.vimode.drawCursor(cursor)

    def handleKeyPress(self, ev):
        # is a number being entered?
        if not self._command and Qt.Key_0 <= ev.key() <= Qt.Key_9:
            self._count *= 10
            self._count += ev.key() - Qt.Key_0
            return True
        # backspace?
        if ev.key() == Qt.Key_Backspace:
            if self._command:
                # remove last key typed
                del self._command[-1]
            else:
                self._count = 0
            return True
        
        # TEMP
        if ev.text():
            self._command.append(ev.text())
        
        cmd = ''.join(self._command)
        for s, f in commands:
            if re.compile(s).match(cmd):
                f(self)
                self._count = 0
                self._command = []
                return True
        
        return True
    
    
    def command(cmds, count=True, set_cursor=True):
        def decorator(func):
            def wrapper(self):
                cursor = self.textCursor()
                if count:
                    for i in range(max(1, self._count)):
                        func(self, cursor)
                else:
                    func(self, cursor)
                if set_cursor:
                    self.setTextCursor(cursor)
            for c in cmds:
                commands.append((c, wrapper))
        return decorator
    
    @command(['h', '<left>'])
    def move_left(self, cursor):
        if cursor.position() > cursor.block().position():
            cursor.movePosition(QTextCursor.PreviousCharacter)
    
    @command(['l', '<right>'])
    def move_right(self, cursor):
        if cursor.position() < cursor.block().position() + cursor.block().length() - 2:
            cursor.movePosition(QTextCursor.NextCharacter)
    
    @command('i', set_cursor=False)
    def insert_mode(self, cursor):
        self.setInsertMode()