0

I am using company to auto-complete. It works generally fine, with the exception of the M-(digit) shortcuts, which have no impact at all. I have no idea why.

This is my init code:

(defun company--my-insert-spc() (interactive)(company-abort)(insert-char #10r32))
(defun company--my-insert-dot() (interactive)(company-abort)(insert-char #10r46))
(use-package company
  :config
  (global-company-mode)
  (setq company-minimum-prefix-length 3)
  (setq company-auto-complete t)
  (setq company-show-numbers t)
  :bind
  (("C-<tab>" . company-complete)
   :map company-active-map
   ("ESC" . company-abort)
   ;; prevent company from completing on its own when we type regular characters
   ("SPC" . company--my-insert-spc)
   ("."   . company--my-insert-dot)
   )
  )

This is the result of M-x describe-keymap company-active-map:

company-active-map
------------------

For more information check the manuals.

Keymap that is enabled during an active completion.

key             binding
---             -------

C-d             company-show-doc-buffer
C-g             company-abort
C-h             company-show-doc-buffer
TAB             company-complete-common
RET             company-complete-selection
C-s             company-search-candidates
C-w             company-show-location
ESC             company-abort
SPC             company--my-insert-spc
.               company--my-insert-dot
<down>          company-select-next-or-abort
<down-mouse-1>  ignore
<down-mouse-3>  ignore
<f1>            company-show-doc-buffer
<mouse-1>       company-complete-mouse
<mouse-3>       company-select-mouse
<remap>         Prefix Command
<return>        company-complete-selection
<tab>           company-complete-common
<up>            company-select-previous-or-abort
<up-mouse-1>    ignore
<up-mouse-3>    ignore

<remap> <scroll-down-command>   company-previous-page
<remap> <scroll-up-command>     company-next-page

Indeed, the M-(digit) shortcuts are not there. Neither M-n or M-p. I am relatively new to emacs, and I don't know how to pinpoint the cause of this.

I did try to workaround this problem by explicitly setting again the shortcuts in the use-package :config section:

(dotimes (i 10)
  (define-key company-active-map (read-kbd-macro (format "M-%d" i)) 'company-complete-number))

... but I get a really baffling warning: while: Key sequence M-0 starts with non-prefix key ESC. Why does ESC appear here? I also get this same warning for the M-n and M-p shortcuts:

  (define-key company-active-map (kbd "M-n") 'company-select-next)
  (define-key company-active-map (kbd "M-p") 'company-select-previous)

This warning does appear to give a clue, but I can't figure out to what exactly.

Update

Based on the warning above, I commented out the ESC keybinding to company-abort, and it made M-(digit) work as expected. So now the question is: why does this incompatibility between ESC and M-(digit) happen and how can I use both shortcuts?

Update 2

The helpful comments by @TianxiangXiong and @phils identified the problem: ESC is the prefix char for M, so emacs will be confounded by the keybindings I've given it. This is a pity since I really wanted those shortcuts. However, based on this comment, I was able workaround this and get what I wanted by simply changing ("ESC" . company-abort) to ("<escape>" . company-abort) in the :config. At least in my system (Ubuntu 16.04) this worked.

jpmag
  • 1
  • 2
  • The `ESC` key is an alternative way to type meta modifier sequences. See `C-h i g (emacs) User Input`. See also the "Interaction with normal keymaps" section of `(elisp) Translation Keymaps`. – phils Mar 28 '17 at 22:51
  • `M` is given the `ESC` keycode by default. See [`meta-prefix-char`](https://www.gnu.org/software/emacs/manual/html_node/elisp/Functions-for-Key-Lookup.html). In general, [don't bind `ESC`](http://emacs.stackexchange.com/questions/31007/binding-m-esc-m-esc#comment47687_31035)--surprising things will happen. – Tianxiang Xiong Mar 28 '17 at 23:47
  • @TianxiangXiong The `meta-prefix-char` link helped, thanks. If you submit your comment as an answer I will gladly accept it. – jpmag Mar 29 '17 at 12:40
  • Note that using `` instead of `ESC` doesn't work in terminal Emacs, [only GUI Emacs](http://emacs.stackexchange.com/a/31036/10269). – Tianxiang Xiong Mar 29 '17 at 18:35

1 Answers1

2

M is given the ESC keycode by default. See meta-prefix-char.

In general, don't bind ESC--surprising things will happen.

Tianxiang Xiong
  • 3,848
  • 16
  • 27