1

When loading auto-complete via auto-complete-config with use-package, then :config is not executed and local keymap is not set. (See example below)

Why that and how to solve this?

Example: save following into a file foo.el and run with emacs -Q -l foo.el.

(message "DEBUG: my-shortconfig start")

;; init package and use-package
(setq package-archives '(("gnu" . "https://elpa.gnu.org/packages/")
                         ("melpa" . "https://melpa.org/packages/")))
(require 'package)
(package-initialize)
(require 'use-package)

;; auto-complete-config:
(use-package auto-complete-config
    :commands (global-auto-complete-mode auto-complete auto-complete-mode)
    :bind (("M-<tab>" . my--auto-complete)
           (:map ac-mode-map
                 ("M-<tab>" . auto-complete)))
    :init (progn
              (message "DEBUG: auto-complete-config init")
              (defun my--auto-complete ()
                  (interactive)
                  (message "DEBUG: my--auto-complete inside")
                  (unless (boundp 'auto-complete-mode)
                      (global-set-key (kbd "M-<tab>") nil)
                      (global-auto-complete-mode 1))
                  (auto-complete))
              (setq ac-auto-start nil
                    ac-quick-help-delay 0.5))
    :config (progn
                (message "DEBUG: auto-complete-config config")))

(message "DEBUG: my-shortconfig done")

The expected Messages, after pressing M-tab in *scratch* buffer, are:

DEBUG: my-shortconfig start
DEBUG: auto-complete-config init
DEBUG: my-shortconfig done
DEBUG: my--auto-complete inside
DEBUG: auto-complete-config config

But I only get:

DEBUG: my-shortconfig start
DEBUG: auto-complete-config init
DEBUG: my-shortconfig done
DEBUG: my--auto-complete inside

Also M-tab is not bound in ac-mode-map

Note: I found some questions to similar problems, but they do not help. auto-complete-config provides auto-complete-config and :config stuff should be executed after (require 'auto-complete-config)

2 Answers 2

2

For starters, you can expand the use-package macro to see what it's doing. Put point at the end and try pp-macroexpand-last-sexp to see what use-package will do.

I suspect nothing here ever actually loads auto-complete-config. The commands you are specifying (and your own custom command) are from auto-complete and not from auto-complete-config. Basically you are telling use package to only load auto-complete-config when it is used, and nothing uses it.

You could put (require 'auto-complete-config) at the start of your my--auto-complete command. Then the first time you call it you'll trigger the :config block as expected.

3
  • thanks, I put (require 'auto-complete-config) into the function and use use-package auto-complete, then it works.
    – jue
    Jun 16, 2017 at 21:11
  • 1
    function pp-macroexpand-last-sexp (instead of pp-macroexpand-expression) needs less typing. :)
    – jue
    Jun 18, 2017 at 0:24
  • @jue Whoops, that's what I meant! Fixed, thanks.
    – glucas
    Jun 18, 2017 at 9:29
1

The following code is the working result of this question.
Now the auto-complete package loading is delayed until the user presses M-tab for the first time (of course you could select a different hot key). After the first M-tab-key press the global M-tab-hotkey setting is freed and normal auto-complete operation takes place.

(message "DEBUG: my-shortconfig start")

;; init package and use-package
(setq package-archives '(("gnu" . "https://elpa.gnu.org/packages/")
                         ("melpa" . "https://melpa.org/packages/")))
(require 'package)
(package-initialize)
(require 'use-package)

;; auto-complete-config:
(use-package auto-complete
    :commands (global-auto-complete-mode auto-complete auto-complete-mode)
    :bind ("M-<tab>" . my--auto-complete)
    :bind (:map ac-mode-map
                ("M-<tab>" . auto-complete))
    :init (progn
              (message "DEBUG: auto-complete-config init")
              (defun my--auto-complete ()
                  (interactive)
                  (message "DEBUG: my--auto-complete inside")
                  (unless (boundp 'auto-complete-mode)
                      (require 'auto-complete-config)
                      (global-set-key (kbd "M-<tab>") nil)
                      (global-auto-complete-mode 1))
                  (auto-complete))
              (setq ac-auto-start nil
                    ac-quick-help-delay 0.5))
    :config (progn
                (message "DEBUG: auto-complete-config config")))

(message "DEBUG: my-shortconfig done")

Note the extra :bind section, the changed package name and the (require auto-complete-config) line inside the function my--auto-complete.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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