Skip to content

Commit 4f784dd

Browse files
committed
Add EmacsBehavior to CodeInput and add example
1 parent 34c8bbd commit 4f784dd

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

examples/widgets/codeinput.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,26 @@ def build(self):
5757
s.parentNode.insertBefore(po, s);
5858
})();
5959
</script>
60+
----------------------Emacs key bindings---------------------
61+
This CodeInput inherits from EmacsBehavior, so you can use Emacs key bindings
62+
if you want! To try out Emacs key bindings, set the "Key bindings" option to
63+
"Emacs". Experiment with the shortcuts below on some of the text in this window
64+
(just be careful not to delete the cheat sheet before you have made note of the
65+
commands!)
66+
67+
Shortcut Description
68+
-------- -----------
69+
Control + a Move cursor to the beginning of the line
70+
Control + e Move cursor to the end of the line
71+
Control + f Move cursor one character to the right
72+
Control + b Move cursor one character to the left
73+
Alt + f Move cursor to the end of the word to the right
74+
Alt + b Move cursor to the start of the word to the left
75+
Alt + Backspace Delete text left of the cursor to the beginning of word
76+
Alt + d Delete text right of the cursor to the end of the word
77+
Alt + w Copy selection
78+
Control + w Cut selection
79+
Control + y Paste selection
6080
'''
6181

6282

@@ -122,11 +142,16 @@ def build(self):
122142
text='File',
123143
values=('Open', 'SaveAs', 'Save', 'Close'))
124144
mnu_file.bind(text=self._file_menu_selected)
145+
key_bindings = Spinner(
146+
text='Key bindings',
147+
values=('Default key bindings', 'Emacs key bindings'))
148+
key_bindings.bind(text=self._bindings_selected)
125149

126150
menu.add_widget(mnu_file)
127151
menu.add_widget(fnt_size)
128152
menu.add_widget(fnt_name)
129153
menu.add_widget(languages)
154+
menu.add_widget(key_bindings)
130155
b.add_widget(menu)
131156

132157
self.codeinput = CodeInput(
@@ -169,6 +194,10 @@ def _file_menu_selected(self, instance, value):
169194
self.codeinput.text = ''
170195
Window.title = 'untitled'
171196

197+
def _bindings_selected(self, instance, value):
198+
value = value.split(' ')[0]
199+
self.codeinput.active_key_bindings = value.lower()
200+
172201
def on_files(self, instance, values):
173202
if not values[0]:
174203
return

kivy/uix/behaviors/emacs.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
cursor to the end of the line, but the inspector will open as well).
3636
'''
3737

38+
from kivy.properties import StringProperty
39+
40+
3841
__all__ = ('EmacsBehavior', )
3942

4043

@@ -44,6 +47,17 @@ class EmacsBehavior(object):
4447
keyboard shortcuts for the :class:`~kivy.uix.textinput.TextInput` widget.
4548
'''
4649

50+
active_key_bindings = StringProperty('emacs')
51+
'''String name which determines the type of key bindings to use with the
52+
:class:`~kivy.uix.textinput.TextInput`. This allows Emacs key bindings to
53+
be enabled/disabled programmatically for widgets that inherit from
54+
:class:`EmacsBehavior`. If the value is not ``'emacs'`` Emacs bindings will
55+
be disabled.
56+
57+
:attr:`active_key_bindings` is a :class:`~kivy.properties.StringProperty`
58+
and defaults to ``'emacs'``
59+
'''
60+
4761
def __init__(self, **kwargs):
4862
super(EmacsBehavior, self).__init__(**kwargs)
4963

@@ -73,7 +87,7 @@ def keyboard_on_key_down(self, window, keycode, text, modifiers):
7387
mod = modifiers[0] if modifiers else None
7488
is_emacs_shortcut = False
7589

76-
if key in range(256):
90+
if key in range(256) and self.active_key_bindings == 'emacs':
7791
is_emacs_shortcut = ((mod == 'ctrl' and
7892
chr(key) in self.bindings['ctrl'].keys()) or
7993
(mod == 'alt' and

kivy/uix/codeinput.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@
4242
from kivy.uix.textinput import TextInput
4343
from kivy.core.text.markup import MarkupLabel as Label
4444
from kivy.cache import Cache
45-
from kivy.properties import ObjectProperty, OptionProperty
45+
from kivy.properties import ObjectProperty, OptionProperty, StringProperty
4646
from kivy.utils import get_hex_from_color, get_color_from_hex
47-
from kivy.uix.behaviors import CodeNavigationBehavior
47+
from kivy.uix.behaviors import CodeNavigationBehavior, EmacsBehavior
4848

4949
Cache_get = Cache.get
5050
Cache_append = Cache.append
5151

5252
# TODO: color chooser for keywords/strings/...
5353

5454

55-
class CodeInput(CodeNavigationBehavior, TextInput):
55+
class CodeInput(CodeNavigationBehavior, EmacsBehavior, TextInput):
5656
'''CodeInput class, used for displaying highlighted code.
5757
'''
5858

@@ -85,6 +85,13 @@ class CodeInput(CodeNavigationBehavior, TextInput):
8585
8686
'''
8787

88+
active_key_bindings = StringProperty('default')
89+
'''String name which determines the type of key bindings to use.
90+
91+
:attr:`active_key_bindings` is a :class:`~kivy.properties.StringProperty`
92+
and defaults to ``'default'``
93+
'''
94+
8895
def __init__(self, **kwargs):
8996
stylename = kwargs.get('style_name', 'default')
9097
style = kwargs['style'] if 'style' in kwargs \

0 commit comments

Comments
 (0)