Skip to content

Commit 4b71b3b

Browse files
committed
add support for vim in the editor
1 parent 867325c commit 4b71b3b

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ build/web/js/require.js: node_modules/requirejs/require.js
151151
build/web/js/codemirror.js: $(CM)/lib/codemirror.js
152152
cp $< $@
153153

154+
build/web/js/codemirror-vim.js: $(CM)/keymap/vim.js
155+
cp $< $@
156+
154157
build/web/js/rulers.js: $(CM)/addon/display/rulers.js
155158
cp $< $@
156159

@@ -209,6 +212,7 @@ MISC_JS = build/web/js/q.js \
209212
build/web/js/url.js \
210213
build/web/js/require.js \
211214
build/web/js/codemirror.js \
215+
build/web/js/codemirror-vim.js \
212216
build/web/js/rulers.js \
213217
build/web/js/mark-selection.js \
214218
build/web/js/pyret-mode.js \
@@ -236,6 +240,7 @@ MISC_JS = build/web/js/q.js \
236240
EDITOR_MISC_JS = build/web/js/q.js \
237241
build/web/js/loader.js \
238242
build/web/js/codemirror.js \
243+
build/web/js/codemirror-vim.js \
239244
build/web/js/rulers.js \
240245
build/web/js/scrollpastend.js \
241246
build/web/js/foldcode.js \

src/web/editor.html

+9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
<body class="default">
3030
<script>
3131
var themeOnLoad = localSettings.getItem('theme') || 'default';
32+
var keybindOnLoad = localSettings.getItem('keybind') || 'default';
3233
document.body.classList.remove("default");
3334
document.body.classList.add(themeOnLoad);
3435
var params = {};
@@ -64,6 +65,7 @@
6465
}
6566
window.addEventListener('load', function() {
6667
document.getElementById('theme-select').value = themeOnLoad;
68+
document.getElementById('keybind-select').value = keybindOnLoad;
6769
}, { once: true });
6870
</script>
6971
<main>
@@ -205,6 +207,13 @@ <h2 id="menutitle" class="screenreader-only">Navigation Controls</h2>
205207
<option value="high-contrast-dark">High Contrast Dark</option>
206208
</select>
207209
</li>
210+
<li id="keybinds" role="presentation" style="white-space: nowrap;">
211+
<label for="keybind-select">Key bindings:</label>
212+
<select id="keybind-select">
213+
<option value="default">Default</option>
214+
<option value="vim">Vim</option>
215+
</select>
216+
</li>
208217
</ul>
209218
</li>
210219

src/web/js/beforePyret.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -188,20 +188,24 @@ $(function() {
188188
const mac = CodeMirror.keyMap.default === CodeMirror.keyMap.macDefault;
189189
const modifier = mac ? "Cmd" : "Ctrl";
190190

191-
var cmOptions = {
192-
extraKeys: CodeMirror.normalizeKeyMap({
191+
defaultKeyMap = CodeMirror.normalizeKeyMap({
193192
"Shift-Enter": function(cm) { runFun(cm.getValue()); },
194193
"Shift-Ctrl-Enter": function(cm) { runFun(cm.getValue()); },
195194
"Tab": "indentAuto",
196195
"Ctrl-I": reindentAllLines,
197-
"Esc Left": "goBackwardSexp",
198196
"Alt-Left": "goBackwardSexp",
199-
"Esc Right": "goForwardSexp",
200197
"Alt-Right": "goForwardSexp",
201198
"Ctrl-Left": "goBackwardToken",
202199
"Ctrl-Right": "goForwardToken",
203200
[`${modifier}-/`]: "toggleComment",
204-
}),
201+
});
202+
CPO.noVimKeyMap = CodeMirror.normalizeKeyMap({
203+
"Esc Left": "goBackwardSexp",
204+
"Esc Right": "goForwardSexp",
205+
});
206+
207+
var cmOptions = {
208+
extraKeys: defaultKeyMap,
205209
indentUnit: 2,
206210
tabSize: 2,
207211
viewportMargin: Infinity,
@@ -221,6 +225,8 @@ $(function() {
221225
cmOptions = merge(cmOptions, options.cmOptions || {});
222226

223227
var CM = CodeMirror.fromTextArea(textarea[0], cmOptions);
228+
// we do this separately so we can more easily add and remove it for vim mode
229+
CM.addKeyMap(CPO.noVimKeyMap);
224230

225231
function firstLineIsNamespace() {
226232
const firstline = CM.getLine(0);

src/web/js/cpo-main.js

+30
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,36 @@
498498
localSettings.change("theme", function(_, newTheme) {
499499
applyTheme(newTheme);
500500
});
501+
502+
var curKeybind = document.getElementById("keybind-select").value;
503+
var keybindSelect = $("#keybind-select");
504+
505+
function applyKeybind(keybinding) {
506+
var cm = CPO.editor.cm;
507+
cm.state.keyMaps = [];
508+
if (keybinding !== 'vim') {
509+
cm.addKeyMap(CPO.noVimKeyMap, true);
510+
}
511+
curKeybind = keybinding;
512+
cm.setOption('keyMap', curKeybind);
513+
}
514+
515+
if (localSettings.getItem('keybind') !== null) {
516+
applyKeybind(localSettings.getItem('keybind'));
517+
} else {
518+
localSettings.setItem('keybind', curKeybind);
519+
}
520+
521+
$("#keybinds").change(function(e) {
522+
var value = e.target.value;
523+
applyKeybind(value);
524+
525+
localSettings.setItem("keybind", curKeybind);
526+
});
527+
528+
localSettings.change("keybind", function(_, newKeybinds) {
529+
applyKeybind(newKeybinds);
530+
});
501531

502532
$('.notificationArea').click(function() {$('.notificationArea span').fadeOut(1000);});
503533

0 commit comments

Comments
 (0)