jwiegley / use-package

A use-package declaration for simplifying your .emacs
https://jwiegley.github.io/use-package
GNU General Public License v3.0
4.42k stars 260 forks source link

[Question] :config part expressions do not evaluate on startup #904

Closed mananamenos closed 3 years ago

mananamenos commented 3 years ago

hi, I have this in my init.el

  (use-package emacs
    :config
    (ido-mode 1)
    (setq ido-enable-flex-matching t)
    (setq ido-everywhere t)
    (setq ido-auto-merge-work-directories-length -1)
    :bind* ("C-," . ido-switch-buffer)
    )

However, on the emacs startup the line (setq ido-auto-merge-work-directories-length -1) does not do its job. I have to manually evaluate that expression once emacs has started up. How can I achieve this to work on the startup?

thomasf commented 3 years ago

What is the emacs package? I don't think thats anything that comes with emacs.

You are probably looking for something like this.

(use-package ido
  :init ;; is run BEFORE package is loaded, when emacs starts
  (setq ido-enable-flex-matching t)
  (setq ido-everywhere t)
  (setq ido-auto-merge-work-directories-length -1)
  (ido-mode 1) ;; causes ido to load 
  :config ;; is run AFTER package is loaded
  (message "doing something that required ido to be loaded first" )
  :bind* ("C-," . ido-switch-buffer) ;; using th binding also causes ido to be loaded
  ) 

Expanding the use-package macro gives some insight in what the code above does where among other things the :config: block is wrapped inside eval-after-load


(progn
  (defvar use-package--warning3
    (function
     (lambda
       (keyword err)
       (let
           ((msg
             (format "%s/%s: %s" 'ido keyword
                     (error-message-string err))))
         (display-warning 'use-package msg :error)))))
  (condition-case-unless-debug err
      (progn
        (unless
            (fboundp 'ido-switch-buffer)
          (autoload
            (function ido-switch-buffer)
            "ido" nil t))
        (condition-case-unless-debug err
            (progn
              (setq ido-enable-flex-matching t)
              (setq ido-everywhere t)
              (setq ido-auto-merge-work-directories-length -1)
              (ido-mode 1))
          (error
           (funcall use-package--warning3 :init err)))
        (eval-after-load 'ido
          '(condition-case-unless-debug err
               (progn
                 (message "doing something that required ido to be loaded first")
                 t)
             (error
              (funcall use-package--warning3 :config err))))
        (bind-keys* :package ido
                    ("C-," . ido-switch-buffer)))
    (error
     (funcall use-package--warning3 :catch err))))

This means that in your example, the code in your :config will run after the package (feature) emacs is loaded which probably doesn't exist so it will never be executed:


...
          (eval-after-load 'emacs
            '(condition-case-unless-debug err
                 (progn
                   (ido-mode 1)
                   (setq ido-enable-flex-matching t)
                   (setq ido-everywhere t)
                   (setq ido-auto-merge-work-directories-length -1)
                   t)
               (error
                (funcall use-package--warning376 :config err))))
....
mananamenos commented 3 years ago

@thomasf thank you very much for a great explanation.