Skip to content

Commit 6961874

Browse files
committed
Merge pull request kivy#3806 from el-ethan/emacs-behavior-example
Add EmacsBehavior to CodeInput and add example
2 parents 52f1063 + ab1080d commit 6961874

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

examples/widgets/codeinput.py

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
from kivy.uix.spinner import Spinner, SpinnerOption
44
from kivy.uix.boxlayout import BoxLayout
55
from kivy.uix.codeinput import CodeInput
6+
from kivy.uix.behaviors import EmacsBehavior
67
from kivy.uix.popup import Popup
78
from kivy.properties import ListProperty
89
from kivy.core.window import Window
910
from kivy.core.text import LabelBase
1011
from pygments import lexers
11-
12+
from pygame import font as fonts
1213
import codecs
1314
import glob
1415
import os
@@ -57,6 +58,26 @@ def build(self):
5758
s.parentNode.insertBefore(po, s);
5859
})();
5960
</script>
61+
----------------------Emacs key bindings---------------------
62+
This CodeInput inherits from EmacsBehavior, so you can use Emacs key bindings
63+
if you want! To try out Emacs key bindings, set the "Key bindings" option to
64+
"Emacs". Experiment with the shortcuts below on some of the text in this window
65+
(just be careful not to delete the cheat sheet before you have made note of the
66+
commands!)
67+
68+
Shortcut Description
69+
-------- -----------
70+
Control + a Move cursor to the beginning of the line
71+
Control + e Move cursor to the end of the line
72+
Control + f Move cursor one character to the right
73+
Control + b Move cursor one character to the left
74+
Alt + f Move cursor to the end of the word to the right
75+
Alt + b Move cursor to the start of the word to the left
76+
Alt + Backspace Delete text left of the cursor to the beginning of word
77+
Alt + d Delete text right of the cursor to the end of the word
78+
Alt + w Copy selection
79+
Control + w Cut selection
80+
Control + y Paste selection
6081
'''
6182

6283

@@ -89,6 +110,13 @@ def cancel(self):
89110
self.dismiss()
90111

91112

113+
class CodeInputWithBindings(EmacsBehavior, CodeInput):
114+
'''CodeInput with keybindings.
115+
To add more bindings, add behavior before CodeInput in class definition
116+
'''
117+
pass
118+
119+
92120
class CodeInputTest(App):
93121

94122
files = ListProperty([None, ])
@@ -122,18 +150,24 @@ def build(self):
122150
text='File',
123151
values=('Open', 'SaveAs', 'Save', 'Close'))
124152
mnu_file.bind(text=self._file_menu_selected)
153+
key_bindings = Spinner(
154+
text='Key bindings',
155+
values=('Default key bindings', 'Emacs key bindings'))
156+
key_bindings.bind(text=self._bindings_selected)
125157

126158
menu.add_widget(mnu_file)
127159
menu.add_widget(fnt_size)
128160
menu.add_widget(fnt_name)
129161
menu.add_widget(languages)
162+
menu.add_widget(key_bindings)
130163
b.add_widget(menu)
131164

132-
self.codeinput = CodeInput(
133-
165+
self.codeinput = CodeInputWithBindings(
134166
lexer=KivyLexer(),
135167
font_size=12,
136-
text=example_text)
168+
text=example_text,
169+
active_key_bindings='default',
170+
)
137171

138172
b.add_widget(self.codeinput)
139173

@@ -169,6 +203,10 @@ def _file_menu_selected(self, instance, value):
169203
self.codeinput.text = ''
170204
Window.title = 'untitled'
171205

206+
def _bindings_selected(self, instance, value):
207+
value = value.split(' ')[0]
208+
self.codeinput.active_key_bindings = value.lower()
209+
172210
def on_files(self, instance, values):
173211
if not values[0]:
174212
return

kivy/uix/behaviors/emacs.py

Lines changed: 17 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,19 @@ 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+
.. versionadded:: 1.9.2
61+
'''
62+
4763
def __init__(self, **kwargs):
4864
super(EmacsBehavior, self).__init__(**kwargs)
4965

@@ -73,7 +89,7 @@ def keyboard_on_key_down(self, window, keycode, text, modifiers):
7389
mod = modifiers[0] if modifiers else None
7490
is_emacs_shortcut = False
7591

76-
if key in range(256):
92+
if key in range(256) and self.active_key_bindings == 'emacs':
7793
is_emacs_shortcut = ((mod == 'ctrl' and
7894
chr(key) in self.bindings['ctrl'].keys()) or
7995
(mod == 'alt' and

kivy/uix/codeinput.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
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
4747
from kivy.uix.behaviors import CodeNavigationBehavior
4848

@@ -85,6 +85,15 @@ 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+
.. versionadded:: 1.9.2
95+
'''
96+
8897
def __init__(self, **kwargs):
8998
stylename = kwargs.get('style_name', 'default')
9099
style = kwargs['style'] if 'style' in kwargs \

0 commit comments

Comments
 (0)