2

The following code:

  1. defines a major mode my-mode where C-d binding is redefined;
  2. opens a my-mode buffer inserting an editable-field widget.


(require 'widget)
(require 'wid-edit)

;; Define my-mode mode
(defvar my-mode-map
  (let ((map widget-keymap))
    (define-key map (kbd "C-d") (lambda () (interactive) (message "Ctrl D")))
    map))
(define-derived-mode my-mode nil "My")


;; Open my-mode buffer and insert editable field
(let ((buf (get-buffer-create "*buf*")))
  (with-current-buffer buf
    (my-mode)
    (widget-create 'editable-field
           :format "Field 1: %v ")
    (widget-setup))
  (switch-to-buffer buf))

My problem is that, C-d binding works only outside the editable field.
How can I make it work inside the field too?

1

You need to define C-d in the keymap passed through the :keymap property of the widget. As a test you can use:

(widget-create 'editable-field
           :format "Field 1: %v "
           :keymap my-mode-map)

Note that the default value for :keymap is not the value of widget-keymap but widget-field-keymap (if that is what you were after). Nevertheless, changing the value of widget-field-keymap does not help since the value of widget-field-keymap is already used in widget-define at load-time of the library widget.

Maybe you should use a modified copy of widget-field-keymap for the widget to have the standard bindings defined in the editable field. Something like that:

(progn
  (switch-to-buffer (get-buffer-create "*buf*"))
  (let ((my-widget-field-keymap (copy-keymap widget-field-keymap)))
    (define-key my-widget-field-keymap (kbd "C-d") (lambda () (interactive) (message "Ctrl D")))
    (widget-create 'editable-field
           :keymap my-widget-field-keymap
           :format "Field 1: %v ")
    (widget-setup)))

See also the widget documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.