0

I'm trying to bind the TAB and shift TAB keys to a custom readonly buffer mode. The skeleton of the mode looks like this:

;; 8< --- exmode.el -------

(defun exmode-i ()
  (interactive)
  (message "Pressed i"))

(defun exmode-tab ()
  (interactive)
  (message "Pressed tab"))

(defun exmode-s-tab ()
  (interactive)
  (message "Pressed shift-tab"))

(defvar ex-mode-map
  (let ((map (make-keymap))) ;;
    (suppress-keymap map t)
    (define-key map  "i"       'exmode-i)
    (define-key map  [tab]     'exmode-tab)
    (define-key map  [S-tab]   'exmode-s-tab)
    map)
  "keymap `ex-mode'.")

(define-derived-mode ex-mode special-mode "ex-mode"
  "Example mode"
  :group 'ex-mode
  (buffer-disable-undo)
  (setq truncate-lines t)
  (setq buffer-read-only t)
  )

(defun setup-exmode-buffer ()
  (interactive)
  (let* ((buffer (generate-new-buffer "*exmode*")))
    (with-current-buffer buffer
      (funcall 'ex-mode)
      (display-buffer buffer))
    ))

(provide 'exmode)
;; 8< --- exmode.el -------

When I (require 'exmode) and M-x setup-exmode-buffer I get a buffer where the i is correcly bound, however the tab and s-tab bindings are strange:

Doing a C-h k + key shows:

When running in term mode (-nw) running under KDE Konsole terminal emulator:

  • press tab : bound to <tab>, runs (indent-for-tab-command &optional ARG)
  • press s-tab : bound to <backtab>, <backtab> not defined.

When running in GUI mode :

  • press tab : bound to <tab>, runs 'exmode-tab, which is the behaviour I want
  • press s-tab : bound to <backtab>, <backtab> not defined.

How can I make exmode-tab and exmode-s-tab to be called when pressing tab and s-tab in both term and GUI mode? I'm running Emacs 27.0.50.

5
  • 1
    By looking at what C-h k returns, and trial and error, I found (kbd "TAB") and (kbd "<backtab>") as the key designation seem to work. I'll be happy to see someone explaining how this should be handled...
    – JeanPierre
    Sep 7 '19 at 13:03
  • 3
    Use (kbd "TAB") (or use (kbd "<tab>"), if you want to distinguish C-i from the <tab> key) and (kbd "<backtab>"), in place of [tab] and [S-tab].
    – Drew
    Sep 7 '19 at 14:22
  • I peeked into Magit's code an there (kbd "C-i") (for tab) and [backtab] (for s-tab) is used. I wonder what the difference and mechanisms behind [tab] (kbd "TAB") (kbd "<tab>) (kbd "C-i") are? And s-tab: [s-tab] (kbd "<backtab>") [backtab]. Sep 7 '19 at 14:30
  • @Drew @JeanPierre: There combinations work: (kbd "<tab>") and (kbd "<backtab>") as well as (kbd "C-i") and [backtab] . Thanks. Sep 7 '19 at 14:45
  • 1
    s- is the Super modifier, not the Shift modifier (that's S-, not s-). You will never go wrong if you use kbd.
    – Drew
    Sep 7 '19 at 16:15

Your Answer

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

Browse other questions tagged or ask your own question.