1 // A number of additional default bindings that are too obscure to
2 // include in the core codemirror.js file.
7 var Pos = CodeMirror.Pos;
9 function moveLines(cm, start, end, dist) {
10 if (!dist || start > end) return 0;
12 var from = cm.clipPos(Pos(start, 0)), to = cm.clipPos(Pos(end));
13 var text = cm.getRange(from, to);
15 if (start <= cm.firstLine())
16 cm.replaceRange("", from, Pos(to.line + 1, 0));
18 cm.replaceRange("", Pos(from.line - 1), to);
19 var target = from.line + dist;
20 if (target <= cm.firstLine()) {
21 cm.replaceRange(text + "\n", Pos(target, 0));
22 return cm.firstLine() - from.line;
24 var targetPos = cm.clipPos(Pos(target - 1));
25 cm.replaceRange("\n" + text, targetPos);
26 return targetPos.line + 1 - from.line;
30 function moveSelectedLines(cm, dist) {
31 var head = cm.getCursor("head"), anchor = cm.getCursor("anchor");
32 cm.operation(function() {
33 var moved = moveLines(cm, Math.min(head.line, anchor.line), Math.max(head.line, anchor.line), dist);
34 cm.setSelection(Pos(anchor.line + moved, anchor.ch), Pos(head.line + moved, head.ch));
38 CodeMirror.commands.moveLinesUp = function(cm) { moveSelectedLines(cm, -1); };
39 CodeMirror.commands.moveLinesDown = function(cm) { moveSelectedLines(cm, 1); };
41 CodeMirror.keyMap["default"]["Alt-Up"] = "moveLinesUp";
42 CodeMirror.keyMap["default"]["Alt-Down"] = "moveLinesDown";