chiark / gitweb /
el/dot-emacs.el (mdw-fontify-rust): Fix integer literal syntax.
[profile] / dot / ipython-key-bindings.py
CommitLineData
bff12eb1
MW
1### -*-python -*-
2
3import IPython as IPY
4import prompt_toolkit as PTK
5
6def 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.
10K = PTK.keys.Keys
11F = PTK.filters
12BUF = PTK.enums.DEFAULT_BUFFER
13ipy = IPY.get_ipython()
14try: pt = ipy.pt_cli
15except AttributeError: pass
16else:
17 reg = pt.application.key_bindings_registry
a894ae88
MW
18 try: bind = reg.add_binding
19 except AttributeError: bind = PTK.key_binding.bindings.utils.create_handle_decorator(reg)
bff12eb1
MW
20
21 def inhibit_history_search(buf, fn):
22 searchp, searchtext = buf.enable_history_search, buf.history_search_text
23 buf.enable_history_search = F.Never()
24 try:
25 fn()
26 finally:
27 buf.enable_history_search, buf.history_search_text = searchp, searchtext
28 @bind(K.ControlP)
29 def prev_line(ev):
30 buf = ev.current_buffer
31 if buf.document.cursor_position_row > 0:
32 buf.cursor_up()
33 elif not buf.selection_state:
34 inhibit_history_search(buf, lambda: buf.history_backward())
35 @bind(K.ControlN)
36 def next_line(ev):
37 buf = ev.current_buffer
38 if buf.document.cursor_position_row < buf.document.line_count - 1:
39 buf.cursor_down()
40 elif not buf.selection_state:
41 inhibit_history_search(buf, lambda: buf.history_forward())
42
43 bind(K.Escape, u'p')(lambda ev: ev.current_buffer.history_backward())
44 bind(K.Escape, u'n')(lambda ev: ev.current_buffer.history_forward())