1 // Copyright (c) 2006-2008 by Martin Stubenschrott <stubenschrott@vimperator.org>
2 // Copyright (c) 2007-2011 by Doug Kearns <dougkearns@gmail.com>
3 // Copyright (c) 2008-2012 Kris Maglione <maglione.k@gmail.com>
5 // This work is licensed for reuse under an MIT license. Details are
6 // given in the LICENSE.txt file included with this file.
11 var Modes = Module("modes", {
12 init: function init() {
14 this._main = 1; // NORMAL
15 this._extended = 0; // NONE
17 this._lastShown = null;
19 this._passNextKey = false;
20 this._passAllKeys = false;
21 this._recording = false;
22 this._replaying = false; // playing a macro
24 this._modeStack = update([], {
27 throw Error("Trying to pop last element in mode stack");
28 return pop.superapply(this, arguments);
36 this.boundProperties = {};
38 this.addMode("BASE", {
40 description: "The base mode for all other modes",
44 this.addMode("MAIN", {
46 description: "The base mode for most other modes",
50 this.addMode("COMMAND", {
52 description: "The base mode for most modes which accept commands rather than input"
55 this.addMode("NORMAL", {
57 description: "Active when nothing is focused",
60 this.addMode("CARET", {
62 description: "Active when the caret is visible in the web content",
66 get pref() prefs.get("accessibility.browsewithcaret"),
67 set pref(val) prefs.set("accessibility.browsewithcaret", val),
69 enter: function (stack) {
70 if (stack.pop && !this.pref)
72 else if (!stack.pop && !this.pref)
78 leave: function (stack) {
79 if (!stack.push && this.pref)
84 this.addMode("INPUT", {
86 description: "The base mode for input modes, including Insert and Command Line",
91 this.addMode("EMBED", {
92 description: "Active when an <embed> or <object> element is focused",
99 this.addMode("PASS_THROUGH", {
100 description: "All keys but <C-v> are ignored by " + config.appName,
106 this.addMode("QUOTE", {
107 description: "The next key sequence is ignored by " + config.appName + ", unless in Pass Through mode",
112 (modes.getStack(1).main == modes.PASS_THROUGH
113 ? (modes.getStack(2).main.display() || modes.getStack(2).main.name)
114 : "PASS THROUGH") + " (next)"
117 preExecute: function (map) { if (modes.main == modes.QUOTE && map.name !== "<C-v>") modes.pop(); },
118 postExecute: function (map) { if (modes.main == modes.QUOTE && map.name === "<C-v>") modes.pop(); },
119 onKeyPress: function (events) { if (modes.main == modes.QUOTE) modes.pop(); }
121 this.addMode("IGNORE", { hidden: true }, {
122 onKeyPress: function (events_) {
123 if (events.isCancelKey(DOM.Event.stringify(events_[0])))
131 this.addMode("MENU", {
132 description: "Active when a menu or other pop-up is open",
137 leave: function leave(stack) {
138 util.timeout(function () {
139 if (stack.pop && !modes.main.input && Events.isInputElement(dactyl.focusedElement))
140 modes.push(modes.INSERT);
145 this.addMode("LINE", {
146 extended: true, hidden: true
149 this.push(this.NORMAL, 0, {
150 enter: function (stack, prev) {
151 if (prefs.get("accessibility.browsewithcaret"))
152 prefs.set("accessibility.browsewithcaret", false);
154 statusline.updateStatus();
155 if (!stack.fromFocus && prev.main.ownsFocus)
156 dactyl.focusContent(true);
157 if (prev.main == modes.NORMAL) {
158 dactyl.focusContent(true);
159 for (let frame in values(buffer.allFrames())) {
160 // clear any selection made
161 let selection = frame.getSelection();
162 if (selection && !selection.isCollapsed)
163 selection.collapseToStart();
171 cleanup: function cleanup() {
176 "io.source": function ioSource(context, file, modTime) {
177 cache.flushEntry("modes.dtd", modTime);
181 _getModeMessage: function _getModeMessage() {
182 // when recording a macro
185 macromode = "recording " + this.recording + " ";
186 else if (this.replaying)
187 macromode = "replaying";
189 if (!options.get("showmode").getKey(this.main.allBases, false))
192 let modeName = this._modeMap[this._main].display();
197 macromode = " " + macromode;
198 return "-- " + modeName + " --" + macromode;
203 __iterator__: function __iterator__() array.iterValues(this.all),
205 get all() this._modes.slice(),
207 get mainModes() (mode for ([k, mode] in Iterator(modes._modeMap)) if (!mode.extended && mode.name == k)),
209 get mainMode() this._modeMap[this._main],
211 get passThrough() !!(this.main & (this.PASS_THROUGH|this.QUOTE)) ^ (this.getStack(1).main === this.PASS_THROUGH),
213 get topOfStack() this._modeStack[this._modeStack.length - 1],
215 addMode: function addMode(name, options, params) {
216 let mode = Modes.Mode(name, options, params);
220 this.modeChars[mode.char] = (this.modeChars[mode.char] || []).concat(mode);
221 this._modeMap[name] = mode;
222 this._modeMap[mode] = mode;
224 this._modes.push(mode);
226 this._mainModes.push(mode);
228 dactyl.triggerObserver("modes.add", mode);
231 removeMode: function removeMode(mode) {
233 if (this[mode.name] == mode)
234 delete this[mode.name];
235 if (this._modeMap[mode.name] == mode)
236 delete this._modeMap[mode.name];
237 if (this._modeMap[mode.mode] == mode)
238 delete this._modeMap[mode.mode];
240 this._mainModes = this._mainModes.filter(function (m) m != mode);
243 dumpStack: function dumpStack() {
244 util.dump("Mode stack:");
245 for (let [i, mode] in array.iterItems(this._modeStack))
246 util.dump(" " + i + ": " + mode);
249 getMode: function getMode(name) this._modeMap[name],
251 getStack: function getStack(idx) this._modeStack[this._modeStack.length - idx - 1] || this._modeStack[0],
253 get stack() this._modeStack.slice(),
255 getCharModes: function getCharModes(chr) (this.modeChars[chr] || []).slice(),
257 have: function have(mode) this._modeStack.some(function (m) isinstance(m.main, mode)),
259 matchModes: function matchModes(obj)
260 this._modes.filter(function (mode) Object.keys(obj)
261 .every(function (k) obj[k] == (mode[k] || false))),
263 // show the current mode string in the command line
264 show: function show() {
268 let msg = this._getModeMessage();
270 if (msg || loaded.commandline)
271 commandline.widgets.mode = msg || null;
274 remove: function remove(mode, covert) {
275 if (covert && this.topOfStack.main != mode) {
276 util.assert(mode != this.NORMAL);
277 for (let m; m = array.nth(this.modeStack, function (m) m.main == mode, 0);)
278 this._modeStack.splice(this._modeStack.indexOf(m));
280 else if (this.stack.some(function (m) m.main == mode)) {
287 delay: function delay(callback, self) { this.delayed.push([callback, self]); },
289 save: function save(id, obj, prop, test) {
290 if (!(id in this.boundProperties))
291 for (let elem in array.iterValues(this._modeStack))
292 elem.saved[id] = { obj: obj, prop: prop, value: obj[prop], test: test };
293 this.boundProperties[id] = { obj: util.weakReference(obj), prop: prop, test: test };
298 set: function set(mainMode, extendedMode, params, stack) {
299 var delayed, oldExtended, oldMain, prev, push;
302 dactyl.reportError(Error(_("mode.recursiveSet")), true);
306 params = params || Object.create(this.getMode(mainMode || this.main).params);
308 if (!stack && mainMode != null && this._modeStack.length > 1)
311 this.withSavedValues(["inSet"], function set() {
314 oldMain = this._main, oldExtended = this._extended;
316 if (extendedMode != null)
317 this._extended = extendedMode;
318 if (mainMode != null) {
319 this._main = mainMode;
321 this._extended = this.NONE;
324 if (stack && stack.pop && stack.pop.params.leave)
325 dactyl.trapErrors("leave", stack.pop.params,
326 stack, this.topOfStack);
328 push = mainMode != null && !(stack && stack.pop) &&
329 Modes.StackElement(this._main, this._extended, params, {});
331 if (push && this.topOfStack) {
332 if (this.topOfStack.params.leave)
333 dactyl.trapErrors("leave", this.topOfStack.params,
334 { push: push }, push);
336 for (let [id, { obj, prop, test }] in Iterator(this.boundProperties)) {
338 delete this.boundProperties[id];
340 this.topOfStack.saved[id] = { obj: obj.get(), prop: prop, value: obj.get()[prop], test: test };
344 delayed = this.delayed;
347 prev = stack && stack.pop || this.topOfStack;
349 this._modeStack.push(push);
352 if (stack && stack.pop)
353 for (let { obj, prop, value, test } in values(this.topOfStack.saved))
354 if (!test || !test(stack, prev))
355 dactyl.trapErrors(function () { obj[prop] = value });
359 if (this.topOfStack.params.enter && prev)
360 dactyl.trapErrors("enter", this.topOfStack.params,
361 push ? { push: push } : stack || {},
364 delayed.forEach(function ([fn, self]) dactyl.trapErrors(fn, self));
366 dactyl.triggerObserver("modes.change", [oldMain, oldExtended], [this._main, this._extended], stack);
370 onCaretChange: function onPrefChange(value) {
371 if (!value && modes.main == modes.CARET)
373 if (value && modes.main == modes.NORMAL)
374 modes.push(modes.CARET);
377 push: function push(mainMode, extendedMode, params) {
378 if (this.main == this.IGNORE)
381 this.set(mainMode, extendedMode, params, { push: this.topOfStack });
384 pop: function pop(mode, args) {
385 while (this._modeStack.length > 1 && this.main != mode) {
386 let a = this._modeStack.pop();
387 this.set(this.topOfStack.main, this.topOfStack.extended, this.topOfStack.params,
388 update({ pop: a }, args));
395 replace: function replace(mode, oldMode, args) {
396 while (oldMode && this._modeStack.length > 1 && this.main != oldMode)
399 if (this._modeStack.length > 1)
400 this.set(mode, null, null,
401 update({ push: this.topOfStack, pop: this._modeStack.pop() },
406 reset: function reset() {
407 if (this._modeStack.length == 1 && this.topOfStack.params.enter)
408 this.topOfStack.params.enter({}, this.topOfStack);
409 while (this._modeStack.length > 1)
413 get recording() this._recording,
414 set recording(value) { this._recording = value; this.show(); },
416 get replaying() this._replaying,
417 set replaying(value) { this._replaying = value; this.show(); },
419 get main() this._main,
420 set main(value) { this.set(value); },
422 get extended() this._extended,
423 set extended(value) { this.set(null, value); }
425 Mode: Class("Mode", {
426 init: function init(name, options, params) {
428 util.assert(options.bases.every(function (m) m instanceof this, this.constructor),
429 _("mode.invalidBases"), false);
432 id: 1 << Modes.Mode._id++,
439 description: Messages.Localized(""),
441 displayName: Class.Memoize(function () this.name.split("_").map(util.capitalize).join(" ")),
443 isinstance: function isinstance(obj)
444 this.allBases.indexOf(obj) >= 0 || callable(obj) && this instanceof obj,
446 allBases: Class.Memoize(function () {
447 let seen = {}, res = [], queue = [this].concat(this.bases);
448 for (let mode in array.iterValues(queue))
449 if (!Set.add(seen, mode)) {
451 queue.push.apply(queue, mode.bases);
456 get bases() this.input ? [modes.INPUT] : [modes.MAIN],
458 get count() !this.insert,
460 _display: Class.Memoize(function _display() this.name.replace("_", " ", "g")),
462 display: function display() this._display,
468 input: Class.Memoize(function input() this.insert || this.bases.length && this.bases.some(function (b) b.input)),
470 insert: Class.Memoize(function insert() this.bases.length && this.bases.some(function (b) b.insert)),
472 ownsFocus: Class.Memoize(function ownsFocus() this.bases.length && this.bases.some(function (b) b.ownsFocus)),
474 passEvent: function passEvent(event) this.input && event.charCode && !(event.ctrlKey || event.altKey || event.metaKey),
476 passUnknown: Class.Memoize(function () options.get("passunknown").getKey(this.name)),
480 get toStringParams() [this.name],
482 valueOf: function valueOf() this.id
486 StackElement: (function () {
487 const StackElement = Struct("main", "extended", "params", "saved");
488 StackElement.className = "Modes.StackElement";
489 StackElement.defaultValue("params", function () this.main.params);
491 update(StackElement.prototype, {
492 get toStringParams() !loaded.modes ? this.main.name : [
494 ["(", modes.all.filter(function (m) this.extended & m, this)
495 .map(function (m) m.name).join("|"),
502 boundProperty: function BoundProperty(desc) {
503 let id = this.cacheId++;
507 return Class.Property(update({
510 init: function bound_init(prop) update(this, {
511 get: function bound_get() {
513 var val = desc.get.call(this, value);
514 return val === undefined ? value : val;
516 set: function bound_set(val) {
517 modes.save(id, this, prop, desc.test);
519 value = desc.set.call(this, val);
520 value = !desc.set || value === undefined ? val : value;
526 cache: function initCache() {
527 function makeTree() {
528 let list = modes.all.filter(function (m) m.name !== m.description);
532 for (let mode in values(list))
533 tree[mode.name] = {};
535 for (let mode in values(list))
536 for (let base in values(mode.bases))
537 tree[base.name][mode.name] = tree[mode.name];
539 let roots = iter([m.name, tree[m.name]]
540 for (m in values(list))
541 if (!m.bases.length)).toObject();
544 let res = ["ul", { "dactyl:highlight": "Dense" }];
545 Object.keys(obj).sort().forEach(function (name) {
546 let mode = modes.getMode(name);
548 ["em", {}, mode.displayName],
549 ": ", mode.description,
561 cache.register("modes.dtd", function ()
562 util.makeDTD(iter({ "modes.tree": makeTree() },
565 mappings: function initMappings() {
566 mappings.add([modes.BASE, modes.NORMAL],
568 "Return to Normal mode",
569 function () { modes.reset(); });
571 mappings.add([modes.INPUT, modes.COMMAND, modes.OPERATOR, modes.PASS_THROUGH, modes.QUOTE],
573 "Return to the previous mode",
574 function () { modes.pop(null, { fromEscape: true }); });
576 mappings.add([modes.AUTOCOMPLETE, modes.MENU], ["<C-c>"],
577 "Leave Autocomplete or Menu mode",
578 function () { modes.pop(); });
580 mappings.add([modes.MENU], ["<Esc>"],
581 "Close the current popup",
583 if (events.popups.active.length)
584 return Events.PASS_THROUGH;
588 mappings.add([modes.MENU], ["<C-[>"],
589 "Close the current popup",
590 function () { events.feedkeys("<Esc>"); });
592 options: function initOptions() {
594 completer: function completer(context, extra) {
595 if (extra.value && context.filter[0] == "!")
597 return completer.superapply(this, arguments);
600 getKey: function getKey(val, default_) {
602 return (array.nth(this.value, function (v) val.some(function (m) m.name === v.mode), 0)
603 || { result: default_ }).result;
605 return Set.has(this.valueMap, val) ? this.valueMap[val] : default_;
608 setter: function (vals) {
609 modes.all.forEach(function (m) { delete m.passUnknown; });
611 vals = vals.map(function (v) update(new String(v.toLowerCase()), {
612 mode: v.replace(/^!/, "").toUpperCase(),
616 this.valueMap = values(vals).map(function (v) [v.mode, v.result]).toObject();
620 validator: function validator(vals) vals.map(function (v) v.replace(/^!/, "")).every(Set.has(this.values)),
622 get values() array.toObject([[m.name.toLowerCase(), m.description] for (m in values(modes._modes)) if (!m.hidden)])
625 options.add(["passunknown", "pu"],
626 "Pass through unknown keys in these modes",
627 "stringlist", "!text_edit,!visual,base",
630 options.add(["showmode", "smd"],
631 "Show the current mode in the command line when it matches this expression",
632 "stringlist", "caret,output_multiline,!normal,base,operator",
635 prefs: function initPrefs() {
636 prefs.watch("accessibility.browsewithcaret",
637 function () { modes.onCaretChange.apply(modes, arguments); });
641 // vim: set fdm=marker sw=4 sts=4 ts=8 et: