From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Alex Schroeder Newsgroups: gmane.emacs.devel Subject: Re: Customizing key bindings Date: Sat, 07 Sep 2002 15:39:55 +0200 Sender: emacs-devel-admin@gnu.org Message-ID: <87lm6dkjf8.fsf_-_@emacswiki.org> References: <200208271621.g7RGLNm30516@rum.cs.yale.edu> <5xhehfe3aj.fsf@kfs2.cua.dk> <874rdfaytt.fsf@pot.cnuce.cnr.it> <5xvg5sh06u.fsf@kfs2.cua.dk> <20020830235528.GA13207@gnu.org> <87ofbji88u.fsf@emacswiki.org> <87sn0scb0b.fsf@emacswiki.org> <87bs7ama8g.fsf@emacswiki.org> NNTP-Posting-Host: localhost.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: main.gmane.org 1031405980 15087 127.0.0.1 (7 Sep 2002 13:39:40 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Sat, 7 Sep 2002 13:39:40 +0000 (UTC) Return-path: Original-Received: from quimby.gnus.org ([80.91.224.244]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 17nfoI-0003vC-00 for ; Sat, 07 Sep 2002 15:39:38 +0200 Original-Received: from monty-python.gnu.org ([199.232.76.173]) by quimby.gnus.org with esmtp (Exim 3.12 #1 (Debian)) id 17ngNF-0000yQ-00 for ; Sat, 07 Sep 2002 16:15:46 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.10) id 17nfoB-0002lv-00; Sat, 07 Sep 2002 09:39:31 -0400 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.10) id 17nfmS-0002aA-00 for emacs-devel@gnu.org; Sat, 07 Sep 2002 09:37:44 -0400 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.10) id 17nfmO-0002Zs-00 for emacs-devel@gnu.org; Sat, 07 Sep 2002 09:37:43 -0400 Original-Received: from relay01.cablecom.net ([62.2.33.101]) by monty-python.gnu.org with esmtp (Exim 4.10) id 17nfmN-0002Zo-00 for emacs-devel@gnu.org; Sat, 07 Sep 2002 09:37:39 -0400 Original-Received: from smtp.swissonline.ch (mail-4.swissonline.ch [62.2.32.85]) by relay01.cablecom.net (8.12.5/8.12.5/SOL/AWF/MXRELAY/20020820) with ESMTP id g87Dbc20034990 for ; Sat, 7 Sep 2002 15:37:38 +0200 (CEST) (envelope-from alex@emacswiki.org) Original-Received: from confusibombus (dclient217-162-239-98.hispeed.ch [217.162.239.98]) by smtp.swissonline.ch (8.11.6/8.11.6/SMTPSOL/AWF/2002040101) with ESMTP id g87DbZF18290 for ; Sat, 7 Sep 2002 15:37:35 +0200 (MEST) Original-Received: from alex by confusibombus with local (Exim 3.35 #1 (Debian)) id 17nfoa-00006q-00 for ; Sat, 07 Sep 2002 15:39:56 +0200 Original-To: emacs-devel@gnu.org X-Face: ^BC$`[IcggstLPyen&dqF+b2'zyK#r.mU*'Nms}@&4zw%SJ#5!/7SMVjBS7'lb;QK)|IPU5U'o1'522W4TyzB3Ab*IBo^iw]l4|kUbdZuUDO6=Um-.4IzhNiV'B"@K#jy_(wW|Zbk[34flKY^|PrQ?$u2\fKg^]AY>wOX#H32i In-Reply-To: <87bs7ama8g.fsf@emacswiki.org> (Alex Schroeder's message of "Sat, 07 Sep 2002 11:15:27 +0200") Original-Lines: 351 User-Agent: Gnus/5.090008 (Oort Gnus v0.08) Emacs/21.2.90 (i686-pc-linux-gnu) Errors-To: emacs-devel-admin@gnu.org X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.0.11 Precedence: bulk List-Help: List-Post: List-Subscribe: , List-Id: Emacs development discussions. List-Unsubscribe: , List-Archive: Xref: main.gmane.org gmane.emacs.devel:7688 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:7688 Alex Schroeder writes: > (defun key-sequence-value-create (widget) > (let ((value (widget-default-get widget)) > (map (or (widget-ancestor-get widget :keymap) > (current-global-map)));; FIXME > (button (widget-create-child-and-convert > widget '(key-sequence-button))) > (field (widget-create-child-value > widget '(key-sequence-field :format " %vOld binding: ") > (widget-get widget :value)))) > (let* ((command (condition-case nil > (lookup-key map (read-kbd-macro value)) > (error nil))) > (binding (key-sequence-describe widget command))) > (widget-put widget :children (list field)) > (widget-put widget :buttons (list binding button))))) Hm, I played around with the widget-browser for the first time. Nice piece of work. :) It helped me solve a problem. New version is attached. Save it, load it, then M-x customize-option my-keymap and play around with it. The only key that has an old binding in my-keymap is f11. The problem with the code above is that none of the parents has a correct :keymap property. What we need, here, is to either set an appropriate property from custom-declare-keymap (how?), or to search for something else: We need the first parent having a :value property that is a keymap. What do you think? Widget object browser. Class: custom-variable :documentation-shown t :custom-state changed :tag "My Keymap" :value my-keymap Here is a way to implement this. (defun widget-ancestor-get (widget property &optional predicate) "Starting from WIDGET, return the value of PROPERTY. The value of PROPERTY will only be returned if it satisfies PREDICATE. PREDICATE is a function accepting one parameter, the value of PROPERTY. If PROPERTY is not specified or nil in WIDGET and the :parent property is non-nil, call `widget-ancestor-get' recusively with the value of the :parent property. Otherwise, return nil." (cond ((and predicate (widget-get widget property) (funcall predicate (widget-get widget property))) (widget-get widget property)) ((and (not predicate) (widget-get widget property))) ((widget-get widget :parent) (widget-ancestor-get (widget-get widget :parent) property)) (nil))) Then we can use the following helper (assuming that widget-ancestor-get will be used in other contexts in the future, eventhough it is defined in cus-key.el only at the moment): (defun key-sequence-find-map (widget) "Find the map that WIDGET is supposed to customize." (let ((sym (widget-ancestor-get widget :value (lambda (sym) (and (symbolp sym) (boundp sym) (keymapp (symbol-value sym))))))) (if sym (symbol-value sym) (current-global-map)))) I have retained the alternative (current-global-map), but I don't know when it would ever be used... If it ever got used, that would indicate a bug to me. What do you think, Per? Alex. ;;; cus-key.el -- Customize support for changing key bindings. (require 'wid-edit) (defvar custom-global-keymap (let ((map (make-sparse-keymap))) (set-keymap-parent map global-map) map) "Global keymap for use by Customize. This is automatically generated from `global-key-bindings', you should never change this manually. Instead, change either `global-map' from Lisp or `global-key-bindings' from Customize.") (defun quoted-key-insert (key) "Insert a string representation of the next key typed. The string representation is a representation understood by `read-kbd-macro'." (interactive "KPress a key: ") (insert (edmacro-format-keys key))) (defvar key-sequence-widget-map (let ((map (make-sparse-keymap))) (set-keymap-parent map widget-field-keymap) (define-key map (kbd "C-q") 'quoted-key-insert) map) "Keymap for the `key-sequence' widget.") (define-widget 'key-sequence-field 'string "Field for entering key bindings." :tag "Key sequence" :error "Not a well-formed key sequence" :validate 'key-sequence-field-validate :keymap key-sequence-widget-map) (defun key-sequence-field-validate (widget) (let ((value (widget-apply widget :value-get))) (condition-case nil (progn (read-kbd-macro value) nil) (error widget)))) (define-widget 'key-sequence-button 'push-button "Button for entering key bindings." :tag "Key sequence" :action 'key-sequence-button-action) (defun key-sequence-button-action (widget &optional event) (let ((key (read-key-sequence "Press key sequence: "))) (widget-value-set (widget-get widget :parent) (edmacro-format-keys key)) (widget-setup))) (define-widget 'key-sequence 'default "Widget for entering key bindings." :tag "Read key sequence" :match 'key-sequence-match :format "%v" :value "" :value-create 'key-sequence-value-create :value-delete 'widget-children-value-delete :value-get 'widget-choice-value-get :validate 'widget-children-validate :notify 'key-sequence-notify) (defun key-sequence-match (widget value) (stringp value)) (defun widget-ancestor-get (widget property &optional predicate) "Starting from WIDGET, return the value of PROPERTY. The value of PROPERTY will only be returned if it satisfies PREDICATE. PREDICATE is a function accepting one parameter, the value of PROPERTY. If PROPERTY is not specified or nil in WIDGET and the :parent property is non-nil, call `widget-ancestor-get' recusively with the value of the :parent property. Otherwise, return nil." (cond ((and predicate (widget-get widget property) (funcall predicate (widget-get widget property))) (widget-get widget property)) ((and (not predicate) (widget-get widget property))) ((widget-get widget :parent) (widget-ancestor-get (widget-get widget :parent) property predicate)) (nil))) (defun key-sequence-find-map (widget) "Find the map that WIDGET is supposed to customize." (let ((sym (widget-ancestor-get widget :value (lambda (sym) (and (symbolp sym) (boundp sym) (keymapp (symbol-value sym))))))) (if sym (symbol-value sym) (current-global-map)))) (defun key-sequence-describe (widget command) "Create a child to WIDGET that describes COMMAND. The child widget is returned." (cond ((functionp command) (widget-create-child-value widget '(function-item) command)) ((null command) (widget-create-child-value widget '(item) "Undefined")) ((numberp command) (widget-create-child-value widget '(item) "Binding too long")) ((keymapp command) (widget-create-child-value widget '(item) "Prefix key")) (t (widget-create-child-value widget '(item) "Dude, this is too weird")))) (defun key-sequence-value-create (widget) (let ((value (widget-default-get widget)) (map (key-sequence-find-map widget)) (button (widget-create-child-and-convert widget '(key-sequence-button))) (field (widget-create-child-value widget '(key-sequence-field :format " %vOld binding: ") (widget-get widget :value)))) (let* ((command (condition-case nil (lookup-key map (read-kbd-macro value)) (error nil))) (binding (key-sequence-describe widget command))) (widget-put widget :children (list field)) (widget-put widget :buttons (list binding button))))) (defun key-sequence-notify (widget child &optional event) "Update the old binding, and notify parent." (let* ((buttons (widget-get widget :buttons)) (binding (car buttons)) (children (widget-get widget :buttons)) (field (car children)) (value (widget-value child)) (map (key-sequence-find-map widget)) (command (condition-case nil (lookup-key map (read-kbd-macro value)) (error nil)))) (save-excursion (goto-char (widget-get binding :from)) (widget-delete binding) (setcar buttons (key-sequence-describe widget command)))) (widget-default-notify widget child event)) (define-widget 'command 'function "An interactive Lisp function." :complete-function (lambda () (interactive) (lisp-complete-symbol 'commandp)) :prompt-match 'commandp :match-alternatives '(commandp) :validate (lambda (widget) (unless (commandp (widget-value widget)) (widget-put widget :error (format "Invalid function: %S" (widget-value widget))) widget)) :value 'ignore :tag "Command") (defmacro defkeymap (symbol map doc &rest args) "Define SYMBOL to be a keymap with value MAP. DOC is the keymap documentation." ;; It is better not to use backquote in this file, ;; because that makes a bootstrapping problem ;; if you need to recompile all the Lisp files using interpreted code. (nconc (list 'custom-declare-keymap (list 'quote symbol) (list 'quote map) doc) args)) (defun custom-declare-keymap (symbol map doc &rest args) "Like `defkeymap', but SYMBOL and MAP are evaluated as normal arguments. MAP should be an expression to evaluate to compute the default value, not the default value itself. The DOC string will be expanded with some standard instructions for customization." ;; Keymaps are variables. The only difference is that we know lots ;; of defcustom properties already. (setq doc (concat doc "\n While entering the name of a key, you can either type keys yourself just as they appear in the manual, as in C-c a. You must use angle brackets for function keys, as in . You can also hit C-q and type the key. C-q will insert the correct string representation for you. For longer sequences, you can also invoke the [Key sequence] button, and type the entire key sequence directly. While entering the name of the command, you can use M-TAB to complete it.")) (apply 'custom-declare-variable symbol map doc :type '(repeat (group key-sequence command)) :set 'custom-set-keymap :get 'custom-get-keymap args)) (defun custom-set-keymap (sym bindings) "Update keymap SYM with BINDINGS. This also does the necessary book-keeping to save away shadowed bindings and restoring them if necessary." (let ((standard-bindings (eval (car (get sym 'standard-value)))) (old-bindings (car (get sym 'custom-bindings)))) ;; When defkeymap is called for the first time, BINDINGS is the ;; standard-value. When customized, BINDINGS is no longer a ;; keymap but an alist of bindings. (if (equal bindings standard-bindings) (set-default sym standard-bindings) ;; remove all keys no longer in bindings (mapc (lambda (bind) (unless (assoc (car bind) bindings) (custom-remove-key sym (read-kbd-macro (car bind))))) old-bindings) ;; define all keys in bindings (mapc (lambda (bind) (custom-add-key sym (read-kbd-macro (car bind)) (cadr bind))) bindings) ;; store the current bindings away (put sym 'custom-bindings (list bindings))))) (defun custom-get-keymap (sym) "Return the additions to the standard-value of keymap SYM. These additions are stored in the custom-bindings property by `custom-set-keymap'." (car (get sym 'custom-bindings))) (defun custom-add-key (sym key def) "Add KEY to the keymap stored in SYM with definition DEF. The shadowed binding is stored, if none has been stored before. The shadowed bindings are stored in the custom-bindings-shadow property." (let* ((map (symbol-value sym)) (old-binding (lookup-key map key)) (shadows (get sym 'custom-bindings-shadow)) (shadow-binding (cdr (assoc key shadows)))) (when (and old-binding (not shadow-binding)) (put sym 'custom-bindings-shadow (cons (cons key old-binding) shadows))) (define-key map key def))) (defun custom-remove-key (sym key) "Remove KEY from the keymap stored in SYM. The shadowed binding is restored, if there is one." (let ((def (cdr (assoc key (get sym 'custom-bindings-shadow)))) (map (symbol-value sym))) ;; when DEF is nil, this is a valid binding (define-key map key def))) ;; Example: (defkeymap my-keymap (let ((map (make-sparse-keymap))) (define-key map (read-kbd-macro "") 'bbdb) map) "Keymap to demonstrate `defkeymap'.") ;; my-keymap ;; (keymapp my-keymap) ;; (apropos "my-keymap") ;; (custom-get-keymap 'my-keymap) ;; (put 'my-keymap 'custom-bindings nil) ;; (get 'my-keymap 'standard-value) ;; (customize-option 'my-keymap) ;; (unintern 'my-keymap) (provide 'cus-key) ;;; cus-key.el ends here