Skip to content

Instantly share code, notes, and snippets.

@u27a4
Created January 1, 2018 03:01
Show Gist options
  • Save u27a4/12843fb781085af508c17f030e991384 to your computer and use it in GitHub Desktop.
Save u27a4/12843fb781085af508c17f030e991384 to your computer and use it in GitHub Desktop.
Defines change mode command for CodeMirror based on 'addon/search/jump-to-line.js'
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: http://codemirror.net/LICENSE
// Defines changeMode command. Uses dialog.js if present.
(function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../lib/codemirror"), require("../dialog/dialog"));
else if (typeof define == "function" && define.amd) // AMD
define(["../../lib/codemirror", "../dialog/dialog"], mod);
else // Plain browser env
mod(CodeMirror);
})(function(CodeMirror) {
"use strict";
function dialog(cm, text, shortText, deflt, f) {
if (cm.openDialog) cm.openDialog(text, f, {value: deflt, selectValueOnOpen: true});
else f(prompt(shortText, deflt));
}
var changeDialog =
'Change mode: <input type="text" style="width: 10em" class="CodeMirror-change-field"/> <span style="color: #888" class="CodeMirror-change-hint">(Use mode:extension or type/subtype syntax)</span>';
CodeMirror.commands.changeMode = function(cm) {
var cur = cm.getCursor();
dialog(cm, changeDialog, "Change mode:", "", function(modeStr) {
var m, mode, spec, info;
if (/\//.test(modeStr)) {
if (info = CodeMirror.findModeByMIME(modeStr)) {
mode = info.mode;
spec = modeStr;
}
} else if (info = CodeMirror.findModeByExtension(modeStr)) {
mode = info.mode;
spec = info.mime;
} else {
var
mode = spec = modeStr.toLowerCase();
}
if (mode) {
cm.setOption("mode", spec);
CodeMirror.autoLoadMode(cm, mode);
} else {
alert("Could not find a mode corresponding to " + modeStr);
}
});
};
CodeMirror.keyMap["default"]["Ctrl-Shift-L"] = "changeMode";
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment