0
(eval-after-load "dired" 
  '(progn
     (define-key dired-mode-map "c" 'dired-create-empty-file)
     (define-key dired-mode-map "r" 'dired-do-compress-to)))

Why is the eval-after-load necessary when this is an init file? From my understanding, it makes it only run when dired is loaded, but isn't the init file also loaded once? I was getting "symbol's value as variable is void" warning for dired-mode-map but it was working fine without.

Is this because the warning is emitted before dired is loaded, but the symbol is resolved by the time emacs is started up?

4
  • I believe you may have edited it to be that way, from the history. Commented Jul 8, 2023 at 18:33
  • Oh sorry, I copied your code to my Emacs to format it. Perhaps I deleted a word by mistake and rewrote it. Sorry again.
    – shynur
    Commented Jul 8, 2023 at 18:35
  • Ah. I actually had that formatted from emacs. Strange. I have indent-tabs-mode off with the tab-width 4. Commented Jul 8, 2023 at 18:39
  • Note that you should prefer the macro with-eval-after-load over eval-after-load. If you look at its code, you'll see that with-eval-after-load avoids explicit quoting by using lambda. Maybe, that does not play such a great role for the init file which is usually not byte-compiled but for byte-compiled library files, explicitly quoted sexps are not byte-compiled, but lambda-expressions (and sharp-quoted expressions) are. There is more (also about lexical scoping) at discourse.doomemacs.org/t/…
    – Tobias
    Commented Jul 9, 2023 at 1:24

1 Answer 1

1

isn't the init file also loaded once?

Yes, it is loaded once. Loaded when you launch Emacs.

See <https://www.gnu.org/software/emacs/manual/html_node/elisp/Startup-Summary.html>.

Why is the eval-after-load necessary when this is an init file?

To make sure that before executing the following code:

(progn
  (define-key dired-mode-map "c" 'dired-create-empty-file)
  (define-key dired-mode-map "r" 'dired-do-compress-to))

variable dired-mode-map has already been defined / set with a value, IOW, to avoid

"symbol's value as variable is void"

After "dired" is loaded, dired-mode-map will get its own value. Since you don't know when dired will be loaded, use eval-after-load is a good choice.

8
  • I see, but since this is in init and dired will always be loaded by the time I'm typing, it's not incorrect, but it gets a warning since the variable is not defined yet Commented Jul 8, 2023 at 19:09
  • 1
    @user129393192: What do you mean by "dired will always be loaded by the time I'm typing"?
    – shynur
    Commented Jul 8, 2023 at 19:24
  • I mean that wrapping it like this only avoids the warning, but functionality would not be affected ,since the symbol is resolved, by the time I am typing. Commented Jul 8, 2023 at 20:33
  • 1
    @user129393192: What do you mean by "the symbol is resolved"? Variable dired-mode-map is defined in package dired and it is initialized only when Emacs loads that package.
    – shynur
    Commented Jul 8, 2023 at 20:37
  • Yes exactly. I was just saying that by the time I am typing that that package will be loaded, so that's why there was no issues with me using it without eval-after-load in my init.file, except for the warning. Commented Jul 8, 2023 at 20:39

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.