Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6956055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:49:06+00:00 2026-05-27T14:49:06+00:00

I want to make a minor-mode (foo-mode) which has its keymap (foo-mode-map), but when

  • 0

I want to make a minor-mode (foo-mode) which has its keymap (foo-mode-map), but when the user presses any key not in (foo-mode-map), the minor-mode should quit. How do I bind the turn-off-foo-mode all other keys?

EDIT: here is the solution I came up with based on the chosen answer. It accepts numeric input as well.

(defalias 'foo-electric-delete 'backward-kill-word)

(defun foo-mode-quit (&optional arg)
  (interactive)
  (let ((global-binding (lookup-key (current-global-map)
                                    (single-key-description last-input-event))))
    (unless (eq last-input-event ?\C-g)
      (push last-input-event unread-command-events))
    (unless (memq global-binding
                  '(negative-argument digit-argument))
      (foo-mode -1))))

(defvar foo-mode-map
  (let ((map (make-keymap)))
    (set-char-table-range (nth 1 map) t 'foo-mode-quit)
    (define-key map "-" 'negative-argument)
    (dolist (k (number-sequence ?0 ?9))
      (define-key map (char-to-string k) 'digit-argument))
    (define-key map [backspace] 'foo-electric-delete)
    map))

(define-minor-mode foo-mode
  "Toggle Foo mode.
     With no argument, this command toggles the mode.
     Non-null prefix argument turns on the mode.
     Null prefix argument turns off the mode.

     When Foo mode is enabled, the control delete key
     gobbles all preceding whitespace except the last.
     See the command \\[foo-electric-delete]."
  ;; The initial value.
  :init-value nil
  ;; The indicator for the mode line.
  :lighter " Foo"
  ;; The minor mode bindings.
  :keymap foo-mode-map
  :group 'foo)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:49:07+00:00Added an answer on May 27, 2026 at 2:49 pm

    I’ve included a full working example for creating a minor mode with the kind of behavior you want; the key is to use set-char-table-range on a keymap created by make-keymap which creates a dense keymap with a full char-table; using this on a sparse keymap created with make-sparse-keymap will not work.

    (defalias 'foo-electric-delete 'backward-kill-word)
    
    (defun foo-mode-quit (&optional arg)
      (interactive)
      (foo-mode -1))
    
    (defvar foo-mode-map
      (let (map (make-keymap))
        (set-char-table-range (nth 1 map) t 'foo-mode-quit)
        (define-key map [backspace] 'foo-electric-delete)
        map))
    
    (define-minor-mode foo-mode
      "Toggle Foo mode.
         With no argument, this command toggles the mode.
         Non-null prefix argument turns on the mode.
         Null prefix argument turns off the mode.
    
         When Foo mode is enabled, the control delete key
         gobbles all preceding whitespace except the last.
         See the command \\[foo-electric-delete]."
      ;; The initial value.
      :init-value nil
      ;; The indicator for the mode line.
      :lighter " Foo"
      ;; The minor mode bindings.
      :keymap foo-mode-map
      :group 'foo)
    
    (defvar major-baz-mode-map '(keymap (t . major-baz-mode-default-function)))
    

    Setting a default binding for a major mode map is easier and I include this example here, but as I noted above, this kind of spare keymap will not work for a minor mode:

    (defvar major-baz-mode-map '(keymap (t . major-baz-mode-default-function)))
    

    This is discussed in the documentation in Format of Keymaps where it says:

    (t . binding)
    
    This specifies a default key binding; any event not bound by other
    elements of the keymap is given binding as its binding. Default
    bindings allow a keymap to bind all possible event types without
    having to enumerate all of them. A keymap that has a default binding
    completely masks any lower-precedence keymap, except for events
    explicitly bound to nil (see below).
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make an entity that has an autogenerated primary key, but also
I want make datetimepicker in my project. Using jquery how it is possible? I
Let's say I want make some of my sources publicly available via my blog
I have style sheet with a class name changebackgroundcolor i want make change in
I am traversing a HTML document using javascript DOM. I want make a list
I want to make an etag that matches what Apache produces. How does apache
I want to make a table in SqlServer that will add, on insert, a
I want to make a copy of an ActiveRecord object, changing a single field
I want to make a .NET Form as a TopMost Form for another external
I want to make sure people can't type the name of a PHP script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.