-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinsert_mode.py
98 lines (77 loc) · 2.74 KB
/
insert_mode.py
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
import curses
from plugin import Plugin
class InsertModePlugin(Plugin):
"""
Adds a command to toggle the insert mode.
"""
__singleton = None
def __new__(cls, *args, **kwargs):
"""
Creates a singleton of the class.
"""
if cls.__singleton is None:
cls.__singleton = super().__new__(cls)
return cls.__singleton
def __init__(self, app):
super().__init__(app)
# Sets up the translations
self.translations = {
"en": {
"insert": "Insert",
"toggled_insert_mode": "Toggled insert mode to {state} "
},
"fr": {
"insert": "Insérer",
"toggled_insert_mode": "Bascule du mode d'insertion sur {state} "
}
}
# Initializes the variable indicating whether the insert mode is enabled
self.insert_mode_enabled = False
# Adds the command to toggle the insert mode
self.add_command("i", self.toggle_insert_mode, self.translate("insert"))
# Assigns the regular add_char_to_text function to a variable
self._regular_add_char_to_text = self.app.add_char_to_text
# Replaces the add_char_to_text function with a custom one
self.app.add_char_to_text = self._add_char_to_text
# Assigns the regular display_text function to a variable
self._regular_display_text = self.app.display_text
# Replaces the display_text function with a custom one
self.app.display_text = self._display_text
def toggle_insert_mode(self):
"""
Toggles the insert mode.
"""
# Toggles insert mode
self.insert_mode_enabled = not self.insert_mode_enabled
# Shows a message to the user indicating the current state of the variable
self.app.stdscr.addstr(self.app.rows - 1, 4, self.translate(
"toggled_insert_mode",
state = self.insert_mode_enabled
))
def _add_char_to_text(self, key:str):
"""
Adds the given character to the text if the insert mode is disabled, or inserts if enabled.
:param key: The character to be inserted.
"""
# If insert mode is disabled, we just use the regular function
if self.insert_mode_enabled is False:
self._regular_add_char_to_text(key)
# If insert mode is enabled, we fetch each character from 'key' and replace the current one with this new character
else:
for character in key:
self.app.current_text = self.app.current_text[:self.app.current_index] + character + self.app.current_text[self.app.current_index+1:]
self.app.current_index += 1
def _display_text(self):
"""
Highlights the cursor in red if we are in insert mode.
"""
# Displays the text
self._regular_display_text()
# If insert mode is enabled, displays the cursor in red
if self.insert_mode_enabled and self.app.cur is not None:
try:
self.app.stdscr.addstr(*self.app.cur, curses.A_REVERSE | curses.color_pair(1))
except curses.error:
pass
def init(app):
return InsertModePlugin(app)