emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Customizing key bindings


From: Per Abrahamsen
Subject: Re: Customizing key bindings
Date: Tue, 10 Sep 2002 12:19:05 +0200
User-agent: Gnus/5.090007 (Oort Gnus v0.07) Emacs/21.1 (sparc-sun-solaris2.8)

Alex Schroeder <address@hidden> writes:

>       * cus-key.el (key-sequence-field-validate): disallow "" as a
>       keybinding.

We may also want to disallow the creation of prefix keys, alt least
for now.  For example, Try to define "X Y" from customize.  Then
remove the binding from customize.  You will notice that "X" continues
to be a prefix key, even though "X Y" is no longer bound.

We should probably find a safe way to allow the user to create new
prefix keys, but for now, let's disallow it.

Actually, thinking about it, we _need_ to solve this, as otherwise the
user will not be able to bind e.g. "C-c x" for a mode that have no C-c
bindings already.

> Except for the handling of nil, this seems "good enough" for me.  :)

The patch below implements "Remove definition", add a check for too
long key bidnings in validate, and fix a bug with the notify function
that (did not) update the "Old binding".

*** cus-key2.el Tue Sep 10 11:58:40 2002
--- cus-key3.el Tue Sep 10 12:15:48 2002
***************
*** 26,39 ****
    :keymap key-sequence-widget-map)
  
  (defun key-sequence-field-validate (widget)
!   (let ((value (widget-apply widget :value-get)))
!     (condition-case nil
!       (progn 
!         (when (string= value "")
!           (error widget))
!         (read-kbd-macro value)
!         nil)
!       (error widget))))
  
  (define-widget 'key-sequence-button 'push-button
    "Button for entering key bindings."
--- 26,64 ----
    :keymap key-sequence-widget-map)
  
  (defun key-sequence-field-validate (widget)
!   (let* ((value (widget-apply widget :value-get))
!        (map1 (or (widget-ancestor-get widget :key-sequence-keymap)
!                 (current-global-map)))
!        (map (if (symbolp map1) (symbol-value map1) map1))
!        (key (condition-case nil
!                 (read-kbd-macro value)
!               nil))
!        (command (and key (lookup-key map key)))))
!   (cond ((null key)
!        ;; Unparsable.
!        widget)
!       ((string-equal key "")
!        ;; Empty prefix.  We can't rebind the entire keymap.
!        widget)
!       ((functionp command)
!        ;; Normal binding, OK.
!        nil)
!       ((null command)
!        ;; Unbound, OK.
!        nil)
!       ((numberp command)
!        ;; We can't allow the creation of new prefix keys, as we have
!        ;; no way to undo such a prefix
!        widget)
!       ((keymapp command)
!        ;; This will turn a prefix key into an ordinary binding.  A
!        ;; bit drastic (as many bindings can be lost), but the user
!        ;; asked for it.
!        nil)
!       (t
!        ;; This is an impossible value according to the lookup-key doc
!        ;; string.  What to do?  Let the user deside.
!        nil)))
  
  (define-widget 'key-sequence-button 'push-button
    "Button for entering key bindings."
***************
*** 115,124 ****
         (children (widget-get widget :buttons))
         (field (car children))
         (value (widget-value child))
!        (map (or (widget-ancestor-get widget :key-sequence-keymap)
                  (current-global-map)))
         (command (condition-case nil
!                     (lookup-key map (read-kbd-macro value))
                    (error nil))))
      (save-excursion
        (goto-char (widget-get binding :from))
--- 140,150 ----
         (children (widget-get widget :buttons))
         (field (car children))
         (value (widget-value child))
!        (map1 (or (widget-ancestor-get widget :key-sequence-keymap)
                  (current-global-map)))
+        (map (if (symbolp map1) (symbol-value map1) map1))
         (command (condition-case nil
!                     (lookup-key map(read-kbd-macro value))
                    (error nil))))
      (save-excursion
        (goto-char (widget-get binding :from))
***************
*** 134,152 ****
    :prompt-match 'commandp
    :match-alternatives '(commandp)
    :validate (lambda (widget)
!             (unless (or (not (widget-value widget))
!                         (commandp (widget-value widget)))
                (widget-put widget :error (format "Invalid function: %S"
                                                  (widget-value widget)))
                widget))
    :value 'ignore
    :tag "Command")
  
  (define-widget 'key-binding 'group
    "Bind a key sequence to a command."
    :value '("" ignore)
    :indent 0
!   :args '(key-sequence (command :tag "New binding")))
  
  (defmacro defkeymap (symbol map doc &rest args)
    "Define SYMBOL to be a keymap with value MAP.
--- 160,184 ----
    :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")
  
+ (define-widget 'key-binding-value 'choice
+   "A valid key binding value for use by `define-key'.
+ This could be a command or nil."
+   :args '((const :tag "Remove definition" nil) )
+   :value 'ignore
+   :tag "New binding")
+ 
  (define-widget 'key-binding 'group
    "Bind a key sequence to a command."
    :value '("" ignore)
    :indent 0
!   :args '(key-sequence key-binding-value))
  
  (defmacro defkeymap (symbol map doc &rest args)
    "Define SYMBOL to be a keymap with value MAP.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]