chiark / gitweb /
dot/zshrc: Fix path ellipsization in the prompt.
[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
18 bind = PTK.key_binding.bindings.utils.create_handle_decorator(reg)
19
20 def inhibit_history_search(buf, fn):
21 searchp, searchtext = buf.enable_history_search, buf.history_search_text
22 buf.enable_history_search = F.Never()
23 try:
24 fn()
25 finally:
26 buf.enable_history_search, buf.history_search_text = searchp, searchtext
27 @bind(K.ControlP)
28 def prev_line(ev):
29 buf = ev.current_buffer
30 if buf.document.cursor_position_row > 0:
31 buf.cursor_up()
32 elif not buf.selection_state:
33 inhibit_history_search(buf, lambda: buf.history_backward())
34 @bind(K.ControlN)
35 def next_line(ev):
36 buf = ev.current_buffer
37 if buf.document.cursor_position_row < buf.document.line_count - 1:
38 buf.cursor_down()
39 elif not buf.selection_state:
40 inhibit_history_search(buf, lambda: buf.history_forward())
41
42 bind(K.Escape, u'p')(lambda ev: ev.current_buffer.history_backward())
43 bind(K.Escape, u'n')(lambda ev: ev.current_buffer.history_forward())