Show More
@@ -1,165 +1,167 b'' | |||||
1 | import signal |
|
1 | import signal | |
2 | import sys |
|
2 | import sys | |
3 |
|
3 | |||
4 | from prompt_toolkit.enums import DEFAULT_BUFFER, SEARCH_BUFFER |
|
4 | from prompt_toolkit.enums import DEFAULT_BUFFER, SEARCH_BUFFER | |
5 | from prompt_toolkit.filters import (HasFocus, HasSelection, Condition, |
|
5 | from prompt_toolkit.filters import (HasFocus, HasSelection, Condition, | |
6 | ViInsertMode, EmacsInsertMode, HasCompletions) |
|
6 | ViInsertMode, EmacsInsertMode, HasCompletions) | |
7 | from prompt_toolkit.filters.cli import ViMode |
|
7 | from prompt_toolkit.filters.cli import ViMode | |
8 | from prompt_toolkit.keys import Keys |
|
8 | from prompt_toolkit.keys import Keys | |
9 | from prompt_toolkit.key_binding.bindings.completion import display_completions_like_readline |
|
9 | from prompt_toolkit.key_binding.bindings.completion import display_completions_like_readline | |
10 |
|
10 | |||
|
11 | from IPython.utils.decorators import undoc | |||
|
12 | ||||
11 | @Condition |
|
13 | @Condition | |
12 | def cursor_in_leading_ws(cli): |
|
14 | def cursor_in_leading_ws(cli): | |
13 | before = cli.application.buffer.document.current_line_before_cursor |
|
15 | before = cli.application.buffer.document.current_line_before_cursor | |
14 | return (not before) or before.isspace() |
|
16 | return (not before) or before.isspace() | |
15 |
|
17 | |||
16 | def register_ipython_shortcuts(registry, shell): |
|
18 | def register_ipython_shortcuts(registry, shell): | |
17 | """Set up the prompt_toolkit keyboard shortcuts for IPython""" |
|
19 | """Set up the prompt_toolkit keyboard shortcuts for IPython""" | |
18 | insert_mode = ViInsertMode() | EmacsInsertMode() |
|
20 | insert_mode = ViInsertMode() | EmacsInsertMode() | |
19 |
|
21 | |||
20 | # Ctrl+J == Enter, seemingly |
|
22 | # Ctrl+J == Enter, seemingly | |
21 | registry.add_binding(Keys.ControlJ, |
|
23 | registry.add_binding(Keys.ControlJ, | |
22 | filter=(HasFocus(DEFAULT_BUFFER) |
|
24 | filter=(HasFocus(DEFAULT_BUFFER) | |
23 | & ~HasSelection() |
|
25 | & ~HasSelection() | |
24 | & insert_mode |
|
26 | & insert_mode | |
25 | ))(newline_or_execute_outer(shell)) |
|
27 | ))(newline_or_execute_outer(shell)) | |
26 |
|
28 | |||
27 | registry.add_binding(Keys.ControlP, |
|
29 | registry.add_binding(Keys.ControlP, | |
28 | filter=(ViInsertMode() & HasFocus(DEFAULT_BUFFER) |
|
30 | filter=(ViInsertMode() & HasFocus(DEFAULT_BUFFER) | |
29 | ))(previous_history_or_previous_completion) |
|
31 | ))(previous_history_or_previous_completion) | |
30 |
|
32 | |||
31 | registry.add_binding(Keys.ControlN, |
|
33 | registry.add_binding(Keys.ControlN, | |
32 | filter=(ViInsertMode() & HasFocus(DEFAULT_BUFFER) |
|
34 | filter=(ViInsertMode() & HasFocus(DEFAULT_BUFFER) | |
33 | ))(next_history_or_next_completion) |
|
35 | ))(next_history_or_next_completion) | |
34 |
|
36 | |||
35 | registry.add_binding(Keys.ControlG, |
|
37 | registry.add_binding(Keys.ControlG, | |
36 | filter=(HasFocus(DEFAULT_BUFFER) & HasCompletions() |
|
38 | filter=(HasFocus(DEFAULT_BUFFER) & HasCompletions() | |
37 | ))(dismiss_completion) |
|
39 | ))(dismiss_completion) | |
38 |
|
40 | |||
39 | registry.add_binding(Keys.ControlC, filter=HasFocus(DEFAULT_BUFFER) |
|
41 | registry.add_binding(Keys.ControlC, filter=HasFocus(DEFAULT_BUFFER) | |
40 | )(reset_buffer) |
|
42 | )(reset_buffer) | |
41 |
|
43 | |||
42 | registry.add_binding(Keys.ControlC, filter=HasFocus(SEARCH_BUFFER) |
|
44 | registry.add_binding(Keys.ControlC, filter=HasFocus(SEARCH_BUFFER) | |
43 | )(reset_search_buffer) |
|
45 | )(reset_search_buffer) | |
44 |
|
46 | |||
45 | supports_suspend = Condition(lambda cli: hasattr(signal, 'SIGTSTP')) |
|
47 | supports_suspend = Condition(lambda cli: hasattr(signal, 'SIGTSTP')) | |
46 | registry.add_binding(Keys.ControlZ, filter=supports_suspend |
|
48 | registry.add_binding(Keys.ControlZ, filter=supports_suspend | |
47 | )(suspend_to_bg) |
|
49 | )(suspend_to_bg) | |
48 |
|
50 | |||
49 | # Ctrl+I == Tab |
|
51 | # Ctrl+I == Tab | |
50 | registry.add_binding(Keys.ControlI, |
|
52 | registry.add_binding(Keys.ControlI, | |
51 | filter=(HasFocus(DEFAULT_BUFFER) |
|
53 | filter=(HasFocus(DEFAULT_BUFFER) | |
52 | & ~HasSelection() |
|
54 | & ~HasSelection() | |
53 | & insert_mode |
|
55 | & insert_mode | |
54 | & cursor_in_leading_ws |
|
56 | & cursor_in_leading_ws | |
55 | ))(indent_buffer) |
|
57 | ))(indent_buffer) | |
56 |
|
58 | |||
57 | if shell.display_completions == 'readlinelike': |
|
59 | if shell.display_completions == 'readlinelike': | |
58 | registry.add_binding(Keys.ControlI, |
|
60 | registry.add_binding(Keys.ControlI, | |
59 | filter=(HasFocus(DEFAULT_BUFFER) |
|
61 | filter=(HasFocus(DEFAULT_BUFFER) | |
60 | & ~HasSelection() |
|
62 | & ~HasSelection() | |
61 | & insert_mode |
|
63 | & insert_mode | |
62 | & ~cursor_in_leading_ws |
|
64 | & ~cursor_in_leading_ws | |
63 | ))(display_completions_like_readline) |
|
65 | ))(display_completions_like_readline) | |
64 |
|
66 | |||
65 | if sys.platform == 'win32': |
|
67 | if sys.platform == 'win32': | |
66 | registry.add_binding(Keys.ControlV, |
|
68 | registry.add_binding(Keys.ControlV, | |
67 | filter=( |
|
69 | filter=( | |
68 | HasFocus( |
|
70 | HasFocus( | |
69 | DEFAULT_BUFFER) & ~ViMode() |
|
71 | DEFAULT_BUFFER) & ~ViMode() | |
70 | ))(win_paste) |
|
72 | ))(win_paste) | |
71 |
|
73 | |||
72 |
|
74 | |||
73 | def newline_or_execute_outer(shell): |
|
75 | def newline_or_execute_outer(shell): | |
74 | def newline_or_execute(event): |
|
76 | def newline_or_execute(event): | |
75 | """When the user presses return, insert a newline or execute the code.""" |
|
77 | """When the user presses return, insert a newline or execute the code.""" | |
76 | b = event.current_buffer |
|
78 | b = event.current_buffer | |
77 | d = b.document |
|
79 | d = b.document | |
78 |
|
80 | |||
79 | if b.complete_state: |
|
81 | if b.complete_state: | |
80 | cc = b.complete_state.current_completion |
|
82 | cc = b.complete_state.current_completion | |
81 | if cc: |
|
83 | if cc: | |
82 | b.apply_completion(cc) |
|
84 | b.apply_completion(cc) | |
83 | else: |
|
85 | else: | |
84 | b.cancel_completion() |
|
86 | b.cancel_completion() | |
85 | return |
|
87 | return | |
86 |
|
88 | |||
87 | if not (d.on_last_line or d.cursor_position_row >= d.line_count |
|
89 | if not (d.on_last_line or d.cursor_position_row >= d.line_count | |
88 | - d.empty_line_count_at_the_end()): |
|
90 | - d.empty_line_count_at_the_end()): | |
89 | b.newline() |
|
91 | b.newline() | |
90 | return |
|
92 | return | |
91 |
|
93 | |||
92 | status, indent = shell.input_splitter.check_complete(d.text + '\n') |
|
94 | status, indent = shell.input_splitter.check_complete(d.text + '\n') | |
93 |
|
95 | |||
94 | if (status != 'incomplete') and b.accept_action.is_returnable: |
|
96 | if (status != 'incomplete') and b.accept_action.is_returnable: | |
95 | b.accept_action.validate_and_handle(event.cli, b) |
|
97 | b.accept_action.validate_and_handle(event.cli, b) | |
96 | else: |
|
98 | else: | |
97 | b.insert_text('\n' + (' ' * (indent or 0))) |
|
99 | b.insert_text('\n' + (' ' * (indent or 0))) | |
98 | return newline_or_execute |
|
100 | return newline_or_execute | |
99 |
|
101 | |||
100 |
|
102 | |||
101 | def previous_history_or_previous_completion(event): |
|
103 | def previous_history_or_previous_completion(event): | |
102 | """ |
|
104 | """ | |
103 | Control-P in vi edit mode on readline is history next, unlike default prompt toolkit. |
|
105 | Control-P in vi edit mode on readline is history next, unlike default prompt toolkit. | |
104 |
|
106 | |||
105 | If completer is open this still select previous completion. |
|
107 | If completer is open this still select previous completion. | |
106 | """ |
|
108 | """ | |
107 | event.current_buffer.auto_up() |
|
109 | event.current_buffer.auto_up() | |
108 |
|
110 | |||
109 |
|
111 | |||
110 | def next_history_or_next_completion(event): |
|
112 | def next_history_or_next_completion(event): | |
111 | """ |
|
113 | """ | |
112 | Control-N in vi edit mode on readline is history previous, unlike default prompt toolkit. |
|
114 | Control-N in vi edit mode on readline is history previous, unlike default prompt toolkit. | |
113 |
|
115 | |||
114 | If completer is open this still select next completion. |
|
116 | If completer is open this still select next completion. | |
115 | """ |
|
117 | """ | |
116 | event.current_buffer.auto_down() |
|
118 | event.current_buffer.auto_down() | |
117 |
|
119 | |||
118 |
|
120 | |||
119 | def dismiss_completion(event): |
|
121 | def dismiss_completion(event): | |
120 | b = event.current_buffer |
|
122 | b = event.current_buffer | |
121 | if b.complete_state: |
|
123 | if b.complete_state: | |
122 | b.cancel_completion() |
|
124 | b.cancel_completion() | |
123 |
|
125 | |||
124 |
|
126 | |||
125 | def reset_buffer(event): |
|
127 | def reset_buffer(event): | |
126 | b = event.current_buffer |
|
128 | b = event.current_buffer | |
127 | if b.complete_state: |
|
129 | if b.complete_state: | |
128 | b.cancel_completion() |
|
130 | b.cancel_completion() | |
129 | else: |
|
131 | else: | |
130 | b.reset() |
|
132 | b.reset() | |
131 |
|
133 | |||
132 |
|
134 | |||
133 | def reset_search_buffer(event): |
|
135 | def reset_search_buffer(event): | |
134 | if event.current_buffer.document.text: |
|
136 | if event.current_buffer.document.text: | |
135 | event.current_buffer.reset() |
|
137 | event.current_buffer.reset() | |
136 | else: |
|
138 | else: | |
137 | event.cli.push_focus(DEFAULT_BUFFER) |
|
139 | event.cli.push_focus(DEFAULT_BUFFER) | |
138 |
|
140 | |||
139 | def suspend_to_bg(event): |
|
141 | def suspend_to_bg(event): | |
140 | event.cli.suspend_to_background() |
|
142 | event.cli.suspend_to_background() | |
141 |
|
143 | |||
142 | def indent_buffer(event): |
|
144 | def indent_buffer(event): | |
143 | event.current_buffer.insert_text(' ' * 4) |
|
145 | event.current_buffer.insert_text(' ' * 4) | |
144 |
|
146 | |||
145 |
|
147 | |||
146 |
|
148 | |||
147 |
|
149 | |||
148 | if sys.platform == 'win32': |
|
150 | if sys.platform == 'win32': | |
149 | from IPython.core.error import TryNext |
|
151 | from IPython.core.error import TryNext | |
150 | from IPython.lib.clipboard import (ClipboardEmpty, |
|
152 | from IPython.lib.clipboard import (ClipboardEmpty, | |
151 | win32_clipboard_get, |
|
153 | win32_clipboard_get, | |
152 | tkinter_clipboard_get) |
|
154 | tkinter_clipboard_get) | |
153 |
|
155 | |||
154 |
|
156 | @undoc | ||
155 | def win_paste(event): |
|
157 | def win_paste(event): | |
156 | try: |
|
158 | try: | |
157 | text = win32_clipboard_get() |
|
159 | text = win32_clipboard_get() | |
158 | except TryNext: |
|
160 | except TryNext: | |
159 | try: |
|
161 | try: | |
160 | text = tkinter_clipboard_get() |
|
162 | text = tkinter_clipboard_get() | |
161 | except (TryNext, ClipboardEmpty): |
|
163 | except (TryNext, ClipboardEmpty): | |
162 | return |
|
164 | return | |
163 | except ClipboardEmpty: |
|
165 | except ClipboardEmpty: | |
164 | return |
|
166 | return | |
165 | event.current_buffer.insert_text(text.replace('\t', ' ' * 4)) |
|
167 | event.current_buffer.insert_text(text.replace('\t', ' ' * 4)) |
General Comments 0
You need to be logged in to leave comments.
Login now