Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(define-key map [tab] 'indent-for-tab-command) is a bug #688

Open
phil-s opened this issue Nov 11, 2021 · 2 comments
Open

(define-key map [tab] 'indent-for-tab-command) is a bug #688

phil-s opened this issue Nov 11, 2021 · 2 comments

Comments

@phil-s
Copy link
Contributor

phil-s commented Nov 11, 2021

    ;; Use the Emacs standard indentation binding. This may upset c-mode
    ;; which does not follow this at the moment, but I see no better
    ;; choice.
    (define-key map [tab] 'indent-for-tab-command)

That last line should be:

    (define-key map (kbd "TAB") 'indent-for-tab-command)

I can see that the original code correctly used TAB (\t) instead of <tab> ([tab]) and it was later changed (commit 3699aca); but I strongly suspect that change was made for purely aesthetic purposes, as it's a bug.

Terminals don't send <tab> events; they only deal with TAB. This is why Emacs (a) translates <tab> to TAB, and then (b) uses TAB consistently for binding keys (and certainly the referenced c-mode-map binds TAB and not <tab>). This convention ensures that GUI and terminal Emacs always do the same thing.

With the current code, C-h k TAB tells us:

  • <tab> runs the command indent-for-tab-command in GUI frames
  • TAB runs the command c-indent-line-or-region in terminal frames
@zonuexe
Copy link
Member

zonuexe commented Jan 9, 2022

@phil-s
Thanks for your suggestion.

I learned that [tab] doesn't work literally by eval the code below.

(local-key-binding [tab] (lambda (interactive) (message "TAB!")))

It looks like he was trying to run indent-for-tab-command directly, avoiding c-indent-line-or-region, but it didn't seem to work.

  (substitute-key-definition 'indent-for-tab-command
			     ;; XXX Is this the right thing to do
			     ;; here?
			     'c-indent-line-or-region
			     c-mode-base-map global-map)

I haven't checked the behavior in old Emacsen, but the code hasn't changed from Emacs23 to the present.

Delete these lines as they are just verbose definitions.

@phil-s
Copy link
Contributor Author

phil-s commented Jan 9, 2022

I'm confused by the reply, but the correct fix for php-mode.el is what I've shown in my initial post, and if you want to sort it in your own config you could do this:

;; Bug fix for https://github.com/emacs-php/php-mode/issues/688
(with-eval-after-load "php-mode"
  (when-let ((cmd (lookup-key php-mode-map [tab])))
    (define-key php-mode-map [tab] nil)
    (define-key php-mode-map (kbd "TAB") cmd)))

Note that local-key-binding isn't for binding keys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants