diff --git a/lib/ace/keyboard/emacs.js b/lib/ace/keyboard/emacs.js index fd533f1d..38e29f96 100644 --- a/lib/ace/keyboard/emacs.js +++ b/lib/ace/keyboard/emacs.js @@ -442,7 +442,7 @@ exports.handler.addCommands({ // Any insertion or mouse click resets mark-mode. // setMark twice in a row at the same place resets markmode. // in multi select mode, ea selection is handled individually - + if (args && args.count) { function moveToMark() { var mark = editor.popEmacsMark(); @@ -452,7 +452,7 @@ exports.handler.addCommands({ else moveToMark(); return; } - + var mark = editor.emacsMark(), ranges = editor.selection.getAllRanges(), rangePositions = ranges.map(function(r) { return {row: r.start.row, column: r.start.column}; }), @@ -467,7 +467,7 @@ exports.handler.addCommands({ if (mark) editor.pushEmacsMark(null); return; } - + if (!mark) { rangePositions.slice(0,-1).forEach(function(pos) { editor.pushEmacsMark(pos); }); editor.setEmacsMark(rangePositions[rangePositions.length-1]); @@ -479,22 +479,26 @@ exports.handler.addCommands({ multiSelectAction: "forEach" }, exchangePointAndMark: { - exec: function(editor, args) { - var sel = editor.selection; - if (args.count) { - var pos = editor.getCursorPosition(); - sel.clearSelection(); - sel.moveCursorToPosition(editor.popEmacsMark()); - editor.pushEmacsMark(pos); - return; - } - var lastMark = editor.getLastEmacsMark(); - var range = sel.getRange(); - if (range.isEmpty()) { - sel.selectToPosition(lastMark); - return; - } - sel.setSelectionRange(range, !sel.isBackwards()); + exec: function (editor, args) { + var restoreMarks = []; + editor.forEachSelection({ + exec: function() { + var sel = editor.selection; + if (args.count) { // replace mark and point + var pos = {row: sel.lead.row, column: sel.lead.column}; + restoreMarks.push(pos); + sel.clearSelection(); + sel.moveCursorToPosition(editor.popEmacsMark()); + } else if (sel.isEmpty()) { // move to mark, forget point + var lastMark = editor.popEmacsMark(); + restoreMarks.push(lastMark); + sel.selectToPosition(lastMark); + } else { // just invert selection + sel.setSelectionRange(sel.getRange(), !sel.isBackwards()); + } + } + }); + restoreMarks.reverse().forEach(function(p) { editor.pushEmacsMark(p); }); }, readOnly: true, handlesCount: true,