branch: elpa/engine-mode commit 449c09afe58372c76fc2729f252ef86d79c181d8 Author: Harry Schwartz <he...@harryrschwartz.com> Commit: Harry Schwartz <he...@harryrschwartz.com>
Nest prefixed keymaps correctly We'd been handling prefixes by defining a string `engine/keymap-prefix` which contained the prefix, concatenating that with the `:keybinding` when an engine was defined, and then binding the engine to that resulting string. That's not great: * Sometimes perfectly valid prefix keys aren't represented as strings (e.g. `(kbd "s-/")`), so concatenation fails. * Engines can't be automatically rebound to a new prefix once they've been defined. * Keymaps are just cleaner and easier to handle. Instead, we're defining a proper keymap and doing away with `engine/keymap-prefix`. This isn't great for backwards compatibility, since folks may have been relying on setting that to get the correct keymap. Sorry about that. =/ Now users will need to use `engine/set-prefix-key`, which binds the keymap. This has the advantage of being a function, instead of just a variable, so we should be able to modify it in the future without disturbing the interface. --- README.md | 10 +++------- engine-mode.el | 29 ++++++++++++++++------------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 108b6cbbb6..ac0be62837 100644 --- a/README.md +++ b/README.md @@ -80,17 +80,13 @@ As mentioned about, see the implementation of the ## Changing the keymap prefix The default keymap prefix for `engine-mode` is `C-c /`. If you'd like -to use a different prefix (say, `C-c s`), you can redefine it: +to bind the keymap to an additional prefix (say, `C-c s`), you totally +can: ```emacs -(setq engine/keymap-prefix (kbd "C-c s")) +(engine/set-keymap-prefix (kbd "C-c s")) ``` -Make sure you redefine the keymap prefix *before* you define your -engines. `defengine` uses the prefix internally, so if you change the -prefix after defining your engines you'll find that they still use the -old prefix. - ## Custom docstrings `defengine` assigns each engine a reasonable default docstring, but diff --git a/engine-mode.el b/engine-mode.el index 6ad319178f..39067525ad 100644 --- a/engine-mode.el +++ b/engine-mode.el @@ -1,7 +1,7 @@ ;;; engine-mode.el --- Define and query search engines from within Emacs. ;; Author: Harry R. Schwartz <he...@harryrschwartz.com> -;; Version: 2015.05.19 +;; Version: 2015.06.13 ;; URL: https://github.com/hrs/engine-mode/engine-mode.el ;; This file is NOT part of GNU Emacs. @@ -23,7 +23,7 @@ ;; results in your default browser. ;; The `defengine' macro can also take an optional key combination, -;; prefixed with `engine/keymap-prefix' (which defaults to "C-c /"): +;; prefixed with "C-c /": ;; (defengine duckduckgo ;; "https://duckduckgo.com/?q=%s" @@ -50,17 +50,24 @@ ;;; Code: (eval-when-compile (require 'cl)) +(defvar engine-mode-map (make-sparse-keymap)) +(defvar engine-mode-prefixed-map (make-sparse-keymap)) + (define-minor-mode engine-mode "Minor mode for defining and querying search engines through Emacs. \\{engine-mode-map}" :global t - :keymap (make-sparse-keymap)) + :keymap engine-mode-map) -(defcustom engine/keymap-prefix (kbd "C-c /") - "Engine-mode keymap prefix." - :group 'engine-mode - :type 'string) +(defun engine/set-keymap-prefix (prefix-key) + "Bind the engine-mode keymap to a new prefix. +For example, to use \"C-c s\" instead of the default \"C-c /\": + +(engine/set-keymap-prefix (kbd \"C-c s\"))" + (define-key engine-mode-map prefix-key engine-mode-prefixed-map)) + +(engine/set-keymap-prefix (kbd "C-c /")) (defcustom engine/browser-function browse-url-browser-function "The default browser function used when opening a URL in an engine. @@ -96,12 +103,9 @@ Defaults to `browse-url-browser-function'." (capitalize (symbol-name engine-name)) " for the selected text. Prompt for input if none is selected.")) -(defun engine/scope-keybinding (keybinding) - (concat engine/keymap-prefix " " keybinding)) - (defun engine/bind-key (engine-name keybinding) (when keybinding - `(define-key engine-mode-map (kbd ,(engine/scope-keybinding keybinding)) + `(define-key engine-mode-prefixed-map ,keybinding (quote ,(engine/function-name engine-name))))) (cl-defmacro defengine (engine-name search-engine-url &key keybinding docstring (browser 'engine/browser-function) (term-transformation-hook 'identity)) @@ -130,8 +134,7 @@ In this case, searching for \"foobar\" will hit the url The optional keyword argument `keybinding' is a string describing the key to bind the new function. -Keybindings are prefixed by the `engine/keymap-prefix', which -defaults to `C-c /'. +Keybindings are in the `engine-mode-map', so they're prefixed. For example, to search Wikipedia, use: