From feae10c6b5585a65c4d5c36d3c2e3eb67c4320e8 Mon Sep 17 00:00:00 2001 From: John Foerch Date: Thu, 21 Aug 2008 19:41:35 -0400 Subject: [PATCH] replace binding-hooks with explicit declaration of prefix commands --- modules/bindings/default/content-buffer/element.js | 32 +++---- modules/bindings/default/universal_argument.js | 8 +- modules/element.js | 12 +-- modules/interactive.js | 5 + modules/keyboard.js | 45 +++++---- modules/universal-argument.js | 103 ++++++++++++--------- 6 files changed, 106 insertions(+), 99 deletions(-) rewrite modules/universal-argument.js (76%) diff --git a/modules/bindings/default/content-buffer/element.js b/modules/bindings/default/content-buffer/element.js index 203e432..2f209bb 100644 --- a/modules/bindings/default/content-buffer/element.js +++ b/modules/bindings/default/content-buffer/element.js @@ -1,5 +1,5 @@ /** - * (C) Copyright 2007 John J. Foerch + * (C) Copyright 2007-2008 John J. Foerch * (C) Copyright 2007-2008 Jeremy Maitin-Shepard * * Use, modification, and distribution are subject to the terms specified in the @@ -9,35 +9,25 @@ require("bindings/default/content-buffer/normal.js"); require("element.js"); -define_key(content_buffer_normal_keymap, "i", null, - $hook = browser_object_class_selector("images")); +define_key(content_buffer_normal_keymap, "i", "browser-object-class-images"); -define_key(content_buffer_normal_keymap, "n", null, - $hook = browser_object_class_selector("links")); +define_key(content_buffer_normal_keymap, "n", "browser-object-class-links"); -define_key(content_buffer_normal_keymap, "m", null, - $hook = browser_object_class_selector("frames")); +define_key(content_buffer_normal_keymap, "m", "browser-object-class-frames"); -define_key(content_buffer_normal_keymap, "e", null, - $hook = browser_object_class_selector("media")); +define_key(content_buffer_normal_keymap, "e", "browser-object-class-media"); -define_key(content_buffer_normal_keymap, "S-8 e", null, - $hook = browser_object_class_selector("media")); +define_key(content_buffer_normal_keymap, "S-8 e", "browser-object-class-media"); -define_key(content_buffer_normal_keymap, "S-8 i", null, - $hook = browser_object_class_selector("images")); +define_key(content_buffer_normal_keymap, "S-8 i", "browser-object-class-images"); -define_key(content_buffer_normal_keymap, "S-8 n", null, - $hook = browser_object_class_selector("links")); +define_key(content_buffer_normal_keymap, "S-8 n", "browser-object-class-links"); -define_key(content_buffer_normal_keymap, "S-8 m", null, - $hook = browser_object_class_selector("frames")); +define_key(content_buffer_normal_keymap, "S-8 m", "browser-object-class-frames"); -define_key(content_buffer_normal_keymap, "S-8 M", null, - $hook = browser_object_class_selector("mathml")); +define_key(content_buffer_normal_keymap, "S-8 M", "browser-object-class-mathml"); -define_key(content_buffer_normal_keymap, "S-8 u", null, - $hook = browser_object_class_selector("url")); +define_key(content_buffer_normal_keymap, "S-8 u", "browser-object-class-url"); function bind_element_operations(keymap) { define_key(keymap, "f", "follow", $category = "Browser object"); diff --git a/modules/bindings/default/universal_argument.js b/modules/bindings/default/universal_argument.js index 91d8eeb..1f677ce 100644 --- a/modules/bindings/default/universal_argument.js +++ b/modules/bindings/default/universal_argument.js @@ -1,13 +1,15 @@ /** * (C) Copyright 2007-2008 Jeremy Maitin-Shepard + * (C) Copyright 2008 John Foerch * * Use, modification, and distribution are subject to the terms specified in the * COPYING file. **/ -define_key(default_base_keymap, "C-u", null, $hook = universal_argument); +define_key(default_base_keymap, "C-u", "universal-argument"); for (var i = 0; i <= 9; ++i) - define_key(universal_argument_keymap, String(i), null, $hook = universal_digit); -define_key(universal_argument_keymap, "subtract", null, $hook = universal_negate); + define_key(universal_argument_keymap, String(i), "universal-digit"); + +define_key(universal_argument_keymap, "subtract", "universal-negate"); diff --git a/modules/element.js b/modules/element.js index 686d33c..8b0ffe4 100644 --- a/modules/element.js +++ b/modules/element.js @@ -50,6 +50,12 @@ function define_browser_object_class(name) { obj.doc = arguments.$doc; if (handler !== undefined) obj.handler = handler; + interactive( + "browser-object-class-"+name, + "A prefix command to specify that the following command operate "+ + "on objects of type: "+name+".", + function (ctx) { ctx._browser_object_class = name; }, + $prefix = true); } define_browser_object_class("images", @@ -119,12 +125,6 @@ interactive_context.prototype.browser_object_class = function (action_name) { return cls; }; -function browser_object_class_selector(name) { - return function (ctx) { - ctx._browser_object_class = name; - } -} - function lookup_browser_object_class(class_name, action) { var obj; if (action != null) { diff --git a/modules/interactive.js b/modules/interactive.js index d68ca5d..00a63f1 100644 --- a/modules/interactive.js +++ b/modules/interactive.js @@ -16,9 +16,13 @@ var interactive_commands = new string_hashmap(); * This is optionally followed by a documentation string. * * This is followed by the command function. + * + * The $prefix keyword, when true, means that the command is a prefix-command. */ +define_keywords("$prefix"); function interactive(name) { + keywords(arguments); var doc = null; var handler; var offset = 1; @@ -31,6 +35,7 @@ function interactive(name) var cmd = { name: name, handler: handler, + prefix: arguments.$prefix, doc: doc, shortdoc: get_shortdoc_string(doc), source_code_reference: get_caller_source_code_reference() }; diff --git a/modules/keyboard.js b/modules/keyboard.js index 4748e20..81342f3 100644 --- a/modules/keyboard.js +++ b/modules/keyboard.js @@ -334,7 +334,7 @@ function kbd (spec, mods) return results; } -define_keywords("$fallthrough", "$hook", "$category"); +define_keywords("$fallthrough", "$category"); function define_key_internal(ref, kmap, keys, new_command, new_keymap) { keywords(arguments); @@ -355,7 +355,6 @@ outer: bind.command = new_command; bind.keymap = new_keymap; bind.fallthrough = args.$fallthrough; - bind.hook = args.$hook; bind.source_code_reference = ref; bind.category = args.$category; } else { @@ -368,7 +367,7 @@ outer: function make_binding() { if (final_binding) { - return {key: key, fallthrough: args.$fallthrough, hook: args.$hook, + return {key: key, fallthrough: args.$fallthrough, command: new_command, keymap: new_keymap, source_code_reference: ref, category: args.$category, @@ -445,7 +444,7 @@ outer: } // bind key to either the keymap or command in the keymap, kmap -define_keywords("$fallthrough", "$hook", "$category"); +define_keywords("$fallthrough", "$category"); function define_key(kmap, keys, cmd) { var orig_keys = keys; @@ -544,6 +543,18 @@ define_window_local_hook("key_press_hook", RUN_HOOK_UNTIL_SUCCESS); function key_press_handler(true_event) { + function show_partial_key_sequence (window, state, ctx) { + if (!state.help_displayed) + { + state.help_timer_ID = window.setTimeout(function () { + window.minibuffer.show(ctx.key_sequence.join(" ")); + state.help_displayed = true; + state.help_timer_ID = null; + }, keyboard_key_sequence_help_timeout); + } + else + window.minibuffer.show(ctx.key_sequence.join(" ")); + } try{ var window = this; var state = window.keyboard; @@ -612,32 +623,18 @@ function key_press_handler(true_event) // Finally, process the binding. ctx.key_sequence.push(format_key_event(event)); if (binding) { - if (binding.hook) { - binding.hook.call(null, ctx); - done = false; - } if (binding.keymap) { state.active_keymap = binding.keymap; - if (!state.help_displayed) - { - state.help_timer_ID = window.setTimeout(function () { - window.minibuffer.show(ctx.key_sequence.join(" ")); - state.help_displayed = true; - state.help_timer_ID = null; - }, keyboard_key_sequence_help_timeout); - } - else - window.minibuffer.show(ctx.key_sequence.join(" ")); - + show_partial_key_sequence(window, state, ctx); // We're going for another round done = false; } else if (binding.command) { call_interactively(ctx, binding.command); - done = true; - } else { - // binding was probably only a hook with no keymap or - // command. - state.active_keymap = null; + if (interactive_commands.get(binding.command).prefix) { + state.active_keymap = null; + show_partial_key_sequence(window, state, ctx); + done = false; + } } } else { window.minibuffer.message(ctx.key_sequence.join(" ") + " is undefined"); diff --git a/modules/universal-argument.js b/modules/universal-argument.js dissimilarity index 76% index ada4e4a..4ee5c8b 100644 --- a/modules/universal-argument.js +++ b/modules/universal-argument.js @@ -1,45 +1,58 @@ -/** - * (C) Copyright 2007-2008 Jeremy Maitin-Shepard - * (C) Copyright 2008 John Foerch - * - * Use, modification, and distribution are subject to the terms specified in the - * COPYING file. -**/ - -define_keymap("universal_argument_keymap"); - -function universal_argument(ctx) -{ - if (ctx.prefix_argument) { - if (typeof(ctx.prefix_argument) == "object") // must be array - ctx.prefix_argument = [ctx.prefix_argument[0] * 4]; - } else - ctx.prefix_argument = [4]; - ctx.overlay_keymap = universal_argument_keymap; -} - -function universal_digit(ctx) -{ - var digit = ctx.event.charCode - 48; - if (ctx.prefix_argument == null) - ctx.prefix_argument = digit; - else if (typeof(ctx.prefix_argument) == "object") { // array - if (ctx.prefix_argument[0] < 0) - ctx.prefix_argument = -digit; - else - ctx.prefix_argument = digit; - } - else if (ctx.prefix_argument < 0) - ctx.prefix_argument = ctx.prefix_argument * 10 - digit; - else - ctx.prefix_argument = ctx.prefix_argument * 10 + digit; -} - -function universal_negate(ctx) -{ - if (typeof ctx.prefix_argument == "object") - ctx.prefix_argument[0] = 0 - ctx.prefix_argument[0]; - else - ctx.prefix_argument = 0 - ctx.prefix_argument; -} - +/** + * (C) Copyright 2007-2008 Jeremy Maitin-Shepard + * (C) Copyright 2008 John Foerch + * + * Use, modification, and distribution are subject to the terms specified in the + * COPYING file. +**/ + +define_keymap("universal_argument_keymap"); + +interactive ( + "universal-argument", + "Begin a numeric argument for the following command.", + function (ctx) + { + if (ctx.prefix_argument) { + if (typeof(ctx.prefix_argument) == "object") // must be array + ctx.prefix_argument = [ctx.prefix_argument[0] * 4]; + } else + ctx.prefix_argument = [4]; + ctx.overlay_keymap = universal_argument_keymap; + }, + $prefix = true); + +interactive ( + "universal-digit", + "Part of the numeric argument for the next command.", + function (ctx) + { + var digit = ctx.event.charCode - 48; + if (ctx.prefix_argument == null) + ctx.prefix_argument = digit; + else if (typeof(ctx.prefix_argument) == "object") { // array + if (ctx.prefix_argument[0] < 0) + ctx.prefix_argument = -digit; + else + ctx.prefix_argument = digit; + } + else if (ctx.prefix_argument < 0) + ctx.prefix_argument = ctx.prefix_argument * 10 - digit; + else + ctx.prefix_argument = ctx.prefix_argument * 10 + digit; + }, + $prefix = true); + +interactive ( + "universal-negate", + "Part of the numeric argument for the next command. "+ + "This command negates the numeric argument.", + function universal_negate(ctx) + { + if (typeof ctx.prefix_argument == "object") + ctx.prefix_argument[0] = 0 - ctx.prefix_argument[0]; + else + ctx.prefix_argument = 0 - ctx.prefix_argument; + }, + $prefix = true); + -- 2.11.4.GIT