Skip to content

Commit 8b950c1

Browse files
committed
Merge pull request ajaxorg#1849 from LivelyKernel/emacs-fixes-2014-03-12
Emacs fixes 2014 03 12
2 parents 6c235fe + 12343e0 commit 8b950c1

6 files changed

Lines changed: 86 additions & 20 deletions

File tree

lib/ace/commands/incremental_search_commands.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ exports.iSearchCommands = [{
9494
}, {
9595
name: "extendSearchTerm",
9696
exec: function(iSearch, string) {
97-
iSearch.addChar(string);
97+
iSearch.addString(string);
9898
},
9999
readOnly: true,
100100
isIncrementalSearchCommand: true
101101
}, {
102102
name: "extendSearchTermSpace",
103103
bindKey: "space",
104-
exec: function(iSearch) { iSearch.addChar(' '); },
104+
exec: function(iSearch) { iSearch.addString(' '); },
105105
readOnly: true,
106106
isIncrementalSearchCommand: true
107107
}, {
@@ -134,6 +134,34 @@ exports.iSearchCommands = [{
134134
},
135135
readOnly: true,
136136
isIncrementalSearchCommand: true
137+
}, {
138+
name: "yankNextWord",
139+
bindKey: "Ctrl-w",
140+
exec: function(iSearch) {
141+
var ed = iSearch.$editor,
142+
range = ed.selection.getRangeOfMovements(function(sel) { sel.moveCursorWordRight(); }),
143+
string = ed.session.getTextRange(range);
144+
iSearch.addString(string);
145+
},
146+
readOnly: true,
147+
isIncrementalSearchCommand: true
148+
}, {
149+
name: "yankNextChar",
150+
bindKey: "Ctrl-Alt-y",
151+
exec: function(iSearch) {
152+
var ed = iSearch.$editor,
153+
range = ed.selection.getRangeOfMovements(function(sel) { sel.moveCursorRight(); }),
154+
string = ed.session.getTextRange(range);
155+
iSearch.addString(string);
156+
},
157+
readOnly: true,
158+
isIncrementalSearchCommand: true
159+
}, {
160+
name: 'recenterTopBottom',
161+
bindKey: 'Ctrl-l',
162+
exec: function(iSearch) { iSearch.$editor.execCommand('recenterTopBottom'); },
163+
readOnly: true,
164+
isIncrementalSearchCommand: true
137165
}];
138166

139167
function IncrementalSearchKeyboardHandler(iSearch) {
@@ -163,6 +191,8 @@ oop.inherits(IncrementalSearchKeyboardHandler, HashHandler);
163191

164192
var handleKeyboard$super = this.handleKeyboard;
165193
this.handleKeyboard = function(data, hashId, key, keyCode) {
194+
if (((hashId === 1/*ctrl*/ || hashId === 8/*command*/) && key === 'v')
195+
|| (hashId === 1/*ctrl*/ && key === 'y')) return null;
166196
var cmd = handleKeyboard$super.call(this, data, hashId, key, keyCode);
167197
if (cmd.command) { return cmd; }
168198
if (hashId == -1) {

lib/ace/incremental_search.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,22 @@ oop.inherits(IncrementalSearch, Search);
7272
this.$options.needle = '';
7373
this.$options.backwards = backwards;
7474
ed.keyBinding.addKeyboardHandler(this.$keyboardHandler);
75+
// we need to completely intercept paste, just registering an event handler does not work
76+
this.$originalEditorOnPaste = ed.onPaste; ed.onPaste = this.onPaste.bind(this);
7577
this.$mousedownHandler = ed.addEventListener('mousedown', this.onMouseDown.bind(this));
7678
this.selectionFix(ed);
7779
this.statusMessage(true);
7880
}
7981

8082
this.deactivate = function(reset) {
8183
this.cancelSearch(reset);
82-
this.$editor.keyBinding.removeKeyboardHandler(this.$keyboardHandler);
84+
var ed = this.$editor;
85+
ed.keyBinding.removeKeyboardHandler(this.$keyboardHandler);
8386
if (this.$mousedownHandler) {
84-
this.$editor.removeEventListener('mousedown', this.$mousedownHandler);
87+
ed.removeEventListener('mousedown', this.$mousedownHandler);
8588
delete this.$mousedownHandler;
8689
}
90+
ed.onPaste = this.$originalEditorOnPaste;
8791
this.message('');
8892
}
8993

@@ -150,9 +154,9 @@ oop.inherits(IncrementalSearch, Search);
150154
return found;
151155
}
152156

153-
this.addChar = function(c) {
157+
this.addString = function(s) {
154158
return this.highlightAndFindWithNeedle(false, function(needle) {
155-
return needle + c;
159+
return needle + s;
156160
});
157161
}
158162

@@ -181,6 +185,10 @@ oop.inherits(IncrementalSearch, Search);
181185
return true;
182186
}
183187

188+
this.onPaste = function(text) {
189+
this.addString(text);
190+
}
191+
184192
this.statusMessage = function(found) {
185193
var options = this.$options, msg = '';
186194
msg += options.backwards ? 'reverse-' : '';

lib/ace/incremental_search_test.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,17 @@ module.exports = {
9595

9696
"test: find simple text incrementally" : function() {
9797
iSearch.activate(editor);
98-
var range = iSearch.addChar('1'), // "1"
98+
var range = iSearch.addString('1'), // "1"
9999
highlightRanges = callHighlighterUpdate(editor.session);
100100
testRanges("Range: [0/3] -> [0/4]", [range], "range");
101101
testRanges("Range: [0/3] -> [0/4],Range: [1/3] -> [1/4]", highlightRanges, "highlight");
102102

103-
range = iSearch.addChar('2'); // "12"
103+
range = iSearch.addString('2'); // "12"
104104
highlightRanges = callHighlighterUpdate(editor.session);
105105
testRanges("Range: [0/3] -> [0/5]", [range], "range");
106106
testRanges("Range: [0/3] -> [0/5],Range: [1/3] -> [1/5]", highlightRanges, "highlight");
107107

108-
range = iSearch.addChar('3'); // "123"
108+
range = iSearch.addString('3'); // "123"
109109
highlightRanges = callHighlighterUpdate(editor.session);
110110
testRanges("Range: [0/3] -> [0/6]", [range], "range");
111111
testRanges("Range: [0/3] -> [0/6]", highlightRanges, "highlight");
@@ -118,7 +118,7 @@ module.exports = {
118118

119119
"test: forward / backward" : function() {
120120
iSearch.activate(editor);
121-
iSearch.addChar('1'); iSearch.addChar('2');
121+
iSearch.addString('1'); iSearch.addString('2');
122122
var range = iSearch.next();
123123
testRanges("Range: [1/3] -> [1/5]", [range], "range");
124124

@@ -131,33 +131,33 @@ module.exports = {
131131

132132
"test: cancelSearch" : function() {
133133
iSearch.activate(editor);
134-
iSearch.addChar('1'); iSearch.addChar('2');
134+
iSearch.addString('1'); iSearch.addString('2');
135135
var range = iSearch.cancelSearch(true);
136136
testRanges("Range: [0/0] -> [0/0]", [range], "range");
137137

138-
iSearch.addChar('1'); range = iSearch.addChar('2');
138+
iSearch.addString('1'); range = iSearch.addString('2');
139139
testRanges("Range: [0/3] -> [0/5]", [range], "range");
140140
},
141141

142142
"test: failing search keeps pos" : function() {
143143
iSearch.activate(editor);
144-
iSearch.addChar('1'); iSearch.addChar('2');
145-
var range = iSearch.addChar('x');
144+
iSearch.addString('1'); iSearch.addString('2');
145+
var range = iSearch.addString('x');
146146
testRanges("", [range], "range");
147147
assert.position(editor.getCursorPosition(), 0, 5);
148148
},
149149

150150
"test: backwards search" : function() {
151151
editor.moveCursorTo(1,0);
152152
iSearch.activate(editor, true);
153-
iSearch.addChar('1'); var range = iSearch.addChar('2');;
153+
iSearch.addString('1'); var range = iSearch.addString('2');;
154154
testRanges("Range: [0/5] -> [0/3]", [range], "range");
155155
assert.position(editor.getCursorPosition(), 0, 3);
156156
},
157157

158158
"test: forwards then backwards, same result, reoriented range" : function() {
159159
iSearch.activate(editor);
160-
iSearch.addChar('1'); var range = iSearch.addChar('2');;
160+
iSearch.addString('1'); var range = iSearch.addString('2');;
161161
testRanges("Range: [0/3] -> [0/5]", [range], "range");
162162
assert.position(editor.getCursorPosition(), 0, 5);
163163

@@ -168,7 +168,7 @@ module.exports = {
168168

169169
"test: reuse prev search via option" : function() {
170170
iSearch.activate(editor);
171-
iSearch.addChar('1'); iSearch.addChar('2');;
171+
iSearch.addString('1'); iSearch.addString('2');;
172172
assert.position(editor.getCursorPosition(), 0, 5);
173173
iSearch.deactivate();
174174

@@ -179,14 +179,14 @@ module.exports = {
179179

180180
"test: don't extend selection range if selection is empty" : function() {
181181
iSearch.activate(editor);
182-
iSearch.addChar('1'); iSearch.addChar('2');;
182+
iSearch.addString('1'); iSearch.addString('2');;
183183
testRanges("Range: [0/5] -> [0/5]", [editor.getSelectionRange()], "sel range");
184184
},
185185

186186
"test: extend selection range if selection exists" : function() {
187187
iSearch.activate(editor);
188188
editor.selection.selectTo(0, 1);
189-
iSearch.addChar('1'); iSearch.addChar('2');;
189+
iSearch.addString('1'); iSearch.addString('2');;
190190
testRanges("Range: [0/0] -> [0/5]", [editor.getSelectionRange()], "sel range");
191191
},
192192

@@ -195,7 +195,7 @@ module.exports = {
195195
editor.keyBinding.addKeyboardHandler(emacs.handler);
196196
emacs.handler.commands.setMark.exec(editor);
197197
iSearch.activate(editor);
198-
iSearch.addChar('1'); iSearch.addChar('2');;
198+
iSearch.addString('1'); iSearch.addString('2');;
199199
testRanges("Range: [0/0] -> [0/5]", [editor.getSelectionRange()], "sel range");
200200
}
201201

lib/ace/keyboard/emacs.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ exports.handler.bindKey = function(key, command) {
219219
};
220220

221221
exports.handler.handleKeyboard = function(data, hashId, key, keyCode) {
222+
// if keyCode == -1 a non-printable key was pressed, such as just
223+
// control. Handling those is currently not supported in this handler
224+
if (keyCode === -1) return undefined;
225+
222226
var editor = data.editor;
223227
// insertstring data.count times
224228
if (hashId == -1) {

lib/ace/occur.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,15 @@ oop.inherits(Occur, Search);
112112
occurSession.$occur = this;
113113
occurSession.$occurMatchingLines = found;
114114
editor.setSession(occurSession);
115+
this.$useEmacsStyleLineStart = this.$originalSession.$useEmacsStyleLineStart;
116+
occurSession.$useEmacsStyleLineStart = this.$useEmacsStyleLineStart;
115117
this.highlight(occurSession, options.re);
116118
occurSession._emit('changeBackMarker');
117119
}
118120

119121
this.displayOriginalContent = function(editor) {
120122
editor.setSession(this.$originalSession);
123+
this.$originalSession.$useEmacsStyleLineStart = this.$useEmacsStyleLineStart;
121124
}
122125

123126
/**

lib/ace/selection.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,27 @@ var Selection = function(session) {
889889
return range;
890890
}
891891

892+
/**
893+
* Saves the current cursor position and calls `func` that can change the cursor
894+
* postion. The result is the range of the starting and eventual cursor position.
895+
* Will reset the cursor position.
896+
* @param {Function} The callback that should change the cursor position
897+
* @returns {Range}
898+
*
899+
**/
900+
this.getRangeOfMovements = function(func) {
901+
var start = this.getCursor();
902+
try {
903+
func.call(null, this);
904+
var end = this.getCursor();
905+
return Range.fromPoints(start,end);
906+
} catch(e) {
907+
return Range.fromPoints(start,start);
908+
} finally {
909+
this.moveCursorToPosition(start);
910+
}
911+
}
912+
892913
this.toJSON = function() {
893914
if (this.rangeCount) {
894915
var data = this.ranges.map(function(r) {

0 commit comments

Comments
 (0)