| 1 |
// CodeMirror, copyright (c) by Marijn Haverbeke and others |
| 2 |
// Distributed under an MIT license: https://codemirror.net/5/LICENSE |
| 3 |
|
| 4 |
// Defines jumpToLine command. Uses dialog.js if present. |
| 5 |
|
| 6 |
(function(mod) { |
| 7 |
if (typeof exports == "object" && typeof module == "object") // CommonJS |
| 8 |
mod(require("../../lib/codemirror"), require("../dialog/dialog")); |
| 9 |
else if (typeof define == "function" && define.amd) // AMD |
| 10 |
define(["../../lib/codemirror", "../dialog/dialog"], mod); |
| 11 |
else // Plain browser env |
| 12 |
mod(CodeMirror); |
| 13 |
})(function(CodeMirror) { |
| 14 |
"use strict"; |
| 15 |
|
| 16 |
// default search panel location |
| 17 |
CodeMirror.defineOption("search", {bottom: false}); |
| 18 |
|
| 19 |
function dialog(cm, text, shortText, deflt, f) { |
| 20 |
if (cm.openDialog) cm.openDialog(text, f, {value: deflt, selectValueOnOpen: true, bottom: cm.options.search.bottom}); |
| 21 |
else f(prompt(shortText, deflt)); |
| 22 |
} |
| 23 |
|
| 24 |
function getJumpDialog(cm) { |
| 25 |
return cm.phrase("Jump to line:") + ' <input type="text" style="width: 10em" class="CodeMirror-search-field"/> <span style="color: #888" class="CodeMirror-search-hint">' + cm.phrase("(Use line:column or scroll% syntax)") + '</span>'; |
| 26 |
} |
| 27 |
|
| 28 |
function interpretLine(cm, string) { |
| 29 |
var num = Number(string) |
| 30 |
if (/^[-+]/.test(string)) return cm.getCursor().line + num |
| 31 |
else return num - 1 |
| 32 |
} |
| 33 |
|
| 34 |
CodeMirror.commands.jumpToLine = function(cm) { |
| 35 |
var cur = cm.getCursor(); |
| 36 |
dialog(cm, getJumpDialog(cm), cm.phrase("Jump to line:"), (cur.line + 1) + ":" + cur.ch, function(posStr) { |
| 37 |
if (!posStr) return; |
| 38 |
|
| 39 |
var match; |
| 40 |
if (match = /^\s*([\+\-]?\d+)\s*\:\s*(\d+)\s*$/.exec(posStr)) { |
| 41 |
cm.setCursor(interpretLine(cm, match[1]), Number(match[2])) |
| 42 |
} else if (match = /^\s*([\+\-]?\d+(\.\d+)?)\%\s*/.exec(posStr)) { |
| 43 |
var line = Math.round(cm.lineCount() * Number(match[1]) / 100); |
| 44 |
if (/^[-+]/.test(match[1])) line = cur.line + line + 1; |
| 45 |
cm.setCursor(line - 1, cur.ch); |
| 46 |
} else if (match = /^\s*\:?\s*([\+\-]?\d+)\s*/.exec(posStr)) { |
| 47 |
cm.setCursor(interpretLine(cm, match[1]), cur.ch); |
| 48 |
} |
| 49 |
}); |
| 50 |
}; |
| 51 |
|
| 52 |
CodeMirror.keyMap["default"]["Alt-G"] = "jumpToLine"; |
| 53 |
}); |