dot/ipython-key-bindings.py: Fix some bogus formatting.
[profile] / dot / ipython-key-bindings.py
1 ### -*-python -*-
2
3 import IPython as IPY
4 import prompt_toolkit as PTK
5
6 def ding():
7 with open('/dev/tty', 'w') as f: f.write('\a')
8
9 ## Key bindings. Alas, IPython's attempt at Emacs keybindings is abysmal.
10 K = PTK.keys.Keys
11 F = PTK.filters
12 BUF = PTK.enums.DEFAULT_BUFFER
13 ipy = IPY.get_ipython()
14 try: pt = ipy.pt_cli
15 except AttributeError: pass
16 else:
17 reg = pt.application.key_bindings_registry
18 try: bind = reg.add_binding
19 except AttributeError:
20 bind = PTK.key_binding.bindings.utils.create_handle_decorator(reg)
21
22 def inhibit_history_search(buf, fn):
23 searchp, searchtext = \
24 buf.enable_history_search, buf.history_search_text
25 buf.enable_history_search = F.Never()
26 try:
27 fn()
28 finally:
29 buf.enable_history_search, buf.history_search_text = \
30 searchp, searchtext
31 @bind(K.ControlP)
32 def prev_line(ev):
33 buf = ev.current_buffer
34 if buf.document.cursor_position_row > 0:
35 buf.cursor_up()
36 elif not buf.selection_state:
37 inhibit_history_search(buf, lambda: buf.history_backward())
38 @bind(K.ControlN)
39 def next_line(ev):
40 buf = ev.current_buffer
41 if buf.document.cursor_position_row < buf.document.line_count - 1:
42 buf.cursor_down()
43 elif not buf.selection_state:
44 inhibit_history_search(buf, lambda: buf.history_forward())
45
46 bind(K.Escape, u'p')(lambda ev: ev.current_buffer.history_backward())
47 bind(K.Escape, u'n')(lambda ev: ev.current_buffer.history_forward())