##// END OF EJS Templates
Delete cell only if you press "d" twice in 1 second.
Brian E. Granger -
Show More
@@ -1,282 +1,293 b''
1 //----------------------------------------------------------------------------
1 //----------------------------------------------------------------------------
2 // Copyright (C) 2011 The IPython Development Team
2 // Copyright (C) 2011 The IPython Development Team
3 //
3 //
4 // Distributed under the terms of the BSD License. The full license is in
4 // Distributed under the terms of the BSD License. The full license is in
5 // the file COPYING, distributed as part of this software.
5 // the file COPYING, distributed as part of this software.
6 //----------------------------------------------------------------------------
6 //----------------------------------------------------------------------------
7
7
8 //============================================================================
8 //============================================================================
9 // Keyboard management
9 // Keyboard management
10 //============================================================================
10 //============================================================================
11
11
12 var IPython = (function (IPython) {
12 var IPython = (function (IPython) {
13 "use strict";
13 "use strict";
14
14
15 var key = IPython.utils.keycodes;
15 var key = IPython.utils.keycodes;
16
16
17 var KeyboardManager = function () {
17 var KeyboardManager = function () {
18 this.mode = 'command';
18 this.mode = 'command';
19 this.enabled = true;
19 this.enabled = true;
20 this.delete_count = 0;
20 this.bind_events();
21 this.bind_events();
21 };
22 };
22
23
23 KeyboardManager.prototype.bind_events = function () {
24 KeyboardManager.prototype.bind_events = function () {
24 var that = this;
25 var that = this;
25 $(document).keydown(function (event) {
26 $(document).keydown(function (event) {
26 return that.handle_keydown(event);
27 return that.handle_keydown(event);
27 });
28 });
28 };
29 };
29
30
30 KeyboardManager.prototype.handle_keydown = function (event) {
31 KeyboardManager.prototype.handle_keydown = function (event) {
31 var notebook = IPython.notebook;
32 var notebook = IPython.notebook;
32
33
33 console.log('keyboard_manager', this.mode, event.keyCode);
34 console.log('keyboard_manager', this.mode, event.keyCode);
34
35
35 if (event.which === key.ESC) {
36 if (event.which === key.ESC) {
36 // Intercept escape at highest level to avoid closing
37 // Intercept escape at highest level to avoid closing
37 // websocket connection with firefox
38 // websocket connection with firefox
38 event.preventDefault();
39 event.preventDefault();
39 }
40 }
40
41
41 if (!this.enabled) {
42 if (!this.enabled) {
42 return true;
43 return true;
43 }
44 }
44
45
45 // Event handlers for both command and edit mode
46 // Event handlers for both command and edit mode
46 if ((event.ctrlKey || event.metaKey) && event.keyCode==83) {
47 if ((event.ctrlKey || event.metaKey) && event.keyCode==83) {
47 // Save (CTRL+S) or (Command+S on Mac)
48 // Save (CTRL+S) or (Command+S on Mac)
48 notebook.save_checkpoint();
49 notebook.save_checkpoint();
49 event.preventDefault();
50 event.preventDefault();
50 return false;
51 return false;
51 } else if (event.which === key.ESC) {
52 } else if (event.which === key.ESC) {
52 // Intercept escape at highest level to avoid closing
53 // Intercept escape at highest level to avoid closing
53 // websocket connection with firefox
54 // websocket connection with firefox
54 event.preventDefault();
55 event.preventDefault();
55 // Don't return yet to allow edit/command modes to handle
56 // Don't return yet to allow edit/command modes to handle
56 } else if (event.which === key.SHIFT) {
57 } else if (event.which === key.SHIFT) {
57 // ignore shift keydown
58 // ignore shift keydown
58 return true;
59 return true;
59 } else if (event.which === key.ENTER && event.shiftKey) {
60 } else if (event.which === key.ENTER && event.shiftKey) {
60 notebook.execute_selected_cell('shift');
61 notebook.execute_selected_cell('shift');
61 return false;
62 return false;
62 } else if (event.which === key.ENTER && event.altKey) {
63 } else if (event.which === key.ENTER && event.altKey) {
63 // Execute code cell, and insert new in place
64 // Execute code cell, and insert new in place
64 notebook.execute_selected_cell('alt');
65 notebook.execute_selected_cell('alt');
65 return false;
66 return false;
66 } else if (event.which === key.ENTER && event.ctrlKey) {
67 } else if (event.which === key.ENTER && event.ctrlKey) {
67 notebook.execute_selected_cell('ctrl');
68 notebook.execute_selected_cell('ctrl');
68 return false;
69 return false;
69 }
70 }
70
71
71 if (this.mode === 'edit') {
72 if (this.mode === 'edit') {
72 return this.handle_edit_mode(event);
73 return this.handle_edit_mode(event);
73 } else if (this.mode === 'command' && !(event.ctrlKey || event.altKey || event.metaKey)) {
74 } else if (this.mode === 'command' && !(event.ctrlKey || event.altKey || event.metaKey)) {
74 return this.handle_command_mode(event);
75 return this.handle_command_mode(event);
75 }
76 }
76 }
77 }
77
78
78 KeyboardManager.prototype.handle_edit_mode = function (event) {
79 KeyboardManager.prototype.handle_edit_mode = function (event) {
79 var notebook = IPython.notebook;
80 var notebook = IPython.notebook;
80
81
81 if (event.which === key.ESC) {
82 if (event.which === key.ESC) {
82 // ESC
83 // ESC
83 notebook.command_mode();
84 notebook.command_mode();
84 return false;
85 return false;
85 } else if (event.which === 77 && event.ctrlKey) {
86 } else if (event.which === 77 && event.ctrlKey) {
86 // Ctrl-m
87 // Ctrl-m
87 notebook.command_mode();
88 notebook.command_mode();
88 return false;
89 return false;
89 } else if (event.which === key.UPARROW && !event.shiftKey) {
90 } else if (event.which === key.UPARROW && !event.shiftKey) {
90 var cell = notebook.get_selected_cell();
91 var cell = notebook.get_selected_cell();
91 if (cell && cell.at_top()) {
92 if (cell && cell.at_top()) {
92 event.preventDefault();
93 event.preventDefault();
93 notebook.command_mode()
94 notebook.command_mode()
94 notebook.select_prev();
95 notebook.select_prev();
95 notebook.edit_mode();
96 notebook.edit_mode();
96 return false;
97 return false;
97 };
98 };
98 } else if (event.which === key.DOWNARROW && !event.shiftKey) {
99 } else if (event.which === key.DOWNARROW && !event.shiftKey) {
99 var cell = notebook.get_selected_cell();
100 var cell = notebook.get_selected_cell();
100 if (cell && cell.at_bottom()) {
101 if (cell && cell.at_bottom()) {
101 event.preventDefault();
102 event.preventDefault();
102 notebook.command_mode()
103 notebook.command_mode()
103 notebook.select_next();
104 notebook.select_next();
104 notebook.edit_mode();
105 notebook.edit_mode();
105 return false;
106 return false;
106 };
107 };
107 };
108 };
108 return true;
109 return true;
109 }
110 }
110
111
111 KeyboardManager.prototype.handle_command_mode = function (event) {
112 KeyboardManager.prototype.handle_command_mode = function (event) {
113 var that = this;
112 var notebook = IPython.notebook;
114 var notebook = IPython.notebook;
113
115
114 if (event.which === key.ENTER && !(event.ctrlKey || event.altKey || event.shiftKey)) {
116 if (event.which === key.ENTER && !(event.ctrlKey || event.altKey || event.shiftKey)) {
115 // Enter edit mode = ENTER alone
117 // Enter edit mode = ENTER alone
116 notebook.edit_mode();
118 notebook.edit_mode();
117 return false;
119 return false;
118 } else if (event.which === key.UPARROW && !event.shiftKey) {
120 } else if (event.which === key.UPARROW && !event.shiftKey) {
119 var index = notebook.get_selected_index();
121 var index = notebook.get_selected_index();
120 if (index !== 0 && index !== null) {
122 if (index !== 0 && index !== null) {
121 notebook.select_prev();
123 notebook.select_prev();
122 var cell = notebook.get_selected_cell();
124 var cell = notebook.get_selected_cell();
123 cell.focus_cell();
125 cell.focus_cell();
124 };
126 };
125 return false;
127 return false;
126 } else if (event.which === key.DOWNARROW && !event.shiftKey) {
128 } else if (event.which === key.DOWNARROW && !event.shiftKey) {
127 var index = notebook.get_selected_index();
129 var index = notebook.get_selected_index();
128 if (index !== (notebook.ncells()-1) && index !== null) {
130 if (index !== (notebook.ncells()-1) && index !== null) {
129 notebook.select_next();
131 notebook.select_next();
130 var cell = notebook.get_selected_cell();
132 var cell = notebook.get_selected_cell();
131 cell.focus_cell();
133 cell.focus_cell();
132 };
134 };
133 return false;
135 return false;
134 } else if (event.which === 88) {
136 } else if (event.which === 88) {
135 // Cut selected cell = x
137 // Cut selected cell = x
136 notebook.cut_cell();
138 notebook.cut_cell();
137 return false;
139 return false;
138 } else if (event.which === 67) {
140 } else if (event.which === 67) {
139 // Copy selected cell = c
141 // Copy selected cell = c
140 notebook.copy_cell();
142 notebook.copy_cell();
141 return false;
143 return false;
142 } else if (event.which === 86) {
144 } else if (event.which === 86) {
143 // Paste below selected cell = v
145 // Paste below selected cell = v
144 notebook.paste_cell_below();
146 notebook.paste_cell_below();
145 return false;
147 return false;
146 } else if (event.which === 68) {
148 } else if (event.which === 68) {
147 // Delete selected cell = d
149 // Delete selected cell = d
148 notebook.delete_cell();
150 var dc = this.delete_count;
151 console.log('delete_count', dc);
152 if (dc === 0) {
153 this.delete_count = 1;
154 setTimeout(function () {
155 that.delete_count = 0;
156 }, 1000);
157 } else if (dc === 1) {
158 notebook.delete_cell();
159 }
149 return false;
160 return false;
150 } else if (event.which === 65) {
161 } else if (event.which === 65) {
151 // Insert code cell above selected = a
162 // Insert code cell above selected = a
152 notebook.insert_cell_above('code');
163 notebook.insert_cell_above('code');
153 notebook.select_prev();
164 notebook.select_prev();
154 return false;
165 return false;
155 } else if (event.which === 66) {
166 } else if (event.which === 66) {
156 // Insert code cell below selected = b
167 // Insert code cell below selected = b
157 notebook.insert_cell_below('code');
168 notebook.insert_cell_below('code');
158 notebook.select_next();
169 notebook.select_next();
159 return false;
170 return false;
160 } else if (event.which === 89) {
171 } else if (event.which === 89) {
161 // To code = y
172 // To code = y
162 notebook.to_code();
173 notebook.to_code();
163 return false;
174 return false;
164 } else if (event.which === 77) {
175 } else if (event.which === 77) {
165 // To markdown = m
176 // To markdown = m
166 notebook.to_markdown();
177 notebook.to_markdown();
167 return false;
178 return false;
168 } else if (event.which === 84) {
179 } else if (event.which === 84) {
169 // To Raw = t
180 // To Raw = t
170 notebook.to_raw();
181 notebook.to_raw();
171 return false;
182 return false;
172 } else if (event.which === 49) {
183 } else if (event.which === 49) {
173 // To Heading 1 = 1
184 // To Heading 1 = 1
174 notebook.to_heading(undefined, 1);
185 notebook.to_heading(undefined, 1);
175 return false;
186 return false;
176 } else if (event.which === 50) {
187 } else if (event.which === 50) {
177 // To Heading 2 = 2
188 // To Heading 2 = 2
178 notebook.to_heading(undefined, 2);
189 notebook.to_heading(undefined, 2);
179 return false;
190 return false;
180 } else if (event.which === 51) {
191 } else if (event.which === 51) {
181 // To Heading 3 = 3
192 // To Heading 3 = 3
182 notebook.to_heading(undefined, 3);
193 notebook.to_heading(undefined, 3);
183 return false;
194 return false;
184 } else if (event.which === 52) {
195 } else if (event.which === 52) {
185 // To Heading 4 = 4
196 // To Heading 4 = 4
186 notebook.to_heading(undefined, 4);
197 notebook.to_heading(undefined, 4);
187 return false;
198 return false;
188 } else if (event.which === 53) {
199 } else if (event.which === 53) {
189 // To Heading 5 = 5
200 // To Heading 5 = 5
190 notebook.to_heading(undefined, 5);
201 notebook.to_heading(undefined, 5);
191 return false;
202 return false;
192 } else if (event.which === 54) {
203 } else if (event.which === 54) {
193 // To Heading 6 = 6
204 // To Heading 6 = 6
194 notebook.to_heading(undefined, 6);
205 notebook.to_heading(undefined, 6);
195 return false;
206 return false;
196 } else if (event.which === 79) {
207 } else if (event.which === 79) {
197 // Toggle output = o
208 // Toggle output = o
198 if (event.shiftKey) {
209 if (event.shiftKey) {
199 notebook.toggle_output_scroll();
210 notebook.toggle_output_scroll();
200 } else {
211 } else {
201 notebook.toggle_output();
212 notebook.toggle_output();
202 };
213 };
203 return false;
214 return false;
204 } else if (event.which === 83) {
215 } else if (event.which === 83) {
205 // Save notebook = s
216 // Save notebook = s
206 notebook.save_checkpoint();
217 notebook.save_checkpoint();
207 return false;
218 return false;
208 } else if (event.which === 74) {
219 } else if (event.which === 74) {
209 // Move cell down = j
220 // Move cell down = j
210 notebook.move_cell_down();
221 notebook.move_cell_down();
211 return false;
222 return false;
212 } else if (event.which === 75) {
223 } else if (event.which === 75) {
213 // Move cell up = k
224 // Move cell up = k
214 notebook.move_cell_up();
225 notebook.move_cell_up();
215 return false;
226 return false;
216 } else if (event.which === 80) {
227 } else if (event.which === 80) {
217 // Select previous = p
228 // Select previous = p
218 notebook.select_prev();
229 notebook.select_prev();
219 return false;
230 return false;
220 } else if (event.which === 78) {
231 } else if (event.which === 78) {
221 // Select next = n
232 // Select next = n
222 notebook.select_next();
233 notebook.select_next();
223 return false;
234 return false;
224 } else if (event.which === 76) {
235 } else if (event.which === 76) {
225 // Toggle line numbers = l
236 // Toggle line numbers = l
226 notebook.cell_toggle_line_numbers();
237 notebook.cell_toggle_line_numbers();
227 return false;
238 return false;
228 } else if (event.which === 73) {
239 } else if (event.which === 73) {
229 // Interrupt kernel = i
240 // Interrupt kernel = i
230 notebook.kernel.interrupt();
241 notebook.kernel.interrupt();
231 return false;
242 return false;
232 } else if (event.which === 190) {
243 } else if (event.which === 190) {
233 // Restart kernel = . # matches qt console
244 // Restart kernel = . # matches qt console
234 notebook.restart_kernel();
245 notebook.restart_kernel();
235 return false;
246 return false;
236 } else if (event.which === 72) {
247 } else if (event.which === 72) {
237 // Show keyboard shortcuts = h
248 // Show keyboard shortcuts = h
238 IPython.quick_help.show_keyboard_shortcuts();
249 IPython.quick_help.show_keyboard_shortcuts();
239 return false;
250 return false;
240 } else if (event.which === 90) {
251 } else if (event.which === 90) {
241 // Undo last cell delete = z
252 // Undo last cell delete = z
242 notebook.undelete_cell();
253 notebook.undelete_cell();
243 return false;
254 return false;
244 } else if (event.which === 189 || event.which === 173) {
255 } else if (event.which === 189 || event.which === 173) {
245 // how fun! '-' is 189 in Chrome, but 173 in FF and Opera
256 // how fun! '-' is 189 in Chrome, but 173 in FF and Opera
246 // Split cell = -
257 // Split cell = -
247 notebook.split_cell();
258 notebook.split_cell();
248 return false;
259 return false;
249 } else if ((event.which === 61 || event.which === 187) && event.shiftKey) {
260 } else if ((event.which === 61 || event.which === 187) && event.shiftKey) {
250 notebook.merge_cell_below();
261 notebook.merge_cell_below();
251 return false;
262 return false;
252 };
263 };
253 // If we havn't handled it, let someone else.
264 // If we havn't handled it, let someone else.
254 return true;
265 return true;
255 };
266 };
256
267
257 KeyboardManager.prototype.edit_mode = function () {
268 KeyboardManager.prototype.edit_mode = function () {
258 console.log('KeyboardManager', 'changing to edit mode');
269 console.log('KeyboardManager', 'changing to edit mode');
259 this.last_mode = this.mode;
270 this.last_mode = this.mode;
260 this.mode = 'edit';
271 this.mode = 'edit';
261 }
272 }
262
273
263 KeyboardManager.prototype.command_mode = function () {
274 KeyboardManager.prototype.command_mode = function () {
264 console.log('KeyboardManager', 'changing to command mode');
275 console.log('KeyboardManager', 'changing to command mode');
265 this.last_mode = this.mode;
276 this.last_mode = this.mode;
266 this.mode = 'command';
277 this.mode = 'command';
267 }
278 }
268
279
269 KeyboardManager.prototype.enable = function () {
280 KeyboardManager.prototype.enable = function () {
270 this.enabled = true;
281 this.enabled = true;
271 }
282 }
272
283
273 KeyboardManager.prototype.disable = function () {
284 KeyboardManager.prototype.disable = function () {
274 this.enabled = false;
285 this.enabled = false;
275 }
286 }
276
287
277
288
278 IPython.KeyboardManager = KeyboardManager;
289 IPython.KeyboardManager = KeyboardManager;
279
290
280 return IPython;
291 return IPython;
281
292
282 }(IPython));
293 }(IPython));
General Comments 0
You need to be logged in to leave comments. Login now